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.

106 lines
2.9 KiB

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