mint.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. use darkfi::{
  2. crypto::{
  3. keypair::PublicKey,
  4. proof::{ProvingKey, VerifyingKey},
  5. util::{mod_r_p, pedersen_commitment_scalar, pedersen_commitment_u64},
  6. Proof,
  7. },
  8. zk::vm::{Witness, ZkCircuit},
  9. zkas::decoder::ZkBinary,
  10. Result,
  11. };
  12. use halo2_gadgets::primitives::{
  13. poseidon,
  14. poseidon::{ConstantLength, P128Pow5T3},
  15. };
  16. use pasta_curves::{
  17. arithmetic::{CurveAffine, Field},
  18. group::Curve,
  19. pallas,
  20. };
  21. use rand::rngs::OsRng;
  22. use simplelog::{ColorChoice::Auto, Config, LevelFilter::Debug, TermLogger, TerminalMode::Mixed};
  23. fn main() -> Result<()> {
  24. TermLogger::init(Debug, Config::default(), Mixed, Auto)?;
  25. /* ANCHOR: main */
  26. let bincode = include_bytes!("mint.zk.bin");
  27. let zkbin = ZkBinary::decode(bincode)?;
  28. // ======
  29. // Prover
  30. // ======
  31. // Witness values
  32. let value = 42;
  33. let token_id = pallas::Base::from(22);
  34. let value_blind = pallas::Scalar::random(&mut OsRng);
  35. let token_blind = pallas::Scalar::random(&mut OsRng);
  36. let serial = pallas::Base::random(&mut OsRng);
  37. let coin_blind = pallas::Base::random(&mut OsRng);
  38. let public_key = PublicKey::random(&mut OsRng);
  39. let coords = public_key.0.to_affine().coordinates().unwrap();
  40. let prover_witnesses = vec![
  41. Witness::Base(Some(*coords.x())),
  42. Witness::Base(Some(*coords.y())),
  43. Witness::Base(Some(pallas::Base::from(value))),
  44. Witness::Base(Some(token_id)),
  45. Witness::Base(Some(serial)),
  46. Witness::Base(Some(coin_blind)),
  47. Witness::Scalar(Some(value_blind)),
  48. Witness::Scalar(Some(token_blind)),
  49. ];
  50. // Create the public inputs
  51. let msgs = [*coords.x(), *coords.y(), pallas::Base::from(value), token_id, serial, coin_blind];
  52. let coin = poseidon::Hash::init(P128Pow5T3, ConstantLength::<6>).hash(msgs);
  53. let value_commit = pedersen_commitment_u64(value, value_blind);
  54. let value_coords = value_commit.to_affine().coordinates().unwrap();
  55. let token_commit = pedersen_commitment_scalar(mod_r_p(token_id), token_blind);
  56. let token_coords = token_commit.to_affine().coordinates().unwrap();
  57. let public_inputs =
  58. vec![coin, *value_coords.x(), *value_coords.y(), *token_coords.x(), *token_coords.y()];
  59. // Create the circuit
  60. let circuit = ZkCircuit::new(prover_witnesses, zkbin.clone());
  61. // Build the proving key and create the zero-knowledge proof
  62. let proving_key = ProvingKey::build(11, &circuit);
  63. let proof = Proof::create(&proving_key, &[circuit], &public_inputs)?;
  64. // ========
  65. // Verifier
  66. // ========
  67. // Construct empty witnesses
  68. let verifier_witnesses = vec![
  69. Witness::Base(None),
  70. Witness::Base(None),
  71. Witness::Base(None),
  72. Witness::Base(None),
  73. Witness::Base(None),
  74. Witness::Base(None),
  75. Witness::Scalar(None),
  76. Witness::Scalar(None),
  77. ];
  78. // Create the circuit
  79. let circuit = ZkCircuit::new(verifier_witnesses, zkbin);
  80. // Build the verifying key and verify the zero-knowledge proof
  81. let verifying_key = VerifyingKey::build(11, &circuit);
  82. proof.verify(&verifying_key, &public_inputs)?;
  83. /* ANCHOR_END: main */
  84. Ok(())
  85. }