Skip to content

Commit

Permalink
Cut duplicate code (#261)
Browse files Browse the repository at this point in the history
* cut duplicate code

* bound --> bind for clarity

* expose asm feature
  • Loading branch information
srinathsetty authored Nov 15, 2023
1 parent 8cb4bec commit bb27f98
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 48 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ harness = false

[features]
default = []
asm = ["halo2curves/asm"]
# Compiles in portable mode, w/o ISA extensions => binary can be executed on all systems.
portable = ["pasta-msm/portable"]
cuda = ["neptune/cuda", "neptune/pasta", "neptune/arity24"]
Expand Down
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -979,11 +979,25 @@ mod tests {
let trivial_circuit2_grumpkin = TrivialCircuit::<<grumpkin::Point as Group>::Scalar>::default();
let cubic_circuit1_grumpkin = CubicCircuit::<<bn256::Point as Group>::Scalar>::default();

#[cfg(feature = "asm")]
test_pp_digest_with::<bn256::Point, grumpkin::Point, _, _>(
&trivial_circuit1_grumpkin,
&trivial_circuit2_grumpkin,
"c4ecd363a6c1473de7e0d24fc1dbb660f563556e2e13fb4614acdff04cab7701",
);
#[cfg(feature = "asm")]
test_pp_digest_with::<bn256::Point, grumpkin::Point, _, _>(
&cubic_circuit1_grumpkin,
&trivial_circuit2_grumpkin,
"4853a6463b6309f6ae76442934d0a423f51f1e10abaddd0d39bf5644ed589100",
);
#[cfg(not(feature = "asm"))]
test_pp_digest_with::<bn256::Point, grumpkin::Point, _, _>(
&trivial_circuit1_grumpkin,
&trivial_circuit2_grumpkin,
"c26cc841d42c19bf98bc2482e66cd30903922f2a923927b85d66f375a821f101",
);
#[cfg(not(feature = "asm"))]
test_pp_digest_with::<bn256::Point, grumpkin::Point, _, _>(
&cubic_circuit1_grumpkin,
&trivial_circuit2_grumpkin,
Expand Down
12 changes: 0 additions & 12 deletions src/provider/bn256_grumpkin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,6 @@ pub mod grumpkin {
};
}

impl<G: Group> TranscriptReprTrait<G> for grumpkin::Base {
fn to_transcript_bytes(&self) -> Vec<u8> {
self.to_repr().to_vec()
}
}

impl<G: Group> TranscriptReprTrait<G> for grumpkin::Scalar {
fn to_transcript_bytes(&self) -> Vec<u8> {
self.to_repr().to_vec()
}
}

impl_traits!(
bn256,
Bn256Compressed,
Expand Down
6 changes: 6 additions & 0 deletions src/provider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,12 @@ macro_rules! impl_traits {
Some($name_curve::from_bytes(&self).unwrap())
}
}

impl<G: Group> TranscriptReprTrait<G> for $name::Scalar {
fn to_transcript_bytes(&self) -> Vec<u8> {
self.to_repr().to_vec()
}
}
};
}

Expand Down
18 changes: 6 additions & 12 deletions src/provider/pasta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,19 +187,13 @@ macro_rules! impl_traits {
Some($name_curve::from_bytes(&self.repr).unwrap())
}
}
};
}

impl<G: Group> TranscriptReprTrait<G> for pallas::Base {
fn to_transcript_bytes(&self) -> Vec<u8> {
self.to_repr().to_vec()
}
}

impl<G: Group> TranscriptReprTrait<G> for pallas::Scalar {
fn to_transcript_bytes(&self) -> Vec<u8> {
self.to_repr().to_vec()
}
impl<G: Group> TranscriptReprTrait<G> for $name::Scalar {
fn to_transcript_bytes(&self) -> Vec<u8> {
self.to_repr().to_vec()
}
}
};
}

impl_traits!(
Expand Down
12 changes: 0 additions & 12 deletions src/provider/secp_secq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,6 @@ pub mod secq256k1 {
};
}

