@@ -22,6 +22,7 @@ use std::convert::From;
22
22
use std:: marker:: PhantomData ;
23
23
use std:: { cmp, error, f64, fmt, mem} ;
24
24
25
+ use miniscript:: context:: SigType ;
25
26
use miniscript:: limits:: MAX_PUBKEYS_PER_MULTISIG ;
26
27
use miniscript:: types:: { self , ErrorKind , ExtData , Property , Type } ;
27
28
use miniscript:: ScriptContext ;
@@ -136,7 +137,7 @@ impl CompilationKey {
136
137
}
137
138
138
139
#[ derive( Copy , Clone , Debug ) ]
139
- struct CompilerExtData {
140
+ struct CompilerExtData < Ctx : ScriptContext > {
140
141
/// If this node is the direct child of a disjunction, this field must
141
142
/// have the probability of its branch being taken. Otherwise it is ignored.
142
143
/// All functions initialize it to `None`.
@@ -148,14 +149,18 @@ struct CompilerExtData {
148
149
/// (total length of all witness pushes, plus their own length prefixes)
149
150
/// for fragments that can be dissatisfied without failing the script.
150
151
dissat_cost : Option < f64 > ,
152
+ /// Signature Type for deciding the number of bytes required for [`CompilerExtData::sat_cost`]
153
+ /// and [`CompilerExtData::dissat_cost`] for the given script.
154
+ sig_type : PhantomData < Ctx > ,
151
155
}
152
156
153
- impl Property for CompilerExtData {
157
+ impl < Ctx : ScriptContext > Property for CompilerExtData < Ctx > {
154
158
fn from_true ( ) -> Self {
155
159
CompilerExtData {
156
160
branch_prob : None ,
157
161
sat_cost : 0.0 ,
158
162
dissat_cost : None ,
163
+ sig_type : PhantomData ,
159
164
}
160
165
}
161
166
@@ -164,38 +169,64 @@ impl Property for CompilerExtData {
164
169
branch_prob : None ,
165
170
sat_cost : f64:: MAX ,
166
171
dissat_cost : Some ( 0.0 ) ,
172
+ sig_type : PhantomData ,
167
173
}
168
174
}
169
175
170
176
fn from_pk_k ( ) -> Self {
171
177
CompilerExtData {
172
178
branch_prob : None ,
173
- sat_cost : 73.0 ,
179
+ sat_cost : match Ctx :: sig_type ( ) {
180
+ SigType :: Ecdsa => 73. ,
181
+ SigType :: Schnorr => 64. ,
182
+ } ,
174
183
dissat_cost : Some ( 1.0 ) ,
184
+ sig_type : PhantomData ,
175
185
}
176
186
}
177
187
178
188
fn from_pk_h ( ) -> Self {
179
189
CompilerExtData {
180
190
branch_prob : None ,
181
- sat_cost : 73.0 + 34.0 ,
182
- dissat_cost : Some ( 1.0 + 34.0 ) ,
191
+ sat_cost : match Ctx :: sig_type ( ) {
192
+ SigType :: Ecdsa => 73.0 + 34.0 ,
193
+ SigType :: Schnorr => 64. + 32. ,
194
+ } ,
195
+ dissat_cost : Some (
196
+ 1.0 + match Ctx :: sig_type ( ) {
197
+ SigType :: Ecdsa => 34.0 ,
198
+ SigType :: Schnorr => 32. ,
199
+ } ,
200
+ ) ,
201
+ sig_type : PhantomData ,
183
202
}
184
203
}
185
204
186
205
fn from_multi ( k : usize , _n : usize ) -> Self {
187
206
CompilerExtData {
188
207
branch_prob : None ,
189
- sat_cost : 1.0 + 73.0 * k as f64 ,
208
+ sat_cost : 1.0
209
+ + match Ctx :: sig_type ( ) {
210
+ SigType :: Ecdsa => 73.0 * k as f64 ,
211
+ SigType :: Schnorr => 64. * k as f64 ,
212
+ } ,
190
213
dissat_cost : Some ( 1.0 * ( k + 1 ) as f64 ) ,
214
+ sig_type : PhantomData ,
191
215
}
192
216
}
193
217
194
218
fn from_hash ( ) -> Self {
195
219
CompilerExtData {
196
220
branch_prob : None ,
197
- sat_cost : 33.0 ,
198
- dissat_cost : Some ( 33.0 ) ,
221
+ sat_cost : match Ctx :: sig_type ( ) {
222
+ SigType :: Ecdsa => 33.0 ,
223
+ SigType :: Schnorr => 32. ,
224
+ } ,
225
+ dissat_cost : Some ( match Ctx :: sig_type ( ) {
226
+ SigType :: Ecdsa => 33.0 ,
227
+ SigType :: Schnorr => 32. ,
228
+ } ) ,
229
+ sig_type : PhantomData ,
199
230
}
200
231
}
201
232
@@ -204,6 +235,7 @@ impl Property for CompilerExtData {
204
235
branch_prob : None ,
205
236
sat_cost : 0.0 ,
206
237
dissat_cost : None ,
238
+ sig_type : PhantomData ,
207
239
}
208
240
}
209
241
@@ -212,6 +244,7 @@ impl Property for CompilerExtData {
212
244
branch_prob : None ,
213
245
sat_cost : self . sat_cost ,
214
246
dissat_cost : self . dissat_cost ,
247
+ sig_type : PhantomData ,
215
248
} )
216
249
}
217
250
@@ -220,6 +253,7 @@ impl Property for CompilerExtData {
220
253
branch_prob : None ,
221
254
sat_cost : self . sat_cost ,
222
255
dissat_cost : self . dissat_cost ,
256
+ sig_type : PhantomData ,
223
257
} )
224
258
}
225
259
@@ -228,6 +262,7 @@ impl Property for CompilerExtData {
228
262
branch_prob : None ,
229
263
sat_cost : self . sat_cost ,
230
264
dissat_cost : self . dissat_cost ,
265
+ sig_type : PhantomData ,
231
266
} )
232
267
}
233
268
@@ -236,6 +271,7 @@ impl Property for CompilerExtData {
236
271
branch_prob : None ,
237
272
sat_cost : 2.0 + self . sat_cost ,
238
273
dissat_cost : Some ( 1.0 ) ,
274
+ sig_type : PhantomData ,
239
275
} )
240
276
}
241
277
@@ -244,6 +280,7 @@ impl Property for CompilerExtData {
244
280
branch_prob : None ,
245
281
sat_cost : self . sat_cost ,
246
282
dissat_cost : None ,
283
+ sig_type : PhantomData ,
247
284
} )
248
285
}
249
286
@@ -252,6 +289,7 @@ impl Property for CompilerExtData {
252
289
branch_prob : None ,
253
290
sat_cost : self . sat_cost ,
254
291
dissat_cost : Some ( 1.0 ) ,
292
+ sig_type : PhantomData ,
255
293
} )
256
294
}
257
295
@@ -260,6 +298,7 @@ impl Property for CompilerExtData {
260
298
branch_prob : None ,
261
299
sat_cost : self . sat_cost ,
262
300
dissat_cost : self . dissat_cost ,
301
+ sig_type : PhantomData ,
263
302
} )
264
303
}
265
304
@@ -268,6 +307,7 @@ impl Property for CompilerExtData {
268
307
branch_prob : None ,
269
308
sat_cost : self . sat_cost ,
270
309
dissat_cost : None ,
310
+ sig_type : PhantomData ,
271
311
} )
272
312
}
273
313
@@ -281,6 +321,7 @@ impl Property for CompilerExtData {
281
321
branch_prob : None ,
282
322
sat_cost : 2.0 + self . sat_cost ,
283
323
dissat_cost : Some ( 1.0 ) ,
324
+ sig_type : PhantomData ,
284
325
} )
285
326
}
286
327
@@ -289,6 +330,7 @@ impl Property for CompilerExtData {
289
330
branch_prob : None ,
290
331
sat_cost : 1.0 + self . sat_cost ,
291
332
dissat_cost : Some ( 2.0 ) ,
333
+ sig_type : PhantomData ,
292
334
} )
293
335
}
294
336
@@ -300,6 +342,7 @@ impl Property for CompilerExtData {
300
342
( Some ( l) , Some ( r) ) => Some ( l + r) ,
301
343
_ => None ,
302
344
} ,
345
+ sig_type : PhantomData ,
303
346
} )
304
347
}
305
348
@@ -308,6 +351,16 @@ impl Property for CompilerExtData {
308
351
branch_prob : None ,
309
352
sat_cost : left. sat_cost + right. sat_cost ,
310
353
dissat_cost : None ,
354
+ sig_type : PhantomData ,
355
+ } )
356
+ }
357
+
358
+ fn and_n ( a : Self , b : Self ) -> Result < Self , types:: ErrorKind > {
359
+ Ok ( CompilerExtData {
360
+ branch_prob : None ,
361
+ sat_cost : a. sat_cost + b. sat_cost ,
362
+ dissat_cost : a. dissat_cost ,
363
+ sig_type : PhantomData ,
311
364
} )
312
365
}
313
366
@@ -323,6 +376,7 @@ impl Property for CompilerExtData {
323
376
sat_cost : lprob * ( l. sat_cost + r. dissat_cost . unwrap ( ) )
324
377
+ rprob * ( r. sat_cost + l. dissat_cost . unwrap ( ) ) ,
325
378
dissat_cost : Some ( l. dissat_cost . unwrap ( ) + r. dissat_cost . unwrap ( ) ) ,
379
+ sig_type : PhantomData ,
326
380
} )
327
381
}
328
382
@@ -337,6 +391,7 @@ impl Property for CompilerExtData {
337
391
branch_prob : None ,
338
392
sat_cost : lprob * l. sat_cost + rprob * ( r. sat_cost + l. dissat_cost . unwrap ( ) ) ,
339
393
dissat_cost : r. dissat_cost . map ( |rd| l. dissat_cost . unwrap ( ) + rd) ,
394
+ sig_type : PhantomData ,
340
395
} )
341
396
}
342
397
@@ -351,6 +406,7 @@ impl Property for CompilerExtData {
351
406
branch_prob : None ,
352
407
sat_cost : lprob * l. sat_cost + rprob * ( r. sat_cost + l. dissat_cost . unwrap ( ) ) ,
353
408
dissat_cost : None ,
409
+ sig_type : PhantomData ,
354
410
} )
355
411
}
356
412
@@ -377,6 +433,7 @@ impl Property for CompilerExtData {
377
433
} else {
378
434
None
379
435
} ,
436
+ sig_type : PhantomData ,
380
437
} )
381
438
}
382
439
@@ -400,14 +457,7 @@ impl Property for CompilerExtData {
400
457
} else {
401
458
None
402
459
} ,
403
- } )
404
- }
405
-
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 ,
460
+ sig_type : PhantomData ,
411
461
} )
412
462
}
413
463
@@ -427,6 +477,7 @@ impl Property for CompilerExtData {
427
477
branch_prob : None ,
428
478
sat_cost : sat_cost * k_over_n + dissat_cost * ( 1.0 - k_over_n) ,
429
479
dissat_cost : Some ( dissat_cost) ,
480
+ sig_type : PhantomData ,
430
481
} )
431
482
}
432
483
}
@@ -437,7 +488,7 @@ struct AstElemExt<Pk: MiniscriptKey, Ctx: ScriptContext> {
437
488
/// The actual Miniscript fragment with type information
438
489
ms : Arc < Miniscript < Pk , Ctx > > ,
439
490
/// Its "type" in terms of compiler data
440
- comp_ext_data : CompilerExtData ,
491
+ comp_ext_data : CompilerExtData < Ctx > ,
441
492
}
442
493
443
494
impl < Pk : MiniscriptKey , Ctx : ScriptContext > AstElemExt < Pk , Ctx > {
@@ -470,8 +521,8 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> AstElemExt<Pk, Ctx> {
470
521
r : & AstElemExt < Pk , Ctx > ,
471
522
) -> Result < AstElemExt < Pk , Ctx > , types:: Error < Pk , Ctx > > {
472
523
let lookup_ext = |n| match n {
473
- 0 => Some ( l. comp_ext_data ) ,
474
- 1 => Some ( r. comp_ext_data ) ,
524
+ 0 => Some ( l. comp_ext_data . clone ( ) ) ,
525
+ 1 => Some ( r. comp_ext_data . clone ( ) ) ,
475
526
_ => unreachable ! ( ) ,
476
527
} ;
477
528
//Types and ExtData are already cached and stored in children. So, we can
@@ -497,9 +548,9 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> AstElemExt<Pk, Ctx> {
497
548
c : & AstElemExt < Pk , Ctx > ,
498
549
) -> Result < AstElemExt < Pk , Ctx > , types:: Error < Pk , Ctx > > {
499
550
let lookup_ext = |n| match n {
500
- 0 => Some ( a. comp_ext_data ) ,
501
- 1 => Some ( b. comp_ext_data ) ,
502
- 2 => Some ( c. comp_ext_data ) ,
551
+ 0 => Some ( a. comp_ext_data . clone ( ) ) ,
552
+ 1 => Some ( b. comp_ext_data . clone ( ) ) ,
553
+ 2 => Some ( c. comp_ext_data . clone ( ) ) ,
503
554
_ => unreachable ! ( ) ,
504
555
} ;
505
556
//Types and ExtData are already cached and stored in children. So, we can
@@ -525,7 +576,7 @@ struct Cast<Pk: MiniscriptKey, Ctx: ScriptContext> {
525
576
node : fn ( Arc < Miniscript < Pk , Ctx > > ) -> Terminal < Pk , Ctx > ,
526
577
ast_type : fn ( types:: Type ) -> Result < types:: Type , ErrorKind > ,
527
578
ext_data : fn ( types:: ExtData ) -> Result < types:: ExtData , ErrorKind > ,
528
- comp_ext_data : fn ( CompilerExtData ) -> Result < CompilerExtData , types:: ErrorKind > ,
579
+ comp_ext_data : fn ( CompilerExtData < Ctx > ) -> Result < CompilerExtData < Ctx > , types:: ErrorKind > ,
529
580
}
530
581
531
582
impl < Pk : MiniscriptKey , Ctx : ScriptContext > Cast < Pk , Ctx > {
@@ -537,7 +588,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Cast<Pk, Ctx> {
537
588
node : ( self . node ) ( Arc :: clone ( & ast. ms ) ) ,
538
589
phantom : PhantomData ,
539
590
} ) ,
540
- comp_ext_data : ( self . comp_ext_data ) ( ast. comp_ext_data ) ?,
591
+ comp_ext_data : ( self . comp_ext_data ) ( ast. comp_ext_data . clone ( ) ) ?,
541
592
} )
542
593
}
543
594
}
@@ -955,19 +1006,19 @@ where
955
1006
let bw = best ( types:: Base :: W , policy_cache, ast, sp, dp) ?;
956
1007
957
1008
let diff = be. cost_1d ( sp, dp) - bw. cost_1d ( sp, dp) ;
958
- best_es. push ( ( be. comp_ext_data , be) ) ;
959
- best_ws. push ( ( bw. comp_ext_data , bw) ) ;
1009
+ best_es. push ( ( be. comp_ext_data . clone ( ) , be) ) ;
1010
+ best_ws. push ( ( bw. comp_ext_data . clone ( ) , bw) ) ;
960
1011
961
1012
if diff < min_value. 1 {
962
1013
min_value. 0 = i;
963
1014
min_value. 1 = diff;
964
1015
}
965
1016
}
966
- sub_ext_data. push ( best_es[ min_value. 0 ] . 0 ) ;
1017
+ sub_ext_data. push ( best_es[ min_value. 0 ] . 0 . clone ( ) ) ;
967
1018
sub_ast. push ( Arc :: clone ( & best_es[ min_value. 0 ] . 1 . ms ) ) ;
968
1019
for ( i, _ast) in subs. iter ( ) . enumerate ( ) {
969
1020
if i != min_value. 0 {
970
- sub_ext_data. push ( best_ws[ i] . 0 ) ;
1021
+ sub_ext_data. push ( best_ws[ i] . 0 . clone ( ) ) ;
971
1022
sub_ast. push ( Arc :: clone ( & best_ws[ i] . 1 . ms ) ) ;
972
1023
}
973
1024
}
@@ -978,7 +1029,7 @@ where
978
1029
Miniscript :: from_ast ( ast)
979
1030
. expect ( "threshold subs, which we just compiled, typeck" ) ,
980
1031
) ,
981
- comp_ext_data : CompilerExtData :: threshold ( k, n, |i| Ok ( sub_ext_data[ i] ) )
1032
+ comp_ext_data : CompilerExtData :: threshold ( k, n, |i| Ok ( sub_ext_data[ i] . clone ( ) ) )
982
1033
. expect ( "threshold subs, which we just compiled, typeck" ) ,
983
1034
} ;
984
1035
insert_wrap ! ( ast_ext) ;
0 commit comments