-
Notifications
You must be signed in to change notification settings - Fork 334
Expand file tree
/
Copy pathprecompiles.cpp
More file actions
382 lines (318 loc) · 13.3 KB
/
Copy pathprecompiles.cpp
File metadata and controls
382 lines (318 loc) · 13.3 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// evmone: Fast Ethereum Virtual Machine implementation
// Copyright 2022 The evmone Authors.
// SPDX-License-Identifier: Apache-2.0
#include "precompiles.hpp"
#include "precompiles_internal.hpp"
#include "precompiles_stubs.hpp"
#include <evmone_precompiles/blake2b.hpp>
#include <evmone_precompiles/bn254.hpp>
#include <evmone_precompiles/ripemd160.hpp>
#include <evmone_precompiles/secp256k1.hpp>
#include <evmone_precompiles/sha256.hpp>
#include <intx/intx.hpp>
#include <array>
#include <bit>
#include <cassert>
#include <limits>
#ifdef EVMONE_PRECOMPILES_SILKPRE
#include "precompiles_silkpre.hpp"
#endif
namespace evmone::state
{
using evmc::bytes;
using evmc::bytes_view;
using namespace evmc::literals;
namespace
{
constexpr auto GasCostMax = std::numeric_limits<int64_t>::max();
inline constexpr int64_t num_words(size_t size_in_bytes) noexcept
{
return static_cast<int64_t>(size_in_bytes / 32 + static_cast<size_t>(size_in_bytes % 32 != 0));
}
template <int BaseCost, int WordCost>
inline constexpr int64_t cost_per_input_word(size_t input_size) noexcept
{
return BaseCost + WordCost * num_words(input_size);
}
} // namespace
PrecompileAnalysis ecrecover_analyze(bytes_view /*input*/, evmc_revision /*rev*/) noexcept
{
return {3000, 32};
}
PrecompileAnalysis sha256_analyze(bytes_view input, evmc_revision /*rev*/) noexcept
{
return {cost_per_input_word<60, 12>(input.size()), 32};
}
PrecompileAnalysis ripemd160_analyze(bytes_view input, evmc_revision /*rev*/) noexcept
{
return {cost_per_input_word<600, 120>(input.size()), 32};
}
PrecompileAnalysis identity_analyze(bytes_view input, evmc_revision /*rev*/) noexcept
{
return {cost_per_input_word<15, 3>(input.size()), input.size()};
}
PrecompileAnalysis ecadd_analyze(bytes_view /*input*/, evmc_revision rev) noexcept
{
return {rev >= EVMC_ISTANBUL ? 150 : 500, 64};
}
PrecompileAnalysis ecmul_analyze(bytes_view /*input*/, evmc_revision rev) noexcept
{
return {rev >= EVMC_ISTANBUL ? 6000 : 40000, 64};
}
PrecompileAnalysis ecpairing_analyze(bytes_view input, evmc_revision rev) noexcept
{
const auto base_cost = (rev >= EVMC_ISTANBUL) ? 45000 : 100000;
const auto element_cost = (rev >= EVMC_ISTANBUL) ? 34000 : 80000;
const auto num_elements = static_cast<int64_t>(input.size() / 192);
return {base_cost + num_elements * element_cost, 32};
}
PrecompileAnalysis blake2bf_analyze(bytes_view input, evmc_revision) noexcept
{
return {input.size() == 213 ? intx::be::unsafe::load<uint32_t>(input.data()) : GasCostMax, 64};
}
PrecompileAnalysis expmod_analyze(bytes_view input, evmc_revision rev) noexcept
{
using namespace intx;
static constexpr size_t input_header_required_size = 3 * sizeof(uint256);
const int64_t min_gas = (rev >= EVMC_BERLIN) ? 200 : 0;
uint8_t input_header[input_header_required_size]{};
std::copy_n(input.data(), std::min(input.size(), input_header_required_size), input_header);
const auto base_len = be::unsafe::load<uint256>(&input_header[0]);
const auto exp_len = be::unsafe::load<uint256>(&input_header[32]);
const auto mod_len = be::unsafe::load<uint256>(&input_header[64]);
if (base_len == 0 && mod_len == 0)
return {min_gas, 0};
static constexpr auto len_limit = std::numeric_limits<size_t>::max();
if (base_len > len_limit || exp_len > len_limit || mod_len > len_limit)
return {GasCostMax, 0};
auto adjusted_len = [input](size_t offset, size_t len) {
const auto head_len = std::min(len, size_t{32});
const auto head_explicit_len =
std::max(std::min(offset + head_len, input.size()), offset) - offset;
const bytes_view head_explicit_bytes(&input[offset], head_explicit_len);
const auto top_byte_index = head_explicit_bytes.find_first_not_of(uint8_t{0});
const size_t exp_bit_width =
(top_byte_index != bytes_view::npos) ?
(head_len - top_byte_index - 1) * 8 +
static_cast<size_t>(std::bit_width(head_explicit_bytes[top_byte_index])) :
0;
return std::max(
8 * (std::max(len, size_t{32}) - 32) + (std::max(exp_bit_width, size_t{1}) - 1),
size_t{1});
};
static constexpr auto mult_complexity_eip2565 = [](const uint256& x) noexcept {
const auto w = (x + 7) >> 3;
return w * w;
};
static constexpr auto mult_complexity_eip198 = [](const uint256& x) noexcept {
const auto x2 = x * x;
if (x <= 64)
return x2;
else if (x <= 1024)
return (x2 >> 2) + 96 * x - 3072;
else
return (x2 >> 4) + 480 * x - 199680;
};
const auto max_len = std::max(mod_len, base_len);
const auto adjusted_exp_len = adjusted_len(
sizeof(input_header) + static_cast<size_t>(base_len), static_cast<size_t>(exp_len));
const auto gas = (rev >= EVMC_BERLIN) ?
mult_complexity_eip2565(max_len) * adjusted_exp_len / 3 :
mult_complexity_eip198(max_len) * adjusted_exp_len / 20;
return {std::max(min_gas, static_cast<int64_t>(std::min(gas, intx::uint256{GasCostMax}))),
static_cast<size_t>(mod_len)};
}
PrecompileAnalysis point_evaluation_analyze(bytes_view, evmc_revision) noexcept
{
static constexpr auto POINT_EVALUATION_PRECOMPILE_GAS = 50000;
return {POINT_EVALUATION_PRECOMPILE_GAS, 64};
}
ExecutionResult ecrecover_execute(const uint8_t* input, size_t input_size, uint8_t* output,
[[maybe_unused]] size_t output_size) noexcept
{
assert(output_size >= 32);
uint8_t input_buffer[128]{};
if (input_size != 0)
std::memcpy(input_buffer, input, std::min(input_size, std::size(input_buffer)));
ethash::hash256 h{};
std::memcpy(h.bytes, input_buffer, sizeof(h));
const auto v = intx::be::unsafe::load<intx::uint256>(input_buffer + 32);
if (v != 27 && v != 28)
return {EVMC_SUCCESS, 0};
const bool parity = v == 28;
const auto r = intx::be::unsafe::load<intx::uint256>(input_buffer + 64);
const auto s = intx::be::unsafe::load<intx::uint256>(input_buffer + 96);
const auto res = evmmax::secp256k1::ecrecover(h, r, s, parity);
if (res)
{
std::memset(output, 0, 12);
std::memcpy(output + 12, res->bytes, 20);
return {EVMC_SUCCESS, 32};
}
else
return {EVMC_SUCCESS, 0};
}
ExecutionResult sha256_execute(const uint8_t* input, size_t input_size, uint8_t* output,
[[maybe_unused]] size_t output_size) noexcept
{
assert(output_size >= 32);
crypto::sha256(reinterpret_cast<std::byte*>(output), reinterpret_cast<const std::byte*>(input),
input_size);
return {EVMC_SUCCESS, 32};
}
ExecutionResult ripemd160_execute(const uint8_t* input, size_t input_size, uint8_t* output,
[[maybe_unused]] size_t output_size) noexcept
{
assert(output_size >= 32);
output = std::fill_n(output, 12, std::uint8_t{0});
crypto::ripemd160(reinterpret_cast<std::byte*>(output),
reinterpret_cast<const std::byte*>(input), input_size);
return {EVMC_SUCCESS, 32};
}
ExecutionResult ecadd_execute(const uint8_t* input, size_t input_size, uint8_t* output,
[[maybe_unused]] size_t output_size) noexcept
{
assert(output_size >= 64);
uint8_t input_buffer[128]{};
if (input_size != 0)
std::memcpy(input_buffer, input, std::min(input_size, std::size(input_buffer)));
const evmmax::bn254::Point p = {intx::be::unsafe::load<intx::uint256>(input_buffer),
intx::be::unsafe::load<intx::uint256>(input_buffer + 32)};
const evmmax::bn254::Point q = {intx::be::unsafe::load<intx::uint256>(input_buffer + 64),
intx::be::unsafe::load<intx::uint256>(input_buffer + 96)};
if (evmmax::bn254::validate(p) && evmmax::bn254::validate(q))
{
const auto res = evmmax::bn254::add(p, q);
intx::be::unsafe::store(output, res.x);
intx::be::unsafe::store(output + 32, res.y);
return {EVMC_SUCCESS, 64};
}
else
return {EVMC_PRECOMPILE_FAILURE, 0};
}
ExecutionResult ecmul_execute(const uint8_t* input, size_t input_size, uint8_t* output,
[[maybe_unused]] size_t output_size) noexcept
{
assert(output_size >= 64);
uint8_t input_buffer[96]{};
if (input_size != 0)
std::memcpy(input_buffer, input, std::min(input_size, std::size(input_buffer)));
const evmmax::bn254::Point p = {intx::be::unsafe::load<intx::uint256>(input_buffer),
intx::be::unsafe::load<intx::uint256>(input_buffer + 32)};
const auto c = intx::be::unsafe::load<intx::uint256>(input_buffer + 64);
if (evmmax::bn254::validate(p))
{
const auto res = evmmax::bn254::mul(p, c);
intx::be::unsafe::store(output, res.x);
intx::be::unsafe::store(output + 32, res.y);
return {EVMC_SUCCESS, 64};
}
else
return {EVMC_PRECOMPILE_FAILURE, 0};
}
ExecutionResult identity_execute(const uint8_t* input, size_t input_size, uint8_t* output,
[[maybe_unused]] size_t output_size) noexcept
{
assert(output_size >= input_size);
std::copy_n(input, input_size, output);
return {EVMC_SUCCESS, input_size};
}
ExecutionResult blake2bf_execute(const uint8_t* input, [[maybe_unused]] size_t input_size,
uint8_t* output, [[maybe_unused]] size_t output_size) noexcept
{
static_assert(std::endian::native == std::endian::little,
"blake2bf only works correctly on little-endian architectures");
assert(input_size >= 213);
assert(output_size >= 64);
const auto rounds = intx::be::unsafe::load<uint32_t>(input);
input += sizeof(rounds);
uint64_t h[8];
std::memcpy(h, input, sizeof(h));
input += sizeof(h);
uint64_t m[16];
std::memcpy(m, input, sizeof(m));
input += sizeof(m);
uint64_t t[2];
std::memcpy(t, input, sizeof(t));
input += sizeof(t);
const auto f = *input;
if (f != 0 && f != 1) [[unlikely]]
return {EVMC_PRECOMPILE_FAILURE, 0};
crypto::blake2b_compress(rounds, h, m, t, f != 0);
std::memcpy(output, h, sizeof(h));
return {EVMC_SUCCESS, sizeof(h)};
}
namespace
{
struct PrecompileTraits
{
decltype(identity_analyze)* analyze = nullptr;
decltype(identity_execute)* execute = nullptr;
};
inline constexpr auto traits = []() noexcept {
std::array<PrecompileTraits, NumPrecompiles> tbl{{
{}, // undefined for 0
{ecrecover_analyze, ecrecover_execute},
{sha256_analyze, sha256_execute},
{ripemd160_analyze, ripemd160_execute},
{identity_analyze, identity_execute},
{expmod_analyze, expmod_stub},
{ecadd_analyze, ecadd_execute},
{ecmul_analyze, ecmul_execute},
{ecpairing_analyze, ecpairing_stub},
{blake2bf_analyze, blake2bf_execute},
{point_evaluation_analyze, point_evaluation_stub},
}};
#ifdef EVMONE_PRECOMPILES_SILKPRE
// tbl[static_cast<size_t>(PrecompileId::ecrecover)].execute = silkpre_ecrecover_execute;
// tbl[static_cast<size_t>(PrecompileId::sha256)].execute = silkpre_sha256_execute;
// tbl[static_cast<size_t>(PrecompileId::ripemd160)].execute = silkpre_ripemd160_execute;
tbl[static_cast<size_t>(PrecompileId::expmod)].execute = silkpre_expmod_execute;
// tbl[static_cast<size_t>(PrecompileId::ecadd)].execute = silkpre_ecadd_execute;
// tbl[static_cast<size_t>(PrecompileId::ecmul)].execute = silkpre_ecmul_execute;
tbl[static_cast<size_t>(PrecompileId::ecpairing)].execute = silkpre_ecpairing_execute;
// tbl[static_cast<size_t>(PrecompileId::blake2bf)].execute = silkpre_blake2bf_execute;
#endif
return tbl;
}();
} // namespace
bool is_precompile(evmc_revision rev, const evmc::address& addr) noexcept
{
// Define compile-time constant,
// TODO(clang18): workaround for Clang Analyzer bug, fixed in clang 18.
// https://github.com/llvm/llvm-project/issues/59493.
static constexpr evmc::address address_boundary{stdx::to_underlying(PrecompileId::latest)};
if (evmc::is_zero(addr) || addr > address_boundary)
return false;
const auto id = addr.bytes[19];
if (rev < EVMC_BYZANTIUM && id >= stdx::to_underlying(PrecompileId::since_byzantium))
return false;
if (rev < EVMC_ISTANBUL && id >= stdx::to_underlying(PrecompileId::since_istanbul))
return false;
if (rev < EVMC_CANCUN && id >= stdx::to_underlying(PrecompileId::since_cancun))
return false;
return true;
}
evmc::Result call_precompile(evmc_revision rev, const evmc_message& msg) noexcept
{
assert(msg.gas >= 0);
const auto id = msg.code_address.bytes[19];
const auto [analyze, execute] = traits[id];
const bytes_view input{msg.input_data, msg.input_size};
const auto [gas_cost, max_output_size] = analyze(input, rev);
const auto gas_left = msg.gas - gas_cost;
if (gas_left < 0)
return evmc::Result{EVMC_OUT_OF_GAS};
// Buffer for the precompile's output.
// Big enough to handle all "expmod" tests, but in case does not match the size requirement
// from analysis, the result will likely be incorrect.
// TODO: Replace with std::pmr::monotonic_buffer_resource?
uint8_t output_buf[4096];
assert(std::size(output_buf) >= max_output_size);
const auto [status_code, output_size] =
execute(msg.input_data, msg.input_size, output_buf, std::size(output_buf));
evmc::Result result{
status_code, status_code == EVMC_SUCCESS ? gas_left : 0, 0, output_buf, output_size};
return result;
}
} // namespace evmone::state