//! BLAKE3-based Merkle tree for append-only assertion diff detection.
//!
//! This crate provides an efficient Merkle tree implementation optimized for
//! StemeDB's append-only assertion store. The primary use case is incremental
//! sync between nodes: quickly identify which assertions differ between local
//! and remote stores.
//!
//! # Design Philosophy
//!
//! - **Append-Only**: Trees grow monotonically, optimized for O(log N) inserts
//! - **Content-Addressed**: Uses BLAKE3 for cryptographic hash verification
//! - **Efficient Diff**: O(log N) comparison to identify divergent subtrees
//! - **Zero-Copy Serialization**: Uses rkyv for fast network transfer
//!
//! # Architecture
//!
//! The tree is a binary Merkle tree where:
//! - Leaves contain assertion hashes (BLAKE3 digests)
//! - Internal nodes contain BLAKE3(left_hash || right_hash)
//! - Root hash represents the entire assertion set
//!
//! # Example
//!
//! ```
//! use stemedb_merkle::MerkleTree;
//!
//! // Create a tree and insert assertion hashes
//! let mut tree = MerkleTree::new();
//! tree.insert([1u8; 32]).expect("insert failed");
//! tree.insert([2u8; 32]).expect("insert failed");
//!
//! // Get the root hash (identifies the entire tree)
//! let root = tree.root().expect("empty tree");
//! assert_eq!(tree.len(), 2);
//!
//! // Compare with another tree
//! let mut other = MerkleTree::new();
//! other.insert([1u8; 32]).expect("insert failed");
//! other.insert([3u8; 32]).expect("insert failed");
//!
//! // Roots differ because trees contain different assertions
//! assert_ne!(tree.root().expect("root"), other.root().expect("root"));
//! ```
//!
//! # Performance Characteristics
//!
//! - Insert: O(log N) - recompute path from leaf to root
//! - Root: O(1) - cached after each insert
//! - Diff: O(log N) - compare subtree hashes to find divergence
//! - Serialize: O(N) - write all nodes to bytes
//!
//! # Crash Recovery
//!
//! The tree can be serialized to disk and restored after crash. Combined with
//! StemeDB's WAL, this enables fast reconstruction of the tree state without
//! replaying all assertions.

#![forbid(unsafe_code)]
#![warn(missing_docs)]

mod diff;
pub mod serialize;
mod tree;

pub use diff::{roots_equal, DiffResult};
pub use serialize::SerializeError;
pub use tree::{Hash, MerkleTree, TreeError};
