From ca0b7c143f311f478b5491042b0a9c7b9c283c36 Mon Sep 17 00:00:00 2001 From: Cyrix126 <58007246+Cyrix126@users.noreply.github.com> Date: Wed, 16 Jul 2025 15:05:27 +0200 Subject: [PATCH 1/2] feat: add fuzzing --- fuzz/.gitignore | 5 ++ fuzz/Cargo.toml | 22 ++++++++ fuzz/fuzz_targets/coinselect.rs | 89 +++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 fuzz/.gitignore create mode 100644 fuzz/Cargo.toml create mode 100644 fuzz/fuzz_targets/coinselect.rs diff --git a/fuzz/.gitignore b/fuzz/.gitignore new file mode 100644 index 0000000..0d4f044 --- /dev/null +++ b/fuzz/.gitignore @@ -0,0 +1,5 @@ +target +corpus +artifacts +coverage +afl.rs diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml new file mode 100644 index 0000000..aaee588 --- /dev/null +++ b/fuzz/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "coinselect_rust-coinselect-fuzz" +version = "0.0.0" +publish = false +edition = "2018" + +[package.metadata] +cargo-fuzz = true + +[dependencies] +libfuzzer-sys = "0.4" +arbitrary = {version="1.4", features=["derive"]} + +[dependencies.rust-coinselect] +path = ".." + +[[bin]] +name = "coinselect" +path = "fuzz_targets/coinselect.rs" +test = false +doc = false +bench = false diff --git a/fuzz/fuzz_targets/coinselect.rs b/fuzz/fuzz_targets/coinselect.rs new file mode 100644 index 0000000..805a18a --- /dev/null +++ b/fuzz/fuzz_targets/coinselect.rs @@ -0,0 +1,89 @@ +#![no_main] + +use arbitrary::{Arbitrary, Unstructured}; +use libfuzzer_sys::fuzz_target; +use rust_coinselect::{ + selectcoin::select_coin, + types::{CoinSelectionOpt, ExcessStrategy, OutputGroup}, +}; + +#[derive(Debug, Arbitrary)] +pub struct ArbitraryOutputGroup { + pub value: u64, + pub weight: u64, + pub input_count: usize, + pub creation_sequence: Option, +} + +impl Into for ArbitraryOutputGroup { + fn into(self) -> OutputGroup { + OutputGroup { + value: self.value, + weight: self.weight, + input_count: self.input_count, + creation_sequence: self.creation_sequence, + } + } +} + +#[derive(Debug, Arbitrary)] +pub struct ArbitraryCoinSelectionOpt { + pub target_value: u64, + pub target_feerate: f32, + pub long_term_feerate: Option, + pub min_absolute_fee: u64, + pub base_weight: u64, + pub change_weight: u64, + pub change_cost: u64, + pub avg_input_weight: u64, + pub avg_output_weight: u64, + pub min_change_value: u64, + pub excess_strategy: ArbitraryExcessStrategy, +} + +impl Into for ArbitraryCoinSelectionOpt { + fn into(self) -> CoinSelectionOpt { + CoinSelectionOpt { + target_value: self.target_value, + target_feerate: self.target_feerate, + long_term_feerate: self.long_term_feerate, + min_absolute_fee: self.min_absolute_fee, + base_weight: self.base_weight, + change_weight: self.change_weight, + change_cost: self.change_cost, + avg_input_weight: self.avg_input_weight, + avg_output_weight: self.avg_output_weight, + min_change_value: self.min_change_value, + excess_strategy: self.excess_strategy.into(), + } + } +} +#[derive(Debug, Arbitrary)] +pub enum ArbitraryExcessStrategy { + ToFee, + ToRecipient, + ToChange, +} + +impl Into for ArbitraryExcessStrategy { + fn into(self) -> ExcessStrategy { + match self { + ArbitraryExcessStrategy::ToFee => ExcessStrategy::ToFee, + ArbitraryExcessStrategy::ToChange => ExcessStrategy::ToChange, + ArbitraryExcessStrategy::ToRecipient => ExcessStrategy::ToRecipient, + } + } +} + +fuzz_target!(|data: &[u8]| { + let mut u = Unstructured::new(&data); + let arbitrary_inputs = Vec::::arbitrary(&mut u).unwrap(); + let mut inputs = Vec::new(); + for o in arbitrary_inputs { + inputs.push(o.into()); + } + let opts = ArbitraryCoinSelectionOpt::arbitrary(&mut u).unwrap().into(); + dbg!(&inputs); + dbg!(&opts); + let _ = select_coin(&inputs, &opts); +}); From 57f88b252fb25ccade41e84e064862144fdcc508 Mon Sep 17 00:00:00 2001 From: Cyrix126 <58007246+Cyrix126@users.noreply.github.com> Date: Wed, 16 Jul 2025 15:12:33 +0200 Subject: [PATCH 2/2] fix: update rust version of fuzz test to the same of the project --- fuzz/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index aaee588..8393066 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -2,7 +2,7 @@ name = "coinselect_rust-coinselect-fuzz" version = "0.0.0" publish = false -edition = "2018" +edition = "2021" [package.metadata] cargo-fuzz = true