@@ -40,6 +40,24 @@ inline constexpr int64_t cost_per_input_word(size_t input_size) noexcept
40
40
{
41
41
return BaseCost + WordCost * num_words (input_size);
42
42
}
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
+
43
61
} // namespace
44
62
45
63
PrecompileAnalysis ecrecover_analyze (bytes_view /* input*/ , evmc_revision /* rev*/ ) noexcept
@@ -153,6 +171,73 @@ PrecompileAnalysis point_evaluation_analyze(bytes_view, evmc_revision) noexcept
153
171
return {POINT_EVALUATION_PRECOMPILE_GAS, 64 };
154
172
}
155
173
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
+
156
241
ExecutionResult ecrecover_execute (const uint8_t * input, size_t input_size, uint8_t * output,
157
242
[[maybe_unused]] size_t output_size) noexcept
158
243
{
@@ -292,6 +377,51 @@ ExecutionResult blake2bf_execute(const uint8_t* input, [[maybe_unused]] size_t i
292
377
return {EVMC_SUCCESS, sizeof (h)};
293
378
}
294
379
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
+
295
425
namespace
296
426
{
297
427
struct PrecompileTraits
@@ -301,19 +431,18 @@ struct PrecompileTraits
301
431
};
302
432
303
433
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}}};
317
446
#ifdef EVMONE_PRECOMPILES_SILKPRE
318
447
// tbl[static_cast<size_t>(PrecompileId::ecrecover)].execute = silkpre_ecrecover_execute;
319
448
// 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
348
477
if (rev < EVMC_CANCUN && id >= stdx::to_underlying (PrecompileId::since_cancun))
349
478
return false ;
350
479
480
+ if (rev < EVMC_PRAGUE && id >= stdx::to_underlying (PrecompileId::since_prague))
481
+ return false ;
482
+
351
483
return true ;
352
484
}
353
485
0 commit comments