zk_arith.rs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 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, true).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(|| {
  45. Proof::create(
  46. &proving_key,
  47. std::slice::from_ref(&circuit),
  48. &public_inputs,
  49. &mut OsRng,
  50. )
  51. })
  52. });
  53. }
  54. prove_group.finish();
  55. let mut verif_group = c.benchmark_group("verify");
  56. verif_group.significance_level(0.01).sample_size(10);
  57. for k in zkbin.k..20 {
  58. let proving_key = ProvingKey::build(k, &circuit.clone());
  59. let proof =
  60. Proof::create(&proving_key, std::slice::from_ref(&circuit), &public_inputs, &mut OsRng)
  61. .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. verif_group.bench_with_input(BenchmarkId::from_parameter(k), &k, |b, &_k| {
  66. b.iter(|| proof.verify(&verifying_key, &public_inputs))
  67. });
  68. }
  69. verif_group.finish();
  70. }
  71. criterion_group!(bench, zk_arith);
  72. criterion_main!(bench);