Skip to content

Commit 9161f39

Browse files
committed
chore: add readme to pyproject
1 parent 5d8a35b commit 9161f39

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

bindings/python/README.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

bindings/python/README.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<p align="center">
2+
<picture>
3+
<img alt="mrkle" src="https://raw.githubusercontent.com/LVivona/mrkle/refs/heads/main/.github/assets/banner.png" style="max-width: 100%;">
4+
</picture>
5+
</p>
6+
<p align="center">
7+
<a href="https://github.com/LVivona/mrkle/blob/main/LICENSE-APACHE.md"><img src="https://img.shields.io/badge/license-Apache_2.0-blue.svg" alt="License"></a>
8+
<a href="https://github.com/LVivona/mrkle/blob/main/LICENSE-MIT.md"><img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="License"></a>
9+
<a href="https://crates.io/crates/mrkle"><img alt="Crates.io Version" src="https://img.shields.io/crates/v/mrkle"></a>
10+
<a href="https://docs.rs/mrkle"><img alt="docs.rs" src="https://img.shields.io/badge/rust-docs.rs-lightgray?logo=rust&logoColor=orange"></a>
11+
<a href="https://pypi.org/project/mrkle/"><img alt="PyPI" src="https://img.shields.io/pypi/v/mrkle"></a>
12+
<a href="https://pypi.org/project/mrkle/"><img alt="Python Version" src="https://img.shields.io/pypi/pyversions/mrkle?logo=python"></a>
13+
</p>
14+
15+
16+
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.
17+
18+
### What is a Merkle Tree?
19+
A Merkle Tree is a tree data structure. where it contains a set of properties such as:
20+
21+
- Each leaf node contains the hash of a data block
22+
- Each non-leaf node contains the hash of its child nodes
23+
- The root hash represents a cryptographic fingerprint of all the data in the tree
24+
25+
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.
26+
27+
### Use Cases
28+
29+
* Blockchain & Cryptocurrencies: Bitcoin and other cryptocurrencies use Merkle Trees to efficiently verify transactions
30+
* Distributed Systems: Verify data integrity across distributed networks
31+
* File Systems: Git uses Merkle Trees to track changes and verify repository integrity
32+
* Database Verification: Ensure data hasn't been tampered with
33+
* Peer-to-Peer Networks: Verify chunks of data in distributed file sharing
34+
35+
36+
### Example
37+
38+
```python
39+
import torch
40+
from mrkle import MrkleTree
41+
42+
def namespaced_state_dict(model: torch.nn.Module) -> dict[str, torch.Tensor]:
43+
"""
44+
Returns a state_dict with the model name prefixed to every key.
45+
"""
46+
sd = model.state_dict()
47+
return {f"{model.__class__.__name__.lower()}.{k}": v.detach().cpu().numpy() for k, v in sd.items()}
48+
49+
class ToyModel(torch.nn.Module):
50+
def __init__(self, in_feature: int, out_feature: int):
51+
super().__init__()
52+
self.ln = torch.nn.Linear(in_feature, out_feature)
53+
self.output = torch.nn.Linear(out_feature, 1)
54+
55+
def forward(self, x: torch.Tensor):
56+
x = self.ln(x)
57+
logits = self.output(torch.tanh(x))
58+
return logits, torch.sigmoid(x)
59+
60+
# Create model + state dict
61+
model = ToyModel(10, 10)
62+
state_dict = namespaced_state_dict(model)
63+
64+
# Construct Merkle tree over model parameters
65+
tree = MrkleTree.from_dict(state_dict, name="sha256", fmt="flatten")
66+
67+
# Root hash identifies the entire model uniquely
68+
print(tree.root())
69+
```
70+
71+
Construct a basic binary Merkle Tree by chunking data into byte slices:
72+
73+
```rust
74+
use mrkle::MrkleTree;
75+
use sha2::Sha256;
76+
77+
// Input data (could also be read from a file)
78+
let data = b"The quick brown fox jumps over the lazy dog. \
79+
This is some extra data to make it larger. \
80+
Merkle trees are cool!";
81+
82+
// Split into fixed-size chunks
83+
let chunk_size = 16;
84+
let chunks: Vec<&[u8]> = data.chunks(chunk_size).collect();
85+
86+
// Build the Merkle tree from chunks
87+
let tree = MrkleTree::<&[u8], Sha256>::from(chunks);
88+
89+
// Get the Merkle root
90+
let root = tree.root();
91+
```

bindings/python/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ dynamic = ["version"]
2727
authors = [
2828
{name = 'Luca Vivona', email = "lucavivona01@gmail.com"}
2929
]
30+
readme = {file = "README.md", content-type = "text/markdown"}
3031
description = "A fast and flexible Merkle Tree library."
3132

3233
[project.optional-dependencies]

0 commit comments

Comments
 (0)