Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compute_cells fuzzing support #541

Merged
merged 2 commits into from
Mar 10, 2025
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
117 changes: 58 additions & 59 deletions fuzz/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ path = "fuzz_targets/fuzz_verify_blob_kzg_proof_batch.rs"
test = false
doc = false

[[bin]]
name = "fuzz_compute_cells"
path = "fuzz_targets/fuzz_compute_cells.rs"
test = false
doc = false

[[bin]]
name = "fuzz_compute_cells_and_kzg_proofs"
path = "fuzz_targets/fuzz_compute_cells_and_kzg_proofs.rs"
Expand Down
1 change: 1 addition & 0 deletions fuzz/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ List available targets like this:
$ cargo fuzz list
fuzz_blob_to_kzg_commitment
fuzz_compute_blob_kzg_proof
fuzz_compute_cells
fuzz_compute_cells_and_kzg_proofs
fuzz_compute_kzg_proof
fuzz_recover_cells_and_kzg_proofs
Expand Down
46 changes: 46 additions & 0 deletions fuzz/fuzz_targets/fuzz_compute_cells.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Run with the following command:
// cargo fuzz run fuzz_compute_cells_and_kzg_proofs

#![no_main]
extern crate core;

use c_kzg::Blob;
use c_kzg::KzgSettings;
use lazy_static::lazy_static;
use libfuzzer_sys::fuzz_target;
use rust_eth_kzg::DASContext;
use std::path::PathBuf;

lazy_static! {
static ref KZG_SETTINGS: KzgSettings = {
let root_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
let trusted_setup_file = root_dir.join("..").join("src").join("trusted_setup.txt");
KzgSettings::load_trusted_setup_file(&trusted_setup_file, 8).unwrap()
};
static ref DAS_CONTEXT: DASContext = DASContext::default();
}

fuzz_target!(|blob: Blob| {
let ckzg_result = KZG_SETTINGS.compute_cells(&blob);
let rkzg_result = DAS_CONTEXT.compute_cells(&blob.into_inner());

match (&ckzg_result, &rkzg_result) {
(Ok(ckzg_cells), Ok(rkzg_cells)) => {
// Ensure the results are the same.
for (ckzg_cell, rkzg_cell) in ckzg_cells.iter().zip(rkzg_cells.iter()) {
assert_eq!(ckzg_cell.to_bytes().as_slice(), rkzg_cell.as_slice())
}
}
(Err(_), Err(_)) => {
// Cannot compare errors, they are unique.
}
_ => {
// There is a disagreement.
panic!(
"mismatch {:?} and {:?}",
&ckzg_result.is_ok(),
&rkzg_result.is_ok()
);
}
}
});
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 @@ -35,7 +35,7 @@ fuzz_target!(|input: Input| {

let ckzg_result = KZG_SETTINGS
.recover_cells_and_kzg_proofs(input.cell_indices.as_slice(), input.cells.as_slice());
let rkzg_result = DAS_CONTEXT.recover_cells_and_proofs(input.cell_indices, cells_bytes);
let rkzg_result = DAS_CONTEXT.recover_cells_and_kzg_proofs(input.cell_indices, cells_bytes);

match (&ckzg_result, &rkzg_result) {
(Ok((ckzg_cells, ckzg_proofs)), Ok((rkzg_cells, rkzg_proofs))) => {
Expand Down
Loading