Skip to content

Commit f5fbed4

Browse files
committed
refactor!(MerkleTree): Use index type aliases
This commit introduces type aliases: - `MerkleTreeNodeIndex` for `u64` - `MerkleTreeLeafIndex` for `u64` - `MerkleTreeHeight` for `u32`. It also refactors `MerkleTree` to avoid using `usize` where-ever possible, particularly in public functions. Instead, these types aliases are used. `MerkleTree` internally stores a `Vec<Digest>`, which induces two limitations: 1. The index type is `usize`, which complicates matters when targeting compilation on 32-bit machines (including 32-bit WASM). 2. This vector can store at most 2^25 nodes, whereas we would like functions associated with Merkle trees to work for Merkle trees that are far larger (without needing to store all the internal nodes explicitly). The second limitation persists. However, the first limitation is fixed by this commit. Also, public functions `nodes()` and `leafs()` now return iterators instead of slices. As these functions were public, this change is breaking. Addresses #250.
1 parent 27a4325 commit f5fbed4

File tree

5 files changed

+134
-80
lines changed

5 files changed

+134
-80
lines changed

twenty-first/benches/merkle_tree_authenticate.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ impl Default for MerkleTreeSampler {
5656
}
5757

5858
impl MerkleTreeSampler {
59-
fn num_leafs(&self) -> usize {
60-
1 << self.tree_height
59+
fn num_leafs(&self) -> MerkleTreeLeafIndex {
60+
(1 << self.tree_height) as MerkleTreeLeafIndex
6161
}
6262

6363
fn leaf_digests(&mut self) -> Vec<Digest> {
@@ -72,7 +72,7 @@ impl MerkleTreeSampler {
7272
MerkleTree::par_new(&leaf_digests).unwrap()
7373
}
7474

75-
fn indices_to_open(&mut self) -> Vec<usize> {
75+
fn indices_to_open(&mut self) -> Vec<MerkleTreeLeafIndex> {
7676
(0..self.num_opened_indices)
7777
.map(|_| self.rng.random_range(0..self.num_leafs()))
7878
.collect()

0 commit comments

Comments
 (0)