burn_proof.rs 4.2 KB

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