diff --git a/src/loader.rs b/src/loader.rs index 8713e2cf..ebca0ec9 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -1,4 +1,4 @@ -use crate::util::{Curve, FieldOps, GroupOps, PrimeField}; +use crate::util::{Curve, FieldOps, GroupOps, Itertools, PrimeField}; use std::{fmt::Debug, iter}; pub mod native; @@ -76,7 +76,7 @@ pub trait LoadedScalar: Clone + Debug + FieldOps { &values .iter() .map(|value| (F::one(), value.clone())) - .collect::>(), + .collect_vec(), constant, ) } @@ -129,7 +129,7 @@ pub trait LoadedScalar: Clone + Debug + FieldOps { iter::successors(Some(self.clone()), |power| Some(power.clone() * self)) .take(n - 1), ) - .collect::>() + .collect_vec() } } diff --git a/src/loader/evm.rs b/src/loader/evm.rs index db71e176..e0754532 100644 --- a/src/loader/evm.rs +++ b/src/loader/evm.rs @@ -1,4 +1,7 @@ -use crate::{scheme::kzg::Cost, util::PrimeField}; +use crate::{ + scheme::kzg::Cost, + util::{Itertools, PrimeField}, +}; use ethereum_types::U256; use std::iter; @@ -46,15 +49,12 @@ where F: PrimeField, { iter::empty() - .chain(instances.into_iter().flatten().flat_map(|value| { - value - .to_repr() - .as_ref() - .iter() - .rev() - .cloned() - .collect::>() - })) + .chain( + instances + .into_iter() + .flatten() + .flat_map(|value| value.to_repr().as_ref().iter().rev().cloned().collect_vec()), + ) .chain(proof) .collect() } diff --git a/src/loader/evm/accumulation.rs b/src/loader/evm/accumulation.rs index dfc4ccdd..cfe4be6f 100644 --- a/src/loader/evm/accumulation.rs +++ b/src/loader/evm/accumulation.rs @@ -2,7 +2,7 @@ use crate::{ loader::evm::loader::{EvmLoader, Scalar}, protocol::Protocol, scheme::kzg::{AccumulationStrategy, Accumulator, SameCurveAccumulation, MSM}, - util::{Curve, PrimeCurveAffine, PrimeField, Transcript, UncompressedEncoding}, + util::{Curve, Itertools, PrimeCurveAffine, PrimeField, Transcript, UncompressedEncoding}, Error, }; use ethereum_types::U256; @@ -56,7 +56,7 @@ where let num_statements = statements .iter() .map(|statements| statements.len()) - .collect::>(); + .collect_vec(); let challenges = transcript.squeeze_n_challenges(accumulator_indices.len()); let accumulators = accumulator_indices @@ -73,7 +73,7 @@ where let rhs = loader.calldataload_ec_point_from_limbs::(offset + 0x100); Accumulator::new(MSM::base(lhs), MSM::base(rhs)) }) - .collect::>(); + .collect_vec(); Some(Accumulator::random_linear_combine( challenges.into_iter().zip(accumulators), diff --git a/src/loader/evm/code.rs b/src/loader/evm/code.rs index 3e336b1c..38069401 100644 --- a/src/loader/evm/code.rs +++ b/src/loader/evm/code.rs @@ -1,3 +1,4 @@ +use crate::util::Itertools; use ethereum_types::U256; use foundry_evm::{revm::opcode::*, HashMap}; @@ -22,7 +23,7 @@ impl Code { constants: HashMap::new(), stack_len: 0, }; - let constants = constants.into_iter().collect::>(); + let constants = constants.into_iter().collect_vec(); for constant in constants.iter() { code.push(*constant); } diff --git a/src/loader/evm/loader.rs b/src/loader/evm/loader.rs index 8f368a59..258ffdd0 100644 --- a/src/loader/evm/loader.rs +++ b/src/loader/evm/loader.rs @@ -4,7 +4,7 @@ use crate::{ modulus, }, loader::{EcPointLoader, LoadedEcPoint, LoadedScalar, Loader, ScalarLoader}, - util::{Curve, FieldOps, PrimeField, UncompressedEncoding}, + util::{Curve, FieldOps, Itertools, PrimeField, UncompressedEncoding}, }; use ethereum_types::{U256, U512}; use std::{ @@ -266,7 +266,7 @@ impl EvmLoader { (ptr..ptr + len) .step_by(0x20) .map(|ptr| self.dup_scalar(&self.scalar(Value::Memory(ptr)))) - .collect::>() + .collect_vec() .first() .unwrap() .ptr() @@ -816,7 +816,7 @@ impl> LoadedScalar for Scalar { } fn batch_invert<'a>(values: impl IntoIterator) { - let values = values.into_iter().collect::>(); + let values = values.into_iter().collect_vec(); let loader = &values.first().unwrap().loader; let products = iter::once(values[0].clone()) .chain( @@ -824,7 +824,7 @@ impl> LoadedScalar for Scalar { .map(|ptr| loader.scalar(Value::Memory(ptr))) .take(values.len() - 1), ) - .collect::>(); + .collect_vec(); loader.code.borrow_mut().push(loader.scalar_modulus); for _ in 2..values.len() { diff --git a/src/loader/evm/test.rs b/src/loader/evm/test.rs index 34cab3c7..9b7e9bde 100644 --- a/src/loader/evm/test.rs +++ b/src/loader/evm/test.rs @@ -1,4 +1,4 @@ -use crate::loader::evm::test::tui::Tui; +use crate::{loader::evm::test::tui::Tui, util::Itertools}; use foundry_evm::{ executor::{backend::Backend, fork::MultiFork, ExecutorBuilder}, revm::AccountInfo, @@ -44,7 +44,7 @@ pub fn execute(code: Vec, calldata: Vec) -> (bool, u64, Vec) { .logs .into_iter() .map(|log| h256_to_u256_be(log.topics[0]).as_u64()) - .collect::>(); + .collect_vec(); if debug { Tui::new(result.debug.unwrap().flatten(0), 0).start(); diff --git a/src/loader/evm/transcript.rs b/src/loader/evm/transcript.rs index cdb6cd44..a5ff68d2 100644 --- a/src/loader/evm/transcript.rs +++ b/src/loader/evm/transcript.rs @@ -7,7 +7,7 @@ use crate::{ native::NativeLoader, Loader, }, - util::{Curve, Group, PrimeField, Transcript, TranscriptRead, UncompressedEncoding}, + util::{Curve, Group, Itertools, PrimeField, Transcript, TranscriptRead, UncompressedEncoding}, Error, }; use ethereum_types::U256; @@ -153,7 +153,7 @@ where } else { None }) - .collect::>(); + .collect_vec(); let hash: [u8; 32] = Keccak256::digest(data).into(); self.buf = hash.to_vec(); u256_to_field(U256::from_big_endian(hash.as_slice())) diff --git a/src/loader/halo2/accumulation.rs b/src/loader/halo2/accumulation.rs index 4c9217c0..cc78ab83 100644 --- a/src/loader/halo2/accumulation.rs +++ b/src/loader/halo2/accumulation.rs @@ -5,7 +5,7 @@ use crate::{ }, protocol::Protocol, scheme::kzg::{AccumulationStrategy, Accumulator, SameCurveAccumulation, MSM}, - util::Transcript, + util::{Itertools, Transcript}, Error, }; use halo2_curves::CurveAffine; @@ -57,7 +57,7 @@ where let assinged = indices .iter() .map(|index| statements[index.0][index.1].assigned()) - .collect::>(); + .collect_vec(); let lhs = loader.assign_ec_point_from_limbs( assinged[..LIMBS].to_vec().try_into().unwrap(), assinged[LIMBS..2 * LIMBS].to_vec().try_into().unwrap(), @@ -68,7 +68,7 @@ where ); Accumulator::new(MSM::base(lhs), MSM::base(rhs)) }) - .collect::>(); + .collect_vec(); Some(Accumulator::random_linear_combine( challenges.into_iter().zip(accumulators), diff --git a/src/loader/halo2/loader.rs b/src/loader/halo2/loader.rs index 775e891b..e811debb 100644 --- a/src/loader/halo2/loader.rs +++ b/src/loader/halo2/loader.rs @@ -1,6 +1,6 @@ use crate::{ loader::{EcPointLoader, LoadedEcPoint, LoadedScalar, Loader, ScalarLoader}, - util::{Curve, Field, FieldOps, Group}, + util::{Curve, Field, FieldOps, Group, Itertools}, }; use halo2_curves::CurveAffine; use halo2_proofs::circuit; @@ -550,7 +550,7 @@ impl<'a, 'b, C: CurveAffine, const LIMBS: usize, const BITS: usize> LoadedEcPoin fn multi_scalar_multiplication( pairs: impl IntoIterator, Self)>, ) -> Self { - let pairs = pairs.into_iter().collect::>(); + let pairs = pairs.into_iter().collect_vec(); let loader = &pairs[0].0.loader; let (non_scaled, scaled) = pairs.iter().fold( diff --git a/src/loader/native/accumulation.rs b/src/loader/native/accumulation.rs index a51cf8d5..d0b67dc9 100644 --- a/src/loader/native/accumulation.rs +++ b/src/loader/native/accumulation.rs @@ -2,7 +2,7 @@ use crate::{ loader::native::NativeLoader, protocol::Protocol, scheme::kzg::{AccumulationStrategy, Accumulator, SameCurveAccumulation, MSM}, - util::{fe_from_limbs, Curve, Group, PrimeCurveAffine, Transcript}, + util::{fe_from_limbs, Curve, Group, Itertools, PrimeCurveAffine, Transcript}, Error, }; use halo2_curves::{ @@ -70,12 +70,12 @@ where indices .iter() .map(|index| statements[index.0][index.1]) - .collect::>() + .collect_vec() .try_into() .unwrap(), ) }) - .collect::>() + .collect_vec() .try_into() .unwrap(); let lhs = ::from_xy(lhs_x, lhs_y) @@ -86,7 +86,7 @@ where .to_curve(); Accumulator::new(MSM::base(lhs), MSM::base(rhs)) }) - .collect::>(); + .collect_vec(); Some(Accumulator::random_linear_combine( challenges.into_iter().zip(accumulators), diff --git a/src/protocol.rs b/src/protocol.rs index e69e45e9..af591b78 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -1,4 +1,4 @@ -use crate::util::{Curve, Domain, Expression, Group, Query}; +use crate::util::{Curve, Domain, Expression, Group, Itertools, Query}; #[cfg(feature = "halo2")] pub mod halo2; @@ -43,7 +43,7 @@ impl Snark { statements .iter() .map(|statements| statements.len()) - .collect::>() + .collect_vec() ); Snark { protocol, diff --git a/src/protocol/halo2.rs b/src/protocol/halo2.rs index be15c3a4..6c30bf5d 100644 --- a/src/protocol/halo2.rs +++ b/src/protocol/halo2.rs @@ -1,6 +1,6 @@ use crate::{ protocol::Protocol, - util::{CommonPolynomial, Domain, Expression, Query, Rotation}, + util::{CommonPolynomial, Domain, Expression, Itertools, Query, Rotation}, }; use halo2_proofs::{ arithmetic::{CurveAffine, CurveExt, FieldExt}, @@ -155,7 +155,7 @@ impl<'a, F: FieldExt> Polynomials<'a, F> { state[*phase as usize] += 1; Some(index) }) - .collect::>(); + .collect_vec(); (num, index) }; @@ -325,7 +325,7 @@ impl<'a, F: FieldExt> Polynomials<'a, F> { Some(Query::new(z, self.rotation_last())) }) }) - .collect::>(), + .collect_vec(), (true, false) => iter::empty() .chain((0..self.num_permutation_z).flat_map(move |i| { let z = self.permutation_poly(t, i); @@ -335,13 +335,13 @@ impl<'a, F: FieldExt> Polynomials<'a, F> { let z = self.permutation_poly(t, i); Query::new(z, self.rotation_last()) })) - .collect::>(), + .collect_vec(), (false, _) => (0..self.num_permutation_z) .flat_map(move |i| { let z = self.permutation_poly(t, i); [Query::new(z, 0), Query::new(z, 1)] }) - .collect::>(), + .collect_vec(), } } @@ -499,11 +499,11 @@ impl<'a, F: FieldExt> Polynomials<'a, F> { .iter() .map(|column| self.query(*column.column_type(), column.index(), 0, t)) .map(Expression::::Polynomial) - .collect::>(); + .collect_vec(); let permutation_fixeds = (0..self.num_permutation_fixed) .map(|i| Query::new(self.num_fixed + i, 0)) .map(Expression::::Polynomial) - .collect::>(); + .collect_vec(); let zs = (0..self.num_permutation_z) .map(|i| { let z = self.permutation_poly(t, i); @@ -513,7 +513,7 @@ impl<'a, F: FieldExt> Polynomials<'a, F> { Expression::::Polynomial(Query::new(z, self.rotation_last())), ) }) - .collect::>(); + .collect_vec(); iter::empty() .chain(zs.first().map(|(z_0, _, _)| l_0 * (one - z_0))) @@ -526,7 +526,7 @@ impl<'a, F: FieldExt> Polynomials<'a, F> { .skip(1) .zip(zs.iter()) .map(|((z, _, _), (_, _, z_prev_last))| l_0 * (z - z_prev_last)) - .collect::>() + .collect_vec() } else { Vec::new() }) @@ -572,7 +572,7 @@ impl<'a, F: FieldExt> Polynomials<'a, F> { }, ), ) - .collect::>() + .collect_vec() } fn lookup_relations(&'a self, t: usize) -> impl IntoIterator> + 'a { @@ -595,7 +595,7 @@ impl<'a, F: FieldExt> Polynomials<'a, F> { Expression::::Polynomial(Query::new(permuted_table, 0)), ) }) - .collect::>(); + .collect_vec(); let compress = |expressions: &'a [plonk::Expression]| { expressions @@ -639,7 +639,7 @@ impl<'a, F: FieldExt> Polynomials<'a, F> { })) }, ) - .collect::>() + .collect_vec() } fn accumulator_indices( diff --git a/src/protocol/halo2/test/circuit/maingate.rs b/src/protocol/halo2/test/circuit/maingate.rs index 28ec3855..b03a3ff4 100644 --- a/src/protocol/halo2/test/circuit/maingate.rs +++ b/src/protocol/halo2/test/circuit/maingate.rs @@ -1,4 +1,4 @@ -use crate::protocol::halo2::test::circuit::plookup::PlookupConfig; +use crate::{protocol::halo2::test::circuit::plookup::PlookupConfig, util::Itertools}; use halo2_proofs::{ arithmetic::FieldExt, circuit::{floor_planner::V1, Chip, Layouter, Value}, @@ -13,10 +13,7 @@ use halo2_wrong_ecc::{ EccConfig, }; use rand::RngCore; -use std::{ - collections::{BTreeMap, BTreeSet}, - iter, -}; +use std::{collections::BTreeMap, iter}; #[derive(Clone)] pub struct MainGateWithRangeConfig { @@ -166,8 +163,8 @@ impl PlookupRangeChip { ); let bits = bits .into_iter() - .collect::>() - .into_iter() + .sorted() + .dedup() .enumerate() .map(|(tag, bit)| (bit, tag)) .collect(); @@ -228,7 +225,7 @@ impl RangeInstructions for PlookupRangeChip { .into_iter() .zip((0..num_limbs).map(|i| F::from(2).pow(&[(limb_bit * i) as u64, 0, 0, 0]))) .map(|(limb, base)| Term::Unassigned(limb, base)) - .collect::>(); + .collect_vec(); self.main_gate .decompose(ctx, &terms, F::zero(), |ctx, is_last| { diff --git a/src/protocol/halo2/test/circuit/plookup.rs b/src/protocol/halo2/test/circuit/plookup.rs index c40b1b47..4e05e076 100644 --- a/src/protocol/halo2/test/circuit/plookup.rs +++ b/src/protocol/halo2/test/circuit/plookup.rs @@ -1,4 +1,4 @@ -use crate::util::{BatchInvert, Field}; +use crate::util::{BatchInvert, EitherOrBoth, Field, Itertools}; use halo2_proofs::{ arithmetic::FieldExt, circuit::{Layouter, Value}, @@ -8,7 +8,6 @@ use halo2_proofs::{ }, poly::Rotation, }; -use itertools::{EitherOrBoth, Itertools}; use std::{collections::BTreeMap, convert::TryFrom, iter, ops::Mul}; fn query( @@ -201,7 +200,7 @@ impl ShuffleConfig { let l_0 = l_0.unwrap_or_else(|| meta.selector()); let zs = iter::repeat_with(|| advice_column_in(meta, z_phase)) .take(num_z) - .collect::>(); + .collect_vec(); let collect_contribution = |value_with_gamma: Vec<(Expression, Expression)>, coeff: Option>, @@ -215,7 +214,7 @@ impl ShuffleConfig { .reduce(|acc, expr| acc * expr) .unwrap() }) - .collect::>(); + .collect_vec(); if let Some(coeff) = coeff { contribution[0] = coeff * contribution[0].clone(); @@ -232,7 +231,7 @@ impl ShuffleConfig { .chain(zs.iter().cloned().zip(iter::repeat(Rotation::cur()))) .chain(Some((zs[0], Rotation::next()))) .map(|(z, at)| meta.query_advice(z, at)) - .collect::>(); + .collect_vec(); let one = Expression::Constant(F::one()); let z_0 = zs[0].clone(); @@ -297,7 +296,7 @@ impl ShuffleConfig { EitherOrBoth::Left(value) | EitherOrBoth::Right(value) => value, EitherOrBoth::Both(lhs, rhs) => lhs * rhs, }) - .collect::>(); + .collect_vec(); let mut z = vec![F::one()]; for i in 0..n { @@ -437,7 +436,7 @@ impl PlookupConfig { }; let mixes = iter::repeat_with(|| advice_column_in(meta, mixes_phase)) .take(t + 1) - .collect::>(); + .collect_vec(); let [beta, gamma] = [beta, gamma].map(|challenge| match challenge { Some(challenge) => { assert!(challenge.phase() >= mixes_phase); @@ -463,7 +462,7 @@ impl PlookupConfig { .reduce(|acc, expr| acc * theta.clone().unwrap() + expr) .unwrap() }) - .collect::>(); + .collect_vec(); let compressed_table = table .iter() .cloned() @@ -495,7 +494,7 @@ impl PlookupConfig { .chain(mixes.iter().cloned().zip(iter::repeat(Rotation::cur()))) .chain(Some((mixes[0], Rotation::next()))) .map(|(column, at)| meta.query_advice(column, at)) - .collect::>(); + .collect_vec(); let [beta, gamma] = [beta, gamma].map(|challenge| meta.query_challenge(challenge)); let one = Expression::Constant(F::one()); let gamma_prime = (one + beta.clone()) * gamma; @@ -586,6 +585,7 @@ impl PlookupConfig { #[cfg(test)] mod test { use super::{PlookupConfig, ShuffleConfig}; + use crate::util::Itertools; use halo2_curves::{bn256::Fr, FieldExt}; use halo2_proofs::{ circuit::{floor_planner::V1, Layouter, Value}, @@ -628,12 +628,12 @@ mod test { let rng = &mut rng; iter::repeat_with(|| F::random(&mut *rng)) .take(n) - .collect::>() + .collect_vec() }); let rhs = shuffled( lhs.iter() .map(|lhs| lhs.iter().map(F::square).collect()) - .collect::>() + .collect_vec() .try_into() .unwrap(), rng, @@ -730,7 +730,7 @@ mod test { let m = rng.next_u32() as usize % n; let mut table = iter::repeat_with(|| [(); W].map(|_| F::random(&mut rng))) .take(m) - .collect::>(); + .collect_vec(); table.extend( iter::repeat( table @@ -846,7 +846,7 @@ mod test { } => (constraint, location), _ => panic!("MockProver::verify has unexpected failure"), }) - .collect::>(), + .collect_vec(), failures ) } diff --git a/src/protocol/halo2/test/kzg.rs b/src/protocol/halo2/test/kzg.rs index ae2f51b1..771e1a70 100644 --- a/src/protocol/halo2/test/kzg.rs +++ b/src/protocol/halo2/test/kzg.rs @@ -88,20 +88,20 @@ macro_rules! halo2_kzg_config { #[macro_export] macro_rules! halo2_kzg_prepare { ($k:expr, $config:expr, $create_circuit:expr) => {{ + use $crate::{ + protocol::halo2::{compile, test::kzg::read_or_create_srs}, + util::{GroupEncoding, Itertools}, + }; use halo2_curves::bn256::{Bn256, G1}; use halo2_proofs::{ plonk::{keygen_pk, keygen_vk}, poly::kzg::commitment::KZGCommitmentScheme, }; - use std::{collections::BTreeSet, iter}; - use $crate::{ - protocol::halo2::{compile, test::kzg::read_or_create_srs}, - util::GroupEncoding, - }; + use std::{iter}; let circuits = iter::repeat_with(|| $create_circuit) .take($config.num_proof) - .collect::>(); + .collect_vec(); let params = read_or_create_srs::($k); let pk = if $config.zk { @@ -121,9 +121,9 @@ macro_rules! halo2_kzg_prepare { protocol.preprocessed.len(), protocol.preprocessed .iter() - .map(|ec_point| ec_point.to_bytes().as_ref().to_vec().try_into().unwrap()) - .collect::>() - .len(), + .map(|ec_point| <[u8; 32]>::try_from(ec_point.to_bytes().as_ref().to_vec()).unwrap()) + .unique() + .count() ); (params, pk, protocol, circuits) @@ -138,12 +138,13 @@ macro_rules! halo2_kzg_create_snark { use $crate::{ collect_slice, protocol::{halo2::test::create_proof_checked, Snark}, + util::Itertools, }; let instances = $circuits .iter() .map(|circuit| circuit.instances()) - .collect::>(); + .collect_vec(); let proof = { collect_slice!(instances, 2); #[allow(clippy::needless_borrow)] @@ -190,7 +191,7 @@ macro_rules! halo2_kzg_create_snark { Snark::new( $protocol.clone(), - instances.into_iter().flatten().collect::>(), + instances.into_iter().flatten().collect_vec(), proof, ) }}; diff --git a/src/protocol/halo2/test/kzg/evm.rs b/src/protocol/halo2/test/kzg/evm.rs index e4441a54..6caf0e68 100644 --- a/src/protocol/halo2/test/kzg/evm.rs +++ b/src/protocol/halo2/test/kzg/evm.rs @@ -31,7 +31,7 @@ macro_rules! halo2_kzg_evm_verify { loader::evm::{encode_calldata, execute, EvmLoader, EvmTranscript}, protocol::halo2::test::kzg::{BITS, LIMBS}, scheme::kzg::{AccumulationScheme, SameCurveAccumulation}, - util::TranscriptRead, + util::{Itertools, TranscriptRead}, }; let loader = EvmLoader::new::(); @@ -41,9 +41,9 @@ macro_rules! halo2_kzg_evm_verify { .map(|instance| { iter::repeat_with(|| transcript.read_scalar().unwrap()) .take(instance.len()) - .collect::>() + .collect_vec() }) - .collect::>(); + .collect_vec(); let mut strategy = SameCurveAccumulation::<_, _, LIMBS, BITS>::default(); <$scheme>::accumulate( $protocol, diff --git a/src/protocol/halo2/test/kzg/halo2.rs b/src/protocol/halo2/test/kzg/halo2.rs index b52092ce..cd8e884b 100644 --- a/src/protocol/halo2/test/kzg/halo2.rs +++ b/src/protocol/halo2/test/kzg/halo2.rs @@ -13,7 +13,7 @@ use crate::{ Protocol, Snark, }, scheme::kzg::{self, AccumulationScheme, ShplonkAccumulationScheme}, - util::{fe_to_limbs, Curve, Group, PrimeCurveAffine}, + util::{fe_to_limbs, Curve, Group, Itertools, PrimeCurveAffine}, }; use halo2_curves::bn256::{Fr, G1Affine, G1}; use halo2_proofs::{ @@ -59,7 +59,7 @@ impl From> for SnarkWitness { statements: snark .statements .into_iter() - .map(|statements| statements.into_iter().map(Value::known).collect::>()) + .map(|statements| statements.into_iter().map(Value::known).collect_vec()) .collect(), proof: Value::known(snark.proof), } @@ -96,9 +96,9 @@ pub fn accumulate<'a, 'b>( statements .iter() .map(|statement| loader.assign_scalar(*statement)) - .collect::>() + .collect_vec() }) - .collect::>(); + .collect_vec(); ShplonkAccumulationScheme::accumulate( &snark.protocol, loader, diff --git a/src/protocol/halo2/util.rs b/src/protocol/halo2/util.rs index d876e58d..437d59cb 100644 --- a/src/protocol/halo2/util.rs +++ b/src/protocol/halo2/util.rs @@ -1,6 +1,9 @@ use crate::{ loader::native::NativeLoader, - util::{Curve, PrimeCurveAffine, PrimeField, Transcript, TranscriptRead, UncompressedEncoding}, + util::{ + Curve, Itertools, PrimeCurveAffine, PrimeField, Transcript, TranscriptRead, + UncompressedEncoding, + }, Error, }; use halo2_proofs::{ @@ -26,7 +29,7 @@ where .chain(coordinates.x().to_repr().as_ref()) .chain(coordinates.y().to_repr().as_ref()) .cloned() - .collect::>() + .collect_vec() .try_into() .unwrap() } diff --git a/src/scheme/kzg/accumulation/plonk.rs b/src/scheme/kzg/accumulation/plonk.rs index 96c6ad00..10c91eed 100644 --- a/src/scheme/kzg/accumulation/plonk.rs +++ b/src/scheme/kzg/accumulation/plonk.rs @@ -8,8 +8,8 @@ use crate::{ msm::MSM, }, util::{ - CommonPolynomial, CommonPolynomialEvaluation, Curve, Expression, Field, Query, Rotation, - TranscriptRead, + CommonPolynomial, CommonPolynomialEvaluation, Curve, Expression, Field, Itertools, Query, + Rotation, TranscriptRead, }, Error, }; @@ -81,7 +81,7 @@ where .iter() .zip(powers_of_u.iter()) .map(|(w, power_of_u)| MSM::base(w.clone()) * power_of_u) - .collect::>(); + .collect_vec(); let lhs = f + rhs .iter() .zip(z_omegas) @@ -119,7 +119,7 @@ impl> PlonkProof { != statements .iter() .map(|statements| statements.len()) - .collect::>() + .collect_vec() { return Err(Error::InvalidInstances); } @@ -145,8 +145,8 @@ impl> PlonkProof { .unzip::<_, _, Vec<_>, Vec<_>>(); ( - auxiliaries.into_iter().flatten().collect::>(), - challenges.into_iter().flatten().collect::>(), + auxiliaries.into_iter().flatten().collect_vec(), + challenges.into_iter().flatten().collect_vec(), ) }; @@ -231,7 +231,7 @@ impl> PlonkProof { .map(|(i, statement)| { common_poly_eval.get(CommonPolynomial::Lagrange(i as i32)) * statement }) - .collect::>(), + .collect_vec(), ) }); let mut evaluations = HashMap::::from_iter( diff --git a/src/scheme/kzg/accumulation/shplonk.rs b/src/scheme/kzg/accumulation/shplonk.rs index ac716a56..e9c31aa8 100644 --- a/src/scheme/kzg/accumulation/shplonk.rs +++ b/src/scheme/kzg/accumulation/shplonk.rs @@ -9,7 +9,7 @@ use crate::{ }, util::{ CommonPolynomial, CommonPolynomialEvaluation, Curve, Domain, Expression, Field, Fraction, - Query, Rotation, TranscriptRead, + Itertools, Query, Rotation, TranscriptRead, }, Error, }; @@ -115,7 +115,7 @@ impl> ShplonkProof { != statements .iter() .map(|statements| statements.len()) - .collect::>() + .collect_vec() { return Err(Error::InvalidInstances); } @@ -141,8 +141,8 @@ impl> ShplonkProof { .unzip::<_, _, Vec<_>, Vec<_>>(); ( - auxiliaries.into_iter().flatten().collect::>(), - challenges.into_iter().flatten().collect::>(), + auxiliaries.into_iter().flatten().collect_vec(), + challenges.into_iter().flatten().collect_vec(), ) }; @@ -232,7 +232,7 @@ impl> ShplonkProof { statement.clone() * common_poly_eval.get(CommonPolynomial::Lagrange(i as i32)) }) - .collect::>(), + .collect_vec(), ) }); let mut evaluations = HashMap::::from_iter( @@ -327,7 +327,7 @@ impl> IntermediateSet { let omegas = rotations .iter() .map(|rotation| domain.rotate_scalar(C::Scalar::one(), *rotation)) - .collect::>(); + .collect_vec(); let normalized_ell_primes = omegas .iter() @@ -341,7 +341,7 @@ impl> IntermediateSet { acc * (*omega_j - omega_i) }) }) - .collect::>(); + .collect_vec(); let z = &powers_of_z[1].clone(); let z_pow_k_minus_one = { @@ -379,7 +379,7 @@ impl> IntermediateSet { ) }) .map(Fraction::one_over) - .collect::>(); + .collect_vec(); let z_s = rotations .iter() @@ -404,14 +404,14 @@ impl> IntermediateSet { .iter_mut() .chain(self.commitment_coeff.as_mut()) .filter_map(Fraction::denom_mut) - .collect::>() + .collect_vec() } else if self.remainder_coeff.is_none() { let barycentric_weights_sum = L::LoadedScalar::sum( &self .evaluation_coeffs .iter() .map(Fraction::evaluate) - .collect::>(), + .collect_vec(), ); self.remainder_coeff = Some(match self.commitment_coeff.clone() { Some(coeff) => Fraction::new(coeff.evaluate(), barycentric_weights_sum), @@ -455,7 +455,7 @@ impl> IntermediateSet { }) .unwrap() }) - .collect::>(), + .collect_vec(), ); (commitment - MSM::scalar(remainder)) * power_of_mu }) @@ -472,9 +472,9 @@ fn intermediate_sets>( let rotations_sets = rotations_sets(protocol); let superset = rotations_sets .iter() - .flat_map(|set| set.rotations.iter()) - .cloned() - .collect::>(); + .flat_map(|set| set.rotations.clone()) + .sorted() + .dedup(); let size = 2.max( (rotations_sets @@ -490,7 +490,6 @@ fn intermediate_sets>( let powers_of_z = z.powers(size); let z_prime_minus_z_omega_i = HashMap::from_iter( superset - .into_iter() .map(|rotation| { ( rotation, diff --git a/src/util.rs b/src/util.rs index 34021b47..eb38e56a 100644 --- a/src/util.rs +++ b/src/util.rs @@ -10,19 +10,25 @@ pub use arithmetic::{ pub use expression::{CommonPolynomial, CommonPolynomialEvaluation, Expression, Query}; pub use transcript::{Transcript, TranscriptRead}; +pub use itertools::{EitherOrBoth, Itertools}; + #[macro_export] macro_rules! collect_slice { ($vec:ident) => { - let $vec = $vec.iter().map(|vec| vec.as_slice()).collect::>(); + use $crate::util::Itertools; + + let $vec = $vec.iter().map(|vec| vec.as_slice()).collect_vec(); }; ($vec:ident, 2) => { + use $crate::util::Itertools; + let $vec = $vec .iter() .map(|vec| { collect_slice!(vec); vec }) - .collect::>(); - let $vec = $vec.iter().map(|vec| vec.as_slice()).collect::>(); + .collect_vec(); + let $vec = $vec.iter().map(|vec| vec.as_slice()).collect_vec(); }; } diff --git a/src/util/arithmetic.rs b/src/util/arithmetic.rs index 6bfc5bc0..0ba1929b 100644 --- a/src/util/arithmetic.rs +++ b/src/util/arithmetic.rs @@ -1,3 +1,4 @@ +use crate::util::Itertools; use num_bigint::BigUint; use num_traits::One; use std::{ @@ -57,7 +58,7 @@ pub fn batch_invert_and_mul(values: &mut [F], coeff: &F) { *acc *= value; Some(*acc) }) - .collect::>(); + .collect_vec(); let mut all_product_inv = products.last().unwrap().invert().unwrap() * coeff; @@ -234,7 +235,7 @@ pub fn fe_to_limbs> shift) & &mask)) - .collect::>() + .collect_vec() .try_into() .unwrap() } diff --git a/src/util/expression.rs b/src/util/expression.rs index 912edf55..41b04ded 100644 --- a/src/util/expression.rs +++ b/src/util/expression.rs @@ -1,6 +1,6 @@ use crate::{ loader::{LoadedScalar, Loader}, - util::{Curve, Domain, Field, Fraction, Rotation}, + util::{Curve, Domain, Field, Fraction, Itertools, Rotation}, }; use std::{ cmp::max, @@ -40,9 +40,7 @@ where z: &L::LoadedScalar, ) -> Self { let zn = z.pow_const(domain.n as u64); - let langranges = BTreeSet::::from_iter(langranges) - .into_iter() - .collect::>(); + let langranges = langranges.into_iter().sorted().dedup().collect_vec(); let one = loader.load_one(); let zn_minus_one = zn.clone() - one; @@ -52,12 +50,12 @@ where let omegas = langranges .iter() .map(|&i| loader.load_const(&domain.rotate_scalar(C::Scalar::one(), Rotation(i)))) - .collect::>(); + .collect_vec(); let lagrange_evals = omegas .iter() .map(|omega| Fraction::new(numer.clone() * omega, z.clone() - omega)) - .collect::>(); + .collect_vec(); Self { zn,