Skip to main content

nectar_primitives/bmt/
constants.rs

1//! Constants used in the Binary Merkle Tree implementation
2
3/// Hash size in bytes (keccak256).
4pub const HASH_SIZE: usize = 32;
5
6/// Size of a segment in the BMT (same as hash size).
7pub(crate) const SEGMENT_SIZE: usize = HASH_SIZE;
8
9/// Log2 of segment size for bit shifting.
10pub(crate) const SEGMENT_SIZE_LOG2: usize = 5; // 32 = 2^5
11
12/// Length of a segment pair (two segments).
13pub(crate) const SEGMENT_PAIR_LENGTH: usize = 2 * SEGMENT_SIZE;
14
15/// Number of branches in the Binary Merkle Tree.
16pub const BRANCHES: usize = 128;
17
18/// Default body size for chunks (128 branches * 32 byte segments = 4096).
19pub const DEFAULT_BODY_SIZE: usize = BRANCHES * SEGMENT_SIZE;
20
21/// Span header size in bytes (u64).
22pub const SPAN_SIZE: usize = std::mem::size_of::<u64>();
23
24/// Proof length in segments (log2(128) = 7).
25pub(crate) const PROOF_LENGTH: usize = 7;
26
27/// Compute number of zero tree levels for a given body size.
28#[inline]
29pub(crate) const fn zero_tree_levels(body_size: usize) -> usize {
30    (body_size / SEGMENT_PAIR_LENGTH).trailing_zeros() as usize + 1
31}
32
33/// Compute number of branches for a given body size.
34#[inline]
35pub(crate) const fn branches_for_body_size(body_size: usize) -> usize {
36    body_size / SEGMENT_SIZE
37}