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