Skip to content

test_worst_modarith()

Documentation for tests/benchmark/test_worst_compute.py::test_worst_modarith@v5.1.0.

Generate fixtures for these test cases for Osaka with:

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

Test running a block with as many "op" instructions with arguments of the parametrized range. The test program consists of code segments evaluating the "op chain": mod[0] = calldataload(0) mod[1] = (fixed_arg op args[indexes[0]]) % mod[0] mod[2] = (fixed_arg op args[indexes[1]]) % mod[1] The "args" is a pool of 15 constants pushed to the EVM stack at the program start. The "fixed_arg" is the 0xFF...FF constant added to the EVM stack by PUSH32 just before executing the "op". The order of accessing the numerators is selected in a way the mod value remains in the range as long as possible.

Source code in tests/benchmark/test_worst_compute.py
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
@pytest.mark.parametrize("mod_bits", [255, 191, 127, 63])
@pytest.mark.parametrize("op", [Op.ADDMOD, Op.MULMOD])
def test_worst_modarith(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    mod_bits: int,
    op: Op,
    gas_benchmark_value: int,
):
    """
    Test running a block with as many "op" instructions with arguments of the parametrized range.
    The test program consists of code segments evaluating the "op chain":
    mod[0] = calldataload(0)
    mod[1] = (fixed_arg op args[indexes[0]]) % mod[0]
    mod[2] = (fixed_arg op args[indexes[1]]) % mod[1]
    The "args" is a pool of 15 constants pushed to the EVM stack at the program start.
    The "fixed_arg" is the 0xFF...FF constant added to the EVM stack by PUSH32
    just before executing the "op".
    The order of accessing the numerators is selected in a way the mod value remains in the range
    as long as possible.
    """
    fixed_arg = 2**256 - 1
    num_args = 15

    max_code_size = fork.max_code_size()

    # Pick the modulus min value so that it is _unlikely_ to drop to the lower word count.
    assert mod_bits >= 63
    mod_min = 2 ** (mod_bits - 63)

    # Select the random seed giving the longest found op chain.
    # You can look for a longer one by increasing the op_chain_len. This will activate
    # the while loop below.
    op_chain_len = 666
    match op, mod_bits:
        case Op.ADDMOD, 255:
            seed = 4
        case Op.ADDMOD, 191:
            seed = 2
        case Op.ADDMOD, 127:
            seed = 2
        case Op.ADDMOD, 63:
            seed = 64
        case Op.MULMOD, 255:
            seed = 5
        case Op.MULMOD, 191:
            seed = 389
        case Op.MULMOD, 127:
            seed = 5
        case Op.MULMOD, 63:
            # For this setup we were not able to find an op-chain longer than 600.
            seed = 4193
            op_chain_len = 600
        case _:
            raise ValueError(f"{mod_bits}-bit {op} not supported.")

    while True:
        rng = random.Random(seed)
        args = [rng.randint(2**255, 2**256 - 1) for _ in range(num_args)]
        initial_mod = rng.randint(2 ** (mod_bits - 1), 2**mod_bits - 1)

        # Evaluate the op chain and collect the order of accessing numerators.
        op_fn = operator.add if op == Op.ADDMOD else operator.mul
        mod = initial_mod
        indexes: list[int] = []
        while mod >= mod_min and len(indexes) < op_chain_len:
            results = [op_fn(a, fixed_arg) % mod for a in args]
            i = max(range(len(results)), key=results.__getitem__)  # And pick the best one.
            mod = results[i]
            indexes.append(i)

        assert len(indexes) == op_chain_len  # Disable if you want to find longer op chains.
        if len(indexes) == op_chain_len:
            break
        seed += 1
        print(f"{seed=}")

    code_constant_pool = sum((Op.PUSH32[n] for n in args), Bytecode())
    code_segment = (
        Op.CALLDATALOAD(0)
        + sum(make_dup(len(args) - i) + Op.PUSH32[fixed_arg] + op for i in indexes)
        + Op.POP
    )
    # Construct the final code. Because of the usage of PUSH32 the code segment is very long,
    # so don't try to include multiple of these.
    code = code_constant_pool + Op.JUMPDEST + code_segment + Op.JUMP(len(code_constant_pool))
    assert (max_code_size - len(code_segment)) < len(code) <= max_code_size

    tx = Transaction(
        to=pre.deploy_contract(code=code),
        data=initial_mod.to_bytes(32, byteorder="big"),
        gas_limit=gas_benchmark_value,
        sender=pre.fund_eoa(),
    )

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

