hanfei-fa 法
Verify ML model weights. Know exactly what changed.
法不阿贵,绳不挠曲。 The law does not favor the noble; the plumb line does not bend for the crooked. — 韩非子
Hierarchical Merkle tree verification for ML model weights in Rust.
- O(1) root hash — single comparison verifies the entire model
- O(k log C) diff — layer-aware diff without scanning unchanged weights
- 4-level hierarchy — Model > Layer > Parameter > Chunk
- 5 hash algorithms — SHA-256, SHA-512, SHA3-256, BLAKE2b, BLAKE3
- Merkle proofs — cryptographic proof that a chunk belongs to the tree
- Streaming —
from_reader()with O(chunk_size) memory for multi-GB files - Parallel — rayon-based parallel hashing
Also available as a Python package: pip install hanfei-fa
Install
[]
= "0.2"
Quick Start
use ;
// Hash a file's chunks
let data = read.unwrap;
let chunks = chunk_bytes;
let hashes: = chunks.iter
.map
.collect;
let tree = new;
println!;
Stream from file (constant memory)
use ;
use File;
let mut f = open.unwrap;
let tree = from_reader.unwrap;
println!;
Diff two model versions
use ;
use BTreeMap;
let blobs_v1: = /* load base model */;
let blobs_v2: = /* load fine-tuned model */;
let tree_v1 = build_model_merkle_tree;
let tree_v2 = build_model_merkle_tree;
let result = compare_model_trees;
println!;
Merkle proofs
let proof = tree.get_proof;
assert!;
// Proof is portable — verifier needs only the proof + expected root hash
Why this exists
Every existing tool hashes model files as opaque blobs. No tool in any ecosystem provides structure-aware verification at the layer/tensor/chunk level:
| Tool | Knows model structure? | Layer-aware diff? |
|---|---|---|
| HuggingFace Hub / Xet | No | No |
| Sigstore Model Signing | No | No |
| DVC / W&B / MLflow | No | No |
| safetensors / PyTorch | No integrity check | No |
| hanfei-fa | Yes | O(k log C) |
Part of the HanFei (韩非) series
| Project | Role | Install |
|---|---|---|
| hanfei-shu 术 | GPU-accelerated MSM for ZK proofs | cargo add hanfei-shu |
| hanfei-fa 法 (this) | Model weight integrity verification | cargo add hanfei-fa / pip install hanfei-fa |
Contributing
Contributions welcome. Fork, branch, PR. We especially welcome:
- safetensors/GGUF native parsing in Rust
- PyO3 bindings for the Python package's native backend
- WASM compilation for browser-based verification
- Additional model architecture patterns in
extract_layer_name()
Found this useful? Please star the repo and cite:
License
Apache-2.0 — Copyright 2026 Geoffrey Wang