Added chunksmatrix

This commit is contained in:
Daniel Sanchez Quiros 2024-04-11 17:03:50 +03:00
parent 98d0073bea
commit 15f4f4f1b9
3 changed files with 53 additions and 3 deletions

View File

@ -20,6 +20,8 @@ members = [
"consensus/carnot-engine",
"consensus/cryptarchia-engine",
"ledger/cryptarchia-ledger",
"tests", "nomos-da/kzgrs",
"tests",
"nomos-da/kzgrs",
"nomos-da/kzgrs-backend",
]
resolver = "2"

View File

@ -0,0 +1,49 @@
#[derive(Clone)]
pub struct Chunk(Vec<u8>);
pub struct Row(Vec<Chunk>);
pub struct Column(Vec<Chunk>);
struct ChunksMatrix(Vec<Row>);
impl Chunk {
pub fn as_bytes(&self) -> Vec<u8> {
self.0.to_vec()
}
pub const fn empty() -> Self {
Self(vec![])
}
}
impl Row {
pub fn as_bytes(&self) -> Vec<u8> {
self.0.iter().map(Chunk::as_bytes).flatten().collect()
}
}
impl Column {
pub fn as_bytes(&self) -> Vec<u8> {
self.0.iter().map(Chunk::as_bytes).flatten().collect()
}
}
impl FromIterator<Chunk> for Column {
fn from_iter<T: IntoIterator<Item = Chunk>>(iter: T) -> Self {
Self(iter.into_iter().collect())
}
}
impl ChunksMatrix {
pub fn columns(&self) -> impl Iterator<Item = Column> + '_ {
let size = self.0.first().map(|r| r.0.len()).unwrap_or(0);
(0..size).map(|i| {
self.0
.iter()
.map(|row| row.0.get(i).cloned().unwrap_or_else(Chunk::empty))
.collect::<Column>()
})
}
pub fn transposed(&self) -> Self {
Self(self.columns().map(|c| Row(c.0)).collect())
}
}

View File

@ -88,8 +88,7 @@ pub fn points_to_bytes<const CHUNK_SIZE: usize>(points: &[Fr]) -> Vec<u8> {
}
points
.iter()
.map(point_to_buff::<CHUNK_SIZE>)
.flatten()
.flat_map(point_to_buff::<CHUNK_SIZE>)
.collect()
}