From 08664b1df94a77fa49d32f098e0d05d6102de170 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Fri, 2 Aug 2019 11:13:59 +0100 Subject: [PATCH] Address various clippy warnings/errors in bellman --- src/domain.rs | 22 ++++++++------ src/gadgets/blake2s.rs | 4 +-- src/gadgets/boolean.rs | 30 +++++++++--------- src/gadgets/multieq.rs | 2 +- src/gadgets/num.rs | 12 ++++---- src/gadgets/sha256.rs | 6 ++-- src/gadgets/test/mod.rs | 6 ++-- src/gadgets/uint32.rs | 66 +++++++++++++++++++--------------------- src/groth16/generator.rs | 2 +- src/groth16/mod.rs | 18 +++++------ src/groth16/verifier.rs | 2 +- src/lib.rs | 5 +-- src/multicore.rs | 2 +- 13 files changed, 90 insertions(+), 87 deletions(-) diff --git a/src/domain.rs b/src/domain.rs index 808d2af..d5a86bd 100644 --- a/src/domain.rs +++ b/src/domain.rs @@ -26,15 +26,19 @@ pub struct EvaluationDomain> { minv: E::Fr, } -impl> EvaluationDomain { - pub fn as_ref(&self) -> &[G] { +impl> AsRef<[G]> for EvaluationDomain { + fn as_ref(&self) -> &[G] { &self.coeffs } +} - pub fn as_mut(&mut self) -> &mut [G] { +impl> AsMut<[G]> for EvaluationDomain { + fn as_mut(&mut self) -> &mut [G] { &mut self.coeffs } +} +impl> EvaluationDomain { pub fn into_coeffs(self) -> Vec { self.coeffs } @@ -64,9 +68,9 @@ impl> EvaluationDomain { coeffs.resize(m, G::group_zero()); Ok(EvaluationDomain { - coeffs: coeffs, - exp: exp, - omega: omega, + coeffs, + exp, + omega, omegainv: omega.inverse().unwrap(), geninv: E::Fr::multiplicative_generator().inverse().unwrap(), minv: E::Fr::from_str(&format!("{}", m)) @@ -291,7 +295,7 @@ fn serial_fft>(a: &mut [T], omega: &E::Fr, log_n: u let mut m = 1; for _ in 0..log_n { - let w_m = omega.pow(&[(n / (2 * m)) as u64]); + let w_m = omega.pow(&[u64::from(n / (2 * m))]); let mut k = 0; while k < n { @@ -337,12 +341,12 @@ fn parallel_fft>( let omega_step = omega.pow(&[(j as u64) << log_new_n]); let mut elt = E::Fr::one(); - for i in 0..(1 << log_new_n) { + for (i, tmp) in tmp.iter_mut().enumerate() { for s in 0..num_cpus { let idx = (i + (s << log_new_n)) % (1 << log_n); let mut t = a[idx]; t.group_mul_assign(&elt); - tmp[i].group_add_assign(&t); + tmp.group_add_assign(&t); elt.mul_assign(&omega_step); } elt.mul_assign(&omega_j); diff --git a/src/gadgets/blake2s.rs b/src/gadgets/blake2s.rs index c5cee23..beefe09 100644 --- a/src/gadgets/blake2s.rs +++ b/src/gadgets/blake2s.rs @@ -382,7 +382,7 @@ pub fn blake2s>( blocks.push(this_block); } - if blocks.len() == 0 { + if blocks.is_empty() { blocks.push((0..16).map(|_| UInt32::constant(0)).collect()); } @@ -433,7 +433,7 @@ mod test { let expected = hex!("c59f682376d137f3f255e671e207d1f2374ebe504e9314208a52d9f88d69e8c8"); let mut out = out.into_iter(); - for b in expected.into_iter() { + for b in expected.iter() { for i in 0..8 { let c = out.next().unwrap().get_value().unwrap(); diff --git a/src/gadgets/boolean.rs b/src/gadgets/boolean.rs index 414b290..b26bb19 100644 --- a/src/gadgets/boolean.rs +++ b/src/gadgets/boolean.rs @@ -60,7 +60,7 @@ impl AllocatedBit { Ok(AllocatedBit { variable: var, - value: value, + value, }) } @@ -93,7 +93,7 @@ impl AllocatedBit { Ok(AllocatedBit { variable: var, - value: value, + value, }) } @@ -302,7 +302,7 @@ pub fn field_into_boolean_vec_le, F: PrimeFie ) -> Result, SynthesisError> { let v = field_into_allocated_bits_le::(cs, value)?; - Ok(v.into_iter().map(|e| Boolean::from(e)).collect()) + Ok(v.into_iter().map(Boolean::from).collect()) } pub fn field_into_allocated_bits_le, F: PrimeField>( @@ -412,24 +412,24 @@ impl Boolean { } pub fn get_value(&self) -> Option { - match self { - &Boolean::Constant(c) => Some(c), - &Boolean::Is(ref v) => v.get_value(), - &Boolean::Not(ref v) => v.get_value().map(|b| !b), + match *self { + Boolean::Constant(c) => Some(c), + Boolean::Is(ref v) => v.get_value(), + Boolean::Not(ref v) => v.get_value().map(|b| !b), } } pub fn lc(&self, one: Variable, coeff: E::Fr) -> LinearCombination { - match self { - &Boolean::Constant(c) => { + match *self { + Boolean::Constant(c) => { if c { LinearCombination::::zero() + (coeff, one) } else { LinearCombination::::zero() } } - &Boolean::Is(ref v) => LinearCombination::::zero() + (coeff, v.get_variable()), - &Boolean::Not(ref v) => { + Boolean::Is(ref v) => LinearCombination::::zero() + (coeff, v.get_variable()), + Boolean::Not(ref v) => { LinearCombination::::zero() + (coeff, one) - (coeff, v.get_variable()) } } @@ -442,10 +442,10 @@ impl Boolean { /// Return a negated interpretation of this boolean. pub fn not(&self) -> Self { - match self { - &Boolean::Constant(c) => Boolean::Constant(!c), - &Boolean::Is(ref v) => Boolean::Not(v.clone()), - &Boolean::Not(ref v) => Boolean::Is(v.clone()), + match *self { + Boolean::Constant(c) => Boolean::Constant(!c), + Boolean::Is(ref v) => Boolean::Not(v.clone()), + Boolean::Not(ref v) => Boolean::Is(v.clone()), } } diff --git a/src/gadgets/multieq.rs b/src/gadgets/multieq.rs index 510802d..095017a 100644 --- a/src/gadgets/multieq.rs +++ b/src/gadgets/multieq.rs @@ -14,7 +14,7 @@ pub struct MultiEq> { impl> MultiEq { pub fn new(cs: CS) -> Self { MultiEq { - cs: cs, + cs, ops: 0, bits_used: 0, lhs: LinearCombination::zero(), diff --git a/src/gadgets/num.rs b/src/gadgets/num.rs index 84843c1..81f4fb3 100644 --- a/src/gadgets/num.rs +++ b/src/gadgets/num.rs @@ -78,7 +78,7 @@ impl AllocatedNum { E: Engine, CS: ConstraintSystem, { - assert!(v.len() > 0); + assert!(!v.is_empty()); // Let's keep this simple for now and just AND them all // manually @@ -132,7 +132,7 @@ impl AllocatedNum { current_run.push(a_bit.clone()); result.push(a_bit); } else { - if current_run.len() > 0 { + if !current_run.is_empty() { // This is the start of a run of zeros, but we need // to k-ary AND against `last_run` first. @@ -183,7 +183,7 @@ impl AllocatedNum { cs.enforce(|| "unpacking constraint", |lc| lc, |lc| lc, |_| lc); // Convert into booleans, and reverse for little-endian bit order - Ok(result.into_iter().map(|b| Boolean::from(b)).rev().collect()) + Ok(result.into_iter().map(Boolean::from).rev().collect()) } /// Convert the allocated number into its little-endian representation. @@ -208,7 +208,7 @@ impl AllocatedNum { cs.enforce(|| "unpacking constraint", |lc| lc, |lc| lc, |_| lc); - Ok(bits.into_iter().map(|b| Boolean::from(b)).collect()) + Ok(bits.into_iter().map(Boolean::from).collect()) } pub fn mul(&self, mut cs: CS, other: &Self) -> Result @@ -238,7 +238,7 @@ impl AllocatedNum { ); Ok(AllocatedNum { - value: value, + value, variable: var, }) } @@ -270,7 +270,7 @@ impl AllocatedNum { ); Ok(AllocatedNum { - value: value, + value, variable: var, }) } diff --git a/src/gadgets/sha256.rs b/src/gadgets/sha256.rs index cb057f8..e346a71 100644 --- a/src/gadgets/sha256.rs +++ b/src/gadgets/sha256.rs @@ -4,6 +4,7 @@ use super::uint32::UInt32; use crate::{ConstraintSystem, SynthesisError}; use pairing::Engine; +#[allow(clippy::unreadable_literal)] const ROUND_CONSTANTS: [u32; 64] = [ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, @@ -15,6 +16,7 @@ const ROUND_CONSTANTS: [u32; 64] = [ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2, ]; +#[allow(clippy::unreadable_literal)] const IV: [u32; 8] = [ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19, ]; @@ -130,7 +132,7 @@ where Ok(match self { Maybe::Concrete(ref v) => return Ok(v.clone()), Maybe::Deferred(mut v) => { - v.extend(others.into_iter().cloned()); + v.extend(others.iter().cloned()); UInt32::addmany(cs, &v)? } }) @@ -286,7 +288,7 @@ mod test { let expected = hex!("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); let mut out = out_bits.into_iter(); - for b in expected.into_iter() { + for b in expected.iter() { for i in (0..8).rev() { let c = out.next().unwrap().get_value().unwrap(); diff --git a/src/gadgets/test/mod.rs b/src/gadgets/test/mod.rs index 58ba040..eed676e 100644 --- a/src/gadgets/test/mod.rs +++ b/src/gadgets/test/mod.rs @@ -66,7 +66,7 @@ fn proc_lc(terms: &[(Variable, E::Fr)]) -> BTreeMap TestConstraintSystem { }; let powers_of_two = (0..E::Fr::NUM_BITS) - .map(|i| E::Fr::from_str("2").unwrap().pow(&[i as u64])) + .map(|i| E::Fr::from_str("2").unwrap().pow(&[u64::from(i)])) .collect::>(); let pp = |s: &mut String, lc: &LinearCombination| { @@ -286,7 +286,7 @@ impl TestConstraintSystem { } } - return true; + true } pub fn num_inputs(&self) -> usize { diff --git a/src/gadgets/uint32.rs b/src/gadgets/uint32.rs index ef3f44a..3467400 100644 --- a/src/gadgets/uint32.rs +++ b/src/gadgets/uint32.rs @@ -33,7 +33,7 @@ impl UInt32 { } UInt32 { - bits: bits, + bits, value: Some(value), } } @@ -69,10 +69,7 @@ impl UInt32 { }) .collect::, SynthesisError>>()?; - Ok(UInt32 { - bits: bits, - value: value, - }) + Ok(UInt32 { bits, value }) } pub fn into_bits_be(&self) -> Vec { @@ -98,7 +95,7 @@ impl UInt32 { } UInt32 { - value: value, + value, bits: bits.iter().rev().cloned().collect(), } } @@ -119,20 +116,20 @@ impl UInt32 { for b in new_bits.iter().rev() { value.as_mut().map(|v| *v <<= 1); - match b { - &Boolean::Constant(b) => { + match *b { + Boolean::Constant(b) => { if b { value.as_mut().map(|v| *v |= 1); } } - &Boolean::Is(ref b) => match b.get_value() { + Boolean::Is(ref b) => match b.get_value() { Some(true) => { value.as_mut().map(|v| *v |= 1); } Some(false) => {} None => value = None, }, - &Boolean::Not(ref b) => match b.get_value() { + Boolean::Not(ref b) => match b.get_value() { Some(false) => { value.as_mut().map(|v| *v |= 1); } @@ -143,7 +140,7 @@ impl UInt32 { } UInt32 { - value: value, + value, bits: new_bits, } } @@ -215,7 +212,7 @@ impl UInt32 { .collect::>()?; Ok(UInt32 { - bits: bits, + bits, value: new_value, }) } @@ -274,7 +271,7 @@ impl UInt32 { .collect::>()?; Ok(UInt32 { - bits: bits, + bits, value: new_value, }) } @@ -294,7 +291,7 @@ impl UInt32 { // Compute the maximum value of the sum so we allocate enough bits for // the result - let mut max_value = (operands.len() as u64) * (u32::max_value() as u64); + let mut max_value = (operands.len() as u64) * (u64::from(u32::max_value())); // Keep track of the resulting value let mut result_value = Some(0u64); @@ -310,7 +307,7 @@ impl UInt32 { // Accumulate the value match op.value { Some(val) => { - result_value.as_mut().map(|v| *v += val as u64); + result_value.as_mut().map(|v| *v += u64::from(val)); } None => { // If any of our operands have unknown value, we won't @@ -408,8 +405,8 @@ mod test { let b = UInt32::from_bits_be(&v); for (i, bit) in b.bits.iter().enumerate() { - match bit { - &Boolean::Constant(bit) => { + match *bit { + Boolean::Constant(bit) => { assert!(bit == ((b.value.unwrap() >> i) & 1 == 1)); } _ => unreachable!(), @@ -443,8 +440,8 @@ mod test { let b = UInt32::from_bits(&v); for (i, bit) in b.bits.iter().enumerate() { - match bit { - &Boolean::Constant(bit) => { + match *bit { + Boolean::Constant(bit) => { assert!(bit == ((b.value.unwrap() >> i) & 1 == 1)); } _ => unreachable!(), @@ -491,14 +488,14 @@ mod test { assert!(r.value == Some(expected)); for b in r.bits.iter() { - match b { - &Boolean::Is(ref b) => { + match *b { + Boolean::Is(ref b) => { assert!(b.get_value().unwrap() == (expected & 1 == 1)); } - &Boolean::Not(ref b) => { + Boolean::Not(ref b) => { assert!(!b.get_value().unwrap() == (expected & 1 == 1)); } - &Boolean::Constant(b) => { + Boolean::Constant(b) => { assert!(b == (expected & 1 == 1)); } } @@ -538,10 +535,10 @@ mod test { assert!(r.value == Some(expected)); for b in r.bits.iter() { - match b { - &Boolean::Is(_) => panic!(), - &Boolean::Not(_) => panic!(), - &Boolean::Constant(b) => { + match *b { + Boolean::Is(_) => panic!(), + Boolean::Not(_) => panic!(), + Boolean::Constant(b) => { assert!(b == (expected & 1 == 1)); } } @@ -576,8 +573,7 @@ mod test { let r = a_bit.xor(cs.namespace(|| "xor"), &b_bit).unwrap(); let r = { let mut cs = MultiEq::new(&mut cs); - let r = UInt32::addmany(cs.namespace(|| "addition"), &[r, c_bit, d_bit]).unwrap(); - r + UInt32::addmany(cs.namespace(|| "addition"), &[r, c_bit, d_bit]).unwrap() }; assert!(cs.is_satisfied()); @@ -585,14 +581,14 @@ mod test { assert!(r.value == Some(expected)); for b in r.bits.iter() { - match b { - &Boolean::Is(ref b) => { + match *b { + Boolean::Is(ref b) => { assert!(b.get_value().unwrap() == (expected & 1 == 1)); } - &Boolean::Not(ref b) => { + Boolean::Not(ref b) => { assert!(!b.get_value().unwrap() == (expected & 1 == 1)); } - &Boolean::Constant(_) => unreachable!(), + Boolean::Constant(_) => unreachable!(), } expected >>= 1; @@ -628,8 +624,8 @@ mod test { let mut tmp = num; for b in &b.bits { - match b { - &Boolean::Constant(b) => { + match *b { + Boolean::Constant(b) => { assert_eq!(b, tmp & 1 == 1); } _ => unreachable!(), diff --git a/src/groth16/generator.rs b/src/groth16/generator.rs index 2ece8b7..7ca6c9a 100644 --- a/src/groth16/generator.rs +++ b/src/groth16/generator.rs @@ -451,7 +451,7 @@ where }; Ok(Parameters { - vk: vk, + vk, h: Arc::new(h.into_iter().map(|e| e.into_affine()).collect()), l: Arc::new(l.into_iter().map(|e| e.into_affine()).collect()), diff --git a/src/groth16/mod.rs b/src/groth16/mod.rs index 9fa3bc8..44c6f22 100644 --- a/src/groth16/mod.rs +++ b/src/groth16/mod.rs @@ -90,7 +90,7 @@ impl Proof { } })?; - Ok(Proof { a: a, b: b, c: c }) + Ok(Proof { a, b, c }) } } @@ -208,13 +208,13 @@ impl VerifyingKey { } Ok(VerifyingKey { - alpha_g1: alpha_g1, - beta_g1: beta_g1, - beta_g2: beta_g2, - gamma_g2: gamma_g2, - delta_g1: delta_g1, - delta_g2: delta_g2, - ic: ic, + alpha_g1, + beta_g1, + beta_g2, + gamma_g2, + delta_g1, + delta_g2, + ic, }) } } @@ -376,7 +376,7 @@ impl Parameters { } Ok(Parameters { - vk: vk, + vk, h: Arc::new(h), l: Arc::new(l), a: Arc::new(a), diff --git a/src/groth16/verifier.rs b/src/groth16/verifier.rs index 065a3b0..5bc0581 100644 --- a/src/groth16/verifier.rs +++ b/src/groth16/verifier.rs @@ -49,7 +49,7 @@ pub fn verify_proof<'a, E: Engine>( (&acc.into_affine().prepare(), &pvk.neg_gamma_g2), (&proof.c.prepare(), &pvk.neg_delta_g2), ] - .into_iter(), + .iter(), )) .unwrap() == pvk.alpha_g1_beta_g2) diff --git a/src/lib.rs b/src/lib.rs index a75c85f..1445107 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -91,6 +91,7 @@ impl Add<(E::Fr, Variable)> for LinearCombination { impl Sub<(E::Fr, Variable)> for LinearCombination { type Output = LinearCombination; + #[allow(clippy::suspicious_arithmetic_impl)] fn sub(self, (mut coeff, var): (E::Fr, Variable)) -> LinearCombination { coeff.negate(); @@ -213,7 +214,7 @@ impl Error for SynthesisError { impl fmt::Display for SynthesisError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { - if let &SynthesisError::IoError(ref e) = self { + if let SynthesisError::IoError(ref e) = *self { write!(f, "I/O error: ")?; e.fmt(f) } else { @@ -278,7 +279,7 @@ pub trait ConstraintSystem: Sized { fn get_root(&mut self) -> &mut Self::Root; /// Begin a namespace for this constraint system. - fn namespace<'a, NR, N>(&'a mut self, name_fn: N) -> Namespace<'a, E, Self::Root> + fn namespace(&mut self, name_fn: N) -> Namespace<'_, E, Self::Root> where NR: Into, N: FnOnce() -> NR, diff --git a/src/multicore.rs b/src/multicore.rs index e8b2dae..7ebc89a 100644 --- a/src/multicore.rs +++ b/src/multicore.rs @@ -23,7 +23,7 @@ mod implementation { // CPUs configured. pub(crate) fn new_with_cpus(cpus: usize) -> Worker { Worker { - cpus: cpus, + cpus, pool: CpuPool::new(cpus), } }