bitcoin_primitives/
taproot.rs

1// SPDX-License-Identifier: CC0-1.0
2
3//! Bitcoin Taproot.
4//!
5//! This module provides support for Taproot tagged hashes.
6
7use hashes::{hash_newtype, sha256t, sha256t_tag};
8
9// Taproot test vectors from BIP-341 state the hashes without any reversing
10sha256t_tag! {
11    pub struct TapLeafTag = hash_str("TapLeaf");
12}
13
14hash_newtype! {
15    /// Taproot-tagged hash with tag \"TapLeaf\".
16    ///
17    /// This is used for computing tapscript script spend hash.
18    pub struct TapLeafHash(sha256t::Hash<TapLeafTag>);
19}
20
21sha256t_tag! {
22    pub struct TapBranchTag = hash_str("TapBranch");
23}
24
25hash_newtype! {
26    /// Tagged hash used in Taproot trees.
27    ///
28    /// See BIP-340 for tagging rules.
29    pub struct TapNodeHash(sha256t::Hash<TapBranchTag>);
30}
31
32sha256t_tag! {
33    pub struct TapTweakTag = hash_str("TapTweak");
34}
35
36hash_newtype! {
37    /// Taproot-tagged hash with tag \"TapTweak\".
38    ///
39    /// This hash type is used while computing the tweaked public key.
40    pub struct TapTweakHash(sha256t::Hash<TapTweakTag>);
41}
42
43impl From<TapLeafHash> for TapNodeHash {
44    fn from(leaf: TapLeafHash) -> TapNodeHash { TapNodeHash::from_byte_array(leaf.to_byte_array()) }
45}