Merge rust-bitcoin/rust-bitcoin#926: Remove redundant code computing tap hashes
f3ebfd6f8b
Remove repeated tap branch hash computing logic (Dr Maxim Orlovsky)1b28375658
Abstract tap branch hash computing into a dedicated method (Dr Maxim Orlovsky) Pull request description: ACKs for top commit: apoelstra: ACKf3ebfd6f8b
Tree-SHA512: d66d544df324a7d25c8cc9dc48ddedf086ac87eb2ed09a8d7a568cc1488ae44e0807d53ccb7a6e61dbeef0d3d62a1cacf0d69ba7b8de9178ac5c13bae944d08b
This commit is contained in:
commit
7fa8ce0bd0
|
@ -131,6 +131,21 @@ impl TapLeafHash {
|
|||
}
|
||||
}
|
||||
|
||||
impl TapBranchHash {
|
||||
/// Computes branch hash given two hashes of the nodes underneath it.
|
||||
pub fn from_node_hashes(a: sha256::Hash, b: sha256::Hash) -> TapBranchHash {
|
||||
let mut eng = TapBranchHash::engine();
|
||||
if a < b {
|
||||
eng.input(&a);
|
||||
eng.input(&b);
|
||||
} else {
|
||||
eng.input(&b);
|
||||
eng.input(&a);
|
||||
};
|
||||
TapBranchHash::from_engine(eng)
|
||||
}
|
||||
}
|
||||
|
||||
/// 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;
|
||||
|
@ -565,16 +580,9 @@ impl NodeInfo {
|
|||
b_leaf.merkle_branch.push(a.hash)?; // add hashing partner
|
||||
all_leaves.push(b_leaf);
|
||||
}
|
||||
let mut eng = TapBranchHash::engine();
|
||||
if a.hash < b.hash {
|
||||
eng.input(&a.hash);
|
||||
eng.input(&b.hash);
|
||||
} else {
|
||||
eng.input(&b.hash);
|
||||
eng.input(&a.hash);
|
||||
};
|
||||
let hash = TapBranchHash::from_node_hashes(a.hash, b.hash);
|
||||
Ok(Self {
|
||||
hash: sha256::Hash::from_engine(eng),
|
||||
hash: sha256::Hash::from_inner(hash.into_inner()),
|
||||
leaves: all_leaves,
|
||||
})
|
||||
}
|
||||
|
@ -779,16 +787,11 @@ impl ControlBlock {
|
|||
let mut curr_hash = TapBranchHash::from_inner(leaf_hash.into_inner());
|
||||
// Verify the proof
|
||||
for elem in self.merkle_branch.as_inner() {
|
||||
let mut eng = TapBranchHash::engine();
|
||||
if curr_hash.as_inner() < elem.as_inner() {
|
||||
eng.input(&curr_hash);
|
||||
eng.input(elem);
|
||||
} else {
|
||||
eng.input(elem);
|
||||
eng.input(&curr_hash);
|
||||
}
|
||||
// Recalculate the curr hash as parent hash
|
||||
curr_hash = TapBranchHash::from_engine(eng);
|
||||
curr_hash = TapBranchHash::from_node_hashes(
|
||||
sha256::Hash::from_inner(curr_hash.into_inner()),
|
||||
*elem
|
||||
);
|
||||
}
|
||||
// compute the taptweak
|
||||
let tweak = TapTweakHash::from_key_and_tweak(self.internal_key, Some(curr_hash));
|
||||
|
|
Loading…
Reference in New Issue