This commit is contained in:
Rostyslav Tyshko 2024-12-02 01:20:08 +01:00
parent 3204df13f1
commit 91786d6b7e
6 changed files with 168 additions and 69 deletions

View File

@ -83,7 +83,12 @@ impl Account {
self.balance = new_balance;
}
pub fn add_asset<Asset: Serialize>(&mut self, asset: Asset, amount: u128, privacy_flag: bool) -> Result<()> {
pub fn add_asset<Asset: Serialize>(
&mut self,
asset: Asset,
amount: u128,
privacy_flag: bool,
) -> Result<()> {
let payload_with_asset = UTXOPayload {
owner: self.address,
asset: serde_json::to_vec(&asset)?,

View File

@ -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<u8> {
// 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<u8> {
let mut input = bincode::serialize(input_utxo).unwrap().to_vec();
@ -53,8 +59,8 @@ pub fn generate_nullifiers(input_utxo: &UTXO, nsk: &[u8]) -> Vec<u8> {
// 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<Vec<u8>> {
input_utxos
.iter()
@ -67,7 +73,7 @@ pub fn generate_commitments(input_utxos: &[UTXO]) -> Vec<Vec<u8>> {
// 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<u8>,
@ -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<u8>, Vec<Vec<u8>>) {
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)

View File

@ -1,2 +1,2 @@
mod se;
mod de;
mod se;

View File

@ -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<u8> {
// 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<u8> {
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<Vec<u8>> {
output_utxos
@ -69,9 +75,9 @@ pub fn generate_commitments(output_utxos: &[UTXO]) -> Vec<Vec<u8>> {
// 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);

View File

@ -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<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 hashes: Vec<_> = commitments.iter().map(|val | val.commitment_hash[0..32].try_into().unwrap()).collect::<Vec<_>>();
let hashes: Vec<_> = commitments
.iter()
.map(|val| val.commitment_hash[0..32].try_into().unwrap())
.collect::<Vec<_>>();
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<bool, monotree::Errors> {
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());
}
}

View File

@ -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;