-
Notifications
You must be signed in to change notification settings - Fork 170
feat(merkle): Pad single tree leaves to next power of two #876
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(merkle): Pad single tree leaves to next power of two #876
Conversation
c677e29
to
6dc1df4
Compare
6dc1df4
to
2977d1a
Compare
crypto/src/merkle_tree/utils.rs
Outdated
if x == 1 { | ||
false | ||
} else { | ||
(x != 0) && ((x & (x - 1)) == 0) | ||
} |
There was a problem hiding this comment.
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:
if x == 1 { | |
false | |
} else { | |
(x != 0) && ((x & (x - 1)) == 0) | |
} | |
(x > 1) && ((x & (x - 1)) == 0) |
There was a problem hiding this comment.
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:
- We need this to keep the smallest tree at two leaves count.
- We couldn't find a better name that wasn't overly verbose to what the function really does.
- 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.
- 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.
There was a problem hiding this comment.
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 ReportAll modified and coverable lines are covered by tests ✅
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. |
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.
Checklist