Просмотр исходного кода

[consensus/tx] transfer tx fixed

mohab metwally 3 лет назад
Родитель
Сommit
2c7b54c702
7 измененных файлов с 44 добавлено и 19 удалено
  1. 16 0
      Cargo.lock
  2. 2 0
      Cargo.toml
  3. 9 5
      src/consensus/leadcoin.rs
  4. 3 0
      src/consensus/mod.rs
  5. 8 7
      src/consensus/rcpt.rs
  6. 3 7
      src/consensus/tx.rs
  7. 3 0
      src/error.rs

+ 16 - 0
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",

+ 2 - 0
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",

+ 9 - 5
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<TransferStx> {
         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,

+ 3 - 0
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;

+ 8 - 7
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<TxRcpt> {
+    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()
     }
 }

+ 3 - 7
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 {
-        //
-    }
 }

+ 3 - 0
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
     // =======================