@@ -131,6 +131,9 @@ pub struct ExtData {
131
131
/// This does **not** include initial witness elements. This element only captures
132
132
/// the additional elements that are pushed during execution.
133
133
pub exec_stack_elem_count_dissat : Option < usize > ,
134
+ /// The miniscript tree depth/height of this node.
135
+ /// Used for checking the max depth of the miniscript tree to prevent stack overflow.
136
+ pub tree_height : usize ,
134
137
}
135
138
136
139
impl Property for ExtData {
@@ -157,6 +160,7 @@ impl Property for ExtData {
157
160
timelock_info : TimelockInfo :: default ( ) ,
158
161
exec_stack_elem_count_sat : Some ( 1 ) ,
159
162
exec_stack_elem_count_dissat : None ,
163
+ tree_height : 0 ,
160
164
}
161
165
}
162
166
@@ -172,6 +176,7 @@ impl Property for ExtData {
172
176
timelock_info : TimelockInfo :: default ( ) ,
173
177
exec_stack_elem_count_sat : None ,
174
178
exec_stack_elem_count_dissat : Some ( 1 ) ,
179
+ tree_height : 0 ,
175
180
}
176
181
}
177
182
@@ -193,6 +198,7 @@ impl Property for ExtData {
193
198
timelock_info : TimelockInfo :: default ( ) ,
194
199
exec_stack_elem_count_sat : Some ( 1 ) , // pushes the pk
195
200
exec_stack_elem_count_dissat : Some ( 1 ) ,
201
+ tree_height : 0 ,
196
202
}
197
203
}
198
204
@@ -214,6 +220,7 @@ impl Property for ExtData {
214
220
timelock_info : TimelockInfo :: default ( ) ,
215
221
exec_stack_elem_count_sat : Some ( 2 ) , // dup and hash push
216
222
exec_stack_elem_count_dissat : Some ( 2 ) ,
223
+ tree_height : 0 ,
217
224
}
218
225
}
219
226
@@ -237,6 +244,7 @@ impl Property for ExtData {
237
244
timelock_info : TimelockInfo :: default ( ) ,
238
245
exec_stack_elem_count_sat : Some ( n) , // n pks
239
246
exec_stack_elem_count_dissat : Some ( n) ,
247
+ tree_height : 0 ,
240
248
}
241
249
}
242
250
@@ -259,6 +267,7 @@ impl Property for ExtData {
259
267
timelock_info : TimelockInfo :: default ( ) ,
260
268
exec_stack_elem_count_sat : Some ( 2 ) , // the two nums before num equal verify
261
269
exec_stack_elem_count_dissat : Some ( 2 ) ,
270
+ tree_height : 0 ,
262
271
}
263
272
}
264
273
@@ -279,6 +288,7 @@ impl Property for ExtData {
279
288
timelock_info : TimelockInfo :: default ( ) ,
280
289
exec_stack_elem_count_sat : Some ( 2 ) , // either size <32> or <hash256> <32 byte>
281
290
exec_stack_elem_count_dissat : Some ( 2 ) ,
291
+ tree_height : 0 ,
282
292
}
283
293
}
284
294
@@ -294,6 +304,7 @@ impl Property for ExtData {
294
304
timelock_info : TimelockInfo :: default ( ) ,
295
305
exec_stack_elem_count_sat : Some ( 2 ) , // either size <32> or <hash256> <32 byte>
296
306
exec_stack_elem_count_dissat : Some ( 2 ) ,
307
+ tree_height : 0 ,
297
308
}
298
309
}
299
310
@@ -309,6 +320,7 @@ impl Property for ExtData {
309
320
timelock_info : TimelockInfo :: default ( ) ,
310
321
exec_stack_elem_count_sat : Some ( 2 ) , // either size <32> or <hash256> <20 byte>
311
322
exec_stack_elem_count_dissat : Some ( 2 ) ,
323
+ tree_height : 0 ,
312
324
}
313
325
}
314
326
@@ -324,6 +336,7 @@ impl Property for ExtData {
324
336
timelock_info : TimelockInfo :: default ( ) ,
325
337
exec_stack_elem_count_sat : Some ( 2 ) , // either size <32> or <hash256> <20 byte>
326
338
exec_stack_elem_count_dissat : Some ( 2 ) ,
339
+ tree_height : 0 ,
327
340
}
328
341
}
329
342
@@ -347,6 +360,7 @@ impl Property for ExtData {
347
360
} ,
348
361
exec_stack_elem_count_sat : Some ( 1 ) , // <t>
349
362
exec_stack_elem_count_dissat : None ,
363
+ tree_height : 0 ,
350
364
}
351
365
}
352
366
@@ -368,6 +382,7 @@ impl Property for ExtData {
368
382
} ,
369
383
exec_stack_elem_count_sat : Some ( 1 ) , // <t>
370
384
exec_stack_elem_count_dissat : None ,
385
+ tree_height : 0 ,
371
386
}
372
387
}
373
388
@@ -383,6 +398,7 @@ impl Property for ExtData {
383
398
timelock_info : self . timelock_info ,
384
399
exec_stack_elem_count_sat : self . exec_stack_elem_count_sat ,
385
400
exec_stack_elem_count_dissat : self . exec_stack_elem_count_dissat ,
401
+ tree_height : self . tree_height + 1 ,
386
402
} )
387
403
}
388
404
@@ -398,6 +414,7 @@ impl Property for ExtData {
398
414
timelock_info : self . timelock_info ,
399
415
exec_stack_elem_count_sat : self . exec_stack_elem_count_sat ,
400
416
exec_stack_elem_count_dissat : self . exec_stack_elem_count_dissat ,
417
+ tree_height : self . tree_height + 1 ,
401
418
} )
402
419
}
403
420
@@ -413,6 +430,7 @@ impl Property for ExtData {
413
430
timelock_info : self . timelock_info ,
414
431
exec_stack_elem_count_sat : self . exec_stack_elem_count_sat ,
415
432
exec_stack_elem_count_dissat : self . exec_stack_elem_count_dissat ,
433
+ tree_height : self . tree_height + 1 ,
416
434
} )
417
435
}
418
436
@@ -431,6 +449,7 @@ impl Property for ExtData {
431
449
// Even all V types push something onto the stack and then remove them
432
450
exec_stack_elem_count_sat : self . exec_stack_elem_count_sat ,
433
451
exec_stack_elem_count_dissat : Some ( 1 ) ,
452
+ tree_height : self . tree_height + 1 ,
434
453
} )
435
454
}
436
455
@@ -447,6 +466,7 @@ impl Property for ExtData {
447
466
timelock_info : self . timelock_info ,
448
467
exec_stack_elem_count_sat : self . exec_stack_elem_count_sat ,
449
468
exec_stack_elem_count_dissat : None ,
469
+ tree_height : self . tree_height + 1 ,
450
470
} )
451
471
}
452
472
@@ -462,6 +482,7 @@ impl Property for ExtData {
462
482
timelock_info : self . timelock_info ,
463
483
exec_stack_elem_count_sat : self . exec_stack_elem_count_sat ,
464
484
exec_stack_elem_count_dissat : Some ( 1 ) ,
485
+ tree_height : self . tree_height + 1 ,
465
486
} )
466
487
}
467
488
@@ -478,6 +499,7 @@ impl Property for ExtData {
478
499
// Technically max(1, self.exec_stack_elem_count_sat), same rationale as cast_dupif
479
500
exec_stack_elem_count_sat : self . exec_stack_elem_count_sat ,
480
501
exec_stack_elem_count_dissat : self . exec_stack_elem_count_dissat ,
502
+ tree_height : self . tree_height + 1 ,
481
503
} )
482
504
}
483
505
@@ -518,6 +540,7 @@ impl Property for ExtData {
518
540
l. exec_stack_elem_count_dissat ,
519
541
r. exec_stack_elem_count_dissat . map ( |x| x + 1 ) ,
520
542
) ,
543
+ tree_height : cmp:: max ( l. tree_height , r. tree_height ) + 1 ,
521
544
} )
522
545
}
523
546
@@ -541,6 +564,7 @@ impl Property for ExtData {
541
564
r. exec_stack_elem_count_sat ,
542
565
) ,
543
566
exec_stack_elem_count_dissat : None ,
567
+ tree_height : cmp:: max ( l. tree_height , r. tree_height ) + 1 ,
544
568
} )
545
569
}
546
570
@@ -580,6 +604,7 @@ impl Property for ExtData {
580
604
l. exec_stack_elem_count_dissat ,
581
605
r. exec_stack_elem_count_dissat . map ( |x| x + 1 ) ,
582
606
) ,
607
+ tree_height : cmp:: max ( l. tree_height , r. tree_height ) + 1 ,
583
608
} )
584
609
}
585
610
@@ -617,6 +642,7 @@ impl Property for ExtData {
617
642
l. exec_stack_elem_count_dissat ,
618
643
r. exec_stack_elem_count_dissat . map ( |x| x + 1 ) ,
619
644
) ,
645
+ tree_height : cmp:: max ( l. tree_height , r. tree_height ) + 1 ,
620
646
} ;
621
647
Ok ( res)
622
648
}
@@ -648,6 +674,7 @@ impl Property for ExtData {
648
674
opt_max ( r. exec_stack_elem_count_sat , l. exec_stack_elem_count_dissat ) ,
649
675
) ,
650
676
exec_stack_elem_count_dissat : None ,
677
+ tree_height : cmp:: max ( l. tree_height , r. tree_height ) + 1 ,
651
678
} )
652
679
}
653
680
@@ -694,6 +721,7 @@ impl Property for ExtData {
694
721
l. exec_stack_elem_count_dissat ,
695
722
r. exec_stack_elem_count_dissat ,
696
723
) ,
724
+ tree_height : cmp:: max ( l. tree_height , r. tree_height ) + 1 ,
697
725
} )
698
726
}
699
727
@@ -736,6 +764,7 @@ impl Property for ExtData {
736
764
a. exec_stack_elem_count_dissat ,
737
765
c. exec_stack_elem_count_dissat ,
738
766
) ,
767
+ tree_height : cmp:: max ( a. tree_height , cmp:: max ( b. tree_height , c. tree_height ) ) + 1 ,
739
768
} )
740
769
}
741
770
@@ -755,6 +784,7 @@ impl Property for ExtData {
755
784
// the max element count is same as max sat element count when satisfying one element + 1
756
785
let mut exec_stack_elem_count_sat_vec = Vec :: with_capacity ( n) ;
757
786
let mut exec_stack_elem_count_dissat = Some ( 0 ) ;
787
+ let mut max_child_height = 0 ;
758
788
759
789
for i in 0 ..n {
760
790
let sub = sub_ck ( i) ?;
@@ -784,6 +814,7 @@ impl Property for ExtData {
784
814
. push ( ( sub. exec_stack_elem_count_sat , sub. exec_stack_elem_count_dissat ) ) ;
785
815
exec_stack_elem_count_dissat =
786
816
opt_max ( exec_stack_elem_count_dissat, sub. exec_stack_elem_count_dissat ) ;
817
+ max_child_height = cmp:: max ( max_child_height, sub. tree_height ) ;
787
818
}
788
819
789
820
stack_elem_count_sat_vec. sort_by ( sat_minus_option_dissat) ;
@@ -854,6 +885,7 @@ impl Property for ExtData {
854
885
timelock_info : TimelockInfo :: combine_threshold ( k, timelocks) ,
855
886
exec_stack_elem_count_sat,
856
887
exec_stack_elem_count_dissat,
888
+ tree_height : max_child_height + 1 ,
857
889
} )
858
890
}
859
891
0 commit comments