Merge branch 'master' into kirk-migrate-ssz-to-little-endian

Signed-off-by: Kirk Baird <baird.k@outlook.com>
This commit is contained in:
Kirk Baird
2019-03-18 11:40:18 +11:00
127 changed files with 4704 additions and 1843 deletions

4
eth2/utils/ssz/fuzz/.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
target
corpus
artifacts

View File

@@ -0,0 +1,105 @@
[package]
name = "ssz-fuzz"
version = "0.0.1"
authors = ["Automatically generated"]
publish = false
[package.metadata]
cargo-fuzz = true
[dependencies]
ethereum-types = "0.4.0"
[dependencies.ssz]
path = ".."
[dependencies.libfuzzer-sys]
git = "https://github.com/rust-fuzz/libfuzzer-sys.git"
# Prevent this from interfering with workspaces
[workspace]
members = ["."]
[[bin]]
name = "fuzz_target_bool_decode"
path = "fuzz_targets/fuzz_target_bool_decode.rs"
[[bin]]
name = "fuzz_target_bool_encode"
path = "fuzz_targets/fuzz_target_bool_encode.rs"
[[bin]]
name = "fuzz_target_u8_decode"
path = "fuzz_targets/fuzz_target_u8_decode.rs"
[[bin]]
name = "fuzz_target_u8_encode"
path = "fuzz_targets/fuzz_target_u8_encode.rs"
[[bin]]
name = "fuzz_target_u16_decode"
path = "fuzz_targets/fuzz_target_u16_decode.rs"
[[bin]]
name = "fuzz_target_u16_encode"
path = "fuzz_targets/fuzz_target_u16_encode.rs"
[[bin]]
name = "fuzz_target_u32_decode"
path = "fuzz_targets/fuzz_target_u32_decode.rs"
[[bin]]
name = "fuzz_target_u32_encode"
path = "fuzz_targets/fuzz_target_u32_encode.rs"
[[bin]]
name = "fuzz_target_u64_decode"
path = "fuzz_targets/fuzz_target_u64_decode.rs"
[[bin]]
name = "fuzz_target_u64_encode"
path = "fuzz_targets/fuzz_target_u64_encode.rs"
[[bin]]
name = "fuzz_target_usize_decode"
path = "fuzz_targets/fuzz_target_usize_decode.rs"
[[bin]]
name = "fuzz_target_usize_encode"
path = "fuzz_targets/fuzz_target_usize_encode.rs"
[[bin]]
name = "fuzz_target_hash256_decode"
path = "fuzz_targets/fuzz_target_hash256_decode.rs"
[[bin]]
name = "fuzz_target_hash256_encode"
path = "fuzz_targets/fuzz_target_hash256_encode.rs"
[[bin]]
name = "fuzz_target_address_decode"
path = "fuzz_targets/fuzz_target_address_decode.rs"
[[bin]]
name = "fuzz_target_address_encode"
path = "fuzz_targets/fuzz_target_address_encode.rs"
[[bin]]
name = "fuzz_target_vec_decode"
path = "fuzz_targets/fuzz_target_vec_decode.rs"
[[bin]]
name = "fuzz_target_vec_address_decode"
path = "fuzz_targets/fuzz_target_vec_address_decode.rs"
[[bin]]
name = "fuzz_target_vec_u64_decode"
path = "fuzz_targets/fuzz_target_vec_u64_decode.rs"
[[bin]]
name = "fuzz_target_vec_bool_decode"
path = "fuzz_targets/fuzz_target_vec_bool_decode.rs"
[[bin]]
name = "fuzz_target_vec_encode"
path = "fuzz_targets/fuzz_target_vec_encode.rs"

View File

@@ -0,0 +1,21 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate ethereum_types;
extern crate ssz;
use ethereum_types::Address;
use ssz::{DecodeError, Decodable};
// Fuzz ssz_decode()
fuzz_target!(|data: &[u8]| {
let result: Result<(Address, usize), DecodeError> = Decodable::ssz_decode(data, 0);
if data.len() >= 20 {
// Should have valid result
let (address, index) = result.unwrap();
assert_eq!(index, 20);
assert_eq!(address, Address::from_slice(&data[..20]));
} else {
// Length of less than 32 should return error
assert_eq!(result, Err(DecodeError::TooShort));
}
});

