Skip to main content

Crate hanfei_fa

Crate hanfei_fa 

Source
Expand description

§hanfei-fa 法

ML model weight integrity verification via hierarchical Merkle trees.

法不阿贵,绳不挠曲。 The law does not favor the noble; the plumb line does not bend for the crooked.

§Features

  • 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 — O(chunk_size) memory for multi-GB files

§Quick Start

use hanfei_fa::{MerkleTree, HashAlgorithm, compute_hash, chunk_bytes};

let data = b"model weight bytes here...";
let chunks = chunk_bytes(data, 16384);
let hashes: Vec<String> = chunks.iter().map(|c| compute_hash(c, HashAlgorithm::Sha256)).collect();
let tree = MerkleTree::new(hashes, 16384);
println!("root: {}", tree.root_hash());

Part of the HanFei (韩非) verifiable AI series.

Re-exports§

pub use hasher::HashAlgorithm;
pub use hasher::compute_hash;
pub use hasher::hash_pair;
pub use hasher::verify_hash;
pub use chunking::chunk_bytes;
pub use chunking::chunk_bytes_iter;
pub use chunking::estimate_chunk_count;
pub use chunking::DEFAULT_CHUNK_SIZE;
pub use merkle_tree::MerkleNode;
pub use merkle_tree::MerkleProof;
pub use merkle_tree::MerkleTree;
pub use merkle_tree::LayerMerkleTree;
pub use merkle_tree::ModelMerkleTree;
pub use merkle_tree::build_model_merkle_tree;
pub use comparison::ComparisonResult;
pub use comparison::compare_model_trees;
pub use comparison::estimate_sync_savings;
pub use strategies::ChunkStrategy;
pub use strategies::FixedChunkStrategy;
pub use strategies::AdaptiveChunkStrategy;

Modules§

chunking
comparison
hasher
merkle_tree
strategies