Skip to content

Commit 2395483

Browse files
committed
precompiles: Add bls precompiles IDs
1 parent 6974fd7 commit 2395483

File tree

4 files changed

+201
-19
lines changed

4 files changed

+201
-19
lines changed

test/state/precompiles.cpp

+145-13
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,24 @@ inline constexpr int64_t cost_per_input_word(size_t input_size) noexcept
4040
{
4141
return BaseCost + WordCost * num_words(input_size);
4242
}
43+
44+
int64_t bls_msm_cost(size_t k, int64_t multiplication_cost) noexcept
45+
{
46+
assert(k > 0 && k < 128);
47+
48+
constexpr int64_t MULTIPLIER = 1000;
49+
constexpr int16_t discount[128] = {1200, 888, 764, 641, 594, 547, 500, 453, 438, 423, 408, 394,
50+
379, 364, 349, 334, 330, 326, 322, 318, 314, 310, 306, 302, 298, 294, 289, 285, 281, 277,
51+
273, 269, 268, 266, 265, 263, 262, 260, 259, 257, 256, 254, 253, 251, 250, 248, 247, 245,
52+
244, 242, 241, 239, 238, 236, 235, 233, 232, 231, 229, 228, 226, 225, 223, 222, 221, 220,
53+
219, 219, 218, 217, 216, 216, 215, 214, 213, 213, 212, 211, 211, 210, 209, 208, 208, 207,
54+
206, 205, 205, 204, 203, 202, 202, 201, 200, 199, 199, 198, 197, 196, 196, 195, 194, 193,
55+
193, 192, 191, 191, 190, 189, 188, 188, 187, 186, 185, 185, 184, 183, 182, 182, 181, 180,
56+
179, 179, 178, 177, 176, 176, 175, 174};
57+
58+
return (static_cast<int64_t>(k) * multiplication_cost * discount[k - 1]) / MULTIPLIER;
59+
}
60+
4361
} // namespace
4462

4563
PrecompileAnalysis ecrecover_analyze(bytes_view /*input*/, evmc_revision /*rev*/) noexcept
@@ -153,6 +171,73 @@ PrecompileAnalysis point_evaluation_analyze(bytes_view, evmc_revision) noexcept
153171
return {POINT_EVALUATION_PRECOMPILE_GAS, 64};
154172
}
155173