View File

@@ -0,0 +1,20 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate ethereum_types;
extern crate ssz;
use ethereum_types::Address;
use ssz::SszStream;
// Fuzz ssz_encode (via ssz_append)
fuzz_target!(|data: &[u8]| {
let mut ssz = SszStream::new();
if data.len() >= 20 {
let hash = Address::from_slice(&data[..20]);
ssz.append(&hash);
let ssz = ssz.drain();
assert_eq!(data[..20], ssz[..20]);
assert_eq!(ssz.len(), 20);
}
});

View File

@@ -0,0 +1,28 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate ssz;
use ssz::{DecodeError, Decodable};
// Fuzz ssz_decode()
fuzz_target!(|data: &[u8]| {
let result: Result<(bool, usize), DecodeError> = Decodable::ssz_decode(data, 0);
if data.len() >= 1 {
// TODO: change to little endian bytes
// https://github.com/sigp/lighthouse/issues/215
if data[0] == u8::pow(2,7) {
let (val_bool, index) = result.unwrap();
assert!(val_bool);
assert_eq!(index, 1);
} else if data[0] == 0 {
let (val_bool, index) = result.unwrap();
assert!(!val_bool);
assert_eq!(index, 1);
} else {
assert_eq!(result, Err(DecodeError::Invalid));
}
} else {
// Length of 0 should return error
assert_eq!(result, Err(DecodeError::TooShort));
}
});

View File

@@ -0,0 +1,22 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate ssz;
use ssz::SszStream;
// Fuzz ssz_encode (via ssz_append)
fuzz_target!(|data: &[u8]| {
let mut ssz = SszStream::new();
let mut val_bool = 0;
if data.len() >= 1 {
val_bool = data[0] % u8::pow(2, 6);
}
ssz.append(&val_bool);
let ssz = ssz.drain();
// TODO: change to little endian bytes
// https://github.com/sigp/lighthouse/issues/215
assert_eq!(val_bool, ssz[0] % u8::pow(2, 6));
assert_eq!(ssz.len(), 1);
});

View File

@@ -0,0 +1,21 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate ethereum_types;
extern crate ssz;
use ethereum_types::H256;
use ssz::{DecodeError, Decodable};
// Fuzz ssz_decode()
fuzz_target!(|data: &[u8]| {
let result: Result<(H256, usize), DecodeError> = Decodable::ssz_decode(data, 0);
if data.len() >= 32 {
// Should have valid result
let (hash, index) = result.unwrap();
assert_eq!(index, 32);
assert_eq!(hash, H256::from_slice(&data[..32]));
} else {
// Length of less than 32 should return error
assert_eq!(result, Err(DecodeError::TooShort));
}
});

View File

@@ -0,0 +1,20 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate ethereum_types;
extern crate ssz;
use ethereum_types::H256;
use ssz::SszStream;
// Fuzz ssz_encode (via ssz_append)
fuzz_target!(|data: &[u8]| {
let mut ssz = SszStream::new();
if data.len() >= 32 {
let hash = H256::from_slice(&data[..32]);
ssz.append(&hash);
let ssz = ssz.drain();
assert_eq!(data[..32], ssz[..32]);
assert_eq!(ssz.len(), 32);
}
});

View File

@@ -0,0 +1,22 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate ssz;
use ssz::{DecodeError, Decodable};
// Fuzz ssz_decode()
fuzz_target!(|data: &[u8]| {
let result: Result<(u16, usize), DecodeError> = Decodable::ssz_decode(data, 0);
if data.len() >= 2 {
// Valid result
let (number_u16, index) = result.unwrap();
assert_eq!(index, 2);
// TODO: change to little endian bytes
// https://github.com/sigp/lighthouse/issues/215
let val = u16::from_be_bytes([data[0], data[1]]);
assert_eq!(number_u16, val);
} else {
// Length of 0 or 1 should return error
assert_eq!(result, Err(DecodeError::TooShort));
}
});

