Skip to content

test_clz_diff()

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

Generate fixtures for these test cases for Bogota 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
@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 3 forks.