mint.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. let bincode = include_bytes!("mint.zk.bin");
  26. let zkbin = ZkBinary::decode(bincode)?;
  27. // ======
  28. // Prover
  29. // ======
  30. // Witness values
  31. let value = 42;
  32. let token_id = pallas::Base::from(22);
  33. let value_blind = pallas::Scalar::random(&mut OsRng);
  34. let token_blind = pallas::Scalar::random(&mut OsRng);
  35. let serial = pallas::Base::random(&mut OsRng);
  36. let coin_blind = pallas::Base::random(&mut OsRng);
  37. let public_key = PublicKey::random(&mut OsRng);
  38. let coords = public_key.0.to_affine().coordinates().unwrap();
  39. let prover_witnesses = vec![
  40. Witness::Base(Some(*coords.x())),
  41. Witness::Base(Some(*coords.y())),
  42. Witness::Base(Some(pallas::Base::from(value))),
  43. Witness::Base(Some(token_id)),
  44. Witness::Base(Some(serial)),
  45. Witness::Base(Some(coin_blind)),
  46. Witness::Scalar(Some(value_blind)),
  47. Witness::Scalar(Some(token_blind)),
  48. ];
  49. // Create the public inputs
  50. let msgs = [*coords.x(), *coords.y(), pallas::Base::from(value), token_id, serial, coin_blind];
  51. let coin = poseidon::Hash::init(P128Pow5T3, ConstantLength::<6>).hash(msgs);
  52. let value_commit = pedersen_commitment_u64(value, value_blind);
  53. let value_coords = value_commit.to_affine().coordinates().unwrap();
  54. let token_commit = pedersen_commitment_scalar(mod_r_p(token_id), token_blind);
  55. let token_coords = token_commit.to_affine().coordinates().unwrap();
  56. let public_inputs =
  57. vec![coin, *value_coords.x(), *value_coords.y(), *token_coords.x(), *token_coords.y()];
  58. // Create the circuit
  59. let circuit = ZkCircuit::new(prover_witnesses, zkbin.clone());
  60. // Build the proving key and create the zero-knowledge proof
  61. let proving_key = ProvingKey::build(11, &circuit);
  62. let proof = Proof::create(&proving_key, &[circuit], &public_inputs)?;
  63. // ========
  64. // Verifier
  65. // ========
  66. // Construct empty witnesses
  67. let verifier_witnesses = vec![
  68. Witness::Base(None),
  69. Witness::Base(None),
  70. Witness::Base(None),
  71. Witness::Base(None),
  72. Witness::Base(None),
  73. Witness::Base(None),
  74. Witness::Scalar(None),
  75. Witness::Scalar(None),
  76. ];
  77. // Create the circuit
  78. let circuit = ZkCircuit::new(verifier_witnesses, zkbin);
  79. // Build the verifying key and verify the zero-knowledge proof
  80. let verifying_key = VerifyingKey::build(11, &circuit);
  81. proof.verify(&verifying_key, &public_inputs)?;
  82. Ok(())
  83. }