Skip to content

Commit e072944

Browse files
committed
fuzz: pull FuzzPk out of miniscript_satisfy target
This should be in a library so it can be used by multiple fuzztests.
1 parent 7e1c1ba commit e072944

File tree

2 files changed

+76
-63
lines changed

2 files changed

+76
-63
lines changed

fuzz/fuzz_targets/miniscript_satisfy.rs

Lines changed: 5 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,14 @@
11
#![allow(unexpected_cfgs)]
22

3-
use std::fmt;
4-
use std::str::FromStr;
53
use std::sync::atomic::{AtomicUsize, Ordering};
64

5+
use descriptor_fuzz::FuzzPk;
76
use honggfuzz::fuzz;
8-
use miniscript::bitcoin::hashes::{hash160, ripemd160, sha256, Hash};
7+
use miniscript::bitcoin::hashes::hash160;
98
use miniscript::bitcoin::locktime::{absolute, relative};
109
use miniscript::bitcoin::taproot::Signature;
1110
use miniscript::bitcoin::{secp256k1, PublicKey, TapLeafHash, TapSighashType, XOnlyPublicKey};
12-
use miniscript::{hash256, Miniscript, MiniscriptKey, Satisfier, Segwitv0, Tap, ToPublicKey};
13-
14-
// FIXME pull this out into a library used by all the fuzztests
15-
#[derive(Clone, PartialOrd, Ord, PartialEq, Eq, Debug, Hash)]
16-
struct FuzzPk {
17-
compressed: bool,
18-
}
19-
20-
impl FuzzPk {
21-
pub fn new_from_control_byte(control: u8) -> Self { Self { compressed: control & 1 == 1 } }
22-
}
23-
24-
impl FromStr for FuzzPk {
25-
type Err = std::num::ParseIntError;
26-
fn from_str(s: &str) -> Result<Self, Self::Err> {
27-
let byte = u8::from_str_radix(s, 16)?;
28-
Ok(Self::new_from_control_byte(byte))
29-
}
30-
}
31-
32-
impl fmt::Display for FuzzPk {
33-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { "[fuzz pubkey]".fmt(f) }
34-
}
35-
36-
impl MiniscriptKey for FuzzPk {
37-
type Sha256 = u8;
38-
type Ripemd160 = u8;
39-
type Hash160 = u8;
40-
type Hash256 = u8;
41-
}
42-
43-
impl ToPublicKey for FuzzPk {
44-
fn to_public_key(&self) -> PublicKey {
45-
let secp_pk = secp256k1::PublicKey::from_slice(&[
46-
0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0x78,
47-
0xce, 0x56, 0x3f, 0x89, 0xa0, 0xed, 0x94, 0x14, 0xf5, 0xaa, 0x28, 0xad, 0x0d, 0x96,
48-
0xd6, 0x79, 0x5f, 0x9c, 0x63, 0x3f, 0x39, 0x79, 0xbf, 0x72, 0xae, 0x82, 0x02, 0x98,
49-
0x3d, 0xc9, 0x89, 0xae, 0xc7, 0xf2, 0xff, 0x2e, 0xd9, 0x1b, 0xdd, 0x69, 0xce, 0x02,
50-
0xfc, 0x07, 0x00, 0xca, 0x10, 0x0e, 0x59, 0xdd, 0xf3,
51-
])
52-
.unwrap();
53-
PublicKey { inner: secp_pk, compressed: self.compressed }
54-
}
55-
56-
fn to_sha256(hash: &Self::Sha256) -> sha256::Hash { sha256::Hash::from_byte_array([*hash; 32]) }
57-
58-
fn to_hash256(hash: &Self::Hash256) -> hash256::Hash {
59-
hash256::Hash::from_byte_array([*hash; 32])
60-
}
61-
62-
fn to_ripemd160(hash: &Self::Ripemd160) -> ripemd160::Hash {
63-
ripemd160::Hash::from_byte_array([*hash; 20])
64-
}
65-
66-
fn to_hash160(hash: &Self::Ripemd160) -> hash160::Hash {
67-
hash160::Hash::from_byte_array([*hash; 20])
68-
}
69-
}
11+
use miniscript::{Miniscript, Satisfier, Segwitv0, Tap};
7012

