Skip to content

Commit f2a6827

Browse files
committed
Fix BinaryHeap direction for Taproot Huffman Encoder
1 parent cccd75d commit f2a6827

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

src/util/taproot.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use io;
1818
use secp256k1::{self, Secp256k1};
1919

2020
use core::fmt;
21+
use core::cmp::Reverse;
2122
#[cfg(feature = "std")]
2223
use std::error;
2324

@@ -208,9 +209,9 @@ impl TaprootSpendInfo {
208209
I: IntoIterator<Item = (u64, Script)>,
209210
C: secp256k1::Verification,
210211
{
211-
let mut node_weights = BinaryHeap::<(u128, NodeInfo)>::new();
212+
let mut node_weights = BinaryHeap::<(Reverse<u128>, NodeInfo)>::new();
212213
for (p, leaf) in script_weights {
213-
node_weights.push((p as u128, NodeInfo::new_leaf_with_ver(leaf, LeafVersion::default())));
214+
node_weights.push((Reverse(p as u128), NodeInfo::new_leaf_with_ver(leaf, LeafVersion::default())));
214215
}
215216
if node_weights.is_empty() {
216217
return Err(TaprootBuilderError::IncompleteTree);
@@ -223,7 +224,7 @@ impl TaprootSpendInfo {
223224
// N.B.: p1 + p2 can never actually saturate as you would need to have 2**64 max u64s
224225
// from the input to overflow. However, saturating is a reasonable behavior here as
225226
// huffman tree construction would treat all such elements as "very likely".
226-
let p = p1.saturating_add(p2);
227+
let p = Reverse(p1.0.saturating_add(p2.0));
227228
node_weights.push((p, NodeInfo::combine(s1, s2)?));
228229
}
229230
// Every iteration of the loop reduces the node_weights.len() by exactly 1

0 commit comments

Comments
 (0)