Skip to content

test_alt_bn128_uncachable()

Documentation for tests/benchmark/compute/precompile/test_alt_bn128.py::test_alt_bn128_uncachable@20373115.

Generate fixtures for these test cases for Amsterdam with:

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

Benchmark ecAdd/ecMul with unique input per call.

Write the precompile's G1 output (64 bytes) back over the input point so each loop iteration receives a distinct input, avoiding precompile result caching in clients.

Source code in tests/benchmark/compute/precompile/test_alt_bn128.py
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
@pytest.mark.repricing
@pytest.mark.parametrize(
    "precompile_address,scalar,target",
    [
        pytest.param(
            EIP196Spec.ECADD, None, Precompile.BN128_ADD, id="ec_add"
        ),
        pytest.param(
            EIP196Spec.ECMUL,
            2,
            Precompile.BN128_MUL,
            id="ec_mul_small_scalar",
        ),
        pytest.param(
            EIP196Spec.ECMUL,
            2**256 - 1,
            Precompile.BN128_MUL,
            id="ec_mul_max_scalar",
        ),
    ],
)
def test_alt_bn128_uncachable(
    benchmark_test: BenchmarkTestFiller,
    pre: Alloc,
    fork: Fork,
    gas_benchmark_value: int,
    tx_gas_limit: int,
    precompile_address: Address,
    scalar: int | None,
    target: OpcodeTarget,
) -> None:
    """
    Benchmark ecAdd/ecMul with unique input per call.

    Write the precompile's G1 output (64 bytes) back over the
    input point so each loop iteration receives a distinct
    input, avoiding precompile result caching in clients.
    """
    intrinsic_gas_calculator = fork.transaction_intrinsic_cost_calculator()
    gsc = fork.gas_costs()
    precompile_cost = (
        gsc.PRECOMPILE_ECMUL
        if precompile_address == EIP196Spec.ECMUL
        else gsc.PRECOMPILE_ECADD
    )
    attack_block = Op.POP(
        Op.STATICCALL(
            gas=Op.GAS,
            address=precompile_address,
            args_size=Op.CALLDATASIZE,
            # One G1 point (2 * 32 bytes), overwrites the input point
            # so each iteration has unique precompile input.
            ret_size=64,
            # gas accounting
            address_warm=True,
            inner_call_cost=precompile_cost,
        ),
    )

    setup = Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE)
    loop = WhileGas(body=attack_block, fork=fork)
    code = setup + loop
    attack_contract_address = pre.deploy_contract(code=code)

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

    seed = 0
    expected_opcode_count = 0
    while remaining_gas > 0:
        gas_available = min(tx_gas_limit, remaining_gas)

        calldata = Bytes(
            _generate_g1_point(seed) + _generate_g1_point(seed + 1000)
            if scalar is None
            else _generate_g1_point(seed) + scalar.to_bytes(32, "big")
        )

        intrinsic = intrinsic_gas_calculator(
            calldata=calldata,
            return_cost_deducted_prior_execution=True,
        )
        gas_for_loop = gas_available - intrinsic - setup.gas_cost(fork)
        if gas_for_loop < loop.gas_cost(fork):
            break
        expected_opcode_count += gas_for_loop // loop.gas_cost(fork)
        txs.append(
            Transaction(
                to=attack_contract_address,
                sender=pre.fund_eoa(),
                gas_limit=gas_available,
                data=calldata,
            )
        )
        remaining_gas -= gas_available
        seed += 1

    benchmark_test(
        target_opcode=target,
        skip_gas_used_validation=True,
        expected_receipt_status=1,
        blocks=[Block(txs=txs)],
        expected_opcode_count=expected_opcode_count,
    )

Parametrized Test Cases

This test generates 3 parametrized test cases across 3 forks.