bench_zk.rs 2.8 KB

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