zk_arith.rs
1 /* This file is part of DarkFi (https://dark.fi) 2 * 3 * Copyright (C) 2020-2025 Dyne.org foundation 4 * 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU Affero General Public License as 7 * published by the Free Software Foundation, either version 3 of the 8 * License, or (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Affero General Public License for more details. 14 * 15 * You should have received a copy of the GNU Affero General Public License 16 * along with this program. If not, see <https://www.gnu.org/licenses/>. 17 */ 18 19 use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; 20 use halo2_proofs::{circuit::Value, pasta::Fp}; 21 use rand::rngs::OsRng; 22 23 use darkfi::{ 24 zk::{ 25 proof::{ProvingKey, VerifyingKey}, 26 vm::ZkCircuit, 27 vm_heap::{empty_witnesses, Witness}, 28 Proof, 29 }, 30 zkas::ZkBinary, 31 }; 32 33 fn zk_arith(c: &mut Criterion) { 34 let bincode = include_bytes!("../proof/arithmetic.zk.bin"); 35 let zkbin = ZkBinary::decode(bincode).unwrap(); 36 37 let a = Fp::from(4); 38 let b = Fp::from(110); 39 40 let prover_witnesses = vec![Witness::Base(Value::known(a)), Witness::Base(Value::known(b))]; 41 let public_inputs = vec![a + b, a * b, a - b]; 42 43 //darkfi::zk::export_witness_json("proof/witness/arithmetic.json", &prover_witnesses, &public_inputs); 44 let circuit = ZkCircuit::new(prover_witnesses.clone(), &zkbin); 45 46 let mut prove_group = c.benchmark_group("prove"); 47 prove_group.significance_level(0.01).sample_size(10); 48 for k in zkbin.k..20 { 49 let proving_key = ProvingKey::build(k, &circuit.clone()); 50 prove_group.bench_with_input(BenchmarkId::from_parameter(k), &k, |b, &_k| { 51 b.iter(|| Proof::create(&proving_key, &[circuit.clone()], &public_inputs, &mut OsRng)) 52 }); 53 } 54 prove_group.finish(); 55 56 let mut verif_group = c.benchmark_group("verify"); 57 verif_group.significance_level(0.01).sample_size(10); 58 for k in zkbin.k..20 { 59 let proving_key = ProvingKey::build(k, &circuit.clone()); 60 let proof = 61 Proof::create(&proving_key, &[circuit.clone()], &public_inputs, &mut OsRng).unwrap(); 62 let verifier_witnesses = empty_witnesses(&zkbin).unwrap(); 63 let circuit = ZkCircuit::new(verifier_witnesses, &zkbin); 64 let verifying_key = VerifyingKey::build(k, &circuit); 65 66 verif_group.bench_with_input(BenchmarkId::from_parameter(k), &k, |b, &_k| { 67 b.iter(|| proof.verify(&verifying_key, &public_inputs)) 68 }); 69 } 70 verif_group.finish(); 71 } 72 73 criterion_group!(bench, zk_arith); 74 criterion_main!(bench);