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

chore: Add correct type casts to eip7594 functions #3697

Open
wants to merge 26 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
8ad8b69
add eip7594 to pylint checks
kevaundray Apr 18, 2024
006cfe6
fix pylint errors
kevaundray Apr 18, 2024
9c9f8f1
temp fix
kevaundray Apr 18, 2024
3554d79
Merge branch 'dev' into pr3697
hwwhww Apr 19, 2024
d5b4e6c
workaround: add a new config `MAX_EXTENDED_MATRIX_SIZE` to avoid `cla…
hwwhww Apr 19, 2024
ec0dc1f
ensure that value is in the correct range
kevaundray Apr 19, 2024
2a49ce2
Merge branch 'dev' into kw/add-eip7594-to-pylint
kevaundray Apr 19, 2024
2c32877
fix new lint errors
kevaundray Apr 19, 2024
b031a80
try: adding back line
kevaundray Apr 22, 2024
0dc414b
try: type ignore
kevaundray Apr 22, 2024
864e45a
Merge branch 'dev' into kw/add-eip7594-to-pylint
kevaundray Apr 22, 2024
0f8b0b4
add copy
kevaundray Apr 22, 2024
d2a8cb5
fix lint
kevaundray Apr 22, 2024
13d73ff
Merge branch 'dev' into kw/add-eip7594-to-pylint
kevaundray Apr 22, 2024
783899c
fix new linter errors
kevaundray Apr 22, 2024
47ef8ff
remove check from CI
kevaundray Apr 22, 2024
ac26ed2
Merge branch 'dev' into kw/add-eip7594-to-pylint
kevaundray Apr 23, 2024
31be7bf
Merge branch 'dev' into kw/add-eip7594-to-pylint
kevaundray Apr 25, 2024
97ce095
remove extra modulus reduction
kevaundray Apr 25, 2024
62ce140
append and reverse list
kevaundray Apr 25, 2024
8c10508
fix new linter error
kevaundray Apr 25, 2024
e5ad8b7
re-arrange BLS_MODULUS to be before b[i]
kevaundray Apr 25, 2024
1cc7e1a
Avoid underflow
asn-d6 Apr 29, 2024
6eb5756
Correctly handle CosetEvals in compute_cells()
asn-d6 Apr 29, 2024
92b25e6
lint
kevaundray Apr 29, 2024
4de4625
Merge branch 'dev' into kw/add-eip7594-to-pylint
kevaundray Apr 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ MARKDOWN_FILES = $(wildcard $(SPEC_DIR)/*/*.md) \
$(wildcard $(SPEC_DIR)/_features/*/*/*.md) \
$(wildcard $(SSZ_DIR)/*.md)

ALL_EXECUTABLE_SPEC_NAMES = phase0 altair bellatrix capella deneb electra whisk
ALL_EXECUTABLE_SPEC_NAMES = phase0 altair bellatrix capella deneb electra whisk eip7594
# The parameters for commands. Use `foreach` to avoid listing specs again.
COVERAGE_SCOPE := $(foreach S,$(ALL_EXECUTABLE_SPEC_NAMES), --cov=eth2spec.$S.$(TEST_PRESET_TYPE))
PYLINT_SCOPE := $(foreach S,$(ALL_EXECUTABLE_SPEC_NAMES), ./eth2spec/$S)
Expand Down
4 changes: 2 additions & 2 deletions specs/_features/eip7594/das-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class DataColumnSidecar(Container):
def get_custody_columns(node_id: NodeID, custody_subnet_count: uint64) -> Sequence[ColumnIndex]:
assert custody_subnet_count <= DATA_COLUMN_SIDECAR_SUBNET_COUNT

subnet_ids = []
subnet_ids: List[uint64] = []
i = 0
while len(subnet_ids) < custody_subnet_count:
subnet_id = (
Expand Down Expand Up @@ -141,7 +141,7 @@ def recover_matrix(cells_dict: Dict[Tuple[BlobIndex, CellID], Cell], blob_count:
extended_matrix = []
for blob_index in range(blob_count):
cell_ids = [cell_id for b_index, cell_id in cells_dict.keys() if b_index == blob_index]
cells = [cells_dict[(blob_index, cell_id)] for cell_id in cell_ids]
cells = [cells_dict[(BlobIndex(blob_index), cell_id)] for cell_id in cell_ids]
cells_bytes = [[bls_field_to_bytes(element) for element in cell] for cell in cells]

full_polynomial = recover_polynomial(cell_ids, cells_bytes)
Expand Down
6 changes: 3 additions & 3 deletions specs/_features/eip7594/p2p-interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ def verify_data_column_sidecar_kzg_proofs(sidecar: DataColumnSidecar) -> bool:

# KZG batch verifies that the cells match the corresponding commitments and proofs
return verify_cell_proof_batch(
row_commitments=sidecar.kzg_commitments,
row_commitments_bytes=sidecar.kzg_commitments,
row_indices=row_ids, # all rows
column_indices=[sidecar.index],
cells=sidecar.column,
proofs=sidecar.kzg_proofs,
cells_bytes=sidecar.column,
proofs_bytes=sidecar.kzg_proofs,
)
```

