@@ -1008,8 +1008,9 @@ where
1008
1008
compile_binary ! ( & mut l_comp[ 3 ] , & mut r_comp[ 2 ] , [ lw, rw] , Terminal :: OrI ) ;
1009
1009
compile_binary ! ( & mut r_comp[ 3 ] , & mut l_comp[ 2 ] , [ rw, lw] , Terminal :: OrI ) ;
1010
1010
}
1011
- Concrete :: Thresh ( k, ref subs) => {
1012
- let n = subs. len ( ) ;
1011
+ Concrete :: Thresh ( ref thresh) => {
1012
+ let k = thresh. k ( ) ;
1013
+ let n = thresh. n ( ) ;
1013
1014
let k_over_n = k as f64 / n as f64 ;
1014
1015
1015
1016
let mut sub_ast = Vec :: with_capacity ( n) ;
@@ -1019,7 +1020,7 @@ where
1019
1020
let mut best_ws = Vec :: with_capacity ( n) ;
1020
1021
1021
1022
let mut min_value = ( 0 , f64:: INFINITY ) ;
1022
- for ( i, ast) in subs . iter ( ) . enumerate ( ) {
1023
+ for ( i, ast) in thresh . iter ( ) . enumerate ( ) {
1023
1024
let sp = sat_prob * k_over_n;
1024
1025
//Expressions must be dissatisfiable
1025
1026
let dp = Some ( dissat_prob. unwrap_or ( 0 as f64 ) + ( 1.0 - k_over_n) * sat_prob) ;
@@ -1037,7 +1038,7 @@ where
1037
1038
}
1038
1039
sub_ext_data. push ( best_es[ min_value. 0 ] . 0 ) ;
1039
1040
sub_ast. push ( Arc :: clone ( & best_es[ min_value. 0 ] . 1 . ms ) ) ;
1040
- for ( i, _ast) in subs . iter ( ) . enumerate ( ) {
1041
+ for ( i, _ast) in thresh . iter ( ) . enumerate ( ) {
1041
1042
if i != min_value. 0 {
1042
1043
sub_ext_data. push ( best_ws[ i] . 0 ) ;
1043
1044
sub_ast. push ( Arc :: clone ( & best_ws[ i] . 1 . ms ) ) ;
@@ -1054,7 +1055,7 @@ where
1054
1055
insert_wrap ! ( ast_ext) ;
1055
1056
}
1056
1057
1057
- let key_vec: Vec < Pk > = subs
1058
+ let key_vec: Vec < Pk > = thresh
1058
1059
. iter ( )
1059
1060
. filter_map ( |s| {
1060
1061
if let Concrete :: Key ( ref pk) = s. as_ref ( ) {
@@ -1065,7 +1066,7 @@ where
1065
1066
} )
1066
1067
. collect ( ) ;
1067
1068
1068
- if key_vec. len ( ) == subs . len ( ) {
1069
+ if key_vec. len ( ) == thresh . n ( ) {
1069
1070
match Ctx :: sig_type ( ) {
1070
1071
SigType :: Schnorr => {
1071
1072
if key_vec. len ( ) <= MAX_PUBKEYS_IN_CHECKSIGADD {
@@ -1079,8 +1080,8 @@ where
1079
1080
}
1080
1081
}
1081
1082
}
1082
- if k == subs . len ( ) {
1083
- let mut it = subs . iter ( ) ;
1083
+ if k == thresh . n ( ) {
1084
+ let mut it = thresh . iter ( ) ;
1084
1085
let mut policy = it. next ( ) . expect ( "No sub policy in thresh() ?" ) . clone ( ) ;
1085
1086
policy = it. fold ( policy, |acc, pol| Concrete :: And ( vec ! [ acc, pol. clone( ) ] ) . into ( ) ) ;
1086
1087
@@ -1247,7 +1248,7 @@ mod tests {
1247
1248
use super :: * ;
1248
1249
use crate :: miniscript:: { Legacy , Segwitv0 , Tap } ;
1249
1250
use crate :: policy:: Liftable ;
1250
- use crate :: { script_num_size, AbsLockTime , RelLockTime , ToPublicKey } ;
1251
+ use crate :: { script_num_size, AbsLockTime , RelLockTime , Threshold , ToPublicKey } ;
1251
1252
1252
1253
type SPolicy = Concrete < String > ;
1253
1254
type BPolicy = Concrete < bitcoin:: PublicKey > ;
@@ -1393,17 +1394,17 @@ mod tests {
1393
1394
(
1394
1395
127 ,
1395
1396
Arc :: new( Concrete :: Thresh (
1396
- 3 ,
1397
- key_pol [ 0 .. 5 ] . iter ( ) . map ( |p| ( p . clone ( ) ) . into ( ) ) . collect ( ) ,
1397
+ Threshold :: from_iter ( 3 , key_pol [ 0 .. 5 ] . iter ( ) . map ( |p| ( p . clone ( ) ) . into ( ) ) )
1398
+ . unwrap ( ) ,
1398
1399
) ) ,
1399
1400
) ,
1400
1401
(
1401
1402
1 ,
1402
1403
Arc :: new( Concrete :: And ( vec![
1403
1404
Arc :: new( Concrete :: Older ( RelLockTime :: from_height( 10000 ) ) ) ,
1404
1405
Arc :: new( Concrete :: Thresh (
1405
- 2 ,
1406
- key_pol [ 5 .. 8 ] . iter ( ) . map ( |p| ( p . clone ( ) ) . into ( ) ) . collect ( ) ,
1406
+ Threshold :: from_iter ( 2 , key_pol [ 5 .. 8 ] . iter ( ) . map ( |p| ( p . clone ( ) ) . into ( ) ) )
1407
+ . unwrap ( ) ,
1407
1408
) ) ,
1408
1409
] ) ) ,
1409
1410
) ,
@@ -1524,11 +1525,12 @@ mod tests {
1524
1525
// and to a ms thresh otherwise.
1525
1526
// k = 1 (or 2) does not compile, see https://github.com/rust-bitcoin/rust-miniscript/issues/114
1526
1527
for k in & [ 10 , 15 , 21 ] {
1527
- let pubkeys: Vec < Arc < Concrete < bitcoin:: PublicKey > > > = keys
1528
- . iter ( )
1529
- . map ( |pubkey| Arc :: new ( Concrete :: Key ( * pubkey) ) )
1530
- . collect ( ) ;
1531
- let big_thresh = Concrete :: Thresh ( * k, pubkeys) ;
1528
+ let thresh: Threshold < Arc < Concrete < bitcoin:: PublicKey > > , 0 > = Threshold :: from_iter (
1529
+ * k,
1530
+ keys. iter ( ) . map ( |pubkey| Arc :: new ( Concrete :: Key ( * pubkey) ) ) ,
1531
+ )
1532
+ . unwrap ( ) ;
1533
+ let big_thresh = Concrete :: Thresh ( thresh) ;
1532
1534
let big_thresh_ms: SegwitMiniScript = big_thresh. compile ( ) . unwrap ( ) ;
1533
1535
if * k == 21 {
1534
1536
// N * (PUSH + pubkey + CHECKSIGVERIFY)
@@ -1564,8 +1566,8 @@ mod tests {
1564
1566
. collect ( ) ;
1565
1567
1566
1568
let thresh_res: Result < SegwitMiniScript , _ > = Concrete :: Or ( vec ! [
1567
- ( 1 , Arc :: new( Concrete :: Thresh ( keys_a . len ( ) , keys_a) ) ) ,
1568
- ( 1 , Arc :: new( Concrete :: Thresh ( keys_b . len ( ) , keys_b) ) ) ,
1569
+ ( 1 , Arc :: new( Concrete :: Thresh ( Threshold :: and_n ( keys_a) ) ) ) ,
1570
+ ( 1 , Arc :: new( Concrete :: Thresh ( Threshold :: and_n ( keys_b) ) ) ) ,
1569
1571
] )
1570
1572
. compile ( ) ;
1571
1573
let script_size = thresh_res. clone ( ) . map ( |m| m. script_size ( ) ) ;
@@ -1582,7 +1584,8 @@ mod tests {
1582
1584
. iter ( )
1583
1585
. map ( |pubkey| Arc :: new ( Concrete :: Key ( * pubkey) ) )
1584
1586
. collect ( ) ;
1585
- let thresh_res: Result < SegwitMiniScript , _ > = Concrete :: Thresh ( keys. len ( ) , keys) . compile ( ) ;
1587
+ let thresh_res: Result < SegwitMiniScript , _ > =
1588
+ Concrete :: Thresh ( Threshold :: and_n ( keys) ) . compile ( ) ;
1586
1589
let n_elements = thresh_res
1587
1590
. clone ( )
1588
1591
. map ( |m| m. max_satisfaction_witness_elements ( ) ) ;
@@ -1598,12 +1601,12 @@ mod tests {
1598
1601
fn shared_limits ( ) {
1599
1602
// Test the maximum number of OPs with a 67-of-68 multisig
1600
1603
let ( keys, _) = pubkeys_and_a_sig ( 68 ) ;
1601
- let keys : Vec < Arc < Concrete < bitcoin :: PublicKey > > > = keys
1602
- . iter ( )
1603
- . map ( |pubkey| Arc :: new ( Concrete :: Key ( * pubkey) ) )
1604
- . collect ( ) ;
1605
- let thresh_res : Result < SegwitMiniScript , _ > =
1606
- Concrete :: Thresh ( keys . len ( ) - 1 , keys ) . compile ( ) ;
1604
+ let thresh = Threshold :: from_iter (
1605
+ keys . len ( ) - 1 ,
1606
+ keys . iter ( ) . map ( |pubkey| Arc :: new ( Concrete :: Key ( * pubkey) ) ) ,
1607
+ )
1608
+ . unwrap ( ) ;
1609
+ let thresh_res : Result < SegwitMiniScript , _ > = Concrete :: Thresh ( thresh ) . compile ( ) ;
1607
1610
let ops_count = thresh_res. clone ( ) . map ( |m| m. ext . ops . op_count ( ) ) ;
1608
1611
assert_eq ! (
1609
1612
thresh_res,
@@ -1613,11 +1616,13 @@ mod tests {
1613
1616
) ;
1614
1617
// For legacy too..
1615
1618
let ( keys, _) = pubkeys_and_a_sig ( 68 ) ;
1616
- let keys: Vec < Arc < Concrete < bitcoin:: PublicKey > > > = keys
1617
- . iter ( )
1618
- . map ( |pubkey| Arc :: new ( Concrete :: Key ( * pubkey) ) )
1619
- . collect ( ) ;
1620
- let thresh_res = Concrete :: Thresh ( keys. len ( ) - 1 , keys) . compile :: < Legacy > ( ) ;
1619
+ let thresh = Threshold :: from_iter (
1620
+ keys. len ( ) - 1 ,
1621
+ keys. iter ( ) . map ( |pubkey| Arc :: new ( Concrete :: Key ( * pubkey) ) ) ,
1622
+ )
1623
+ . unwrap ( ) ;
1624
+
1625
+ let thresh_res = Concrete :: Thresh ( thresh) . compile :: < Legacy > ( ) ;
1621
1626
let ops_count = thresh_res. clone ( ) . map ( |m| m. ext . ops . op_count ( ) ) ;
1622
1627
assert_eq ! (
1623
1628
thresh_res,
0 commit comments