Skip to content

Commit 14d6f53

Browse files
committed
Refactor common spec code and constants
1 parent a2d18fe commit 14d6f53

File tree

6 files changed

+307
-753
lines changed

6 files changed

+307
-753
lines changed

converted-ethereum-tests.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,4 @@ GeneralStateTests/stPreCompiledContracts/idPrecomps.json
104104
([#1244](https://github.com/ethereum/execution-spec-tests/pull/1244))
105105
GeneralStateTests/stPreCompiledContracts/delegatecall09Undefined.json
106106
GeneralStateTests/stPreCompiledContracts2/CALLBlake2f.json
107+
GeneralStateTests/stPreCompiledContracts2/CALLCODEBlake2f.json
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""Common classes used in the BLAKE2b precompile tests."""
2+
3+
from dataclasses import dataclass
4+
5+
from ethereum_test_types.helpers import TestParameterGroup
6+
7+
from .spec import Spec
8+
9+
10+
@dataclass(kw_only=True, frozen=True, repr=False)
11+
class Blake2bInput(TestParameterGroup):
12+
"""
13+
Helper class that defines the BLAKE2b precompile inputs and creates the
14+
call data from them. Returns all inputs encoded as bytes.
15+
16+
Attributes:
17+
rounds_length (int): An optional integer representing the bytes length
18+
for the number of rounds. Defaults to the expected length of 4.
19+
rounds (int | str): A hex string or integer value representing the number of rounds.
20+
h (str): A hex string that represents the state vector.
21+
m (str): A hex string that represents the message block vector.
22+
t_0 (int | str): A hex string or integer value that represents the first offset counter.
23+
t_1 (int | str): A hex string or integer value that represents the second offset counter.
24+
f (bool): An optional boolean that represents the final block indicator flag.
25+
Defaults to True.
26+
27+
"""
28+
29+
rounds_length: int = Spec.BLAKE2_PRECOMPILE_ROUNDS_LENGTH
30+
rounds: int | str = Spec.BLAKE2B_PRECOMPILE_ROUNDS
31+
h: str
32+
m: str
33+
t_0: int | str
34+
t_1: int | str
35+
f: bool | int = True
36+
37+
def create_blake2b_tx_data(self):
38+
"""Generate input for the BLAKE2b precompile."""
39+
_rounds = self.rounds.to_bytes(length=self.rounds_length, byteorder="big")
40+
_h = bytes.fromhex(self.h)
41+
_m = bytes.fromhex(self.m)
42+
_t_0 = (
43+
bytes.fromhex(self.t_0)
44+
if isinstance(self.t_0, str)
45+
else self.t_0.to_bytes(length=Spec.BLAKE2_PRECOMPILE_T_0_LENGTH, byteorder="little")
46+
)
47+
_t_1 = (
48+
bytes.fromhex(self.t_1)
49+
if isinstance(self.t_1, str)
50+
else self.t_1.to_bytes(length=Spec.BLAKE2_PRECOMPILE_T_1_LENGTH, byteorder="little")
51+
)
52+
_f = int(self.f).to_bytes(length=Spec.BLAKE2_PRECOMPILE_F_LENGTH, byteorder="big")
53+
54+
return _rounds + _h + _m + _t_0 + _t_1 + _f
55+
56+
57+
@dataclass(kw_only=True, frozen=True, repr=False)
58+
class ExpectedOutput(TestParameterGroup):
59+
"""
60+
Expected test result.
61+
62+
Attributes:
63+
call_succeeds (str | bool): A hex string or boolean to indicate whether the call was
64+
successful or not.
65+
data_1 (str): String value of the first updated state vector.
66+
data_2 (str): String value of the second updated state vector.
67+
68+
"""
69+
70+
call_succeeds: str | bool
71+
data_1: str
72+
data_2: str
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""pytest fixtures for testing the BLAKE2b precompile."""
2+
3+
import pytest
4+
5+
from ethereum_test_tools.vm.opcode import Opcodes as Op
6+
from ethereum_test_vm.bytecode import Bytecode
7+
8+
from .spec import Spec
9+
10+
11+
@pytest.fixture
12+
def blake2b_contract_bytecode(call_opcode: Op) -> Bytecode:
13+
"""
14+
Contract code that performs the provided opcode (CALL or CALLCODE) to the BLAKE2b precompile
15+
and stores the result.
16+
"""
17+
return (
18+
Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE())
19+
+ Op.SSTORE(
20+
0,
21+
call_opcode(
22+
address=Spec.BLAKE2_PRECOMPILE_ADDRESS,
23+
args_offset=0,
24+
args_size=Op.CALLDATASIZE(),
25+
ret_offset=0x200,
26+
ret_size=0x40,
27+
),
28+
)
29+
+ Op.SSTORE(1, Op.MLOAD(0x200))
30+
+ Op.SSTORE(2, Op.MLOAD(0x220))
31+
+ Op.STOP()
32+
)

tests/istanbul/eip152_blake2/spec.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""Defines EIP-152 specification constants and functions."""
2+
3+
from dataclasses import dataclass
4+
5+
6+
@dataclass(frozen=True)
7+
class ReferenceSpec:
8+
"""Defines the reference spec version and git path."""
9+
10+
git_path: str
11+
version: str
12+
13+
14+
ref_spec_152 = ReferenceSpec("EIPS/eip-152.md", "2762bfcff3e549ef263342e5239ef03ac2b07400")
15+
16+
17+
# Constants
18+
@dataclass(frozen=True)
19+
class Spec:
20+
"""
21+
Parameters from the EIP-152 specifications as defined at
22+
https://eips.ethereum.org/EIPS/eip-152#specification.
23+
24+
If the parameter is not currently used within the tests, it is commented
25+
out.
26+
"""
27+
28+
BLAKE2_PRECOMPILE_ADDRESS = 0x09
29+
30+
# The following constants are used to define the bytes length of the
31+
# parameters passed to the BLAKE2 precompile.
32+
BLAKE2_PRECOMPILE_ROUNDS_LENGTH = 4
33+
# BLAKE2_PRECOMPILE_M_LENGTH = 128
34+
BLAKE2_PRECOMPILE_T_0_LENGTH = 8
35+
BLAKE2_PRECOMPILE_T_1_LENGTH = 8
36+
BLAKE2_PRECOMPILE_F_LENGTH = 1
37+
38+
# Constants for BLAKE2b and BLAKE2s spec defined at https://datatracker.ietf.org/doc/html/rfc7693#section-3.2
39+
BLAKE2B_PRECOMPILE_ROUNDS = 12
40+
BLAKE2B_PRECOMPILE_H_LENGTH = 64
41+
42+
# BLAKE2S_PRECOMPILE_ROUNDS = 10
43+
# BLAKE2S_PRECOMPILE_H_LENGTH = 32
44+
45+
46+
class SpecTestVectors:
47+
"""Defines common test parameters for the BLAKE2b precompile."""
48+
49+
# The following constants are used to define common test parameters
50+
# Origin of vectors defined at https://datatracker.ietf.org/doc/html/rfc7693.html#appendix-A
51+
BLAKE2_STATE_VECTOR = "48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b" # noqa:E501
52+
BLAKE2_MESSAGE_BLOCK_VECTOR = "6162630000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" # noqa:E501
53+
BLAKE2_OFFSET_COUNTER_0 = 3
54+
BLAKE2_OFFSET_COUNTER_1 = 0

0 commit comments

Comments
 (0)