From 7bdb59e386b93c37cdc2c2b88fc2945ab0e64d57 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Wed, 25 Jun 2025 10:18:23 +0200 Subject: Increase tree's speed avoiding to store data in a vec --- src/merkletree.rs | 9 ++------- src/node.rs | 16 +++------------- src/proof.rs | 7 +------ 3 files changed, 6 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/merkletree.rs b/src/merkletree.rs index 362077e..c8962f4 100644 --- a/src/merkletree.rs +++ b/src/merkletree.rs @@ -49,7 +49,7 @@ impl MerkleTree { let mut leaves: Vec = data_slices .iter() - .map(|data| Node::new_leaf(data, hasher.hash(data))) + .map(|data| Node::new_leaf(hasher.hash(data))) .collect(); if leaves.len() % 2 != 0 { @@ -87,12 +87,7 @@ impl MerkleTree { buffer.extend_from_slice(right_hash); let hash = hasher.hash(&buffer); - next_level.push(Node::new_internal( - &buffer, - hash, - left.clone(), - right.clone(), - )); + next_level.push(Node::new_internal(hash, left.clone(), right.clone())); } std::mem::swap(&mut current_level, &mut next_level); diff --git a/src/node.rs b/src/node.rs index e7e6b0f..e784a69 100644 --- a/src/node.rs +++ b/src/node.rs @@ -43,8 +43,6 @@ pub struct Node { hash: String, /// Type of the node: leaf or internal. status: NodeStatus, - /// Data in bytes. - data: Vec, } impl Node { @@ -53,11 +51,9 @@ impl Node { /// # Arguments /// /// * `hasher` - A reference to a hashing strategy. - /// * `data` - The data to be hashed and stored as a leaf. - pub fn new_leaf(data: &[u8], hash: String) -> Self { + pub fn new_leaf(hash: String) -> Self { Self { hash, - data: data.to_vec(), status: NodeStatus::Leaf, } } @@ -66,17 +62,16 @@ impl Node { /// /// # Arguments /// - /// * `hasher` - A reference to a hashing strategy. + /// * `hash` - An hash value for the following node. /// * `left` - Left child node. /// * `right` - Right child node. /// /// # Behavior /// /// The internal node hash is computed as the hash of the concatenated children's hashes. - pub fn new_internal(data: &[u8], hash: String, left: Node, right: Node) -> Self { + pub fn new_internal(hash: String, left: Node, right: Node) -> Self { Self { hash, - data: data.to_vec(), status: NodeStatus::Internal(Box::new(left), Box::new(right)), } } @@ -86,11 +81,6 @@ impl Node { &self.hash } - /// Returns the data value in bytes format. - pub fn data(&self) -> &[u8] { - &self.data - } - /// Returns a reference to the node's type (leaf or internal). pub fn status(&self) -> &NodeStatus { &self.status diff --git a/src/proof.rs b/src/proof.rs index 4c42841..c893b08 100644 --- a/src/proof.rs +++ b/src/proof.rs @@ -110,12 +110,7 @@ impl Proofer for DefaultProofer<'_> { buffer.extend_from_slice(right_hash); let hash = self.hasher.hash(&buffer); - next_level.push(Node::new_internal( - &buffer, - hash, - left.clone(), - right.clone(), - )); + next_level.push(Node::new_internal(hash, left.clone(), right.clone())); } current_level = next_level; current_index /= 2; -- cgit v1.2.3-71-g8e6c