Skip to content

test_worst_precompile_only_data_input()

Documentation for tests/benchmark/test_worst_compute.py::test_worst_precompile_only_data_input@88e9fb8f.

Generate fixtures for these test cases for Osaka with:

fill -v tests/benchmark/test_worst_compute.py::test_worst_precompile_only_data_input -m benchmark

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

Source code in tests/benchmark/test_worst_compute.py
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
@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"),
    ],
)
def test_worst_precompile_only_data_input(
    benchmark_test: BenchmarkTestFiller,
    fork: Fork,
    address: Address,
    static_cost: int,
    per_word_dynamic_cost: int,
    bytes_per_unit_of_work: int,
    gas_benchmark_value: int,
) -> None:
    """
    Test running a block with as many precompile calls which have a single
    `data` input.
    """
    # Intrinsic gas cost is paid once.
    intrinsic_gas_calculator = fork.transaction_intrinsic_cost_calculator()
    available_gas = gas_benchmark_value - 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

    attack_block = Op.POP(Op.STATICCALL(Op.GAS, address, 0, optimal_input_length, 0, 0))

    benchmark_test(
        code_generator=JumpLoopGenerator(
            setup=Op.CODECOPY(0, 0, optimal_input_length), attack_block=attack_block
        ),
    )

Parametrized Test Cases

This test case is only parametrized by fork.

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