MerklePath: document indexing order of nodes

I've left the iteration order of `MerklePath::inner_nodes()`
unspecified, but other methods of iteration and indexing are now
specified to be in order of deepest to shallowest.
This commit is contained in:
Qyriad 2025-04-04 13:56:29 +02:00
parent dcf66efb56
commit bf5b0e3255

View file

@ -11,6 +11,9 @@ use crate::{
// ================================================================================================ // ================================================================================================
/// A merkle path container, composed of a sequence of nodes of a Merkle tree. /// A merkle path container, composed of a sequence of nodes of a Merkle tree.
///
/// Indexing into this type starts at the deepest part of the path and gets shallower. That is,
/// the node at index `0` is deeper than the node at index `self.len() - 1`.
#[derive(Clone, Debug, Default, PartialEq, Eq)] #[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct MerklePath { pub struct MerklePath {
@ -22,6 +25,8 @@ impl MerklePath {
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
/// Creates a new Merkle path from a list of nodes. /// Creates a new Merkle path from a list of nodes.
///
/// The list is assumed to be in order of deepest to shallowest.
pub fn new(nodes: Vec<RpoDigest>) -> Self { pub fn new(nodes: Vec<RpoDigest>) -> Self {
assert!(nodes.len() <= u8::MAX.into(), "MerklePath may have at most 256 items"); assert!(nodes.len() <= u8::MAX.into(), "MerklePath may have at most 256 items");
Self { nodes } Self { nodes }
@ -35,7 +40,7 @@ impl MerklePath {
self.nodes.len() as u8 self.nodes.len() as u8
} }
/// Returns a reference to the [MerklePath]'s nodes. /// Returns a reference to the [MerklePath]'s nodes, in order of deepest to shallowest.
pub fn nodes(&self) -> &[RpoDigest] { pub fn nodes(&self) -> &[RpoDigest] {
&self.nodes &self.nodes
} }