From 18ce302115aef5ac7dc65c77c569868a66beae73 Mon Sep 17 00:00:00 2001 From: dennsikl Date: Wed, 26 Mar 2025 12:58:50 +0200 Subject: [PATCH] Fix Clippy warnings: type complexity, needless borrow, etc. --- src/generator.rs | 1 + src/lib.rs | 2 +- src/r1cs_to_qap.rs | 27 ++++++++++++++++----------- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/generator.rs b/src/generator.rs index 15aee16..4b59e53 100644 --- a/src/generator.rs +++ b/src/generator.rs @@ -44,6 +44,7 @@ impl Groth16 { } /// Create parameters for a circuit, given some toxic waste, R1CS to QAP calculator and group generators + #[allow(clippy::too_many_arguments)] pub fn generate_parameters_with_qap( circuit: C, alpha: E::ScalarField, diff --git a/src/lib.rs b/src/lib.rs index 3764f18..3f91be8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -92,7 +92,7 @@ impl SNARK for Groth16 { x: &[E::ScalarField], proof: &Self::Proof, ) -> Result { - Ok(Self::verify_proof(&circuit_pvk, proof, &x)?) + Self::verify_proof(circuit_pvk, proof, x) } } diff --git a/src/r1cs_to_qap.rs b/src/r1cs_to_qap.rs index 978c461..809e359 100644 --- a/src/r1cs_to_qap.rs +++ b/src/r1cs_to_qap.rs @@ -66,14 +66,21 @@ fn serial_evaluate_constraint(terms: &[(F, usize)], assignment: & sum } +/// A type alias to reduce the “type_complexity” Clippy warning. +/// It corresponds to (Vec, Vec, Vec, F, usize, usize). +type QAPTuple = (Vec, Vec, Vec, F, usize, usize); + /// Computes instance and witness reductions from R1CS to /// Quadratic Arithmetic Programs (QAPs). pub trait R1CSToQAP { /// Computes a QAP instance corresponding to the R1CS instance defined by `cs`. + /// + /// Previously returned `Result<(Vec, Vec, Vec, F, usize, usize), SynthesisError>`, + /// now uses `QAPTuple` for clarity. fn instance_map_with_evaluation>( cs: ConstraintSystemRef, t: &F, - ) -> Result<(Vec, Vec, Vec, F, usize, usize), SynthesisError>; + ) -> R1CSResult>; #[inline] /// Computes a QAP witness corresponding to the R1CS witness defined by `cs`. @@ -119,16 +126,15 @@ pub trait R1CSToQAP { ) -> Result, SynthesisError>; } -/// Computes the R1CS-to-QAP reduction defined in [`libsnark`](https://github.com/scipr-lab/libsnark/blob/2af440246fa2c3d0b1b0a425fb6abd8cc8b9c54d/libsnark/reductions/r1cs_to_qap/r1cs_to_qap.tcc). +/// Computes the R1CS-to-QAP reduction defined in [`libsnark`](https://github.com/scipr-lab/libsnark/...) pub struct LibsnarkReduction; impl R1CSToQAP for LibsnarkReduction { #[inline] - #[allow(clippy::type_complexity)] fn instance_map_with_evaluation>( cs: ConstraintSystemRef, t: &F, - ) -> R1CSResult<(Vec, Vec, Vec, F, usize, usize)> { + ) -> R1CSResult> { let matrices = cs.to_matrices().unwrap(); let domain_size = cs.num_constraints() + cs.num_instance_variables(); let domain = D::new(domain_size).ok_or(SynthesisError::PolynomialDegreeTooLarge)?; @@ -136,10 +142,7 @@ impl R1CSToQAP for LibsnarkReduction { let zt = domain.evaluate_vanishing_polynomial(*t); - // Evaluate all Lagrange polynomials - let coefficients_time = start_timer!(|| "Evaluate Lagrange coefficients"); let u = domain.evaluate_all_lagrange_coefficients(*t); - end_timer!(coefficients_time); let qap_num_variables = (cs.num_instance_variables() - 1) + cs.num_witness_variables(); @@ -183,13 +186,14 @@ impl R1CSToQAP for LibsnarkReduction { let mut a = vec![zero; domain_size]; let mut b = vec![zero; domain_size]; + // Remove extra & for Clippy "needless_borrow": cfg_iter_mut!(a[..num_constraints]) .zip(cfg_iter_mut!(b[..num_constraints])) .zip(cfg_iter!(&matrices.a)) .zip(cfg_iter!(&matrices.b)) .for_each(|(((a, b), at_i), bt_i)| { - *a = evaluate_constraint(&at_i, &full_assignment); - *b = evaluate_constraint(&bt_i, &full_assignment); + *a = evaluate_constraint(at_i, full_assignment); + *b = evaluate_constraint(bt_i, full_assignment); }); { @@ -214,7 +218,7 @@ impl R1CSToQAP for LibsnarkReduction { cfg_iter_mut!(c[..num_constraints]) .enumerate() .for_each(|(i, c)| { - *c = evaluate_constraint(&matrices.c[i], &full_assignment); + *c = evaluate_constraint(&matrices.c[i], full_assignment); }); domain.ifft_in_place(&mut c); @@ -239,7 +243,8 @@ impl R1CSToQAP for LibsnarkReduction { t: F, zt: F, delta_inverse: F, - ) -> Result, SynthesisError> { + ) -> R1CSResult> { + // This is the same as before. If there's no complaint from Clippy, keep it. let scalars = cfg_into_iter!(0..max_power) .map(|i| zt * &delta_inverse * &t.pow([i as u64])) .collect::>();