Skip to content

Commit 789e24e

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

File tree

2 files changed

+53
-20
lines changed

2 files changed

+53
-20
lines changed

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

+36-12
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ Cells are the smallest unit of blob data that can come with their own KZG proofs
8181

8282
## Helper functions
8383

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

8698
#### `g2_lincomb`
@@ -397,28 +409,32 @@ def compute_cells(blob: Blob) -> Vector[Cell, CELLS_PER_BLOB]:
397409
#### `verify_cell_proof`
398410

399411
```python
400-
def verify_cell_proof(commitment: KZGCommitment,
412+
def verify_cell_proof(commitment_bytes: Bytes48,
401413
cell_id: int,
402-
cell: Cell,
403-
proof: KZGProof) -> bool:
414+
cell_bytes: Vector[Bytes32, FIELD_ELEMENTS_PER_CELL],
415+
proof: Bytes48) -> bool:
404416
"""
405417
Check a cell proof
406418
407419
Public method.
408420
"""
409421
coset = coset_for_cell(cell_id)
410422

411-
return verify_kzg_proof_multi_impl(commitment, coset, cell, proof)
423+
return verify_kzg_proof_multi_impl(
424+
bytes_to_kzg_commitment(commitment_bytes),
425+
coset,
426+
bytes_to_cell(cell_bytes),
427+
bytes_to_kzg_proof(proof))
412428
```
413429

414430
#### `verify_cell_proof_batch`
415431

416432
```python
417-
def verify_cell_proof_batch(row_commitments: Sequence[KZGCommitment],
433+
def verify_cell_proof_batch(row_commitments_bytes: Sequence[Bytes48],
418434
row_ids: Sequence[int],
419435
column_ids: Sequence[int],
420-
cells: Sequence[Cell],
421-
proofs: Sequence[KZGProof]) -> bool:
436+
cells_bytes: Sequence[Vector[Bytes32, FIELD_ELEMENTS_PER_CELL]],
437+
proofs_bytes: Sequence[Bytes48]) -> bool:
422438
"""
423439
Check multiple cell proofs. This function implements the naive algorithm of checking every cell
424440
individually; an efficient algorithm can be found here:
@@ -430,10 +446,14 @@ def verify_cell_proof_batch(row_commitments: Sequence[KZGCommitment],
430446
431447
Public method.
432448
"""
433-
434449
# Get commitments via row IDs
435-
commitments = [row_commitments[row_id] for row_id in row_ids]
436-
450+
commitments_bytes = [row_commitments_bytes[row_id] for row_id in row_ids]
451+
452+
# Get objects from bytes
453+
commitments = [bytes_to_kzg_commitment(commitment_bytes) for commitment_bytes in commitments_bytes]
454+
cells = [bytes_to_cell(cell_bytes) for cell_bytes in cells_bytes]
455+
proofs = [bytes_to_kzg_proof(proof_bytes) for proof_bytes in proofs_bytes]
456+
437457
return all(
438458
verify_kzg_proof_multi_impl(commitment, coset_for_cell(column_id), cell, proof)
439459
for commitment, column_id, cell, proof in zip(commitments, column_ids, cells, proofs)
@@ -445,7 +465,8 @@ def verify_cell_proof_batch(row_commitments: Sequence[KZGCommitment],
445465
### `recover_polynomial`
446466

447467
```python
448-
def recover_polynomial(cell_ids: Sequence[CellID], cells: Sequence[Cell]) -> Polynomial:
468+
def recover_polynomial(cell_ids: Sequence[CellID],
469+
cells_bytes: Sequence[Vector[Bytes32, FIELD_ELEMENTS_PER_CELL]]) -> Polynomial:
449470
"""
450471
Recovers a polynomial from 2 * FIELD_ELEMENTS_PER_CELL evaluations, half of which can be missing.
451472
@@ -455,7 +476,10 @@ def recover_polynomial(cell_ids: Sequence[CellID], cells: Sequence[Cell]) -> Pol
455476
456477
Public method.
457478
"""
458-
assert len(cell_ids) == len(cells)
479+
assert len(cell_ids) == len(cells_bytes)
480+
481+
cells = [bytes_to_cell(cell_bytes) for cell_bytes in cells_bytes]
482+
459483
assert len(cells) >= CELLS_PER_BLOB // 2
460484
missing_cell_ids = [cell_id for cell_id in range(CELLS_PER_BLOB) if cell_id not in cell_ids]
461485
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)