diff --git a/.forge-snapshots/permitBatchTransferFromMultipleTokens.snap b/.forge-snapshots/permitBatchTransferFromMultipleTokens.snap index 85a1adbe..ba0b9890 100644 --- a/.forge-snapshots/permitBatchTransferFromMultipleTokens.snap +++ b/.forge-snapshots/permitBatchTransferFromMultipleTokens.snap @@ -1 +1 @@ -143701 \ No newline at end of file +143548 \ No newline at end of file diff --git a/.forge-snapshots/permitBatchTransferFromSingleToken.snap b/.forge-snapshots/permitBatchTransferFromSingleToken.snap index 40cd7da8..46ff9e95 100644 --- a/.forge-snapshots/permitBatchTransferFromSingleToken.snap +++ b/.forge-snapshots/permitBatchTransferFromSingleToken.snap @@ -1 +1 @@ -89017 \ No newline at end of file +88864 \ No newline at end of file diff --git a/.forge-snapshots/permitTransferFromBatchTypedWitness.snap b/.forge-snapshots/permitTransferFromBatchTypedWitness.snap index 91e8f6d4..b9ec0f7d 100644 --- a/.forge-snapshots/permitTransferFromBatchTypedWitness.snap +++ b/.forge-snapshots/permitTransferFromBatchTypedWitness.snap @@ -1 +1 @@ -121451 \ No newline at end of file +121298 \ No newline at end of file diff --git a/.forge-snapshots/permitTransferFromCompactSig.snap b/.forge-snapshots/permitTransferFromCompactSig.snap index 122e4017..adf64bd3 100644 --- a/.forge-snapshots/permitTransferFromCompactSig.snap +++ b/.forge-snapshots/permitTransferFromCompactSig.snap @@ -1 +1 @@ -88685 \ No newline at end of file +88532 \ No newline at end of file diff --git a/.forge-snapshots/permitTransferFromSingleToken.snap b/.forge-snapshots/permitTransferFromSingleToken.snap index 6c3f5018..d54c0afa 100644 --- a/.forge-snapshots/permitTransferFromSingleToken.snap +++ b/.forge-snapshots/permitTransferFromSingleToken.snap @@ -1 +1 @@ -88737 \ No newline at end of file +88584 \ No newline at end of file diff --git a/.forge-snapshots/permitTransferFromTypedWitness.snap b/.forge-snapshots/permitTransferFromTypedWitness.snap index 93cd094f..9e71284b 100644 --- a/.forge-snapshots/permitTransferFromTypedWitness.snap +++ b/.forge-snapshots/permitTransferFromTypedWitness.snap @@ -1 +1 @@ -91157 \ No newline at end of file +91004 \ No newline at end of file diff --git a/.forge-snapshots/single recipient 2 tokens.snap b/.forge-snapshots/single recipient 2 tokens.snap index e6558df0..71dfe54a 100644 --- a/.forge-snapshots/single recipient 2 tokens.snap +++ b/.forge-snapshots/single recipient 2 tokens.snap @@ -1 +1 @@ -118757 \ No newline at end of file +118604 \ No newline at end of file diff --git a/.forge-snapshots/single recipient many tokens.snap b/.forge-snapshots/single recipient many tokens.snap index 497b063b..54e652c9 100644 --- a/.forge-snapshots/single recipient many tokens.snap +++ b/.forge-snapshots/single recipient many tokens.snap @@ -1 +1 @@ -134435 \ No newline at end of file +134282 \ No newline at end of file diff --git a/.gas-snapshot b/.gas-snapshot index 32bba8d2..a1693c16 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -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) @@ -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) diff --git a/src/SignatureTransfer.sol b/src/SignatureTransfer.sol index ae5940db..a159c9ec 100644 --- a/src/SignatureTransfer.sol +++ b/src/SignatureTransfer.sol @@ -146,7 +146,7 @@ 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 @@ -154,10 +154,9 @@ contract SignatureTransfer is ISignatureTransfer, EIP712 { /// @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; - if ((bitmap >> bitPos) & 1 == 1) revert InvalidNonce(); - - nonceBitmap[from][wordPos] = bitmap | (1 << bitPos); + if (flipped & bit == 0) revert InvalidNonce(); } }