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.

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