diff --git a/nomos-da/kzgrs-backend/src/common.rs b/nomos-da/kzgrs-backend/src/common.rs index 4b52c1e8..6986fe81 100644 --- a/nomos-da/kzgrs-backend/src/common.rs +++ b/nomos-da/kzgrs-backend/src/common.rs @@ -2,7 +2,7 @@ pub struct Chunk(Vec); pub struct Row(Vec); pub struct Column(Vec); -struct ChunksMatrix(Vec); +pub struct ChunksMatrix(Vec); impl Chunk { pub fn as_bytes(&self) -> Vec { @@ -14,15 +14,27 @@ impl Chunk { } } +impl From<&[u8]> for Chunk { + fn from(value: &[u8]) -> Self { + Self(value.to_vec()) + } +} + impl Row { pub fn as_bytes(&self) -> Vec { - self.0.iter().map(Chunk::as_bytes).flatten().collect() + self.0.iter().flat_map(Chunk::as_bytes).collect() } } impl Column { pub fn as_bytes(&self) -> Vec { - self.0.iter().map(Chunk::as_bytes).flatten().collect() + self.0.iter().flat_map(Chunk::as_bytes).collect() + } +} + +impl FromIterator for Row { + fn from_iter>(iter: T) -> Self { + Self(iter.into_iter().collect()) } } @@ -47,3 +59,9 @@ impl ChunksMatrix { Self(self.columns().map(|c| Row(c.0)).collect()) } } + +impl FromIterator for ChunksMatrix { + fn from_iter>(iter: T) -> Self { + Self(iter.into_iter().collect()) + } +} diff --git a/nomos-da/kzgrs-backend/src/encoder.rs b/nomos-da/kzgrs-backend/src/encoder.rs new file mode 100644 index 00000000..567afc45 --- /dev/null +++ b/nomos-da/kzgrs-backend/src/encoder.rs @@ -0,0 +1,51 @@ +use crate::common::{Chunk, ChunksMatrix}; +use kzgrs::{Commitment, Proof, BYTES_PER_FIELD_ELEMENT}; + +pub struct DaEncoderParams { + column_count: usize, + bytes_per_chunk: usize, +} + +impl DaEncoderParams { + const MAX_BLS12_381_ENCODING_CHUNK_SIZE: usize = 31; + + const fn default_with(column_count: usize) -> Self { + Self { + column_count, + bytes_per_chunk: Self::MAX_BLS12_381_ENCODING_CHUNK_SIZE, + } + } +} + +pub struct EncodedData { + data: Vec, + chunked_data: ChunksMatrix, + extended_data: ChunksMatrix, + row_commitments: Vec, + row_proofs: Vec>, + column_commitments: Vec, + aggregated_column_commitment: Commitment, + aggregated_column_proofs: Vec, +} + +struct DaEncoder { + params: DaEncoderParams, +} + +impl DaEncoder { + pub const fn new(settings: DaEncoderParams) -> Self { + assert!(settings.bytes_per_chunk < BYTES_PER_FIELD_ELEMENT); + Self { params: settings } + } + + fn chunkify(&self, data: &[u8]) -> ChunksMatrix { + let size = self.params.column_count * self.params.bytes_per_chunk; + data.windows(size) + .map(|d| { + d.windows(self.params.bytes_per_chunk) + .map(Chunk::from) + .collect() + }) + .collect() + } +} diff --git a/nomos-da/kzgrs/src/lib.rs b/nomos-da/kzgrs/src/lib.rs index 130b2090..49521bf4 100644 --- a/nomos-da/kzgrs/src/lib.rs +++ b/nomos-da/kzgrs/src/lib.rs @@ -1,3 +1,17 @@ pub mod common; pub mod kzg; pub mod rs; + +use ark_bls12_381::{Bls12_381, Fr}; +use ark_poly_commit::kzg10; +use std::mem; + +pub use common::{bytes_to_evaluations, bytes_to_polynomial, KzgRsError}; +pub use kzg::{commit_polynomial, generate_element_proof, verify_element_proof}; +pub use rs::{decode, encode}; + +pub type Commitment = kzg10::Commitment; +pub type Proof = kzg10::Proof; +pub type FieldElement = ark_bls12_381::Fr; + +pub const BYTES_PER_FIELD_ELEMENT: usize = mem::size_of::();