Skip to content

Commit ca1354d

Browse files
authored
feat(tests): add test cases for P256 input hash special values (#2203)
Make sure a valid P256 signature is correctly verified in the full range of hash values: `0–2**256-1`. Add also tests with invalid signature but with valid inputs (r and s in range, Q on curve).
1 parent e55fbce commit ca1354d

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

tests/osaka/eip7951_p256verify_precompiles/spec.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ class Spec:
9999
B = 0x5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B ## Curve Coefficient
100100
N = 0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551 ## Subgroup Order
101101

102+
Gx = 0x6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296 ## Generator Point X
103+
Gy = 0x4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5 ## Generator Point Y
104+
102105
# Other constants
103106
SUCCESS_RETURN_VALUE = b"\x01".rjust(32, b"\x00")
104107
INVALID_RETURN_VALUE = b""

tests/osaka/eip7951_p256verify_precompiles/test_p256verify.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,50 @@ def test_wycheproof_extra(state_test: StateTestFiller, pre: Alloc, post: dict, t
8989
state_test(env=Environment(), pre=pre, post=post, tx=tx)
9090

9191

92+
@pytest.mark.parametrize(
93+
"input_data",
94+
[
95+
pytest.param(
96+
H(0) + R(Spec.Gx) + S(Spec.Gx) + X(Spec.Gx) + Y(Spec.Gy),
97+
id="hash_0",
98+
),
99+
pytest.param(
100+
H(Spec.N - 1) + R(Spec.Gx) + S(Spec.Gx - 1) + X(Spec.Gx) + Y(Spec.Gy),
101+
id="hash_N_minus_1",
102+
),
103+
pytest.param(
104+
H(Spec.N) + R(Spec.Gx) + S(Spec.Gx) + X(Spec.Gx) + Y(Spec.Gy),
105+
id="hash_N",
106+
),
107+
pytest.param(
108+
H(Spec.P - 1)
109+
+ R(Spec.Gx)
110+
+ S(Spec.Gx + Spec.P - 1 - Spec.N)
111+
+ X(Spec.Gx)
112+
+ Y(Spec.Gy),
113+
id="hash_P_minus_1",
114+
),
115+
pytest.param(
116+
H(Spec.P) + R(Spec.Gx) + S(Spec.Gx + Spec.P - Spec.N) + X(Spec.Gx) + Y(Spec.Gy),
117+
id="hash_P",
118+
),
119+
pytest.param(
120+
H(2**256 - 1)
121+
+ R(Spec.Gx)
122+
+ S(Spec.Gx + 2**256 - 1 - Spec.N)
123+
+ X(Spec.Gx)
124+
+ Y(Spec.Gy),
125+
id="hash_max",
126+
),
127+
],
128+
)
129+
@pytest.mark.parametrize("expected_output", [Spec.SUCCESS_RETURN_VALUE], ids=[""])
130+
@pytest.mark.parametrize("precompile_address", [Spec.P256VERIFY], ids=[""])
131+
def test_valid(state_test: StateTestFiller, pre: Alloc, post: dict, tx: Transaction):
132+
"""Positive tests for the P256VERIFY precompile."""
133+
state_test(env=Environment(), pre=pre, post=post, tx=tx)
134+
135+
92136
@pytest.mark.parametrize(
93137
"input_data",
94138
[
@@ -105,6 +149,30 @@ def test_wycheproof_extra(state_test: StateTestFiller, pre: Alloc, post: dict, t
105149
H(0) + R(0) + S(0) + X(0) + Y(0),
106150
id="input_all_zeros",
107151
),
152+
pytest.param(
153+
H(0) + Spec.R0 + Spec.S0 + Spec.X0 + Spec.Y0,
154+
id="hash_0",
155+
),
156+
pytest.param(
157+
H(Spec.N - 1) + Spec.R0 + Spec.S0 + Spec.X0 + Spec.Y0,
158+
id="hash_N_minus_1",
159+
),
160+
pytest.param(
161+
H(Spec.N) + Spec.R0 + Spec.S0 + Spec.X0 + Spec.Y0,
162+
id="hash_N",
163+
),
164+
pytest.param(
165+
H(Spec.P - 1) + Spec.R0 + Spec.S0 + Spec.X0 + Spec.Y0,
166+
id="hash_P_minus_1",
167+
),
168+
pytest.param(
169+
H(Spec.P) + Spec.R0 + Spec.S0 + Spec.X0 + Spec.Y0,
170+
id="hash_P",
171+
),
172+
pytest.param(
173+
H(2**256 - 1) + Spec.R0 + Spec.S0 + Spec.X0 + Spec.Y0,
174+
id="hash_max",
175+
),
108176
pytest.param(
109177
Spec.H0 + R(0) + Spec.S0 + Spec.X0 + Spec.Y0,
110178
id="r_eq_to_zero",

0 commit comments

Comments
 (0)