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.

175 lines
5.1 KiB

[refactorings] Leftovers (pot-pourri?) (#184) * test: compute_path * refactor: path computation - Improve path concatenation by utilizing built-in `join` method * refactor: replace `PartialEq` with derived instance - Derive `PartialEq` for `SatisfyingAssignment` struct - Remove redundant manual implementation of `PartialEq` Cargo-expand generates: ``` #[automatically_derived] impl<G: ::core::cmp::PartialEq + Group> ::core::cmp::PartialEq for SatisfyingAssignment<G> where G::Scalar: PrimeField, G::Scalar: ::core::cmp::PartialEq, G::Scalar: ::core::cmp::PartialEq, G::Scalar: ::core::cmp::PartialEq, G::Scalar: ::core::cmp::PartialEq, G::Scalar: ::core::cmp::PartialEq, { #[inline] fn eq(&self, other: &SatisfyingAssignment<G>) -> bool { self.a_aux_density == other.a_aux_density && self.b_input_density == other.b_input_density && self.b_aux_density == other.b_aux_density && self.a == other.a && self.b == other.b && self.c == other.c && self.input_assignment == other.input_assignment && self.aux_assignment == other.aux_assignment } } ``` * refactor: avoid default for PhantomData Unit type * refactor: replace fold with sum where applicable - Simplify code by replacing `fold` with `sum` in various instances * refactor: decompression method in sumcheck.rs * refactor: test functions to use slice instead of vector conversion * refactor: use more references in functions - Update parameter types to use references instead of owned values in various functions that do not need them - Replace cloning instances with references
1 year ago
  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. // To run these benchmarks, first download `criterion` with `cargo install cargo install cargo-criterion`.
  19. // Then `cargo criterion --bench recursive-snark`. The results are located in `target/criterion/data/<name-of-benchmark>`.
  20. // For flamegraphs, run `cargo criterion --bench recursive-snark --features flamegraph -- --profile-time <secs>`.
  21. // The results are located in `target/criterion/profile/<name-of-benchmark>`.
  22. cfg_if::cfg_if! {
  23. if #[cfg(feature = "flamegraph")] {
  24. criterion_group! {
  25. name = recursive_snark;
  26. config = Criterion::default().warm_up_time(Duration::from_millis(3000)).with_profiler(pprof::criterion::PProfProfiler::new(100, pprof::criterion::Output::Flamegraph(None)));
  27. targets = bench_recursive_snark
  28. }
  29. } else {
  30. criterion_group! {
  31. name = recursive_snark;
  32. config = Criterion::default().warm_up_time(Duration::from_millis(3000));
  33. targets = bench_recursive_snark
  34. }
  35. }
  36. }
  37. criterion_main!(recursive_snark);
  38. fn bench_recursive_snark(c: &mut Criterion) {
  39. let num_cons_verifier_circuit_primary = 9819;
  40. // we vary the number of constraints in the step circuit
  41. for &num_cons_in_augmented_circuit in
  42. [9819, 16384, 32768, 65536, 131072, 262144, 524288, 1048576].iter()
  43. {
  44. // number of constraints in the step circuit
  45. let num_cons = num_cons_in_augmented_circuit - num_cons_verifier_circuit_primary;
  46. let mut group = c.benchmark_group(format!("RecursiveSNARK-StepCircuitSize-{num_cons}"));
  47. group.sample_size(10);
  48. let c_primary = NonTrivialTestCircuit::new(num_cons);
  49. let c_secondary = TrivialTestCircuit::default();
  50. // Produce public parameters
  51. let pp = PublicParams::<G1, G2, C1, C2>::setup(c_primary.clone(), c_secondary.clone());
  52. // Bench time to produce a recursive SNARK;
  53. // we execute a certain number of warm-up steps since executing
  54. // the first step is cheaper than other steps owing to the presence of
  55. // a lot of zeros in the satisfying assignment
  56. let num_warmup_steps = 10;
  57. let mut recursive_snark: RecursiveSNARK<G1, G2, C1, C2> = RecursiveSNARK::new(
  58. &pp,
  59. &c_primary,
  60. &c_secondary,
  61. vec![<G1 as Group>::Scalar::from(2u64)],
  62. vec![<G2 as Group>::Scalar::from(2u64)],
  63. );
  64. for i in 0..num_warmup_steps {
  65. let res = recursive_snark.prove_step(
  66. &pp,
  67. &c_primary,
  68. &c_secondary,
  69. vec![<G1 as Group>::Scalar::from(2u64)],
  70. vec![<G2 as Group>::Scalar::from(2u64)],
  71. );
  72. assert!(res.is_ok());
  73. // verify the recursive snark at each step of recursion
  74. let res = recursive_snark.verify(
  75. &pp,
  76. i + 1,
  77. &[<G1 as Group>::Scalar::from(2u64)],
  78. &[<G2 as Group>::Scalar::from(2u64)],
  79. );
  80. assert!(res.is_ok());
  81. }
  82. group.bench_function("Prove", |b| {
  83. b.iter(|| {
  84. // produce a recursive SNARK for a step of the recursion
  85. assert!(black_box(&mut recursive_snark.clone())
  86. .prove_step(
  87. black_box(&pp),
  88. black_box(&c_primary),
  89. black_box(&c_secondary),
  90. black_box(vec![<G1 as Group>::Scalar::from(2u64)]),
  91. black_box(vec![<G2 as Group>::Scalar::from(2u64)]),
  92. )
  93. .is_ok());
  94. })
  95. });
  96. // Benchmark the verification time
  97. group.bench_function("Verify", |b| {
  98. b.iter(|| {
  99. assert!(black_box(&recursive_snark)
  100. .verify(
  101. black_box(&pp),
  102. black_box(num_warmup_steps),
  103. black_box(&[<G1 as Group>::Scalar::from(2u64)]),
  104. black_box(&[<G2 as Group>::Scalar::from(2u64)]),
  105. )
  106. .is_ok());
  107. });
  108. });
  109. group.finish();
  110. }
  111. }
  112. #[derive(Clone, Debug, Default)]
  113. struct NonTrivialTestCircuit<F: PrimeField> {
  114. num_cons: usize,
  115. _p: PhantomData<F>,
  116. }
  117. impl<F> NonTrivialTestCircuit<F>
  118. where
  119. F: PrimeField,
  120. {
  121. pub fn new(num_cons: usize) -> Self {
  122. Self {
  123. num_cons,
  124. _p: Default::default(),
  125. }
  126. }
  127. }
  128. impl<F> StepCircuit<F> for NonTrivialTestCircuit<F>
  129. where
  130. F: PrimeField,
  131. {
  132. fn arity(&self) -> usize {
  133. 1
  134. }
  135. fn synthesize<CS: ConstraintSystem<F>>(
  136. &self,
  137. cs: &mut CS,
  138. z: &[AllocatedNum<F>],
  139. ) -> Result<Vec<AllocatedNum<F>>, SynthesisError> {
  140. // Consider a an equation: `x^2 = y`, where `x` and `y` are respectively the input and output.
  141. let mut x = z[0].clone();
  142. let mut y = x.clone();
  143. for i in 0..self.num_cons {
  144. y = x.square(cs.namespace(|| format!("x_sq_{i}")))?;
  145. x = y.clone();
  146. }
  147. Ok(vec![y])
  148. }
  149. fn output(&self, z: &[F]) -> Vec<F> {
  150. let mut x = z[0];
  151. let mut y = x;
  152. for _i in 0..self.num_cons {
  153. y = x * x;
  154. x = y;
  155. }
  156. vec![y]
  157. }
  158. }