refactor(fuzz): remove custom fuzz_utils module
What changed, and why it matters
This commit is a routine code cleanup in the project's fuzz-testing harness. It removes a small set of hand-written helper functions and replaces them with a standard library approach for splitting fuzz input into slices. There is no change to the actual Bitcoin library code that users rely on, and nothing in the commit suggests a security fix.
No security action needed. Treat as normal maintenance/refactoring.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors fuzz/fuzz_targets/bitcoin/deserialize_psbt.rs to use arbitrary::Unstructured and <&[u8]>::arbitrary instead of custom consume_random_bytes helpers. It deletes fuzz/src/fuzz_utils.rs and fuzz/src/lib.rs. The fuzz target’s behavior is preserved: it still deserializes two PSBT blobs, checks serialize/deserialize round-tripping, and verifies combine() symmetry. The change is purely in how fuzz input is parsed.
Changed components
fuzz/fuzz_targets/bitcoin/deserialize_psbt.rsfuzz/src/fuzz_utils.rsfuzz/src/lib.rsInspect captured patch +24 / −78
diff --git a/fuzz/fuzz_targets/bitcoin/deserialize_psbt.rs b/fuzz/fuzz_targets/bitcoin/deserialize_psbt.rs
index 7b36cf69..c0d73691 100644
--- a/fuzz/fuzz_targets/bitcoin/deserialize_psbt.rs
+++ b/fuzz/fuzz_targets/bitcoin/deserialize_psbt.rs
@@ -1,29 +1,30 @@
-use bitcoin_fuzz::fuzz_utils::consume_random_bytes;
+use arbitrary::{Arbitrary, Unstructured};
use honggfuzz::fuzz;
fn do_test(data: &[u8]) {
- let mut new_data = data;
- let bytes = consume_random_bytes(&mut new_data);
- let psbt: Result<bitcoin::psbt::Psbt, _> = bitcoin::psbt::Psbt::deserialize(bytes);
- match psbt {
- Err(_) => {}
- Ok(mut psbt) => {
- let ser = bitcoin::psbt::Psbt::serialize(&psbt);
- let deser = bitcoin::psbt::Psbt::deserialize(&ser).unwrap();
- // Since the fuzz data could order psbt fields differently, we compare to our deser/ser instead of data
- assert_eq!(ser, bitcoin::psbt::Psbt::serialize(&deser));
-
- let new_bytes = consume_random_bytes(&mut new_data);
- let psbt_b: Result<bitcoin::psbt::Psbt, _> =
- bitcoin::psbt::Psbt::deserialize(new_bytes);
- match psbt_b {
- Err(_) => {}
- Ok(mut psbt_b) => {
- assert_eq!(psbt_b.combine(psbt.clone()).is_ok(), psbt.combine(psbt_b).is_ok());
- }
- }
- }
- }
+ let mut unstructured = Unstructured::new(data);
+
+ let Ok(bytes_a) = <&[u8]>::arbitrary(&mut unstructured) else {
+ return;
+ };
+ let Ok(bytes_b) = <&[u8]>::arbitrary(&mut unstructured) else {
+ return;
+ };
+
+ let Ok(psbt_a) = bitcoin::psbt::Psbt::deserialize(bytes_a) else {
+ return;
+ };
+
+ let ser = bitcoin::psbt::Psbt::serialize(&psbt_a);
+ let deser = bitcoin::psbt::Psbt::deserialize(&ser).unwrap();
+ assert_eq!(ser, bitcoin::psbt::Psbt::serialize(&deser));
+
+ let Ok(mut psbt_b) = bitcoin::psbt::Psbt::deserialize(bytes_b) else {
+ return;
+ };
+
+ let mut psbt_a_clone = psbt_a.clone();
+ assert_eq!(psbt_b.combine(psbt_a).is_ok(), psbt_a_clone.combine(psbt_b).is_ok());
}
fn main() {
diff --git a/fuzz/src/fuzz_utils.rs b/fuzz/src/fuzz_utils.rs
deleted file mode 100644
index 883729c3..00000000
--- a/fuzz/src/fuzz_utils.rs
+++ /dev/null
@@ -1,50 +0,0 @@
-// SPDX-License-Identifier: CC0-1.0
-
-//! Helper functions for fuzzing.
-
-pub fn consume_random_bytes<'a>(data: &mut &'a [u8]) -> &'a [u8] {
- if data.is_empty() {
- return &[];
- }
-
- let length = (data[0] as usize) % (data.len() + 1);
- let (bytes, rest) = data.split_at(length);
- *data = rest;
-
- bytes
-}
-
-#[allow(dead_code)]
-pub fn consume_u64(data: &mut &[u8]) -> u64 {
- // We need at least 8 bytes to read a u64
- if data.len() < 8 {
- return 0;
- }
-
- let (u64_bytes, rest) = data.split_at(8);
- *data = rest;
-
- u64::from_le_bytes([
- u64_bytes[0],
- u64_bytes[1],
- u64_bytes[2],
- u64_bytes[3],
- u64_bytes[4],
- u64_bytes[5],
- u64_bytes[6],
- u64_bytes[7],
- ])
-}
-
-#[allow(dead_code)]
-pub fn consume_u32(data: &mut &[u8]) -> u32 {
- // We need at least 4 bytes to read a u32
- if data.len() < 4 {
- return 0;
- }
-
- let (u32_bytes, rest) = data.split_at(4);
- *data = rest;
-
- u32::from_le_bytes([u32_bytes[0], u32_bytes[1], u32_bytes[2], u32_bytes[3]])
-}
diff --git a/fuzz/src/lib.rs b/fuzz/src/lib.rs
deleted file mode 100644
index 3421bc22..00000000
--- a/fuzz/src/lib.rs
+++ /dev/null
@@ -1,5 +0,0 @@
-// SPDX-License-Identifier: CC0-1.0
-
-//! Fuzzing
-
-pub mod fuzz_utils;
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.