Skip to content

Commit a9e3aad

Browse files
authored
Use bls.Scalar as the base class for BLSFieldElement (#3907)
1 parent 3196f32 commit a9e3aad

File tree

13 files changed

+248
-215
lines changed

13 files changed

+248
-215
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ MARKDOWN_FILES = $(wildcard $(SPEC_DIR)/*/*.md) \
3434
$(wildcard $(SPEC_DIR)/_features/*/*/*.md) \
3535
$(wildcard $(SSZ_DIR)/*.md)
3636

37-
ALL_EXECUTABLE_SPEC_NAMES = phase0 altair bellatrix capella deneb electra whisk eip6800 eip7732
37+
ALL_EXECUTABLE_SPEC_NAMES = phase0 altair bellatrix capella deneb electra whisk eip6800 eip7594 eip7732
3838
# The parameters for commands. Use `foreach` to avoid listing specs again.
3939
COVERAGE_SCOPE := $(foreach S,$(ALL_EXECUTABLE_SPEC_NAMES), --cov=eth2spec.$S.$(TEST_PRESET_TYPE))
4040
PYLINT_SCOPE := $(foreach S,$(ALL_EXECUTABLE_SPEC_NAMES), ./eth2spec/$S)

pysetup/helpers.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ def format_constant(name: str, vardef: VariableDefinition) -> str:
119119
hardcoded_func_dep_presets = reduce(lambda obj, builder: {**obj, **builder.hardcoded_func_dep_presets(spec_object)}, builders, {})
120120
# Concatenate all strings
121121
imports = reduce(lambda txt, builder: (txt + "\n\n" + builder.imports(preset_name) ).strip("\n"), builders, "")
122+
classes = reduce(lambda txt, builder: (txt + "\n\n" + builder.classes() ).strip("\n"), builders, "")
122123
preparations = reduce(lambda txt, builder: (txt + "\n\n" + builder.preparations() ).strip("\n"), builders, "")
123124
sundry_functions = reduce(lambda txt, builder: (txt + "\n\n" + builder.sundry_functions() ).strip("\n"), builders, "")
124125
# Keep engine from the most recent fork
@@ -154,6 +155,8 @@ def format_constant(name: str, vardef: VariableDefinition) -> str:
154155
constant_vars_spec,
155156
preset_vars_spec,
156157
config_spec,
158+
# Custom classes which are not required to be SSZ containers.
159+
classes,
157160
ordered_class_objects_spec,
158161
protocols_spec,
159162
functions_spec,

pysetup/spec_builders/base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ def imports(cls, preset_name: str) -> str:
1515
"""
1616
return ""
1717

18+
@classmethod
19+
def classes(cls) -> str:
20+
"""
21+
Define special classes.
22+
"""
23+
return ""
24+
1825
@classmethod
1926
def preparations(cls) -> str:
2027
"""

pysetup/spec_builders/deneb.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@ def imports(cls, preset_name: str):
1212
from eth2spec.capella import {preset_name} as capella
1313
'''
1414

15+
@classmethod
16+
def classes(cls):
17+
return f'''
18+
class BLSFieldElement(bls.Scalar):
19+
pass
20+
21+
22+
class Polynomial(list):
23+
def __init__(self, evals: Optional[Sequence[BLSFieldElement]] = None):
24+
if evals is None:
25+
evals = [BLSFieldElement(0)] * FIELD_ELEMENTS_PER_BLOB
26+
if len(evals) != FIELD_ELEMENTS_PER_BLOB:
27+
raise ValueError("expected FIELD_ELEMENTS_PER_BLOB evals")
28+
super().__init__(evals)
29+
'''
1530

1631
@classmethod
1732
def preparations(cls):

pysetup/spec_builders/eip7594.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,41 @@ def imports(cls, preset_name: str):
1212
return f'''
1313
from eth2spec.deneb import {preset_name} as deneb
1414
'''
15-
15+
16+
17+
@classmethod
18+
def classes(cls):
19+
return f'''
20+
class PolynomialCoeff(list):
21+
def __init__(self, coeffs: Sequence[BLSFieldElement]):
22+
if len(coeffs) > FIELD_ELEMENTS_PER_EXT_BLOB:
23+
raise ValueError("expected <= FIELD_ELEMENTS_PER_EXT_BLOB coeffs")
24+
super().__init__(coeffs)
25+
26+
27+
class Coset(list):
28+
def __init__(self, coeffs: Optional[Sequence[BLSFieldElement]] = None):
29+
if coeffs is None:
30+
coeffs = [BLSFieldElement(0)] * FIELD_ELEMENTS_PER_CELL
31+
if len(coeffs) != FIELD_ELEMENTS_PER_CELL:
32+
raise ValueError("expected FIELD_ELEMENTS_PER_CELL coeffs")
33+
super().__init__(coeffs)
34+
35+
36+
class CosetEvals(list):
37+
def __init__(self, evals: Optional[Sequence[BLSFieldElement]] = None):
38+
if evals is None:
39+
evals = [BLSFieldElement(0)] * FIELD_ELEMENTS_PER_CELL
40+
if len(evals) != FIELD_ELEMENTS_PER_CELL:
41+
raise ValueError("expected FIELD_ELEMENTS_PER_CELL coeffs")
42+
super().__init__(evals)
43+
'''
1644

1745
@classmethod
1846
def sundry_functions(cls) -> str:
1947
return """
2048
def retrieve_column_sidecars(beacon_block_root: Root) -> Sequence[DataColumnSidecar]:
49+
# pylint: disable=unused-argument
2150
return []
2251
"""
2352

setup.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def _update_constant_vars_with_kzg_setups(constant_vars, preset_name):
183183
constant_vars['KZG_SETUP_G1_MONOMIAL'] = VariableDefinition(constant_vars['KZG_SETUP_G1_MONOMIAL'].value, str(kzg_setups[0]), comment, None)
184184
constant_vars['KZG_SETUP_G1_LAGRANGE'] = VariableDefinition(constant_vars['KZG_SETUP_G1_LAGRANGE'].value, str(kzg_setups[1]), comment, None)
185185
constant_vars['KZG_SETUP_G2_MONOMIAL'] = VariableDefinition(constant_vars['KZG_SETUP_G2_MONOMIAL'].value, str(kzg_setups[2]), comment, None)
186-
186+
187187

188188
def get_spec(file_name: Path, preset: Dict[str, str], config: Dict[str, str], preset_name=str) -> SpecObject:
189189
functions: Dict[str, str] = {}
@@ -261,10 +261,17 @@ def get_spec(file_name: Path, preset: Dict[str, str], config: Dict[str, str], pr
261261
# marko parses `**X**` as a list containing a X
262262
description = description[0].children
263263

264+
if isinstance(name, list):
265+
# marko parses `[X]()` as a list containing a X
266+
name = name[0].children
264267
if isinstance(value, list):
265268
# marko parses `**X**` as a list containing a X
266269
value = value[0].children
267270

271+
# Skip types that have been defined elsewhere
272+
if description is not None and description.startswith("<!-- predefined-type -->"):
273+
continue
274+
268275
if not _is_constant_id(name):
269276
# Check for short type declarations
270277
if value.startswith(("uint", "Bytes", "ByteList", "Union", "Vector", "List", "ByteVector")):
@@ -569,7 +576,7 @@ def run(self):
569576
RUAMEL_YAML_VERSION,
570577
"lru-dict==1.2.0",
571578
MARKO_VERSION,
572-
"py_arkworks_bls12381==0.3.4",
573-
"curdleproofs==0.1.1",
579+
"py_arkworks_bls12381==0.3.8",
580+
"curdleproofs==0.1.2",
574581
]
575582
)

0 commit comments

Comments
 (0)