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