Skip to content

Conversation

PatStiles
Copy link
Contributor

Pad Single Tree leaves to next power of two

Description

For merkle trees with a single leaf, pads the number of leaves to the next power of two by appending the last element.

Type of change

Please delete options that are not relevant.

  • New feature

Checklist

  • Unit tests added

@PatStiles PatStiles requested a review from a team as a code owner June 21, 2024 01:22
@PatStiles PatStiles force-pushed the feat/handle_one_leaf_in_merkle_tree branch from c677e29 to 6dc1df4 Compare June 21, 2024 17:44
@PatStiles PatStiles force-pushed the feat/handle_one_leaf_in_merkle_tree branch from 6dc1df4 to 2977d1a Compare June 24, 2024 19:25
Comment on lines 32 to 36
if x == 1 {
false
} else {
(x != 0) && ((x & (x - 1)) == 0)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking, this is not true. 1 is 2^0, so it is indeed a power of 2. That said, here's a simpler alternative:

Suggested change
if x == 1 {
false
} else {
(x != 0) && ((x & (x - 1)) == 0)
}
(x > 1) && ((x & (x - 1)) == 0)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because technically we're lying, I expect a comment clarifying that:

  1. We need this to keep the smallest tree at two leaves count.
  2. We couldn't find a better name that wasn't overly verbose to what the function really does.
  3. The function is private (please make it private as well) and is only used to ensure the tree has a power of 2 number of leaves.
  4. A little white lie here saves us a lot of edge cases all around the code.

Otherwise, in a few months we will look at this, see it lies, conclude it's a bug, and proceed to "fix" it causing unexplainable bugs elsewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Oppen will address this now.

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 72.01%. Comparing base (e465d7c) to head (012df5b).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #876   +/-   ##
=======================================
  Coverage   72.00%   72.01%           
=======================================
  Files         149      149           
  Lines       33373    33377    +4     
=======================================
+ Hits        24031    24036    +5     
+ Misses       9342     9341    -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@MauroToscano MauroToscano merged commit dfe4f0e into lambdaclass:main Jul 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants