| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- use std::{io, time::Instant};
- use halo2_gadgets::{
- primitives,
- primitives::poseidon::{ConstantLength, P128Pow5T3},
- };
- use log::debug;
- use pasta_curves::{
- arithmetic::{CurveAffine, FieldExt},
- group::Curve,
- pallas,
- };
- use super::{
- proof::{Proof, ProvingKey, VerifyingKey},
- util::{mod_r_p, pedersen_commitment_scalar, pedersen_commitment_u64},
- };
- use crate::{
- circuit::mint_contract::MintContract,
- serial::{Decodable, Encodable},
- types::*,
- Result,
- };
- pub struct MintProofKeys {
- pub vk: VerifyingKey,
- pub pk: ProvingKey,
- }
- impl MintProofKeys {
- pub fn initialize() -> Self {
- let start = Instant::now();
- debug!("Building proof verifying key for the mint contract...");
- let vk = VerifyingKey::build(11, MintContract::default());
- debug!("Building proof proving key for the mint contract...");
- let pk = ProvingKey::build(11, MintContract::default());
- debug!("Setup: [{:?}]", start.elapsed());
- MintProofKeys { vk, pk }
- }
- }
- pub struct MintRevealedValues {
- pub value_commit: DrkValueCommit,
- pub token_commit: DrkValueCommit,
- //pub coin: [u8; 32],
- pub coin: pallas::Base,
- }
- impl MintRevealedValues {
- fn compute(
- value: u64,
- token_id: DrkTokenId,
- value_blind: DrkValueBlind,
- token_blind: DrkValueBlind,
- serial: DrkSerial,
- coin_blind: DrkCoinBlind,
- public_key: DrkPublicKey,
- ) -> Self {
- let value_commit = pedersen_commitment_u64(value, value_blind);
- let token_commit = pedersen_commitment_scalar(mod_r_p(token_id), token_blind);
- let coords = public_key.to_affine().coordinates().unwrap();
- let messages = [
- [*coords.x(), *coords.y()],
- [DrkValue::from_u64(value), token_id],
- [serial, coin_blind],
- ];
- let mut coin = DrkCoin::zero();
- for msg in messages.iter() {
- coin += primitives::poseidon::Hash::init(P128Pow5T3, ConstantLength::<2>).hash(*msg);
- }
- //let coin = hash.to_bytes();
- MintRevealedValues { value_commit, token_commit, coin }
- }
- fn make_outputs(&self) -> [DrkCircuitField; 5] {
- let value_coords = self.value_commit.to_affine().coordinates().unwrap();
- let token_coords = self.token_commit.to_affine().coordinates().unwrap();
- vec![
- //DrkCircuitField::from_bytes(&self.coin).unwrap(),
- self.coin,
- *value_coords.x(),
- *value_coords.y(),
- *token_coords.x(),
- *token_coords.y(),
- ]
- .try_into()
- .unwrap()
- }
- }
- impl Encodable for MintRevealedValues {
- fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
- let mut len = 0;
- len += self.value_commit.encode(&mut s)?;
- len += self.token_commit.encode(&mut s)?;
- len += self.coin.encode(&mut s)?;
- Ok(len)
- }
- }
- impl Decodable for MintRevealedValues {
- fn decode<D: io::Read>(mut d: D) -> Result<Self> {
- Ok(Self {
- value_commit: Decodable::decode(&mut d)?,
- token_commit: Decodable::decode(&mut d)?,
- coin: Decodable::decode(d)?,
- })
- }
- }
- #[allow(clippy::too_many_arguments)]
- pub fn create_mint_proof(
- value: u64,
- token_id: DrkTokenId,
- value_blind: DrkValueBlind,
- token_blind: DrkValueBlind,
- serial: DrkSerial,
- coin_blind: DrkCoinBlind,
- public_key: DrkPublicKey,
- ) -> Result<(Proof, MintRevealedValues)> {
- const K: u32 = 11;
- let revealed = MintRevealedValues::compute(
- value,
- token_id,
- value_blind,
- token_blind,
- serial,
- coin_blind,
- public_key,
- );
- let coords = public_key.to_affine().coordinates().unwrap();
- let c = MintContract {
- pub_x: Some(*coords.x()),
- pub_y: Some(*coords.y()),
- value: Some(DrkValue::from_u64(value)),
- asset: Some(token_id),
- serial: Some(serial),
- coin_blind: Some(coin_blind),
- value_blind: Some(value_blind),
- asset_blind: Some(token_blind),
- };
- let start = Instant::now();
- // TODO: Don't always build this
- let pk = ProvingKey::build(K, MintContract::default());
- debug!("Setup: [{:?}]", start.elapsed());
- let start = Instant::now();
- let public_inputs = revealed.make_outputs();
- let proof = Proof::create(&pk, &[c], &public_inputs)?;
- debug!("Prove: [{:?}]", start.elapsed());
- Ok((proof, revealed))
- }
- pub fn verify_mint_proof(
- vk: &VerifyingKey,
- proof: &Proof,
- revealed: &MintRevealedValues,
- ) -> Result<()> {
- let public_inputs = revealed.make_outputs();
- Ok(proof.verify(vk, &public_inputs)?)
- }
|