From 90ae6d5204df8614dcb90d4846743786dabc9434 Mon Sep 17 00:00:00 2001 From: Anton Suprunchuk Date: Thu, 7 Oct 2021 11:57:42 +0300 Subject: [PATCH] feat: replaced all vector references with slices --- src/algorithms/sha256.rs | 2 +- src/hasher.rs | 4 ++-- src/merkle_proof.rs | 8 ++++---- src/merkle_tree.rs | 12 ++++++------ src/partial_tree.rs | 4 ++-- src/utils/collections.rs | 2 +- src/utils/indices.rs | 6 +++--- tests/merkle_proof_test.rs | 2 +- 8 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/algorithms/sha256.rs b/src/algorithms/sha256.rs index b3befff..7a7c9b9 100644 --- a/src/algorithms/sha256.rs +++ b/src/algorithms/sha256.rs @@ -8,7 +8,7 @@ pub struct Sha256Algorithm {} impl Hasher for Sha256Algorithm { type Hash = [u8; 32]; - fn hash(data: &Vec) -> [u8; 32] { + fn hash(data: &[u8]) -> [u8; 32] { let mut hasher = Sha256::new(); hasher.update(data); diff --git a/src/hasher.rs b/src/hasher.rs index 7c0df1f..2d52664 100644 --- a/src/hasher.rs +++ b/src/hasher.rs @@ -17,7 +17,7 @@ use std::mem; /// impl Hasher for Sha256Algorithm { /// type Hash = [u8; 32]; /// -/// fn hash(data: &Vec) -> [u8; 32] { +/// fn hash(data: &[u8]) -> [u8; 32] { /// let mut hasher = Sha256::new(); /// /// hasher.update(data); @@ -42,7 +42,7 @@ pub trait Hasher: Clone { /// This associated function takes arbitrary bytes and returns hash of it. /// Used by `concat_and_hash` function to build a tree from concatenated hashes - fn hash(data: &Vec) -> Self::Hash; + fn hash(data: &[u8]) -> Self::Hash; /// Used by `MerkleTree` and `MerkleProof` when calculating the root. /// The provided default implementation follows propagates left node if it doesn't diff --git a/src/merkle_proof.rs b/src/merkle_proof.rs index 15032f7..d90648e 100644 --- a/src/merkle_proof.rs +++ b/src/merkle_proof.rs @@ -64,7 +64,7 @@ impl MerkleProof { } /// Returns - pub fn proof_hashes(&self) -> &Vec { + pub fn proof_hashes(&self) -> &[T::Hash] { &self.proof_hashes } @@ -76,7 +76,7 @@ impl MerkleProof { } /// Calculates merkle root based on provided leaves and proof hashes - pub fn root(&self, leaf_indices: &Vec, leaf_hashes: &Vec, total_leaves_count: usize) -> T::Hash { + pub fn root(&self, leaf_indices: &[usize], leaf_hashes: &[T::Hash], total_leaves_count: usize) -> T::Hash { let tree_depth = utils::indices::tree_depth(total_leaves_count); // Zipping indices and hashes into a vector of (original_index_in_tree, leaf_hash) @@ -109,13 +109,13 @@ impl MerkleProof { } /// Calculates the root and serializes it into a hex string - pub fn hex_root(&self, leaf_indices: &Vec, leaf_hashes: &Vec, total_leaves_count: usize) -> String { + pub fn hex_root(&self, leaf_indices: &[usize], leaf_hashes: &[T::Hash], total_leaves_count: usize) -> String { let root = self.root(leaf_indices, leaf_hashes, total_leaves_count); utils::collections::to_hex_string(&root) } /// Verifies - pub fn verify(&self, root: T::Hash, leaf_indices: &Vec, leaf_hashes: &Vec, total_leaves_count: usize) -> bool { + pub fn verify(&self, root: T::Hash, leaf_indices: &[usize], leaf_hashes: &[T::Hash], total_leaves_count: usize) -> bool { let extracted_root = self.root(leaf_indices, leaf_hashes, total_leaves_count); root == extracted_root } diff --git a/src/merkle_tree.rs b/src/merkle_tree.rs index 820a43b..390a078 100644 --- a/src/merkle_tree.rs +++ b/src/merkle_tree.rs @@ -20,7 +20,7 @@ use crate::partial_tree::PartialTree; /// /// let leaf_values = ["a", "b", "c", "d", "e", "f"]; /// let expected_root_hex = "1f7379539707bcaea00564168d1d4d626b09b73f8a2a365234c62d763f854da2"; -/// let leaves = leaf_values +/// let leaves: Vec<[u8; 32]> = leaf_values /// .iter() /// .map(|x| Sha256::hash(x.as_bytes().to_vec().as_ref())) /// .collect(); @@ -124,10 +124,10 @@ impl MerkleTree { } /// Clones leave hashes and build the tree from them - pub fn from_leaves(leaves: &Vec) -> Self { + pub fn from_leaves(leaves: &[T::Hash]) -> Self { let mut tree = Self::new(); - tree.append(leaves.clone().as_mut()); + tree.append(leaves.to_vec().as_mut()); tree.commit(); tree @@ -147,7 +147,7 @@ impl MerkleTree { /// Returns helper nodes required to build a partial tree for the given indices /// to be able to extract a root from it. Useful in constructing merkle proofs - fn helper_nodes(&self, leaf_indices: &Vec) -> Vec { + fn helper_nodes(&self, leaf_indices: &[usize]) -> Vec { let mut helper_nodes = Vec::::new(); for layer in self.helper_node_tuples(leaf_indices) { @@ -161,7 +161,7 @@ impl MerkleTree { /// Gets all helper nodes required to build a partial merkle tree for the given indices, /// cloning all required hashes into the resulting vector. - fn helper_node_tuples(&self, leaf_indices: &Vec) -> Vec> { + fn helper_node_tuples(&self, leaf_indices: &[usize]) -> Vec> { let mut current_layer_indices = leaf_indices.to_vec(); let mut helper_nodes: Vec> = Vec::new(); @@ -190,7 +190,7 @@ impl MerkleTree { } /// Returns merkle proof required to prove inclusion of items at given indices - pub fn proof(&self, leaf_indices: &Vec) -> MerkleProof { + pub fn proof(&self, leaf_indices: &[usize]) -> MerkleProof { MerkleProof::::new(self.helper_nodes(leaf_indices)) } diff --git a/src/partial_tree.rs b/src/partial_tree.rs index e9168c0..a155526 100644 --- a/src/partial_tree.rs +++ b/src/partial_tree.rs @@ -18,7 +18,7 @@ impl PartialTree { /// This is a helper function to build a full tree from a full set of leaves without any /// helper indices - pub fn from_leaves(leaves: &Vec) -> Result { + pub fn from_leaves(leaves: &[T::Hash]) -> Result { let leaf_tuples: Vec<(usize, T::Hash)> = leaves.iter().cloned().enumerate().collect(); Self::build( @@ -162,7 +162,7 @@ impl PartialTree { } /// Returns partial tree layers - pub fn layers(&self) -> &Vec> { + pub fn layers(&self) -> &[Vec<(usize, T::Hash)>] { &self.layers } diff --git a/src/utils/collections.rs b/src/utils/collections.rs index 71962bb..942dc7c 100644 --- a/src/utils/collections.rs +++ b/src/utils/collections.rs @@ -15,6 +15,6 @@ pub fn to_hex_string>>(bytes: &T) -> String { /// Find a difference between two vectors and return a third vector /// containing the difference. This function preserves the first /// vector order. -pub fn difference(a: &Vec, b: &Vec) -> Vec { +pub fn difference(a: &[T], b: &[T]) -> Vec { a.iter().cloned().filter(|x| !b.contains(x)).collect() } diff --git a/src/utils/indices.rs b/src/utils/indices.rs index 5508ddb..b2580aa 100644 --- a/src/utils/indices.rs +++ b/src/utils/indices.rs @@ -19,7 +19,7 @@ pub fn get_sibling_index(index: usize) -> usize { index - 1 } -pub fn sibling_indices(indices: &Vec) -> Vec { +pub fn sibling_indices(indices: &[usize]) -> Vec { indices.iter().cloned().map(get_sibling_index).collect() } @@ -30,7 +30,7 @@ pub fn parent_index(index: usize) -> usize { return get_sibling_index(index) / 2; } -pub fn parent_indices(indices: &Vec) -> Vec { +pub fn parent_indices(indices: &[usize]) -> Vec { let mut parents: Vec = indices.iter().cloned().map(parent_index).collect(); parents.dedup(); parents @@ -66,7 +66,7 @@ pub fn uneven_layers(tree_leaves_count: usize) -> HashMap { } /// Returns layered proof indices -pub fn proof_indices_by_layers(sorted_leaf_indices: &Vec, leaves_count: usize) -> Vec> { +pub fn proof_indices_by_layers(sorted_leaf_indices: &[usize], leaves_count: usize) -> Vec> { let depth = tree_depth(leaves_count); let uneven_layers = uneven_layers(leaves_count); diff --git a/tests/merkle_proof_test.rs b/tests/merkle_proof_test.rs index 3a15129..36b121e 100644 --- a/tests/merkle_proof_test.rs +++ b/tests/merkle_proof_test.rs @@ -12,7 +12,7 @@ pub mod root { let expected_root = test_data.expected_root_hex.clone(); let leaf_hashes = &test_data.leaf_hashes; let indices_to_prove = vec![3, 4]; - let leaves_to_prove = indices_to_prove.iter().cloned().map(|i| leaf_hashes.get(i).unwrap().clone()).collect(); + let leaves_to_prove: Vec<[u8; 32]> = indices_to_prove.iter().cloned().map(|i| leaf_hashes.get(i).unwrap().clone()).collect(); let merkle_tree = MerkleTree::::from_leaves(&test_data.leaf_hashes); let proof = merkle_tree.proof(&indices_to_prove);