7113
struct FuzzSatisfier<'b> {
7214
idx: AtomicUsize,
@@ -198,14 +140,14 @@ fn do_test(data: &[u8]) {
198140

199141
let s = String::from_utf8_lossy(s);
200142
if control & 1 == 1 {
201-
let ms = match Miniscript::<FuzzPk, Segwitv0>::from_str(&s) {
143+
let ms = match s.parse::<Miniscript<FuzzPk, Segwitv0>>() {
202144
Ok(d) => d,
203145
Err(_) => return,
204146
};
205147

206148
let _ = ms.build_template(&fuzz_sat);
207149
} else {
208-
let ms = match Miniscript::<FuzzPk, Tap>::from_str(&s) {
150+
let ms = match s.parse::<Miniscript<FuzzPk, Tap>>() {
209151
Ok(d) => d,
210152
Err(_) => return,
211153
};

fuzz/src/lib.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Written in 2019 by Andrew Poelstra <[email protected]>
2+
// SPDX-License-Identifier: CC0-1.0
3+
4+
//! Miniscript Fuzzing Library
5+
//!
6+
//! This contains data structures and utilities used by the fuzz tests.
7+
8+
use core::{fmt, str};
9+
10+
use miniscript::bitcoin::hashes::{hash160, ripemd160, sha256, Hash};
11+
use miniscript::bitcoin::{secp256k1, PublicKey};
12+
use miniscript::{hash256, MiniscriptKey, ToPublicKey};
13+
14+
/// A public key which is encoded as a single hex byte (two hex characters).
15+
///
16+
/// Implements `ToPublicKey` but (for now) always maps to the same bitcoin public key.
17+
#[derive(Clone, PartialOrd, Ord, PartialEq, Eq, Debug, Hash)]
18+
pub struct FuzzPk {
19+
compressed: bool,
20+
}
21+
22+
impl FuzzPk {
23+
pub fn new_from_control_byte(control: u8) -> Self { Self { compressed: control & 1 == 1 } }
24+
}
25+
26+
impl str::FromStr for FuzzPk {
27+
type Err = std::num::ParseIntError;
28+
fn from_str(s: &str) -> Result<Self, Self::Err> {
29+
let byte = u8::from_str_radix(s, 16)?;
30+
Ok(Self::new_from_control_byte(byte))
31+
}
32+
}
33+
34+
impl fmt::Display for FuzzPk {
35+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { "[fuzz pubkey]".fmt(f) }
36+
}
37+
38+
impl MiniscriptKey for FuzzPk {
39+
type Sha256 = u8;
40+
type Ripemd160 = u8;
41+
type Hash160 = u8;
42+
type Hash256 = u8;
43+
}
44+
45+
impl ToPublicKey for FuzzPk {
46+
fn to_public_key(&self) -> PublicKey {
47+
let secp_pk = secp256k1::PublicKey::from_slice(&[
48+
0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0x78,
49+
0xce, 0x56, 0x3f, 0x89, 0xa0, 0xed, 0x94, 0x14, 0xf5, 0xaa, 0x28, 0xad, 0x0d, 0x96,
50+
0xd6, 0x79, 0x5f, 0x9c, 0x63, 0x3f, 0x39, 0x79, 0xbf, 0x72, 0xae, 0x82, 0x02, 0x98,
51+
0x3d, 0xc9, 0x89, 0xae, 0xc7, 0xf2, 0xff, 0x2e, 0xd9, 0x1b, 0xdd, 0x69, 0xce, 0x02,
52+
0xfc, 0x07, 0x00, 0xca, 0x10, 0x0e, 0x59, 0xdd, 0xf3,
53+
])
54+
.unwrap();
55+
PublicKey { inner: secp_pk, compressed: self.compressed }
56+
}
57+
58+
fn to_sha256(hash: &Self::Sha256) -> sha256::Hash { sha256::Hash::from_byte_array([*hash; 32]) }
59+
60+
fn to_hash256(hash: &Self::Hash256) -> hash256::Hash {
61+
hash256::Hash::from_byte_array([*hash; 32])
62+
}
63+
64+
fn to_ripemd160(hash: &Self::Ripemd160) -> ripemd160::Hash {
65+
ripemd160::Hash::from_byte_array([*hash; 20])
66+
}
67+
68+
fn to_hash160(hash: &Self::Ripemd160) -> hash160::Hash {
69+
hash160::Hash::from_byte_array([*hash; 20])
70+
}
71+
}

0 commit comments

Comments
 (0)