Skip to content
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

bitwise optimizations #120

Merged
merged 4 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1 +1 @@
143500
143347
2 changes: 1 addition & 1 deletion .forge-snapshots/permitBatchTransferFromSingleToken.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
88950
88797
2 changes: 1 addition & 1 deletion .forge-snapshots/permitTransferFromBatchTypedWitness.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
121317
121164
2 changes: 1 addition & 1 deletion .forge-snapshots/permitTransferFromCompactSig.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
88685
88532
2 changes: 1 addition & 1 deletion .forge-snapshots/permitTransferFromSingleToken.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
88737
88584
2 changes: 1 addition & 1 deletion .forge-snapshots/permitTransferFromTypedWitness.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
91157
91004
2 changes: 1 addition & 1 deletion .forge-snapshots/single recipient 2 tokens.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
118623
118470
2 changes: 1 addition & 1 deletion .forge-snapshots/single recipient many tokens.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
133765
133612
63 changes: 32 additions & 31 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ CompactSignature:testCompactSignature27() (gas: 253)
CompactSignature:testCompactSignature28() (gas: 141)
EIP712Test:testDomainSeparator() (gas: 5759)
EIP712Test:testDomainSeparatorAfterFork() (gas: 10652)
NonceBitmapTest:testHighNonces() (gas: 36088)
NonceBitmapTest:testInvalidateFullWord() (gas: 62606)
NonceBitmapTest:testInvalidateNonzeroWord() (gas: 85721)
NonceBitmapTest:testLowNonces() (gas: 41019)
NonceBitmapTest:testNonceWordBoundary() (gas: 57948)
NonceBitmapTest:testUseTwoRandomNonces(uint256,uint256) (runs: 256, μ: 49304, ~: 51829)
NonceBitmapTest:testUsingNonceTwiceFails(uint256) (runs: 256, μ: 32616, ~: 32616)
NonceBitmapTest:testHighNonces() (gas: 35980)
NonceBitmapTest:testInvalidateFullWord() (gas: 62805)
NonceBitmapTest:testInvalidateNonzeroWord() (gas: 85285)
NonceBitmapTest:testLowNonces() (gas: 40693)
NonceBitmapTest:testNonceLLL() (gas: 21789)
NonceBitmapTest:testNonceWordBoundary() (gas: 42060)
NonceBitmapTest:testUseTwoRandomNonces(uint256,uint256) (runs: 256, μ: 48918, ~: 51523)
NonceBitmapTest:testUsingNonceTwiceFails(uint256) (runs: 256, μ: 21793, ~: 21816)
Permit2LibTest:testOZSafePermit() (gas: 24311)
Permit2LibTest:testOZSafePermitPlusOZSafeTransferFrom() (gas: 129066)
Permit2LibTest:testOZSafeTransferFrom() (gas: 38787)
Expand All @@ -50,28 +51,28 @@ Permit2LibTest:testTransferFrom2() (gas: 38404)
Permit2LibTest:testTransferFrom2Full() (gas: 53437)
Permit2LibTest:testTransferFrom2InvalidAmount() (gas: 12519)
Permit2LibTest:testTransferFrom2NonPermitToken() (gas: 53305)
SignatureTransferTest:testGasMultiplePermitBatchTransferFrom() (gas: 270959)
SignatureTransferTest:testGasSinglePermitBatchTransferFrom() (gas: 186309)
SignatureTransferTest:testGasSinglePermitTransferFrom() (gas: 124095)
SignatureTransferTest:testInvalidateUnorderedNonces() (gas: 56540)
SignatureTransferTest:testPermitBatchTransferFrom() (gas: 162076)
SignatureTransferTest:testPermitBatchTransferFromSingleRecipient() (gas: 190355)
SignatureTransferTest:testPermitBatchTransferFromTypedWitness() (gas: 242493)
SignatureTransferTest:testPermitBatchTransferFromTypedWitnessInvalidType() (gas: 87340)
SignatureTransferTest:testPermitBatchTransferFromTypedWitnessInvalidTypeHash() (gas: 86803)
SignatureTransferTest:testPermitBatchTransferFromTypedWitnessInvalidTypeName() (gas: 88276)
SignatureTransferTest:testPermitBatchTransferFromTypedWitnessInvalidWitness() (gas: 88321)
SignatureTransferTest:testGasMultiplePermitBatchTransferFrom() (gas: 270806)
SignatureTransferTest:testGasSinglePermitBatchTransferFrom() (gas: 186156)
SignatureTransferTest:testGasSinglePermitTransferFrom() (gas: 123942)
SignatureTransferTest:testInvalidateUnorderedNonces() (gas: 41090)
SignatureTransferTest:testPermitBatchTransferFrom() (gas: 161923)
SignatureTransferTest:testPermitBatchTransferFromSingleRecipient() (gas: 190202)
SignatureTransferTest:testPermitBatchTransferFromTypedWitness() (gas: 242340)
SignatureTransferTest:testPermitBatchTransferFromTypedWitnessInvalidType() (gas: 87187)
SignatureTransferTest:testPermitBatchTransferFromTypedWitnessInvalidTypeHash() (gas: 86650)
SignatureTransferTest:testPermitBatchTransferFromTypedWitnessInvalidTypeName() (gas: 88123)
SignatureTransferTest:testPermitBatchTransferFromTypedWitnessInvalidWitness() (gas: 88168)
SignatureTransferTest:testPermitBatchTransferInvalidAmountsLengthMismatch() (gas: 43738)
SignatureTransferTest:testPermitBatchTransferMultiAddr() (gas: 160450)
SignatureTransferTest:testPermitBatchTransferSingleRecipientManyTokens() (gas: 211974)
SignatureTransferTest:testPermitTransferFrom() (gas: 93208)
SignatureTransferTest:testPermitTransferFromCompactSig() (gas: 124159)
SignatureTransferTest:testPermitTransferFromIncorrectSigLength() (gas: 51430)
SignatureTransferTest:testPermitTransferFromInvalidNonce() (gas: 93215)
SignatureTransferTest:testPermitTransferFromRandomNonceAndAmount(uint256,uint128) (runs: 256, μ: 95842, ~: 96818)
SignatureTransferTest:testPermitTransferFromToSpender() (gas: 93494)
SignatureTransferTest:testPermitTransferFromTypedWitness() (gas: 127964)
SignatureTransferTest:testPermitTransferFromTypedWitnessInvalidType() (gas: 58668)
SignatureTransferTest:testPermitTransferFromTypedWitnessInvalidTypeName() (gas: 59722)
SignatureTransferTest:testPermitTransferFromTypedWitnessInvalidTypehash() (gas: 57658)
SignatureTransferTest:testPermitTransferSpendLessThanFull(uint256,uint128) (runs: 256, μ: 98177, ~: 99895)
SignatureTransferTest:testPermitBatchTransferMultiAddr() (gas: 160297)
SignatureTransferTest:testPermitBatchTransferSingleRecipientManyTokens() (gas: 211821)
SignatureTransferTest:testPermitTransferFrom() (gas: 93055)
SignatureTransferTest:testPermitTransferFromCompactSig() (gas: 124006)
SignatureTransferTest:testPermitTransferFromIncorrectSigLength() (gas: 51277)
SignatureTransferTest:testPermitTransferFromInvalidNonce() (gas: 73250)
SignatureTransferTest:testPermitTransferFromRandomNonceAndAmount(uint256,uint128) (runs: 256, μ: 95689, ~: 96665)
SignatureTransferTest:testPermitTransferFromToSpender() (gas: 93341)
SignatureTransferTest:testPermitTransferFromTypedWitness() (gas: 127811)
SignatureTransferTest:testPermitTransferFromTypedWitnessInvalidType() (gas: 58515)
SignatureTransferTest:testPermitTransferFromTypedWitnessInvalidTypeName() (gas: 59569)
SignatureTransferTest:testPermitTransferFromTypedWitnessInvalidTypehash() (gas: 57505)
SignatureTransferTest:testPermitTransferSpendLessThanFull(uint256,uint128) (runs: 256, μ: 98024, ~: 99742)
9 changes: 4 additions & 5 deletions src/SignatureTransfer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,17 @@ contract SignatureTransfer is ISignatureTransfer, EIP712 {
/// @dev The last 8 bits of the nonce value is the position of the bit in the bitmap
function bitmapPositions(uint256 nonce) private pure returns (uint256 wordPos, uint256 bitPos) {
wordPos = uint248(nonce >> 8);
bitPos = uint8(nonce & 255);
bitPos = uint8(nonce);
}

/// @notice Checks whether a nonce is taken and sets the bit at the bit position in the bitmap at the word position
/// @param from The address to use the nonce at
/// @param nonce The nonce to spend
function _useUnorderedNonce(address from, uint256 nonce) internal {
(uint256 wordPos, uint256 bitPos) = bitmapPositions(nonce);
uint256 bitmap = nonceBitmap[from][wordPos];
uint256 bit = 1 << bitPos;
uint256 flipped = nonceBitmap[from][wordPos] ^= bit;
Copy link
Collaborator

Choose a reason for hiding this comment

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

woah this is so smart. love it

Copy link
Contributor

@PaulRBerg PaulRBerg May 23, 2023

Choose a reason for hiding this comment

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

Smart, yes, but this approach runs counter to the Checks-Effects-Interactions pattern by switching the order of the checks and the effects.

It's also not super intuitive what the order of operations is - I opened an issue about this: #217


if ((bitmap >> bitPos) & 1 == 1) revert InvalidNonce();

nonceBitmap[from][wordPos] = bitmap | (1 << bitPos);
if (flipped & bit == 0) revert InvalidNonce();
}
}
6 changes: 6 additions & 0 deletions test/NonceBitmap.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ contract NonceBitmapTest is Test {
permit2 = new MockPermit2();
}

function testNonceLLL() public {
Copy link
Collaborator

Choose a reason for hiding this comment

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

what is LLL?

Copy link
Member Author

Choose a reason for hiding this comment

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

oops lemme remove

permit2.useUnorderedNonce(address(this), 5);
vm.expectRevert(InvalidNonce.selector);
permit2.useUnorderedNonce(address(this), 5);
}

function testLowNonces() public {
permit2.useUnorderedNonce(address(this), 5);
permit2.useUnorderedNonce(address(this), 0);
Expand Down