Skip to content

Commit

Permalink
Small perf items (#12)
Browse files Browse the repository at this point in the history
* Optimize generator creation in provider module

- Refactored the `from_label` function in pasta.rs removing redundant collect, flatten operations and Vec<usize> creation,

* fix: rename VanillaRO -> NativeRO

* refactor: Optimize circuit methods and enhance trait compatibility

- Replaced the `.map()` function with the `.map_or()` function in `r1cs.rs` for efficiency.
- Added `Eq` trait derivation to `NovaAugmentedCircuitParams` in `circuit.rs` to enable comparisons.
- Introduced `Eq` trait to `TrivialTestCircuit` struct in `traits/circuit.rs` without altering its input or output.
- Optimized performance by refactoring the use of iterator `into_iter()` to the iterator itself in `gadgets/r1cs.rs`.
  • Loading branch information
huitseeker authored Aug 23, 2023
1 parent 2b9930b commit 045206f
Show file tree
Hide file tree
Showing 9 changed files with 10 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use bellpepper_core::{
use ff::Field;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Abomonation)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Abomonation)]
pub struct NovaAugmentedCircuitParams {
limb_width: usize,
n_limbs: usize,
Expand Down
4 changes: 2 additions & 2 deletions src/gadgets/r1cs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ impl<G: Group> AllocatedRelaxedR1CSInstance<G> {
.collect::<Result<Vec<AllocatedNum<G::Base>>, _>>()?;

// absorb each of the limbs of X[0]
for limb in X0_bn.into_iter() {
for limb in X0_bn {
ro.absorb(&limb);
}

Expand All @@ -224,7 +224,7 @@ impl<G: Group> AllocatedRelaxedR1CSInstance<G> {
.collect::<Result<Vec<AllocatedNum<G::Base>>, _>>()?;

// absorb each of the limbs of X[1]
for limb in X1_bn.into_iter() {
for limb in X1_bn {
ro.absorb(&limb);
}

Expand Down
7 changes: 1 addition & 6 deletions src/provider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ macro_rules! impl_traits {
uniform_bytes_vec.push(uniform_bytes);
}
let gens_proj: Vec<$name_curve> = (0..n)
.collect::<Vec<usize>>()
.into_par_iter()
.map(|i| {
let hash = $name_curve::hash_to_curve("from_uniform_bytes");
Expand All @@ -202,9 +201,8 @@ macro_rules! impl_traits {
if gens_proj.len() > num_threads {
let chunk = (gens_proj.len() as f64 / num_threads as f64).ceil() as usize;
(0..num_threads)
.collect::<Vec<usize>>()
.into_par_iter()
.map(|i| {
.flat_map(|i| {
let start = i * chunk;
let end = if i == num_threads - 1 {
gens_proj.len()
Expand All @@ -219,9 +217,6 @@ macro_rules! impl_traits {
vec![]
}
})
.collect::<Vec<Vec<$name_curve_affine>>>()
.into_par_iter()
.flatten()
.collect()
} else {
let mut gens = vec![$name_curve_affine::identity(); n];
Expand Down
7 changes: 1 addition & 6 deletions src/provider/pasta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ macro_rules! impl_traits {
uniform_bytes_vec.push(uniform_bytes);
}
let ck_proj: Vec<$name_curve> = (0..n)
.collect::<Vec<usize>>()
.into_par_iter()
.map(|i| {
let hash = $name_curve::hash_to_curve("from_uniform_bytes");
Expand All @@ -118,9 +117,8 @@ macro_rules! impl_traits {
if ck_proj.len() > num_threads {
let chunk = (ck_proj.len() as f64 / num_threads as f64).ceil() as usize;
(0..num_threads)
.collect::<Vec<usize>>()
.into_par_iter()
.map(|i| {
.flat_map(|i| {
let start = i * chunk;
let end = if i == num_threads - 1 {
ck_proj.len()
Expand All @@ -135,9 +133,6 @@ macro_rules! impl_traits {
vec![]
}
})
.collect::<Vec<Vec<$name_curve_affine>>>()
.into_par_iter()
.flatten()
.collect()
} else {
let mut ck = vec![$name_curve_affine::identity(); n];
Expand Down
2 changes: 1 addition & 1 deletion src/provider/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ where
Scalar: PrimeField + PrimeFieldBits + Serialize + for<'de> Deserialize<'de>,
Scalar::Repr: Abomonation,
{
type VanillaRO<T: PrimeField> = PoseidonRO<Scalar, T>;
type NativeRO<T: PrimeField> = PoseidonRO<Scalar, T>;
type Constants = PoseidonConstantsCircuit<Scalar>;

/// Initialize the internal state and set the poseidon constants
Expand Down
2 changes: 1 addition & 1 deletion src/r1cs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub fn commitment_key_size<G: Group>(
) -> usize {
let num_cons = S.num_cons;
let num_vars = S.num_vars;
let generators_hint = commitment_key_floor.map(|f| f(S)).unwrap_or(0);
let generators_hint = commitment_key_floor.map_or(0, |f| f(S));
max(max(num_cons, num_vars), generators_hint)
}

Expand Down
2 changes: 1 addition & 1 deletion src/spartan/ppsnark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1839,7 +1839,7 @@ where
// finish the final step of the sum-check
let (claim_init_expected_row, claim_audit_expected_row) = {
let addr = IdentityPolynomial::new(r_prod.len()).evaluate(&r_prod);
let val = EqPolynomial::new(tau.to_vec()).evaluate(&r_prod);
let val = EqPolynomial::new(tau).evaluate(&r_prod);
(
hash_func(&addr, &val, &G::Scalar::ZERO),
hash_func(&addr, &val, &self.eval_row_audit_ts),
Expand Down
2 changes: 1 addition & 1 deletion src/traits/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub trait StepCircuit<F: PrimeField>: Send + Sync + Clone {
}

/// A trivial step circuit that simply returns the input
#[derive(Clone, Debug, Default, PartialEq)]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct TrivialTestCircuit<F: PrimeField> {
_p: PhantomData<F>,
}
Expand Down
2 changes: 1 addition & 1 deletion src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub trait ROTrait<Base: PrimeField, Scalar> {
/// A helper trait that defines the behavior of a hash function that we use as an RO in the circuit model
pub trait ROCircuitTrait<Base: PrimeField> {
/// the vanilla alter ego of this trait - this constrains it to use the same constants
type VanillaRO<T: PrimeField>: ROTrait<Base, T, Constants = Self::Constants>;
type NativeRO<T: PrimeField>: ROTrait<Base, T, Constants = Self::Constants>;

/// A type representing constants/parameters associated with the hash function on this Base field
type Constants: Default
Expand Down

0 comments on commit 045206f

Please sign in to comment.