Skip to content

Test Modexp

Documentation for tests/byzantium/eip198_modexp_precompile/test_modexp.py.

Generate fixtures for these test cases with:

fill -v tests/byzantium/eip198_modexp_precompile/test_modexp.py
Test EIP-198: MODEXP Precompile

Tests the MODEXP precompile, located at address 0x0000..0005. Test cases from the EIP are labelled with EIP-198-caseX in the test id.

test_modexp(state_test, input, output, pre)

Test the MODEXP precompile

Source code in tests/byzantium/eip198_modexp_precompile/test_modexp.py
 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
@pytest.mark.valid_from("Byzantium")
@pytest.mark.parametrize(
    ["input", "output"],
    [
        (
            ModExpInput(base="", exponent="", modulus="02"),
            ExpectedOutput(call_return_code="0x01", returned_data="0x01"),
        ),
        (
            ModExpInput(base="", exponent="", modulus="0002"),
            ExpectedOutput(call_return_code="0x01", returned_data="0x0001"),
        ),
        (
            ModExpInput(base="00", exponent="00", modulus="02"),
            ExpectedOutput(call_return_code="0x01", returned_data="0x01"),
        ),
        (
            ModExpInput(base="", exponent="01", modulus="02"),
            ExpectedOutput(call_return_code="0x01", returned_data="0x00"),
        ),
        (
            ModExpInput(base="01", exponent="01", modulus="02"),
            ExpectedOutput(call_return_code="0x01", returned_data="0x01"),
        ),
        (
            ModExpInput(base="02", exponent="01", modulus="03"),
            ExpectedOutput(call_return_code="0x01", returned_data="0x02"),
        ),
        (
            ModExpInput(base="02", exponent="02", modulus="05"),
            ExpectedOutput(call_return_code="0x01", returned_data="0x04"),
        ),
        (
            ModExpInput(base="", exponent="", modulus=""),
            ExpectedOutput(call_return_code="0x01", returned_data="0x"),
        ),
        (
            ModExpInput(base="", exponent="", modulus="00"),
            ExpectedOutput(call_return_code="0x01", returned_data="0x00"),
        ),
        (
            ModExpInput(base="", exponent="", modulus="01"),
            ExpectedOutput(call_return_code="0x01", returned_data="0x00"),
        ),
        (
            ModExpInput(base="", exponent="", modulus="0001"),
            ExpectedOutput(call_return_code="0x01", returned_data="0x0000"),
        ),
        # Test cases from EIP 198.
        pytest.param(
            ModExpInput(
                base="03",
                exponent="fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e",
                modulus="fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f",
            ),
            ExpectedOutput(
                call_return_code="0x01",
                returned_data="0000000000000000000000000000000000000000000000000000000000000001",
            ),
            id="EIP-198-case1",
        ),
        pytest.param(
            ModExpInput(
                base="",
                exponent="fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e",
                modulus="fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f",
            ),
            ExpectedOutput(
                call_return_code="0x01",
                returned_data="0000000000000000000000000000000000000000000000000000000000000000",
            ),
            id="EIP-198-case2",
        ),
        pytest.param(  # Note: This is the only test case which goes out-of-gas.
            ModExpRawInput(
                raw_input="0000000000000000000000000000000000000000000000000000000000000000"
                "0000000000000000000000000000000000000000000000000000000000000020"
                "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
                "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"
                "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd"
            ),
            ExpectedOutput(
                call_return_code="0x00",
                returned_data="0000000000000000000000000000000000000000000000000000000000000000",
            ),
            id="EIP-198-case3-raw-input-out-of-gas",
        ),
        pytest.param(
            ModExpInput(
                base="03",
                exponent="ffff",
                modulus="8000000000000000000000000000000000000000000000000000000000000000",
                extra_data="07",
            ),
            ExpectedOutput(
                call_return_code="0x01",
                returned_data="0x3b01b01ac41f2d6e917c6d6a221ce793802469026d9ab7578fa2e79e4da6aaab",
            ),
            id="EIP-198-case4-extra-data_07",
        ),
        pytest.param(
            ModExpRawInput(
                raw_input="0000000000000000000000000000000000000000000000000000000000000001"
                "0000000000000000000000000000000000000000000000000000000000000002"
                "0000000000000000000000000000000000000000000000000000000000000020"
                "03"
                "ffff"
                "80"
            ),
            ExpectedOutput(
                call_return_code="0x01",
                returned_data="0x3b01b01ac41f2d6e917c6d6a221ce793802469026d9ab7578fa2e79e4da6aaab",
            ),
            id="EIP-198-case5-raw-input",
        ),
    ],
    ids=lambda param: param.__repr__(),  # only required to remove parameter names (input/output)
)
def test_modexp(
    state_test: StateTestFiller, input: ModExpInput, output: ExpectedOutput, pre: Alloc
):
    """
    Test the MODEXP precompile
    """
    env = Environment()
    sender = pre.fund_eoa()

    account = pre.deploy_contract(
        # Store all CALLDATA into memory (offset 0)
        Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE())
        # Store the returned CALL status (success = 1, fail = 0) into slot 0:
        + Op.SSTORE(
            0,
            # Setup stack to CALL into ModExp with the CALLDATA and CALL into it (+ pop value)
            Op.CALL(Op.GAS(), 0x05, 0, 0, Op.CALLDATASIZE(), 0, 0),
        )
        # Store contract deployment code to deploy the returned data from ModExp as
        # contract code (16 bytes)
        + Op.MSTORE(
            0,
            (
                (
                    # Need to `ljust` this PUSH32 in order to ensure the code starts
                    # in memory at offset 0 (memory right-aligns stack items which are not
                    # 32 bytes)
                    Op.PUSH32(
                        bytes(
                            Op.CODECOPY(0, 16, Op.SUB(Op.CODESIZE(), 16))
                            + Op.RETURN(0, Op.SUB(Op.CODESIZE, 16))
                        ).ljust(32, bytes(1))
                    )
                )
            ),
        )
        # RETURNDATACOPY the returned data from ModExp into memory (offset 16 bytes)
        + Op.RETURNDATACOPY(16, 0, Op.RETURNDATASIZE())
        # CREATE contract with the deployment code + the returned data from ModExp
        + Op.CREATE(0, 0, Op.ADD(16, Op.RETURNDATASIZE()))
        # STOP (handy for tracing)
        + Op.STOP(),
    )

    tx = Transaction(
        ty=0x0,
        to=account,
        data=input.create_modexp_tx_data(),
        gas_limit=500000,
        gas_price=10,
        protected=True,
        sender=sender,
    )

    post = {}
    if output.call_return_code != "0x00":
        contract_address = compute_create_address(account, 1)
        post[contract_address] = Account(code=output.returned_data)
    post[account] = Account(storage={0: output.call_return_code})

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