Skip to content

test_ec_pairing()

Documentation for tests/benchmark/compute/precompile/test_alt_bn128.py::test_ec_pairing@ffe48572.

Generate fixtures for these test cases for Bogota with:

fill -v tests/benchmark/compute/precompile/test_alt_bn128.py::test_ec_pairing --gas-benchmark-values 1

Benchmark ecpairing precompile with unique inputs per call.

Source code in tests/benchmark/compute/precompile/test_alt_bn128.py
@pytest.mark.repricing
@pytest.mark.parametrize("num_pairs", [1, 3, 6, 12, 24])
def test_ec_pairing(
    benchmark_test: BenchmarkTestFiller,
    pre: Alloc,
    fork: Fork,
    gas_benchmark_value: int,
    tx_gas_limit: int,
    num_pairs: int,
) -> None:
    """Benchmark ecpairing precompile with unique inputs per call."""
    pair_size = num_pairs * 192
    gsc = fork.gas_costs()
    intrinsic_gas_calculator = fork.transaction_intrinsic_cost_calculator()
    mem_exp = fork.memory_expansion_gas_calculator()
    precompile_cost = (
        gsc.PRECOMPILE_ECPAIRING_BASE
        + gsc.PRECOMPILE_ECPAIRING_PER_POINT * num_pairs
    )

    # Each iteration: STATICCALL ecpairing at advancing calldata offset,
    # then advance offset by pair_size at memory[CALLDATASIZE].
    # The loop condition checks remaining gas against one body execution.
    attack_block = Op.POP(
        Op.STATICCALL(
            gas=Op.GAS,
            address=EIP197Spec.ECPAIRING,
            args_offset=Op.MLOAD(Op.CALLDATASIZE),
            args_size=pair_size,
            # gas accounting
            address_warm=True,
            inner_call_cost=precompile_cost,
        ),
    ) + Op.MSTORE(
        Op.CALLDATASIZE,
        Op.ADD(Op.MLOAD(Op.CALLDATASIZE), pair_size),
    )

    setup = Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE)
    loop = While(
        body=attack_block,
        condition=Op.GT(Op.CALLDATASIZE, Op.MLOAD(Op.CALLDATASIZE)),
    )
    code = setup + loop
    attack_contract_address = pre.deploy_contract(code=code)

    iteration_cost = loop.gas_cost(fork)
    setup_cost = setup.gas_cost(fork)

    # Conservative per-variant estimate for sizing the calldata:
    # one loop iteration + worst-case calldata intrinsic (all non-zero)
    # + CALLDATACOPY copy and linear memory expansion.
    words_per_variant = math.ceil(pair_size / 32)
    per_variant_gas = (
        iteration_cost
        + pair_size * 16
        + words_per_variant * (gsc.OPCODE_COPY_PER_WORD + gsc.MEMORY_PER_WORD)
    )
    empty_intrinsic = intrinsic_gas_calculator(
        calldata=[], return_cost_deducted_prior_execution=True
    )
    fixed_overhead = empty_intrinsic + setup_cost + mem_exp(new_bytes=32)

    seed_offset = 0
    txs: list[Transaction] = []
    remaining_gas = gas_benchmark_value

    expected_opcode_count = 0
    while remaining_gas > 0:
        per_tx_gas = min(tx_gas_limit, remaining_gas)
        per_tx_variants = max(
            1, (per_tx_gas - fixed_overhead) // per_variant_gas
        )
        calldata = Bytes(
            b"".join(
                _generate_bn128_pairs(num_pairs, seed=42 + seed_offset + i)
                for i in range(per_tx_variants)
            )
        )

        execution_intrinsic = intrinsic_gas_calculator(
            calldata=calldata,
            return_cost_deducted_prior_execution=True,
        )
        gas_for_loop = (
            per_tx_gas
            - execution_intrinsic
            - setup_cost
            - math.ceil(len(calldata) / 32) * gsc.OPCODE_COPY_PER_WORD
            - mem_exp(new_bytes=len(calldata) + 32)
        )

        if gas_for_loop < per_tx_variants * iteration_cost:
            break
        expected_opcode_count += per_tx_variants

        txs.append(
            Transaction(
                to=attack_contract_address,
                sender=pre.fund_eoa(),
                gas_limit=per_tx_gas,
                data=calldata,
            )
        )
        remaining_gas -= per_tx_gas
        seed_offset += per_tx_variants

    assert len(txs) != 0, "No transactions were added to the test."

    benchmark_test(
        target_opcode=Precompile.BN128_PAIRING,
        skip_gas_used_validation=True,
        verifications=[ReceiptStatusExpected()],
        expected_opcode_count=expected_opcode_count,
        blocks=[Block(txs=txs)],
    )

Parametrized Test Cases

This test generates 5 parametrized test cases across 4 forks.