Skip to content

Commit

Permalink
Refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
markopoloparadox committed Oct 2, 2023
1 parent 6c24f3c commit cf958f7
Showing 1 changed file with 18 additions and 25 deletions.
43 changes: 18 additions & 25 deletions kate/src/com.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,40 +380,21 @@ pub fn build_proof<M: Metrics>(
.zip(result_bytes.par_chunks_exact_mut(SPROOF_SIZE));

let locked_errors = Mutex::new(Vec::<Error>::new());
cell_iter.for_each(|(cell, res)| {
let Ok(r_index) = usize::try_from(cell.row.0) else {
if let Ok(mut errors) = locked_errors.lock() {
errors.push(Error::ConversionFailed)
}
return;
};

let get_cell_row = |cell: &Cell| -> Result<(Vec<BlsScalar>, usize, usize), Error> {
let r_index = usize::try_from(cell.row.0)?;
if r_index >= ext_rows || cell.col >= block_dims.cols {
if let Ok(mut errors) = locked_errors.lock() {
errors.push(Error::IndexOutOfRange)
}
return;
return Err(Error::IndexOutOfRange);
}

let Ok(c_index) = usize::try_from(cell.col.0) else {
if let Ok(mut errors) = locked_errors.lock() {
errors.push(Error::ConversionFailed)
}
return;
};
let c_index = usize::try_from(cell.col.0)?;

let get_ext_data_matrix =
|j: usize| ext_data_matrix[r_index.saturating_add(j.saturating_mul(ext_rows))];

// construct polynomial per extended matrix row
#[cfg(feature = "parallel")]
let row: Vec<BlsScalar> = {
let Some(capacity) = ext_cols.checked_add(1) else {
if let Ok(mut errors) = locked_errors.lock() {
errors.push(Error::BlockTooBig)
};
return;
};
let mut row = Vec::with_capacity(capacity);
let mut row = Vec::with_capacity(ext_cols.checked_add(1).ok_or(Error::BlockTooBig)?);
(0..ext_cols)
.into_par_iter()
.map(get_ext_data_matrix)
Expand All @@ -425,6 +406,18 @@ pub fn build_proof<M: Metrics>(
.map(get_ext_data_matrix)
.collect::<Vec<BlsScalar>>();

Ok((row, r_index, c_index))
};

cell_iter.for_each(|(cell, res)| {
let result = get_cell_row(cell);
let Ok((row, r_index, c_index)) = result else {
if let Ok(mut errors) = locked_errors.lock() {
errors.push(result.err().expect("We checked before that this is OK. "))
}
return;
};

// row has to be a power of 2, otherwise interpolate() function panics TODO: cache evaluations
let poly = Evaluations::from_vec_and_domain(row, row_eval_domain).interpolate();
let witness = prover_key.compute_single_witness(&poly, &row_dom_x_pts[c_index]);
Expand Down

0 comments on commit cf958f7

Please sign in to comment.