impl<G: Group> TranscriptReprTrait<G> for secp256k1::Base {
fn to_transcript_bytes(&self) -> Vec<u8> {
self.to_repr().to_vec()
}
}

impl<G: Group> TranscriptReprTrait<G> for secp256k1::Scalar {
fn to_transcript_bytes(&self) -> Vec<u8> {
self.to_repr().to_vec()
}
}

impl_traits!(
secp256k1,
Secp256k1Compressed,
Expand Down
2 changes: 1 addition & 1 deletion src/spartan/polys/multilinear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl<Scalar: PrimeField> MultilinearPolynomial<Scalar> {
/// Bounds the polynomial's top variable using the given scalar.
///
/// This operation modifies the polynomial in-place.
pub fn bound_poly_var_top(&mut self, r: &Scalar) {
pub fn bind_poly_var_top(&mut self, r: &Scalar) {
let n = self.len() / 2;

let (left, right) = self.Z.split_at_mut(n);
Expand Down
6 changes: 3 additions & 3 deletions src/spartan/ppsnark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ impl<G: Group> SumcheckEngine<G> for MemorySumcheckInstance<G> {
&mut self.poly_eq,
]
.par_iter_mut()
.for_each(|poly| poly.bound_poly_var_top(r));
.for_each(|poly| poly.bind_poly_var_top(r));
}

fn final_claims(&self) -> Vec<Vec<G::Scalar>> {
Expand Down Expand Up @@ -675,7 +675,7 @@ impl<G: Group> SumcheckEngine<G> for OuterSumcheckInstance<G> {
&mut self.poly_Mz,
]
.par_iter_mut()
.for_each(|poly| poly.bound_poly_var_top(r));
.for_each(|poly| poly.bind_poly_var_top(r));
}

fn final_claims(&self) -> Vec<Vec<G::Scalar>> {
Expand Down Expand Up @@ -725,7 +725,7 @@ impl<G: Group> SumcheckEngine<G> for InnerSumcheckInstance<G> {
&mut self.poly_val,
]
.par_iter_mut()
.for_each(|poly| poly.bound_poly_var_top(r));
.for_each(|poly| poly.bind_poly_var_top(r));
}

fn final_claims(&self) -> Vec<Vec<G::Scalar>> {
Expand Down
16 changes: 8 additions & 8 deletions src/spartan/sumcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ impl<G: Group> SumcheckProof<G> {

// bound all tables to the verifier's challenege
rayon::join(
|| poly_A.bound_poly_var_top(&r_i),
|| poly_B.bound_poly_var_top(&r_i),
|| poly_A.bind_poly_var_top(&r_i),
|| poly_B.bind_poly_var_top(&r_i),
);
}

Expand Down Expand Up @@ -181,8 +181,8 @@ impl<G: Group> SumcheckProof<G> {
.zip(poly_B_vec.par_iter_mut())
.for_each(|(poly_A, poly_B)| {
let _ = rayon::join(
|| poly_A.bound_poly_var_top(&r_i),
|| poly_B.bound_poly_var_top(&r_i),
|| poly_A.bind_poly_var_top(&r_i),
|| poly_B.bind_poly_var_top(&r_i),
);
});

Expand Down Expand Up @@ -339,14 +339,14 @@ impl<G: Group> SumcheckProof<G> {
rayon::join(
|| {
rayon::join(
|| poly_A.bound_poly_var_top(&r_i),
|| poly_B.bound_poly_var_top(&r_i),
|| poly_A.bind_poly_var_top(&r_i),
|| poly_B.bind_poly_var_top(&r_i),
)
},
|| {
rayon::join(
|| poly_C.bound_poly_var_top(&r_i),
|| poly_D.bound_poly_var_top(&r_i),
|| poly_C.bind_poly_var_top(&r_i),
|| poly_D.bind_poly_var_top(&r_i),
)
},
);
Expand Down

0 comments on commit bb27f98

Please sign in to comment.