@@ -114,33 +114,25 @@ def test_worst_keccak(
114
114
36_000_000 ,
115
115
],
116
116
)
117
- @pytest .mark .parametrize (
118
- "base_mod_length, exp_length" ,
119
- [
120
- (32 , 32 ),
121
- ],
122
- )
123
117
def test_worst_modexp (
124
118
blockchain_test : BlockchainTestFiller ,
125
119
pre : Alloc ,
126
120
fork : Fork ,
127
121
gas_limit : int ,
128
- base_mod_length : int ,
129
- exp_length : int ,
122
+
130
123
):
131
124
"""Test running a block with as many MODEXP calls as possible."""
132
125
env = Environment (gas_limit = gas_limit )
133
126
127
+ base_mod_length = 32
128
+ exp_length = 32
129
+
134
130
base = 2 ** (8 * base_mod_length ) - 1
135
- mod = 2 ** (8 * base_mod_length ) - 1
131
+ mod = 2 ** (8 * base_mod_length ) - 2 # Prevnts base == mod
136
132
exp = 2 ** (8 * exp_length ) - 1
137
133
138
- # EIP-7883 (TODO: generalize)
139
- mul_complexity = math .ceil (base_mod_length / 8 ) ** 2
140
- iter_complexity = exp .bit_length () - 1
141
- gas_cost = math .floor ((mul_complexity * iter_complexity ) / 3 )
142
-
143
- mem_prep = (
134
+ # MODEXP calldata
135
+ calldata = (
144
136
Op .MSTORE (0 * 32 , 32 )
145
137
+ Op .MSTORE (1 * 32 , 32 )
146
138
+ Op .MSTORE (2 * 32 , 32 )
@@ -149,14 +141,19 @@ def test_worst_modexp(
149
141
+ Op .MSTORE (5 * 32 , mod )
150
142
)
151
143
144
+ # EIP-2565
145
+ mul_complexity = math .ceil (base_mod_length / 8 ) ** 2
146
+ iter_complexity = exp .bit_length () - 1
147
+ gas_cost = math .floor ((mul_complexity * iter_complexity ) / 3 )
152
148
attack_block = Op .STATICCALL (gas_cost , 0x5 , 0 , 32 * 6 , 0 , 0 ) + Op .POP
153
149
150
+ # The attack contract is: JUMPDEST + [attack_block]* + PUSH0 + JUMP
154
151
jumpdest = Op .JUMPDEST
155
- jump_back = Op .JUMP (len (mem_prep ))
156
- max_iters_loop = (MAX_CODE_SIZE - len (mem_prep ) - len (jumpdest ) - len (jump_back )) // len (
152
+ jump_back = Op .JUMP (len (calldata ))
153
+ max_iters_loop = (MAX_CODE_SIZE - len (calldata ) - len (jumpdest ) - len (jump_back )) // len (
157
154
attack_block
158
155
)
159
- code = mem_prep + jumpdest + sum ([attack_block ] * max_iters_loop ) + jump_back
156
+ code = calldata + jumpdest + sum ([attack_block ] * max_iters_loop ) + jump_back
160
157
if len (code ) > MAX_CODE_SIZE :
161
158
# Must never happen, but keep it as a sanity check.
162
159
raise ValueError (f"Code size { len (code )} exceeds maximum code size { MAX_CODE_SIZE } " )
0 commit comments