Skip to content

test_amortized_bn128_pairings()

Documentation for tests/benchmark/test_worst_compute.py::test_amortized_bn128_pairings@0f7c73a7.

Generate fixtures for these test cases for Prague with:

fill -v tests/benchmark/test_worst_compute.py::test_amortized_bn128_pairings --fork Prague

Test running a block with as many BN128 pairings as possible.

Source code in tests/benchmark/test_worst_compute.py
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
@pytest.mark.valid_from("Cancun")
@pytest.mark.slow()
def test_amortized_bn128_pairings(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
):
    """Test running a block with as many BN128 pairings as possible."""
    env = Environment()

    base_cost = 45_000
    pairing_cost = 34_000
    size_per_pairing = 192

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

    # This is a theoretical maximum number of pairings that can be done in a block.
    # It is only used for an upper bound for calculating the optimal number of pairings below.
    maximum_number_of_pairings = (env.gas_limit - base_cost) // pairing_cost

    # Discover the optimal number of pairings balancing two dimensions:
    # 1. Amortize the precompile base cost as much as possible.
    # 2. The cost of the memory expansion.
    max_pairings = 0
    optimal_per_call_num_pairings = 0
    for i in range(1, maximum_number_of_pairings + 1):
        # We'll pass all pairing arguments via calldata.
        available_gas_after_intrinsic = env.gas_limit - intrinsic_gas_calculator(
            calldata=[0xFF] * size_per_pairing * i  # 0xFF is to indicate non-zero bytes.
        )
        available_gas_after_expansion = max(
            0,
            available_gas_after_intrinsic - mem_exp_gas_calculator(new_bytes=i * size_per_pairing),
        )

        # This is ignoring "glue" opcodes, but helps to have a rough idea of the right
        # cutting point.
        approx_gas_cost_per_call = gsc.G_WARM_ACCOUNT_ACCESS + base_cost + i * pairing_cost

        num_precompile_calls = available_gas_after_expansion // approx_gas_cost_per_call
        num_pairings_done = num_precompile_calls * i  # Each precompile call does i pairings.

        if num_pairings_done > max_pairings:
            max_pairings = num_pairings_done
            optimal_per_call_num_pairings = i

    calldata = Op.CALLDATACOPY(size=Op.CALLDATASIZE)
    attack_block = Op.POP(Op.STATICCALL(Op.GAS, 0x08, 0, Op.CALLDATASIZE, 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,
        data=_generate_bn128_pairs(optimal_per_call_num_pairings, 42),
        sender=pre.fund_eoa(),
    )

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

Parametrized Test Cases

This test case is only parametrized by fork and fixture format.

Test ID (Abbreviated)
...fork_Cancun-state_test
...fork_Cancun-blockchain_test_from_state_test
...fork_Prague-state_test
...fork_Prague-blockchain_test_from_state_test
...fork_Osaka-state_test
...fork_Osaka-blockchain_test_from_state_test