mint_proof.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. use std::io;
  2. use std::time::Instant;
  3. use halo2_gadgets::primitives;
  4. use halo2_gadgets::primitives::poseidon::{ConstantLength, P128Pow5T3};
  5. use log::debug;
  6. use pasta_curves::{
  7. arithmetic::{CurveAffine, FieldExt},
  8. group::Curve,
  9. };
  10. use super::{
  11. proof::{Proof, ProvingKey, VerifyingKey},
  12. util::{mod_r_p, pedersen_commitment_scalar, pedersen_commitment_u64},
  13. };
  14. use crate::{
  15. circuit::mint_contract::MintContract,
  16. serial::{Decodable, Encodable},
  17. types::*,
  18. Result,
  19. };
  20. pub struct MintRevealedValues {
  21. pub value_commit: DrkValueCommit,
  22. pub token_commit: DrkValueCommit,
  23. pub coin: [u8; 32],
  24. }
  25. impl MintRevealedValues {
  26. 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: DrkPublicKey,
  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.to_affine().coordinates().unwrap();
  38. let messages = [
  39. [*coords.x(), *coords.y()],
  40. [DrkValue::from_u64(value), token_id],
  41. [serial, coin_blind],
  42. ];
  43. let mut hash = DrkCoin::zero();
  44. for msg in messages.iter() {
  45. hash += primitives::poseidon::Hash::init(P128Pow5T3, ConstantLength::<2>).hash(*msg);
  46. }
  47. let coin = hash.to_bytes();
  48. MintRevealedValues {
  49. value_commit,
  50. token_commit,
  51. coin,
  52. }
  53. }
  54. fn make_outputs(&self) -> [DrkCircuitField; 5] {
  55. let value_coords = self.value_commit.to_affine().coordinates().unwrap();
  56. let token_coords = self.value_commit.to_affine().coordinates().unwrap();
  57. vec![
  58. DrkCircuitField::from_bytes(&self.coin).unwrap(),
  59. *value_coords.x(),
  60. *value_coords.y(),
  61. *token_coords.x(),
  62. *token_coords.y(),
  63. ]
  64. .try_into()
  65. .unwrap()
  66. }
  67. }
  68. impl Encodable for MintRevealedValues {
  69. fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
  70. let mut len = 0;
  71. len += self.value_commit.encode(&mut s)?;
  72. len += self.token_commit.encode(&mut s)?;
  73. len += self.coin.encode(&mut s)?;
  74. Ok(len)
  75. }
  76. }
  77. impl Decodable for MintRevealedValues {
  78. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  79. Ok(Self {
  80. value_commit: Decodable::decode(&mut d)?,
  81. token_commit: Decodable::decode(&mut d)?,
  82. coin: Decodable::decode(d)?,
  83. })
  84. }
  85. }
  86. #[allow(clippy::too_many_arguments)]
  87. pub fn create_mint_proof(
  88. value: u64,
  89. token_id: DrkTokenId,
  90. value_blind: DrkValueBlind,
  91. token_blind: DrkValueBlind,
  92. serial: DrkSerial,
  93. coin_blind: DrkCoinBlind,
  94. public_key: DrkPublicKey,
  95. ) -> Result<(Proof, MintRevealedValues)> {
  96. const K: u32 = 11;
  97. let revealed = MintRevealedValues::compute(
  98. value,
  99. token_id,
  100. value_blind,
  101. token_blind,
  102. serial,
  103. coin_blind,
  104. public_key,
  105. );
  106. let coords = public_key.to_affine().coordinates().unwrap();
  107. let c = MintContract {
  108. pub_x: Some(*coords.x()),
  109. pub_y: Some(*coords.y()),
  110. value: Some(DrkValue::from_u64(value)),
  111. asset: Some(token_id),
  112. serial: Some(serial),
  113. coin_blind: Some(coin_blind),
  114. value_blind: Some(value_blind),
  115. asset_blind: Some(token_blind),
  116. };
  117. let start = Instant::now();
  118. // TODO: Don't always build this
  119. let pk = ProvingKey::build(K, MintContract::default());
  120. debug!("Setup: [{:?}]", start.elapsed());
  121. let start = Instant::now();
  122. let public_inputs = revealed.make_outputs();
  123. let proof = Proof::create(&pk, &[c], &public_inputs)?;
  124. debug!("Prove: [{:?}]", start.elapsed());
  125. Ok((proof, revealed))
  126. }
  127. pub fn verify_mint_proof(
  128. vk: &VerifyingKey,
  129. proof: Proof,
  130. revealed: &MintRevealedValues,
  131. ) -> Result<()> {
  132. let public_inputs = revealed.make_outputs();
  133. Ok(proof.verify(vk, &public_inputs)?)
  134. }