View File

@@ -0,0 +1,22 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate ssz;
use ssz::SszStream;
// Fuzz ssz_encode (via ssz_append)
fuzz_target!(|data: &[u8]| {
let mut ssz = SszStream::new();
let mut number_u16 = 0;
if data.len() >= 2 {
number_u16 = u16::from_be_bytes([data[0], data[1]]);
}
ssz.append(&number_u16);
let ssz = ssz.drain();
// TODO: change to little endian bytes
// https://github.com/sigp/lighthouse/issues/215
assert_eq!(ssz.len(), 2);
assert_eq!(number_u16, u16::from_be_bytes([ssz[0], ssz[1]]));
});

View File

@@ -0,0 +1,22 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate ssz;
use ssz::{DecodeError, Decodable};
// Fuzz ssz_decode()
fuzz_target!(|data: &[u8]| {
let result: Result<(u32, usize), DecodeError> = Decodable::ssz_decode(data, 0);
if data.len() >= 4 {
// Valid result
let (number_u32, index) = result.unwrap();
assert_eq!(index, 4);
// TODO: change to little endian bytes
// https://github.com/sigp/lighthouse/issues/215
let val = u32::from_be_bytes([data[0], data[1], data[2], data[3]]);
assert_eq!(number_u32, val);
} else {
// Length less then 4 should return error
assert_eq!(result, Err(DecodeError::TooShort));
}
});

View File

@@ -0,0 +1,22 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate ssz;
use ssz::SszStream;
// Fuzz ssz_encode (via ssz_append)
fuzz_target!(|data: &[u8]| {
let mut ssz = SszStream::new();
let mut number_u32 = 0;
if data.len() >= 4 {
number_u32 = u32::from_be_bytes([data[0], data[1], data[2], data[3]]);
}
ssz.append(&number_u32);
let ssz = ssz.drain();
// TODO: change to little endian bytes
// https://github.com/sigp/lighthouse/issues/215
assert_eq!(ssz.len(), 4);
assert_eq!(number_u32, u32::from_be_bytes([ssz[0], ssz[1], ssz[2], ssz[3]]));
});

View File

@@ -0,0 +1,31 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate ssz;
use ssz::{DecodeError, Decodable};
// Fuzz ssz_decode()
fuzz_target!(|data: &[u8]| {
let result: Result<(u64, usize), DecodeError> = Decodable::ssz_decode(data, 0);
if data.len() >= 8 {
// Valid result
let (number_u64, index) = result.unwrap();
assert_eq!(index, 8);
// TODO: change to little endian bytes
// https://github.com/sigp/lighthouse/issues/215
let val = u64::from_be_bytes([
data[0],
data[1],
data[2],
data[3],
data[4],
data[5],
data[6],
data[7],
]);
assert_eq!(number_u64, val);
} else {
// Length less then 8 should return error
assert_eq!(result, Err(DecodeError::TooShort));
}
});

View File

@@ -0,0 +1,40 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate ssz;
use ssz::SszStream;
// Fuzz ssz_encode (via ssz_append)
fuzz_target!(|data: &[u8]| {
let mut ssz = SszStream::new();
let mut number_u64 = 0;
if data.len() >= 8 {
number_u64 = u64::from_be_bytes([
data[0],
data[1],
data[2],
data[3],
data[4],
data[5],
data[6],
data[7],
]);
}
ssz.append(&number_u64);
let ssz = ssz.drain();
// TODO: change to little endian bytes
// https://github.com/sigp/lighthouse/issues/215
assert_eq!(ssz.len(), 8);
assert_eq!(number_u64, u64::from_be_bytes([
ssz[0],
ssz[1],
ssz[2],
ssz[3],
ssz[4],
ssz[5],
ssz[6],
ssz[7],
]));
});

View File

