@@ -139,6 +139,9 @@ pub struct ExtData {
139
139
/// This does **not** include initial witness elements. This element only captures
140
140
/// the additional elements that are pushed during execution.
141
141
pub exec_stack_elem_count_dissat : Option < usize > ,
142
+ /// The miniscript tree depth/height of this node.
143
+ /// Used for checking the max depth of the miniscript tree to prevent stack overflow.
144
+ pub tree_height : usize ,
142
145
}
143
146
144
147
impl Property for ExtData {
@@ -165,6 +168,7 @@ impl Property for ExtData {
165
168
timelock_info : TimelockInfo :: default ( ) ,
166
169
exec_stack_elem_count_sat : Some ( 1 ) ,
167
170
exec_stack_elem_count_dissat : None ,
171
+ tree_height : 0 ,
168
172
}
169
173
}
170
174
@@ -180,6 +184,7 @@ impl Property for ExtData {
180
184
timelock_info : TimelockInfo :: default ( ) ,
181
185
exec_stack_elem_count_sat : None ,
182
186
exec_stack_elem_count_dissat : Some ( 1 ) ,
187
+ tree_height : 0 ,
183
188
}
184
189
}
185
190
@@ -201,6 +206,7 @@ impl Property for ExtData {
201
206
timelock_info : TimelockInfo :: default ( ) ,
202
207
exec_stack_elem_count_sat : Some ( 1 ) , // pushes the pk
203
208
exec_stack_elem_count_dissat : Some ( 1 ) ,
209
+ tree_height : 0 ,
204
210
}
205
211
}
206
212
@@ -222,6 +228,7 @@ impl Property for ExtData {
222
228
timelock_info : TimelockInfo :: default ( ) ,
223
229
exec_stack_elem_count_sat : Some ( 2 ) , // dup and hash push
224
230
exec_stack_elem_count_dissat : Some ( 2 ) ,
231
+ tree_height : 0 ,
225
232
}
226
233
}
227
234
@@ -245,6 +252,7 @@ impl Property for ExtData {
245
252
timelock_info : TimelockInfo :: default ( ) ,
246
253
exec_stack_elem_count_sat : Some ( n) , // n pks
247
254
exec_stack_elem_count_dissat : Some ( n) ,
255
+ tree_height : 0 ,
248
256
}
249
257
}
250
258
@@ -267,6 +275,7 @@ impl Property for ExtData {
267
275
timelock_info : TimelockInfo :: default ( ) ,
268
276
exec_stack_elem_count_sat : Some ( 2 ) , // the two nums before num equal verify
269
277
exec_stack_elem_count_dissat : Some ( 2 ) ,
278
+ tree_height : 0 ,
270
279
}
271
280
}
272
281
@@ -287,6 +296,7 @@ impl Property for ExtData {
287
296
timelock_info : TimelockInfo :: default ( ) ,
288
297
exec_stack_elem_count_sat : Some ( 2 ) , // either size <32> or <hash256> <32 byte>
289
298
exec_stack_elem_count_dissat : Some ( 2 ) ,
299
+ tree_height : 0 ,
290
300
}
291
301
}
292
302
@@ -302,6 +312,7 @@ impl Property for ExtData {
302
312
timelock_info : TimelockInfo :: default ( ) ,
303
313
exec_stack_elem_count_sat : Some ( 2 ) , // either size <32> or <hash256> <32 byte>
304
314
exec_stack_elem_count_dissat : Some ( 2 ) ,
315
+ tree_height : 0 ,
305
316
}
306
317
}
307
318
@@ -317,6 +328,7 @@ impl Property for ExtData {
317
328
timelock_info : TimelockInfo :: default ( ) ,
318
329
exec_stack_elem_count_sat : Some ( 2 ) , // either size <32> or <hash256> <20 byte>
319
330
exec_stack_elem_count_dissat : Some ( 2 ) ,
331
+ tree_height : 0 ,
320
332
}
321
333
}
322
334
@@ -332,6 +344,7 @@ impl Property for ExtData {
332
344
timelock_info : TimelockInfo :: default ( ) ,
333
345
exec_stack_elem_count_sat : Some ( 2 ) , // either size <32> or <hash256> <20 byte>
334
346
exec_stack_elem_count_dissat : Some ( 2 ) ,
347
+ tree_height : 0 ,
335
348
}
336
349
}
337
350
@@ -357,6 +370,7 @@ impl Property for ExtData {
357
370
} ,
358
371
exec_stack_elem_count_sat : Some ( 1 ) , // <t>
359
372
exec_stack_elem_count_dissat : None ,
373
+ tree_height : 0 ,
360
374
}
361
375
}
362
376
@@ -378,6 +392,7 @@ impl Property for ExtData {
378
392
} ,
379
393
exec_stack_elem_count_sat : Some ( 1 ) , // <t>
380
394
exec_stack_elem_count_dissat : None ,
395
+ tree_height : 0 ,
381
396
}
382
397
}
383
398
@@ -393,6 +408,7 @@ impl Property for ExtData {
393
408
timelock_info : self . timelock_info ,
394
409
exec_stack_elem_count_sat : self . exec_stack_elem_count_sat ,
395
410
exec_stack_elem_count_dissat : self . exec_stack_elem_count_dissat ,
411
+ tree_height : self . tree_height + 1 ,
396
412
} )
397
413
}
398
414
@@ -408,6 +424,7 @@ impl Property for ExtData {
408
424
timelock_info : self . timelock_info ,
409
425
exec_stack_elem_count_sat : self . exec_stack_elem_count_sat ,
410
426
exec_stack_elem_count_dissat : self . exec_stack_elem_count_dissat ,
427
+ tree_height : self . tree_height + 1 ,
411
428
} )
412
429
}
413
430
@@ -423,6 +440,7 @@ impl Property for ExtData {
423
440
timelock_info : self . timelock_info ,
424
441
exec_stack_elem_count_sat : self . exec_stack_elem_count_sat ,
425
442
exec_stack_elem_count_dissat : self . exec_stack_elem_count_dissat ,
443
+ tree_height : self . tree_height + 1 ,
426
444
} )
427
445
}
428
446
@@ -441,6 +459,7 @@ impl Property for ExtData {
441
459
// Even all V types push something onto the stack and then remove them
442
460
exec_stack_elem_count_sat : self . exec_stack_elem_count_sat ,
443
461
exec_stack_elem_count_dissat : Some ( 1 ) ,
462
+ tree_height : self . tree_height + 1 ,
444
463
} )
445
464
}
446
465
@@ -457,6 +476,7 @@ impl Property for ExtData {
457
476
timelock_info : self . timelock_info ,
458
477
exec_stack_elem_count_sat : self . exec_stack_elem_count_sat ,
459
478
exec_stack_elem_count_dissat : None ,
479
+ tree_height : self . tree_height + 1 ,
460
480
} )
461
481
}
462
482
@@ -472,6 +492,7 @@ impl Property for ExtData {
472
492
timelock_info : self . timelock_info ,
473
493
exec_stack_elem_count_sat : self . exec_stack_elem_count_sat ,
474
494
exec_stack_elem_count_dissat : Some ( 1 ) ,
495
+ tree_height : self . tree_height + 1 ,
475
496
} )
476
497
}
477
498
@@ -488,6 +509,7 @@ impl Property for ExtData {
488
509
// Technically max(1, self.exec_stack_elem_count_sat), same rationale as cast_dupif
489
510
exec_stack_elem_count_sat : self . exec_stack_elem_count_sat ,
490
511
exec_stack_elem_count_dissat : self . exec_stack_elem_count_dissat ,
512
+ tree_height : self . tree_height + 1 ,
491
513
} )
492
514
}
493
515
@@ -528,6 +550,7 @@ impl Property for ExtData {
528
550
l. exec_stack_elem_count_dissat ,
529
551
r. exec_stack_elem_count_dissat . map ( |x| x + 1 ) ,
530
552
) ,
553
+ tree_height : cmp:: max ( l. tree_height , r. tree_height ) + 1 ,
531
554
} )
532
555
}
533
556
@@ -555,6 +578,7 @@ impl Property for ExtData {
555
578
r. exec_stack_elem_count_sat ,
556
579
) ,
557
580
exec_stack_elem_count_dissat : None ,
581
+ tree_height : cmp:: max ( l. tree_height , r. tree_height ) + 1 ,
558
582
} )
559
583
}
560
584
@@ -603,6 +627,7 @@ impl Property for ExtData {
603
627
l. exec_stack_elem_count_dissat ,
604
628
r. exec_stack_elem_count_dissat . map ( |x| x + 1 ) ,
605
629
) ,
630
+ tree_height : cmp:: max ( l. tree_height , r. tree_height ) + 1 ,
606
631
} )
607
632
}
608
633
@@ -640,6 +665,7 @@ impl Property for ExtData {
640
665
l. exec_stack_elem_count_dissat ,
641
666
r. exec_stack_elem_count_dissat . map ( |x| x + 1 ) ,
642
667
) ,
668
+ tree_height : cmp:: max ( l. tree_height , r. tree_height ) + 1 ,
643
669
} ;
644
670
Ok ( res)
645
671
}
@@ -671,6 +697,7 @@ impl Property for ExtData {
671
697
opt_max ( r. exec_stack_elem_count_sat , l. exec_stack_elem_count_dissat ) ,
672
698
) ,
673
699
exec_stack_elem_count_dissat : None ,
700
+ tree_height : cmp:: max ( l. tree_height , r. tree_height ) + 1 ,
674
701
} )
675
702
}
676
703
@@ -717,6 +744,7 @@ impl Property for ExtData {
717
744
l. exec_stack_elem_count_dissat ,
718
745
r. exec_stack_elem_count_dissat ,
719
746
) ,
747
+ tree_height : cmp:: max ( l. tree_height , r. tree_height ) + 1 ,
720
748
} )
721
749
}
722
750
@@ -762,6 +790,7 @@ impl Property for ExtData {
762
790
a. exec_stack_elem_count_dissat ,
763
791
c. exec_stack_elem_count_dissat ,
764
792
) ,
793
+ tree_height : cmp:: max ( a. tree_height , cmp:: max ( b. tree_height , c. tree_height ) ) + 1 ,
765
794
} )
766
795
}
767
796
@@ -781,6 +810,7 @@ impl Property for ExtData {
781
810
// the max element count is same as max sat element count when satisfying one element + 1
782
811
let mut exec_stack_elem_count_sat_vec = Vec :: with_capacity ( n) ;
783
812
let mut exec_stack_elem_count_dissat = Some ( 0 ) ;
813
+ let mut max_child_height = 0 ;
784
814
785
815
for i in 0 ..n {
786
816
let sub = sub_ck ( i) ?;
@@ -814,6 +844,7 @@ impl Property for ExtData {
814
844
exec_stack_elem_count_dissat,
815
845
sub. exec_stack_elem_count_dissat ,
816
846
) ;
847
+ max_child_height = cmp:: max ( max_child_height, sub. tree_height ) + 1 ;
817
848
}
818
849
819
850
stack_elem_count_sat_vec. sort_by ( sat_minus_option_dissat) ;
@@ -885,6 +916,7 @@ impl Property for ExtData {
885
916
timelock_info : TimelockInfo :: combine_threshold ( k, timelocks) ,
886
917
exec_stack_elem_count_sat,
887
918
exec_stack_elem_count_dissat,
919
+ tree_height : max_child_height + 1 ,
888
920
} )
889
921
}
890
922
0 commit comments