mint_proof.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. use std::time::Instant;
  2. use halo2_gadgets::poseidon::primitives as poseidon;
  3. use halo2_proofs::circuit::Value;
  4. use log::debug;
  5. use pasta_curves::{arithmetic::CurveAffine, group::Curve, pallas};
  6. use rand::rngs::OsRng;
  7. use crate::{
  8. crypto::{
  9. coin::Coin,
  10. keypair::PublicKey,
  11. proof::{Proof, ProvingKey, VerifyingKey},
  12. types::{DrkCoinBlind, DrkSerial, DrkTokenId, DrkValue, DrkValueBlind, DrkValueCommit},
  13. util::{mod_r_p, pedersen_commitment_scalar, pedersen_commitment_u64},
  14. },
  15. util::serial::{SerialDecodable, SerialEncodable},
  16. zk::circuit::mint_contract::MintContract,
  17. Result,
  18. };
  19. #[derive(Debug, Clone, PartialEq, Eq, SerialEncodable, SerialDecodable)]
  20. pub struct MintRevealedValues {
  21. pub value_commit: DrkValueCommit,
  22. pub token_commit: DrkValueCommit,
  23. pub coin: Coin,
  24. }
  25. impl MintRevealedValues {
  26. pub fn compute(
  27. value: u64,
  28. token_id: DrkTokenId,
  29. value_blind: DrkValueBlind,
  30. token_blind: DrkValueBlind,
  31. serial: DrkSerial,
  32. coin_blind: DrkCoinBlind,
  33. public_key: PublicKey,
  34. ) -> Self {
  35. let value_commit = pedersen_commitment_u64(value, value_blind);
  36. let token_commit = pedersen_commitment_scalar(mod_r_p(token_id), token_blind);
  37. let coords = public_key.0.to_affine().coordinates().unwrap();
  38. let messages =
  39. [*coords.x(), *coords.y(), DrkValue::from(value), token_id, serial, coin_blind];
  40. let coin =
  41. poseidon::Hash::<_, poseidon::P128Pow5T3, poseidon::ConstantLength<6>, 3, 2>::init()
  42. .hash(messages);
  43. MintRevealedValues { value_commit, token_commit, coin: Coin(coin) }
  44. }
  45. pub fn make_outputs(&self) -> [pallas::Base; 5] {
  46. let value_coords = self.value_commit.to_affine().coordinates().unwrap();
  47. let token_coords = self.token_commit.to_affine().coordinates().unwrap();
  48. vec![
  49. self.coin.0,
  50. *value_coords.x(),
  51. *value_coords.y(),
  52. *token_coords.x(),
  53. *token_coords.y(),
  54. ]
  55. .try_into()
  56. .unwrap()
  57. }
  58. }
  59. #[allow(clippy::too_many_arguments)]
  60. pub fn create_mint_proof(
  61. pk: &ProvingKey,
  62. value: u64,
  63. token_id: DrkTokenId,
  64. value_blind: DrkValueBlind,
  65. token_blind: DrkValueBlind,
  66. serial: DrkSerial,
  67. coin_blind: DrkCoinBlind,
  68. public_key: PublicKey,
  69. ) -> Result<(Proof, MintRevealedValues)> {
  70. let revealed = MintRevealedValues::compute(
  71. value,
  72. token_id,
  73. value_blind,
  74. token_blind,
  75. serial,
  76. coin_blind,
  77. public_key,
  78. );
  79. let coords = public_key.0.to_affine().coordinates().unwrap();
  80. let c = MintContract {
  81. pub_x: Value::known(*coords.x()),
  82. pub_y: Value::known(*coords.y()),
  83. value: Value::known(DrkValue::from(value)),
  84. token: Value::known(token_id),
  85. serial: Value::known(serial),
  86. coin_blind: Value::known(coin_blind),
  87. value_blind: Value::known(value_blind),
  88. token_blind: Value::known(token_blind),
  89. };
  90. let start = Instant::now();
  91. let public_inputs = revealed.make_outputs();
  92. let proof = Proof::create(pk, &[c], &public_inputs, &mut OsRng)?;
  93. debug!("Prove mint: [{:?}]", start.elapsed());
  94. Ok((proof, revealed))
  95. }
  96. pub fn verify_mint_proof(
  97. vk: &VerifyingKey,
  98. proof: &Proof,
  99. revealed: &MintRevealedValues,
  100. ) -> Result<()> {
  101. let start = Instant::now();
  102. let public_inputs = revealed.make_outputs();
  103. proof.verify(vk, &public_inputs)?;
  104. debug!("Verify mint: [{:?}]", start.elapsed());
  105. Ok(())
  106. }