Skip to content

Commit

Permalink
fix(tests): EIP-2537: Update vectors (ethereum#1063)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
2 people authored and fselmo committed Jan 24, 2025
1 parent 3642550 commit 3e60536
Show file tree
Hide file tree
Showing 31 changed files with 977 additions and 906 deletions.
23 changes: 21 additions & 2 deletions tests/prague/eip2537_bls_12_381_precompiles/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,28 @@


@pytest.fixture
def precompile_gas(precompile_address: int, input_data: bytes) -> int:
def vector_gas_value() -> int | None:
"""
Gas value from the test vector if any.
If `None` it means that the test scenario did not come from a file, so no comparison is needed.
The `vectors_from_file` function reads the gas value from the file and overwrites this fixture.
"""
return None


@pytest.fixture
def precompile_gas(
precompile_address: int, input_data: bytes, vector_gas_value: int | None
) -> int:
"""Gas cost for the precompile."""
return GAS_CALCULATION_FUNCTION_MAP[precompile_address](len(input_data))
calculated_gas = GAS_CALCULATION_FUNCTION_MAP[precompile_address](len(input_data))
if vector_gas_value is not None:
assert (
calculated_gas == vector_gas_value
), f"Calculated gas {calculated_gas} != Vector gas {vector_gas_value}"
return calculated_gas


@pytest.fixture
Expand Down
2 changes: 1 addition & 1 deletion tests/prague/eip2537_bls_12_381_precompiles/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Vector(BaseModel):

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


class FailVector(BaseModel):
Expand Down
45 changes: 19 additions & 26 deletions tests/prague/eip2537_bls_12_381_precompiles/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,34 +162,34 @@ class Spec:

# Test constants (from https://github.com/ethereum/bls12-381-tests/tree/eip-2537)
P1 = PointG1( # random point in G1
0x112B98340EEE2777CC3C14163DEA3EC97977AC3DC5C70DA32E6E87578F44912E902CCEF9EFE28D4A78B8999DFBCA9426, # noqa: E501
0x186B28D92356C4DFEC4B5201AD099DBDEDE3781F8998DDF929B4CD7756192185CA7B8F4EF7088F813270AC3D48868A21, # noqa: E501
0x112B98340EEE2777CC3C14163DEA3EC97977AC3DC5C70DA32E6E87578F44912E902CCEF9EFE28D4A78B8999DFBCA9426,
0x186B28D92356C4DFEC4B5201AD099DBDEDE3781F8998DDF929B4CD7756192185CA7B8F4EF7088F813270AC3D48868A21,
)
G1 = PointG1(
0x17F1D3A73197D7942695638C4FA9AC0FC3688C4F9774B905A14E3A3F171BAC586C55E83FF97A1AEFFB3AF00ADB22C6BB, # noqa: E501
0x8B3F481E3AAA0F1A09E30ED741D8AE4FCF5E095D5D00AF600DB18CB2C04B3EDD03CC744A2888AE40CAA232946C5E7E1, # noqa: E501
0x17F1D3A73197D7942695638C4FA9AC0FC3688C4F9774B905A14E3A3F171BAC586C55E83FF97A1AEFFB3AF00ADB22C6BB,
0x8B3F481E3AAA0F1A09E30ED741D8AE4FCF5E095D5D00AF600DB18CB2C04B3EDD03CC744A2888AE40CAA232946C5E7E1,
)
# point at infinity in G1
INF_G1 = PointG1(0, 0)
# random point in G2
P2 = PointG2(
(
0x103121A2CEAAE586D240843A398967325F8EB5A93E8FEA99B62B9F88D8556C80DD726A4B30E84A36EEABAF3592937F27, # noqa: E501
0x86B990F3DA2AEAC0A36143B7D7C824428215140DB1BB859338764CB58458F081D92664F9053B50B3FBD2E4723121B68, # noqa: E501
0x103121A2CEAAE586D240843A398967325F8EB5A93E8FEA99B62B9F88D8556C80DD726A4B30E84A36EEABAF3592937F27,
0x86B990F3DA2AEAC0A36143B7D7C824428215140DB1BB859338764CB58458F081D92664F9053B50B3FBD2E4723121B68,
),
(
0xF9E7BA9A86A8F7624AA2B42DCC8772E1AF4AE115685E60ABC2C9B90242167ACEF3D0BE4050BF935EED7C3B6FC7BA77E, # noqa: E501
0xD22C3652D0DC6F0FC9316E14268477C2049EF772E852108D269D9C38DBA1D4802E8DAE479818184C08F9A569D878451, # noqa: E501
0xF9E7BA9A86A8F7624AA2B42DCC8772E1AF4AE115685E60ABC2C9B90242167ACEF3D0BE4050BF935EED7C3B6FC7BA77E,
0xD22C3652D0DC6F0FC9316E14268477C2049EF772E852108D269D9C38DBA1D4802E8DAE479818184C08F9A569D878451,
),
)
G2 = PointG2(
(
0x24AA2B2F08F0A91260805272DC51051C6E47AD4FA403B02B4510B647AE3D1770BAC0326A805BBEFD48056C8C121BDB8, # noqa: E501
0x13E02B6052719F607DACD3A088274F65596BD0D09920B61AB5DA61BBDC7F5049334CF11213945D57E5AC7D055D042B7E, # noqa: E501
0x24AA2B2F08F0A91260805272DC51051C6E47AD4FA403B02B4510B647AE3D1770BAC0326A805BBEFD48056C8C121BDB8,
0x13E02B6052719F607DACD3A088274F65596BD0D09920B61AB5DA61BBDC7F5049334CF11213945D57E5AC7D055D042B7E,
),
(
0xCE5D527727D6E118CC9CDC6DA2E351AADFD9BAA8CBDD3A76D429A695160D12C923AC9CC3BACA289E193548608B82801, # noqa: E501
0x606C4A02EA734CC32ACD2B02BC28B99CB3E287E85A763AF267492AB572E99AB3F370D275CEC1DA1AAA9075FF05F79BE, # noqa: E501
0xCE5D527727D6E118CC9CDC6DA2E351AADFD9BAA8CBDD3A76D429A695160D12C923AC9CC3BACA289E193548608B82801,
0x606C4A02EA734CC32ACD2B02BC28B99CB3E287E85A763AF267492AB572E99AB3F370D275CEC1DA1AAA9075FF05F79BE,
),
)
# point at infinity in G2
Expand All @@ -203,18 +203,18 @@ class Spec:
P2_NOT_IN_SUBGROUP = PointG2(
(1, 1),
(
0x17FAA6201231304F270B858DAD9462089F2A5B83388E4B10773ABC1EEF6D193B9FCE4E8EA2D9D28E3C3A315AA7DE14CA, # noqa: E501
0xCC12449BE6AC4E7F367E7242250427C4FB4C39325D3164AD397C1837A90F0EA1A534757DF374DD6569345EB41ED76E, # noqa: E501
0x17FAA6201231304F270B858DAD9462089F2A5B83388E4B10773ABC1EEF6D193B9FCE4E8EA2D9D28E3C3A315AA7DE14CA,
0xCC12449BE6AC4E7F367E7242250427C4FB4C39325D3164AD397C1837A90F0EA1A534757DF374DD6569345EB41ED76E,
),
)
P2_NOT_IN_SUBGROUP_TIMES_2 = PointG2(
(
0x919F97860ECC3E933E3477FCAC0E2E4FCC35A6E886E935C97511685232456263DEF6665F143CCCCB44C733333331553, # noqa: E501
0x18B4376B50398178FA8D78ED2654B0FFD2A487BE4DBE6B69086E61B283F4E9D58389CCCB8EDC99995718A66666661555, # noqa: E501
0x919F97860ECC3E933E3477FCAC0E2E4FCC35A6E886E935C97511685232456263DEF6665F143CCCCB44C733333331553,
0x18B4376B50398178FA8D78ED2654B0FFD2A487BE4DBE6B69086E61B283F4E9D58389CCCB8EDC99995718A66666661555,
),
(
0x26898F699C4B07A405AB4183A10B47F923D1C0FDA1018682DD2CCC88968C1B90D44534D6B9270CF57F8DC6D4891678A, # noqa: E501
0x3270414330EAD5EC92219A03A24DFA059DBCBE610868BE1851CC13DAC447F60B40D41113FD007D3307B19ADD4B0F061, # noqa: E501
0x26898F699C4B07A405AB4183A10B47F923D1C0FDA1018682DD2CCC88968C1B90D44534D6B9270CF57F8DC6D4891678A,
0x3270414330EAD5EC92219A03A24DFA059DBCBE610868BE1851CC13DAC447F60B40D41113FD007D3307B19ADD4B0F061,
),
)

Expand Down Expand Up @@ -248,14 +248,7 @@ def msm_discount(group: BLS12Group, k: int) -> int:
def msm_gas_func_gen(
group: BLS12Group, len_per_pair: int, multiplication_cost: int
) -> Callable[[int], int]:
"""
<<<<<<< HEAD
Generate a function that calculates the gas cost for the G1MSM and G2MSM precompiles.
=======
Generate function that calculates the gas cost for the G1MSM and G2MSM
precompiles.
>>>>>>> 4e264c33c6 (chore(ruff): changes to `tests/prague`.)
"""
"""Generate a function that calculates the gas cost for the G1MSM and G2MSM precompiles."""

def msm_gas(input_length: int) -> int:
"""Calculate gas cost for the G1MSM and G2MSM precompiles."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,25 @@


@pytest.mark.parametrize(
"input_data,expected_output",
"input_data,expected_output,vector_gas_value",
vectors_from_file("add_G1_bls.json")
+ [
pytest.param(
Spec.INF_G1 + Spec.INF_G1,
Spec.INF_G1,
None,
id="inf_plus_inf",
),
pytest.param(
Spec.P1_NOT_IN_SUBGROUP + Spec.P1_NOT_IN_SUBGROUP,
Spec.P1_NOT_IN_SUBGROUP_TIMES_2,
None,
id="not_in_subgroup_1",
),
pytest.param(
Spec.P1_NOT_IN_SUBGROUP + Spec.P1_NOT_IN_SUBGROUP_TIMES_2,
Spec.INF_G1,
None,
id="not_in_subgroup_2",
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@


@pytest.mark.parametrize(
"input_data,expected_output",
"input_data,expected_output,vector_gas_value",
vectors_from_file("multiexp_G1_bls.json")
+ [
pytest.param(
(Spec.P1 + Scalar(Spec.Q)) * (len(Spec.G1MSM_DISCOUNT_TABLE) - 1),
Spec.INF_G1,
None,
id="max_discount",
),
pytest.param(
(Spec.P1 + Scalar(Spec.Q)) * len(Spec.G1MSM_DISCOUNT_TABLE),
Spec.INF_G1,
None,
id="max_discount_plus_1",
),
],
Expand Down
10 changes: 9 additions & 1 deletion tests/prague/eip2537_bls_12_381_precompiles/test_bls12_g1mul.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@


@pytest.mark.parametrize(
"input_data,expected_output",
"input_data,expected_output,vector_gas_value",
vectors_from_file("mul_G1_bls.json")
+ [
pytest.param(
Spec.INF_G1 + Scalar(0),
Spec.INF_G1,
None,
id="bls_g1mul_(0*inf=inf)",
),
pytest.param(
Spec.INF_G1 + Scalar(2**256 - 1),
Spec.INF_G1,
None,
id="bls_g1mul_(2**256-1*inf=inf)",
),
pytest.param(
Expand All @@ -40,31 +42,37 @@
0x3DA1F13DDEF2B8B5A46CD543CE56C0A90B8B3B0D6D43DEC95836A5FD2BACD6AA8F692601F870CF22E05DDA5E83F460B, # noqa: E501
0x18D64F3C0E9785365CBDB375795454A8A4FA26F30B9C4F6E33CA078EB5C29B7AEA478B076C619BC1ED22B14C95569B2D, # noqa: E501
),
None,
id="bls_g1mul_(2**256-1*P1)",
),
pytest.param(
Spec.P1 + Scalar(Spec.Q - 1),
-Spec.P1, # negated P1
None,
id="bls_g1mul_(q-1*P1)",
),
pytest.param(
Spec.P1 + Scalar(Spec.Q),
Spec.INF_G1,
None,
id="bls_g1mul_(q*P1)",
),
pytest.param(
Spec.P1 + Scalar(Spec.Q + 1),
Spec.P1,
None,
id="bls_g1mul_(q+1*P1)",
),
pytest.param(
Spec.P1 + Scalar(2 * Spec.Q),
Spec.INF_G1,
None,
id="bls_g1mul_(2q*P1)",
),
pytest.param(
Spec.P1 + Scalar((2**256 // Spec.Q) * Spec.Q),
Spec.INF_G1,
None,
id="bls_g1mul_(Nq*P1)",
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@


@pytest.mark.parametrize(
"input_data,expected_output",
"input_data,expected_output,vector_gas_value",
vectors_from_file("add_G2_bls.json")
+ [
pytest.param(
Spec.P2_NOT_IN_SUBGROUP + Spec.P2_NOT_IN_SUBGROUP,
Spec.P2_NOT_IN_SUBGROUP_TIMES_2,
None,
id="not_in_subgroup",
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@


@pytest.mark.parametrize(
"input_data,expected_output",
"input_data,expected_output,vector_gas_value",
vectors_from_file("multiexp_G2_bls.json")
+ [
pytest.param(
(Spec.P2 + Scalar(Spec.Q)) * (len(Spec.G2MSM_DISCOUNT_TABLE) - 1),
Spec.INF_G2,
None,
id="max_discount",
marks=pytest.mark.slow,
),
pytest.param(
(Spec.P2 + Scalar(Spec.Q)) * len(Spec.G2MSM_DISCOUNT_TABLE),
Spec.INF_G2,
None,
id="max_discount_plus_1",
marks=pytest.mark.slow,
),
Expand Down
11 changes: 10 additions & 1 deletion tests/prague/eip2537_bls_12_381_precompiles/test_bls12_g2mul.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@


@pytest.mark.parametrize(
"input_data,expected_output",
"input_data,expected_output,vector_gas_value",
vectors_from_file("mul_G2_bls.json")
+ [
pytest.param(
Spec.INF_G2 + Scalar(0),
Spec.INF_G2,
None,
id="bls_g2mul_(0*inf=inf)",
),
pytest.param(
Spec.INF_G2 + Scalar(2**256 - 1),
Spec.INF_G2,
None,
id="bls_g2mul_(2**256-1*inf=inf)",
),
pytest.param(
Expand All @@ -46,36 +48,43 @@
0x5397DAD1357CF8333189821B737172B18099ECF7EE8BDB4B3F05EBCCDF40E1782A6C71436D5ACE0843D7F361CBC6DB2, # noqa: E501
),
),
None,
id="bls_g2mul_(2**256-1*P2)",
),
pytest.param(
Spec.P2 + Scalar(Spec.Q - 1),
-Spec.P2, # negated P2
None,
id="bls_g2mul_(q-1*P2)",
),
pytest.param(
Spec.P2 + Scalar(Spec.Q),
Spec.INF_G2,
None,
id="bls_g2mul_(q*P2)",
),
pytest.param(
Spec.G2 + Scalar(Spec.Q),
Spec.INF_G2,
None,
id="bls_g2mul_(q*G2)",
),
pytest.param(
Spec.P2 + Scalar(Spec.Q + 1),
Spec.P2,
None,
id="bls_g2mul_(q+1*P2)",
),
pytest.param(
Spec.P2 + Scalar(2 * Spec.Q),
Spec.INF_G2,
None,
id="bls_g2mul_(2q*P2)",
),
pytest.param(
Spec.P2 + Scalar((2**256 // Spec.Q) * Spec.Q),
Spec.INF_G2,
None,
id="bls_g2mul_(Nq*P2)",
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@


@pytest.mark.parametrize(
"input_data,expected_output",
"input_data,expected_output,vector_gas_value",
vectors_from_file("map_fp2_to_G2_bls.json")
+ [
pytest.param(
FP2((0, 0)),
G2_POINT_ZERO_FP,
None,
id="fp_0",
),
pytest.param(
Expand All @@ -52,6 +53,7 @@
0x171565CE4FCD047B35EA6BCEE4EF6FDBFEC8CC73B7ACDB3A1EC97A776E13ACDFEFFC21ED6648E3F0EEC53DDB6C20FB61, # noqa: E501
),
),
None,
id="fp_p_minus_1",
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@


@pytest.mark.parametrize(
"input_data,expected_output",
"input_data,expected_output,vector_gas_value",
vectors_from_file("map_fp_to_G1_bls.json")
+ [
pytest.param(
FP(0),
G1_POINT_ZERO_FP,
None,
id="fp_0",
),
pytest.param(
Expand All @@ -40,6 +41,7 @@
0x1073311196F8EF19477219CCEE3A48035FF432295AA9419EED45D186027D88B90832E14C4F0E2AA4D15F54D1C3ED0F93, # noqa: E501
0x16B3A3B2E3DDDF6A11459DDAF657FDE21C4F10282A56029D9B55AB3CE1F41E1CF39AD27E0EA35823C7D3250E81FF3D66, # noqa: E501
),
None,
id="fp_p_minus_1",
),
],
Expand Down
Loading

0 comments on commit 3e60536

Please sign in to comment.