zk-inclusion-proof.rs 4.5 KB

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