mint_proof.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 MintProofKeys {
  23. pub vk: VerifyingKey,
  24. pub pk: ProvingKey,
  25. }
  26. impl MintProofKeys {
  27. pub fn initialize() -> Self {
  28. let start = Instant::now();
  29. debug!("Building proof verifying key for the mint contract...");
  30. let vk = VerifyingKey::build(11, MintContract::default());
  31. debug!("Building proof proving key for the mint contract...");
  32. let pk = ProvingKey::build(11, MintContract::default());
  33. debug!("Setup: [{:?}]", start.elapsed());
  34. MintProofKeys { vk, pk }
  35. }
  36. }
  37. pub struct MintRevealedValues {
  38. pub value_commit: DrkValueCommit,
  39. pub token_commit: DrkValueCommit,
  40. //pub coin: [u8; 32],
  41. pub coin: pallas::Base,
  42. }
  43. impl MintRevealedValues {
  44. fn compute(
  45. value: u64,
  46. token_id: DrkTokenId,
  47. value_blind: DrkValueBlind,
  48. token_blind: DrkValueBlind,
  49. serial: DrkSerial,
  50. coin_blind: DrkCoinBlind,
  51. public_key: DrkPublicKey,
  52. ) -> Self {
  53. let value_commit = pedersen_commitment_u64(value, value_blind);
  54. let token_commit = pedersen_commitment_scalar(mod_r_p(token_id), token_blind);
  55. let coords = public_key.to_affine().coordinates().unwrap();
  56. let messages = [
  57. [*coords.x(), *coords.y()],
  58. [DrkValue::from_u64(value), token_id],
  59. [serial, coin_blind],
  60. ];
  61. let mut coin = DrkCoin::zero();
  62. for msg in messages.iter() {
  63. coin += primitives::poseidon::Hash::init(P128Pow5T3, ConstantLength::<2>).hash(*msg);
  64. }
  65. //let coin = hash.to_bytes();
  66. MintRevealedValues { value_commit, token_commit, coin }
  67. }
  68. fn make_outputs(&self) -> [DrkCircuitField; 5] {
  69. let value_coords = self.value_commit.to_affine().coordinates().unwrap();
  70. let token_coords = self.token_commit.to_affine().coordinates().unwrap();
  71. vec![
  72. //DrkCircuitField::from_bytes(&self.coin).unwrap(),
  73. self.coin,
  74. *value_coords.x(),
  75. *value_coords.y(),
  76. *token_coords.x(),
  77. *token_coords.y(),
  78. ]
  79. .try_into()
  80. .unwrap()
  81. }
  82. }
  83. impl Encodable for MintRevealedValues {
  84. fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
  85. let mut len = 0;
  86. len += self.value_commit.encode(&mut s)?;
  87. len += self.token_commit.encode(&mut s)?;
  88. len += self.coin.encode(&mut s)?;
  89. Ok(len)
  90. }
  91. }
  92. impl Decodable for MintRevealedValues {
  93. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  94. Ok(Self {
  95. value_commit: Decodable::decode(&mut d)?,
  96. token_commit: Decodable::decode(&mut d)?,
  97. coin: Decodable::decode(d)?,
  98. })
  99. }
  100. }
  101. #[allow(clippy::too_many_arguments)]
  102. pub fn create_mint_proof(
  103. value: u64,
  104. token_id: DrkTokenId,
  105. value_blind: DrkValueBlind,
  106. token_blind: DrkValueBlind,
  107. serial: DrkSerial,
  108. coin_blind: DrkCoinBlind,
  109. public_key: DrkPublicKey,
  110. ) -> Result<(Proof, MintRevealedValues)> {
  111. const K: u32 = 11;
  112. let revealed = MintRevealedValues::compute(
  113. value,
  114. token_id,
  115. value_blind,
  116. token_blind,
  117. serial,
  118. coin_blind,
  119. public_key,
  120. );
  121. let coords = public_key.to_affine().coordinates().unwrap();
  122. let c = MintContract {
  123. pub_x: Some(*coords.x()),
  124. pub_y: Some(*coords.y()),
  125. value: Some(DrkValue::from_u64(value)),
  126. asset: Some(token_id),
  127. serial: Some(serial),
  128. coin_blind: Some(coin_blind),
  129. value_blind: Some(value_blind),
  130. asset_blind: Some(token_blind),
  131. };
  132. let start = Instant::now();
  133. // TODO: Don't always build this
  134. let pk = ProvingKey::build(K, MintContract::default());
  135. debug!("Setup: [{:?}]", start.elapsed());
  136. let start = Instant::now();
  137. let public_inputs = revealed.make_outputs();
  138. let proof = Proof::create(&pk, &[c], &public_inputs)?;
  139. debug!("Prove: [{:?}]", start.elapsed());
  140. Ok((proof, revealed))
  141. }
  142. pub fn verify_mint_proof(
  143. vk: &VerifyingKey,
  144. proof: &Proof,
  145. revealed: &MintRevealedValues,
  146. ) -> Result<()> {
  147. let public_inputs = revealed.make_outputs();
  148. Ok(proof.verify(vk, &public_inputs)?)
  149. }