pub struct MerkleTree { /* private fields */ }
Expand description

A MerkleTree contains levels of hashes that can be used to verify the integrity of data.

While a single hash could be used to integrity check some data, if the data (or hash) is corrupt, a single hash can not determine what part of the data is corrupt. A MerkleTree, however, contains a hash for every 8K block of data, allowing it to identify which 8K blocks of data are corrupt. A MerkleTree also allows individual 8K blocks of data to be verified without having to verify the rest of the data.

Furthermore, a MerkleTree contains multiple levels of hashes, where each level contains hashes of 8K blocks of hashes of the lower level. The top level always contains a single hash, the merkle root. This tree of hashes allows a MerkleTree to determine which of its own hashes are corrupt, if any.

§Structure Details

A merkle tree contains levels. A level is a row of the tree, starting at 0 and counting upward. Level 0 represents the leaves of the tree which contain hashes of chunks of the input stream.

Each level consists of a hash for each 8K block of hashes from the previous level (or, for level 0, each 8K block of data). When computing a hash, the 8K block is prepended with a block identity.

A block identity is the binary OR of the starting byte index of the block within the current level and the current level index, followed by the length of the block. For level 0, the length of the block is 8K, except for the last block, which may be less than 8K. All other levels use a block length of 8K.

Implementations§

source§

impl MerkleTree

source

pub fn from_levels(levels: Vec<Vec<Hash>>) -> MerkleTree

Creates a MerkleTree from a well-formed tree of hashes.

A tree of hashes is well-formed iff:

  • The length of the last level is 1.
  • The length of every hash level is the length of the prior hash level divided by HASHES_PER_BLOCK, rounded up to the nearest integer.
source

pub fn root(&self) -> Hash

The root hash of the merkle tree.

source

pub fn from_reader(reader: impl Read) -> Result<MerkleTree, Error>

Creates a MerkleTree from all of the bytes of a Reader.

§Examples
let data_to_hash = [0xffu8; 8192];
let tree = MerkleTree::from_reader(&data_to_hash[..]).unwrap();
assert_eq!(
    tree.root(),
    "68d131bc271f9c192d4f6dcd8fe61bef90004856da19d0f2f514a7f4098b0737".parse().unwrap()
);

Trait Implementations§

source§

impl AsRef<[Vec<GenericDigest<FuchsiaMerkleMarker>>]> for MerkleTree

source§

fn as_ref(&self) -> &[Vec<Hash>]

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Clone for MerkleTree

source§

fn clone(&self) -> MerkleTree

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for MerkleTree

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl From<MerkleTreeBuilder> for MerkleTree

source§

fn from(builder: MerkleTreeBuilder) -> Self

Converts to this type from the input type.
source§

impl Hash for MerkleTree

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Ord for MerkleTree

source§

fn cmp(&self, other: &MerkleTree) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for MerkleTree

source§

fn eq(&self, other: &MerkleTree) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for MerkleTree

source§

fn partial_cmp(&self, other: &MerkleTree) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Eq for MerkleTree

source§

impl StructuralPartialEq for MerkleTree

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.