Skip to content

test_modular_comparison()

Documentation for tests/osaka/eip7951_p256verify_precompiles/test_p256verify.py::test_modular_comparison@88e9fb8f.

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
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
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
@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="x_coordinate_exceeds_n",
        ),
        pytest.param(
            Spec.H1 + Spec.R1 + Spec.S1 + Spec.X1 + Spec.Y1,
            Spec.SUCCESS_RETURN_VALUE,
            id="x_coordinate_exceeds_n_v2",
        ),
        # Test cases where compute x-coordinate exceeds curve order N,
        # but the signature is invalid.
        # This is a modification of the above test by taking -h, -r, -s
        # what gives the same u1 and u2 and in the result the same point R.
        pytest.param(
            H(Spec.N - Spec.H1.value)
            + R(Spec.N - Spec.R1.value)
            + S(Spec.N - Spec.S1.value)
            + Spec.X1
            + Spec.Y1,
            Spec.INVALID_RETURN_VALUE,
            id="invalid_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=[""])
@EIPChecklist.Precompile.Test.Inputs.Valid()
@EIPChecklist.Precompile.Test.Inputs.Invalid.Crypto()
def test_modular_comparison(
    state_test: StateTestFiller, pre: Alloc, post: dict, tx: Transaction
) -> None:
    """
    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--x_coordinate_exceeds_n 256 bb5a52f42f9c9261ed4361f59422a1e3 0036e7c32b270c8807a419feca605023 00000000000000000000000000000000 4319055358e8617b0c46353d039cdaab ffffffff00000000ffffffffffffffff bce6faada7179e84f3b9cac2fc63254e 0ad99500288d466940031d72a9f5445a 4d43784640855bf0a69874d2de5fe103 c5011e6ef2c42dcd50d5d3d29f99ae6e ba2c80c9244f4c5422f0979ff0c3ba5e 00000000000000000000000000000000 00000000000000000000000000000001
...fork_Osaka-state_test--x_coordinate_exceeds_n_v2 256 532eaabd9574880dbf76b9b8cc00832c 20a6ec113d682299550d7a6e0f345e25 00000000000000000000000000000000 4319055358e8617b0c46353d039cdaab ffffffff00000000ffffffffffffffff bce6faada7179e84f3b9cac2fc63254e d705d16f80987e2d9b1a6957d29ce22f ebf7d10fa515153182415c8361baaca4 b1fc105ee5ce80d514ec1238beae2037 a6f83625593620d460819e8682160926 00000000000000000000000000000000 00000000000000000000000000000001
...fork_Osaka-state_test--invalid_x_coordinate_exceeds_n 256 acd155416a8b77f34089464733ff7cd3 9c400e9c69af7beb9eac5054ed2ec72c ffffffff00000000ffffffffffffffff 79cdf55a4e2f3d09e7739585f8c64aa6 00000000000000000000000000000000 00000000000000000000000000000003 d705d16f80987e2d9b1a6957d29ce22f ebf7d10fa515153182415c8361baaca4 b1fc105ee5ce80d514ec1238beae2037 a6f83625593620d460819e8682160926
...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--x_coordinate_exceeds_n 256 bb5a52f42f9c9261ed4361f59422a1e3 0036e7c32b270c8807a419feca605023 00000000000000000000000000000000 4319055358e8617b0c46353d039cdaab ffffffff00000000ffffffffffffffff bce6faada7179e84f3b9cac2fc63254e 0ad99500288d466940031d72a9f5445a 4d43784640855bf0a69874d2de5fe103 c5011e6ef2c42dcd50d5d3d29f99ae6e ba2c80c9244f4c5422f0979ff0c3ba5e 00000000000000000000000000000000 00000000000000000000000000000001
...fork_Osaka-blockchain_test_from_state_test--x_coordinate_exceeds_n_v2 256 532eaabd9574880dbf76b9b8cc00832c 20a6ec113d682299550d7a6e0f345e25 00000000000000000000000000000000 4319055358e8617b0c46353d039cdaab ffffffff00000000ffffffffffffffff bce6faada7179e84f3b9cac2fc63254e d705d16f80987e2d9b1a6957d29ce22f ebf7d10fa515153182415c8361baaca4 b1fc105ee5ce80d514ec1238beae2037 a6f83625593620d460819e8682160926 00000000000000000000000000000000 00000000000000000000000000000001
...fork_Osaka-blockchain_test_from_state_test--invalid_x_coordinate_exceeds_n 256 acd155416a8b77f34089464733ff7cd3 9c400e9c69af7beb9eac5054ed2ec72c ffffffff00000000ffffffffffffffff 79cdf55a4e2f3d09e7739585f8c64aa6 00000000000000000000000000000000 00000000000000000000000000000003 d705d16f80987e2d9b1a6957d29ce22f ebf7d10fa515153182415c8361baaca4 b1fc105ee5ce80d514ec1238beae2037 a6f83625593620d460819e8682160926
...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