diff --git a/accounts/src/account_core/mod.rs b/accounts/src/account_core/mod.rs index f6b0cd2..5ce19a7 100644 --- a/accounts/src/account_core/mod.rs +++ b/accounts/src/account_core/mod.rs @@ -83,7 +83,12 @@ impl Account { self.balance = new_balance; } - pub fn add_asset(&mut self, asset: Asset, amount: u128, privacy_flag: bool) -> Result<()> { + pub fn add_asset( + &mut self, + asset: Asset, + amount: u128, + privacy_flag: bool, + ) -> Result<()> { let payload_with_asset = UTXOPayload { owner: self.address, asset: serde_json::to_vec(&asset)?, diff --git a/node_core/src/executions/de.rs b/node_core/src/executions/de.rs index 8ca1899..e8b9fef 100644 --- a/node_core/src/executions/de.rs +++ b/node_core/src/executions/de.rs @@ -1,16 +1,22 @@ +use bincode; use k256::Scalar; -use secp256k1_zkp::{compute_adaptive_blinding_factor, verify_commitments_sum_to_equal, CommitmentSecrets, Generator, PedersenCommitment, Tag, Tweak, SECP256K1}; +use monotree::hasher::Blake3; +use monotree::{Hasher, Monotree, Proof}; use rand::thread_rng; +use secp256k1_zkp::{ + compute_adaptive_blinding_factor, verify_commitments_sum_to_equal, CommitmentSecrets, + Generator, PedersenCommitment, Tag, Tweak, SECP256K1, +}; +use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; -use serde::{Serialize, Deserialize}; -use storage::{commitment::Commitment, commitments_sparse_merkle_tree::CommitmentsSparseMerkleTree, nullifier::UTXONullifier, nullifier_sparse_merkle_tree::NullifierSparseMerkleTree}; +use storage::{ + commitment::Commitment, commitments_sparse_merkle_tree::CommitmentsSparseMerkleTree, + nullifier::UTXONullifier, nullifier_sparse_merkle_tree::NullifierSparseMerkleTree, +}; use utxo::{ utxo_core::{UTXOPayload, UTXO}, utxo_tree::UTXOSparseMerkleTree, }; -use monotree::hasher::Blake3; -use monotree::{Hasher, Monotree, Proof}; -use bincode; fn commitment_secrets_random(value: u64) -> CommitmentSecrets { CommitmentSecrets { @@ -43,7 +49,7 @@ fn hash(input: &[u8]) -> Vec { // Generate nullifiers -// takes the input_utxo and nsk +// takes the input_utxo and nsk // returns the nullifiers[i], where the nullifier[i] = hash(in_commitments[i] || nsk) where the hash function pub fn generate_nullifiers(input_utxo: &UTXO, nsk: &[u8]) -> Vec { let mut input = bincode::serialize(input_utxo).unwrap().to_vec(); @@ -53,8 +59,8 @@ pub fn generate_nullifiers(input_utxo: &UTXO, nsk: &[u8]) -> Vec { // Generate commitments for output UTXOs -// uses the list of input_utxos[] -// returns in_commitments[] where each in_commitments[i] = Commitment(in_utxos[i]) where the commitment +// uses the list of input_utxos[] +// returns in_commitments[] where each in_commitments[i] = Commitment(in_utxos[i]) where the commitment pub fn generate_commitments(input_utxos: &[UTXO]) -> Vec> { input_utxos .iter() @@ -67,7 +73,7 @@ pub fn generate_commitments(input_utxos: &[UTXO]) -> Vec> { // Validate inclusion proof for in_commitments -// takes the in_commitments[i] as a leaf, the root hash root_commitment and the path in_commitments_proofs[i][], +// takes the in_commitments[i] as a leaf, the root hash root_commitment and the path in_commitments_proofs[i][], // returns True if the in_commitments[i] is in the tree with root hash root_commitment otherwise returns False, as membership proof. pub fn validate_in_commitments_proof( in_commitment: &Vec, @@ -84,16 +90,23 @@ pub fn validate_in_commitments_proof( hasher: Blake3::new(), }; - let commitments: Vec<_> = in_commitments_proof.into_iter().map(|n_p| Commitment { commitment_hash: n_p.clone() }).collect(); + let commitments: Vec<_> = in_commitments_proof + .into_iter() + .map(|n_p| Commitment { + commitment_hash: n_p.clone(), + }) + .collect(); nsmt.insert_items(commitments).unwrap(); - - nsmt.get_non_membership_proof(in_commitment.clone()).unwrap().1.is_some() + nsmt.get_non_membership_proof(in_commitment.clone()) + .unwrap() + .1 + .is_some() } // Validate non-membership proof for nullifiers -// takes the nullifiers[i], path nullifiers_proof[i][] and the root hash root_nullifier, +// takes the nullifiers[i], path nullifiers_proof[i][] and the root hash root_nullifier, // returns True if the nullifiers[i] is not in the tree with root hash root_nullifier otherwise returns False, as non-membership proof. pub fn validate_nullifiers_proof( nullifier: [u8; 32], @@ -106,16 +119,21 @@ pub fn validate_nullifiers_proof( hasher: Blake3::new(), }; - let nullifiers: Vec<_> = nullifiers_proof.into_iter().map(|n_p| UTXONullifier { utxo_hash: *n_p }).collect(); + let nullifiers: Vec<_> = nullifiers_proof + .into_iter() + .map(|n_p| UTXONullifier { utxo_hash: *n_p }) + .collect(); nsmt.insert_items(nullifiers).unwrap(); - - nsmt.get_non_membership_proof(nullifier).unwrap().1.is_none() + nsmt.get_non_membership_proof(nullifier) + .unwrap() + .1 + .is_none() } // Check balances -// takes the public_info and output_utxos[], +// takes the public_info and output_utxos[], // returns the True if the token amount in public_info matches the sum of all output_utxos[], otherwise return False. pub fn check_balances(public_info: u128, output_utxos: &[UTXO]) -> bool { let total_output: u128 = output_utxos.iter().map(|utxo| utxo.amount).sum(); @@ -124,9 +142,13 @@ pub fn check_balances(public_info: u128, output_utxos: &[UTXO]) -> bool { // Verify Pedersen commitment -// takes the public_info, secret_r and pedersen_commitment and +// takes the public_info, secret_r and pedersen_commitment and // checks that commitment(public_info,secret_r) is equal pedersen_commitment where the commitment is pedersen commitment. -pub fn verify_commitment(public_info: u64, secret_r: &[u8], pedersen_commitment: &PedersenCommitment) -> bool { +pub fn verify_commitment( + public_info: u64, + secret_r: &[u8], + pedersen_commitment: &PedersenCommitment, +) -> bool { let commitment_secrets = CommitmentSecrets { value: public_info, value_blinding_factor: Tweak::from_slice(secret_r).unwrap(), @@ -139,7 +161,6 @@ pub fn verify_commitment(public_info: u64, secret_r: &[u8], pedersen_commitment: commitment == *pedersen_commitment } - fn de_kernel( root_commitment: &[u8], root_nullifier: [u8; 32], @@ -151,16 +172,27 @@ fn de_kernel( ) -> (Vec, Vec>) { check_balances(public_info as u128, input_utxos); - let nullifiers: Vec<_> = input_utxos.into_iter().map(|utxo| generate_nullifiers(&utxo, &nullifier_secret_key.to_bytes())).collect(); + let nullifiers: Vec<_> = input_utxos + .into_iter() + .map(|utxo| generate_nullifiers(&utxo, &nullifier_secret_key.to_bytes())) + .collect(); let in_commitments = generate_commitments(&input_utxos); for in_commitment in in_commitments { - validate_in_commitments_proof(&in_commitment, root_commitment.to_vec(), in_commitments_proof); + validate_in_commitments_proof( + &in_commitment, + root_commitment.to_vec(), + in_commitments_proof, + ); } for nullifier in nullifiers.iter() { - validate_nullifiers_proof(nullifier[0..32].try_into().unwrap(), root_nullifier, nullifiers_proof); + validate_nullifiers_proof( + nullifier[0..32].try_into().unwrap(), + root_nullifier, + nullifiers_proof, + ); } (vec![], nullifiers) diff --git a/node_core/src/executions/mod.rs b/node_core/src/executions/mod.rs index b997b2b..712e3e2 100644 --- a/node_core/src/executions/mod.rs +++ b/node_core/src/executions/mod.rs @@ -1,2 +1,2 @@ -mod se; mod de; +mod se; diff --git a/node_core/src/executions/se.rs b/node_core/src/executions/se.rs index 8428fbc..ed8d04e 100644 --- a/node_core/src/executions/se.rs +++ b/node_core/src/executions/se.rs @@ -1,16 +1,22 @@ +use bincode; use k256::Scalar; -use secp256k1_zkp::{compute_adaptive_blinding_factor, verify_commitments_sum_to_equal, CommitmentSecrets, Generator, PedersenCommitment, Tag, Tweak, SECP256K1}; +use monotree::hasher::Blake3; +use monotree::{Hasher, Monotree, Proof}; use rand::thread_rng; +use secp256k1_zkp::{ + compute_adaptive_blinding_factor, verify_commitments_sum_to_equal, CommitmentSecrets, + Generator, PedersenCommitment, Tag, Tweak, SECP256K1, +}; +use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; -use serde::{Serialize, Deserialize}; -use storage::{commitment::Commitment, commitments_sparse_merkle_tree::CommitmentsSparseMerkleTree, nullifier::UTXONullifier, nullifier_sparse_merkle_tree::NullifierSparseMerkleTree}; +use storage::{ + commitment::Commitment, commitments_sparse_merkle_tree::CommitmentsSparseMerkleTree, + nullifier::UTXONullifier, nullifier_sparse_merkle_tree::NullifierSparseMerkleTree, +}; use utxo::{ utxo_core::{UTXOPayload, UTXO}, utxo_tree::UTXOSparseMerkleTree, }; -use monotree::hasher::Blake3; -use monotree::{Hasher, Monotree, Proof}; -use bincode; fn commitment_secrets_random(value: u64) -> CommitmentSecrets { CommitmentSecrets { @@ -43,8 +49,8 @@ fn hash(input: &[u8]) -> Vec { // Generate nullifiers -// takes the pedersen_commitment and nsk then -// returns a list of nullifiers, where the nullifier = hash(pedersen_commitment || nsk) where the hash function will be determined +// takes the pedersen_commitment and nsk then +// returns a list of nullifiers, where the nullifier = hash(pedersen_commitment || nsk) where the hash function will be determined pub fn generate_nullifiers(pedersen_commitment: &PedersenCommitment, nsk: &[u8]) -> Vec { let mut input = pedersen_commitment.serialize().to_vec(); @@ -54,8 +60,8 @@ pub fn generate_nullifiers(pedersen_commitment: &PedersenCommitment, nsk: &[u8]) // Generate commitments for output UTXOs -// uses the list of output_utxos[] and -// returns out_commitments[] where each out_commitments[i] = Commitment(output_utxos[i]) +// uses the list of output_utxos[] and +// returns out_commitments[] where each out_commitments[i] = Commitment(output_utxos[i]) // where the commitment will be determined pub fn generate_commitments(output_utxos: &[UTXO]) -> Vec> { output_utxos @@ -69,9 +75,9 @@ pub fn generate_commitments(output_utxos: &[UTXO]) -> Vec> { // Validate inclusion proof for in_commitments -// takes the pedersen_commitment as a leaf, the root hash root_commitment and the path in_commitments_proof[], -// returns True if the pedersen_commitment is in the tree with root hash root_commitment -// otherwise +// takes the pedersen_commitment as a leaf, the root hash root_commitment and the path in_commitments_proof[], +// returns True if the pedersen_commitment is in the tree with root hash root_commitment +// otherwise // returns False, as membership proof. pub fn validate_in_commitments_proof( pedersen_commitment: &PedersenCommitment, @@ -84,17 +90,25 @@ pub fn validate_in_commitments_proof( hasher: Blake3::new(), }; - let commitments: Vec<_> = in_commitments_proof.into_iter().map(|n_p| Commitment { commitment_hash: n_p.clone() }).collect(); + let commitments: Vec<_> = in_commitments_proof + .into_iter() + .map(|n_p| Commitment { + commitment_hash: n_p.clone(), + }) + .collect(); nsmt.insert_items(commitments).unwrap(); - nsmt.get_non_membership_proof(pedersen_commitment.serialize().to_vec()).unwrap().1.is_some() + nsmt.get_non_membership_proof(pedersen_commitment.serialize().to_vec()) + .unwrap() + .1 + .is_some() } // Validate non-membership proof for nullifiers -// takes the nullifier, path nullifiers_proof[] and the root hash root_nullifier, -// returns True if the nullifier is not in the tree with root hash root_nullifier -// otherwise +// takes the nullifier, path nullifiers_proof[] and the root hash root_nullifier, +// returns True if the nullifier is not in the tree with root hash root_nullifier +// otherwise // returns False, as non-membership proof. pub fn validate_nullifiers_proof( nullifier: [u8; 32], @@ -107,16 +121,21 @@ pub fn validate_nullifiers_proof( hasher: Blake3::new(), }; - let nullifiers: Vec<_> = nullifiers_proof.into_iter().map(|n_p| UTXONullifier { utxo_hash: *n_p }).collect(); + let nullifiers: Vec<_> = nullifiers_proof + .into_iter() + .map(|n_p| UTXONullifier { utxo_hash: *n_p }) + .collect(); nsmt.insert_items(nullifiers).unwrap(); - - nsmt.get_non_membership_proof(nullifier).unwrap().1.is_none() + nsmt.get_non_membership_proof(nullifier) + .unwrap() + .1 + .is_none() } // Check balances -// takes the public_info and output_utxos[], +// takes the public_info and output_utxos[], // returns the True if the token amount in public_info matches the sum of all output_utxos[], otherwise return False. pub fn check_balances(public_info: u128, output_utxos: &[UTXO]) -> bool { let total_output: u128 = output_utxos.iter().map(|utxo| utxo.amount).sum(); @@ -125,9 +144,13 @@ pub fn check_balances(public_info: u128, output_utxos: &[UTXO]) -> bool { // Verify Pedersen commitment -// takes the public_info, secret_r and pedersen_commitment and +// takes the public_info, secret_r and pedersen_commitment and // checks that commitment(public_info,secret_r) is equal pedersen_commitment where the commitment is pedersen commitment. -pub fn verify_commitment(public_info: u64, secret_r: &[u8], pedersen_commitment: &PedersenCommitment) -> bool { +pub fn verify_commitment( + public_info: u64, + secret_r: &[u8], + pedersen_commitment: &PedersenCommitment, +) -> bool { let commitment_secrets = CommitmentSecrets { value: public_info, value_blinding_factor: Tweak::from_slice(secret_r).unwrap(), @@ -157,7 +180,11 @@ fn se_kernel( let nullifier = generate_nullifiers(&pedersen_commitment, &nullifier_secret_key.to_bytes()); - validate_in_commitments_proof(&pedersen_commitment, root_commitment.to_vec(), in_commitments_proof); + validate_in_commitments_proof( + &pedersen_commitment, + root_commitment.to_vec(), + in_commitments_proof, + ); verify_commitment(public_info, secret_r, &pedersen_commitment); diff --git a/storage/src/commitments_sparse_merkle_tree.rs b/storage/src/commitments_sparse_merkle_tree.rs index c6ef7b4..96f7eac 100644 --- a/storage/src/commitments_sparse_merkle_tree.rs +++ b/storage/src/commitments_sparse_merkle_tree.rs @@ -22,11 +22,16 @@ impl CommitmentsSparseMerkleTree { } pub fn insert_item(&mut self, commitment: Commitment) -> Result<(), monotree::Errors> { - let root = self.curr_root.as_ref().map(|val | val[0..32].try_into().unwrap()); + let root = self + .curr_root + .as_ref() + .map(|val| val[0..32].try_into().unwrap()); - let new_root = self - .tree - .insert(root, &commitment.commitment_hash[0..32].try_into().unwrap(), &commitment.commitment_hash[0..32].try_into().unwrap())?; + let new_root = self.tree.insert( + root, + &commitment.commitment_hash[0..32].try_into().unwrap(), + &commitment.commitment_hash[0..32].try_into().unwrap(), + )?; self.curr_root = new_root.map(|val| val.to_vec()); @@ -34,13 +39,19 @@ impl CommitmentsSparseMerkleTree { } pub fn insert_items(&mut self, commitments: Vec) -> Result<(), monotree::Errors> { - let root = self.curr_root.as_ref().map(|val | val[0..32].try_into().unwrap()); + let root = self + .curr_root + .as_ref() + .map(|val| val[0..32].try_into().unwrap()); - let hashes: Vec<_> = commitments.iter().map(|val | val.commitment_hash[0..32].try_into().unwrap()).collect::>(); + let hashes: Vec<_> = commitments + .iter() + .map(|val| val.commitment_hash[0..32].try_into().unwrap()) + .collect::>(); let new_root = self.tree.inserts(root, &hashes, &hashes)?; - self.curr_root = new_root.map(|val | val[0..32].try_into().unwrap()); + self.curr_root = new_root.map(|val| val[0..32].try_into().unwrap()); Ok(()) } @@ -50,7 +61,12 @@ impl CommitmentsSparseMerkleTree { commitment_hash: CommitmentHashType, ) -> Result { self.tree - .get(self.curr_root.as_ref().map(|val | val[0..32].try_into().unwrap()), &commitment_hash[0..32].try_into().unwrap()) + .get( + self.curr_root + .as_ref() + .map(|val| val[0..32].try_into().unwrap()), + &commitment_hash[0..32].try_into().unwrap(), + ) .map(|data| data.is_some()) } @@ -63,7 +79,12 @@ impl CommitmentsSparseMerkleTree { for nullifier_hash in commitment_hashes { let is_included = self .tree - .get(self.curr_root.as_ref().map(|val | val[0..32].try_into().unwrap()), nullifier_hash[0..32].try_into().unwrap()) + .get( + self.curr_root + .as_ref() + .map(|val| val[0..32].try_into().unwrap()), + nullifier_hash[0..32].try_into().unwrap(), + ) .map(|data| data.is_some())?; inclusions.push(is_included); @@ -82,8 +103,12 @@ impl CommitmentsSparseMerkleTree { Err(monotree::Errors::new("Is a member")) } else { Ok(( - self.tree - .get_merkle_proof(self.curr_root.as_ref().map(|val | val[0..32].try_into().unwrap()), &commitment_hash)?, + self.tree.get_merkle_proof( + self.curr_root + .as_ref() + .map(|val| val[0..32].try_into().unwrap()), + &commitment_hash, + )?, self.curr_root.clone(), )) } @@ -105,8 +130,12 @@ impl CommitmentsSparseMerkleTree { )); } else { non_membership_proofs.push(( - self.tree - .get_merkle_proof(self.curr_root.as_ref().map(|val | val[0..32].try_into().unwrap()), commitment_hash)?, + self.tree.get_merkle_proof( + self.curr_root + .as_ref() + .map(|val| val[0..32].try_into().unwrap()), + commitment_hash, + )?, self.curr_root.clone(), )) }; @@ -131,7 +160,9 @@ mod tests { use monotree::Monotree; fn create_nullifier(hash: CommitmentHashType) -> Commitment { - Commitment { commitment_hash: hash } + Commitment { + commitment_hash: hash, + } } #[test] @@ -239,11 +270,15 @@ mod tests { #[test] fn test_insert_and_get_proofs_of_existing_items() { let mut tree = CommitmentsSparseMerkleTree::new(); - let nullifiers = vec![create_nullifier([1u8; 32].to_vec()), create_nullifier([2u8; 32].to_vec())]; + let nullifiers = vec![ + create_nullifier([1u8; 32].to_vec()), + create_nullifier([2u8; 32].to_vec()), + ]; tree.insert_items(nullifiers).unwrap(); - let proof_result = tree.get_non_membership_proofs(&[[1u8; 32].to_vec(), [2u8; 32].to_vec()]); + let proof_result = + tree.get_non_membership_proofs(&[[1u8; 32].to_vec(), [2u8; 32].to_vec()]); assert!(proof_result.is_err()); } } diff --git a/storage/src/lib.rs b/storage/src/lib.rs index 0a7dc72..2e87492 100644 --- a/storage/src/lib.rs +++ b/storage/src/lib.rs @@ -9,12 +9,12 @@ use rocksdb::{ }; pub mod block; +pub mod commitment; +pub mod commitments_sparse_merkle_tree; pub mod error; pub mod merkle_tree_public; pub mod nullifier; -pub mod commitment; pub mod nullifier_sparse_merkle_tree; -pub mod commitments_sparse_merkle_tree; pub mod transaction; pub mod utxo_commitment;