Struct NodeIndex

Source
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

Source

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}.

Source

pub const fn new_unchecked(depth: u8, value: u64) -> NodeIndex

Creates a new node index without checking its validity.

Source

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 a u8.
  • value is greater than or equal to 2^{depth}.
Source

pub const fn root() -> NodeIndex

Creates a new node index pointing to the root of the tree.

Source

pub const fn sibling(self) -> NodeIndex

Computes sibling index of the current node.

Source

pub const fn left_child(self) -> NodeIndex

Returns left child index of the current node.

Source

pub const fn right_child(self) -> NodeIndex

Returns right child index of the current node.

Source

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.

Source

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.

Source

pub const fn to_scalar_index(&self) -> u64

Returns the scalar representation of the depth/value pair.

It is computed as 2^depth + value.

Source

pub const fn depth(&self) -> u8

Returns the depth of the current instance.

Source

pub const fn value(&self) -> u64

Returns the value of this index.

Source

pub const fn is_value_odd(&self) -> bool

Returns true if the current instance points to a right sibling node.

Source

pub const fn is_root(&self) -> bool

Returns true if the depth is 0.

Source

pub fn move_up(&mut self)

Traverses one level towards the root, decrementing the depth by 1.

Source

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.

Source

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 Clone for NodeIndex

Source§

fn clone(&self) -> NodeIndex

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for NodeIndex

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for NodeIndex

Source§

fn default() -> NodeIndex

Returns the “default value” for a type. Read more
Source§

impl Deserializable for NodeIndex

Source§

fn read_from<R>(source: &mut R) -> Result<NodeIndex, DeserializationError>
where R: ByteReader,

Reads a sequence of bytes from the provided source, attempts to deserialize these bytes into Self, and returns the result. Read more
Source§

fn read_from_bytes(bytes: &[u8]) -> Result<Self, DeserializationError>

Attempts to deserialize the provided bytes into Self and returns the result. Read more
Source§

impl Display for NodeIndex

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<const DEPTH: u8> From<LeafIndex<DEPTH>> for NodeIndex

Source§

fn from(value: LeafIndex<DEPTH>) -> NodeIndex

Converts to this type from the input type.
Source§

impl Hash for NodeIndex

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for NodeIndex

Source§

fn cmp(&self, other: &NodeIndex) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for NodeIndex

Source§

fn eq(&self, other: &NodeIndex) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for NodeIndex

Source§

fn partial_cmp(&self, other: &NodeIndex) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serializable for NodeIndex

Source§

fn write_into<W>(&self, target: &mut W)
where W: ByteWriter,

Serializes self into bytes and writes these bytes into the target.
Source§

fn to_bytes(&self) -> Vec<u8>

Serializes self into a vector of bytes.
Source§

fn get_size_hint(&self) -> usize

Returns an estimate of how many bytes are needed to represent self. Read more
Source§

impl<const DEPTH: u8> TryFrom<NodeIndex> for LeafIndex<DEPTH>

Source§

type Error = MerkleError

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

fn try_from( node_index: NodeIndex, ) -> Result<LeafIndex<DEPTH>, <LeafIndex<DEPTH> as TryFrom<NodeIndex>>::Error>

Performs the conversion.
Source§

impl Copy for NodeIndex

Source§

impl Eq for NodeIndex

Source§

impl StructuralPartialEq for NodeIndex

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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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