lez-fuzzing/fuzz/fuzz_targets/fuzz_encoding_roundtrip.rs
Roman 8bd0a1a612
fix: add new fuzz targets
- template for adding targets
2026-04-15 15:47:01 +08:00

46 lines
2.0 KiB
Rust

#![no_main]
//! Fuzz target: encoding round-trip for all transaction types.
//!
//! Invariant: `decode(encode(tx)) == Ok(tx)` and `encode(decode(encode(tx))) == encode(tx)`
//! for every `PublicTransaction` and `ProgramDeploymentTransaction`.
//!
//! `PrivacyPreservingTransaction` is excluded because its ZK receipt cannot be
//! reconstructed in a fuzzing loop.
use arbitrary::{Arbitrary, Unstructured};
use fuzz_props::arbitrary_types::{ArbProgramDeploymentTransaction, ArbPublicTransaction};
use libfuzzer_sys::fuzz_target;
use nssa::{ProgramDeploymentTransaction, PublicTransaction};
fuzz_target!(|data: &[u8]| {
let mut u = Unstructured::new(data);
// ── Test 1: PublicTransaction round-trip ──────────────────────────────────
if let Ok(wrapped) = ArbPublicTransaction::arbitrary(&mut u) {
let tx = wrapped.0;
let encoded = tx.to_bytes();
let decoded = PublicTransaction::from_bytes(&encoded)
.expect("INVARIANT VIOLATION: PublicTransaction::to_bytes() produced un-decodable output");
let re_encoded = decoded.to_bytes();
assert_eq!(
encoded,
re_encoded,
"INVARIANT VIOLATION: encode(decode(encode(tx))) != encode(tx) for PublicTransaction"
);
}
// ── Test 2: ProgramDeploymentTransaction round-trip ───────────────────────
if let Ok(wrapped) = ArbProgramDeploymentTransaction::arbitrary(&mut u) {
let tx = wrapped.0;
let encoded = tx.to_bytes();
let decoded = ProgramDeploymentTransaction::from_bytes(&encoded)
.expect("INVARIANT VIOLATION: ProgramDeploymentTransaction::to_bytes() produced un-decodable output");
let re_encoded = decoded.to_bytes();
assert_eq!(
encoded,
re_encoded,
"INVARIANT VIOLATION: encode(decode(encode(tx))) != encode(tx) for ProgramDeploymentTransaction"
);
}
});