@@ -0,0 +1,21 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate ssz;
use ssz::{DecodeError, Decodable};
// Fuzz ssz_decode()
fuzz_target!(|data: &[u8]| {
let result: Result<(u8, usize), DecodeError> = Decodable::ssz_decode(data, 0);
if data.len() >= 1 {
// Should have valid result
let (number_u8, index) = result.unwrap();
// TODO: change to little endian bytes
// https://github.com/sigp/lighthouse/issues/215
assert_eq!(index, 1);
assert_eq!(number_u8, data[0]);
} else {
// Length of 0 should return error
assert_eq!(result, Err(DecodeError::TooShort));
}
});

View File

@@ -0,0 +1,22 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate ssz;
use ssz::SszStream;
// Fuzz ssz_encode (via ssz_append)
fuzz_target!(|data: &[u8]| {
let mut ssz = SszStream::new();
let mut number_u8 = 0;
if data.len() >= 1 {
number_u8 = data[0];
}
ssz.append(&number_u8);
let ssz = ssz.drain();
// TODO: change to little endian bytes
// https://github.com/sigp/lighthouse/issues/215
assert_eq!(number_u8, ssz[0]);
assert_eq!(ssz.len(), 1);
});

View File

@@ -0,0 +1,32 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate ssz;
use ssz::{DecodeError, Decodable};
// Fuzz ssz_decode()
fuzz_target!(|data: &[u8]| {
// Note: we assume architecture is 64 bit -> usize == 64 bits
let result: Result<(usize, usize), DecodeError> = Decodable::ssz_decode(data, 0);
if data.len() >= 8 {
// Valid result
let (number_usize, index) = result.unwrap();
assert_eq!(index, 8);
// TODO: change to little endian bytes
// https://github.com/sigp/lighthouse/issues/215
let val = u64::from_be_bytes([
data[0],
data[1],
data[2],
data[3],
data[4],
data[5],
data[6],
data[7],
]);
assert_eq!(number_usize, val as usize);
} else {
// Length less then 8 should return error
assert_eq!(result, Err(DecodeError::TooShort));
}
});

View File

@@ -0,0 +1,40 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate ssz;
use ssz::SszStream;
// Fuzz ssz_encode (via ssz_append)
fuzz_target!(|data: &[u8]| {
let mut ssz = SszStream::new();
let mut number_usize = 0;
if data.len() >= 8 {
number_usize = u64::from_be_bytes([
data[0],
data[1],
data[2],
data[3],
data[4],
data[5],
data[6],
data[7],
]) as usize;
}
ssz.append(&number_usize);
let ssz = ssz.drain();
// TODO: change to little endian bytes
// https://github.com/sigp/lighthouse/issues/215
assert_eq!(ssz.len(), 8);
assert_eq!(number_usize, u64::from_be_bytes([
ssz[0],
ssz[1],
ssz[2],
ssz[3],
ssz[4],
ssz[5],
ssz[6],
ssz[7],
]) as usize);
});

View File

@@ -0,0 +1,12 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate ethereum_types;
extern crate ssz;
use ethereum_types::{Address};
use ssz::{DecodeError, Decodable};
// Fuzz ssz_decode()
fuzz_target!(|data: &[u8]| {
let _result: Result<(Vec<Address>, usize), DecodeError> = Decodable::ssz_decode(data, 0);
});

View File

@@ -0,0 +1,10 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate ssz;
use ssz::{DecodeError, Decodable};
// Fuzz ssz_decode()
fuzz_target!(|data: &[u8]| {
let _result: Result<(Vec<bool>, usize), DecodeError> = Decodable::ssz_decode(data, 0);
});

View File

@@ -0,0 +1,12 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate ethereum_types;
extern crate ssz;
use ethereum_types::{Address, H256};
use ssz::{DecodeError, Decodable};
// Fuzz ssz_decode()
fuzz_target!(|data: &[u8]| {
let _result: Result<(Vec<u8>, usize), DecodeError> = Decodable::ssz_decode(data, 0);
});

View File

@@ -0,0 +1,15 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate ethereum_types;
extern crate ssz;
use ethereum_types::{Address, H256};
use ssz::SszStream;
// Fuzz ssz_encode()
fuzz_target!(|data: &[u8]| {
let mut ssz = SszStream::new();
let data_vec = data.to_vec();
ssz.append(&data_vec);
});

