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