2022-06-29 04:05:31 +00:00
|
|
|
// SPDX-License-Identifier: CC0-1.0
|
2019-05-14 17:07:53 +00:00
|
|
|
|
2021-11-05 21:58:18 +00:00
|
|
|
//! Bitcoin Taproot.
|
|
|
|
//!
|
|
|
|
//! This module provides support for taproot tagged hashes.
|
2019-05-14 17:07:53 +00:00
|
|
|
//!
|
2021-11-24 23:01:21 +00:00
|
|
|
|
2023-10-31 12:41:07 +00:00
|
|
|
pub mod serialized_signature;
|
|
|
|
|
2022-11-30 01:04:20 +00:00
|
|
|
use core::cmp::Reverse;
|
2021-10-06 23:00:21 +00:00
|
|
|
use core::fmt;
|
2023-02-24 10:02:16 +00:00
|
|
|
use core::iter::FusedIterator;
|
2019-05-14 17:07:53 +00:00
|
|
|
|
2023-03-22 03:09:58 +00:00
|
|
|
use hashes::{sha256t_hash_newtype, Hash, HashEngine};
|
2023-03-28 01:16:47 +00:00
|
|
|
use internals::write_err;
|
2022-11-30 01:04:20 +00:00
|
|
|
use secp256k1::{self, Scalar, Secp256k1};
|
|
|
|
|
2022-05-02 22:13:57 +00:00
|
|
|
use crate::consensus::Encodable;
|
2023-02-27 22:47:29 +00:00
|
|
|
use crate::crypto::key::{TapTweak, TweakedPublicKey, UntweakedPublicKey, XOnlyPublicKey};
|
2022-11-30 01:04:20 +00:00
|
|
|
use crate::prelude::*;
|
2022-07-30 12:22:18 +00:00
|
|
|
use crate::{io, Script, ScriptBuf};
|
2021-10-08 20:08:08 +00:00
|
|
|
|
2023-10-10 04:08:50 +00:00
|
|
|
// Re-export these so downstream only has to use one `taproot` module.
|
|
|
|
#[rustfmt::skip]
|
|
|
|
#[doc(inline)]
|
|
|
|
pub use crate::crypto::taproot::{SigFromSliceError, Signature};
|
|
|
|
|
2021-11-12 21:16:40 +00:00
|
|
|
// Taproot test vectors from BIP-341 state the hashes without any reversing
|
2023-04-02 13:09:48 +00:00
|
|
|
sha256t_hash_newtype! {
|
2023-04-02 13:53:43 +00:00
|
|
|
pub struct TapLeafTag = hash_str("TapLeaf");
|
2023-02-03 03:11:53 +00:00
|
|
|
|
2023-04-02 13:09:48 +00:00
|
|
|
/// Taproot-tagged hash with tag \"TapLeaf\".
|
|
|
|
///
|
|
|
|
/// This is used for computing tapscript script spend hash.
|
|
|
|
#[hash_newtype(forward)]
|
|
|
|
pub struct TapLeafHash(_);
|
|
|
|
|
2023-04-02 13:53:43 +00:00
|
|
|
pub struct TapBranchTag = hash_str("TapBranch");
|
2023-04-02 13:09:48 +00:00
|
|
|
|
|
|
|
/// Tagged hash used in taproot trees.
|
|
|
|
///
|
|
|
|
/// See BIP-340 for tagging rules.
|
|
|
|
#[hash_newtype(forward)]
|
|
|
|
pub struct TapNodeHash(_);
|
|
|
|
|
2023-04-02 13:53:43 +00:00
|
|
|
pub struct TapTweakTag = hash_str("TapTweak");
|
2023-04-02 13:09:48 +00:00
|
|
|
|
|
|
|
/// Taproot-tagged hash with tag \"TapTweak\".
|
|
|
|
///
|
|
|
|
/// This hash type is used while computing the tweaked public key.
|
|
|
|
#[hash_newtype(forward)]
|
|
|
|
pub struct TapTweakHash(_);
|
|
|
|
}
|
2022-07-21 00:23:28 +00:00
|
|
|
|
2021-11-03 18:21:44 +00:00
|
|
|
impl TapTweakHash {
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Creates a new BIP341 [`TapTweakHash`] from key and tweak. Produces `H_taptweak(P||R)` where
|
|
|
|
/// `P` is the internal key and `R` is the merkle root.
|
2021-11-03 18:21:44 +00:00
|
|
|
pub fn from_key_and_tweak(
|
2021-11-29 02:19:21 +00:00
|
|
|
internal_key: UntweakedPublicKey,
|
2022-12-15 15:54:32 +00:00
|
|
|
merkle_root: Option<TapNodeHash>,
|
2021-11-03 18:21:44 +00:00
|
|
|
) -> TapTweakHash {
|
|
|
|
let mut eng = TapTweakHash::engine();
|
|
|
|
// always hash the key
|
|
|
|
eng.input(&internal_key.serialize());
|
|
|
|
if let Some(h) = merkle_root {
|
2022-12-15 19:52:07 +00:00
|
|
|
eng.input(h.as_ref());
|
2021-11-03 18:21:44 +00:00
|
|
|
} else {
|
|
|
|
// nothing to hash
|
|
|
|
}
|
|
|
|
TapTweakHash::from_engine(eng)
|
|
|
|
}
|
2022-06-28 02:47:29 +00:00
|
|
|
|
|
|
|
/// Converts a `TapTweakHash` into a `Scalar` ready for use with key tweaking API.
|
2022-08-01 22:33:22 +00:00
|
|
|
pub fn to_scalar(self) -> Scalar {
|
2022-06-28 02:47:29 +00:00
|
|
|
// This is statistically extremely unlikely to panic.
|
2023-01-28 22:47:24 +00:00
|
|
|
Scalar::from_be_bytes(self.to_byte_array()).expect("hash value greater than curve order")
|
2022-06-28 02:47:29 +00:00
|
|
|
}
|
2021-11-03 18:21:44 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 22:03:49 +00:00
|
|
|
impl TapLeafHash {
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Computes the leaf hash from components.
|
2021-10-08 22:03:49 +00:00
|
|
|
pub fn from_script(script: &Script, ver: LeafVersion) -> TapLeafHash {
|
|
|
|
let mut eng = TapLeafHash::engine();
|
2022-11-30 01:04:20 +00:00
|
|
|
ver.to_consensus().consensus_encode(&mut eng).expect("engines don't error");
|
|
|
|
script.consensus_encode(&mut eng).expect("engines don't error");
|
2021-10-08 22:03:49 +00:00
|
|
|
TapLeafHash::from_engine(eng)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-24 10:02:16 +00:00
|
|
|
impl From<LeafNode> for TapNodeHash {
|
|
|
|
fn from(leaf: LeafNode) -> TapNodeHash { leaf.node_hash() }
|
2022-09-16 12:29:05 +00:00
|
|
|
}
|
|
|
|
|
2023-02-24 10:02:16 +00:00
|
|
|
impl From<&LeafNode> for TapNodeHash {
|
|
|
|
fn from(leaf: &LeafNode) -> TapNodeHash { leaf.node_hash() }
|
2022-09-16 12:29:05 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 15:54:32 +00:00
|
|
|
impl TapNodeHash {
|
2022-03-31 13:16:39 +00:00
|
|
|
/// Computes branch hash given two hashes of the nodes underneath it.
|
2022-12-15 16:17:39 +00:00
|
|
|
pub fn from_node_hashes(a: TapNodeHash, b: TapNodeHash) -> TapNodeHash {
|
2023-01-11 12:25:08 +00:00
|
|
|
Self::combine_node_hashes(a, b).0
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Computes branch hash given two hashes of the nodes underneath it and returns
|
|
|
|
/// whether the left node was the one hashed first.
|
|
|
|
fn combine_node_hashes(a: TapNodeHash, b: TapNodeHash) -> (TapNodeHash, bool) {
|
2022-12-15 15:54:32 +00:00
|
|
|
let mut eng = TapNodeHash::engine();
|
2022-03-31 13:16:39 +00:00
|
|
|
if a < b {
|
2022-12-15 19:52:07 +00:00
|
|
|
eng.input(a.as_ref());
|
|
|
|
eng.input(b.as_ref());
|
2022-03-31 13:16:39 +00:00
|
|
|
} else {
|
2022-12-15 19:52:07 +00:00
|
|
|
eng.input(b.as_ref());
|
|
|
|
eng.input(a.as_ref());
|
2022-03-31 13:16:39 +00:00
|
|
|
};
|
2023-01-11 12:25:08 +00:00
|
|
|
(TapNodeHash::from_engine(eng), a < b)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Assumes the given 32 byte array as hidden [`TapNodeHash`].
|
|
|
|
///
|
2023-02-24 10:02:16 +00:00
|
|
|
/// Similar to [`TapLeafHash::from_byte_array`], but explicitly conveys that the
|
2023-01-11 12:25:08 +00:00
|
|
|
/// hash is constructed from a hidden node. This also has better ergonomics
|
|
|
|
/// because it does not require the caller to import the Hash trait.
|
2022-11-02 22:36:37 +00:00
|
|
|
pub fn assume_hidden(hash: [u8; 32]) -> TapNodeHash { TapNodeHash::from_byte_array(hash) }
|
2022-12-15 16:17:39 +00:00
|
|
|
|
|
|
|
/// Computes the [`TapNodeHash`] from a script and a leaf version.
|
|
|
|
pub fn from_script(script: &Script, ver: LeafVersion) -> TapNodeHash {
|
|
|
|
TapNodeHash::from(TapLeafHash::from_script(script, ver))
|
|
|
|
}
|
2022-03-31 13:16:39 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 15:54:32 +00:00
|
|
|
impl From<TapLeafHash> for TapNodeHash {
|
2023-01-28 22:47:24 +00:00
|
|
|
fn from(leaf: TapLeafHash) -> TapNodeHash { TapNodeHash::from_byte_array(leaf.to_byte_array()) }
|
2022-12-15 15:54:32 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Maximum depth of a taproot tree script spend path.
|
2021-10-06 23:00:21 +00:00
|
|
|
// https://github.com/bitcoin/bitcoin/blob/e826b22da252e0599c61d21c98ff89f366b3120f/src/script/interpreter.h#L229
|
|
|
|
pub const TAPROOT_CONTROL_MAX_NODE_COUNT: usize = 128;
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Size of a taproot control node.
|
2021-10-06 23:00:21 +00:00
|
|
|
// https://github.com/bitcoin/bitcoin/blob/e826b22da252e0599c61d21c98ff89f366b3120f/src/script/interpreter.h#L228
|
|
|
|
pub const TAPROOT_CONTROL_NODE_SIZE: usize = 32;
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Tapleaf mask for getting the leaf version from first byte of control block.
|
2021-10-06 23:00:21 +00:00
|
|
|
// https://github.com/bitcoin/bitcoin/blob/e826b22da252e0599c61d21c98ff89f366b3120f/src/script/interpreter.h#L225
|
|
|
|
pub const TAPROOT_LEAF_MASK: u8 = 0xfe;
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Tapscript leaf version.
|
2021-10-06 23:00:21 +00:00
|
|
|
// https://github.com/bitcoin/bitcoin/blob/e826b22da252e0599c61d21c98ff89f366b3120f/src/script/interpreter.h#L226
|
|
|
|
pub const TAPROOT_LEAF_TAPSCRIPT: u8 = 0xc0;
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Taproot annex prefix.
|
2021-11-24 23:01:21 +00:00
|
|
|
pub const TAPROOT_ANNEX_PREFIX: u8 = 0x50;
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Tapscript control base size.
|
2021-10-06 23:00:21 +00:00
|
|
|
// https://github.com/bitcoin/bitcoin/blob/e826b22da252e0599c61d21c98ff89f366b3120f/src/script/interpreter.h#L227
|
|
|
|
pub const TAPROOT_CONTROL_BASE_SIZE: usize = 33;
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Tapscript control max size.
|
2021-10-06 23:00:21 +00:00
|
|
|
// https://github.com/bitcoin/bitcoin/blob/e826b22da252e0599c61d21c98ff89f366b3120f/src/script/interpreter.h#L230
|
|
|
|
pub const TAPROOT_CONTROL_MAX_SIZE: usize =
|
|
|
|
TAPROOT_CONTROL_BASE_SIZE + TAPROOT_CONTROL_NODE_SIZE * TAPROOT_CONTROL_MAX_NODE_COUNT;
|
|
|
|
|
2021-10-08 22:03:49 +00:00
|
|
|
// type alias for versioned tap script corresponding merkle proof
|
2022-07-30 12:22:18 +00:00
|
|
|
type ScriptMerkleProofMap = BTreeMap<(ScriptBuf, LeafVersion), BTreeSet<TaprootMerkleBranch>>;
|
2021-11-24 23:01:21 +00:00
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Represents taproot spending information.
|
2021-11-03 18:21:44 +00:00
|
|
|
///
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Taproot output corresponds to a combination of a single public key condition (known as the
|
|
|
|
/// internal key), and zero or more general conditions encoded in scripts organized in the form of a
|
|
|
|
/// binary tree.
|
2021-11-03 18:21:44 +00:00
|
|
|
///
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Taproot can be spent by either:
|
|
|
|
/// - Spending using the key path i.e., with secret key corresponding to the tweaked `output_key`.
|
|
|
|
/// - By satisfying any of the scripts in the script spend path. Each script can be satisfied by
|
|
|
|
/// providing a witness stack consisting of the script's inputs, plus the script itself and the
|
|
|
|
/// control block.
|
2021-11-03 18:21:44 +00:00
|
|
|
///
|
2022-03-28 03:07:07 +00:00
|
|
|
/// If one or more of the spending conditions consist of just a single key (after aggregation), the
|
|
|
|
/// most likely key should be made the internal key.
|
|
|
|
/// See [BIP341](https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki) for more details on
|
|
|
|
/// choosing internal keys for a taproot application.
|
|
|
|
///
|
|
|
|
/// Note: This library currently does not support
|
|
|
|
/// [annex](https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#cite_note-5).
|
2021-11-03 18:21:44 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
pub struct TaprootSpendInfo {
|
|
|
|
/// The BIP341 internal key.
|
2021-11-29 02:19:21 +00:00
|
|
|
internal_key: UntweakedPublicKey,
|
2022-03-28 03:07:07 +00:00
|
|
|
/// The merkle root of the script tree (None if there are no scripts).
|
2022-12-15 15:54:32 +00:00
|
|
|
merkle_root: Option<TapNodeHash>,
|
2022-03-28 03:07:07 +00:00
|
|
|
/// The sign final output pubkey as per BIP 341.
|
2022-01-03 02:52:44 +00:00
|
|
|
output_key_parity: secp256k1::Parity,
|
2022-03-28 03:07:07 +00:00
|
|
|
/// The tweaked output key.
|
2021-11-29 02:19:21 +00:00
|
|
|
output_key: TweakedPublicKey,
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Map from (script, leaf_version) to (sets of) [`TaprootMerkleBranch`]. More than one control
|
|
|
|
/// block for a given script is only possible if it appears in multiple branches of the tree. In
|
|
|
|
/// all cases, keeping one should be enough for spending funds, but we keep all of the paths so
|
|
|
|
/// that a full tree can be constructed again from spending data if required.
|
2021-10-08 22:03:49 +00:00
|
|
|
script_map: ScriptMerkleProofMap,
|
2021-11-03 18:21:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TaprootSpendInfo {
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Creates a new [`TaprootSpendInfo`] from a list of scripts (with default script version) and
|
|
|
|
/// weights of satisfaction for that script.
|
2021-11-15 18:02:46 +00:00
|
|
|
///
|
2022-03-28 03:07:07 +00:00
|
|
|
/// See [`TaprootBuilder::with_huffman_tree`] for more detailed documentation.
|
2021-11-06 11:56:51 +00:00
|
|
|
pub fn with_huffman_tree<C, I>(
|
|
|
|
secp: &Secp256k1<C>,
|
2021-11-29 02:19:21 +00:00
|
|
|
internal_key: UntweakedPublicKey,
|
2021-11-06 11:56:51 +00:00
|
|
|
script_weights: I,
|
|
|
|
) -> Result<Self, TaprootBuilderError>
|
|
|
|
where
|
2022-07-30 12:22:18 +00:00
|
|
|
I: IntoIterator<Item = (u32, ScriptBuf)>,
|
2021-11-06 11:56:51 +00:00
|
|
|
C: secp256k1::Verification,
|
|
|
|
{
|
2022-07-29 00:20:16 +00:00
|
|
|
let builder = TaprootBuilder::with_huffman_tree(script_weights)?;
|
|
|
|
Ok(builder.finalize(secp, internal_key).expect("Huffman Tree is always complete"))
|
2021-11-06 11:56:51 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Creates a new key spend with `internal_key` and `merkle_root`. Provide [`None`] for
|
2022-03-30 23:56:36 +00:00
|
|
|
/// the `merkle_root` if there is no script path.
|
2021-11-03 18:21:44 +00:00
|
|
|
///
|
|
|
|
/// *Note*: As per BIP341
|
|
|
|
///
|
2022-03-28 03:07:07 +00:00
|
|
|
/// When the merkle root is [`None`], the output key commits to an unspendable script path
|
|
|
|
/// instead of having no script path. This is achieved by computing the output key point as
|
|
|
|
/// `Q = P + int(hashTapTweak(bytes(P)))G`. See also [`TaprootSpendInfo::tap_tweak`].
|
2021-11-03 18:21:44 +00:00
|
|
|
///
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Refer to BIP 341 footnote ('Why should the output key always have a taproot commitment, even
|
|
|
|
/// if there is no script path?') for more details.
|
2021-11-03 18:21:44 +00:00
|
|
|
pub fn new_key_spend<C: secp256k1::Verification>(
|
|
|
|
secp: &Secp256k1<C>,
|
2021-11-29 02:19:21 +00:00
|
|
|
internal_key: UntweakedPublicKey,
|
2022-12-15 15:54:32 +00:00
|
|
|
merkle_root: Option<TapNodeHash>,
|
2021-11-03 18:21:44 +00:00
|
|
|
) -> Self {
|
2021-12-02 03:43:38 +00:00
|
|
|
let (output_key, parity) = internal_key.tap_tweak(secp, merkle_root);
|
2021-11-03 18:21:44 +00:00
|
|
|
Self {
|
2022-05-25 03:15:20 +00:00
|
|
|
internal_key,
|
|
|
|
merkle_root,
|
2021-11-03 18:21:44 +00:00
|
|
|
output_key_parity: parity,
|
2022-05-25 03:15:20 +00:00
|
|
|
output_key,
|
2021-11-03 18:21:44 +00:00
|
|
|
script_map: BTreeMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Returns the `TapTweakHash` for this [`TaprootSpendInfo`] i.e., the tweak using `internal_key`
|
|
|
|
/// and `merkle_root`.
|
2021-11-03 18:21:44 +00:00
|
|
|
pub fn tap_tweak(&self) -> TapTweakHash {
|
|
|
|
TapTweakHash::from_key_and_tweak(self.internal_key, self.merkle_root)
|
|
|
|
}
|
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Returns the internal key for this [`TaprootSpendInfo`].
|
2022-11-30 01:04:20 +00:00
|
|
|
pub fn internal_key(&self) -> UntweakedPublicKey { self.internal_key }
|
2021-11-03 18:21:44 +00:00
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Returns the merkle root for this [`TaprootSpendInfo`].
|
2022-12-15 15:54:32 +00:00
|
|
|
pub fn merkle_root(&self) -> Option<TapNodeHash> { self.merkle_root }
|
2021-11-03 18:21:44 +00:00
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Returns the output key (the key used in script pubkey) for this [`TaprootSpendInfo`].
|
2022-11-30 01:04:20 +00:00
|
|
|
pub fn output_key(&self) -> TweakedPublicKey { self.output_key }
|
2021-11-03 18:21:44 +00:00
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Returns the parity of the output key. See also [`TaprootSpendInfo::output_key`].
|
2022-11-30 01:04:20 +00:00
|
|
|
pub fn output_key_parity(&self) -> secp256k1::Parity { self.output_key_parity }
|
2021-10-08 20:08:08 +00:00
|
|
|
|
2023-06-02 06:55:26 +00:00
|
|
|
/// Returns a reference to the internal script map.
|
|
|
|
pub fn script_map(&self) -> &ScriptMerkleProofMap { &self.script_map }
|
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Computes the [`TaprootSpendInfo`] from `internal_key` and `node`.
|
|
|
|
///
|
|
|
|
/// This is useful when you want to manually build a taproot tree without using
|
|
|
|
/// [`TaprootBuilder`].
|
2022-03-27 05:01:31 +00:00
|
|
|
pub fn from_node_info<C: secp256k1::Verification>(
|
2021-10-08 20:08:08 +00:00
|
|
|
secp: &Secp256k1<C>,
|
2021-11-29 02:19:21 +00:00
|
|
|
internal_key: UntweakedPublicKey,
|
2021-10-08 20:08:08 +00:00
|
|
|
node: NodeInfo,
|
|
|
|
) -> TaprootSpendInfo {
|
|
|
|
// Create as if it is a key spend path with the given merkle root
|
2022-12-15 16:17:39 +00:00
|
|
|
let root_hash = Some(node.hash);
|
2021-10-08 20:08:08 +00:00
|
|
|
let mut info = TaprootSpendInfo::new_key_spend(secp, internal_key, root_hash);
|
2023-01-11 12:25:08 +00:00
|
|
|
|
2021-10-08 20:08:08 +00:00
|
|
|
for leaves in node.leaves {
|
2023-01-11 12:25:08 +00:00
|
|
|
match leaves.leaf {
|
|
|
|
TapLeaf::Hidden(_) => {
|
|
|
|
// We don't store any information about hidden nodes in TaprootSpendInfo.
|
|
|
|
}
|
|
|
|
TapLeaf::Script(script, ver) => {
|
|
|
|
let key = (script, ver);
|
|
|
|
let value = leaves.merkle_branch;
|
|
|
|
match info.script_map.get_mut(&key) {
|
|
|
|
None => {
|
|
|
|
let mut set = BTreeSet::new();
|
|
|
|
set.insert(value);
|
|
|
|
info.script_map.insert(key, set);
|
2022-11-02 22:36:37 +00:00
|
|
|
}
|
2023-01-11 12:25:08 +00:00
|
|
|
Some(set) => {
|
|
|
|
set.insert(value);
|
|
|
|
}
|
|
|
|
}
|
2022-11-02 22:36:37 +00:00
|
|
|
}
|
2021-10-08 20:08:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
info
|
|
|
|
}
|
2021-10-08 22:03:49 +00:00
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Constructs a [`ControlBlock`] for particular script with the given version.
|
|
|
|
///
|
|
|
|
/// # Returns
|
|
|
|
///
|
|
|
|
/// - If there are multiple control blocks possible, returns the shortest one.
|
|
|
|
/// - If the script is not contained in the [`TaprootSpendInfo`], returns `None`.
|
2022-07-30 12:22:18 +00:00
|
|
|
pub fn control_block(&self, script_ver: &(ScriptBuf, LeafVersion)) -> Option<ControlBlock> {
|
2021-10-08 22:03:49 +00:00
|
|
|
let merkle_branch_set = self.script_map.get(script_ver)?;
|
|
|
|
// Choose the smallest one amongst the multiple script maps
|
|
|
|
let smallest = merkle_branch_set
|
|
|
|
.iter()
|
|
|
|
.min_by(|x, y| x.0.len().cmp(&y.0.len()))
|
2022-07-30 12:22:18 +00:00
|
|
|
.expect("Invariant: ScriptBuf map key must contain non-empty set value");
|
2021-10-08 22:03:49 +00:00
|
|
|
Some(ControlBlock {
|
|
|
|
internal_key: self.internal_key,
|
|
|
|
output_key_parity: self.output_key_parity,
|
2021-11-16 10:45:23 +00:00
|
|
|
leaf_version: script_ver.1,
|
2021-10-08 22:03:49 +00:00
|
|
|
merkle_branch: smallest.clone(),
|
|
|
|
})
|
|
|
|
}
|
2021-10-08 20:08:08 +00:00
|
|
|
}
|
|
|
|
|
2022-09-16 12:29:05 +00:00
|
|
|
impl From<TaprootSpendInfo> for TapTweakHash {
|
2022-11-30 01:04:20 +00:00
|
|
|
fn from(spend_info: TaprootSpendInfo) -> TapTweakHash { spend_info.tap_tweak() }
|
2022-09-16 12:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&TaprootSpendInfo> for TapTweakHash {
|
2022-11-30 01:04:20 +00:00
|
|
|
fn from(spend_info: &TaprootSpendInfo) -> TapTweakHash { spend_info.tap_tweak() }
|
2022-09-16 12:29:05 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Builder for building taproot iteratively. Users can specify tap leaf or omitted/hidden branches
|
|
|
|
/// in a depth-first search (DFS) walk order to construct this tree.
|
|
|
|
///
|
|
|
|
/// See Wikipedia for more details on [DFS](https://en.wikipedia.org/wiki/Depth-first_search).
|
2022-11-05 19:52:38 +00:00
|
|
|
// Similar to Taproot Builder in Bitcoin Core.
|
2021-10-08 20:08:08 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
pub struct TaprootBuilder {
|
2022-11-05 19:52:38 +00:00
|
|
|
// The following doc-comment is from Bitcoin Core, but modified for Rust. It describes the
|
2022-03-28 03:07:07 +00:00
|
|
|
// current state of the builder for a given tree.
|
2021-10-08 20:08:08 +00:00
|
|
|
//
|
2022-03-28 03:07:07 +00:00
|
|
|
// For each level in the tree, one NodeInfo object may be present. Branch at index 0 is
|
|
|
|
// information about the root; further values are for deeper subtrees being explored.
|
2021-10-08 20:08:08 +00:00
|
|
|
//
|
2022-03-28 03:07:07 +00:00
|
|
|
// During the construction of Taptree, for every right branch taken to reach the position we're
|
|
|
|
// currently working on, there will be a `(Some(_))` entry in branch corresponding to the left
|
|
|
|
// branch at that level.
|
2021-10-08 20:08:08 +00:00
|
|
|
//
|
|
|
|
// For example, imagine this tree: - N0 -
|
|
|
|
// / \
|
|
|
|
// N1 N2
|
|
|
|
// / \ / \
|
|
|
|
// A B C N3
|
|
|
|
// / \
|
|
|
|
// D E
|
|
|
|
//
|
2022-03-28 03:07:07 +00:00
|
|
|
// Initially, branch is empty. After processing leaf A, it would become {None, None, A}. When
|
|
|
|
// processing leaf B, an entry at level 2 already exists, and it would thus be combined with it
|
|
|
|
// to produce a level 1 entry, resulting in {None, N1}. Adding C and D takes us to {None, N1, C}
|
|
|
|
// and {None, N1, C, D} respectively. When E is processed, it is combined with D, and then C,
|
|
|
|
// and then N1, to produce the root, resulting in {N0}.
|
2021-10-08 20:08:08 +00:00
|
|
|
//
|
2022-03-28 03:07:07 +00:00
|
|
|
// This structure allows processing with just O(log n) overhead if the leaves are computed on
|
|
|
|
// the fly.
|
2021-10-08 20:08:08 +00:00
|
|
|
//
|
2022-03-28 03:07:07 +00:00
|
|
|
// As an invariant, there can never be None entries at the end. There can also not be more than
|
|
|
|
// 128 entries (as that would mean more than 128 levels in the tree). The depth of newly added
|
|
|
|
// entries will always be at least equal to the current size of branch (otherwise it does not
|
|
|
|
// correspond to a depth-first traversal of a tree). A branch is only empty if no entries have
|
|
|
|
// ever be processed. A branch having length 1 corresponds to being done.
|
2021-10-08 20:08:08 +00:00
|
|
|
branch: Vec<Option<NodeInfo>>,
|
2021-11-03 18:21:44 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 20:08:08 +00:00
|
|
|
impl TaprootBuilder {
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Creates a new instance of [`TaprootBuilder`].
|
2022-11-30 01:04:20 +00:00
|
|
|
pub fn new() -> Self { TaprootBuilder { branch: vec![] } }
|
2022-03-24 21:54:58 +00:00
|
|
|
|
2023-01-11 12:25:08 +00:00
|
|
|
/// Creates a new instance of [`TaprootBuilder`] with a capacity hint for `size` elements.
|
|
|
|
///
|
|
|
|
/// The size here should be maximum depth of the tree.
|
|
|
|
pub fn with_capacity(size: usize) -> Self {
|
2022-11-02 22:36:37 +00:00
|
|
|
TaprootBuilder { branch: Vec::with_capacity(size) }
|
2023-01-11 12:25:08 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Creates a new [`TaprootSpendInfo`] from a list of scripts (with default script version) and
|
|
|
|
/// weights of satisfaction for that script.
|
|
|
|
///
|
|
|
|
/// The weights represent the probability of each branch being taken. If probabilities/weights
|
|
|
|
/// for each condition are known, constructing the tree as a Huffman Tree is the optimal way to
|
|
|
|
/// minimize average case satisfaction cost. This function takes as input an iterator of
|
2022-07-30 12:22:18 +00:00
|
|
|
/// `tuple(u32, ScriptBuf)` where `u32` represents the satisfaction weights of the branch. For
|
2022-03-28 03:07:07 +00:00
|
|
|
/// example, [(3, S1), (2, S2), (5, S3)] would construct a [`TapTree`] that has optimal
|
2022-03-24 21:54:58 +00:00
|
|
|
/// satisfaction weight when probability for S1 is 30%, S2 is 20% and S3 is 50%.
|
|
|
|
///
|
|
|
|
/// # Errors:
|
|
|
|
///
|
2022-03-28 03:07:07 +00:00
|
|
|
/// - When the optimal Huffman Tree has a depth more than 128.
|
|
|
|
/// - If the provided list of script weights is empty.
|
2022-03-24 21:54:58 +00:00
|
|
|
///
|
|
|
|
/// # Edge Cases:
|
2022-03-28 03:07:07 +00:00
|
|
|
///
|
|
|
|
/// If the script weight calculations overflow, a sub-optimal tree may be generated. This should
|
|
|
|
/// not happen unless you are dealing with billions of branches with weights close to 2^32.
|
|
|
|
///
|
2023-01-08 15:01:41 +00:00
|
|
|
/// [`TapTree`]: crate::taproot::TapTree
|
2022-11-30 01:04:20 +00:00
|
|
|
pub fn with_huffman_tree<I>(script_weights: I) -> Result<Self, TaprootBuilderError>
|
2022-03-24 21:54:58 +00:00
|
|
|
where
|
2022-07-30 12:22:18 +00:00
|
|
|
I: IntoIterator<Item = (u32, ScriptBuf)>,
|
2022-03-24 21:54:58 +00:00
|
|
|
{
|
2022-03-28 15:15:28 +00:00
|
|
|
let mut node_weights = BinaryHeap::<(Reverse<u32>, NodeInfo)>::new();
|
2022-03-24 21:54:58 +00:00
|
|
|
for (p, leaf) in script_weights {
|
2022-11-30 01:04:20 +00:00
|
|
|
node_weights
|
|
|
|
.push((Reverse(p), NodeInfo::new_leaf_with_ver(leaf, LeafVersion::TapScript)));
|
2022-03-24 21:54:58 +00:00
|
|
|
}
|
|
|
|
if node_weights.is_empty() {
|
2022-07-29 00:18:57 +00:00
|
|
|
return Err(TaprootBuilderError::EmptyTree);
|
2022-03-24 21:54:58 +00:00
|
|
|
}
|
|
|
|
while node_weights.len() > 1 {
|
|
|
|
// Combine the last two elements and insert a new node
|
|
|
|
let (p1, s1) = node_weights.pop().expect("len must be at least two");
|
|
|
|
let (p2, s2) = node_weights.pop().expect("len must be at least two");
|
|
|
|
// Insert the sum of first two in the tree as a new node
|
|
|
|
// N.B.: p1 + p2 can not practically saturate as you would need to have 2**32 max u32s
|
|
|
|
// from the input to overflow. However, saturating is a reasonable behavior here as
|
|
|
|
// huffman tree construction would treat all such elements as "very likely".
|
|
|
|
let p = Reverse(p1.0.saturating_add(p2.0));
|
|
|
|
node_weights.push((p, NodeInfo::combine(s1, s2)?));
|
|
|
|
}
|
|
|
|
// Every iteration of the loop reduces the node_weights.len() by exactly 1
|
|
|
|
// Therefore, the loop will eventually terminate with exactly 1 element
|
2022-03-28 15:15:28 +00:00
|
|
|
debug_assert_eq!(node_weights.len(), 1);
|
2022-03-24 21:54:58 +00:00
|
|
|
let node = node_weights.pop().expect("huffman tree algorithm is broken").1;
|
2022-11-30 01:04:20 +00:00
|
|
|
Ok(TaprootBuilder { branch: vec![Some(node)] })
|
2022-03-24 21:54:58 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Adds a leaf script at `depth` to the builder with script version `ver`. Errors if the leaves
|
|
|
|
/// are not provided in DFS walk order. The depth of the root node is 0.
|
2021-10-08 20:08:08 +00:00
|
|
|
pub fn add_leaf_with_ver(
|
|
|
|
self,
|
2022-03-31 13:12:05 +00:00
|
|
|
depth: u8,
|
2022-07-30 12:22:18 +00:00
|
|
|
script: ScriptBuf,
|
2021-10-08 20:08:08 +00:00
|
|
|
ver: LeafVersion,
|
|
|
|
) -> Result<Self, TaprootBuilderError> {
|
|
|
|
let leaf = NodeInfo::new_leaf_with_ver(script, ver);
|
|
|
|
self.insert(leaf, depth)
|
|
|
|
}
|
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Adds a leaf script at `depth` to the builder with default script version. Errors if the
|
|
|
|
/// leaves are not provided in DFS walk order. The depth of the root node is 0.
|
|
|
|
///
|
|
|
|
/// See [`TaprootBuilder::add_leaf_with_ver`] for adding a leaf with specific version.
|
2022-07-30 12:22:18 +00:00
|
|
|
pub fn add_leaf(self, depth: u8, script: ScriptBuf) -> Result<Self, TaprootBuilderError> {
|
2021-11-24 23:01:21 +00:00
|
|
|
self.add_leaf_with_ver(depth, script, LeafVersion::TapScript)
|
2021-10-08 20:08:08 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Adds a hidden/omitted node at `depth` to the builder. Errors if the leaves are not provided
|
|
|
|
/// in DFS walk order. The depth of the root node is 0.
|
2022-11-30 01:04:20 +00:00
|
|
|
pub fn add_hidden_node(
|
|
|
|
self,
|
|
|
|
depth: u8,
|
2022-12-15 16:17:39 +00:00
|
|
|
hash: TapNodeHash,
|
2022-11-30 01:04:20 +00:00
|
|
|
) -> Result<Self, TaprootBuilderError> {
|
2022-04-05 20:16:59 +00:00
|
|
|
let node = NodeInfo::new_hidden_node(hash);
|
2021-10-08 20:08:08 +00:00
|
|
|
self.insert(node, depth)
|
|
|
|
}
|
|
|
|
|
2022-04-05 20:26:36 +00:00
|
|
|
/// Checks if the builder has finalized building a tree.
|
2022-11-30 01:04:20 +00:00
|
|
|
pub fn is_finalizable(&self) -> bool { self.branch.len() == 1 && self.branch[0].is_some() }
|
2021-10-27 10:20:21 +00:00
|
|
|
|
2023-01-11 12:25:08 +00:00
|
|
|
/// Converts the builder into a [`NodeInfo`] if the builder is a full tree with possibly
|
|
|
|
/// hidden nodes
|
|
|
|
///
|
|
|
|
/// # Errors:
|
|
|
|
///
|
2023-10-04 02:44:07 +00:00
|
|
|
/// [`IncompleteBuilderError::NotFinalized`] if the builder is not finalized. The builder
|
|
|
|
/// can be restored by calling [`IncompleteBuilderError::into_builder`]
|
|
|
|
pub fn try_into_node_info(mut self) -> Result<NodeInfo, IncompleteBuilderError> {
|
2023-01-11 12:25:08 +00:00
|
|
|
if self.branch().len() != 1 {
|
2023-10-04 02:44:07 +00:00
|
|
|
return Err(IncompleteBuilderError::NotFinalized(self));
|
2023-01-11 12:25:08 +00:00
|
|
|
}
|
2022-11-02 22:36:37 +00:00
|
|
|
Ok(self
|
|
|
|
.branch
|
|
|
|
.pop()
|
|
|
|
.expect("length checked above")
|
|
|
|
.expect("invariant guarantees node info exists"))
|
2023-01-11 12:25:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts the builder into a [`TapTree`] if the builder is a full tree and
|
|
|
|
/// does not contain any hidden nodes
|
2023-10-04 02:44:07 +00:00
|
|
|
pub fn try_into_taptree(self) -> Result<TapTree, IncompleteBuilderError> {
|
2023-01-11 12:25:08 +00:00
|
|
|
let node = self.try_into_node_info()?;
|
|
|
|
if node.has_hidden_nodes {
|
|
|
|
// Reconstruct the builder as it was if it has hidden nodes
|
2023-10-04 02:44:07 +00:00
|
|
|
return Err(IncompleteBuilderError::HiddenParts(TaprootBuilder {
|
2022-11-02 22:36:37 +00:00
|
|
|
branch: vec![Some(node)],
|
|
|
|
}));
|
2023-01-11 12:25:08 +00:00
|
|
|
}
|
|
|
|
Ok(TapTree(node))
|
|
|
|
}
|
|
|
|
|
2022-04-01 06:06:18 +00:00
|
|
|
/// Checks if the builder has hidden nodes.
|
|
|
|
pub fn has_hidden_nodes(&self) -> bool {
|
2022-05-25 03:36:51 +00:00
|
|
|
self.branch.iter().flatten().any(|node| node.has_hidden_nodes)
|
2022-04-01 06:06:18 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Creates a [`TaprootSpendInfo`] with the given internal key.
|
2022-04-03 16:19:55 +00:00
|
|
|
///
|
2022-07-29 00:33:26 +00:00
|
|
|
/// Returns the unmodified builder as Err if the builder is not finalizable.
|
|
|
|
/// See also [`TaprootBuilder::is_finalizable`]
|
2021-10-08 20:08:08 +00:00
|
|
|
pub fn finalize<C: secp256k1::Verification>(
|
|
|
|
mut self,
|
|
|
|
secp: &Secp256k1<C>,
|
2021-11-29 02:19:21 +00:00
|
|
|
internal_key: UntweakedPublicKey,
|
2022-07-29 00:20:16 +00:00
|
|
|
) -> Result<TaprootSpendInfo, TaprootBuilder> {
|
|
|
|
match self.branch.len() {
|
|
|
|
0 => Ok(TaprootSpendInfo::new_key_spend(secp, internal_key, None)),
|
2022-11-30 01:04:20 +00:00
|
|
|
1 =>
|
2022-07-29 00:20:16 +00:00
|
|
|
if let Some(Some(node)) = self.branch.pop() {
|
|
|
|
Ok(TaprootSpendInfo::from_node_info(secp, internal_key, node))
|
|
|
|
} else {
|
|
|
|
unreachable!("Size checked above. Builder guarantees the last element is Some")
|
2022-11-30 01:04:20 +00:00
|
|
|
},
|
2022-07-29 00:20:16 +00:00
|
|
|
_ => Err(self),
|
2021-10-08 20:08:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-30 01:04:20 +00:00
|
|
|
pub(crate) fn branch(&self) -> &[Option<NodeInfo>] { &self.branch }
|
2021-10-27 10:20:21 +00:00
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Inserts a leaf at `depth`.
|
2022-03-31 13:12:05 +00:00
|
|
|
fn insert(mut self, mut node: NodeInfo, mut depth: u8) -> Result<Self, TaprootBuilderError> {
|
2021-10-08 20:08:08 +00:00
|
|
|
// early error on invalid depth. Though this will be checked later
|
|
|
|
// while constructing TaprootMerkelBranch
|
2022-03-31 13:12:05 +00:00
|
|
|
if depth as usize > TAPROOT_CONTROL_MAX_NODE_COUNT {
|
|
|
|
return Err(TaprootBuilderError::InvalidMerkleTreeDepth(depth as usize));
|
2021-10-08 20:08:08 +00:00
|
|
|
}
|
|
|
|
// We cannot insert a leaf at a lower depth while a deeper branch is unfinished. Doing
|
|
|
|
// so would mean the add_leaf/add_hidden invocations do not correspond to a DFS traversal of a
|
|
|
|
// binary tree.
|
2022-05-04 05:48:23 +00:00
|
|
|
if (depth as usize + 1) < self.branch.len() {
|
2021-10-08 20:08:08 +00:00
|
|
|
return Err(TaprootBuilderError::NodeNotInDfsOrder);
|
|
|
|
}
|
|
|
|
|
2022-03-31 13:12:05 +00:00
|
|
|
while self.branch.len() == depth as usize + 1 {
|
2021-10-08 20:08:08 +00:00
|
|
|
let child = match self.branch.pop() {
|
|
|
|
None => unreachable!("Len of branch checked to be >= 1"),
|
|
|
|
Some(Some(child)) => child,
|
|
|
|
// Needs an explicit push to add the None that we just popped.
|
|
|
|
// Cannot use .last() because of borrow checker issues.
|
|
|
|
Some(None) => {
|
|
|
|
self.branch.push(None);
|
|
|
|
break;
|
|
|
|
} // Cannot combine further
|
|
|
|
};
|
|
|
|
if depth == 0 {
|
|
|
|
// We are trying to combine two nodes at root level.
|
|
|
|
// Can't propagate further up than the root
|
|
|
|
return Err(TaprootBuilderError::OverCompleteTree);
|
|
|
|
}
|
|
|
|
node = NodeInfo::combine(node, child)?;
|
|
|
|
// Propagate to combine nodes at a lower depth
|
|
|
|
depth -= 1;
|
|
|
|
}
|
|
|
|
|
2022-03-31 13:12:05 +00:00
|
|
|
if self.branch.len() < depth as usize + 1 {
|
2021-10-08 20:08:08 +00:00
|
|
|
// add enough nodes so that we can insert node at depth `depth`
|
2022-03-31 13:12:05 +00:00
|
|
|
let num_extra_nodes = depth as usize + 1 - self.branch.len();
|
2023-03-05 17:19:03 +00:00
|
|
|
self.branch.extend((0..num_extra_nodes).map(|_| None));
|
2021-10-08 20:08:08 +00:00
|
|
|
}
|
|
|
|
// Push the last node to the branch
|
2022-03-31 13:12:05 +00:00
|
|
|
self.branch[depth as usize] = Some(node);
|
2021-10-08 20:08:08 +00:00
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-25 03:42:34 +00:00
|
|
|
impl Default for TaprootBuilder {
|
2022-11-30 01:04:20 +00:00
|
|
|
fn default() -> Self { Self::new() }
|
2022-05-25 03:42:34 +00:00
|
|
|
}
|
|
|
|
|
2023-01-08 15:01:41 +00:00
|
|
|
/// Error happening when [`TapTree`] is constructed from a [`TaprootBuilder`]
|
|
|
|
/// having hidden branches or not being finalized.
|
Make error types uniform
On our way to v1.0.0 we are defining a standard for our error types,
this includes:
- Uses the following derives (unless not possible, usually because of `io::Error`)
`#[derive(Debug, Clone, PartialEq, Eq)]`
- Has `non_exhaustive` unless we really know we can commit to not adding
anything.
Furthermore, we are trying to make the codebase easy to read. Error code
is write-once-read-many (well it should be) so if we make all the error
code super uniform the users can flick to an error and quickly see what
it includes. In an effort to achieve this I have made up a style and
over recent times have change much of the error code to that new style,
this PR audits _all_ error types in the code base and enforces the
style, specifically:
- Is layed out: definition, [impl block], Display impl, error::Error impl, From impls
- `error::Error` impl matches on enum even if it returns `None` for all variants
- Display/Error impls import enum variants locally
- match uses *self and `ref e`
- error::Error variants that return `Some` come first, `None` after
Re: non_exhaustive
To make dev and review easier I have added `non_exhaustive` to _every_
error type. We can then remove it error by error as we see fit. This is
because it takes a bit of thinking to do and review where as this patch
should not take much brain power to review.
2023-10-04 02:55:45 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2023-01-08 15:01:41 +00:00
|
|
|
#[non_exhaustive]
|
2023-10-04 02:44:07 +00:00
|
|
|
pub enum IncompleteBuilderError {
|
2023-01-08 15:01:41 +00:00
|
|
|
/// Indicates an attempt to construct a tap tree from a builder containing incomplete branches.
|
|
|
|
NotFinalized(TaprootBuilder),
|
|
|
|
/// Indicates an attempt to construct a tap tree from a builder containing hidden parts.
|
|
|
|
HiddenParts(TaprootBuilder),
|
|
|
|
}
|
|
|
|
|
2023-10-04 02:44:07 +00:00
|
|
|
impl IncompleteBuilderError {
|
2023-01-08 15:01:41 +00:00
|
|
|
/// Converts error into the original incomplete [`TaprootBuilder`] instance.
|
|
|
|
pub fn into_builder(self) -> TaprootBuilder {
|
2023-10-04 02:44:07 +00:00
|
|
|
use IncompleteBuilderError::*;
|
|
|
|
|
2023-01-08 15:01:41 +00:00
|
|
|
match self {
|
2023-10-04 02:44:07 +00:00
|
|
|
NotFinalized(builder) | HiddenParts(builder) => builder,
|
2023-01-08 15:01:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 02:44:07 +00:00
|
|
|
impl core::fmt::Display for IncompleteBuilderError {
|
2023-01-08 15:01:41 +00:00
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
2023-10-04 02:44:07 +00:00
|
|
|
use IncompleteBuilderError::*;
|
|
|
|
|
2023-01-08 15:01:41 +00:00
|
|
|
f.write_str(match self {
|
2023-10-04 02:44:07 +00:00
|
|
|
NotFinalized(_) =>
|
2023-01-08 15:01:41 +00:00
|
|
|
"an attempt to construct a tap tree from a builder containing incomplete branches.",
|
2023-10-04 02:44:07 +00:00
|
|
|
HiddenParts(_) =>
|
2023-01-08 15:01:41 +00:00
|
|
|
"an attempt to construct a tap tree from a builder containing hidden parts.",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "std")]
|
2023-10-04 02:44:07 +00:00
|
|
|
impl std::error::Error for IncompleteBuilderError {
|
2023-01-08 15:01:41 +00:00
|
|
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
2023-10-04 02:44:07 +00:00
|
|
|
use IncompleteBuilderError::*;
|
2023-01-08 15:01:41 +00:00
|
|
|
|
2023-10-04 02:44:07 +00:00
|
|
|
match *self {
|
2023-01-08 15:01:41 +00:00
|
|
|
NotFinalized(_) | HiddenParts(_) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 12:25:08 +00:00
|
|
|
/// Error happening when [`TapTree`] is constructed from a [`NodeInfo`]
|
|
|
|
/// having hidden branches.
|
Make error types uniform
On our way to v1.0.0 we are defining a standard for our error types,
this includes:
- Uses the following derives (unless not possible, usually because of `io::Error`)
`#[derive(Debug, Clone, PartialEq, Eq)]`
- Has `non_exhaustive` unless we really know we can commit to not adding
anything.
Furthermore, we are trying to make the codebase easy to read. Error code
is write-once-read-many (well it should be) so if we make all the error
code super uniform the users can flick to an error and quickly see what
it includes. In an effort to achieve this I have made up a style and
over recent times have change much of the error code to that new style,
this PR audits _all_ error types in the code base and enforces the
style, specifically:
- Is layed out: definition, [impl block], Display impl, error::Error impl, From impls
- `error::Error` impl matches on enum even if it returns `None` for all variants
- Display/Error impls import enum variants locally
- match uses *self and `ref e`
- error::Error variants that return `Some` come first, `None` after
Re: non_exhaustive
To make dev and review easier I have added `non_exhaustive` to _every_
error type. We can then remove it error by error as we see fit. This is
because it takes a bit of thinking to do and review where as this patch
should not take much brain power to review.
2023-10-04 02:55:45 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2023-01-11 12:25:08 +00:00
|
|
|
#[non_exhaustive]
|
2023-10-04 02:48:24 +00:00
|
|
|
pub enum HiddenNodesError {
|
2023-01-11 12:25:08 +00:00
|
|
|
/// Indicates an attempt to construct a tap tree from a builder containing hidden parts.
|
|
|
|
HiddenParts(NodeInfo),
|
|
|
|
}
|
|
|
|
|
2023-10-04 02:48:24 +00:00
|
|
|
impl HiddenNodesError {
|
2023-01-11 12:25:08 +00:00
|
|
|
/// Converts error into the original incomplete [`NodeInfo`] instance.
|
|
|
|
pub fn into_node_info(self) -> NodeInfo {
|
Make error types uniform
On our way to v1.0.0 we are defining a standard for our error types,
this includes:
- Uses the following derives (unless not possible, usually because of `io::Error`)
`#[derive(Debug, Clone, PartialEq, Eq)]`
- Has `non_exhaustive` unless we really know we can commit to not adding
anything.
Furthermore, we are trying to make the codebase easy to read. Error code
is write-once-read-many (well it should be) so if we make all the error
code super uniform the users can flick to an error and quickly see what
it includes. In an effort to achieve this I have made up a style and
over recent times have change much of the error code to that new style,
this PR audits _all_ error types in the code base and enforces the
style, specifically:
- Is layed out: definition, [impl block], Display impl, error::Error impl, From impls
- `error::Error` impl matches on enum even if it returns `None` for all variants
- Display/Error impls import enum variants locally
- match uses *self and `ref e`
- error::Error variants that return `Some` come first, `None` after
Re: non_exhaustive
To make dev and review easier I have added `non_exhaustive` to _every_
error type. We can then remove it error by error as we see fit. This is
because it takes a bit of thinking to do and review where as this patch
should not take much brain power to review.
2023-10-04 02:55:45 +00:00
|
|
|
use HiddenNodesError::*;
|
|
|
|
|
2023-01-11 12:25:08 +00:00
|
|
|
match self {
|
Make error types uniform
On our way to v1.0.0 we are defining a standard for our error types,
this includes:
- Uses the following derives (unless not possible, usually because of `io::Error`)
`#[derive(Debug, Clone, PartialEq, Eq)]`
- Has `non_exhaustive` unless we really know we can commit to not adding
anything.
Furthermore, we are trying to make the codebase easy to read. Error code
is write-once-read-many (well it should be) so if we make all the error
code super uniform the users can flick to an error and quickly see what
it includes. In an effort to achieve this I have made up a style and
over recent times have change much of the error code to that new style,
this PR audits _all_ error types in the code base and enforces the
style, specifically:
- Is layed out: definition, [impl block], Display impl, error::Error impl, From impls
- `error::Error` impl matches on enum even if it returns `None` for all variants
- Display/Error impls import enum variants locally
- match uses *self and `ref e`
- error::Error variants that return `Some` come first, `None` after
Re: non_exhaustive
To make dev and review easier I have added `non_exhaustive` to _every_
error type. We can then remove it error by error as we see fit. This is
because it takes a bit of thinking to do and review where as this patch
should not take much brain power to review.
2023-10-04 02:55:45 +00:00
|
|
|
HiddenParts(node_info) => node_info,
|
2023-01-11 12:25:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-08 15:01:41 +00:00
|
|
|
|
2023-10-04 02:48:24 +00:00
|
|
|
impl core::fmt::Display for HiddenNodesError {
|
2023-01-11 12:25:08 +00:00
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
Make error types uniform
On our way to v1.0.0 we are defining a standard for our error types,
this includes:
- Uses the following derives (unless not possible, usually because of `io::Error`)
`#[derive(Debug, Clone, PartialEq, Eq)]`
- Has `non_exhaustive` unless we really know we can commit to not adding
anything.
Furthermore, we are trying to make the codebase easy to read. Error code
is write-once-read-many (well it should be) so if we make all the error
code super uniform the users can flick to an error and quickly see what
it includes. In an effort to achieve this I have made up a style and
over recent times have change much of the error code to that new style,
this PR audits _all_ error types in the code base and enforces the
style, specifically:
- Is layed out: definition, [impl block], Display impl, error::Error impl, From impls
- `error::Error` impl matches on enum even if it returns `None` for all variants
- Display/Error impls import enum variants locally
- match uses *self and `ref e`
- error::Error variants that return `Some` come first, `None` after
Re: non_exhaustive
To make dev and review easier I have added `non_exhaustive` to _every_
error type. We can then remove it error by error as we see fit. This is
because it takes a bit of thinking to do and review where as this patch
should not take much brain power to review.
2023-10-04 02:55:45 +00:00
|
|
|
use HiddenNodesError::*;
|
|
|
|
|
2023-01-11 12:25:08 +00:00
|
|
|
f.write_str(match self {
|
Make error types uniform
On our way to v1.0.0 we are defining a standard for our error types,
this includes:
- Uses the following derives (unless not possible, usually because of `io::Error`)
`#[derive(Debug, Clone, PartialEq, Eq)]`
- Has `non_exhaustive` unless we really know we can commit to not adding
anything.
Furthermore, we are trying to make the codebase easy to read. Error code
is write-once-read-many (well it should be) so if we make all the error
code super uniform the users can flick to an error and quickly see what
it includes. In an effort to achieve this I have made up a style and
over recent times have change much of the error code to that new style,
this PR audits _all_ error types in the code base and enforces the
style, specifically:
- Is layed out: definition, [impl block], Display impl, error::Error impl, From impls
- `error::Error` impl matches on enum even if it returns `None` for all variants
- Display/Error impls import enum variants locally
- match uses *self and `ref e`
- error::Error variants that return `Some` come first, `None` after
Re: non_exhaustive
To make dev and review easier I have added `non_exhaustive` to _every_
error type. We can then remove it error by error as we see fit. This is
because it takes a bit of thinking to do and review where as this patch
should not take much brain power to review.
2023-10-04 02:55:45 +00:00
|
|
|
HiddenParts(_) =>
|
2023-01-11 12:25:08 +00:00
|
|
|
"an attempt to construct a tap tree from a node_info containing hidden parts.",
|
|
|
|
})
|
|
|
|
}
|
2023-01-08 15:01:41 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 12:25:08 +00:00
|
|
|
#[cfg(feature = "std")]
|
2023-10-04 02:48:24 +00:00
|
|
|
impl std::error::Error for HiddenNodesError {
|
2023-01-11 12:25:08 +00:00
|
|
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
Make error types uniform
On our way to v1.0.0 we are defining a standard for our error types,
this includes:
- Uses the following derives (unless not possible, usually because of `io::Error`)
`#[derive(Debug, Clone, PartialEq, Eq)]`
- Has `non_exhaustive` unless we really know we can commit to not adding
anything.
Furthermore, we are trying to make the codebase easy to read. Error code
is write-once-read-many (well it should be) so if we make all the error
code super uniform the users can flick to an error and quickly see what
it includes. In an effort to achieve this I have made up a style and
over recent times have change much of the error code to that new style,
this PR audits _all_ error types in the code base and enforces the
style, specifically:
- Is layed out: definition, [impl block], Display impl, error::Error impl, From impls
- `error::Error` impl matches on enum even if it returns `None` for all variants
- Display/Error impls import enum variants locally
- match uses *self and `ref e`
- error::Error variants that return `Some` come first, `None` after
Re: non_exhaustive
To make dev and review easier I have added `non_exhaustive` to _every_
error type. We can then remove it error by error as we see fit. This is
because it takes a bit of thinking to do and review where as this patch
should not take much brain power to review.
2023-10-04 02:55:45 +00:00
|
|
|
use HiddenNodesError::*;
|
2023-01-11 12:25:08 +00:00
|
|
|
|
|
|
|
match self {
|
|
|
|
HiddenParts(_) => None,
|
|
|
|
}
|
|
|
|
}
|
2023-01-08 15:01:41 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 12:25:08 +00:00
|
|
|
/// Taproot Tree representing a complete binary tree without any hidden nodes.
|
|
|
|
///
|
|
|
|
/// This is in contrast to [`NodeInfo`], which allows hidden nodes.
|
|
|
|
/// The implementations for Eq, PartialEq and Hash compare the merkle root of the tree
|
|
|
|
//
|
|
|
|
// This is a bug in BIP370 that does not specify how to share trees with hidden nodes,
|
|
|
|
// for which we need a separate type.
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
|
|
|
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
|
|
|
#[cfg_attr(feature = "serde", serde(crate = "actual_serde"))]
|
|
|
|
#[cfg_attr(feature = "serde", serde(into = "NodeInfo"))]
|
|
|
|
#[cfg_attr(feature = "serde", serde(try_from = "NodeInfo"))]
|
|
|
|
pub struct TapTree(NodeInfo);
|
2023-01-08 15:01:41 +00:00
|
|
|
|
2023-01-11 12:25:08 +00:00
|
|
|
impl From<TapTree> for NodeInfo {
|
2023-01-08 15:01:41 +00:00
|
|
|
#[inline]
|
2023-01-11 12:25:08 +00:00
|
|
|
fn from(tree: TapTree) -> Self { tree.into_node_info() }
|
2023-01-08 15:01:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TapTree {
|
2023-01-11 12:25:08 +00:00
|
|
|
/// Gets the reference to inner [`NodeInfo`] of this tree root.
|
2022-11-02 22:36:37 +00:00
|
|
|
pub fn node_info(&self) -> &NodeInfo { &self.0 }
|
2023-01-08 15:01:41 +00:00
|
|
|
|
2023-01-11 12:25:08 +00:00
|
|
|
/// Gets the inner [`NodeInfo`] of this tree root.
|
2022-11-02 22:36:37 +00:00
|
|
|
pub fn into_node_info(self) -> NodeInfo { self.0 }
|
2023-01-08 15:01:41 +00:00
|
|
|
|
|
|
|
/// Returns [`TapTreeIter<'_>`] iterator for a taproot script tree, operating in DFS order over
|
|
|
|
/// tree [`ScriptLeaf`]s.
|
2022-11-02 22:36:37 +00:00
|
|
|
pub fn script_leaves(&self) -> ScriptLeaves { ScriptLeaves { leaf_iter: self.0.leaf_nodes() } }
|
2023-01-08 15:01:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<TaprootBuilder> for TapTree {
|
2023-10-04 02:44:07 +00:00
|
|
|
type Error = IncompleteBuilderError;
|
2023-01-08 15:01:41 +00:00
|
|
|
|
|
|
|
/// Constructs [`TapTree`] from a [`TaprootBuilder`] if it is complete binary tree.
|
|
|
|
///
|
|
|
|
/// # Returns
|
|
|
|
///
|
2023-10-04 02:44:07 +00:00
|
|
|
/// A [`TapTree`] iff the `builder` is complete, otherwise return [`IncompleteBuilderError`]
|
2023-01-08 15:01:41 +00:00
|
|
|
/// error with the content of incomplete `builder` instance.
|
2022-11-02 22:36:37 +00:00
|
|
|
fn try_from(builder: TaprootBuilder) -> Result<Self, Self::Error> { builder.try_into_taptree() }
|
2023-01-11 12:25:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<NodeInfo> for TapTree {
|
2023-10-04 02:48:24 +00:00
|
|
|
type Error = HiddenNodesError;
|
2023-01-11 12:25:08 +00:00
|
|
|
|
|
|
|
/// Constructs [`TapTree`] from a [`NodeInfo`] if it is complete binary tree.
|
|
|
|
///
|
|
|
|
/// # Returns
|
|
|
|
///
|
2023-10-04 02:48:24 +00:00
|
|
|
/// A [`TapTree`] iff the [`NodeInfo`] has no hidden nodes, otherwise return
|
|
|
|
/// [`HiddenNodesError`] error with the content of incomplete [`NodeInfo`] instance.
|
2023-01-11 12:25:08 +00:00
|
|
|
fn try_from(node_info: NodeInfo) -> Result<Self, Self::Error> {
|
|
|
|
if node_info.has_hidden_nodes {
|
2023-10-04 02:48:24 +00:00
|
|
|
Err(HiddenNodesError::HiddenParts(node_info))
|
2023-01-08 15:01:41 +00:00
|
|
|
} else {
|
2023-01-11 12:25:08 +00:00
|
|
|
Ok(TapTree(node_info))
|
2023-01-08 15:01:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-24 10:02:16 +00:00
|
|
|
/// Iterator for a taproot script tree, operating in DFS order yielding [`ScriptLeaf`].
|
|
|
|
///
|
|
|
|
/// Returned by [`TapTree::script_leaves`]. [`TapTree`] does not allow hidden nodes,
|
|
|
|
/// so this iterator is guaranteed to yield all known leaves.
|
|
|
|
pub struct ScriptLeaves<'tree> {
|
|
|
|
leaf_iter: LeafNodes<'tree>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tree> Iterator for ScriptLeaves<'tree> {
|
|
|
|
type Item = ScriptLeaf<'tree>;
|
|
|
|
|
|
|
|
#[inline]
|
2022-11-02 22:36:37 +00:00
|
|
|
fn next(&mut self) -> Option<Self::Item> { ScriptLeaf::from_leaf_node(self.leaf_iter.next()?) }
|
2023-01-11 12:25:08 +00:00
|
|
|
|
2023-02-24 10:02:16 +00:00
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) { self.leaf_iter.size_hint() }
|
2023-01-08 15:01:41 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 22:36:37 +00:00
|
|
|
impl<'tree> ExactSizeIterator for ScriptLeaves<'tree> {}
|
2023-02-24 10:02:16 +00:00
|
|
|
|
2022-11-02 22:36:37 +00:00
|
|
|
impl<'tree> FusedIterator for ScriptLeaves<'tree> {}
|
2023-01-08 15:01:41 +00:00
|
|
|
|
2022-11-02 22:36:37 +00:00
|
|
|
impl<'tree> DoubleEndedIterator for ScriptLeaves<'tree> {
|
2023-01-08 15:01:41 +00:00
|
|
|
#[inline]
|
2023-02-24 10:02:16 +00:00
|
|
|
fn next_back(&mut self) -> Option<Self::Item> {
|
2022-11-02 22:36:37 +00:00
|
|
|
ScriptLeaf::from_leaf_node(self.leaf_iter.next_back()?)
|
2023-02-24 10:02:16 +00:00
|
|
|
}
|
2023-01-08 15:01:41 +00:00
|
|
|
}
|
2023-02-24 10:02:16 +00:00
|
|
|
/// Iterator for a taproot script tree, operating in DFS order yielding [`LeafNode`].
|
|
|
|
///
|
|
|
|
/// Returned by [`NodeInfo::leaf_nodes`]. This can potentially yield hidden nodes.
|
|
|
|
pub struct LeafNodes<'a> {
|
|
|
|
leaf_iter: core::slice::Iter<'a, LeafNode>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Iterator for LeafNodes<'a> {
|
|
|
|
type Item = &'a LeafNode;
|
2023-01-08 15:01:41 +00:00
|
|
|
|
2023-01-11 12:25:08 +00:00
|
|
|
#[inline]
|
2023-02-24 10:02:16 +00:00
|
|
|
fn next(&mut self) -> Option<Self::Item> { self.leaf_iter.next() }
|
|
|
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) { self.leaf_iter.size_hint() }
|
2023-01-11 12:25:08 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 22:36:37 +00:00
|
|
|
impl<'tree> ExactSizeIterator for LeafNodes<'tree> {}
|
2023-02-24 10:02:16 +00:00
|
|
|
|
2022-11-02 22:36:37 +00:00
|
|
|
impl<'tree> FusedIterator for LeafNodes<'tree> {}
|
2023-02-24 10:02:16 +00:00
|
|
|
|
2022-11-02 22:36:37 +00:00
|
|
|
impl<'tree> DoubleEndedIterator for LeafNodes<'tree> {
|
2023-02-24 10:02:16 +00:00
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<Self::Item> { self.leaf_iter.next_back() }
|
|
|
|
}
|
2023-01-11 12:25:08 +00:00
|
|
|
/// Represents the node information in taproot tree. In contrast to [`TapTree`], this
|
|
|
|
/// is allowed to have hidden leaves as children.
|
2022-03-28 03:07:07 +00:00
|
|
|
///
|
2022-03-30 23:57:37 +00:00
|
|
|
/// Helper type used in merkle tree construction allowing one to build sparse merkle trees. The node
|
|
|
|
/// represents part of the tree that has information about all of its descendants.
|
|
|
|
/// See how [`TaprootBuilder`] works for more details.
|
|
|
|
///
|
|
|
|
/// You can use [`TaprootSpendInfo::from_node_info`] to a get a [`TaprootSpendInfo`] from the merkle
|
|
|
|
/// root [`NodeInfo`].
|
2023-01-11 12:25:08 +00:00
|
|
|
#[derive(Debug, Clone, PartialOrd, Ord)]
|
2022-03-27 05:01:31 +00:00
|
|
|
pub struct NodeInfo {
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Merkle hash for this node.
|
2022-12-15 16:17:39 +00:00
|
|
|
pub(crate) hash: TapNodeHash,
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Information about leaves inside this node.
|
2023-02-24 10:02:16 +00:00
|
|
|
pub(crate) leaves: Vec<LeafNode>,
|
2022-04-01 06:05:41 +00:00
|
|
|
/// Tracks information on hidden nodes below this node.
|
|
|
|
pub(crate) has_hidden_nodes: bool,
|
2021-10-08 20:08:08 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 12:25:08 +00:00
|
|
|
impl PartialEq for NodeInfo {
|
|
|
|
fn eq(&self, other: &Self) -> bool { self.hash.eq(&other.hash) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl core::hash::Hash for NodeInfo {
|
|
|
|
fn hash<H: core::hash::Hasher>(&self, state: &mut H) { self.hash.hash(state) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for NodeInfo {}
|
|
|
|
|
2021-10-08 20:08:08 +00:00
|
|
|
impl NodeInfo {
|
2022-03-27 05:01:31 +00:00
|
|
|
/// Creates a new [`NodeInfo`] with omitted/hidden info.
|
2022-12-15 16:17:39 +00:00
|
|
|
pub fn new_hidden_node(hash: TapNodeHash) -> Self {
|
2022-11-30 01:04:20 +00:00
|
|
|
Self { hash, leaves: vec![], has_hidden_nodes: true }
|
2021-10-08 20:08:08 +00:00
|
|
|
}
|
|
|
|
|
2022-07-30 12:22:18 +00:00
|
|
|
/// Creates a new leaf [`NodeInfo`] with given [`ScriptBuf`] and [`LeafVersion`].
|
|
|
|
pub fn new_leaf_with_ver(script: ScriptBuf, ver: LeafVersion) -> Self {
|
2021-10-08 20:08:08 +00:00
|
|
|
Self {
|
2022-12-15 16:17:39 +00:00
|
|
|
hash: TapNodeHash::from_script(&script, ver),
|
2023-02-24 10:02:16 +00:00
|
|
|
leaves: vec![LeafNode::new_script(script, ver)],
|
2022-04-01 06:05:41 +00:00
|
|
|
has_hidden_nodes: false,
|
2021-10-08 20:08:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-27 05:01:31 +00:00
|
|
|
/// Combines two [`NodeInfo`] to create a new parent.
|
|
|
|
pub fn combine(a: Self, b: Self) -> Result<Self, TaprootBuilderError> {
|
2021-10-08 20:08:08 +00:00
|
|
|
let mut all_leaves = Vec::with_capacity(a.leaves.len() + b.leaves.len());
|
2023-01-11 12:25:08 +00:00
|
|
|
let (hash, left_first) = TapNodeHash::combine_node_hashes(a.hash, b.hash);
|
|
|
|
let (a, b) = if left_first { (a, b) } else { (b, a) };
|
2021-10-08 20:08:08 +00:00
|
|
|
for mut a_leaf in a.leaves {
|
|
|
|
a_leaf.merkle_branch.push(b.hash)?; // add hashing partner
|
|
|
|
all_leaves.push(a_leaf);
|
|
|
|
}
|
|
|
|
for mut b_leaf in b.leaves {
|
|
|
|
b_leaf.merkle_branch.push(a.hash)?; // add hashing partner
|
|
|
|
all_leaves.push(b_leaf);
|
|
|
|
}
|
|
|
|
Ok(Self {
|
2022-12-15 16:17:39 +00:00
|
|
|
hash,
|
2021-10-08 20:08:08 +00:00
|
|
|
leaves: all_leaves,
|
2022-11-30 01:04:20 +00:00
|
|
|
has_hidden_nodes: a.has_hidden_nodes || b.has_hidden_nodes,
|
2021-10-08 20:08:08 +00:00
|
|
|
})
|
|
|
|
}
|
2023-02-24 10:02:16 +00:00
|
|
|
|
|
|
|
/// Creates an iterator over all leaves (including hidden leaves) in the tree.
|
2022-11-02 22:36:37 +00:00
|
|
|
pub fn leaf_nodes(&self) -> LeafNodes { LeafNodes { leaf_iter: self.leaves.iter() } }
|
2021-10-08 20:08:08 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 12:25:08 +00:00
|
|
|
impl TryFrom<TaprootBuilder> for NodeInfo {
|
2023-10-04 02:44:07 +00:00
|
|
|
type Error = IncompleteBuilderError;
|
2023-01-11 12:25:08 +00:00
|
|
|
|
|
|
|
fn try_from(builder: TaprootBuilder) -> Result<Self, Self::Error> {
|
|
|
|
builder.try_into_node_info()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
impl serde::Serialize for NodeInfo {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: serde::Serializer,
|
|
|
|
{
|
|
|
|
use serde::ser::SerializeSeq;
|
|
|
|
let mut seq = serializer.serialize_seq(Some(self.leaves.len() * 2))?;
|
|
|
|
for tap_leaf in self.leaves.iter() {
|
|
|
|
seq.serialize_element(&tap_leaf.merkle_branch().len())?;
|
|
|
|
seq.serialize_element(&tap_leaf.leaf)?;
|
|
|
|
}
|
|
|
|
seq.end()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
impl<'de> serde::Deserialize<'de> for NodeInfo {
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
|
where
|
|
|
|
D: serde::Deserializer<'de>,
|
|
|
|
{
|
|
|
|
struct SeqVisitor;
|
|
|
|
impl<'de> serde::de::Visitor<'de> for SeqVisitor {
|
|
|
|
type Value = NodeInfo;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
formatter.write_str("Taproot tree in DFS walk order")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
|
|
|
|
where
|
|
|
|
A: serde::de::SeqAccess<'de>,
|
|
|
|
{
|
2022-11-02 22:36:37 +00:00
|
|
|
let size = seq
|
|
|
|
.size_hint()
|
|
|
|
.map(|x| core::mem::size_of::<usize>() * 8 - x.leading_zeros() as usize)
|
2023-01-11 12:25:08 +00:00
|
|
|
.map(|x| x / 2) // Each leaf is serialized as two elements.
|
|
|
|
.unwrap_or(0)
|
|
|
|
.min(TAPROOT_CONTROL_MAX_NODE_COUNT); // no more than 128 nodes
|
|
|
|
let mut builder = TaprootBuilder::with_capacity(size);
|
|
|
|
while let Some(depth) = seq.next_element()? {
|
2022-11-02 22:36:37 +00:00
|
|
|
let tap_leaf: TapLeaf = seq
|
2023-01-11 12:25:08 +00:00
|
|
|
.next_element()?
|
|
|
|
.ok_or_else(|| serde::de::Error::custom("Missing tap_leaf"))?;
|
|
|
|
match tap_leaf {
|
|
|
|
TapLeaf::Script(script, ver) => {
|
2022-11-02 22:36:37 +00:00
|
|
|
builder =
|
|
|
|
builder.add_leaf_with_ver(depth, script, ver).map_err(|e| {
|
|
|
|
serde::de::Error::custom(format!("Leaf insertion error: {}", e))
|
|
|
|
})?;
|
|
|
|
}
|
2023-01-11 12:25:08 +00:00
|
|
|
TapLeaf::Hidden(h) => {
|
|
|
|
builder = builder.add_hidden_node(depth, h).map_err(|e| {
|
2022-11-02 22:36:37 +00:00
|
|
|
serde::de::Error::custom(format!(
|
|
|
|
"Hidden node insertion error: {}",
|
|
|
|
e
|
|
|
|
))
|
2023-01-11 12:25:08 +00:00
|
|
|
})?;
|
2022-11-02 22:36:37 +00:00
|
|
|
}
|
2023-01-11 12:25:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
NodeInfo::try_from(builder).map_err(|e| {
|
|
|
|
serde::de::Error::custom(format!("Incomplete taproot tree: {}", e))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
deserializer.deserialize_seq(SeqVisitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Leaf node in a taproot tree. Can be either hidden or known.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
|
|
|
#[cfg_attr(feature = "serde", serde(crate = "actual_serde"))]
|
|
|
|
pub enum TapLeaf {
|
|
|
|
/// A known script
|
|
|
|
Script(ScriptBuf, LeafVersion),
|
|
|
|
/// Hidden Node with the given leaf hash
|
|
|
|
Hidden(TapNodeHash),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TapLeaf {
|
|
|
|
/// Obtains the hidden leaf hash if the leaf is hidden.
|
|
|
|
pub fn as_hidden(&self) -> Option<&TapNodeHash> {
|
|
|
|
if let Self::Hidden(v) = self {
|
|
|
|
Some(v)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-24 10:02:16 +00:00
|
|
|
/// Obtains a reference to script and version if the leaf is known.
|
2023-01-11 12:25:08 +00:00
|
|
|
pub fn as_script(&self) -> Option<(&Script, LeafVersion)> {
|
|
|
|
if let Self::Script(script, ver) = self {
|
|
|
|
Some((script, *ver))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Store information about taproot leaf node.
|
2021-10-08 20:08:08 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
2023-02-24 10:02:16 +00:00
|
|
|
pub struct LeafNode {
|
2023-01-11 12:25:08 +00:00
|
|
|
/// The [`TapLeaf`]
|
|
|
|
leaf: TapLeaf,
|
2022-03-28 03:07:07 +00:00
|
|
|
/// The merkle proof (hashing partners) to get this node.
|
2022-03-31 13:56:48 +00:00
|
|
|
merkle_branch: TaprootMerkleBranch,
|
2021-10-08 20:08:08 +00:00
|
|
|
}
|
|
|
|
|
2023-02-24 10:02:16 +00:00
|
|
|
impl LeafNode {
|
2022-04-04 15:59:31 +00:00
|
|
|
/// Creates an new [`ScriptLeaf`] from `script` and `ver` and no merkle branch.
|
2023-01-11 12:25:08 +00:00
|
|
|
pub fn new_script(script: ScriptBuf, ver: LeafVersion) -> Self {
|
|
|
|
Self { leaf: TapLeaf::Script(script, ver), merkle_branch: TaprootMerkleBranch(vec![]) }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates an new [`ScriptLeaf`] from `hash` and no merkle branch.
|
|
|
|
pub fn new_hidden(hash: TapNodeHash) -> Self {
|
|
|
|
Self { leaf: TapLeaf::Hidden(hash), merkle_branch: TaprootMerkleBranch(vec![]) }
|
2021-10-08 20:08:08 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 14:29:56 +00:00
|
|
|
/// Returns the depth of this script leaf in the tap tree.
|
|
|
|
#[inline]
|
|
|
|
pub fn depth(&self) -> u8 {
|
2022-07-22 03:39:54 +00:00
|
|
|
// Depth is guarded by TAPROOT_CONTROL_MAX_NODE_COUNT.
|
2023-01-11 12:25:08 +00:00
|
|
|
u8::try_from(self.merkle_branch().0.len()).expect("depth is guaranteed to fit in a u8")
|
2022-03-31 14:29:56 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 12:25:08 +00:00
|
|
|
/// Computes a leaf hash for this [`ScriptLeaf`] if the leaf is known.
|
2023-02-24 10:02:16 +00:00
|
|
|
///
|
2023-01-11 12:25:08 +00:00
|
|
|
/// This [`TapLeafHash`] is useful while signing taproot script spends.
|
2023-02-24 10:02:16 +00:00
|
|
|
///
|
|
|
|
/// See [`LeafNode::node_hash`] for computing the [`TapNodeHash`] which returns the hidden node
|
|
|
|
/// hash if the node is hidden.
|
2022-03-31 12:59:34 +00:00
|
|
|
#[inline]
|
2023-01-11 12:25:08 +00:00
|
|
|
pub fn leaf_hash(&self) -> Option<TapLeafHash> {
|
|
|
|
let (script, ver) = self.leaf.as_script()?;
|
|
|
|
Some(TapLeafHash::from_script(script, ver))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Computes the [`TapNodeHash`] for this [`ScriptLeaf`]. This returns the
|
|
|
|
/// leaf hash if the leaf is known and the hidden node hash if the leaf is
|
|
|
|
/// hidden.
|
2023-02-24 10:02:16 +00:00
|
|
|
/// See also, [`LeafNode::leaf_hash`].
|
2023-01-11 12:25:08 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn node_hash(&self) -> TapNodeHash {
|
|
|
|
match self.leaf {
|
2022-11-02 22:36:37 +00:00
|
|
|
TapLeaf::Script(ref script, ver) => TapLeafHash::from_script(script, ver).into(),
|
2023-01-11 12:25:08 +00:00
|
|
|
TapLeaf::Hidden(ref hash) => *hash,
|
|
|
|
}
|
|
|
|
}
|
2022-03-31 13:01:48 +00:00
|
|
|
|
2023-01-11 12:25:08 +00:00
|
|
|
/// Returns reference to the leaf script if the leaf is known.
|
2022-03-31 13:01:48 +00:00
|
|
|
#[inline]
|
2023-01-11 12:25:08 +00:00
|
|
|
pub fn script(&self) -> Option<&Script> { self.leaf.as_script().map(|x| x.0) }
|
2022-03-31 13:01:48 +00:00
|
|
|
|
2023-01-11 12:25:08 +00:00
|
|
|
/// Returns leaf version of the script if the leaf is known.
|
2022-03-31 13:01:48 +00:00
|
|
|
#[inline]
|
2023-01-11 12:25:08 +00:00
|
|
|
pub fn leaf_version(&self) -> Option<LeafVersion> { self.leaf.as_script().map(|x| x.1) }
|
2022-03-31 13:01:48 +00:00
|
|
|
|
|
|
|
/// Returns reference to the merkle proof (hashing partners) to get this
|
|
|
|
/// node in form of [`TaprootMerkleBranch`].
|
|
|
|
#[inline]
|
2022-11-30 01:04:20 +00:00
|
|
|
pub fn merkle_branch(&self) -> &TaprootMerkleBranch { &self.merkle_branch }
|
2023-01-11 12:25:08 +00:00
|
|
|
|
|
|
|
/// Returns a reference to the leaf of this [`ScriptLeaf`].
|
|
|
|
#[inline]
|
2022-11-02 22:36:37 +00:00
|
|
|
pub fn leaf(&self) -> &TapLeaf { &self.leaf }
|
2021-10-08 20:08:08 +00:00
|
|
|
}
|
2021-10-08 22:03:49 +00:00
|
|
|
|
2023-02-24 10:02:16 +00:00
|
|
|
/// Script leaf node in a taproot tree along with the merkle proof to get this node.
|
|
|
|
/// Returned by [`TapTree::script_leaves`]
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
pub struct ScriptLeaf<'leaf> {
|
|
|
|
/// The version of the script leaf.
|
|
|
|
version: LeafVersion,
|
|
|
|
/// The script.
|
|
|
|
script: &'leaf Script,
|
|
|
|
/// The merkle proof (hashing partners) to get this node.
|
|
|
|
merkle_branch: &'leaf TaprootMerkleBranch,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'leaf> ScriptLeaf<'leaf> {
|
|
|
|
/// Obtains the version of the script leaf.
|
2022-11-02 22:36:37 +00:00
|
|
|
pub fn version(&self) -> LeafVersion { self.version }
|
2023-02-24 10:02:16 +00:00
|
|
|
|
|
|
|
/// Obtains a reference to the script inside the leaf.
|
2022-11-02 22:36:37 +00:00
|
|
|
pub fn script(&self) -> &Script { self.script }
|
2023-02-24 10:02:16 +00:00
|
|
|
|
|
|
|
/// Obtains a reference to the merkle proof of the leaf.
|
2022-11-02 22:36:37 +00:00
|
|
|
pub fn merkle_branch(&self) -> &TaprootMerkleBranch { self.merkle_branch }
|
2023-02-24 10:02:16 +00:00
|
|
|
|
|
|
|
/// Obtains a script leaf from the leaf node if the leaf is not hidden.
|
|
|
|
pub fn from_leaf_node(leaf_node: &'leaf LeafNode) -> Option<Self> {
|
|
|
|
let (script, ver) = leaf_node.leaf.as_script()?;
|
2022-11-02 22:36:37 +00:00
|
|
|
Some(Self { version: ver, script, merkle_branch: &leaf_node.merkle_branch })
|
2023-02-24 10:02:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// The merkle proof for inclusion of a tree in a taptree hash.
|
2021-10-06 23:00:21 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
|
2021-10-28 07:43:02 +00:00
|
|
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
2022-05-25 06:41:59 +00:00
|
|
|
#[cfg_attr(feature = "serde", serde(crate = "actual_serde"))]
|
2023-01-11 12:21:38 +00:00
|
|
|
#[cfg_attr(feature = "serde", serde(into = "Vec<TapNodeHash>"))]
|
|
|
|
#[cfg_attr(feature = "serde", serde(try_from = "Vec<TapNodeHash>"))]
|
2022-12-15 16:17:39 +00:00
|
|
|
pub struct TaprootMerkleBranch(Vec<TapNodeHash>);
|
2021-10-06 23:00:21 +00:00
|
|
|
|
|
|
|
impl TaprootMerkleBranch {
|
2023-12-07 22:25:10 +00:00
|
|
|
/// Returns a reference to the slice of hashes.
|
|
|
|
#[deprecated(since = "TBD", note = "Use `as_slice` instead")]
|
2022-12-15 16:17:39 +00:00
|
|
|
pub fn as_inner(&self) -> &[TapNodeHash] { &self.0 }
|
2021-10-06 23:00:21 +00:00
|
|
|
|
2023-12-07 22:25:10 +00:00
|
|
|
/// Returns a reference to the slice of hashes.
|
|
|
|
pub fn as_slice(&self) -> &[TapNodeHash] { &self.0 }
|
|
|
|
|
2023-01-11 12:25:08 +00:00
|
|
|
/// Returns the number of nodes in this merkle proof.
|
|
|
|
pub fn len(&self) -> usize { self.0.len() }
|
|
|
|
|
|
|
|
/// Checks if this merkle proof is empty.
|
|
|
|
pub fn is_empty(&self) -> bool { self.0.is_empty() }
|
|
|
|
|
2023-02-04 04:19:20 +00:00
|
|
|
/// Decodes bytes from control block.
|
|
|
|
///
|
|
|
|
/// This reads the branch as encoded in the control block: the concatenated 32B byte chunks -
|
|
|
|
/// one for each hash.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// The function returns an error if the the number of bytes is not an integer multiple of 32 or
|
|
|
|
/// if the number of hashes exceeds 128.
|
|
|
|
pub fn decode(sl: &[u8]) -> Result<Self, TaprootError> {
|
2021-10-06 23:00:21 +00:00
|
|
|
if sl.len() % TAPROOT_CONTROL_NODE_SIZE != 0 {
|
|
|
|
Err(TaprootError::InvalidMerkleBranchSize(sl.len()))
|
|
|
|
} else if sl.len() > TAPROOT_CONTROL_NODE_SIZE * TAPROOT_CONTROL_MAX_NODE_COUNT {
|
2022-01-24 00:31:39 +00:00
|
|
|
Err(TaprootError::InvalidMerkleTreeDepth(sl.len() / TAPROOT_CONTROL_NODE_SIZE))
|
2021-10-06 23:00:21 +00:00
|
|
|
} else {
|
|
|
|
let inner = sl
|
2022-04-21 01:29:33 +00:00
|
|
|
.chunks_exact(TAPROOT_CONTROL_NODE_SIZE)
|
2021-10-06 23:00:21 +00:00
|
|
|
.map(|chunk| {
|
2022-12-15 16:17:39 +00:00
|
|
|
TapNodeHash::from_slice(chunk)
|
2022-04-21 01:29:33 +00:00
|
|
|
.expect("chunks_exact always returns the correct size")
|
2021-10-06 23:00:21 +00:00
|
|
|
})
|
|
|
|
.collect();
|
2022-04-21 01:29:33 +00:00
|
|
|
|
2021-10-06 23:00:21 +00:00
|
|
|
Ok(TaprootMerkleBranch(inner))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-25 07:16:54 +00:00
|
|
|
/// Creates a merkle proof from list of hashes.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
/// If inner proof length is more than [`TAPROOT_CONTROL_MAX_NODE_COUNT`] (128).
|
2022-12-15 16:17:39 +00:00
|
|
|
fn from_collection<T: AsRef<[TapNodeHash]> + Into<Vec<TapNodeHash>>>(
|
2022-11-30 01:04:20 +00:00
|
|
|
collection: T,
|
|
|
|
) -> Result<Self, TaprootError> {
|
2022-05-25 07:16:54 +00:00
|
|
|
if collection.as_ref().len() > TAPROOT_CONTROL_MAX_NODE_COUNT {
|
|
|
|
Err(TaprootError::InvalidMerkleTreeDepth(collection.as_ref().len()))
|
|
|
|
} else {
|
|
|
|
Ok(TaprootMerkleBranch(collection.into()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Serializes to a writer.
|
|
|
|
///
|
|
|
|
/// # Returns
|
|
|
|
///
|
|
|
|
/// The number of bytes written to the writer.
|
2023-11-05 04:33:44 +00:00
|
|
|
pub fn encode<Write: io::Write + ?Sized>(&self, writer: &mut Write) -> io::Result<usize> {
|
2021-10-06 23:00:21 +00:00
|
|
|
for hash in self.0.iter() {
|
2022-12-15 19:52:07 +00:00
|
|
|
writer.write_all(hash.as_ref())?;
|
2021-10-06 23:00:21 +00:00
|
|
|
}
|
2022-12-15 16:17:39 +00:00
|
|
|
Ok(self.0.len() * TapNodeHash::LEN)
|
2021-10-06 23:00:21 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Serializes `self` as bytes.
|
2021-10-06 23:00:21 +00:00
|
|
|
pub fn serialize(&self) -> Vec<u8> {
|
2023-01-28 22:47:24 +00:00
|
|
|
self.0.iter().flat_map(|e| e.as_byte_array()).copied().collect::<Vec<u8>>()
|
2021-10-06 23:00:21 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Appends elements to proof.
|
2022-12-15 16:17:39 +00:00
|
|
|
fn push(&mut self, h: TapNodeHash) -> Result<(), TaprootBuilderError> {
|
2021-10-08 20:08:08 +00:00
|
|
|
if self.0.len() >= TAPROOT_CONTROL_MAX_NODE_COUNT {
|
|
|
|
Err(TaprootBuilderError::InvalidMerkleTreeDepth(self.0.len()))
|
|
|
|
} else {
|
|
|
|
self.0.push(h);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Returns the inner list of hashes.
|
2023-12-07 22:25:10 +00:00
|
|
|
#[deprecated(since = "TBD", note = "Use `into_vec` instead")]
|
2022-12-15 16:17:39 +00:00
|
|
|
pub fn into_inner(self) -> Vec<TapNodeHash> { self.0 }
|
2023-12-07 22:25:10 +00:00
|
|
|
|
|
|
|
/// Returns the list of hashes stored in a `Vec`.
|
|
|
|
pub fn into_vec(self) -> Vec<TapNodeHash> { self.0 }
|
2021-10-06 23:00:21 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 07:16:54 +00:00
|
|
|
macro_rules! impl_try_from {
|
|
|
|
($from:ty) => {
|
|
|
|
impl TryFrom<$from> for TaprootMerkleBranch {
|
|
|
|
type Error = TaprootError;
|
|
|
|
|
|
|
|
/// Creates a merkle proof from list of hashes.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
/// If inner proof length is more than [`TAPROOT_CONTROL_MAX_NODE_COUNT`] (128).
|
|
|
|
fn try_from(v: $from) -> Result<Self, Self::Error> {
|
|
|
|
TaprootMerkleBranch::from_collection(v)
|
|
|
|
}
|
|
|
|
}
|
2022-11-30 01:04:20 +00:00
|
|
|
};
|
2022-05-25 07:16:54 +00:00
|
|
|
}
|
2022-12-15 16:17:39 +00:00
|
|
|
impl_try_from!(&[TapNodeHash]);
|
|
|
|
impl_try_from!(Vec<TapNodeHash>);
|
|
|
|
impl_try_from!(Box<[TapNodeHash]>);
|
2022-05-25 07:16:54 +00:00
|
|
|
|
2023-02-01 14:31:24 +00:00
|
|
|
macro_rules! impl_try_from_array {
|
|
|
|
($($len:expr),* $(,)?) => {
|
|
|
|
$(
|
|
|
|
impl From<[TapNodeHash; $len]> for TaprootMerkleBranch {
|
|
|
|
fn from(a: [TapNodeHash; $len]) -> Self {
|
|
|
|
Self(a.to_vec())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Implement for all values [0, 128] inclusive.
|
|
|
|
//
|
|
|
|
// The reason zero is included is that `TaprootMerkleBranch` doesn't contain the hash of the node
|
|
|
|
// that's being proven - it's not needed because the script is already right before control block.
|
2022-11-02 22:36:37 +00:00
|
|
|
impl_try_from_array!(
|
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
|
|
|
|
26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
|
|
|
|
50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
|
|
|
|
74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97,
|
|
|
|
98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116,
|
|
|
|
117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128
|
|
|
|
);
|
2023-02-01 14:31:24 +00:00
|
|
|
|
2023-01-11 12:21:38 +00:00
|
|
|
impl From<TaprootMerkleBranch> for Vec<TapNodeHash> {
|
|
|
|
fn from(branch: TaprootMerkleBranch) -> Self { branch.0 }
|
|
|
|
}
|
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Control block data structure used in Tapscript satisfaction.
|
2021-10-06 23:00:21 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
2021-10-28 07:43:02 +00:00
|
|
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
2022-05-25 06:41:59 +00:00
|
|
|
#[cfg_attr(feature = "serde", serde(crate = "actual_serde"))]
|
2021-10-06 23:00:21 +00:00
|
|
|
pub struct ControlBlock {
|
2022-03-28 03:07:07 +00:00
|
|
|
/// The tapleaf version.
|
2021-10-06 23:00:21 +00:00
|
|
|
pub leaf_version: LeafVersion,
|
2022-03-28 03:07:07 +00:00
|
|
|
/// The parity of the output key (NOT THE INTERNAL KEY WHICH IS ALWAYS XONLY).
|
2022-01-03 02:52:44 +00:00
|
|
|
pub output_key_parity: secp256k1::Parity,
|
2022-03-28 03:07:07 +00:00
|
|
|
/// The internal key.
|
2021-11-29 02:19:21 +00:00
|
|
|
pub internal_key: UntweakedPublicKey,
|
2022-03-28 03:07:07 +00:00
|
|
|
/// The merkle proof of a script associated with this leaf.
|
2021-10-06 23:00:21 +00:00
|
|
|
pub merkle_branch: TaprootMerkleBranch,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ControlBlock {
|
2023-02-04 04:19:20 +00:00
|
|
|
/// Decodes bytes representing a `ControlBlock`.
|
|
|
|
///
|
|
|
|
/// This is an extra witness element that provides the proof that taproot script pubkey is
|
|
|
|
/// correctly computed with some specified leaf hash. This is the last element in taproot
|
|
|
|
/// witness when spending a output via script path.
|
2021-10-06 23:00:21 +00:00
|
|
|
///
|
2022-03-28 03:07:07 +00:00
|
|
|
/// # Errors
|
|
|
|
///
|
2022-03-30 23:57:05 +00:00
|
|
|
/// - [`TaprootError::InvalidControlBlockSize`] if `sl` is not of size 1 + 32 + 32N for any N >= 0.
|
|
|
|
/// - [`TaprootError::InvalidParity`] if first byte of `sl` is not a valid output key parity.
|
|
|
|
/// - [`TaprootError::InvalidTaprootLeafVersion`] if first byte of `sl` is not a valid leaf version.
|
|
|
|
/// - [`TaprootError::InvalidInternalKey`] if internal key is invalid (first 32 bytes after the parity byte).
|
|
|
|
/// - [`TaprootError::InvalidMerkleTreeDepth`] if merkle tree is too deep (more than 128 levels).
|
2023-02-04 04:19:20 +00:00
|
|
|
pub fn decode(sl: &[u8]) -> Result<ControlBlock, TaprootError> {
|
2021-10-06 23:00:21 +00:00
|
|
|
if sl.len() < TAPROOT_CONTROL_BASE_SIZE
|
|
|
|
|| (sl.len() - TAPROOT_CONTROL_BASE_SIZE) % TAPROOT_CONTROL_NODE_SIZE != 0
|
|
|
|
{
|
|
|
|
return Err(TaprootError::InvalidControlBlockSize(sl.len()));
|
|
|
|
}
|
2022-11-30 01:04:20 +00:00
|
|
|
let output_key_parity =
|
|
|
|
secp256k1::Parity::from_i32((sl[0] & 1) as i32).map_err(TaprootError::InvalidParity)?;
|
2021-11-24 23:01:21 +00:00
|
|
|
let leaf_version = LeafVersion::from_consensus(sl[0] & TAPROOT_LEAF_MASK)?;
|
2021-11-29 02:19:21 +00:00
|
|
|
let internal_key = UntweakedPublicKey::from_slice(&sl[1..TAPROOT_CONTROL_BASE_SIZE])
|
2021-10-06 23:00:21 +00:00
|
|
|
.map_err(TaprootError::InvalidInternalKey)?;
|
2023-02-04 04:19:20 +00:00
|
|
|
let merkle_branch = TaprootMerkleBranch::decode(&sl[TAPROOT_CONTROL_BASE_SIZE..])?;
|
2022-11-30 01:04:20 +00:00
|
|
|
Ok(ControlBlock { leaf_version, output_key_parity, internal_key, merkle_branch })
|
2021-10-06 23:00:21 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Returns the size of control block. Faster and more efficient than calling
|
|
|
|
/// `Self::serialize().len()`. Can be handy for fee estimation.
|
2021-10-06 23:00:21 +00:00
|
|
|
pub fn size(&self) -> usize {
|
2023-12-07 22:25:10 +00:00
|
|
|
TAPROOT_CONTROL_BASE_SIZE + TAPROOT_CONTROL_NODE_SIZE * self.merkle_branch.len()
|
2021-10-06 23:00:21 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Serializes to a writer.
|
|
|
|
///
|
|
|
|
/// # Returns
|
|
|
|
///
|
|
|
|
/// The number of bytes written to the writer.
|
2023-11-05 04:33:44 +00:00
|
|
|
pub fn encode<Write: io::Write + ?Sized>(&self, writer: &mut Write) -> io::Result<usize> {
|
2022-11-30 01:04:20 +00:00
|
|
|
let first_byte: u8 =
|
|
|
|
i32::from(self.output_key_parity) as u8 | self.leaf_version.to_consensus();
|
2022-01-25 13:50:06 +00:00
|
|
|
writer.write_all(&[first_byte])?;
|
|
|
|
writer.write_all(&self.internal_key.serialize())?;
|
2023-09-10 01:04:34 +00:00
|
|
|
self.merkle_branch.encode(writer)?;
|
2022-01-25 13:50:06 +00:00
|
|
|
Ok(self.size())
|
2021-10-06 23:00:21 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Serializes the control block.
|
|
|
|
///
|
|
|
|
/// This would be required when using [`ControlBlock`] as a witness element while spending an
|
2022-05-02 23:02:58 +00:00
|
|
|
/// output via script path. This serialization does not include the [`crate::VarInt`] prefix that would
|
2022-03-28 03:07:07 +00:00
|
|
|
/// be applied when encoding this element as a witness.
|
2021-10-06 23:00:21 +00:00
|
|
|
pub fn serialize(&self) -> Vec<u8> {
|
|
|
|
let mut buf = Vec::with_capacity(self.size());
|
2022-01-24 00:31:39 +00:00
|
|
|
self.encode(&mut buf).expect("writers don't error");
|
2021-10-06 23:00:21 +00:00
|
|
|
buf
|
|
|
|
}
|
2021-10-08 22:03:49 +00:00
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Verifies that a control block is correct proof for a given output key and script.
|
|
|
|
///
|
|
|
|
/// Only checks that script is contained inside the taptree described by output key. Full
|
|
|
|
/// verification must also execute the script with witness data.
|
2021-10-08 22:03:49 +00:00
|
|
|
pub fn verify_taproot_commitment<C: secp256k1::Verification>(
|
|
|
|
&self,
|
|
|
|
secp: &Secp256k1<C>,
|
2022-02-25 02:30:20 +00:00
|
|
|
output_key: XOnlyPublicKey,
|
2021-10-08 22:03:49 +00:00
|
|
|
script: &Script,
|
|
|
|
) -> bool {
|
|
|
|
// compute the script hash
|
|
|
|
// Initially the curr_hash is the leaf hash
|
2022-12-15 16:17:39 +00:00
|
|
|
let mut curr_hash = TapNodeHash::from_script(script, self.leaf_version);
|
2021-10-08 22:03:49 +00:00
|
|
|
// Verify the proof
|
2023-12-07 22:25:10 +00:00
|
|
|
for elem in self.merkle_branch.as_slice() {
|
2021-10-08 22:03:49 +00:00
|
|
|
// Recalculate the curr hash as parent hash
|
2022-12-15 16:17:39 +00:00
|
|
|
curr_hash = TapNodeHash::from_node_hashes(curr_hash, *elem);
|
2021-10-08 22:03:49 +00:00
|
|
|
}
|
|
|
|
// compute the taptweak
|
2022-11-30 01:04:20 +00:00
|
|
|
let tweak =
|
|
|
|
TapTweakHash::from_key_and_tweak(self.internal_key, Some(curr_hash)).to_scalar();
|
|
|
|
self.internal_key.tweak_add_check(secp, &output_key, self.output_key_parity, tweak)
|
2021-10-08 22:03:49 +00:00
|
|
|
}
|
2021-10-06 23:00:21 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 15:03:15 +00:00
|
|
|
/// Inner type representing future (non-tapscript) leaf versions. See [`LeafVersion::Future`].
|
|
|
|
///
|
|
|
|
/// NB: NO PUBLIC CONSTRUCTOR!
|
2022-01-10 08:09:44 +00:00
|
|
|
/// The only way to construct this is by converting `u8` to [`LeafVersion`] and then extracting it.
|
2022-01-07 15:03:15 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
|
|
|
|
pub struct FutureLeafVersion(u8);
|
|
|
|
|
|
|
|
impl FutureLeafVersion {
|
|
|
|
pub(self) fn from_consensus(version: u8) -> Result<FutureLeafVersion, TaprootError> {
|
|
|
|
match version {
|
2022-11-30 01:04:20 +00:00
|
|
|
TAPROOT_LEAF_TAPSCRIPT => unreachable!(
|
|
|
|
"FutureLeafVersion::from_consensus should be never called for 0xC0 value"
|
|
|
|
),
|
|
|
|
TAPROOT_ANNEX_PREFIX =>
|
|
|
|
Err(TaprootError::InvalidTaprootLeafVersion(TAPROOT_ANNEX_PREFIX)),
|
2022-01-07 15:03:15 +00:00
|
|
|
odd if odd & 0xFE != odd => Err(TaprootError::InvalidTaprootLeafVersion(odd)),
|
2022-11-30 01:04:20 +00:00
|
|
|
even => Ok(FutureLeafVersion(even)),
|
2022-01-07 15:03:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Returns the consensus representation of this [`FutureLeafVersion`].
|
2022-01-07 15:03:15 +00:00
|
|
|
#[inline]
|
2022-11-30 01:04:20 +00:00
|
|
|
pub fn to_consensus(self) -> u8 { self.0 }
|
2022-01-07 15:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for FutureLeafVersion {
|
|
|
|
#[inline]
|
2022-11-30 01:04:20 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Display::fmt(&self.0, f) }
|
2022-01-07 15:03:15 +00:00
|
|
|
}
|
|
|
|
|
2022-01-09 19:46:51 +00:00
|
|
|
impl fmt::LowerHex for FutureLeafVersion {
|
|
|
|
#[inline]
|
2022-11-30 01:04:20 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::LowerHex::fmt(&self.0, f) }
|
2022-01-09 19:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::UpperHex for FutureLeafVersion {
|
|
|
|
#[inline]
|
2022-11-30 01:04:20 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::UpperHex::fmt(&self.0, f) }
|
2022-01-09 19:46:51 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// The leaf version for tapleafs.
|
2021-10-06 23:00:21 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
2021-11-24 23:01:21 +00:00
|
|
|
pub enum LeafVersion {
|
2022-03-28 03:07:07 +00:00
|
|
|
/// BIP-342 tapscript.
|
2021-11-24 23:01:21 +00:00
|
|
|
TapScript,
|
2021-10-06 23:00:21 +00:00
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Future leaf version.
|
2022-11-30 01:04:20 +00:00
|
|
|
Future(FutureLeafVersion),
|
2021-10-06 23:00:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LeafVersion {
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Creates a [`LeafVersion`] from consensus byte representation.
|
2022-01-07 15:03:04 +00:00
|
|
|
///
|
|
|
|
/// # Errors
|
2022-03-28 03:07:07 +00:00
|
|
|
///
|
2022-01-07 15:03:04 +00:00
|
|
|
/// - If the last bit of the `version` is odd.
|
|
|
|
/// - If the `version` is 0x50 ([`TAPROOT_ANNEX_PREFIX`]).
|
2021-11-24 23:01:21 +00:00
|
|
|
pub fn from_consensus(version: u8) -> Result<Self, TaprootError> {
|
|
|
|
match version {
|
|
|
|
TAPROOT_LEAF_TAPSCRIPT => Ok(LeafVersion::TapScript),
|
2022-11-30 01:04:20 +00:00
|
|
|
TAPROOT_ANNEX_PREFIX =>
|
|
|
|
Err(TaprootError::InvalidTaprootLeafVersion(TAPROOT_ANNEX_PREFIX)),
|
2022-01-07 15:03:15 +00:00
|
|
|
future => FutureLeafVersion::from_consensus(future).map(LeafVersion::Future),
|
2021-10-06 23:00:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Returns the consensus representation of this [`LeafVersion`].
|
2022-01-19 02:46:51 +00:00
|
|
|
pub fn to_consensus(self) -> u8 {
|
2021-11-24 23:01:21 +00:00
|
|
|
match self {
|
|
|
|
LeafVersion::TapScript => TAPROOT_LEAF_TAPSCRIPT,
|
2022-01-19 02:46:51 +00:00
|
|
|
LeafVersion::Future(version) => version.to_consensus(),
|
2021-11-24 23:01:21 +00:00
|
|
|
}
|
2021-10-06 23:00:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-24 23:01:21 +00:00
|
|
|
impl fmt::Display for LeafVersion {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match (self, f.alternate()) {
|
2022-01-09 19:50:22 +00:00
|
|
|
(LeafVersion::TapScript, true) => f.write_str("tapscript"),
|
|
|
|
(LeafVersion::TapScript, false) => fmt::Display::fmt(&TAPROOT_LEAF_TAPSCRIPT, f),
|
|
|
|
(LeafVersion::Future(version), true) => write!(f, "future_script_{:#02x}", version.0),
|
|
|
|
(LeafVersion::Future(version), false) => fmt::Display::fmt(version, f),
|
2021-11-24 23:01:21 +00:00
|
|
|
}
|
2021-10-06 23:00:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-09 19:52:38 +00:00
|
|
|
impl fmt::LowerHex for LeafVersion {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2022-01-19 02:46:51 +00:00
|
|
|
fmt::LowerHex::fmt(&self.to_consensus(), f)
|
2022-01-09 19:52:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::UpperHex for LeafVersion {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2022-01-19 02:46:51 +00:00
|
|
|
fmt::UpperHex::fmt(&self.to_consensus(), f)
|
2022-01-09 19:52:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Serializes [`LeafVersion`] as a `u8` using consensus encoding.
|
2022-01-07 14:31:21 +00:00
|
|
|
#[cfg(feature = "serde")]
|
2022-06-01 22:08:56 +00:00
|
|
|
impl serde::Serialize for LeafVersion {
|
2022-01-07 14:31:21 +00:00
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
2022-01-24 00:33:03 +00:00
|
|
|
where
|
2022-06-01 22:08:56 +00:00
|
|
|
S: serde::Serializer,
|
2022-01-07 14:31:21 +00:00
|
|
|
{
|
2022-01-19 02:46:51 +00:00
|
|
|
serializer.serialize_u8(self.to_consensus())
|
2022-01-07 14:31:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Deserializes [`LeafVersion`] as a `u8` using consensus encoding.
|
2022-01-07 14:31:21 +00:00
|
|
|
#[cfg(feature = "serde")]
|
2022-06-01 22:08:56 +00:00
|
|
|
impl<'de> serde::Deserialize<'de> for LeafVersion {
|
2022-01-24 00:33:03 +00:00
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
|
where
|
2022-11-30 01:04:20 +00:00
|
|
|
D: serde::Deserializer<'de>,
|
2022-01-24 00:33:03 +00:00
|
|
|
{
|
2022-01-07 14:31:21 +00:00
|
|
|
struct U8Visitor;
|
2022-06-01 22:08:56 +00:00
|
|
|
impl<'de> serde::de::Visitor<'de> for U8Visitor {
|
2022-01-07 14:31:21 +00:00
|
|
|
type Value = LeafVersion;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
formatter.write_str("a valid consensus-encoded taproot leaf version")
|
|
|
|
}
|
|
|
|
|
2023-01-01 19:42:29 +00:00
|
|
|
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
|
2022-11-30 01:04:20 +00:00
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
2022-01-07 14:31:21 +00:00
|
|
|
{
|
2023-01-01 19:42:29 +00:00
|
|
|
let value = u8::try_from(value).map_err(|_| {
|
|
|
|
E::invalid_value(
|
|
|
|
serde::de::Unexpected::Unsigned(value),
|
|
|
|
&"consensus-encoded leaf version as u8",
|
|
|
|
)
|
|
|
|
})?;
|
2022-01-07 14:31:21 +00:00
|
|
|
LeafVersion::from_consensus(value).map_err(|_| {
|
2022-11-30 01:04:20 +00:00
|
|
|
E::invalid_value(
|
|
|
|
::serde::de::Unexpected::Unsigned(value as u64),
|
|
|
|
&"consensus-encoded leaf version as u8",
|
|
|
|
)
|
2022-01-07 14:31:21 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
deserializer.deserialize_u8(U8Visitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Detailed error type for taproot builder.
|
2023-07-27 01:10:22 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2022-05-31 04:29:50 +00:00
|
|
|
#[non_exhaustive]
|
2021-10-08 20:08:08 +00:00
|
|
|
pub enum TaprootBuilderError {
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Merkle tree depth must not be more than 128.
|
2021-10-08 20:08:08 +00:00
|
|
|
InvalidMerkleTreeDepth(usize),
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Nodes must be added specified in DFS walk order.
|
2021-10-08 20:08:08 +00:00
|
|
|
NodeNotInDfsOrder,
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Two nodes at depth 0 are not allowed.
|
2021-10-08 20:08:08 +00:00
|
|
|
OverCompleteTree,
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Invalid taproot internal key.
|
2021-10-08 20:08:08 +00:00
|
|
|
InvalidInternalKey(secp256k1::Error),
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Called finalize on a empty tree.
|
2021-10-08 20:08:08 +00:00
|
|
|
EmptyTree,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for TaprootBuilderError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
Make error types uniform
On our way to v1.0.0 we are defining a standard for our error types,
this includes:
- Uses the following derives (unless not possible, usually because of `io::Error`)
`#[derive(Debug, Clone, PartialEq, Eq)]`
- Has `non_exhaustive` unless we really know we can commit to not adding
anything.
Furthermore, we are trying to make the codebase easy to read. Error code
is write-once-read-many (well it should be) so if we make all the error
code super uniform the users can flick to an error and quickly see what
it includes. In an effort to achieve this I have made up a style and
over recent times have change much of the error code to that new style,
this PR audits _all_ error types in the code base and enforces the
style, specifically:
- Is layed out: definition, [impl block], Display impl, error::Error impl, From impls
- `error::Error` impl matches on enum even if it returns `None` for all variants
- Display/Error impls import enum variants locally
- match uses *self and `ref e`
- error::Error variants that return `Some` come first, `None` after
Re: non_exhaustive
To make dev and review easier I have added `non_exhaustive` to _every_
error type. We can then remove it error by error as we see fit. This is
because it takes a bit of thinking to do and review where as this patch
should not take much brain power to review.
2023-10-04 02:55:45 +00:00
|
|
|
use TaprootBuilderError::*;
|
|
|
|
|
2021-10-08 20:08:08 +00:00
|
|
|
match *self {
|
Make error types uniform
On our way to v1.0.0 we are defining a standard for our error types,
this includes:
- Uses the following derives (unless not possible, usually because of `io::Error`)
`#[derive(Debug, Clone, PartialEq, Eq)]`
- Has `non_exhaustive` unless we really know we can commit to not adding
anything.
Furthermore, we are trying to make the codebase easy to read. Error code
is write-once-read-many (well it should be) so if we make all the error
code super uniform the users can flick to an error and quickly see what
it includes. In an effort to achieve this I have made up a style and
over recent times have change much of the error code to that new style,
this PR audits _all_ error types in the code base and enforces the
style, specifically:
- Is layed out: definition, [impl block], Display impl, error::Error impl, From impls
- `error::Error` impl matches on enum even if it returns `None` for all variants
- Display/Error impls import enum variants locally
- match uses *self and `ref e`
- error::Error variants that return `Some` come first, `None` after
Re: non_exhaustive
To make dev and review easier I have added `non_exhaustive` to _every_
error type. We can then remove it error by error as we see fit. This is
because it takes a bit of thinking to do and review where as this patch
should not take much brain power to review.
2023-10-04 02:55:45 +00:00
|
|
|
InvalidMerkleTreeDepth(d) => {
|
2022-11-30 01:04:20 +00:00
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"Merkle Tree depth({}) must be less than {}",
|
|
|
|
d, TAPROOT_CONTROL_MAX_NODE_COUNT
|
|
|
|
)
|
2022-05-04 05:02:18 +00:00
|
|
|
}
|
Make error types uniform
On our way to v1.0.0 we are defining a standard for our error types,
this includes:
- Uses the following derives (unless not possible, usually because of `io::Error`)
`#[derive(Debug, Clone, PartialEq, Eq)]`
- Has `non_exhaustive` unless we really know we can commit to not adding
anything.
Furthermore, we are trying to make the codebase easy to read. Error code
is write-once-read-many (well it should be) so if we make all the error
code super uniform the users can flick to an error and quickly see what
it includes. In an effort to achieve this I have made up a style and
over recent times have change much of the error code to that new style,
this PR audits _all_ error types in the code base and enforces the
style, specifically:
- Is layed out: definition, [impl block], Display impl, error::Error impl, From impls
- `error::Error` impl matches on enum even if it returns `None` for all variants
- Display/Error impls import enum variants locally
- match uses *self and `ref e`
- error::Error variants that return `Some` come first, `None` after
Re: non_exhaustive
To make dev and review easier I have added `non_exhaustive` to _every_
error type. We can then remove it error by error as we see fit. This is
because it takes a bit of thinking to do and review where as this patch
should not take much brain power to review.
2023-10-04 02:55:45 +00:00
|
|
|
NodeNotInDfsOrder => {
|
2021-10-08 20:08:08 +00:00
|
|
|
write!(f, "add_leaf/add_hidden must be called in DFS walk order",)
|
|
|
|
}
|
Make error types uniform
On our way to v1.0.0 we are defining a standard for our error types,
this includes:
- Uses the following derives (unless not possible, usually because of `io::Error`)
`#[derive(Debug, Clone, PartialEq, Eq)]`
- Has `non_exhaustive` unless we really know we can commit to not adding
anything.
Furthermore, we are trying to make the codebase easy to read. Error code
is write-once-read-many (well it should be) so if we make all the error
code super uniform the users can flick to an error and quickly see what
it includes. In an effort to achieve this I have made up a style and
over recent times have change much of the error code to that new style,
this PR audits _all_ error types in the code base and enforces the
style, specifically:
- Is layed out: definition, [impl block], Display impl, error::Error impl, From impls
- `error::Error` impl matches on enum even if it returns `None` for all variants
- Display/Error impls import enum variants locally
- match uses *self and `ref e`
- error::Error variants that return `Some` come first, `None` after
Re: non_exhaustive
To make dev and review easier I have added `non_exhaustive` to _every_
error type. We can then remove it error by error as we see fit. This is
because it takes a bit of thinking to do and review where as this patch
should not take much brain power to review.
2023-10-04 02:55:45 +00:00
|
|
|
OverCompleteTree => write!(
|
2021-10-08 20:08:08 +00:00
|
|
|
f,
|
|
|
|
"Attempted to create a tree with two nodes at depth 0. There must\
|
|
|
|
only be a exactly one node at depth 0",
|
|
|
|
),
|
Make error types uniform
On our way to v1.0.0 we are defining a standard for our error types,
this includes:
- Uses the following derives (unless not possible, usually because of `io::Error`)
`#[derive(Debug, Clone, PartialEq, Eq)]`
- Has `non_exhaustive` unless we really know we can commit to not adding
anything.
Furthermore, we are trying to make the codebase easy to read. Error code
is write-once-read-many (well it should be) so if we make all the error
code super uniform the users can flick to an error and quickly see what
it includes. In an effort to achieve this I have made up a style and
over recent times have change much of the error code to that new style,
this PR audits _all_ error types in the code base and enforces the
style, specifically:
- Is layed out: definition, [impl block], Display impl, error::Error impl, From impls
- `error::Error` impl matches on enum even if it returns `None` for all variants
- Display/Error impls import enum variants locally
- match uses *self and `ref e`
- error::Error variants that return `Some` come first, `None` after
Re: non_exhaustive
To make dev and review easier I have added `non_exhaustive` to _every_
error type. We can then remove it error by error as we see fit. This is
because it takes a bit of thinking to do and review where as this patch
should not take much brain power to review.
2023-10-04 02:55:45 +00:00
|
|
|
InvalidInternalKey(ref e) => {
|
2022-05-25 02:56:51 +00:00
|
|
|
write_err!(f, "invalid internal x-only key"; e)
|
2021-10-08 20:08:08 +00:00
|
|
|
}
|
Make error types uniform
On our way to v1.0.0 we are defining a standard for our error types,
this includes:
- Uses the following derives (unless not possible, usually because of `io::Error`)
`#[derive(Debug, Clone, PartialEq, Eq)]`
- Has `non_exhaustive` unless we really know we can commit to not adding
anything.
Furthermore, we are trying to make the codebase easy to read. Error code
is write-once-read-many (well it should be) so if we make all the error
code super uniform the users can flick to an error and quickly see what
it includes. In an effort to achieve this I have made up a style and
over recent times have change much of the error code to that new style,
this PR audits _all_ error types in the code base and enforces the
style, specifically:
- Is layed out: definition, [impl block], Display impl, error::Error impl, From impls
- `error::Error` impl matches on enum even if it returns `None` for all variants
- Display/Error impls import enum variants locally
- match uses *self and `ref e`
- error::Error variants that return `Some` come first, `None` after
Re: non_exhaustive
To make dev and review easier I have added `non_exhaustive` to _every_
error type. We can then remove it error by error as we see fit. This is
because it takes a bit of thinking to do and review where as this patch
should not take much brain power to review.
2023-10-04 02:55:45 +00:00
|
|
|
EmptyTree => {
|
2021-10-08 20:08:08 +00:00
|
|
|
write!(f, "Called finalize on an empty tree")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "std")]
|
2022-05-04 05:56:24 +00:00
|
|
|
impl std::error::Error for TaprootBuilderError {
|
|
|
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
Make error types uniform
On our way to v1.0.0 we are defining a standard for our error types,
this includes:
- Uses the following derives (unless not possible, usually because of `io::Error`)
`#[derive(Debug, Clone, PartialEq, Eq)]`
- Has `non_exhaustive` unless we really know we can commit to not adding
anything.
Furthermore, we are trying to make the codebase easy to read. Error code
is write-once-read-many (well it should be) so if we make all the error
code super uniform the users can flick to an error and quickly see what
it includes. In an effort to achieve this I have made up a style and
over recent times have change much of the error code to that new style,
this PR audits _all_ error types in the code base and enforces the
style, specifically:
- Is layed out: definition, [impl block], Display impl, error::Error impl, From impls
- `error::Error` impl matches on enum even if it returns `None` for all variants
- Display/Error impls import enum variants locally
- match uses *self and `ref e`
- error::Error variants that return `Some` come first, `None` after
Re: non_exhaustive
To make dev and review easier I have added `non_exhaustive` to _every_
error type. We can then remove it error by error as we see fit. This is
because it takes a bit of thinking to do and review where as this patch
should not take much brain power to review.
2023-10-04 02:55:45 +00:00
|
|
|
use TaprootBuilderError::*;
|
2022-05-04 05:56:24 +00:00
|
|
|
|
|
|
|
match self {
|
|
|
|
InvalidInternalKey(e) => Some(e),
|
2022-11-30 01:04:20 +00:00
|
|
|
InvalidMerkleTreeDepth(_) | NodeNotInDfsOrder | OverCompleteTree | EmptyTree => None,
|
2022-05-04 05:56:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-08 20:08:08 +00:00
|
|
|
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Detailed error type for taproot utilities.
|
2023-07-27 01:10:22 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2022-05-31 04:29:50 +00:00
|
|
|
#[non_exhaustive]
|
2021-10-06 23:00:21 +00:00
|
|
|
pub enum TaprootError {
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Proof size must be a multiple of 32.
|
2021-10-06 23:00:21 +00:00
|
|
|
InvalidMerkleBranchSize(usize),
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Merkle tree depth must not be more than 128.
|
2021-10-06 23:00:21 +00:00
|
|
|
InvalidMerkleTreeDepth(usize),
|
2022-03-28 03:07:07 +00:00
|
|
|
/// The last bit of tapleaf version must be zero.
|
2021-10-06 23:00:21 +00:00
|
|
|
InvalidTaprootLeafVersion(u8),
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Invalid control block size.
|
2021-10-06 23:00:21 +00:00
|
|
|
InvalidControlBlockSize(usize),
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Invalid taproot internal key.
|
2021-10-06 23:00:21 +00:00
|
|
|
InvalidInternalKey(secp256k1::Error),
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Invalid parity for internal key.
|
2022-03-10 22:41:32 +00:00
|
|
|
InvalidParity(secp256k1::InvalidParityValue),
|
2022-03-28 03:07:07 +00:00
|
|
|
/// Empty tap tree.
|
2021-10-08 20:08:08 +00:00
|
|
|
EmptyTree,
|
2021-10-06 23:00:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for TaprootError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
Make error types uniform
On our way to v1.0.0 we are defining a standard for our error types,
this includes:
- Uses the following derives (unless not possible, usually because of `io::Error`)
`#[derive(Debug, Clone, PartialEq, Eq)]`
- Has `non_exhaustive` unless we really know we can commit to not adding
anything.
Furthermore, we are trying to make the codebase easy to read. Error code
is write-once-read-many (well it should be) so if we make all the error
code super uniform the users can flick to an error and quickly see what
it includes. In an effort to achieve this I have made up a style and
over recent times have change much of the error code to that new style,
this PR audits _all_ error types in the code base and enforces the
style, specifically:
- Is layed out: definition, [impl block], Display impl, error::Error impl, From impls
- `error::Error` impl matches on enum even if it returns `None` for all variants
- Display/Error impls import enum variants locally
- match uses *self and `ref e`
- error::Error variants that return `Some` come first, `None` after
Re: non_exhaustive
To make dev and review easier I have added `non_exhaustive` to _every_
error type. We can then remove it error by error as we see fit. This is
because it takes a bit of thinking to do and review where as this patch
should not take much brain power to review.
2023-10-04 02:55:45 +00:00
|
|
|
use TaprootError::*;
|
|
|
|
|
2021-10-06 23:00:21 +00:00
|
|
|
match *self {
|
Make error types uniform
On our way to v1.0.0 we are defining a standard for our error types,
this includes:
- Uses the following derives (unless not possible, usually because of `io::Error`)
`#[derive(Debug, Clone, PartialEq, Eq)]`
- Has `non_exhaustive` unless we really know we can commit to not adding
anything.
Furthermore, we are trying to make the codebase easy to read. Error code
is write-once-read-many (well it should be) so if we make all the error
code super uniform the users can flick to an error and quickly see what
it includes. In an effort to achieve this I have made up a style and
over recent times have change much of the error code to that new style,
this PR audits _all_ error types in the code base and enforces the
style, specifically:
- Is layed out: definition, [impl block], Display impl, error::Error impl, From impls
- `error::Error` impl matches on enum even if it returns `None` for all variants
- Display/Error impls import enum variants locally
- match uses *self and `ref e`
- error::Error variants that return `Some` come first, `None` after
Re: non_exhaustive
To make dev and review easier I have added `non_exhaustive` to _every_
error type. We can then remove it error by error as we see fit. This is
because it takes a bit of thinking to do and review where as this patch
should not take much brain power to review.
2023-10-04 02:55:45 +00:00
|
|
|
InvalidMerkleBranchSize(sz) => write!(
|
2021-10-06 23:00:21 +00:00
|
|
|
f,
|
|
|
|
"Merkle branch size({}) must be a multiple of {}",
|
|
|
|
sz, TAPROOT_CONTROL_NODE_SIZE
|
|
|
|
),
|
Make error types uniform
On our way to v1.0.0 we are defining a standard for our error types,
this includes:
- Uses the following derives (unless not possible, usually because of `io::Error`)
`#[derive(Debug, Clone, PartialEq, Eq)]`
- Has `non_exhaustive` unless we really know we can commit to not adding
anything.
Furthermore, we are trying to make the codebase easy to read. Error code
is write-once-read-many (well it should be) so if we make all the error
code super uniform the users can flick to an error and quickly see what
it includes. In an effort to achieve this I have made up a style and
over recent times have change much of the error code to that new style,
this PR audits _all_ error types in the code base and enforces the
style, specifically:
- Is layed out: definition, [impl block], Display impl, error::Error impl, From impls
- `error::Error` impl matches on enum even if it returns `None` for all variants
- Display/Error impls import enum variants locally
- match uses *self and `ref e`
- error::Error variants that return `Some` come first, `None` after
Re: non_exhaustive
To make dev and review easier I have added `non_exhaustive` to _every_
error type. We can then remove it error by error as we see fit. This is
because it takes a bit of thinking to do and review where as this patch
should not take much brain power to review.
2023-10-04 02:55:45 +00:00
|
|
|
InvalidMerkleTreeDepth(d) => write!(
|
2021-10-06 23:00:21 +00:00
|
|
|
f,
|
|
|
|
"Merkle Tree depth({}) must be less than {}",
|
|
|
|
d, TAPROOT_CONTROL_MAX_NODE_COUNT
|
|
|
|
),
|
Make error types uniform
On our way to v1.0.0 we are defining a standard for our error types,
this includes:
- Uses the following derives (unless not possible, usually because of `io::Error`)
`#[derive(Debug, Clone, PartialEq, Eq)]`
- Has `non_exhaustive` unless we really know we can commit to not adding
anything.
Furthermore, we are trying to make the codebase easy to read. Error code
is write-once-read-many (well it should be) so if we make all the error
code super uniform the users can flick to an error and quickly see what
it includes. In an effort to achieve this I have made up a style and
over recent times have change much of the error code to that new style,
this PR audits _all_ error types in the code base and enforces the
style, specifically:
- Is layed out: definition, [impl block], Display impl, error::Error impl, From impls
- `error::Error` impl matches on enum even if it returns `None` for all variants
- Display/Error impls import enum variants locally
- match uses *self and `ref e`
- error::Error variants that return `Some` come first, `None` after
Re: non_exhaustive
To make dev and review easier I have added `non_exhaustive` to _every_
error type. We can then remove it error by error as we see fit. This is
because it takes a bit of thinking to do and review where as this patch
should not take much brain power to review.
2023-10-04 02:55:45 +00:00
|
|
|
InvalidTaprootLeafVersion(v) => {
|
2022-12-15 16:17:39 +00:00
|
|
|
write!(f, "Leaf version({}) must have the least significant bit 0", v)
|
|
|
|
}
|
Make error types uniform
On our way to v1.0.0 we are defining a standard for our error types,
this includes:
- Uses the following derives (unless not possible, usually because of `io::Error`)
`#[derive(Debug, Clone, PartialEq, Eq)]`
- Has `non_exhaustive` unless we really know we can commit to not adding
anything.
Furthermore, we are trying to make the codebase easy to read. Error code
is write-once-read-many (well it should be) so if we make all the error
code super uniform the users can flick to an error and quickly see what
it includes. In an effort to achieve this I have made up a style and
over recent times have change much of the error code to that new style,
this PR audits _all_ error types in the code base and enforces the
style, specifically:
- Is layed out: definition, [impl block], Display impl, error::Error impl, From impls
- `error::Error` impl matches on enum even if it returns `None` for all variants
- Display/Error impls import enum variants locally
- match uses *self and `ref e`
- error::Error variants that return `Some` come first, `None` after
Re: non_exhaustive
To make dev and review easier I have added `non_exhaustive` to _every_
error type. We can then remove it error by error as we see fit. This is
because it takes a bit of thinking to do and review where as this patch
should not take much brain power to review.
2023-10-04 02:55:45 +00:00
|
|
|
InvalidControlBlockSize(sz) => write!(
|
2021-10-06 23:00:21 +00:00
|
|
|
f,
|
|
|
|
"Control Block size({}) must be of the form 33 + 32*m where 0 <= m <= {} ",
|
|
|
|
sz, TAPROOT_CONTROL_MAX_NODE_COUNT
|
|
|
|
),
|
Make error types uniform
On our way to v1.0.0 we are defining a standard for our error types,
this includes:
- Uses the following derives (unless not possible, usually because of `io::Error`)
`#[derive(Debug, Clone, PartialEq, Eq)]`
- Has `non_exhaustive` unless we really know we can commit to not adding
anything.
Furthermore, we are trying to make the codebase easy to read. Error code
is write-once-read-many (well it should be) so if we make all the error
code super uniform the users can flick to an error and quickly see what
it includes. In an effort to achieve this I have made up a style and
over recent times have change much of the error code to that new style,
this PR audits _all_ error types in the code base and enforces the
style, specifically:
- Is layed out: definition, [impl block], Display impl, error::Error impl, From impls
- `error::Error` impl matches on enum even if it returns `None` for all variants
- Display/Error impls import enum variants locally
- match uses *self and `ref e`
- error::Error variants that return `Some` come first, `None` after
Re: non_exhaustive
To make dev and review easier I have added `non_exhaustive` to _every_
error type. We can then remove it error by error as we see fit. This is
because it takes a bit of thinking to do and review where as this patch
should not take much brain power to review.
2023-10-04 02:55:45 +00:00
|
|
|
InvalidInternalKey(ref e) => {
|
2022-12-15 16:17:39 +00:00
|
|
|
write_err!(f, "invalid internal x-only key"; e)
|
|
|
|
}
|
Make error types uniform
On our way to v1.0.0 we are defining a standard for our error types,
this includes:
- Uses the following derives (unless not possible, usually because of `io::Error`)
`#[derive(Debug, Clone, PartialEq, Eq)]`
- Has `non_exhaustive` unless we really know we can commit to not adding
anything.
Furthermore, we are trying to make the codebase easy to read. Error code
is write-once-read-many (well it should be) so if we make all the error
code super uniform the users can flick to an error and quickly see what
it includes. In an effort to achieve this I have made up a style and
over recent times have change much of the error code to that new style,
this PR audits _all_ error types in the code base and enforces the
style, specifically:
- Is layed out: definition, [impl block], Display impl, error::Error impl, From impls
- `error::Error` impl matches on enum even if it returns `None` for all variants
- Display/Error impls import enum variants locally
- match uses *self and `ref e`
- error::Error variants that return `Some` come first, `None` after
Re: non_exhaustive
To make dev and review easier I have added `non_exhaustive` to _every_
error type. We can then remove it error by error as we see fit. This is
because it takes a bit of thinking to do and review where as this patch
should not take much brain power to review.
2023-10-04 02:55:45 +00:00
|
|
|
InvalidParity(_) => write!(f, "invalid parity value for internal key"),
|
|
|
|
EmptyTree => write!(f, "Taproot Tree must contain at least one script"),
|
2021-10-06 23:00:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "std")]
|
2022-05-04 05:56:24 +00:00
|
|
|
impl std::error::Error for TaprootError {
|
|
|
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
Make error types uniform
On our way to v1.0.0 we are defining a standard for our error types,
this includes:
- Uses the following derives (unless not possible, usually because of `io::Error`)
`#[derive(Debug, Clone, PartialEq, Eq)]`
- Has `non_exhaustive` unless we really know we can commit to not adding
anything.
Furthermore, we are trying to make the codebase easy to read. Error code
is write-once-read-many (well it should be) so if we make all the error
code super uniform the users can flick to an error and quickly see what
it includes. In an effort to achieve this I have made up a style and
over recent times have change much of the error code to that new style,
this PR audits _all_ error types in the code base and enforces the
style, specifically:
- Is layed out: definition, [impl block], Display impl, error::Error impl, From impls
- `error::Error` impl matches on enum even if it returns `None` for all variants
- Display/Error impls import enum variants locally
- match uses *self and `ref e`
- error::Error variants that return `Some` come first, `None` after
Re: non_exhaustive
To make dev and review easier I have added `non_exhaustive` to _every_
error type. We can then remove it error by error as we see fit. This is
because it takes a bit of thinking to do and review where as this patch
should not take much brain power to review.
2023-10-04 02:55:45 +00:00
|
|
|
use TaprootError::*;
|
2022-05-04 05:56:24 +00:00
|
|
|
|
|
|
|
match self {
|
|
|
|
InvalidInternalKey(e) => Some(e),
|
|
|
|
InvalidMerkleBranchSize(_)
|
|
|
|
| InvalidMerkleTreeDepth(_)
|
|
|
|
| InvalidTaprootLeafVersion(_)
|
|
|
|
| InvalidControlBlockSize(_)
|
|
|
|
| InvalidParity(_)
|
|
|
|
| EmptyTree => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-14 17:07:53 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2022-11-30 01:04:20 +00:00
|
|
|
use core::str::FromStr;
|
|
|
|
|
2023-03-22 03:09:58 +00:00
|
|
|
use hashes::sha256t::Tag;
|
|
|
|
use hashes::{sha256, Hash, HashEngine};
|
2023-07-21 00:38:34 +00:00
|
|
|
use hex::FromHex;
|
2022-11-30 01:04:20 +00:00
|
|
|
use secp256k1::{VerifyOnly, XOnlyPublicKey};
|
2021-11-12 21:50:37 +00:00
|
|
|
|
2019-05-14 17:07:53 +00:00
|
|
|
use super::*;
|
2023-02-27 22:47:29 +00:00
|
|
|
use crate::sighash::{TapSighash, TapSighashTag};
|
2022-11-30 01:04:20 +00:00
|
|
|
use crate::{Address, Network};
|
2021-11-12 21:50:37 +00:00
|
|
|
extern crate serde_json;
|
2019-05-14 17:07:53 +00:00
|
|
|
|
2023-01-01 19:44:40 +00:00
|
|
|
#[cfg(feature = "serde")]
|
2023-01-11 12:21:38 +00:00
|
|
|
use {
|
2023-08-10 22:44:58 +00:00
|
|
|
hex::test_hex_unwrap as hex,
|
2023-01-11 12:21:38 +00:00
|
|
|
serde_test::Configure,
|
|
|
|
serde_test::{assert_tokens, Token},
|
|
|
|
};
|
2023-01-01 19:44:40 +00:00
|
|
|
|
2019-05-14 17:07:53 +00:00
|
|
|
fn tag_engine(tag_name: &str) -> sha256::HashEngine {
|
|
|
|
let mut engine = sha256::Hash::engine();
|
|
|
|
let tag_hash = sha256::Hash::hash(tag_name.as_bytes());
|
2022-12-15 19:52:07 +00:00
|
|
|
engine.input(tag_hash.as_ref());
|
|
|
|
engine.input(tag_hash.as_ref());
|
2019-05-14 17:07:53 +00:00
|
|
|
engine
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_midstates() {
|
|
|
|
// test that engine creation roundtrips
|
|
|
|
assert_eq!(tag_engine("TapLeaf").midstate(), TapLeafTag::engine().midstate());
|
|
|
|
assert_eq!(tag_engine("TapBranch").midstate(), TapBranchTag::engine().midstate());
|
|
|
|
assert_eq!(tag_engine("TapTweak").midstate(), TapTweakTag::engine().midstate());
|
|
|
|
assert_eq!(tag_engine("TapSighash").midstate(), TapSighashTag::engine().midstate());
|
|
|
|
|
|
|
|
// check that hash creation is the same as building into the same engine
|
|
|
|
fn empty_hash(tag_name: &str) -> [u8; 32] {
|
|
|
|
let mut e = tag_engine(tag_name);
|
|
|
|
e.input(&[]);
|
2023-01-28 22:47:24 +00:00
|
|
|
TapNodeHash::from_engine(e).to_byte_array()
|
2019-05-14 17:07:53 +00:00
|
|
|
}
|
2023-01-28 22:47:24 +00:00
|
|
|
assert_eq!(empty_hash("TapLeaf"), TapLeafHash::hash(&[]).to_byte_array());
|
|
|
|
assert_eq!(empty_hash("TapBranch"), TapNodeHash::hash(&[]).to_byte_array());
|
|
|
|
assert_eq!(empty_hash("TapTweak"), TapTweakHash::hash(&[]).to_byte_array());
|
|
|
|
assert_eq!(empty_hash("TapSighash"), TapSighash::hash(&[]).to_byte_array());
|
2019-05-14 17:07:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_vectors_core() {
|
|
|
|
//! Test vectors taken from Core
|
|
|
|
|
|
|
|
// uninitialized writers
|
|
|
|
// CHashWriter writer = HasherTapLeaf;
|
|
|
|
// writer.GetSHA256().GetHex()
|
|
|
|
assert_eq!(
|
2023-01-07 15:39:11 +00:00
|
|
|
TapLeafHash::from_engine(TapLeafTag::engine()).to_string(),
|
2021-11-12 21:16:40 +00:00
|
|
|
"5212c288a377d1f8164962a5a13429f9ba6a7b84e59776a52c6637df2106facb"
|
2019-05-14 17:07:53 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2022-12-15 15:54:32 +00:00
|
|
|
TapNodeHash::from_engine(TapBranchTag::engine()).to_string(),
|
2021-11-12 21:16:40 +00:00
|
|
|
"53c373ec4d6f3c53c1f5fb2ff506dcefe1a0ed74874f93fa93c8214cbe9ffddf"
|
2019-05-14 17:07:53 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-01-07 15:39:11 +00:00
|
|
|
TapTweakHash::from_engine(TapTweakTag::engine()).to_string(),
|
2021-11-12 21:16:40 +00:00
|
|
|
"8aa4229474ab0100b2d6f0687f031d1fc9d8eef92a042ad97d279bff456b15e4"
|
2019-05-14 17:07:53 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-02-07 02:14:44 +00:00
|
|
|
TapSighash::from_engine(TapSighashTag::engine()).to_string(),
|
2021-11-12 21:16:40 +00:00
|
|
|
"dabc11914abcd8072900042a2681e52f8dba99ce82e224f97b5fdb7cd4b9c803"
|
2019-05-14 17:07:53 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// 0-byte
|
|
|
|
// CHashWriter writer = HasherTapLeaf;
|
|
|
|
// writer << std::vector<unsigned char>{};
|
|
|
|
// writer.GetSHA256().GetHex()
|
|
|
|
// Note that Core writes the 0 length prefix when an empty vector is written.
|
|
|
|
assert_eq!(
|
2023-01-07 15:39:11 +00:00
|
|
|
TapLeafHash::hash(&[0]).to_string(),
|
2021-11-12 21:16:40 +00:00
|
|
|
"ed1382037800c9dd938dd8854f1a8863bcdeb6705069b4b56a66ec22519d5829"
|
2019-05-14 17:07:53 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2022-12-15 15:54:32 +00:00
|
|
|
TapNodeHash::hash(&[0]).to_string(),
|
2021-11-12 21:16:40 +00:00
|
|
|
"92534b1960c7e6245af7d5fda2588db04aa6d646abc2b588dab2b69e5645eb1d"
|
2019-05-14 17:07:53 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-01-07 15:39:11 +00:00
|
|
|
TapTweakHash::hash(&[0]).to_string(),
|
2021-11-12 21:16:40 +00:00
|
|
|
"cd8737b5e6047fc3f16f03e8b9959e3440e1bdf6dd02f7bb899c352ad490ea1e"
|
2019-05-14 17:07:53 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-02-07 02:14:44 +00:00
|
|
|
TapSighash::hash(&[0]).to_string(),
|
2021-11-12 21:16:40 +00:00
|
|
|
"c2fd0de003889a09c4afcf676656a0d8a1fb706313ff7d509afb00c323c010cd"
|
2019-05-14 17:07:53 +00:00
|
|
|
);
|
|
|
|
}
|
2021-10-11 01:46:20 +00:00
|
|
|
|
2022-11-30 01:04:20 +00:00
|
|
|
fn _verify_tap_commitments(
|
|
|
|
secp: &Secp256k1<VerifyOnly>,
|
|
|
|
out_spk_hex: &str,
|
|
|
|
script_hex: &str,
|
|
|
|
control_block_hex: &str,
|
|
|
|
) {
|
2022-01-03 02:52:44 +00:00
|
|
|
let out_pk = XOnlyPublicKey::from_str(&out_spk_hex[4..]).unwrap();
|
2021-12-02 03:40:24 +00:00
|
|
|
let out_pk = TweakedPublicKey::dangerous_assume_tweaked(out_pk);
|
2022-07-30 12:22:18 +00:00
|
|
|
let script = ScriptBuf::from_hex(script_hex).unwrap();
|
2022-11-30 01:04:20 +00:00
|
|
|
let control_block =
|
2023-02-04 04:19:20 +00:00
|
|
|
ControlBlock::decode(&Vec::<u8>::from_hex(control_block_hex).unwrap()).unwrap();
|
2023-01-07 15:39:11 +00:00
|
|
|
assert_eq!(control_block_hex, control_block.serialize().to_lower_hex_string());
|
2022-02-25 02:30:20 +00:00
|
|
|
assert!(control_block.verify_taproot_commitment(secp, out_pk.to_inner(), &script));
|
2021-10-11 01:46:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn control_block_verify() {
|
2022-11-30 01:04:20 +00:00
|
|
|
let secp = Secp256k1::verification_only();
|
2022-11-05 19:52:38 +00:00
|
|
|
// test vectors obtained from printing values in feature_taproot.py from Bitcoin Core
|
2021-10-11 01:46:20 +00:00
|
|
|
_verify_tap_commitments(&secp, "51205dc8e62b15e0ebdf44751676be35ba32eed2e84608b290d4061bbff136cd7ba9", "6a", "c1a9d6f66cd4b25004f526bfa873e56942f98e8e492bd79ed6532b966104817c2bda584e7d32612381cf88edc1c02e28a296e807c16ad22f591ee113946e48a71e0641e660d1e5392fb79d64838c2b84faf04b7f5f283c9d8bf83e39e177b64372a0cd22eeab7e093873e851e247714eff762d8a30be699ba4456cfe6491b282e193a071350ae099005a5950d74f73ba13077a57bc478007fb0e4d1099ce9cf3d4");
|
|
|
|
_verify_tap_commitments(&secp, "5120e208c869c40d8827101c5ad3238018de0f3f5183d77a0c53d18ac28ddcbcd8ad", "f4", "c0a0eb12e60a52614986c623cbb6621dcdba3a47e3be6b37e032b7a11c7b98f40090ab1f4890d51115998242ebce636efb9ede1b516d9eb8952dc1068e0335306199aaf103cceb41d9bc37ec231aca89b984b5fd3c65977ce764d51033ac65adb4da14e029b1e154a85bfd9139e7aa2720b6070a4ceba8264ca61d5d3ac27aceb9ef4b54cd43c2d1fd5e11b5c2e93cf29b91ea3dc5b832201f02f7473a28c63246");
|
2022-11-30 01:04:20 +00:00
|
|
|
_verify_tap_commitments(
|
|
|
|
&secp,
|
|
|
|
"5120567666e7df90e0450bb608e17c01ed3fbcfa5355a5f8273e34e583bfaa70ce09",
|
|
|
|
"203455139bf238a3067bd72ed77e0ab8db590330f55ed58dba7366b53bf4734279ac",
|
|
|
|
"c1a0eb12e60a52614986c623cbb6621dcdba3a47e3be6b37e032b7a11c7b98f400",
|
|
|
|
);
|
2021-10-11 01:46:20 +00:00
|
|
|
_verify_tap_commitments(&secp, "5120580a19e47269414a55eb86d5d0c6c9b371455d9fd2154412a57dec840df99fe1", "6a", "bca0eb12e60a52614986c623cbb6621dcdba3a47e3be6b37e032b7a11c7b98f40042ba1bd1c63c03ccff60d4c4d53a653f87909eb3358e7fa45c9d805231fb08c933e1f4e0f9d17f591df1419df7d5b7eb5f744f404c5ef9ecdb1b89b18cafa3a816d8b5dba3205f9a9c05f866d91f40d2793a7586d502cb42f46c7a11f66ad4aa");
|
|
|
|
_verify_tap_commitments(&secp, "5120228b94a4806254a38d6efa8a134c28ebc89546209559dfe40b2b0493bafacc5b", "6a50", "c0a0eb12e60a52614986c623cbb6621dcdba3a47e3be6b37e032b7a11c7b98f4009c9aed3dfd11ab0e78bf87ef3bf296269dc4b0f7712140386d6980992bab4b45");
|
2022-11-30 01:04:20 +00:00
|
|
|
_verify_tap_commitments(
|
|
|
|
&secp,
|
|
|
|
"5120567666e7df90e0450bb608e17c01ed3fbcfa5355a5f8273e34e583bfaa70ce09",
|
|
|
|
"203455139bf238a3067bd72ed77e0ab8db590330f55ed58dba7366b53bf4734279ac",
|
|
|
|
"c1a0eb12e60a52614986c623cbb6621dcdba3a47e3be6b37e032b7a11c7b98f400",
|
|
|
|
);
|
|
|
|
_verify_tap_commitments(
|
|
|
|
&secp,
|
|
|
|
"5120b0a79103c31fe51eea61d2873bad8a25a310da319d7e7a85f825fa7a00ea3f85",
|
|
|
|
"203455139bf238a3067bd72ed77e0ab8db590330f55ed58dba7366b53bf4734279ad51",
|
|
|
|
"c1a0eb12e60a52614986c623cbb6621dcdba3a47e3be6b37e032b7a11c7b98f400",
|
|
|
|
);
|
2021-10-11 01:46:20 +00:00
|
|
|
_verify_tap_commitments(&secp, "5120f2f62e854a0012aeba78cd4ba4a0832447a5262d4c6eb4f1c95c7914b536fc6c", "6a86", "c1a0eb12e60a52614986c623cbb6621dcdba3a47e3be6b37e032b7a11c7b98f4009ad3d30479f0689dbdf59a6b840d60ad485b2effbed1825a75ce19a44e460e09056f60ea686d79cfa4fb79f197b2e905ac857a983be4a5a41a4873e865aa950780c0237de279dc063e67deec46ef8e1bc351bf12c4d67a6d568001faf097e797e6ee620f53cfe0f8acaddf2063c39c3577853bb46d61ffcba5a024c3e1216837");
|
|
|
|
_verify_tap_commitments(&secp, "51202a4772070b49bae68b44315032cdbf9c40c7c2f896781b32b931b73dbfb26d7e", "6af8", "c0a0eb12e60a52614986c623cbb6621dcdba3a47e3be6b37e032b7a11c7b98f4006f183944a14618fc7fe9ceade0f58e43a19d3c3b179ea6c43c29616413b6971c99aaf103cceb41d9bc37ec231aca89b984b5fd3c65977ce764d51033ac65adb4c3462adec78cd04f3cc156bdadec50def99feae0dc6a23664e8a2b0d42d6ca9eb968dfdf46c23af642b2688351904e0a0630e71ffac5bcaba33b9b2c8a7495ec");
|
|
|
|
_verify_tap_commitments(&secp, "5120a32b0b8cfafe0f0f8d5870030ba4d19a8725ad345cb3c8420f86ac4e0dff6207", "4c", "e8a0eb12e60a52614986c623cbb6621dcdba3a47e3be6b37e032b7a11c7b98f400615da7ac8d078e5fc7f4690fc2127ba40f0f97cc070ade5b3a7919783d91ef3f13734aab908ae998e57848a01268fe8217d70bc3ee8ea8ceae158ae964a4b5f3af20b50d7019bf47fde210eee5c52f1cfe71cfca78f2d3e7c1fd828c80351525");
|
2022-11-30 01:04:20 +00:00
|
|
|
_verify_tap_commitments(
|
|
|
|
&secp,
|
|
|
|
"5120b0a79103c31fe51eea61d2873bad8a25a310da319d7e7a85f825fa7a00ea3f85",
|
|
|
|
"203455139bf238a3067bd72ed77e0ab8db590330f55ed58dba7366b53bf4734279ad51",
|
|
|
|
"c1a0eb12e60a52614986c623cbb6621dcdba3a47e3be6b37e032b7a11c7b98f400",
|
|
|
|
);
|
2021-10-11 01:46:20 +00:00
|
|
|
_verify_tap_commitments(&secp, "51208678459f1fa0f80e9b89b8ffdcaf46a022bdf60aa45f1fed9a96145edf4ec400", "6a50", "c0a0eb12e60a52614986c623cbb6621dcdba3a47e3be6b37e032b7a11c7b98f4001eff29e1a89e650076b8d3c56302881d09c9df215774ed99993aaed14acd6615");
|
|
|
|
_verify_tap_commitments(&secp, "5120017316303aed02bcdec424c851c9eacbe192b013139bd9634c4e19b3475b06e1", "61", "02a0eb12e60a52614986c623cbb6621dcdba3a47e3be6b37e032b7a11c7b98f40050462265ca552b23cbb4fe021b474313c8cb87d4a18b3f7bdbeb2b418279ba31fc6509d829cd42336f563363cb3538d78758e0876c71e13012eb2b656eb0edb051a2420a840d5c8c6c762abc7410af2c311f606b20ca2ace56a8139f84b1379a");
|
|
|
|
_verify_tap_commitments(&secp, "5120896d4d5d2236e86c6e9320e86d1a7822e652907cbd508360e8c71aefc127c77d", "61", "14a0eb12e60a52614986c623cbb6621dcdba3a47e3be6b37e032b7a11c7b98f4001ab0e9d9a4858a0e69605fe9c5a42d739fbe26fa79650e7074f462b02645f7ea1c91802b298cd91e6b5af57c6a013d93397cd2ecbd5569382cc27becf44ff4fff8960b20f846160c159c58350f6b6072cf1b3daa5185b7a42524fb72cbc252576ae46732b8e31ac24bfa7d72f4c3713e8696f99d8ac6c07e4c820a03f249f144");
|
|
|
|
_verify_tap_commitments(&secp, "512093c7378d96518a75448821c4f7c8f4bae7ce60f804d03d1f0628dd5dd0f5de51", "04ffffffff203455139bf238a3067bd72ed77e0ab8db590330f55ed58dba7366b53bf4734279ba04feffffff87ab", "c1a0eb12e60a52614986c623cbb6621dcdba3a47e3be6b37e032b7a11c7b98f400c9a5cd1f6c8a81f5648e39f9810591df1c9a8f1fe97c92e03ecd7c0c016c951983e05473c6e8238cb4c780ea2ce62552b2a3eee068ceffc00517cd7b97e10dad");
|
|
|
|
_verify_tap_commitments(&secp, "5120b28d75a7179de6feb66b8bb0bfa2b2c739d1a41cf7366a1b393804a844db8a28", "61", "c4a0eb12e60a52614986c623cbb6621dcdba3a47e3be6b37e032b7a11c7b98f400eebc95ded88fb8050094e8dfa958c3be0894eaff0fafae678206b26918d8d7ac47039d40fe34d04b4155df7f1be7f2a49253c7e87812ea9e569e683ac27459e652d6503aa32d64734d00adfee8798b2eed28858abf3bd038e8fa58eb7df4a2d9");
|
|
|
|
_verify_tap_commitments(&secp, "512043e4aa733fc6f43c78a31c2b3c192623acf5cc8c01199ebcc4de88067baca83e", "bd4c", "c1a0eb12e60a52614986c623cbb6621dcdba3a47e3be6b37e032b7a11c7b98f4003f7be6f8848b5bddf332c4d7bd83077f73701e2479f70e02b5730e841234d082b8b41ebea96ffd937715d9faeaa6895e6ef3b22919c554b75df12b3371d328023e443d1df50634ecc1cd169803a1e546f0d44304d8fc5056c408e597fed469b8437d6660eaad3cf72e35ba6e5ff7ddd5e293c1e7e813c871df4f46508e9946ec");
|
|
|
|
_verify_tap_commitments(&secp, "5120ee9aecb28f5f35ce1f8b5ec80275ac0f81bca4a21b29b4632fb4bcbef8823e6a", "2021a5981b13be29c9d4ea179ea44a8b773ea8c02d68f6f6eefd98de20d4bd055fac", "c13359c284c196b6e80f0cf1d93b6a397cf7ee722f0427b705bd954b88ada8838bd2622fd0e104fc50aa763b43c6a792d7d117029983abd687223b4344a9402c618bba7f5fc3fa8a57491f6842acde88c1e675ca35caea3b1a69ee2c2d9b10f615");
|
|
|
|
_verify_tap_commitments(&secp, "5120885274df2252b44764dcef53c21f21154e8488b7e79fafbc96b9ebb22ad0200d", "6a50", "c1a0eb12e60a52614986c623cbb6621dcdba3a47e3be6b37e032b7a11c7b98f4000793597254158918e3369507f2d6fdbef17d18b1028bbb0719450ded0f42c58f");
|
|
|
|
_verify_tap_commitments(&secp, "512066f6f6f91d47674d198a28388e1eb05ec24e6ddbba10f16396b1a80c08675121", "6a50", "c1a0eb12e60a52614986c623cbb6621dcdba3a47e3be6b37e032b7a11c7b98f400fe92aff70a2e8e2a4f34a913b99612468a41e0f8ecaff9a729a173d11013c27e");
|
|
|
|
_verify_tap_commitments(&secp, "5120868ed9307bd4637491ff03e3aa2c216a08fe213cac8b6cedbb9ab31dbfa6512c", "61", "a2a0eb12e60a52614986c623cbb6621dcdba3a47e3be6b37e032b7a11c7b98f400da584e7d32612381cf88edc1c02e28a296e807c16ad22f591ee113946e48a71e46c7eccffefd2d573ec014130e508f0c9963ccebd7830409f7b1b1301725e9fa759d4ef857ec8e0bb42d6d31609d3c7e77de3bfa28c38f93393a6ddbabe819ec560ed4f061fbe742a5fd2a648d5209469420434c8753da3fa7067cc2bb4c172a");
|
|
|
|
_verify_tap_commitments(&secp, "5120c1a00a9baa82888fd7d30291135a7eaa9e9966a5f16db2b10460572f8b108d8d", "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "5ba0eb12e60a52614986c623cbb6621dcdba3a47e3be6b37e032b7a11c7b98f4007960d7b37dd1361aee34510e77acb4d27ddca17648a17e28475032538c1eb500f5a747f2c0893f79fe153ae918ac3d696de9322aa679aae62051ff5ed83aa502b338bd907346abd4cd9cf06117cb35d55a5a8dd950843522f8de7b5c7fba1804c38b0778d3d76b383f6db6fdf9d6e770da8fffbfa5152c0b8b38129885bcdee6");
|
|
|
|
_verify_tap_commitments(&secp, "5120bb9abeff7286b76dfc61800c548fe2621ff47506e47201a85c543b4a9a96fead", "75203455139bf238a3067bd72ed77e0ab8db590330f55ed58dba7366b53bf47342796ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6eadac", "c0a0eb12e60a52614986c623cbb6621dcdba3a47e3be6b37e032b7a11c7b98f4003eb5cdc419e0a6a800f34583ce750f387be34879c26f4230991bd61da743ad9d34d288e79397b709ac22ad8cc57645d593af3e15b97a876362117177ab2519c000000000000000000000000000000000000000000000000000000000000000007160c3a48c8b17bc3aeaf01db9e0a96ac47a5a9fa329e046856e7765e89c8a93ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff07feb9aa7cd72c78e66a85414cd19289f8b0ab1415013dc2a007666aa9248ec1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001fccc8bea662a9442a94f7ba0643c1d7ee7cc689f3b3506b7c8c99fd3f3b3d7772972dcdf2550cf95b65098aea67f72fef10abdcf1cef9815af8f4c4644b060e0000000000000000000000000000000000000000000000000000000000000000");
|
|
|
|
_verify_tap_commitments(&secp, "5120afddc189ea51094b4cbf463806792e9c8b35dfdc5e01228c78376380d0046b00", "4d09024747703eb9f759ce5ecd839109fecc40974ab16f5173ea390daaa5a78f7abe898165c90990062af998c5dc7989818393158a2c62b7ece727e7f5400d2efd33db8732599f6d1dce6b5b68d2d47317f2de6c9df118f61227f98453225036618aaf058140f2415d134fa69ba041c724ad81387f8c568d12ddc49eb32a71532096181b3f85fd465b8e9a176bb19f45c070baad47a2cc4505414b88c31cb5b0a192b2d2d56c404a37070b04d42c875c4ac351224f5b254f9ad0b820f43cad292d6565f796bf083173e14723f1e543c85a61689ddd5cb6666b240c15c38ce3320bf0c3be9e0322e5ef72366c294d3a2d7e8b8e7db875e7ae814537554f10b91c72b8b413e026bd5d5e917de4b54fa8f43f38771a7f242aa32dcb7ca1b0588dbf54af7ab9455047fbb894cdfdd242166db784276430eb47d4df092a6b8cb160eb982fe7d14a44283bdb4a9861ca65c06fd8b2546cfbfe38bc77f527de1b9bfd2c95a3e283b7b1d1d2b2fa291256a90a7003aefcef47ceabf113865a494af43e96a38b0b00919855eb7722ea2363e0ddfc9c51c08631d01e2a2d56e786b4ff6f1e5d415facc9c2619c285d9ad43001878294157cb025f639fb954271fd1d6173f6bc16535672f6abdd72b0284b4ff3eaf5b7247719d7c39365622610efae6562bef6e08a0b370fba75bb04dbdb90a482d8417e057f8bd021ea6ac32d0d48b08be9f77833b11e5e739960c9837d7583", "c0a0eb12e60a52614986c623cbb6621dcdba3a47e3be6b37e032b7a11c7b98f400ff698adfda0327f188e2ee35f7aecc0f90c9138a350d450648d968c2b5dd7ef94ddd3ec418dc0d03ee4956feb708d838ed2b20e5a193465a6a1467fd3054e1ea141ea4c4c503a6271e19a090e2a69a24282e3be04c4f98720f7a0eb274d9693d13a8e3c139aa625fa2aefd09854570527f9ac545bda1b689719f5cb715612c07");
|
|
|
|
_verify_tap_commitments(&secp, "5120afddc189ea51094b4cbf463806792e9c8b35dfdc5e01228c78376380d0046b00", "83", "c0a0eb12e60a52614986c623cbb6621dcdba3a47e3be6b37e032b7a11c7b98f4007388cda01113397d4cd00bcfbd08fd68c3cfe3a42cbfe3a7651c1d5e6dacf1ad99aaf103cceb41d9bc37ec231aca89b984b5fd3c65977ce764d51033ac65adb4b59764bec92507e4a4c3f01a06f05980163ca10f1c549bfe01f85fa4f109a1295e607f5ed9f1008048474de336f11f67a1fbf2012f58944dede0ab19a3ca81f5");
|
|
|
|
_verify_tap_commitments(&secp, "512093c7378d96518a75448821c4f7c8f4bae7ce60f804d03d1f0628dd5dd0f5de51", "04ffffffff203455139bf238a3067bd72ed77e0ab8db590330f55ed58dba7366b53bf4734279ba04feffffff87ab", "c1a0eb12e60a52614986c623cbb6621dcdba3a47e3be6b37e032b7a11c7b98f400c9a5cd1f6c8a81f5648e39f9810591df1c9a8f1fe97c92e03ecd7c0c016c951983e05473c6e8238cb4c780ea2ce62552b2a3eee068ceffc00517cd7b97e10dad");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn build_huffman_tree() {
|
|
|
|
let secp = Secp256k1::verification_only();
|
2022-11-30 01:04:20 +00:00
|
|
|
let internal_key = UntweakedPublicKey::from_str(
|
|
|
|
"93c7378d96518a75448821c4f7c8f4bae7ce60f804d03d1f0628dd5dd0f5de51",
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-10-11 01:46:20 +00:00
|
|
|
|
|
|
|
let script_weights = vec![
|
2022-07-30 12:22:18 +00:00
|
|
|
(10, ScriptBuf::from_hex("51").unwrap()), // semantics of script don't matter for this test
|
|
|
|
(20, ScriptBuf::from_hex("52").unwrap()),
|
|
|
|
(20, ScriptBuf::from_hex("53").unwrap()),
|
|
|
|
(30, ScriptBuf::from_hex("54").unwrap()),
|
|
|
|
(19, ScriptBuf::from_hex("55").unwrap()),
|
2021-10-11 01:46:20 +00:00
|
|
|
];
|
2022-11-30 01:04:20 +00:00
|
|
|
let tree_info =
|
|
|
|
TaprootSpendInfo::with_huffman_tree(&secp, internal_key, script_weights.clone())
|
|
|
|
.unwrap();
|
2021-10-11 01:46:20 +00:00
|
|
|
|
2021-11-15 02:39:42 +00:00
|
|
|
/* The resulting tree should put the scripts into a tree similar
|
|
|
|
* to the following:
|
|
|
|
*
|
|
|
|
* 1 __/\__
|
|
|
|
* / \
|
|
|
|
* /\ / \
|
|
|
|
* 2 54 52 53 /\
|
|
|
|
* 3 55 51
|
|
|
|
*/
|
|
|
|
|
2022-06-07 05:20:07 +00:00
|
|
|
for (script, length) in [("51", 3), ("52", 2), ("53", 2), ("54", 2), ("55", 3)].iter() {
|
2021-11-15 02:39:42 +00:00
|
|
|
assert_eq!(
|
2022-06-07 05:20:07 +00:00
|
|
|
*length,
|
2021-11-15 02:39:42 +00:00
|
|
|
tree_info
|
|
|
|
.script_map
|
2022-07-30 12:22:18 +00:00
|
|
|
.get(&(ScriptBuf::from_hex(script).unwrap(), LeafVersion::TapScript))
|
2021-11-15 02:39:42 +00:00
|
|
|
.expect("Present Key")
|
|
|
|
.iter()
|
|
|
|
.next()
|
|
|
|
.expect("Present Path")
|
|
|
|
.0
|
|
|
|
.len()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-11 01:46:20 +00:00
|
|
|
// Obtain the output key
|
|
|
|
let output_key = tree_info.output_key();
|
|
|
|
|
|
|
|
// Try to create and verify a control block from each path
|
|
|
|
for (_weights, script) in script_weights {
|
2021-11-24 23:01:21 +00:00
|
|
|
let ver_script = (script, LeafVersion::TapScript);
|
2021-10-11 01:46:20 +00:00
|
|
|
let ctrl_block = tree_info.control_block(&ver_script).unwrap();
|
2022-11-30 01:04:20 +00:00
|
|
|
assert!(ctrl_block.verify_taproot_commitment(
|
|
|
|
&secp,
|
|
|
|
output_key.to_inner(),
|
|
|
|
&ver_script.0
|
|
|
|
))
|
2021-10-11 01:46:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn taptree_builder() {
|
|
|
|
let secp = Secp256k1::verification_only();
|
2022-11-30 01:04:20 +00:00
|
|
|
let internal_key = UntweakedPublicKey::from_str(
|
|
|
|
"93c7378d96518a75448821c4f7c8f4bae7ce60f804d03d1f0628dd5dd0f5de51",
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-10-11 01:46:20 +00:00
|
|
|
|
|
|
|
let builder = TaprootBuilder::new();
|
|
|
|
// Create a tree as shown below
|
|
|
|
// For example, imagine this tree:
|
|
|
|
// A, B , C are at depth 2 and D,E are at 3
|
|
|
|
// ....
|
|
|
|
// / \
|
|
|
|
// /\ /\
|
|
|
|
// / \ / \
|
|
|
|
// A B C / \
|
|
|
|
// D E
|
2022-07-30 12:22:18 +00:00
|
|
|
let a = ScriptBuf::from_hex("51").unwrap();
|
|
|
|
let b = ScriptBuf::from_hex("52").unwrap();
|
|
|
|
let c = ScriptBuf::from_hex("53").unwrap();
|
|
|
|
let d = ScriptBuf::from_hex("54").unwrap();
|
|
|
|
let e = ScriptBuf::from_hex("55").unwrap();
|
2021-10-11 01:46:20 +00:00
|
|
|
let builder = builder.add_leaf(2, a.clone()).unwrap();
|
|
|
|
let builder = builder.add_leaf(2, b.clone()).unwrap();
|
|
|
|
let builder = builder.add_leaf(2, c.clone()).unwrap();
|
|
|
|
let builder = builder.add_leaf(3, d.clone()).unwrap();
|
2022-07-29 00:38:26 +00:00
|
|
|
|
|
|
|
// Trying to finalize an incomplete tree returns the Err(builder)
|
|
|
|
let builder = builder.finalize(&secp, internal_key).unwrap_err();
|
2021-10-11 01:46:20 +00:00
|
|
|
let builder = builder.add_leaf(3, e.clone()).unwrap();
|
|
|
|
|
2023-01-11 12:25:08 +00:00
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
{
|
|
|
|
let tree = TapTree::try_from(builder.clone()).unwrap();
|
|
|
|
// test roundtrip serialization with serde_test
|
|
|
|
#[rustfmt::skip]
|
|
|
|
assert_tokens(&tree.readable(), &[
|
|
|
|
Token::Seq { len: Some(10) },
|
|
|
|
Token::U64(2), Token::TupleVariant { name: "TapLeaf", variant: "Script", len: 2}, Token::Str("51"), Token::U8(192), Token::TupleVariantEnd,
|
|
|
|
Token::U64(2), Token::TupleVariant { name: "TapLeaf", variant: "Script", len: 2}, Token::Str("52"), Token::U8(192), Token::TupleVariantEnd,
|
|
|
|
Token::U64(3), Token::TupleVariant { name: "TapLeaf", variant: "Script", len: 2}, Token::Str("55"), Token::U8(192), Token::TupleVariantEnd,
|
|
|
|
Token::U64(3), Token::TupleVariant { name: "TapLeaf", variant: "Script", len: 2}, Token::Str("54"), Token::U8(192), Token::TupleVariantEnd,
|
|
|
|
Token::U64(2), Token::TupleVariant { name: "TapLeaf", variant: "Script", len: 2}, Token::Str("53"), Token::U8(192), Token::TupleVariantEnd,
|
|
|
|
Token::SeqEnd,
|
|
|
|
],);
|
|
|
|
|
|
|
|
let node_info = TapTree::try_from(builder.clone()).unwrap().into_node_info();
|
|
|
|
// test roundtrip serialization with serde_test
|
|
|
|
#[rustfmt::skip]
|
|
|
|
assert_tokens(&node_info.readable(), &[
|
|
|
|
Token::Seq { len: Some(10) },
|
|
|
|
Token::U64(2), Token::TupleVariant { name: "TapLeaf", variant: "Script", len: 2}, Token::Str("51"), Token::U8(192), Token::TupleVariantEnd,
|
|
|
|
Token::U64(2), Token::TupleVariant { name: "TapLeaf", variant: "Script", len: 2}, Token::Str("52"), Token::U8(192), Token::TupleVariantEnd,
|
|
|
|
Token::U64(3), Token::TupleVariant { name: "TapLeaf", variant: "Script", len: 2}, Token::Str("55"), Token::U8(192), Token::TupleVariantEnd,
|
|
|
|
Token::U64(3), Token::TupleVariant { name: "TapLeaf", variant: "Script", len: 2}, Token::Str("54"), Token::U8(192), Token::TupleVariantEnd,
|
|
|
|
Token::U64(2), Token::TupleVariant { name: "TapLeaf", variant: "Script", len: 2}, Token::Str("53"), Token::U8(192), Token::TupleVariantEnd,
|
|
|
|
Token::SeqEnd,
|
|
|
|
],);
|
|
|
|
}
|
|
|
|
|
2021-10-11 01:46:20 +00:00
|
|
|
let tree_info = builder.finalize(&secp, internal_key).unwrap();
|
|
|
|
let output_key = tree_info.output_key();
|
|
|
|
|
2023-11-14 04:13:38 +00:00
|
|
|
for script in [a, b, c, d, e] {
|
2021-11-24 23:01:21 +00:00
|
|
|
let ver_script = (script, LeafVersion::TapScript);
|
2021-10-11 01:46:20 +00:00
|
|
|
let ctrl_block = tree_info.control_block(&ver_script).unwrap();
|
2022-11-30 01:04:20 +00:00
|
|
|
assert!(ctrl_block.verify_taproot_commitment(
|
|
|
|
&secp,
|
|
|
|
output_key.to_inner(),
|
|
|
|
&ver_script.0
|
|
|
|
))
|
2021-10-11 01:46:20 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-12 21:50:37 +00:00
|
|
|
|
2023-01-01 19:44:40 +00:00
|
|
|
#[test]
|
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
fn test_leaf_version_serde() {
|
|
|
|
let leaf_version = LeafVersion::TapScript;
|
|
|
|
// use serde_test to test serialization and deserialization
|
|
|
|
assert_tokens(&leaf_version, &[Token::U8(192)]);
|
|
|
|
|
|
|
|
let json = serde_json::to_string(&leaf_version).unwrap();
|
|
|
|
let leaf_version2 = serde_json::from_str(&json).unwrap();
|
|
|
|
assert_eq!(leaf_version, leaf_version2);
|
|
|
|
}
|
|
|
|
|
2023-01-11 12:21:38 +00:00
|
|
|
#[test]
|
|
|
|
#[cfg(feature = "serde")]
|
|
|
|
fn test_merkle_branch_serde() {
|
|
|
|
let dummy_hash = hex!("03ba2a4dcd914fed29a1c630c7e811271b081a0e2f2f52cf1c197583dfd46c1b");
|
|
|
|
let hash1 = TapNodeHash::from_slice(&dummy_hash).unwrap();
|
|
|
|
let dummy_hash = hex!("8d79dedc2fa0b55167b5d28c61dbad9ce1191a433f3a1a6c8ee291631b2c94c9");
|
|
|
|
let hash2 = TapNodeHash::from_slice(&dummy_hash).unwrap();
|
|
|
|
let merkle_branch = TaprootMerkleBranch::from_collection(vec![hash1, hash2]).unwrap();
|
|
|
|
// use serde_test to test serialization and deserialization
|
|
|
|
serde_test::assert_tokens(
|
|
|
|
&merkle_branch.readable(),
|
|
|
|
&[
|
|
|
|
Token::Seq { len: Some(2) },
|
|
|
|
Token::Str("03ba2a4dcd914fed29a1c630c7e811271b081a0e2f2f52cf1c197583dfd46c1b"),
|
|
|
|
Token::Str("8d79dedc2fa0b55167b5d28c61dbad9ce1191a433f3a1a6c8ee291631b2c94c9"),
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-11-12 21:50:37 +00:00
|
|
|
#[test]
|
|
|
|
fn bip_341_tests() {
|
|
|
|
fn process_script_trees(
|
|
|
|
v: &serde_json::Value,
|
|
|
|
mut builder: TaprootBuilder,
|
2022-07-30 12:22:18 +00:00
|
|
|
leaves: &mut Vec<(ScriptBuf, LeafVersion)>,
|
2022-03-31 13:12:05 +00:00
|
|
|
depth: u8,
|
2021-11-12 21:50:37 +00:00
|
|
|
) -> TaprootBuilder {
|
|
|
|
if v.is_null() {
|
|
|
|
// nothing to push
|
|
|
|
} else if v.is_array() {
|
|
|
|
for leaf in v.as_array().unwrap() {
|
2022-11-30 01:04:20 +00:00
|
|
|
builder = process_script_trees(leaf, builder, leaves, depth + 1);
|
2021-11-12 21:50:37 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-01-20 00:24:48 +00:00
|
|
|
let script = ScriptBuf::from_hex(v["script"].as_str().unwrap()).unwrap();
|
2022-11-30 01:04:20 +00:00
|
|
|
let ver =
|
|
|
|
LeafVersion::from_consensus(v["leafVersion"].as_u64().unwrap() as u8).unwrap();
|
2021-11-12 21:50:37 +00:00
|
|
|
leaves.push((script.clone(), ver));
|
|
|
|
builder = builder.add_leaf_with_ver(depth, script, ver).unwrap();
|
|
|
|
}
|
|
|
|
builder
|
|
|
|
}
|
|
|
|
|
|
|
|
let data = bip_341_read_json();
|
|
|
|
// Check the version of data
|
|
|
|
assert!(data["version"] == 1);
|
|
|
|
let secp = &secp256k1::Secp256k1::verification_only();
|
|
|
|
|
|
|
|
for arr in data["scriptPubKey"].as_array().unwrap() {
|
2022-11-30 01:04:20 +00:00
|
|
|
let internal_key =
|
|
|
|
XOnlyPublicKey::from_str(arr["given"]["internalPubkey"].as_str().unwrap()).unwrap();
|
2021-11-12 21:50:37 +00:00
|
|
|
// process the tree
|
|
|
|
let script_tree = &arr["given"]["scriptTree"];
|
|
|
|
let mut merkle_root = None;
|
|
|
|
if script_tree.is_null() {
|
|
|
|
assert!(arr["intermediary"]["merkleRoot"].is_null());
|
|
|
|
} else {
|
2022-11-30 01:04:20 +00:00
|
|
|
merkle_root = Some(
|
2022-12-15 15:54:32 +00:00
|
|
|
TapNodeHash::from_str(arr["intermediary"]["merkleRoot"].as_str().unwrap())
|
2022-11-30 01:04:20 +00:00
|
|
|
.unwrap(),
|
|
|
|
);
|
2021-11-12 21:50:37 +00:00
|
|
|
let leaf_hashes = arr["intermediary"]["leafHashes"].as_array().unwrap();
|
|
|
|
let ctrl_blks = arr["expected"]["scriptPathControlBlocks"].as_array().unwrap();
|
|
|
|
let mut builder = TaprootBuilder::new();
|
|
|
|
let mut leaves = vec![];
|
2022-06-07 05:07:59 +00:00
|
|
|
builder = process_script_trees(script_tree, builder, &mut leaves, 0);
|
2021-11-12 21:50:37 +00:00
|
|
|
let spend_info = builder.finalize(secp, internal_key).unwrap();
|
|
|
|
for (i, script_ver) in leaves.iter().enumerate() {
|
|
|
|
let expected_leaf_hash = leaf_hashes[i].as_str().unwrap();
|
2023-02-04 04:19:20 +00:00
|
|
|
let expected_ctrl_blk = ControlBlock::decode(
|
2022-11-30 01:04:20 +00:00
|
|
|
&Vec::<u8>::from_hex(ctrl_blks[i].as_str().unwrap()).unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-11-12 21:50:37 +00:00
|
|
|
|
|
|
|
let leaf_hash = TapLeafHash::from_script(&script_ver.0, script_ver.1);
|
|
|
|
let ctrl_blk = spend_info.control_block(script_ver).unwrap();
|
2023-01-07 15:39:11 +00:00
|
|
|
assert_eq!(leaf_hash.to_string(), expected_leaf_hash);
|
2021-11-12 21:50:37 +00:00
|
|
|
assert_eq!(ctrl_blk, expected_ctrl_blk);
|
|
|
|
}
|
|
|
|
}
|
2022-11-30 01:04:20 +00:00
|
|
|
let expected_output_key =
|
|
|
|
XOnlyPublicKey::from_str(arr["intermediary"]["tweakedPubkey"].as_str().unwrap())
|
|
|
|
.unwrap();
|
|
|
|
let expected_tweak =
|
|
|
|
TapTweakHash::from_str(arr["intermediary"]["tweak"].as_str().unwrap()).unwrap();
|
|
|
|
let expected_spk =
|
2023-01-20 00:24:48 +00:00
|
|
|
ScriptBuf::from_hex(arr["expected"]["scriptPubKey"].as_str().unwrap()).unwrap();
|
2022-11-30 01:04:20 +00:00
|
|
|
let expected_addr =
|
2022-11-02 22:36:37 +00:00
|
|
|
Address::from_str(arr["expected"]["bip350Address"].as_str().unwrap())
|
|
|
|
.unwrap()
|
|
|
|
.assume_checked();
|
2021-11-12 21:50:37 +00:00
|
|
|
|
|
|
|
let tweak = TapTweakHash::from_key_and_tweak(internal_key, merkle_root);
|
2022-06-07 05:07:59 +00:00
|
|
|
let (output_key, _parity) = internal_key.tap_tweak(secp, merkle_root);
|
|
|
|
let addr = Address::p2tr(secp, internal_key, merkle_root, Network::Bitcoin);
|
2021-11-12 21:50:37 +00:00
|
|
|
let spk = addr.script_pubkey();
|
|
|
|
|
2022-01-19 02:46:51 +00:00
|
|
|
assert_eq!(expected_output_key, output_key.to_inner());
|
2021-11-12 21:50:37 +00:00
|
|
|
assert_eq!(expected_tweak, tweak);
|
|
|
|
assert_eq!(expected_addr, addr);
|
|
|
|
assert_eq!(expected_spk, spk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bip_341_read_json() -> serde_json::Value {
|
2023-10-31 10:39:31 +00:00
|
|
|
let json_str = include_str!("../../tests/data/bip341_tests.json");
|
2021-11-12 21:50:37 +00:00
|
|
|
serde_json::from_str(json_str).expect("JSON was not well-formatted")
|
|
|
|
}
|
2019-05-14 17:07:53 +00:00
|
|
|
}
|