Skip to main content

MerkleTree

Struct MerkleTree 

Source
pub struct MerkleTree { /* private fields */ }
Expand description

In-memory Merkle tree over the responder’s claimed keys.

Leaves are BLAKE3(DOMAIN_LEAF || key || BLAKE3(bytes)), sorted by key. Internal nodes are BLAKE3(DOMAIN_NODE || left || right). When a level has an odd number of nodes, the last node is paired with itself — i.e. node_hash(x, x) — so the level above has ceil(n/2) nodes. This is a standard self-pair construction (NOT node promotion) and deterministically maps any non-empty key set to a single root.

Rebuilt by the responder whenever its key set changes meaningfully (debounced in the integration layer; not this module’s concern).

Implementations§

Source§

impl MerkleTree

Source

pub fn build(entries: Vec<(XorName, [u8; 32])>) -> Result<Self, CommitmentError>

Build a Merkle tree over (key, bytes_hash) pairs.

entries does not need to be sorted; this method sorts internally so the produced root is deterministic per key set. Duplicate keys are an error: the responder must deduplicate before calling.

§Errors

Returns an error if entries is empty (no commitment to make), if entries.len() > MAX_COMMITMENT_KEY_COUNT, or if it contains duplicate keys.

Source

pub fn root(&self) -> [u8; 32]

The Merkle root of this tree.

unwrap-free: build guarantees at least one level with at least one entry, so last().first() is always Some.

Source

pub fn key_count(&self) -> u32

The number of leaves (== claimed keys).

Source

pub fn path_for(&self, key: &XorName) -> Option<Vec<[u8; 32]>>

Inclusion path for key from its leaf up to (but not including) the root.

Returns None if key is not in this tree.

Source

pub fn sorted_keys(&self) -> Vec<XorName>

The keys this tree commits to, in sorted order.

sorted_keys()[i] is the key at leaf index i. Used by the responder’s audit-answer path to recover the leaf_index field for a challenged key in O(log n) via binary search.

Source

pub fn key_at(&self, idx: usize) -> Option<XorName>

The key at sorted leaf index idx, if in range.

Used by the subtree-proof builder to enumerate the keys of a contiguous leaf range without cloning the whole key list.

Source

pub fn key_index(&self, key: &XorName) -> Option<usize>

The sorted leaf index of key, if committed. O(log n) binary search over the (key-sorted) leaves — no separate key list needed, so callers don’t have to keep a duplicate sorted_keys Vec alongside the tree.

Source

pub fn contains_key(&self, key: &XorName) -> bool

Whether key is committed. Allocation-free membership check via the same binary search as Self::key_index.

Source

pub fn node_at(&self, level: usize, index: u64) -> Option<[u8; 32]>

The node hash at (level, index), where level counts up from the leaves (level == 0 is the leaf level, the last level is the root).

Returns None if out of range. Used by the subtree-proof builder to read sibling cut-hashes along the path from the root to the selected subtree; honours the same left-packed self-pair construction as the rest of the tree (a caller asking for an out-of-range sibling on an odd-length level should substitute the node itself).

Source

pub fn levels_count(&self) -> usize

The number of levels in the tree (1 for a single-leaf tree; the last index is the root level). depth == levels_count() - 1.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more