Expand Down
52 changes: 28 additions & 24 deletions specs/_features/eip7594/polynomial-commitments-sampling.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def bytes_to_cell(cell_bytes: Vector[Bytes32, FIELD_ELEMENTS_PER_CELL]) -> Cell:
"""
Convert untrusted bytes into a Cell.
"""
return [bytes_to_bls_field(element) for element in cell_bytes]
return Cell([bytes_to_bls_field(element) for element in cell_bytes])
```

### Linear combinations
Expand Down Expand Up @@ -165,7 +165,7 @@ def polynomial_eval_to_coeff(polynomial: Polynomial) -> PolynomialCoeff:
roots_of_unity = compute_roots_of_unity(FIELD_ELEMENTS_PER_BLOB)
polynomial_coeff = fft_field(bit_reversal_permutation(list(polynomial)), roots_of_unity, inv=True)

return polynomial_coeff
return PolynomialCoeff(polynomial_coeff)
```

#### `add_polynomialcoeff`
Expand All @@ -178,7 +178,7 @@ def add_polynomialcoeff(a: PolynomialCoeff, b: PolynomialCoeff) -> PolynomialCoe
a, b = (a, b) if len(a) >= len(b) else (b, a)
length_a = len(a)
length_b = len(b)
return [(a[i] + (b[i] if i < length_b else 0)) % BLS_MODULUS for i in range(length_a)]
return PolynomialCoeff([(a[i] + (b[i] if i < length_b else 0)) % BLS_MODULUS for i in range(length_a)])
```

#### `neg_polynomialcoeff`
Expand All @@ -188,7 +188,7 @@ def neg_polynomialcoeff(a: PolynomialCoeff) -> PolynomialCoeff:
"""
Negative of coefficient form polynomial ``a``
"""
return [(BLS_MODULUS - x) % BLS_MODULUS for x in a]
return PolynomialCoeff([(BLS_MODULUS - x) % BLS_MODULUS for x in a])
```

#### `multiply_polynomialcoeff`
Expand All @@ -200,10 +200,10 @@ def multiply_polynomialcoeff(a: PolynomialCoeff, b: PolynomialCoeff) -> Polynomi
"""
assert len(a) + len(b) <= FIELD_ELEMENTS_PER_EXT_BLOB

r = [0]
r = PolynomialCoeff([0])
for power, coef in enumerate(a):
summand = [0] * power + [int(coef) * int(x) % BLS_MODULUS for x in b]
r = add_polynomialcoeff(r, summand)
r = add_polynomialcoeff(r, PolynomialCoeff(summand))
return r
```

Expand All @@ -214,8 +214,8 @@ def divide_polynomialcoeff(a: PolynomialCoeff, b: PolynomialCoeff) -> Polynomial
"""
Long polynomial division for two coefficient form polynomials ``a`` and ``b``
"""
a = [x for x in a]
o = []

o: List[BLSFieldElement] = []
apos = len(a) - 1
bpos = len(b) - 1
diff = apos - bpos
Expand All @@ -226,7 +226,7 @@ def divide_polynomialcoeff(a: PolynomialCoeff, b: PolynomialCoeff) -> Polynomial
a[diff + i] = (int(a[diff + i]) - int(b[i]) * int(quot)) % BLS_MODULUS
apos -= 1
diff -= 1
return [x % BLS_MODULUS for x in o]
return PolynomialCoeff([x % BLS_MODULUS for x in o])
```

#### `shift_polynomialcoeff`
Expand All @@ -243,7 +243,7 @@ def shift_polynomialcoeff(polynomial_coeff: PolynomialCoeff, factor: BLSFieldEle
for p in polynomial_coeff:
o.append(int(p) * factor_power % BLS_MODULUS)
factor_power = factor_power * inv_factor % BLS_MODULUS
return o
return PolynomialCoeff(o)
```

#### `interpolate_polynomialcoeff`
Expand All @@ -256,19 +256,20 @@ def interpolate_polynomialcoeff(xs: Sequence[BLSFieldElement], ys: Sequence[BLSF
Outputs a coefficient form polynomial. Leading coefficients may be zero.
"""
assert len(xs) == len(ys)
r = [0]
r = PolynomialCoeff([0])

for i in range(len(xs)):
summand = [ys[i]]
summand = PolynomialCoeff([ys[i]])
for j in range(len(ys)):
if j != i:
weight_adjustment = bls_modular_inverse(int(xs[i]) - int(xs[j]))
weight_adjustment = bls_modular_inverse(BLSFieldElement(int(xs[i]) - int(xs[j])))
summand = multiply_polynomialcoeff(
summand, [(- int(weight_adjustment) * int(xs[j])) % BLS_MODULUS, weight_adjustment]
summand,
PolynomialCoeff([(- int(weight_adjustment) * int(xs[j])) % BLS_MODULUS, weight_adjustment])
)
r = add_polynomialcoeff(r, summand)

