@@ -22,6 +22,7 @@ use std::convert::From;
2222use std:: marker:: PhantomData ;
2323use std:: { cmp, error, f64, fmt, mem} ;
2424
25+ use miniscript:: context:: SigType ;
2526use miniscript:: limits:: MAX_PUBKEYS_PER_MULTISIG ;
2627use miniscript:: types:: { self , ErrorKind , ExtData , Property , Type } ;
2728use miniscript:: ScriptContext ;
@@ -167,19 +168,30 @@ impl Property for CompilerExtData {
167168 }
168169 }
169170
170- fn from_pk_k ( ) -> Self {
171+ fn from_pk_k < Ctx : ScriptContext > ( ) -> Self {
171172 CompilerExtData {
172173 branch_prob : None ,
173- sat_cost : 73.0 ,
174+ sat_cost : match Ctx :: sig_type ( ) {
175+ SigType :: Ecdsa => 73.0 ,
176+ SigType :: Schnorr => 1.0 /* <var_int> */ + 64.0 /* sig */ + 1.0 , /* <sighash_type> */
177+ } ,
174178 dissat_cost : Some ( 1.0 ) ,
175179 }
176180 }
177181
178- fn from_pk_h ( ) -> Self {
182+ fn from_pk_h < Ctx : ScriptContext > ( ) -> Self {
179183 CompilerExtData {
180184 branch_prob : None ,
181- sat_cost : 73.0 + 34.0 ,
182- dissat_cost : Some ( 1.0 + 34.0 ) ,
185+ sat_cost : match Ctx :: sig_type ( ) {
186+ SigType :: Ecdsa => 73.0 + 34.0 ,
187+ SigType :: Schnorr => 66.0 + 33.0 ,
188+ } ,
189+ dissat_cost : Some (
190+ 1.0 + match Ctx :: sig_type ( ) {
191+ SigType :: Ecdsa => 34.0 ,
192+ SigType :: Schnorr => 33.0 ,
193+ } ,
194+ ) ,
183195 }
184196 }
185197
@@ -191,6 +203,14 @@ impl Property for CompilerExtData {
191203 }
192204 }
193205
206+ fn from_multi_a ( k : usize , n : usize ) -> Self {
207+ CompilerExtData {
208+ branch_prob : None ,
209+ sat_cost : 66.0 * k as f64 + ( n - k) as f64 ,
210+ dissat_cost : Some ( n as f64 ) , /* <w_n> ... <w_1> := 0x00 ... 0x00 (n times) */
211+ }
212+ }
213+
194214 fn from_hash ( ) -> Self {
195215 CompilerExtData {
196216 branch_prob : None ,
@@ -311,6 +331,14 @@ impl Property for CompilerExtData {
311331 } )
312332 }
313333
334+ fn and_n ( a : Self , b : Self ) -> Result < Self , types:: ErrorKind > {
335+ Ok ( CompilerExtData {
336+ branch_prob : None ,
337+ sat_cost : a. sat_cost + b. sat_cost ,
338+ dissat_cost : a. dissat_cost ,
339+ } )
340+ }
341+
314342 fn or_b ( l : Self , r : Self ) -> Result < Self , types:: ErrorKind > {
315343 let lprob = l
316344 . branch_prob
@@ -403,14 +431,6 @@ impl Property for CompilerExtData {
403431 } )
404432 }
405433
406- fn and_n ( a : Self , b : Self ) -> Result < Self , types:: ErrorKind > {
407- Ok ( CompilerExtData {
408- branch_prob : None ,
409- sat_cost : a. sat_cost + b. sat_cost ,
410- dissat_cost : a. dissat_cost ,
411- } )
412- }
413-
414434 fn threshold < S > ( k : usize , n : usize , mut sub_ck : S ) -> Result < Self , types:: ErrorKind >
415435 where
416436 S : FnMut ( usize ) -> Result < Self , types:: ErrorKind > ,
@@ -1226,6 +1246,7 @@ mod tests {
12261246 . unwrap ( ) ;
12271247 assert ! ( pol. compile:: <Segwitv0 >( ) . is_ok( ) ) ;
12281248 }
1249+
12291250 #[ test]
12301251 fn compile_basic ( ) {
12311252 assert ! ( policy_compile_lift_check( "pk(A)" ) . is_ok( ) ) ;
@@ -1272,8 +1293,8 @@ mod tests {
12721293 ) ;
12731294
12741295 let policy = SPolicy :: from_str (
1275- "and(and(and(or(127@thresh(2,pk(A),pk(B),thresh(2,or(127@pk(A),1@pk(B)),after(100),or(and(pk(C),after(200)),and(pk(D),sha256(66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925))),pk(E))),1@pk(F)),sha256(66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925)),or(127@pk(G),1@after(300))),or(127@after(400),pk(H)))"
1276- ) . expect ( "parsing" ) ;
1296+ "and(and(and(or(127@thresh(2,pk(A),pk(B),thresh(2,or(127@pk(A),1@pk(B)),after(100),or(and(pk(C),after(200)),and(pk(D),sha256(66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925))),pk(E))),1@pk(F)),sha256(66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925)),or(127@pk(G),1@after(300))),or(127@after(400),pk(H)))"
1297+ ) . expect ( "parsing" ) ;
12771298 let compilation: DummySegwitAstElemExt =
12781299 best_t ( & mut BTreeMap :: new ( ) , & policy, 1.0 , None ) . unwrap ( ) ;
12791300
@@ -1565,15 +1586,17 @@ mod benches {
15651586 use super :: { CompilerError , Concrete } ;
15661587 use miniscript:: Segwitv0 ;
15671588 use Miniscript ;
1589+
15681590 type SegwitMsRes = Result < Miniscript < String , Segwitv0 > , CompilerError > ;
1591+
15691592 #[ bench]
15701593 pub fn compile_basic ( bh : & mut Bencher ) {
15711594 let h = ( 0 ..64 ) . map ( |_| "a" ) . collect :: < String > ( ) ;
15721595 let pol = Concrete :: < String > :: from_str ( & format ! (
15731596 "and(thresh(2,and(sha256({}),or(sha256({}),pk(A))),pk(B),pk(C),pk(D),sha256({})),pk(E))" ,
15741597 h, h, h
15751598 ) )
1576- . expect ( "parsing" ) ;
1599+ . expect ( "parsing" ) ;
15771600 bh. iter ( || {
15781601 let pt: SegwitMsRes = pol. compile ( ) ;
15791602 black_box ( pt) . unwrap ( ) ;
0 commit comments