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.

206 lines
6.4 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. extern crate byteorder;
  2. extern crate core;
  3. extern crate criterion;
  4. extern crate digest;
  5. extern crate libspartan;
  6. extern crate merlin;
  7. extern crate rand;
  8. extern crate sha3;
  9. use libspartan::math::Math;
  10. use libspartan::r1csinstance::{R1CSCommitmentGens, R1CSInstance};
  11. use libspartan::r1csproof::R1CSGens;
  12. use libspartan::spartan::{NIZKGens, SNARKGens, NIZK, SNARK};
  13. use merlin::Transcript;
  14. use criterion::*;
  15. fn snark_encode_benchmark(c: &mut Criterion) {
  16. for &s in [10, 12, 16].iter() {
  17. let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
  18. let mut group = c.benchmark_group("SNARK_encode_benchmark");
  19. group.plot_config(plot_config);
  20. let num_vars = s.pow2();
  21. let num_cons = num_vars;
  22. let num_inputs = 10;
  23. let (inst, _vars, _input) =
  24. R1CSInstance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
  25. let n = inst.get_num_vars();
  26. let r1cs_size = inst.size();
  27. let gens_r1cs = R1CSCommitmentGens::new(&r1cs_size, b"gens_r1cs");
  28. let name = format!("SNARK_encode_{}", n);
  29. group.bench_function(&name, move |b| {
  30. b.iter(|| {
  31. SNARK::encode(black_box(&inst), black_box(&gens_r1cs));
  32. });
  33. });
  34. group.finish();
  35. }
  36. }
  37. fn snark_prove_benchmark(c: &mut Criterion) {
  38. for &s in [10, 12, 16].iter() {
  39. let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
  40. let mut group = c.benchmark_group("SNARK_prove_benchmark");
  41. group.plot_config(plot_config);
  42. let num_vars = s.pow2();
  43. let num_cons = num_vars;
  44. let num_inputs = 10;
  45. let (inst, vars, input) = R1CSInstance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
  46. let n = inst.get_num_vars();
  47. let r1cs_size = inst.size();
  48. let gens_r1cs_eval = R1CSCommitmentGens::new(&r1cs_size, b"gens_r1cs_eval");
  49. let gens_r1cs_sat = R1CSGens::new(num_cons, num_vars, b"gens_r1cs_sat");
  50. // produce a proof of satisfiability
  51. let (_comm, decomm) = SNARK::encode(&inst, &gens_r1cs_eval);
  52. let gens = SNARKGens::new(gens_r1cs_sat, gens_r1cs_eval);
  53. let name = format!("SNARK_prove_{}", n);
  54. group.bench_function(&name, move |b| {
  55. b.iter(|| {
  56. let mut prover_transcript = Transcript::new(b"example");
  57. SNARK::prove(
  58. black_box(&inst),
  59. black_box(&decomm),
  60. black_box(vars.clone()),
  61. black_box(&input),
  62. black_box(&gens),
  63. black_box(&mut prover_transcript),
  64. );
  65. });
  66. });
  67. group.finish();
  68. }
  69. }
  70. fn snark_verify_benchmark(c: &mut Criterion) {
  71. for &s in [10, 12, 16].iter() {
  72. let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
  73. let mut group = c.benchmark_group("SNARK_verify_benchmark");
  74. group.plot_config(plot_config);
  75. let num_vars = s.pow2();
  76. let num_cons = num_vars;
  77. let num_inputs = 10;
  78. let (inst, vars, input) = R1CSInstance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
  79. let n = inst.get_num_vars();
  80. let r1cs_size = inst.size();
  81. let gens_r1cs_eval = R1CSCommitmentGens::new(&r1cs_size, b"gens_r1cs_eval");
  82. // create a commitment to R1CSInstance
  83. let (comm, decomm) = SNARK::encode(&inst, &gens_r1cs_eval);
  84. let gens_r1cs_sat = R1CSGens::new(num_cons, num_vars, b"gens_r1cs_sat");
  85. let gens = SNARKGens::new(gens_r1cs_sat, gens_r1cs_eval);
  86. // produce a proof of satisfiability
  87. let mut prover_transcript = Transcript::new(b"example");
  88. let proof = SNARK::prove(&inst, &decomm, vars, &input, &gens, &mut prover_transcript);
  89. let name = format!("SNARK_verify_{}", n);
  90. group.bench_function(&name, move |b| {
  91. b.iter(|| {
  92. let mut verifier_transcript = Transcript::new(b"example");
  93. assert!(proof
  94. .verify(
  95. black_box(&comm),
  96. black_box(&input),
  97. black_box(&mut verifier_transcript),
  98. black_box(&gens)
  99. )
  100. .is_ok());
  101. });
  102. });
  103. group.finish();
  104. }
  105. }
  106. fn nizk_prove_benchmark(c: &mut Criterion) {
  107. for &s in [10, 12, 16].iter() {
  108. let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
  109. let mut group = c.benchmark_group("NIZK_prove_benchmark");
  110. group.plot_config(plot_config);
  111. let num_vars = s.pow2();
  112. let num_cons = num_vars;
  113. let num_inputs = 10;
  114. let (inst, vars, input) = R1CSInstance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
  115. let n = inst.get_num_vars();
  116. let gens_r1cs_sat = R1CSGens::new(num_cons, num_vars, b"gens_r1cs_sat");
  117. let gens = NIZKGens::new(gens_r1cs_sat);
  118. let name = format!("NIZK_prove_{}", n);
  119. group.bench_function(&name, move |b| {
  120. b.iter(|| {
  121. let mut prover_transcript = Transcript::new(b"example");
  122. NIZK::prove(
  123. black_box(&inst),
  124. black_box(vars.clone()),
  125. black_box(&input),
  126. black_box(&gens),
  127. black_box(&mut prover_transcript),
  128. );
  129. });
  130. });
  131. group.finish();
  132. }
  133. }
  134. fn nizk_verify_benchmark(c: &mut Criterion) {
  135. for &s in [10, 12, 16].iter() {
  136. let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
  137. let mut group = c.benchmark_group("NIZK_verify_benchmark");
  138. group.plot_config(plot_config);
  139. let num_vars = s.pow2();
  140. let num_cons = num_vars;
  141. let num_inputs = 10;
  142. let (inst, vars, input) = R1CSInstance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
  143. let n = inst.get_num_vars();
  144. let gens_r1cs_sat = R1CSGens::new(num_cons, num_vars, b"gens_r1cs_sat");
  145. let gens = NIZKGens::new(gens_r1cs_sat);
  146. // produce a proof of satisfiability
  147. let mut prover_transcript = Transcript::new(b"example");
  148. let proof = NIZK::prove(&inst, vars, &input, &gens, &mut prover_transcript);
  149. let name = format!("NIZK_verify_{}", n);
  150. group.bench_function(&name, move |b| {
  151. b.iter(|| {
  152. let mut verifier_transcript = Transcript::new(b"example");
  153. assert!(proof
  154. .verify(
  155. black_box(&inst),
  156. black_box(&input),
  157. black_box(&mut verifier_transcript),
  158. black_box(&gens)
  159. )
  160. .is_ok());
  161. });
  162. });
  163. group.finish();
  164. }
  165. }
  166. fn set_duration() -> Criterion {
  167. Criterion::default().sample_size(10)
  168. // .measurement_time(Duration::new(0, 50000000))
  169. }
  170. criterion_group! {
  171. name = benches_spartan;
  172. config = set_duration();
  173. targets = snark_encode_benchmark, snark_prove_benchmark, snark_verify_benchmark, nizk_prove_benchmark, nizk_verify_benchmark
  174. }
  175. criterion_main!(benches_spartan);