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.

158 lines
4.3 KiB

  1. #![allow(non_snake_case)]
  2. use bellperson::{gadgets::num::AllocatedNum, ConstraintSystem, SynthesisError};
  3. use core::marker::PhantomData;
  4. use criterion::*;
  5. use ff::PrimeField;
  6. use nova_snark::{
  7. traits::{
  8. circuit::{StepCircuit, TrivialTestCircuit},
  9. Group,
  10. },
  11. PublicParams, RecursiveSNARK,
  12. };
  13. use std::time::Duration;
  14. type G1 = pasta_curves::pallas::Point;
  15. type G2 = pasta_curves::vesta::Point;
  16. type C1 = NonTrivialTestCircuit<<G1 as Group>::Scalar>;
  17. type C2 = TrivialTestCircuit<<G2 as Group>::Scalar>;
  18. criterion_group! {
  19. name = recursive_snark;
  20. config = Criterion::default().warm_up_time(Duration::from_millis(3000));
  21. targets = bench_recursive_snark
  22. }
  23. criterion_main!(recursive_snark);
  24. fn bench_recursive_snark(c: &mut Criterion) {
  25. let num_cons_verifier_circuit_primary = 20584;
  26. // we vary the number of constraints in the step circuit
  27. for &num_cons_in_augmented_circuit in
  28. [20584, 32768, 65536, 131072, 262144, 524288, 1048576].iter()
  29. {
  30. // number of constraints in the step circuit
  31. let num_cons = num_cons_in_augmented_circuit - num_cons_verifier_circuit_primary;
  32. let mut group = c.benchmark_group(format!("RecursiveSNARK-StepCircuitSize-{}", num_cons));
  33. group.sample_size(10);
  34. // Produce public parameters
  35. let pp = PublicParams::<G1, G2, C1, C2>::setup(
  36. NonTrivialTestCircuit::new(num_cons),
  37. TrivialTestCircuit::default(),
  38. );
  39. // Bench time to produce a recursive SNARK;
  40. // we execute a certain number of warm-up steps since executing
  41. // the first step is cheaper than other steps owing to the presence of
  42. // a lot of zeros in the satisfying assignment
  43. let num_warmup_steps = 10;
  44. let mut recursive_snark: Option<RecursiveSNARK<G1, G2, C1, C2>> = None;
  45. for i in 0..num_warmup_steps {
  46. let res = RecursiveSNARK::prove_step(
  47. &pp,
  48. recursive_snark,
  49. NonTrivialTestCircuit::new(num_cons),
  50. TrivialTestCircuit::default(),
  51. <G1 as Group>::Scalar::from(2u64),
  52. <G2 as Group>::Scalar::from(2u64),
  53. );
  54. assert!(res.is_ok());
  55. let recursive_snark_unwrapped = res.unwrap();
  56. // verify the recursive snark at each step of recursion
  57. let res = recursive_snark_unwrapped.verify(
  58. &pp,
  59. i + 1,
  60. <G1 as Group>::Scalar::from(2u64),
  61. <G2 as Group>::Scalar::from(2u64),
  62. );
  63. assert!(res.is_ok());
  64. // set the running variable for the next iteration
  65. recursive_snark = Some(recursive_snark_unwrapped);
  66. }
  67. group.bench_function("Prove", |b| {
  68. b.iter(|| {
  69. // produce a recursive SNARK for a step of the recursion
  70. assert!(RecursiveSNARK::prove_step(
  71. black_box(&pp),
  72. black_box(recursive_snark.clone()),
  73. black_box(NonTrivialTestCircuit::new(num_cons)),
  74. black_box(TrivialTestCircuit::default()),
  75. black_box(<G1 as Group>::Scalar::from(2u64)),
  76. black_box(<G2 as Group>::Scalar::from(2u64)),
  77. )
  78. .is_ok());
  79. })
  80. });
  81. let recursive_snark = recursive_snark.unwrap();
  82. // Benchmark the verification time
  83. group.bench_function("Verify", |b| {
  84. b.iter(|| {
  85. assert!(black_box(&recursive_snark)
  86. .verify(
  87. black_box(&pp),
  88. black_box(num_warmup_steps),
  89. black_box(<G1 as Group>::Scalar::from(2u64)),
  90. black_box(<G2 as Group>::Scalar::from(2u64)),
  91. )
  92. .is_ok());
  93. });
  94. });
  95. group.finish();
  96. }
  97. }
  98. #[derive(Clone, Debug, Default)]
  99. struct NonTrivialTestCircuit<F: PrimeField> {
  100. num_cons: usize,
  101. _p: PhantomData<F>,
  102. }
  103. impl<F> NonTrivialTestCircuit<F>
  104. where
  105. F: PrimeField,
  106. {
  107. pub fn new(num_cons: usize) -> Self {
  108. Self {
  109. num_cons,
  110. _p: Default::default(),
  111. }
  112. }
  113. }
  114. impl<F> StepCircuit<F> for NonTrivialTestCircuit<F>
  115. where
  116. F: PrimeField,
  117. {
  118. fn synthesize<CS: ConstraintSystem<F>>(
  119. &self,
  120. cs: &mut CS,
  121. z: AllocatedNum<F>,
  122. ) -> Result<AllocatedNum<F>, SynthesisError> {
  123. // Consider a an equation: `x^2 = y`, where `x` and `y` are respectively the input and output.
  124. let mut x = z;
  125. let mut y = x.clone();
  126. for i in 0..self.num_cons {
  127. y = x.square(cs.namespace(|| format!("x_sq_{}", i)))?;
  128. x = y.clone();
  129. }
  130. Ok(y)
  131. }
  132. fn output(&self, z: &F) -> F {
  133. let mut x = *z;
  134. let mut y = x;
  135. for _i in 0..self.num_cons {
  136. y = x * x;
  137. x = y;
  138. }
  139. y
  140. }
  141. }