zk_arith.rs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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(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. //darkfi::zk::export_witness_json("proof/witness/arithmetic.json", &prover_witnesses, &public_inputs);
  38. let circuit = ZkCircuit::new(prover_witnesses.clone(), &zkbin);
  39. let mut prove_group = c.benchmark_group("prove");
  40. prove_group.significance_level(0.01).sample_size(10);
  41. for k in zkbin.k..20 {
  42. let proving_key = ProvingKey::build(k, &circuit.clone());
  43. prove_group.bench_with_input(BenchmarkId::from_parameter(k), &k, |b, &_k| {
  44. b.iter(|| Proof::create(&proving_key, &[circuit.clone()], &public_inputs, &mut OsRng))
  45. });
  46. }
  47. prove_group.finish();
  48. let mut verif_group = c.benchmark_group("verify");
  49. verif_group.significance_level(0.01).sample_size(10);
  50. for k in zkbin.k..20 {
  51. let proving_key = ProvingKey::build(k, &circuit.clone());
  52. let proof =
  53. Proof::create(&proving_key, &[circuit.clone()], &public_inputs, &mut OsRng).unwrap();
  54. let verifier_witnesses = empty_witnesses(&zkbin).unwrap();
  55. let circuit = ZkCircuit::new(verifier_witnesses, &zkbin);
  56. let verifying_key = VerifyingKey::build(k, &circuit);
  57. verif_group.bench_with_input(BenchmarkId::from_parameter(k), &k, |b, &_k| {
  58. b.iter(|| proof.verify(&verifying_key, &public_inputs))
  59. });
  60. }
  61. verif_group.finish();
  62. }
  63. criterion_group!(bench, zk_arith);
  64. criterion_main!(bench);