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
252 changes: 181 additions & 71 deletions fuzz/Cargo.lock

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ cc = "1.2"

[dependencies]
arbitrary = "1.4"
rust_eth_kzg = { git = "https://github.com/crate-crypto/rust-eth-kzg" }
c-kzg = { path = "..", features = ["arbitrary"] }
constantine = { git = "https://github.com/mratsim/constantine", package = "constantine-ethereum-kzg" }
lazy_static = "1.5"
libfuzzer-sys = "0.4"

[dependencies.c-kzg]
path = ".."
rust_eth_kzg = { git = "https://github.com/crate-crypto/rust-eth-kzg" }

# Prevent this from interfering with workspaces
[workspace]
Expand Down
23 changes: 13 additions & 10 deletions fuzz/fuzz_targets/fuzz_blob_to_kzg_commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extern crate core;

use lazy_static::lazy_static;
use libfuzzer_sys::fuzz_target;
use rust_eth_kzg::DASContext;
use std::cell::UnsafeCell;
use std::env;
use std::path::PathBuf;
Expand All @@ -29,20 +30,17 @@ fn get_root_dir() -> PathBuf {
}

///////////////////////////////////////////////////////////////////////////////
// CKZG Initialization
// Initialization
///////////////////////////////////////////////////////////////////////////////

lazy_static! {
static ref KZG_SETTINGS: c_kzg::KzgSettings = {
let trusted_setup_file = get_root_dir().join("src").join("trusted_setup.txt");
c_kzg::KzgSettings::load_trusted_setup_file(&trusted_setup_file, 0).unwrap()
};
static ref DAS_CONTEXT: DASContext = DASContext::default();
}

///////////////////////////////////////////////////////////////////////////////
// Constantine Initialization
///////////////////////////////////////////////////////////////////////////////

