-
Notifications
You must be signed in to change notification settings - Fork 12.3k
Add Packing library #4992
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
Merged
Add Packing library #4992
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 | ||
| --- | ||
|
|
||
| `Packing`: Added a new utility for packing and unpacking multiple values into a single bytes32. Includes initial support for packing two `uint128` in an `Uint128x2` type. |
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 |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| pragma solidity ^0.8.20; | ||
|
|
||
| /** | ||
| * @dev Helper library packing and unpacking multiple values into bytes32 | ||
| */ | ||
| library Packing { | ||
| type Uint128x2 is bytes32; | ||
|
|
||
| /// @dev Cast a bytes32 into a Uint128x2 | ||
| function asUint128x2(bytes32 self) internal pure returns (Uint128x2) { | ||
| return Uint128x2.wrap(self); | ||
| } | ||
|
|
||
| /// @dev Cast a Uint128x2 into a bytes32 | ||
| function asBytes32(Uint128x2 self) internal pure returns (bytes32) { | ||
| return Uint128x2.unwrap(self); | ||
| } | ||
|
|
||
| /// @dev Pack two uint128 into a Uint128x2 | ||
| function pack(uint128 first128, uint128 second128) internal pure returns (Uint128x2) { | ||
| return Uint128x2.wrap(bytes32(bytes16(first128)) | bytes32(uint256(second128))); | ||
| } | ||
|
|
||
| /// @dev Split a Uint128x2 into two uint128 | ||
| function split(Uint128x2 self) internal pure returns (uint128, uint128) { | ||
| return (first(self), second(self)); | ||
| } | ||
|
|
||
| /// @dev Get the first element of a Uint128x2 counting from higher to lower bytes | ||
| function first(Uint128x2 self) internal pure returns (uint128) { | ||
| return uint128(bytes16(Uint128x2.unwrap(self))); | ||
| } | ||
|
|
||
| /// @dev Get the second element of a Uint128x2 counting from higher to lower bytes | ||
| function second(Uint128x2 self) internal pure returns (uint128) { | ||
| return uint128(uint256(Uint128x2.unwrap(self))); | ||
| } | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| pragma solidity ^0.8.20; | ||
|
|
||
| import {Test} from "forge-std/Test.sol"; | ||
| import {Packing} from "@openzeppelin/contracts/utils/Packing.sol"; | ||
|
|
||
| contract PackingTest is Test { | ||
| using Packing for *; | ||
|
|
||
| // Pack a pair of arbitrary uint128, and check that split recovers the correct values | ||
| function testUint128x2(uint128 first, uint128 second) external { | ||
| Packing.Uint128x2 packed = Packing.pack(first, second); | ||
| assertEq(packed.first(), first); | ||
| assertEq(packed.second(), second); | ||
|
|
||
| (uint128 recoveredFirst, uint128 recoveredSecond) = packed.split(); | ||
| assertEq(recoveredFirst, first); | ||
| assertEq(recoveredSecond, second); | ||
| } | ||
|
|
||
| // split an arbitrary bytes32 into a pair of uint128, and check that repack matches the input | ||
| function testUint128x2(bytes32 input) external { | ||
| (uint128 first, uint128 second) = input.asUint128x2().split(); | ||
| assertEq(Packing.pack(first, second).asBytes32(), input); | ||
| } | ||
| } |
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,27 @@ | ||
| const { ethers } = require('hardhat'); | ||
| const { expect } = require('chai'); | ||
| const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers'); | ||
| const { generators } = require('../helpers/random'); | ||
|
|
||
| async function fixture() { | ||
| return { mock: await ethers.deployContract('$Packing') }; | ||
| } | ||
|
|
||
| describe('Packing', function () { | ||
| beforeEach(async function () { | ||
| Object.assign(this, await loadFixture(fixture)); | ||
| }); | ||
|
|
||
| it('Uint128x2', async function () { | ||
| const first = generators.uint256() % 2n ** 128n; | ||
| const second = generators.uint256() % 2n ** 128n; | ||
| const packed = ethers.hexlify(ethers.toBeArray((first << 128n) | second)); | ||
|
|
||
| expect(await this.mock.$asUint128x2(packed)).to.equal(packed); | ||
| expect(await this.mock.$asBytes32(packed)).to.equal(packed); | ||
| expect(await this.mock.$pack(first, second)).to.equal(packed); | ||
| expect(await this.mock.$split(packed)).to.deep.equal([first, second]); | ||
| expect(await this.mock.$first(packed)).to.equal(first); | ||
| expect(await this.mock.$second(packed)).to.equal(second); | ||
| }); | ||
| }); |
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.
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.
For now we only have
(uint128, uint128) <> bytes32, but we can imagine having other types, such asuint64x4Everything here is designed to scale to other types.