Skip to content

Commit

Permalink
feat: Allow merkle trees of 1 node (its root) (#919)
Browse files Browse the repository at this point in the history
* feat: change is_power_of_two to allow merkle trees of 1 node (its root)

* fix: unit tests

* chore: cargo fmt

* Update crypto/src/merkle_tree/proof.rs

Co-authored-by: Mario Rugiero <[email protected]>

---------

Co-authored-by: Diego K <[email protected]>
Co-authored-by: Mario Rugiero <[email protected]>
  • Loading branch information
3 people authored Sep 27, 2024
1 parent 0103cee commit efd46f0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
2 changes: 1 addition & 1 deletion crypto/src/merkle_tree/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ mod tests {

let values: Vec<FE> = vec![FE::new(1)]; // Single element
let merkle_tree = MerkleTree::<TestBackend<U64PF>>::build(&values).unwrap();
assert_eq!(merkle_tree.root, FE::new(4)); // Adjusted expected value
assert_eq!(merkle_tree.root, FE::new(2)); // Adjusted expected value
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions crypto/src/merkle_tree/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ mod tests {
let merkle_tree = MerkleTree::<TestBackend<U64PF>>::build(&values).unwrap();

// Update the expected root value based on the actual logic of TestBackend
// For example, if combining two `1`s results in `4`, update this accordingly
let expected_root = FE::new(4); // Assuming combining two `1`s results in `4`
// For example, in this case hashing a single `1` results in `2`
let expected_root = FE::new(2); // Assuming hashing a `1`s results in `2`
assert_eq!(
merkle_tree.root, expected_root,
"The root of the Merkle tree does not match the expected value."
Expand Down
14 changes: 6 additions & 8 deletions crypto/src/merkle_tree/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ pub fn complete_until_power_of_two<T: Clone>(mut values: Vec<T>) -> Vec<T> {
}

// ! NOTE !
// We in this function we say 2^0 = 1 is not a power of two when it is infact true. We
// need this to maintain that the smallest tree at two leaves counts and we could not find
// a name that wasn't overly verbose. In the case of a single value being present in a leaf node
// this indicates we pad to next power of two (i.e. 2). The function is private and is only used
// to ensure the tree has a power of 2 number of leaves.
// In this function we say 2^0 = 1 is a power of two.
// In turn, this makes the smallest tree of one leaf, possible.
// The function is private and is only used to ensure the tree
// has a power of 2 number of leaves.
fn is_power_of_two(x: usize) -> bool {
(x > 1) && ((x & (x - 1)) == 0)
(x & (x - 1)) == 0
}

// ! CAUTION !
Expand Down Expand Up @@ -124,9 +123,8 @@ mod tests {

let mut expected_leaves = vec![FE::new(2)];
expected_leaves.extend([FE::new(2)]);
assert_eq!(hashed_leaves.len(), 2);
assert_eq!(hashed_leaves.len(), 1);
assert_eq!(hashed_leaves[0], expected_leaves[0]);
assert_eq!(hashed_leaves[1], expected_leaves[1]);
}

const ROOT: usize = 0;
Expand Down

0 comments on commit efd46f0

Please sign in to comment.