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
impl MerkleTree
Sourcepub fn from_levels(levels: Vec<Vec<Hash>>) -> MerkleTree
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.
Sourcepub fn from_reader(reader: impl Read) -> Result<MerkleTree, Error>
pub fn from_reader(reader: impl Read) -> Result<MerkleTree, Error>
Creates a MerkleTree
from all of the bytes of a Read
er.
§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
impl AsRef<[Vec<GenericDigest<FuchsiaMerkleMarker>>]> for MerkleTree
Source§impl Clone for MerkleTree
impl Clone for MerkleTree
Source§fn clone(&self) -> MerkleTree
fn clone(&self) -> MerkleTree
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for MerkleTree
impl Debug for MerkleTree
Source§impl From<MerkleTreeBuilder> for MerkleTree
impl From<MerkleTreeBuilder> for MerkleTree
Source§fn from(builder: MerkleTreeBuilder) -> Self
fn from(builder: MerkleTreeBuilder) -> Self
Source§impl Hash for MerkleTree
impl Hash for MerkleTree
Source§impl Ord for MerkleTree
impl Ord for MerkleTree
Source§fn cmp(&self, other: &MerkleTree) -> Ordering
fn cmp(&self, other: &MerkleTree) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for MerkleTree
impl PartialEq for MerkleTree
Source§impl PartialOrd for MerkleTree
impl PartialOrd for MerkleTree
impl Eq for MerkleTree
impl StructuralPartialEq for MerkleTree
Auto Trait Implementations§
impl Freeze for MerkleTree
impl RefUnwindSafe for MerkleTree
impl Send for MerkleTree
impl Sync for MerkleTree
impl Unpin for MerkleTree
impl UnwindSafe for MerkleTree
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)