nam_sparse_merkle_tree/lib.rs
1//! Constructs a new `SparseMerkleTree<H, V, S>`.
2//!
3//! # Examples
4//!
5//! ```
6//! use nam_sparse_merkle_tree::{
7//! blake2b::Blake2bHasher, default_store::DefaultStore,
8//! error::Error, MerkleProof,
9//! SparseMerkleTree, traits::Value, H256, Hash,
10//! };
11//! use blake2b_rs::{Blake2b, Blake2bBuilder};
12//!
13//! // define SMT
14//! type SMT = SparseMerkleTree<Blake2bHasher, Hash, Word, DefaultStore<Hash, Word, 32>, 32>;
15//!
16//! // define SMT value
17//! #[derive(Default, Clone, PartialEq)]
18//! pub struct Word(String, H256);
19//! impl Value for Word {
20//! fn as_slice(&self) -> &[u8] {
21//! self.1.as_slice()
22//! }
23//! fn zero() -> Self {
24//! Default::default()
25//! }
26//! }
27//!
28//! // helper function
29//! fn new_blake2b() -> Blake2b {
30//! Blake2bBuilder::new(32).personal(b"SMT").build()
31//! }
32//!
33//! fn construct_smt() {
34//! let mut tree = SMT::default();
35//! for (i, word) in "The quick brown fox jumps over the lazy dog"
36//! .split_whitespace()
37//! .enumerate()
38//! {
39//! let key: Hash = {
40//! let mut buf = [0u8; 32];
41//! let mut hasher = new_blake2b();
42//! hasher.update(&(i as u32).to_le_bytes());
43//! hasher.finalize(&mut buf);
44//! buf.into()
45//! };
46//! let hash: H256 = {
47//! let mut buf = [0u8; 32];
48//! if !word.is_empty() {
49//! let mut hasher = new_blake2b();
50//! hasher.update(word.as_bytes());
51//! hasher.finalize(&mut buf);
52//! }
53//! buf.into()
54//! };
55//! let value = Word(word.to_string(), hash);
56//! // insert key value into tree
57//! tree.update(key, value).expect("update");
58//! }
59//!
60//! println!("SMT root is {:?} ", tree.root());
61//! }
62//! ```
63
64#![cfg_attr(not(feature = "std"), no_std)]
65
66#[cfg(feature = "blake2b")]
67pub mod blake2b;
68pub mod default_store;
69pub mod error;
70pub mod h256;
71pub mod internal_key;
72pub mod merge;
73pub mod merkle_proof;
74pub mod proof_ics23;
75pub mod sha256;
76#[cfg(test)]
77mod tests;
78pub mod traits;
79pub mod tree;
80
81pub use h256::{Hash, H256};
82pub use internal_key::InternalKey;
83pub use merkle_proof::{CompiledMerkleProof, MerkleProof};
84pub use traits::Key;
85pub use tree::SparseMerkleTree;
86
87/// Expected path size: log2(256) * 2, used for hint vector capacity
88pub const EXPECTED_PATH_SIZE: usize = 16;
89/// Height of sparse merkle tree
90pub const TREE_HEIGHT: usize = 256;
91/// Key limit size
92pub const KEY_LIMIT: usize = 4_294_967_295u32 as usize;
93
94cfg_if::cfg_if! {
95 if #[cfg(feature = "std")] {
96 use std::collections;
97 use std::vec;
98 use std::string;
99 } else {
100 extern crate alloc;
101 use alloc::collections;
102 use alloc::vec;
103 use alloc::string;
104 }
105}