Skip to content

Commit 4612076

Browse files
committed
docs(MerkleTree): Document index conventions in type aliases' docstrings
1 parent d04e8aa commit 4612076

File tree

1 file changed

+60
-14
lines changed

1 file changed

+60
-14
lines changed

twenty-first/src/util_types/merkle_tree.rs

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,47 @@ lazy_static! {
2121
.unwrap_or(DEFAULT_PARALLELIZATION_CUTOFF);
2222
}
2323

24+
/// Indexes internal nodes of a [`MerkleTree`].
25+
///
26+
/// The following convention is used.
27+
/// - Nothing lives at index 0.
28+
/// - Index 1 points to the root.
29+
/// - Indices 2 and 3 contain the two children of the root.
30+
/// - Indices 4 and 5 contain the two children of node 2.
31+
/// - Indices 6 and 7 contain the two children of node 3.
32+
/// - And so on. In general, the position (starting at 0) of the top bit
33+
/// indicates the number of layers of separation between this node and the
34+
/// root.
35+
/// - The node indices corresponding to leafs range from (1<<tree_height) to
36+
/// (2<<tree_height)-1.
37+
///
38+
/// For example:
39+
/// ```markdown
40+
/// ──── 1 ──── ╮
41+
/// ╱ ╲ │
42+
/// 2 3 │
43+
/// ╱ ╲ ╱ ╲ ├╴ node indices
44+
/// ╱ ╲ ╱ ╲ │
45+
/// 4 5 6 7 │
46+
/// ╱ ╲ ╱ ╲ ╱ ╲ ╱ ╲ │
47+
/// 8 9 10 11 12 13 14 15 ╯
48+
/// ```
49+
///
50+
/// Type alias for [u64].
2451
pub type MerkleTreeNodeIndex = u64;
52+
53+
/// Indexes the leafs of a Merkle tree, left to right, starting with zero and
54+
/// ending with one less than a power of two. The exponent of that power of two
55+
/// coincides with the tree's height.
56+
///
57+
/// Type alias for [u64].
2558
pub type MerkleTreeLeafIndex = u64;
59+
60+
/// Counts the number of layers in the Merkle tree, not including the root.
61+
/// Equivalently, counts the number of nodes on a path from a leaf to the root,
62+
/// including the leaf but not the root.
63+
///
64+
/// Type alias for [u32].
2665
pub type MerkleTreeHeight = u32;
2766

2867
/// The maximum number of nodes in Merkle trees that functions in this module
@@ -202,14 +241,14 @@ impl MerkleTree {
202241
Ok(nodes)
203242
}
204243

205-
/// Compute the node indices for an authentication structure.
244+
/// Compute the [`MerkleTreeNodeIndex`]es for an authentication structure.
206245
///
207-
/// Given a list of leaf indices, return the indices of exactly those nodes
208-
/// that are needed to prove (or verify) that the indicated leafs are in the
209-
/// Merkle tree.
246+
/// Given a list of [`MerkleTreeLeafIndex`]es, return the (node) indices of
247+
/// exactly those nodes that are needed to prove (or verify) that the
248+
/// indicated leafs are in the Merkle tree.
210249
///
211-
/// Returns an error if any of the leaf indices is bigger than the number of
212-
/// leafs.
250+
/// Returns an error if any of the leaf indices is bigger than or equal to
251+
/// the total number of leafs in the tree.
213252
pub fn authentication_structure_node_indices(
214253
num_leafs: MerkleTreeLeafIndex,
215254
leaf_indices: &[MerkleTreeLeafIndex],
@@ -243,11 +282,14 @@ impl MerkleTree {
243282
Ok(set_difference.sorted_unstable().rev())
244283
}
245284

246-
/// Generate a de-duplicated authentication structure for the given leaf indices.
285+
/// Generate a de-duplicated authentication structure for the given
286+
/// [`MerkleTreeLeafIndex`]es.
287+
///
247288
/// If a single index is supplied, the authentication structure is the
248289
/// authentication path for the indicated leaf.
249290
///
250-
/// For example, consider the following Merkle tree.
291+
/// For example, consider the following Merkle tree, and note the difference
292+
/// between [`MerkleTreeLeafIndex`] and [`MerkleTreeNodeIndex`].
251293
///
252294
/// ```markdown
253295
/// ──── 1 ──── ╮
@@ -303,7 +345,7 @@ impl MerkleTree {
303345
leaf_count.ilog2()
304346
}
305347

306-
/// The node at the given node index, if it exists.
348+
/// The node at the given [`MerkleTreeNodeIndex`], if it exists.
307349
///
308350
/// Note that nodes are 1-indexed, meaning that the root lives at index 1
309351
/// and all the other nodes have larger indices.
@@ -332,12 +374,14 @@ impl MerkleTree {
332374
self.nodes.iter().skip(num_leafs)
333375
}
334376

335-
/// The leaf at the given index, if it exists.
377+
/// The leaf at the given [`MerkleTreeLeafIndex`], if it exists.
336378
pub fn leaf(&self, index: MerkleTreeLeafIndex) -> Option<Digest> {
337379
let first_leaf_index = self.num_leafs();
338380
self.node(first_leaf_index + index)
339381
}
340382

383+
/// Produce a [`Vec`] of ([`MerkleTreeLeafIndex`], [`Digest`]) covering all
384+
/// leafs.
341385
pub fn indexed_leafs(
342386
&self,
343387
indices: &[MerkleTreeLeafIndex],
@@ -349,10 +393,12 @@ impl MerkleTree {
349393
indices.iter().map(maybe_indexed_leaf).collect()
350394
}
351395

352-
/// A full inclusion proof for the leafs at the supplied indices, including the
353-
/// leafs. Generally, using [`authentication_structure`][auth_structure] is
354-
/// preferable. Use this method only if the verifier needs explicit access to the
355-
/// leafs, _i.e._, cannot compute them from other information.
396+
/// A full inclusion proof for the leafs at the supplied
397+
/// [`MerkleTreeLeafIndex`]es, *including* the leafs
398+
///
399+
/// Generally, using [`authentication_structure`][auth_structure] is
400+
/// preferable. Use this method only if the verifier needs explicit access
401+
/// to the leafs, _i.e._, cannot compute them from other information.
356402
///
357403
/// [auth_structure]: Self::authentication_structure
358404
pub fn inclusion_proof_for_leaf_indices(

0 commit comments

Comments
 (0)