burn_proof.rs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. use darkfi::{
  19. crypto::{
  20. proof::{ProvingKey, VerifyingKey},
  21. util::poseidon_hash,
  22. Proof,
  23. },
  24. zk::{
  25. vm::{Witness, ZkCircuit},
  26. vm_stack::empty_witnesses,
  27. },
  28. zkas::decoder::ZkBinary,
  29. Result,
  30. };
  31. use darkfi_sdk::crypto::{
  32. pedersen::{pedersen_commitment_base, pedersen_commitment_u64},
  33. MerkleNode, Nullifier, PublicKey, SecretKey,
  34. };
  35. use halo2_gadgets::poseidon::primitives as poseidon;
  36. use halo2_proofs::circuit::Value;
  37. use incrementalmerkletree::{bridgetree::BridgeTree, Tree};
  38. use pasta_curves::{
  39. arithmetic::CurveAffine,
  40. group::{ff::Field, Curve},
  41. pallas,
  42. };
  43. use rand::rngs::OsRng;
  44. #[test]
  45. fn burn_proof() -> Result<()> {
  46. /* ANCHOR: main */
  47. let bincode = include_bytes!("../proof/burn.zk.bin");
  48. let zkbin = ZkBinary::decode(bincode)?;
  49. // ======
  50. // Prover
  51. // ======
  52. // Witness values
  53. let value = 42;
  54. let token_id = pallas::Base::random(&mut OsRng);
  55. let value_blind = pallas::Scalar::random(&mut OsRng);
  56. let token_blind = pallas::Scalar::random(&mut OsRng);
  57. let serial = pallas::Base::random(&mut OsRng);
  58. let coin_blind = pallas::Base::random(&mut OsRng);
  59. let secret = SecretKey::random(&mut OsRng);
  60. let sig_secret = SecretKey::random(&mut OsRng);
  61. // Build the coin
  62. let coin2 = {
  63. let (pub_x, pub_y) = PublicKey::from_secret(secret).xy();
  64. let messages = [pub_x, pub_y, pallas::Base::from(value), token_id, serial, coin_blind];
  65. poseidon::Hash::<_, poseidon::P128Pow5T3, poseidon::ConstantLength<6>, 3, 2>::init()
  66. .hash(messages)
  67. };
  68. // Fill the merkle tree with some random coins that we want to witness,
  69. // and also add the above coin.
  70. let mut tree = BridgeTree::<MerkleNode, 32>::new(100);
  71. let coin0 = pallas::Base::random(&mut OsRng);
  72. let coin1 = pallas::Base::random(&mut OsRng);
  73. let coin3 = pallas::Base::random(&mut OsRng);
  74. tree.append(&MerkleNode::from(coin0));
  75. tree.witness();
  76. tree.append(&MerkleNode::from(coin1));
  77. tree.append(&MerkleNode::from(coin2));
  78. let leaf_pos = tree.witness().unwrap();
  79. tree.append(&MerkleNode::from(coin3));
  80. tree.witness();
  81. let root = tree.root(0).unwrap();
  82. let merkle_path = tree.authentication_path(leaf_pos, &root).unwrap();
  83. let leaf_pos: u64 = leaf_pos.into();
  84. let prover_witnesses = vec![
  85. Witness::Base(Value::known(secret.inner())),
  86. Witness::Base(Value::known(serial)),
  87. Witness::Base(Value::known(pallas::Base::from(value))),
  88. Witness::Base(Value::known(token_id)),
  89. Witness::Base(Value::known(coin_blind)),
  90. Witness::Scalar(Value::known(value_blind)),
  91. Witness::Scalar(Value::known(token_blind)),
  92. Witness::Uint32(Value::known(leaf_pos.try_into().unwrap())),
  93. Witness::MerklePath(Value::known(merkle_path.try_into().unwrap())),
  94. Witness::Base(Value::known(sig_secret.inner())),
  95. ];
  96. // Create the public inputs
  97. let nullifier = Nullifier::from(poseidon_hash::<2>([secret.inner(), serial]));
  98. let value_commit = pedersen_commitment_u64(value, value_blind);
  99. let value_coords = value_commit.to_affine().coordinates().unwrap();
  100. let token_commit = pedersen_commitment_base(token_id, token_blind);
  101. let token_coords = token_commit.to_affine().coordinates().unwrap();
  102. let sig_pubkey = PublicKey::from_secret(sig_secret);
  103. let (sig_x, sig_y) = sig_pubkey.xy();
  104. let merkle_root = tree.root(0).unwrap();
  105. let public_inputs = vec![
  106. nullifier.inner(),
  107. *value_coords.x(),
  108. *value_coords.y(),
  109. *token_coords.x(),
  110. *token_coords.y(),
  111. merkle_root.inner(),
  112. sig_x,
  113. sig_y,
  114. ];
  115. // Create the circuit
  116. let circuit = ZkCircuit::new(prover_witnesses, zkbin.clone());
  117. let proving_key = ProvingKey::build(13, &circuit);
  118. let proof = Proof::create(&proving_key, &[circuit], &public_inputs, &mut OsRng)?;
  119. // ========
  120. // Verifier
  121. // ========
  122. // Construct empty witnesses
  123. let verifier_witnesses = empty_witnesses(&zkbin);
  124. // Create the circuit
  125. let circuit = ZkCircuit::new(verifier_witnesses, zkbin);
  126. let verifying_key = VerifyingKey::build(13, &circuit);
  127. proof.verify(&verifying_key, &public_inputs)?;
  128. /* ANCHOR_END: main */
  129. Ok(())
  130. }