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.

148 lines
4.6 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
  1. extern crate core;
  2. extern crate criterion;
  3. extern crate digest;
  4. extern crate libspartan;
  5. extern crate merlin;
  6. extern crate sha3;
  7. use std::time::{Duration, SystemTime};
  8. use libspartan::{
  9. parameters::POSEIDON_PARAMETERS_FR_377, poseidon_transcript::PoseidonTranscript, Instance,
  10. NIZKGens, NIZK,
  11. };
  12. use criterion::*;
  13. fn nizk_prove_benchmark(c: &mut Criterion) {
  14. for &s in [24, 28, 30].iter() {
  15. let mut group = c.benchmark_group("R1CS_prove_benchmark");
  16. let num_vars = (2_usize).pow(s as u32);
  17. let num_cons = num_vars;
  18. let num_inputs = 10;
  19. let start = SystemTime::now();
  20. let (inst, vars, inputs) = Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
  21. let end = SystemTime::now();
  22. let duration = end.duration_since(start).unwrap();
  23. println!(
  24. "Generating r1cs instance with {} constraints took {} ms",
  25. num_cons,
  26. duration.as_millis()
  27. );
  28. let gens = NIZKGens::new(num_cons, num_vars, num_inputs);
  29. let name = format!("R1CS_prove_{}", num_vars);
  30. group
  31. .measurement_time(Duration::from_secs(60))
  32. .bench_function(&name, move |b| {
  33. b.iter(|| {
  34. let mut prover_transcript = PoseidonTranscript::new(&POSEIDON_PARAMETERS_FR_377);
  35. NIZK::prove(
  36. black_box(&inst),
  37. black_box(vars.clone()),
  38. black_box(&inputs),
  39. black_box(&gens),
  40. black_box(&mut prover_transcript),
  41. );
  42. });
  43. });
  44. group.finish();
  45. }
  46. }
  47. fn nizk_verify_benchmark(c: &mut Criterion) {
  48. for &s in [4, 6, 8, 10, 12, 16, 20, 24, 28, 30].iter() {
  49. let mut group = c.benchmark_group("R1CS_verify_benchmark");
  50. let num_vars = (2_usize).pow(s as u32);
  51. let num_cons = num_vars;
  52. // these are the public io
  53. let num_inputs = 10;
  54. let start = SystemTime::now();
  55. let (inst, vars, inputs) = Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
  56. let end = SystemTime::now();
  57. let duration = end.duration_since(start).unwrap();
  58. println!(
  59. "Generating r1cs instance with {} constraints took {} ms",
  60. num_cons,
  61. duration.as_millis()
  62. );
  63. let gens = NIZKGens::new(num_cons, num_vars, num_inputs);
  64. // produce a proof of satisfiability
  65. let mut prover_transcript = PoseidonTranscript::new(&POSEIDON_PARAMETERS_FR_377);
  66. let proof = NIZK::prove(&inst, vars, &inputs, &gens, &mut prover_transcript);
  67. let name = format!("R1CS_verify_{}", num_cons);
  68. group
  69. .measurement_time(Duration::from_secs(60))
  70. .bench_function(&name, move |b| {
  71. b.iter(|| {
  72. let mut verifier_transcript = PoseidonTranscript::new(&POSEIDON_PARAMETERS_FR_377);
  73. assert!(proof
  74. .verify(
  75. black_box(&inst),
  76. black_box(&inputs),
  77. black_box(&mut verifier_transcript),
  78. black_box(&gens),
  79. )
  80. .is_ok());
  81. });
  82. });
  83. group.finish();
  84. }
  85. }
  86. fn nizk_verify_groth16_benchmark(c: &mut Criterion) {
  87. for &s in [4, 6, 8, 10, 12, 16, 20, 24, 28, 30].iter() {
  88. let mut group = c.benchmark_group("R1CS_verify_groth16_benchmark");
  89. let num_vars = (2_usize).pow(s as u32);
  90. let num_cons = num_vars;
  91. // these are the public io
  92. let num_inputs = 10;
  93. let start = SystemTime::now();
  94. let (inst, vars, inputs) = Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
  95. let end = SystemTime::now();
  96. let duration = end.duration_since(start).unwrap();
  97. println!(
  98. "Generating r1cs instance with {} constraints took {} ms",
  99. num_cons,
  100. duration.as_millis()
  101. );
  102. // produce a proof of satisfiability
  103. let mut prover_transcript = PoseidonTranscript::new(&POSEIDON_PARAMETERS_FR_377);
  104. let gens = NIZKGens::new(num_cons, num_vars, num_inputs);
  105. let proof = NIZK::prove(&inst, vars, &inputs, &gens, &mut prover_transcript);
  106. let name = format!("R1CS_verify_groth16_{}", num_cons);
  107. group
  108. .measurement_time(Duration::from_secs(60))
  109. .bench_function(&name, move |b| {
  110. b.iter(|| {
  111. let mut verifier_transcript = PoseidonTranscript::new(&POSEIDON_PARAMETERS_FR_377);
  112. assert!(proof
  113. .verify_groth16(
  114. black_box(&inst),
  115. black_box(&inputs),
  116. black_box(&mut verifier_transcript),
  117. black_box(&gens)
  118. )
  119. .is_ok());
  120. });
  121. });
  122. group.finish();
  123. }
  124. }
  125. fn set_duration() -> Criterion {
  126. Criterion::default().sample_size(2)
  127. }
  128. criterion_group! {
  129. name = benches_nizk;
  130. config = set_duration();
  131. targets = nizk_prove_benchmark, nizk_verify_benchmark, nizk_verify_groth16_benchmark
  132. }
  133. criterion_main!(benches_nizk);