From 9fbacbf9670b9346c668e8acec716da769119629 Mon Sep 17 00:00:00 2001 From: Johns Beharry Date: Wed, 27 Feb 2019 04:51:23 -0400 Subject: [PATCH] chore(merkle_root): move function into lib.rs Signed-off-by: Johns Beharry --- eth2/utils/hashing/src/lib.rs | 59 +++++++++++++++++++++++++++ eth2/utils/hashing/src/merkle_root.rs | 41 ------------------- 2 files changed, 59 insertions(+), 41 deletions(-) delete mode 100644 eth2/utils/hashing/src/merkle_root.rs diff --git a/eth2/utils/hashing/src/lib.rs b/eth2/utils/hashing/src/lib.rs index b2bd5a279..33c31ab19 100644 --- a/eth2/utils/hashing/src/lib.rs +++ b/eth2/utils/hashing/src/lib.rs @@ -8,6 +8,33 @@ pub fn hash(input: &[u8]) -> Vec { result } +/// Generate Merkle Root +/// +/// Outputs a `Vec` byte array of the merkle root given a set of leaf node values. +/// Expects leaf nodes to already be hashed. +pub fn merkle_root(values: &[Vec]) -> Vec { + let values_len = values.len(); + + // vector to store hashes + // filled with 0 as placeholders + let mut o: Vec> = vec![vec![0]; values_len]; + + // append values to the end + o.append(&mut values.to_vec()); + + // traverse backwards as values are at the end + // then fill placeholders with a hash of two leaf nodes + for i in (0..values_len).rev() { + let mut current_value: Vec = o[i * 2].clone(); + current_value.append(&mut o[i * 2 + 1].clone()); + + o[i] = hash(¤t_value[..]); + } + + // the root hash will be at index 1 + o[1].clone() +} + #[cfg(test)] mod tests { use super::*; @@ -25,4 +52,36 @@ mod tests { ]; assert_eq!(expected, output.as_slice()); } + + #[test] + fn test_merkle_root() { + // hash the leaf nodes + let mut input = vec![ + hash("a".as_bytes()), + hash("b".as_bytes()), + hash("c".as_bytes()), + hash("d".as_bytes()), + ]; + + // generate a merkle tree and return the root + let output = merkle_root(&input[..]); + + // create merkle root manually + let mut leaf_1_2: Vec = input[0].clone(); // a + leaf_1_2.append(&mut input[1].clone()); // b + + let mut leaf_3_4: Vec = input[2].clone(); // c + leaf_3_4.append(&mut input[3].clone()); // d + + let node_1 = hash(&leaf_1_2[..]); + let node_2 = hash(&leaf_3_4[..]); + + let mut root: Vec = node_1.clone(); // ab + root.append(&mut node_2.clone()); // cd + + let expected = hash(&root[..]); + + assert_eq!(&expected[..], output.as_slice()); + + } } diff --git a/eth2/utils/hashing/src/merkle_root.rs b/eth2/utils/hashing/src/merkle_root.rs deleted file mode 100644 index 6c11d9d80..000000000 --- a/eth2/utils/hashing/src/merkle_root.rs +++ /dev/null @@ -1,41 +0,0 @@ -use crate::hash; - -pub fn merkle_root(values: &[Vec]) -> Vec { - let values_len = values.len(); - let mut o: Vec> = vec![vec![0]; values_len]; - - o.append(&mut values.to_vec()); - - for i in (0..values_len).rev() { - let mut current_value: Vec = o[i * 2].clone(); - current_value.append(&mut o[i * 2 + 1].clone()); - - o[i] = hash(¤t_value[..]); - } - - o[1].clone() -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_merkle_root() { - let input = vec![ - "a".as_bytes().to_vec(), - "b".as_bytes().to_vec(), - "c".as_bytes().to_vec(), - "d".as_bytes().to_vec() - ]; - - let output = merkle_root(&input[..]); - - // merkle root of [[a],[b],[c],[d]] - let expected = &[ - 183, 91, 96, 122, 144, 174, 84, 92, 97, 156, 140, 192, 66, 221, 55, 229, - 234, 48, 118, 7, 61, 207, 39, 125, 150, 32, 94, 90, 19, 88, 122, 163, - ]; - assert_eq!(expected, output.as_slice()); - } -}