Skip to content

Commit

Permalink
Merge pull request ethereum#3797 from jtraglia/rename-cellid-to-celli…
Browse files Browse the repository at this point in the history
…ndex

Rename `CellID` to `CellIndex`
  • Loading branch information
asn-d6 committed Jun 11, 2024
2 parents c5e9c3c + d137553 commit 7b7ada7
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 204 deletions.
12 changes: 6 additions & 6 deletions specs/_features/eip7594/das-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ def compute_extended_matrix(blobs: Sequence[Blob]) -> List[MatrixEntry, MAX_CELL
extended_matrix = []
for blob_index, blob in enumerate(blobs):
cells, proofs = compute_cells_and_kzg_proofs(blob)
for cell_id, (cell, proof) in enumerate(zip(cells, proofs)):
for cell_index, (cell, proof) in enumerate(zip(cells, proofs)):
extended_matrix.append(MatrixEntry(
cell=cell,
kzg_proof=proof,
row_index=blob_index,
column_index=cell_id,
column_index=cell_index,
))
return extended_matrix
```
Expand All @@ -174,17 +174,17 @@ def recover_matrix(partial_matrix: Sequence[MatrixEntry],
"""
extended_matrix = []
for blob_index in range(blob_count):
cell_ids = [e.column_index for e in partial_matrix if e.row_index == blob_index]
cell_indices = [e.column_index for e in partial_matrix if e.row_index == blob_index]
cells = [e.cell for e in partial_matrix if e.row_index == blob_index]
proofs = [e.kzg_proof for e in partial_matrix if e.row_index == blob_index]

recovered_cells, recovered_proofs = recover_cells_and_kzg_proofs(cell_ids, cells, proofs)
for cell_id, (cell, proof) in enumerate(zip(recovered_cells, recovered_proofs)):
recovered_cells, recovered_proofs = recover_cells_and_kzg_proofs(cell_indices, cells, proofs)
for cell_index, (cell, proof) in enumerate(zip(recovered_cells, recovered_proofs)):
extended_matrix.append(MatrixEntry(
cell=cell,
kzg_proof=proof,
row_index=blob_index,
column_index=cell_id,
column_index=cell_index,
))
return extended_matrix
```
Expand Down
81 changes: 41 additions & 40 deletions specs/_features/eip7594/polynomial-commitments-sampling.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ The following is a list of the public methods:
| `Coset` | `Vector[BLSFieldElement, FIELD_ELEMENTS_PER_CELL]` | The evaluation domain of a cell |
| `CosetEvals` | `Vector[BLSFieldElement, FIELD_ELEMENTS_PER_CELL]` | The internal representation of a cell (the evaluations over its Coset) |
| `Cell` | `ByteVector[BYTES_PER_FIELD_ELEMENT * FIELD_ELEMENTS_PER_CELL]` | The unit of blob data that can come with its own KZG proof |
| `CellID` | `uint64` | Validation: `x < CELLS_PER_EXT_BLOB` |
| `CellIndex` | `uint64` | Validation: `x < CELLS_PER_EXT_BLOB` |

## Constants

Expand Down Expand Up @@ -414,15 +414,15 @@ def verify_kzg_proof_multi_impl(commitment: KZGCommitment,
#### `coset_for_cell`

```python
def coset_for_cell(cell_id: CellID) -> Coset:
def coset_for_cell(cell_index: CellIndex) -> Coset:
"""
Get the coset for a given ``cell_id``
Get the coset for a given ``cell_index``.
"""
assert cell_id < CELLS_PER_EXT_BLOB
assert cell_index < CELLS_PER_EXT_BLOB
roots_of_unity_brp = bit_reversal_permutation(
compute_roots_of_unity(FIELD_ELEMENTS_PER_EXT_BLOB)
)
return Coset(roots_of_unity_brp[FIELD_ELEMENTS_PER_CELL * cell_id:FIELD_ELEMENTS_PER_CELL * (cell_id + 1)])
return Coset(roots_of_unity_brp[FIELD_ELEMENTS_PER_CELL * cell_index:FIELD_ELEMENTS_PER_CELL * (cell_index + 1)])
```

## Cells
Expand Down Expand Up @@ -451,7 +451,7 @@ def compute_cells_and_kzg_proofs(blob: Blob) -> Tuple[
proofs = []

for i in range(CELLS_PER_EXT_BLOB):
coset = coset_for_cell(CellID(i))
coset = coset_for_cell(CellIndex(i))
proof, ys = compute_kzg_proof_multi_impl(polynomial_coeff, coset)
cells.append(coset_evals_to_cell(ys))
proofs.append(proof)
Expand All @@ -477,9 +477,9 @@ def compute_cells(blob: Blob) -> Vector[Cell, CELLS_PER_EXT_BLOB]:
compute_roots_of_unity(FIELD_ELEMENTS_PER_EXT_BLOB))
extended_data_rbo = bit_reversal_permutation(extended_data)
cells = []
for cell_id in range(CELLS_PER_EXT_BLOB):
start = cell_id * FIELD_ELEMENTS_PER_CELL
end = (cell_id + 1) * FIELD_ELEMENTS_PER_CELL
for cell_index in range(CELLS_PER_EXT_BLOB):
start = cell_index * FIELD_ELEMENTS_PER_CELL
end = (cell_index + 1) * FIELD_ELEMENTS_PER_CELL
cells.append(coset_evals_to_cell(CosetEvals(extended_data_rbo[start:end])))
return cells
```
Expand All @@ -490,7 +490,7 @@ def compute_cells(blob: Blob) -> Vector[Cell, CELLS_PER_EXT_BLOB]:

```python
def verify_cell_kzg_proof(commitment_bytes: Bytes48,
cell_id: CellID,
cell_index: CellIndex,
cell: Cell,
proof_bytes: Bytes48) -> bool:
"""
Expand All @@ -499,11 +499,11 @@ def verify_cell_kzg_proof(commitment_bytes: Bytes48,
Public method.
"""
assert len(commitment_bytes) == BYTES_PER_COMMITMENT
assert cell_id < CELLS_PER_EXT_BLOB
assert cell_index < CELLS_PER_EXT_BLOB
assert len(cell) == BYTES_PER_CELL
assert len(proof_bytes) == BYTES_PER_PROOF

coset = coset_for_cell(cell_id)
coset = coset_for_cell(cell_index)

return verify_kzg_proof_multi_impl(
bytes_to_kzg_commitment(commitment_bytes),
Expand Down Expand Up @@ -565,7 +565,7 @@ def verify_cell_kzg_proof_batch(row_commitments_bytes: Sequence[Bytes48],
### `construct_vanishing_polynomial`

```python
def construct_vanishing_polynomial(missing_cell_ids: Sequence[CellID]) -> Tuple[
def construct_vanishing_polynomial(missing_cell_indices: Sequence[CellIndex]) -> Tuple[
Sequence[BLSFieldElement],
Sequence[BLSFieldElement]]:
"""
Expand All @@ -577,8 +577,8 @@ def construct_vanishing_polynomial(missing_cell_ids: Sequence[CellID]) -> Tuple[

# Compute polynomial that vanishes at all the missing cells (over the small domain)
short_zero_poly = vanishing_polynomialcoeff([
roots_of_unity_reduced[reverse_bits(missing_cell_id, CELLS_PER_EXT_BLOB)]
for missing_cell_id in missing_cell_ids
roots_of_unity_reduced[reverse_bits(missing_cell_index, CELLS_PER_EXT_BLOB)]
for missing_cell_index in missing_cell_indices
])

# Extend vanishing polynomial to full domain using the closed form of the vanishing polynomial over a coset
Expand All @@ -592,12 +592,12 @@ def construct_vanishing_polynomial(missing_cell_ids: Sequence[CellID]) -> Tuple[
zero_poly_eval_brp = bit_reversal_permutation(zero_poly_eval)

# Sanity check
for cell_id in range(CELLS_PER_EXT_BLOB):
start = cell_id * FIELD_ELEMENTS_PER_CELL
end = (cell_id + 1) * FIELD_ELEMENTS_PER_CELL
if cell_id in missing_cell_ids:
for cell_index in range(CELLS_PER_EXT_BLOB):
start = cell_index * FIELD_ELEMENTS_PER_CELL
end = (cell_index + 1) * FIELD_ELEMENTS_PER_CELL
if cell_index in missing_cell_indices:
assert all(a == 0 for a in zero_poly_eval_brp[start:end])
else: # cell_id in cell_ids
else: # cell_index in cell_indices
assert all(a != 0 for a in zero_poly_eval_brp[start:end])

return zero_poly_coeff, zero_poly_eval
Expand All @@ -606,7 +606,7 @@ def construct_vanishing_polynomial(missing_cell_ids: Sequence[CellID]) -> Tuple[
### `recover_shifted_data`

```python
def recover_shifted_data(cell_ids: Sequence[CellID],
def recover_shifted_data(cell_indices: Sequence[CellIndex],
cells: Sequence[Cell],
zero_poly_eval: Sequence[BLSFieldElement],
zero_poly_coeff: Sequence[BLSFieldElement],
Expand All @@ -621,9 +621,9 @@ def recover_shifted_data(cell_ids: Sequence[CellID],
shift_inv = div(BLSFieldElement(1), shift_factor)

extended_evaluation_rbo = [0] * FIELD_ELEMENTS_PER_EXT_BLOB
for cell_id, cell in zip(cell_ids, cells):
start = cell_id * FIELD_ELEMENTS_PER_CELL
end = (cell_id + 1) * FIELD_ELEMENTS_PER_CELL
for cell_index, cell in zip(cell_indices, cells):
start = cell_index * FIELD_ELEMENTS_PER_CELL
end = (cell_index + 1) * FIELD_ELEMENTS_PER_CELL
extended_evaluation_rbo[start:end] = cell
extended_evaluation = bit_reversal_permutation(extended_evaluation_rbo)

Expand Down Expand Up @@ -673,7 +673,7 @@ def recover_original_data(eval_shifted_extended_evaluation: Sequence[BLSFieldEle
### `recover_cells_and_kzg_proofs`

```python
def recover_cells_and_kzg_proofs(cell_ids: Sequence[CellID],
def recover_cells_and_kzg_proofs(cell_indices: Sequence[CellIndex],
cells: Sequence[Cell],
proofs_bytes: Sequence[Bytes48]) -> Tuple[
Vector[Cell, CELLS_PER_EXT_BLOB],
Expand All @@ -689,14 +689,14 @@ def recover_cells_and_kzg_proofs(cell_ids: Sequence[CellID],
Public method.
"""
assert len(cell_ids) == len(cells) == len(proofs_bytes)
assert len(cell_indices) == len(cells) == len(proofs_bytes)
# Check we have enough cells to be able to perform the reconstruction
assert CELLS_PER_EXT_BLOB / 2 <= len(cell_ids) <= CELLS_PER_EXT_BLOB
assert CELLS_PER_EXT_BLOB / 2 <= len(cell_indices) <= CELLS_PER_EXT_BLOB
# Check for duplicates
assert len(cell_ids) == len(set(cell_ids))
# Check that the cell ids are within bounds
for cell_id in cell_ids:
assert cell_id < CELLS_PER_EXT_BLOB
assert len(cell_indices) == len(set(cell_indices))
# Check that the cell indices are within bounds
for cell_index in cell_indices:
assert cell_index < CELLS_PER_EXT_BLOB
# Check that each cell is the correct length
for cell in cells:
assert len(cell) == BYTES_PER_CELL
Expand All @@ -710,11 +710,12 @@ def recover_cells_and_kzg_proofs(cell_ids: Sequence[CellID],
# Convert cells to coset evals
cosets_evals = [cell_to_coset_evals(cell) for cell in cells]

missing_cell_ids = [CellID(cell_id) for cell_id in range(CELLS_PER_EXT_BLOB) if cell_id not in cell_ids]
zero_poly_coeff, zero_poly_eval = construct_vanishing_polynomial(missing_cell_ids)
missing_cell_indices = [CellIndex(cell_index) for cell_index in range(CELLS_PER_EXT_BLOB)
if cell_index not in cell_indices]
zero_poly_coeff, zero_poly_eval = construct_vanishing_polynomial(missing_cell_indices)

eval_shifted_extended_evaluation, eval_shifted_zero_poly, shift_inv = recover_shifted_data(
cell_ids,
cell_indices,
cosets_evals,
zero_poly_eval,
zero_poly_coeff,
Expand All @@ -728,9 +729,9 @@ def recover_cells_and_kzg_proofs(cell_ids: Sequence[CellID],
roots_of_unity_extended,
)

for cell_id, coset_evals in zip(cell_ids, cosets_evals):
start = cell_id * FIELD_ELEMENTS_PER_CELL
end = (cell_id + 1) * FIELD_ELEMENTS_PER_CELL
for cell_index, coset_evals in zip(cell_indices, cosets_evals):
start = cell_index * FIELD_ELEMENTS_PER_CELL
end = (cell_index + 1) * FIELD_ELEMENTS_PER_CELL
assert reconstructed_data[start:end] == coset_evals

recovered_cells = [
Expand All @@ -740,11 +741,11 @@ def recover_cells_and_kzg_proofs(cell_ids: Sequence[CellID],
polynomial_eval = reconstructed_data[:FIELD_ELEMENTS_PER_BLOB]
polynomial_coeff = polynomial_eval_to_coeff(polynomial_eval)
recovered_proofs = [None] * CELLS_PER_EXT_BLOB
for i, cell_id in enumerate(cell_ids):
recovered_proofs[cell_id] = bytes_to_kzg_proof(proofs_bytes[i])
for i, cell_index in enumerate(cell_indices):
recovered_proofs[cell_index] = bytes_to_kzg_proof(proofs_bytes[i])
for i in range(CELLS_PER_EXT_BLOB):
if recovered_proofs[i] is None:
coset = coset_for_cell(CellID(i))
coset = coset_for_cell(CellIndex(i))
proof, ys = compute_kzg_proof_multi_impl(polynomial_coeff, coset)
assert coset_evals_to_cell(ys) == recovered_cells[i]
recovered_proofs[i] = proof
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ def test_verify_cell_kzg_proof(spec):
commitment = spec.blob_to_kzg_commitment(blob)
cells, proofs = spec.compute_cells_and_kzg_proofs(blob)

cell_id = 0
assert spec.verify_cell_kzg_proof(commitment, cell_id, cells[cell_id], proofs[cell_id])
cell_id = 1
assert spec.verify_cell_kzg_proof(commitment, cell_id, cells[cell_id], proofs[cell_id])
cell_index = 0
assert spec.verify_cell_kzg_proof(commitment, cell_index, cells[cell_index], proofs[cell_index])
cell_index = 1
assert spec.verify_cell_kzg_proof(commitment, cell_index, cells[cell_index], proofs[cell_index])


@with_eip7594_and_later
Expand Down Expand Up @@ -77,19 +77,19 @@ def test_recover_cells_and_kzg_proofs(spec):
cells, proofs = spec.compute_cells_and_kzg_proofs(blob)

# Compute the cells we will be recovering from
cell_ids = []
cell_indices = []
# First figure out just the indices of the cells
for i in range(N_SAMPLES):
j = rng.randint(0, spec.CELLS_PER_EXT_BLOB - 1)
while j in cell_ids:
while j in cell_indices:
j = rng.randint(0, spec.CELLS_PER_EXT_BLOB - 1)
cell_ids.append(j)
cell_indices.append(j)
# Now the cells/proofs themselves
known_cells = [cells[cell_id] for cell_id in cell_ids]
known_proofs = [proofs[cell_id] for cell_id in cell_ids]
known_cells = [cells[cell_index] for cell_index in cell_indices]
known_proofs = [proofs[cell_index] for cell_index in cell_indices]

# Recover the missing cells and proofs
recovered_cells, recovered_proofs = spec.recover_cells_and_kzg_proofs(cell_ids, known_cells, known_proofs)
recovered_cells, recovered_proofs = spec.recover_cells_and_kzg_proofs(cell_indices, known_cells, known_proofs)
recovered_data = [x for xs in recovered_cells for x in xs]

# Check that the original data match the non-extended portion of the recovered data
Expand Down
6 changes: 3 additions & 3 deletions tests/formats/kzg_7594/recover_cells_and_kzg_proofs.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ The test data is declared in a `data.yaml` file:

```yaml
input:
cell_ids: List[CellID] -- the cell identifier for each cell
cell_indices: List[CellIndex] -- the cell indices
cells: List[Cell] -- the partial collection of cells
output: Tuple[List[Cell], List[KZGProof]] -- all cells and proofs
```

- `CellID` is an unsigned 64-bit integer.
- `CellIndex` is an unsigned 64-bit integer.
- `Cell` is a 2048-byte hexadecimal string, prefixed with `0x`.
- `KZGProof` is a 48-byte hexadecimal string, prefixed with `0x`.

All byte(s) fields are encoded as strings, hexadecimal encoding, prefixed with `0x`.

## Condition

The `recover_cells_and_kzg_proofs` handler should recover missing cells and proofs, and the result should match the expected `output`. If any cell is invalid (e.g. incorrect length or one of the 32-byte blocks does not represent a BLS field element), any proof is invalid (e.g. not on the curve or not in the G1 subgroup of the BLS curve), or any `cell_id` is invalid (e.g. greater than the number of cells for an extended blob), it should error, i.e. the output should be `null`.
The `recover_cells_and_kzg_proofs` handler should recover missing cells and proofs, and the result should match the expected `output`. If any cell is invalid (e.g. incorrect length or one of the 32-byte blocks does not represent a BLS field element), any proof is invalid (e.g. not on the curve or not in the G1 subgroup of the BLS curve), or any `cell_index` is invalid (e.g. greater than the number of cells for an extended blob), it should error, i.e. the output should be `null`.
6 changes: 3 additions & 3 deletions tests/formats/kzg_7594/verify_cell_kzg_proof.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ The test data is declared in a `data.yaml` file:
```yaml
input:
commitment: Bytes48 -- the KZG commitment
cell_id: CellID -- the identifier for the cell
cell_index: CellIndex -- the cell index
cell: Cell -- the cell
proof: Bytes48 -- the KZG proof for the cell
output: bool -- true (correct proof) or false (incorrect proof)
```

- `Bytes48` is a 48-byte hexadecimal string, prefixed with `0x`.
- `CellID` is an unsigned 64-bit integer.
- `CellIndex` is an unsigned 64-bit integer.
- `Cell` is a 2048-byte hexadecimal string, prefixed with `0x`.

All byte(s) fields are encoded as strings, hexadecimal encoding, prefixed with `0x`.

## Condition

The `verify_cell_kzg_proof` handler should verify that `commitment` is a correct KZG commitment to `cell` by using the cell KZG proof `proof`, and the result should match the expected `output`. If the commitment or proof is invalid (e.g. not on the curve or not in the G1 subgroup of the BLS curve), `cell` is invalid (e.g. incorrect length or one of the 32-byte blocks does not represent a BLS field element), or `cell_id` is invalid (e.g. greater than the number of cells for an extended blob), it should error, i.e. the output should be `null`.
The `verify_cell_kzg_proof` handler should verify that `commitment` is a correct KZG commitment to `cell` by using the cell KZG proof `proof`, and the result should match the expected `output`. If the commitment or proof is invalid (e.g. not on the curve or not in the G1 subgroup of the BLS curve), `cell` is invalid (e.g. incorrect length or one of the 32-byte blocks does not represent a BLS field element), or `cell_index` is invalid (e.g. greater than the number of cells for an extended blob), it should error, i.e. the output should be `null`.
2 changes: 1 addition & 1 deletion tests/formats/kzg_7594/verify_cell_kzg_proof_batch.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ All byte(s) fields are encoded as strings, hexadecimal encoding, prefixed with `

## Condition

The `verify_cell_kzg_proof_batch` handler should verify that `row_commitments` are correct KZG commitments to `cells` by using the cell KZG proofs `proofs`, and the result should match the expected `output`. If any of the commitments or proofs are invalid (e.g. not on the curve or not in the G1 subgroup of the BLS curve), any cell is invalid (e.g. incorrect length or one of the 32-byte blocks does not represent a BLS field element), or any `cell_id` is invalid (e.g. greater than the number of cells for an extended blob), it should error, i.e. the output should be `null`.
The `verify_cell_kzg_proof_batch` handler should verify that `row_commitments` are correct KZG commitments to `cells` by using the cell KZG proofs `proofs`, and the result should match the expected `output`. If any of the commitments or proofs are invalid (e.g. not on the curve or not in the G1 subgroup of the BLS curve), any cell is invalid (e.g. incorrect length or one of the 32-byte blocks does not represent a BLS field element), or any `cell_index` is invalid (e.g. greater than the number of cells for an extended blob), it should error, i.e. the output should be `null`.
Loading

0 comments on commit 7b7ada7

Please sign in to comment.