From 8ea1195068d39cba871b0f3babc15b9c90cfb337 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 17 Jun 2025 12:18:16 +0200 Subject: Add test on lib.rs --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/lib.rs | 55 +++++++++++++++++++++++++++++++++++++++++++- tests/pics/cubbit.png.enc.0 | Bin 0 -> 4255 bytes tests/pics/cubbit.png.enc.1 | Bin 0 -> 4255 bytes tests/pics/cubbit.png.enc.2 | Bin 0 -> 4255 bytes 6 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 tests/pics/cubbit.png.enc.0 create mode 100644 tests/pics/cubbit.png.enc.1 create mode 100644 tests/pics/cubbit.png.enc.2 diff --git a/Cargo.lock b/Cargo.lock index 1f281fc..70417cf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -69,7 +69,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa" [[package]] -name = "mt" +name = "mt-rs" version = "0.1.0" dependencies = [ "hex", diff --git a/Cargo.toml b/Cargo.toml index d699a86..a709534 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "mt" +name = "mt-rs" author = "Santo Cariotti " version = "0.1.0" edition = "2024" diff --git a/src/lib.rs b/src/lib.rs index aaae31c..497dbac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,59 @@ //! This library provides modular components to build and verify binary Merkle trees //! with pluggable hash functions. - +//! +//! ## Example: Merkle Tree with File Inputs and Proof Verification +//! +//! ```rust +//! use mt_rs::hasher::SHA256Hasher; +//! use mt_rs::merkletree::MerkleTree; +//! use mt_rs::proof::{DefaultProofer, Proofer}; +//! use std::fs; +//! +//! let hasher = SHA256Hasher::new(); +//! +//! let files = [ +//! fs::read("tests/pics/cubbit.png.enc.0").expect("file 0 not found"), +//! fs::read("tests/pics/cubbit.png.enc.1").expect("file 1 not found"), +//! fs::read("tests/pics/cubbit.png.enc.2").expect("file 2 not found"), +//! ]; +//! +//! let tree = MerkleTree::new(hasher.clone(), files.clone()); +//! +//! assert_eq!(tree.height(), 3); +//! assert_eq!(tree.len(), 4); +//! assert_eq!( +//! tree.root().hash(), +//! "a08c44656fb3f561619b8747a0d1dabe97126d9ed6e0cafbd7ce08ebe12d55ca" +//! ); +//! +//! let proofer = DefaultProofer::new(&hasher, tree.leaves().clone()); +//! +//! let proof = proofer.generate(0).expect("proof generation failed"); +//! +//! assert!(proofer.verify( +//! &proof, +//! &files[0], +//! "a08c44656fb3f561619b8747a0d1dabe97126d9ed6e0cafbd7ce08ebe12d55ca", +//! &hasher +//! )); +//! +//! assert!(!proofer.verify( +//! &proof, +//! &files[0], +//! "a08c44656fb3f561619b87_NOT_VALID_HASH_9ed6e0cafbd7ce08ebe12d55ca", +//! &hasher +//! )); +//! +//! let proof = proofer.generate(1).expect("proof generation failed"); +//! +//! assert!(proofer.verify( +//! &proof, +//! &files[1], +//! "a08c44656fb3f561619b8747a0d1dabe97126d9ed6e0cafbd7ce08ebe12d55ca", +//! &hasher +//! )); +//! +//! ``` pub mod hasher; pub mod merkletree; pub mod node; diff --git a/tests/pics/cubbit.png.enc.0 b/tests/pics/cubbit.png.enc.0 new file mode 100644 index 0000000..c91fc05 Binary files /dev/null and b/tests/pics/cubbit.png.enc.0 differ diff --git a/tests/pics/cubbit.png.enc.1 b/tests/pics/cubbit.png.enc.1 new file mode 100644 index 0000000..afdda5b Binary files /dev/null and b/tests/pics/cubbit.png.enc.1 differ diff --git a/tests/pics/cubbit.png.enc.2 b/tests/pics/cubbit.png.enc.2 new file mode 100644 index 0000000..5221dba Binary files /dev/null and b/tests/pics/cubbit.png.enc.2 differ -- cgit v1.2.3-71-g8e6c