diff --git a/kate/benches/reconstruct.rs b/kate/benches/reconstruct.rs index 62464b2..214a00a 100644 --- a/kate/benches/reconstruct.rs +++ b/kate/benches/reconstruct.rs @@ -70,7 +70,7 @@ fn sample_cells_from_matrix(matrix: &DMatrix, columns: Option<&[u16]> let position = Position::new(row_pos, col_idx); debug_assert!(*row_idx < col_view.len()); let data = col_view[*row_idx].to_bytes(); - DataCell::new(position, data.unwrap()) + DataCell::new(position, data.unwrap().to_vec()) }) .collect::>() }) diff --git a/kate/recovery/src/com.rs b/kate/recovery/src/com.rs index 5f8135a..f9eb1e7 100644 --- a/kate/recovery/src/com.rs +++ b/kate/recovery/src/com.rs @@ -217,10 +217,16 @@ pub fn reconstruct_extrinsics( #[cfg(feature = "std")] pub fn reconstruct_columns( dimensions: matrix::Dimensions, - cells: &[data::SingleCell], + cells: &[data::Cell], ) -> Result>, ReconstructionError> { // Convert cells into DataCells - let data_cells: Vec = cells.iter().cloned().map(Into::into).collect(); + let data_cells: Vec = cells + .iter() + .map(|cell| match cell { + data::Cell::SingleCell(sc) => data::DataCell::from(sc.clone()), + data::Cell::MultiProofCell(mc) => data::DataCell::from(mc.clone()), + }) + .collect(); // Map cells by column let columns = map_cells(dimensions, data_cells)?; @@ -329,7 +335,7 @@ pub fn decode_app_extrinsics( .filter(|cell| !cell.data.is_empty()) { None => app_data.extend(vec![0; CHUNK_SIZE]), - Some(cell) => app_data.extend(cell.data), + Some(cell) => app_data.extend(cell.data.clone()), } } @@ -897,19 +903,19 @@ mod tests { let cells = vec![ DataCell { position: Position { row: 0, col: 0 }, - data: coded[0].to_bytes().unwrap(), + data: coded[0].to_bytes().unwrap().to_vec(), }, DataCell { position: Position { row: 4, col: 0 }, - data: coded[4].to_bytes().unwrap(), + data: coded[4].to_bytes().unwrap().to_vec(), }, DataCell { position: Position { row: 6, col: 0 }, - data: coded[6].to_bytes().unwrap(), + data: coded[6].to_bytes().unwrap().to_vec(), }, DataCell { position: Position { row: 2, col: 0 }, - data: coded[2].to_bytes().unwrap(), + data: coded[2].to_bytes().unwrap().to_vec(), }, ]; @@ -935,19 +941,19 @@ mod tests { let cells = vec![ DataCell { position: Position { row: 0, col: 0 }, - data: coded[0].to_bytes().unwrap(), + data: coded[0].to_bytes().unwrap().to_vec(), }, DataCell { position: Position { row: 0, col: 0 }, - data: coded[0].to_bytes().unwrap(), + data: coded[0].to_bytes().unwrap().to_vec(), }, DataCell { position: Position { row: 6, col: 0 }, - data: coded[6].to_bytes().unwrap(), + data: coded[6].to_bytes().unwrap().to_vec(), }, DataCell { position: Position { row: 2, col: 0 }, - data: coded[2].to_bytes().unwrap(), + data: coded[2].to_bytes().unwrap().to_vec(), }, ]; @@ -968,15 +974,15 @@ mod tests { let cells = vec![ DataCell { position: Position { row: 4, col: 0 }, - data: coded[4].to_bytes().unwrap(), + data: coded[4].to_bytes().unwrap().to_vec(), }, DataCell { position: Position { row: 6, col: 0 }, - data: coded[6].to_bytes().unwrap(), + data: coded[6].to_bytes().unwrap().to_vec(), }, DataCell { position: Position { row: 2, col: 0 }, - data: coded[2].to_bytes().unwrap(), + data: coded[2].to_bytes().unwrap().to_vec(), }, ]; @@ -997,19 +1003,19 @@ mod tests { let cells = vec![ DataCell { position: Position { row: 0, col: 0 }, - data: coded[0].to_bytes().unwrap(), + data: coded[0].to_bytes().unwrap().to_vec(), }, DataCell { position: Position { row: 5, col: 0 }, - data: coded[4].to_bytes().unwrap(), + data: coded[4].to_bytes().unwrap().to_vec(), }, DataCell { position: Position { row: 6, col: 0 }, - data: coded[6].to_bytes().unwrap(), + data: coded[6].to_bytes().unwrap().to_vec(), }, DataCell { position: Position { row: 2, col: 0 }, - data: coded[2].to_bytes().unwrap(), + data: coded[2].to_bytes().unwrap().to_vec(), }, ]; diff --git a/kate/recovery/src/data.rs b/kate/recovery/src/data.rs index c074c03..4ceb1a2 100644 --- a/kate/recovery/src/data.rs +++ b/kate/recovery/src/data.rs @@ -15,10 +15,10 @@ use alloc::string::String; /// Position and data of a cell in extended matrix #[derive(Default, Debug, Clone, Constructor)] pub struct DataCell { - /// SingleCell's position + /// Cell's position pub position: Position, - /// SingleCell's data - pub data: [u8; 32], + /// Cell's data + pub data: Vec, } /// Position and content of a cell in extended matrix @@ -281,25 +281,40 @@ impl TryFrom for SingleCell { /// Merges cells data per row. /// Cells are sorted before merge. -pub fn rows(dimensions: Dimensions, cells: &[&SingleCell]) -> Vec<(RowIndex, Vec)> { +pub fn rows(dimensions: Dimensions, cells: &[&Cell]) -> Vec<(RowIndex, Vec)> { let mut sorted_cells = cells.to_vec(); - sorted_cells - .sort_by(|a, b| (a.position.row, a.position.col).cmp(&(b.position.row, b.position.col))); + sorted_cells.sort_by(|a, b| { + (a.position().row, a.position().col).cmp(&(b.position().row, b.position().col)) + }); let mut rows = BTreeMap::new(); for cell in sorted_cells { - rows.entry(RowIndex(cell.position.row)) - .or_insert_with(Vec::default) + let row_index = RowIndex(cell.position().row); + rows.entry(row_index) + .or_insert_with(Vec::new) .extend(cell.data()); } rows.retain(|_, row| row.len() == dimensions.row_byte_size()); - rows.into_iter().collect::>() + rows.into_iter().collect() } +/// Converts a `SingleCell` into a `DataCell`.`position` here refers +/// to the index of a cell in the main grid. impl From for DataCell { fn from(cell: SingleCell) -> Self { + DataCell { + position: cell.position, + data: cell.data().to_vec(), + } + } +} + +/// Converts a `MultiProofCell` into a `DataCell`.`position` here refers +/// to the index of a multiproof cell on the target multiproof grid. +impl From for DataCell { + fn from(cell: MultiProofCell) -> Self { DataCell { position: cell.position, data: cell.data(), @@ -307,6 +322,15 @@ impl From for DataCell { } } +impl From for DataCell { + fn from(cell: Cell) -> Self { + match cell { + Cell::SingleCell(sc) => DataCell::from(sc), + Cell::MultiProofCell(mc) => DataCell::from(mc), + } + } +} + #[cfg(test)] mod tests { use std::convert::TryInto; @@ -336,13 +360,13 @@ mod tests { let dimensions = Dimensions::new(1, 2).unwrap(); let cell_variants = vec![ - cell(position(1, 1), content([3; 32])).into(), - cell(position(1, 0), content([2; 32])).into(), - cell(position(0, 0), content([0; 32])).into(), - cell(position(0, 1), content([1; 32])).into(), + Cell::from(cell(position(1, 1), content([3; 32]))), + Cell::from(cell(position(1, 0), content([2; 32]))), + Cell::from(cell(position(0, 0), content([0; 32]))), + Cell::from(cell(position(0, 1), content([1; 32]))), ]; - let cells: Vec<&SingleCell> = cell_variants.iter().collect(); + let cells: Vec<&Cell> = cell_variants.iter().collect(); let mut rows = rows(dimensions, &cells); rows.sort_by_key(|(key, _)| key.0); @@ -363,12 +387,12 @@ mod tests { let dimensions = Dimensions::new(1, 2).unwrap(); let cell_variants = vec![ - cell(position(1, 1), content([3; 32])).into(), - cell(position(0, 0), content([0; 32])).into(), - cell(position(0, 1), content([1; 32])).into(), + Cell::from(cell(position(1, 1), content([3; 32]))), + Cell::from(cell(position(0, 0), content([0; 32]))), + Cell::from(cell(position(0, 1), content([1; 32]))), ]; - let cells: Vec<&SingleCell> = cell_variants.iter().collect(); + let cells: Vec<&Cell> = cell_variants.iter().collect(); let mut rows = rows(dimensions, &cells); rows.sort_by_key(|(key, _)| key.0); diff --git a/kate/src/com.rs b/kate/src/com.rs index 2aed9ea..0f0b80f 100644 --- a/kate/src/com.rs +++ b/kate/src/com.rs @@ -858,7 +858,7 @@ mod tests { let position = Position::new(row_pos, col_idx); debug_assert!(*row_idx < col_view.len()); let data = col_view[*row_idx].to_bytes(); - DataCell::new(position, data.unwrap()) + DataCell::new(position, data.unwrap().to_vec()) }) .collect::>() }) @@ -1083,7 +1083,7 @@ get erasure coded to ensure redundancy."#; let col: usize = position.col.into(); let row = usize::try_from(position.row).unwrap(); let data = matrix.get((row, col)).map(ArkScalar::to_bytes).unwrap(); - DataCell::new(position, data.unwrap()) + DataCell::new(position, data.unwrap().to_vec()) }) .collect::>(); let data = &decode_app_extrinsics(&index, dimensions, cells, xt.app_id).unwrap()[0]; diff --git a/kate/src/gridgen/tests/formatting.rs b/kate/src/gridgen/tests/formatting.rs index 4d15a77..acabfc3 100644 --- a/kate/src/gridgen/tests/formatting.rs +++ b/kate/src/gridgen/tests/formatting.rs @@ -129,7 +129,8 @@ get erasure coded to ensure redundancy."#; .get((pos.row as usize, pos.col as usize)) .unwrap() .to_bytes() - .unwrap(), + .unwrap() + .to_vec(), }) .collect::>(); let data = &decode_app_extrinsics(&grid.lookup, bdims, cells, xt.app_id).unwrap()[0]; diff --git a/kate/src/gridgen/tests/mod.rs b/kate/src/gridgen/tests/mod.rs index 30aa760..f555be6 100644 --- a/kate/src/gridgen/tests/mod.rs +++ b/kate/src/gridgen/tests/mod.rs @@ -60,7 +60,8 @@ fn sample_cells(grid: &EvaluationGrid, columns: Option>) -> Vec