Skip to content

Commit

Permalink
Digest simplifications (#238)
Browse files Browse the repository at this point in the history
* remove unused digest computations

* avoid a verifier having to recompute a digest

* update crate version
  • Loading branch information
srinathsetty committed Oct 27, 2023
1 parent bd6e4e1 commit 71ecb66
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
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.24.0"
version = "0.25.0"
authors = ["Srinath Setty <srinath@microsoft.com>"]
edition = "2021"
description = "Recursive zkSNARKs without trusted setup"
Expand Down
19 changes: 0 additions & 19 deletions src/r1cs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ mod util;

use crate::{
constants::{BN_LIMB_WIDTH, BN_N_LIMBS},
digest::{DigestComputer, SimpleDigestible},
errors::NovaError,
gadgets::{
nonnative::{bignat::nat_to_limbs, util::f_to_nat},
Expand All @@ -18,8 +17,6 @@ use crate::{
};
use core::{cmp::max, marker::PhantomData};
use ff::Field;
use once_cell::sync::OnceCell;

use rayon::prelude::*;
use serde::{Deserialize, Serialize};

Expand All @@ -41,12 +38,8 @@ pub struct R1CSShape<G: Group> {
pub(crate) A: SparseMatrix<G::Scalar>,
pub(crate) B: SparseMatrix<G::Scalar>,
pub(crate) C: SparseMatrix<G::Scalar>,
#[serde(skip, default = "OnceCell::new")]
pub(crate) digest: OnceCell<G::Scalar>,
}

impl<G: Group> SimpleDigestible for R1CSShape<G> {}

/// A type that holds a witness for a given R1CS instance
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct R1CSWitness<G: Group> {
Expand Down Expand Up @@ -141,19 +134,9 @@ impl<G: Group> R1CSShape<G> {
A,
B,
C,
digest: OnceCell::new(),
})
}

/// returned the digest of the `R1CSShape`
pub fn digest(&self) -> G::Scalar {
self
.digest
.get_or_try_init(|| DigestComputer::new(self).digest())
.cloned()
.expect("Failure retrieving digest")
}

// Checks regularity conditions on the R1CSShape, required in Spartan-class SNARKs
// Panics if num_cons, num_vars, or num_io are not powers of two, or if num_io > num_vars
#[inline]
Expand Down Expand Up @@ -321,7 +304,6 @@ impl<G: Group> R1CSShape<G> {
A: self.A.clone(),
B: self.B.clone(),
C: self.C.clone(),
digest: OnceCell::new(),
};
}

Expand Down Expand Up @@ -357,7 +339,6 @@ impl<G: Group> R1CSShape<G> {
A: A_padded,
B: B_padded,
C: C_padded,
digest: OnceCell::new(),
}
}
}
Expand Down
31 changes: 25 additions & 6 deletions src/spartan/ppsnark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,16 @@ pub struct ProverKey<G: Group, EE: EvaluationEngineTrait<G>> {
#[derive(Clone, Serialize, Deserialize)]
#[serde(bound = "")]
pub struct VerifierKey<G: Group, EE: EvaluationEngineTrait<G>> {
num_cons: usize,
num_vars: usize,
vk_ee: EE::VerifierKey,
S_comm: R1CSShapeSparkCommitment<G>,
digest: G::Scalar,
}

#[derive(Clone, Serialize, Deserialize)]
#[serde(bound = "")]
struct VerifierKeyInternal<G: Group, EE: EvaluationEngineTrait<G>> {
num_cons: usize,
num_vars: usize,
vk_ee: EE::VerifierKey,
Expand All @@ -674,7 +684,7 @@ pub struct VerifierKey<G: Group, EE: EvaluationEngineTrait<G>> {
digest: OnceCell<G::Scalar>,
}

impl<G: Group, EE: EvaluationEngineTrait<G>> SimpleDigestible for VerifierKey<G, EE> {}
impl<G: Group, EE: EvaluationEngineTrait<G>> SimpleDigestible for VerifierKeyInternal<G, EE> {}

/// A succinct proof of knowledge of a witness to a relaxed R1CS instance
/// The proof is produced using Spartan's combination of the sum-check and
Expand Down Expand Up @@ -842,14 +852,14 @@ impl<G: Group, EE: EvaluationEngineTrait<G>> RelaxedR1CSSNARK<G, EE> {
}
}

impl<G: Group, EE: EvaluationEngineTrait<G>> VerifierKey<G, EE> {
impl<G: Group, EE: EvaluationEngineTrait<G>> VerifierKeyInternal<G, EE> {
fn new(
num_cons: usize,
num_vars: usize,
S_comm: R1CSShapeSparkCommitment<G>,
vk_ee: EE::VerifierKey,
) -> Self {
VerifierKey {
VerifierKeyInternal {
num_cons,
num_vars,
S_comm,
Expand Down Expand Up @@ -887,14 +897,23 @@ impl<G: Group, EE: EvaluationEngineTrait<G>> RelaxedR1CSSNARKTrait<G> for Relaxe
let S_repr = R1CSShapeSparkRepr::new(&S);
let S_comm = S_repr.commit(ck);

let vk = VerifierKey::new(S.num_cons, S.num_vars, S_comm.clone(), vk_ee);
let vk_internal: VerifierKeyInternal<G, EE> =
VerifierKeyInternal::new(S.num_cons, S.num_vars, S_comm.clone(), vk_ee.clone());

let pk = ProverKey {
pk_ee,
S,
S_repr,
S_comm: S_comm.clone(),
vk_digest: vk_internal.digest(),
};

let vk = VerifierKey {
num_cons: vk_internal.num_cons,
num_vars: vk_internal.num_vars,
vk_ee,
S_comm,
vk_digest: vk.digest(),
digest: vk_internal.digest(),
};

Ok((pk, vk))
Expand Down Expand Up @@ -1516,7 +1535,7 @@ impl<G: Group, EE: EvaluationEngineTrait<G>> RelaxedR1CSSNARKTrait<G> for Relaxe
let mut u_vec: Vec<PolyEvalInstance<G>> = Vec::new();

// append the verifier key (including commitment to R1CS matrices) and the RelaxedR1CSInstance to the transcript
transcript.absorb(b"vk", &vk.digest());
transcript.absorb(b"vk", &vk.digest);
transcript.absorb(b"U", U);

let comm_Az = Commitment::<G>::decompress(&self.comm_Az)?;
Expand Down

0 comments on commit 71ecb66

Please sign in to comment.