Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion kate/benches/reconstruct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn sample_cells_from_matrix(matrix: &DMatrix<ArkScalar>, 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::<Vec<_>>()
})
Expand Down
42 changes: 24 additions & 18 deletions kate/recovery/src/com.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HashMap<u16, Vec<[u8; CHUNK_SIZE]>>, ReconstructionError> {
// Convert cells into DataCells
let data_cells: Vec<data::DataCell> = cells.iter().cloned().map(Into::into).collect();
let data_cells: Vec<data::DataCell> = 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)?;
Expand Down Expand Up @@ -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()),
}
}

Expand Down Expand Up @@ -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(),
},
];

Expand All @@ -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(),
},
];

Expand All @@ -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(),
},
];

Expand All @@ -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(),
},
];

Expand Down
60 changes: 42 additions & 18 deletions kate/recovery/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>,
}

/// Position and content of a cell in extended matrix
Expand Down Expand Up @@ -281,32 +281,56 @@ impl TryFrom<Cell> for SingleCell {

/// Merges cells data per row.
/// Cells are sorted before merge.
pub fn rows(dimensions: Dimensions, cells: &[&SingleCell]) -> Vec<(RowIndex, Vec<u8>)> {
pub fn rows(dimensions: Dimensions, cells: &[&Cell]) -> Vec<(RowIndex, Vec<u8>)> {
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::<Vec<(_, _)>>()
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<SingleCell> 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<MultiProofCell> for DataCell {
fn from(cell: MultiProofCell) -> Self {
DataCell {
position: cell.position,
data: cell.data(),
}
}
}

impl From<Cell> 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;
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions kate/src/com.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>()
})
Expand Down Expand Up @@ -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::<Vec<_>>();
let data = &decode_app_extrinsics(&index, dimensions, cells, xt.app_id).unwrap()[0];
Expand Down
3 changes: 2 additions & 1 deletion kate/src/gridgen/tests/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();
let data = &decode_app_extrinsics(&grid.lookup, bdims, cells, xt.app_id).unwrap()[0];
Expand Down
3 changes: 2 additions & 1 deletion kate/src/gridgen/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ fn sample_cells(grid: &EvaluationGrid, columns: Option<Vec<usize>>) -> Vec<DataC
.evals
.get((y, *x))
.and_then(|s: &ArkScalar| s.to_bytes().ok())
.unwrap();
.unwrap()
.to_vec();
// SAFETY: `y` and `x` can be casted safetly becasue `x < g_cols (u16)` and `y
// < g_rows(u16)`
let position = Position::from((y as u32, *x as u16));
Expand Down
Loading