ergo_merkle_tree/
lib.rs

1//! Ergo Merkle Tree and Merkle verification tools
2
3// Coding conventions
4#![forbid(unsafe_code)]
5#![deny(non_upper_case_globals)]
6#![deny(non_camel_case_types)]
7#![deny(non_snake_case)]
8#![deny(unused_mut)]
9#![deny(dead_code)]
10#![deny(unused_imports)]
11#![deny(missing_docs)]
12// Clippy exclusions
13#![allow(clippy::unit_arg)]
14#![deny(rustdoc::broken_intra_doc_links)]
15#![deny(clippy::unwrap_used)]
16#![deny(clippy::expect_used)]
17#![deny(clippy::todo)]
18#![deny(clippy::unimplemented)]
19#![deny(clippy::panic)]
20
21use ergo_chain_types::Digest32;
22
23// Constants for hashing
24/// Hash size for all nodes in [`crate::MerkleTree`], [`crate::MerkleProof`] and [`crate::BatchMerkleProof`]
25pub const HASH_SIZE: usize = 32;
26pub(crate) const INTERNAL_PREFIX: u8 = 1;
27pub(crate) const LEAF_PREFIX: u8 = 0;
28
29#[cfg(feature = "json")]
30pub(crate) mod json;
31
32// Unwrap is safe here since the hash is guaranteed to be 32 bytes
33#[allow(clippy::unwrap_used)]
34// Generates a hash of data prefixed with `prefix`
35pub(crate) fn prefixed_hash(prefix: u8, data: &[u8]) -> Digest32 {
36    let mut bytes = vec![prefix];
37    bytes.extend_from_slice(data);
38    let hash = blake2b256_hash(bytes.as_slice());
39    Digest32::from(hash)
40}
41
42#[allow(clippy::unwrap_used)]
43// Generates a hash of data prefixed with `prefix`, allows for an optional second hash
44pub(crate) fn prefixed_hash2<'a>(
45    prefix: u8,
46    data: impl Into<Option<&'a [u8]>>,
47    data2: impl Into<Option<&'a [u8]>>,
48) -> Digest32 {
49    let mut bytes = vec![prefix];
50    if let Some(data) = data.into() {
51        bytes.extend_from_slice(data);
52    }
53    if let Some(data2) = data2.into() {
54        bytes.extend_from_slice(data2);
55    };
56    let hash = blake2b256_hash(bytes.as_slice());
57    Digest32::from(hash)
58}
59
60mod batchmerkleproof;
61mod merkleproof;
62mod merkletree;
63
64pub use batchmerkleproof::BatchMerkleProof;
65pub use merkleproof::*;
66pub use merkletree::*;
67use sigma_util::hash::blake2b256_hash;