174+
PrecompileAnalysis bls12_g1add_analyze(bytes_view, evmc_revision) noexcept
175+
{
176+
static constexpr auto BLS12_G1ADD_PRECOMPILE_GAS = 500;
177+
return {BLS12_G1ADD_PRECOMPILE_GAS, 128};
178+
}
179+
180+
PrecompileAnalysis bls12_g1mul_analyze(bytes_view, evmc_revision) noexcept
181+
{
182+
static constexpr auto BLS12_G1MUL_PRECOMPILE_GAS = 12000;
183+
return {BLS12_G1MUL_PRECOMPILE_GAS, 128};
184+
}
185+
186+
PrecompileAnalysis bls12_g1msm_analyze(bytes_view input, evmc_revision) noexcept
187+
{
188+
if (input.size() % 160 != 0)
189+
return {GasCostMax, 0};
190+
191+
static constexpr auto BLS12_G1MUL_PRECOMPILE_GAS = 12000;
192+
return {bls_msm_cost(input.size() / 160, BLS12_G1MUL_PRECOMPILE_GAS), 128};
193+
}
194+
195+
PrecompileAnalysis bls12_g2add_analyze(bytes_view, evmc_revision) noexcept
196+
{
197+
static constexpr auto BLS12_G2ADD_PRECOMPILE_GAS = 800;
198+
return {BLS12_G2ADD_PRECOMPILE_GAS, 256};
199+
}
200+
201+
PrecompileAnalysis bls12_g2mul_analyze(bytes_view, evmc_revision) noexcept
202+
{
203+
static constexpr auto BLS12_G2MUL_PRECOMPILE_GAS = 45000;
204+
return {BLS12_G2MUL_PRECOMPILE_GAS, 256};
205+
}
206+
207+
PrecompileAnalysis bls12_g2msm_analyze(bytes_view input, evmc_revision) noexcept
208+
{
209+
if (input.size() % 228 != 0)
210+
return {GasCostMax, 0};
211+
212+
static constexpr auto BLS12_G2MUL_PRECOMPILE_GAS = 45000;
213+
return {bls_msm_cost(input.size() / 228, BLS12_G2MUL_PRECOMPILE_GAS), 256};
214+
}
215+
216+
PrecompileAnalysis bls12_pairing_check_analyze(bytes_view input, evmc_revision) noexcept
217+
{
218+
if (input.size() % 384 != 0)
219+
return {GasCostMax, 0};
220+
221+
static constexpr auto BLS12_PAIRING_CHECK_START_PRECOMPILE_GAS = 65000;
222+
static constexpr auto BLS12_PAIRING_CHECK_PER_PAIR_PRECOMPILE_GAS = 43000;
223+
return {
224+
static_cast<int64_t>((input.size() / 384) * BLS12_PAIRING_CHECK_PER_PAIR_PRECOMPILE_GAS +
225+
BLS12_PAIRING_CHECK_START_PRECOMPILE_GAS),
226+
32};
227+
}
228+
229+
PrecompileAnalysis bls12_map_fp_to_g1_analyze(bytes_view, evmc_revision) noexcept
230+
{
231+
static constexpr auto BLS12_FP_TO_G1_PRECOMPILE_GAS = 5500;
232+
return {BLS12_FP_TO_G1_PRECOMPILE_GAS, 128};
233+
}
234+
235+
PrecompileAnalysis bls12_map_fp2_to_g2_analyze(bytes_view, evmc_revision) noexcept
236+
{
237+
static constexpr auto BLS12_FP2_TO_G2_PRECOMPILE_GAS = 75000;
238+
return {BLS12_FP2_TO_G2_PRECOMPILE_GAS, 256};
239+
}
240+
156241
ExecutionResult ecrecover_execute(const uint8_t* input, size_t input_size, uint8_t* output,
157242
[[maybe_unused]] size_t output_size) noexcept
158243
{
@@ -292,6 +377,51 @@ ExecutionResult blake2bf_execute(const uint8_t* input, [[maybe_unused]] size_t i
292377
return {EVMC_SUCCESS, sizeof(h)};
293378
}
294379

380+
ExecutionResult bls12_g1add_execute(const uint8_t*, size_t, uint8_t*, size_t) noexcept
381+
{
382+
return {EVMC_PRECOMPILE_FAILURE, 0};
383+
}
384+
385+
ExecutionResult bls12_g1mul_execute(const uint8_t*, size_t, uint8_t*, size_t) noexcept
386+
{
387+
return {EVMC_PRECOMPILE_FAILURE, 0};
388+
}
389+
390+
ExecutionResult bls12_g1msm_execute(const uint8_t*, size_t, uint8_t*, size_t) noexcept
391+
{
392+
return {EVMC_PRECOMPILE_FAILURE, 0};
393+
}
394+
395+
ExecutionResult bls12_g2add_execute(const uint8_t*, size_t, uint8_t*, size_t) noexcept
396+
{
397+
return {EVMC_PRECOMPILE_FAILURE, 0};
398+
}
399+
400+
ExecutionResult bls12_g2mul_execute(const uint8_t*, size_t, uint8_t*, size_t) noexcept
401+
{
402+
return {EVMC_PRECOMPILE_FAILURE, 0};
403+
}
404+
405+
ExecutionResult bls12_g2msm_execute(const uint8_t*, size_t, uint8_t*, size_t) noexcept
406+
{
407+
return {EVMC_PRECOMPILE_FAILURE, 0};
408+
}
409+
410+
ExecutionResult bls12_pairing_check_execute(const uint8_t*, size_t, uint8_t*, size_t) noexcept
411+
{
412+
return {EVMC_PRECOMPILE_FAILURE, 0};
413+
}
414+
415+
ExecutionResult bls12_map_fp_to_g1_execute(const uint8_t*, size_t, uint8_t*, size_t) noexcept
416+
{
417+
return {EVMC_PRECOMPILE_FAILURE, 0};
418+
}
419+
420+
ExecutionResult bls12_map_fp2_to_g2_execute(const uint8_t*, size_t, uint8_t*, size_t) noexcept
421+
{
422+
return {EVMC_PRECOMPILE_FAILURE, 0};
423+
}
424+
295425
namespace
296426
{
297427
struct PrecompileTraits
@@ -301,19 +431,18 @@ struct PrecompileTraits
301431
};
302432

303433
inline constexpr auto traits = []() noexcept {
304-
std::array<PrecompileTraits, NumPrecompiles> tbl{{
305-
{}, // undefined for 0
306-
{ecrecover_analyze, ecrecover_execute},
307-
{sha256_analyze, sha256_execute},
308-
{ripemd160_analyze, ripemd160_execute},
309-
{identity_analyze, identity_execute},
310-
{expmod_analyze, expmod_stub},
311-
{ecadd_analyze, ecadd_execute},
312-
{ecmul_analyze, ecmul_execute},
313-
{ecpairing_analyze, ecpairing_stub},
314-
{blake2bf_analyze, blake2bf_execute},
315-
{point_evaluation_analyze, point_evaluation_stub},
316-
}};
434+
std::array<PrecompileTraits, NumPrecompiles> tbl{{{}, // undefined for 0
435+
{ecrecover_analyze, ecrecover_execute}, {sha256_analyze, sha256_execute},
436+
{ripemd160_analyze, ripemd160_execute}, {identity_analyze, identity_execute},
437+
{expmod_analyze, expmod_stub}, {ecadd_analyze, ecadd_execute},
438+
{ecmul_analyze, ecmul_execute}, {ecpairing_analyze, ecpairing_stub},
439+
{blake2bf_analyze, blake2bf_execute}, {point_evaluation_analyze, point_evaluation_stub},
440+
{bls12_g1add_analyze, bls12_g1add_execute}, {bls12_g1mul_analyze, bls12_g1mul_execute},
441+
{bls12_g1msm_analyze, bls12_g1msm_execute}, {bls12_g2add_analyze, bls12_g2add_execute},
442+
{bls12_g2mul_analyze, bls12_g2mul_execute}, {bls12_g2msm_analyze, bls12_g2msm_execute},
443+
{bls12_pairing_check_analyze, bls12_pairing_check_execute},
444+
{bls12_map_fp_to_g1_analyze, bls12_map_fp_to_g1_execute},
445+
{bls12_map_fp2_to_g2_analyze, bls12_map_fp2_to_g2_execute}}};
317446
#ifdef EVMONE_PRECOMPILES_SILKPRE
318447
// tbl[static_cast<size_t>(PrecompileId::ecrecover)].execute = silkpre_ecrecover_execute;
319448
// tbl[static_cast<size_t>(PrecompileId::sha256)].execute = silkpre_sha256_execute;
@@ -348,6 +477,9 @@ bool is_precompile(evmc_revision rev, const evmc::address& addr) noexcept
348477
if (rev < EVMC_CANCUN && id >= stdx::to_underlying(PrecompileId::since_cancun))
349478
return false;
350479

480+
if (rev < EVMC_PRAGUE && id >= stdx::to_underlying(PrecompileId::since_prague))
481+
return false;
482+
351483
return true;
352484
}
353485

test/state/precompiles.hpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,21 @@ enum class PrecompileId : uint8_t
2222
ecpairing = 0x08,
2323
blake2bf = 0x09,
2424
point_evaluation = 0x0a,
25+
bls12_g1add = 0x0b,
26+
bls12_g1mul = 0x0c,
27+
bls12_g1msm = 0x0d,
28+
bls12_g2add = 0x0e,
29+
bls12_g2mul = 0x0f,
30+
bls12_g2msm = 0x10,
31+
bls12_pairing_check = 0x11,
32+
bls12_map_fp_to_g1 = 0x12,
33+
bls12_map_fp2_to_g2 = 0x13,
2534

2635
since_byzantium = expmod, ///< The first precompile introduced in Byzantium.
2736
since_istanbul = blake2bf, ///< The first precompile introduced in Istanbul.
2837
since_cancun = point_evaluation, ///< The first precompile introduced in Cancun.
29-
latest = point_evaluation ///< The latest introduced precompile (highest address).
38+
since_prague = bls12_g1add, ///< The first precompile introduced in Prague.
39+
latest = bls12_map_fp2_to_g2 ///< The latest introduced precompile (highest address).
3040
};
3141

