-
Notifications
You must be signed in to change notification settings - Fork 12.1k
Add reverseBits
operations to Bytes.sol
#5724
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
Add reverseBits
operations to Bytes.sol
#5724
Conversation
🦋 Changeset detectedLatest commit: fff6ad2 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
reverseBits
operations to Math.sol
Should that be in Math, or should we start a It doesn't feel like this is a math operation strictly speaking. |
reverseBits
operations to Math.solreverseBits
operations to Bytes.sol
After the review and changes made I think it makes sense to add it to the |
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
For the record, I tried seeing is assembly would save gas: function reverseBits256(bytes32 value) internal pure returns (bytes32) {
assembly ("memory-safe") {
let mask
// swap bytes
mask := mul(div(not(0), 0xFFFF), 0xFF)
value := or(and(shr(8, value), mask), shl(8, and(value, mask)))
// swap 2-bytes long pairs
mask := mul(div(not(0), 0xFFFFFFFF), 0xFFFF)
value := or(and(shr(16, value), mask), shl(16, and(value, mask)))
// swap 4-bytes long pairs
mask := mul(div(not(0), 0xFFFFFFFFFFFFFFFF), 0xFFFFFFFF)
value := or(and(shr(32, value), mask), shl(32, and(value, mask)))
// swap 8-bytes long pairs
mask := mul(div(not(0), 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF), 0xFFFFFFFFFFFFFFFF)
value := or(and(shr(64, value), mask), shl(64, and(value, mask)))
// swap 16-bytes long pairs
value := or(shr(128, value), shl(128, value))
}
return value;
} It doesn't ! |
It all looks good to me, except the name of the functions. Lets take the example of what it reverses are not the inidividual bits, its the bytes. So IMO we should either:
|
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.
IMO, most of the unit tests are not really necessary, and the fuzzing covers its already.
But that is definitelly not a merge blocker.
LGTM.
Needed for #5680
PR Checklist
npx changeset add
)