From 437a3a5700d4c9bf3ef1370fb8251adb6e73c27f Mon Sep 17 00:00:00 2001 From: Oleksandr Pravdyvyi Date: Fri, 28 Mar 2025 17:12:28 +0200 Subject: [PATCH] feat: making tags for utxo to privately filter decoding attempts --- accounts/src/account_core/mod.rs | 6 +++- node_core/src/lib.rs | 47 +++++++++++++++++++++----------- node_core/src/storage/mod.rs | 26 ++++++++++-------- storage/src/transaction.rs | 5 ++-- 4 files changed, 53 insertions(+), 31 deletions(-) diff --git a/accounts/src/account_core/mod.rs b/accounts/src/account_core/mod.rs index 91bdfe8..3421a63 100644 --- a/accounts/src/account_core/mod.rs +++ b/accounts/src/account_core/mod.rs @@ -4,7 +4,7 @@ use anyhow::Result; use k256::AffinePoint; use log::info; use serde::Serialize; -use storage::{merkle_tree_public::TreeHashType, nullifier::UTXONullifier}; +use storage::{merkle_tree_public::TreeHashType, nullifier::UTXONullifier, transaction::Tag}; use utxo::{ utxo_core::{UTXOPayload, UTXO}, utxo_tree::UTXOSparseMerkleTree, @@ -122,6 +122,10 @@ impl Account { info!("Account address is {:?}", hex::encode(self.address)); info!("Account balance is {:?}", self.balance); } + + pub fn make_tag(&self) -> Tag { + self.address[0] + } } impl Default for Account { diff --git a/node_core/src/lib.rs b/node_core/src/lib.rs index d5092fc..33a0fd5 100644 --- a/node_core/src/lib.rs +++ b/node_core/src/lib.rs @@ -189,6 +189,8 @@ impl NodeCore { &serde_json::to_vec(&utxo).unwrap(), ); + let tag = accout.make_tag(); + let comm = generate_commitments(&vec![utxo]); Ok(( @@ -206,7 +208,7 @@ impl NodeCore { receipt, ) .unwrap(), - encoded_data: vec![(encoded_data.0, encoded_data.1.to_vec())], + encoded_data: vec![(encoded_data.0, encoded_data.1.to_vec(), tag)], ephemeral_pub_key: eph_pub_key.to_vec(), } .into(), @@ -235,13 +237,16 @@ impl NodeCore { let encoded_data = utxos .iter() .map(|utxo| { - Account::encrypt_data( - &ephm_key_holder, - accout.key_holder.viewing_public_key, - &serde_json::to_vec(&utxo).unwrap(), + ( + Account::encrypt_data( + &ephm_key_holder, + accout.key_holder.viewing_public_key, + &serde_json::to_vec(&utxo).unwrap(), + ), + accout.make_tag(), ) }) - .map(|(ciphertext, nonce)| (ciphertext, nonce.to_vec())) + .map(|((ciphertext, nonce), tag)| (ciphertext, nonce.to_vec(), tag)) .collect(); let comm = generate_commitments(&utxos); @@ -305,7 +310,7 @@ impl NodeCore { let eph_pub_key = ephm_key_holder.generate_ephemeral_public_key().to_bytes(); - let encoded_data: Vec<(Vec, Vec)> = utxos + let encoded_data: Vec<(Vec, Vec, u8)> = utxos .iter() .map(|utxo_enc| { let accout_enc = acc_map_read_guard.acc_map.get(&utxo_enc.owner).unwrap(); @@ -316,7 +321,9 @@ impl NodeCore { &serde_json::to_vec(&utxo_enc).unwrap(), ); - (ciphertext, nonce.to_vec()) + let tag = accout_enc.make_tag(); + + (ciphertext, nonce.to_vec(), tag) }) .collect(); @@ -387,7 +394,7 @@ impl NodeCore { let eph_pub_key = ephm_key_holder.generate_ephemeral_public_key().to_bytes(); - let mut encoded_data: Vec<(Vec, Vec)> = resulting_utxos_receiver + let mut encoded_data: Vec<(Vec, Vec, u8)> = resulting_utxos_receiver .iter() .map(|utxo_enc| { let accout_enc = acc_map_read_guard.acc_map.get(&utxo_enc.owner).unwrap(); @@ -398,11 +405,13 @@ impl NodeCore { &serde_json::to_vec(&utxo_enc).unwrap(), ); - (ciphertext, nonce.to_vec()) + let tag = accout_enc.make_tag(); + + (ciphertext, nonce.to_vec(), tag) }) .collect(); - let encoded_data_1: Vec<(Vec, Vec)> = resulting_utxos_not_spent + let encoded_data_1: Vec<(Vec, Vec, u8)> = resulting_utxos_not_spent .iter() .map(|utxo_enc| { let accout_enc = acc_map_read_guard.acc_map.get(&utxo_enc.owner).unwrap(); @@ -413,7 +422,9 @@ impl NodeCore { &serde_json::to_vec(&utxo_enc).unwrap(), ); - (ciphertext, nonce.to_vec()) + let tag = accout_enc.make_tag(); + + (ciphertext, nonce.to_vec(), tag) }) .collect(); @@ -489,7 +500,7 @@ impl NodeCore { let eph_pub_key = ephm_key_holder.generate_ephemeral_public_key().to_bytes(); - let encoded_data: Vec<(Vec, Vec)> = utxos + let encoded_data: Vec<(Vec, Vec, u8)> = utxos .iter() .map(|utxo_enc| { let accout_enc = acc_map_read_guard.acc_map.get(&utxo_enc.owner).unwrap(); @@ -500,7 +511,9 @@ impl NodeCore { &serde_json::to_vec(&utxo_enc).unwrap(), ); - (ciphertext, nonce.to_vec()) + let tag = accout_enc.make_tag(); + + (ciphertext, nonce.to_vec(), tag) }) .collect(); @@ -1113,7 +1126,7 @@ impl NodeCore { let eph_pub_key = ephm_key_holder.generate_ephemeral_public_key().to_bytes(); - let encoded_data: Vec<(Vec, Vec)> = utxos + let encoded_data: Vec<(Vec, Vec, u8)> = utxos .iter() .map(|utxo_enc| { let accout_enc = acc_map_read_guard.acc_map.get(&utxo_enc.owner).unwrap(); @@ -1124,7 +1137,9 @@ impl NodeCore { &serde_json::to_vec(&utxo_enc).unwrap(), ); - (ciphertext, nonce.to_vec()) + let tag = accout_enc.make_tag(); + + (ciphertext, nonce.to_vec(), tag) }) .collect(); diff --git a/node_core/src/storage/mod.rs b/node_core/src/storage/mod.rs index 31dc538..4afcb62 100644 --- a/node_core/src/storage/mod.rs +++ b/node_core/src/storage/mod.rs @@ -111,7 +111,7 @@ impl NodeChainStore { if ephemeral_public_key_sender.is_some().into() { let ephemeral_public_key_sender = ephemeral_public_key_sender.unwrap(); - for (ciphertext, nonce) in tx.encoded_data.clone() { + for (ciphertext, nonce, tag) in tx.encoded_data.clone() { let slice = nonce.as_slice(); let nonce = accounts::key_management::constants_types::Nonce::clone_from_slice( @@ -119,19 +119,21 @@ impl NodeChainStore { ); for (acc_id, acc) in self.acc_map.iter_mut() { - let decoded_data_curr_acc = acc.decrypt_data( - ephemeral_public_key_sender, - ciphertext.clone(), - nonce, - ); + if acc_id[0] == tag { + let decoded_data_curr_acc = acc.decrypt_data( + ephemeral_public_key_sender, + ciphertext.clone(), + nonce, + ); - if let Ok(decoded_data_curr_acc) = decoded_data_curr_acc { - let decoded_utxo_try = - serde_json::from_slice::(&decoded_data_curr_acc); + if let Ok(decoded_data_curr_acc) = decoded_data_curr_acc { + let decoded_utxo_try = + serde_json::from_slice::(&decoded_data_curr_acc); - if let Ok(utxo) = decoded_utxo_try { - if &utxo.owner == acc_id { - acc.utxo_tree.insert_item(utxo)?; + if let Ok(utxo) = decoded_utxo_try { + if &utxo.owner == acc_id { + acc.utxo_tree.insert_item(utxo)?; + } } } } diff --git a/storage/src/transaction.rs b/storage/src/transaction.rs index 15672ae..2a7f23c 100644 --- a/storage/src/transaction.rs +++ b/storage/src/transaction.rs @@ -12,6 +12,7 @@ use sha2::digest::typenum::{UInt, UTerm}; pub type CipherText = Vec; pub type Nonce = GenericArray, B1>, B0>, B0>>; +pub type Tag = u8; #[derive(Debug, Serialize, Deserialize, Clone, Copy)] pub enum TxKind { @@ -39,7 +40,7 @@ pub struct Transaction { ///Execution proof (private part) pub execution_proof_private: String, ///Encoded blobs of data - pub encoded_data: Vec<(CipherText, Vec)>, + pub encoded_data: Vec<(CipherText, Vec, Tag)>, ///Transaction senders ephemeral pub key pub ephemeral_pub_key: Vec, } @@ -61,7 +62,7 @@ pub struct TransactionPayload { ///Execution proof (private part) pub execution_proof_private: String, ///Encoded blobs of data - pub encoded_data: Vec<(CipherText, Vec)>, + pub encoded_data: Vec<(CipherText, Vec, Tag)>, ///Transaction senders ephemeral pub key pub ephemeral_pub_key: Vec, }