Skip to content

Commit ec75566

Browse files
committed
precompiles: Update gas costs for BLS precompiles
This updates the gas costs of BLS precompiles. Discounts tables for MSMs are now split between G1 and G2 variants.
1 parent 9899329 commit ec75566

File tree

1 file changed

+37
-29
lines changed

1 file changed

+37
-29
lines changed

test/state/precompiles.cpp

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <bit>
1818
#include <cassert>
1919
#include <limits>
20+
#include <span>
2021

2122
#ifdef EVMONE_PRECOMPILES_SILKPRE
2223
#include "precompiles_silkpre.hpp"
@@ -42,25 +43,6 @@ constexpr int64_t cost_per_input_word(size_t input_size) noexcept
4243
{
4344
return BaseCost + WordCost * num_words(input_size);
4445
}
45-
46-
int64_t bls_msm_cost(size_t k, int64_t multiplication_cost) noexcept
47-
{
48-
assert(k > 0);
49-
50-
static constexpr int64_t MULTIPLIER = 1000;
51-
static constexpr int16_t DISCOUNT[128] = {1200, 888, 764, 641, 594, 547, 500, 453, 438, 423,
52-
408, 394, 379, 364, 349, 334, 330, 326, 322, 318, 314, 310, 306, 302, 298, 294, 289, 285,
53-
281, 277, 273, 269, 268, 266, 265, 263, 262, 260, 259, 257, 256, 254, 253, 251, 250, 248,
54-
247, 245, 244, 242, 241, 239, 238, 236, 235, 233, 232, 231, 229, 228, 226, 225, 223, 222,
55-
221, 220, 219, 219, 218, 217, 216, 216, 215, 214, 213, 213, 212, 211, 211, 210, 209, 208,
56-
208, 207, 206, 205, 205, 204, 203, 202, 202, 201, 200, 199, 199, 198, 197, 196, 196, 195,
57-
194, 193, 193, 192, 191, 191, 190, 189, 188, 188, 187, 186, 185, 185, 184, 183, 182, 182,
58-
181, 180, 179, 179, 178, 177, 176, 176, 175, 174};
59-
60-
const auto d = DISCOUNT[std::min(k, std::size(DISCOUNT)) - 1];
61-
return (static_cast<int64_t>(k) * multiplication_cost * d) / MULTIPLIER;
62-
}
63-
6446
} // namespace
6547

6648
PrecompileAnalysis ecrecover_analyze(bytes_view /*input*/, evmc_revision /*rev*/) noexcept
@@ -176,7 +158,7 @@ PrecompileAnalysis point_evaluation_analyze(bytes_view, evmc_revision) noexcept
176158

177159
PrecompileAnalysis bls12_g1add_analyze(bytes_view, evmc_revision) noexcept
178160
{
179-
static constexpr auto BLS12_G1ADD_PRECOMPILE_GAS = 500;
161+
static constexpr auto BLS12_G1ADD_PRECOMPILE_GAS = 375;
180162
return {BLS12_G1ADD_PRECOMPILE_GAS, 128};
181163
}
182164

@@ -188,32 +170,58 @@ PrecompileAnalysis bls12_g1mul_analyze(bytes_view, evmc_revision) noexcept
188170

189171
PrecompileAnalysis bls12_g1msm_analyze(bytes_view input, evmc_revision) noexcept
190172
{
173+
static constexpr auto G1MUL_GAS_COST = 12000;
174+
static constexpr uint16_t DISCOUNTS[] = {1000, 949, 848, 797, 764, 750, 738, 728, 719, 712, 705,
175+
698, 692, 687, 682, 677, 673, 669, 665, 661, 658, 654, 651, 648, 645, 642, 640, 637, 635,
176+
632, 630, 627, 625, 623, 621, 619, 617, 615, 613, 611, 609, 608, 606, 604, 603, 601, 599,
177+
598, 596, 595, 593, 592, 591, 589, 588, 586, 585, 584, 582, 581, 580, 579, 577, 576, 575,
178+
574, 573, 572, 570, 569, 568, 567, 566, 565, 564, 563, 562, 561, 560, 559, 558, 557, 556,
179+
555, 554, 553, 552, 551, 550, 549, 548, 547, 547, 546, 545, 544, 543, 542, 541, 540, 540,
180+
539, 538, 537, 536, 536, 535, 534, 533, 532, 532, 531, 530, 529, 528, 528, 527, 526, 525,
181+
525, 524, 523, 522, 522, 521, 520, 520, 519};
182+
191183
if (input.empty() || input.size() % 160 != 0)
192184
return {GasCostMax, 0};
193185

194-
static constexpr auto BLS12_G1MUL_PRECOMPILE_GAS = 12000;
195-
return {bls_msm_cost(input.size() / 160, BLS12_G1MUL_PRECOMPILE_GAS), 128};
186+
const auto k = input.size() / 160;
187+
assert(k > 0);
188+
const auto discount = DISCOUNTS[std::min(k, std::size(DISCOUNTS)) - 1];
189+
const auto cost = (G1MUL_GAS_COST * discount * static_cast<int64_t>(k)) / 1000;
190+
return {cost, 128};
196191
}
197192

