From c30ef617fb6f0544666a57332e2b65a8cd69f81c Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 22 Oct 2024 13:33:56 +1100 Subject: [PATCH] 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. --- bitcoin/src/taproot/mod.rs | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/bitcoin/src/taproot/mod.rs b/bitcoin/src/taproot/mod.rs index b18d87a7e..6fb892825 100644 --- a/bitcoin/src/taproot/mod.rs +++ b/bitcoin/src/taproot/mod.rs @@ -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::::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::::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 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::::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::::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 { 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