Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ readme = "README.md"
repository = "https://github.com/sigp/tree_hash"
keywords = ["ethereum"]
categories = ["cryptography::cryptocurrencies"]

[patch.crates-io]
ethereum_ssz = { path = "../ethereum_ssz/ssz" }
34 changes: 33 additions & 1 deletion tree_hash/src/impls.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;
use alloy_primitives::{Address, FixedBytes, U128, U256};
use ssz::{Bitfield, Fixed, Variable};
use ssz::{Bitfield, Fixed, Progressive, Variable};
use std::sync::Arc;
use typenum::Unsigned;

Expand Down Expand Up @@ -208,6 +208,38 @@ impl<N: Unsigned + Clone> TreeHash for Bitfield<Variable<N>> {
}
}

impl TreeHash for Bitfield<Progressive> {
fn tree_hash_type() -> TreeHashType {
TreeHashType::List
}

fn tree_hash_packed_encoding(&self) -> PackedEncoding {
unreachable!("ProgressiveBitField should never be packed.")
}

fn tree_hash_packing_factor() -> usize {
unreachable!("ProgressiveBitField should never be packed.")
}

fn tree_hash_root(&self) -> Hash256 {
// FIXME(sproul): unclear if this is intended or a bug in the spec tests
// See: https://github.com/ethereum/consensus-specs/issues/4795
if self.is_empty() {
return mix_in_length(&Hash256::ZERO, 0);
}

let mut hasher = ProgressiveMerkleHasher::new();
hasher
.write(self.as_slice())
.expect("ProgessiveBitList should not exceed tree hash leaf limit");

let bitfield_root = hasher
.finish()
.expect("ProgressiveBitList tree hash buffer should not exceed leaf limit");
mix_in_length(&bitfield_root, self.len())
}
}

impl<N: Unsigned + Clone> TreeHash for Bitfield<Fixed<N>> {
fn tree_hash_type() -> TreeHashType {
TreeHashType::Vector
Expand Down
11 changes: 11 additions & 0 deletions tree_hash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ pub mod impls;
mod merkle_hasher;
mod merkleize_padded;
mod merkleize_standard;
mod progressive_merkle_hasher;

pub use merkle_hasher::{Error, MerkleHasher};
pub use merkleize_padded::merkleize_padded;
pub use merkleize_standard::merkleize_standard;
pub use progressive_merkle_hasher::{
Error as ProgressiveMerkleHasherError, ProgressiveMerkleHasher,
};

use ethereum_hashing::{hash_fixed, ZERO_HASHES, ZERO_HASHES_MAX_INDEX};
use smallvec::SmallVec;
Expand Down Expand Up @@ -89,6 +93,13 @@ pub fn mix_in_selector(root: &Hash256, selector: u8) -> Option<Hash256> {
Some(Hash256::from_slice(&root))
}

pub fn mix_in_active_fields(root: Hash256, active_fields: [u8; BYTES_PER_CHUNK]) -> Hash256 {
Hash256::from(ethereum_hashing::hash32_concat(
root.as_slice(),
&active_fields,
))
}

/// Returns a cached padding node for a given height.
fn get_zero_hash(height: usize) -> &'static [u8] {
if height <= ZERO_HASHES_MAX_INDEX {
Expand Down
Loading
Loading