lighthouse/eth2/utils/tree_hash/src/lib.rs

75 lines
1.9 KiB
Rust
Raw Normal View History

2019-04-25 23:55:03 +00:00
pub mod impls;
2019-04-26 00:37:50 +00:00
pub mod merkleize;
2019-04-15 01:14:30 +00:00
pub const BYTES_PER_CHUNK: usize = 32;
pub const HASHSIZE: usize = 32;
2019-04-25 23:55:03 +00:00
pub const MERKLE_HASH_CHUNK: usize = 2 * BYTES_PER_CHUNK;
2019-04-15 23:34:23 +00:00
2019-04-15 01:14:30 +00:00
#[derive(Debug, PartialEq, Clone)]
2019-04-15 23:34:23 +00:00
pub enum TreeHashType {
2019-04-15 01:14:30 +00:00
Basic,
2019-04-17 00:57:36 +00:00
Vector,
2019-04-15 01:14:30 +00:00
List,
2019-04-17 00:57:36 +00:00
Container,
2019-04-15 01:14:30 +00:00
}
2019-04-25 23:55:03 +00:00
pub trait TreeHash {
fn tree_hash_type() -> TreeHashType;
fn tree_hash_packed_encoding(&self) -> Vec<u8>;
fn tree_hash_packing_factor() -> usize;
fn tree_hash_root(&self) -> Vec<u8>;
}
pub trait SignedRoot: TreeHash {
fn signed_root(&self) -> Vec<u8>;
}
2019-04-16 02:29:39 +00:00
#[macro_export]
2019-04-17 01:57:57 +00:00
macro_rules! tree_hash_ssz_encoding_as_vector {
2019-04-16 02:29:39 +00:00
($type: ident) => {
impl tree_hash::TreeHash for $type {
fn tree_hash_type() -> tree_hash::TreeHashType {
2019-04-17 00:57:36 +00:00
tree_hash::TreeHashType::Vector
2019-04-16 02:29:39 +00:00
}
fn tree_hash_packed_encoding(&self) -> Vec<u8> {
2019-04-17 00:57:36 +00:00
unreachable!("Vector should never be packed.")
2019-04-16 02:29:39 +00:00
}
fn tree_hash_packing_factor() -> usize {
2019-04-17 00:57:36 +00:00
unreachable!("Vector should never be packed.")
2019-04-16 02:29:39 +00:00
}
fn tree_hash_root(&self) -> Vec<u8> {
2019-04-26 00:37:50 +00:00
tree_hash::merkleize::merkle_root(&ssz::ssz_encode(self))
2019-04-16 02:29:39 +00:00
}
}
};
}
2019-04-17 01:57:57 +00:00
#[macro_export]
macro_rules! tree_hash_ssz_encoding_as_list {
($type: ident) => {
impl tree_hash::TreeHash for $type {
fn tree_hash_type() -> tree_hash::TreeHashType {
tree_hash::TreeHashType::List
}
fn tree_hash_packed_encoding(&self) -> Vec<u8> {
unreachable!("List should never be packed.")
}
fn tree_hash_packing_factor() -> usize {
unreachable!("List should never be packed.")
}
fn tree_hash_root(&self) -> Vec<u8> {
ssz::ssz_encode(self).tree_hash_root()
}
}
};
}