ECDSA.sol 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. /**
  11. * @dev Returns the address that signed a hashed message (`hash`) with
  12. * `signature`. This address can then be used for verification purposes.
  13. *
  14. * The `ecrecover` EVM opcode allows for malleable (non-unique) signatures:
  15. * this function rejects them by requiring the `s` value to be in the lower
  16. * half order, and the `v` value to be either 27 or 28.
  17. *
  18. * IMPORTANT: `hash` _must_ be the result of a hash operation for the
  19. * verification to be secure: it is possible to craft signatures that
  20. * recover to arbitrary addresses for non-hashed data. A safe way to ensure
  21. * this is by receiving a hash of the original message (which may otherwise
  22. * be too long), and then calling {toEthSignedMessageHash} on it.
  23. *
  24. * Documentation for signature generation:
  25. * - with https://web3js.readthedocs.io/en/v1.3.4/web3-eth-accounts.html#sign[Web3.js]
  26. * - with https://docs.ethers.io/v5/api/signer/#Signer-signMessage[ethers]
  27. */
  28. function recover(bytes32 hash, bytes memory signature) internal pure returns (address) {
  29. // Check the signature length
  30. // - case 65: r,s,v signature (standard)
  31. // - case 64: r,vs signature (cf https://eips.ethereum.org/EIPS/eip-2098) _Available since v4.1._
  32. if (signature.length == 65) {
  33. bytes32 r;
  34. bytes32 s;
  35. uint8 v;
  36. // ecrecover takes the signature parameters, and the only way to get them
  37. // currently is to use assembly.
  38. assembly {
  39. r := mload(add(signature, 0x20))
  40. s := mload(add(signature, 0x40))
  41. v := byte(0, mload(add(signature, 0x60)))
  42. }
  43. return recover(hash, v, r, s);
  44. } else if (signature.length == 64) {
  45. bytes32 r;
  46. bytes32 vs;
  47. // ecrecover takes the signature parameters, and the only way to get them
  48. // currently is to use assembly.
  49. assembly {
  50. r := mload(add(signature, 0x20))
  51. vs := mload(add(signature, 0x40))
  52. }
  53. return recover(hash, r, vs);
  54. } else {
  55. revert("ECDSA: invalid signature length");
  56. }
  57. }
  58. /**
  59. * @dev Overload of {ECDSA-recover} that receives the `r` and `vs` short-signature fields separately.
  60. *
  61. * See https://eips.ethereum.org/EIPS/eip-2098[EIP-2098 short signatures]
  62. *
  63. * _Available since v4.2._
  64. */
  65. function recover(
  66. bytes32 hash,
  67. bytes32 r,
  68. bytes32 vs
  69. ) internal pure returns (address) {
  70. bytes32 s;
  71. uint8 v;
  72. assembly {
  73. s := and(vs, 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff)
  74. v := add(shr(255, vs), 27)
  75. }
  76. return recover(hash, v, r, s);
  77. }
  78. /**
  79. * @dev Overload of {ECDSA-recover} that receives the `v`, `r` and `s` signature fields separately.
  80. */
  81. function recover(
  82. bytes32 hash,
  83. uint8 v,
  84. bytes32 r,
  85. bytes32 s
  86. ) internal pure returns (address) {
  87. // EIP-2 still allows signature malleability for ecrecover(). Remove this possibility and make the signature
  88. // unique. Appendix F in the Ethereum Yellow paper (https://ethereum.github.io/yellowpaper/paper.pdf), defines
  89. // the valid range for s in (301): 0 < s < secp256k1n ÷ 2 + 1, and for v in (302): v ∈ {27, 28}. Most
  90. // signatures from current libraries generate a unique signature with an s-value in the lower half order.
  91. //
  92. // If your library generates malleable signatures, such as s-values in the upper range, calculate a new s-value
  93. // with 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 - s1 and flip v from 27 to 28 or
  94. // vice versa. If your library also generates signatures with 0/1 for v instead 27/28, add 27 to v to accept
  95. // these malleable signatures as well.
  96. require(
  97. uint256(s) <= 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0,
  98. "ECDSA: invalid signature 's' value"
  99. );
  100. require(v == 27 || v == 28, "ECDSA: invalid signature 'v' value");
  101. // If the signature is valid (and not malleable), return the signer address
  102. address signer = ecrecover(hash, v, r, s);
  103. require(signer != address(0), "ECDSA: invalid signature");
  104. return signer;
  105. }
  106. /**
  107. * @dev Returns an Ethereum Signed Message, created from a `hash`. This
  108. * produces hash corresponding to the one signed with the
  109. * https://eth.wiki/json-rpc/API#eth_sign[`eth_sign`]
  110. * JSON-RPC method as part of EIP-191.
  111. *
  112. * See {recover}.
  113. */
  114. function toEthSignedMessageHash(bytes32 hash) internal pure returns (bytes32) {
  115. // 32 is the length in bytes of hash,
  116. // enforced by the type signature above
  117. return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", hash));
  118. }
  119. /**
  120. * @dev Returns an Ethereum Signed Typed Data, created from a
  121. * `domainSeparator` and a `structHash`. This produces hash corresponding
  122. * to the one signed with the
  123. * https://eips.ethereum.org/EIPS/eip-712[`eth_signTypedData`]
  124. * JSON-RPC method as part of EIP-712.
  125. *
  126. * See {recover}.
  127. */
  128. function toTypedDataHash(bytes32 domainSeparator, bytes32 structHash) internal pure returns (bytes32) {
  129. return keccak256(abi.encodePacked("\x19\x01", domainSeparator, structHash));
  130. }
  131. }