burn.rs 4.9 KB

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