View File

@@ -0,0 +1,10 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate ssz;
use ssz::{DecodeError, Decodable};
// Fuzz ssz_decode()
fuzz_target!(|data: &[u8]| {
let _result: Result<(Vec<u64>, usize), DecodeError> = Decodable::ssz_decode(data, 0);
});

View File

@@ -3,55 +3,55 @@ use super::{merkle_hash, ssz_encode, TreeHash};
use hashing::hash;
impl TreeHash for u8 {
fn hash_tree_root_internal(&self) -> Vec<u8> {
fn hash_tree_root(&self) -> Vec<u8> {
ssz_encode(self)
}
}
impl TreeHash for u16 {
fn hash_tree_root_internal(&self) -> Vec<u8> {
fn hash_tree_root(&self) -> Vec<u8> {
ssz_encode(self)
}
}
impl TreeHash for u32 {
fn hash_tree_root_internal(&self) -> Vec<u8> {
fn hash_tree_root(&self) -> Vec<u8> {
ssz_encode(self)
}
}
impl TreeHash for u64 {
fn hash_tree_root_internal(&self) -> Vec<u8> {
fn hash_tree_root(&self) -> Vec<u8> {
ssz_encode(self)
}
}
impl TreeHash for usize {
fn hash_tree_root_internal(&self) -> Vec<u8> {
fn hash_tree_root(&self) -> Vec<u8> {
ssz_encode(self)
}
}
impl TreeHash for bool {
fn hash_tree_root_internal(&self) -> Vec<u8> {
fn hash_tree_root(&self) -> Vec<u8> {
ssz_encode(self)
}
}
impl TreeHash for Address {
fn hash_tree_root_internal(&self) -> Vec<u8> {
fn hash_tree_root(&self) -> Vec<u8> {
ssz_encode(self)
}
}
impl TreeHash for H256 {
fn hash_tree_root_internal(&self) -> Vec<u8> {
fn hash_tree_root(&self) -> Vec<u8> {
ssz_encode(self)
}
}
impl TreeHash for [u8] {
fn hash_tree_root_internal(&self) -> Vec<u8> {
fn hash_tree_root(&self) -> Vec<u8> {
if self.len() > 32 {
return hash(&self);
}
@@ -63,12 +63,12 @@ impl<T> TreeHash for Vec<T>
where
T: TreeHash,
{
/// Returns the merkle_hash of a list of hash_tree_root_internal values created
/// Returns the merkle_hash of a list of hash_tree_root values created
/// from the given list.
/// Note: A byte vector, Vec<u8>, must be converted to a slice (as_slice())
/// to be handled properly (i.e. hashed) as byte array.
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut tree_hashes = self.iter().map(|x| x.hash_tree_root_internal()).collect();
fn hash_tree_root(&self) -> Vec<u8> {
let mut tree_hashes = self.iter().map(|x| x.hash_tree_root()).collect();
merkle_hash(&mut tree_hashes)
}
}
@@ -79,7 +79,7 @@ mod tests {
#[test]
fn test_impl_tree_hash_vec() {
let result = vec![1u32, 2, 3, 4, 5, 6, 7].hash_tree_root_internal();
let result = vec![1u32, 2, 3, 4, 5, 6, 7].hash_tree_root();
assert_eq!(result.len(), 32);
}
}

View File

@@ -1,44 +1,31 @@
use hashing::hash;
const SSZ_CHUNK_SIZE: usize = 128;
const BYTES_PER_CHUNK: usize = 32;
const HASHSIZE: usize = 32;
pub trait TreeHash {
fn hash_tree_root_internal(&self) -> Vec<u8>;
fn hash_tree_root(&self) -> Vec<u8> {
let mut result = self.hash_tree_root_internal();
zpad(&mut result, HASHSIZE);
result
}
fn hash_tree_root(&self) -> Vec<u8>;
}
/// 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
let (mut chunk_size, mut chunkz) = list_to_blob(list);
let mut chunkz = list_to_blob(list);
// get data_len as bytes. It will hashed will the merkle root
let mut datalen = list.len().to_le_bytes().to_vec();
zpad(&mut datalen, 32);
// Tree-hash
// merklelize
while chunkz.len() > HASHSIZE {
let mut new_chunkz: Vec<u8> = Vec::new();
for two_chunks in chunkz.chunks(chunk_size * 2) {
if two_chunks.len() == chunk_size {
// Odd number of chunks
let mut c = two_chunks.to_vec();
c.append(&mut vec![0; SSZ_CHUNK_SIZE]);
new_chunkz.append(&mut hash(&c));
} else {
// Hash two chuncks together
new_chunkz.append(&mut hash(two_chunks));
}
for two_chunks in chunkz.chunks(BYTES_PER_CHUNK * 2) {
// Hash two chuncks together
new_chunkz.append(&mut hash(two_chunks));
}
chunk_size = HASHSIZE;
chunkz = new_chunkz;
}
@@ -46,17 +33,13 @@ pub fn merkle_hash(list: &mut Vec<Vec<u8>>) -> Vec<u8> {
hash(&chunkz)
}
fn list_to_blob(list: &mut Vec<Vec<u8>>) -> (usize, Vec<u8>) {
let chunk_size = if list.is_empty() || list[0].len() < SSZ_CHUNK_SIZE {
SSZ_CHUNK_SIZE
} else {
list[0].len()
};
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
let (items_per_chunk, chunk_count) = if list.is_empty() {
(1, 1)
} else {
let items_per_chunk = SSZ_CHUNK_SIZE / list[0].len();
let items_per_chunk = BYTES_PER_CHUNK / list[0].len();
let chunk_count = list.len() / items_per_chunk;
(items_per_chunk, chunk_count)
};
@@ -64,20 +47,20 @@ fn list_to_blob(list: &mut Vec<Vec<u8>>) -> (usize, Vec<u8>) {
let mut chunkz = Vec::new();
if list.is_empty() {
// handle and empty list
chunkz.append(&mut vec![0; SSZ_CHUNK_SIZE]);
} else if list[0].len() <= SSZ_CHUNK_SIZE {
chunkz.append(&mut vec![0; BYTES_PER_CHUNK * 2]);
} else if list[0].len() <= BYTES_PER_CHUNK {
// just create a blob here; we'll divide into
// chunked slices when we merklize
let mut chunk = Vec::with_capacity(chunk_size);
let mut chunk = Vec::with_capacity(BYTES_PER_CHUNK);
let mut item_count_in_chunk = 0;
chunkz.reserve(chunk_count * chunk_size);
chunkz.reserve(chunk_count * BYTES_PER_CHUNK);
for item in list.iter_mut() {
item_count_in_chunk += 1;
chunk.append(item);
// completed chunk?
if item_count_in_chunk == items_per_chunk {
zpad(&mut chunk, chunk_size);
zpad(&mut chunk, BYTES_PER_CHUNK);
chunkz.append(&mut chunk);
item_count_in_chunk = 0;
}
@@ -85,18 +68,18 @@ fn list_to_blob(list: &mut Vec<Vec<u8>>) -> (usize, Vec<u8>) {
// left-over uncompleted chunk?
if item_count_in_chunk != 0 {
zpad(&mut chunk, chunk_size);
zpad(&mut chunk, BYTES_PER_CHUNK);
chunkz.append(&mut chunk);
}
} else {
// chunks larger than SSZ_CHUNK_SIZE
chunkz.reserve(chunk_count * chunk_size);
for item in list.iter_mut() {
chunkz.append(item);
}
}
(chunk_size, chunkz)
// 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
}
/// right pads with zeros making 'bytes' 'size' in length
@@ -112,9 +95,9 @@ mod tests {
#[test]
fn test_merkle_hash() {
let data1 = vec![1; 100];
let data2 = vec![2; 100];
let data3 = vec![3; 100];
let data1 = vec![1; 32];
let data2 = vec![2; 32];
let data3 = vec![3; 32];
let mut list = vec![data1, data2, data3];
let result = merkle_hash(&mut list);