struct SafeEthKzgContext {
inner: UnsafeCell<constantine::EthKzgContext<'static>>,
}
Expand Down Expand Up @@ -81,18 +79,23 @@ fuzz_target!(|blob: c_kzg::Blob| {

let ckzg_result = KZG_SETTINGS.blob_to_kzg_commitment(&blob);
let cnst_result = cnst.blob_to_kzg_commitment(&blob);
let rkzg_result = DAS_CONTEXT.blob_to_kzg_commitment(&blob);

match (&ckzg_result, &cnst_result) {
(Ok(ckzg_commitment), Ok(cnst_commitment)) => {
match (&ckzg_result, &cnst_result, &rkzg_result) {
(Ok(ckzg_commitment), Ok(cnst_commitment), Ok(rkzg_commitment)) => {
// Ensure the results are the same.
assert_eq!(*ckzg_commitment.as_slice(), *cnst_commitment.as_slice());
assert_eq!(ckzg_commitment.as_slice(), cnst_commitment.as_slice());
assert_eq!(ckzg_commitment.as_slice(), rkzg_commitment.as_slice());
}
(Err(_), Err(_)) => {
(Err(_), Err(_), Err(_)) => {
// Cannot compare errors, they are unique.
}
_ => {
// There is a disagreement.
panic!("mismatch {:?} and {:?}", &ckzg_result, &cnst_result);
panic!(
"mismatch: {:?}, {:?}, {:?}",
&ckzg_result, &cnst_result, &rkzg_result
);
}
}
});
23 changes: 13 additions & 10 deletions fuzz/fuzz_targets/fuzz_compute_blob_kzg_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ extern crate core;
use arbitrary::Arbitrary;
use lazy_static::lazy_static;
use libfuzzer_sys::fuzz_target;
use rust_eth_kzg::DASContext;
use std::cell::UnsafeCell;
use std::env;
use std::path::PathBuf;
use std::sync::{Arc, OnceLock};
use std::env;

///////////////////////////////////////////////////////////////////////////////
// Helper Functions
Expand All @@ -30,20 +31,17 @@ fn get_root_dir() -> PathBuf {
}

///////////////////////////////////////////////////////////////////////////////
// CKZG Initialization
// Initialization
///////////////////////////////////////////////////////////////////////////////

lazy_static! {
static ref KZG_SETTINGS: c_kzg::KzgSettings = {
let trusted_setup_file = get_root_dir().join("src").join("trusted_setup.txt");
c_kzg::KzgSettings::load_trusted_setup_file(&trusted_setup_file, 0).unwrap()
};
static ref DAS_CONTEXT: DASContext = DASContext::default();
}

///////////////////////////////////////////////////////////////////////////////
// Constantine Initialization
///////////////////////////////////////////////////////////////////////////////

struct SafeEthKzgContext {
inner: UnsafeCell<constantine::EthKzgContext<'static>>,
}
Expand Down Expand Up @@ -88,18 +86,23 @@ fuzz_target!(|input: Input| {

let ckzg_result = KZG_SETTINGS.compute_blob_kzg_proof(&input.blob, &input.commitment);
let cnst_result = cnst.compute_blob_kzg_proof(&input.blob, &input.commitment);
let rkzg_result = DAS_CONTEXT.compute_blob_kzg_proof(&input.blob, &input.commitment);

match (&ckzg_result, &cnst_result) {
(Ok(ckzg_proof), Ok(cnst_proof)) => {
match (&ckzg_result, &cnst_result, &rkzg_result) {
(Ok(ckzg_proof), Ok(cnst_proof), Ok(rkzg_proof)) => {
// Ensure the results are the same.
assert_eq!(*ckzg_proof.as_slice(), *cnst_proof.as_slice());
assert_eq!(*ckzg_proof.as_slice(), *rkzg_proof.as_slice());
}
(Err(_), Err(_)) => {
(Err(_), Err(_), Err(_)) => {
// Cannot compare errors, they are unique.
}
_ => {
// There is a disagreement.
panic!("mismatch {:?} and {:?}", &ckzg_result, &cnst_result);
panic!(
"mismatch: {:?}, {:?}, {:?}",
&ckzg_result, &cnst_result, &rkzg_result
);
}
}
});
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/fuzz_compute_cells.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fuzz_target!(|blob: Blob| {
_ => {
// There is a disagreement.
panic!(
"mismatch {:?} and {:?}",
"mismatch: {:?}, {:?}",
&ckzg_result.is_ok(),
&rkzg_result.is_ok()
);
Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/fuzz_compute_cells_and_kzg_proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fuzz_target!(|blob: Blob| {
_ => {
// There is a disagreement.
panic!(
"mismatch {:?} and {:?}",
"mismatch: {:?}, {:?}",
&ckzg_result.is_ok(),
&rkzg_result.is_ok()
);
Expand Down
22 changes: 13 additions & 9 deletions fuzz/fuzz_targets/fuzz_compute_kzg_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ extern crate core;
use arbitrary::Arbitrary;
use lazy_static::lazy_static;
use libfuzzer_sys::fuzz_target;
use rust_eth_kzg::DASContext;
use std::cell::UnsafeCell;
use std::env;
use std::path::PathBuf;
Expand All @@ -30,20 +31,17 @@ fn get_root_dir() -> PathBuf {
}

///////////////////////////////////////////////////////////////////////////////
// CKZG Initialization
// Initialization
///////////////////////////////////////////////////////////////////////////////

lazy_static! {
static ref KZG_SETTINGS: c_kzg::KzgSettings = {
let trusted_setup_file = get_root_dir().join("src").join("trusted_setup.txt");
c_kzg::KzgSettings::load_trusted_setup_file(&trusted_setup_file, 0).unwrap()
};
static ref DAS_CONTEXT: DASContext = DASContext::default();
}

///////////////////////////////////////////////////////////////////////////////
// Constantine Initialization
///////////////////////////////////////////////////////////////////////////////

struct SafeEthKzgContext {
inner: UnsafeCell<constantine::EthKzgContext<'static>>,
}
Expand Down Expand Up @@ -88,19 +86,25 @@ fuzz_target!(|input: Input| {

let ckzg_result = KZG_SETTINGS.compute_kzg_proof(&input.blob, &input.z);
let cnst_result = cnst.compute_kzg_proof(&input.blob, &input.z);
let rkzg_result = DAS_CONTEXT.compute_kzg_proof(&input.blob, *input.z);

match (&ckzg_result, &cnst_result) {
(Ok((ckzg_proof, ckzg_y)), Ok((cnst_proof, cnst_y))) => {
match (&ckzg_result, &cnst_result, &rkzg_result) {
(Ok((ckzg_proof, ckzg_y)), Ok((cnst_proof, cnst_y)), Ok((rkzg_proof, rkzg_y))) => {
// Ensure the results are the same.
assert_eq!(*ckzg_proof.as_slice(), *cnst_proof.as_slice());
assert_eq!(*ckzg_proof.as_slice(), *rkzg_proof.as_slice());
assert_eq!(*ckzg_y.as_slice(), *cnst_y.as_slice());
assert_eq!(*ckzg_y.as_slice(), *rkzg_y.as_slice());
}
(Err(_), Err(_)) => {
(Err(_), Err(_), Err(_)) => {
// Cannot compare errors, they are unique.
}
_ => {
// There is a disagreement.
panic!("mismatch {:?} and {:?}", &ckzg_result, &cnst_result);
panic!(
"mismatch: {:?}, {:?}, {:?}",
&ckzg_result, &cnst_result, &rkzg_result
);
}
}
});
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/fuzz_recover_cells_and_kzg_proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fuzz_target!(|input: Input| {
_ => {
// There is a disagreement.
panic!(
"mismatch {:?} and {:?}",
"mismatch: {:?}, {:?}",
&ckzg_result.is_ok(),
&rkzg_result.is_ok()
);
Expand Down
26 changes: 21 additions & 5 deletions fuzz/fuzz_targets/fuzz_verify_blob_kzg_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ extern crate core;
use arbitrary::Arbitrary;
use lazy_static::lazy_static;
use libfuzzer_sys::fuzz_target;
use rust_eth_kzg::DASContext;
use std::cell::UnsafeCell;
use std::env;
use std::path::PathBuf;
Expand All @@ -30,14 +31,15 @@ fn get_root_dir() -> PathBuf {
}

///////////////////////////////////////////////////////////////////////////////
// CKZG Initialization
// Initialization
///////////////////////////////////////////////////////////////////////////////

lazy_static! {
static ref KZG_SETTINGS: c_kzg::KzgSettings = {
let trusted_setup_file = get_root_dir().join("src").join("trusted_setup.txt");
c_kzg::KzgSettings::load_trusted_setup_file(&trusted_setup_file, 0).unwrap()
};
static ref DAS_CONTEXT: DASContext = DASContext::default();
}

///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -90,17 +92,31 @@ fuzz_target!(|input: Input| {
let ckzg_result =
KZG_SETTINGS.verify_blob_kzg_proof(&input.blob, &input.commitment, &input.proof);
let cnst_result = cnst.verify_blob_kzg_proof(&input.blob, &input.commitment, &input.proof);
let rkzg_result =
DAS_CONTEXT.verify_blob_kzg_proof(&input.blob, &input.commitment, &input.proof);

match (&ckzg_result, &cnst_result) {
(Ok(ckzg_valid), Ok(cnst_valid)) => {
match (&ckzg_result, &cnst_result, &rkzg_result) {
(Ok(ckzg_valid), Ok(cnst_valid), Ok(())) => {
assert_eq!(*ckzg_valid, *cnst_valid);
assert_eq!(*ckzg_valid, true);
}
(Err(_), Err(_)) => {
(Ok(ckzg_valid), Ok(cnst_valid), Err(err)) => {
// If ckzg was Ok, ensure the proof was rejected.
assert_eq!(*ckzg_valid, false);
assert_eq!(*cnst_valid, false);
if !err.is_proof_invalid() {
panic!("Expected InvalidProof, got {:?}", err);
}
}
(Err(_), Err(_), Err(_)) => {
// Cannot compare errors, they are unique.
}
_ => {
// There is a disagreement.
panic!("mismatch {:?} and {:?}", &ckzg_result, &cnst_result);
panic!(
"mismatch: {:?}, {:?}, {:?}",
&ckzg_result, &cnst_result, rkzg_result
);
}
}
});
48 changes: 37 additions & 11 deletions fuzz/fuzz_targets/fuzz_verify_blob_kzg_proof_batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ extern crate core;
use arbitrary::Arbitrary;
use lazy_static::lazy_static;
use libfuzzer_sys::fuzz_target;
use rust_eth_kzg::DASContext;
use std::cell::UnsafeCell;
use std::env;
use std::path::PathBuf;
Expand All @@ -30,20 +31,17 @@ fn get_root_dir() -> PathBuf {
}

///////////////////////////////////////////////////////////////////////////////
// CKZG Initialization
// Initialization
///////////////////////////////////////////////////////////////////////////////

lazy_static! {
static ref KZG_SETTINGS: c_kzg::KzgSettings = {
let trusted_setup_file = get_root_dir().join("src").join("trusted_setup.txt");
c_kzg::KzgSettings::load_trusted_setup_file(&trusted_setup_file, 0).unwrap()
};
static ref DAS_CONTEXT: DASContext = DASContext::default();
}

///////////////////////////////////////////////////////////////////////////////
// Constantine Initialization
///////////////////////////////////////////////////////////////////////////////

struct SafeEthKzgContext {
inner: UnsafeCell<constantine::EthKzgContext<'static>>,
}
Expand Down Expand Up @@ -89,13 +87,26 @@ fuzz_target!(|input: Input| {
.get_or_init(|| initialize_constantine_ctx())
.get();

/* A version for constantine */
let blobs: Vec<[u8; c_kzg::BYTES_PER_BLOB]> =
input.blobs.iter().map(|b| b.clone().into_inner()).collect();
let commitments: Vec<[u8; c_kzg::BYTES_PER_COMMITMENT]> =
input.commitments.iter().map(|c| c.into_inner()).collect();
let proofs: Vec<[u8; c_kzg::BYTES_PER_PROOF]> =
input.proofs.iter().map(|p| p.into_inner()).collect();

/* A second version for rust-eth-kzg */
let blobs_vec: Vec<[u8; c_kzg::BYTES_PER_BLOB]> =
input.blobs.iter().map(|b| b.clone().into_inner()).collect();
let blobs_refs: Vec<&[u8; c_kzg::BYTES_PER_BLOB]> = blobs_vec.iter().collect();
let commitments_vec: Vec<[u8; c_kzg::BYTES_PER_COMMITMENT]> =
input.commitments.iter().map(|c| c.into_inner()).collect();
let commitments_refs: Vec<&[u8; c_kzg::BYTES_PER_COMMITMENT]> =
commitments_vec.iter().collect();
let proofs_vec: Vec<[u8; c_kzg::BYTES_PER_PROOF]> =
input.proofs.iter().map(|p| p.into_inner()).collect();
let proofs_refs: Vec<&[u8; c_kzg::BYTES_PER_PROOF]> = proofs_vec.iter().collect();

let ckzg_result =
KZG_SETTINGS.verify_blob_kzg_proof_batch(&input.blobs, &input.commitments, &input.proofs);
let cnst_result = cnst.verify_blob_kzg_proof_batch(
Expand All @@ -104,17 +115,32 @@ fuzz_target!(|input: Input| {
proofs.as_slice(),
&input.secure_random_bytes,
);

match (&ckzg_result, &cnst_result) {
(Ok(ckzg_valid), Ok(cnst_valid)) => {
assert_eq!(*ckzg_valid, *cnst_valid);
let rkzg_result =
DAS_CONTEXT.verify_blob_kzg_proof_batch(blobs_refs, commitments_refs, proofs_refs);

match (&ckzg_result, &cnst_result, &rkzg_result) {
(Ok(ckzg_valid), Ok(cnst_valid), Ok(())) => {
// One returns a boolean, the other just says Ok.
assert_eq!(*ckzg_valid, true);
assert_eq!(*cnst_valid, true);
}
(Ok(ckzg_valid), Ok(cnst_valid), Err(err)) => {
// If ckzg was Ok, ensure the proof was rejected.
assert_eq!(*ckzg_valid, false);
assert_eq!(*cnst_valid, false);
if !err.is_proof_invalid() {
panic!("Expected InvalidProof, got {:?}", err);
}
}
(Err(_), Err(_)) => {
(Err(_), Err(_), Err(_)) => {
// Cannot compare errors, they are unique.
}
_ => {
// There is a disagreement.
panic!("mismatch {:?} and {:?}", &ckzg_result, &cnst_result);
panic!(
"mismatch: {:?}, {:?}, {:?}",
&ckzg_result, &cnst_result, &rkzg_result
);
}
}
});
Loading
Loading