8
8
from ethereum_test_tools import Alloc , Environment , StateTestFiller , Transaction
9
9
from ethereum_test_tools import Opcodes as Op
10
10
11
- from .conftest import G1_POINT_NEAR_P , G1_POINT_NEAR_ZERO
11
+ # from .conftest import G1_POINT_NEAR_P, G1_POINT_NEAR_ZERO
12
12
from .helpers import add_points_g1 , vectors_from_file
13
13
from .spec import PointG1 , Spec , ref_spec_2537
14
14
23
23
24
24
@pytest .mark .parametrize (
25
25
"input_data,expected_output,vector_gas_value" ,
26
+ # Test vectors from the reference spec (from the cryptography team)
26
27
vectors_from_file ("add_G1_bls.json" )
27
28
+ [
29
+ # Identity (infinity) element test cases.
30
+ # Checks that any point added to the identity element (INF) equals itself.
31
+ pytest .param (
32
+ Spec .G1 + Spec .INF_G1 ,
33
+ Spec .G1 ,
34
+ None ,
35
+ id = "generator_plus_inf" ,
36
+ ),
37
+ pytest .param (
38
+ Spec .INF_G1 + Spec .G1 ,
39
+ Spec .G1 ,
40
+ None ,
41
+ id = "inf_plus_generator" ,
42
+ ),
28
43
pytest .param (
29
44
Spec .INF_G1 + Spec .INF_G1 ,
30
45
Spec .INF_G1 ,
31
46
None ,
32
47
id = "inf_plus_inf" ,
33
48
),
34
49
pytest .param (
35
- Spec .P1_NOT_IN_SUBGROUP + Spec .P1_NOT_IN_SUBGROUP ,
36
- Spec .P1_NOT_IN_SUBGROUP_TIMES_2 ,
50
+ Spec .INF_G1 + Spec .P1 ,
51
+ Spec .P1 ,
37
52
None ,
38
- id = "not_in_subgroup_1 " ,
53
+ id = "inf_plus_point " ,
39
54
),
55
+ # Basic arithmetic properties test cases.
56
+ # Checks fundamental properties of the BLS12-381 curve.
40
57
pytest .param (
41
- Spec .P1_NOT_IN_SUBGROUP + Spec .P1_NOT_IN_SUBGROUP_TIMES_2 ,
58
+ Spec .P1 + ( - Spec .P1 ) ,
42
59
Spec .INF_G1 ,
43
60
None ,
44
- id = "not_in_subgroup_2 " ,
61
+ id = "point_plus_neg_point " ,
45
62
),
46
63
pytest .param (
47
- Spec .P1 + (- Spec .P1 ),
64
+ Spec .G1 + (- Spec .G1 ),
48
65
Spec .INF_G1 ,
49
66
None ,
50
- id = "point_plus_negative " ,
67
+ id = "generator_plus_neg_point " ,
51
68
),
52
69
pytest .param (
53
70
Spec .P1 + Spec .G1 ,
67
84
None ,
68
85
id = "point_doubling" ,
69
86
),
70
- # Boundary point test cases
87
+ pytest .param ( # (P + G) + P = P + (G + P)
88
+ add_points_g1 (Spec .P1 , Spec .G1 ) + Spec .P1 ,
89
+ add_points_g1 (Spec .P1 , add_points_g1 (Spec .G1 , Spec .P1 )),
90
+ None ,
91
+ id = "associativity_check" ,
92
+ ),
93
+ pytest .param ( # -(P+G) = (-P)+(-G)
94
+ (- (add_points_g1 (Spec .P1 , Spec .G1 ))) + Spec .INF_G1 ,
95
+ add_points_g1 ((- Spec .P1 ), (- Spec .G1 )),
96
+ None ,
97
+ id = "negation_of_sum" ,
98
+ ),
71
99
pytest .param (
72
- G1_POINT_NEAR_P + Spec .INF_G1 ,
73
- G1_POINT_NEAR_P ,
100
+ add_points_g1 ( Spec . G1 , Spec . G1 ) + add_points_g1 ( Spec .P1 , Spec . P1 ) ,
101
+ add_points_g1 ( add_points_g1 ( Spec . G1 , Spec . G1 ), add_points_g1 ( Spec . P1 , Spec . P1 )) ,
74
102
None ,
75
- id = "near_p_boundary_plus_inf " ,
103
+ id = "double_generator_plus_double_point " ,
76
104
),
77
105
pytest .param (
78
- G1_POINT_NEAR_ZERO + Spec .INF_G1 ,
79
- G1_POINT_NEAR_ZERO ,
106
+ add_points_g1 (Spec .G1 , Spec .G1 ) + add_points_g1 (Spec .G1 , Spec .G1 ),
107
+ add_points_g1 (add_points_g1 (Spec .G1 , Spec .G1 ), add_points_g1 (Spec .G1 , Spec .G1 )),
108
+ None ,
109
+ id = "double_generator_plus_double_generator" ,
110
+ ),
111
+ pytest .param ( # (x,y) + (x,-y) = INF
112
+ PointG1 (Spec .P1 .x , Spec .P1 .y ) + PointG1 (Spec .P1 .x , Spec .P - Spec .P1 .y ),
113
+ Spec .INF_G1 ,
80
114
None ,
81
- id = "near_zero_boundary_plus_inf " ,
115
+ id = "point_plus_reflected_point " ,
82
116
),
117
+ # Not in the r-order subgroup test cases.
118
+ # Checks that any point on the curve but not in the subgroup is used for operations.
83
119
pytest .param (
84
- G1_POINT_NEAR_ZERO + G1_POINT_NEAR_P ,
85
- add_points_g1 (G1_POINT_NEAR_ZERO , G1_POINT_NEAR_P ),
120
+ Spec .P1_NOT_IN_SUBGROUP + Spec .P1_NOT_IN_SUBGROUP ,
121
+ Spec .P1_NOT_IN_SUBGROUP_TIMES_2 ,
122
+ None ,
123
+ id = "non_sub_plus_non_sub" ,
124
+ ),
125
+ pytest .param ( # `P1_NOT_IN_SUBGROUP` has an small order subgroup of 3: 3P = INF.
126
+ Spec .P1_NOT_IN_SUBGROUP + Spec .P1_NOT_IN_SUBGROUP_TIMES_2 ,
127
+ Spec .INF_G1 ,
86
128
None ,
87
- id = "near_zero_boundary_plus_near_p_boundary " ,
129
+ id = "non_sub_order_3_to_inf " ,
88
130
),
89
131
pytest .param (
90
- G1_POINT_NEAR_P + G1_POINT_NEAR_P ,
91
- add_points_g1 ( G1_POINT_NEAR_P , G1_POINT_NEAR_P ) ,
132
+ Spec . P1_NOT_IN_SUBGROUP + Spec . INF_G1 ,
133
+ Spec . P1_NOT_IN_SUBGROUP ,
92
134
None ,
93
- id = "doubling_near_p_boundary " ,
135
+ id = "non_sub_plus_inf " ,
94
136
),
95
137
pytest .param (
96
- Spec .G1 + G1_POINT_NEAR_P + G1_POINT_NEAR_ZERO ,
97
- add_points_g1 (add_points_g1 ( Spec .G1 , G1_POINT_NEAR_P ), G1_POINT_NEAR_ZERO ),
138
+ Spec .G1 + Spec . P1_NOT_IN_SUBGROUP ,
139
+ add_points_g1 (Spec .G1 , Spec . P1_NOT_IN_SUBGROUP ),
98
140
None ,
99
- id = "chained_boundary_points " ,
141
+ id = "generator_plus_non_sub " ,
100
142
),
101
143
pytest .param (
102
- G1_POINT_NEAR_P + PointG1 ( G1_POINT_NEAR_P . x , Spec .P - G1_POINT_NEAR_P . y ),
144
+ Spec . P1_NOT_IN_SUBGROUP + ( - Spec .P1_NOT_IN_SUBGROUP ),
103
145
Spec .INF_G1 ,
104
146
None ,
105
- id = "near_p_boundary_plus_its_neg" ,
147
+ id = "non_sub_plus_neg_non_sub" ,
148
+ ),
149
+ pytest .param (
150
+ Spec .P1 + Spec .P1_NOT_IN_SUBGROUP ,
151
+ add_points_g1 (Spec .P1 , Spec .P1_NOT_IN_SUBGROUP ),
152
+ None ,
153
+ id = "in_sub_plus_non_sub" ,
106
154
),
155
+ pytest .param (
156
+ Spec .P1_NOT_IN_SUBGROUP_TIMES_2 + Spec .P1 ,
157
+ add_points_g1 (Spec .P1_NOT_IN_SUBGROUP_TIMES_2 , Spec .P1 ),
158
+ None ,
159
+ id = "doubled_non_sub_plus_in_sub" ,
160
+ ),
161
+ pytest .param (
162
+ Spec .P1_NOT_IN_SUBGROUP_TIMES_2 + (- Spec .P1_NOT_IN_SUBGROUP ),
163
+ Spec .P1_NOT_IN_SUBGROUP ,
164
+ None ,
165
+ id = "doubled_non_sub_plus_neg" ,
166
+ ),
167
+ # Boundary point test cases. TODO: requires generated points
168
+ # pytest.param(
169
+ # G1_POINT_NEAR_P + Spec.INF_G1,
170
+ # G1_POINT_NEAR_P,
171
+ # None,
172
+ # id="near_p_boundary_plus_inf",
173
+ # ),
174
+ # pytest.param(
175
+ # G1_POINT_NEAR_ZERO + Spec.INF_G1,
176
+ # G1_POINT_NEAR_ZERO,
177
+ # None,
178
+ # id="near_zero_boundary_plus_inf",
179
+ # ),
180
+ # pytest.param(
181
+ # G1_POINT_NEAR_ZERO + G1_POINT_NEAR_P,
182
+ # add_points_g1(G1_POINT_NEAR_ZERO, G1_POINT_NEAR_P),
183
+ # None,
184
+ # id="near_zero_boundary_plus_near_p_boundary",
185
+ # ),
186
+ # pytest.param(
187
+ # G1_POINT_NEAR_P + G1_POINT_NEAR_P,
188
+ # add_points_g1(G1_POINT_NEAR_P, G1_POINT_NEAR_P),
189
+ # None,
190
+ # id="doubling_near_p_boundary",
191
+ # ),
192
+ # pytest.param(
193
+ # Spec.G1 + G1_POINT_NEAR_P + G1_POINT_NEAR_ZERO,
194
+ # add_points_g1(add_points_g1(Spec.G1, G1_POINT_NEAR_P), G1_POINT_NEAR_ZERO),
195
+ # None,
196
+ # id="chained_boundary_points",
197
+ # ),
198
+ # pytest.param(
199
+ # G1_POINT_NEAR_P + PointG1(G1_POINT_NEAR_P.x, Spec.P - G1_POINT_NEAR_P.y),
200
+ # Spec.INF_G1,
201
+ # None,
202
+ # id="near_p_boundary_plus_its_neg",
203
+ # ),
107
204
],
108
205
)
109
206
def test_valid (
@@ -123,6 +220,7 @@ def test_valid(
123
220
124
221
@pytest .mark .parametrize (
125
222
"input_data" ,
223
+ # Test vectors from the reference spec (from the cryptography team)
126
224
vectors_from_file ("fail-add_G1_bls.json" )
127
225
+ [
128
226
pytest .param (
@@ -222,13 +320,26 @@ def test_valid(
222
320
id = "swapped_coordinates" ,
223
321
),
224
322
pytest .param (
225
- PointG1 (
226
- Spec .P1 .x ,
227
- Spec .P - Spec .P1 .y , # The other y-coordinate for the same x
228
- )
229
- + Spec .INF_G1 ,
230
- id = "non_canonical_y_coordinate" ,
323
+ b"\x00 " * 96 ,
324
+ id = "all_zero_96_bytes" ,
325
+ ),
326
+ pytest .param (
327
+ b"\xff " + b"\x00 " * 47 + b"\xff " + b"\x00 " * 47 ,
328
+ id = "bad_inf_flag" ,
329
+ ),
330
+ pytest .param (
331
+ b"\xc0 " + b"\x00 " * 47 + b"\xc0 " + b"\x00 " * 47 ,
332
+ id = "comp_instead_of_uncomp" ,
231
333
),
334
+ # TODO: requires generated points
335
+ # pytest.param(
336
+ # PointG1() + Spec.INF_G1,
337
+ # id="random_off_curve_1",
338
+ # ),
339
+ # pytest.param(
340
+ # PointG1() + Spec.INF_G1,
341
+ # id="random_off_curve_2",
342
+ # ),
232
343
],
233
344
)
234
345
@pytest .mark .parametrize ("expected_output" , [Spec .INVALID ], ids = ["" ])
0 commit comments