3242
/// The total number of known precompiles ids, including 0.

test/state/precompiles_internal.hpp

+30
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ PrecompileAnalysis ecmul_analyze(evmc::bytes_view input, evmc_revision rev) noex
2929
PrecompileAnalysis ecpairing_analyze(evmc::bytes_view input, evmc_revision rev) noexcept;
3030
PrecompileAnalysis blake2bf_analyze(evmc::bytes_view input, evmc_revision rev) noexcept;
3131
PrecompileAnalysis point_evaluation_analyze(evmc::bytes_view input, evmc_revision rev) noexcept;
32+
PrecompileAnalysis bls12_g1add_analyze(evmc::bytes_view input, evmc_revision rev) noexcept;
33+
PrecompileAnalysis bls12_g1add_analyze(evmc::bytes_view input, evmc_revision) noexcept;
34+
PrecompileAnalysis bls12_g1mul_analyze(evmc::bytes_view input, evmc_revision) noexcept;
35+
PrecompileAnalysis bls12_g1msm_analyze(evmc::bytes_view input, evmc_revision) noexcept;
36+
PrecompileAnalysis bls12_g2add_analyze(evmc::bytes_view input, evmc_revision) noexcept;
37+
PrecompileAnalysis bls12_g2mul_analyze(evmc::bytes_view input, evmc_revision) noexcept;
38+
PrecompileAnalysis bls12_g2msm_analyze(evmc::bytes_view input, evmc_revision) noexcept;
39+
PrecompileAnalysis bls12_pairing_check_analyze(evmc::bytes_view input, evmc_revision) noexcept;
40+
PrecompileAnalysis bls12_map_fp_to_g1_analyze(evmc::bytes_view input, evmc_revision) noexcept;
41+
PrecompileAnalysis bls12_map_fp2_to_g2_analyze(evmc::bytes_view input, evmc_revision) noexcept;
3242

