arnaucube
arnaucube/NovaPublic Self-hosted repository
Nova/benches/compressed-snark.rs
benches/compressed-snark.rsRaw
  1#![allow(non_snake_case)]
  2
  3use criterion::*;
  4use nova_snark::{
  5  traits::{circuit::TrivialTestCircuit, Group},
  6  CompressedSNARK, PublicParams, RecursiveSNARK,
  7};
  8use std::time::Duration;
  9
 10type G1 = pasta_curves::pallas::Point;
 11type G2 = pasta_curves::vesta::Point;
 12type S1 = nova_snark::spartan_with_ipa_pc::RelaxedR1CSSNARK<G1>;
 13type S2 = nova_snark::spartan_with_ipa_pc::RelaxedR1CSSNARK<G2>;
 14type C1 = TrivialTestCircuit<<G1 as Group>::Scalar>;
 15type C2 = TrivialTestCircuit<<G2 as Group>::Scalar>;
 16
 17fn compressed_snark_benchmark(c: &mut Criterion) {
 18  let num_samples = 10;
 19  bench_compressed_snark(c, num_samples);
 20}
 21
 22fn set_duration() -> Criterion {
 23  Criterion::default().warm_up_time(Duration::from_millis(3000))
 24}
 25
 26criterion_group! {
 27name = compressed_snark;
 28config = set_duration();
 29targets = compressed_snark_benchmark
 30}
 31
 32criterion_main!(compressed_snark);
 33
 34fn bench_compressed_snark(c: &mut Criterion, num_samples: usize) {
 35  let mut group = c.benchmark_group("CompressedSNARK");
 36  group.sample_size(num_samples);
 37
 38  // Produce public parameters
 39  let pp = PublicParams::<G1, G2, C1, C2>::setup(
 40    TrivialTestCircuit::default(),
 41    TrivialTestCircuit::default(),
 42  );
 43
 44  // produce a recursive SNARK
 45  let num_steps = 3;
 46  let mut recursive_snark: Option<RecursiveSNARK<G1, G2, C1, C2>> = None;
 47
 48  for i in 0..num_steps {
 49    let res = RecursiveSNARK::prove_step(
 50      &pp,
 51      recursive_snark,
 52      TrivialTestCircuit::default(),
 53      TrivialTestCircuit::default(),
 54      <G1 as Group>::Scalar::one(),
 55      <G2 as Group>::Scalar::zero(),
 56    );
 57    assert!(res.is_ok());
 58    let recursive_snark_unwrapped = res.unwrap();
 59
 60    // verify the recursive snark at each step of recursion
 61    let res = recursive_snark_unwrapped.verify(
 62      &pp,
 63      i + 1,
 64      <G1 as Group>::Scalar::one(),
 65      <G2 as Group>::Scalar::zero(),
 66    );
 67    assert!(res.is_ok());
 68
 69    // set the running variable for the next iteration
 70    recursive_snark = Some(recursive_snark_unwrapped);
 71  }
 72
 73  // Bench time to produce a compressed SNARK
 74  let recursive_snark = recursive_snark.unwrap();
 75  group.bench_function("Prove", |b| {
 76    b.iter(|| {
 77      assert!(CompressedSNARK::<_, _, _, _, S1, S2>::prove(
 78        black_box(&pp),
 79        black_box(&recursive_snark)
 80      )
 81      .is_ok());
 82    })
 83  });
 84  let res = CompressedSNARK::<_, _, _, _, S1, S2>::prove(&pp, &recursive_snark);
 85  assert!(res.is_ok());
 86  let compressed_snark = res.unwrap();
 87
 88  // Benchmark the verification time
 89  let name = "Verify";
 90  group.bench_function(name, |b| {
 91    b.iter(|| {
 92      assert!(black_box(&compressed_snark)
 93        .verify(
 94          black_box(&pp),
 95          black_box(num_steps),
 96          black_box(<G1 as Group>::Scalar::zero()),
 97          black_box(<G2 as Group>::Scalar::zero()),
 98        )
 99        .is_ok());
100    })
101  });
102
103  group.finish();
104}