From 746af53e08b4442ccf156e41069c42bd8e2e959c Mon Sep 17 00:00:00 2001 From: Srinath Setty Date: Tue, 31 Jan 2023 10:32:49 -0800 Subject: [PATCH] use serde feature to compute the digest of the shape (#132) --- examples/minroot.rs | 9 +++++++++ src/r1cs.rs | 41 ++++++++++++++++------------------------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/examples/minroot.rs b/examples/minroot.rs index a54fc3f..122abda 100644 --- a/examples/minroot.rs +++ b/examples/minroot.rs @@ -5,6 +5,7 @@ type G1 = pasta_curves::pallas::Point; type G2 = pasta_curves::vesta::Point; use ::bellperson::{gadgets::num::AllocatedNum, ConstraintSystem, SynthesisError}; use ff::PrimeField; +use flate2::{write::ZlibEncoder, Compression}; use nova_snark::{ traits::{ circuit::{StepCircuit, TrivialTestCircuit}, @@ -268,6 +269,14 @@ fn main() { assert!(res.is_ok()); let compressed_snark = res.unwrap(); + let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default()); + bincode::serialize_into(&mut encoder, &compressed_snark).unwrap(); + let compressed_snark_encoded = encoder.finish().unwrap(); + println!( + "CompressedSNARK::len {:?} bytes", + compressed_snark_encoded.len() + ); + // verify the compressed SNARK println!("Verifying a CompressedSNARK..."); let start = Instant::now(); diff --git a/src/r1cs.rs b/src/r1cs.rs index fea5b9f..e046565 100644 --- a/src/r1cs.rs +++ b/src/r1cs.rs @@ -9,7 +9,7 @@ use super::{ traits::{AbsorbInROTrait, AppendToTranscriptTrait, Group, ROTrait}, }; use core::cmp::max; -use ff::{Field, PrimeField}; +use ff::Field; use flate2::{write::ZlibEncoder, Compression}; use itertools::concat; use merlin::Transcript; @@ -311,27 +311,28 @@ impl R1CSShape { B: &[(usize, usize, G::Scalar)], C: &[(usize, usize, G::Scalar)], ) -> G::Scalar { - let shape_serialized = R1CSShapeSerialized { + #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] + struct R1CSShapeWithoutDigest { + num_cons: usize, + num_vars: usize, + num_io: usize, + A: Vec<(usize, usize, G::Scalar)>, + B: Vec<(usize, usize, G::Scalar)>, + C: Vec<(usize, usize, G::Scalar)>, + } + + let shape = R1CSShapeWithoutDigest:: { num_cons, num_vars, num_io, - A: A - .par_iter() - .map(|(i, j, v)| (*i, *j, v.to_repr().as_ref().to_vec())) - .collect(), - B: B - .par_iter() - .map(|(i, j, v)| (*i, *j, v.to_repr().as_ref().to_vec())) - .collect(), - C: C - .par_iter() - .map(|(i, j, v)| (*i, *j, v.to_repr().as_ref().to_vec())) - .collect(), + A: A.to_vec(), + B: B.to_vec(), + C: C.to_vec(), }; // obtain a vector of bytes representing the R1CS shape let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default()); - bincode::serialize_into(&mut encoder, &shape_serialized).unwrap(); + bincode::serialize_into(&mut encoder, &shape).unwrap(); let shape_bytes = encoder.finish().unwrap(); // convert shape_bytes into a short digest @@ -431,16 +432,6 @@ impl R1CSShape { } } -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -struct R1CSShapeSerialized { - num_cons: usize, - num_vars: usize, - num_io: usize, - A: Vec<(usize, usize, Vec)>, - B: Vec<(usize, usize, Vec)>, - C: Vec<(usize, usize, Vec)>, -} - impl AppendToTranscriptTrait for R1CSShape { fn append_to_transcript(&self, _label: &'static [u8], transcript: &mut Transcript) { self