From 3c6356767e8c1a17cc6d4585aba828e5c42b92a4 Mon Sep 17 00:00:00 2001 From: Rostyslav Tyshko Date: Mon, 2 Dec 2024 01:19:30 +0100 Subject: [PATCH] add validate_in_commitments_proof and validate_nullifiers_proof to se module --- node_core/src/executions/se.rs | 46 ++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/node_core/src/executions/se.rs b/node_core/src/executions/se.rs index 17c0d5b..0eaaab7 100644 --- a/node_core/src/executions/se.rs +++ b/node_core/src/executions/se.rs @@ -67,3 +67,49 @@ pub fn generate_commitments(output_utxos: &[UTXO]) -> Vec> { .collect() } +// 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 +// returns False, as membership proof. +pub fn validate_in_commitments_proof( + pedersen_commitment: &PedersenCommitment, + root_commitment: Vec, + in_commitments_proof: &[Vec], +) -> bool { + let mut nsmt = CommitmentsSparseMerkleTree { + curr_root: Option::Some(root_commitment), + tree: Monotree::default(), + hasher: Blake3::new(), + }; + + 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() +} + +// 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 +// returns False, as non-membership proof. +pub fn validate_nullifiers_proof( + nullifier: [u8; 32], + root_nullifier: [u8; 32], + nullifiers_proof: &[[u8; 32]], +) -> bool { + let mut nsmt = NullifierSparseMerkleTree { + curr_root: Option::Some(root_nullifier), + tree: Monotree::default(), + hasher: Blake3::new(), + }; + + 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() +}