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.

167 lines
4.6 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. CompressedSNARK, PublicParams, RecursiveSNARK,
  12. };
  13. use std::time::Duration;
  14. type G1 = pasta_curves::pallas::Point;
  15. type G2 = pasta_curves::vesta::Point;
  16. type EE1 = nova_snark::provider::ipa_pc::EvaluationEngine<G1>;
  17. type EE2 = nova_snark::provider::ipa_pc::EvaluationEngine<G2>;
  18. type S1 = nova_snark::spartan::RelaxedR1CSSNARK<G1, EE1>;
  19. type S2 = nova_snark::spartan::RelaxedR1CSSNARK<G2, EE2>;
  20. type C1 = NonTrivialTestCircuit<<G1 as Group>::Scalar>;
  21. type C2 = TrivialTestCircuit<<G2 as Group>::Scalar>;
  22. criterion_group! {
  23. name = compressed_snark;
  24. config = Criterion::default().warm_up_time(Duration::from_millis(3000));
  25. targets = bench_compressed_snark
  26. }
  27. criterion_main!(compressed_snark);
  28. fn bench_compressed_snark(c: &mut Criterion) {
  29. let num_samples = 10;
  30. let num_cons_verifier_circuit_primary = 9819;
  31. // we vary the number of constraints in the step circuit
  32. for &num_cons_in_augmented_circuit in
  33. [9819, 16384, 32768, 65536, 131072, 262144, 524288, 1048576].iter()
  34. {
  35. // number of constraints in the step circuit
  36. let num_cons = num_cons_in_augmented_circuit - num_cons_verifier_circuit_primary;
  37. let mut group = c.benchmark_group(format!("CompressedSNARK-StepCircuitSize-{num_cons}"));
  38. group.sample_size(num_samples);
  39. // Produce public parameters
  40. let pp = PublicParams::<G1, G2, C1, C2>::setup(
  41. NonTrivialTestCircuit::new(num_cons),
  42. TrivialTestCircuit::default(),
  43. );
  44. // Produce prover and verifier keys for CompressedSNARK
  45. let (pk, vk) = CompressedSNARK::<_, _, _, _, S1, S2>::setup(&pp);
  46. // produce a recursive SNARK
  47. let num_steps = 3;
  48. let mut recursive_snark: Option<RecursiveSNARK<G1, G2, C1, C2>> = None;
  49. for i in 0..num_steps {
  50. let res = RecursiveSNARK::prove_step(
  51. &pp,
  52. recursive_snark,
  53. NonTrivialTestCircuit::new(num_cons),
  54. TrivialTestCircuit::default(),
  55. vec![<G1 as Group>::Scalar::from(2u64)],
  56. vec![<G2 as Group>::Scalar::from(2u64)],
  57. );
  58. assert!(res.is_ok());
  59. let recursive_snark_unwrapped = res.unwrap();
  60. // verify the recursive snark at each step of recursion
  61. let res = recursive_snark_unwrapped.verify(
  62. &pp,
  63. i + 1,
  64. vec![<G1 as Group>::Scalar::from(2u64)],
  65. vec![<G2 as Group>::Scalar::from(2u64)],
  66. );
  67. assert!(res.is_ok());
  68. // set the running variable for the next iteration
  69. recursive_snark = Some(recursive_snark_unwrapped);
  70. }
  71. // Bench time to produce a compressed SNARK
  72. let recursive_snark = recursive_snark.unwrap();
  73. group.bench_function("Prove", |b| {
  74. b.iter(|| {
  75. assert!(CompressedSNARK::<_, _, _, _, S1, S2>::prove(
  76. black_box(&pp),
  77. black_box(&pk),
  78. black_box(&recursive_snark)
  79. )
  80. .is_ok());
  81. })
  82. });
  83. let res = CompressedSNARK::<_, _, _, _, S1, S2>::prove(&pp, &pk, &recursive_snark);
  84. assert!(res.is_ok());
  85. let compressed_snark = res.unwrap();
  86. // Benchmark the verification time
  87. group.bench_function("Verify", |b| {
  88. b.iter(|| {
  89. assert!(black_box(&compressed_snark)
  90. .verify(
  91. black_box(&vk),
  92. black_box(num_steps),
  93. black_box(vec![<G1 as Group>::Scalar::from(2u64)]),
  94. black_box(vec![<G2 as Group>::Scalar::from(2u64)]),
  95. )
  96. .is_ok());
  97. })
  98. });
  99. group.finish();
  100. }
  101. }
  102. #[derive(Clone, Debug, Default)]
  103. struct NonTrivialTestCircuit<F: PrimeField> {
  104. num_cons: usize,
  105. _p: PhantomData<F>,
  106. }
  107. impl<F> NonTrivialTestCircuit<F>
  108. where
  109. F: PrimeField,
  110. {
  111. pub fn new(num_cons: usize) -> Self {
  112. Self {
  113. num_cons,
  114. _p: Default::default(),
  115. }
  116. }
  117. }
  118. impl<F> StepCircuit<F> for NonTrivialTestCircuit<F>
  119. where
  120. F: PrimeField,
  121. {
  122. fn arity(&self) -> usize {
  123. 1
  124. }
  125. fn synthesize<CS: ConstraintSystem<F>>(
  126. &self,
  127. cs: &mut CS,
  128. z: &[AllocatedNum<F>],
  129. ) -> Result<Vec<AllocatedNum<F>>, SynthesisError> {
  130. // Consider a an equation: `x^2 = y`, where `x` and `y` are respectively the input and output.
  131. let mut x = z[0].clone();
  132. let mut y = x.clone();
  133. for i in 0..self.num_cons {
  134. y = x.square(cs.namespace(|| format!("x_sq_{i}")))?;
  135. x = y.clone();
  136. }
  137. Ok(vec![y])
  138. }
  139. fn output(&self, z: &[F]) -> Vec<F> {
  140. let mut x = z[0];
  141. let mut y = x;
  142. for _i in 0..self.num_cons {
  143. y = x * x;
  144. x = y;
  145. }
  146. vec![y]
  147. }
  148. }