diff --git a/src/crypto/spend_proof.rs b/src/crypto/burn_proof.rs similarity index 78% rename from src/crypto/spend_proof.rs rename to src/crypto/burn_proof.rs index 67e4c4ba2..ea6471b2d 100644 --- a/src/crypto/spend_proof.rs +++ b/src/crypto/burn_proof.rs @@ -1,4 +1,4 @@ -use std::{io, time::Instant}; +use std::time::Instant; use halo2_gadgets::primitives::{ poseidon, @@ -20,13 +20,13 @@ use crate::{ merkle_node::MerkleNode, types::*, }, - util::serial::{Decodable, Encodable}, - zk::circuit::spend_contract::SpendContract, + util::serial::{SerialDecodable, SerialEncodable}, + zk::circuit::burn_contract::BurnContract, Result, }; -#[derive(Debug, Clone, PartialEq)] -pub struct SpendRevealedValues { +#[derive(Debug, Clone, PartialEq, SerialEncodable, SerialDecodable)] +pub struct BurnRevealedValues { pub value_commit: DrkValueCommit, pub token_commit: DrkValueCommit, pub nullifier: Nullifier, @@ -34,7 +34,7 @@ pub struct SpendRevealedValues { pub signature_public: PublicKey, } -impl SpendRevealedValues { +impl BurnRevealedValues { #[allow(clippy::too_many_arguments)] pub fn compute( value: u64, @@ -77,7 +77,7 @@ impl SpendRevealedValues { let value_commit = pedersen_commitment_u64(value, value_blind); let token_commit = pedersen_commitment_scalar(mod_r_p(token_id), token_blind); - SpendRevealedValues { + BurnRevealedValues { value_commit, token_commit, nullifier: Nullifier(nullifier), @@ -107,32 +107,8 @@ impl SpendRevealedValues { } } -impl Encodable for SpendRevealedValues { - fn encode(&self, mut s: S) -> Result { - let mut len = 0; - len += self.value_commit.encode(&mut s)?; - len += self.token_commit.encode(&mut s)?; - len += self.nullifier.encode(&mut s)?; - len += self.merkle_root.encode(&mut s)?; - len += self.signature_public.encode(s)?; - Ok(len) - } -} - -impl Decodable for SpendRevealedValues { - fn decode(mut d: D) -> Result { - Ok(Self { - value_commit: Decodable::decode(&mut d)?, - token_commit: Decodable::decode(&mut d)?, - nullifier: Decodable::decode(&mut d)?, - merkle_root: Decodable::decode(&mut d)?, - signature_public: Decodable::decode(d)?, - }) - } -} - #[allow(clippy::too_many_arguments)] -pub fn create_spend_proof( +pub fn create_burn_proof( pk: &ProvingKey, value: u64, token_id: DrkTokenId, @@ -144,8 +120,8 @@ pub fn create_spend_proof( leaf_position: incrementalmerkletree::Position, merkle_path: Vec, signature_secret: SecretKey, -) -> Result<(Proof, SpendRevealedValues)> { - let revealed = SpendRevealedValues::compute( +) -> Result<(Proof, BurnRevealedValues)> { + let revealed = BurnRevealedValues::compute( value, token_id, value_blind, @@ -160,7 +136,7 @@ pub fn create_spend_proof( let leaf_position: u64 = leaf_position.into(); - let c = SpendContract { + let c = BurnContract { secret_key: Some(secret.0), serial: Some(serial), value: Some(DrkValue::from(value)), @@ -181,10 +157,10 @@ pub fn create_spend_proof( Ok((proof, revealed)) } -pub fn verify_spend_proof( +pub fn verify_burn_proof( vk: &VerifyingKey, - proof: Proof, - revealed: &SpendRevealedValues, + proof: &Proof, + revealed: &BurnRevealedValues, ) -> Result<()> { let public_inputs = revealed.make_outputs(); Ok(proof.verify(vk, &public_inputs)?) diff --git a/src/crypto/mint_proof.rs b/src/crypto/mint_proof.rs index a5633c45f..7df36f8fb 100644 --- a/src/crypto/mint_proof.rs +++ b/src/crypto/mint_proof.rs @@ -1,4 +1,4 @@ -use std::{io, time::Instant}; +use std::time::Instant; use halo2_gadgets::primitives::{ poseidon, @@ -16,12 +16,12 @@ use crate::{ types::{DrkCoinBlind, DrkSerial, DrkTokenId, DrkValue, DrkValueBlind, DrkValueCommit}, util::{mod_r_p, pedersen_commitment_scalar, pedersen_commitment_u64}, }, - util::serial::{Decodable, Encodable}, + util::serial::{SerialDecodable, SerialEncodable}, zk::circuit::mint_contract::MintContract, Result, }; -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, SerialEncodable, SerialDecodable)] pub struct MintRevealedValues { pub value_commit: DrkValueCommit, pub token_commit: DrkValueCommit, @@ -66,26 +66,6 @@ impl MintRevealedValues { } } -impl Encodable for MintRevealedValues { - fn encode(&self, mut s: S) -> Result { - 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(mut d: D) -> Result { - 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( pk: &ProvingKey, diff --git a/src/crypto/mod.rs b/src/crypto/mod.rs index cad790376..07a9ddf1a 100644 --- a/src/crypto/mod.rs +++ b/src/crypto/mod.rs @@ -4,21 +4,21 @@ pub mod constants; pub mod diffie_hellman; pub mod keypair; //pub mod loader; +pub mod burn_proof; pub mod merkle_node; pub mod mint_proof; pub mod note; pub mod nullifier; pub mod proof; pub mod schnorr; -pub mod spend_proof; pub mod token_id; pub mod token_list; pub mod types; pub mod util; +pub use burn_proof::BurnRevealedValues; pub use mint_proof::MintRevealedValues; pub use proof::Proof; -pub use spend_proof::SpendRevealedValues; use keypair::SecretKey; @@ -28,6 +28,7 @@ pub struct OwnCoin { pub note: note::Note, pub secret: SecretKey, pub nullifier: nullifier::Nullifier, + pub leaf_position: incrementalmerkletree::Position, } pub type OwnCoins = Vec; diff --git a/src/zk/circuit/spend_contract.rs b/src/zk/circuit/burn_contract.rs similarity index 98% rename from src/zk/circuit/spend_contract.rs rename to src/zk/circuit/burn_contract.rs index 4a0b8bc44..269d043a2 100644 --- a/src/zk/circuit/spend_contract.rs +++ b/src/zk/circuit/burn_contract.rs @@ -31,7 +31,7 @@ use crate::crypto::{ #[allow(dead_code)] #[derive(Clone, Debug)] -pub struct SpendConfig { +pub struct BurnConfig { primary: Column, advices: [Column; 10], ecc_config: EccConfig, @@ -44,7 +44,7 @@ pub struct SpendConfig { poseidon_config: PoseidonConfig, } -impl SpendConfig { +impl BurnConfig { fn ecc_chip(&self) -> EccChip { EccChip::construct(self.ecc_config.clone()) } @@ -91,7 +91,7 @@ const BURN_SIGKEYX_OFFSET: usize = 6; const BURN_SIGKEYY_OFFSET: usize = 7; #[derive(Default, Debug)] -pub struct SpendContract { +pub struct BurnContract { pub secret_key: Option, pub serial: Option, pub value: Option, @@ -105,12 +105,12 @@ pub struct SpendContract { pub sig_secret: Option, } -impl UtilitiesInstructions for SpendContract { +impl UtilitiesInstructions for BurnContract { type Var = AssignedCell; } -impl Circuit for SpendContract { - type Config = SpendConfig; +impl Circuit for BurnContract { + type Config = BurnConfig; type FloorPlanner = SimpleFloorPlanner; fn without_witnesses(&self) -> Self { @@ -218,7 +218,7 @@ impl Circuit for SpendContract { (sinsemilla_config_2, merkle_config_2) }; - SpendConfig { + BurnConfig { primary, advices, ecc_config, diff --git a/src/zk/circuit/mod.rs b/src/zk/circuit/mod.rs index 2e96d3bb7..59c3acfe0 100644 --- a/src/zk/circuit/mod.rs +++ b/src/zk/circuit/mod.rs @@ -1,5 +1,5 @@ -pub mod mint_contract; -pub mod spend_contract; +pub mod burn_contract; +pub use burn_contract::BurnContract; +pub mod mint_contract; pub use mint_contract::MintContract; -pub use spend_contract::SpendContract;