pub struct NodeIndex { /* private fields */ }
Expand description
Address to an arbitrary node in a binary tree using level order form.
The position is represented by the pair (depth, pos)
, where for a given depth d
elements
are numbered from $0..(2^d)-1$. Example:
depth
0 0
1 0 1
2 0 1 2 3
3 0 1 2 3 4 5 6 7
The root is represented by the pair $(0, 0)$, its left child is $(1, 0)$ and its right child $(1, 1)$.
Implementations§
Source§impl NodeIndex
impl NodeIndex
Sourcepub const fn new(depth: u8, value: u64) -> Result<NodeIndex, MerkleError>
pub const fn new(depth: u8, value: u64) -> Result<NodeIndex, MerkleError>
Creates a new node index.
§Errors
Returns an error if the value
is greater than or equal to 2^{depth}.
Sourcepub const fn new_unchecked(depth: u8, value: u64) -> NodeIndex
pub const fn new_unchecked(depth: u8, value: u64) -> NodeIndex
Creates a new node index without checking its validity.
Sourcepub fn from_elements(
depth: &BaseElement,
value: &BaseElement,
) -> Result<NodeIndex, MerkleError>
pub fn from_elements( depth: &BaseElement, value: &BaseElement, ) -> Result<NodeIndex, MerkleError>
Creates a node index from a pair of field elements representing the depth and value.
§Errors
Returns an error if:
depth
doesn’t fit in au8
.value
is greater than or equal to 2^{depth}.
Sourcepub const fn left_child(self) -> NodeIndex
pub const fn left_child(self) -> NodeIndex
Returns left child index of the current node.
Sourcepub const fn right_child(self) -> NodeIndex
pub const fn right_child(self) -> NodeIndex
Returns right child index of the current node.
Sourcepub const fn parent(self) -> NodeIndex
pub const fn parent(self) -> NodeIndex
Returns the parent of the current node. This is the same as Self::move_up()
, but returns
a new value instead of mutating self
.
Sourcepub const fn build_node(&self, slf: Word, sibling: Word) -> [Word; 2]
pub const fn build_node(&self, slf: Word, sibling: Word) -> [Word; 2]
Builds a node to be used as input of a hash function when computing a Merkle path.
Will evaluate the parity of the current instance to define the result.
Sourcepub const fn to_scalar_index(&self) -> u64
pub const fn to_scalar_index(&self) -> u64
Returns the scalar representation of the depth/value pair.
It is computed as 2^depth + value
.
Sourcepub const fn is_value_odd(&self) -> bool
pub const fn is_value_odd(&self) -> bool
Returns true
if the current instance points to a right sibling node.
Sourcepub fn move_up_to(&mut self, depth: u8)
pub fn move_up_to(&mut self, depth: u8)
Traverses towards the root until the specified depth is reached.
Assumes that the specified depth is smaller than the current depth.
Sourcepub fn proof_indices(&self) -> impl ExactSizeIterator + use<>
pub fn proof_indices(&self) -> impl ExactSizeIterator + use<>
Return an iterator of the indices required for a Merkle proof of inclusion of a node at
self
.
This is exclusive on both ends: neither self
nor the root index are included in the
returned iterator.
Trait Implementations§
Source§impl Deserializable for NodeIndex
impl Deserializable for NodeIndex
Source§fn read_from<R>(source: &mut R) -> Result<NodeIndex, DeserializationError>where
R: ByteReader,
fn read_from<R>(source: &mut R) -> Result<NodeIndex, DeserializationError>where
R: ByteReader,
source
, attempts to deserialize these bytes
into Self
, and returns the result. Read moreSource§fn read_from_bytes(bytes: &[u8]) -> Result<Self, DeserializationError>
fn read_from_bytes(bytes: &[u8]) -> Result<Self, DeserializationError>
Source§impl Ord for NodeIndex
impl Ord for NodeIndex
Source§impl PartialOrd for NodeIndex
impl PartialOrd for NodeIndex
Source§impl Serializable for NodeIndex
impl Serializable for NodeIndex
Source§fn write_into<W>(&self, target: &mut W)where
W: ByteWriter,
fn write_into<W>(&self, target: &mut W)where
W: ByteWriter,
self
into bytes and writes these bytes into the target
.