zk_from_json.rs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 rand::rngs::OsRng;
  20. use std::{fs::File, io::Read};
  21. use darkfi::{
  22. zk::{
  23. proof::{ProvingKey, VerifyingKey},
  24. vm::ZkCircuit,
  25. vm_heap::empty_witnesses,
  26. Proof,
  27. },
  28. zkas::ZkBinary,
  29. };
  30. // Use a witness.json file to benchmark a ZK file
  31. fn zk_from_json(c: &mut Criterion) {
  32. #[rustfmt::skip]
  33. let tests = [
  34. (
  35. "Opcodes",
  36. "proof/opcodes.zk.bin",
  37. "proof/witness/opcodes.json"
  38. ),
  39. (
  40. "Arithmetic",
  41. "proof/arithmetic.zk.bin",
  42. "proof/witness/arithmetic.json"
  43. ),
  44. (
  45. "SMT",
  46. "proof/smt.zk.bin",
  47. "proof/witness/smt.json"
  48. ),
  49. (
  50. "DAO::mint",
  51. "src/contract/dao/proof/mint.zk.bin",
  52. "src/contract/dao/proof/witness/mint.json"
  53. ),
  54. (
  55. "DAO::propose-input",
  56. "src/contract/dao/proof/propose-input.zk.bin",
  57. "src/contract/dao/proof/witness/propose-input.json"
  58. ),
  59. (
  60. "DAO::propose",
  61. "src/contract/dao/proof/propose-main.zk.bin",
  62. "src/contract/dao/proof/witness/propose-main.json"
  63. ),
  64. (
  65. "DAO::vote-input",
  66. "src/contract/dao/proof/vote-input.zk.bin",
  67. "src/contract/dao/proof/witness/vote-input.json"
  68. ),
  69. (
  70. "DAO::vote",
  71. "src/contract/dao/proof/vote-main.zk.bin",
  72. "src/contract/dao/proof/witness/vote-main.json"
  73. ),
  74. (
  75. "DAO::exec",
  76. "src/contract/dao/proof/exec.zk.bin",
  77. "src/contract/dao/proof/witness/exec.json"
  78. ),
  79. (
  80. "DAO::auth_xfer-coin",
  81. "src/contract/dao/proof/auth-money-transfer-enc-coin.zk.bin",
  82. "src/contract/dao/proof/witness/auth-money-transfer-enc-coin.json"
  83. ),
  84. (
  85. "DAO::auth_xfer",
  86. "src/contract/dao/proof/auth-money-transfer.zk.bin",
  87. "src/contract/dao/proof/witness/auth-money-transfer.json"
  88. ),
  89. (
  90. "Money::xfer-mint",
  91. "src/contract/money/proof/mint_v1.zk.bin",
  92. "src/contract/money/proof/witness/mint_v1.json"
  93. ),
  94. (
  95. "Money::xfer-burn",
  96. "src/contract/money/proof/burn_v1.zk.bin",
  97. "src/contract/money/proof/witness/burn_v1.json"
  98. ),
  99. (
  100. "Money::fee",
  101. "src/contract/money/proof/fee_v1.zk.bin",
  102. "src/contract/money/proof/witness/fee_v1.json"
  103. ),
  104. (
  105. "Money::auth_token-mint",
  106. "src/contract/money/proof/auth_token_mint_v1.zk.bin",
  107. "src/contract/money/proof/witness/auth_token_mint_v1.json"
  108. ),
  109. (
  110. "Money::token-mint",
  111. "src/contract/money/proof/token_mint_v1.zk.bin",
  112. "src/contract/money/proof/witness/token_mint_v1.json"
  113. ),
  114. (
  115. "Money::token-freeze",
  116. "src/contract/money/proof/token_freeze_v1.zk.bin",
  117. "src/contract/money/proof/witness/token_freeze_v1.json"
  118. ),
  119. ];
  120. println!("Running ZK Json benchmarks");
  121. for (name, proof, witness) in &tests {
  122. println!("Benchmarking '{}': {} {}", name, proof, witness);
  123. run_benchmark(c, name, proof, witness);
  124. println!("Done!");
  125. }
  126. }
  127. fn run_benchmark(c: &mut Criterion, name: &str, proof: &str, witness: &str) {
  128. let mut bincode = Vec::new();
  129. let mut f = File::open(proof).unwrap();
  130. f.read_to_end(&mut bincode).unwrap();
  131. let zkbin = ZkBinary::decode(&bincode).unwrap();
  132. let (prover_witnesses, public_inputs) = darkfi::zk::import_witness_json(witness);
  133. let circuit = ZkCircuit::new(prover_witnesses.clone(), &zkbin);
  134. let mut prove_group = c.benchmark_group(format!("prove {}", name));
  135. prove_group.significance_level(0.01).sample_size(10);
  136. for k in zkbin.k..20 {
  137. let proving_key = ProvingKey::build(k, &circuit.clone());
  138. prove_group.bench_with_input(BenchmarkId::from_parameter(k), &k, |b, &_k| {
  139. b.iter(|| Proof::create(&proving_key, &[circuit.clone()], &public_inputs, &mut OsRng))
  140. });
  141. }
  142. prove_group.finish();
  143. let mut verif_group = c.benchmark_group(format!("verify {}", name));
  144. verif_group.significance_level(0.01).sample_size(10);
  145. for k in zkbin.k..20 {
  146. let proving_key = ProvingKey::build(k, &circuit.clone());
  147. let proof =
  148. Proof::create(&proving_key, &[circuit.clone()], &public_inputs, &mut OsRng).unwrap();
  149. let verifier_witnesses = empty_witnesses(&zkbin).unwrap();
  150. let circuit = ZkCircuit::new(verifier_witnesses, &zkbin);
  151. let verifying_key = VerifyingKey::build(k, &circuit);
  152. verif_group.bench_with_input(BenchmarkId::from_parameter(k), &k, |b, &_k| {
  153. b.iter(|| proof.verify(&verifying_key, &public_inputs))
  154. });
  155. }
  156. verif_group.finish();
  157. }
  158. criterion_group!(bench, zk_from_json);
  159. criterion_main!(bench);