Skip to content

Commit 89d153d

Browse files
refactor: update target opcode count display name (#2591)
1 parent 9167dd9 commit 89d153d

File tree

14 files changed

+144
-40
lines changed

14 files changed

+144
-40
lines changed

packages/testing/src/execution_testing/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
BlockchainTest,
4343
BlockchainTestFiller,
4444
Header,
45+
OpcodeTarget,
4546
StateTest,
4647
StateTestFiller,
4748
TransactionTest,
@@ -174,6 +175,7 @@
174175
"Macros",
175176
"MemoryVariable",
176177
"NetworkWrappedTransaction",
178+
"OpcodeTarget",
177179
"Op",
178180
"Opcode",
179181
"OpcodeCallArg",

packages/testing/src/execution_testing/specs/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
from .base import BaseTest, TestSpec
44
from .base_static import BaseStaticTest
5-
from .benchmark import BenchmarkTest, BenchmarkTestFiller, BenchmarkTestSpec
5+
from .benchmark import (
6+
BenchmarkTest,
7+
BenchmarkTestFiller,
8+
BenchmarkTestSpec,
9+
OpcodeTarget,
10+
)
611
from .blobs import BlobsTest, BlobsTestFiller, BlobsTestSpec
712
from .blockchain import (
813
Block,
@@ -35,6 +40,7 @@
3540
"BlockchainTestSpec",
3641
"Block",
3742
"Header",
43+
"OpcodeTarget",
3844
"StateStaticTest",
3945
"StateTest",
4046
"StateTestFiller",

packages/testing/src/execution_testing/specs/benchmark.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@
4545
from .blockchain import Block, BlockchainTest
4646

4747

48+
@dataclass(frozen=True)
49+
class OpcodeTarget:
50+
"""
51+
Map a display name to an underlying opcode for count validation.
52+
53+
Use when the fixture metadata should show a descriptive label (e.g. a
54+
precompile name) while opcode-count validation targets the real EVM
55+
opcode that gets executed (e.g. STATICCALL).
56+
"""
57+
58+
name: str
59+
opcode: Op
60+
61+
def __str__(self) -> str:
62+
"""Return the display name."""
63+
return self.name
64+
65+
4866
@dataclass(kw_only=True)
4967
class BenchmarkCodeGenerator(ABC):
5068
"""Abstract base class for generating benchmark bytecode."""
@@ -287,7 +305,7 @@ class BenchmarkTest(BaseTest):
287305
default_factory=lambda: int(Environment().gas_limit)
288306
)
289307
fixed_opcode_count: float | None = None
290-
target_opcode: Op | None = None
308+
target_opcode: Op | OpcodeTarget | None = None
291309
code_generator: BenchmarkCodeGenerator | None = None
292310
# By default, benchmark tests require neither of these
293311
include_full_post_state_in_output: bool = False
@@ -523,7 +541,12 @@ def _verify_target_opcode_count(
523541
# fixed_opcode_count is in thousands units
524542
expected = self.fixed_opcode_count * 1000
525543

526-
actual = opcode_count.root.get(self.target_opcode, 0)
544+
count_opcode = (
545+
self.target_opcode.opcode
546+
if isinstance(self.target_opcode, OpcodeTarget)
547+
else self.target_opcode
548+
)
549+
actual = opcode_count.root.get(count_opcode, 0)
527550
tolerance = expected * 0.05 # 5% tolerance
528551

529552
if abs(actual - expected) > tolerance:

tests/benchmark/compute/helpers.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
Initcode,
1717
IteratingBytecode,
1818
Op,
19+
OpcodeTarget,
1920
TransactionWithCost,
2021
While,
2122
compute_create2_address,
@@ -27,6 +28,30 @@
2728
FieldElement,
2829
)
2930

31+
32+
class Precompile:
33+
"""Target opcode labels for precompile benchmarks."""
34+
35+
ECRECOVER = OpcodeTarget("ECRECOVER", Op.STATICCALL)
36+
SHA256 = OpcodeTarget("SHA2-256", Op.STATICCALL)
37+
RIPEMD160 = OpcodeTarget("RIPEMD-160", Op.STATICCALL)
38+
IDENTITY = OpcodeTarget("IDENTITY", Op.STATICCALL)
39+
MODEXP = OpcodeTarget("MODEXP", Op.STATICCALL)
40+
BN128_ADD = OpcodeTarget("BN128_ADD", Op.STATICCALL)
41+
BN128_MUL = OpcodeTarget("BN128_MUL", Op.STATICCALL)
42+
BN128_PAIRING = OpcodeTarget("BN128_PAIRING", Op.STATICCALL)
43+
BLAKE2F = OpcodeTarget("BLAKE2F", Op.STATICCALL)
44+
POINT_EVALUATION = OpcodeTarget("POINT_EVALUATION", Op.STATICCALL)
45+
P256VERIFY = OpcodeTarget("P256VERIFY", Op.STATICCALL)
46+
BLS12_G1ADD = OpcodeTarget("BLS12_G1ADD", Op.STATICCALL)
47+
BLS12_G1MSM = OpcodeTarget("BLS12_G1MSM", Op.STATICCALL)
48+
BLS12_G2ADD = OpcodeTarget("BLS12_G2ADD", Op.STATICCALL)
49+
BLS12_G2MSM = OpcodeTarget("BLS12_G2MSM", Op.STATICCALL)
50+
BLS12_PAIRING = OpcodeTarget("BLS12_PAIRING", Op.STATICCALL)
51+
BLS12_MAP_FP_TO_G1 = OpcodeTarget("BLS12_MAP_FP_TO_G1", Op.STATICCALL)
52+
BLS12_MAP_FP2_TO_G2 = OpcodeTarget("BLS12_MAP_FP2_TO_G2", Op.STATICCALL)
53+
54+
3055
DEFAULT_BINOP_ARGS = (
3156
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F,
3257
0x73EDA753299D7D483339D80809A1D80553BDA402FFFE5BFEFFFFFFFF00000001,

tests/benchmark/compute/precompile/test_alt_bn128.py

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@
1313
Fork,
1414
JumpLoopGenerator,
1515
Op,
16+
OpcodeTarget,
1617
Transaction,
1718
While,
1819
)
1920
from py_ecc.bn128 import G1, G2, multiply
2021

21-
from ..helpers import concatenate_parameters
22+
from ..helpers import Precompile, concatenate_parameters
2223

2324

2425
@pytest.mark.parametrize(
25-
"precompile_address,calldata",
26+
"precompile_address,calldata,target",
2627
[
2728
pytest.param(
2829
0x06,
@@ -34,6 +35,7 @@
3435
"06614E20C147E940F2D70DA3F74C9A17DF361706A4485C742BD6788478FA17D7",
3536
]
3637
),
38+
Precompile.BN128_ADD,
3739
id="bn128_add",
3840
marks=pytest.mark.repricing,
3941
),
@@ -49,6 +51,7 @@
4951
"0000000000000000000000000000000000000000000000000000000000000000",
5052
]
5153
),
54+
Precompile.BN128_ADD,
5255
id="bn128_add_infinities",
5356
marks=pytest.mark.repricing,
5457
),
@@ -64,6 +67,7 @@
6467
"0000000000000000000000000000000000000000000000000000000000000002",
6568
]
6669
),
70+
Precompile.BN128_ADD,
6771
id="bn128_add_1_2",
6872
),
6973
pytest.param(
@@ -75,6 +79,7 @@
7579
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
7680
]
7781
),
82+
Precompile.BN128_MUL,
7883
id="bn128_mul",
7984
),
8085
# Ported from
@@ -88,6 +93,7 @@
8893
"0000000000000000000000000000000000000000000000000000000000000002",
8994
]
9095
),
96+
Precompile.BN128_MUL,
9197
id="bn128_mul_infinities_2_scalar",
9298
),
9399
# Ported from
@@ -101,6 +107,7 @@
101107
"25f8c89ea3437f44f8fc8b6bfbb6312074dc6f983809a5e809ff4e1d076dd585",
102108
]
103109
),
110+
Precompile.BN128_MUL,
104111
id="bn128_mul_infinities_32_byte_scalar",
105112
marks=pytest.mark.repricing,
106113
),
@@ -115,6 +122,7 @@
115122
"0000000000000000000000000000000000000000000000000000000000000002",
116123
]
117124
),
125+
Precompile.BN128_MUL,
118126
id="bn128_mul_1_2_2_scalar",
119127
),
120128
# Ported from
@@ -128,6 +136,7 @@
128136
"25f8c89ea3437f44f8fc8b6bfbb6312074dc6f983809a5e809ff4e1d076dd585",
129137
]
130138
),
139+
Precompile.BN128_MUL,
131140
id="bn128_mul_1_2_32_byte_scalar",
132141
),
133142
# Ported from
@@ -141,6 +150,7 @@
141150
"0000000000000000000000000000000000000000000000000000000000000002",
142151
]
143152
),
153+
Precompile.BN128_MUL,
144154
id="bn128_mul_32_byte_coord_and_2_scalar",
145155
marks=pytest.mark.repricing,
146156
),
@@ -155,6 +165,7 @@
155165
"25f8c89ea3437f44f8fc8b6bfbb6312074dc6f983809a5e809ff4e1d076dd585",
156166
]
157167
),
168+
Precompile.BN128_MUL,
158169
id="bn128_mul_32_byte_coord_and_scalar",
159170
marks=pytest.mark.repricing,
160171
),
@@ -178,6 +189,7 @@
178189
"12C85EA5DB8C6DEB4AAB71808DCB408FE3D1E7690C43D37B4CE6CC0166FA7DAA",
179190
]
180191
),
192+
Precompile.BN128_PAIRING,
181193
id="bn128_two_pairings",
182194
),
183195
pytest.param(
@@ -193,11 +205,17 @@
193205
"120A2A4CF30C1BF9845F20C6FE39E07EA2CCE61F0C9BB048165FE5E4DE877550",
194206
]
195207
),
208+
Precompile.BN128_PAIRING,
196209
id="bn128_one_pairing",
197210
),
198211
# Ported from
199212
# https://github.com/NethermindEth/nethermind/blob/ceb8d57b8530ce8181d7427c115ca593386909d6/tools/EngineRequestsGenerator/TestCase.cs#L353
200-
pytest.param(0x08, [], id="ec_pairing_zero_input"),
213+
pytest.param(
214+
0x08,
215+
[],
216+
Precompile.BN128_PAIRING,
217+
id="ec_pairing_zero_input",
218+
),
201219
pytest.param(
202220
0x08,
203221
concatenate_parameters(
@@ -218,11 +236,13 @@
218236
"3a8eb0b0996252cb548a4487da97b02422ebc0e834613f954de6c7e0afdc1fc0",
219237
]
220238
),
239+
Precompile.BN128_PAIRING,
221240
id="ec_pairing_2_sets",
222241
),
223242
pytest.param(
224243
0x08,
225244
concatenate_parameters([""]),
245+
Precompile.BN128_PAIRING,
226246
id="ec_pairing_1_pair",
227247
),
228248
pytest.param(
@@ -245,6 +265,7 @@
245265
"12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa",
246266
]
247267
),
268+
Precompile.BN128_PAIRING,
248269
id="ec_pairing_2_pair",
249270
),
250271
pytest.param(
@@ -272,6 +293,7 @@
272293
"12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa",
273294
]
274295
),
296+
Precompile.BN128_PAIRING,
275297
id="ec_pairing_3_pair",
276298
),
277299
pytest.param(
@@ -308,6 +330,7 @@
308330
"2dc4cb08068b4aa5f14b7f1096ab35d5c13d78319ec7e66e9f67a1ff20cbbf03",
309331
]
310332
),
333+
Precompile.BN128_PAIRING,
311334
id="ec_pairing_4_pair",
312335
),
313336
pytest.param(
@@ -351,6 +374,7 @@
351374
"1ac5dac62d2332faa8069faca3b0d27fcdf95d8c8bafc9074ee72b5c1f33aa70",
352375
]
353376
),
377+
Precompile.BN128_PAIRING,
354378
id="ec_pairing_5_pair",
355379
),
356380
pytest.param(
@@ -360,6 +384,7 @@
360384
"0000000000000000000000000000000000000000000000000000000000000000",
361385
]
362386
),
387+
Precompile.BN128_PAIRING,
363388
id="ec_pairing_1_pair_empty",
364389
),
365390
],
@@ -368,6 +393,7 @@ def test_alt_bn128(
368393
benchmark_test: BenchmarkTestFiller,
369394
precompile_address: Address,
370395
calldata: bytes,
396+
target: OpcodeTarget,
371397
) -> None:
372398
"""Benchmark ALT_BN128 precompile."""
373399
attack_block = Op.POP(
@@ -377,7 +403,7 @@ def test_alt_bn128(
377403
)
378404

379405
benchmark_test(
380-
target_opcode=Op.STATICCALL,
406+
target_opcode=target,
381407
code_generator=JumpLoopGenerator(
382408
setup=Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE),
383409
attack_block=attack_block,
@@ -493,7 +519,7 @@ def test_bn128_pairings_amortized(
493519
)
494520

495521
benchmark_test(
496-
target_opcode=Op.STATICCALL,
522+
target_opcode=Precompile.BN128_PAIRING,
497523
code_generator=JumpLoopGenerator(
498524
setup=setup,
499525
attack_block=attack_block,
@@ -520,7 +546,7 @@ def test_alt_bn128_benchmark(
520546
)
521547

522548
benchmark_test(
523-
target_opcode=Op.STATICCALL,
549+
target_opcode=Precompile.BN128_PAIRING,
524550
code_generator=JumpLoopGenerator(
525551
setup=Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE),
526552
attack_block=attack_block,
@@ -633,7 +659,7 @@ def test_ec_pairing(
633659
seed_offset += per_tx_variants
634660

635661
benchmark_test(
636-
target_opcode=Op.STATICCALL,
662+
target_opcode=Precompile.BN128_PAIRING,
637663
skip_gas_used_validation=True,
638664
blocks=[Block(txs=txs)],
639665
)
@@ -652,11 +678,21 @@ def _generate_g1_point(seed: int) -> Bytes:
652678

653679
@pytest.mark.repricing
654680
@pytest.mark.parametrize(
655-
"precompile_address,scalar",
681+
"precompile_address,scalar,target",
656682
[
657-
pytest.param(0x06, None, id="ec_add"),
658-
pytest.param(0x07, 2, id="ec_mul_small_scalar"),
659-
pytest.param(0x07, 2**256 - 1, id="ec_mul_max_scalar"),
683+
pytest.param(0x06, None, Precompile.BN128_ADD, id="ec_add"),
684+
pytest.param(
685+
0x07,
686+
2,
687+
Precompile.BN128_MUL,
688+
id="ec_mul_small_scalar",
689+
),
690+
pytest.param(
691+
0x07,
692+
2**256 - 1,
693+
Precompile.BN128_MUL,
694+
id="ec_mul_max_scalar",
695+
),
660696
],
661697
)
662698
def test_alt_bn128_uncachable(
@@ -667,6 +703,7 @@ def test_alt_bn128_uncachable(
667703
tx_gas_limit: int,
668704
precompile_address: Address,
669705
scalar: int | None,
706+
target: OpcodeTarget,
670707
) -> None:
671708
"""
672709
Benchmark ecAdd/ecMul with unique input per call.
@@ -722,6 +759,6 @@ def test_alt_bn128_uncachable(
722759
seed += 1
723760

724761
benchmark_test(
725-
target_opcode=Op.STATICCALL,
762+
target_opcode=target,
726763
blocks=[Block(txs=txs)],
727764
)

0 commit comments

Comments
 (0)