Skip to content

Commit dd5efdd

Browse files
spencer-tbmarioevz
andauthored
feat(tests): add bls top bytes boundary edge cases (#2085)
* feat(tests): add bls top bytes boundary edge cases. * Use `MAX_FP_BIT_SET` constant and add more cases --------- Co-authored-by: Mario Vega <[email protected]>
1 parent 058baa6 commit dd5efdd

File tree

9 files changed

+90
-0
lines changed

9 files changed

+90
-0
lines changed

tests/prague/eip2537_bls_12_381_precompiles/spec.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ class Spec:
135135
P = (X - 1) ** 2 * Q // 3 + X
136136
LEN_PER_PAIR = len(PointG1() + PointG2())
137137
MSM_MULTIPLIER = 1_000
138+
MAX_FP_BIT_SET = 1 << (48 * 8)
138139
# fmt: off
139140
G1MSM_DISCOUNT_TABLE = [
140141
0,

tests/prague/eip2537_bls_12_381_precompiles/test_bls12_g1add.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,14 @@ def test_valid(
341341
b"\xc0" + b"\x00" * 47 + b"\xc0" + b"\x00" * 47,
342342
id="comp_instead_of_uncomp",
343343
),
344+
pytest.param(
345+
PointG1(Spec.P1.x | Spec.MAX_FP_BIT_SET, Spec.P1.y) + Spec.P1,
346+
id="non_zero_byte_16_boundary_violation_x",
347+
),
348+
pytest.param(
349+
PointG1(Spec.P1.x, Spec.P1.y | Spec.MAX_FP_BIT_SET) + Spec.P1,
350+
id="non_zero_byte_16_boundary_violation_y",
351+
),
344352
# Not on the curve cases using random generated points.
345353
pytest.param(
346354
G1_POINTS_NOT_ON_CURVE[0] + Spec.INF_G1,

tests/prague/eip2537_bls_12_381_precompiles/test_bls12_g1mul.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,14 @@ def test_valid(
249249
Scalar(1) + Scalar(1),
250250
id="two_scalars",
251251
),
252+
pytest.param(
253+
PointG1(Spec.P1.x | Spec.MAX_FP_BIT_SET, Spec.P1.y) + Scalar(1),
254+
id="non_zero_byte_16_boundary_violation_x",
255+
),
256+
pytest.param(
257+
PointG1(Spec.P1.x, Spec.P1.y | Spec.MAX_FP_BIT_SET) + Scalar(1),
258+
id="non_zero_byte_16_boundary_violation_y",
259+
),
252260
# Not in the r-order subgroup test cases.
253261
pytest.param(
254262
Spec.P1_NOT_IN_SUBGROUP + Scalar(1),

tests/prague/eip2537_bls_12_381_precompiles/test_bls12_g2add.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,22 @@ def test_valid(
365365
bytes(Spec.G2) + bytes(Spec.G2)[128:],
366366
id="mixed_g1_g2_points",
367367
),
368+
pytest.param(
369+
PointG2((Spec.P2.x[0] | Spec.MAX_FP_BIT_SET, Spec.P2.x[1]), Spec.P2.y) + Spec.P2,
370+
id="non_zero_byte_16_boundary_violation_x1",
371+
),
372+
pytest.param(
373+
PointG2((Spec.P2.x[0], Spec.P2.x[1] | Spec.MAX_FP_BIT_SET), Spec.P2.y) + Spec.P2,
374+
id="non_zero_byte_16_boundary_violation_x2",
375+
),
376+
pytest.param(
377+
PointG2(Spec.P2.x, (Spec.P2.y[0] | Spec.MAX_FP_BIT_SET, Spec.P2.y[1])) + Spec.P2,
378+
id="non_zero_byte_16_boundary_violation_y1",
379+
),
380+
pytest.param(
381+
PointG2(Spec.P2.x, (Spec.P2.y[0], Spec.P2.y[1] | Spec.MAX_FP_BIT_SET)) + Spec.P2,
382+
id="non_zero_byte_16_boundary_violation_y2",
383+
),
368384
# Not on the curve cases using random generated points.
369385
pytest.param(
370386
G2_POINTS_NOT_ON_CURVE[0] + Spec.INF_G2,

tests/prague/eip2537_bls_12_381_precompiles/test_bls12_g2mul.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,22 @@ def test_valid(
267267
bytes(Spec.G2) + bytes(Scalar(0))[128:],
268268
id="mixed_g2_scalar_truncated",
269269
),
270+
pytest.param(
271+
PointG2((Spec.P2.x[0] | Spec.MAX_FP_BIT_SET, Spec.P2.x[1]), Spec.P2.y) + Scalar(1),
272+
id="non_zero_byte_16_boundary_violation_x1",
273+
),
274+
pytest.param(
275+
PointG2((Spec.P2.x[0], Spec.P2.x[1] | Spec.MAX_FP_BIT_SET), Spec.P2.y) + Scalar(1),
276+
id="non_zero_byte_16_boundary_violation_x2",
277+
),
278+
pytest.param(
279+
PointG2(Spec.P2.x, (Spec.P2.y[0] | Spec.MAX_FP_BIT_SET, Spec.P2.y[1])) + Scalar(1),
280+
id="non_zero_byte_16_boundary_violation_y1",
281+
),
282+
pytest.param(
283+
PointG2(Spec.P2.x, (Spec.P2.y[0], Spec.P2.y[1] | Spec.MAX_FP_BIT_SET)) + Scalar(1),
284+
id="non_zero_byte_16_boundary_violation_y2",
285+
),
270286
# Not in the r-order subgroup test cases.
271287
pytest.param(
272288
Spec.P2_NOT_IN_SUBGROUP + Scalar(0),

tests/prague/eip2537_bls_12_381_precompiles/test_bls12_map_fp2_to_g2.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ def test_isogeny_kernel_values(
142142
pytest.param(bytes(FP2((0, 0)))[:95], id="fp2_one_byte_short"),
143143
pytest.param(bytes([0xFF]) + bytes(FP2((0, 0)))[1:], id="fp2_invalid_first_byte"),
144144
pytest.param(Spec.INF_G2, id="g2_inf_input"),
145+
pytest.param(
146+
FP2(((Spec.P - 1) | Spec.MAX_FP_BIT_SET, Spec.P - 1)),
147+
id="non_zero_byte_16_boundary_violation_c0",
148+
),
149+
pytest.param(
150+
FP2((Spec.P - 1, (Spec.P - 1) | Spec.MAX_FP_BIT_SET)),
151+
id="non_zero_byte_16_boundary_violation_c1",
152+
),
145153
],
146154
)
147155
@pytest.mark.parametrize("expected_output", [Spec.INVALID], ids=[""])

tests/prague/eip2537_bls_12_381_precompiles/test_bls12_map_fp_to_g1.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ def test_isogeny_kernel_values(
121121
pytest.param(bytes(FP(0))[:47], id="fp_one_byte_short"),
122122
pytest.param(bytes([0xFF]) + bytes(FP(0))[1:], id="fp_invalid_first_byte"),
123123
pytest.param(Spec.INF_G1, id="g1_inf_input"),
124+
pytest.param(
125+
FP((Spec.P - 1) | Spec.MAX_FP_BIT_SET), id="non_zero_byte_16_boundary_violation"
126+
),
124127
],
125128
)
126129
@pytest.mark.parametrize("expected_output", [Spec.INVALID], ids=[""])

tests/prague/eip2537_bls_12_381_precompiles/test_bls12_pairing.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,31 @@ def test_valid_multi_inf(
292292
Spec.INF_G1 + PointG2(Spec.P2.x, (Spec.P2.y[0], Spec.P2.y[1] + Spec.P)),
293293
id="inf_g1_with_g2_y_c1_above_p",
294294
),
295+
# Non-zero byte 16 boundary violation test cases.
296+
pytest.param(
297+
PointG1(Spec.G1.x | Spec.MAX_FP_BIT_SET, Spec.G1.y) + Spec.INF_G2,
298+
id="non_zero_byte_16_boundary_violation_g1_x",
299+
),
300+
pytest.param(
301+
PointG1(Spec.G1.x, Spec.G1.y | Spec.MAX_FP_BIT_SET) + Spec.INF_G2,
302+
id="non_zero_byte_16_boundary_violation_g1_y",
303+
),
304+
pytest.param(
305+
Spec.INF_G1 + PointG2((Spec.G2.x[0] | Spec.MAX_FP_BIT_SET, Spec.G2.x[1]), Spec.G2.y),
306+
id="non_zero_byte_16_boundary_violation_g1_x1",
307+
),
308+
pytest.param(
309+
Spec.INF_G1 + PointG2((Spec.G2.x[0], Spec.G2.x[1] | Spec.MAX_FP_BIT_SET), Spec.G2.y),
310+
id="non_zero_byte_16_boundary_violation_g1_x2",
311+
),
312+
pytest.param(
313+
Spec.INF_G1 + PointG2(Spec.G2.x, (Spec.G2.y[0] | Spec.MAX_FP_BIT_SET, Spec.G2.y[1])),
314+
id="non_zero_byte_16_boundary_violation_g1_y1",
315+
),
316+
pytest.param(
317+
Spec.INF_G1 + PointG2(Spec.G2.x, (Spec.G2.y[0], Spec.G2.y[1] | Spec.MAX_FP_BIT_SET)),
318+
id="non_zero_byte_16_boundary_violation_g1_y2",
319+
),
295320
],
296321
)
297322
@pytest.mark.parametrize("expected_output", [Spec.INVALID], ids=[""])

tests/prague/eip2537_bls_12_381_precompiles/vectors/fail-add_G1_bls.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,10 @@
2828
"Input": "1000000000000000000000000000000017f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb0000000000000000000000000000000008b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e100000000000000000000000000000000112b98340eee2777cc3c14163dea3ec97977ac3dc5c70da32e6e87578f44912e902ccef9efe28d4a78b8999dfbca942600000000000000000000000000000000186b28d92356c4dfec4b5201ad099dbdede3781f8998ddf929b4cd7756192185ca7b8f4ef7088f813270ac3d48868a21",
2929
"ExpectedError": "invalid field element top bytes",
3030
"Name": "bls_g1add_violate_top_bytes"
31+
},
32+
{
33+
"Input": "0000000000000000000000000000000017f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb0000000000000000000000000000000108b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e10000000000000000000000000000000017f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb0000000000000000000000000000000008b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1",
34+
"ExpectedError": "invalid field element top bytes",
35+
"Name": "bls_g1add_top16_bytes_boundary_violation"
3136
}
3237
]

0 commit comments

Comments
 (0)