Skip to content

Commit

Permalink
Add Libfuzzer
Browse files Browse the repository at this point in the history
  • Loading branch information
yancyribbens committed Aug 29, 2024
1 parent 12d4d33 commit 1d07893
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ jobs:
override: true
- name: Running test script
env: ${{ matrix.env }}
run: cd bitcoin-coin-selection && ../contrib/test.sh
run: ./contrib/test.sh
39 changes: 35 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
[workspace]
members = [
"bitcoin-coin-selection", "fuzz",
[package]
authors = [
"Yancy Ribbens <[email protected]>",
]
resolver = "1"
edition = "2018"
homepage = "https://github.com/rust-bitcoin/rust-bitcoin-coin-selection/"
license = "CC0-1.0"
name = "bitcoin-coin-selection"
repository = "https://github.com/rust-bitcoin/rust-bitcoin-coin-selection/"
version = "0.5.0"
# documentation = "https://docs.rs/bitcoin-coin-selection/"
description = "Libary providing utility functions to efficiently select a set of UTXOs."
keywords = ["bitcoin", "coin-selection", "coin", "coinselection", "utxo"]
readme = "README.md"

[dependencies]
bitcoin = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "cfb53c78667dafe8aea488f104f65a2a29a2f94d"}
rand = {version = "0.8.5", default-features = false, optional = true}

[dev-dependencies]
criterion = "0.3"
bitcoin-coin-selection = {path = ".", features = ["rand"]}
rand = "0.8.5"

[[bench]]
name = "coin_selection"
harness = false

[patch.crates-io]
bitcoin_hashes = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "cfb53c78667dafe8aea488f104f65a2a29a2f94d" }
base58ck = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "cfb53c78667dafe8aea488f104f65a2a29a2f94d" }
bitcoin-internals = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "cfb53c78667dafe8aea488f104f65a2a29a2f94d" }
bitcoin-io = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "cfb53c78667dafe8aea488f104f65a2a29a2f94d" }
bitcoin-primitives = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "cfb53c78667dafe8aea488f104f65a2a29a2f94d" }
bitcoin-addresses = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "cfb53c78667dafe8aea488f104f65a2a29a2f94d" }
bitcoin-units = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "cfb53c78667dafe8aea488f104f65a2a29a2f94d" }
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ Note: criterion requires rustc version 1.65 to run the benchmarks.

## Fuzz

To run fuzzer:

Single Random Draw: `cargo hfuzz run single_random_draw_select_coins`
Branch and Bound: `cargo hfuzz run branch_and_bound_select_coins`
Select Coins (both SRD and BNB): `cargo hfuzz run select_coins`
Fuzz with `cargo fuzz run select_coins_srd`, `cargo fuzz run select_coins_bnb` or `cargo fuzz run select_coins`.

### performance comparison

Expand Down
File renamed without changes.
27 changes: 0 additions & 27 deletions bitcoin-coin-selection/Cargo.toml

This file was deleted.

45 changes: 35 additions & 10 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,23 +1,48 @@
[package]
name = "fuzz-coin-selection"
version = "0.1.0"
edition = "2021"
name = "bitcoin-coin-selection-fuzz"
version = "0.0.0"
publish = false
edition = "2018"

[package.metadata]
cargo-fuzz = true

[dependencies]
honggfuzz = "0.5.56"
bitcoin-coin-selection = { path= "../bitcoin-coin-selection", features=["rand"] }
libfuzzer-sys = "0.4"
rand = "0.8.5"
bitcoin = { git = "https://github.com/yancyribbens/rust-bitcoin.git", rev = "edcd2fb5d78be71a60709d18fb367fd56171ff26" }
bitcoin = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "cfb53c78667dafe8aea488f104f65a2a29a2f94d", features = ["arbitrary"] }
arbitrary = { version = "1", features = ["derive"] }

[dependencies.bitcoin-coin-selection]
path = ".."
features = ["rand"]

[[bin]]
name = "single_random_draw_select_coins"
path = "fuzz_targets/single_random_draw_select_coins.rs"
name = "select_coins_srd"
path = "fuzz_targets/select_coins_srd.rs"
test = false
doc = false
bench = false

[[bin]]
name = "branch_and_bound_select_coins"
path = "fuzz_targets/branch_and_bound_select_coins.rs"
name = "select_coins_bnb"
path = "fuzz_targets/select_coins_bnb.rs"
test = false
doc = false
bench = false

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

