123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- // SPDX-License-Identifier: MIT
- // OpenZeppelin Contracts (last updated v5.1.0) (utils/cryptography/ECDSA.sol)
- pragma solidity ^0.8.20;
- /**
- * @dev Elliptic Curve Digital Signature Algorithm (ECDSA) operations.
- *
- * These functions can be used to verify that a message was signed by the holder
- * of the private keys of a given address.
- */
- library ECDSA {
- enum RecoverError {
- NoError,
- InvalidSignature,
- InvalidSignatureLength,
- InvalidSignatureS
- }
- /**
- * @dev The signature derives the `address(0)`.
- */
- error ECDSAInvalidSignature();
- /**
- * @dev The signature has an invalid length.
- */
- error ECDSAInvalidSignatureLength(uint256 length);
- /**
- * @dev The signature has an S value that is in the upper half order.
- */
- error ECDSAInvalidSignatureS(bytes32 s);
- /**
- * @dev Returns the address that signed a hashed message (`hash`) with `signature` or an error. This will not
- * return address(0) without also returning an error description. Errors are documented using an enum (error type)
- * and a bytes32 providing additional information about the error.
- *
- * If no error is returned, then the address can be used for verification purposes.
- *
- * The `ecrecover` EVM precompile 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.
- *
- * NOTE: This function only supports 65-byte signatures. ERC-2098 short signatures are rejected. This restriction
- * is DEPRECATED and will be removed in v6.0. Developers SHOULD NOT use signatures as unique identifiers; use hash
- * invalidation or nonces for replay protection.
- *
- * 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 {MessageHashUtils-toEthSignedMessageHash} on it.
- *
- * 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]
- */
- function tryRecover(
- bytes32 hash,
- bytes memory signature
- ) internal pure returns (address recovered, RecoverError err, bytes32 errArg) {
- if (signature.length == 65) {
- bytes32 r;
- bytes32 s;
- uint8 v;
- // ecrecover takes the signature parameters, and the only way to get them
- // currently is to use assembly.
- assembly ("memory-safe") {
- r := mload(add(signature, 0x20))
- s := mload(add(signature, 0x40))
- v := byte(0, mload(add(signature, 0x60)))
- }
- return tryRecover(hash, v, r, s);
- } else {
- return (address(0), RecoverError.InvalidSignatureLength, bytes32(signature.length));
- }
- }
- /**
- * @dev Variant of {tryRecover} that takes a signature in calldata
- */
- function tryRecoverCalldata(
- bytes32 hash,
- bytes calldata signature
- ) internal pure returns (address recovered, RecoverError err, bytes32 errArg) {
- if (signature.length == 65) {
- bytes32 r;
- bytes32 s;
- uint8 v;
- // ecrecover takes the signature parameters, calldata slices would work here, but are
- // significantly more expensive (length check) than using calldataload in assembly.
- assembly ("memory-safe") {
- r := calldataload(signature.offset)
- s := calldataload(add(signature.offset, 0x20))
- v := byte(0, calldataload(add(signature.offset, 0x40)))
- }
- return tryRecover(hash, v, r, s);
- } else {
- return (address(0), RecoverError.InvalidSignatureLength, bytes32(signature.length));
- }
- }
- /**
- * @dev Returns the address that signed a hashed message (`hash`) with
- * `signature`. This address can then be used for verification purposes.
- *
- * The `ecrecover` EVM precompile 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.
- *
- * NOTE: This function only supports 65-byte signatures. ERC-2098 short signatures are rejected. This restriction
- * is DEPRECATED and will be removed in v6.0. Developers SHOULD NOT use signatures as unique identifiers; use hash
- * invalidation or nonces for replay protection.
- *
- * 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 {MessageHashUtils-toEthSignedMessageHash} on it.
- */
- function recover(bytes32 hash, bytes memory signature) internal pure returns (address) {
- (address recovered, RecoverError error, bytes32 errorArg) = tryRecover(hash, signature);
- _throwError(error, errorArg);
- return recovered;
- }
- /**
- * @dev Variant of {recover} that takes a signature in calldata
- */
- function recoverCalldata(bytes32 hash, bytes calldata signature) internal pure returns (address) {
- (address recovered, RecoverError error, bytes32 errorArg) = tryRecoverCalldata(hash, signature);
- _throwError(error, errorArg);
- 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[ERC-2098 short signatures]
- */
- function tryRecover(
- bytes32 hash,
- bytes32 r,
- bytes32 vs
- ) internal pure returns (address recovered, RecoverError err, bytes32 errArg) {
- unchecked {
- bytes32 s = vs & bytes32(0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff);
- // We do not check for an overflow here since the shift operation results in 0 or 1.
- uint8 v = uint8((uint256(vs) >> 255) + 27);
- return tryRecover(hash, v, r, s);
- }
- }
- /**
- * @dev Overload of {ECDSA-recover} that receives the `r and `vs` short-signature fields separately.
- */
- function recover(bytes32 hash, bytes32 r, bytes32 vs) internal pure returns (address) {
- (address recovered, RecoverError error, bytes32 errorArg) = tryRecover(hash, r, vs);
- _throwError(error, errorArg);
- return recovered;
- }
- /**
- * @dev Overload of {ECDSA-tryRecover} that receives the `v`,
- * `r` and `s` signature fields separately.
- */
- function tryRecover(
- bytes32 hash,
- uint8 v,
- bytes32 r,
- bytes32 s
- ) internal pure returns (address recovered, RecoverError err, bytes32 errArg) {
- // 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
- // signatures from current libraries generate a unique signature with an s-value in the lower half order.
- //
- // If your library generates malleable signatures, such as s-values in the upper range, calculate a new s-value
- // 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.
- if (uint256(s) > 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) {
- return (address(0), RecoverError.InvalidSignatureS, s);
- }
- // If the signature is valid (and not malleable), return the signer address
- address signer = ecrecover(hash, v, r, s);
- if (signer == address(0)) {
- return (address(0), RecoverError.InvalidSignature, bytes32(0));
- }
- return (signer, RecoverError.NoError, bytes32(0));
- }
- /**
- * @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, bytes32 errorArg) = tryRecover(hash, v, r, s);
- _throwError(error, errorArg);
- return recovered;
- }
- /**
- * @dev Parse a signature into its `v`, `r` and `s` components. Supports 65-byte and 64-byte (ERC-2098)
- * formats. Returns (0,0,0) for invalid signatures. Consider skipping {tryRecover} or {recover} if so.
- */
- function parse(bytes memory signature) internal pure returns (uint8 v, bytes32 r, bytes32 s) {
- assembly ("memory-safe") {
- // Check the signature length
- switch mload(signature)
- // - case 65: r,s,v signature (standard)
- case 65 {
- r := mload(add(signature, 0x20))
- s := mload(add(signature, 0x40))
- v := byte(0, mload(add(signature, 0x60)))
- }
- // - case 64: r,vs signature (cf https://eips.ethereum.org/EIPS/eip-2098)
- case 64 {
- let vs := mload(add(signature, 0x40))
- r := mload(add(signature, 0x20))
- s := and(vs, shr(1, not(0)))
- v := add(shr(255, vs), 27)
- }
- default {
- r := 0
- s := 0
- v := 0
- }
- }
- }
- /**
- * @dev Variant of {parse} that takes a signature in calldata
- */
- function parseCalldata(bytes calldata signature) internal pure returns (uint8 v, bytes32 r, bytes32 s) {
- assembly ("memory-safe") {
- // Check the signature length
- switch signature.length
- // - case 65: r,s,v signature (standard)
- case 65 {
- r := calldataload(signature.offset)
- s := calldataload(add(signature.offset, 0x20))
- v := byte(0, calldataload(add(signature.offset, 0x40)))
- }
- // - case 64: r,vs signature (cf https://eips.ethereum.org/EIPS/eip-2098)
- case 64 {
- let vs := calldataload(add(signature.offset, 0x20))
- r := calldataload(signature.offset)
- s := and(vs, shr(1, not(0)))
- v := add(shr(255, vs), 27)
- }
- default {
- r := 0
- s := 0
- v := 0
- }
- }
- }
- /**
- * @dev Optionally reverts with the corresponding custom error according to the `error` argument provided.
- */
- function _throwError(RecoverError error, bytes32 errorArg) private pure {
- if (error == RecoverError.NoError) {
- return; // no error: do nothing
- } else if (error == RecoverError.InvalidSignature) {
- revert ECDSAInvalidSignature();
- } else if (error == RecoverError.InvalidSignatureLength) {
- revert ECDSAInvalidSignatureLength(uint256(errorArg));
- } else if (error == RecoverError.InvalidSignatureS) {
- revert ECDSAInvalidSignatureS(errorArg);
- }
- }
- }
|