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