arnaucube
arnaucube/NovaPublic Self-hosted repository
Nova/benches/recursive-snark.rs
benches/recursive-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  PublicParams, RecursiveSNARK,
 13};
 14use std::time::Duration;
 15
 16type G1 = pasta_curves::pallas::Point;
 17type G2 = pasta_curves::vesta::Point;
 18type C1 = NonTrivialTestCircuit<<G1 as Group>::Scalar>;
 19type C2 = TrivialTestCircuit<<G2 as Group>::Scalar>;
 20
 21criterion_group! {
 22name = recursive_snark;
 23config = Criterion::default().warm_up_time(Duration::from_millis(3000));
 24targets = bench_recursive_snark
 25}
 26
 27criterion_main!(recursive_snark);
 28
 29fn bench_recursive_snark(c: &mut Criterion) {
 30  let num_cons_verifier_circuit_primary = 9819;
 31  // we vary the number of constraints in the step circuit
 32  for &num_cons_in_augmented_circuit in
 33    [9819, 16384, 32768, 65536, 131072, 262144, 524288, 1048576].iter()
 34  {
 35    // number of constraints in the step circuit
 36    let num_cons = num_cons_in_augmented_circuit - num_cons_verifier_circuit_primary;
 37
 38    let mut group = c.benchmark_group(format!("RecursiveSNARK-StepCircuitSize-{num_cons}"));
 39    group.sample_size(10);
 40
 41    let c_primary = NonTrivialTestCircuit::new(num_cons);
 42    let c_secondary = TrivialTestCircuit::default();
 43
 44    // Produce public parameters
 45    let pp = PublicParams::<G1, G2, C1, C2>::setup(c_primary.clone(), c_secondary.clone());
 46
 47    // Bench time to produce a recursive SNARK;
 48    // we execute a certain number of warm-up steps since executing
 49    // the first step is cheaper than other steps owing to the presence of
 50    // a lot of zeros in the satisfying assignment
 51    let num_warmup_steps = 10;
 52    let mut recursive_snark: RecursiveSNARK<G1, G2, C1, C2> = RecursiveSNARK::new(
 53      &pp,
 54      &c_primary,
 55      &c_secondary,
 56      vec![<G1 as Group>::Scalar::from(2u64)],
 57      vec![<G2 as Group>::Scalar::from(2u64)],
 58    );
 59
 60    for i in 0..num_warmup_steps {
 61      let res = recursive_snark.prove_step(
 62        &pp,
 63        &c_primary,
 64        &c_secondary,
 65        vec![<G1 as Group>::Scalar::from(2u64)],
 66        vec![<G2 as Group>::Scalar::from(2u64)],
 67      );
 68      assert!(res.is_ok());
 69
 70      // verify the recursive snark at each step of recursion
 71      let res = recursive_snark.verify(
 72        &pp,
 73        i + 1,
 74        &[<G1 as Group>::Scalar::from(2u64)],
 75        &[<G2 as Group>::Scalar::from(2u64)],
 76      );
 77      assert!(res.is_ok());
 78    }
 79
 80    group.bench_function("Prove", |b| {
 81      b.iter(|| {
 82        // produce a recursive SNARK for a step of the recursion
 83        assert!(black_box(&mut recursive_snark.clone())
 84          .prove_step(
 85            black_box(&pp),
 86            black_box(&c_primary),
 87            black_box(&c_secondary),
 88            black_box(vec![<G1 as Group>::Scalar::from(2u64)]),
 89            black_box(vec![<G2 as Group>::Scalar::from(2u64)]),
 90          )
 91          .is_ok());
 92      })
 93    });
 94
 95    // Benchmark the verification time
 96    group.bench_function("Verify", |b| {
 97      b.iter(|| {
 98        assert!(black_box(&recursive_snark)
 99          .verify(
100            black_box(&pp),
101            black_box(num_warmup_steps),
102            black_box(&vec![<G1 as Group>::Scalar::from(2u64)][..]),
103            black_box(&vec![<G2 as Group>::Scalar::from(2u64)][..]),
104          )
105          .is_ok());
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 arity(&self) -> usize {
134    1
135  }
136
137  fn synthesize<CS: ConstraintSystem<F>>(
138    &self,
139    cs: &mut CS,
140    z: &[AllocatedNum<F>],
141  ) -> Result<Vec<AllocatedNum<F>>, SynthesisError> {
142    // Consider a an equation: `x^2 = y`, where `x` and `y` are respectively the input and output.
143    let mut x = z[0].clone();
144    let mut y = x.clone();
145    for i in 0..self.num_cons {
146      y = x.square(cs.namespace(|| format!("x_sq_{i}")))?;
147      x = y.clone();
148    }
149    Ok(vec![y])
150  }
151
152  fn output(&self, z: &[F]) -> Vec<F> {
153    let mut x = z[0];
154    let mut y = x;
155    for _i in 0..self.num_cons {
156      y = x * x;
157      x = y;
158    }
159    vec![y]
160  }
161}