-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new(tests): EIP-2537: BLS12-381 Precompiles (#499)
* new(tests): Add eip-2537 vectors * new(tests): eip-2537 * refactor(tests): eip2537 * new(tests): more eip2537 tests * fix(tests): typos * fix(tests): eip2537: fix k length msm g1 * refactor(tests): eip-2537 * new(tests): eip-2537: multiplication gas tests * nit(tests): eip-2537: function names * fix(tests): eip2537: unused imports * new(tests): eip2537: new msm gas tests * refactor(tests): eip2537 * refactor(tests): eip2537 spec * fix(tests): eip2537, add more tests * fix(tests): eip2537: format vector * fix(tests): eip2537: update vectors from https://github.com/ethereum/bls12-381-tests/tree/eip-2537 * fix(tests): eip2537: conditional integrity check * refactor(tests): eip2537: spec * new(tests): eip2537: pairing tests * fix(tests): eip2537: spec * fix(tests): eip2537: use `Spec.INVALID` constant * new(tests): eip2537: pairing tests * new(tests): eip2537: pairing invalid gas/length tests * new(tests): eip2537: invalid encoding, G1 add, mul, msm, not in subgroup point * new(tests): eip2537: call type tests * new(tests): eip2537: g2 not in subgroup * new(tests): eip2537: incorrect subgroup tests * changelog * fix: tox * fix(tests): eip2537: comments * fix(tests): eip2537: github reference spec hash * fix(tests): eip-2537: refresh vectors * fix(tests): eip-2537: move subgroup tests valid -> invalid * fix(tests): eip-2537: load failing test vectors * feat(forks): Add BLS precompiles to Prague * fix(tests): EIP-2537: spec reference hash update * feat(tests): eip-2537: Add inputs to mul precompiles * new(tests): eip-2537: more tests * fix(tests): hardcode fork name * fix(tests): EIP-2537: load MSM table from file * new(tests): EIP-2537: test precompiles before fork is active
- Loading branch information
Showing
38 changed files
with
3,772 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
""" | ||
abstract: Tests [EIP-2537: Precompile for BLS12-381 curve operations](https://eips.ethereum.org/EIPS/eip-2537) | ||
Tests for [EIP-2537: Precompile for BLS12-381 curve operations](https://eips.ethereum.org/EIPS/eip-2537). | ||
""" # noqa: E501 |
177 changes: 177 additions & 0 deletions
177
tests/prague/eip2537_bls_12_381_precompiles/conftest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
""" | ||
Shared pytest definitions local to EIP-2537 tests. | ||
""" | ||
from typing import SupportsBytes | ||
|
||
import pytest | ||
from ethereum.crypto.hash import keccak256 | ||
|
||
from ethereum_test_tools import Storage, TestAddress, Transaction | ||
from ethereum_test_tools.vm import Opcodes as Op | ||
|
||
from .spec import GAS_CALCULATION_FUNCTION_MAP | ||
|
||
|
||
@pytest.fixture | ||
def precompile_gas(precompile_address: int, input: bytes) -> int: | ||
"""Gas cost for the precompile.""" | ||
return GAS_CALCULATION_FUNCTION_MAP[precompile_address](len(input)) | ||
|
||
|
||
@pytest.fixture | ||
def precompile_gas_modifier() -> int: | ||
""" | ||
Used to modify the gas passed to the precompile, for testing purposes. | ||
By default the call is made with the exact gas amount required for the given opcode, | ||
but when this fixture is overridden, the gas amount can be modified to, e.g., test | ||
a lower amount and test if the precompile call fails. | ||
""" | ||
return 0 | ||
|
||
|
||
@pytest.fixture | ||
def call_opcode() -> Op: | ||
""" | ||
Type of call used to call the precompile. | ||
By default it is Op.CALL, but it can be overridden in the test. | ||
""" | ||
return Op.CALL | ||
|
||
|
||
@pytest.fixture | ||
def call_contract_post_storage() -> Storage: | ||
""" | ||
Storage of the test contract after the transaction is executed. | ||
Note: Fixture `call_contract_code` fills the actual expected storage values. | ||
""" | ||
return Storage() | ||
|
||
|
||
@pytest.fixture | ||
def call_succeeds( | ||
expected_output: bytes | SupportsBytes, | ||
) -> bool: | ||
""" | ||
By default, depending on the expected output, we can deduce if the call is expected to succeed | ||
or fail. | ||
""" | ||
return len(bytes(expected_output)) > 0 | ||
|
||
|
||
@pytest.fixture | ||
def call_contract_code( | ||
precompile_address: int, | ||
precompile_gas: int, | ||
precompile_gas_modifier: int, | ||
expected_output: bytes | SupportsBytes, | ||
call_succeeds: bool, | ||
call_opcode: Op, | ||
call_contract_post_storage: Storage, | ||
) -> bytes: | ||
""" | ||
Code of the test contract. | ||
Args: | ||
precompile_address: | ||
Address of the precompile to call. | ||
precompile_gas: | ||
Gas cost for the precompile, which is automatically calculated by the `precompile_gas` | ||
fixture, but can be overridden in the test. | ||
precompile_gas_modifier: | ||
Gas cost modifier for the precompile, which is automatically set to zero by the | ||
`precompile_gas_modifier` fixture, but can be overridden in the test. | ||
expected_output: | ||
Expected output of the precompile call. This value is used to determine if the call is | ||
expected to succeed or fail. | ||
call_succeeds: | ||
Boolean that indicates if the call is expected to succeed or fail. | ||
call_opcode: | ||
Type of call used to call the precompile (Op.CALL, Op.CALLCODE, Op.DELEGATECALL, | ||
Op.STATICCALL). | ||
call_contract_post_storage: | ||
Storage of the test contract after the transaction is executed. | ||
""" | ||
expected_output = bytes(expected_output) | ||
|
||
assert call_opcode in [Op.CALL, Op.CALLCODE, Op.DELEGATECALL, Op.STATICCALL] | ||
value = [0] if call_opcode in [Op.CALL, Op.CALLCODE] else [] | ||
|
||
code = ( | ||
Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE()) | ||
+ Op.SSTORE( | ||
call_contract_post_storage.store_next(call_succeeds), | ||
call_opcode( | ||
precompile_gas + precompile_gas_modifier, | ||
precompile_address, | ||
*value, # Optional, only used for CALL and CALLCODE. | ||
0, | ||
Op.CALLDATASIZE(), | ||
0, | ||
0, | ||
), | ||
) | ||
+ Op.SSTORE( | ||
call_contract_post_storage.store_next(len(expected_output)), | ||
Op.RETURNDATASIZE(), | ||
) | ||
) | ||
if call_succeeds: | ||
# Add integrity check only if the call is expected to succeed. | ||
code += Op.RETURNDATACOPY(0, 0, Op.RETURNDATASIZE()) + Op.SSTORE( | ||
call_contract_post_storage.store_next(keccak256(expected_output)), | ||
Op.SHA3(0, Op.RETURNDATASIZE()), | ||
) | ||
|
||
return code | ||
|
||
|
||
@pytest.fixture | ||
def call_contract_address() -> int: | ||
"""Address where the test contract will be deployed.""" | ||
return 0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | ||
|
||
|
||
@pytest.fixture | ||
def pre(call_contract_address: int, call_contract_code: bytes): | ||
"""Pre-allocation for every test.""" | ||
return { | ||
call_contract_address: { | ||
"balance": 0, | ||
"nonce": 1, | ||
"code": call_contract_code, | ||
}, | ||
TestAddress: { | ||
"balance": 1_000_000_000_000_000, | ||
"nonce": 0, | ||
}, | ||
} | ||
|
||
|
||
@pytest.fixture | ||
def post(call_contract_address: int, call_contract_post_storage: Storage): | ||
"""Test expected post outcome.""" | ||
return { | ||
call_contract_address: { | ||
"storage": call_contract_post_storage, | ||
}, | ||
} | ||
|
||
|
||
@pytest.fixture | ||
def tx_gas_limit(precompile_gas: int) -> int: | ||
""" | ||
Transaction gas limit used for the test (Can be overridden in the test). | ||
""" | ||
return 10_000_000 + precompile_gas | ||
|
||
|
||
@pytest.fixture | ||
def tx(input: bytes, tx_gas_limit: int, call_contract_address: int) -> Transaction: | ||
"""Transaction for the test.""" | ||
return Transaction( | ||
gas_limit=tx_gas_limit, | ||
input=input, | ||
to=call_contract_address, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
""" | ||
Helper functions for the EIP-2537 BLS12-381 precompiles tests. | ||
""" | ||
import os | ||
from typing import Annotated, List | ||
|
||
import pytest | ||
from pydantic import BaseModel, BeforeValidator, ConfigDict, RootModel, TypeAdapter | ||
from pydantic.alias_generators import to_pascal | ||
|
||
|
||
def current_python_script_directory(*args: str) -> str: | ||
""" | ||
Get the current Python script directory, optionally appending additional path components. | ||
""" | ||
return os.path.join(os.path.dirname(os.path.realpath(__file__)), *args) | ||
|
||
|
||
class TestVector(BaseModel): | ||
""" | ||
Test vector for the BLS12-381 precompiles. | ||
""" | ||
|
||
input: Annotated[bytes, BeforeValidator(bytes.fromhex)] | ||
expected: Annotated[bytes, BeforeValidator(bytes.fromhex)] | ||
gas: int | ||
name: str | ||
|
||
model_config = ConfigDict(alias_generator=to_pascal) | ||
|
||
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) | ||
|
||
|
||
class FailTestVector(BaseModel): | ||
""" | ||
Test vector for the BLS12-381 precompiles. | ||
""" | ||
|
||
input: Annotated[bytes, BeforeValidator(bytes.fromhex)] | ||
expected_error: str | ||
name: str | ||
|
||
model_config = ConfigDict(alias_generator=to_pascal) | ||
|
||
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, id=self.name) | ||
|
||
|
||
class TestVectorList(RootModel): | ||
""" | ||
List of test vectors for the BLS12-381 precompiles. | ||
""" | ||
|
||
root: List[TestVector | FailTestVector] | ||
|
||
|
||
TestVectorListAdapter = TypeAdapter(TestVectorList) | ||
|
||
|
||
def vectors_from_file(filename: str) -> List: | ||
""" | ||
Load test vectors from a file. | ||
""" | ||
with open( | ||
current_python_script_directory( | ||
"vectors", | ||
filename, | ||
), | ||
"rb", | ||
) as f: | ||
return [v.to_pytest_param() for v in TestVectorListAdapter.validate_json(f.read()).root] |
1 change: 1 addition & 0 deletions
1
tests/prague/eip2537_bls_12_381_precompiles/msm_discount_table.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[0,1200,888,764,641,594,547,500,453,438,423,408,394,379,364,349,334,330,326,322,318,314,310,306,302,298,294,289,285,281,277,273,269,268,266,265,263,262,260,259,257,256,254,253,251,250,248,247,245,244,242,241,239,238,236,235,233,232,231,229,228,226,225,223,222,221,220,219,219,218,217,216,216,215,214,213,213,212,211,211,210,209,208,208,207,206,205,205,204,203,202,202,201,200,199,199,198,197,196,196,195,194,193,193,192,191,191,190,189,188,188,187,186,185,185,184,183,182,182,181,180,179,179,178,177,176,176,175,174] |
Oops, something went wrong.