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.

105 lines
3.1 KiB

  1. use criterion::{black_box, criterion_group, criterion_main, Criterion};
  2. use ark_circom::{read_zkey, CircomReduction, WitnessCalculator};
  3. use ark_std::rand::thread_rng;
  4. use ark_bn254::Bn254;
  5. use ark_groth16::{create_proof_with_reduction_and_matrices, prepare_verifying_key, verify_proof};
  6. use std::{collections::HashMap, fs::File};
  7. fn bench_groth(c: &mut Criterion, num_validators: u32, num_constraints: u32) {
  8. let i = num_validators;
  9. let j = num_constraints;
  10. let path = format!(
  11. "./test-vectors/complex-circuit/complex-circuit-{}-{}.zkey",
  12. i, j
  13. );
  14. let mut file = File::open(&path).unwrap();
  15. let (params, matrices) = read_zkey(&mut file).unwrap();
  16. let num_inputs = matrices.num_instance_variables;
  17. let num_constraints = matrices.num_constraints;
  18. let inputs = {
  19. let mut inputs: HashMap<String, Vec<num_bigint::BigInt>> = HashMap::new();
  20. let values = inputs.entry("a".to_string()).or_insert_with(Vec::new);
  21. values.push(3.into());
  22. inputs
  23. };
  24. let mut wtns = WitnessCalculator::new(&format!(
  25. "./test-vectors/complex-circuit/complex-circuit-{}-{}.wasm",
  26. i, j
  27. ))
  28. .unwrap();
  29. let full_assignment = wtns
  30. .calculate_witness_element::<Bn254, _>(inputs, false)
  31. .unwrap();
  32. let mut rng = thread_rng();
  33. use ark_std::UniformRand;
  34. let rng = &mut rng;
  35. let r = ark_bn254::Fr::rand(rng);
  36. let s = ark_bn254::Fr::rand(rng);
  37. let proof = create_proof_with_reduction_and_matrices::<_, CircomReduction>(
  38. &params,
  39. r,
  40. s,
  41. &matrices,
  42. num_inputs,
  43. num_constraints,
  44. full_assignment.as_slice(),
  45. )
  46. .unwrap();
  47. let pvk = prepare_verifying_key(&params.vk);
  48. let inputs = &full_assignment[1..num_inputs];
  49. let verified = verify_proof(&pvk, &proof, inputs).unwrap();
  50. assert!(verified);
  51. c.bench_function(&format!("groth proof {} {}", i, j), |b| {
  52. b.iter(|| {
  53. black_box(
  54. create_proof_with_reduction_and_matrices::<_, CircomReduction>(
  55. &params,
  56. r,
  57. s,
  58. &matrices,
  59. num_inputs,
  60. num_constraints,
  61. full_assignment.as_slice(),
  62. )
  63. .unwrap(),
  64. );
  65. })
  66. });
  67. }
  68. cfg_if::cfg_if! {
  69. if #[cfg(feature = "bench-complex-all")] {
  70. const MIN_NUM_VARIABLES_POWER: u32 = 3;
  71. const MAX_NUM_VARIABLES_POWER: u32 = 5;
  72. const MAX_NUM_CONSTRAINTS_POWER: u32 = 5;
  73. fn groth_all(c: &mut Criterion) {
  74. for i in MIN_NUM_VARIABLES_POWER..=MAX_NUM_VARIABLES_POWER {
  75. for j in i..=MAX_NUM_CONSTRAINTS_POWER {
  76. let i = 10_u32.pow(i);
  77. let j = 10_u32.pow(j);
  78. bench_groth(c, i, j);
  79. }
  80. }
  81. }
  82. criterion_group!(benches, groth_all);
  83. } else {
  84. fn groth(c: &mut Criterion) {
  85. bench_groth(c, 10000, 10000);
  86. }
  87. criterion_group!(benches, groth);
  88. }
  89. }
  90. criterion_main!(benches);