Skip to content

Fix Clippy warnings: type complexity, needless borrow, etc. #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl<E: Pairing, QAP: R1CSToQAP> Groth16<E, QAP> {
}

/// 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<C>(
circuit: C,
alpha: E::ScalarField,
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl<E: Pairing, QAP: R1CSToQAP> SNARK<E::ScalarField> for Groth16<E, QAP> {
x: &[E::ScalarField],
proof: &Self::Proof,
) -> Result<bool, Self::Error> {
Ok(Self::verify_proof(&circuit_pvk, proof, &x)?)
Self::verify_proof(circuit_pvk, proof, x)
}
}

Expand Down
27 changes: 16 additions & 11 deletions src/r1cs_to_qap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,21 @@ fn serial_evaluate_constraint<F: PrimeField>(terms: &[(F, usize)], assignment: &
sum
}

/// A type alias to reduce the “type_complexity” Clippy warning.
/// It corresponds to (Vec<F>, Vec<F>, Vec<F>, F, usize, usize).
type QAPTuple<F> = (Vec<F>, Vec<F>, Vec<F>, 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<F>, Vec<F>, Vec<F>, F, usize, usize), SynthesisError>`,
/// now uses `QAPTuple<F>` for clarity.
fn instance_map_with_evaluation<F: PrimeField, D: EvaluationDomain<F>>(
cs: ConstraintSystemRef<F>,
t: &F,
) -> Result<(Vec<F>, Vec<F>, Vec<F>, F, usize, usize), SynthesisError>;
) -> R1CSResult<QAPTuple<F>>;

#[inline]
/// Computes a QAP witness corresponding to the R1CS witness defined by `cs`.
Expand Down Expand Up @@ -119,27 +126,23 @@ pub trait R1CSToQAP {
) -> Result<Vec<F>, 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<F: PrimeField, D: EvaluationDomain<F>>(
cs: ConstraintSystemRef<F>,
t: &F,
) -> R1CSResult<(Vec<F>, Vec<F>, Vec<F>, F, usize, usize)> {
) -> R1CSResult<QAPTuple<F>> {
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)?;
let domain_size = domain.size();

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();

Expand Down Expand Up @@ -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);
});

{
Expand All @@ -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);
Expand All @@ -239,7 +243,8 @@ impl R1CSToQAP for LibsnarkReduction {
t: F,
zt: F,
delta_inverse: F,
) -> Result<Vec<F>, SynthesisError> {
) -> R1CSResult<Vec<F>> {
// 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::<Vec<_>>();
Expand Down