zk-inclusion-proof.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2022 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. // ../zkas simple.zk
  19. use darkfi::{
  20. zk::{
  21. proof::{Proof, ProvingKey, VerifyingKey},
  22. vm::{Witness, ZkCircuit},
  23. vm_heap::empty_witnesses,
  24. },
  25. zkas::decoder::ZkBinary,
  26. Result,
  27. };
  28. use darkfi_sdk::{
  29. bridgetree::Hashable,
  30. crypto::{poseidon_hash, MerkleNode, MerkleTree},
  31. pasta::{group::ff::Field, pallas},
  32. };
  33. use darkfi_serial::Encodable;
  34. use halo2_proofs::circuit::Value;
  35. use rand::rngs::OsRng;
  36. fn main() -> Result<()> {
  37. let mut tree = MerkleTree::new(100);
  38. // Add 10 random things to the tree
  39. for _ in 0..10 {
  40. let random_leaf = pallas::Base::random(&mut OsRng);
  41. let node = MerkleNode::from(random_leaf);
  42. tree.append(node);
  43. }
  44. let leaf = pallas::Base::random(&mut OsRng);
  45. let node = MerkleNode::from(leaf);
  46. tree.append(node);
  47. let leaf_position = tree.mark().unwrap();
  48. // Add 10 more random things to the tree
  49. for _ in 0..10 {
  50. let random_leaf = pallas::Base::random(&mut OsRng);
  51. let node = MerkleNode::from(random_leaf);
  52. tree.append(node);
  53. }
  54. // Now begin zk proof API
  55. let bincode = include_bytes!("../proof/inclusion_proof.zk.bin");
  56. let zkbin = ZkBinary::decode(bincode)?;
  57. // ======
  58. // Prover
  59. // ======
  60. // Bigger k = more rows, but slower circuit
  61. // Number of rows is 2^k
  62. let k = 11;
  63. println!("k = {}", k);
  64. // Witness values
  65. let merkle_path = tree.witness(leaf_position, 0).unwrap();
  66. let leaf_position: u64 = leaf_position.into();
  67. let blind = pallas::Base::random(&mut OsRng);
  68. let prover_witnesses = vec![
  69. Witness::Base(Value::known(leaf)),
  70. Witness::Uint32(Value::known(leaf_position.try_into().unwrap())),
  71. Witness::MerklePath(Value::known(merkle_path.clone().try_into().unwrap())),
  72. Witness::Base(Value::known(blind)),
  73. ];
  74. // Create the public inputs
  75. let merkle_root = {
  76. let position: u64 = leaf_position.into();
  77. let mut current = MerkleNode::from(leaf);
  78. for (level, sibling) in merkle_path.iter().enumerate() {
  79. let level = level as u8;
  80. current = if position & (1 << level) == 0 {
  81. MerkleNode::combine(level.into(), &current, sibling)
  82. } else {
  83. MerkleNode::combine(level.into(), sibling, &current)
  84. };
  85. }
  86. current
  87. };
  88. let enc_leaf = poseidon_hash([leaf, blind]);
  89. let public_inputs = vec![merkle_root.inner(), enc_leaf];
  90. // Create the circuit
  91. let circuit = ZkCircuit::new(prover_witnesses, zkbin.clone());
  92. let now = std::time::Instant::now();
  93. let proving_key = ProvingKey::build(k, &circuit);
  94. println!("ProvingKey built [{} s]", now.elapsed().as_secs_f64());
  95. let now = std::time::Instant::now();
  96. let proof = Proof::create(&proving_key, &[circuit], &public_inputs, &mut OsRng)?;
  97. println!("Proof created [{} s]", now.elapsed().as_secs_f64());
  98. // ========
  99. // Verifier
  100. // ========
  101. // Construct empty witnesses
  102. let verifier_witnesses = empty_witnesses(&zkbin);
  103. // Create the circuit
  104. let circuit = ZkCircuit::new(verifier_witnesses, zkbin);
  105. let now = std::time::Instant::now();
  106. let verifying_key = VerifyingKey::build(k, &circuit);
  107. println!("VerifyingKey built [{} s]", now.elapsed().as_secs_f64());
  108. let now = std::time::Instant::now();
  109. proof.verify(&verifying_key, &public_inputs)?;
  110. println!("proof verify [{} s]", now.elapsed().as_secs_f64());
  111. let mut data = vec![];
  112. proof.encode(&mut data)?;
  113. println!("proof size: {}", data.len());
  114. Ok(())
  115. }