zk_arith.rs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 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. use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
  19. use halo2_proofs::{circuit::Value, pasta::Fp};
  20. use rand::rngs::OsRng;
  21. use darkfi::{
  22. zk::{
  23. proof::{ProvingKey, VerifyingKey},
  24. vm::ZkCircuit,
  25. vm_heap::{empty_witnesses, Witness},
  26. Proof,
  27. },
  28. zkas::ZkBinary,
  29. };
  30. fn zk_arith_bench(c: &mut Criterion) {
  31. let bincode = include_bytes!("../proof/arithmetic.zk.bin");
  32. let zkbin = ZkBinary::decode(bincode).unwrap();
  33. let a = Fp::from(4);
  34. let b = Fp::from(110);
  35. let prover_witnesses = vec![Witness::Base(Value::known(a)), Witness::Base(Value::known(b))];
  36. let public_inputs = vec![a + b, a * b, a - b];
  37. let circuit = ZkCircuit::new(prover_witnesses.clone(), &zkbin);
  38. let mut prove_group = c.benchmark_group("prove");
  39. for k in 11..20 {
  40. let proving_key = ProvingKey::build(k, &circuit.clone());
  41. prove_group.bench_with_input(BenchmarkId::from_parameter(k), &k, |b, &_k| {
  42. b.iter(|| Proof::create(&proving_key, &[circuit.clone()], &public_inputs, &mut OsRng))
  43. });
  44. }
  45. prove_group.finish();
  46. let mut verif_group = c.benchmark_group("verify");
  47. for k in 11..20 {
  48. let proving_key = ProvingKey::build(k, &circuit.clone());
  49. let proof =
  50. Proof::create(&proving_key, &[circuit.clone()], &public_inputs, &mut OsRng).unwrap();
  51. let verifier_witnesses = empty_witnesses(&zkbin).unwrap();
  52. let circuit = ZkCircuit::new(verifier_witnesses, &zkbin);
  53. let verifying_key = VerifyingKey::build(k, &circuit);
  54. verif_group.bench_with_input(BenchmarkId::from_parameter(k), &k, |b, &_k| {
  55. b.iter(|| proof.verify(&verifying_key, &public_inputs))
  56. });
  57. }
  58. verif_group.finish();
  59. }
  60. criterion_group!(benches, zk_arith_bench);
  61. criterion_main!(benches);