arnaucube
arnaucube/NovaPublic Self-hosted repository
Nova/benches/compressed-snark.rs
benches/compressed-snark.rsRaw
  1#![allow(non_snake_case)]
  2
  3use bellperson::{gadgets::num::AllocatedNum, ConstraintSystem, SynthesisError};
  4use core::marker::PhantomData;
  5use criterion::*;
  6use ff::PrimeField;
  7use nova_snark::{
  8  traits::{
  9    circuit::{StepCircuit, TrivialTestCircuit},
 10    Group,
 11  },
 12  CompressedSNARK, PublicParams, RecursiveSNARK,
 13};
 14use std::time::Duration;
 15
 16type G1 = pasta_curves::pallas::Point;
 17type G2 = pasta_curves::vesta::Point;
 18type S1 = nova_snark::spartan_with_ipa_pc::RelaxedR1CSSNARK<G1>;
 19type S2 = nova_snark::spartan_with_ipa_pc::RelaxedR1CSSNARK<G2>;
 20type C1 = NonTrivialTestCircuit<<G1 as Group>::Scalar>;
 21type C2 = TrivialTestCircuit<<G2 as Group>::Scalar>;
 22
 23criterion_group! {
 24name = compressed_snark;
 25config = Criterion::default().warm_up_time(Duration::from_millis(3000));
 26targets = bench_compressed_snark
 27}
 28
 29criterion_main!(compressed_snark);
 30
 31fn bench_compressed_snark(c: &mut Criterion) {
 32  let num_samples = 10;
 33  let num_cons_verifier_circuit_primary = 20584;
 34  // we vary the number of constraints in the step circuit
 35  for &num_cons_in_augmented_circuit in
 36    [20584, 32768, 65536, 131072, 262144, 524288, 1048576].iter()
 37  {
 38    // number of constraints in the step circuit
 39    let num_cons = num_cons_in_augmented_circuit - num_cons_verifier_circuit_primary;
 40
 41    let mut group = c.benchmark_group(format!("CompressedSNARK-StepCircuitSize-{}", num_cons));
 42    group.sample_size(num_samples);
 43
 44    // Produce public parameters
 45    let pp = PublicParams::<G1, G2, C1, C2>::setup(
 46      NonTrivialTestCircuit::new(num_cons),
 47      TrivialTestCircuit::default(),
 48    );
 49
 50    // produce a recursive SNARK
 51    let num_steps = 3;
 52    let mut recursive_snark: Option<RecursiveSNARK<G1, G2, C1, C2>> = None;
 53
 54    for i in 0..num_steps {
 55      let res = RecursiveSNARK::prove_step(
 56        &pp,
 57        recursive_snark,
 58        NonTrivialTestCircuit::new(num_cons),
 59        TrivialTestCircuit::default(),
 60        <G1 as Group>::Scalar::from(2u64),
 61        <G2 as Group>::Scalar::from(2u64),
 62      );
 63      assert!(res.is_ok());
 64      let recursive_snark_unwrapped = res.unwrap();
 65
 66      // verify the recursive snark at each step of recursion
 67      let res = recursive_snark_unwrapped.verify(
 68        &pp,
 69        i + 1,
 70        <G1 as Group>::Scalar::from(2u64),
 71        <G2 as Group>::Scalar::from(2u64),
 72      );
 73      assert!(res.is_ok());
 74
 75      // set the running variable for the next iteration
 76      recursive_snark = Some(recursive_snark_unwrapped);
 77    }
 78
 79    // Bench time to produce a compressed SNARK
 80    let recursive_snark = recursive_snark.unwrap();
 81    group.bench_function("Prove", |b| {
 82      b.iter(|| {
 83        assert!(CompressedSNARK::<_, _, _, _, S1, S2>::prove(
 84          black_box(&pp),
 85          black_box(&recursive_snark)
 86        )
 87        .is_ok());
 88      })
 89    });
 90    let res = CompressedSNARK::<_, _, _, _, S1, S2>::prove(&pp, &recursive_snark);
 91    assert!(res.is_ok());
 92    let compressed_snark = res.unwrap();
 93
 94    // Benchmark the verification time
 95    group.bench_function("Verify", |b| {
 96      b.iter(|| {
 97        assert!(black_box(&compressed_snark)
 98          .verify(
 99            black_box(&pp),
100            black_box(num_steps),
101            black_box(<G1 as Group>::Scalar::from(2u64)),
102            black_box(<G2 as Group>::Scalar::from(2u64)),
103          )
104          .is_ok());
105      })
106    });
107
108    group.finish();
109  }
110}
111
112#[derive(Clone, Debug, Default)]
113struct NonTrivialTestCircuit<F: PrimeField> {
114  num_cons: usize,
115  _p: PhantomData<F>,
116}
117
118impl<F> NonTrivialTestCircuit<F>
119where
120  F: PrimeField,
121{
122  pub fn new(num_cons: usize) -> Self {
123    Self {
124      num_cons,
125      _p: Default::default(),
126    }
127  }
128}
129impl<F> StepCircuit<F> for NonTrivialTestCircuit<F>
130where
131  F: PrimeField,
132{
133  fn synthesize<CS: ConstraintSystem<F>>(
134    &self,
135    cs: &mut CS,
136    z: AllocatedNum<F>,
137  ) -> Result<AllocatedNum<F>, SynthesisError> {
138    // Consider a an equation: `x^2 = y`, where `x` and `y` are respectively the input and output.
139    let mut x = z;
140    let mut y = x.clone();
141    for i in 0..self.num_cons {
142      y = x.square(cs.namespace(|| format!("x_sq_{}", i)))?;
143      x = y.clone();
144    }
145    Ok(y)
146  }
147
148  fn compute(&self, z: &F) -> F {
149    let mut x = *z;
150    let mut y = x;
151    for _i in 0..self.num_cons {
152      y = x * x;
153      x = y;
154    }
155    y
156  }
157}