|
| 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 | +} |
| 72 | + |
| 73 | +impl old_miniscript::MiniscriptKey for FuzzPk { |
| 74 | + type Sha256 = u8; |
| 75 | + type Ripemd160 = u8; |
| 76 | + type Hash160 = u8; |
| 77 | + type Hash256 = u8; |
| 78 | +} |
| 79 | + |
| 80 | +impl old_miniscript::ToPublicKey for FuzzPk { |
| 81 | + fn to_public_key(&self) -> PublicKey { |
| 82 | + let secp_pk = secp256k1::PublicKey::from_slice(&[ |
| 83 | + 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0x78, |
| 84 | + 0xce, 0x56, 0x3f, 0x89, 0xa0, 0xed, 0x94, 0x14, 0xf5, 0xaa, 0x28, 0xad, 0x0d, 0x96, |
| 85 | + 0xd6, 0x79, 0x5f, 0x9c, 0x63, 0x3f, 0x39, 0x79, 0xbf, 0x72, 0xae, 0x82, 0x02, 0x98, |
| 86 | + 0x3d, 0xc9, 0x89, 0xae, 0xc7, 0xf2, 0xff, 0x2e, 0xd9, 0x1b, 0xdd, 0x69, 0xce, 0x02, |
| 87 | + 0xfc, 0x07, 0x00, 0xca, 0x10, 0x0e, 0x59, 0xdd, 0xf3, |
| 88 | + ]) |
| 89 | + .unwrap(); |
| 90 | + PublicKey { inner: secp_pk, compressed: self.compressed } |
| 91 | + } |
| 92 | + |
| 93 | + fn to_sha256(hash: &Self::Sha256) -> sha256::Hash { sha256::Hash::from_byte_array([*hash; 32]) } |
| 94 | + |
| 95 | + fn to_hash256(hash: &Self::Hash256) -> old_miniscript::hash256::Hash { |
| 96 | + old_miniscript::hash256::Hash::from_byte_array([*hash; 32]) |
| 97 | + } |
| 98 | + |
| 99 | + fn to_ripemd160(hash: &Self::Ripemd160) -> ripemd160::Hash { |
| 100 | + ripemd160::Hash::from_byte_array([*hash; 20]) |
| 101 | + } |
| 102 | + |
| 103 | + fn to_hash160(hash: &Self::Ripemd160) -> hash160::Hash { |
| 104 | + hash160::Hash::from_byte_array([*hash; 20]) |
| 105 | + } |
| 106 | +} |
0 commit comments