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.

112 lines
2.6 KiB

  1. #![allow(non_snake_case)]
  2. use nova_snark::{
  3. traits::{Group, StepCircuit},
  4. PublicParams, RecursiveSNARK,
  5. };
  6. type G1 = pasta_curves::pallas::Point;
  7. type G2 = pasta_curves::vesta::Point;
  8. use bellperson::{gadgets::num::AllocatedNum, ConstraintSystem, SynthesisError};
  9. use core::marker::PhantomData;
  10. use criterion::*;
  11. use ff::PrimeField;
  12. use std::time::Duration;
  13. fn recursive_snark_benchmark(c: &mut Criterion) {
  14. let num_samples = 10;
  15. for num_steps in 1..10 {
  16. bench_recursive_snark(c, num_samples, num_steps);
  17. }
  18. }
  19. fn set_duration() -> Criterion {
  20. Criterion::default().warm_up_time(Duration::from_millis(3000))
  21. }
  22. criterion_group! {
  23. name = recursive_snark;
  24. config = set_duration();
  25. targets = recursive_snark_benchmark
  26. }
  27. criterion_main!(recursive_snark);
  28. fn bench_recursive_snark(c: &mut Criterion, num_samples: usize, num_steps: usize) {
  29. let mut group = c.benchmark_group(format!("RecursiveSNARK-NumSteps-{}", num_steps));
  30. group.sample_size(num_samples);
  31. // Produce public parameters
  32. let pp = PublicParams::<
  33. G1,
  34. G2,
  35. TrivialTestCircuit<<G1 as Group>::Scalar>,
  36. TrivialTestCircuit<<G2 as Group>::Scalar>,
  37. >::setup(
  38. TrivialTestCircuit {
  39. _p: Default::default(),
  40. },
  41. TrivialTestCircuit {
  42. _p: Default::default(),
  43. },
  44. );
  45. // Bench time to produce a recursive SNARK
  46. group.bench_function("Prove", |b| {
  47. b.iter(|| {
  48. // produce a recursive SNARK
  49. assert!(RecursiveSNARK::prove(
  50. black_box(&pp),
  51. black_box(num_steps),
  52. black_box(<G1 as Group>::Scalar::zero()),
  53. black_box(<G2 as Group>::Scalar::zero()),
  54. )
  55. .is_ok());
  56. })
  57. });
  58. let res = RecursiveSNARK::prove(
  59. &pp,
  60. num_steps,
  61. <G1 as Group>::Scalar::zero(),
  62. <G2 as Group>::Scalar::zero(),
  63. );
  64. assert!(res.is_ok());
  65. let recursive_snark = res.unwrap();
  66. // Benchmark the verification time
  67. let name = "Verify";
  68. group.bench_function(name, |b| {
  69. b.iter(|| {
  70. assert!(black_box(&recursive_snark)
  71. .verify(
  72. black_box(&pp),
  73. black_box(num_steps),
  74. black_box(<G1 as Group>::Scalar::zero()),
  75. black_box(<G2 as Group>::Scalar::zero()),
  76. )
  77. .is_ok());
  78. });
  79. });
  80. group.finish();
  81. }
  82. #[derive(Clone, Debug)]
  83. struct TrivialTestCircuit<F: PrimeField> {
  84. _p: PhantomData<F>,
  85. }
  86. impl<F> StepCircuit<F> for TrivialTestCircuit<F>
  87. where
  88. F: PrimeField,
  89. {
  90. fn synthesize<CS: ConstraintSystem<F>>(
  91. &self,
  92. _cs: &mut CS,
  93. z: AllocatedNum<F>,
  94. ) -> Result<AllocatedNum<F>, SynthesisError> {
  95. Ok(z)
  96. }
  97. fn compute(&self, z: &F) -> F {
  98. *z
  99. }
  100. }