mint_proof.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. use std::{io, time::Instant};
  2. use halo2_gadgets::{
  3. primitives,
  4. primitives::poseidon::{ConstantLength, P128Pow5T3},
  5. };
  6. use log::debug;
  7. use pasta_curves::{
  8. arithmetic::{CurveAffine, FieldExt},
  9. group::Curve,
  10. pallas,
  11. };
  12. use super::{
  13. proof::{Proof, ProvingKey, VerifyingKey},
  14. util::{mod_r_p, pedersen_commitment_scalar, pedersen_commitment_u64},
  15. };
  16. use crate::{
  17. circuit::mint_contract::MintContract,
  18. serial::{Decodable, Encodable},
  19. types::*,
  20. Result,
  21. };
  22. pub struct MintRevealedValues {
  23. pub value_commit: DrkValueCommit,
  24. pub token_commit: DrkValueCommit,
  25. //pub coin: [u8; 32],
  26. pub coin: pallas::Base,
  27. }
  28. impl MintRevealedValues {
  29. fn compute(
  30. value: u64,
  31. token_id: DrkTokenId,
  32. value_blind: DrkValueBlind,
  33. token_blind: DrkValueBlind,
  34. serial: DrkSerial,
  35. coin_blind: DrkCoinBlind,
  36. public_key: DrkPublicKey,
  37. ) -> Self {
  38. let value_commit = pedersen_commitment_u64(value, value_blind);
  39. let token_commit = pedersen_commitment_scalar(mod_r_p(token_id), token_blind);
  40. let coords = public_key.to_affine().coordinates().unwrap();
  41. let messages = [
  42. [*coords.x(), *coords.y()],
  43. [DrkValue::from_u64(value), token_id],
  44. [serial, coin_blind],
  45. ];
  46. let mut coin = DrkCoin::zero();
  47. for msg in messages.iter() {
  48. coin += primitives::poseidon::Hash::init(P128Pow5T3, ConstantLength::<2>).hash(*msg);
  49. }
  50. //let coin = hash.to_bytes();
  51. MintRevealedValues { value_commit, token_commit, coin }
  52. }
  53. fn make_outputs(&self) -> [DrkCircuitField; 5] {
  54. let value_coords = self.value_commit.to_affine().coordinates().unwrap();
  55. let token_coords = self.token_commit.to_affine().coordinates().unwrap();
  56. vec![
  57. //DrkCircuitField::from_bytes(&self.coin).unwrap(),
  58. self.coin.clone(),
  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. }