-
Notifications
You must be signed in to change notification settings - Fork 331
Expand file tree
/
Copy pathsecp256k1.cpp
More file actions
266 lines (210 loc) · 8.15 KB
/
secp256k1.cpp
File metadata and controls
266 lines (210 loc) · 8.15 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// evmone: Fast Ethereum Virtual Machine implementation
// Copyright 2023 The evmone Authors.
// SPDX-License-Identifier: Apache-2.0
#include "secp256k1.hpp"
#include "keccak.hpp"
namespace evmmax::secp256k1
{
namespace
{
constexpr auto B = ecc::FieldElement<Curve>{7};
constexpr AffinePoint G{0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798_u256,
0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8_u256};
} // namespace
// FIXME: Change to "uncompress_point".
std::optional<ecc::FieldElement<Curve>> calculate_y(
const ecc::FieldElement<Curve>& x, bool y_parity) noexcept
{
// Calculate y = √(x³ + 7).
const auto xxx = x * x * x;
const auto opt_y = field_sqrt(xxx + B);
if (!opt_y.has_value())
return std::nullopt;
// Negate if different parity requested.
const auto& y = *opt_y;
const auto candidate_parity = (y.value() & 1) != 0;
return (candidate_parity == y_parity) ? y : -y;
}
evmc::address to_address(const AffinePoint& pt) noexcept
{
// This performs Ethereum's address hashing on an uncompressed pubkey.
uint8_t serialized[64];
pt.to_bytes(serialized);
const auto hashed = ethash::keccak256(serialized, sizeof(serialized));
evmc::address ret{};
std::memcpy(ret.bytes, hashed.bytes + 12, 20);
return ret;
}
std::optional<AffinePoint> secp256k1_ecdsa_recover(std::span<const uint8_t, 32> hash,
std::span<const uint8_t, 32> r_bytes, std::span<const uint8_t, 32> s_bytes,
bool parity) noexcept
{
// Follows "Elliptic Curve Digital Signature Algorithm - Public key recovery"
// https://en.wikipedia.org/wiki/Elliptic_Curve_Digital_Signature_Algorithm#Public_key_recovery
const auto r = intx::be::unsafe::load<uint256>(r_bytes.data());
const auto s = intx::be::unsafe::load<uint256>(s_bytes.data());
// 1. Validate r and s are within [1, n-1].
if (r == 0 || r >= Curve::ORDER || s == 0 || s >= Curve::ORDER) [[unlikely]]
return std::nullopt;
// static_assert(Curve::BETA.value() ==
// 0x851695D49A83F8F47C65C9F7A2F5D6C71AE3A4617C510EA45F0E1E4A1E6D8B63_u256);
// 3. Hash of the message is already calculated in e.
// 4. Convert hash e to z field element by doing z = e % n.
// https://www.rfc-editor.org/rfc/rfc6979#section-2.3.2
// Converting to Montgomery form performs the e % n reduction.
const ModArith n{Curve::ORDER};
const auto z = intx::be::unsafe::load<uint256>(hash.data());
const auto z_mont = n.to_mont(z);
// 5. Calculate u1 and u2.
const auto r_n = n.to_mont(r);
const auto r_inv = n.inv(r_n);
const auto z_neg = n.sub(0, z_mont);
const auto u1_mont = n.mul(z_neg, r_inv);
const auto u1 = n.from_mont(u1_mont);
const auto s_mont = n.to_mont(s);
const auto u2_mont = n.mul(s_mont, r_inv);
const auto u2 = n.from_mont(u2_mont);
assert(u2 != 0); // Because s != 0 and r_inv != 0.
// 2. Calculate y coordinate of R from r and v.
const auto r_mont = ecc::FieldElement<Curve>{r};
const auto y = calculate_y(r_mont, parity);
if (!y.has_value()) [[unlikely]]
return std::nullopt;
// 6. Calculate public key point Q = u1×G + u2×R.
const auto R = AffinePoint{r_mont, *y};
// u1 and u2 are less than `Curve::ORDER`, so the multiplications will not reduce.
const auto [u1k1, u1k2] = ecc::decompose<Curve>(u1);
const auto [u2k1, u2k2] = ecc::decompose<Curve>(u2);
const auto LG = AffinePoint{Curve::BETA * G.x, !u1k2.sign ? G.y : -G.y};
const auto LR = AffinePoint{Curve::BETA * R.x, !u2k2.sign ? R.y : -R.y};
const auto Q = shamir_multiply(u1k1.value, !u1k1.sign ? G : AffinePoint{G.x, -G.y}, u1k2.value,
LG, u2k1.value, !u2k1.sign ? R : AffinePoint{R.x, -R.y}, u2k2.value, LR);
// The public key mustn't be the point at infinity. This check is cheaper on a non-affine point.
if (Q == 0) [[unlikely]]
return std::nullopt;
return to_affine(Q);
}
std::optional<evmc::address> ecrecover(std::span<const uint8_t, 32> hash,
std::span<const uint8_t, 32> r_bytes, std::span<const uint8_t, 32> s_bytes,
bool parity) noexcept
{
// TODO(C++23): use std::optional::and_then.
const auto pubkey = secp256k1_ecdsa_recover(hash, r_bytes, s_bytes, parity);
if (!pubkey.has_value())
return std::nullopt;
return to_address(*pubkey);
}
std::optional<ecc::FieldElement<Curve>> field_sqrt(const ecc::FieldElement<Curve>& x) noexcept
{
// Computes modular exponentiation
// x^0x3fffffffffffffffffffffffffffffffffffffffffffffffffffffffbfffff0c
// Operations: 253 squares 13 multiplies
// Main part generated by github.com/mmcloughlin/addchain v0.4.0.
// addchain search 0x3fffffffffffffffffffffffffffffffffffffffffffffffffffffffbfffff0c
// > secp256k1_sqrt.acc
// addchain gen -tmpl expmod.tmpl secp256k1_sqrt.acc
// > secp256k1_sqrt.cpp
//
// Exponentiation computation is derived from the addition chain:
//
// _10 = 2*1
// _11 = 1 + _10
// _1100 = _11 << 2
// _1111 = _11 + _1100
// _11110 = 2*_1111
// _11111 = 1 + _11110
// _1111100 = _11111 << 2
// _1111111 = _11 + _1111100
// x11 = _1111111 << 4 + _1111
// x22 = x11 << 11 + x11
// x27 = x22 << 5 + _11111
// x54 = x27 << 27 + x27
// x108 = x54 << 54 + x54
// x216 = x108 << 108 + x108
// x223 = x216 << 7 + _1111111
// return ((x223 << 23 + x22) << 6 + _11) << 2
// Allocate Temporaries.
ecc::FieldElement<Curve> z;
ecc::FieldElement<Curve> t0;
ecc::FieldElement<Curve> t1;
ecc::FieldElement<Curve> t2;
ecc::FieldElement<Curve> t3;
// Step 1: z = x^0x2
z = x * x;
// Step 2: z = x^0x3
z = x * z;
// Step 4: t0 = x^0xc
t0 = z * z;
for (int i = 1; i < 2; ++i)
t0 = t0 * t0;
// Step 5: t0 = x^0xf
t0 = z * t0;
// Step 6: t1 = x^0x1e
t1 = t0 * t0;
// Step 7: t2 = x^0x1f
t2 = x * t1;
// Step 9: t1 = x^0x7c
t1 = t2 * t2;
for (int i = 1; i < 2; ++i)
t1 = t1 * t1;
// Step 10: t1 = x^0x7f
t1 = z * t1;
// Step 14: t3 = x^0x7f0
t3 = t1 * t1;
for (int i = 1; i < 4; ++i)
t3 = t3 * t3;
// Step 15: t0 = x^0x7ff
t0 = t0 * t3;
// Step 26: t3 = x^0x3ff800
t3 = t0 * t0;
for (int i = 1; i < 11; ++i)
t3 = t3 * t3;
// Step 27: t0 = x^0x3fffff
t0 = t0 * t3;
// Step 32: t3 = x^0x7ffffe0
t3 = t0 * t0;
for (int i = 1; i < 5; ++i)
t3 = t3 * t3;
// Step 33: t2 = x^0x7ffffff
t2 = t2 * t3;
// Step 60: t3 = x^0x3ffffff8000000
t3 = t2 * t2;
for (int i = 1; i < 27; ++i)
t3 = t3 * t3;
// Step 61: t2 = x^0x3fffffffffffff
t2 = t2 * t3;
// Step 115: t3 = x^0xfffffffffffffc0000000000000
t3 = t2 * t2;
for (int i = 1; i < 54; ++i)
t3 = t3 * t3;
// Step 116: t2 = x^0xfffffffffffffffffffffffffff
t2 = t2 * t3;
// Step 224: t3 = x^0xfffffffffffffffffffffffffff000000000000000000000000000
t3 = t2 * t2;
for (int i = 1; i < 108; ++i)
t3 = t3 * t3;
// Step 225: t2 = x^0xffffffffffffffffffffffffffffffffffffffffffffffffffffff
t2 = t2 * t3;
// Step 232: t2 = x^0x7fffffffffffffffffffffffffffffffffffffffffffffffffffff80
for (int i = 0; i < 7; ++i)
t2 = t2 * t2;
// Step 233: t1 = x^0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffff
t1 = t1 * t2;
// Step 256: t1 = x^0x3fffffffffffffffffffffffffffffffffffffffffffffffffffffff800000
for (int i = 0; i < 23; ++i)
t1 = t1 * t1;
// Step 257: t0 = x^0x3fffffffffffffffffffffffffffffffffffffffffffffffffffffffbfffff
t0 = t0 * t1;
// Step 263: t0 = x^0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc0
for (int i = 0; i < 6; ++i)
t0 = t0 * t0;
// Step 264: z = x^0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc3
z = z * t0;
// Step 266: z = x^0x3fffffffffffffffffffffffffffffffffffffffffffffffffffffffbfffff0c
for (int i = 0; i < 2; ++i)
z = z * z;
if (z * z != x)
return std::nullopt; // Computed value is not the square root.
return z;
}
} // namespace evmmax::secp256k1