Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support tree hash by value #33

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ itertools = "0.13.0"
[dev-dependencies]
serde_json = "1.0.0"
tree_hash_derive = "0.8.0"

[patch.crates-io]
tree_hash = { git = "https://github.com/sigp/tree_hash", branch = "self-by-value" }
tree_hash_derive = { git = "https://github.com/sigp/tree_hash", branch = "self-by-value" }
12 changes: 6 additions & 6 deletions src/bitfield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,41 +643,41 @@
}
}

impl<N: Unsigned + Clone> tree_hash::TreeHash for Bitfield<Variable<N>> {
impl<N: Unsigned + Clone> tree_hash::TreeHash for &Bitfield<Variable<N>> {
fn tree_hash_type() -> tree_hash::TreeHashType {
tree_hash::TreeHashType::List
}

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

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

fn tree_hash_root(&self) -> Hash256 {
fn tree_hash_root(self) -> Hash256 {

Check warning on line 659 in src/bitfield.rs

View check run for this annotation

Codecov / codecov/patch

src/bitfield.rs#L659

Added line #L659 was not covered by tests
// Note: we use `as_slice` because it does _not_ have the length-delimiting bit set (or
// present).
let root = bitfield_bytes_tree_hash_root::<N>(self.as_slice());
tree_hash::mix_in_length(&root, self.len())
}
}

impl<N: Unsigned + Clone> tree_hash::TreeHash for Bitfield<Fixed<N>> {
impl<N: Unsigned + Clone> tree_hash::TreeHash for &Bitfield<Fixed<N>> {
fn tree_hash_type() -> tree_hash::TreeHashType {
tree_hash::TreeHashType::Vector
}

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

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

fn tree_hash_root(&self) -> Hash256 {
fn tree_hash_root(self) -> Hash256 {

Check warning on line 680 in src/bitfield.rs

View check run for this annotation

Codecov / codecov/patch

src/bitfield.rs#L680

Added line #L680 was not covered by tests
bitfield_bytes_tree_hash_root::<N>(self.as_slice())
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/fixed_vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,23 +177,23 @@ impl<T, N: Unsigned> IntoIterator for FixedVector<T, N> {
}
}

impl<T, N: Unsigned> tree_hash::TreeHash for FixedVector<T, N>
impl<'a, T, N: Unsigned> tree_hash::TreeHash for &'a FixedVector<T, N>
where
T: tree_hash::TreeHash,
&'a T: tree_hash::TreeHash,
{
fn tree_hash_type() -> tree_hash::TreeHashType {
tree_hash::TreeHashType::Vector
}

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

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

fn tree_hash_root(&self) -> Hash256 {
fn tree_hash_root(self) -> Hash256 {
vec_tree_hash_root::<T, N>(&self.vec)
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/tree_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ use typenum::Unsigned;

/// A helper function providing common functionality between the `TreeHash` implementations for
/// `FixedVector` and `VariableList`.
pub fn vec_tree_hash_root<T, N>(vec: &[T]) -> Hash256
pub fn vec_tree_hash_root<'a, T: 'a, N>(vec: &'a [T]) -> Hash256
where
T: TreeHash,
&'a T: TreeHash,
N: Unsigned,
{
match T::tree_hash_type() {
match <&T>::tree_hash_type() {
TreeHashType::Basic => {
let mut hasher = MerkleHasher::with_leaves(
(N::to_usize() + T::tree_hash_packing_factor() - 1) / T::tree_hash_packing_factor(),
(N::to_usize() + <&T>::tree_hash_packing_factor() - 1)
/ <&T>::tree_hash_packing_factor(),
);

for item in vec {
Expand Down
8 changes: 4 additions & 4 deletions src/variable_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,23 +193,23 @@ impl<T, N: Unsigned> IntoIterator for VariableList<T, N> {
}
}

impl<T, N: Unsigned> tree_hash::TreeHash for VariableList<T, N>
impl<'a, T, N: Unsigned> tree_hash::TreeHash for &'a VariableList<T, N>
where
T: tree_hash::TreeHash,
&'a T: tree_hash::TreeHash,
{
fn tree_hash_type() -> tree_hash::TreeHashType {
tree_hash::TreeHashType::List
}

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

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

fn tree_hash_root(&self) -> Hash256 {
fn tree_hash_root(self) -> Hash256 {
let root = vec_tree_hash_root::<T, N>(&self.vec);

tree_hash::mix_in_length(&root, self.len())
Expand Down
Loading