-
Notifications
You must be signed in to change notification settings - Fork 78
/
secp256k1.rs
204 lines (167 loc) · 8.31 KB
/
secp256k1.rs
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
use crate::prelude::types::{Address, EthGas};
use crate::prelude::{sdk, vec::Vec, Borrowed, H256};
use crate::{EvmPrecompileResult, Precompile, PrecompileOutput};
use evm::{Context, ExitError};
mod costs {
use crate::prelude::types::EthGas;
pub(super) const ECRECOVER_BASE: EthGas = EthGas::new(3_000);
}
mod consts {
pub(super) const INPUT_LEN: usize = 128;
}
/// See: https://ethereum.github.io/yellowpaper/paper.pdf
/// See: https://docs.soliditylang.org/en/develop/units-and-global-variables.html#mathematical-and-cryptographic-functions
/// See: https://etherscan.io/address/0000000000000000000000000000000000000001
// Quite a few library methods rely on this and that should be changed. This
// should only be for precompiles.
pub fn ecrecover(hash: H256, signature: &[u8]) -> Result<Address, ExitError> {
assert_eq!(signature.len(), 65);
#[cfg(feature = "contract")]
return sdk::ecrecover(hash, signature).map_err(|e| ExitError::Other(Borrowed(e.as_str())));
#[cfg(not(feature = "contract"))]
internal_impl(hash, signature)
}
#[cfg(not(feature = "contract"))]
fn internal_impl(hash: H256, signature: &[u8]) -> Result<Address, ExitError> {
use sha3::Digest;
let hash = libsecp256k1::Message::parse_slice(hash.as_bytes()).unwrap();
let v = signature[64];
let signature = libsecp256k1::Signature::parse_standard_slice(&signature[0..64])
.map_err(|_| ExitError::Other(Borrowed(sdk::ECRecoverErr.as_str())))?;
let bit = match v {
0..=26 => v,
_ => v - 27,
};
if let Ok(recovery_id) = libsecp256k1::RecoveryId::parse(bit) {
if let Ok(public_key) = libsecp256k1::recover(&hash, &signature, &recovery_id) {
// recover returns a 65-byte key, but addresses come from the raw 64-byte key
let r = sha3::Keccak256::digest(&public_key.serialize()[1..]);
return Address::try_from_slice(&r[12..])
.map_err(|_| ExitError::Other(Borrowed("ERR_INCORRECT_ADDRESS")));
}
}
Err(ExitError::Other(Borrowed(sdk::ECRecoverErr.as_str())))
}
pub(super) struct ECRecover;
impl ECRecover {
pub(super) const ADDRESS: Address = super::make_address(0, 1);
}
impl Precompile for ECRecover {
fn required_gas(_input: &[u8]) -> Result<EthGas, ExitError> {
Ok(costs::ECRECOVER_BASE)
}
fn run(
&self,
input: &[u8],
target_gas: Option<EthGas>,
_context: &Context,
_is_static: bool,
) -> EvmPrecompileResult {
let cost = Self::required_gas(input)?;
if let Some(target_gas) = target_gas {
if cost > target_gas {
return Err(ExitError::OutOfGas);
}
}
let mut input = input.to_vec();
input.resize(consts::INPUT_LEN, 0);
let mut hash = [0; 32];
hash.copy_from_slice(&input[0..32]);
let mut v = [0; 32];
v.copy_from_slice(&input[32..64]);
let mut signature = [0; 65]; // signature is (r, s, v), typed (uint256, uint256, uint8)
signature[0..32].copy_from_slice(&input[64..96]); // r
signature[32..64].copy_from_slice(&input[96..128]); // s
let v_bit = match v[31] {
27 | 28 if v[..31] == [0; 31] => v[31] - 27,
_ => {
return Ok(PrecompileOutput::without_logs(cost, Vec::new()).into());
}
};
signature[64] = v_bit; // v
let address_res = ecrecover(H256::from_slice(&hash), &signature);
let output = match address_res {
Ok(a) => {
let mut output = [0u8; 32];
output[12..32].copy_from_slice(a.as_bytes());
output.to_vec()
}
Err(_) => Vec::new(),
};
Ok(PrecompileOutput::without_logs(cost, output).into())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::utils::new_context;
fn ecverify(hash: H256, signature: &[u8], signer: Address) -> bool {
matches!(ecrecover(hash, signature), Ok(s) if s == signer)
}
#[test]
fn test_ecverify() {
let hash = H256::from_slice(
&hex::decode("1111111111111111111111111111111111111111111111111111111111111111")
.unwrap(),
);
let signature =
&hex::decode("b9f0bb08640d3c1c00761cdd0121209268f6fd3816bc98b9e6f3cc77bf82b69812ac7a61788a0fdc0e19180f14c945a8e1088a27d92a74dce81c0981fb6447441b")
.unwrap();
let signer = Address::try_from_slice(
&hex::decode("1563915e194D8CfBA1943570603F7606A3115508").unwrap(),
)
.unwrap();
assert!(ecverify(hash, &signature, signer));
}
#[test]
fn test_ecrecover() {
let input = hex::decode("47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad000000000000000000000000000000000000000000000000000000000000001b650acf9d3f5f0a2c799776a1254355d5f4061762a237396a99a0e0e3fc2bcd6729514a0dacb2e623ac4abd157cb18163ff942280db4d5caad66ddf941ba12e03").unwrap();
let expected =
hex::decode("000000000000000000000000c08b5542d177ac6686946920409741463a15dddb")
.unwrap();
let res = ECRecover
.run(&input, Some(EthGas::new(3_000)), &new_context(), false)
.unwrap()
.output;
assert_eq!(res, expected);
// out of gas
let input = hex::decode("47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad000000000000000000000000000000000000000000000000000000000000001b650acf9d3f5f0a2c799776a1254355d5f4061762a237396a99a0e0e3fc2bcd6729514a0dacb2e623ac4abd157cb18163ff942280db4d5caad66ddf941ba12e03").unwrap();
let res = ECRecover.run(&input, Some(EthGas::new(2_999)), &new_context(), false);
assert!(matches!(res, Err(ExitError::OutOfGas)));
// bad inputs
let input = hex::decode("47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad000000000000000000000000000000000000000000000000000000000000001a650acf9d3f5f0a2c799776a1254355d5f4061762a237396a99a0e0e3fc2bcd6729514a0dacb2e623ac4abd157cb18163ff942280db4d5caad66ddf941ba12e03").unwrap();
let expected: Vec<u8> = Vec::new();
let res = ECRecover
.run(&input, Some(EthGas::new(3_000)), &new_context(), false)
.unwrap()
.output;
assert_eq!(res, expected);
let input = hex::decode("47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad000000000000000000000000000000000000000000000000000000000000001b000000000000000000000000000000000000000000000000000000000000001b0000000000000000000000000000000000000000000000000000000000000000").unwrap();
let expected: Vec<u8> = Vec::new();
let res = ECRecover
.run(&input, Some(EthGas::new(3_000)), &new_context(), false)
.unwrap()
.output;
assert_eq!(res, expected);
let input = hex::decode("47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad000000000000000000000000000000000000000000000000000000000000001b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001b").unwrap();
let expected: Vec<u8> = Vec::new();
let res = ECRecover
.run(&input, Some(EthGas::new(3_000)), &new_context(), false)
.unwrap()
.output;
assert_eq!(res, expected);
let input = hex::decode("47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad000000000000000000000000000000000000000000000000000000000000001bffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000000000000000000000000000000000000000000000000000000000001b").unwrap();
let expected: Vec<u8> = Vec::new();
let res = ECRecover
.run(&input, Some(EthGas::new(3_000)), &new_context(), false)
.unwrap()
.output;
assert_eq!(res, expected);
// Why is this test returning an address???
// let input = hex::decode("47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad000000000000000000000000000000000000000000000000000000000000001b000000000000000000000000000000000000000000000000000000000000001bffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap();
// let expected: Vec<u8> = Vec::new();
//
// let res = ecrecover_raw(&input, Some(500)).unwrap().output;
// assert_eq!(res, expected);
}
}