return r
return PolynomialCoeff(r)
```

#### `vanishing_polynomialcoeff`
Expand All @@ -278,9 +279,9 @@ def vanishing_polynomialcoeff(xs: Sequence[BLSFieldElement]) -> PolynomialCoeff:
"""
Compute the vanishing polynomial on ``xs`` (in coefficient form)
"""
p = [1]
p = PolynomialCoeff([1])
for x in xs:
p = multiply_polynomialcoeff(p, [-int(x), 1])
p = multiply_polynomialcoeff(p, PolynomialCoeff([-int(x), 1]))
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
return p
```

Expand Down Expand Up @@ -390,7 +391,7 @@ def compute_cells_and_proofs(blob: Blob) -> Tuple[
proofs = []

for i in range(CELLS_PER_BLOB):
coset = coset_for_cell(i)
coset = coset_for_cell(CellID(i))
proof, ys = compute_kzg_proof_multi_impl(polynomial_coeff, coset)
cells.append(ys)
proofs.append(proof)
Expand Down Expand Up @@ -484,6 +485,7 @@ def verify_cell_proof_batch(row_commitments_bytes: Sequence[Bytes48],

```python
def construct_vanishing_polynomial(missing_cell_ids: Sequence[CellID]) -> Tuple[
Sequence[BLSFieldElement],
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
Sequence[BLSFieldElement],
Sequence[BLSFieldElement]]:
"""
Expand All @@ -500,7 +502,7 @@ def construct_vanishing_polynomial(missing_cell_ids: Sequence[CellID]) -> Tuple[
])

# Extend vanishing polynomial to full domain using the closed form of the vanishing polynomial over a coset
zero_poly_coeff = [0] * FIELD_ELEMENTS_PER_EXT_BLOB
zero_poly_coeff = [BLSFieldElement(0)] * FIELD_ELEMENTS_PER_EXT_BLOB
for i, coeff in enumerate(short_zero_poly):
zero_poly_coeff[i * FIELD_ELEMENTS_PER_CELL] = coeff

Expand Down Expand Up @@ -549,12 +551,13 @@ def recover_shifted_data(cell_ids: Sequence[CellID],
extended_evaluation_times_zero = [BLSFieldElement(int(a) * int(b) % BLS_MODULUS)
for a, b in zip(zero_poly_eval, extended_evaluation)]

extended_evaluations_fft = fft_field(extended_evaluation_times_zero, roots_of_unity_extended, inv=True)
extended_evaluations_fft = PolynomialCoeff(
fft_field(extended_evaluation_times_zero, roots_of_unity_extended, inv=True))

# Compute (E*Z)(k*x)
shifted_extended_evaluation = shift_polynomialcoeff(extended_evaluations_fft, shift_factor)
# Compute Z(k*x)
shifted_zero_poly = shift_polynomialcoeff(zero_poly_coeff, shift_factor)
shifted_zero_poly = shift_polynomialcoeff(PolynomialCoeff(zero_poly_coeff), shift_factor)

eval_shifted_extended_evaluation = fft_field(shifted_extended_evaluation, roots_of_unity_extended)
eval_shifted_zero_poly = fft_field(shifted_zero_poly, roots_of_unity_extended)
Expand All @@ -578,7 +581,8 @@ def recover_original_data(eval_shifted_extended_evaluation: Sequence[BLSFieldEle
for a, b in zip(eval_shifted_extended_evaluation, eval_shifted_zero_poly)
]

shifted_reconstructed_poly = fft_field(eval_shifted_reconstructed_poly, roots_of_unity_extended, inv=True)
shifted_reconstructed_poly = PolynomialCoeff(
fft_field(eval_shifted_reconstructed_poly, roots_of_unity_extended, inv=True))

# Unshift P(k*x) by k^{-1} to get P(x)
reconstructed_poly = shift_polynomialcoeff(shifted_reconstructed_poly, shift_inv)
Expand Down Expand Up @@ -615,7 +619,7 @@ def recover_polynomial(cell_ids: Sequence[CellID],
# Convert from bytes to cells
cells = [bytes_to_cell(cell_bytes) for cell_bytes in cells_bytes]

missing_cell_ids = [cell_id for cell_id in range(CELLS_PER_BLOB) if cell_id not in cell_ids]
missing_cell_ids = [CellID(cell_id) for cell_id in range(CELLS_PER_BLOB) if cell_id not in cell_ids]
zero_poly_coeff, zero_poly_eval, zero_poly_eval_brp = construct_vanishing_polynomial(missing_cell_ids)

eval_shifted_extended_evaluation, eval_shifted_zero_poly, shift_inv = recover_shifted_data(
Expand All @@ -638,5 +642,5 @@ def recover_polynomial(cell_ids: Sequence[CellID],
end = (cell_id + 1) * FIELD_ELEMENTS_PER_CELL
assert reconstructed_data[start:end] == cell

return reconstructed_data
return Polynomial(reconstructed_data)
```