-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathECDSA.sol
241 lines (211 loc) · 8.12 KB
/
ECDSA.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
/**
* @title Elliptic Curve Digital Signature Algorithm (ECDSA) operations
* @dev derived from https://github.com/OpenZeppelin/openzeppelin-contracts (MIT license)
*/
library ECDSA {
error ECDSA__InvalidS();
error ECDSA__InvalidSignature();
error ECDSA__InvalidSignatureLength();
error ECDSA__InvalidV();
/**
* @notice recover signer of hashed message from signature, reverting on failure
* @param hash hashed data payload
* @param signature signed data payload
* @return signer recovered message signer
*/
function recover(
bytes32 hash,
bytes memory signature
) internal pure returns (address signer) {
function() pure errorFn;
(signer, errorFn) = _tryRecover(hash, signature);
if (signer == address(0)) errorFn();
}
/**
* @notice recover signer of hashed message from signature v, r, and s values, reverting on failure
* @param hash hashed data payload
* @param v signature "v" value
* @param r signature "r" value
* @param s signature "s" value
* @return signer recovered message signer
*/
function recover(
bytes32 hash,
uint8 v,
bytes32 r,
bytes32 s
) internal pure returns (address signer) {
function() pure errorFn;
(signer, errorFn) = _tryRecover(hash, v, r, s);
if (signer == address(0)) errorFn();
}
/**
* @notice attempt to recover signer of hashed message from signature
* @param hash hashed data payload
* @param signature signed data payload
* @return signer recovered message signer (zero address on recovery failure)
*/
function tryRecover(
bytes32 hash,
bytes memory signature
) internal pure returns (address signer) {
(signer, ) = _tryRecover(hash, signature);
}
/**
* @notice attempt to recover signer of hashed message from signature v, r, and s values
* @param hash hashed data payload
* @param v signature "v" value
* @param r signature "r" value
* @param s signature "s" value
* @return signer recovered message signer (zero address on recovery failure)
*/
function tryRecover(
bytes32 hash,
uint8 v,
bytes32 r,
bytes32 s
) internal pure returns (address signer) {
(signer, ) = _tryRecover(hash, v, r, s);
}
/**
* @notice attempt to recover signer of hashed message from signature
* @param hash hashed data payload
* @param signature signed data payload
* @return signer recovered message signer (zero address on recovery failure)
* @return errorFn wrapper function around custom error revert
*/
function _tryRecover(
bytes32 hash,
bytes memory signature
) private pure returns (address signer, function() pure errorFn) {
if (signature.length != 65) {
return (address(0), _revert_ECDSA__InvalidSignatureLength);
}
bytes32 r;
bytes32 s;
uint8 v;
assembly ('memory-safe') {
r := mload(add(signature, 0x20))
s := mload(add(signature, 0x40))
v := byte(0, mload(add(signature, 0x60)))
}
(signer, errorFn) = _tryRecover(hash, v, r, s);
}
/**
* @notice attempt to recover signer of hashed message from signature v, r, and s values
* @param hash hashed data payload
* @param v signature "v" value
* @param r signature "r" value
* @param s signature "s" value
* @return signer recovered message signer (zero address on recovery failure)
* @return errorFn wrapper function around custom error revert
*/
function _tryRecover(
bytes32 hash,
uint8 v,
bytes32 r,
bytes32 s
) private pure returns (address signer, function() pure errorFn) {
// 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 (281): 0 < s < secp256k1n ÷ 2 + 1, and for v in (282): 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), _revert_ECDSA__InvalidS);
if (v != 27 && v != 28) return (address(0), _revert_ECDSA__InvalidV);
// If the signature is valid (and not malleable), return the signer address
signer = ecrecover(hash, v, r, s);
if (signer == address(0))
return (address(0), _revert_ECDSA__InvalidSignature);
}
/**
* @notice generate an "Ethereum Signed Message" in the format returned by the eth_sign JSON-RPC method
* @param payloadHash hashed data payload
* @return recoverableHash hash to validate against signature via ECDSA function
*/
function toEthSignRecoverableHash(
bytes32 payloadHash
) internal pure returns (bytes32 recoverableHash) {
assembly {
// assembly block equivalent to:
//
// recoverableHash = keccak256(
// abi.encodePacked(
// '\x19Ethereum Signed Message:\n32',
// payloadHash
// )
// );
// load free memory pointer
let pointer := mload(64)
mstore(pointer, '\x19Ethereum Signed Message:\n32')
mstore(add(pointer, 28), payloadHash)
recoverableHash := keccak256(pointer, 60)
}
}
/**
* @notice generate an EIP712 signable hash of typed structured data
* @param domainSeparator EIP712 domain separator
* @param structHash hash of underlying signed data generated through the EIP712 "hashStruct" process
* @return recoverableHash hash to validate against signature via ECDSA function
*/
function toEIP712RecoverableHash(
bytes32 domainSeparator,
bytes32 structHash
) internal pure returns (bytes32 recoverableHash) {
assembly {
// assembly block equivalent to:
//
// recoverableHash = keccak256(
// abi.encodePacked(
// uint16(0x1901),
// domainSeparator,
// structHash
// )
// );
// load free memory pointer
let pointer := mload(64)
// this magic value is the EIP-191 signed data header, consisting of
// the hardcoded 0x19 and the one-byte version 0x01
mstore(
pointer,
0x1901000000000000000000000000000000000000000000000000000000000000
)
mstore(add(pointer, 2), domainSeparator)
mstore(add(pointer, 34), structHash)
recoverableHash := keccak256(pointer, 66)
}
}
/**
* @notice wrapper function for passing custom errors internally
*/
function _revert_ECDSA__InvalidS() private pure {
revert ECDSA__InvalidS();
}
/**
* @notice wrapper function for passing custom errors internally
*/
function _revert_ECDSA__InvalidSignature() private pure {
revert ECDSA__InvalidSignature();
}
/**
* @notice wrapper function for passing custom errors internally
*/
function _revert_ECDSA__InvalidSignatureLength() private pure {
revert ECDSA__InvalidSignatureLength();
}
/**
* @notice wrapper function for passing custom errors internally
*/
function _revert_ECDSA__InvalidV() private pure {
revert ECDSA__InvalidV();
}
}