| Crates.io | mrkle |
| lib.rs | mrkle |
| version | 0.0.1-rc-0 |
| created_at | 2025-10-13 07:30:47.226306+00 |
| updated_at | 2025-10-13 07:30:47.226306+00 |
| description | A fast and flexible Merkle Tree library. |
| homepage | https://mrkle.org |
| repository | https://github.com/LVivona/mrkle |
| max_upload_size | |
| id | 1880128 |
| size | 222,405 |
A fast and flexible Merkle Tree library for Rust, providing efficient construction of Merkle Trees, verification of Merkle Proofs for single and multiple elements, and generic support for any hashable data type.
A Merkle Tree is a tree data structure. where it contains a set of properties such as:
This data structure enables efficient and secure verification that a data element is part of a larger dataset without needing to download the entire dataset.
Blockchain & Cryptocurrencies: Bitcoin and other cryptocurrencies use Merkle Trees to efficiently verify transactions
Distributed Systems: Verify data integrity across distributed networks
File Systems: Git uses Merkle Trees to track changes and verify repository integrity
Database Verification: Ensure data hasn't been tampered with
Peer-to-Peer Networks: Verify chunks of data in distributed file sharing
import torch
from mrkle import MrkleTree
def namespaced_state_dict(model: torch.nn.Module) -> dict[str, torch.Tensor]:
"""
Returns a state_dict with the model name prefixed to every key.
"""
sd = model.state_dict()
return {f"{model.__class__.__name__.lower()}.{k}": v.detach().cpu().numpy() for k, v in sd.items()}
class ToyModel(torch.nn.Module):
def __init__(self, in_feature: int, out_feature: int):
super().__init__()
self.ln = torch.nn.Linear(in_feature, out_feature)
self.output = torch.nn.Linear(out_feature, 1)
def forward(self, x: torch.Tensor):
x = self.ln(x)
logits = self.output(torch.tanh(x))
return logits, torch.sigmoid(x)
# Create model + state dict
model = ToyModel(10, 10)
state_dict = namespaced_state_dict(model)
# Construct Merkle tree over model parameters
tree = MrkleTree.from_dict(state_dict, name="sha256", fmt="flatten")
# Root hash identifies the entire model uniquely
print(tree.root())
Construct a basic binary Merkle Tree by chunking data into byte slices:
use mrkle::MrkleTree;
use sha2::Sha256;
// Input data (could also be read from a file)
let data = b"The quick brown fox jumps over the lazy dog. \
This is some extra data to make it larger. \
Merkle trees are cool!";
// Split into fixed-size chunks
let chunk_size = 16;
let chunks: Vec<&[u8]> = data.chunks(chunk_size).collect();
// Build the Merkle tree from chunks
let tree = MrkleTree::<&[u8], Sha256>::from(chunks);
// Get the Merkle root
let root = tree.root();