diff --git a/src/bin/bench_field_mul_interleaved.rs b/src/bin/bench_field_mul_interleaved.rs index d04415f1..737105ad 100644 --- a/src/bin/bench_field_mul_interleaved.rs +++ b/src/bin/bench_field_mul_interleaved.rs @@ -14,8 +14,8 @@ const EXPONENT: usize = 1000000000; fn main() { let mut bases = [F::ZERO; WIDTH]; - for i in 0..WIDTH { - bases[i] = F::rand(); + for base_i in bases.iter_mut() { + *base_i = F::rand(); } let mut state = [F::ONE; WIDTH]; diff --git a/src/bin/bench_ldes.rs b/src/bin/bench_ldes.rs index ecdcd4fb..5620bd80 100644 --- a/src/bin/bench_ldes.rs +++ b/src/bin/bench_ldes.rs @@ -3,9 +3,8 @@ use std::time::Instant; use rayon::prelude::*; use plonky2::field::crandall_field::CrandallField; -use plonky2::field::fft; use plonky2::field::field::Field; -use plonky2::polynomial::polynomial::{PolynomialCoeffs, PolynomialValues}; +use plonky2::polynomial::polynomial::PolynomialValues; type F = CrandallField; @@ -18,9 +17,9 @@ fn main() { let start = Instant::now(); (0usize..PROVER_POLYS).into_par_iter().for_each(|i| { - let mut values = vec![CrandallField::ZERO; DEGREE]; + let mut values = vec![F::ZERO; DEGREE]; for j in 0usize..DEGREE { - values[j] = CrandallField((i * j) as u64); + values[j] = F::from_canonical_u64((i * j) as u64); } let poly_values = PolynomialValues::new(values); let start = Instant::now(); diff --git a/src/bin/bench_recursion.rs b/src/bin/bench_recursion.rs index 81409642..566fb056 100644 --- a/src/bin/bench_recursion.rs +++ b/src/bin/bench_recursion.rs @@ -1,24 +1,14 @@ -use std::thread; -use std::time::Instant; - use env_logger::Env; -use rayon::prelude::*; use plonky2::circuit_builder::CircuitBuilder; use plonky2::circuit_data::CircuitConfig; use plonky2::field::crandall_field::CrandallField; -use plonky2::field::fft; use plonky2::field::field::Field; use plonky2::gates::constant::ConstantGate; use plonky2::gates::gmimc::GMiMCGate; -use plonky2::gmimc::gmimc_permute_array; -use plonky2::hash::{GMIMC_CONSTANTS, GMIMC_ROUNDS}; -use plonky2::polynomial::polynomial::PolynomialCoeffs; +use plonky2::hash::GMIMC_ROUNDS; use plonky2::witness::PartialWitness; -// 113 wire polys, 3 Z polys, 4 parts of quotient poly. -const PROVER_POLYS: usize = 113 + 3 + 4; - fn main() { // Set the default log filter. This can be overridden using the `RUST_LOG` environment variable, // e.g. `RUST_LOG=debug`. diff --git a/src/bin/field_search.rs b/src/bin/field_search.rs index e0fdb7e6..6c433339 100644 --- a/src/bin/field_search.rs +++ b/src/bin/field_search.rs @@ -53,5 +53,5 @@ fn is_prime(n: u64) -> bool { d += 2; } - return true; + true } diff --git a/src/field/crandall_field.rs b/src/field/crandall_field.rs index ae5dd89e..8983ef67 100644 --- a/src/field/crandall_field.rs +++ b/src/field/crandall_field.rs @@ -69,6 +69,7 @@ impl Field for CrandallField { *self * *self * *self } + #[allow(clippy::many_single_char_names)] // The names are from the paper. fn try_inverse(&self) -> Option { if self.is_zero() { return None; @@ -164,6 +165,7 @@ impl Add for CrandallField { type Output = Self; #[inline] + #[allow(clippy::suspicious_arithmetic_impl)] fn add(self, rhs: Self) -> Self { let (sum, over) = self.0.overflowing_add(rhs.0); Self(sum.overflowing_sub((over as u64) * Self::ORDER).0) @@ -180,6 +182,7 @@ impl Sub for CrandallField { type Output = Self; #[inline] + #[allow(clippy::suspicious_arithmetic_impl)] fn sub(self, rhs: Self) -> Self { let (diff, under) = self.0.overflowing_sub(rhs.0); Self(diff.overflowing_add((under as u64) * Self::ORDER).0) @@ -212,6 +215,7 @@ impl MulAssign for CrandallField { impl Div for CrandallField { type Output = Self; + #[allow(clippy::suspicious_arithmetic_impl)] fn div(self, rhs: Self) -> Self::Output { self * rhs.inverse() } diff --git a/src/field/fft.rs b/src/field/fft.rs index af7a42f6..f38ea204 100644 --- a/src/field/fft.rs +++ b/src/field/fft.rs @@ -66,8 +66,8 @@ pub(crate) fn ifft_with_precomputation_power_of_2( fft_with_precomputation_power_of_2(PolynomialCoeffs { coeffs: values }, precomputation); // We reverse all values except the first, and divide each by n. - result[0] = result[0] * n_inv; - result[n / 2] = result[n / 2] * n_inv; + result[0] *= n_inv; + result[n / 2] *= n_inv; for i in 1..(n / 2) { let j = n - i; let result_i = result[j] * n_inv; diff --git a/src/field/field.rs b/src/field/field.rs index c9368208..ed81cb6b 100644 --- a/src/field/field.rs +++ b/src/field/field.rs @@ -105,7 +105,7 @@ pub trait Field: let mut current = Self::ONE; for _i in 0..order { subgroup.push(current); - current = current * generator; + current *= generator; } subgroup } @@ -149,7 +149,7 @@ pub trait Field: for j in 0..power.bits() { if (power.to_canonical_u64() >> j & 1) != 0 { - product = product * current; + product *= current; } current = current.square(); } diff --git a/src/fri.rs b/src/fri.rs index 13885cc9..2bb4935d 100644 --- a/src/fri.rs +++ b/src/fri.rs @@ -126,13 +126,12 @@ fn fri_proof_of_work(current_hash: Hash, config: &FriConfig) -> F { (0u64..) .find(|&i| { hash_n_to_1( - Vec::from_iter( - current_hash - .elements - .iter() - .copied() - .chain(Some(F::from_canonical_u64(i))), - ), + current_hash + .elements + .iter() + .copied() + .chain(Some(F::from_canonical_u64(i))) + .collect(), false, ) .to_canonical_u64() @@ -149,14 +148,13 @@ fn fri_verify_proof_of_work( config: &FriConfig, ) -> Result<()> { let hash = hash_n_to_1( - Vec::from_iter( - challenger - .get_hash() - .elements - .iter() - .copied() - .chain(Some(proof.pow_witness)), - ), + challenger + .get_hash() + .elements + .iter() + .copied() + .chain(Some(proof.pow_witness)) + .collect(), false, ); ensure!( diff --git a/src/gadgets/arithmetic.rs b/src/gadgets/arithmetic.rs index 00885e7e..8fa5a226 100644 --- a/src/gadgets/arithmetic.rs +++ b/src/gadgets/arithmetic.rs @@ -91,10 +91,8 @@ impl CircuitBuilder { return Some(self.constant(x + y)); } - if first_term_zero { - if const_1.is_one() { - return Some(addend); - } + if first_term_zero && const_1.is_one() { + return Some(addend); } if second_term_zero { diff --git a/src/gmimc.rs b/src/gmimc.rs index 3b7d1e2c..9a65d49d 100644 --- a/src/gmimc.rs +++ b/src/gmimc.rs @@ -35,7 +35,7 @@ pub fn gmimc_compress( F::ZERO, F::ZERO, ]; - let state_1 = gmimc_permute::(state_0, constants.clone()); + let state_1 = gmimc_permute::(state_0, constants); [state_1[0], state_1[1], state_1[2], state_1[3]] } @@ -96,7 +96,7 @@ pub fn gmimc_permute_naive( let f = (xs[active] + constants[r]).cube(); for i in 0..W { if i != active { - xs[i] = xs[i] + f; + xs[i] += f; } } } diff --git a/src/hash.rs b/src/hash.rs index d87d3e28..786fc57c 100644 --- a/src/hash.rs +++ b/src/hash.rs @@ -173,9 +173,7 @@ impl CircuitBuilder { // Overwrite the first r elements with the inputs. This differs from a standard sponge, // where we would xor or add in the inputs. This is a well-known variant, though, // sometimes called "overwrite mode". - for i in 0..input_chunk.len() { - state[i] = input_chunk[i]; - } + state[..input_chunk.len()].copy_from_slice(input_chunk); state = self.permute(state); } @@ -268,10 +266,7 @@ pub(crate) fn merkle_root(vecs: Vec>) -> Hash { } pub(crate) fn merkle_root_inner(vecs: Vec>) -> Hash { - let mut hashes = vecs - .into_iter() - .map(|leaf_set| hash_or_noop(leaf_set)) - .collect::>(); + let mut hashes = vecs.into_iter().map(hash_or_noop).collect::>(); while hashes.len() > 1 { hashes = hashes .chunks(2) diff --git a/src/plonk_challenger.rs b/src/plonk_challenger.rs index a98a6fc3..a8f5e605 100644 --- a/src/plonk_challenger.rs +++ b/src/plonk_challenger.rs @@ -107,6 +107,12 @@ impl Challenger { } } +impl Default for Challenger { + fn default() -> Self { + Self::new() + } +} + /// A recursive version of `Challenger`. pub(crate) struct RecursiveChallenger { sponge_state: [Target; SPONGE_WIDTH], diff --git a/src/polynomial/division.rs b/src/polynomial/division.rs index 31f746f4..0b5055ef 100644 --- a/src/polynomial/division.rs +++ b/src/polynomial/division.rs @@ -9,15 +9,15 @@ use crate::util::log2_strict; pub(crate) fn divide_by_z_h(mut a: PolynomialCoeffs, n: usize) -> PolynomialCoeffs { // TODO: Is this special case needed? if a.coeffs.iter().all(|p| *p == F::ZERO) { - return a.clone(); + return a; } let g = F::MULTIPLICATIVE_GROUP_GENERATOR; let mut g_pow = F::ONE; // Multiply the i-th coefficient of `a` by `g^i`. Then `new_a(w^j) = old_a(g.w^j)`. a.coeffs.iter_mut().for_each(|x| { - *x = (*x) * g_pow; - g_pow = g * g_pow; + *x *= g_pow; + g_pow *= g; }); let root = F::primitive_root_of_unity(log2_strict(a.len())); @@ -43,7 +43,7 @@ pub(crate) fn divide_by_z_h(mut a: PolynomialCoeffs, n: usize) -> P .iter_mut() .zip(denominators_inv.iter()) .for_each(|(x, &d)| { - *x = (*x) * d; + *x *= d; }); // `p` is the interpolating polynomial of `a_eval` on `{w^i}`. let mut p = ifft(a_eval); @@ -52,8 +52,8 @@ pub(crate) fn divide_by_z_h(mut a: PolynomialCoeffs, n: usize) -> P let g_inv = g.inverse(); let mut g_inv_pow = F::ONE; p.coeffs.iter_mut().for_each(|x| { - *x = (*x) * g_inv_pow; - g_inv_pow = g_inv_pow * g_inv; + *x *= g_inv_pow; + g_inv_pow *= g_inv; }); p } diff --git a/src/polynomial/polynomial.rs b/src/polynomial/polynomial.rs index 11949494..0444b1e8 100644 --- a/src/polynomial/polynomial.rs +++ b/src/polynomial/polynomial.rs @@ -31,7 +31,7 @@ impl PolynomialValues { } pub fn lde(self, rate_bits: usize) -> Self { - let mut coeffs = ifft(self).lde(rate_bits); + let coeffs = ifft(self).lde(rate_bits); fft(coeffs) } } @@ -88,7 +88,7 @@ impl PolynomialCoeffs { polys.into_iter().map(|p| p.lde(rate_bits)).collect() } - pub(crate) fn lde(mut self, rate_bits: usize) -> Self { + pub(crate) fn lde(self, rate_bits: usize) -> Self { let original_size = self.len(); let lde_size = original_size << rate_bits; let Self { mut coeffs } = self; diff --git a/src/witness.rs b/src/witness.rs index 4c5e89a8..42b7150c 100644 --- a/src/witness.rs +++ b/src/witness.rs @@ -79,3 +79,9 @@ impl PartialWitness { } } } + +impl Default for PartialWitness { + fn default() -> Self { + Self::new() + } +}