Skip to content

Commit

Permalink
use Vec<Vec<u8>> with inner recursion method
Browse files Browse the repository at this point in the history
  • Loading branch information
liamsi committed Nov 9, 2019
1 parent e34f1b6 commit 315c026
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions tendermint/src/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ pub type Hash = [u8; HASH_SIZE];

/// Compute a simple Merkle root from the arbitrary byte arrays
pub fn simple_hash_from_byte_slices(byte_slices: Vec<Vec<u8>>) -> Hash {
simple_hash_from_byte_slices_inner(byte_slices.as_slice())
}

// recurse into subtrees
fn simple_hash_from_byte_slices_inner(byte_slices: &[Vec<u8>]) -> Hash {
let length = byte_slices.len();
match length {
0 => [0; HASH_SIZE],
1 => leaf_hash(byte_slices[0].to_vec()),
1 => leaf_hash(byte_slices[0].as_slice()),
_ => {
let k = get_split_point(length);
let left = simple_hash_from_byte_slices(byte_slices[0..k].to_vec());
let right = simple_hash_from_byte_slices(byte_slices[k..].to_vec());
let left = simple_hash_from_byte_slices_inner(&byte_slices[0..k]);
let right = simple_hash_from_byte_slices_inner(&byte_slices[k..]);
inner_hash(&left, &right)
}
}
Expand All @@ -34,11 +39,11 @@ fn get_split_point(length: usize) -> usize {
}

// tmhash(0x00 || leaf)
fn leaf_hash(bytes: Vec<u8>) -> Hash {
fn leaf_hash(bytes: &[u8]) -> Hash {
// make a new array starting with 0 and copy in the bytes
let mut leaf_bytes = Vec::with_capacity(bytes.len() + 1);
leaf_bytes.push(0x00);
leaf_bytes.extend_from_slice(&bytes);
leaf_bytes.extend_from_slice(bytes);

// hash it !
let digest = Sha256::digest(&leaf_bytes);
Expand Down

0 comments on commit 315c026

Please sign in to comment.