| Crates.io | merkleproof |
| lib.rs | merkleproof |
| version | 0.1.0 |
| created_at | 2025-03-27 06:41:57.399967+00 |
| updated_at | 2025-03-27 06:41:57.399967+00 |
| description | A robust implementation of Merkle trees for data verification in distributed systems |
| homepage | |
| repository | https://github.com/0xsouravm/merkle-tree-rs |
| max_upload_size | |
| id | 1607663 |
| size | 38,023 |
A robust, efficient, and well-documented implementation of Merkle trees in Rust.
sha2 and hex)Add this to your Cargo.toml:
[dependencies]
merkleproof = "0.1.0"
use merkleproof::MerkleTree;
// Create a tree with your data
let data = vec![
b"Transaction 1".to_vec(),
b"Transaction 2".to_vec(),
b"Transaction 3".to_vec(),
];
let tree = MerkleTree::new(data.clone());
println!("Merkle Root: {}", tree.root_hash_hex());
// Generate a proof for a specific item
if let Some(proof) = tree.generate_proof(&data[1]) {
// Verify the proof
let root_hash = tree.root_hash().unwrap();
let is_valid = MerkleTree::verify_proof(&data[1], &proof, &root_hash);
if is_valid {
println!("Data verified successfully!");
}
}
A Merkle tree (hash tree) is a binary tree structure where:
This structure enables efficient verification of data integrity in distributed systems:
Root Hash
/ \
/ \
Hash(H_A+H_B) Hash(H_C+H_D)
/ \ / \
H_A H_B H_C H_D
| | | |
Data A Data B Data C Data D
use merkleproof::MerkleTree;
// Create data items
let data_items = vec![
b"Alice sends 5 BTC to Bob".to_vec(),
b"Bob sends 3 BTC to Charlie".to_vec(),
b"David sends 2 BTC to Eve".to_vec(),
];
// Create a new Merkle tree
let tree = MerkleTree::new(data_items.clone());
// Get the root hash as a hex string
println!("Merkle Root: {}", tree.root_hash_hex());
use merkleproof::MerkleTree;
let data_items = vec![
b"Item 1".to_vec(),
b"Item 2".to_vec(),
b"Item 3".to_vec(),
];
let tree = MerkleTree::new(data_items.clone());
let root_hash = tree.root_hash().unwrap();
// Generate a proof for Item 2
let item_to_verify = &data_items[1];
if let Some(proof) = tree.generate_proof(item_to_verify) {
// Verify the proof
let is_valid = MerkleTree::verify_proof(item_to_verify, &proof, &root_hash);
assert!(is_valid);
// Tampered data will fail verification
let mut tampered_item = item_to_verify.clone();
tampered_item[0] ^= 1; // Flip a bit
let is_valid = MerkleTree::verify_proof(&tampered_item, &proof, &root_hash);
assert!(!is_valid);
}
MerkleNodeRepresents nodes in the Merkle tree:
Leaf: Contains original data and its hashBranch: Contains left and right children and their combined hashMerkleTreeThe main structure with:
root: Root node of the treeleaves: All leaf nodes for efficient proof generation// Creates a new Merkle tree from data items
let tree = MerkleTree::new(data_items);
// Get the binary root hash
let root_hash = tree.root_hash();
// Get the hexadecimal root hash string
let root_hash_hex = tree.root_hash_hex();
// Generate a proof for specific data
let proof = tree.generate_proof(&data);
// Verify a proof against the root hash
let is_valid = MerkleTree::verify_proof(&data, &proof, &root_hash);
Merkle trees are widely used in:
Contributions are welcome! Please feel free to submit a Pull Request. Here are some specific areas where contributions would be particularly valuable:
This project is licensed under the MIT License - see the LICENSE file for details.