[patch.crates-io]
bitcoin_hashes = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "cfb53c78667dafe8aea488f104f65a2a29a2f94d" }
base58ck = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "cfb53c78667dafe8aea488f104f65a2a29a2f94d" }
bitcoin-internals = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "cfb53c78667dafe8aea488f104f65a2a29a2f94d" }
bitcoin-io = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "cfb53c78667dafe8aea488f104f65a2a29a2f94d" }
bitcoin-primitives = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "cfb53c78667dafe8aea488f104f65a2a29a2f94d" }
bitcoin-addresses = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "cfb53c78667dafe8aea488f104f65a2a29a2f94d" }
bitcoin-units = { git = "https://github.com/rust-bitcoin/rust-bitcoin.git", rev = "cfb53c78667dafe8aea488f104f65a2a29a2f94d" }
24 changes: 8 additions & 16 deletions fuzz/fuzz_targets/select_coins.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
use bitcoin_coin_selection::WeightedUtxo;
#![no_main]

use bitcoin_coin_selection::select_coins;
use honggfuzz::fuzz;
use arbitrary::Arbitrary;

use bitcoin::TxOut;
use bitcoin::Amount;
use bitcoin::FeeRate;
use bitcoin::Weight;
use bitcoin::{TxOut, FeeRate, Amount, Weight};
use bitcoin_coin_selection::{select_coins, WeightedUtxo};
use libfuzzer_sys::fuzz_target;

#[derive(Arbitrary, Debug)]
pub struct Utxo {
Expand All @@ -34,11 +30,7 @@ pub struct Params {
weighted_utxos: Vec<Utxo>,
}

fn main() {
loop {
fuzz!(|params: Params| {
let Params { target: t, cost_of_change: c, fee_rate: f, long_term_fee_rate: ltf, weighted_utxos: wu } = params;
select_coins(t, c, f, ltf, &wu);
});
}
}
fuzz_target!(|params: Params| {
let Params { target: t, cost_of_change: c, fee_rate: f, long_term_fee_rate: ltf, weighted_utxos: wu } = params;
select_coins(t, c, f, ltf, &wu);
});
36 changes: 36 additions & 0 deletions fuzz/fuzz_targets/select_coins_bnb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#![no_main]

use arbitrary::Arbitrary;
use bitcoin::{TxOut, FeeRate, Amount, Weight};
use bitcoin_coin_selection::{select_coins_bnb, WeightedUtxo};
use libfuzzer_sys::fuzz_target;

#[derive(Arbitrary, Debug)]
pub struct Utxo {
output: TxOut,
satisfaction_weight: Weight
}

impl WeightedUtxo for Utxo {
fn satisfaction_weight(&self) -> Weight {
self.satisfaction_weight
}

fn value(&self) -> Amount {
self.output.value
}
}

#[derive(Arbitrary, Debug)]
pub struct Params {
target: Amount,
cost_of_change: Amount,
fee_rate: FeeRate,
long_term_fee_rate: FeeRate,
weighted_utxos: Vec<Utxo>,
}

fuzz_target!(|params: Params| {
let Params { target: t, cost_of_change: c, fee_rate: f, long_term_fee_rate: lt_f, weighted_utxos: wu } = params;
select_coins_bnb(t, c, f, lt_f, &wu);
});
35 changes: 35 additions & 0 deletions fuzz/fuzz_targets/select_coins_srd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#![no_main]

use arbitrary::Arbitrary;
use bitcoin::{TxOut, FeeRate, Amount, Weight};
use bitcoin_coin_selection::{select_coins_srd, WeightedUtxo};
use libfuzzer_sys::fuzz_target;
use rand::thread_rng;

#[derive(Arbitrary, Debug)]
pub struct Utxo {
output: TxOut,
satisfaction_weight: Weight
}

impl WeightedUtxo for Utxo {
fn satisfaction_weight(&self) -> Weight {
self.satisfaction_weight
}

fn value(&self) -> Amount {
self.output.value
}
}

#[derive(Arbitrary, Debug)]
pub struct Params {
target: Amount,
fee_rate: FeeRate,
weighted_utxos: Vec<Utxo>,
}

fuzz_target!(|params: Params| {
let Params { target: t, fee_rate: f, weighted_utxos: wu } = params;
select_coins_srd(t, f, &wu, &mut thread_rng());
});
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 1d07893

Please sign in to comment.