Skip to content

Commit 0fb47b7

Browse files
committed
satisfaction: use new Threshold type
1 parent ff8f077 commit 0fb47b7

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

src/miniscript/satisfy.rs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ use crate::plan::AssetProvider;
1919
use crate::prelude::*;
2020
use crate::util::witness_size;
2121
use crate::{
22-
AbsLockTime, Miniscript, MiniscriptKey, RelLockTime, ScriptContext, Terminal, ToPublicKey,
22+
AbsLockTime, Miniscript, MiniscriptKey, RelLockTime, ScriptContext, Terminal, Threshold,
23+
ToPublicKey,
2324
};
2425

2526
/// Type alias for 32 byte Preimage.
@@ -930,8 +931,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Satisfaction<Placeholder<Pk>> {
930931

931932
// produce a non-malleable satisafaction for thesh frag
932933
fn thresh<Ctx, Sat, F>(
933-
k: usize,
934-
subs: &[Arc<Miniscript<Pk, Ctx>>],
934+
thresh: &Threshold<Arc<Miniscript<Pk, Ctx>>, 0>,
935935
stfr: &Sat,
936936
root_has_sig: bool,
937937
leaf_hash: &TapLeafHash,
@@ -945,7 +945,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Satisfaction<Placeholder<Pk>> {
945945
Satisfaction<Placeholder<Pk>>,
946946
) -> Satisfaction<Placeholder<Pk>>,
947947
{
948-
let mut sats = subs
948+
let mut sats = thresh
949949
.iter()
950950
.map(|s| {
951951
Self::satisfy_helper(
@@ -959,7 +959,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Satisfaction<Placeholder<Pk>> {
959959
})
960960
.collect::<Vec<_>>();
961961
// Start with the to-return stack set to all dissatisfactions
962-
let mut ret_stack = subs
962+
let mut ret_stack = thresh
963963
.iter()
964964
.map(|s| {
965965
Self::dissatisfy_helper(
@@ -976,7 +976,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Satisfaction<Placeholder<Pk>> {
976976
// Sort everything by (sat cost - dissat cost), except that
977977
// satisfactions without signatures beat satisfactions with
978978
// signatures
979-
let mut sat_indices = (0..subs.len()).collect::<Vec<_>>();
979+
let mut sat_indices = (0..thresh.n()).collect::<Vec<_>>();
980980
sat_indices.sort_by_key(|&i| {
981981
let stack_weight = match (&sats[i].stack, &ret_stack[i].stack) {
982982
(&Witness::Unavailable, _) | (&Witness::Impossible, _) => i64::MAX,
@@ -995,7 +995,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Satisfaction<Placeholder<Pk>> {
995995
(is_impossible, sats[i].has_sig, stack_weight)
996996
});
997997

998-
for i in 0..k {
998+
for i in 0..thresh.k() {
999999
mem::swap(&mut ret_stack[sat_indices[i]], &mut sats[sat_indices[i]]);
10001000
}
10011001

@@ -1004,8 +1004,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Satisfaction<Placeholder<Pk>> {
10041004
// then the threshold branch is impossible to satisfy
10051005
// For example, the fragment thresh(2, hash, 0, 0, 0)
10061006
// is has an impossible witness
1007-
assert!(k > 0);
1008-
if sats[sat_indices[k - 1]].stack == Witness::Impossible {
1007+
if sats[sat_indices[thresh.k() - 1]].stack == Witness::Impossible {
10091008
Satisfaction {
10101009
stack: Witness::Impossible,
10111010
// If the witness is impossible, we don't care about the
@@ -1024,9 +1023,8 @@ impl<Pk: MiniscriptKey + ToPublicKey> Satisfaction<Placeholder<Pk>> {
10241023
// For example, the fragment thresh(2, hash, hash, 0, 0)
10251024
// is uniquely satisfyiable because there is no satisfaction
10261025
// for the 0 fragment
1027-
else if k < sat_indices.len()
1028-
&& !sats[sat_indices[k]].has_sig
1029-
&& sats[sat_indices[k]].stack != Witness::Impossible
1026+
else if !sats[sat_indices[thresh.k()]].has_sig
1027+
&& sats[sat_indices[thresh.k()]].stack != Witness::Impossible
10301028
{
10311029
// All arguments should be `d`, so dissatisfactions have no
10321030
// signatures; and in this branch we assume too many weak
@@ -1062,8 +1060,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Satisfaction<Placeholder<Pk>> {
10621060

10631061
// produce a possily malleable satisafaction for thesh frag
10641062
fn thresh_mall<Ctx, Sat, F>(
1065-
k: usize,
1066-
subs: &[Arc<Miniscript<Pk, Ctx>>],
1063+
thresh: &Threshold<Arc<Miniscript<Pk, Ctx>>, 0>,
10671064
stfr: &Sat,
10681065
root_has_sig: bool,
10691066
leaf_hash: &TapLeafHash,
@@ -1077,7 +1074,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Satisfaction<Placeholder<Pk>> {
10771074
Satisfaction<Placeholder<Pk>>,
10781075
) -> Satisfaction<Placeholder<Pk>>,
10791076
{
1080-
let mut sats = subs
1077+
let mut sats = thresh
10811078
.iter()
10821079
.map(|s| {
10831080
Self::satisfy_helper(
@@ -1091,7 +1088,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Satisfaction<Placeholder<Pk>> {
10911088
})
10921089
.collect::<Vec<_>>();
10931090
// Start with the to-return stack set to all dissatisfactions
1094-
let mut ret_stack = subs
1091+
let mut ret_stack = thresh
10951092
.iter()
10961093
.map(|s| {
10971094
Self::dissatisfy_helper(
@@ -1108,7 +1105,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Satisfaction<Placeholder<Pk>> {
11081105
// Sort everything by (sat cost - dissat cost), except that
11091106
// satisfactions without signatures beat satisfactions with
11101107
// signatures
1111-
let mut sat_indices = (0..subs.len()).collect::<Vec<_>>();
1108+
let mut sat_indices = (0..thresh.n()).collect::<Vec<_>>();
11121109
sat_indices.sort_by_key(|&i| {
11131110
// For malleable satifactions, directly choose smallest weights
11141111
match (&sats[i].stack, &ret_stack[i].stack) {
@@ -1122,7 +1119,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Satisfaction<Placeholder<Pk>> {
11221119
});
11231120

11241121
// swap the satisfactions
1125-
for i in 0..k {
1122+
for i in 0..thresh.k() {
11261123
mem::swap(&mut ret_stack[sat_indices[i]], &mut sats[sat_indices[i]]);
11271124
}
11281125

@@ -1236,8 +1233,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Satisfaction<Placeholder<Pk>> {
12361233
Satisfaction<Placeholder<Pk>>,
12371234
) -> Satisfaction<Placeholder<Pk>>,
12381235
G: FnMut(
1239-
usize,
1240-
&[Arc<Miniscript<Pk, Ctx>>],
1236+
&Threshold<Arc<Miniscript<Pk, Ctx>>, 0>,
12411237
&Sat,
12421238
bool,
12431239
&TapLeafHash,
@@ -1496,7 +1492,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Satisfaction<Placeholder<Pk>> {
14961492
)
14971493
}
14981494
Terminal::Thresh(ref thresh) => {
1499-
thresh_fn(thresh.k(), thresh.data(), stfr, root_has_sig, leaf_hash, min_fn)
1495+
thresh_fn(thresh, stfr, root_has_sig, leaf_hash, min_fn)
15001496
}
15011497
Terminal::Multi(ref thresh) => {
15021498
// Collect all available signatures
@@ -1606,8 +1602,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Satisfaction<Placeholder<Pk>> {
16061602
Satisfaction<Placeholder<Pk>>,
16071603
) -> Satisfaction<Placeholder<Pk>>,
16081604
G: FnMut(
1609-
usize,
1610-
&[Arc<Miniscript<Pk, Ctx>>],
1605+
&Threshold<Arc<Miniscript<Pk, Ctx>>, 0>,
16111606
&Sat,
16121607
bool,
16131608
&TapLeafHash,

0 commit comments

Comments
 (0)