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.

137 lines
4.3 KiB

  1. extern crate libspartan;
  2. extern crate merlin;
  3. use libspartan::{
  4. parameters::poseidon_params, poseidon_transcript::PoseidonTranscript, Instance, SNARKGens,
  5. SNARK,
  6. };
  7. use criterion::*;
  8. fn snark_encode_benchmark(c: &mut Criterion) {
  9. for &s in [10, 12, 16].iter() {
  10. let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
  11. let mut group = c.benchmark_group("SNARK_encode_benchmark");
  12. group.plot_config(plot_config);
  13. let num_vars = (2_usize).pow(s as u32);
  14. let num_cons = num_vars;
  15. let num_inputs = 10;
  16. let (inst, _vars, _inputs) =
  17. Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
  18. // produce public parameters
  19. let gens = SNARKGens::new(num_cons, num_vars, num_inputs, num_cons);
  20. // produce a commitment to R1CS instance
  21. let name = format!("SNARK_encode_{}", num_cons);
  22. group.bench_function(&name, move |b| {
  23. b.iter(|| {
  24. SNARK::encode(black_box(&inst), black_box(&gens));
  25. });
  26. });
  27. group.finish();
  28. }
  29. }
  30. fn snark_prove_benchmark(c: &mut Criterion) {
  31. for &s in [10, 12, 16].iter() {
  32. let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
  33. let mut group = c.benchmark_group("SNARK_prove_benchmark");
  34. group.plot_config(plot_config);
  35. let num_vars = (2_usize).pow(s as u32);
  36. let num_cons = num_vars;
  37. let num_inputs = 10;
  38. let params = poseidon_params();
  39. let (inst, vars, inputs) = Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
  40. // produce public parameters
  41. let gens = SNARKGens::new(num_cons, num_vars, num_inputs, num_cons);
  42. // produce a commitment to R1CS instance
  43. let (comm, decomm) = SNARK::encode(&inst, &gens);
  44. // produce a proof
  45. let name = format!("SNARK_prove_{}", num_cons);
  46. group.bench_function(&name, move |b| {
  47. b.iter(|| {
  48. let mut prover_transcript = PoseidonTranscript::new(&params);
  49. SNARK::prove(
  50. black_box(&inst),
  51. black_box(&comm),
  52. black_box(&decomm),
  53. black_box(vars.clone()),
  54. black_box(&inputs),
  55. black_box(&gens),
  56. black_box(&mut prover_transcript),
  57. );
  58. });
  59. });
  60. group.finish();
  61. }
  62. }
  63. fn snark_verify_benchmark(c: &mut Criterion) {
  64. for &s in [10, 12, 16].iter() {
  65. let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
  66. let mut group = c.benchmark_group("SNARK_verify_benchmark");
  67. group.plot_config(plot_config);
  68. let params = poseidon_params();
  69. let num_vars = (2_usize).pow(s as u32);
  70. let num_cons = num_vars;
  71. let num_inputs = 10;
  72. let (inst, vars, inputs) = Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
  73. // produce public parameters
  74. let gens = SNARKGens::new(num_cons, num_vars, num_inputs, num_cons);
  75. // produce a commitment to R1CS instance
  76. let (comm, decomm) = SNARK::encode(&inst, &gens);
  77. // produce a proof of satisfiability
  78. let mut prover_transcript = PoseidonTranscript::new(&params);
  79. let proof = SNARK::prove(
  80. &inst,
  81. &comm,
  82. &decomm,
  83. vars,
  84. &inputs,
  85. &gens,
  86. &mut prover_transcript,
  87. );
  88. // verify the proof
  89. let name = format!("SNARK_verify_{}", num_cons);
  90. group.bench_function(&name, move |b| {
  91. b.iter(|| {
  92. let mut verifier_transcript = PoseidonTranscript::new(&params);
  93. assert!(proof
  94. .verify(
  95. black_box(&comm),
  96. black_box(&inputs),
  97. black_box(&mut verifier_transcript),
  98. black_box(&gens)
  99. )
  100. .is_ok());
  101. });
  102. });
  103. group.finish();
  104. }
  105. }
  106. fn set_duration() -> Criterion {
  107. Criterion::default().sample_size(10)
  108. }
  109. criterion_group! {
  110. name = benches_snark;
  111. config = set_duration();
  112. targets = snark_verify_benchmark
  113. }
  114. criterion_main!(benches_snark);