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.

106 lines
3.1 KiB

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