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.

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