#merkle-tree #merkle #binary-tree #crypto

no-std merkle-lite

A simple, fast, and composable binary Merkle tree and proof for Rust Crypto hash functions

16 releases

0.1.0 Jan 23, 2023
0.0.15 Jan 23, 2023

#1427 in Cryptography

Download history 29/week @ 2024-07-26 17/week @ 2024-08-02 2/week @ 2024-08-09 5/week @ 2024-08-23 9/week @ 2024-08-30 1/week @ 2024-09-06 25/week @ 2024-09-20 16/week @ 2024-09-27

53 downloads per month

MIT/Apache

42KB
721 lines

merkle-lite

CI License Cargo Documentation

A simple, fast, and composable binary Merkle tree and proof for Rust Crypto hash functions.

Examples

It's super simple to compose MerkleTree from the ordered array of hashes and verify the proof of inclusion with MerkleProof:

use merkle_lite::MerkleTree;
use rand_core::RngCore;

// Composes MerkleTree from the 50,000 random hashes.
let tree: MerkleTree<sha3::Sha3_256> = std::iter::repeat([0u8; 32])
    .map(|mut leaf| {
        rand_core::OsRng.fill_bytes(&mut leaf);
        leaf
    })
    .take(50_000)
    .collect();

// Verifies the proof of inclusion for the arbitrary leaves.
let tree_leaves = tree.get_leaves();
let leaf_indices = [12, 0, 1, 1201, 13_903, 980];
let leaf_hashes: Vec<_> = leaf_indices
    .iter()
    .map(|index| (*index, tree_leaves[*index]))
    .collect();
assert_eq!(
    tree.proof(&leaf_indices)
        .expect("proof")
        .verify(&leaf_hashes)
        .expect("verify")
        .as_ref(),
    tree.root().expect("root"),
);

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~335KB