3343
ExecutionResult ecrecover_execute(
3444
const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size) noexcept;
@@ -44,4 +54,24 @@ ExecutionResult ecmul_execute(
4454
const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size) noexcept;
4555
ExecutionResult blake2bf_execute(
4656
const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size) noexcept;
57+
ExecutionResult bls12_g1add_execute(
58+
const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size) noexcept;
59+
ExecutionResult bls12_g1add_execute(
60+
const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size) noexcept;
61+
ExecutionResult bls12_g1mul_execute(
62+
const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size) noexcept;
63+
ExecutionResult bls12_g1msm_execute(
64+
const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size) noexcept;
65+
ExecutionResult bls12_g2add_execute(
66+
const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size) noexcept;
67+
ExecutionResult bls12_g2mul_execute(
68+
const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size) noexcept;
69+
ExecutionResult bls12_g2msm_execute(
70+
const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size) noexcept;
71+
ExecutionResult bls12_pairing_check_execute(
72+
const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size) noexcept;
73+
ExecutionResult bls12_map_fp_to_g1_execute(
74+
const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size) noexcept;
75+
ExecutionResult bls12_map_fp2_to_g2_execute(
76+
const uint8_t* input, size_t input_size, uint8_t* output, size_t output_size) noexcept;
4777
} // namespace evmone::state

test/unittests/state_precompiles_test.cpp

+15-5
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,21 @@ TEST(state_precompiles, is_precompile)
3434
// Cancun:
3535
EXPECT_EQ(is_precompile(rev, 0x0a_address), rev >= EVMC_CANCUN);
3636

37+
// Prague:
38+
EXPECT_EQ(is_precompile(rev, 0x0b_address), rev >= EVMC_PRAGUE);
39+
EXPECT_EQ(is_precompile(rev, 0x0c_address), rev >= EVMC_PRAGUE);
40+
EXPECT_EQ(is_precompile(rev, 0x0d_address), rev >= EVMC_PRAGUE);
41+
EXPECT_EQ(is_precompile(rev, 0x0e_address), rev >= EVMC_PRAGUE);
42+
EXPECT_EQ(is_precompile(rev, 0x0f_address), rev >= EVMC_PRAGUE);
43+
EXPECT_EQ(is_precompile(rev, 0x10_address), rev >= EVMC_PRAGUE);
44+
EXPECT_EQ(is_precompile(rev, 0x11_address), rev >= EVMC_PRAGUE);
45+
EXPECT_EQ(is_precompile(rev, 0x12_address), rev >= EVMC_PRAGUE);
46+
EXPECT_EQ(is_precompile(rev, 0x13_address), rev >= EVMC_PRAGUE);
47+
3748
// Future?
38-
EXPECT_FALSE(is_precompile(rev, 0x0b_address));
39-
EXPECT_FALSE(is_precompile(rev, 0x0c_address));
40-
EXPECT_FALSE(is_precompile(rev, 0x0d_address));
41-
EXPECT_FALSE(is_precompile(rev, 0x0e_address));
42-
EXPECT_FALSE(is_precompile(rev, 0x0f_address));
49+
EXPECT_FALSE(is_precompile(rev, 0x14_address));
50+
EXPECT_FALSE(is_precompile(rev, 0x15_address));
51+
EXPECT_FALSE(is_precompile(rev, 0x16_address));
52+
EXPECT_FALSE(is_precompile(rev, 0x17_address));
4353
}
4454
}

0 commit comments

Comments
 (0)