ECDSA.sol 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. /**
  4. * @dev Elliptic Curve Digital Signature Algorithm (ECDSA) operations.
  5. *
  6. * These functions can be used to verify that a message was signed by the holder
  7. * of the private keys of a given address.
  8. */
  9. library ECDSA {
  10. enum RecoverError {
  11. NoError,
  12. InvalidSignature,
  13. InvalidSignatureLength,
  14. InvalidSignatureS,
  15. InvalidSignatureV
  16. }
  17. function _throwError(RecoverError error) private pure {
  18. if (error == RecoverError.NoError) {
  19. return; // no error: do nothing
  20. } else if (error == RecoverError.InvalidSignature) {
  21. revert("ECDSA: invalid signature");
  22. } else if (error == RecoverError.InvalidSignatureLength) {
  23. revert("ECDSA: invalid signature length");
  24. } else if (error == RecoverError.InvalidSignatureS) {
  25. revert("ECDSA: invalid signature 's' value");
  26. } else if (error == RecoverError.InvalidSignatureV) {
  27. revert("ECDSA: invalid signature 'v' value");
  28. }
  29. }
  30. /**
  31. * @dev Returns the address that signed a hashed message (`hash`) with
  32. * `signature` or error string. This address can then be used for verification purposes.
  33. *
  34. * The `ecrecover` EVM opcode allows for malleable (non-unique) signatures:
  35. * this function rejects them by requiring the `s` value to be in the lower
  36. * half order, and the `v` value to be either 27 or 28.
  37. *
  38. * IMPORTANT: `hash` _must_ be the result of a hash operation for the
  39. * verification to be secure: it is possible to craft signatures that
  40. * recover to arbitrary addresses for non-hashed data. A safe way to ensure
  41. * this is by receiving a hash of the original message (which may otherwise
  42. * be too long), and then calling {toEthSignedMessageHash} on it.
  43. *
  44. * Documentation for signature generation:
  45. * - with https://web3js.readthedocs.io/en/v1.3.4/web3-eth-accounts.html#sign[Web3.js]
  46. * - with https://docs.ethers.io/v5/api/signer/#Signer-signMessage[ethers]
  47. *
  48. * _Available since v4.3._
  49. */
  50. function tryRecover(bytes32 hash, bytes memory signature) internal pure returns (address, RecoverError) {
  51. // Check the signature length
  52. // - case 65: r,s,v signature (standard)
  53. // - case 64: r,vs signature (cf https://eips.ethereum.org/EIPS/eip-2098) _Available since v4.1._
  54. if (signature.length == 65) {
  55. bytes32 r;
  56. bytes32 s;
  57. uint8 v;
  58. // ecrecover takes the signature parameters, and the only way to get them
  59. // currently is to use assembly.
  60. assembly {
  61. r := mload(add(signature, 0x20))
  62. s := mload(add(signature, 0x40))
  63. v := byte(0, mload(add(signature, 0x60)))
  64. }
  65. return tryRecover(hash, v, r, s);
  66. } else if (signature.length == 64) {
  67. bytes32 r;
  68. bytes32 vs;
  69. // ecrecover takes the signature parameters, and the only way to get them
  70. // currently is to use assembly.
  71. assembly {
  72. r := mload(add(signature, 0x20))
  73. vs := mload(add(signature, 0x40))
  74. }
  75. return tryRecover(hash, r, vs);
  76. } else {
  77. return (address(0), RecoverError.InvalidSignatureLength);
  78. }
  79. }
  80. /**
  81. * @dev Returns the address that signed a hashed message (`hash`) with
  82. * `signature`. This address can then be used for verification purposes.
  83. *
  84. * The `ecrecover` EVM opcode allows for malleable (non-unique) signatures:
  85. * this function rejects them by requiring the `s` value to be in the lower
  86. * half order, and the `v` value to be either 27 or 28.
  87. *
  88. * IMPORTANT: `hash` _must_ be the result of a hash operation for the
  89. * verification to be secure: it is possible to craft signatures that
  90. * recover to arbitrary addresses for non-hashed data. A safe way to ensure
  91. * this is by receiving a hash of the original message (which may otherwise
  92. * be too long), and then calling {toEthSignedMessageHash} on it.
  93. */
  94. function recover(bytes32 hash, bytes memory signature) internal pure returns (address) {
  95. (address recovered, RecoverError error) = tryRecover(hash, signature);
  96. _throwError(error);
  97. return recovered;
  98. }
  99. /**
  100. * @dev Overload of {ECDSA-tryRecover} that receives the `r` and `vs` short-signature fields separately.
  101. *
  102. * See https://eips.ethereum.org/EIPS/eip-2098[EIP-2098 short signatures]
  103. *
  104. * _Available since v4.3._
  105. */
  106. function tryRecover(
  107. bytes32 hash,
  108. bytes32 r,
  109. bytes32 vs
  110. ) internal pure returns (address, RecoverError) {
  111. bytes32 s;
  112. uint8 v;
  113. assembly {
  114. s := and(vs, 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff)
  115. v := add(shr(255, vs), 27)
  116. }
  117. return tryRecover(hash, v, r, s);
  118. }
  119. /**
  120. * @dev Overload of {ECDSA-recover} that receives the `r and `vs` short-signature fields separately.
  121. *
  122. * _Available since v4.2._
  123. */
  124. function recover(
  125. bytes32 hash,
  126. bytes32 r,
  127. bytes32 vs
  128. ) internal pure returns (address) {
  129. (address recovered, RecoverError error) = tryRecover(hash, r, vs);
  130. _throwError(error);
  131. return recovered;
  132. }
  133. /**
  134. * @dev Overload of {ECDSA-tryRecover} that receives the `v`,
  135. * `r` and `s` signature fields separately.
  136. *
  137. * _Available since v4.3._
  138. */
  139. function tryRecover(
  140. bytes32 hash,
  141. uint8 v,
  142. bytes32 r,
  143. bytes32 s
  144. ) internal pure returns (address, RecoverError) {
  145. // EIP-2 still allows signature malleability for ecrecover(). Remove this possibility and make the signature
  146. // unique. Appendix F in the Ethereum Yellow paper (https://ethereum.github.io/yellowpaper/paper.pdf), defines
  147. // the valid range for s in (301): 0 < s < secp256k1n ÷ 2 + 1, and for v in (302): v ∈ {27, 28}. Most
  148. // signatures from current libraries generate a unique signature with an s-value in the lower half order.
  149. //
  150. // If your library generates malleable signatures, such as s-values in the upper range, calculate a new s-value
  151. // with 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 - s1 and flip v from 27 to 28 or
  152. // vice versa. If your library also generates signatures with 0/1 for v instead 27/28, add 27 to v to accept
  153. // these malleable signatures as well.
  154. if (uint256(s) > 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) {
  155. return (address(0), RecoverError.InvalidSignatureS);
  156. }
  157. if (v != 27 && v != 28) {
  158. return (address(0), RecoverError.InvalidSignatureV);
  159. }
  160. // If the signature is valid (and not malleable), return the signer address
  161. address signer = ecrecover(hash, v, r, s);
  162. if (signer == address(0)) {
  163. return (address(0), RecoverError.InvalidSignature);
  164. }
  165. return (signer, RecoverError.NoError);
  166. }
  167. /**
  168. * @dev Overload of {ECDSA-recover} that receives the `v`,
  169. * `r` and `s` signature fields separately.
  170. */
  171. function recover(
  172. bytes32 hash,
  173. uint8 v,
  174. bytes32 r,
  175. bytes32 s
  176. ) internal pure returns (address) {
  177. (address recovered, RecoverError error) = tryRecover(hash, v, r, s);
  178. _throwError(error);
  179. return recovered;
  180. }
  181. /**
  182. * @dev Returns an Ethereum Signed Message, created from a `hash`. This
  183. * produces hash corresponding to the one signed with the
  184. * https://eth.wiki/json-rpc/API#eth_sign[`eth_sign`]
  185. * JSON-RPC method as part of EIP-191.
  186. *
  187. * See {recover}.
  188. */
  189. function toEthSignedMessageHash(bytes32 hash) internal pure returns (bytes32) {
  190. // 32 is the length in bytes of hash,
  191. // enforced by the type signature above
  192. return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", hash));
  193. }
  194. /**
  195. * @dev Returns an Ethereum Signed Typed Data, created from a
  196. * `domainSeparator` and a `structHash`. This produces hash corresponding
  197. * to the one signed with the
  198. * https://eips.ethereum.org/EIPS/eip-712[`eth_signTypedData`]
  199. * JSON-RPC method as part of EIP-712.
  200. *
  201. * See {recover}.
  202. */
  203. function toTypedDataHash(bytes32 domainSeparator, bytes32 structHash) internal pure returns (bytes32) {
  204. return keccak256(abi.encodePacked("\x19\x01", domainSeparator, structHash));
  205. }
  206. }