From 2c7b54c702f0e584ae4a516e5912e5a96a98a26f Mon Sep 17 00:00:00 2001 From: mohab metwally Date: Sun, 20 Nov 2022 17:48:37 +0200 Subject: [PATCH] [consensus/tx] transfer tx fixed --- Cargo.lock | 16 ++++++++++++++++ Cargo.toml | 2 ++ src/consensus/leadcoin.rs | 14 +++++++++----- src/consensus/mod.rs | 3 +++ src/consensus/rcpt.rs | 15 ++++++++------- src/consensus/tx.rs | 10 +++------- src/error.rs | 3 +++ 7 files changed, 44 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f01ef26a9..139fecb58 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1044,6 +1044,21 @@ dependencies = [ "typenum", ] +[[package]] +name = "crypto_api" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f855e87e75a4799e18b8529178adcde6fd4f97c1449ff4821e747ff728bb102" + +[[package]] +name = "crypto_api_chachapoly" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b63ae1025a6981f91b70bdcf11827189f49b01aaa3720115b330cd325d1c3809" +dependencies = [ + "crypto_api", +] + [[package]] name = "crypto_box" version = "0.8.2" @@ -1261,6 +1276,7 @@ dependencies = [ "chacha20poly1305", "chrono", "clap 3.2.23", + "crypto_api_chachapoly", "darkfi-derive", "darkfi-derive-internal", "darkfi-sdk", diff --git a/Cargo.toml b/Cargo.toml index a90057efc..46933e035 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -115,6 +115,7 @@ rand = {version = "0.8.5", optional = true} blake2b_simd = {version = "1.0.0", optional = true} blake3 = {version = "1.3.1", optional = true} chacha20poly1305 = {version = "0.10.1", optional = true} +crypto_api_chachapoly = {version = "0.5.0", optional = true} halo2_proofs = {version = "0.2.0", optional = true} halo2_gadgets = {version = "0.2.0", optional = true} incrementalmerkletree = {version = "0.3.0", optional = true} @@ -170,6 +171,7 @@ blockchain = [ "rand", "sled", "url", + "crypto_api_chachapoly", "async-runtime", "crypto", diff --git a/src/consensus/leadcoin.rs b/src/consensus/leadcoin.rs index 78c1da0f7..b58bf571e 100644 --- a/src/consensus/leadcoin.rs +++ b/src/consensus/leadcoin.rs @@ -38,6 +38,7 @@ use crate::{ zkas::ZkBinary, Result, }; +use darkfi_serial::{Encodable, Decodable, SerialDecodable, SerialEncodable}; pub const MERKLE_DEPTH_LEADCOIN: usize = 32; pub const MERKLE_DEPTH: u8 = 32; @@ -49,10 +50,12 @@ pub const PREFIX_CM: u64 = 4; pub const PREFIX_PK: u64 = 5; pub const PREFIX_SN: u64 = 6; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, SerialDecodable, SerialEncodable)] pub struct TransferStx { - /// commitments [coin3_commitment, coin4_commitment] - pub commitments: [pallas::Point; 2], + /// coin3_commitment in zk + pub change_coin_commitment: pallas::Point, + /// coin4_commitment in zk + pub transfered_coin_commitment: pallas::Point, /// nullifiers coin1_nullifier pub nullifier: pallas::Base, /// sk coin pos @@ -383,7 +386,7 @@ impl LeadCoin { pk: &ProvingKey, change_coin: TxRcpt, change_pk: pallas::Base, //change coin public key - transfered_coin: TxRcpt + transfered_coin: TxRcpt, transfered_pk: pallas::Base // recipient coin's public key ) -> Result { assert!(change_coin.value+transfered_coin.value==self.value @@ -435,7 +438,8 @@ impl LeadCoin { let cm4_msg = poseidon_hash(cm4_msg_in); let cm4 = pedersen_commitment_base(cm4_msg, transfered_coin.opening); let tx = TransferStx { - commitments: [cm3, cm4], + change_coin_commitment: cm3, + transfered_coin_commitment: cm4, nullifier: self.sn, tau: self.tau, root: self.coin1_commitment_root, diff --git a/src/consensus/mod.rs b/src/consensus/mod.rs index 3f523a949..aa68f0261 100644 --- a/src/consensus/mod.rs +++ b/src/consensus/mod.rs @@ -57,3 +57,6 @@ pub mod wallet; /// received transaction. pub mod rcpt; pub use rcpt::{TxRcpt,EncryptedTxRcpt}; + +pub mod tx; +pub use tx::Tx; diff --git a/src/consensus/rcpt.rs b/src/consensus/rcpt.rs index d71a9cd50..3262ee3ac 100644 --- a/src/consensus/rcpt.rs +++ b/src/consensus/rcpt.rs @@ -1,5 +1,6 @@ use darkfi_sdk::{ crypto::{ + keypair::{PublicKey}, diffie_hellman::{kdf_sapling, sapling_ka_agree}, pedersen::{pedersen_commitment_base, pedersen_commitment_u64}, poseidon_hash, @@ -15,13 +16,13 @@ use incrementalmerkletree::{bridgetree::BridgeTree, Tree}; use log::debug; use rand::rngs::OsRng; +use darkfi_serial::{Encodable, Decodable, SerialDecodable, SerialEncodable}; use super::constants::{EPOCH_LENGTH}; use crate::{ crypto::{proof::ProvingKey, Proof}, zk::{vm::ZkCircuit, vm_stack::Witness}, zkas::ZkBinary, - serial::darkfi_derive::{SerialDecodable, SerialEncodable}; - Result, + Result, Error, }; use crypto_api_chachapoly::ChachaPolyIetf; @@ -52,7 +53,7 @@ impl TxRcpt { let key = kdf_sapling(&shared_secret, &ephem_public); let mut input = Vec::new(); - self.encode(&mut input)?; + self.encode(&mut input).unwrap(); let mut ciphertext = [0u8; CIPHER_SIZE]; assert_eq!( @@ -62,7 +63,7 @@ impl TxRcpt { CIPHER_SIZE ); - Ok(EncryptedTxRcpt { ciphertext, ephem_public }) + EncryptedTxRcpt { ciphertext, ephem_public } } } @@ -74,7 +75,7 @@ pub struct EncryptedTxRcpt { } impl EncryptedTxRcpt { - pub fn decrypt(&self, secret: &SecretKey) -> Result { + pub fn decrypt(&self, secret: &SecretKey) -> TxRcpt { let shared_secret = sapling_ka_agree(secret, &self.ephem_public); let key = kdf_sapling(&shared_secret, &self.ephem_public); @@ -82,10 +83,10 @@ impl EncryptedTxRcpt { assert_eq!( ChachaPolyIetf::aead_cipher() .open_to(&mut plaintext, &self.ciphertext, &[], key.as_ref(), &[0u8; 12]) - .map_err(|_| Error::NoteDecryptionFailed)?, + .map_err(|_| Error::TxRcptDecryptionError).unwrap(), PLAINTEXT_SIZE ); - TxRcpt::decode(&plaintext[..]) + TxRcpt::decode(&plaintext[..]).unwrap() } } diff --git a/src/consensus/tx.rs b/src/consensus/tx.rs index 9c31349e9..cd1e2dc37 100644 --- a/src/consensus/tx.rs +++ b/src/consensus/tx.rs @@ -1,10 +1,10 @@ +use darkfi_serial::{Encodable, Decodable, SerialDecodable, SerialEncodable}; use crate::{ - consensus::{EncryptedTxRcpt, TransferStx}, - serial::darkfi_derive::{SerialDecodable, SerialEncodable}; + consensus::{EncryptedTxRcpt, leadcoin::TransferStx}, }; /// transfer transaction -#[derive(Debug, Clone, Copy, SerialDecodable, SerialEncodable)] +#[derive(Debug, Clone, SerialDecodable, SerialEncodable)] pub struct Tx { pub xfer: TransferStx, pub cipher: EncryptedTxRcpt, @@ -16,8 +16,4 @@ impl Tx { //TODO: verify tx true } - - pub fn leadcoin(&self) -> LeadCoin { - // - } } diff --git a/src/error.rs b/src/error.rs index cfbaa826a..89574fa1b 100644 --- a/src/error.rs +++ b/src/error.rs @@ -196,6 +196,9 @@ pub enum Error { #[error("Invalid DNS Name {0}")] RustlsInvalidDns(String), + #[error("unable to decrypt rcpt")] + TxRcptDecryptionError, + // ======================= // Protocol-related errors // =======================