From 4e122abbcefc2f0aecc00652d957a95b1bcfdc96 Mon Sep 17 00:00:00 2001 From: jonesmarvin8 <83104039+jonesmarvin8@users.noreply.github.com> Date: Fri, 5 Sep 2025 16:03:49 -0400 Subject: [PATCH 1/6] added comments for Merkle tree --- nssa/src/merkle_tree/mod.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/nssa/src/merkle_tree/mod.rs b/nssa/src/merkle_tree/mod.rs index 2306efd..a27501c 100644 --- a/nssa/src/merkle_tree/mod.rs +++ b/nssa/src/merkle_tree/mod.rs @@ -39,11 +39,11 @@ impl MerkleTree { if tree_depth == capacity_depth { 0 } else { + // 2^(capacity_depth - tree_depth) - 1 (1 << (capacity_depth - tree_depth)) - 1 } } - - /// Number of levels required to hold all values + /// Number of levels required to hold all nodes fn depth(&self) -> usize { self.length.next_power_of_two().trailing_zeros() as usize } @@ -57,6 +57,7 @@ impl MerkleTree { } pub fn with_capacity(capacity: usize) -> Self { + //adjust capacity to ensure power of two let capacity = capacity.next_power_of_two(); let total_depth = capacity.trailing_zeros() as usize; @@ -75,6 +76,8 @@ impl MerkleTree { } } + /// reallocate storage of Merkle tree for double capacity. + /// current tree is embedded into the new tree as a subtree fn reallocate_to_double_capacity(&mut self) { let old_capacity = self.capacity; let new_capacity = old_capacity << 1; @@ -102,9 +105,11 @@ impl MerkleTree { let mut node_index = new_index + self.capacity - 1; let mut node_hash = hash_value(&value); + //add new node to nodes self.set_node(node_index, node_hash); self.length += 1; + //updates Merkle path of the newly inserted node for _ in 0..self.depth() { let parent_index = (node_index - 1) >> 1; let left_child = self.get_node((parent_index << 1) + 1); @@ -129,6 +134,7 @@ impl MerkleTree { while node_index != root_index { let parent_index = (node_index - 1) >> 1; + //left children have odd indices, and right children have even indices let is_left_child = node_index & 1 == 1; let sibling_index = if is_left_child { node_index + 1 From 45b31d304c114660513415db69a4f89fcddf81a7 Mon Sep 17 00:00:00 2001 From: jonesmarvin8 <83104039+jonesmarvin8@users.noreply.github.com> Date: Fri, 5 Sep 2025 16:26:56 -0400 Subject: [PATCH 2/6] Update nssa/src/merkle_tree/mod.rs Co-authored-by: Sergio Chouhy <41742639+schouhy@users.noreply.github.com> --- nssa/src/merkle_tree/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nssa/src/merkle_tree/mod.rs b/nssa/src/merkle_tree/mod.rs index a27501c..ebc13e0 100644 --- a/nssa/src/merkle_tree/mod.rs +++ b/nssa/src/merkle_tree/mod.rs @@ -57,7 +57,7 @@ impl MerkleTree { } pub fn with_capacity(capacity: usize) -> Self { - //adjust capacity to ensure power of two + // Adjust capacity to ensure power of two let capacity = capacity.next_power_of_two(); let total_depth = capacity.trailing_zeros() as usize; From 6e7520bfeacfd0c5a9a98b0dd48db788d31214d0 Mon Sep 17 00:00:00 2001 From: jonesmarvin8 <83104039+jonesmarvin8@users.noreply.github.com> Date: Fri, 5 Sep 2025 16:27:03 -0400 Subject: [PATCH 3/6] Update nssa/src/merkle_tree/mod.rs Co-authored-by: Sergio Chouhy <41742639+schouhy@users.noreply.github.com> --- nssa/src/merkle_tree/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nssa/src/merkle_tree/mod.rs b/nssa/src/merkle_tree/mod.rs index ebc13e0..5944525 100644 --- a/nssa/src/merkle_tree/mod.rs +++ b/nssa/src/merkle_tree/mod.rs @@ -76,8 +76,8 @@ impl MerkleTree { } } - /// reallocate storage of Merkle tree for double capacity. - /// current tree is embedded into the new tree as a subtree + /// Reallocates storage of Merkle tree for double capacity. + /// The current tree is embedded into the new tree as a subtree fn reallocate_to_double_capacity(&mut self) { let old_capacity = self.capacity; let new_capacity = old_capacity << 1; From 73c11600ba2c26e6c5d1d1231cf5683f6a64d232 Mon Sep 17 00:00:00 2001 From: jonesmarvin8 <83104039+jonesmarvin8@users.noreply.github.com> Date: Fri, 5 Sep 2025 16:27:43 -0400 Subject: [PATCH 4/6] Update nssa/src/merkle_tree/mod.rs Co-authored-by: Sergio Chouhy <41742639+schouhy@users.noreply.github.com> --- nssa/src/merkle_tree/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nssa/src/merkle_tree/mod.rs b/nssa/src/merkle_tree/mod.rs index 5944525..8ce8560 100644 --- a/nssa/src/merkle_tree/mod.rs +++ b/nssa/src/merkle_tree/mod.rs @@ -105,7 +105,7 @@ impl MerkleTree { let mut node_index = new_index + self.capacity - 1; let mut node_hash = hash_value(&value); - //add new node to nodes + // Insert the new node at the bottom layer self.set_node(node_index, node_hash); self.length += 1; From 1e7c3e155501af8b9f6fb58143a4f09ff18b605a Mon Sep 17 00:00:00 2001 From: jonesmarvin8 <83104039+jonesmarvin8@users.noreply.github.com> Date: Fri, 5 Sep 2025 16:27:55 -0400 Subject: [PATCH 5/6] Update nssa/src/merkle_tree/mod.rs Co-authored-by: Sergio Chouhy <41742639+schouhy@users.noreply.github.com> --- nssa/src/merkle_tree/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nssa/src/merkle_tree/mod.rs b/nssa/src/merkle_tree/mod.rs index 8ce8560..0caff55 100644 --- a/nssa/src/merkle_tree/mod.rs +++ b/nssa/src/merkle_tree/mod.rs @@ -134,7 +134,7 @@ impl MerkleTree { while node_index != root_index { let parent_index = (node_index - 1) >> 1; - //left children have odd indices, and right children have even indices + // Left children have odd indices, and right children have even indices let is_left_child = node_index & 1 == 1; let sibling_index = if is_left_child { node_index + 1 From 26acdbed6169a3a2b8a9cd4e8754f4b42b9ab56a Mon Sep 17 00:00:00 2001 From: jonesmarvin8 <83104039+jonesmarvin8@users.noreply.github.com> Date: Fri, 5 Sep 2025 16:28:13 -0400 Subject: [PATCH 6/6] Update nssa/src/merkle_tree/mod.rs Co-authored-by: Sergio Chouhy <41742639+schouhy@users.noreply.github.com> --- nssa/src/merkle_tree/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nssa/src/merkle_tree/mod.rs b/nssa/src/merkle_tree/mod.rs index 0caff55..7b30d78 100644 --- a/nssa/src/merkle_tree/mod.rs +++ b/nssa/src/merkle_tree/mod.rs @@ -109,7 +109,7 @@ impl MerkleTree { self.set_node(node_index, node_hash); self.length += 1; - //updates Merkle path of the newly inserted node + // Update upper levels for the newly inserted node for _ in 0..self.depth() { let parent_index = (node_index - 1) >> 1; let left_child = self.get_node((parent_index << 1) + 1);