Skip to content

Commit

Permalink
feat(tests): EIP-2537: add cross-check to verify JSON gas values
Browse files Browse the repository at this point in the history
  • Loading branch information
marioevz committed Jan 15, 2025
1 parent 2816267 commit debf661
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 12 deletions.
17 changes: 15 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,22 @@


@pytest.fixture
def precompile_gas(precompile_address: int, input_data: bytes) -> int:
def vector_gas_value() -> int | None:
"""Gas value from the test vector."""
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
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 @@ -20,7 +20,10 @@
]


@pytest.mark.parametrize("input_data,expected_output", vectors_from_file("multiexp_G2_bls.json"))
@pytest.mark.parametrize(
"input_data,expected_output,vector_gas_value",
vectors_from_file("multiexp_G2_bls.json"),
)
def test_valid(
state_test: StateTestFiller,
pre: Alloc,
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
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("pairing_check_bls.json")
+ [
pytest.param(
Spec.INF_G1 + Spec.INF_G2,
Spec.PAIRING_TRUE,
None,
id="inf_pair",
),
pytest.param(
(Spec.INF_G1 + Spec.INF_G2) * 1000,
Spec.PAIRING_TRUE,
None,
id="multi_inf_pair",
),
],
Expand Down

0 comments on commit debf661

Please sign in to comment.