Skip to content

test_modular_comparison()

Documentation for tests/osaka/eip7951_p256verify_precompiles/test_p256verify.py::test_modular_comparison@v5.0.0.

Generate fixtures for these test cases for Osaka with:

fill -v tests/osaka/eip7951_p256verify_precompiles/test_p256verify.py::test_modular_comparison --fork Osaka

Test the modular comparison condition for secp256r1 precompile.

This tests that when the x-coordinate of R' exceeds the curve order N, the verification should use modular arithmetic: r' ≡ r (mod N) instead of direct equality r' == r.

Source code in tests/osaka/eip7951_p256verify_precompiles/test_p256verify.py
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
@pytest.mark.parametrize(
    "input_data,expected_output",
    [
        # Test case where computed x-coordinate exceeds curve order N
        # This tests the modular comparison: r' ≡ r (mod N)
        pytest.param(
            Spec.H0
            # R: A value that when used in ECDSA verification produces an x-coordinate > N
            + R(0x000000000000000000000000000000004319055358E8617B0C46353D039CDAAB)
            + S(0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC63254E)
            # X, Y: Public key coordinates that will produce x-coordinate > N during verification
            + X(0x0AD99500288D466940031D72A9F5445A4D43784640855BF0A69874D2DE5FE103)
            + Y(0xC5011E6EF2C42DCD50D5D3D29F99AE6EBA2C80C9244F4C5422F0979FF0C3BA5E),
            Spec.SUCCESS_RETURN_VALUE,
            id="modular_comparison_x_coordinate_exceeds_n",
        ),
        pytest.param(
            Spec.H0
            + R(Spec.N + 1)  # R = N + 1 ≡ 1 (mod N)
            + Spec.S0
            + Spec.X0
            + Spec.Y0,
            Spec.INVALID_RETURN_VALUE,  # Should fail because R = 1 is not a valid signature
            id="r_equals_n_plus_one",
        ),
        pytest.param(
            Spec.H0
            + R(Spec.N + 2)  # R = N + 2 ≡ 2 (mod N)
            + Spec.S0
            + Spec.X0
            + Spec.Y0,
            Spec.INVALID_RETURN_VALUE,  # Should fail because R = 2 is not a valid signature
            id="r_equals_n_plus_two",
        ),
    ],
)
@pytest.mark.parametrize("precompile_address", [Spec.P256VERIFY], ids=[""])
@pytest.mark.eip_checklist("precompile/test/inputs/valid")
@pytest.mark.eip_checklist("precompile/test/inputs/invalid/crypto")
def test_modular_comparison(state_test: StateTestFiller, pre: Alloc, post: dict, tx: Transaction):
    """
    Test the modular comparison condition for secp256r1 precompile.

    This tests that when the x-coordinate of R' exceeds the curve order N,
    the verification should use modular arithmetic:
    r' ≡ r (mod N) instead of direct equality r' == r.
    """
    state_test(env=Environment(), pre=pre, post=post, tx=tx)

Parametrized Test Cases

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

Test ID (Abbreviated) precompile_address input_data expected_output
...fork_Osaka-state_test--modular_comparison_x_coordinate_exceeds_n 256 bb5a52f42f9c9261ed4361f59422a1e3 0036e7c32b270c8807a419feca605023 00000000000000000000000000000000 4319055358e8617b0c46353d039cdaab ffffffff00000000ffffffffffffffff bce6faada7179e84f3b9cac2fc63254e 0ad99500288d466940031d72a9f5445a 4d43784640855bf0a69874d2de5fe103 c5011e6ef2c42dcd50d5d3d29f99ae6e ba2c80c9244f4c5422f0979ff0c3ba5e 00000000000000000000000000000000 00000000000000000000000000000001
...fork_Osaka-state_test--r_equals_n_plus_one 256 bb5a52f42f9c9261ed4361f59422a1e3 0036e7c32b270c8807a419feca605023 ffffffff00000000ffffffffffffffff bce6faada7179e84f3b9cac2fc632552 4cd60b855d442f5b3c7b11eb6c4e0ae7 525fe710fab9aa7c77a67f79e6fadd76 2927b10512bae3eddcfe467828128bad 2903269919f7086069c8c4df6c732838 c7787964eaac00e5921fb1498a60f460 6766b3d9685001558d1a974e7341513e
...fork_Osaka-state_test--r_equals_n_plus_two 256 bb5a52f42f9c9261ed4361f59422a1e3 0036e7c32b270c8807a419feca605023 ffffffff00000000ffffffffffffffff bce6faada7179e84f3b9cac2fc632553 4cd60b855d442f5b3c7b11eb6c4e0ae7 525fe710fab9aa7c77a67f79e6fadd76 2927b10512bae3eddcfe467828128bad 2903269919f7086069c8c4df6c732838 c7787964eaac00e5921fb1498a60f460 6766b3d9685001558d1a974e7341513e
...fork_Osaka-blockchain_test_from_state_test--modular_comparison_x_coordinate_exceeds_n 256 bb5a52f42f9c9261ed4361f59422a1e3 0036e7c32b270c8807a419feca605023 00000000000000000000000000000000 4319055358e8617b0c46353d039cdaab ffffffff00000000ffffffffffffffff bce6faada7179e84f3b9cac2fc63254e 0ad99500288d466940031d72a9f5445a 4d43784640855bf0a69874d2de5fe103 c5011e6ef2c42dcd50d5d3d29f99ae6e ba2c80c9244f4c5422f0979ff0c3ba5e 00000000000000000000000000000000 00000000000000000000000000000001
...fork_Osaka-blockchain_test_from_state_test--r_equals_n_plus_one 256 bb5a52f42f9c9261ed4361f59422a1e3 0036e7c32b270c8807a419feca605023 ffffffff00000000ffffffffffffffff bce6faada7179e84f3b9cac2fc632552 4cd60b855d442f5b3c7b11eb6c4e0ae7 525fe710fab9aa7c77a67f79e6fadd76 2927b10512bae3eddcfe467828128bad 2903269919f7086069c8c4df6c732838 c7787964eaac00e5921fb1498a60f460 6766b3d9685001558d1a974e7341513e
...fork_Osaka-blockchain_test_from_state_test--r_equals_n_plus_two 256 bb5a52f42f9c9261ed4361f59422a1e3 0036e7c32b270c8807a419feca605023 ffffffff00000000ffffffffffffffff bce6faada7179e84f3b9cac2fc632553 4cd60b855d442f5b3c7b11eb6c4e0ae7 525fe710fab9aa7c77a67f79e6fadd76 2927b10512bae3eddcfe467828128bad 2903269919f7086069c8c4df6c732838 c7787964eaac00e5921fb1498a60f460 6766b3d9685001558d1a974e7341513e