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