burn_proof.rs 5.0 KB

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