Skip to content

test_arithmetic()

Documentation for tests/benchmark/compute/instruction/test_arithmetic.py::test_arithmetic@tests-bal@v5.7.0.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/benchmark/compute/instruction/test_arithmetic.py::test_arithmetic --gas-benchmark-values 1

Benchmark arithmetic instructions.

Supports both binary ops and ternary operations. The execution starts with initial values on the stack.

Source code in tests/benchmark/compute/instruction/test_arithmetic.py
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
@pytest.mark.parametrize(
    "opcode,opcode_args",
    [
        pytest.param(
            Op.ADD,
            DEFAULT_BINOP_ARGS,
            marks=pytest.mark.repricing,
        ),
        pytest.param(
            Op.MUL,
            DEFAULT_BINOP_ARGS,
            marks=pytest.mark.repricing,
        ),
        pytest.param(
            # After every 2 SUB operations, values return to initial.
            Op.SUB,
            DEFAULT_BINOP_ARGS,
            marks=pytest.mark.repricing,
        ),
        pytest.param(
            # This has the cycle of 2:
            # v[0] = a // b
            # v[1] = a // v[0] = a // (a // b) = b
            # v[2] = a // b
            Op.DIV,
            (
                0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F,
                # We want the first divisor to be slightly bigger than 2**128:
                # this is the worst case for the division algorithm with
                # optimized paths for division by 1 and 2 words.
                0x100000000000000000000000000000033,
            ),
            marks=pytest.mark.repricing,
        ),
        pytest.param(
            # This has the cycle of 2, see above.
            Op.DIV,
            (
                0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F,
                # We want the first divisor to be slightly bigger than 2**64:
                # this is the worst case for the division algorithm with an
                # optimized path for division by 1 word.
                0x10000000000000033,
            ),
        ),
        pytest.param(
            # Same as DIV-0
            # But the numerator made positive, and the divisor made negative.
            Op.SDIV,
            (
                0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F,
                0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFCD,
            ),
            marks=pytest.mark.repricing,
        ),
        pytest.param(
            # Same as DIV-1
            # But the numerator made positive, and the divisor made negative.
            Op.SDIV,
            (
                0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F,
                0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFCD,
            ),
        ),
        pytest.param(
            # Not suitable for MOD, as values quickly become zero.
            Op.MOD,
            DEFAULT_BINOP_ARGS,
            marks=pytest.mark.repricing,
        ),
        pytest.param(
            # Not suitable for SMOD, as values quickly become zero.
            Op.SMOD,
            DEFAULT_BINOP_ARGS,
            marks=pytest.mark.repricing,
        ),
        pytest.param(
            # This keeps the values unchanged
            # pow(2**256-1, 2**256-1, 2**256) == 2**256-1.
            Op.EXP,
            (
                0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF,
                0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF,
            ),
            marks=pytest.mark.repricing,
        ),
        pytest.param(
            # Not great, as we always sign-extend the 4 bytes.
            Op.SIGNEXTEND,
            (
                3,
                0xFFDADADA,  # Negative to have more work.
            ),
            marks=pytest.mark.repricing,
        ),
        pytest.param(
            Op.ADDMOD,
            (
                0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F,
                0x73EDA753299D7D483339D80809A1D80553BDA402FFFE5BFEFFFFFFFF00000001,
                0x100000000000000000000000000000033,
            ),
            marks=pytest.mark.repricing,
        ),
        pytest.param(
            Op.MULMOD,
            (
                0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F,
                0x73EDA753299D7D483339D80809A1D80553BDA402FFFE5BFEFFFFFFFF00000001,
                0x100000000000000000000000000000033,
            ),
            marks=pytest.mark.repricing,
        ),
    ],
    ids=lambda param: "" if isinstance(param, tuple) else param,
)
def test_arithmetic(
    benchmark_test: BenchmarkTestFiller,
    opcode: Op,
    opcode_args: tuple[int, ...],
) -> None:
    """
    Benchmark arithmetic instructions.

    Supports both binary ops and ternary operations.
    The execution starts with initial values on the stack.
    """
    tx_data = b"".join(
        arg.to_bytes(32, byteorder="big") for arg in opcode_args
    )

    depth = len(opcode_args)
    dup_op = make_dup(depth - 1)

    setup = sum((Op.CALLDATALOAD(32 * i) for i in range(depth)), Bytecode())
    setup += dup_op * depth

    attack_block = dup_op * (depth - 1) + opcode

    cleanup = Op.POP * depth + dup_op * depth

    benchmark_test(
        target_opcode=opcode,
        code_generator=JumpLoopGenerator(
            setup=setup,
            attack_block=attack_block,
            cleanup=cleanup,
            tx_kwargs={"data": tx_data},
        ),
    )

Parametrized Test Cases

This test generates 13 parametrized test cases across 3 forks.