Skip to content

Commit

Permalink
refactor: use Itertools methods
Browse files Browse the repository at this point in the history
  • Loading branch information
han0110 committed Jul 23, 2022
1 parent 6358c0c commit 5c19b64
Show file tree
Hide file tree
Showing 23 changed files with 128 additions and 122 deletions.
6 changes: 3 additions & 3 deletions src/loader.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -76,7 +76,7 @@ pub trait LoadedScalar<F: PrimeField>: Clone + Debug + FieldOps {
&values
.iter()
.map(|value| (F::one(), value.clone()))
.collect::<Vec<_>>(),
.collect_vec(),
constant,
)
}
Expand Down Expand Up @@ -129,7 +129,7 @@ pub trait LoadedScalar<F: PrimeField>: Clone + Debug + FieldOps {
iter::successors(Some(self.clone()), |power| Some(power.clone() * self))
.take(n - 1),
)
.collect::<Vec<_>>()
.collect_vec()
}
}

Expand Down
20 changes: 10 additions & 10 deletions src/loader/evm.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -46,15 +49,12 @@ where
F: PrimeField<Repr = [u8; 32]>,
{
iter::empty()
.chain(instances.into_iter().flatten().flat_map(|value| {
value
.to_repr()
.as_ref()
.iter()
.rev()
.cloned()
.collect::<Vec<_>>()
}))
.chain(
instances
.into_iter()
.flatten()
.flat_map(|value| value.to_repr().as_ref().iter().rev().cloned().collect_vec()),
)
.chain(proof)
.collect()
}
Expand Down
6 changes: 3 additions & 3 deletions src/loader/evm/accumulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,7 +56,7 @@ where
let num_statements = statements
.iter()
.map(|statements| statements.len())
.collect::<Vec<_>>();
.collect_vec();

let challenges = transcript.squeeze_n_challenges(accumulator_indices.len());
let accumulators = accumulator_indices
Expand All @@ -73,7 +73,7 @@ where
let rhs = loader.calldataload_ec_point_from_limbs::<LIMBS, BITS>(offset + 0x100);
Accumulator::new(MSM::base(lhs), MSM::base(rhs))
})
.collect::<Vec<_>>();
.collect_vec();

Some(Accumulator::random_linear_combine(
challenges.into_iter().zip(accumulators),
Expand Down
3 changes: 2 additions & 1 deletion src/loader/evm/code.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::util::Itertools;
use ethereum_types::U256;
use foundry_evm::{revm::opcode::*, HashMap};

Expand All @@ -22,7 +23,7 @@ impl Code {
constants: HashMap::new(),
stack_len: 0,
};
let constants = constants.into_iter().collect::<Vec<_>>();
let constants = constants.into_iter().collect_vec();
for constant in constants.iter() {
code.push(*constant);
}
Expand Down
8 changes: 4 additions & 4 deletions src/loader/evm/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -266,7 +266,7 @@ impl EvmLoader {
(ptr..ptr + len)
.step_by(0x20)
.map(|ptr| self.dup_scalar(&self.scalar(Value::Memory(ptr))))
.collect::<Vec<_>>()
.collect_vec()
.first()
.unwrap()
.ptr()
Expand Down Expand Up @@ -816,15 +816,15 @@ impl<F: PrimeField<Repr = [u8; 0x20]>> LoadedScalar<F> for Scalar {
}

fn batch_invert<'a>(values: impl IntoIterator<Item = &'a mut Self>) {
let values = values.into_iter().collect::<Vec<_>>();
let values = values.into_iter().collect_vec();
let loader = &values.first().unwrap().loader;
let products = iter::once(values[0].clone())
.chain(
iter::repeat_with(|| loader.allocate(0x20))
.map(|ptr| loader.scalar(Value::Memory(ptr)))
.take(values.len() - 1),
)
.collect::<Vec<_>>();
.collect_vec();

loader.code.borrow_mut().push(loader.scalar_modulus);
for _ in 2..values.len() {
Expand Down
4 changes: 2 additions & 2 deletions src/loader/evm/test.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -44,7 +44,7 @@ pub fn execute(code: Vec<u8>, calldata: Vec<u8>) -> (bool, u64, Vec<u64>) {
.logs
.into_iter()
.map(|log| h256_to_u256_be(log.topics[0]).as_u64())
.collect::<Vec<_>>();
.collect_vec();

if debug {
Tui::new(result.debug.unwrap().flatten(0), 0).start();
Expand Down
4 changes: 2 additions & 2 deletions src/loader/evm/transcript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -153,7 +153,7 @@ where
} else {
None
})
.collect::<Vec<_>>();
.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()))
Expand Down
6 changes: 3 additions & 3 deletions src/loader/halo2/accumulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
},
protocol::Protocol,
scheme::kzg::{AccumulationStrategy, Accumulator, SameCurveAccumulation, MSM},
util::Transcript,
util::{Itertools, Transcript},
Error,
};
use halo2_curves::CurveAffine;
Expand Down Expand Up @@ -57,7 +57,7 @@ where
let assinged = indices
.iter()
.map(|index| statements[index.0][index.1].assigned())
.collect::<Vec<_>>();
.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(),
Expand All @@ -68,7 +68,7 @@ where
);
Accumulator::new(MSM::base(lhs), MSM::base(rhs))
})
.collect::<Vec<_>>();
.collect_vec();

