Skip to content

test_clz_diff()

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

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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
@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.