From b813727df2111fd6b982fab174873a5fa40e4b36 Mon Sep 17 00:00:00 2001 From: Greg Zaverucha Date: Fri, 4 Aug 2023 13:40:00 -0700 Subject: [PATCH] Proverkey (#2) * Do not duplicate R1CS instance in ProverKey The R1CSShape object was being stored in both the implementation of the RelaxedR1CSSNARKTrait's ProverKey and the Spartan SNARK's ProverKey. Now we store it once in the top level object (RelaxedR1CSSNARKTrait) and pass it to the Spartan implementation. This saves memory and makes serialization of the ProverKey about twice as fast. Both are significant when there are a large number of constraints. Signed-off-by: Greg Zaverucha * Clippy fix & whitespace Signed-off-by: Greg Zaverucha * Whitespace Signed-off-by: Greg Zaverucha * cargo fmt --------- Signed-off-by: Greg Zaverucha Co-authored-by: Srinath Setty --- src/lib.rs | 2 +- src/spartan/ppsnark.rs | 17 ++++++++--------- src/spartan/snark.rs | 25 ++++++++++++------------- src/traits/snark.rs | 1 + 4 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ad15ff9..39f2ba4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -110,7 +110,7 @@ impl, C: Circuit> SNARK SumcheckEngine for InnerSumcheckInstance { #[serde(bound = "")] pub struct ProverKey> { pk_ee: EE::ProverKey, - S: R1CSShape, S_repr: R1CSShapeSparkRepr, S_comm: R1CSShapeSparkCommitment, vk_digest: G::Scalar, // digest of verifier's key @@ -944,7 +943,6 @@ impl> RelaxedR1CSSNARKTrait> RelaxedR1CSSNARKTrait, pk: &Self::ProverKey, + S: &R1CSShape, U: &RelaxedR1CSInstance, W: &RelaxedR1CSWitness, ) -> Result { - let W = W.pad(&pk.S); // pad the witness + let W = W.pad(S); // pad the witness let mut transcript = G::TE::new(b"RelaxedR1CSSNARK"); // a list of polynomial evaluation claims that will be batched let mut w_u_vec = Vec::new(); // sanity check that R1CSShape has certain size characteristics - assert_eq!(pk.S.num_cons.next_power_of_two(), pk.S.num_cons); - assert_eq!(pk.S.num_vars.next_power_of_two(), pk.S.num_vars); - assert!(pk.S.num_io < pk.S.num_vars); + assert_eq!(S.num_cons.next_power_of_two(), S.num_cons); + assert_eq!(S.num_vars.next_power_of_two(), S.num_vars); + assert!(S.num_io < S.num_vars); // append the verifier key (which includes commitment to R1CS matrices) and the RelaxedR1CSInstance to the transcript transcript.absorb(b"vk", &pk.vk_digest); @@ -979,7 +978,7 @@ impl> RelaxedR1CSSNARKTrait> RelaxedR1CSSNARKTrait> RelaxedR1CSSNARKTrait> { pk_ee: EE::ProverKey, - S: R1CSShape, vk_digest: G::Scalar, // digest of the verifier's key } @@ -70,7 +69,7 @@ impl> RelaxedR1CSSNARKTrait Result<(Self::ProverKey, Self::VerifierKey), SpartanError> { let (pk_ee, vk_ee) = EE::setup(ck); - let S = S.pad(); + let S = &S.pad(); let vk = { let mut vk = VerifierKey { @@ -84,7 +83,6 @@ impl> RelaxedR1CSSNARKTrait> RelaxedR1CSSNARKTrait, pk: &Self::ProverKey, + S: &R1CSShape, U: &RelaxedR1CSInstance, W: &RelaxedR1CSWitness, ) -> Result { - let W = W.pad(&pk.S); // pad the witness + let W = W.pad(S); // pad the witness let mut transcript = G::TE::new(b"RelaxedR1CSSNARK"); // sanity check that R1CSShape has certain size characteristics - assert_eq!(pk.S.num_cons.next_power_of_two(), pk.S.num_cons); - assert_eq!(pk.S.num_vars.next_power_of_two(), pk.S.num_vars); - assert!(pk.S.num_io < pk.S.num_vars); + assert_eq!(S.num_cons.next_power_of_two(), S.num_cons); + assert_eq!(S.num_vars.next_power_of_two(), S.num_vars); + assert!(S.num_io < S.num_vars); // append the digest of vk (which includes R1CS matrices) and the RelaxedR1CSInstance to the transcript transcript.absorb(b"vk", &pk.vk_digest); @@ -114,8 +113,8 @@ impl> RelaxedR1CSSNARKTrait> RelaxedR1CSSNARKTrait>(); ( @@ -207,7 +206,7 @@ impl> RelaxedR1CSSNARKTrait> RelaxedR1CSSNARKTrait: fn prove( ck: &CommitmentKey, pk: &Self::ProverKey, + S: &R1CSShape, U: &RelaxedR1CSInstance, W: &RelaxedR1CSWitness, ) -> Result;