Skip to content

Commit

Permalink
Clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
howardwu committed Jun 26, 2021
1 parent 47c0cb2 commit 452bff7
Show file tree
Hide file tree
Showing 69 changed files with 716 additions and 729 deletions.
2 changes: 1 addition & 1 deletion algorithms/benches/snark/gm17.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<F: Field> ConstraintSynthesizer<F> for Benchmark<F> {
let new_entry = {
let (input_1_val, input_1_var) = variables[i];
let (input_2_val, input_2_var) = variables[i + 1];
let result_val = input_1_val.and_then(|input_1| input_2_val.map(|input_2| input_1 * &input_2));
let result_val = input_1_val.and_then(|input_1| input_2_val.map(|input_2| input_1 * input_2));
let result_var = cs.alloc(
|| format!("result_{}", i),
|| result_val.ok_or(SynthesisError::AssignmentMissing),
Expand Down
4 changes: 2 additions & 2 deletions algorithms/examples/snark/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl<F: Field> ConstraintSynthesizer<F> for Benchmark<F> {

for i in 0..self.num_constraints - 1 {
if i % 2 != 0 {
let c_val = a_val * &b_val;
let c_val = a_val * b_val;
let c_var = cs.alloc(|| format!("{}", i), || Ok(c_val))?;

cs.enforce(
Expand All @@ -63,7 +63,7 @@ impl<F: Field> ConstraintSynthesizer<F> for Benchmark<F> {
b_val = c_val;
b_var = c_var;
} else {
let c_val = a_val + &b_val;
let c_val = a_val + b_val;
let c_var = cs.alloc(|| format!("{}", i), || Ok(c_val))?;

cs.enforce(
Expand Down
78 changes: 39 additions & 39 deletions algorithms/src/encoding/elligator2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ impl<P: MontgomeryModelParameters + TEModelParameters, G: Group + ProjectiveCurv

// Compute the parameters for the alternate Montgomery form: v^2 == u^3 + A * u^2 + B * u.
let (a, b) = {
let a = Self::A * &Self::B.inverse().unwrap();
let b = P::BaseField::one() * &Self::B.square().inverse().unwrap();
let a = Self::A * Self::B.inverse().unwrap();
let b = P::BaseField::one() * Self::B.square().inverse().unwrap();
(a, b)
};

Expand All @@ -65,7 +65,7 @@ impl<P: MontgomeryModelParameters + TEModelParameters, G: Group + ProjectiveCurv
let u = Self::D;

// Let ur2 = u * r^2;
let ur2 = r.square() * &u;
let ur2 = r.square() * u;

{
// Verify u is a quadratic nonresidue.
Expand All @@ -77,32 +77,32 @@ impl<P: MontgomeryModelParameters + TEModelParameters, G: Group + ProjectiveCurv

// Verify A^2 * ur^2 != B(1 + ur^2)^2.
let a2 = a.square();
assert_ne!(a2 * &ur2, (P::BaseField::one() + ur2).square() * &b);
assert_ne!(a2 * ur2, (P::BaseField::one() + ur2).square() * b);
}

// Let v = -A / (1 + ur^2).
let v = (P::BaseField::one() + ur2).inverse().unwrap() * &(-a);
let v = (P::BaseField::one() + ur2).inverse().unwrap() * (-a);

// Let e = legendre(v^3 + Av^2 + Bv).
let v2 = v.square();
let v3 = v2 * &v;
let av2 = a * &v2;
let bv = b * &v;
let v3 = v2 * v;
let av2 = a * v2;
let bv = b * v;
let e = (v3 + (av2 + bv)).legendre();

// Let x = ev - ((1 - e) * A/2).
let two = P::BaseField::one().double();
let x = match e {
LegendreSymbol::Zero => -(a * &two.inverse().unwrap()),
LegendreSymbol::Zero => -(a * two.inverse().unwrap()),
LegendreSymbol::QuadraticResidue => v,
LegendreSymbol::QuadraticNonResidue => (-v) - &a,
LegendreSymbol::QuadraticNonResidue => (-v) - a,
};

// Let y = -e * sqrt(x^3 + Ax^2 + Bx).
let x2 = x.square();
let x3 = x2 * &x;
let ax2 = a * &x2;
let bx = b * &x;
let x3 = x2 * x;
let ax2 = a * x2;
let bx = b * x;
let value = (x3 + (ax2 + bx)).sqrt().unwrap();
let y = match e {
LegendreSymbol::Zero => P::BaseField::zero(),
Expand All @@ -118,35 +118,35 @@ impl<P: MontgomeryModelParameters + TEModelParameters, G: Group + ProjectiveCurv
// Enforce v^2 == u^3 + A * u^2 + B * u
let v2 = v.square();
let u2 = u.square();
let u3 = u2 * &u;
assert_eq!(v2, u3 + (a * &u2) + (b * &u));
let u3 = u2 * u;
assert_eq!(v2, u3 + (a * u2) + (b * u));
}

// Convert the alternate Montgomery element (u, v) to Montgomery element (s, t).
let (s, t) = {
let s = u * &Self::B;
let t = v * &Self::B;
let s = u * Self::B;
let t = v * Self::B;

// Ensure (s, t) is a valid Montgomery element
#[cfg(debug_assertions)]
{
// Enforce B * t^2 == s^3 + A * s^2 + s
let t2 = t.square();
let s2 = s.square();
let s3 = s2 * &s;
assert_eq!(Self::B * &t2, s3 + (Self::A * &s2) + s);
let s3 = s2 * s;
assert_eq!(Self::B * t2, s3 + (Self::A * s2) + s);
}

(s, t)
};

// Convert the Montgomery element (s, t) to the twisted Edwards element (x, y).
let (x, y) = {
let x = s * &t.inverse().unwrap();
let x = s * t.inverse().unwrap();

let numerator = s - &P::BaseField::one();
let numerator = s - P::BaseField::one();
let denominator = s + P::BaseField::one();
let y = numerator * &denominator.inverse().unwrap();
let y = numerator * denominator.inverse().unwrap();

(x, y)
};
Expand All @@ -169,39 +169,39 @@ impl<P: MontgomeryModelParameters + TEModelParameters, G: Group + ProjectiveCurv

// Compute the parameters for the alternate Montgomery form: v^2 == u^3 + A * u^2 + B * u.
let (a, b) = {
let a = Self::A * &Self::B.inverse().unwrap();
let b = P::BaseField::one() * &Self::B.square().inverse().unwrap();
let a = Self::A * Self::B.inverse().unwrap();
let b = P::BaseField::one() * Self::B.square().inverse().unwrap();
(a, b)
};

// Convert the twisted Edwards element (x, y) to the alternate Montgomery element (u, v)
let (u_reconstructed, v_reconstructed) = {
let numerator = P::BaseField::one() + y;
let denominator = P::BaseField::one() - &y;
let denominator = P::BaseField::one() - y;

let u = numerator * &(denominator.inverse().unwrap());
let v = numerator * &((denominator * &x).inverse().unwrap());
let u = numerator * (denominator.inverse().unwrap());
let v = numerator * ((denominator * x).inverse().unwrap());

// Ensure (u, v) is a valid Montgomery element
#[cfg(debug_assertions)]
{
// Enforce B * v^2 == u^3 + A * u^2 + u
let v2 = v.square();
let u2 = u.square();
let u3 = u2 * &u;
assert_eq!(Self::B * &v2, u3 + (Self::A * &u2) + u);
let u3 = u2 * u;
assert_eq!(Self::B * v2, u3 + (Self::A * u2) + u);
}

let u = u * &Self::B.inverse().unwrap();
let v = v * &Self::B.inverse().unwrap();
let u = u * Self::B.inverse().unwrap();
let v = v * Self::B.inverse().unwrap();

// Ensure (u, v) is a valid alternate Montgomery element.
{
// Enforce v^2 == u^3 + A * u^2 + B * u
let v2 = v.square();
let u2 = u.square();
let u3 = u2 * &u;
assert_eq!(v2, u3 + (a * &u2) + (b * &u));
let u3 = u2 * u;
assert_eq!(v2, u3 + (a * u2) + (b * u));
}

(u, v)
Expand All @@ -227,21 +227,21 @@ impl<P: MontgomeryModelParameters + TEModelParameters, G: Group + ProjectiveCurv
}

// Verify -ux(x + A) is a residue.
assert_eq!((-(u * &x) * &(x + a)).legendre(), LegendreSymbol::QuadraticResidue);
assert_eq!((-(u * x) * (x + a)).legendre(), LegendreSymbol::QuadraticResidue);
}

let exists_in_sqrt_fq2 = v_reconstructed.square().sqrt().unwrap() == v_reconstructed;

let element = if exists_in_sqrt_fq2 {
// Let value = sqrt(-x / ((x + A) * u)).
let numerator = -x;
let denominator = (x + a) * &u;
(numerator * &denominator.inverse().unwrap()).sqrt().unwrap()
let denominator = (x + a) * u;
(numerator * denominator.inverse().unwrap()).sqrt().unwrap()
} else {
// Let value2 = sqrt(-(x + A) / ux)).
let numerator = -x - &a;
let denominator = x * &u;
(numerator * &denominator.inverse().unwrap()).sqrt().unwrap()
let numerator = -x - a;
let denominator = x * u;
(numerator * denominator.inverse().unwrap()).sqrt().unwrap()
};

let element = if sign_high {
Expand Down
2 changes: 1 addition & 1 deletion algorithms/src/encryption/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ impl<G: Group + ProjectiveCurve, SG: Group + CanonicalSerialize + CanonicalDeser
};

// m_i <- c_i - h_i
let m_i = *c_i - &h_i;
let m_i = *c_i - h_i;

plaintext.push(m_i);
i += one;
Expand Down
14 changes: 7 additions & 7 deletions algorithms/src/fft/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,20 +215,20 @@ impl<F: FftField> EvaluationDomain<F> {
}
u
} else {
let mut l = (t_size - &one) * &self.size_inv;
let mut l = (t_size - one) * self.size_inv;
let mut r = one;
let mut u = vec![F::zero(); size];
let mut ls = vec![F::zero(); size];
for i in 0..size {
u[i] = tau - &r;
u[i] = tau - r;
ls[i] = l;
l *= &self.group_gen;
r *= &self.group_gen;
}

batch_inversion(u.as_mut_slice());
cfg_iter_mut!(u).zip(ls).for_each(|(tau_minus_r, l)| {
*tau_minus_r = l * &*tau_minus_r;
*tau_minus_r = l * *tau_minus_r;
});
u
}
Expand All @@ -243,7 +243,7 @@ impl<F: FftField> EvaluationDomain<F> {
/// This evaluates the vanishing polynomial for this domain at tau.
/// For multiplicative subgroups, this polynomial is `z(X) = X^self.size - 1`.
pub fn evaluate_vanishing_polynomial(&self, tau: F) -> F {
tau.pow(&[self.size]) - &F::one()
tau.pow(&[self.size]) - F::one()
}

/// Return an iterator over the elements of the domain.
Expand Down Expand Up @@ -350,7 +350,7 @@ impl<F: FftField> EvaluationDomain<F> {
if log_powers.len() <= LOG_ROOTS_OF_UNITY_PARALLEL_SIZE {
powers[0] = F::one();
for i in 1..powers.len() {
powers[i] = powers[i - 1] * &log_powers[0];
powers[i] = powers[i - 1] * log_powers[0];
}
return;
}
Expand Down Expand Up @@ -609,7 +609,7 @@ mod tests {
// Do lagrange interpolation, and compare against the actual evaluation
let mut interpolated_evaluation = Fr::zero();
for i in 0..domain_size {
interpolated_evaluation += lagrange_coefficients[i] * &polynomial_evaluations[i];
interpolated_evaluation += lagrange_coefficients[i] * polynomial_evaluations[i];
}
assert_eq!(actual_evaluations, interpolated_evaluation);
}
Expand Down Expand Up @@ -675,7 +675,7 @@ mod tests {
let polynomial_evaluations = domain.fft(&random_polynomial.coeffs);
let polynomial_coset_evaluations = domain.coset_fft(&random_polynomial.coeffs);
for (i, x) in domain.elements().enumerate() {
let coset_x = Fr::multiplicative_generator() * &x;
let coset_x = Fr::multiplicative_generator() * x;

assert_eq!(polynomial_evaluations[i], random_polynomial.evaluate(x));
assert_eq!(polynomial_coset_evaluations[i], random_polynomial.evaluate(coset_x));
Expand Down
2 changes: 1 addition & 1 deletion algorithms/src/fft/polynomial/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl<F: Field> DenseOrSparsePolynomial<'_, F> {
// Can unwrap here because we know self is not zero.
let divisor_leading_inv = divisor.leading_coefficient().unwrap().inverse().unwrap();
while !remainder.is_zero() && remainder.degree() >= divisor.degree() {
let cur_q_coeff = *remainder.coeffs.last().unwrap() * &divisor_leading_inv;
let cur_q_coeff = *remainder.coeffs.last().unwrap() * divisor_leading_inv;
let cur_q_degree = remainder.degree() - divisor.degree();
quotient[cur_q_degree] = cur_q_coeff;

Expand Down
2 changes: 1 addition & 1 deletion algorithms/src/fft/polynomial/sparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl<F: Field> SparsePolynomial<F> {
}
let mut total = F::zero();
for (i, c) in &self.coeffs {
total += *c * &point.pow(&[*i as u64]);
total += *c * point.pow(&[*i as u64]);
}
total
}
Expand Down
4 changes: 2 additions & 2 deletions algorithms/src/signature/schnorr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ where
};

// k - xe;
let prover_response = random_scalar - &(verifier_challenge * private_key);
let prover_response = random_scalar - (verifier_challenge * private_key);
let signature = SchnorrOutput {
prover_response,
verifier_challenge,
Expand Down Expand Up @@ -271,7 +271,7 @@ where
}

let new_sig = SchnorrOutput {
prover_response: *prover_response - &(*verifier_challenge * &multiplier),
prover_response: *prover_response - (*verifier_challenge * multiplier),
verifier_challenge: *verifier_challenge,
};
end_timer!(rand_signature_time);
Expand Down
16 changes: 8 additions & 8 deletions algorithms/src/snark/gm17/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,15 @@ where
scalar_bits,
g_window,
&g_table,
&cfg_iter!(a).map(|a| *a * &gamma).collect::<Vec<_>>(),
&cfg_iter!(a).map(|a| *a * gamma).collect::<Vec<_>>(),
);
end_timer!(a_time);

// Compute the G_gamma-query
let g_gamma_time = start_timer!(|| "Calculate G gamma");
let gamma_z = zt * &gamma;
let gamma_z = zt * gamma;
let alpha_beta = alpha + beta;
let ab_gamma_z = alpha_beta * &gamma * &zt;
let ab_gamma_z = alpha_beta * gamma * zt;
let g_gamma = g.into_affine().mul(gamma);
let g_gamma_z = g.into_affine().mul(gamma_z);
let h_gamma = h.into_affine().mul(gamma);
Expand All @@ -253,13 +253,13 @@ where
let g_gamma2_z2 = g.into_affine().mul(gamma_z.square());

// Compute the vector G_gamma2_z_t := Z(t) * t^i * gamma^2 * G
let gamma2_z_t = gamma_z * &gamma;
let gamma2_z_t = gamma_z * gamma;
let mut g_gamma2_z_t = FixedBaseMSM::multi_scalar_mul::<E::G1Projective>(
scalar_bits,
g_window,
&g_table,
&cfg_into_iter!(0..m_raw + 1)
.map(|i| gamma2_z_t * &(t.pow([i as u64])))
.map(|i| gamma2_z_t * (t.pow([i as u64])))
.collect::<Vec<_>>(),
);
end_timer!(g_gamma_time);
Expand All @@ -271,21 +271,21 @@ where
g_window,
&g_table,
&cfg_into_iter!(0..sap_num_variables + 1)
.map(|i| c[i] * &gamma + (a[i] * &alpha_beta))
.map(|i| c[i] * gamma + (a[i] * alpha_beta))
.collect::<Vec<_>>(),
);
let (verifier_query, c_query_1) = result.split_at_mut(assembly.num_public_variables);
end_timer!(c1_time);

// Compute the C_2-query
let c2_time = start_timer!(|| "Calculate C2");
let double_gamma2_z = (zt * &gamma.square()).double();
let double_gamma2_z = (zt * gamma.square()).double();
let mut c_query_2 = FixedBaseMSM::multi_scalar_mul::<E::G1Projective>(
scalar_bits,
g_window,
&g_table,
&cfg_into_iter!(0..sap_num_variables + 1)
.map(|i| a[i] * &double_gamma2_z)
.map(|i| a[i] * double_gamma2_z)
.collect::<Vec<_>>(),
);
drop(g_table);
Expand Down
4 changes: 2 additions & 2 deletions algorithms/src/snark/gm17/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ where
// Compute C
let c_acc_time = start_timer!(|| "Compute C");
let r_2 = r + r;
let r2 = r * &r;
let d1_r_2 = d1 * &r_2;
let r2 = r * r;
let d1_r_2 = d1 * r_2;

let c1_acc_time = start_timer!(|| "Compute C1");
let (_, c1_aux_source) = params.get_c_query_1(0)?;
Expand Down
Loading

0 comments on commit 452bff7

Please sign in to comment.