burn.rs 4.7 KB

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