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.

122 lines
2.9 KiB

  1. #![allow(non_snake_case)]
  2. extern crate flate2;
  3. //use flate2::{write::ZlibEncoder, Compression};
  4. use nova_snark::{
  5. traits::{Group, StepCircuit},
  6. CompressedSNARK, PublicParams, RecursiveSNARK,
  7. };
  8. type G1 = pasta_curves::pallas::Point;
  9. type G2 = pasta_curves::vesta::Point;
  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::prove(black_box(&pp), black_box(&recursive_snark)).is_ok());
  59. })
  60. });
  61. let res = CompressedSNARK::prove(&pp, &recursive_snark);
  62. assert!(res.is_ok());
  63. let compressed_snark = res.unwrap();
  64. // Output the proof size
  65. //let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
  66. //bincode::serialize_into(&mut encoder, &compressed_snark).unwrap();
  67. //let proof_encoded = encoder.finish().unwrap();
  68. //println!(
  69. // "ProofSize: {} B",
  70. // proof_encoded.len(),
  71. //);
  72. // Benchmark the verification time
  73. let name = "Verify";
  74. group.bench_function(name, |b| {
  75. b.iter(|| {
  76. assert!(black_box(&compressed_snark)
  77. .verify(
  78. black_box(&pp),
  79. black_box(num_steps),
  80. black_box(<G1 as Group>::Scalar::zero()),
  81. black_box(<G2 as Group>::Scalar::zero()),
  82. )
  83. .is_ok());
  84. })
  85. });
  86. group.finish();
  87. }
  88. #[derive(Clone, Debug)]
  89. struct TrivialTestCircuit<F: PrimeField> {
  90. _p: PhantomData<F>,
  91. }
  92. impl<F> StepCircuit<F> for TrivialTestCircuit<F>
  93. where
  94. F: PrimeField,
  95. {
  96. fn synthesize<CS: ConstraintSystem<F>>(
  97. &self,
  98. _cs: &mut CS,
  99. z: AllocatedNum<F>,
  100. ) -> Result<AllocatedNum<F>, SynthesisError> {
  101. Ok(z)
  102. }
  103. fn compute(&self, z: &F) -> F {
  104. *z
  105. }
  106. }