From e72935ec49f52046719e94e1c35928a8448f159d Mon Sep 17 00:00:00 2001 From: Daniel Sanchez Quiros Date: Tue, 5 Mar 2024 13:54:06 +0100 Subject: [PATCH] Added compute row commitments Added row commitments size test --- da/encoder.py | 8 +++++--- da/test_encoder.py | 18 +++++++++++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/da/encoder.py b/da/encoder.py index f6720b2..c97596f 100644 --- a/da/encoder.py +++ b/da/encoder.py @@ -1,10 +1,11 @@ from dataclasses import dataclass from itertools import batched -from typing import List +from typing import List, Sequence from eth2spec.eip7594.mainnet import KZGCommitment as Commitment, KZGProof as Proof from da.common import ChunksMatrix from da.kzg_rs import kzg, rs, poly +from da.kzg_rs.common import GLOBAL_PARAMETERS @dataclass class DAEncoderParams: @@ -31,8 +32,9 @@ class DAEncoder: size: int = self.params.column_count * self.params.bytes_per_field_element return ChunksMatrix(batched(data, size)) - def _compute_row_kzg_commitments(self, rows: List[bytearray]) -> List[Commitment]: - ... + @staticmethod + def _compute_row_kzg_commitments(rows: Sequence[bytearray]) -> List[Commitment]: + return [kzg.bytes_to_commitment(row, GLOBAL_PARAMETERS) for row in rows] def _rs_encode_rows(self, chunks_matrix: ChunksMatrix) -> ChunksMatrix: ... diff --git a/da/test_encoder.py b/da/test_encoder.py index 61404f2..b3ffc6c 100644 --- a/da/test_encoder.py +++ b/da/test_encoder.py @@ -4,7 +4,7 @@ from typing import List from unittest import TestCase from da import encoder -from da.encoder import DAEncoderParams, Commitment +from da.encoder import DAEncoderParams, Commitment, DAEncoder from eth2spec.eip7594.mainnet import BYTES_PER_FIELD_ELEMENT from da.kzg_rs.common import BLS_MODULUS @@ -12,6 +12,17 @@ from da.kzg_rs.common import BLS_MODULUS class TestEncoder(TestCase): + def setUp(self): + self.params: DAEncoderParams = DAEncoderParams(column_count=10, bytes_per_field_element=32) + self.encoder: DAEncoder = DAEncoder(self.params) + self.elements = 100 + self.data = bytearray( + chain.from_iterable( + randrange(BLS_MODULUS).to_bytes(length=self.params.bytes_per_field_element, byteorder='big') + for _ in range(self.elements) + ) + ) + def assert_encoding(self, encoder_params: DAEncoderParams, data: bytearray): encoded_data = encoder.DAEncoder(encoder_params).encode(data) self.assertEqual(encoded_data.data, data) @@ -30,9 +41,10 @@ class TestEncoder(TestCase): for column in chunks_matrix: self.assertEqual(len(column), encoder_settings.bytes_per_field_element*encoder_settings.column_count) - def test_compute_row_kzg_commitments(self): - pass + chunks_matrix = self.encoder._chunkify_data(self.data) + commitments = self.encoder._compute_row_kzg_commitments(chunks_matrix) + self.assertEqual(len(commitments), len(chunks_matrix)) def test_rs_encode_rows(self): pass