diff --git a/Cargo.toml b/Cargo.toml index c98204b..e1be4e2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,6 +49,10 @@ harness = false name = "compressed-snark" harness = false +[[bench]] +name = "compute-digest" +harness = false + [features] default = [] # Compiles in portable mode, w/o ISA extensions => binary can be executed on all systems. diff --git a/benches/compute-digest.rs b/benches/compute-digest.rs new file mode 100644 index 0000000..47bdda1 --- /dev/null +++ b/benches/compute-digest.rs @@ -0,0 +1,84 @@ +use std::{marker::PhantomData, time::Duration}; + +use bellperson::{gadgets::num::AllocatedNum, ConstraintSystem, SynthesisError}; +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use ff::PrimeField; +use nova_snark::{ + traits::{ + circuit::{StepCircuit, TrivialTestCircuit}, + Group, + }, + PublicParams, +}; + +type G1 = pasta_curves::pallas::Point; +type G2 = pasta_curves::vesta::Point; +type C1 = NonTrivialTestCircuit<::Scalar>; +type C2 = TrivialTestCircuit<::Scalar>; + +criterion_group! { +name = compute_digest; +config = Criterion::default().warm_up_time(Duration::from_millis(3000)).sample_size(10); +targets = bench_compute_digest +} + +criterion_main!(compute_digest); + +fn bench_compute_digest(c: &mut Criterion) { + c.bench_function("compute_digest", |b| { + b.iter(|| { + PublicParams::::setup(black_box(C1::new(10)), black_box(C2::default())) + }) + }); +} + +#[derive(Clone, Debug, Default)] +struct NonTrivialTestCircuit { + num_cons: usize, + _p: PhantomData, +} + +impl NonTrivialTestCircuit +where + F: PrimeField, +{ + pub fn new(num_cons: usize) -> Self { + Self { + num_cons, + _p: Default::default(), + } + } +} +impl StepCircuit for NonTrivialTestCircuit +where + F: PrimeField, +{ + fn arity(&self) -> usize { + 1 + } + + fn synthesize>( + &self, + cs: &mut CS, + z: &[AllocatedNum], + ) -> Result>, SynthesisError> { + // Consider a an equation: `x^2 = y`, where `x` and `y` are respectively the input and output. + let mut x = z[0].clone(); + let mut y = x.clone(); + for i in 0..self.num_cons { + y = x.square(cs.namespace(|| format!("x_sq_{i}")))?; + x = y.clone(); + } + Ok(vec![y]) + } + + fn output(&self, z: &[F]) -> Vec { + let mut x = z[0]; + let mut y = x; + for _i in 0..self.num_cons { + y = x * x; + x = y; + } + vec![y] + } +} diff --git a/src/lib.rs b/src/lib.rs index c416517..930ae1f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,7 +36,6 @@ use constants::{BN_LIMB_WIDTH, BN_N_LIMBS, NUM_FE_WITHOUT_IO_FOR_CRHF, NUM_HASH_ use core::marker::PhantomData; use errors::NovaError; use ff::Field; -use flate2::{write::ZlibEncoder, Compression}; use gadgets::utils::scalar_as_base; use nifs::NIFS; use r1cs::{R1CSInstance, R1CSShape, R1CSWitness, RelaxedR1CSInstance, RelaxedR1CSWitness}; @@ -757,13 +756,10 @@ type CE = ::CE; fn compute_digest(o: &T) -> G::Scalar { // obtain a vector of bytes representing public parameters - let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default()); - bincode::serialize_into(&mut encoder, o).unwrap(); - let pp_bytes = encoder.finish().unwrap(); - + let bytes = bincode::serialize(o).unwrap(); // convert pp_bytes into a short digest let mut hasher = Sha3_256::new(); - hasher.input(&pp_bytes); + hasher.input(&bytes); let digest = hasher.result(); // truncate the digest to NUM_HASH_BITS bits @@ -851,6 +847,46 @@ mod tests { } } + fn test_pp_digest_with(circuit1: T1, circuit2: T2, expected: &str) + where + G1: Group::Scalar>, + G2: Group::Scalar>, + T1: StepCircuit, + T2: StepCircuit, + { + let pp = PublicParams::::setup(circuit1, circuit2); + + let digest_str = pp + .digest + .to_repr() + .as_ref() + .iter() + .map(|b| format!("{:02x}", b)) + .collect::(); + assert_eq!(digest_str, expected); + } + + #[test] + fn test_pp_digest() { + type G1 = pasta_curves::pallas::Point; + type G2 = pasta_curves::vesta::Point; + let trivial_circuit1 = TrivialTestCircuit::<::Scalar>::default(); + let trivial_circuit2 = TrivialTestCircuit::<::Scalar>::default(); + let cubic_circuit1 = CubicCircuit::<::Scalar>::default(); + + test_pp_digest_with::( + trivial_circuit1, + trivial_circuit2.clone(), + "39a4ea9dd384346fdeb6b5857c7be56fa035153b616d55311f3191dfbceea603", + ); + + test_pp_digest_with::( + cubic_circuit1, + trivial_circuit2, + "3f7b25f589f2da5ab26254beba98faa54f6442ebf5fa5860caf7b08b576cab00", + ); + } + fn test_ivc_trivial_with() where G1: Group::Scalar>,