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 = 9819;
34 // we vary the number of constraints in the step circuit
35 for &num_cons_in_augmented_circuit in
36 [9819, 16384, 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 vec![<G1 as Group>::Scalar::from(2u64)],
61 vec![<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 vec![<G1 as Group>::Scalar::from(2u64)],
71 vec![<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(vec![<G1 as Group>::Scalar::from(2u64)]),
102 black_box(vec![<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 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}