diff options
author | Russell O'Connor <roconnor@blockstream.io> | 2022-08-19 11:59:18 -0400 |
---|---|---|
committer | Russell O'Connor <roconnor@blockstream.io> | 2022-11-21 12:38:53 -0500 |
commit | dee89438b82e94474ebaa31367035f98b4636dac (patch) | |
tree | 4c4f663303788784b299f476accc0a8b9f0a2475 /src/script/interpreter.cpp | |
parent | 8e3fc9942729716e95907008fcf36eee758c3a6a (diff) |
Abstract out ComputeTapbranchHash
Diffstat (limited to 'src/script/interpreter.cpp')
-rw-r--r-- | src/script/interpreter.cpp | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/src/script/interpreter.cpp b/src/script/interpreter.cpp index 5148265b9c..c06d0370c9 100644 --- a/src/script/interpreter.cpp +++ b/src/script/interpreter.cpp @@ -1830,6 +1830,17 @@ uint256 ComputeTapleafHash(uint8_t leaf_version, Span<const unsigned char> scrip return (HashWriter{HASHER_TAPLEAF} << leaf_version << CompactSizeWriter(script.size()) << script).GetSHA256(); } +uint256 ComputeTapbranchHash(Span<const unsigned char> a, Span<const unsigned char> b) +{ + HashWriter ss_branch{HASHER_TAPBRANCH}; + if (std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end())) { + ss_branch << a << b; + } else { + ss_branch << b << a; + } + return ss_branch.GetSHA256(); +} + uint256 ComputeTaprootMerkleRoot(Span<const unsigned char> control, const uint256& tapleaf_hash) { assert(control.size() >= TAPROOT_CONTROL_BASE_SIZE); @@ -1839,14 +1850,8 @@ uint256 ComputeTaprootMerkleRoot(Span<const unsigned char> control, const uint25 const int path_len = (control.size() - TAPROOT_CONTROL_BASE_SIZE) / TAPROOT_CONTROL_NODE_SIZE; uint256 k = tapleaf_hash; for (int i = 0; i < path_len; ++i) { - HashWriter ss_branch{HASHER_TAPBRANCH}; Span node{Span{control}.subspan(TAPROOT_CONTROL_BASE_SIZE + TAPROOT_CONTROL_NODE_SIZE * i, TAPROOT_CONTROL_NODE_SIZE)}; - if (std::lexicographical_compare(k.begin(), k.end(), node.begin(), node.end())) { - ss_branch << k << node; - } else { - ss_branch << node << k; - } - k = ss_branch.GetSHA256(); + k = ComputeTapbranchHash(k, node); } return k; } |