You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.8 KiB

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