Skip to content

Commit

Permalink
graceful error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
afkbyte committed Jun 4, 2024
1 parent 8eed5f6 commit 2fe1885
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions arbitrator/prover/src/kzgbn254.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ pub fn prove_kzg_preimage_bn254(

// transform polynomial into coefficient form
let mut blob_polynomial_coefficient_form = blob_polynomial_evaluation_form.clone();
blob_polynomial_coefficient_form.transform_to_form(PolynomialFormat::InCoefficientForm)?;
match blob_polynomial_coefficient_form.transform_to_form(PolynomialFormat::InCoefficientForm) {
Ok(_) => (),
Err(err) => return Err(err.into()),
};

let blob_coefficients = blob_polynomial_coefficient_form.to_vec();
let mut blob_bytes = Vec::new();
Expand All @@ -96,7 +99,10 @@ pub fn prove_kzg_preimage_bn254(
println!("blob header {:?}", blob_header);

// decode blob header, version is currently unused however in the future we probabky
let (_, length) = decode_codec_blob_header(&blob_header);
let length = match decode_codec_blob_header(&blob_header) {
Ok((_, length)) => length,
Err(err) => return Err(err),
};

let length_usize = length as usize;

Expand All @@ -115,16 +121,26 @@ pub fn prove_kzg_preimage_bn254(
let mut padded_proving_offset_bytes: [u8; 32] = [0u8; 32];
padded_proving_offset_bytes[32 - proving_offset_bytes.len()..].copy_from_slice(&proving_offset_bytes);

let proven_y_fr = blob_polynomial_coefficient_form.get_at_index(proving_offset as usize).unwrap();
let z_fr = kzg.get_nth_root_of_unity(proving_offset as usize).unwrap();
let proven_y_fr = match blob_polynomial_coefficient_form.get_at_index(proving_offset as usize) {
Some(value) => value,
None => return Err(eyre::eyre!("Index out of bounds")),
};

let z_fr = match kzg.get_nth_root_of_unity(proving_offset as usize) {
Some(value) => value,
None => return Err(eyre::eyre!("Failed to get nth root of unity")),
};

let proven_y = proven_y_fr.into_bigint().to_bytes_be();

let g2_generator = G2Affine::generator();
let z_g2= (g2_generator * z_fr).into_affine();

// if we are loading in g2 pow2 this is index 0 not 1
let g2_tau: G2Affine = kzg.get_g2_points().get(1).unwrap().clone();
let g2_tau: G2Affine = match kzg.get_g2_points().get(1) {
Some(point) => point.clone(),
None => return Err(eyre::eyre!("Failed to get G2 point at index 1")),
};
let g2_tau_minus_g2_z = (g2_tau - z_g2).into_affine();

let kzg_proof = match kzg.compute_kzg_proof_with_roots_of_unity(&blob_polynomial_coefficient_form, proving_offset as u64) {
Expand Down Expand Up @@ -188,10 +204,17 @@ pub fn deserialize_montgomery_elements(data: &[Fr], buffer: &mut Vec<u8>) {
buffer.extend(temp_buffer);
}

fn decode_codec_blob_header(codec_blob_header: &[u8]) -> (u8, u32){
fn decode_codec_blob_header(codec_blob_header: &[u8]) -> Result<(u8, u32)> {
ensure!(
codec_blob_header.len() == 32,
"Codec blob header must be 32 bytes long",
);

let version = codec_blob_header[1];
let length_bytes: [u8; 4] = codec_blob_header[2..6].try_into().expect("Slice with incorrect length");
let length_bytes: [u8; 4] = codec_blob_header[2..6]
.try_into()
.map_err(|_| eyre::eyre!("Failed to decode length bytes"))?;
let length = u32::from_be_bytes(length_bytes);

(version, length)
}
Ok((version, length))
}

0 comments on commit 2fe1885

Please sign in to comment.