Move combine_node_hashes out of TapNodeHash

Currently `combine_node_hashes` is an associated function, it is also
private. It is called from within other methods of the `TapNodeHash`.

In preparation for moving the `TapNodeHash` to `primitives` while
leaving all the methods in `bitcoin` in an extension trait; move the
associated function out of `TapNodeHash` and make it a stand alone
private function.
This commit is contained in:
Tobin C. Harding 2024-10-22 13:33:56 +11:00
parent b4f52ac87a
commit c30ef617fb
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
1 changed files with 17 additions and 17 deletions

View File

@ -110,22 +110,7 @@ impl From<&LeafNode> for TapNodeHash {
impl TapNodeHash {
/// Computes branch hash given two hashes of the nodes underneath it.
pub fn from_node_hashes(a: TapNodeHash, b: TapNodeHash) -> TapNodeHash {
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) {
let mut eng = sha256t::Hash::<TapBranchTag>::engine();
if a < b {
eng.input(a.as_ref());
eng.input(b.as_ref());
} else {
eng.input(b.as_ref());
eng.input(a.as_ref());
};
let inner = sha256t::Hash::<TapBranchTag>::from_engine(eng);
(TapNodeHash::from_byte_array(inner.to_byte_array()), a < b)
combine_node_hashes(a, b).0
}
/// Assumes the given 32 byte array as hidden [`TapNodeHash`].
@ -145,6 +130,21 @@ impl From<TapLeafHash> for TapNodeHash {
fn from(leaf: TapLeafHash) -> TapNodeHash { TapNodeHash::from_byte_array(leaf.to_byte_array()) }
}
/// 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) {
let mut eng = sha256t::Hash::<TapBranchTag>::engine();
if a < b {
eng.input(a.as_ref());
eng.input(b.as_ref());
} else {
eng.input(b.as_ref());
eng.input(a.as_ref());
};
let inner = sha256t::Hash::<TapBranchTag>::from_engine(eng);
(TapNodeHash::from_byte_array(inner.to_byte_array()), a < b)
}
/// Maximum depth of a Taproot tree script spend path.
// https://github.com/bitcoin/bitcoin/blob/e826b22da252e0599c61d21c98ff89f366b3120f/src/script/interpreter.h#L229
pub const TAPROOT_CONTROL_MAX_NODE_COUNT: usize = 128;
@ -853,7 +853,7 @@ impl NodeInfo {
/// Combines two [`NodeInfo`] to create a new parent.
pub fn combine(a: Self, b: Self) -> Result<Self, TaprootBuilderError> {
let mut all_leaves = Vec::with_capacity(a.leaves.len() + b.leaves.len());
let (hash, left_first) = TapNodeHash::combine_node_hashes(a.hash, b.hash);
let (hash, left_first) = combine_node_hashes(a.hash, b.hash);
let (a, b) = if left_first { (a, b) } else { (b, a) };
for mut a_leaf in a.leaves {
a_leaf.merkle_branch.push(b.hash)?; // add hashing partner