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.

116 lines
2.7 KiB

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