Skip to content

Commit

Permalink
refactor: Enhance efficiency of generating power polynomials
Browse files Browse the repository at this point in the history
- Introduced `#[must_use]` annotation to the `squares` function in `power.rs` to ensure the returned result is handled appropriately.
- avoid re-computing powers of tau over and over in batched_ppsnark
  • Loading branch information
huitseeker committed Feb 8, 2024
1 parent 5b82c3c commit 2c46050
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/spartan/batched_ppsnark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ impl<E: Engine, EE: EvaluationEngineTrait<E>> BatchedRelaxedR1CSSNARKTrait<E>
// N[i] = max(|Aᵢ|+|Bᵢ|+|Cᵢ|, 2*num_varsᵢ, num_consᵢ)
let N = pk.S_repr.iter().map(|s| s.N).collect::<Vec<_>>();
assert!(N.iter().all(|&Ni| Ni.is_power_of_two()));
let N_max = *N.iter().max().unwrap();

let num_instances = U.len();

Expand Down Expand Up @@ -247,13 +248,15 @@ impl<E: Engine, EE: EvaluationEngineTrait<E>> BatchedRelaxedR1CSSNARKTrait<E>

// Compute eq(tau) for each instance in log2(Ni) variables
let tau = transcript.squeeze(b"t")?;
let all_taus = PowPolynomial::squares(&tau, N_max.log_2());

let (polys_tau, coords_tau): (Vec<_>, Vec<_>) = N
.iter()
.par_iter()
.map(|&N_i| {
let log_Ni = N_i.log_2();
let poly = PowPolynomial::new(&tau, log_Ni);
let evals = poly.evals();
let coords = poly.coordinates();
let eqp: EqPolynomial<_> = all_taus[..log_Ni].iter().cloned().collect();
let evals = eqp.evals();
let coords = eqp.r;
(evals, coords)
})
.unzip();
Expand Down
3 changes: 2 additions & 1 deletion src/spartan/polys/eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rayon::prelude::{IndexedParallelIterator, IntoParallelRefMutIterator, Parall
/// For instance, for e = 6 (with a binary representation of 0b110), the vector r would be [1, 1, 0].
#[derive(Debug)]
pub struct EqPolynomial<Scalar> {
pub(crate) r: Vec<Scalar>,
pub(in crate::spartan) r: Vec<Scalar>,
}

impl<Scalar: PrimeField> EqPolynomial<Scalar> {
Expand All @@ -43,6 +43,7 @@ impl<Scalar: PrimeField> EqPolynomial<Scalar> {
/// Evaluates the `EqPolynomial` at all the `2^|r|` points in its domain.
///
/// Returns a vector of Scalars, each corresponding to the polynomial evaluation at a specific point.
#[must_use = "this returns an expensive vector and leaves self unchanged"]
pub fn evals(&self) -> Vec<Scalar> {
Self::evals_from_points(&self.r)
}
Expand Down

0 comments on commit 2c46050

Please sign in to comment.