Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize ppsnark #250

Merged
merged 7 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nova-snark"
version = "0.28.0"
version = "0.29.0"
authors = ["Srinath Setty <[email protected]>"]
edition = "2021"
description = "Recursive zkSNARKs without trusted setup"
Expand Down
3 changes: 3 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,7 @@ pub enum NovaError {
/// returned when there is an error creating a digest
#[error("DigestError")]
DigestError,
/// returned when the prover cannot prove the provided statement due to completeness error
#[error("InternalError")]
InternalError,
}
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,6 @@ mod tests {
assert_eq!(zn_secondary, vec![<G2 as Group>::Scalar::from(2460515u64)]);

// run the compressed snark with Spark compiler

// produce the prover and verifier keys for compressed snark
let (pk, vk) =
CompressedSNARK::<_, _, _, _, SPrime<G1, E1>, SPrime<G2, E2>>::setup(&pp).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/provider/ipa_pc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ where

// we can compute an inversion only if acc is non-zero
if acc == G::Scalar::ZERO {
return Err(NovaError::InvalidInputLength);
return Err(NovaError::InternalError);
}

// compute the inverse once for all entries
Expand Down
9 changes: 6 additions & 3 deletions src/spartan/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,22 +225,25 @@ mod tests {
type G = pasta_curves::pallas::Point;
type EE = crate::provider::ipa_pc::EvaluationEngine<G>;
type S = crate::spartan::snark::RelaxedR1CSSNARK<G, EE>;
type Spp = crate::spartan::ppsnark::RelaxedR1CSSNARK<G, EE>;
test_direct_snark_with::<G, S>();

type Spp = crate::spartan::ppsnark::RelaxedR1CSSNARK<G, EE>;
test_direct_snark_with::<G, Spp>();

type G2 = bn256::Point;
type EE2 = crate::provider::ipa_pc::EvaluationEngine<G2>;
type S2 = crate::spartan::snark::RelaxedR1CSSNARK<G2, EE2>;
type S2pp = crate::spartan::ppsnark::RelaxedR1CSSNARK<G2, EE2>;
test_direct_snark_with::<G2, S2>();

type S2pp = crate::spartan::ppsnark::RelaxedR1CSSNARK<G2, EE2>;
test_direct_snark_with::<G2, S2pp>();

type G3 = secp256k1::Point;
type EE3 = crate::provider::ipa_pc::EvaluationEngine<G3>;
type S3 = crate::spartan::snark::RelaxedR1CSSNARK<G3, EE3>;
type S3pp = crate::spartan::ppsnark::RelaxedR1CSSNARK<G3, EE3>;
test_direct_snark_with::<G3, S3>();

type S3pp = crate::spartan::ppsnark::RelaxedR1CSSNARK<G3, EE3>;
test_direct_snark_with::<G3, S3pp>();
}

Expand Down
38 changes: 27 additions & 11 deletions src/spartan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod sumcheck;
use crate::{traits::Group, Commitment};
use ff::Field;
use polys::multilinear::SparsePolynomial;
use rayon::{iter::IntoParallelRefIterator, prelude::*};

fn powers<G: Group>(s: &G::Scalar, n: usize) -> Vec<G::Scalar> {
assert!(n >= 1);
Expand Down Expand Up @@ -58,14 +59,29 @@ impl<G: Group> PolyEvalWitness<G> {
PolyEvalWitness { p }
}

// This method panics unless all vectors in p_vec are of the same length
fn batch(p_vec: &[&Vec<G::Scalar>], s: &G::Scalar) -> PolyEvalWitness<G> {
p_vec
.iter()
.for_each(|p| assert_eq!(p.len(), p_vec[0].len()));

let powers_of_s = powers::<G>(s, p_vec.len());
let mut p = vec![G::Scalar::ZERO; p_vec[0].len()];
for i in 0..p_vec.len() {
for (j, item) in p.iter_mut().enumerate().take(p_vec[i].len()) {
*item += p_vec[i][j] * powers_of_s[i]
}
}

let p = p_vec
.par_iter()
.zip(powers_of_s.par_iter())
.map(|(v, &weight)| {
// compute the weighted sum for each vector
v.iter().map(|&x| x * weight).collect::<Vec<G::Scalar>>()
})
.reduce(
|| vec![G::Scalar::ZERO; p_vec[0].len()],
|acc, v| {
// perform vector addition to combine the weighted vectors
acc.into_iter().zip(v).map(|(x, y)| x + y).collect()
},
);

PolyEvalWitness { p }
}
}
Expand Down Expand Up @@ -101,15 +117,15 @@ impl<G: Group> PolyEvalInstance<G> {
) -> PolyEvalInstance<G> {
let powers_of_s = powers::<G>(s, c_vec.len());
let e = e_vec
.iter()
.zip(powers_of_s.iter())
.par_iter()
.zip(powers_of_s.par_iter())
.map(|(e, p)| *e * p)
.sum();
let c = c_vec
.iter()
.zip(powers_of_s.iter())
.par_iter()
.zip(powers_of_s.par_iter())
.map(|(c, p)| *c * *p)
.fold(Commitment::<G>::default(), |acc, item| acc + item);
.reduce(Commitment::<G>::default, |acc, item| acc + item);

PolyEvalInstance {
c,
Expand Down
29 changes: 29 additions & 0 deletions src/spartan/polys/identity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use core::marker::PhantomData;
use ff::PrimeField;

pub struct IdentityPolynomial<Scalar: PrimeField> {
ell: usize,
_p: PhantomData<Scalar>,
}

impl<Scalar: PrimeField> IdentityPolynomial<Scalar> {
pub fn new(ell: usize) -> Self {
IdentityPolynomial {
ell,
_p: PhantomData,
}
}

pub fn evaluate(&self, r: &[Scalar]) -> Scalar {
assert_eq!(self.ell, r.len());
let mut power_of_two = 1_u64;
(0..self.ell)
.rev()
.map(|i| {
let result = Scalar::from(power_of_two) * r[i];
power_of_two *= 2;
result
})
.fold(Scalar::ZERO, |acc, item| acc + item)
}
}
1 change: 1 addition & 0 deletions src/spartan/polys/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! This module contains the definitions of polynomial types used in the Spartan SNARK.
pub mod eq;
pub(crate) mod identity;
pub mod multilinear;
pub(crate) mod power;
pub(crate) mod univariate;
Loading
Loading