Skip to content

test_worst_precompile_only_data_input()

Documentation for tests/zkevm/test_worst_compute.py::test_worst_precompile_only_data_input@64f949d0.

Generate fixtures for these test cases for Prague with:

fill -v tests/zkevm/test_worst_compute.py::test_worst_precompile_only_data_input --fork Prague

Test running a block with as many precompile calls which have a single data input.

Source code in tests/zkevm/test_worst_compute.py
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
@pytest.mark.valid_from("Cancun")
@pytest.mark.parametrize(
    "address,static_cost,per_word_dynamic_cost,bytes_per_unit_of_work",
    [
        pytest.param(0x02, 60, 12, 64, id="SHA2-256"),
        pytest.param(0x03, 600, 120, 64, id="RIPEMD-160"),
        pytest.param(0x04, 15, 3, 1, id="IDENTITY"),
    ],
)
@pytest.mark.slow()
def test_worst_precompile_only_data_input(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    address: Address,
    static_cost: int,
    per_word_dynamic_cost: int,
    bytes_per_unit_of_work: int,
):
    """Test running a block with as many precompile calls which have a single `data` input."""
    env = Environment()

    # Intrinsic gas cost is paid once.
    intrinsic_gas_calculator = fork.transaction_intrinsic_cost_calculator()
    available_gas = env.gas_limit - intrinsic_gas_calculator()

    gsc = fork.gas_costs()
    mem_exp_gas_calculator = fork.memory_expansion_gas_calculator()

    # Discover the optimal input size to maximize precompile work, not precompile calls.
    max_work = 0
    optimal_input_length = 0
    for input_length in range(1, 1_000_000, 32):
        parameters_gas = (
            gsc.G_BASE  # PUSH0 = arg offset
            + gsc.G_BASE  # PUSH0 = arg size
            + gsc.G_BASE  # PUSH0 = arg size
            + gsc.G_VERY_LOW  # PUSH0 = arg offset
            + gsc.G_VERY_LOW  # PUSHN = address
            + gsc.G_BASE  # GAS
        )
        iteration_gas_cost = (
            parameters_gas
            + +static_cost  # Precompile static cost
            + math.ceil(input_length / 32) * per_word_dynamic_cost  # Precompile dynamic cost
            + gsc.G_BASE  # POP
        )
        # From the available gas, we subtract the mem expansion costs considering we know the
        # current input size length.
        available_gas_after_expansion = max(
            0, available_gas - mem_exp_gas_calculator(new_bytes=input_length)
        )
        # Calculate how many calls we can do.
        num_calls = available_gas_after_expansion // iteration_gas_cost
        total_work = num_calls * math.ceil(input_length / bytes_per_unit_of_work)

        # If we found an input size that is better (reg permutations/gas), then save it.
        if total_work > max_work:
            max_work = total_work
            optimal_input_length = input_length

    calldata = Op.CODECOPY(0, 0, optimal_input_length)
    attack_block = Op.POP(Op.STATICCALL(Op.GAS, address, 0, optimal_input_length, 0, 0))
    code = code_loop_precompile_call(calldata, attack_block, fork)

    code_address = pre.deploy_contract(code=code)

    tx = Transaction(
        to=code_address,
        gas_limit=env.gas_limit,
        sender=pre.fund_eoa(),
    )

    state_test(
        env=env,
        pre=pre,
        post={},
        tx=tx,
    )

Parametrized Test Cases

The interactive table below is also available as a standalone page.

Test ID (Abbreviated) address static_cost per_word_dynamic_cost bytes_per_unit_of_work
...fork_Cancun-state_test-SHA2-256 2 60 12 64
...fork_Cancun-state_test-RIPEMD-160 3 600 120 64
...fork_Cancun-state_test-IDENTITY 4 15 3 1
...fork_Cancun-blockchain_test_from_state_test-SHA2-256 2 60 12 64
...fork_Cancun-blockchain_test_from_state_test-RIPEMD-160 3 600 120 64
...fork_Cancun-blockchain_test_from_state_test-IDENTITY 4 15 3 1
...fork_Prague-state_test-SHA2-256 2 60 12 64
...fork_Prague-state_test-RIPEMD-160 3 600 120 64
...fork_Prague-state_test-IDENTITY 4 15 3 1
...fork_Prague-blockchain_test_from_state_test-SHA2-256 2 60 12 64
...fork_Prague-blockchain_test_from_state_test-RIPEMD-160 3 600 120 64
...fork_Prague-blockchain_test_from_state_test-IDENTITY 4 15 3 1
...fork_Osaka-state_test-SHA2-256 2 60 12 64
...fork_Osaka-state_test-RIPEMD-160 3 600 120 64
...fork_Osaka-state_test-IDENTITY 4 15 3 1
...fork_Osaka-blockchain_test_from_state_test-SHA2-256 2 60 12 64
...fork_Osaka-blockchain_test_from_state_test-RIPEMD-160 3 600 120 64
...fork_Osaka-blockchain_test_from_state_test-IDENTITY 4 15 3 1