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

52 lines
1.3 KiB
Rust
Raw Normal View History

2019-04-15 23:14:33 +00:00
pub mod cached_tree_hash;
2019-04-16 01:14:28 +00:00
pub mod signed_root;
2019-04-15 23:14:33 +00:00
pub mod standard_tree_hash;
2019-04-15 01:14:30 +00:00
pub const BYTES_PER_CHUNK: usize = 32;
pub const HASHSIZE: usize = 32;
pub const MERKLE_HASH_CHUNCK: usize = 2 * BYTES_PER_CHUNK;
pub use cached_tree_hash::{BTreeOverlay, CachedTreeHashSubTree, Error, TreeHashCache};
2019-04-16 01:14:28 +00:00
pub use signed_root::SignedRoot;
2019-04-16 02:29:39 +00:00
pub use standard_tree_hash::{merkle_root, TreeHash};
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
}
fn num_sanitized_leaves(num_bytes: usize) -> usize {
let leaves = (num_bytes + HASHSIZE - 1) / HASHSIZE;
leaves.next_power_of_two()
}
2019-04-15 23:14:33 +00:00
fn num_nodes(num_leaves: usize) -> usize {
2 * num_leaves - 1
2019-04-15 01:14:30 +00:00
}
2019-04-16 02:29:39 +00:00
#[macro_export]
macro_rules! impl_tree_hash_for_ssz_bytes {
($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> {
tree_hash::merkle_root(&ssz::ssz_encode(self))
}
}
};
}