Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new(tests): EIP-2537: BLS12-381 Precompiles #499

Merged
merged 42 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
4aee58a
new(tests): Add eip-2537 vectors
marioevz Apr 9, 2024
fae3d8c
new(tests): eip-2537
marioevz Apr 10, 2024
fb49683
refactor(tests): eip2537
marioevz Apr 11, 2024
ed8dd47
new(tests): more eip2537 tests
marioevz Apr 11, 2024
9952744
fix(tests): typos
marioevz Apr 11, 2024
60e7241
fix(tests): eip2537: fix k length msm g1
marioevz Apr 11, 2024
da38ba8
refactor(tests): eip-2537
marioevz Apr 11, 2024
841efaf
new(tests): eip-2537: multiplication gas tests
marioevz Apr 11, 2024
a4ddae0
nit(tests): eip-2537: function names
marioevz Apr 11, 2024
ccce27d
fix(tests): eip2537: unused imports
marioevz Apr 12, 2024
27d65e8
new(tests): eip2537: new msm gas tests
marioevz Apr 12, 2024
51c08a6
refactor(tests): eip2537
marioevz Apr 12, 2024
72a4743
refactor(tests): eip2537 spec
marioevz Apr 12, 2024
ed999cc
fix(tests): eip2537, add more tests
marioevz Apr 12, 2024
51a5ba0
fix(tests): eip2537: format vector
marioevz Apr 12, 2024
0cd5c76
fix(tests): eip2537: update vectors from https://github.com/ethereum/…
marioevz Apr 12, 2024
f26d65e
fix(tests): eip2537: conditional integrity check
marioevz Apr 12, 2024
bcb00b1
refactor(tests): eip2537: spec
marioevz Apr 12, 2024
18d24aa
new(tests): eip2537: pairing tests
marioevz Apr 12, 2024
718c888
fix(tests): eip2537: spec
marioevz Apr 15, 2024
99f8fd7
fix(tests): eip2537: use `Spec.INVALID` constant
marioevz Apr 15, 2024
9c7573d
new(tests): eip2537: pairing tests
marioevz Apr 15, 2024
e81eee4
new(tests): eip2537: pairing invalid gas/length tests
marioevz Apr 15, 2024
19cb0d1
new(tests): eip2537: invalid encoding, G1 add, mul, msm, not in subgr…
marioevz Apr 16, 2024
d1444d5
new(tests): eip2537: call type tests
marioevz Apr 16, 2024
2fd19e5
new(tests): eip2537: g2 not in subgroup
marioevz Apr 16, 2024
3fd33b9
new(tests): eip2537: incorrect subgroup tests
marioevz Apr 16, 2024
f05e271
changelog
marioevz Apr 16, 2024
f0e7c71
fix: tox
marioevz Apr 16, 2024
a42b0d0
fix(tests): eip2537: comments
marioevz Apr 16, 2024
9de07f7
fix(tests): eip2537: github reference spec hash
marioevz Apr 16, 2024
96f9119
fix(tests): eip-2537: refresh vectors
marioevz Apr 24, 2024
e23fa75
fix(tests): eip-2537: move subgroup tests valid -> invalid
marioevz Apr 24, 2024
baf5447
fix(tests): eip-2537: load failing test vectors
marioevz Apr 24, 2024
fc2480e
feat(forks): Add BLS precompiles to Prague
marioevz Apr 24, 2024
d97d4c7
fix(tests): EIP-2537: spec reference hash update
marioevz Apr 24, 2024
8936ef6
feat(tests): eip-2537: Add inputs to mul precompiles
marioevz May 17, 2024
64c9f00
new(tests): eip-2537: more tests
marioevz May 17, 2024
b938256
fix(tests): hardcode fork name
marioevz May 24, 2024
9e66ae5
fix(tests): EIP-2537: load MSM table from file
marioevz May 24, 2024
747c1af
Merge branch 'main' into eip-2537
marioevz May 24, 2024
c582c78
new(tests): EIP-2537: test precompiles before fork is active
marioevz May 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Test fixtures for use by clients are available for each release on the [Github r

- ✨ Add `test_double_kill` and `test_recreate` which test resurrection of accounts killed with `SELFDESTRUCT` ([#488](https://github.com/ethereum/execution-spec-tests/pull/488)).
- ✨ Add eof example valid invalid tests from ori, fetch EOF Container implementation ([#535](https://github.com/ethereum/execution-spec-tests/pull/535)).
- ✨ Add tests for [EIP-2537: Precompile for BLS12-381 curve operations](https://eips.ethereum.org/EIPS/eip-2537) ([#499](https://github.com/ethereum/execution-spec-tests/pull/499)).

### 🛠️ Framework

Expand Down
17 changes: 17 additions & 0 deletions src/ethereum_test_forks/forks/forks.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,23 @@ def solc_min_version(cls) -> Version:
"""
return Version.parse("1.0.0") # set a high version; currently unknown

@classmethod
def precompiles(cls, block_number: int = 0, timestamp: int = 0) -> List[int]:
"""
At Prague, pre-compile for BLS operations are added:

G1ADD = 0x0B
G1MUL = 0x0C
G1MSM = 0x0D
G2ADD = 0x0E
G2MUL = 0x0F
G2MSM = 0x10
PAIRING = 0x11
MAP_FP_TO_G1 = 0x12
MAP_FP2_TO_G2 = 0x13
"""
return list(range(0xB, 0x13 + 1)) + super(Prague, cls).precompiles(block_number, timestamp)

@classmethod
def engine_new_payload_version(
cls, block_number: int = 0, timestamp: int = 0
Expand Down
4 changes: 4 additions & 0 deletions tests/prague/eip2537_bls_12_381_precompiles/__init__.py
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
166 changes: 166 additions & 0 deletions tests/prague/eip2537_bls_12_381_precompiles/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
"""
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_contract_code(
precompile_address: int,
precompile_gas: int,
precompile_gas_modifier: int,
expected_output: bytes | SupportsBytes,
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_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)

# Depending on the expected output, we can deduce if the call is expected to succeed or fail.
call_succeeds = len(expected_output) > 0

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,
)
77 changes: 77 additions & 0 deletions tests/prague/eip2537_bls_12_381_precompiles/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""
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() -> str:
"""
Get the current Python script directory.
"""
return os.path.dirname(os.path.realpath(__file__))


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.
"""
full_path = os.path.join(
current_python_script_directory(),
"vectors",
filename,
)
with open(full_path, "rb") as f:
return [v.to_pytest_param() for v in TestVectorListAdapter.validate_json(f.read()).root]
Loading