tests/bench: delete bench_zk, add zk_arith (uses criterion crate)

This commit is contained in:
zero
2024-03-26 17:19:08 +01:00
parent f8f446f916
commit afa1856236
3 changed files with 180 additions and 36 deletions

71
bench/zk_arith.rs Normal file
View File

@@ -0,0 +1,71 @@
/* This file is part of DarkFi (https://dark.fi)
*
* Copyright (C) 2020-2024 Dyne.org foundation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use halo2_proofs::{circuit::Value, pasta::Fp};
use rand::rngs::OsRng;
use darkfi::{
zk::{
proof::{ProvingKey, VerifyingKey},
vm::ZkCircuit,
vm_heap::{empty_witnesses, Witness},
Proof,
},
zkas::ZkBinary,
};
fn zk_arith_bench(c: &mut Criterion) {
let bincode = include_bytes!("../proof/arithmetic.zk.bin");
let zkbin = ZkBinary::decode(bincode).unwrap();
let a = Fp::from(4);
let b = Fp::from(110);
let prover_witnesses = vec![Witness::Base(Value::known(a)), Witness::Base(Value::known(b))];
let public_inputs = vec![a + b, a * b, a - b];
let circuit = ZkCircuit::new(prover_witnesses.clone(), &zkbin);
let mut prove_group = c.benchmark_group("prove");
for k in 11..20 {
let proving_key = ProvingKey::build(k, &circuit.clone());
prove_group.bench_with_input(BenchmarkId::from_parameter(k), &k, |b, &_k| {
b.iter(|| Proof::create(&proving_key, &[circuit.clone()], &public_inputs, &mut OsRng))
});
}
prove_group.finish();
let mut verif_group = c.benchmark_group("verify");
for k in 11..20 {
let proving_key = ProvingKey::build(k, &circuit.clone());
let proof =
Proof::create(&proving_key, &[circuit.clone()], &public_inputs, &mut OsRng).unwrap();
let verifier_witnesses = empty_witnesses(&zkbin).unwrap();
let circuit = ZkCircuit::new(verifier_witnesses, &zkbin);
let verifying_key = VerifyingKey::build(k, &circuit);
verif_group.bench_with_input(BenchmarkId::from_parameter(k), &k, |b, &_k| {
b.iter(|| proof.verify(&verifying_key, &public_inputs))
});
}
verif_group.finish();
}
criterion_group!(benches, zk_arith_bench);
criterion_main!(benches);