pub fn check(&self, val: &[u8], proof: &Path<D>) -> Result<(), MerkleTreeError> {
        let mut idx = proof.index;

        let mut h = D::digest(val).to_vec();
        for p in &proof.values {
            if (idx & 0b1) == 0 {
                h = D::new().chain(h).chain(p).finalize().to_vec();
            } else {
                h = D::new().chain(p).chain(h).finalize().to_vec();
            }
            idx >>= 1;
        }

        if h == self.value {
            return Ok(());
        }
        Err(MerkleTreeError::InvalidPath)
    }
/// Check an inclusion proof that `val` is part of the tree by traveling the whole path
/// until the root.
///
/// # Error
/// Returns an error if the path is invalid.
///
/// # Example
/// ```
/// # use rand_core::{OsRng, RngCore};
/// # use atms::merkle_tree::MerkleTree;
/// # use blake2::Blake2b;
/// # fn main() {
/// let mut rng = OsRng::default();
/// // We generate the keys.
/// let mut keys = Vec::with_capacity(32);
/// for _ in 0..32 {
///     let mut leaf = [0u8; 32];
///     rng.fill_bytes(&mut leaf);
///     keys.push(leaf.to_vec());
/// }
/// // Compute the Merkle tree of the keys.
/// let mt = MerkleTree::<Blake2b>::create(&keys);
/// // Compute the path of key in position 3.
/// let path = mt.get_path(3);
/// // Verify the proof of membership with respect to the merkle commitment.
/// assert!(mt.to_commitment().check(&keys[3], &path).is_ok());
///
/// # }
Check an inclusion proof that `val` is part of the tree by traveling the whole path
until the root.
Check an inclusion proof that ` val ` is part of the tree by traveling the whole path until the root .
pub fn check(&self, val: &[u8], proof: &Path<D>) -> Result<(), MerkleTreeError> {
        let mut idx = proof.index;
        let mut h = D::digest(val).to_vec();
        for p in &proof.values {
            if (idx & 0b1) == 0 {
                h = D::new().chain(h).chain(p).finalize().to_vec();
            } else {
                h = D::new().chain(p).chain(h).finalize().to_vec();
            }
            idx >>= 1;
        }
        if h == self.value {
            return Ok(());
        }
        Err(MerkleTreeError::InvalidPath)
    }
pub fn check ( & self , val : & [ u8 ] , proof : & Path < D > ) -> Result < ( ) , MerkleTreeError > { let mut idx = proof . index ; let mut h = D :: digest ( val ) . to_vec ( ) ; for p in & proof . values { if ( idx & 0b1 ) == 0 { h = D :: new ( ) . chain ( h ) . chain ( p ) . finalize ( ) . to_vec ( ) ; } else { h = D :: new ( ) . chain ( p ) . chain ( h ) . finalize ( ) . to_vec ( ) ; } idx >>= 1 ; } if h == self . value { return Ok ( ( ) ) ; } Err ( MerkleTreeError :: InvalidPath ) }
Check an inclusion proof that `val` is part of the tree by traveling the whole path
until the root.
Check an inclusion proof that ` val ` is part of the tree by traveling the whole path until the root .
&self
val
&[u8]
proof
&Path<D>
error
Returns an error if the path is invalid.
Returns an error if the path is invalid .
example
None
use rand_core::{osrng, rngcore};
None
use atms::merkle_tree::merkletree;
None
use blake2::blake2b;
None
fn main() {
let mut rng = OsRng::default();
We generate the keys.
let mut keys = Vec::with_capacity(32);
for _ in 0..32 {
let mut leaf = [0u8; 32];
rng.fill_bytes(&mut leaf);
keys.push(leaf.to_vec());
}
Compute the Merkle tree of the keys.
let mt = MerkleTree::::create(&keys);
Compute the path of key in position 3.
let path = mt.get_path(3);
Verify the proof of membership with respect to the merkle commitment.
let mut rng = OsRng :: default () ; We generate the keys . let mut keys = Vec :: with_capacity ( 32 ) ; for _ in 0 .. 32 { let mut leaf = [ 0u8 ; 32 ] ; rng . fill_bytes ( &mut leaf ) ; keys . push ( leaf . to_vec () ) ; } Compute the Merkle tree of the keys . let mt = MerkleTree :::: create ( &keys ) ; Compute the path of key in position 3 . let path = mt . get_path ( 3 ) ; Verify the proof of membership with respect to the merkle commitment .
}
None