mint_proof.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. use std::{io, time::Instant};
  2. use halo2_gadgets::primitives::{
  3. poseidon,
  4. poseidon::{ConstantLength, P128Pow5T3},
  5. };
  6. use log::debug;
  7. use pasta_curves::{arithmetic::CurveAffine, group::Curve, pallas};
  8. use rand::rngs::OsRng;
  9. use crate::{
  10. crypto::{
  11. coin::Coin,
  12. keypair::PublicKey,
  13. proof::{Proof, ProvingKey, VerifyingKey},
  14. types::{DrkCoinBlind, DrkSerial, DrkTokenId, DrkValue, DrkValueBlind, DrkValueCommit},
  15. util::{mod_r_p, pedersen_commitment_scalar, pedersen_commitment_u64},
  16. },
  17. util::serial::{Decodable, Encodable},
  18. zk::circuit::mint_contract::MintContract,
  19. Result,
  20. };
  21. #[derive(Debug, Clone, PartialEq)]
  22. pub struct MintRevealedValues {
  23. pub value_commit: DrkValueCommit,
  24. pub token_commit: DrkValueCommit,
  25. pub coin: Coin,
  26. }
  27. impl MintRevealedValues {
  28. pub fn compute(
  29. value: u64,
  30. token_id: DrkTokenId,
  31. value_blind: DrkValueBlind,
  32. token_blind: DrkValueBlind,
  33. serial: DrkSerial,
  34. coin_blind: DrkCoinBlind,
  35. public_key: PublicKey,
  36. ) -> Self {
  37. let value_commit = pedersen_commitment_u64(value, value_blind);
  38. let token_commit = pedersen_commitment_scalar(mod_r_p(token_id), token_blind);
  39. let coords = public_key.0.to_affine().coordinates().unwrap();
  40. let messages =
  41. [*coords.x(), *coords.y(), DrkValue::from(value), token_id, serial, coin_blind];
  42. let coin = poseidon::Hash::<_, P128Pow5T3, ConstantLength<6>, 3, 2>::init().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. impl Encodable for MintRevealedValues {
  60. fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
  61. let mut len = 0;
  62. len += self.value_commit.encode(&mut s)?;
  63. len += self.token_commit.encode(&mut s)?;
  64. len += self.coin.encode(&mut s)?;
  65. Ok(len)
  66. }
  67. }
  68. impl Decodable for MintRevealedValues {
  69. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  70. Ok(Self {
  71. value_commit: Decodable::decode(&mut d)?,
  72. token_commit: Decodable::decode(&mut d)?,
  73. coin: Decodable::decode(d)?,
  74. })
  75. }
  76. }
  77. #[allow(clippy::too_many_arguments)]
  78. pub fn create_mint_proof(
  79. pk: &ProvingKey,
  80. value: u64,
  81. token_id: DrkTokenId,
  82. value_blind: DrkValueBlind,
  83. token_blind: DrkValueBlind,
  84. serial: DrkSerial,
  85. coin_blind: DrkCoinBlind,
  86. public_key: PublicKey,
  87. ) -> Result<(Proof, MintRevealedValues)> {
  88. let revealed = MintRevealedValues::compute(
  89. value,
  90. token_id,
  91. value_blind,
  92. token_blind,
  93. serial,
  94. coin_blind,
  95. public_key,
  96. );
  97. let coords = public_key.0.to_affine().coordinates().unwrap();
  98. let c = MintContract {
  99. pub_x: Some(*coords.x()),
  100. pub_y: Some(*coords.y()),
  101. value: Some(DrkValue::from(value)),
  102. token: Some(token_id),
  103. serial: Some(serial),
  104. coin_blind: Some(coin_blind),
  105. value_blind: Some(value_blind),
  106. token_blind: Some(token_blind),
  107. };
  108. let start = Instant::now();
  109. let public_inputs = revealed.make_outputs();
  110. let proof = Proof::create(pk, &[c], &public_inputs, &mut OsRng)?;
  111. debug!("Prove: [{:?}]", start.elapsed());
  112. Ok((proof, revealed))
  113. }
  114. pub fn verify_mint_proof(
  115. vk: &VerifyingKey,
  116. proof: &Proof,
  117. revealed: &MintRevealedValues,
  118. ) -> Result<()> {
  119. let public_inputs = revealed.make_outputs();
  120. Ok(proof.verify(vk, &public_inputs)?)
  121. }