-
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
Merged
ernestognw
merged 13 commits into
OpenZeppelin:master
from
ernestognw:feat/math-reverse-bits
Jul 10, 2025
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
bdd2cf1
Add operations to Math.sol
ernestognw 3385718
Rename to in Math library and update corresponding tests for consis…
ernestognw 40d7922
Update return types of reverseBits functions to match their respectiv…
ernestognw 89860bc
Refactor reverseBits functions in to use fixed-size byte types
ernestognw 9b58730
Test nits
ernestognw 77ffa8c
Simplify
ernestognw ce91c80
up
ernestognw b3e3add
Move reverse functions to Bytes.sol
ernestognw 2f3107c
Move Bytes.t.sol
ernestognw a50d8b9
Update contracts/utils/Bytes.sol
ernestognw 4957157
Rename reverseBits functions to reverseBytes
ernestognw ebcb2e5
Update Bytes.t.sol
Amxx fff6ad2
Update Bytes.t.sol
Amxx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'openzeppelin-solidity': minor | ||
--- | ||
|
||
`Bytes`: Add `reverseBits256`, `reverseBits128`, `reverseBits64`, `reverseBits32`, and `reverseBits16` functions to reverse byte order for converting between little-endian and big-endian representations. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
module.exports = { | ||
MAX_UINT16: 2n ** 16n - 1n, | ||
MAX_UINT32: 2n ** 32n - 1n, | ||
MAX_UINT48: 2n ** 48n - 1n, | ||
MAX_UINT64: 2n ** 64n - 1n, | ||
MAX_UINT128: 2n ** 128n - 1n, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.20; | ||
|
||
import {Test, stdError} from "forge-std/Test.sol"; | ||
|
||
import {Bytes} from "@openzeppelin/contracts/utils/Bytes.sol"; | ||
|
||
contract BytesTest is Test { | ||
// REVERSE BITS | ||
function testSymbolicReverseBits256(bytes32 value) public pure { | ||
assertEq(Bytes.reverseBits256(Bytes.reverseBits256(value)), value); | ||
} | ||
|
||
function testSymbolicReverseBits128(bytes16 value) public pure { | ||
assertEq(Bytes.reverseBits128(Bytes.reverseBits128(value)), value); | ||
} | ||
|
||
function testSymbolicReverseBits128Dirty(bytes16 value) public pure { | ||
assertEq(Bytes.reverseBits128(Bytes.reverseBits128(_dirtyBytes128(value))), value); | ||
} | ||
|
||
function testSymbolicReverseBits64(bytes8 value) public pure { | ||
assertEq(Bytes.reverseBits64(Bytes.reverseBits64(value)), value); | ||
} | ||
|
||
function testSymbolicReverseBits64Dirty(bytes8 value) public pure { | ||
assertEq(Bytes.reverseBits64(Bytes.reverseBits64(_dirtyBytes64(value))), value); | ||
} | ||
|
||
function testSymbolicReverseBits32(bytes4 value) public pure { | ||
assertEq(Bytes.reverseBits32(Bytes.reverseBits32(value)), value); | ||
} | ||
|
||
function testSymbolicReverseBits32Dirty(bytes4 value) public pure { | ||
assertEq(Bytes.reverseBits32(Bytes.reverseBits32(_dirtyBytes32(value))), value); | ||
} | ||
|
||
function testSymbolicReverseBits16(bytes2 value) public pure { | ||
assertEq(Bytes.reverseBits16(Bytes.reverseBits16(value)), value); | ||
} | ||
|
||
function testSymbolicReverseBits16Dirty(bytes2 value) public pure { | ||
assertEq(Bytes.reverseBits16(Bytes.reverseBits16(_dirtyBytes16(value))), value); | ||
} | ||
|
||
// Helpers | ||
function _dirtyBytes128(bytes16 value) private pure returns (bytes16) { | ||
bytes16 dirty = value; | ||
assembly ("memory-safe") { | ||
dirty := or(dirty, shr(128, not(0))) | ||
} | ||
return dirty; | ||
} | ||
|
||
function _dirtyBytes64(bytes8 value) private pure returns (bytes8) { | ||
bytes8 dirty = value; | ||
assembly ("memory-safe") { | ||
dirty := or(dirty, shr(192, not(0))) | ||
} | ||
return dirty; | ||
} | ||
|
||
function _dirtyBytes32(bytes4 value) private pure returns (bytes4) { | ||
bytes4 dirty = value; | ||
assembly ("memory-safe") { | ||
dirty := or(dirty, shr(224, not(0))) | ||
} | ||
return dirty; | ||
} | ||
|
||
function _dirtyBytes16(bytes2 value) private pure returns (bytes2) { | ||
bytes2 dirty = value; | ||
assembly ("memory-safe") { | ||
dirty := or(dirty, shr(240, not(0))) | ||
} | ||
return dirty; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.