Skip to content

Commit

Permalink
chore!: unify the error type in eip7594 package (#90)
Browse files Browse the repository at this point in the history
* chore: add new error type

* chore: modify code
  • Loading branch information
kevaundray authored Jul 17, 2024
1 parent 23504e6 commit b7891c2
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 48 deletions.
6 changes: 3 additions & 3 deletions bindings/c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub use eip7594::constants::{
BYTES_PER_BLOB, BYTES_PER_CELL, BYTES_PER_COMMITMENT, BYTES_PER_FIELD_ELEMENT,
CELLS_PER_EXT_BLOB, FIELD_ELEMENTS_PER_BLOB,
};
use eip7594::verifier::VerifierError;
use eip7594::{verifier::VerifierError, Error};

// This is a wrapper around the PeerDASContext from the eip7594 library.
// We need to wrap it as some bindgen tools cannot pick up items
Expand Down Expand Up @@ -194,11 +194,11 @@ pub extern "C" fn compute_cells_and_kzg_proofs(
// From the callers perspective, as long as the verification procedure is invalid, it doesn't matter why it is invalid.
// We need to unwrap it here because the FFI API is not rich enough to distinguish invalid proof vs invalid input.
fn verification_result_to_bool_cresult(
verification_result: Result<(), VerifierError>,
verification_result: Result<(), Error>,
) -> Result<bool, CResult> {
match verification_result {
Ok(_) => Ok(true),
Err(VerifierError::InvalidProof) => Ok(false),
Err(Error::VerifierError(VerifierError::InvalidProof)) => Ok(false),
Err(err) => Err(CResult::with_error(&format!("{:?}", err))),
}
}
Expand Down
11 changes: 6 additions & 5 deletions bindings/java/rust_code/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use eip7594::Error as DASError;

#[derive(Debug)]
pub enum Error {
Jni(jni::errors::Error),
Expand All @@ -6,8 +8,7 @@ pub enum Error {
got: usize,
name: &'static str,
},
Prover(eip7594::prover::ProverError),
Verifier(eip7594::verifier::VerifierError),
DASError(DASError),
}

impl From<jni::errors::Error> for Error {
Expand All @@ -16,8 +17,8 @@ impl From<jni::errors::Error> for Error {
}
}

impl From<eip7594::prover::ProverError> for Error {
fn from(err: eip7594::prover::ProverError) -> Self {
Error::Prover(err)
impl From<DASError> for Error {
fn from(err: DASError) -> Self {
Error::DASError(err)
}
}
8 changes: 4 additions & 4 deletions bindings/java/rust_code/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use c_peerdas_kzg::PeerDASContext;
use eip7594::verifier::VerifierError;
use eip7594::Error as DASError;
use jni::objects::JObjectArray;
use jni::objects::{JByteArray, JClass, JLongArray, JObject, JValue};
use jni::sys::{jboolean, jlong};
Expand Down Expand Up @@ -149,8 +150,8 @@ fn verify_cell_kzg_proof_batch<'local>(
proofs,
) {
Ok(_) => Ok(jboolean::from(true)),
Err(VerifierError::InvalidProof) => Ok(jboolean::from(false)),
Err(err) => Err(Error::Verifier(err)),
Err(DASError::VerifierError(VerifierError::InvalidProof)) => Ok(jboolean::from(false)),
Err(err) => Err(Error::DASError(err)),
}
}

Expand Down Expand Up @@ -287,13 +288,12 @@ fn cells_and_proofs_to_jobject<'local>(
fn throw_on_error(env: &mut JNIEnv, err: Error, func_name: &'static str) {
let reason = match err {
Error::Jni(err) => format!("{:?}", err),
Error::Prover(err) => format!("{:?}", err),
Error::Verifier(err) => format!("{:?}", err),
Error::IncorrectSize {
expected,
got,
name,
} => format!("{name} is not the correct size. expected: {expected}\ngot: {got}"),
Error::DASError(err) => format!("{:?}", err),
};
let msg = format!(
"function {} has thrown an exception, with reason: {}",
Expand Down
4 changes: 2 additions & 2 deletions bindings/node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use napi::{
};
use napi_derive::napi;

use eip7594::{constants, verifier::VerifierError, PeerDASContext};
use eip7594::{constants, verifier::VerifierError, Error as DASError, PeerDASContext};

#[napi]
pub const BYTES_PER_COMMITMENT: u32 = constants::BYTES_PER_COMMITMENT as u32;
Expand Down Expand Up @@ -197,7 +197,7 @@ impl PeerDASContextJs {
ctx.verify_cell_kzg_proof_batch(commitments, row_indices, column_indices, cells, proofs);
match valid {
Ok(_) => Ok(true),
Err(VerifierError::InvalidProof) => Ok(false),
Err(DASError::VerifierError(VerifierError::InvalidProof)) => Ok(false),
Err(err) => Err(Error::from_reason(format!(
"failed to compute verify_cell_kzg_proof_batch: {:?}",
err
Expand Down
38 changes: 24 additions & 14 deletions eip7594/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,38 @@ use erasure_codes::errors::RSError;

use crate::CellIndex;

/// Errors that can occur while calling a method in the Prover API
/// Errors that can occur either during proving or verification.
#[derive(Debug)]
pub enum ProverError {
// TODO: This will be getting removed, waiting for consensus-specs PR
NumProofsDoesNotEqualNumCells,
pub enum Error {
Prover(ProverError),
VerifierError(VerifierError),
Serialization(SerializationError),
RecoveryFailure(VerifierError),
}

impl From<SerializationError> for ProverError {
impl From<ProverError> for Error {
fn from(value: ProverError) -> Self {
Error::Prover(value)
}
}
impl From<VerifierError> for Error {
fn from(value: VerifierError) -> Self {
Error::VerifierError(value)
}
}
impl From<SerializationError> for Error {
fn from(value: SerializationError) -> Self {
ProverError::Serialization(value)
Error::Serialization(value)
}
}

/// Errors that can occur while calling a method in the Prover API
#[derive(Debug)]
pub enum ProverError {
// TODO: This will be getting removed, waiting for consensus-specs PR
NumProofsDoesNotEqualNumCells,
RecoveryFailure(VerifierError),
}

impl From<VerifierError> for ProverError {
fn from(value: VerifierError) -> Self {
ProverError::RecoveryFailure(value)
Expand All @@ -26,7 +43,6 @@ impl From<VerifierError> for ProverError {
/// Errors that can occur while calling a method in the Verifier API
#[derive(Debug)]
pub enum VerifierError {
Serialization(SerializationError),
NumCellIndicesNotEqualToNumCells {
num_cell_indices: usize,
num_cells: usize,
Expand Down Expand Up @@ -73,12 +89,6 @@ impl From<RSError> for VerifierError {
}
}

impl From<SerializationError> for VerifierError {
fn from(value: SerializationError) -> Self {
VerifierError::Serialization(value)
}
}

/// Errors that can occur during deserialization of untrusted input from the public API
/// or the trusted setup.
#[derive(Debug)]
Expand Down
1 change: 1 addition & 0 deletions eip7594/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub type CellIndex = u64;
pub type RowIndex = u64;

mod errors;
pub use errors::Error;

/// The context that will be used to create and verify proofs.
#[derive(Debug)]
Expand Down
22 changes: 11 additions & 11 deletions eip7594/src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use crate::{
CELLS_PER_EXT_BLOB, FIELD_ELEMENTS_PER_BLOB, FIELD_ELEMENTS_PER_CELL,
FIELD_ELEMENTS_PER_EXT_BLOB,
},
serialization::{self, serialize_g1_compressed},
errors::Error,
serialization::{
deserialize_blob_to_scalars, serialize_cells_and_proofs, serialize_g1_compressed,
},
trusted_setup::TrustedSetup,
BlobRef, Cell, CellIndex, CellRef, KZGCommitment, KZGProof, PeerDASContext,
};
Expand Down Expand Up @@ -58,10 +61,10 @@ impl ProverContext {

impl PeerDASContext {
/// Computes the KZG commitment to the polynomial represented by the blob.
pub fn blob_to_kzg_commitment(&self, blob: BlobRef) -> Result<KZGCommitment, ProverError> {
pub fn blob_to_kzg_commitment(&self, blob: BlobRef) -> Result<KZGCommitment, Error> {
self.thread_pool.install(|| {
// Deserialize the blob into scalars.
let scalars = serialization::deserialize_blob_to_scalars(blob)?;
let scalars = deserialize_blob_to_scalars(blob)?;

// Compute commitment
let commitment = self
Expand All @@ -78,11 +81,11 @@ impl PeerDASContext {
pub fn compute_cells_and_kzg_proofs(
&self,
blob: BlobRef,
) -> Result<([Cell; CELLS_PER_EXT_BLOB], [KZGProof; CELLS_PER_EXT_BLOB]), ProverError> {
) -> Result<([Cell; CELLS_PER_EXT_BLOB], [KZGProof; CELLS_PER_EXT_BLOB]), Error> {
self.thread_pool.install(|| {
// Deserialization
//
let scalars = serialization::deserialize_blob_to_scalars(blob)?;
let scalars = deserialize_blob_to_scalars(blob)?;

// Computation
//
Expand All @@ -91,7 +94,7 @@ impl PeerDASContext {
.kzg_multipoint_prover
.compute_multi_opening_proofs(ProverInput::Data(scalars));

Ok(serialization::serialize_cells_and_proofs(cells, proofs))
Ok(serialize_cells_and_proofs(cells, proofs))
})
}

Expand All @@ -106,7 +109,7 @@ impl PeerDASContext {
&self,
cell_indices: Vec<CellIndex>,
cells: Vec<CellRef>,
) -> Result<([Cell; CELLS_PER_EXT_BLOB], [KZGProof; CELLS_PER_EXT_BLOB]), ProverError> {
) -> Result<([Cell; CELLS_PER_EXT_BLOB], [KZGProof; CELLS_PER_EXT_BLOB]), Error> {
self.thread_pool.install(|| {
// Recover polynomial
//
Expand All @@ -119,10 +122,7 @@ impl PeerDASContext {
.kzg_multipoint_prover
.compute_multi_opening_proofs(ProverInput::PolyCoeff(poly_coeff));

Ok(serialization::serialize_cells_and_proofs(
coset_evaluations,
proofs,
))
Ok(serialize_cells_and_proofs(coset_evaluations, proofs))
})
}
}
19 changes: 12 additions & 7 deletions eip7594/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
constants::{
CELLS_PER_EXT_BLOB, EXTENSION_FACTOR, FIELD_ELEMENTS_PER_BLOB, FIELD_ELEMENTS_PER_EXT_BLOB,
},
errors::Error,
serialization::{deserialize_cells, deserialize_compressed_g1_points},
trusted_setup::TrustedSetup,
Bytes48Ref, CellIndex, CellRef, PeerDASContext, RowIndex,
Expand Down Expand Up @@ -76,7 +77,7 @@ impl PeerDASContext {
cell_indices: Vec<CellIndex>,
cells: Vec<CellRef>,
proofs_bytes: Vec<Bytes48Ref>,
) -> Result<(), VerifierError> {
) -> Result<(), Error> {
self.thread_pool.install(|| {
// Validation
//
Expand Down Expand Up @@ -123,7 +124,7 @@ impl PeerDASContext {
if ok {
Ok(())
} else {
Err(VerifierError::InvalidProof)
Err(VerifierError::InvalidProof.into())
}
})
}
Expand All @@ -132,7 +133,7 @@ impl PeerDASContext {
&self,
cell_indices: Vec<CellIndex>,
cells: Vec<CellRef>,
) -> Result<Vec<Scalar>, VerifierError> {
) -> Result<Vec<Scalar>, Error> {
// Validation
//
validation::recover_polynomial_coeff(&cell_indices, &cells)?;
Expand Down Expand Up @@ -164,10 +165,14 @@ impl PeerDASContext {
let missing_cell_indices = find_missing_cell_indices(&cell_indices_normal_order);

// Recover the polynomial in monomial form, that one can use to generate the cells.
let recovered_polynomial_coeff = self.verifier_ctx.rs.recover_polynomial_coefficient(
flattened_coset_evaluations_normal_order,
BlockErasureIndices(missing_cell_indices),
)?;
let recovered_polynomial_coeff = self
.verifier_ctx
.rs
.recover_polynomial_coefficient(
flattened_coset_evaluations_normal_order,
BlockErasureIndices(missing_cell_indices),
)
.map_err(VerifierError::from)?;

Ok(recovered_polynomial_coeff)
}
Expand Down
4 changes: 2 additions & 2 deletions eip7594/tests/verify_cell_kzg_proof_batch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use common::collect_test_files;
use eip7594::verifier::VerifierError;
use eip7594::{verifier::VerifierError, Error};
use serde_::TestVector;
use std::fs;

Expand Down Expand Up @@ -147,7 +147,7 @@ fn test_verify_cell_kzg_proof_batch() {
// We arrive at this point if the proof verified as true
assert!(test.output.unwrap())
}
Err(VerifierError::InvalidProof) => {
Err(Error::VerifierError(VerifierError::InvalidProof)) => {
assert!(test.output.unwrap() == false);
}
Err(_) => {
Expand Down

0 comments on commit b7891c2

Please sign in to comment.