From 0a8b8563c089243bede072a03ad6fd9bff19b6a3 Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Wed, 7 May 2025 21:48:52 -0300 Subject: [PATCH 01/19] zkevm: more precompiles Signed-off-by: Ignacio Hagopian --- tests/zkevm/test_worst_compute.py | 120 +++++++++++++++++++++++++++--- 1 file changed, 108 insertions(+), 12 deletions(-) diff --git a/tests/zkevm/test_worst_compute.py b/tests/zkevm/test_worst_compute.py index 1b6f19c0d56..afd0ef89003 100644 --- a/tests/zkevm/test_worst_compute.py +++ b/tests/zkevm/test_worst_compute.py @@ -19,8 +19,8 @@ Bytecode, Environment, Transaction, - While, ) +from ethereum_test_tools.code.generators import While from ethereum_test_tools.vm.opcode import Opcodes as Op from ethereum_test_vm import Opcode @@ -29,7 +29,6 @@ MAX_CODE_SIZE = 24 * 1024 KECCAK_RATE = 136 -ECRECOVER_GAS_COST = 3_000 @pytest.mark.valid_from("Cancun") @@ -237,23 +236,120 @@ def test_worst_modexp( @pytest.mark.valid_from("Cancun") -def test_worst_ecrecover( +@pytest.mark.parametrize( + "gas_limit", + [ + Environment().gas_limit, + ], +) +@pytest.mark.parametrize( + "precompile_address,precompile_gas_cost,input,length", + [ + pytest.param( + 0x01, + 3_000, + [ + 0x38D18ACB67D25C8BB9942764B62F18E17054F66A817BD4295423ADF9ED98873E, + 27, + 0x38D18ACB67D25C8BB9942764B62F18E17054F66A817BD4295423ADF9ED98873E, + 0x789D1DD423D25F0772D2748D60F7E4B81BB14D086EBA8E8E8EFB6DCFF8A4AE02, + ], + 32 * 4, + id="ecrecover", + ), + pytest.param( + 0x06, + 150, + [ + 0x18B18ACFB4C2C30276DB5411368E7185B311DD124691610C5D3B74034E093DC9, + 0x063C909C4720840CB5134CB9F59FA749755796819658D32EFC0D288198F37266, + 0x07C2B7F58A84BD6145F00C9C2BC0BB1A187F20FF2C92963A88019E7C6A014EED, + 0x06614E20C147E940F2D70DA3F74C9A17DF361706A4485C742BD6788478FA17D7, + ], + 32 * 4, + id="bn128add", + ), + pytest.param( + 0x07, + 6_000, + [ + 0x1A87B0584CE92F4593D161480614F2989035225609F08058CCFA3D0F940FEBE3, + 0x1A2F3C951F6DADCC7EE9007DFF81504B0FCD6D7CF59996EFDC33D92BF7F9F8F6, + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + ], + 32 * 4, + id="bn128mul", + ), + pytest.param( + 0x08, + 45_000 + 2 * 34_000, + [ + 0x1C76476F4DEF4BB94541D57EBBA1193381FFA7AA76ADA664DD31C16024C43F59, + 0x3034DD2920F673E204FEE2811C678745FC819B55D3E9D294E45C9B03A76AEF41, + 0x209DD15EBFF5D46C4BD888E51A93CF99A7329636C63514396B4A452003A35BF7, + 0x04BF11CA01483BFA8B34B43561848D28905960114C8AC04049AF4B6315A41678, + 0x2BB8324AF6CFC93537A2AD1A445CFD0CA2A71ACD7AC41FADBF933C2A51BE344D, + 0x120A2A4CF30C1BF9845F20C6FE39E07EA2CCE61F0C9BB048165FE5E4DE877550, + 0x111E129F1CF1097710D41C4AC70FCDFA5BA2023C6FF1CBEAC322DE49D1B6DF7C, + 0x103188585E2364128FE25C70558F1560F4F9350BAF3959E603CC91486E110936, + 0x198E9393920D483A7260BFB731FB5D25F1AA493335A9E71297E485B7AEF312C2, + 0x1800DEEF121F1E76426A00665E5C4479674322D4F75EDADD46DEBD5CD992F6ED, + 0x090689D0585FF075EC9E99AD690C3395BC4B313370B38EF355ACDADCD122975B, + 0x12C85EA5DB8C6DEB4AAB71808DCB408FE3D1E7690C43D37B4CE6CC0166FA7DAA, + ], + 32 * 12, + id="bn128pairing", + ), + pytest.param( + 0x09, + 0xFFFF, + [ + 0x0000FFFF48C9BDF267E6096A3BA7CA8485AE67BB2BF894FE72F36E3CF1361D5F, + 0x3AF54FA5D182E6AD7F520E511F6C3E2B8C68059B6BBD41FBABD9831F79217E13, + 0x19CDE05B61626300000000000000000000000000000000000000000000000000, + 0x0000000000000000000000000000000000000000000000000000000000000000, + 0x0000000000000000000000000000000000000000000000000000000000000000, + 0x0000000000000000000000000000000000000000000000000000000000000000, + 0x0000000000000000000000000000000000000000000000000000000000000000, + 0x0000000000000000000000000000000000000000000000000000000000000000, + 0x0000000003000000000000000000000000000001000000000000000000000000, + ], + 213, + id="blake2f", + ), + pytest.param( + 0x0A, + 50_000, + [ + 0x01E798154708FE7789429634053CBF9F99B619F9F084048927333FCE637F549B, + 0x564C0A11A0F704F4FC3E8ACFE0F8245F0AD1347B378FBF96E206DA11A5D36306, + 0x24D25032E67A7E6A4910DF5834B8FE70E6BCFEEAC0352434196BDF4B2485D5A1, + 0x8F59A8D2A1A625A17F3FEA0FE5EB8C896DB3764F3185481BC22F91B4AAFFCCA2, + 0x5F26936857BC3A7C2539EA8EC3A952B7873033E038326E87ED3E1276FD140253, + 0xFA08E9FC25FB2D9A98527FC22A2C9612FBEAFDAD446CBC7BCDBDCD780AF2C16A, + ], + 192, + id="point_evaluation", + ), + ], +) +def test_worst_precompile_fixed_cost( blockchain_test: BlockchainTestFiller, pre: Alloc, fork: Fork, + gas_limit: int, + precompile_address: Address, + precompile_gas_cost: Address, + input: list[int], + length: int, ): - """Test running a block with as many ECRECOVER calls as possible.""" - env = Environment() + """Test running a block filled with a precompile with fixed cost.""" + env = Environment(gas_limit=gas_limit) # Calldata - calldata = ( - Op.MSTORE(0 * 32, 0x38D18ACB67D25C8BB9942764B62F18E17054F66A817BD4295423ADF9ED98873E) - + Op.MSTORE(1 * 32, 27) - + Op.MSTORE(2 * 32, 0x38D18ACB67D25C8BB9942764B62F18E17054F66A817BD4295423ADF9ED98873E) - + Op.MSTORE(3 * 32, 0x789D1DD423D25F0772D2748D60F7E4B81BB14D086EBA8E8E8EFB6DCFF8A4AE02) - ) + calldata = sum([Op.MSTORE(i * 32, input) for i, input in enumerate(input)]) - attack_block = Op.POP(Op.STATICCALL(ECRECOVER_GAS_COST, 0x1, 0, 32 * 4, 0, 0)) + attack_block = Op.POP(Op.STATICCALL(precompile_gas_cost, precompile_address, 0, length, 0, 0)) code = code_loop_precompile_call(calldata, attack_block) code_address = pre.deploy_contract(code=bytes(code)) From 9e149af5c831a58d6ed80dd0509883e01d0f699c Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Thu, 8 May 2025 11:02:42 -0300 Subject: [PATCH 02/19] improve params format Signed-off-by: Ignacio Hagopian --- tests/zkevm/test_worst_compute.py | 109 ++++++++++++++++++------------ 1 file changed, 64 insertions(+), 45 deletions(-) diff --git a/tests/zkevm/test_worst_compute.py b/tests/zkevm/test_worst_compute.py index afd0ef89003..8d674ad491c 100644 --- a/tests/zkevm/test_worst_compute.py +++ b/tests/zkevm/test_worst_compute.py @@ -249,10 +249,10 @@ def test_worst_modexp( 0x01, 3_000, [ - 0x38D18ACB67D25C8BB9942764B62F18E17054F66A817BD4295423ADF9ED98873E, - 27, - 0x38D18ACB67D25C8BB9942764B62F18E17054F66A817BD4295423ADF9ED98873E, - 0x789D1DD423D25F0772D2748D60F7E4B81BB14D086EBA8E8E8EFB6DCFF8A4AE02, + "38D18ACB67D25C8BB9942764B62F18E17054F66A817BD4295423ADF9ED98873E", + "000000000000000000000000000000000000000000000000000000000000001B", + "38D18ACB67D25C8BB9942764B62F18E17054F66A817BD4295423ADF9ED98873E", + "789D1DD423D25F0772D2748D60F7E4B81BB14D086EBA8E8E8EFB6DCFF8A4AE02", ], 32 * 4, id="ecrecover", @@ -261,58 +261,58 @@ def test_worst_modexp( 0x06, 150, [ - 0x18B18ACFB4C2C30276DB5411368E7185B311DD124691610C5D3B74034E093DC9, - 0x063C909C4720840CB5134CB9F59FA749755796819658D32EFC0D288198F37266, - 0x07C2B7F58A84BD6145F00C9C2BC0BB1A187F20FF2C92963A88019E7C6A014EED, - 0x06614E20C147E940F2D70DA3F74C9A17DF361706A4485C742BD6788478FA17D7, + "18B18ACFB4C2C30276DB5411368E7185B311DD124691610C5D3B74034E093DC9", + "063C909C4720840CB5134CB9F59FA749755796819658D32EFC0D288198F37266", + "07C2B7F58A84BD6145F00C9C2BC0BB1A187F20FF2C92963A88019E7C6A014EED", + "06614E20C147E940F2D70DA3F74C9A17DF361706A4485C742BD6788478FA17D7", ], 32 * 4, - id="bn128add", + id="bn128_add", ), pytest.param( 0x07, 6_000, [ - 0x1A87B0584CE92F4593D161480614F2989035225609F08058CCFA3D0F940FEBE3, - 0x1A2F3C951F6DADCC7EE9007DFF81504B0FCD6D7CF59996EFDC33D92BF7F9F8F6, - 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, + "1A87B0584CE92F4593D161480614F2989035225609F08058CCFA3D0F940FEBE3", + "1A2F3C951F6DADCC7EE9007DFF81504B0FCD6D7CF59996EFDC33D92BF7F9F8F6", + "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", ], 32 * 4, - id="bn128mul", + id="bn128_mul", ), pytest.param( 0x08, 45_000 + 2 * 34_000, [ - 0x1C76476F4DEF4BB94541D57EBBA1193381FFA7AA76ADA664DD31C16024C43F59, - 0x3034DD2920F673E204FEE2811C678745FC819B55D3E9D294E45C9B03A76AEF41, - 0x209DD15EBFF5D46C4BD888E51A93CF99A7329636C63514396B4A452003A35BF7, - 0x04BF11CA01483BFA8B34B43561848D28905960114C8AC04049AF4B6315A41678, - 0x2BB8324AF6CFC93537A2AD1A445CFD0CA2A71ACD7AC41FADBF933C2A51BE344D, - 0x120A2A4CF30C1BF9845F20C6FE39E07EA2CCE61F0C9BB048165FE5E4DE877550, - 0x111E129F1CF1097710D41C4AC70FCDFA5BA2023C6FF1CBEAC322DE49D1B6DF7C, - 0x103188585E2364128FE25C70558F1560F4F9350BAF3959E603CC91486E110936, - 0x198E9393920D483A7260BFB731FB5D25F1AA493335A9E71297E485B7AEF312C2, - 0x1800DEEF121F1E76426A00665E5C4479674322D4F75EDADD46DEBD5CD992F6ED, - 0x090689D0585FF075EC9E99AD690C3395BC4B313370B38EF355ACDADCD122975B, - 0x12C85EA5DB8C6DEB4AAB71808DCB408FE3D1E7690C43D37B4CE6CC0166FA7DAA, + "1C76476F4DEF4BB94541D57EBBA1193381FFA7AA76ADA664DD31C16024C43F59", + "3034DD2920F673E204FEE2811C678745FC819B55D3E9D294E45C9B03A76AEF41", + "209DD15EBFF5D46C4BD888E51A93CF99A7329636C63514396B4A452003A35BF7", + "04BF11CA01483BFA8B34B43561848D28905960114C8AC04049AF4B6315A41678", + "2BB8324AF6CFC93537A2AD1A445CFD0CA2A71ACD7AC41FADBF933C2A51BE344D", + "120A2A4CF30C1BF9845F20C6FE39E07EA2CCE61F0C9BB048165FE5E4DE877550", + "111E129F1CF1097710D41C4AC70FCDFA5BA2023C6FF1CBEAC322DE49D1B6DF7C", + "103188585E2364128FE25C70558F1560F4F9350BAF3959E603CC91486E110936", + "198E9393920D483A7260BFB731FB5D25F1AA493335A9E71297E485B7AEF312C2", + "1800DEEF121F1E76426A00665E5C4479674322D4F75EDADD46DEBD5CD992F6ED", + "090689D0585FF075EC9E99AD690C3395BC4B313370B38EF355ACDADCD122975B", + "12C85EA5DB8C6DEB4AAB71808DCB408FE3D1E7690C43D37B4CE6CC0166FA7DAA", ], 32 * 12, - id="bn128pairing", + id="bn128_pairing", ), pytest.param( 0x09, 0xFFFF, [ - 0x0000FFFF48C9BDF267E6096A3BA7CA8485AE67BB2BF894FE72F36E3CF1361D5F, - 0x3AF54FA5D182E6AD7F520E511F6C3E2B8C68059B6BBD41FBABD9831F79217E13, - 0x19CDE05B61626300000000000000000000000000000000000000000000000000, - 0x0000000000000000000000000000000000000000000000000000000000000000, - 0x0000000000000000000000000000000000000000000000000000000000000000, - 0x0000000000000000000000000000000000000000000000000000000000000000, - 0x0000000000000000000000000000000000000000000000000000000000000000, - 0x0000000000000000000000000000000000000000000000000000000000000000, - 0x0000000003000000000000000000000000000001000000000000000000000000, + "0000FFFF48C9BDF267E6096A3BA7CA8485AE67BB2BF894FE72F36E3CF1361D5F", + "3AF54FA5D182E6AD7F520E511F6C3E2B8C68059B6BBD41FBABD9831F79217E13", + "19CDE05B61626300000000000000000000000000000000000000000000000000", + "0000000000000000000000000000000000000000000000000000000000000000", + "0000000000000000000000000000000000000000000000000000000000000000", + "0000000000000000000000000000000000000000000000000000000000000000", + "0000000000000000000000000000000000000000000000000000000000000000", + "0000000000000000000000000000000000000000000000000000000000000000", + "0000000003000000000000000000000000000001000000000000000000000000", ], 213, id="blake2f", @@ -321,16 +321,26 @@ def test_worst_modexp( 0x0A, 50_000, [ - 0x01E798154708FE7789429634053CBF9F99B619F9F084048927333FCE637F549B, - 0x564C0A11A0F704F4FC3E8ACFE0F8245F0AD1347B378FBF96E206DA11A5D36306, - 0x24D25032E67A7E6A4910DF5834B8FE70E6BCFEEAC0352434196BDF4B2485D5A1, - 0x8F59A8D2A1A625A17F3FEA0FE5EB8C896DB3764F3185481BC22F91B4AAFFCCA2, - 0x5F26936857BC3A7C2539EA8EC3A952B7873033E038326E87ED3E1276FD140253, - 0xFA08E9FC25FB2D9A98527FC22A2C9612FBEAFDAD446CBC7BCDBDCD780AF2C16A, + "01E798154708FE7789429634053CBF9F99B619F9F084048927333FCE637F549B", + "564C0A11A0F704F4FC3E8ACFE0F8245F0AD1347B378FBF96E206DA11A5D36306", + "24D25032E67A7E6A4910DF5834B8FE70E6BCFEEAC0352434196BDF4B2485D5A1", + "8F59A8D2A1A625A17F3FEA0FE5EB8C896DB3764F3185481BC22F91B4AAFFCCA2", + "5F26936857BC3A7C2539EA8EC3A952B7873033E038326E87ED3E1276FD140253", + "FA08E9FC25FB2D9A98527FC22A2C9612FBEAFDAD446CBC7BCDBDCD780AF2C16A", ], 192, id="point_evaluation", ), + pytest.param( + 0x0B, + 375, + [ + "000000000000000000000000000000000572CBEA904D67468808C8EB50A9450C9721DB309128012543902D0AC358A62AE28F75BB8F1C7C42C39A8C5529BF0F4E00000000000000000000000000000000166A9D8CABC673A322FDA673779D8E3822BA3ECB8670E461F73BB9021D5FD76A", + "4C56D9D4CD16BD1BBA86881979749D280000000000000000000000000000000009ECE308F9D1F0131765212DECA99697B112D61F9BE9A5F1F3780A51335B3FF981747A0B2CA2179B96D2C0C9024E522400000000000000000000000000000000032B80D3A6F5B09F8A84623389C5F80C", + ], + 256, + id="bls12_g1add", + ), ], ) def test_worst_precompile_fixed_cost( @@ -339,15 +349,24 @@ def test_worst_precompile_fixed_cost( fork: Fork, gas_limit: int, precompile_address: Address, - precompile_gas_cost: Address, - input: list[int], + precompile_gas_cost: int, + input: list[str], length: int, ): """Test running a block filled with a precompile with fixed cost.""" env = Environment(gas_limit=gas_limit) - # Calldata - calldata = sum([Op.MSTORE(i * 32, input) for i, input in enumerate(input)]) + concatenated_hex_string = "".join(input) + concatenated_bytes = bytes.fromhex(concatenated_hex_string) + + padding_length = (32 - (len(concatenated_bytes) % 32)) % 32 + input_bytes = concatenated_bytes + b"\x00" * padding_length + + calldata = Bytecode() + for i in range(0, len(input_bytes), 32): + chunk = input_bytes[i : i + 32] + value_to_store = int.from_bytes(chunk, "big") + calldata += Op.MSTORE(i, value_to_store) attack_block = Op.POP(Op.STATICCALL(precompile_gas_cost, precompile_address, 0, length, 0, 0)) code = code_loop_precompile_call(calldata, attack_block) From edaef739268c7535ddf17da4eaabbf9ea2e67479 Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Thu, 8 May 2025 11:15:53 -0300 Subject: [PATCH 03/19] improve parameters Signed-off-by: Ignacio Hagopian --- tests/zkevm/test_worst_compute.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/tests/zkevm/test_worst_compute.py b/tests/zkevm/test_worst_compute.py index 8d674ad491c..8bde4e73801 100644 --- a/tests/zkevm/test_worst_compute.py +++ b/tests/zkevm/test_worst_compute.py @@ -304,15 +304,13 @@ def test_worst_modexp( 0x09, 0xFFFF, [ - "0000FFFF48C9BDF267E6096A3BA7CA8485AE67BB2BF894FE72F36E3CF1361D5F", - "3AF54FA5D182E6AD7F520E511F6C3E2B8C68059B6BBD41FBABD9831F79217E13", - "19CDE05B61626300000000000000000000000000000000000000000000000000", - "0000000000000000000000000000000000000000000000000000000000000000", - "0000000000000000000000000000000000000000000000000000000000000000", - "0000000000000000000000000000000000000000000000000000000000000000", - "0000000000000000000000000000000000000000000000000000000000000000", - "0000000000000000000000000000000000000000000000000000000000000000", - "0000000003000000000000000000000000000001000000000000000000000000", + "0000FFFF", + "48C9BDF267E6096A3BA7CA8485AE67BB2BF894FE72F36E3CF1361D5F3AF54FA5D182E6AD7F520E511F6C3E2B8C68059B6BBD41FBABD9831F79217E1319CDE05B", + "61626300000000000000000000000000000000000000000000000000", + "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "000000000300000000000000000000", + "00000000010000000000000000000000", + "00", ], 213, id="blake2f", @@ -324,9 +322,8 @@ def test_worst_modexp( "01E798154708FE7789429634053CBF9F99B619F9F084048927333FCE637F549B", "564C0A11A0F704F4FC3E8ACFE0F8245F0AD1347B378FBF96E206DA11A5D36306", "24D25032E67A7E6A4910DF5834B8FE70E6BCFEEAC0352434196BDF4B2485D5A1", - "8F59A8D2A1A625A17F3FEA0FE5EB8C896DB3764F3185481BC22F91B4AAFFCCA2", - "5F26936857BC3A7C2539EA8EC3A952B7873033E038326E87ED3E1276FD140253", - "FA08E9FC25FB2D9A98527FC22A2C9612FBEAFDAD446CBC7BCDBDCD780AF2C16A", + "8F59A8D2A1A625A17F3FEA0FE5EB8C896DB3764F3185481BC22F91B4AAFFCCA25F26936857BC3A7C2539EA8EC3A952B7", + "873033E038326E87ED3E1276FD140253FA08E9FC25FB2D9A98527FC22A2C9612FBEAFDAD446CBC7BCDBDCD780AF2C16A", ], 192, id="point_evaluation", From d5dc499c60ca190a7d3b486c4587dbf4b6e55583 Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Thu, 8 May 2025 11:40:02 -0300 Subject: [PATCH 04/19] add bls12_g1msm Signed-off-by: Ignacio Hagopian --- tests/zkevm/test_worst_compute.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/tests/zkevm/test_worst_compute.py b/tests/zkevm/test_worst_compute.py index 8bde4e73801..5720daff01a 100644 --- a/tests/zkevm/test_worst_compute.py +++ b/tests/zkevm/test_worst_compute.py @@ -243,11 +243,10 @@ def test_worst_modexp( ], ) @pytest.mark.parametrize( - "precompile_address,precompile_gas_cost,input,length", + "precompile_address,input,length", [ pytest.param( 0x01, - 3_000, [ "38D18ACB67D25C8BB9942764B62F18E17054F66A817BD4295423ADF9ED98873E", "000000000000000000000000000000000000000000000000000000000000001B", @@ -259,7 +258,6 @@ def test_worst_modexp( ), pytest.param( 0x06, - 150, [ "18B18ACFB4C2C30276DB5411368E7185B311DD124691610C5D3B74034E093DC9", "063C909C4720840CB5134CB9F59FA749755796819658D32EFC0D288198F37266", @@ -271,7 +269,6 @@ def test_worst_modexp( ), pytest.param( 0x07, - 6_000, [ "1A87B0584CE92F4593D161480614F2989035225609F08058CCFA3D0F940FEBE3", "1A2F3C951F6DADCC7EE9007DFF81504B0FCD6D7CF59996EFDC33D92BF7F9F8F6", @@ -282,14 +279,15 @@ def test_worst_modexp( ), pytest.param( 0x08, - 45_000 + 2 * 34_000, [ + # First pairing "1C76476F4DEF4BB94541D57EBBA1193381FFA7AA76ADA664DD31C16024C43F59", "3034DD2920F673E204FEE2811C678745FC819B55D3E9D294E45C9B03A76AEF41", "209DD15EBFF5D46C4BD888E51A93CF99A7329636C63514396B4A452003A35BF7", "04BF11CA01483BFA8B34B43561848D28905960114C8AC04049AF4B6315A41678", "2BB8324AF6CFC93537A2AD1A445CFD0CA2A71ACD7AC41FADBF933C2A51BE344D", "120A2A4CF30C1BF9845F20C6FE39E07EA2CCE61F0C9BB048165FE5E4DE877550", + # Second pairing "111E129F1CF1097710D41C4AC70FCDFA5BA2023C6FF1CBEAC322DE49D1B6DF7C", "103188585E2364128FE25C70558F1560F4F9350BAF3959E603CC91486E110936", "198E9393920D483A7260BFB731FB5D25F1AA493335A9E71297E485B7AEF312C2", @@ -297,12 +295,11 @@ def test_worst_modexp( "090689D0585FF075EC9E99AD690C3395BC4B313370B38EF355ACDADCD122975B", "12C85EA5DB8C6DEB4AAB71808DCB408FE3D1E7690C43D37B4CE6CC0166FA7DAA", ], - 32 * 12, + 32 * 2 * 6, id="bn128_pairing", ), pytest.param( 0x09, - 0xFFFF, [ "0000FFFF", "48C9BDF267E6096A3BA7CA8485AE67BB2BF894FE72F36E3CF1361D5F3AF54FA5D182E6AD7F520E511F6C3E2B8C68059B6BBD41FBABD9831F79217E1319CDE05B", @@ -317,7 +314,6 @@ def test_worst_modexp( ), pytest.param( 0x0A, - 50_000, [ "01E798154708FE7789429634053CBF9F99B619F9F084048927333FCE637F549B", "564C0A11A0F704F4FC3E8ACFE0F8245F0AD1347B378FBF96E206DA11A5D36306", @@ -330,7 +326,6 @@ def test_worst_modexp( ), pytest.param( 0x0B, - 375, [ "000000000000000000000000000000000572CBEA904D67468808C8EB50A9450C9721DB309128012543902D0AC358A62AE28F75BB8F1C7C42C39A8C5529BF0F4E00000000000000000000000000000000166A9D8CABC673A322FDA673779D8E3822BA3ECB8670E461F73BB9021D5FD76A", "4C56D9D4CD16BD1BBA86881979749D280000000000000000000000000000000009ECE308F9D1F0131765212DECA99697B112D61F9BE9A5F1F3780A51335B3FF981747A0B2CA2179B96D2C0C9024E522400000000000000000000000000000000032B80D3A6F5B09F8A84623389C5F80C", @@ -338,6 +333,16 @@ def test_worst_modexp( 256, id="bls12_g1add", ), + pytest.param( + 0x0C, + [ + "0000000000000000000000000000000017f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb0000000000000000000000000000000008b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e10000000000000000000000000000000000000000000000000000000000000032", + "000000000000000000000000000000000e12039459c60491672b6a6282355d8765ba6272387fb91a3e9604fa2a81450cf16b870bb446fc3a3e0a187fff6f89450000000000000000000000000000000018b6c1ed9f45d3cbc0b01b9d038dcecacbd702eb26469a0eb3905bd421461712f67f782b4735849644c1772c93fe3d090000000000000000000000000000000000000000000000000000000000000033", + "00000000000000000000000000000000147b327c8a15b39634a426af70c062b50632a744eddd41b5a4686414ef4cd9746bb11d0a53c6c2ff21bbcf331e07ac9200000000000000000000000000000000078c2e9782fa5d9ab4e728684382717aa2b8fad61b5f5e7cf3baa0bc9465f57342bb7c6d7b232e70eebcdbf70f903a450000000000000000000000000000000000000000000000000000000000000034", + ], + 3 * 160, + id="bls12_g1msm", + ), ], ) def test_worst_precompile_fixed_cost( @@ -346,7 +351,6 @@ def test_worst_precompile_fixed_cost( fork: Fork, gas_limit: int, precompile_address: Address, - precompile_gas_cost: int, input: list[str], length: int, ): @@ -365,7 +369,7 @@ def test_worst_precompile_fixed_cost( value_to_store = int.from_bytes(chunk, "big") calldata += Op.MSTORE(i, value_to_store) - attack_block = Op.POP(Op.STATICCALL(precompile_gas_cost, precompile_address, 0, length, 0, 0)) + attack_block = Op.POP(Op.STATICCALL(Op.GAS, precompile_address, 0, length, 0, 0)) code = code_loop_precompile_call(calldata, attack_block) code_address = pre.deploy_contract(code=bytes(code)) From 37cd025dbec3b7800416f18961dba53815ce5cc0 Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Thu, 8 May 2025 11:58:13 -0300 Subject: [PATCH 05/19] add bls12_g2msm Signed-off-by: Ignacio Hagopian --- tests/zkevm/test_worst_compute.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/zkevm/test_worst_compute.py b/tests/zkevm/test_worst_compute.py index 5720daff01a..02538277f4f 100644 --- a/tests/zkevm/test_worst_compute.py +++ b/tests/zkevm/test_worst_compute.py @@ -343,6 +343,25 @@ def test_worst_modexp( 3 * 160, id="bls12_g1msm", ), + pytest.param( + 0x0D, + [ + "000000000000000000000000000000001638533957d540a9d2370f17cc7ed5863bc0b995b8825e0ee1ea1e1e4d00dbae81f14b0bf3611b78c952aacab827a053000000000000000000000000000000000a4edef9c1ed7f729f520e47730a124fd70662a904ba1074728114d1031e1572c6c886f6b57ec72a6178288c47c33577000000000000000000000000000000000468fb440d82b0630aeb8dca2b5256789a66da69bf91009cbfe6bd221e47aa8ae88dece9764bf3bd999d95d71e4c9899000000000000000000000000000000000f6d4552fa65dd2638b361543f887136a43253d9c66c411697003f7a13c308f5422e1aa0a59c8967acdefd8b6e36ccf3", + "00000000000000000000000000000000122915c824a0857e2ee414a3dccb23ae691ae54329781315a0c75df1c04d6d7a50a030fc866f09d516020ef82324afae0000000000000000000000000000000009380275bbc8e5dcea7dc4dd7e0550ff2ac480905396eda55062650f8d251c96eb480673937cc6d9d6a44aaa56ca66dc000000000000000000000000000000000b21da7955969e61010c7a1abc1a6f0136961d1e3b20b1a7326ac738fef5c721479dfd948b52fdf2455e44813ecfd8920000000000000000000000000000000008f239ba329b3967fe48d718a36cfe5f62a7e42e0bf1c1ed714150a166bfbd6bcf6b3b58b975b9edea56d53f23a0e849", + ], + 2 * 256, + id="bls12_g2add", + ), + pytest.param( + 0x0E, + [ + "00000000000000000000000000000000024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb80000000000000000000000000000000013e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e000000000000000000000000000000000ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801000000000000000000000000000000000606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79be0000000000000000000000000000000000000000000000000000000000000032", + "0000000000000000000000000000000019d5f05b4f134bb37d89a03e87c8b729e6bdc062f3ae0ddc5265b270e40a6a5691f51ff60b764ea760651caf395101840000000000000000000000000000000015532df6a12b7c160a0831ef8321b18feb6ce7997c0718b205873608085be3afeec5b5d5251a0f85f7f5b7271271e0660000000000000000000000000000000004623ac0df1e019d337dc9488c17ef9e214dc33c63f96a90fea288e836dbd85079cb3cec42ae693e9c16af3c3204d86e0000000000000000000000000000000011ba77f71923c1b6a711a48fa4085c4885290079448a4b597030cc84aa14647136513cec6d11c4453ca74e906bbca1e10000000000000000000000000000000000000000000000000000000000000033", + "00000000000000000000000000000000176a7158b310c9ff1bfc21b81903de99c90440792ebe6d9637652ee34acf53b43c2f31738bbc96d71dcadbbf0e3190af000000000000000000000000000000000a592641967934a97e012f7d6412c4f6ff0f177a1b466b9b49c9deb7498decc80d0c809448aa9fa6fbbb6f537515703000000000000000000000000000000000031d84356ef619e688a10247f122e1aa0d3def3e35f94043f64c634198421487ca96af5f0160384bba92bd5494506c4d000000000000000000000000000000000db8fefe735779489c957785fa8e45d24e086ef0c2aba2e3adba888f0aeee51385a82898524c443f017ee40be635048c0000000000000000000000000000000000000000000000000000000000000034", + ], + 3 * 288, + id="bls12_g2msm", + ), ], ) def test_worst_precompile_fixed_cost( From 1385299573402c4bf28d8001b38acc4dc2d7110d Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Thu, 8 May 2025 12:03:10 -0300 Subject: [PATCH 06/19] add blas12 pairing check Signed-off-by: Ignacio Hagopian --- tests/zkevm/test_worst_compute.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/zkevm/test_worst_compute.py b/tests/zkevm/test_worst_compute.py index 02538277f4f..1995e7cf134 100644 --- a/tests/zkevm/test_worst_compute.py +++ b/tests/zkevm/test_worst_compute.py @@ -362,6 +362,14 @@ def test_worst_modexp( 3 * 288, id="bls12_g2msm", ), + pytest.param( + 0x0F, + [ + "000000000000000000000000000000000572cbea904d67468808c8eb50a9450c9721db309128012543902d0ac358a62ae28f75bb8f1c7c42c39a8c5529bf0f4e00000000000000000000000000000000166a9d8cabc673a322fda673779d8e3822ba3ecb8670e461f73bb9021d5fd76a4c56d9d4cd16bd1bba86881979749d2800000000000000000000000000000000122915c824a0857e2ee414a3dccb23ae691ae54329781315a0c75df1c04d6d7a50a030fc866f09d516020ef82324afae0000000000000000000000000000000009380275bbc8e5dcea7dc4dd7e0550ff2ac480905396eda55062650f8d251c96eb480673937cc6d9d6a44aaa56ca66dc000000000000000000000000000000000b21da7955969e61010c7a1abc1a6f0136961d1e3b20b1a7326ac738fef5c721479dfd948b52fdf2455e44813ecfd8920000000000000000000000000000000008f239ba329b3967fe48d718a36cfe5f62a7e42e0bf1c1ed714150a166bfbd6bcf6b3b58b975b9edea56d53f23a0e8490000000000000000000000000000000006e82f6da4520f85c5d27d8f329eccfa05944fd1096b20734c894966d12a9e2a9a9744529d7212d33883113a0cadb9090000000000000000000000000000000017d81038f7d60bee9110d9c0d6d1102fe2d998c957f28e31ec284cc04134df8e47e8f82ff3af2e60a6d9688a4563477c00000000000000000000000000000000024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb80000000000000000000000000000000013e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e000000000000000000000000000000000d1b3cc2c7027888be51d9ef691d77bcb679afda66c73f17f9ee3837a55024f78c71363275a75d75d86bab79f74782aa0000000000000000000000000000000013fa4d4a0ad8b1ce186ed5061789213d993923066dddaf1040bc3ff59f825c78df74f2d75467e25e0f55f8a00fa030ed" + ], + 384, + id="bls12_pairing_check", + ), ], ) def test_worst_precompile_fixed_cost( From e456490988c055c772f194e029f3f1e0e31318f1 Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Thu, 8 May 2025 12:09:45 -0300 Subject: [PATCH 07/19] add bls_map tests Signed-off-by: Ignacio Hagopian --- tests/zkevm/test_worst_compute.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/zkevm/test_worst_compute.py b/tests/zkevm/test_worst_compute.py index 1995e7cf134..3ffba64a892 100644 --- a/tests/zkevm/test_worst_compute.py +++ b/tests/zkevm/test_worst_compute.py @@ -370,6 +370,22 @@ def test_worst_modexp( 384, id="bls12_pairing_check", ), + pytest.param( + 0x10, + [ + "0000000000000000000000000000000014406e5bfb9209256a3820879a29ac2f62d6aca82324bf3ae2aa7d3c54792043bd8c791fccdb080c1a52dc68b8b69350", + ], + 64, + id="bls12_fp_to_g1", + ), + pytest.param( + 0x11, + [ + "0000000000000000000000000000000014406e5bfb9209256a3820879a29ac2f62d6aca82324bf3ae2aa7d3c54792043bd8c791fccdb080c1a52dc68b8b69350000000000000000000000000000000000e885bb33996e12f07da69073e2c0cc880bc8eff26d2a724299eb12d54f4bcf26f4748bb020e80a7e3794a7b0e47a641", + ], + 128, + id="bls12_fp_to_g2", + ), ], ) def test_worst_precompile_fixed_cost( From 6048137c394956a48971e70c534c3fbb61ad5a1c Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Fri, 16 May 2025 12:54:06 -0300 Subject: [PATCH 08/19] rebase & format Signed-off-by: Ignacio Hagopian --- tests/zkevm/test_worst_compute.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/zkevm/test_worst_compute.py b/tests/zkevm/test_worst_compute.py index 3ffba64a892..b599bf70243 100644 --- a/tests/zkevm/test_worst_compute.py +++ b/tests/zkevm/test_worst_compute.py @@ -20,7 +20,6 @@ Environment, Transaction, ) -from ethereum_test_tools.code.generators import While from ethereum_test_tools.vm.opcode import Opcodes as Op from ethereum_test_vm import Opcode From 40ef4dc7bf5a7d94898a0cc92ae1d8cc9d56b6ca Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Fri, 16 May 2025 13:16:19 -0300 Subject: [PATCH 09/19] remove input length parameter Signed-off-by: Ignacio Hagopian --- tests/zkevm/test_worst_compute.py | 28 +++++++--------------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/tests/zkevm/test_worst_compute.py b/tests/zkevm/test_worst_compute.py index b599bf70243..a604dd1a11e 100644 --- a/tests/zkevm/test_worst_compute.py +++ b/tests/zkevm/test_worst_compute.py @@ -242,7 +242,7 @@ def test_worst_modexp( ], ) @pytest.mark.parametrize( - "precompile_address,input,length", + "precompile_address,input", [ pytest.param( 0x01, @@ -252,7 +252,6 @@ def test_worst_modexp( "38D18ACB67D25C8BB9942764B62F18E17054F66A817BD4295423ADF9ED98873E", "789D1DD423D25F0772D2748D60F7E4B81BB14D086EBA8E8E8EFB6DCFF8A4AE02", ], - 32 * 4, id="ecrecover", ), pytest.param( @@ -263,7 +262,6 @@ def test_worst_modexp( "07C2B7F58A84BD6145F00C9C2BC0BB1A187F20FF2C92963A88019E7C6A014EED", "06614E20C147E940F2D70DA3F74C9A17DF361706A4485C742BD6788478FA17D7", ], - 32 * 4, id="bn128_add", ), pytest.param( @@ -273,7 +271,6 @@ def test_worst_modexp( "1A2F3C951F6DADCC7EE9007DFF81504B0FCD6D7CF59996EFDC33D92BF7F9F8F6", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", ], - 32 * 4, id="bn128_mul", ), pytest.param( @@ -294,7 +291,6 @@ def test_worst_modexp( "090689D0585FF075EC9E99AD690C3395BC4B313370B38EF355ACDADCD122975B", "12C85EA5DB8C6DEB4AAB71808DCB408FE3D1E7690C43D37B4CE6CC0166FA7DAA", ], - 32 * 2 * 6, id="bn128_pairing", ), pytest.param( @@ -302,13 +298,10 @@ def test_worst_modexp( [ "0000FFFF", "48C9BDF267E6096A3BA7CA8485AE67BB2BF894FE72F36E3CF1361D5F3AF54FA5D182E6AD7F520E511F6C3E2B8C68059B6BBD41FBABD9831F79217E1319CDE05B", - "61626300000000000000000000000000000000000000000000000000", - "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", - "000000000300000000000000000000", - "00000000010000000000000000000000", - "00", + "6162630000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "00000000000300000000000000000000", + "01", ], - 213, id="blake2f", ), pytest.param( @@ -320,7 +313,6 @@ def test_worst_modexp( "8F59A8D2A1A625A17F3FEA0FE5EB8C896DB3764F3185481BC22F91B4AAFFCCA25F26936857BC3A7C2539EA8EC3A952B7", "873033E038326E87ED3E1276FD140253FA08E9FC25FB2D9A98527FC22A2C9612FBEAFDAD446CBC7BCDBDCD780AF2C16A", ], - 192, id="point_evaluation", ), pytest.param( @@ -329,7 +321,6 @@ def test_worst_modexp( "000000000000000000000000000000000572CBEA904D67468808C8EB50A9450C9721DB309128012543902D0AC358A62AE28F75BB8F1C7C42C39A8C5529BF0F4E00000000000000000000000000000000166A9D8CABC673A322FDA673779D8E3822BA3ECB8670E461F73BB9021D5FD76A", "4C56D9D4CD16BD1BBA86881979749D280000000000000000000000000000000009ECE308F9D1F0131765212DECA99697B112D61F9BE9A5F1F3780A51335B3FF981747A0B2CA2179B96D2C0C9024E522400000000000000000000000000000000032B80D3A6F5B09F8A84623389C5F80C", ], - 256, id="bls12_g1add", ), pytest.param( @@ -339,7 +330,6 @@ def test_worst_modexp( "000000000000000000000000000000000e12039459c60491672b6a6282355d8765ba6272387fb91a3e9604fa2a81450cf16b870bb446fc3a3e0a187fff6f89450000000000000000000000000000000018b6c1ed9f45d3cbc0b01b9d038dcecacbd702eb26469a0eb3905bd421461712f67f782b4735849644c1772c93fe3d090000000000000000000000000000000000000000000000000000000000000033", "00000000000000000000000000000000147b327c8a15b39634a426af70c062b50632a744eddd41b5a4686414ef4cd9746bb11d0a53c6c2ff21bbcf331e07ac9200000000000000000000000000000000078c2e9782fa5d9ab4e728684382717aa2b8fad61b5f5e7cf3baa0bc9465f57342bb7c6d7b232e70eebcdbf70f903a450000000000000000000000000000000000000000000000000000000000000034", ], - 3 * 160, id="bls12_g1msm", ), pytest.param( @@ -348,7 +338,6 @@ def test_worst_modexp( "000000000000000000000000000000001638533957d540a9d2370f17cc7ed5863bc0b995b8825e0ee1ea1e1e4d00dbae81f14b0bf3611b78c952aacab827a053000000000000000000000000000000000a4edef9c1ed7f729f520e47730a124fd70662a904ba1074728114d1031e1572c6c886f6b57ec72a6178288c47c33577000000000000000000000000000000000468fb440d82b0630aeb8dca2b5256789a66da69bf91009cbfe6bd221e47aa8ae88dece9764bf3bd999d95d71e4c9899000000000000000000000000000000000f6d4552fa65dd2638b361543f887136a43253d9c66c411697003f7a13c308f5422e1aa0a59c8967acdefd8b6e36ccf3", "00000000000000000000000000000000122915c824a0857e2ee414a3dccb23ae691ae54329781315a0c75df1c04d6d7a50a030fc866f09d516020ef82324afae0000000000000000000000000000000009380275bbc8e5dcea7dc4dd7e0550ff2ac480905396eda55062650f8d251c96eb480673937cc6d9d6a44aaa56ca66dc000000000000000000000000000000000b21da7955969e61010c7a1abc1a6f0136961d1e3b20b1a7326ac738fef5c721479dfd948b52fdf2455e44813ecfd8920000000000000000000000000000000008f239ba329b3967fe48d718a36cfe5f62a7e42e0bf1c1ed714150a166bfbd6bcf6b3b58b975b9edea56d53f23a0e849", ], - 2 * 256, id="bls12_g2add", ), pytest.param( @@ -358,7 +347,6 @@ def test_worst_modexp( "0000000000000000000000000000000019d5f05b4f134bb37d89a03e87c8b729e6bdc062f3ae0ddc5265b270e40a6a5691f51ff60b764ea760651caf395101840000000000000000000000000000000015532df6a12b7c160a0831ef8321b18feb6ce7997c0718b205873608085be3afeec5b5d5251a0f85f7f5b7271271e0660000000000000000000000000000000004623ac0df1e019d337dc9488c17ef9e214dc33c63f96a90fea288e836dbd85079cb3cec42ae693e9c16af3c3204d86e0000000000000000000000000000000011ba77f71923c1b6a711a48fa4085c4885290079448a4b597030cc84aa14647136513cec6d11c4453ca74e906bbca1e10000000000000000000000000000000000000000000000000000000000000033", "00000000000000000000000000000000176a7158b310c9ff1bfc21b81903de99c90440792ebe6d9637652ee34acf53b43c2f31738bbc96d71dcadbbf0e3190af000000000000000000000000000000000a592641967934a97e012f7d6412c4f6ff0f177a1b466b9b49c9deb7498decc80d0c809448aa9fa6fbbb6f537515703000000000000000000000000000000000031d84356ef619e688a10247f122e1aa0d3def3e35f94043f64c634198421487ca96af5f0160384bba92bd5494506c4d000000000000000000000000000000000db8fefe735779489c957785fa8e45d24e086ef0c2aba2e3adba888f0aeee51385a82898524c443f017ee40be635048c0000000000000000000000000000000000000000000000000000000000000034", ], - 3 * 288, id="bls12_g2msm", ), pytest.param( @@ -366,7 +354,6 @@ def test_worst_modexp( [ "000000000000000000000000000000000572cbea904d67468808c8eb50a9450c9721db309128012543902d0ac358a62ae28f75bb8f1c7c42c39a8c5529bf0f4e00000000000000000000000000000000166a9d8cabc673a322fda673779d8e3822ba3ecb8670e461f73bb9021d5fd76a4c56d9d4cd16bd1bba86881979749d2800000000000000000000000000000000122915c824a0857e2ee414a3dccb23ae691ae54329781315a0c75df1c04d6d7a50a030fc866f09d516020ef82324afae0000000000000000000000000000000009380275bbc8e5dcea7dc4dd7e0550ff2ac480905396eda55062650f8d251c96eb480673937cc6d9d6a44aaa56ca66dc000000000000000000000000000000000b21da7955969e61010c7a1abc1a6f0136961d1e3b20b1a7326ac738fef5c721479dfd948b52fdf2455e44813ecfd8920000000000000000000000000000000008f239ba329b3967fe48d718a36cfe5f62a7e42e0bf1c1ed714150a166bfbd6bcf6b3b58b975b9edea56d53f23a0e8490000000000000000000000000000000006e82f6da4520f85c5d27d8f329eccfa05944fd1096b20734c894966d12a9e2a9a9744529d7212d33883113a0cadb9090000000000000000000000000000000017d81038f7d60bee9110d9c0d6d1102fe2d998c957f28e31ec284cc04134df8e47e8f82ff3af2e60a6d9688a4563477c00000000000000000000000000000000024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb80000000000000000000000000000000013e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e000000000000000000000000000000000d1b3cc2c7027888be51d9ef691d77bcb679afda66c73f17f9ee3837a55024f78c71363275a75d75d86bab79f74782aa0000000000000000000000000000000013fa4d4a0ad8b1ce186ed5061789213d993923066dddaf1040bc3ff59f825c78df74f2d75467e25e0f55f8a00fa030ed" ], - 384, id="bls12_pairing_check", ), pytest.param( @@ -374,7 +361,6 @@ def test_worst_modexp( [ "0000000000000000000000000000000014406e5bfb9209256a3820879a29ac2f62d6aca82324bf3ae2aa7d3c54792043bd8c791fccdb080c1a52dc68b8b69350", ], - 64, id="bls12_fp_to_g1", ), pytest.param( @@ -382,7 +368,6 @@ def test_worst_modexp( [ "0000000000000000000000000000000014406e5bfb9209256a3820879a29ac2f62d6aca82324bf3ae2aa7d3c54792043bd8c791fccdb080c1a52dc68b8b69350000000000000000000000000000000000e885bb33996e12f07da69073e2c0cc880bc8eff26d2a724299eb12d54f4bcf26f4748bb020e80a7e3794a7b0e47a641", ], - 128, id="bls12_fp_to_g2", ), ], @@ -394,7 +379,6 @@ def test_worst_precompile_fixed_cost( gas_limit: int, precompile_address: Address, input: list[str], - length: int, ): """Test running a block filled with a precompile with fixed cost.""" env = Environment(gas_limit=gas_limit) @@ -411,7 +395,9 @@ def test_worst_precompile_fixed_cost( value_to_store = int.from_bytes(chunk, "big") calldata += Op.MSTORE(i, value_to_store) - attack_block = Op.POP(Op.STATICCALL(Op.GAS, precompile_address, 0, length, 0, 0)) + attack_block = Op.POP( + Op.STATICCALL(Op.GAS, precompile_address, 0, len(concatenated_bytes), 0, 0) + ) code = code_loop_precompile_call(calldata, attack_block) code_address = pre.deploy_contract(code=bytes(code)) From 98cdccd480bf28fc4bb404894bb039b1ddfdebb1 Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Fri, 16 May 2025 14:02:05 -0300 Subject: [PATCH 10/19] use bls12381 precompile addresses from spec and simplify parameter Signed-off-by: Ignacio Hagopian --- tests/zkevm/test_worst_compute.py | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/tests/zkevm/test_worst_compute.py b/tests/zkevm/test_worst_compute.py index a604dd1a11e..f7e2e7edbe8 100644 --- a/tests/zkevm/test_worst_compute.py +++ b/tests/zkevm/test_worst_compute.py @@ -21,7 +21,7 @@ Transaction, ) from ethereum_test_tools.vm.opcode import Opcodes as Op -from ethereum_test_vm import Opcode +from tests.prague.eip2537_bls_12_381_precompiles.spec import Spec as BLS12381Spec REFERENCE_SPEC_GIT_PATH = "TODO" REFERENCE_SPEC_VERSION = "TODO" @@ -235,12 +235,6 @@ def test_worst_modexp( @pytest.mark.valid_from("Cancun") -@pytest.mark.parametrize( - "gas_limit", - [ - Environment().gas_limit, - ], -) @pytest.mark.parametrize( "precompile_address,input", [ @@ -316,7 +310,7 @@ def test_worst_modexp( id="point_evaluation", ), pytest.param( - 0x0B, + BLS12381Spec.G1ADD, [ "000000000000000000000000000000000572CBEA904D67468808C8EB50A9450C9721DB309128012543902D0AC358A62AE28F75BB8F1C7C42C39A8C5529BF0F4E00000000000000000000000000000000166A9D8CABC673A322FDA673779D8E3822BA3ECB8670E461F73BB9021D5FD76A", "4C56D9D4CD16BD1BBA86881979749D280000000000000000000000000000000009ECE308F9D1F0131765212DECA99697B112D61F9BE9A5F1F3780A51335B3FF981747A0B2CA2179B96D2C0C9024E522400000000000000000000000000000000032B80D3A6F5B09F8A84623389C5F80C", @@ -324,7 +318,7 @@ def test_worst_modexp( id="bls12_g1add", ), pytest.param( - 0x0C, + BLS12381Spec.G1MSM, [ "0000000000000000000000000000000017f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb0000000000000000000000000000000008b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e10000000000000000000000000000000000000000000000000000000000000032", "000000000000000000000000000000000e12039459c60491672b6a6282355d8765ba6272387fb91a3e9604fa2a81450cf16b870bb446fc3a3e0a187fff6f89450000000000000000000000000000000018b6c1ed9f45d3cbc0b01b9d038dcecacbd702eb26469a0eb3905bd421461712f67f782b4735849644c1772c93fe3d090000000000000000000000000000000000000000000000000000000000000033", @@ -333,7 +327,7 @@ def test_worst_modexp( id="bls12_g1msm", ), pytest.param( - 0x0D, + BLS12381Spec.G2ADD, [ "000000000000000000000000000000001638533957d540a9d2370f17cc7ed5863bc0b995b8825e0ee1ea1e1e4d00dbae81f14b0bf3611b78c952aacab827a053000000000000000000000000000000000a4edef9c1ed7f729f520e47730a124fd70662a904ba1074728114d1031e1572c6c886f6b57ec72a6178288c47c33577000000000000000000000000000000000468fb440d82b0630aeb8dca2b5256789a66da69bf91009cbfe6bd221e47aa8ae88dece9764bf3bd999d95d71e4c9899000000000000000000000000000000000f6d4552fa65dd2638b361543f887136a43253d9c66c411697003f7a13c308f5422e1aa0a59c8967acdefd8b6e36ccf3", "00000000000000000000000000000000122915c824a0857e2ee414a3dccb23ae691ae54329781315a0c75df1c04d6d7a50a030fc866f09d516020ef82324afae0000000000000000000000000000000009380275bbc8e5dcea7dc4dd7e0550ff2ac480905396eda55062650f8d251c96eb480673937cc6d9d6a44aaa56ca66dc000000000000000000000000000000000b21da7955969e61010c7a1abc1a6f0136961d1e3b20b1a7326ac738fef5c721479dfd948b52fdf2455e44813ecfd8920000000000000000000000000000000008f239ba329b3967fe48d718a36cfe5f62a7e42e0bf1c1ed714150a166bfbd6bcf6b3b58b975b9edea56d53f23a0e849", @@ -341,7 +335,7 @@ def test_worst_modexp( id="bls12_g2add", ), pytest.param( - 0x0E, + BLS12381Spec.G2MSM, [ "00000000000000000000000000000000024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb80000000000000000000000000000000013e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e000000000000000000000000000000000ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801000000000000000000000000000000000606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79be0000000000000000000000000000000000000000000000000000000000000032", "0000000000000000000000000000000019d5f05b4f134bb37d89a03e87c8b729e6bdc062f3ae0ddc5265b270e40a6a5691f51ff60b764ea760651caf395101840000000000000000000000000000000015532df6a12b7c160a0831ef8321b18feb6ce7997c0718b205873608085be3afeec5b5d5251a0f85f7f5b7271271e0660000000000000000000000000000000004623ac0df1e019d337dc9488c17ef9e214dc33c63f96a90fea288e836dbd85079cb3cec42ae693e9c16af3c3204d86e0000000000000000000000000000000011ba77f71923c1b6a711a48fa4085c4885290079448a4b597030cc84aa14647136513cec6d11c4453ca74e906bbca1e10000000000000000000000000000000000000000000000000000000000000033", @@ -350,21 +344,21 @@ def test_worst_modexp( id="bls12_g2msm", ), pytest.param( - 0x0F, + BLS12381Spec.PAIRING, [ "000000000000000000000000000000000572cbea904d67468808c8eb50a9450c9721db309128012543902d0ac358a62ae28f75bb8f1c7c42c39a8c5529bf0f4e00000000000000000000000000000000166a9d8cabc673a322fda673779d8e3822ba3ecb8670e461f73bb9021d5fd76a4c56d9d4cd16bd1bba86881979749d2800000000000000000000000000000000122915c824a0857e2ee414a3dccb23ae691ae54329781315a0c75df1c04d6d7a50a030fc866f09d516020ef82324afae0000000000000000000000000000000009380275bbc8e5dcea7dc4dd7e0550ff2ac480905396eda55062650f8d251c96eb480673937cc6d9d6a44aaa56ca66dc000000000000000000000000000000000b21da7955969e61010c7a1abc1a6f0136961d1e3b20b1a7326ac738fef5c721479dfd948b52fdf2455e44813ecfd8920000000000000000000000000000000008f239ba329b3967fe48d718a36cfe5f62a7e42e0bf1c1ed714150a166bfbd6bcf6b3b58b975b9edea56d53f23a0e8490000000000000000000000000000000006e82f6da4520f85c5d27d8f329eccfa05944fd1096b20734c894966d12a9e2a9a9744529d7212d33883113a0cadb9090000000000000000000000000000000017d81038f7d60bee9110d9c0d6d1102fe2d998c957f28e31ec284cc04134df8e47e8f82ff3af2e60a6d9688a4563477c00000000000000000000000000000000024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb80000000000000000000000000000000013e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e000000000000000000000000000000000d1b3cc2c7027888be51d9ef691d77bcb679afda66c73f17f9ee3837a55024f78c71363275a75d75d86bab79f74782aa0000000000000000000000000000000013fa4d4a0ad8b1ce186ed5061789213d993923066dddaf1040bc3ff59f825c78df74f2d75467e25e0f55f8a00fa030ed" ], id="bls12_pairing_check", ), pytest.param( - 0x10, + BLS12381Spec.MAP_FP_TO_G1, [ "0000000000000000000000000000000014406e5bfb9209256a3820879a29ac2f62d6aca82324bf3ae2aa7d3c54792043bd8c791fccdb080c1a52dc68b8b69350", ], id="bls12_fp_to_g1", ), pytest.param( - 0x11, + BLS12381Spec.MAP_FP2_TO_G2, [ "0000000000000000000000000000000014406e5bfb9209256a3820879a29ac2f62d6aca82324bf3ae2aa7d3c54792043bd8c791fccdb080c1a52dc68b8b69350000000000000000000000000000000000e885bb33996e12f07da69073e2c0cc880bc8eff26d2a724299eb12d54f4bcf26f4748bb020e80a7e3794a7b0e47a641", ], @@ -376,12 +370,11 @@ def test_worst_precompile_fixed_cost( blockchain_test: BlockchainTestFiller, pre: Alloc, fork: Fork, - gas_limit: int, precompile_address: Address, input: list[str], ): """Test running a block filled with a precompile with fixed cost.""" - env = Environment(gas_limit=gas_limit) + env = Environment() concatenated_hex_string = "".join(input) concatenated_bytes = bytes.fromhex(concatenated_hex_string) From 9e4495153883306d1c7ea597f5fdaec30b730a75 Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Fri, 16 May 2025 14:03:13 -0300 Subject: [PATCH 11/19] make the linter happy Signed-off-by: Ignacio Hagopian --- tests/zkevm/test_worst_compute.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/zkevm/test_worst_compute.py b/tests/zkevm/test_worst_compute.py index f7e2e7edbe8..fc773cd5710 100644 --- a/tests/zkevm/test_worst_compute.py +++ b/tests/zkevm/test_worst_compute.py @@ -236,7 +236,7 @@ def test_worst_modexp( @pytest.mark.valid_from("Cancun") @pytest.mark.parametrize( - "precompile_address,input", + "precompile_address,parameters", [ pytest.param( 0x01, @@ -371,12 +371,12 @@ def test_worst_precompile_fixed_cost( pre: Alloc, fork: Fork, precompile_address: Address, - input: list[str], + parameters: list[str], ): """Test running a block filled with a precompile with fixed cost.""" env = Environment() - concatenated_hex_string = "".join(input) + concatenated_hex_string = "".join(parameters) concatenated_bytes = bytes.fromhex(concatenated_hex_string) padding_length = (32 - (len(concatenated_bytes) % 32)) % 32 From ad4a2028b9b0a1b336019882bad3878c57c90771 Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Fri, 16 May 2025 15:09:45 -0300 Subject: [PATCH 12/19] use symbolic parameters for bls12381 precompiles Signed-off-by: Ignacio Hagopian --- tests/zkevm/test_worst_compute.py | 56 +++++++++++++++++-------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/tests/zkevm/test_worst_compute.py b/tests/zkevm/test_worst_compute.py index fc773cd5710..270d2ec09c2 100644 --- a/tests/zkevm/test_worst_compute.py +++ b/tests/zkevm/test_worst_compute.py @@ -6,7 +6,7 @@ """ import math -import random +from typing import SupportsBytes import pytest @@ -21,7 +21,8 @@ Transaction, ) from ethereum_test_tools.vm.opcode import Opcodes as Op -from tests.prague.eip2537_bls_12_381_precompiles.spec import Spec as BLS12381Spec +from tests.prague.eip2537_bls_12_381_precompiles import spec as bls12381_spec +from tests.prague.eip2537_bls_12_381_precompiles.spec import BytesConcatenation REFERENCE_SPEC_GIT_PATH = "TODO" REFERENCE_SPEC_VERSION = "TODO" @@ -310,57 +311,58 @@ def test_worst_modexp( id="point_evaluation", ), pytest.param( - BLS12381Spec.G1ADD, + bls12381_spec.Spec.G1ADD, [ - "000000000000000000000000000000000572CBEA904D67468808C8EB50A9450C9721DB309128012543902D0AC358A62AE28F75BB8F1C7C42C39A8C5529BF0F4E00000000000000000000000000000000166A9D8CABC673A322FDA673779D8E3822BA3ECB8670E461F73BB9021D5FD76A", - "4C56D9D4CD16BD1BBA86881979749D280000000000000000000000000000000009ECE308F9D1F0131765212DECA99697B112D61F9BE9A5F1F3780A51335B3FF981747A0B2CA2179B96D2C0C9024E522400000000000000000000000000000000032B80D3A6F5B09F8A84623389C5F80C", + bls12381_spec.Spec.G1, + bls12381_spec.Spec.P1, ], id="bls12_g1add", ), pytest.param( - BLS12381Spec.G1MSM, + bls12381_spec.Spec.G1MSM, [ - "0000000000000000000000000000000017f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb0000000000000000000000000000000008b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e10000000000000000000000000000000000000000000000000000000000000032", - "000000000000000000000000000000000e12039459c60491672b6a6282355d8765ba6272387fb91a3e9604fa2a81450cf16b870bb446fc3a3e0a187fff6f89450000000000000000000000000000000018b6c1ed9f45d3cbc0b01b9d038dcecacbd702eb26469a0eb3905bd421461712f67f782b4735849644c1772c93fe3d090000000000000000000000000000000000000000000000000000000000000033", - "00000000000000000000000000000000147b327c8a15b39634a426af70c062b50632a744eddd41b5a4686414ef4cd9746bb11d0a53c6c2ff21bbcf331e07ac9200000000000000000000000000000000078c2e9782fa5d9ab4e728684382717aa2b8fad61b5f5e7cf3baa0bc9465f57342bb7c6d7b232e70eebcdbf70f903a450000000000000000000000000000000000000000000000000000000000000034", + (bls12381_spec.Spec.P1 + bls12381_spec.Scalar(bls12381_spec.Spec.Q)) + * (len(bls12381_spec.Spec.G1MSM_DISCOUNT_TABLE) - 1), ], id="bls12_g1msm", ), pytest.param( - BLS12381Spec.G2ADD, + bls12381_spec.Spec.G2ADD, [ - "000000000000000000000000000000001638533957d540a9d2370f17cc7ed5863bc0b995b8825e0ee1ea1e1e4d00dbae81f14b0bf3611b78c952aacab827a053000000000000000000000000000000000a4edef9c1ed7f729f520e47730a124fd70662a904ba1074728114d1031e1572c6c886f6b57ec72a6178288c47c33577000000000000000000000000000000000468fb440d82b0630aeb8dca2b5256789a66da69bf91009cbfe6bd221e47aa8ae88dece9764bf3bd999d95d71e4c9899000000000000000000000000000000000f6d4552fa65dd2638b361543f887136a43253d9c66c411697003f7a13c308f5422e1aa0a59c8967acdefd8b6e36ccf3", - "00000000000000000000000000000000122915c824a0857e2ee414a3dccb23ae691ae54329781315a0c75df1c04d6d7a50a030fc866f09d516020ef82324afae0000000000000000000000000000000009380275bbc8e5dcea7dc4dd7e0550ff2ac480905396eda55062650f8d251c96eb480673937cc6d9d6a44aaa56ca66dc000000000000000000000000000000000b21da7955969e61010c7a1abc1a6f0136961d1e3b20b1a7326ac738fef5c721479dfd948b52fdf2455e44813ecfd8920000000000000000000000000000000008f239ba329b3967fe48d718a36cfe5f62a7e42e0bf1c1ed714150a166bfbd6bcf6b3b58b975b9edea56d53f23a0e849", + bls12381_spec.Spec.G2, + bls12381_spec.Spec.P2, ], id="bls12_g2add", ), pytest.param( - BLS12381Spec.G2MSM, + bls12381_spec.Spec.G2MSM, [ - "00000000000000000000000000000000024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb80000000000000000000000000000000013e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e000000000000000000000000000000000ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801000000000000000000000000000000000606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79be0000000000000000000000000000000000000000000000000000000000000032", - "0000000000000000000000000000000019d5f05b4f134bb37d89a03e87c8b729e6bdc062f3ae0ddc5265b270e40a6a5691f51ff60b764ea760651caf395101840000000000000000000000000000000015532df6a12b7c160a0831ef8321b18feb6ce7997c0718b205873608085be3afeec5b5d5251a0f85f7f5b7271271e0660000000000000000000000000000000004623ac0df1e019d337dc9488c17ef9e214dc33c63f96a90fea288e836dbd85079cb3cec42ae693e9c16af3c3204d86e0000000000000000000000000000000011ba77f71923c1b6a711a48fa4085c4885290079448a4b597030cc84aa14647136513cec6d11c4453ca74e906bbca1e10000000000000000000000000000000000000000000000000000000000000033", - "00000000000000000000000000000000176a7158b310c9ff1bfc21b81903de99c90440792ebe6d9637652ee34acf53b43c2f31738bbc96d71dcadbbf0e3190af000000000000000000000000000000000a592641967934a97e012f7d6412c4f6ff0f177a1b466b9b49c9deb7498decc80d0c809448aa9fa6fbbb6f537515703000000000000000000000000000000000031d84356ef619e688a10247f122e1aa0d3def3e35f94043f64c634198421487ca96af5f0160384bba92bd5494506c4d000000000000000000000000000000000db8fefe735779489c957785fa8e45d24e086ef0c2aba2e3adba888f0aeee51385a82898524c443f017ee40be635048c0000000000000000000000000000000000000000000000000000000000000034", + # TODO: the //2 is only required due to a limitation of the max contract size limit. + # In a further iteration we can insert the inputs as calldata or storage and avoid + # having to do PUSHes which has this limtiation. This also applies to G1MSM. + (bls12381_spec.Spec.P2 + bls12381_spec.Scalar(bls12381_spec.Spec.Q)) + * (len(bls12381_spec.Spec.G2MSM_DISCOUNT_TABLE) // 2), ], id="bls12_g2msm", ), pytest.param( - BLS12381Spec.PAIRING, + bls12381_spec.Spec.PAIRING, [ - "000000000000000000000000000000000572cbea904d67468808c8eb50a9450c9721db309128012543902d0ac358a62ae28f75bb8f1c7c42c39a8c5529bf0f4e00000000000000000000000000000000166a9d8cabc673a322fda673779d8e3822ba3ecb8670e461f73bb9021d5fd76a4c56d9d4cd16bd1bba86881979749d2800000000000000000000000000000000122915c824a0857e2ee414a3dccb23ae691ae54329781315a0c75df1c04d6d7a50a030fc866f09d516020ef82324afae0000000000000000000000000000000009380275bbc8e5dcea7dc4dd7e0550ff2ac480905396eda55062650f8d251c96eb480673937cc6d9d6a44aaa56ca66dc000000000000000000000000000000000b21da7955969e61010c7a1abc1a6f0136961d1e3b20b1a7326ac738fef5c721479dfd948b52fdf2455e44813ecfd8920000000000000000000000000000000008f239ba329b3967fe48d718a36cfe5f62a7e42e0bf1c1ed714150a166bfbd6bcf6b3b58b975b9edea56d53f23a0e8490000000000000000000000000000000006e82f6da4520f85c5d27d8f329eccfa05944fd1096b20734c894966d12a9e2a9a9744529d7212d33883113a0cadb9090000000000000000000000000000000017d81038f7d60bee9110d9c0d6d1102fe2d998c957f28e31ec284cc04134df8e47e8f82ff3af2e60a6d9688a4563477c00000000000000000000000000000000024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb80000000000000000000000000000000013e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e000000000000000000000000000000000d1b3cc2c7027888be51d9ef691d77bcb679afda66c73f17f9ee3837a55024f78c71363275a75d75d86bab79f74782aa0000000000000000000000000000000013fa4d4a0ad8b1ce186ed5061789213d993923066dddaf1040bc3ff59f825c78df74f2d75467e25e0f55f8a00fa030ed" + bls12381_spec.Spec.G1 + bls12381_spec.Spec.INF_G2, ], id="bls12_pairing_check", ), pytest.param( - BLS12381Spec.MAP_FP_TO_G1, + bls12381_spec.Spec.MAP_FP_TO_G1, [ - "0000000000000000000000000000000014406e5bfb9209256a3820879a29ac2f62d6aca82324bf3ae2aa7d3c54792043bd8c791fccdb080c1a52dc68b8b69350", + bls12381_spec.FP(bls12381_spec.Spec.P - 1), ], id="bls12_fp_to_g1", ), pytest.param( - BLS12381Spec.MAP_FP2_TO_G2, + bls12381_spec.Spec.MAP_FP2_TO_G2, [ - "0000000000000000000000000000000014406e5bfb9209256a3820879a29ac2f62d6aca82324bf3ae2aa7d3c54792043bd8c791fccdb080c1a52dc68b8b69350000000000000000000000000000000000e885bb33996e12f07da69073e2c0cc880bc8eff26d2a724299eb12d54f4bcf26f4748bb020e80a7e3794a7b0e47a641", + bls12381_spec.FP2((bls12381_spec.Spec.P - 1, bls12381_spec.Spec.P - 1)), ], id="bls12_fp_to_g2", ), @@ -371,13 +373,17 @@ def test_worst_precompile_fixed_cost( pre: Alloc, fork: Fork, precompile_address: Address, - parameters: list[str], + parameters: list[str] | list[BytesConcatenation], ): """Test running a block filled with a precompile with fixed cost.""" env = Environment() - concatenated_hex_string = "".join(parameters) - concatenated_bytes = bytes.fromhex(concatenated_hex_string) + concatenated_bytes = bytes() + if isinstance(parameters[0], str): + concatenated_hex_string = "".join(parameters) + concatenated_bytes = bytes.fromhex(concatenated_hex_string) + else: + concatenated_bytes = b"".join(bytes(p) for p in parameters) padding_length = (32 - (len(concatenated_bytes) % 32)) % 32 input_bytes = concatenated_bytes + b"\x00" * padding_length From b4c1b3e2149ecc07012c917c9727a8c70c27fa96 Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Fri, 16 May 2025 16:04:48 -0300 Subject: [PATCH 13/19] lints Signed-off-by: Ignacio Hagopian --- tests/zkevm/test_worst_compute.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/tests/zkevm/test_worst_compute.py b/tests/zkevm/test_worst_compute.py index 270d2ec09c2..2231b8ae51c 100644 --- a/tests/zkevm/test_worst_compute.py +++ b/tests/zkevm/test_worst_compute.py @@ -6,7 +6,7 @@ """ import math -from typing import SupportsBytes +from typing import cast import pytest @@ -337,7 +337,7 @@ def test_worst_modexp( pytest.param( bls12381_spec.Spec.G2MSM, [ - # TODO: the //2 is only required due to a limitation of the max contract size limit. + # TODO: the //2 is required due to a limitation of the max contract size limit. # In a further iteration we can insert the inputs as calldata or storage and avoid # having to do PUSHes which has this limtiation. This also applies to G1MSM. (bls12381_spec.Spec.P2 + bls12381_spec.Scalar(bls12381_spec.Spec.Q)) @@ -348,7 +348,8 @@ def test_worst_modexp( pytest.param( bls12381_spec.Spec.PAIRING, [ - bls12381_spec.Spec.G1 + bls12381_spec.Spec.INF_G2, + bls12381_spec.Spec.G1, + bls12381_spec.Spec.G2, ], id="bls12_pairing_check", ), @@ -373,17 +374,24 @@ def test_worst_precompile_fixed_cost( pre: Alloc, fork: Fork, precompile_address: Address, - parameters: list[str] | list[BytesConcatenation], + parameters: list[str] | list[BytesConcatenation] | list[bytes], ): """Test running a block filled with a precompile with fixed cost.""" env = Environment() - concatenated_bytes = bytes() - if isinstance(parameters[0], str): - concatenated_hex_string = "".join(parameters) + concatenated_bytes: bytes + if all(isinstance(p, str) for p in parameters): + parameters_str = cast(list[str], parameters) + concatenated_hex_string = "".join(parameters_str) concatenated_bytes = bytes.fromhex(concatenated_hex_string) + elif all(isinstance(p, (bytes, BytesConcatenation)) for p in parameters): + parameters_bytes_list = [bytes(p) for p in parameters] + concatenated_bytes = b"".join(parameters_bytes_list) else: - concatenated_bytes = b"".join(bytes(p) for p in parameters) + raise TypeError( + "parameters must be a list of strings (hex) " + "or a list of byte-like objects (bytes or BytesConcatenation)." + ) padding_length = (32 - (len(concatenated_bytes) % 32)) % 32 input_bytes = concatenated_bytes + b"\x00" * padding_length From bc60f7d72679ec86e9708db1bb6d816d5deeb27d Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Fri, 16 May 2025 16:12:04 -0300 Subject: [PATCH 14/19] linter Signed-off-by: Ignacio Hagopian --- tests/zkevm/test_worst_compute.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/zkevm/test_worst_compute.py b/tests/zkevm/test_worst_compute.py index 2231b8ae51c..296b49dc117 100644 --- a/tests/zkevm/test_worst_compute.py +++ b/tests/zkevm/test_worst_compute.py @@ -385,7 +385,9 @@ def test_worst_precompile_fixed_cost( concatenated_hex_string = "".join(parameters_str) concatenated_bytes = bytes.fromhex(concatenated_hex_string) elif all(isinstance(p, (bytes, BytesConcatenation)) for p in parameters): - parameters_bytes_list = [bytes(p) for p in parameters] + parameters_bytes_list = [ + bytes(p) for p in cast(list[BytesConcatenation | bytes], parameters) + ] concatenated_bytes = b"".join(parameters_bytes_list) else: raise TypeError( From c9b85d1ce43ba38f38eacbdbe1c6d67f0ee9220c Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Fri, 16 May 2025 16:49:50 -0300 Subject: [PATCH 15/19] add slow marker Signed-off-by: Ignacio Hagopian --- tests/zkevm/test_worst_compute.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/zkevm/test_worst_compute.py b/tests/zkevm/test_worst_compute.py index 296b49dc117..754db507702 100644 --- a/tests/zkevm/test_worst_compute.py +++ b/tests/zkevm/test_worst_compute.py @@ -115,6 +115,7 @@ def test_worst_keccak( pytest.param(0x04, 15, 3, 1, id="IDENTITY"), ], ) +@pytest.mark.slow() def test_worst_precompile_only_data_input( blockchain_test: BlockchainTestFiller, pre: Alloc, @@ -369,6 +370,7 @@ def test_worst_modexp( ), ], ) +@pytest.mark.slow() def test_worst_precompile_fixed_cost( blockchain_test: BlockchainTestFiller, pre: Alloc, From d9bd7a5f137994426c7610c2b48367961f51121f Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Tue, 20 May 2025 13:37:51 -0300 Subject: [PATCH 16/19] rebase fixes Signed-off-by: Ignacio Hagopian --- tests/zkevm/test_worst_compute.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/zkevm/test_worst_compute.py b/tests/zkevm/test_worst_compute.py index 754db507702..dbe2548ffe7 100644 --- a/tests/zkevm/test_worst_compute.py +++ b/tests/zkevm/test_worst_compute.py @@ -6,6 +6,7 @@ """ import math +import random from typing import cast import pytest @@ -20,7 +21,9 @@ Environment, Transaction, ) +from ethereum_test_tools.code.generators import While from ethereum_test_tools.vm.opcode import Opcodes as Op +from ethereum_test_vm.opcode import Opcode from tests.prague.eip2537_bls_12_381_precompiles import spec as bls12381_spec from tests.prague.eip2537_bls_12_381_precompiles.spec import BytesConcatenation From 80ef8d3780fe99ac68c16293332d8e2225ffe7f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Wed, 21 May 2025 13:18:48 +0200 Subject: [PATCH 17/19] reuse some constants and precompile inputs --- tests/zkevm/test_worst_compute.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/tests/zkevm/test_worst_compute.py b/tests/zkevm/test_worst_compute.py index dbe2548ffe7..05d6b3fc41a 100644 --- a/tests/zkevm/test_worst_compute.py +++ b/tests/zkevm/test_worst_compute.py @@ -24,6 +24,9 @@ from ethereum_test_tools.code.generators import While from ethereum_test_tools.vm.opcode import Opcodes as Op from ethereum_test_vm.opcode import Opcode +from tests.cancun.eip4844_blobs.spec import Spec as BlobsSpec +from tests.istanbul.eip152_blake2.common import Blake2bInput +from tests.istanbul.eip152_blake2.spec import Spec as Blake2bSpec from tests.prague.eip2537_bls_12_381_precompiles import spec as bls12381_spec from tests.prague.eip2537_bls_12_381_precompiles.spec import BytesConcatenation @@ -293,18 +296,14 @@ def test_worst_modexp( id="bn128_pairing", ), pytest.param( - 0x09, + Blake2bSpec.BLAKE2_PRECOMPILE_ADDRESS, [ - "0000FFFF", - "48C9BDF267E6096A3BA7CA8485AE67BB2BF894FE72F36E3CF1361D5F3AF54FA5D182E6AD7F520E511F6C3E2B8C68059B6BBD41FBABD9831F79217E1319CDE05B", - "6162630000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", - "00000000000300000000000000000000", - "01", + Blake2bInput(rounds=0xFFFF, f=True), ], id="blake2f", ), pytest.param( - 0x0A, + BlobsSpec.POINT_EVALUATION_PRECOMPILE_ADDRESS, [ "01E798154708FE7789429634053CBF9F99B619F9F084048927333FCE637F549B", "564C0A11A0F704F4FC3E8ACFE0F8245F0AD1347B378FBF96E206DA11A5D36306", @@ -377,7 +376,6 @@ def test_worst_modexp( def test_worst_precompile_fixed_cost( blockchain_test: BlockchainTestFiller, pre: Alloc, - fork: Fork, precompile_address: Address, parameters: list[str] | list[BytesConcatenation] | list[bytes], ): From dc8f652306e29a02585ee4fde22fb8ebeff3b0b4 Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Wed, 21 May 2025 08:58:00 -0300 Subject: [PATCH 18/19] comments and fixes Signed-off-by: Ignacio Hagopian --- tests/zkevm/test_worst_compute.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/zkevm/test_worst_compute.py b/tests/zkevm/test_worst_compute.py index 05d6b3fc41a..93d6a584a05 100644 --- a/tests/zkevm/test_worst_compute.py +++ b/tests/zkevm/test_worst_compute.py @@ -249,6 +249,9 @@ def test_worst_modexp( pytest.param( 0x01, [ + # TODO: below inputs are covering a regular signature. Worth exploring + # if there are other inputs that can be used to make the precompile + # take longer. "38D18ACB67D25C8BB9942764B62F18E17054F66A817BD4295423ADF9ED98873E", "000000000000000000000000000000000000000000000000000000000000001B", "38D18ACB67D25C8BB9942764B62F18E17054F66A817BD4295423ADF9ED98873E", @@ -278,6 +281,10 @@ def test_worst_modexp( pytest.param( 0x08, [ + # TODO: the following are only two inputs, but this can be extended + # to more inputs to amortize costs as much as possible. Additionally, + # there might be worse pairings that can be used. + # # First pairing "1C76476F4DEF4BB94541D57EBBA1193381FFA7AA76ADA664DD31C16024C43F59", "3034DD2920F673E204FEE2811C678745FC819B55D3E9D294E45C9B03A76AEF41", @@ -298,7 +305,7 @@ def test_worst_modexp( pytest.param( Blake2bSpec.BLAKE2_PRECOMPILE_ADDRESS, [ - Blake2bInput(rounds=0xFFFF, f=True), + Blake2bInput(rounds=0xFFFF, f=True).create_blake2b_tx_data(), ], id="blake2f", ), From f963b141620bc4056acde531a6f918970cc220ee Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Wed, 21 May 2025 09:24:31 -0300 Subject: [PATCH 19/19] clarify comment Signed-off-by: Ignacio Hagopian --- tests/zkevm/test_worst_compute.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/zkevm/test_worst_compute.py b/tests/zkevm/test_worst_compute.py index 93d6a584a05..ebe7bfc9265 100644 --- a/tests/zkevm/test_worst_compute.py +++ b/tests/zkevm/test_worst_compute.py @@ -249,9 +249,8 @@ def test_worst_modexp( pytest.param( 0x01, [ - # TODO: below inputs are covering a regular signature. Worth exploring - # if there are other inputs that can be used to make the precompile - # take longer. + # The inputs below are a valid signature, thus ECRECOVER call won't + # be short-circuited by validations and do actual work. "38D18ACB67D25C8BB9942764B62F18E17054F66A817BD4295423ADF9ED98873E", "000000000000000000000000000000000000000000000000000000000000001B", "38D18ACB67D25C8BB9942764B62F18E17054F66A817BD4295423ADF9ED98873E",