Skip to content

Commit a4331e7

Browse files
committed
Public methods must accept raw bytes
1 parent b4d418d commit a4331e7

File tree

2 files changed

+55
-20
lines changed

2 files changed

+55
-20
lines changed

specs/_features/eip7594/polynomial-commitments-sampling.md

+38-12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
- [Preset](#preset)
1313
- [Cells](#cells)
1414
- [Helper functions](#helper-functions)
15+
- [BLS12-381 helpers](#bls12-381-helpers)
16+
- [`bytes_to_cell`](#bytes_to_cell)
1517
- [Linear combinations](#linear-combinations)
1618
- [`g2_lincomb`](#g2_lincomb)
1719
- [FFTs](#ffts)
@@ -81,6 +83,18 @@ Cells are the smallest unit of blob data that can come with their own KZG proofs
8183

8284
## Helper functions
8385

86+
### BLS12-381 helpers
87+
88+
#### `bytes_to_cell`
89+
90+
```python
91+
def bytes_to_cell(cell_bytes: Vector[Bytes32, FIELD_ELEMENTS_PER_CELL]) -> Cell:
92+
"""
93+
Convert untrusted bytes into a Cell.
94+
"""
95+
return [bytes_to_bls_field(element) for element in cell_bytes]
96+
```
97+
8498
### Linear combinations
8599

86100
#### `g2_lincomb`
@@ -397,28 +411,32 @@ def compute_cells(blob: Blob) -> Vector[Cell, CELLS_PER_BLOB]:
397411
#### `verify_cell_proof`
398412

399413
```python
400-
def verify_cell_proof(commitment: KZGCommitment,
414+
def verify_cell_proof(commitment_bytes: Bytes48,
401415
cell_id: int,
402-
cell: Cell,
403-
proof: KZGProof) -> bool:
416+
cell_bytes: Vector[Bytes32, FIELD_ELEMENTS_PER_CELL],
417+
proof: Bytes48) -> bool:
404418
"""
405419
Check a cell proof
406420
407421
Public method.
408422
"""
409423
coset = coset_for_cell(cell_id)
410424

411-
return verify_kzg_proof_multi_impl(commitment, coset, cell, proof)
425+
return verify_kzg_proof_multi_impl(
426+
bytes_to_kzg_commitment(commitment_bytes),
427+
coset,
428+
bytes_to_cell(cell_bytes),
429+
bytes_to_kzg_proof(proof))
412430
```
413431

414432
#### `verify_cell_proof_batch`
415433

416434
```python
417-
def verify_cell_proof_batch(row_commitments: Sequence[KZGCommitment],
435+
def verify_cell_proof_batch(row_commitments_bytes: Sequence[Bytes48],
418436
row_ids: Sequence[int],
419437
column_ids: Sequence[int],
420-
cells: Sequence[Cell],
421-
proofs: Sequence[KZGProof]) -> bool:
438+
cells_bytes: Sequence[Vector[Bytes32, FIELD_ELEMENTS_PER_CELL]],
439+
proofs_bytes: Sequence[Bytes48]) -> bool:
422440
"""
423441
Check multiple cell proofs. This function implements the naive algorithm of checking every cell
424442
individually; an efficient algorithm can be found here:
@@ -430,10 +448,14 @@ def verify_cell_proof_batch(row_commitments: Sequence[KZGCommitment],
430448
431449
Public method.
432450
"""
433-
434451
# Get commitments via row IDs
435-
commitments = [row_commitments[row_id] for row_id in row_ids]
436-
452+
commitments_bytes = [row_commitments_bytes[row_id] for row_id in row_ids]
453+
454+
# Get objects from bytes
455+
commitments = [bytes_to_kzg_commitment(commitment_bytes) for commitment_bytes in commitments_bytes]
456+
cells = [bytes_to_cell(cell_bytes) for cell_bytes in cells_bytes]
457+
proofs = [bytes_to_kzg_proof(proof_bytes) for proof_bytes in proofs_bytes]
458+
437459
return all(
438460
verify_kzg_proof_multi_impl(commitment, coset_for_cell(column_id), cell, proof)
439461
for commitment, column_id, cell, proof in zip(commitments, column_ids, cells, proofs)
@@ -445,7 +467,8 @@ def verify_cell_proof_batch(row_commitments: Sequence[KZGCommitment],
445467
### `recover_polynomial`
446468

447469
```python
448-
def recover_polynomial(cell_ids: Sequence[CellID], cells: Sequence[Cell]) -> Polynomial:
470+
def recover_polynomial(cell_ids: Sequence[CellID],
471+
cells_bytes: Sequence[Vector[Bytes32, FIELD_ELEMENTS_PER_CELL]]) -> Polynomial:
449472
"""
450473
Recovers a polynomial from 2 * FIELD_ELEMENTS_PER_CELL evaluations, half of which can be missing.
451474
@@ -455,7 +478,10 @@ def recover_polynomial(cell_ids: Sequence[CellID], cells: Sequence[Cell]) -> Pol
455478
456479
Public method.
457480
"""
458-
assert len(cell_ids) == len(cells)
481+
assert len(cell_ids) == len(cells_bytes)
482+
483+
cells = [bytes_to_cell(cell_bytes) for cell_bytes in cells_bytes]
484+
459485
assert len(cells) >= CELLS_PER_BLOB // 2
460486
missing_cell_ids = [cell_id for cell_id in range(CELLS_PER_BLOB) if cell_id not in cell_ids]
461487
roots_of_unity_reduced = compute_roots_of_unity(CELLS_PER_BLOB)

tests/core/pyspec/eth2spec/test/eip7594/unittests/polynomial_commitments/test_polynomial_commitments.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
from eth2spec.utils.bls import BLS_MODULUS
1111

1212

13+
def field_element_bytes(x):
14+
return int.to_bytes(x % BLS_MODULUS, 32, "big")
15+
16+
1317
@with_eip7594_and_later
1418
@spec_test
1519
@single_phase
@@ -34,10 +38,13 @@ def test_verify_cell_proof(spec):
3438
blob = get_sample_blob(spec)
3539
commitment = spec.blob_to_kzg_commitment(blob)
3640
cells, proofs = spec.compute_cells_and_proofs(blob)
41+
42+
cells_bytes = [[field_element_bytes(element) for element in cell] for cell in cells]
43+
3744
cell_id = 0
38-
assert spec.verify_cell_proof(commitment, cell_id, cells[cell_id], proofs[cell_id])
45+
assert spec.verify_cell_proof(commitment, cell_id, cells_bytes[cell_id], proofs[cell_id])
3946
cell_id = 1
40-
assert spec.verify_cell_proof(commitment, cell_id, cells[cell_id], proofs[cell_id])
47+
assert spec.verify_cell_proof(commitment, cell_id, cells_bytes[cell_id], proofs[cell_id])
4148

4249

4350
@with_eip7594_and_later
@@ -48,12 +55,14 @@ def test_verify_cell_proof_batch(spec):
4855
commitment = spec.blob_to_kzg_commitment(blob)
4956
cells, proofs = spec.compute_cells_and_proofs(blob)
5057

58+
cells_bytes = [[field_element_bytes(element) for element in cell] for cell in cells]
59+
5160
assert spec.verify_cell_proof_batch(
52-
row_commitments=[commitment],
61+
row_commitments_bytes=[commitment],
5362
row_ids=[0],
5463
column_ids=[0, 1],
55-
cells=cells[0:1],
56-
proofs=proofs,
64+
cells_bytes=cells_bytes[0:1],
65+
proofs_bytes=proofs,
5766
)
5867

5968

@@ -73,21 +82,21 @@ def test_recover_polynomial(spec):
7382

7483
# Extend data with Reed-Solomon and split the extended data in cells
7584
cells = spec.compute_cells(blob)
85+
cells_bytes = [[field_element_bytes(element) for element in cell] for cell in cells]
7686

7787
# Compute the cells we will be recovering from
7888
cell_ids = []
79-
known_cells = []
8089
# First figure out just the indices of the cells
8190
for i in range(N_SAMPLES):
8291
j = rng.randint(0, spec.CELLS_PER_BLOB)
8392
while j in cell_ids:
8493
j = rng.randint(0, spec.CELLS_PER_BLOB)
8594
cell_ids.append(j)
8695
# Now the cells themselves
87-
known_cells = [cells[cell_id] for cell_id in cell_ids]
96+
known_cells_bytes = [cells_bytes[cell_id] for cell_id in cell_ids]
8897

8998
# Recover the data
90-
recovered_data = spec.recover_polynomial(cell_ids, known_cells)
99+
recovered_data = spec.recover_polynomial(cell_ids, known_cells_bytes)
91100

92101
# Check that the original data match the non-extended portion of the recovered data
93102
assert original_polynomial == recovered_data[:len(recovered_data) // 2]

0 commit comments

Comments
 (0)