pub struct TreeID(/* private fields */);
Expand description
Unique identifiers for binary tree nodes. Reproduced from flat-tree.
Implementations§
Source§impl TreeID
impl TreeID
Sourcepub const fn new(height: u8, index: u64) -> Self
pub const fn new(height: u8, index: u64) -> Self
Returns a node’s unique id within the tree, given its height and index.
§Examples
use merkle_log::TreeID;
assert_eq!(TreeID::new(0, 0), TreeID::from(0));
assert_eq!(TreeID::new(0, 1), TreeID::from(2));
assert_eq!(TreeID::new(0, 2), TreeID::from(4));
assert_eq!(TreeID::new(1, 0), TreeID::from(1));
assert_eq!(TreeID::new(1, 1), TreeID::from(5));
assert_eq!(TreeID::new(1, 2), TreeID::from(9));
assert_eq!(TreeID::new(1, 3), TreeID::from(13));
assert_eq!(TreeID::new(2, 0), TreeID::from(3));
assert_eq!(TreeID::new(2, 1), TreeID::from(11));
assert_eq!(TreeID::new(2, 2), TreeID::from(19));
assert_eq!(TreeID::new(3, 0), TreeID::from(7));
assert_eq!(TreeID::new(3, 1), TreeID::from(23));
Sourcepub const fn first(height: u8) -> Self
pub const fn first(height: u8) -> Self
Returns the first node’s unique id at a given height.
§Examples
use merkle_log::TreeID;
assert_eq!(TreeID::first(0), TreeID::from(0));
assert_eq!(TreeID::first(1), TreeID::from(1));
assert_eq!(TreeID::first(2), TreeID::from(3));
// test roots
assert_eq!(TreeID::first(62), TreeID::ROOT.left().unwrap());
assert_eq!(TreeID::first(TreeID::MAX_HEIGHT), TreeID::ROOT);
Sourcepub const fn last(height: u8) -> Self
pub const fn last(height: u8) -> Self
Returns the last node’s unique id at a given height.
§Examples
use merkle_log::TreeID;
assert_eq!(TreeID::last(0), TreeID::MAX_LEAF);
assert_eq!(TreeID::last(1), TreeID::MAX_LEAF.parent());
assert_eq!(TreeID::last(2), TreeID::MAX_LEAF.parent().parent());
// test roots
assert_eq!(TreeID::last(62), TreeID::ROOT.right().unwrap());
assert_eq!(TreeID::last(TreeID::MAX_HEIGHT), TreeID::ROOT);
Sourcepub const fn leaf(index: u64) -> Self
pub const fn leaf(index: u64) -> Self
Returns a leaf node’s unique id at a given index.
§Examples
use merkle_log::TreeID;
assert_eq!(TreeID::leaf(0), TreeID::from(0));
assert_eq!(TreeID::leaf(1), TreeID::from(2));
assert_eq!(TreeID::leaf(2), TreeID::from(4));
assert_eq!(TreeID::leaf(3), TreeID::from(6));
Sourcepub const fn is_leaf(&self) -> bool
pub const fn is_leaf(&self) -> bool
Determines if the id represents a leaf node.
§Examples
use merkle_log::TreeID;
assert_eq!(TreeID::from(0).is_leaf(), true);
assert_eq!(TreeID::from(1).is_leaf(), false);
assert_eq!(TreeID::from(2).is_leaf(), true);
assert_eq!(TreeID::from(3).is_leaf(), false);
Sourcepub const fn is_left(&self) -> bool
pub const fn is_left(&self) -> bool
Determines if the id represents a left node of its parent.
§Examples
use merkle_log::TreeID;
assert_eq!(TreeID::from(0).is_left(), true);
assert_eq!(TreeID::from(1).is_left(), true);
assert_eq!(TreeID::from(2).is_left(), false);
assert_eq!(TreeID::from(3).is_left(), true);
assert_eq!(TreeID::from(4).is_left(), true);
assert_eq!(TreeID::from(5).is_left(), false);
assert_eq!(TreeID::from(6).is_left(), false);
// test root
assert_eq!(TreeID::ROOT.is_left(), true);
Sourcepub const fn is_right(&self) -> bool
pub const fn is_right(&self) -> bool
Determines if the id represents a right node of its parent.
§Examples
use merkle_log::TreeID;
assert_eq!(TreeID::from(0).is_right(), false);
assert_eq!(TreeID::from(1).is_right(), false);
assert_eq!(TreeID::from(2).is_right(), true);
assert_eq!(TreeID::from(3).is_right(), false);
assert_eq!(TreeID::from(4).is_right(), false);
assert_eq!(TreeID::from(5).is_right(), true);
assert_eq!(TreeID::from(6).is_right(), true);
// test root
assert_eq!(TreeID::ROOT.is_right(), false);
Sourcepub const fn is_left_of(&self, other: &Self) -> bool
pub const fn is_left_of(&self, other: &Self) -> bool
Determines if the id represents a left node of its parent.
§Examples
use merkle_log::TreeID;
assert_eq!(TreeID::from(0).is_left_of(&TreeID::from(0)), false);
assert_eq!(TreeID::from(0).is_left_of(&TreeID::from(1)), true);
assert_eq!(TreeID::from(2).is_left_of(&TreeID::from(1)), false);
assert_eq!(TreeID::from(1).is_left_of(&TreeID::from(2)), true);
Sourcepub const fn is_right_of(&self, other: &Self) -> bool
pub const fn is_right_of(&self, other: &Self) -> bool
Determines if the id represents a right node of its parent.
§Examples
use merkle_log::TreeID;
assert_eq!(TreeID::from(0).is_right_of(&TreeID::from(0)), false);
assert_eq!(TreeID::from(0).is_right_of(&TreeID::from(1)), false);
assert_eq!(TreeID::from(2).is_right_of(&TreeID::from(1)), true);
assert_eq!(TreeID::from(1).is_right_of(&TreeID::from(2)), false);
Sourcepub const fn is_first(self) -> bool
pub const fn is_first(self) -> bool
Determines if the id is the first among nodes of the same height.
§Examples
use merkle_log::TreeID;
assert_eq!(TreeID::from(0).is_first(), true);
assert_eq!(TreeID::from(1).is_first(), true);
assert_eq!(TreeID::from(2).is_first(), false);
assert_eq!(TreeID::from(3).is_first(), true);
assert_eq!(TreeID::from(4).is_first(), false);
assert_eq!(TreeID::from(5).is_first(), false);
assert_eq!(TreeID::from(6).is_first(), false);
assert_eq!(TreeID::from(7).is_first(), true);
// test root
assert_eq!(TreeID::ROOT.is_first(), true);
Sourcepub const fn is_last(self) -> bool
pub const fn is_last(self) -> bool
Determines if the id is the last among nodes of the same height.
§Examples
use merkle_log::TreeID;
assert_eq!(TreeID::MAX_LEAF.is_last(), true);
assert_eq!(TreeID::MAX_LEAF.parent().is_last(), true);
assert_eq!(TreeID::MAX_LEAF.sibling().is_last(), false);
assert_eq!(TreeID::MAX_LEAF.parent().sibling().is_last(), false);
// test root
assert_eq!(TreeID::ROOT.is_last(), true);
Sourcepub const fn index(&self) -> u64
pub const fn index(&self) -> u64
Returns a node’s index among nodes of the same height.
§Examples
use merkle_log::TreeID;
assert_eq!(TreeID::from(0).index(), 0);
assert_eq!(TreeID::from(1).index(), 0);
assert_eq!(TreeID::from(2).index(), 1);
assert_eq!(TreeID::from(3).index(), 0);
assert_eq!(TreeID::from(4).index(), 2);
assert_eq!(TreeID::from(5).index(), 1);
assert_eq!(TreeID::from(6).index(), 3);
assert_eq!(TreeID::from(7).index(), 0);
assert_eq!(TreeID::from(8).index(), 4);
assert_eq!(TreeID::from(9).index(), 2);
assert_eq!(TreeID::from(10).index(), 5);
// test root
assert_eq!(TreeID::ROOT.index(), 0);
Sourcepub const fn height(&self) -> u8
pub const fn height(&self) -> u8
Returns a node’s height in the tree.
§Examples
use merkle_log::TreeID;
assert_eq!(TreeID::from(0).height(), 0);
assert_eq!(TreeID::from(1).height(), 1);
assert_eq!(TreeID::from(2).height(), 0);
assert_eq!(TreeID::from(3).height(), 2);
assert_eq!(TreeID::from(4).height(), 0);
// test root
assert_eq!(TreeID::ROOT.height(), TreeID::MAX_HEIGHT);
Sourcepub const fn size(&self) -> u64
pub const fn size(&self) -> u64
Returns the total number of nodes the node spans.
§Examples
use merkle_log::TreeID;
assert_eq!(TreeID::from(0).size(), 1);
assert_eq!(TreeID::from(2).size(), 1);
assert_eq!(TreeID::from(1).size(), 3);
assert_eq!(TreeID::from(3).size(), 7);
assert_eq!(TreeID::from(7).size(), 15);
// test root
assert_eq!(TreeID::ROOT.size(), u64::MAX);
Sourcepub const fn num_leaves(&self) -> u64
pub const fn num_leaves(&self) -> u64
Returns the number of leaf nodes the node spans.
§Examples
use merkle_log::TreeID;
assert_eq!(TreeID::from(0).num_leaves(), 0);
assert_eq!(TreeID::from(2).num_leaves(), 0);
assert_eq!(TreeID::from(1).num_leaves(), 2);
assert_eq!(TreeID::from(5).num_leaves(), 2);
assert_eq!(TreeID::from(3).num_leaves(), 4);
assert_eq!(TreeID::from(7).num_leaves(), 8);
// test root
assert_eq!(TreeID::ROOT.num_leaves(), TreeID::MAX_LEAF.index() + 1);
Sourcepub const fn span(&self) -> (Self, Self)
pub const fn span(&self) -> (Self, Self)
Returns the left- and right-most node ids in the tree the node spans.
§Examples
use merkle_log::TreeID;
assert_eq!(TreeID::from(0).span(), (TreeID::from(0), TreeID::from(0)));
assert_eq!(TreeID::from(2).span(), (TreeID::from(2), TreeID::from(2)));
assert_eq!(TreeID::from(1).span(), (TreeID::from(0), TreeID::from(2)));
assert_eq!(TreeID::from(3).span(), (TreeID::from(0), TreeID::from(6)));
assert_eq!(TreeID::from(23).span(), (TreeID::from(16), TreeID::from(30)));
assert_eq!(TreeID::from(27).span(), (TreeID::from(24), TreeID::from(30)));
// test root
assert_eq!(TreeID::ROOT.span(), (TreeID::MIN_LEAF, TreeID::MAX_LEAF));
Sourcepub const fn spans(&self, other: &Self) -> bool
pub const fn spans(&self, other: &Self) -> bool
Determines if the id’s tree spans (i.e. contains) another id.
§Examples
use merkle_log::TreeID;
assert_eq!(TreeID::from(0).spans(&TreeID::from(0)), true);
assert_eq!(TreeID::from(0).spans(&TreeID::from(1)), false);
assert_eq!(TreeID::from(0).spans(&TreeID::from(2)), false);
assert_eq!(TreeID::from(1).spans(&TreeID::from(0)), true);
assert_eq!(TreeID::from(1).spans(&TreeID::from(1)), true);
assert_eq!(TreeID::from(1).spans(&TreeID::from(2)), true);
assert_eq!(TreeID::from(3).spans(&TreeID::from(1)), true);
assert_eq!(TreeID::from(3).spans(&TreeID::from(5)), true);
assert_eq!(TreeID::from(3).spans(&TreeID::from(7)), false);
// test root
assert_eq!(TreeID::ROOT.spans(&TreeID::MIN_LEAF), true);
assert_eq!(TreeID::ROOT.spans(&TreeID::MAX_LEAF), true);
Sourcepub const fn root_id(&self) -> Self
pub const fn root_id(&self) -> Self
Returns the lowest root id of a [MerkleLog
] that contains this node.
§Examples
use merkle_log::TreeID;
assert_eq!(TreeID::from(0).root_id(), TreeID::from(0));
assert_eq!(TreeID::from(1).root_id(), TreeID::from(1));
assert_eq!(TreeID::from(2).root_id(), TreeID::from(1));
assert_eq!(TreeID::from(3).root_id(), TreeID::from(3));
assert_eq!(TreeID::from(4).root_id(), TreeID::from(3));
assert_eq!(TreeID::from(5).root_id(), TreeID::from(3));
assert_eq!(TreeID::from(6).root_id(), TreeID::from(3));
assert_eq!(TreeID::from(7).root_id(), TreeID::from(7));
assert_eq!(TreeID::from(8).root_id(), TreeID::from(7));
assert_eq!(TreeID::from(9).root_id(), TreeID::from(7));
// test root
assert_eq!(TreeID::ROOT.root_id(), TreeID::ROOT);
Sourcepub const fn sort_index(&self) -> u64
pub const fn sort_index(&self) -> u64
Returns a node’s sort index, i.e. the index in a list sorted by when the node completes a subtree and becomes immutable.
§Examples
use merkle_log::TreeID;
assert_eq!(TreeID::from(0).sort_index(), 0);
assert_eq!(TreeID::from(2).sort_index(), 1);
assert_eq!(TreeID::from(1).sort_index(), 2);
assert_eq!(TreeID::from(4).sort_index(), 3);
assert_eq!(TreeID::from(6).sort_index(), 4);
assert_eq!(TreeID::from(5).sort_index(), 5);
assert_eq!(TreeID::from(3).sort_index(), 6);
assert_eq!(TreeID::from(8).sort_index(), 7);
assert_eq!(TreeID::from(10).sort_index(), 8);
assert_eq!(TreeID::from(9).sort_index(), 9);
assert_eq!(TreeID::from(12).sort_index(), 10);
assert_eq!(TreeID::from(14).sort_index(), 11);
assert_eq!(TreeID::from(13).sort_index(), 12);
assert_eq!(TreeID::from(11).sort_index(), 13);
assert_eq!(TreeID::from(7).sort_index(), 14);
// check final nodes
assert_eq!(TreeID::MAX_LEAF.sort_index(), TreeID::MAX_SORT_INDEX - TreeID::MAX_HEIGHT as u64);
assert_eq!(TreeID::ROOT.sort_index(), TreeID::MAX_SORT_INDEX);
Sourcepub const fn sibling(&self) -> Self
pub const fn sibling(&self) -> Self
Returns the sibling id of a node.
§Examples
use merkle_log::TreeID;
assert_eq!(TreeID::from(0).sibling(), TreeID::from(2));
assert_eq!(TreeID::from(2).sibling(), TreeID::from(0));
assert_eq!(TreeID::from(1).sibling(), TreeID::from(5));
assert_eq!(TreeID::from(5).sibling(), TreeID::from(1));
// test root
// assert_eq!(TreeID::ROOT.sibling(), TreeID::ROOT);
Sourcepub const fn parent(&self) -> Self
pub const fn parent(&self) -> Self
Returns the parent id of a node.
§Examples
use merkle_log::TreeID;
assert_eq!(TreeID::from(0).parent(), TreeID::from(1));
assert_eq!(TreeID::from(1).parent(), TreeID::from(3));
assert_eq!(TreeID::from(2).parent(), TreeID::from(1));
assert_eq!(TreeID::from(3).parent(), TreeID::from(7));
assert_eq!(TreeID::from(4).parent(), TreeID::from(5));
assert_eq!(TreeID::from(5).parent(), TreeID::from(3));
assert_eq!(TreeID::from(6).parent(), TreeID::from(5));
assert_eq!(TreeID::from(7).parent(), TreeID::from(15));
assert_eq!(TreeID::from(8).parent(), TreeID::from(9));
// test root
// assert_eq!(TreeID::ROOT.sibling(), TreeID::ROOT);
Sourcepub const fn uncle(&self) -> Self
pub const fn uncle(&self) -> Self
Given a node, returns its parent’s sibling’s id.
§Examples
use merkle_log::TreeID;
assert_eq!(TreeID::from(0).uncle(), TreeID::from(5));
assert_eq!(TreeID::from(2).uncle(), TreeID::from(5));
assert_eq!(TreeID::from(1).uncle(), TreeID::from(11));
assert_eq!(TreeID::from(5).uncle(), TreeID::from(11));
assert_eq!(TreeID::from(9).uncle(), TreeID::from(3));
// test root
// assert_eq!(TreeID::ROOT.sibling(), TreeID::ROOT);
Sourcepub const fn left(&self) -> Option<Self>
pub const fn left(&self) -> Option<Self>
Returns the id of the node’s left child.
§Examples
use merkle_log::TreeID;
assert_eq!(TreeID::from(0).left(), None);
assert_eq!(TreeID::from(1).left(), Some(TreeID::from(0)));
assert_eq!(TreeID::from(3).left(), Some(TreeID::from(1)));
Sourcepub const fn right(&self) -> Option<Self>
pub const fn right(&self) -> Option<Self>
Returns the id of the node’s right child.
§Examples
use merkle_log::TreeID;
assert_eq!(TreeID::from(0).right(), None);
assert_eq!(TreeID::from(1).right(), Some(TreeID::from(2)));
assert_eq!(TreeID::from(3).right(), Some(TreeID::from(5)));
Sourcepub fn proving_ids(&self, to_height: u8) -> impl Iterator<Item = Self>
pub fn proving_ids(&self, to_height: u8) -> impl Iterator<Item = Self>
Given the id of a node in a balanced tree, produce the ids of nodes required for a traditional merkle tree proof, excluding the (sub)root.
§Examples
use std::collections::BTreeSet;
use merkle_log::*;
assert_eq!(TreeID::from(0).proving_ids(0).collect::<Vec<_>>(), &[]);
assert_eq!(TreeID::from(0).proving_ids(1).collect::<Vec<_>>(), &[TreeID::from(2)]);
assert_eq!(TreeID::from(0).proving_ids(2).collect::<Vec<_>>(), &[TreeID::from(2), TreeID::from(5)]);
Sourcepub fn appending_ids(new_len: u64) -> impl Iterator<Item = Self>
pub fn appending_ids(new_len: u64) -> impl Iterator<Item = Self>
The ids whose values are required to append the next entry to the log, sorted left to right.
§Examples
use merkle_log::*;
assert_eq!(TreeID::appending_ids(1).collect::<Vec<_>>(), &[]);
assert_eq!(TreeID::appending_ids(2).collect::<Vec<_>>(), &[TreeID::from(0)]);
assert_eq!(TreeID::appending_ids(3).collect::<Vec<_>>(), &[TreeID::from(1)]);
assert_eq!(TreeID::appending_ids(4).collect::<Vec<_>>(), &[TreeID::from(1), TreeID::from(4)]);
assert_eq!(TreeID::appending_ids(5).collect::<Vec<_>>(), &[TreeID::from(3)]);
assert_eq!(TreeID::appending_ids(6).collect::<Vec<_>>(), &[TreeID::from(3), TreeID::from(8)]);
assert_eq!(TreeID::appending_ids(7).collect::<Vec<_>>(), &[TreeID::from(3), TreeID::from(9)]);
assert_eq!(TreeID::appending_ids(8).collect::<Vec<_>>(), &[TreeID::from(3), TreeID::from(9), TreeID::from(12)]);
assert_eq!(TreeID::appending_ids(9).collect::<Vec<_>>(), &[TreeID::from(7)]);
assert_eq!(TreeID::appending_ids(10).collect::<Vec<_>>(), &[TreeID::from(7), TreeID::from(16)]);
Sourcepub fn subroot_ids(len: u64) -> impl Iterator<Item = Self>
pub fn subroot_ids(len: u64) -> impl Iterator<Item = Self>
The root ids of the highest complete subtrees within a log whose length
is len
, sorted left to right.
§Examples
use merkle_log::*;
assert_eq!(TreeID::subroot_ids(0).collect::<Vec<_>>(), &[]);
assert_eq!(TreeID::subroot_ids(1).collect::<Vec<_>>(), &[TreeID::from(0)]);
assert_eq!(TreeID::subroot_ids(2).collect::<Vec<_>>(), &[TreeID::from(1)]);
assert_eq!(TreeID::subroot_ids(3).collect::<Vec<_>>(), &[TreeID::from(1), TreeID::from(4)]);
assert_eq!(TreeID::subroot_ids(4).collect::<Vec<_>>(), &[TreeID::from(3)]);
assert_eq!(TreeID::subroot_ids(5).collect::<Vec<_>>(), &[TreeID::from(3), TreeID::from(8)]);
assert_eq!(TreeID::subroot_ids(6).collect::<Vec<_>>(), &[TreeID::from(3), TreeID::from(9)]);
assert_eq!(TreeID::subroot_ids(7).collect::<Vec<_>>(), &[TreeID::from(3), TreeID::from(9), TreeID::from(12)]);
assert_eq!(TreeID::subroot_ids(8).collect::<Vec<_>>(), &[TreeID::from(7)]);
assert_eq!(TreeID::subroot_ids(9).collect::<Vec<_>>(), &[TreeID::from(7), TreeID::from(16)]);
assert_eq!(TreeID::subroot_ids(10).collect::<Vec<_>>(), &[TreeID::from(7), TreeID::from(17)]);
// test root
// assert_eq!(TreeID::subroot_ids(TreeID::MAX_LEN).count() as u64, 0u64);