Skip to content

Commit

Permalink
Create custom Error type so we don't leak c_kzg info from our module
Browse files Browse the repository at this point in the history
  • Loading branch information
EchoAlice committed Oct 11, 2023
1 parent aac268b commit 5bf73fe
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
26 changes: 22 additions & 4 deletions ethereum-consensus/src/deneb/polynomial_commitments.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{deneb::blob_sidecar::Blob, primitives::Bytes32, ssz::prelude::*};
use c_kzg::Error;
pub use c_kzg::KzgSettings;
use std::fmt;
use thiserror::Error;

pub const BYTES_PER_FIELD_ELEMENT: usize = 32;
pub const BYTES_PER_COMMITMENT: usize = 48;
Expand All @@ -11,6 +12,22 @@ pub type FieldElement = Bytes32;
pub type KzgCommitment = ByteVector<BYTES_PER_COMMITMENT>;
pub type KzgProof = ByteVector<BYTES_PER_PROOF>;

#[derive(Debug, Error)]
pub struct Error(c_kzg::Error);

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// Implement custom formatting logic here
write!(f, "Custom error: {:?}", self.0)
}
}

impl From<c_kzg::Error> for Error {
fn from(inner_error: c_kzg::Error) -> Self {
Error(inner_error)
}
}

pub struct ProofAndEvaluation {
pub proof: KzgProof,
pub evaluation: Bytes32,
Expand Down Expand Up @@ -77,7 +94,7 @@ pub fn verify_kzg_proof(
kzg_settings,
)?;

res.then_some(()).ok_or(Error::InvalidKzgProof(String::from("Invalid Proof")))
res.then_some(()).ok_or(Error(c_kzg::Error::InvalidKzgProof(String::from("Invalid Proof"))))
}

pub fn verify_blob_kzg_proof<const BYTES_PER_BLOB: usize>(
Expand All @@ -92,7 +109,7 @@ pub fn verify_blob_kzg_proof<const BYTES_PER_BLOB: usize>(

let res = c_kzg::KzgProof::verify_blob_kzg_proof(&blob, &commitment, &proof, kzg_settings)?;

res.then_some(()).ok_or(Error::InvalidKzgProof(String::from("Invalid Blob")))
res.then_some(()).ok_or(Error(c_kzg::Error::InvalidKzgProof(String::from("Invalid Blob"))))
}

pub fn verify_blob_kzg_proof_batch<const BYTES_PER_BLOB: usize>(
Expand Down Expand Up @@ -125,5 +142,6 @@ pub fn verify_blob_kzg_proof_batch<const BYTES_PER_BLOB: usize>(
kzg_settings,
)?;

res.then_some(()).ok_or(Error::InvalidKzgProof(String::from("Invalid Proof Batch")))
res.then_some(())
.ok_or(Error(c_kzg::Error::InvalidKzgProof(String::from("Invalid Proof Batch"))))
}
2 changes: 2 additions & 0 deletions ethereum-consensus/src/state_transition/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ pub enum Error {
UnknownPreset(String),
#[error(transparent)]
ExecutionEngine(#[from] ExecutionEngineError),
#[error(transparent)]
PolynomialCommitment(#[from] crate::deneb::polynomial_commitments::Error),
}

#[derive(Debug, Error)]
Expand Down

0 comments on commit 5bf73fe

Please sign in to comment.