Skip to content

test_clz_diff()

Documentation for tests/benchmark/compute/instruction/test_bitwise.py::test_clz_diff@8db70f93.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/benchmark/compute/instruction/test_bitwise.py::test_clz_diff --gas-benchmark-values 1

Benchmark CLZ instruction with different input.

Source code in tests/benchmark/compute/instruction/test_bitwise.py
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
@pytest.mark.valid_from("Osaka")
def test_clz_diff(
    benchmark_test: BenchmarkTestFiller,
    pre: Alloc,
    fork: Fork,
) -> None:
    """Benchmark CLZ instruction with different input."""
    max_code_size = fork.max_code_size()

    code_prefix = Op.JUMPDEST
    code_suffix = Op.PUSH0 + Op.JUMP

    available_code_size = max_code_size - len(code_prefix) - len(code_suffix)

    code_seq = Bytecode()

    for i in range(available_code_size):
        value = (2**256 - 1) >> (i % 256)
        clz_op = Op.CLZ(value) + Op.POP
        if len(code_seq) + len(clz_op) > available_code_size:
            break
        code_seq += clz_op

    attack_code = code_prefix + code_seq + code_suffix
    assert len(attack_code) <= max_code_size

    tx = Transaction(
        to=pre.deploy_contract(code=attack_code),
        sender=pre.fund_eoa(),
    )

    benchmark_test(
        target_opcode=Op.CLZ,
        tx=tx,
    )

Parametrized Test Cases

This test generates 1 parametrized test case across 2 forks.