Skip to content

Commit 6a1ceac

Browse files
committed
Fix e/o bug in miniscript threshold correctness rules
This will not affect existing users of rust-miniscript as we were previously more strict in the miniscript that we accepted. This allows more more scripts to parsed as miniscripts. This allows correctly tagging thresh z, o values as per the spec. For example, miniscript with thresh(2,older(9),older(10)) to be tagged as `z` and thresh(2,pk(),older()) as `o` Found while doing a line to line comparison with c++ codebase and spec
1 parent a2d6fff commit 6a1ceac

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/miniscript/types/correctness.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -474,15 +474,19 @@ impl Property for Correctness {
474474
})
475475
}
476476

477-
fn threshold<S>(k: usize, n: usize, mut sub_ck: S) -> Result<Self, ErrorKind>
477+
fn threshold<S>(_k: usize, n: usize, mut sub_ck: S) -> Result<Self, ErrorKind>
478478
where
479479
S: FnMut(usize) -> Result<Self, ErrorKind>,
480480
{
481-
let mut is_n = k == n;
481+
let mut num_args = 0;
482482
for i in 0..n {
483483
let subtype = sub_ck(i)?;
484+
num_args += match subtype.input {
485+
Input::Zero => 0,
486+
Input::One | Input::OneNonZero => 1,
487+
Input::Any | Input::AnyNonZero => 2, // we only check if num args is max 1
488+
};
484489
if i == 0 {
485-
is_n &= subtype.input == Input::OneNonZero || subtype.input == Input::AnyNonZero;
486490
if subtype.base != Base::B {
487491
return Err(ErrorKind::ThresholdBase(i, subtype.base));
488492
}
@@ -501,7 +505,11 @@ impl Property for Correctness {
501505

502506
Ok(Correctness {
503507
base: Base::B,
504-
input: if is_n { Input::AnyNonZero } else { Input::Any },
508+
input: match num_args {
509+
0 => Input::Zero,
510+
1 => Input::One,
511+
_ => Input::Any,
512+
},
505513
dissatisfiable: true,
506514
unit: true,
507515
})

0 commit comments

Comments
 (0)