Some(Accumulator::random_linear_combine(
challenges.into_iter().zip(accumulators),
Expand Down
4 changes: 2 additions & 2 deletions src/loader/halo2/loader.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -550,7 +550,7 @@ impl<'a, 'b, C: CurveAffine, const LIMBS: usize, const BITS: usize> LoadedEcPoin
fn multi_scalar_multiplication(
pairs: impl IntoIterator<Item = (Scalar<'a, 'b, C, LIMBS, BITS>, Self)>,
) -> Self {
let pairs = pairs.into_iter().collect::<Vec<_>>();
let pairs = pairs.into_iter().collect_vec();
let loader = &pairs[0].0.loader;

let (non_scaled, scaled) = pairs.iter().fold(
Expand Down
8 changes: 4 additions & 4 deletions src/loader/native/accumulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -70,12 +70,12 @@ where
indices
.iter()
.map(|index| statements[index.0][index.1])
.collect::<Vec<_>>()
.collect_vec()
.try_into()
.unwrap(),
)
})
.collect::<Vec<_>>()
.collect_vec()
.try_into()
.unwrap();
let lhs = <C::AffineExt as CurveAffine>::from_xy(lhs_x, lhs_y)
Expand All @@ -86,7 +86,7 @@ where
.to_curve();
Accumulator::new(MSM::base(lhs), MSM::base(rhs))
})
.collect::<Vec<_>>();
.collect_vec();

Some(Accumulator::random_linear_combine(
challenges.into_iter().zip(accumulators),
Expand Down
4 changes: 2 additions & 2 deletions src/protocol.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -43,7 +43,7 @@ impl<C: Curve> Snark<C> {
statements
.iter()
.map(|statements| statements.len())
.collect::<Vec<_>>()
.collect_vec()
);
Snark {
protocol,
Expand Down
24 changes: 12 additions & 12 deletions src/protocol/halo2.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -155,7 +155,7 @@ impl<'a, F: FieldExt> Polynomials<'a, F> {
state[*phase as usize] += 1;
Some(index)
})
.collect::<Vec<_>>();
.collect_vec();
(num, index)
};

Expand Down Expand Up @@ -325,7 +325,7 @@ impl<'a, F: FieldExt> Polynomials<'a, F> {
Some(Query::new(z, self.rotation_last()))
})
})
.collect::<Vec<_>>(),
.collect_vec(),
(true, false) => iter::empty()
.chain((0..self.num_permutation_z).flat_map(move |i| {
let z = self.permutation_poly(t, i);
Expand All @@ -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::<Vec<_>>(),
.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::<Vec<_>>(),
.collect_vec(),
}
}

Expand Down Expand Up @@ -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::<F>::Polynomial)
.collect::<Vec<_>>();
.collect_vec();
let permutation_fixeds = (0..self.num_permutation_fixed)
.map(|i| Query::new(self.num_fixed + i, 0))
.map(Expression::<F>::Polynomial)
.collect::<Vec<_>>();
.collect_vec();
let zs = (0..self.num_permutation_z)
.map(|i| {
let z = self.permutation_poly(t, i);
Expand All @@ -513,7 +513,7 @@ impl<'a, F: FieldExt> Polynomials<'a, F> {
Expression::<F>::Polynomial(Query::new(z, self.rotation_last())),
)
})
.collect::<Vec<_>>();
.collect_vec();

iter::empty()
.chain(zs.first().map(|(z_0, _, _)| l_0 * (one - z_0)))
Expand All @@ -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::<Vec<_>>()
.collect_vec()
} else {
Vec::new()
})
Expand Down Expand Up @@ -572,7 +572,7 @@ impl<'a, F: FieldExt> Polynomials<'a, F> {
},
),
)
.collect::<Vec<_>>()
.collect_vec()
}

fn lookup_relations(&'a self, t: usize) -> impl IntoIterator<Item = Expression<F>> + 'a {
Expand All @@ -595,7 +595,7 @@ impl<'a, F: FieldExt> Polynomials<'a, F> {
Expression::<F>::Polynomial(Query::new(permuted_table, 0)),
)
})
.collect::<Vec<_>>();
.collect_vec();

let compress = |expressions: &'a [plonk::Expression<F>]| {
expressions
Expand Down Expand Up @@ -639,7 +639,7 @@ impl<'a, F: FieldExt> Polynomials<'a, F> {
}))
},
)
.collect::<Vec<_>>()
.collect_vec()
}

fn accumulator_indices(
Expand Down
13 changes: 5 additions & 8 deletions src/protocol/halo2/test/circuit/maingate.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -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 {
Expand Down Expand Up @@ -166,8 +163,8 @@ impl<F: FieldExt> PlookupRangeChip<F> {
);
let bits = bits
.into_iter()
.collect::<BTreeSet<usize>>()
.into_iter()
.sorted()
.dedup()
.enumerate()
.map(|(tag, bit)| (bit, tag))
.collect();
Expand Down Expand Up @@ -228,7 +225,7 @@ impl<F: FieldExt> RangeInstructions<F> for PlookupRangeChip<F> {
.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::<Vec<_>>();
.collect_vec();

self.main_gate
.decompose(ctx, &terms, F::zero(), |ctx, is_last| {
Expand Down
Loading

0 comments on commit 5c19b64

Please sign in to comment.