mirror of
https://github.com/tlsnotary/rs-merkle.git
synced 2026-01-08 22:38:07 -05:00
feat: replaced all vector references with slices
This commit is contained in:
@@ -8,7 +8,7 @@ pub struct Sha256Algorithm {}
|
||||
impl Hasher for Sha256Algorithm {
|
||||
type Hash = [u8; 32];
|
||||
|
||||
fn hash(data: &Vec<u8>) -> [u8; 32] {
|
||||
fn hash(data: &[u8]) -> [u8; 32] {
|
||||
let mut hasher = Sha256::new();
|
||||
|
||||
hasher.update(data);
|
||||
|
||||
@@ -17,7 +17,7 @@ use std::mem;
|
||||
/// impl Hasher for Sha256Algorithm {
|
||||
/// type Hash = [u8; 32];
|
||||
///
|
||||
/// fn hash(data: &Vec<u8>) -> [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<u8>) -> 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
|
||||
|
||||
@@ -64,7 +64,7 @@ impl<T: Hasher> MerkleProof<T> {
|
||||
}
|
||||
|
||||
/// Returns
|
||||
pub fn proof_hashes(&self) -> &Vec<T::Hash> {
|
||||
pub fn proof_hashes(&self) -> &[T::Hash] {
|
||||
&self.proof_hashes
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ impl<T: Hasher> MerkleProof<T> {
|
||||
}
|
||||
|
||||
/// Calculates merkle root based on provided leaves and proof hashes
|
||||
pub fn root(&self, leaf_indices: &Vec<usize>, leaf_hashes: &Vec<T::Hash>, 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<T: Hasher> MerkleProof<T> {
|
||||
}
|
||||
|
||||
/// Calculates the root and serializes it into a hex string
|
||||
pub fn hex_root(&self, leaf_indices: &Vec<usize>, leaf_hashes: &Vec<T::Hash>, 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<usize>, leaf_hashes: &Vec<T::Hash>, 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
|
||||
}
|
||||
|
||||
@@ -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<T: Hasher> MerkleTree<T> {
|
||||
}
|
||||
|
||||
/// Clones leave hashes and build the tree from them
|
||||
pub fn from_leaves(leaves: &Vec<T::Hash>) -> 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<T: Hasher> MerkleTree<T> {
|
||||
|
||||
/// 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<usize>) -> Vec<T::Hash> {
|
||||
fn helper_nodes(&self, leaf_indices: &[usize]) -> Vec<T::Hash> {
|
||||
let mut helper_nodes = Vec::<T::Hash>::new();
|
||||
|
||||
for layer in self.helper_node_tuples(leaf_indices) {
|
||||
@@ -161,7 +161,7 @@ impl<T: Hasher> MerkleTree<T> {
|
||||
|
||||
/// 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<usize>) -> Vec<Vec<(usize, T::Hash)>> {
|
||||
fn helper_node_tuples(&self, leaf_indices: &[usize]) -> Vec<Vec<(usize, T::Hash)>> {
|
||||
let mut current_layer_indices = leaf_indices.to_vec();
|
||||
let mut helper_nodes: Vec<Vec<(usize, T::Hash)>> = Vec::new();
|
||||
|
||||
@@ -190,7 +190,7 @@ impl<T: Hasher> MerkleTree<T> {
|
||||
}
|
||||
|
||||
/// Returns merkle proof required to prove inclusion of items at given indices
|
||||
pub fn proof(&self, leaf_indices: &Vec<usize>) -> MerkleProof<T> {
|
||||
pub fn proof(&self, leaf_indices: &[usize]) -> MerkleProof<T> {
|
||||
MerkleProof::<T>::new(self.helper_nodes(leaf_indices))
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ impl<T: Hasher> PartialTree<T> {
|
||||
|
||||
/// 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<T::Hash>) -> Result<Self, Error> {
|
||||
pub fn from_leaves(leaves: &[T::Hash]) -> Result<Self, Error> {
|
||||
let leaf_tuples: Vec<(usize, T::Hash)> = leaves.iter().cloned().enumerate().collect();
|
||||
|
||||
Self::build(
|
||||
@@ -162,7 +162,7 @@ impl<T: Hasher> PartialTree<T> {
|
||||
}
|
||||
|
||||
/// Returns partial tree layers
|
||||
pub fn layers(&self) -> &Vec<Vec<(usize, T::Hash)>> {
|
||||
pub fn layers(&self) -> &[Vec<(usize, T::Hash)>] {
|
||||
&self.layers
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,6 @@ pub fn to_hex_string<T: Clone + Into<Vec<u8>>>(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<T: Clone + PartialEq>(a: &Vec<T>, b: &Vec<T>) -> Vec<T> {
|
||||
pub fn difference<T: Clone + PartialEq>(a: &[T], b: &[T]) -> Vec<T> {
|
||||
a.iter().cloned().filter(|x| !b.contains(x)).collect()
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ pub fn get_sibling_index(index: usize) -> usize {
|
||||
index - 1
|
||||
}
|
||||
|
||||
pub fn sibling_indices(indices: &Vec<usize>) -> Vec<usize> {
|
||||
pub fn sibling_indices(indices: &[usize]) -> Vec<usize> {
|
||||
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<usize>) -> Vec<usize> {
|
||||
pub fn parent_indices(indices: &[usize]) -> Vec<usize> {
|
||||
let mut parents: Vec<usize> = indices.iter().cloned().map(parent_index).collect();
|
||||
parents.dedup();
|
||||
parents
|
||||
@@ -66,7 +66,7 @@ pub fn uneven_layers(tree_leaves_count: usize) -> HashMap<usize, usize> {
|
||||
}
|
||||
|
||||
/// Returns layered proof indices
|
||||
pub fn proof_indices_by_layers(sorted_leaf_indices: &Vec<usize>, leaves_count: usize) -> Vec<Vec<usize>> {
|
||||
pub fn proof_indices_by_layers(sorted_leaf_indices: &[usize], leaves_count: usize) -> Vec<Vec<usize>> {
|
||||
let depth = tree_depth(leaves_count);
|
||||
let uneven_layers = uneven_layers(leaves_count);
|
||||
|
||||
|
||||
@@ -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::<Sha256>::from_leaves(&test_data.leaf_hashes);
|
||||
let proof = merkle_tree.proof(&indices_to_prove);
|
||||
|
||||
Reference in New Issue
Block a user