73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164 | @pytest.mark.repricing
@pytest.mark.parametrize("size", [32, 256, 1024])
def test_ripemd160_uncachable(
benchmark_test: BenchmarkTestFiller,
pre: Alloc,
fork: Fork,
gas_benchmark_value: int,
tx_gas_limit: int,
size: int,
) -> None:
"""Benchmark RIPEMD-160 with unique input per call."""
gsc = fork.gas_costs()
intrinsic_gas_calculator = fork.transaction_intrinsic_cost_calculator()
precompile_cost = (
# static cost
gsc.PRECOMPILE_RIPEMD160_BASE
# dynamic cost
+ math.ceil(size / 32) * gsc.PRECOMPILE_RIPEMD160_PER_WORD
)
attack_block = Op.POP(
Op.STATICCALL(
gas=Op.GAS,
address=0x03,
args_size=size,
ret_size=0x20,
# gas accounting
address_warm=True,
inner_call_cost=precompile_cost,
),
)
setup = Op.CALLDATACOPY(
0,
0,
Op.CALLDATASIZE,
# gas accounting
data_size=size,
old_memory_size=0,
new_memory_size=size,
)
setup_cost = setup.gas_cost(fork)
loop = WhileGas(
body=attack_block,
fork=fork,
extra_gas=precompile_cost,
)
code = setup + loop
attack_contract_address = pre.deploy_contract(code=code)
iteration_cost = loop.gas_cost(fork)
txs: list[Transaction] = []
remaining_gas = gas_benchmark_value
rng = random.Random(42)
expected_opcode_count = 0
while remaining_gas > 0:
per_tx_gas = min(tx_gas_limit, remaining_gas)
calldata = Bytes(rng.randbytes(size))
intrinsic = intrinsic_gas_calculator(
calldata=calldata,
return_cost_deducted_prior_execution=True,
)
gas_for_loop = per_tx_gas - intrinsic - setup_cost
if gas_for_loop < iteration_cost:
break
expected_opcode_count += gas_for_loop // iteration_cost
txs.append(
Transaction(
to=attack_contract_address,
sender=pre.fund_eoa(),
gas_limit=per_tx_gas,
data=calldata,
)
)
remaining_gas -= per_tx_gas
assert len(txs) != 0, "No transactions were added to the test."
benchmark_test(
target_opcode=Precompile.RIPEMD160,
skip_gas_used_validation=True,
expected_receipt_status=1,
blocks=[Block(txs=txs)],
expected_opcode_count=expected_opcode_count,
)
|