Optimize EOA signature verification (#2661)
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com> Co-authored-by: Francisco Giordano <frangio.1@gmail.com>
This commit is contained in:
@ -9,9 +9,31 @@ pragma solidity ^0.8.0;
|
||||
* of the private keys of a given address.
|
||||
*/
|
||||
library ECDSA {
|
||||
enum RecoverError {
|
||||
NoError,
|
||||
InvalidSignature,
|
||||
InvalidSignatureLength,
|
||||
InvalidSignatureS,
|
||||
InvalidSignatureV
|
||||
}
|
||||
|
||||
function _throwError(RecoverError error) private pure {
|
||||
if (error == RecoverError.NoError) {
|
||||
return; // no error: do nothing
|
||||
} else if (error == RecoverError.InvalidSignature) {
|
||||
revert("ECDSA: invalid signature");
|
||||
} else if (error == RecoverError.InvalidSignatureLength) {
|
||||
revert("ECDSA: invalid signature length");
|
||||
} else if (error == RecoverError.InvalidSignatureS) {
|
||||
revert("ECDSA: invalid signature 's' value");
|
||||
} else if (error == RecoverError.InvalidSignatureV) {
|
||||
revert("ECDSA: invalid signature 'v' value");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns the address that signed a hashed message (`hash`) with
|
||||
* `signature`. This address can then be used for verification purposes.
|
||||
* `signature` or error string. This address can then be used for verification purposes.
|
||||
*
|
||||
* The `ecrecover` EVM opcode allows for malleable (non-unique) signatures:
|
||||
* this function rejects them by requiring the `s` value to be in the lower
|
||||
@ -26,8 +48,10 @@ library ECDSA {
|
||||
* Documentation for signature generation:
|
||||
* - with https://web3js.readthedocs.io/en/v1.3.4/web3-eth-accounts.html#sign[Web3.js]
|
||||
* - with https://docs.ethers.io/v5/api/signer/#Signer-signMessage[ethers]
|
||||
*
|
||||
* _Available since v4.3._
|
||||
*/
|
||||
function recover(bytes32 hash, bytes memory signature) internal pure returns (address) {
|
||||
function tryRecover(bytes32 hash, bytes memory signature) internal pure returns (address, RecoverError) {
|
||||
// Check the signature length
|
||||
// - case 65: r,s,v signature (standard)
|
||||
// - case 64: r,vs signature (cf https://eips.ethereum.org/EIPS/eip-2098) _Available since v4.1._
|
||||
@ -42,7 +66,7 @@ library ECDSA {
|
||||
s := mload(add(signature, 0x40))
|
||||
v := byte(0, mload(add(signature, 0x60)))
|
||||
}
|
||||
return recover(hash, v, r, s);
|
||||
return tryRecover(hash, v, r, s);
|
||||
} else if (signature.length == 64) {
|
||||
bytes32 r;
|
||||
bytes32 vs;
|
||||
@ -52,17 +76,56 @@ library ECDSA {
|
||||
r := mload(add(signature, 0x20))
|
||||
vs := mload(add(signature, 0x40))
|
||||
}
|
||||
return recover(hash, r, vs);
|
||||
return tryRecover(hash, r, vs);
|
||||
} else {
|
||||
revert("ECDSA: invalid signature length");
|
||||
return (address(0), RecoverError.InvalidSignatureLength);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Overload of {ECDSA-recover} that receives the `r` and `vs` short-signature fields separately.
|
||||
* @dev Returns the address that signed a hashed message (`hash`) with
|
||||
* `signature`. This address can then be used for verification purposes.
|
||||
*
|
||||
* The `ecrecover` EVM opcode allows for malleable (non-unique) signatures:
|
||||
* this function rejects them by requiring the `s` value to be in the lower
|
||||
* half order, and the `v` value to be either 27 or 28.
|
||||
*
|
||||
* IMPORTANT: `hash` _must_ be the result of a hash operation for the
|
||||
* verification to be secure: it is possible to craft signatures that
|
||||
* recover to arbitrary addresses for non-hashed data. A safe way to ensure
|
||||
* this is by receiving a hash of the original message (which may otherwise
|
||||
* be too long), and then calling {toEthSignedMessageHash} on it.
|
||||
*/
|
||||
function recover(bytes32 hash, bytes memory signature) internal pure returns (address) {
|
||||
(address recovered, RecoverError error) = tryRecover(hash, signature);
|
||||
_throwError(error);
|
||||
return recovered;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Overload of {ECDSA-tryRecover} that receives the `r` and `vs` short-signature fields separately.
|
||||
*
|
||||
* See https://eips.ethereum.org/EIPS/eip-2098[EIP-2098 short signatures]
|
||||
*
|
||||
* _Available since v4.3._
|
||||
*/
|
||||
function tryRecover(
|
||||
bytes32 hash,
|
||||
bytes32 r,
|
||||
bytes32 vs
|
||||
) internal pure returns (address, RecoverError) {
|
||||
bytes32 s;
|
||||
uint8 v;
|
||||
assembly {
|
||||
s := and(vs, 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff)
|
||||
v := add(shr(255, vs), 27)
|
||||
}
|
||||
return tryRecover(hash, v, r, s);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Overload of {ECDSA-recover} that receives the `r and `vs` short-signature fields separately.
|
||||
*
|
||||
* _Available since v4.2._
|
||||
*/
|
||||
function recover(
|
||||
@ -70,24 +133,23 @@ library ECDSA {
|
||||
bytes32 r,
|
||||
bytes32 vs
|
||||
) internal pure returns (address) {
|
||||
bytes32 s;
|
||||
uint8 v;
|
||||
assembly {
|
||||
s := and(vs, 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff)
|
||||
v := add(shr(255, vs), 27)
|
||||
}
|
||||
return recover(hash, v, r, s);
|
||||
(address recovered, RecoverError error) = tryRecover(hash, r, vs);
|
||||
_throwError(error);
|
||||
return recovered;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Overload of {ECDSA-recover} that receives the `v`, `r` and `s` signature fields separately.
|
||||
* @dev Overload of {ECDSA-tryRecover} that receives the `v`,
|
||||
* `r` and `s` signature fields separately.
|
||||
*
|
||||
* _Available since v4.3._
|
||||
*/
|
||||
function recover(
|
||||
function tryRecover(
|
||||
bytes32 hash,
|
||||
uint8 v,
|
||||
bytes32 r,
|
||||
bytes32 s
|
||||
) internal pure returns (address) {
|
||||
) internal pure returns (address, RecoverError) {
|
||||
// EIP-2 still allows signature malleability for ecrecover(). Remove this possibility and make the signature
|
||||
// unique. Appendix F in the Ethereum Yellow paper (https://ethereum.github.io/yellowpaper/paper.pdf), defines
|
||||
// the valid range for s in (301): 0 < s < secp256k1n ÷ 2 + 1, and for v in (302): v ∈ {27, 28}. Most
|
||||
@ -97,17 +159,35 @@ library ECDSA {
|
||||
// with 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 - s1 and flip v from 27 to 28 or
|
||||
// vice versa. If your library also generates signatures with 0/1 for v instead 27/28, add 27 to v to accept
|
||||
// these malleable signatures as well.
|
||||
require(
|
||||
uint256(s) <= 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0,
|
||||
"ECDSA: invalid signature 's' value"
|
||||
);
|
||||
require(v == 27 || v == 28, "ECDSA: invalid signature 'v' value");
|
||||
if (uint256(s) > 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) {
|
||||
return (address(0), RecoverError.InvalidSignatureS);
|
||||
}
|
||||
if (v != 27 && v != 28) {
|
||||
return (address(0), RecoverError.InvalidSignatureV);
|
||||
}
|
||||
|
||||
// If the signature is valid (and not malleable), return the signer address
|
||||
address signer = ecrecover(hash, v, r, s);
|
||||
require(signer != address(0), "ECDSA: invalid signature");
|
||||
if (signer == address(0)) {
|
||||
return (address(0), RecoverError.InvalidSignature);
|
||||
}
|
||||
|
||||
return signer;
|
||||
return (signer, RecoverError.NoError);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Overload of {ECDSA-recover} that receives the `v`,
|
||||
* `r` and `s` signature fields separately.
|
||||
*/
|
||||
function recover(
|
||||
bytes32 hash,
|
||||
uint8 v,
|
||||
bytes32 r,
|
||||
bytes32 s
|
||||
) internal pure returns (address) {
|
||||
(address recovered, RecoverError error) = tryRecover(hash, v, r, s);
|
||||
_throwError(error);
|
||||
return recovered;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -22,14 +22,14 @@ library SignatureChecker {
|
||||
bytes32 hash,
|
||||
bytes memory signature
|
||||
) internal view returns (bool) {
|
||||
if (Address.isContract(signer)) {
|
||||
try IERC1271(signer).isValidSignature(hash, signature) returns (bytes4 magicValue) {
|
||||
return magicValue == IERC1271.isValidSignature.selector;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return ECDSA.recover(hash, signature) == signer;
|
||||
(address recovered, ECDSA.RecoverError error) = ECDSA.tryRecover(hash, signature);
|
||||
if (error == ECDSA.RecoverError.NoError && recovered == signer) {
|
||||
return true;
|
||||
}
|
||||
|
||||
(bool success, bytes memory result) = signer.staticcall(
|
||||
abi.encodeWithSelector(IERC1271.isValidSignature.selector, hash, signature)
|
||||
);
|
||||
return (success && result.length == 32 && abi.decode(result, (bytes4)) == IERC1271.isValidSignature.selector);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user