@@ -137,6 +137,9 @@ pub struct ExtData {
137
137
/// This does **not** include initial witness elements. This element only captures
138
138
/// the additional elements that are pushed during execution.
139
139
pub exec_stack_elem_count_dissat : Option < usize > ,
140
+ /// The miniscript tree depth/height of this node.
141
+ /// Used for checking the max depth of the miniscript tree to prevent stack overflow.
142
+ pub tree_height : usize ,
140
143
}
141
144
142
145
impl Property for ExtData {
@@ -163,6 +166,7 @@ impl Property for ExtData {
163
166
timelock_info : TimelockInfo :: default ( ) ,
164
167
exec_stack_elem_count_sat : Some ( 1 ) ,
165
168
exec_stack_elem_count_dissat : None ,
169
+ tree_height : 0 ,
166
170
}
167
171
}
168
172
@@ -178,6 +182,7 @@ impl Property for ExtData {
178
182
timelock_info : TimelockInfo :: default ( ) ,
179
183
exec_stack_elem_count_sat : None ,
180
184
exec_stack_elem_count_dissat : Some ( 1 ) ,
185
+ tree_height : 0 ,
181
186
}
182
187
}
183
188
@@ -199,6 +204,7 @@ impl Property for ExtData {
199
204
timelock_info : TimelockInfo :: default ( ) ,
200
205
exec_stack_elem_count_sat : Some ( 1 ) , // pushes the pk
201
206
exec_stack_elem_count_dissat : Some ( 1 ) ,
207
+ tree_height : 0 ,
202
208
}
203
209
}
204
210
@@ -220,6 +226,7 @@ impl Property for ExtData {
220
226
timelock_info : TimelockInfo :: default ( ) ,
221
227
exec_stack_elem_count_sat : Some ( 2 ) , // dup and hash push
222
228
exec_stack_elem_count_dissat : Some ( 2 ) ,
229
+ tree_height : 0 ,
223
230
}
224
231
}
225
232
@@ -243,6 +250,7 @@ impl Property for ExtData {
243
250
timelock_info : TimelockInfo :: default ( ) ,
244
251
exec_stack_elem_count_sat : Some ( n) , // n pks
245
252
exec_stack_elem_count_dissat : Some ( n) ,
253
+ tree_height : 0 ,
246
254
}
247
255
}
248
256
@@ -265,6 +273,7 @@ impl Property for ExtData {
265
273
timelock_info : TimelockInfo :: default ( ) ,
266
274
exec_stack_elem_count_sat : Some ( 2 ) , // the two nums before num equal verify
267
275
exec_stack_elem_count_dissat : Some ( 2 ) ,
276
+ tree_height : 0 ,
268
277
}
269
278
}
270
279
@@ -285,6 +294,7 @@ impl Property for ExtData {
285
294
timelock_info : TimelockInfo :: default ( ) ,
286
295
exec_stack_elem_count_sat : Some ( 2 ) , // either size <32> or <hash256> <32 byte>
287
296
exec_stack_elem_count_dissat : Some ( 2 ) ,
297
+ tree_height : 0 ,
288
298
}
289
299
}
290
300
@@ -300,6 +310,7 @@ impl Property for ExtData {
300
310
timelock_info : TimelockInfo :: default ( ) ,
301
311
exec_stack_elem_count_sat : Some ( 2 ) , // either size <32> or <hash256> <32 byte>
302
312
exec_stack_elem_count_dissat : Some ( 2 ) ,
313
+ tree_height : 0 ,
303
314
}
304
315
}
305
316
@@ -315,6 +326,7 @@ impl Property for ExtData {
315
326
timelock_info : TimelockInfo :: default ( ) ,
316
327
exec_stack_elem_count_sat : Some ( 2 ) , // either size <32> or <hash256> <20 byte>
317
328
exec_stack_elem_count_dissat : Some ( 2 ) ,
329
+ tree_height : 0 ,
318
330
}
319
331
}
320
332
@@ -330,6 +342,7 @@ impl Property for ExtData {
330
342
timelock_info : TimelockInfo :: default ( ) ,
331
343
exec_stack_elem_count_sat : Some ( 2 ) , // either size <32> or <hash256> <20 byte>
332
344
exec_stack_elem_count_dissat : Some ( 2 ) ,
345
+ tree_height : 0 ,
333
346
}
334
347
}
335
348
@@ -355,6 +368,7 @@ impl Property for ExtData {
355
368
} ,
356
369
exec_stack_elem_count_sat : Some ( 1 ) , // <t>
357
370
exec_stack_elem_count_dissat : None ,
371
+ tree_height : 0 ,
358
372
}
359
373
}
360
374
@@ -376,6 +390,7 @@ impl Property for ExtData {
376
390
} ,
377
391
exec_stack_elem_count_sat : Some ( 1 ) , // <t>
378
392
exec_stack_elem_count_dissat : None ,
393
+ tree_height : 0 ,
379
394
}
380
395
}
381
396
@@ -391,6 +406,7 @@ impl Property for ExtData {
391
406
timelock_info : self . timelock_info ,
392
407
exec_stack_elem_count_sat : self . exec_stack_elem_count_sat ,
393
408
exec_stack_elem_count_dissat : self . exec_stack_elem_count_dissat ,
409
+ tree_height : self . tree_height + 1 ,
394
410
} )
395
411
}
396
412
@@ -406,6 +422,7 @@ impl Property for ExtData {
406
422
timelock_info : self . timelock_info ,
407
423
exec_stack_elem_count_sat : self . exec_stack_elem_count_sat ,
408
424
exec_stack_elem_count_dissat : self . exec_stack_elem_count_dissat ,
425
+ tree_height : self . tree_height + 1 ,
409
426
} )
410
427
}
411
428
@@ -421,6 +438,7 @@ impl Property for ExtData {
421
438
timelock_info : self . timelock_info ,
422
439
exec_stack_elem_count_sat : self . exec_stack_elem_count_sat ,
423
440
exec_stack_elem_count_dissat : self . exec_stack_elem_count_dissat ,
441
+ tree_height : self . tree_height + 1 ,
424
442
} )
425
443
}
426
444
@@ -439,6 +457,7 @@ impl Property for ExtData {
439
457
// Even all V types push something onto the stack and then remove them
440
458
exec_stack_elem_count_sat : self . exec_stack_elem_count_sat ,
441
459
exec_stack_elem_count_dissat : Some ( 1 ) ,
460
+ tree_height : self . tree_height + 1 ,
442
461
} )
443
462
}
444
463
@@ -455,6 +474,7 @@ impl Property for ExtData {
455
474
timelock_info : self . timelock_info ,
456
475
exec_stack_elem_count_sat : self . exec_stack_elem_count_sat ,
457
476
exec_stack_elem_count_dissat : None ,
477
+ tree_height : self . tree_height + 1 ,
458
478
} )
459
479
}
460
480
@@ -470,6 +490,7 @@ impl Property for ExtData {
470
490
timelock_info : self . timelock_info ,
471
491
exec_stack_elem_count_sat : self . exec_stack_elem_count_sat ,
472
492
exec_stack_elem_count_dissat : Some ( 1 ) ,
493
+ tree_height : self . tree_height + 1 ,
473
494
} )
474
495
}
475
496
@@ -486,6 +507,7 @@ impl Property for ExtData {
486
507
// Technically max(1, self.exec_stack_elem_count_sat), same rationale as cast_dupif
487
508
exec_stack_elem_count_sat : self . exec_stack_elem_count_sat ,
488
509
exec_stack_elem_count_dissat : self . exec_stack_elem_count_dissat ,
510
+ tree_height : self . tree_height + 1 ,
489
511
} )
490
512
}
491
513
@@ -526,6 +548,7 @@ impl Property for ExtData {
526
548
l. exec_stack_elem_count_dissat ,
527
549
r. exec_stack_elem_count_dissat . map ( |x| x + 1 ) ,
528
550
) ,
551
+ tree_height : cmp:: max ( l. tree_height , r. tree_height ) + 1 ,
529
552
} )
530
553
}
531
554
@@ -553,6 +576,7 @@ impl Property for ExtData {
553
576
r. exec_stack_elem_count_sat ,
554
577
) ,
555
578
exec_stack_elem_count_dissat : None ,
579
+ tree_height : cmp:: max ( l. tree_height , r. tree_height ) + 1 ,
556
580
} )
557
581
}
558
582
@@ -601,6 +625,7 @@ impl Property for ExtData {
601
625
l. exec_stack_elem_count_dissat ,
602
626
r. exec_stack_elem_count_dissat . map ( |x| x + 1 ) ,
603
627
) ,
628
+ tree_height : cmp:: max ( l. tree_height , r. tree_height ) + 1 ,
604
629
} )
605
630
}
606
631
@@ -638,6 +663,7 @@ impl Property for ExtData {
638
663
l. exec_stack_elem_count_dissat ,
639
664
r. exec_stack_elem_count_dissat . map ( |x| x + 1 ) ,
640
665
) ,
666
+ tree_height : cmp:: max ( l. tree_height , r. tree_height ) + 1 ,
641
667
} ;
642
668
Ok ( res)
643
669
}
@@ -669,6 +695,7 @@ impl Property for ExtData {
669
695
opt_max ( r. exec_stack_elem_count_sat , l. exec_stack_elem_count_dissat ) ,
670
696
) ,
671
697
exec_stack_elem_count_dissat : None ,
698
+ tree_height : cmp:: max ( l. tree_height , r. tree_height ) + 1 ,
672
699
} )
673
700
}
674
701
@@ -716,6 +743,7 @@ impl Property for ExtData {
716
743
l. exec_stack_elem_count_dissat ,
717
744
r. exec_stack_elem_count_dissat ,
718
745
) ,
746
+ tree_height : cmp:: max ( l. tree_height , r. tree_height ) + 1 ,
719
747
} )
720
748
}
721
749
@@ -761,6 +789,7 @@ impl Property for ExtData {
761
789
a. exec_stack_elem_count_dissat ,
762
790
c. exec_stack_elem_count_dissat ,
763
791
) ,
792
+ tree_height : cmp:: max ( a. tree_height , cmp:: max ( b. tree_height , c. tree_height ) ) + 1 ,
764
793
} )
765
794
}
766
795
@@ -780,6 +809,7 @@ impl Property for ExtData {
780
809
// the max element count is same as max sat element count when satisfying one element + 1
781
810
let mut exec_stack_elem_count_sat_vec = Vec :: with_capacity ( n) ;
782
811
let mut exec_stack_elem_count_dissat = Some ( 0 ) ;
812
+ let mut max_child_height = 0 ;
783
813
784
814
for i in 0 ..n {
785
815
let sub = sub_ck ( i) ?;
@@ -813,6 +843,7 @@ impl Property for ExtData {
813
843
exec_stack_elem_count_dissat,
814
844
sub. exec_stack_elem_count_dissat ,
815
845
) ;
846
+ max_child_height = cmp:: max ( max_child_height, sub. tree_height ) + 1 ;
816
847
}
817
848
818
849
stack_elem_count_sat_vec. sort_by ( sat_minus_option_dissat) ;
@@ -884,6 +915,7 @@ impl Property for ExtData {
884
915
timelock_info : TimelockInfo :: combine_threshold ( k, timelocks) ,
885
916
exec_stack_elem_count_sat,
886
917
exec_stack_elem_count_dissat,
918
+ tree_height : max_child_height + 1 ,
887
919
} )
888
920
}
889
921
0 commit comments