Skip to content

Commit ec064ba

Browse files
fix(tests): EIP-2537: Update vectors (ethereum#1063)
* fix(tests): EIP-2537 update vectors * fix(tests): EIP-2537: vector update 2 * feat(tests): EIP-2537: add cross-check to verify JSON gas values * Update tests/prague/eip2537_bls_12_381_precompiles/conftest.py * docs(tests): add source of 2537 JSON test vectors * style(tests): remove superflous line-too-long (E501) pragmas Ruff does not apply E501 to lines that consist of a single "word" * style(tests): clean-up git merge artifact from docstring --------- Co-authored-by: danceratopz <[email protected]>
1 parent b870605 commit ec064ba

31 files changed

+977
-906
lines changed

prague/eip2537_bls_12_381_precompiles/conftest.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,28 @@
1111

1212

1313
@pytest.fixture
14-
def precompile_gas(precompile_address: int, input_data: bytes) -> int:
14+
def vector_gas_value() -> int | None:
15+
"""
16+
Gas value from the test vector if any.
17+
18+
If `None` it means that the test scenario did not come from a file, so no comparison is needed.
19+
20+
The `vectors_from_file` function reads the gas value from the file and overwrites this fixture.
21+
"""
22+
return None
23+
24+
25+
@pytest.fixture
26+
def precompile_gas(
27+
precompile_address: int, input_data: bytes, vector_gas_value: int | None
28+
) -> int:
1529
"""Gas cost for the precompile."""
16-
return GAS_CALCULATION_FUNCTION_MAP[precompile_address](len(input_data))
30+
calculated_gas = GAS_CALCULATION_FUNCTION_MAP[precompile_address](len(input_data))
31+
if vector_gas_value is not None:
32+
assert (
33+
calculated_gas == vector_gas_value
34+
), f"Calculated gas {calculated_gas} != Vector gas {vector_gas_value}"
35+
return calculated_gas
1736

1837

1938
@pytest.fixture

prague/eip2537_bls_12_381_precompiles/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Vector(BaseModel):
2525

2626
def to_pytest_param(self):
2727
"""Convert the test vector to a tuple that can be used as a parameter in a pytest test."""
28-
return pytest.param(self.input, self.expected, id=self.name)
28+
return pytest.param(self.input, self.expected, self.gas, id=self.name)
2929

3030

3131
class FailVector(BaseModel):

prague/eip2537_bls_12_381_precompiles/spec.py

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -162,34 +162,34 @@ class Spec:
162162

163163
# Test constants (from https://github.com/ethereum/bls12-381-tests/tree/eip-2537)
164164
P1 = PointG1( # random point in G1
165-
0x112B98340EEE2777CC3C14163DEA3EC97977AC3DC5C70DA32E6E87578F44912E902CCEF9EFE28D4A78B8999DFBCA9426, # noqa: E501
166-
0x186B28D92356C4DFEC4B5201AD099DBDEDE3781F8998DDF929B4CD7756192185CA7B8F4EF7088F813270AC3D48868A21, # noqa: E501
165+
0x112B98340EEE2777CC3C14163DEA3EC97977AC3DC5C70DA32E6E87578F44912E902CCEF9EFE28D4A78B8999DFBCA9426,
166+
0x186B28D92356C4DFEC4B5201AD099DBDEDE3781F8998DDF929B4CD7756192185CA7B8F4EF7088F813270AC3D48868A21,
167167
)
168168
G1 = PointG1(
169-
0x17F1D3A73197D7942695638C4FA9AC0FC3688C4F9774B905A14E3A3F171BAC586C55E83FF97A1AEFFB3AF00ADB22C6BB, # noqa: E501
170-
0x8B3F481E3AAA0F1A09E30ED741D8AE4FCF5E095D5D00AF600DB18CB2C04B3EDD03CC744A2888AE40CAA232946C5E7E1, # noqa: E501
169+
0x17F1D3A73197D7942695638C4FA9AC0FC3688C4F9774B905A14E3A3F171BAC586C55E83FF97A1AEFFB3AF00ADB22C6BB,
170+
0x8B3F481E3AAA0F1A09E30ED741D8AE4FCF5E095D5D00AF600DB18CB2C04B3EDD03CC744A2888AE40CAA232946C5E7E1,
171171
)
172172
# point at infinity in G1
173173
INF_G1 = PointG1(0, 0)
174174
# random point in G2
175175
P2 = PointG2(
176176
(
177-
0x103121A2CEAAE586D240843A398967325F8EB5A93E8FEA99B62B9F88D8556C80DD726A4B30E84A36EEABAF3592937F27, # noqa: E501
178-
0x86B990F3DA2AEAC0A36143B7D7C824428215140DB1BB859338764CB58458F081D92664F9053B50B3FBD2E4723121B68, # noqa: E501
177+
0x103121A2CEAAE586D240843A398967325F8EB5A93E8FEA99B62B9F88D8556C80DD726A4B30E84A36EEABAF3592937F27,
178+
0x86B990F3DA2AEAC0A36143B7D7C824428215140DB1BB859338764CB58458F081D92664F9053B50B3FBD2E4723121B68,
179179
),
180180
(
181-
0xF9E7BA9A86A8F7624AA2B42DCC8772E1AF4AE115685E60ABC2C9B90242167ACEF3D0BE4050BF935EED7C3B6FC7BA77E, # noqa: E501
182-
0xD22C3652D0DC6F0FC9316E14268477C2049EF772E852108D269D9C38DBA1D4802E8DAE479818184C08F9A569D878451, # noqa: E501
181+
0xF9E7BA9A86A8F7624AA2B42DCC8772E1AF4AE115685E60ABC2C9B90242167ACEF3D0BE4050BF935EED7C3B6FC7BA77E,
182+
0xD22C3652D0DC6F0FC9316E14268477C2049EF772E852108D269D9C38DBA1D4802E8DAE479818184C08F9A569D878451,
183183
),
184184
)
185185
G2 = PointG2(
186186
(
187-
0x24AA2B2F08F0A91260805272DC51051C6E47AD4FA403B02B4510B647AE3D1770BAC0326A805BBEFD48056C8C121BDB8, # noqa: E501
188-
0x13E02B6052719F607DACD3A088274F65596BD0D09920B61AB5DA61BBDC7F5049334CF11213945D57E5AC7D055D042B7E, # noqa: E501
187+
0x24AA2B2F08F0A91260805272DC51051C6E47AD4FA403B02B4510B647AE3D1770BAC0326A805BBEFD48056C8C121BDB8,
188+
0x13E02B6052719F607DACD3A088274F65596BD0D09920B61AB5DA61BBDC7F5049334CF11213945D57E5AC7D055D042B7E,
189189
),
190190
(
191-
0xCE5D527727D6E118CC9CDC6DA2E351AADFD9BAA8CBDD3A76D429A695160D12C923AC9CC3BACA289E193548608B82801, # noqa: E501
192-
0x606C4A02EA734CC32ACD2B02BC28B99CB3E287E85A763AF267492AB572E99AB3F370D275CEC1DA1AAA9075FF05F79BE, # noqa: E501
191+
0xCE5D527727D6E118CC9CDC6DA2E351AADFD9BAA8CBDD3A76D429A695160D12C923AC9CC3BACA289E193548608B82801,
192+
0x606C4A02EA734CC32ACD2B02BC28B99CB3E287E85A763AF267492AB572E99AB3F370D275CEC1DA1AAA9075FF05F79BE,
193193
),
194194
)
195195
# point at infinity in G2
@@ -203,18 +203,18 @@ class Spec:
203203
P2_NOT_IN_SUBGROUP = PointG2(
204204
(1, 1),
205205
(
206-
0x17FAA6201231304F270B858DAD9462089F2A5B83388E4B10773ABC1EEF6D193B9FCE4E8EA2D9D28E3C3A315AA7DE14CA, # noqa: E501
207-
0xCC12449BE6AC4E7F367E7242250427C4FB4C39325D3164AD397C1837A90F0EA1A534757DF374DD6569345EB41ED76E, # noqa: E501
206+
0x17FAA6201231304F270B858DAD9462089F2A5B83388E4B10773ABC1EEF6D193B9FCE4E8EA2D9D28E3C3A315AA7DE14CA,
207+
0xCC12449BE6AC4E7F367E7242250427C4FB4C39325D3164AD397C1837A90F0EA1A534757DF374DD6569345EB41ED76E,
208208
),
209209
)
210210
P2_NOT_IN_SUBGROUP_TIMES_2 = PointG2(
211211
(
212-
0x919F97860ECC3E933E3477FCAC0E2E4FCC35A6E886E935C97511685232456263DEF6665F143CCCCB44C733333331553, # noqa: E501
213-
0x18B4376B50398178FA8D78ED2654B0FFD2A487BE4DBE6B69086E61B283F4E9D58389CCCB8EDC99995718A66666661555, # noqa: E501
212+
0x919F97860ECC3E933E3477FCAC0E2E4FCC35A6E886E935C97511685232456263DEF6665F143CCCCB44C733333331553,
213+
0x18B4376B50398178FA8D78ED2654B0FFD2A487BE4DBE6B69086E61B283F4E9D58389CCCB8EDC99995718A66666661555,
214214
),
215215
(
216-
0x26898F699C4B07A405AB4183A10B47F923D1C0FDA1018682DD2CCC88968C1B90D44534D6B9270CF57F8DC6D4891678A, # noqa: E501
217-
0x3270414330EAD5EC92219A03A24DFA059DBCBE610868BE1851CC13DAC447F60B40D41113FD007D3307B19ADD4B0F061, # noqa: E501
216+
0x26898F699C4B07A405AB4183A10B47F923D1C0FDA1018682DD2CCC88968C1B90D44534D6B9270CF57F8DC6D4891678A,
217+
0x3270414330EAD5EC92219A03A24DFA059DBCBE610868BE1851CC13DAC447F60B40D41113FD007D3307B19ADD4B0F061,
218218
),
219219
)
220220

@@ -248,14 +248,7 @@ def msm_discount(group: BLS12Group, k: int) -> int:
248248
def msm_gas_func_gen(
249249
group: BLS12Group, len_per_pair: int, multiplication_cost: int
250250
) -> Callable[[int], int]:
251-
"""
252-
<<<<<<< HEAD
253-
Generate a function that calculates the gas cost for the G1MSM and G2MSM precompiles.
254-
=======
255-
Generate function that calculates the gas cost for the G1MSM and G2MSM
256-
precompiles.
257-
>>>>>>> 4e264c33c6 (chore(ruff): changes to `tests/prague`.)
258-
"""
251+
"""Generate a function that calculates the gas cost for the G1MSM and G2MSM precompiles."""
259252

260253
def msm_gas(input_length: int) -> int:
261254
"""Calculate gas cost for the G1MSM and G2MSM precompiles."""

prague/eip2537_bls_12_381_precompiles/test_bls12_g1add.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,25 @@
2121

2222

2323
@pytest.mark.parametrize(
24-
"input_data,expected_output",
24+
"input_data,expected_output,vector_gas_value",
2525
vectors_from_file("add_G1_bls.json")
2626
+ [
2727
pytest.param(
2828
Spec.INF_G1 + Spec.INF_G1,
2929
Spec.INF_G1,
30+
None,
3031
id="inf_plus_inf",
3132
),
3233
pytest.param(
3334
Spec.P1_NOT_IN_SUBGROUP + Spec.P1_NOT_IN_SUBGROUP,
3435
Spec.P1_NOT_IN_SUBGROUP_TIMES_2,
36+
None,
3537
id="not_in_subgroup_1",
3638
),
3739
pytest.param(
3840
Spec.P1_NOT_IN_SUBGROUP + Spec.P1_NOT_IN_SUBGROUP_TIMES_2,
3941
Spec.INF_G1,
42+
None,
4043
id="not_in_subgroup_2",
4144
),
4245
],

prague/eip2537_bls_12_381_precompiles/test_bls12_g1msm.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@
2121

2222

2323
@pytest.mark.parametrize(
24-
"input_data,expected_output",
24+
"input_data,expected_output,vector_gas_value",
2525
vectors_from_file("multiexp_G1_bls.json")
2626
+ [
2727
pytest.param(
2828
(Spec.P1 + Scalar(Spec.Q)) * (len(Spec.G1MSM_DISCOUNT_TABLE) - 1),
2929
Spec.INF_G1,
30+
None,
3031
id="max_discount",
3132
),
3233
pytest.param(
3334
(Spec.P1 + Scalar(Spec.Q)) * len(Spec.G1MSM_DISCOUNT_TABLE),
3435
Spec.INF_G1,
36+
None,
3537
id="max_discount_plus_1",
3638
),
3739
],

prague/eip2537_bls_12_381_precompiles/test_bls12_g1mul.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@
2121

2222

2323
@pytest.mark.parametrize(
24-
"input_data,expected_output",
24+
"input_data,expected_output,vector_gas_value",
2525
vectors_from_file("mul_G1_bls.json")
2626
+ [
2727
pytest.param(
2828
Spec.INF_G1 + Scalar(0),
2929
Spec.INF_G1,
30+
None,
3031
id="bls_g1mul_(0*inf=inf)",
3132
),
3233
pytest.param(
3334
Spec.INF_G1 + Scalar(2**256 - 1),
3435
Spec.INF_G1,
36+
None,
3537
id="bls_g1mul_(2**256-1*inf=inf)",
3638
),
3739
pytest.param(
@@ -40,31 +42,37 @@
4042
0x3DA1F13DDEF2B8B5A46CD543CE56C0A90B8B3B0D6D43DEC95836A5FD2BACD6AA8F692601F870CF22E05DDA5E83F460B, # noqa: E501
4143
0x18D64F3C0E9785365CBDB375795454A8A4FA26F30B9C4F6E33CA078EB5C29B7AEA478B076C619BC1ED22B14C95569B2D, # noqa: E501
4244
),
45+
None,
4346
id="bls_g1mul_(2**256-1*P1)",
4447
),
4548
pytest.param(
4649
Spec.P1 + Scalar(Spec.Q - 1),
4750
-Spec.P1, # negated P1
51+
None,
4852
id="bls_g1mul_(q-1*P1)",
4953
),
5054
pytest.param(
5155
Spec.P1 + Scalar(Spec.Q),
5256
Spec.INF_G1,
57+
None,
5358
id="bls_g1mul_(q*P1)",
5459
),
5560
pytest.param(
5661
Spec.P1 + Scalar(Spec.Q + 1),
5762
Spec.P1,
63+
None,
5864
id="bls_g1mul_(q+1*P1)",
5965
),
6066
pytest.param(
6167
Spec.P1 + Scalar(2 * Spec.Q),
6268
Spec.INF_G1,
69+
None,
6370
id="bls_g1mul_(2q*P1)",
6471
),
6572
pytest.param(
6673
Spec.P1 + Scalar((2**256 // Spec.Q) * Spec.Q),
6774
Spec.INF_G1,
75+
None,
6876
id="bls_g1mul_(Nq*P1)",
6977
),
7078
],

prague/eip2537_bls_12_381_precompiles/test_bls12_g2add.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@
2121

2222

2323
@pytest.mark.parametrize(
24-
"input_data,expected_output",
24+
"input_data,expected_output,vector_gas_value",
2525
vectors_from_file("add_G2_bls.json")
2626
+ [
2727
pytest.param(
2828
Spec.P2_NOT_IN_SUBGROUP + Spec.P2_NOT_IN_SUBGROUP,
2929
Spec.P2_NOT_IN_SUBGROUP_TIMES_2,
30+
None,
3031
id="not_in_subgroup",
3132
),
3233
],

prague/eip2537_bls_12_381_precompiles/test_bls12_g2msm.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,20 @@
2121

2222

2323
@pytest.mark.parametrize(
24-
"input_data,expected_output",
24+
"input_data,expected_output,vector_gas_value",
2525
vectors_from_file("multiexp_G2_bls.json")
2626
+ [
2727
pytest.param(
2828
(Spec.P2 + Scalar(Spec.Q)) * (len(Spec.G2MSM_DISCOUNT_TABLE) - 1),
2929
Spec.INF_G2,
30+
None,
3031
id="max_discount",
3132
marks=pytest.mark.slow,
3233
),
3334
pytest.param(
3435
(Spec.P2 + Scalar(Spec.Q)) * len(Spec.G2MSM_DISCOUNT_TABLE),
3536
Spec.INF_G2,
37+
None,
3638
id="max_discount_plus_1",
3739
marks=pytest.mark.slow,
3840
),

prague/eip2537_bls_12_381_precompiles/test_bls12_g2mul.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@
2121

2222

2323
@pytest.mark.parametrize(
24-
"input_data,expected_output",
24+
"input_data,expected_output,vector_gas_value",
2525
vectors_from_file("mul_G2_bls.json")
2626
+ [
2727
pytest.param(
2828
Spec.INF_G2 + Scalar(0),
2929
Spec.INF_G2,
30+
None,
3031
id="bls_g2mul_(0*inf=inf)",
3132
),
3233
pytest.param(
3334
Spec.INF_G2 + Scalar(2**256 - 1),
3435
Spec.INF_G2,
36+
None,
3537
id="bls_g2mul_(2**256-1*inf=inf)",
3638
),
3739
pytest.param(
@@ -46,36 +48,43 @@
4648
0x5397DAD1357CF8333189821B737172B18099ECF7EE8BDB4B3F05EBCCDF40E1782A6C71436D5ACE0843D7F361CBC6DB2, # noqa: E501
4749
),
4850
),
51+
None,
4952
id="bls_g2mul_(2**256-1*P2)",
5053
),
5154
pytest.param(
5255
Spec.P2 + Scalar(Spec.Q - 1),
5356
-Spec.P2, # negated P2
57+
None,
5458
id="bls_g2mul_(q-1*P2)",
5559
),
5660
pytest.param(
5761
Spec.P2 + Scalar(Spec.Q),
5862
Spec.INF_G2,
63+
None,
5964
id="bls_g2mul_(q*P2)",
6065
),
6166
pytest.param(
6267
Spec.G2 + Scalar(Spec.Q),
6368
Spec.INF_G2,
69+
None,
6470
id="bls_g2mul_(q*G2)",
6571
),
6672
pytest.param(
6773
Spec.P2 + Scalar(Spec.Q + 1),
6874
Spec.P2,
75+
None,
6976
id="bls_g2mul_(q+1*P2)",
7077
),
7178
pytest.param(
7279
Spec.P2 + Scalar(2 * Spec.Q),
7380
Spec.INF_G2,
81+
None,
7482
id="bls_g2mul_(2q*P2)",
7583
),
7684
pytest.param(
7785
Spec.P2 + Scalar((2**256 // Spec.Q) * Spec.Q),
7886
Spec.INF_G2,
87+
None,
7988
id="bls_g2mul_(Nq*P2)",
8089
),
8190
],

prague/eip2537_bls_12_381_precompiles/test_bls12_map_fp2_to_g2.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@
3232

3333

3434
@pytest.mark.parametrize(
35-
"input_data,expected_output",
35+
"input_data,expected_output,vector_gas_value",
3636
vectors_from_file("map_fp2_to_G2_bls.json")
3737
+ [
3838
pytest.param(
3939
FP2((0, 0)),
4040
G2_POINT_ZERO_FP,
41+
None,
4142
id="fp_0",
4243
),
4344
pytest.param(
@@ -52,6 +53,7 @@
5253
0x171565CE4FCD047B35EA6BCEE4EF6FDBFEC8CC73B7ACDB3A1EC97A776E13ACDFEFFC21ED6648E3F0EEC53DDB6C20FB61, # noqa: E501
5354
),
5455
),
56+
None,
5557
id="fp_p_minus_1",
5658
),
5759
],

0 commit comments

Comments
 (0)