Skip to content

Commit

Permalink
Tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
Vectorized committed Jun 21, 2023
1 parent 061a436 commit 61283c4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ ECDSATest:testBytesToEthSignedMessageHashLong() (gas: 677)
ECDSATest:testBytesToEthSignedMessageHashLongZeroBytes() (gas: 2872560)
ECDSATest:testBytesToEthSignedMessageHashShort() (gas: 593)
ECDSATest:testEmptyCalldataHelpers() (gas: 525)
ECDSATest:testRecoverAndTryRecover(bytes32) (runs: 256, μ: 43668, ~: 39655)
ECDSATest:testRecoverAndTryRecover(bytes32) (runs: 256, μ: 43983, ~: 39781)
ECDSATest:testRecoverWithInvalidLongSignatureReverts() (gas: 7416)
ECDSATest:testRecoverWithInvalidShortSignatureReturnsZero() (gas: 7232)
ECDSATest:testRecoverWithInvalidSignatureReverts() (gas: 7973)
Expand Down Expand Up @@ -802,7 +802,7 @@ SafeTransferLibTest:testTryTransferETHWithNoGrief() (gas: 537110)
SafeTransferLibTest:testTryTransferETHWithNoStorageWrites() (gas: 192502)
SafeTransferLibTest:test__codesize() (gas: 32510)
SignatureCheckerLibTest:testEmptyCalldataHelpers() (gas: 3829)
SignatureCheckerLibTest:testSignatureChecker(bytes32) (runs: 256, μ: 54971, ~: 47170)
SignatureCheckerLibTest:testSignatureChecker(bytes32) (runs: 256, μ: 55869, ~: 50117)
SignatureCheckerLibTest:testSignatureCheckerOnEOAWithInvalidSignature() (gas: 21438)
SignatureCheckerLibTest:testSignatureCheckerOnEOAWithInvalidSigner() (gas: 30938)
SignatureCheckerLibTest:testSignatureCheckerOnEOAWithMatchingSignerAndSignature() (gas: 17860)
Expand Down
14 changes: 5 additions & 9 deletions src/utils/ECDSA.sol
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,7 @@ library ECDSA {
function toEthSignedMessageHash(bytes32 hash) internal pure returns (bytes32 result) {
/// @solidity memory-safe-assembly
assembly {
// Store into scratch space for keccak256.
mstore(0x20, hash)
mstore(0x20, hash) // Store into scratch space for keccak256.
mstore(0x00, "\x00\x00\x00\x00\x19Ethereum Signed Message:\n32") // 28 bytes.
result := keccak256(0x04, 0x3c) // `32 * 2 - (32 - 28) = 60 = 0x3c`.
}
Expand All @@ -414,28 +413,25 @@ library ECDSA {
/// This produces a hash corresponding to the one signed with the
/// [`eth_sign`](https://eth.wiki/json-rpc/API#eth_sign)
/// JSON-RPC method as part of EIP-191.
// Supports lengths up to 999999 bytes.
/// Note: Supports lengths of `s` up to 999999 bytes.
function toEthSignedMessageHash(bytes memory s) internal pure returns (bytes32 result) {
/// @solidity memory-safe-assembly
assembly {
let sLength := mload(s)
let o := 0x20
mstore(o, "\x19Ethereum Signed Message:\n") // 26 bytes, zero-right-padded.
mstore(0x00, 0x00)
// Convert the length of the bytes to ASCII decimal representation.
// Convert the `s.length` to ASCII decimal representation: `base10(s.length)`.
for { let temp := sLength } 1 {} {
o := sub(o, 1)
mstore8(o, add(48, mod(temp, 10)))
temp := div(temp, 10)
if iszero(temp) { break }
}
let n := sub(0x3a, o) // Header length: `26 + 32 - o`.
// Do an out-of-gas revert if the header length exceeds 32 bytes.
// This allows for `sLength` up to 999999, which is 6 decimal digits long.
// Throw an out-of-offset error (consumes all gas) if the header exceeds 32 bytes.
returndatacopy(returndatasize(), returndatasize(), gt(n, 0x20))
// Temporarily store the header in memory.
mstore(s, or(mload(0x00), mload(n)))
// Compute the keccak256 of the memory.
mstore(s, or(mload(0x00), mload(n))) // Temporarily store the header.
result := keccak256(add(s, sub(0x20, n)), add(n, sLength))
mstore(s, sLength) // Restore the length.
}
Expand Down

0 comments on commit 61283c4

Please sign in to comment.