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 all 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 @@
143701
143548
2 changes: 1 addition & 1 deletion .forge-snapshots/permitBatchTransferFromSingleToken.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
89017
88864
2 changes: 1 addition & 1 deletion .forge-snapshots/permitTransferFromBatchTypedWitness.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
121451
121298
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 @@
118757
118604
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 @@
134435
134282
64 changes: 32 additions & 32 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ 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: 35958)
NonceBitmapTest:testInvalidateFullWord() (gas: 62805)
NonceBitmapTest:testInvalidateNonzeroWord() (gas: 85285)
NonceBitmapTest:testLowNonces() (gas: 40671)
NonceBitmapTest:testNonceWordBoundary() (gas: 42042)
NonceBitmapTest:testUseTwoRandomNonces(uint256,uint256) (runs: 256, μ: 48832, ~: 51523)
NonceBitmapTest:testUsingNonceTwiceFails(uint256) (runs: 256, μ: 21775, ~: 21798)
Permit2LibTest:testOZSafePermit() (gas: 24311)
Permit2LibTest:testOZSafePermitPlusOZSafeTransferFrom() (gas: 129066)
Permit2LibTest:testOZSafeTransferFrom() (gas: 38787)
Expand All @@ -50,29 +50,29 @@ Permit2LibTest:testTransferFrom2() (gas: 38404)
Permit2LibTest:testTransferFrom2Full() (gas: 53437)
Permit2LibTest:testTransferFrom2InvalidAmount() (gas: 12519)
Permit2LibTest:testTransferFrom2NonPermitToken() (gas: 53305)
SignatureTransferTest:testGasMultiplePermitBatchTransferFrom() (gas: 271182)
SignatureTransferTest:testGasSinglePermitBatchTransferFrom() (gas: 186398)
SignatureTransferTest:testGasSinglePermitTransferFrom() (gas: 124117)
SignatureTransferTest:testInvalidateUnorderedNonces() (gas: 56562)
SignatureTransferTest:testPermitBatchMultiPermitSingleTransfer() (gas: 133761)
SignatureTransferTest:testPermitBatchTransferFrom() (gas: 162210)
SignatureTransferTest:testPermitBatchTransferFromSingleRecipient() (gas: 190511)
SignatureTransferTest:testPermitBatchTransferFromTypedWitness() (gas: 242649)
SignatureTransferTest:testPermitBatchTransferFromTypedWitnessInvalidType() (gas: 87340)
SignatureTransferTest:testPermitBatchTransferFromTypedWitnessInvalidTypeHash() (gas: 86825)
SignatureTransferTest:testPermitBatchTransferFromTypedWitnessInvalidTypeName() (gas: 88298)
SignatureTransferTest:testPermitBatchTransferFromTypedWitnessInvalidWitness() (gas: 88343)
SignatureTransferTest:testGasMultiplePermitBatchTransferFrom() (gas: 271029)
SignatureTransferTest:testGasSinglePermitBatchTransferFrom() (gas: 186245)
SignatureTransferTest:testGasSinglePermitTransferFrom() (gas: 123964)
SignatureTransferTest:testInvalidateUnorderedNonces() (gas: 41108)
SignatureTransferTest:testPermitBatchMultiPermitSingleTransfer() (gas: 133608)
SignatureTransferTest:testPermitBatchTransferFrom() (gas: 162057)
SignatureTransferTest:testPermitBatchTransferFromSingleRecipient() (gas: 190358)
SignatureTransferTest:testPermitBatchTransferFromTypedWitness() (gas: 242496)
SignatureTransferTest:testPermitBatchTransferFromTypedWitnessInvalidType() (gas: 87187)
SignatureTransferTest:testPermitBatchTransferFromTypedWitnessInvalidTypeHash() (gas: 86672)
SignatureTransferTest:testPermitBatchTransferFromTypedWitnessInvalidTypeName() (gas: 88145)
SignatureTransferTest:testPermitBatchTransferFromTypedWitnessInvalidWitness() (gas: 88190)
SignatureTransferTest:testPermitBatchTransferInvalidAmountsLengthMismatch() (gas: 43760)
SignatureTransferTest:testPermitBatchTransferMultiAddr() (gas: 160606)
SignatureTransferTest:testPermitBatchTransferSingleRecipientManyTokens() (gas: 212666)
SignatureTransferTest:testPermitTransferFrom() (gas: 93208)
SignatureTransferTest:testPermitTransferFromCompactSig() (gas: 124181)
SignatureTransferTest:testPermitTransferFromIncorrectSigLength() (gas: 51430)
SignatureTransferTest:testPermitTransferFromInvalidNonce() (gas: 93237)
SignatureTransferTest:testPermitTransferFromRandomNonceAndAmount(uint256,uint128) (runs: 256, μ: 95864, ~: 96840)
SignatureTransferTest:testPermitTransferFromToSpender() (gas: 93516)
SignatureTransferTest:testPermitTransferFromTypedWitness() (gas: 127964)
SignatureTransferTest:testPermitTransferFromTypedWitnessInvalidType() (gas: 58690)
SignatureTransferTest:testPermitTransferFromTypedWitnessInvalidTypeName() (gas: 59744)
SignatureTransferTest:testPermitTransferFromTypedWitnessInvalidTypehash() (gas: 57680)
SignatureTransferTest:testPermitTransferSpendLessThanFull(uint256,uint128) (runs: 256, μ: 98199, ~: 99917)
SignatureTransferTest:testPermitBatchTransferMultiAddr() (gas: 160453)
SignatureTransferTest:testPermitBatchTransferSingleRecipientManyTokens() (gas: 212513)
SignatureTransferTest:testPermitTransferFrom() (gas: 93055)
SignatureTransferTest:testPermitTransferFromCompactSig() (gas: 124028)
SignatureTransferTest:testPermitTransferFromIncorrectSigLength() (gas: 51277)
SignatureTransferTest:testPermitTransferFromInvalidNonce() (gas: 73272)
SignatureTransferTest:testPermitTransferFromRandomNonceAndAmount(uint256,uint128) (runs: 256, μ: 95711, ~: 96687)
SignatureTransferTest:testPermitTransferFromToSpender() (gas: 93363)
SignatureTransferTest:testPermitTransferFromTypedWitness() (gas: 127811)
SignatureTransferTest:testPermitTransferFromTypedWitnessInvalidType() (gas: 58537)
SignatureTransferTest:testPermitTransferFromTypedWitnessInvalidTypeName() (gas: 59591)
SignatureTransferTest:testPermitTransferFromTypedWitnessInvalidTypehash() (gas: 57527)
SignatureTransferTest:testPermitTransferSpendLessThanFull(uint256,uint128) (runs: 256, μ: 98046, ~: 99764)
9 changes: 4 additions & 5 deletions src/SignatureTransfer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,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();
}
}