2024-02-12 14:35:23 +00:00
|
|
|
from dataclasses import dataclass
|
2024-03-08 13:16:14 +00:00
|
|
|
from itertools import batched, chain
|
|
|
|
from typing import List, Sequence, Tuple
|
|
|
|
from hashlib import sha3_256
|
2024-02-12 14:35:23 +00:00
|
|
|
|
2024-03-08 13:16:14 +00:00
|
|
|
from eth2spec.eip7594.mainnet import KZGCommitment as Commitment, KZGProof as Proof, BLSFieldElement
|
|
|
|
|
|
|
|
from da.common import ChunksMatrix, Chunk, Row, Column
|
|
|
|
from da.kzg_rs import kzg, rs
|
2024-03-20 10:03:39 +00:00
|
|
|
from da.kzg_rs.common import GLOBAL_PARAMETERS, ROOTS_OF_UNITY, BLS_MODULUS, BYTES_PER_FIELD_ELEMENT
|
2024-03-08 13:16:14 +00:00
|
|
|
from da.kzg_rs.poly import Polynomial
|
2024-02-12 14:35:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class DAEncoderParams:
|
|
|
|
column_count: int
|
2024-03-20 10:03:39 +00:00
|
|
|
bytes_per_chunk: int
|
2024-02-12 14:35:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class EncodedData:
|
2024-03-08 13:16:14 +00:00
|
|
|
data: bytes
|
2024-03-11 09:01:34 +00:00
|
|
|
chunked_data: ChunksMatrix
|
2024-02-12 14:35:23 +00:00
|
|
|
extended_matrix: ChunksMatrix
|
|
|
|
row_commitments: List[Commitment]
|
|
|
|
row_proofs: List[List[Proof]]
|
|
|
|
column_commitments: List[Commitment]
|
|
|
|
aggregated_column_commitment: Commitment
|
|
|
|
aggregated_column_proofs: List[Proof]
|
|
|
|
|
|
|
|
|
|
|
|
class DAEncoder:
|
|
|
|
def __init__(self, params: DAEncoderParams):
|
2024-03-20 10:03:39 +00:00
|
|
|
# we can only encode up to 31 bytes per element which fits without problem in a 32 byte element
|
|
|
|
assert params.bytes_per_chunk < BYTES_PER_FIELD_ELEMENT
|
2024-02-12 14:35:23 +00:00
|
|
|
self.params = params
|
|
|
|
|
2024-03-08 13:16:14 +00:00
|
|
|
def _chunkify_data(self, data: bytes) -> ChunksMatrix:
|
2024-03-20 10:03:39 +00:00
|
|
|
size: int = self.params.column_count * self.params.bytes_per_chunk
|
2024-03-08 13:16:14 +00:00
|
|
|
return ChunksMatrix(
|
2024-03-20 10:03:39 +00:00
|
|
|
Row(Chunk(int.from_bytes(chunk, byteorder="big").to_bytes(length=BYTES_PER_FIELD_ELEMENT))
|
|
|
|
for chunk in batched(b, self.params.bytes_per_chunk)
|
|
|
|
)
|
2024-03-08 13:16:14 +00:00
|
|
|
for b in batched(data, size)
|
|
|
|
)
|
2024-02-12 14:35:23 +00:00
|
|
|
|
2024-03-20 10:03:39 +00:00
|
|
|
def _compute_row_kzg_commitments(self, matrix: ChunksMatrix) -> List[Tuple[Polynomial, Commitment]]:
|
|
|
|
return [
|
|
|
|
kzg.bytes_to_commitment(
|
|
|
|
row.as_bytes(),
|
|
|
|
GLOBAL_PARAMETERS,
|
|
|
|
)
|
|
|
|
for row in matrix
|
|
|
|
]
|
2024-02-12 14:35:23 +00:00
|
|
|
|
|
|
|
def _rs_encode_rows(self, chunks_matrix: ChunksMatrix) -> ChunksMatrix:
|
2024-03-08 13:16:14 +00:00
|
|
|
def __rs_encode_row(row: Row) -> Row:
|
|
|
|
polynomial = kzg.bytes_to_polynomial(row.as_bytes())
|
|
|
|
return Row(
|
|
|
|
Chunk(BLSFieldElement.to_bytes(
|
|
|
|
x,
|
2024-03-20 10:03:39 +00:00
|
|
|
# fixed to 32 bytes as bls_field_elements are 32bytes (256bits) encoded
|
|
|
|
length=32, byteorder="big"
|
2024-03-08 13:16:14 +00:00
|
|
|
)) for x in rs.encode(polynomial, 2, ROOTS_OF_UNITY)
|
|
|
|
)
|
|
|
|
return ChunksMatrix(__rs_encode_row(row) for row in chunks_matrix)
|
2024-02-12 14:35:23 +00:00
|
|
|
|
2024-03-08 13:16:14 +00:00
|
|
|
@staticmethod
|
|
|
|
def _compute_rows_proofs(
|
|
|
|
chunks_matrix: ChunksMatrix,
|
|
|
|
polynomials: Sequence[Polynomial],
|
|
|
|
row_commitments: Sequence[Commitment]
|
|
|
|
) -> List[List[Proof]]:
|
|
|
|
proofs = []
|
|
|
|
for row, poly, commitment in zip(chunks_matrix, polynomials, row_commitments):
|
|
|
|
proofs.append(
|
|
|
|
[
|
|
|
|
kzg.generate_element_proof(i, poly, GLOBAL_PARAMETERS, ROOTS_OF_UNITY)
|
|
|
|
for i in range(len(row))
|
|
|
|
]
|
|
|
|
)
|
|
|
|
return proofs
|
2024-02-12 14:35:23 +00:00
|
|
|
|
2024-03-08 13:16:14 +00:00
|
|
|
def _compute_column_kzg_commitments(self, chunks_matrix: ChunksMatrix) -> List[Tuple[Polynomial, Commitment]]:
|
|
|
|
return self._compute_row_kzg_commitments(chunks_matrix.transposed())
|
2024-02-12 14:35:23 +00:00
|
|
|
|
2024-03-08 13:16:14 +00:00
|
|
|
@staticmethod
|
|
|
|
def _compute_aggregated_column_commitment(
|
|
|
|
chunks_matrix: ChunksMatrix, column_commitments: Sequence[Commitment]
|
|
|
|
) -> Tuple[Polynomial, Commitment]:
|
|
|
|
data = bytes(chain.from_iterable(
|
2024-03-11 09:01:34 +00:00
|
|
|
DAEncoder.hash_column_and_commitment(column, commitment)
|
2024-03-08 13:16:14 +00:00
|
|
|
for column, commitment in zip(chunks_matrix.columns, column_commitments)
|
|
|
|
))
|
|
|
|
return kzg.bytes_to_commitment(data, GLOBAL_PARAMETERS)
|
2024-02-12 14:35:23 +00:00
|
|
|
|
2024-03-08 13:16:14 +00:00
|
|
|
@staticmethod
|
2024-02-12 14:35:23 +00:00
|
|
|
def _compute_aggregated_column_proofs(
|
2024-03-08 13:16:14 +00:00
|
|
|
polynomial: Polynomial,
|
|
|
|
column_commitments: Sequence[Commitment],
|
2024-02-12 14:35:23 +00:00
|
|
|
) -> List[Proof]:
|
2024-03-08 13:16:14 +00:00
|
|
|
return [
|
|
|
|
kzg.generate_element_proof(i, polynomial, GLOBAL_PARAMETERS, ROOTS_OF_UNITY)
|
|
|
|
for i in range(len(column_commitments))
|
|
|
|
]
|
2024-02-12 14:35:23 +00:00
|
|
|
|
2024-03-08 13:16:14 +00:00
|
|
|
def encode(self, data: bytes) -> EncodedData:
|
2024-02-12 14:35:23 +00:00
|
|
|
chunks_matrix = self._chunkify_data(data)
|
2024-03-08 13:16:14 +00:00
|
|
|
row_polynomials, row_commitments = zip(*self._compute_row_kzg_commitments(chunks_matrix))
|
2024-02-12 14:35:23 +00:00
|
|
|
extended_matrix = self._rs_encode_rows(chunks_matrix)
|
2024-03-08 13:16:14 +00:00
|
|
|
row_proofs = self._compute_rows_proofs(extended_matrix, row_polynomials, row_commitments)
|
|
|
|
column_polynomials, column_commitments = zip(*self._compute_column_kzg_commitments(extended_matrix))
|
|
|
|
aggregated_column_polynomial, aggregated_column_commitment = (
|
|
|
|
self._compute_aggregated_column_commitment(extended_matrix, column_commitments)
|
|
|
|
)
|
|
|
|
aggregated_column_proofs = self._compute_aggregated_column_proofs(
|
|
|
|
aggregated_column_polynomial, column_commitments
|
|
|
|
)
|
2024-02-12 14:35:23 +00:00
|
|
|
result = EncodedData(
|
|
|
|
data,
|
2024-03-11 09:01:34 +00:00
|
|
|
chunks_matrix,
|
2024-02-12 14:35:23 +00:00
|
|
|
extended_matrix,
|
|
|
|
row_commitments,
|
|
|
|
row_proofs,
|
|
|
|
column_commitments,
|
|
|
|
aggregated_column_commitment,
|
|
|
|
aggregated_column_proofs
|
|
|
|
)
|
|
|
|
return result
|
2024-03-08 13:16:14 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2024-03-11 09:01:34 +00:00
|
|
|
def hash_column_and_commitment(column: Column, commitment: Commitment) -> bytes:
|
2024-03-08 13:16:14 +00:00
|
|
|
# TODO: Check correctness of bytes to blsfieldelement using modulus over the hash
|
|
|
|
return (
|
|
|
|
int.from_bytes(sha3_256(column.as_bytes() + bytes(commitment)).digest()) % BLS_MODULUS
|
|
|
|
).to_bytes(32, byteorder="big")
|