Skip to content

Commit 63fa287

Browse files
refactor!(MerkleTree): Drop maxima
Drop the `const`s `MAX_NUM_NODES`, `MAX_NUM_LEAFS`, and `MAX_TREE_HEIGHT`. The consts supposedly guarantee cross-platform support. However: - It is unclear if any platform supports trees with 2^64 nodes, so the support guarantee is kind of vacuous. - Transferring Merkle trees between different machines is not a supported use case. Cf. #251. Co-authored-by: Ferdinand Sauer <ferdinand@neptune.cash>
1 parent 4612076 commit 63fa287

File tree

1 file changed

+13
-33
lines changed

1 file changed

+13
-33
lines changed

twenty-first/src/util_types/merkle_tree.rs

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -64,26 +64,6 @@ pub type MerkleTreeLeafIndex = u64;
6464
/// Type alias for [u32].
6565
pub type MerkleTreeHeight = u32;
6666

67-
/// The maximum number of nodes in Merkle trees that functions in this module
68-
/// support.
69-
///
70-
/// This constant enforces that all compilation targets have a consistent
71-
/// [`MAX_TREE_HEIGHT`] and [`MAX_NUM_LEAFS`].
72-
///
73-
/// Note that the struct [`MerkleTree`] can hold only 2^25 nodes. This constant
74-
/// applies to associated functions that do not take `self`.
75-
const MAX_NUM_NODES: MerkleTreeNodeIndex =
76-
MerkleTreeNodeIndex::MAX ^ (MerkleTreeNodeIndex::MAX >> 1);
77-
78-
/// The maximum number of leafs in Merkle trees that functions in this module
79-
/// support.
80-
///
81-
/// See also: [`MAX_NUM_NODES`], [`MAX_TREE_HEIGHT`].
82-
const MAX_NUM_LEAFS: MerkleTreeLeafIndex = MAX_NUM_NODES / 2;
83-
84-
/// The maximum height of Merkle trees that functions in this module support.
85-
pub const MAX_TREE_HEIGHT: MerkleTreeHeight = MAX_NUM_LEAFS.ilog2() as MerkleTreeHeight;
86-
8767
/// The index of the root node.
8868
pub(crate) const ROOT_INDEX: MerkleTreeNodeIndex = 1;
8969

@@ -163,9 +143,6 @@ impl MerkleTree {
163143
/// If you need to read the root, try [`root()`](Self::root) instead.
164144
const ROOT_INDEX: usize = ROOT_INDEX as usize;
165145

166-
const MAX_NUM_NODES: usize = 1_usize << 25;
167-
const MAX_NUM_LEAFS: usize = Self::MAX_NUM_NODES / 2;
168-
169146
/// Build a MerkleTree with the given leafs.
170147
///
171148
/// [`MerkleTree::par_new`] is equivalent and usually faster.
@@ -231,11 +208,16 @@ impl MerkleTree {
231208
if !num_leafs.is_power_of_two() {
232209
return Err(MerkleTreeError::IncorrectNumberOfLeafs);
233210
}
234-
if num_leafs > Self::MAX_NUM_LEAFS {
235-
return Err(MerkleTreeError::TreeTooHigh);
236-
}
237211

238-
let mut nodes = vec![Digest::default(); 2 * num_leafs];
212+
let num_nodes = 2 * num_leafs;
213+
let mut nodes = Vec::new();
214+
215+
// Use `try_reserve_exact` because we want to get an error not a panic
216+
// if allocation fails. The error can be bubbled up.
217+
nodes
218+
.try_reserve_exact(num_nodes)
219+
.map_err(|_| MerkleTreeError::TreeTooHigh)?;
220+
nodes.resize(num_nodes, Digest::default());
239221
nodes[num_leafs..].copy_from_slice(leafs);
240222

241223
Ok(nodes)
@@ -498,10 +480,8 @@ impl PartialMerkleTree {
498480
}
499481

500482
fn num_leafs(&self) -> Result<MerkleTreeLeafIndex> {
501-
if self.tree_height > MAX_TREE_HEIGHT {
502-
return Err(MerkleTreeError::TreeTooHigh);
503-
}
504-
Ok(1 << self.tree_height)
483+
1u64.checked_shl(self.tree_height)
484+
.ok_or(MerkleTreeError::TreeTooHigh)
505485
}
506486

507487
/// Compute all computable digests of the partial Merkle tree, modifying self.
@@ -663,7 +643,7 @@ pub enum MerkleTreeError {
663643
#[error("The number of leafs must be a power of two.")]
664644
IncorrectNumberOfLeafs,
665645

666-
#[error("Tree height must not exceed {MAX_TREE_HEIGHT}.")]
646+
#[error("Tree height must not exceed 63.")]
667647
TreeTooHigh,
668648
}
669649

@@ -923,7 +903,7 @@ pub mod merkle_tree_test {
923903
#[proptest(cases = 40)]
924904
fn incorrect_tree_height_leads_to_verification_failure(
925905
#[filter(#test_tree.has_non_trivial_proof())] test_tree: MerkleTreeToTest,
926-
#[strategy(0..=MAX_TREE_HEIGHT)]
906+
#[strategy(0_u32..64)]
927907
#[filter(#test_tree.tree.height() != #incorrect_height)]
928908
incorrect_height: MerkleTreeHeight,
929909
) {

0 commit comments

Comments
 (0)