bench_zk.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 halo2_proofs::{circuit::Value, pasta::Fp};
  19. use rand::rngs::OsRng;
  20. use darkfi::{
  21. zk::{
  22. proof::{ProvingKey, VerifyingKey},
  23. vm::ZkCircuit,
  24. vm_heap::{empty_witnesses, Witness},
  25. Proof,
  26. },
  27. zkas::ZkBinary,
  28. Result,
  29. };
  30. const SAMPLES: u128 = 10;
  31. #[test]
  32. fn bench_zk() -> Result<()> {
  33. let bincode = include_bytes!("../proof/arithmetic.zk.bin");
  34. let zkbin = ZkBinary::decode(bincode)?;
  35. let a = Fp::from(4);
  36. let b = Fp::from(110);
  37. // Values for the proof
  38. let prover_witnesses = vec![Witness::Base(Value::known(a)), Witness::Base(Value::known(b))];
  39. let public_inputs = vec![a + b, a * b, a - b];
  40. // I tried cargo bench, but there's no way to display k=X for each individual bench
  41. // TODO: make a benchmark group and use bench_with_input (cargo bench)
  42. // see https://github.com/getsentry/relay/blob/master/relay-cardinality/benches/redis_impl.rs#L137-L165
  43. for k in 11..20 {
  44. println!("Benchmarking k={}", k);
  45. let circuit = ZkCircuit::new(prover_witnesses.clone(), &zkbin);
  46. let proving_key = ProvingKey::build(k, &circuit);
  47. let mut total = 0;
  48. for _ in 0..SAMPLES {
  49. let now = std::time::Instant::now();
  50. let _ = Proof::create(&proving_key, &[circuit.clone()], &public_inputs, &mut OsRng)?;
  51. total += now.elapsed().as_millis();
  52. }
  53. println!("Avg proving time: {} ms", total / SAMPLES);
  54. let proof = Proof::create(&proving_key, &[circuit], &public_inputs, &mut OsRng)?;
  55. let verifier_witnesses = empty_witnesses(&zkbin)?;
  56. let circuit = ZkCircuit::new(verifier_witnesses, &zkbin);
  57. let verifying_key = VerifyingKey::build(k, &circuit);
  58. let mut total = 0;
  59. for _ in 0..SAMPLES {
  60. let now = std::time::Instant::now();
  61. proof.verify(&verifying_key, &public_inputs)?;
  62. total += now.elapsed().as_millis();
  63. }
  64. println!("Avg verification time: {} ms", total / SAMPLES);
  65. }
  66. Ok(())
  67. }