198193
PrecompileAnalysis bls12_g2add_analyze(bytes_view, evmc_revision) noexcept
199194
{
200-
static constexpr auto BLS12_G2ADD_PRECOMPILE_GAS = 800;
195+
static constexpr auto BLS12_G2ADD_PRECOMPILE_GAS = 600;
201196
return {BLS12_G2ADD_PRECOMPILE_GAS, 256};
202197
}
203198

204199
PrecompileAnalysis bls12_g2mul_analyze(bytes_view, evmc_revision) noexcept
205200
{
206-
static constexpr auto BLS12_G2MUL_PRECOMPILE_GAS = 45000;
201+
static constexpr auto BLS12_G2MUL_PRECOMPILE_GAS = 22500;
207202
return {BLS12_G2MUL_PRECOMPILE_GAS, 256};
208203
}
209204

210205
PrecompileAnalysis bls12_g2msm_analyze(bytes_view input, evmc_revision) noexcept
211206
{
207+
static constexpr auto G2MUL_GAS_COST = 22500;
208+
static constexpr uint16_t DISCOUNTS[] = {1000, 1000, 923, 884, 855, 832, 812, 796, 782, 770,
209+
759, 749, 740, 732, 724, 717, 711, 704, 699, 693, 688, 683, 679, 674, 670, 666, 663, 659,
210+
655, 652, 649, 646, 643, 640, 637, 634, 632, 629, 627, 624, 622, 620, 618, 615, 613, 611,
211+
609, 607, 606, 604, 602, 600, 598, 597, 595, 593, 592, 590, 589, 587, 586, 584, 583, 582,
212+
580, 579, 578, 576, 575, 574, 573, 571, 570, 569, 568, 567, 566, 565, 563, 562, 561, 560,
213+
559, 558, 557, 556, 555, 554, 553, 552, 552, 551, 550, 549, 548, 547, 546, 545, 545, 544,
214+
543, 542, 541, 541, 540, 539, 538, 537, 537, 536, 535, 535, 534, 533, 532, 532, 531, 530,
215+
530, 529, 528, 528, 527, 526, 526, 525, 524, 524};
216+
212217
if (input.empty() || input.size() % 288 != 0)
213218
return {GasCostMax, 0};
214219

215-
static constexpr auto BLS12_G2MUL_PRECOMPILE_GAS = 45000;
216-
return {bls_msm_cost(input.size() / 288, BLS12_G2MUL_PRECOMPILE_GAS), 256};
220+
const auto k = input.size() / 288;
221+
assert(k > 0);
222+
const auto discount = DISCOUNTS[std::min(k, std::size(DISCOUNTS)) - 1];
223+
const auto cost = (G2MUL_GAS_COST * discount * static_cast<int64_t>(k)) / 1000;
224+
return {cost, 256};
217225
}
218226

219227
PrecompileAnalysis bls12_pairing_check_analyze(bytes_view input, evmc_revision) noexcept
@@ -225,8 +233,8 @@ PrecompileAnalysis bls12_pairing_check_analyze(bytes_view input, evmc_revision)
225233

226234
const auto npairs = static_cast<int64_t>(input.size()) / PAIR_SIZE;
227235

228-
static constexpr auto BLS12_PAIRING_CHECK_BASE_FEE_PRECOMPILE_GAS = 65000;
229-
static constexpr auto BLS12_PAIRING_CHECK_FEE_PRECOMPILE_GAS = 43000;
236+
static constexpr auto BLS12_PAIRING_CHECK_BASE_FEE_PRECOMPILE_GAS = 37700;
237+
static constexpr auto BLS12_PAIRING_CHECK_FEE_PRECOMPILE_GAS = 32600;
230238
return {BLS12_PAIRING_CHECK_BASE_FEE_PRECOMPILE_GAS +
231239
BLS12_PAIRING_CHECK_FEE_PRECOMPILE_GAS * npairs,
232240
32};
@@ -240,7 +248,7 @@ PrecompileAnalysis bls12_map_fp_to_g1_analyze(bytes_view, evmc_revision) noexcep
240248

241249
PrecompileAnalysis bls12_map_fp2_to_g2_analyze(bytes_view, evmc_revision) noexcept
242250
{
243-
static constexpr auto BLS12_MAP_FP2_TO_G2_PRECOMPILE_GAS = 75000;
251+
static constexpr auto BLS12_MAP_FP2_TO_G2_PRECOMPILE_GAS = 23800;
244252
return {BLS12_MAP_FP2_TO_G2_PRECOMPILE_GAS, 256};
245253
}
246254

0 commit comments

Comments
 (0)