2019-02-14 01:09:18 +00:00
|
|
|
use hashing::hash;
|
|
|
|
|
2019-03-08 23:24:07 +00:00
|
|
|
const BYTES_PER_CHUNK: usize = 32;
|
2019-02-14 01:09:18 +00:00
|
|
|
const HASHSIZE: usize = 32;
|
|
|
|
|
|
|
|
pub trait TreeHash {
|
2019-03-08 23:24:07 +00:00
|
|
|
fn hash_tree_root(&self) -> Vec<u8>;
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a 32 byte hash of 'list' - a vector of byte vectors.
|
|
|
|
/// Note that this will consume 'list'.
|
|
|
|
pub fn merkle_hash(list: &mut Vec<Vec<u8>>) -> Vec<u8> {
|
|
|
|
// flatten list
|
2019-03-08 23:24:07 +00:00
|
|
|
let mut chunkz = list_to_blob(list);
|
2019-02-14 01:09:18 +00:00
|
|
|
|
|
|
|
// get data_len as bytes. It will hashed will the merkle root
|
2019-02-17 17:30:18 +00:00
|
|
|
let mut datalen = list.len().to_le_bytes().to_vec();
|
|
|
|
zpad(&mut datalen, 32);
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-03-08 23:24:07 +00:00
|
|
|
// merklelize
|
2019-02-14 01:09:18 +00:00
|
|
|
while chunkz.len() > HASHSIZE {
|
|
|
|
let mut new_chunkz: Vec<u8> = Vec::new();
|
|
|
|
|
2019-03-08 23:24:07 +00:00
|
|
|
for two_chunks in chunkz.chunks(BYTES_PER_CHUNK * 2) {
|
|
|
|
// Hash two chuncks together
|
|
|
|
new_chunkz.append(&mut hash(two_chunks));
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
chunkz = new_chunkz;
|
|
|
|
}
|
|
|
|
|
2019-02-17 17:30:18 +00:00
|
|
|
chunkz.append(&mut datalen);
|
2019-02-14 01:09:18 +00:00
|
|
|
hash(&chunkz)
|
|
|
|
}
|
|
|
|
|
2019-03-08 23:24:07 +00:00
|
|
|
fn list_to_blob(list: &mut Vec<Vec<u8>>) -> Vec<u8> {
|
|
|
|
// pack - fit as many many items per chunk as we can and then
|
|
|
|
// right pad to BYTES_PER_CHUNCK
|
2019-02-18 19:55:44 +00:00
|
|
|
let (items_per_chunk, chunk_count) = if list.is_empty() {
|
|
|
|
(1, 1)
|
|
|
|
} else {
|
2019-03-08 23:24:07 +00:00
|
|
|
let items_per_chunk = BYTES_PER_CHUNK / list[0].len();
|
2019-02-18 19:55:44 +00:00
|
|
|
let chunk_count = list.len() / items_per_chunk;
|
|
|
|
(items_per_chunk, chunk_count)
|
|
|
|
};
|
2019-02-17 17:30:18 +00:00
|
|
|
|
|
|
|
let mut chunkz = Vec::new();
|
2019-02-14 01:09:18 +00:00
|
|
|
if list.is_empty() {
|
|
|
|
// handle and empty list
|
2019-03-08 23:24:07 +00:00
|
|
|
chunkz.append(&mut vec![0; BYTES_PER_CHUNK * 2]);
|
|
|
|
} else if list[0].len() <= BYTES_PER_CHUNK {
|
2019-02-14 01:09:18 +00:00
|
|
|
// just create a blob here; we'll divide into
|
|
|
|
// chunked slices when we merklize
|
2019-03-08 23:24:07 +00:00
|
|
|
let mut chunk = Vec::with_capacity(BYTES_PER_CHUNK);
|
2019-02-17 17:30:18 +00:00
|
|
|
let mut item_count_in_chunk = 0;
|
2019-03-08 23:24:07 +00:00
|
|
|
chunkz.reserve(chunk_count * BYTES_PER_CHUNK);
|
2019-02-14 01:09:18 +00:00
|
|
|
for item in list.iter_mut() {
|
2019-02-17 17:30:18 +00:00
|
|
|
item_count_in_chunk += 1;
|
|
|
|
chunk.append(item);
|
|
|
|
|
|
|
|
// completed chunk?
|
|
|
|
if item_count_in_chunk == items_per_chunk {
|
2019-03-08 23:24:07 +00:00
|
|
|
zpad(&mut chunk, BYTES_PER_CHUNK);
|
2019-02-17 17:30:18 +00:00
|
|
|
chunkz.append(&mut chunk);
|
|
|
|
item_count_in_chunk = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// left-over uncompleted chunk?
|
|
|
|
if item_count_in_chunk != 0 {
|
2019-03-08 23:24:07 +00:00
|
|
|
zpad(&mut chunk, BYTES_PER_CHUNK);
|
2019-02-17 17:30:18 +00:00
|
|
|
chunkz.append(&mut chunk);
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
2019-02-17 17:30:18 +00:00
|
|
|
}
|
|
|
|
|
2019-03-08 23:24:07 +00:00
|
|
|
// extend the number of chunks to a power of two if necessary
|
|
|
|
if !chunk_count.is_power_of_two() {
|
|
|
|
let zero_chunks_count = chunk_count.next_power_of_two() - chunk_count;
|
|
|
|
chunkz.append(&mut vec![0; zero_chunks_count * BYTES_PER_CHUNK]);
|
|
|
|
}
|
|
|
|
|
|
|
|
chunkz
|
2019-02-17 17:30:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// right pads with zeros making 'bytes' 'size' in length
|
|
|
|
fn zpad(bytes: &mut Vec<u8>, size: usize) {
|
|
|
|
if bytes.len() < size {
|
|
|
|
bytes.resize(size, 0);
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_merkle_hash() {
|
2019-03-08 23:24:07 +00:00
|
|
|
let data1 = vec![1; 32];
|
|
|
|
let data2 = vec![2; 32];
|
|
|
|
let data3 = vec![3; 32];
|
2019-02-14 01:09:18 +00:00
|
|
|
let mut list = vec![data1, data2, data3];
|
|
|
|
let result = merkle_hash(&mut list);
|
|
|
|
|
|
|
|
//note: should test againt a known test hash value
|
|
|
|
assert_eq!(HASHSIZE, result.len());
|
|
|
|
}
|
|
|
|
}
|