From 2f99d7375809616345f44832c0006b737f23f519 Mon Sep 17 00:00:00 2001 From: Rostyslav Tyshko Date: Mon, 2 Dec 2024 01:14:31 +0100 Subject: [PATCH] add generate_commitments and generate_nullifiers to de module --- node_core/src/executions/de.rs | 67 ++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 node_core/src/executions/de.rs diff --git a/node_core/src/executions/de.rs b/node_core/src/executions/de.rs new file mode 100644 index 0000000..268f0fa --- /dev/null +++ b/node_core/src/executions/de.rs @@ -0,0 +1,67 @@ +use k256::Scalar; +use secp256k1_zkp::{compute_adaptive_blinding_factor, verify_commitments_sum_to_equal, CommitmentSecrets, Generator, PedersenCommitment, Tag, Tweak, SECP256K1}; +use rand::thread_rng; +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 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 { + value, + value_blinding_factor: Tweak::new(&mut thread_rng()), + generator_blinding_factor: Tweak::new(&mut thread_rng()), + } +} + +pub fn tag_random() -> Tag { + use rand::thread_rng; + use rand::RngCore; + + let mut bytes = [0u8; 32]; + thread_rng().fill_bytes(&mut bytes); + + Tag::from(bytes) +} + +pub fn commit(comm: &CommitmentSecrets, tag: Tag) -> PedersenCommitment { + let generator = Generator::new_blinded(SECP256K1, tag, comm.generator_blinding_factor); + + PedersenCommitment::new(SECP256K1, comm.value, comm.value_blinding_factor, generator) +} + +// Hash function placeholder (replace with your cryptographic library's hash). +fn hash(input: &[u8]) -> Vec { + Sha256::digest(input).to_vec() +} + +// Generate nullifiers + +// 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(); + input.extend_from_slice(nsk); + hash(&input) +} + +// 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 +pub fn generate_commitments(input_utxos: &[UTXO]) -> Vec> { + input_utxos + .iter() + .map(|utxo| { + let serialized = bincode::serialize(utxo).unwrap(); // Serialize UTXO. + hash(&serialized) + }) + .collect() +} +