Parametrized Test Cases

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

Test ID (Abbreviated) op mod_bits
...fork_Prague-state_test-op_ADDMOD-mod_bits_255 ADDMOD 255
...fork_Prague-state_test-op_ADDMOD-mod_bits_191 ADDMOD 191
...fork_Prague-state_test-op_ADDMOD-mod_bits_127 ADDMOD 127
...fork_Prague-state_test-op_ADDMOD-mod_bits_63 ADDMOD 63
...fork_Prague-state_test-op_MULMOD-mod_bits_255 MULMOD 255
...fork_Prague-state_test-op_MULMOD-mod_bits_191 MULMOD 191
...fork_Prague-state_test-op_MULMOD-mod_bits_127 MULMOD 127
...fork_Prague-state_test-op_MULMOD-mod_bits_63 MULMOD 63
...fork_Prague-blockchain_test_from_state_test-op_ADDMOD-mod_bits_255 ADDMOD 255
...fork_Prague-blockchain_test_from_state_test-op_ADDMOD-mod_bits_191 ADDMOD 191
...fork_Prague-blockchain_test_from_state_test-op_ADDMOD-mod_bits_127 ADDMOD 127
...fork_Prague-blockchain_test_from_state_test-op_ADDMOD-mod_bits_63 ADDMOD 63
...fork_Prague-blockchain_test_from_state_test-op_MULMOD-mod_bits_255 MULMOD 255
...fork_Prague-blockchain_test_from_state_test-op_MULMOD-mod_bits_191 MULMOD 191
...fork_Prague-blockchain_test_from_state_test-op_MULMOD-mod_bits_127 MULMOD 127
...fork_Prague-blockchain_test_from_state_test-op_MULMOD-mod_bits_63 MULMOD 63
...fork_Osaka-state_test-op_ADDMOD-mod_bits_255 ADDMOD 255
...fork_Osaka-state_test-op_ADDMOD-mod_bits_191 ADDMOD 191
...fork_Osaka-state_test-op_ADDMOD-mod_bits_127 ADDMOD 127
...fork_Osaka-state_test-op_ADDMOD-mod_bits_63 ADDMOD 63
...fork_Osaka-state_test-op_MULMOD-mod_bits_255 MULMOD 255
...fork_Osaka-state_test-op_MULMOD-mod_bits_191 MULMOD 191
...fork_Osaka-state_test-op_MULMOD-mod_bits_127 MULMOD 127
...fork_Osaka-state_test-op_MULMOD-mod_bits_63 MULMOD 63
...fork_Osaka-blockchain_test_from_state_test-op_ADDMOD-mod_bits_255 ADDMOD 255
...fork_Osaka-blockchain_test_from_state_test-op_ADDMOD-mod_bits_191 ADDMOD 191
...fork_Osaka-blockchain_test_from_state_test-op_ADDMOD-mod_bits_127 ADDMOD 127
...fork_Osaka-blockchain_test_from_state_test-op_ADDMOD-mod_bits_63 ADDMOD 63
...fork_Osaka-blockchain_test_from_state_test-op_MULMOD-mod_bits_255 MULMOD 255
...fork_Osaka-blockchain_test_from_state_test-op_MULMOD-mod_bits_191 MULMOD 191
...fork_Osaka-blockchain_test_from_state_test-op_MULMOD-mod_bits_127 MULMOD 127
...fork_Osaka-blockchain_test_from_state_test-op_MULMOD-mod_bits_63 MULMOD 63