Coverage Report

Created: 2026-10-07 20:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/bitcoin/src/script/miniscript.h
Line
Count
Source
1
// Copyright (c) 2019-present The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
#ifndef BITCOIN_SCRIPT_MINISCRIPT_H
6
#define BITCOIN_SCRIPT_MINISCRIPT_H
7
8
#include <consensus/consensus.h>
9
#include <crypto/hex_base.h>
10
#include <policy/policy.h>
11
#include <script/interpreter.h>
12
#include <script/parsing.h>
13
#include <script/script.h>
14
#include <serialize.h>
15
#include <util/check.h>
16
#include <util/strencodings.h>
17
#include <util/string.h>
18
#include <util/vector.h>
19
20
#include <algorithm>
21
#include <concepts>
22
#include <cstddef>
23
#include <cstdint>
24
#include <functional>
25
#include <memory>
26
#include <optional>
27
#include <set>
28
#include <span>
29
#include <stdexcept>
30
#include <string>
31
#include <string_view>
32
#include <tuple>
33
#include <utility>
34
#include <variant>
35
#include <vector>
36
37
namespace miniscript {
38
39
/** This type encapsulates the miniscript type system properties.
40
 *
41
 * Every miniscript expression is one of 4 basic types, and additionally has
42
 * a number of boolean type properties.
43
 *
44
 * The basic types are:
45
 * - "B" Base:
46
 *   - Takes its inputs from the top of the stack.
47
 *   - When satisfied, pushes a nonzero value of up to 4 bytes onto the stack.
48
 *   - When dissatisfied, pushes a 0 onto the stack.
49
 *   - This is used for most expressions, and required for the top level one.
50
 *   - For example: older(n) = <n> OP_CHECKSEQUENCEVERIFY.
51
 * - "V" Verify:
52
 *   - Takes its inputs from the top of the stack.
53
 *   - When satisfied, pushes nothing.
54
 *   - Cannot be dissatisfied.
55
 *   - This can be obtained by adding an OP_VERIFY to a B, modifying the last opcode
56
 *     of a B to its -VERIFY version (only for OP_CHECKSIG, OP_CHECKSIGVERIFY,
57
 *     OP_NUMEQUAL and OP_EQUAL), or by combining a V fragment under some conditions.
58
 *   - For example vc:pk_k(key) = <key> OP_CHECKSIGVERIFY
59
 * - "K" Key:
60
 *   - Takes its inputs from the top of the stack.
61
 *   - Becomes a B when followed by OP_CHECKSIG.
62
 *   - Always pushes a public key onto the stack, for which a signature is to be
63
 *     provided to satisfy the expression.
64
 *   - For example pk_h(key) = OP_DUP OP_HASH160 <Hash160(key)> OP_EQUALVERIFY
65
 * - "W" Wrapped:
66
 *   - Takes its input from one below the top of the stack.
67
 *   - When satisfied, pushes a nonzero value (like B) on top of the stack, or one below.
68
 *   - When dissatisfied, pushes 0 op top of the stack or one below.
69
 *   - Is always "OP_SWAP [B]" or "OP_TOALTSTACK [B] OP_FROMALTSTACK".
70
 *   - For example sc:pk_k(key) = OP_SWAP <key> OP_CHECKSIG
71
 *
72
 * There are type properties that help reasoning about correctness:
73
 * - "z" Zero-arg:
74
 *   - Is known to always consume exactly 0 stack elements.
75
 *   - For example after(n) = <n> OP_CHECKLOCKTIMEVERIFY
76
 * - "o" One-arg:
77
 *   - Is known to always consume exactly 1 stack element.
78
 *   - Conflicts with property 'z'
79
 *   - For example sha256(hash) = OP_SIZE 32 OP_EQUALVERIFY OP_SHA256 <hash> OP_EQUAL
80
 * - "n" Nonzero:
81
 *   - For every way this expression can be satisfied, a satisfaction exists that never needs
82
 *     a zero top stack element.
83
 *   - Conflicts with property 'z' and with type 'W'.
84
 * - "d" Dissatisfiable:
85
 *   - There is an easy way to construct a dissatisfaction for this expression.
86
 *   - Conflicts with type 'V'.
87
 * - "u" Unit:
88
 *   - In case of satisfaction, an exact 1 is put on the stack (rather than just nonzero).
89
 *   - Conflicts with type 'V'.
90
 *
91
 * Additional type properties help reasoning about nonmalleability:
92
 * - "e" Expression:
93
 *   - This implies property 'd', but the dissatisfaction is nonmalleable.
94
 *   - This generally requires 'e' for all subexpressions which are invoked for that
95
 *     dissatisfaction, and property 'f' for the unexecuted subexpressions in that case.
96
 *   - Conflicts with type 'V'.
97
 * - "f" Forced:
98
 *   - Dissatisfactions (if any) for this expression always involve at least one signature.
99
 *   - Is always true for type 'V'.
100
 * - "s" Safe:
101
 *   - Satisfactions for this expression always involve at least one signature.
102
 * - "m" Nonmalleable:
103
 *   - For every way this expression can be satisfied (which may be none),
104
 *     a nonmalleable satisfaction exists.
105
 *   - This generally requires 'm' for all subexpressions, and 'e' for all subexpressions
106
 *     which are dissatisfied when satisfying the parent.
107
 *
108
 * One type property is an implementation detail:
109
 * - "x" Expensive verify:
110
 *   - Expressions with this property have a script whose last opcode is not EQUAL, CHECKSIG, or CHECKMULTISIG.
111
 *   - Not having this property means that it can be converted to a V at no cost (by switching to the
112
 *     -VERIFY version of the last opcode).
113
 *
114
 * Five more type properties for representing timelock information. Spend paths
115
 * in miniscripts containing conflicting timelocks and heightlocks cannot be spent together.
116
 * This helps users detect if miniscript does not match the semantic behaviour the
117
 * user expects.
118
 * - "g" Whether the branch contains a relative time timelock
119
 * - "h" Whether the branch contains a relative height timelock
120
 * - "i" Whether the branch contains an absolute time timelock
121
 * - "j" Whether the branch contains an absolute height timelock
122
 * - "k"
123
 *   - Whether all satisfactions of this expression don't contain a mix of heightlock and timelock
124
 *     of the same type.
125
 *   - If the miniscript does not have the "k" property, the miniscript template will not match
126
 *     the user expectation of the corresponding spending policy.
127
 * For each of these properties the subset rule holds: an expression with properties X, Y, and Z, is also
128
 * valid in places where an X, a Y, a Z, an XY, ... is expected.
129
*/
130
class Type {
131
    //! Internal bitmap of properties (see ""_mst operator for details).
132
    uint32_t m_flags;
133
134
    //! Internal constructor used by the ""_mst operator.
135
0
    explicit constexpr Type(uint32_t flags) : m_flags(flags) {}
136
137
public:
138
    //! The only way to publicly construct a Type is using this literal operator.
139
    friend consteval Type operator""_mst(const char* c, size_t l);
140
141
    //! Compute the type with the union of properties.
142
0
    constexpr Type operator|(Type x) const { return Type(m_flags | x.m_flags); }
143
144
    //! Compute the type with the intersection of properties.
145
0
    constexpr Type operator&(Type x) const { return Type(m_flags & x.m_flags); }
146
147
    //! Check whether the left hand's properties are superset of the right's (= left is a subtype of right).
148
0
    constexpr bool operator<<(Type x) const { return (x.m_flags & ~m_flags) == 0; }
149
150
    //! Comparison operator to enable use in sets/maps (total ordering incompatible with <<).
151
0
    constexpr bool operator<(Type x) const { return m_flags < x.m_flags; }
152
153
    //! Equality operator.
154
0
    constexpr bool operator==(Type x) const { return m_flags == x.m_flags; }
155
156
    //! The empty type if x is false, itself otherwise.
157
0
    constexpr Type If(bool x) const { return Type(x ? m_flags : 0); }
  Branch (157:51): [True: 0, False: 0]
158
};
159
160
//! Literal operator to construct Type objects.
161
inline consteval Type operator""_mst(const char* c, size_t l)
162
{
163
    Type typ{0};
164
165
    for (const char *p = c; p < c + l; p++) {
166
        typ = typ | Type(
167
            *p == 'B' ? 1 << 0 : // Base type
168
            *p == 'V' ? 1 << 1 : // Verify type
169
            *p == 'K' ? 1 << 2 : // Key type
170
            *p == 'W' ? 1 << 3 : // Wrapped type
171
            *p == 'z' ? 1 << 4 : // Zero-arg property
172
            *p == 'o' ? 1 << 5 : // One-arg property
173
            *p == 'n' ? 1 << 6 : // Nonzero arg property
174
            *p == 'd' ? 1 << 7 : // Dissatisfiable property
175
            *p == 'u' ? 1 << 8 : // Unit property
176
            *p == 'e' ? 1 << 9 : // Expression property
177
            *p == 'f' ? 1 << 10 : // Forced property
178
            *p == 's' ? 1 << 11 : // Safe property
179
            *p == 'm' ? 1 << 12 : // Nonmalleable property
180
            *p == 'x' ? 1 << 13 : // Expensive verify
181
            *p == 'g' ? 1 << 14 : // older: contains relative time timelock   (csv_time)
182
            *p == 'h' ? 1 << 15 : // older: contains relative height timelock (csv_height)
183
            *p == 'i' ? 1 << 16 : // after: contains time timelock   (cltv_time)
184
            *p == 'j' ? 1 << 17 : // after: contains height timelock   (cltv_height)
185
            *p == 'k' ? 1 << 18 : // does not contain a combination of height and time locks
186
            (throw std::logic_error("Unknown character in _mst literal"), 0)
187
        );
188
    }
189
190
    return typ;
191
}
192
193
using Opcode = std::pair<opcodetype, std::vector<unsigned char>>;
194
195
template<typename Key> class Node;
196
197
//! Unordered traversal of a miniscript node tree.
198
template <typename Key, std::invocable<const Node<Key>&> Fn>
199
void ForEachNode(const Node<Key>& root, Fn&& fn)
200
0
{
201
0
    std::vector<std::reference_wrapper<const Node<Key>>> stack{root};
202
0
    while (!stack.empty()) {
  Branch (202:12): [True: 0, False: 0]
203
0
        const Node<Key>& node = stack.back();
204
0
        std::invoke(fn, node);
205
0
        stack.pop_back();
206
0
        for (const auto& sub : node.Subs()) {
  Branch (206:30): [True: 0, False: 0]
207
0
            stack.emplace_back(sub);
208
0
        }
209
0
    }
210
0
}
211
212
//! The different node types in miniscript.
213
enum class Fragment {
214
    JUST_0,    //!< OP_0
215
    JUST_1,    //!< OP_1
216
    PK_K,      //!< [key]
217
    PK_H,      //!< OP_DUP OP_HASH160 [keyhash] OP_EQUALVERIFY
218
    OLDER,     //!< [n] OP_CHECKSEQUENCEVERIFY
219
    AFTER,     //!< [n] OP_CHECKLOCKTIMEVERIFY
220
    SHA256,    //!< OP_SIZE 32 OP_EQUALVERIFY OP_SHA256 [hash] OP_EQUAL
221
    HASH256,   //!< OP_SIZE 32 OP_EQUALVERIFY OP_HASH256 [hash] OP_EQUAL
222
    RIPEMD160, //!< OP_SIZE 32 OP_EQUALVERIFY OP_RIPEMD160 [hash] OP_EQUAL
223
    HASH160,   //!< OP_SIZE 32 OP_EQUALVERIFY OP_HASH160 [hash] OP_EQUAL
224
    WRAP_A,    //!< OP_TOALTSTACK [X] OP_FROMALTSTACK
225
    WRAP_S,    //!< OP_SWAP [X]
226
    WRAP_C,    //!< [X] OP_CHECKSIG
227
    WRAP_D,    //!< OP_DUP OP_IF [X] OP_ENDIF
228
    WRAP_V,    //!< [X] OP_VERIFY (or -VERIFY version of last opcode in X)
229
    WRAP_J,    //!< OP_SIZE OP_0NOTEQUAL OP_IF [X] OP_ENDIF
230
    WRAP_N,    //!< [X] OP_0NOTEQUAL
231
    AND_V,     //!< [X] [Y]
232
    AND_B,     //!< [X] [Y] OP_BOOLAND
233
    OR_B,      //!< [X] [Y] OP_BOOLOR
234
    OR_C,      //!< [X] OP_NOTIF [Y] OP_ENDIF
235
    OR_D,      //!< [X] OP_IFDUP OP_NOTIF [Y] OP_ENDIF
236
    OR_I,      //!< OP_IF [X] OP_ELSE [Y] OP_ENDIF
237
    ANDOR,     //!< [X] OP_NOTIF [Z] OP_ELSE [Y] OP_ENDIF
238
    THRESH,    //!< [X1] ([Xn] OP_ADD)* [k] OP_EQUAL
239
    MULTI,     //!< [k] [key_n]* [n] OP_CHECKMULTISIG (only available within P2WSH context)
240
    MULTI_A,   //!< [key_0] OP_CHECKSIG ([key_n] OP_CHECKSIGADD)* [k] OP_NUMEQUAL (only within Tapscript ctx)
241
    // AND_N(X,Y) is represented as ANDOR(X,Y,0)
242
    // WRAP_T(X) is represented as AND_V(X,1)
243
    // WRAP_L(X) is represented as OR_I(0,X)
244
    // WRAP_U(X) is represented as OR_I(X,0)
245
};
246
247
enum class Availability {
248
    NO,
249
    YES,
250
    MAYBE,
251
};
252
253
enum class MiniscriptContext {
254
    P2WSH,
255
    TAPSCRIPT,
256
};
257
258
/** Whether the context Tapscript, ensuring the only other possibility is P2WSH. */
259
constexpr bool IsTapscript(MiniscriptContext ms_ctx)
260
0
{
261
0
    switch (ms_ctx) {
  Branch (261:13): [True: 0, False: 0]
262
0
        case MiniscriptContext::P2WSH: return false;
  Branch (262:9): [True: 0, False: 0]
263
0
        case MiniscriptContext::TAPSCRIPT: return true;
  Branch (263:9): [True: 0, False: 0]
264
0
    }
265
0
    assert(false);
  Branch (265:5): [Folded, False: 0]
266
0
}
267
268
namespace internal {
269
270
//! The maximum size of a witness item for a Miniscript under Tapscript context. (A BIP340 signature with a sighash type byte.)
271
inline constexpr uint32_t MAX_TAPMINISCRIPT_STACK_ELEM_SIZE{65};
272
273
//! version + nLockTime
274
inline constexpr uint32_t TX_OVERHEAD{4 + 4};
275
//! prevout + nSequence + scriptSig
276
inline constexpr uint32_t TXIN_BYTES_NO_WITNESS{36 + 4 + 1};
277
//! nValue + script len + OP_0 + pushdata 32.
278
inline constexpr uint32_t P2WSH_TXOUT_BYTES{8 + 1 + 1 + 33};
279
//! Data other than the witness in a transaction. Overhead + vin count + one vin + vout count + one vout + segwit marker
280
inline constexpr uint32_t TX_BODY_LEEWAY_WEIGHT{(TX_OVERHEAD + GetSizeOfCompactSize(1) + TXIN_BYTES_NO_WITNESS + GetSizeOfCompactSize(1) + P2WSH_TXOUT_BYTES) * WITNESS_SCALE_FACTOR + 2};
281
//! Maximum possible stack size to spend a Taproot output (excluding the script itself).
282
inline constexpr uint32_t MAX_TAPSCRIPT_SAT_SIZE{GetSizeOfCompactSize(MAX_STACK_SIZE) + (GetSizeOfCompactSize(MAX_TAPMINISCRIPT_STACK_ELEM_SIZE) + MAX_TAPMINISCRIPT_STACK_ELEM_SIZE) * MAX_STACK_SIZE + GetSizeOfCompactSize(TAPROOT_CONTROL_MAX_SIZE) + TAPROOT_CONTROL_MAX_SIZE};
283
/** The maximum size of a script depending on the context. */
284
constexpr uint32_t MaxScriptSize(MiniscriptContext ms_ctx)
285
0
{
286
0
    if (IsTapscript(ms_ctx)) {
  Branch (286:9): [True: 0, False: 0]
287
        // Leaf scripts under Tapscript are not explicitly limited in size. They are only implicitly
288
        // bounded by the maximum standard size of a spending transaction. Let the maximum script
289
        // size conservatively be small enough such that even a maximum sized witness and a reasonably
290
        // sized spending transaction can spend an output paying to this script without running into
291
        // the maximum standard tx size limit.
292
0
        constexpr auto max_size{MAX_STANDARD_TX_WEIGHT - TX_BODY_LEEWAY_WEIGHT - MAX_TAPSCRIPT_SAT_SIZE};
293
0
        return max_size - GetSizeOfCompactSize(max_size);
294
0
    }
295
0
    return MAX_STANDARD_P2WSH_SCRIPT_SIZE;
296
0
}
297
298
//! Helper function for Node::CalcType.
299
Type ComputeType(Fragment fragment, Type x, Type y, Type z, const std::vector<Type>& sub_types, uint32_t k, size_t data_size, size_t n_subs, size_t n_keys, MiniscriptContext ms_ctx);
300
301
//! Helper function for Node::CalcScriptLen.
302
size_t ComputeScriptLen(Fragment fragment, Type sub0typ, size_t subsize, uint32_t k, size_t n_subs, size_t n_keys, MiniscriptContext ms_ctx);
303
304
//! A helper sanitizer/checker for the output of CalcType.
305
Type SanitizeType(Type x);
306
307
//! An object representing a sequence of witness stack elements.
308
struct InputStack {
309
    /** Whether this stack is valid for its intended purpose (satisfaction or dissatisfaction of a Node).
310
     *  The MAYBE value is used for size estimation, when keys/preimages may actually be unavailable,
311
     *  but may be available at signing time. This makes the InputStack structure and signing logic,
312
     *  filled with dummy signatures/preimages usable for witness size estimation.
313
     */
314
    Availability available = Availability::YES;
315
    //! Whether this stack contains a digital signature.
316
    bool has_sig = false;
317
    //! Whether this stack is malleable (can be turned into an equally valid other stack by a third party).
318
    bool malleable = false;
319
    //! Whether this stack is non-canonical (using a construction known to be unnecessary for satisfaction).
320
    //! Note that this flag does not affect the satisfaction algorithm; it is only used for sanity checking.
321
    bool non_canon = false;
322
    //! Serialized witness size.
323
    size_t size = 0;
324
    //! Data elements.
325
    std::vector<std::vector<unsigned char>> stack;
326
    //! Construct an empty stack (valid).
327
0
    InputStack() = default;
328
    //! Construct a valid single-element stack (with an element up to 75 bytes).
329
0
    InputStack(std::vector<unsigned char> in) : size(in.size() + 1), stack(Vector(std::move(in))) {}
330
    //! Change availability
331
    InputStack& SetAvailable(Availability avail);
332
    //! Mark this input stack as having a signature.
333
    InputStack& SetWithSig();
334
    //! Mark this input stack as non-canonical (known to not be necessary in non-malleable satisfactions).
335
    InputStack& SetNonCanon();
336
    //! Mark this input stack as malleable.
337
    InputStack& SetMalleable(bool x = true);
338
    //! Concatenate two input stacks.
339
    friend InputStack operator+(InputStack a, InputStack b);
340
    //! Choose between two potential input stacks.
341
    friend InputStack operator|(InputStack a, InputStack b);
342
};
343
344
/** A stack consisting of a single zero-length element (interpreted as 0 by the script interpreter in numeric context). */
345
inline const auto ZERO = InputStack(std::vector<unsigned char>());
346
/** A stack consisting of a single malleable 32-byte 0x0000...0000 element (for dissatisfying hash challenges). */
347
inline const auto ZERO32 = InputStack(std::vector<unsigned char>(32, 0)).SetMalleable();
348
/** A stack consisting of a single 0x01 element (interpreted as 1 by the script interpreted in numeric context). */
349
inline const auto ONE = InputStack(Vector((unsigned char)1));
350
/** The empty stack. */
351
inline const auto EMPTY = InputStack();
352
/** A stack representing the lack of any (dis)satisfactions. */
353
inline const auto INVALID = InputStack().SetAvailable(Availability::NO);
354
355
//! A pair of a satisfaction and a dissatisfaction InputStack.
356
struct InputResult {
357
    InputStack nsat, sat;
358
359
    template<typename A, typename B>
360
0
    InputResult(A&& in_nsat, B&& in_sat) : nsat(std::forward<A>(in_nsat)), sat(std::forward<B>(in_sat)) {}
Unexecuted instantiation: _ZN10miniscript8internal11InputResultC2IRKNS0_10InputStackERS3_EEOT_OT0_
Unexecuted instantiation: _ZN10miniscript8internal11InputResultC2INS0_10InputStackERS3_EEOT_OT0_
Unexecuted instantiation: _ZN10miniscript8internal11InputResultC2INS0_10InputStackES3_EEOT_OT0_
Unexecuted instantiation: _ZN10miniscript8internal11InputResultC2IRKNS0_10InputStackES5_EEOT_OT0_
Unexecuted instantiation: _ZN10miniscript8internal11InputResultC2IRNS0_10InputStackES3_EEOT_OT0_
Unexecuted instantiation: _ZN10miniscript8internal11InputResultC2IRKNS0_10InputStackES3_EEOT_OT0_
361
};
362
363
//! Class whose objects represent the maximum of a list of integers.
364
template <typename I>
365
class MaxInt
366
{
367
    bool valid;
368
    I value;
369
370
public:
371
0
    MaxInt() : valid(false), value(0) {}
372
0
    MaxInt(I val) : valid(true), value(val) {}
373
374
0
    bool Valid() const { return valid; }
375
0
    I Value() const { return value; }
376
377
0
    friend MaxInt<I> operator+(const MaxInt<I>& a, const MaxInt<I>& b) {
378
0
        if (!a.valid || !b.valid) return {};
  Branch (378:13): [True: 0, False: 0]
  Branch (378:25): [True: 0, False: 0]
379
0
        return a.value + b.value;
380
0
    }
381
382
0
    friend MaxInt<I> operator|(const MaxInt<I>& a, const MaxInt<I>& b) {
383
0
        if (!a.valid) return b;
  Branch (383:13): [True: 0, False: 0]
384
0
        if (!b.valid) return a;
  Branch (384:13): [True: 0, False: 0]
385
0
        return std::max(a.value, b.value);
386
0
    }
387
};
388
389
struct Ops {
390
    //! Non-push opcodes.
391
    uint32_t count;
392
    //! Number of keys in possibly executed OP_CHECKMULTISIG(VERIFY)s to satisfy.
393
    MaxInt<uint32_t> sat;
394
    //! Number of keys in possibly executed OP_CHECKMULTISIG(VERIFY)s to dissatisfy.
395
    MaxInt<uint32_t> dsat;
396
397
0
    Ops(uint32_t in_count, MaxInt<uint32_t> in_sat, MaxInt<uint32_t> in_dsat) : count(in_count), sat(in_sat), dsat(in_dsat) {};
398
};
399
400
/** A data structure to help the calculation of stack size limits.
401
 *
402
 * Conceptually, every SatInfo object corresponds to a (possibly empty) set of script execution
403
 * traces (sequences of opcodes).
404
 * - SatInfo{} corresponds to the empty set.
405
 * - SatInfo{n, e} corresponds to a single trace whose net effect is removing n elements from the
406
 *   stack (may be negative for a net increase), and reaches a maximum of e stack elements more
407
 *   than it ends with.
408
 * - operator| is the union operation: (a | b) corresponds to the union of the traces in a and the
409
 *   traces in b.
410
 * - operator+ is the concatenation operator: (a + b) corresponds to the set of traces formed by
411
 *   concatenating any trace in a with any trace in b.
412
 *
413
 * Its fields are:
414
 * - valid is true if the set is non-empty.
415
 * - netdiff (if valid) is the largest difference between stack size at the beginning and at the
416
 *   end of the script across all traces in the set.
417
 * - exec (if valid) is the largest difference between stack size anywhere during execution and at
418
 *   the end of the script, across all traces in the set (note that this is not necessarily due
419
 *   to the same trace as the one that resulted in the value for netdiff).
420
 *
421
 * This allows us to build up stack size limits for any script efficiently, by starting from the
422
 * individual opcodes miniscripts correspond to, using concatenation to construct scripts, and
423
 * using the union operation to choose between execution branches. Since any top-level script
424
 * satisfaction ends with a single stack element, we know that for a full script:
425
 * - netdiff+1 is the maximal initial stack size (relevant for P2WSH stack limits).
426
 * - exec+1 is the maximal stack size reached during execution (relevant for P2TR stack limits).
427
 *
428
 * Mathematically, SatInfo forms a semiring:
429
 * - operator| is the semiring addition operator, with identity SatInfo{}, and which is commutative
430
 *   and associative.
431
 * - operator+ is the semiring multiplication operator, with identity SatInfo{0}, and which is
432
 *   associative.
433
 * - operator+ is distributive over operator|, so (a + (b | c)) = (a+b | a+c). This means we do not
434
 *   need to actually materialize all possible full execution traces over the whole script (which
435
 *   may be exponential in the length of the script); instead we can use the union operation at the
436
 *   individual subexpression level, and concatenate the result with subexpressions before and
437
 *   after it.
438
 * - It is not a commutative semiring, because a+b can differ from b+a. For example, "OP_1 OP_DROP"
439
 *   has exec=1, while "OP_DROP OP_1" has exec=0.
440
 */
441
class SatInfo
442
{
443
    //! Whether a canonical satisfaction/dissatisfaction is possible at all.
444
    bool valid;
445
    //! How much higher the stack size at start of execution can be compared to at the end.
446
    int32_t netdiff;
447
    //! How much higher the stack size can be during execution compared to at the end.
448
    int32_t exec;
449
450
public:
451
    /** Empty script set. */
452
0
    constexpr SatInfo() noexcept : valid(false), netdiff(0), exec(0) {}
453
454
    /** Script set with a single script in it, with specified netdiff and exec. */
455
    constexpr SatInfo(int32_t in_netdiff, int32_t in_exec) noexcept :
456
0
        valid{true}, netdiff{in_netdiff}, exec{in_exec} {}
457
458
0
    bool Valid() const { return valid; }
459
0
    int32_t NetDiff() const { return netdiff; }
460
0
    int32_t Exec() const { return exec; }
461
462
    /** Script set union. */
463
    constexpr friend SatInfo operator|(const SatInfo& a, const SatInfo& b) noexcept
464
0
    {
465
        // Union with an empty set is itself.
466
0
        if (!a.valid) return b;
  Branch (466:13): [True: 0, False: 0]
467
0
        if (!b.valid) return a;
  Branch (467:13): [True: 0, False: 0]
468
        // Otherwise the netdiff and exec of the union is the maximum of the individual values.
469
0
        return {std::max(a.netdiff, b.netdiff), std::max(a.exec, b.exec)};
470
0
    }
471
472
    /** Script set concatenation. */
473
    constexpr friend SatInfo operator+(const SatInfo& a, const SatInfo& b) noexcept
474
0
    {
475
        // Concatenation with an empty set yields an empty set.
476
0
        if (!a.valid || !b.valid) return {};
  Branch (476:13): [True: 0, False: 0]
  Branch (476:25): [True: 0, False: 0]
477
        // Otherwise, the maximum stack size difference for the combined scripts is the sum of the
478
        // netdiffs, and the maximum stack size difference anywhere is either b.exec (if the
479
        // maximum occurred in b) or b.netdiff+a.exec (if the maximum occurred in a).
480
0
        return {a.netdiff + b.netdiff, std::max(b.exec, b.netdiff + a.exec)};
481
0
    }
482
483
    /** The empty script. */
484
0
    static constexpr SatInfo Empty() noexcept { return {0, 0}; }
485
    /** A script consisting of a single push opcode. */
486
0
    static constexpr SatInfo Push() noexcept { return {-1, 0}; }
487
    /** A script consisting of a single hash opcode. */
488
0
    static constexpr SatInfo Hash() noexcept { return {0, 0}; }
489
    /** A script consisting of just a repurposed nop (OP_CHECKLOCKTIMEVERIFY, OP_CHECKSEQUENCEVERIFY). */
490
0
    static constexpr SatInfo Nop() noexcept { return {0, 0}; }
491
    /** A script consisting of just OP_IF or OP_NOTIF. Note that OP_ELSE and OP_ENDIF have no stack effect. */
492
0
    static constexpr SatInfo If() noexcept { return {1, 1}; }
493
    /** A script consisting of just a binary operator (OP_BOOLAND, OP_BOOLOR, OP_ADD). */
494
0
    static constexpr SatInfo BinaryOp() noexcept { return {1, 1}; }
495
496
    // Scripts for specific individual opcodes.
497
0
    static constexpr SatInfo OP_DUP() noexcept { return {-1, 0}; }
498
0
    static constexpr SatInfo OP_IFDUP(bool nonzero) noexcept { return {nonzero ? -1 : 0, 0}; }
  Branch (498:72): [True: 0, False: 0]
499
0
    static constexpr SatInfo OP_EQUALVERIFY() noexcept { return {2, 2}; }
500
0
    static constexpr SatInfo OP_EQUAL() noexcept { return {1, 1}; }
501
0
    static constexpr SatInfo OP_SIZE() noexcept { return {-1, 0}; }
502
0
    static constexpr SatInfo OP_CHECKSIG() noexcept { return {1, 1}; }
503
0
    static constexpr SatInfo OP_0NOTEQUAL() noexcept { return {0, 0}; }
504
0
    static constexpr SatInfo OP_VERIFY() noexcept { return {1, 1}; }
505
};
506
507
class StackSize
508
{
509
    SatInfo sat, dsat;
510
511
public:
512
0
    constexpr StackSize(SatInfo in_sat, SatInfo in_dsat) noexcept : sat(in_sat), dsat(in_dsat) {};
513
0
    constexpr StackSize(SatInfo in_both) noexcept : sat(in_both), dsat(in_both) {};
514
515
0
    const SatInfo& Sat() const { return sat; }
516
0
    const SatInfo& Dsat() const { return dsat; }
517
};
518
519
struct WitnessSize {
520
    //! Maximum witness size to satisfy;
521
    MaxInt<uint32_t> sat;
522
    //! Maximum witness size to dissatisfy;
523
    MaxInt<uint32_t> dsat;
524
525
0
    WitnessSize(MaxInt<uint32_t> in_sat, MaxInt<uint32_t> in_dsat) : sat(in_sat), dsat(in_dsat) {};
526
};
527
528
struct NoDupCheck {};
529
530
} // namespace internal
531
532
//! A node in a miniscript expression.
533
template <typename Key>
534
class Node
535
{
536
    //! What node type this node is.
537
    enum Fragment fragment;
538
    //! The k parameter (time for OLDER/AFTER, threshold for THRESH(_M))
539
    uint32_t k = 0;
540
    //! The keys used by this expression (only for PK_K/PK_H/MULTI)
541
    std::vector<Key> keys;
542
    //! The data bytes in this expression (only for HASH160/HASH256/SHA256/RIPEMD160).
543
    std::vector<unsigned char> data;
544
    //! Subexpressions (for WRAP_*/AND_*/OR_*/ANDOR/THRESH)
545
    std::vector<Node> subs;
546
    //! The Script context for this node. Either P2WSH or Tapscript.
547
    MiniscriptContext m_script_ctx;
548
549
public:
550
    // Permit 1 level deep recursion since we own instances of our own type.
551
    // NOLINTBEGIN(misc-no-recursion)
552
    ~Node()
553
0
    {
554
        // Destroy the subexpressions iteratively after moving out their
555
        // subexpressions to avoid a stack-overflow due to recursive calls to
556
        // the subs' destructors.
557
        // We move vectors in order to only update array-pointers inside them
558
        // rather than moving individual Node instances which would involve
559
        // moving/copying each Node field.
560
0
        std::vector<std::vector<Node>> queue;
561
0
        queue.push_back(std::move(subs));
562
0
        do {
563
0
            auto flattening{std::move(queue.back())};
564
0
            queue.pop_back();
565
0
            for (Node& n : flattening) {
  Branch (565:26): [True: 0, False: 0]
  Branch (565:26): [True: 0, False: 0]
  Branch (565:26): [True: 0, False: 0]
  Branch (565:26): [True: 0, False: 0]
566
0
                if (!n.subs.empty()) queue.push_back(std::move(n.subs));
  Branch (566:21): [True: 0, False: 0]
  Branch (566:21): [True: 0, False: 0]
  Branch (566:21): [True: 0, False: 0]
  Branch (566:21): [True: 0, False: 0]
567
0
            }
568
0
        } while (!queue.empty());
  Branch (568:18): [True: 0, False: 0]
  Branch (568:18): [True: 0, False: 0]
  Branch (568:18): [True: 0, False: 0]
  Branch (568:18): [True: 0, False: 0]
569
0
    }
Unexecuted instantiation: _ZN10miniscript4NodeI7CPubKeyED2Ev
Unexecuted instantiation: miniscript.cpp:_ZN10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEED2Ev
Unexecuted instantiation: _ZN10miniscript4NodeIjED2Ev
Unexecuted instantiation: _ZN10miniscript4NodeI11XOnlyPubKeyED2Ev
570
    // NOLINTEND(misc-no-recursion)
571
572
    Node<Key> Clone() const
573
0
    {
574
        // Use TreeEval() to avoid a stack-overflow due to recursion
575
0
        auto upfn = [](const Node& node, std::span<Node> children) {
576
0
            std::vector<Node> new_subs;
577
0
            for (auto& child : children) {
  Branch (577:30): [True: 0, False: 0]
578
                // It's fine to move from children as they are new nodes having
579
                // been produced by calling this function one level down.
580
0
                new_subs.push_back(std::move(child));
581
0
            }
582
0
            return Node{internal::NoDupCheck{}, node.m_script_ctx, node.fragment, std::move(new_subs), node.keys, node.data, node.k};
583
0
        };
584
0
        return TreeEval<Node>(upfn);
585
0
    }
586
587
0
    enum Fragment Fragment() const { return fragment; }
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE8FragmentEv
Unexecuted instantiation: _ZNK10miniscript4NodeIjE8FragmentEv
588
0
    uint32_t K() const { return k; }
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE1KEv
Unexecuted instantiation: _ZNK10miniscript4NodeIjE1KEv
589
0
    const std::vector<Key>& Keys() const { return keys; }
590
0
    const std::vector<unsigned char>& Data() const { return data; }
591
0
    const std::vector<Node>& Subs() const { return subs; }
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE4SubsEv
Unexecuted instantiation: _ZNK10miniscript4NodeIjE4SubsEv
592
593
private:
594
    //! Cached ops counts.
595
    internal::Ops ops;
596
    //! Cached stack size bounds.
597
    internal::StackSize ss;
598
    //! Cached witness size bounds.
599
    internal::WitnessSize ws;
600
    //! Cached expression type (computed by CalcType and fed through SanitizeType).
601
    Type typ;
602
    //! Cached script length (computed by CalcScriptLen).
603
    size_t scriptlen;
604
    //! Whether a public key appears more than once in this node. This value is initialized
605
    //! by all constructors except the NoDupCheck ones. The NoDupCheck ones skip the
606
    //! computation, requiring it to be done manually by invoking DuplicateKeyCheck().
607
    //! DuplicateKeyCheck(), or a non-NoDupCheck constructor, will compute has_duplicate_keys
608
    //! for all subnodes as well.
609
    mutable std::optional<bool> has_duplicate_keys;
610
611
    // Constructor which takes all of the data that a Node could possibly contain.
612
    // This is kept private as no valid fragment has all of these arguments.
613
    // Only used by Clone()
614
    Node(internal::NoDupCheck, MiniscriptContext script_ctx, enum Fragment nt, std::vector<Node> sub, std::vector<Key> key, std::vector<unsigned char> arg, uint32_t val)
615
0
        : fragment(nt), k(val), keys(std::move(key)), data(std::move(arg)), subs(std::move(sub)), m_script_ctx{script_ctx}, ops(CalcOps()), ss(CalcStackSize()), ws(CalcWitnessSize()), typ(CalcType()), scriptlen(CalcScriptLen()) {}
616
617
    //! Compute the length of the script for this miniscript (including children).
618
    size_t CalcScriptLen() const
619
0
    {
620
0
        size_t subsize = 0;
621
0
        for (const auto& sub : subs) {
  Branch (621:30): [True: 0, False: 0]
  Branch (621:30): [True: 0, False: 0]
  Branch (621:30): [True: 0, False: 0]
  Branch (621:30): [True: 0, False: 0]
622
0
            subsize += sub.ScriptSize();
623
0
        }
624
0
        Type sub0type = subs.size() > 0 ? subs[0].GetType() : ""_mst;
  Branch (624:25): [True: 0, False: 0]
  Branch (624:25): [True: 0, False: 0]
  Branch (624:25): [True: 0, False: 0]
  Branch (624:25): [True: 0, False: 0]
625
0
        return internal::ComputeScriptLen(fragment, sub0type, subsize, k, subs.size(), keys.size(), m_script_ctx);
626
0
    }
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE13CalcScriptLenEv
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEE13CalcScriptLenEv
Unexecuted instantiation: _ZNK10miniscript4NodeIjE13CalcScriptLenEv
Unexecuted instantiation: _ZNK10miniscript4NodeI11XOnlyPubKeyE13CalcScriptLenEv
627
628
    /* Apply a recursive algorithm to a Miniscript tree, without actual recursive calls.
629
     *
630
     * The algorithm is defined by two functions: downfn and upfn. Conceptually, the
631
     * result can be thought of as first using downfn to compute a "state" for each node,
632
     * from the root down to the leaves. Then upfn is used to compute a "result" for each
633
     * node, from the leaves back up to the root, which is then returned. In the actual
634
     * implementation, both functions are invoked in an interleaved fashion, performing a
635
     * depth-first traversal of the tree.
636
     *
637
     * In more detail, it is invoked as node.TreeEvalMaybe<Result>(root, downfn, upfn):
638
     * - root is the state of the root node, of type State.
639
     * - downfn is a callable (State&, const Node&, size_t) -> State, which given a
640
     *   node, its state, and an index of one of its children, computes the state of that
641
     *   child. It can modify the state. Children of a given node will have downfn()
642
     *   called in order.
643
     * - upfn is a callable (State&&, const Node&, std::span<Result>) -> std::optional<Result>,
644
     *   which given a node, its state, and a span of the results of its children,
645
     *   computes the result of the node. If std::nullopt is returned by upfn,
646
     *   TreeEvalMaybe() immediately returns std::nullopt.
647
     * The return value of TreeEvalMaybe is the result of the root node.
648
     *
649
     * Result type cannot be bool due to the std::vector<bool> specialization.
650
     */
651
    template<typename Result, typename State, typename DownFn, typename UpFn>
652
    std::optional<Result> TreeEvalMaybe(State root_state, DownFn downfn, UpFn upfn) const
653
0
    {
654
        /** Entries of the explicit stack tracked in this algorithm. */
655
0
        struct StackElem
656
0
        {
657
0
            const Node& node; //!< The node being evaluated.
658
0
            size_t expanded; //!< How many children of this node have been expanded.
659
0
            State state; //!< The state for that node.
660
661
0
            StackElem(const Node& node_, size_t exp_, State&& state_) :
662
0
                node(node_), expanded(exp_), state(std::move(state_)) {}
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeI7CPubKeyE13TreeEvalMaybeI7CScriptbZNKS2_8ToScriptIN12_GLOBAL__N_113ParserContextEEES4_RKT_EUlbRKS2_mE_ZNKS2_8TreeEvalIS4_bRSD_ZNKS5_IS7_EES4_SA_EUlbSC_St4spanIS4_Lm18446744073709551615EEE_EES8_T0_OT1_T2_EUlObSC_SH_E_EESt8optionalIS8_ESJ_SK_SM_EN9StackElemC2ESC_mSN_
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeI7CPubKeyE13TreeEvalMaybeISt8optionalISt3setIS1_ZNKS2_17DuplicateKeyCheckIN12_GLOBAL__N_113ParserContextEEEvRKT_E4CompSaIS1_EEEZNKS2_8TreeEvalISF_ZNKS6_IS8_EEvSB_EUlRKS2_St4spanISF_Lm18446744073709551615EEE_EES9_T0_E10DummyStateZNKSG_ISF_SL_EES9_SM_EUlSN_SI_mE_ZNKSG_ISF_SL_EES9_SM_EUlSN_SI_SK_E_EES4_IS9_ESM_T1_T2_EN9StackElemC2ESI_mOSN_
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeI7CPubKeyE13TreeEvalMaybeINS_8internal11InputResultEZNKS2_8TreeEvalIS5_ZNKS2_12ProduceInputIN12_GLOBAL__N_116SatisfierContextEEES5_RKT_EUlRKS2_St4spanIS5_Lm18446744073709551615EEE0_EESA_T0_E10DummyStateZNKS6_IS5_SH_EESA_SI_EUlSJ_SE_mE_ZNKS6_IS5_SH_EESA_SI_EUlSJ_SE_SG_E_EESt8optionalISA_ESI_T1_T2_EN9StackElemC2ESE_mOSJ_
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeI7CPubKeyE13TreeEvalMaybeIiZNKS2_8TreeEvalIiZNKS2_13IsSatisfiableIZN12_GLOBAL__N_18TestNodeENS_17MiniscriptContextERKSt8optionalIS2_ER18FuzzedDataProviderE3$_0EEbT_EUlRKS2_St4spanIiLm18446744073709551615EEE_EESF_T0_E10DummyStateZNKS4_IiSK_EESF_SL_EUlSM_SH_mE_ZNKS4_IiSK_EESF_SL_EUlSM_SH_SJ_E_EES8_ISF_ESL_T1_T2_EN9StackElemC2ESH_mOSM_
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeI7CPubKeyE13TreeEvalMaybeINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEbZNKS2_8ToStringIN12_GLOBAL__N_113ParserContextEEESt8optionalIS9_ERKT_RbEUlbRKS2_mE_ZNKSA_ISC_EESE_SH_SI_EUlbSK_St4spanIS9_Lm18446744073709551615EEE_EESD_ISF_ET0_T1_T2_EN9StackElemC2ESK_mOb
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeI7CPubKeyE13TreeEvalMaybeISt8optionalISt3setIS1_ZNKS2_17DuplicateKeyCheckIN12_GLOBAL__N_113KeyComparatorEEEvRKT_E4CompSaIS1_EEEZNKS2_8TreeEvalISF_ZNKS6_IS8_EEvSB_EUlRKS2_St4spanISF_Lm18446744073709551615EEE_EES9_T0_E10DummyStateZNKSG_ISF_SL_EES9_SM_EUlSN_SI_mE_ZNKSG_ISF_SL_EES9_SM_EUlSN_SI_SK_E_EES4_IS9_ESM_T1_T2_EN9StackElemC2ESI_mOSN_
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEE13TreeEvalMaybeISt8optionalISt3setIS3_ZNKS4_17DuplicateKeyCheckIS2_EEvRKT_E4CompSaIS3_EEEZNKS4_8TreeEvalISF_ZNKS8_IS2_EEvSB_EUlRKS4_St4spanISF_Lm18446744073709551615EEE_EES9_T0_E10DummyStateZNKSG_ISF_SL_EES9_SM_EUlSN_SI_mE_ZNKSG_ISF_SL_EES9_SM_EUlSN_SI_SK_E_EES6_IS9_ESM_T1_T2_EN9StackElemC2ESI_mOSN_
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEE13TreeEvalMaybeI7CScriptbZNKS4_8ToScriptIS2_EES6_RKT_EUlbRKS4_mE_ZNKS4_8TreeEvalIS6_bRSD_ZNKS7_IS2_EES6_SA_EUlbSC_St4spanIS6_Lm18446744073709551615EEE_EES8_T0_OT1_T2_EUlObSC_SH_E_EESt8optionalIS8_ESJ_SK_SM_EN9StackElemC2ESC_mSN_
Unexecuted instantiation: descriptor.cpp:_ZZNK10miniscript4NodeIjE13TreeEvalMaybeISt8optionalISt3setIjZNKS1_17DuplicateKeyCheckIN12_GLOBAL__N_19KeyParserEEEvRKT_E4CompSaIjEEEZNKS1_8TreeEvalISE_ZNKS5_IS7_EEvSA_EUlRKS1_St4spanISE_Lm18446744073709551615EEE_EES8_T0_E10DummyStateZNKSF_ISE_SK_EES8_SL_EUlSM_SH_mE_ZNKSF_ISE_SK_EES8_SL_EUlSM_SH_SJ_E_EES3_IS8_ESL_T1_T2_EN9StackElemC2ESH_mOSM_
Unexecuted instantiation: _ZZNK10miniscript4NodeIjE13TreeEvalMaybeIPKS1_ZNKS1_8TreeEvalIS4_ZNKS1_13FindInsaneSubEvEUlRS3_St4spanIS4_Lm18446744073709551615EEE_EET_T0_E10DummyStateZNKS5_IS4_S9_EESA_SB_EUlSC_S6_mE_ZNKS5_IS4_S9_EESA_SB_EUlSC_S6_S8_E_EESt8optionalISA_ESB_T1_T2_EN9StackElemC2ES6_mOSC_
Unexecuted instantiation: descriptor.cpp:_ZZNK10miniscript4NodeIjE13TreeEvalMaybeINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEbZNKS1_8ToStringIN12_GLOBAL__N_19KeyParserEEESt8optionalIS8_ERKT_RbEUlbRKS1_mE_ZNKS9_ISB_EESD_SG_SH_EUlbSJ_St4spanIS8_Lm18446744073709551615EEE_EESC_ISE_ET0_T1_T2_EN9StackElemC2ESJ_mOb
Unexecuted instantiation: descriptor.cpp:_ZZNK10miniscript4NodeIjE13TreeEvalMaybeI7CScriptbZNKS1_8ToScriptIN12_GLOBAL__N_111ScriptMakerEEES3_RKT_EUlbRKS1_mE_ZNKS1_8TreeEvalIS3_bRSC_ZNKS4_IS6_EES3_S9_EUlbSB_St4spanIS3_Lm18446744073709551615EEE_EES7_T0_OT1_T2_EUlObSB_SG_E_EESt8optionalIS7_ESI_SJ_SL_EN9StackElemC2ESB_mSM_
Unexecuted instantiation: descriptor.cpp:_ZZNK10miniscript4NodeIjE13TreeEvalMaybeINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEbZNKS1_8ToStringIN12_GLOBAL__N_111StringMakerEEESt8optionalIS8_ERKT_RbEUlbRKS1_mE_ZNKS9_ISB_EESD_SG_SH_EUlbSJ_St4spanIS8_Lm18446744073709551615EEE_EESC_ISE_ET0_T1_T2_EN9StackElemC2ESJ_mOb
Unexecuted instantiation: _ZZNK10miniscript4NodeIjE13TreeEvalMaybeIS1_ZNKS1_8TreeEvalIS1_ZNKS1_5CloneEvEUlRKS1_St4spanIS1_Lm18446744073709551615EEE_EET_T0_E10DummyStateZNKS3_IS1_S8_EES9_SA_EUlSB_S5_mE_ZNKS3_IS1_S8_EES9_SA_EUlSB_S5_S7_E_EESt8optionalIS9_ESA_T1_T2_EN9StackElemC2ES5_mOSB_
Unexecuted instantiation: _ZZNK10miniscript4NodeI11XOnlyPubKeyE13TreeEvalMaybeISt8optionalISt3setIS1_ZNKS2_17DuplicateKeyCheckI12TapSatisfierEEvRKT_E4CompSaIS1_EEEZNKS2_8TreeEvalISE_ZNKS6_IS7_EEvSA_EUlRKS2_St4spanISE_Lm18446744073709551615EEE_EES8_T0_E10DummyStateZNKSF_ISE_SK_EES8_SL_EUlSM_SH_mE_ZNKSF_ISE_SK_EES8_SL_EUlSM_SH_SJ_E_EES4_IS8_ESL_T1_T2_EN9StackElemC2ESH_mOSM_
Unexecuted instantiation: _ZZNK10miniscript4NodeI11XOnlyPubKeyE13TreeEvalMaybeINS_8internal11InputResultEZNKS2_8TreeEvalIS5_ZNKS2_12ProduceInputI12TapSatisfierEES5_RKT_EUlRKS2_St4spanIS5_Lm18446744073709551615EEE0_EES9_T0_E10DummyStateZNKS6_IS5_SG_EES9_SH_EUlSI_SD_mE_ZNKS6_IS5_SG_EES9_SH_EUlSI_SD_SF_E_EESt8optionalIS9_ESH_T1_T2_EN9StackElemC2ESD_mOSI_
Unexecuted instantiation: _ZZNK10miniscript4NodeI7CPubKeyE13TreeEvalMaybeISt8optionalISt3setIS1_ZNKS2_17DuplicateKeyCheckI12WshSatisfierEEvRKT_E4CompSaIS1_EEEZNKS2_8TreeEvalISE_ZNKS6_IS7_EEvSA_EUlRKS2_St4spanISE_Lm18446744073709551615EEE_EES8_T0_E10DummyStateZNKSF_ISE_SK_EES8_SL_EUlSM_SH_mE_ZNKSF_ISE_SK_EES8_SL_EUlSM_SH_SJ_E_EES4_IS8_ESL_T1_T2_EN9StackElemC2ESH_mOSM_
Unexecuted instantiation: _ZZNK10miniscript4NodeI7CPubKeyE13TreeEvalMaybeINS_8internal11InputResultEZNKS2_8TreeEvalIS5_ZNKS2_12ProduceInputI12WshSatisfierEES5_RKT_EUlRKS2_St4spanIS5_Lm18446744073709551615EEE0_EES9_T0_E10DummyStateZNKS6_IS5_SG_EES9_SH_EUlSI_SD_mE_ZNKS6_IS5_SG_EES9_SH_EUlSI_SD_SF_E_EESt8optionalIS9_ESH_T1_T2_EN9StackElemC2ESD_mOSI_
663
0
        };
664
        /* Stack of tree nodes being explored. */
665
0
        std::vector<StackElem> stack;
666
        /* Results of subtrees so far. Their order and mapping to tree nodes
667
         * is implicitly defined by stack. */
668
0
        std::vector<Result> results;
669
0
        stack.emplace_back(*this, 0, std::move(root_state));
670
671
        /* Here is a demonstration of the algorithm, for an example tree A(B,C(D,E),F).
672
         * State variables are omitted for simplicity.
673
         *
674
         * First: stack=[(A,0)] results=[]
675
         *        stack=[(A,1),(B,0)] results=[]
676
         *        stack=[(A,1)] results=[B]
677
         *        stack=[(A,2),(C,0)] results=[B]
678
         *        stack=[(A,2),(C,1),(D,0)] results=[B]
679
         *        stack=[(A,2),(C,1)] results=[B,D]
680
         *        stack=[(A,2),(C,2),(E,0)] results=[B,D]
681
         *        stack=[(A,2),(C,2)] results=[B,D,E]
682
         *        stack=[(A,2)] results=[B,C]
683
         *        stack=[(A,3),(F,0)] results=[B,C]
684
         *        stack=[(A,3)] results=[B,C,F]
685
         * Final: stack=[] results=[A]
686
         */
687
0
        while (stack.size()) {
  Branch (687:16): [True: 0, False: 0]
  Branch (687:16): [True: 0, False: 0]
  Branch (687:16): [True: 0, False: 0]
  Branch (687:16): [True: 0, False: 0]
  Branch (687:16): [True: 0, False: 0]
  Branch (687:16): [True: 0, False: 0]
  Branch (687:16): [True: 0, False: 0]
  Branch (687:16): [True: 0, False: 0]
  Branch (687:16): [True: 0, False: 0]
  Branch (687:16): [True: 0, False: 0]
  Branch (687:16): [True: 0, False: 0]
  Branch (687:16): [True: 0, False: 0]
  Branch (687:16): [True: 0, False: 0]
  Branch (687:16): [True: 0, False: 0]
  Branch (687:16): [True: 0, False: 0]
  Branch (687:16): [True: 0, False: 0]
  Branch (687:16): [True: 0, False: 0]
  Branch (687:16): [True: 0, False: 0]
688
0
            const Node& node = stack.back().node;
689
0
            if (stack.back().expanded < node.subs.size()) {
  Branch (689:17): [True: 0, False: 0]
  Branch (689:17): [True: 0, False: 0]
  Branch (689:17): [True: 0, False: 0]
  Branch (689:17): [True: 0, False: 0]
  Branch (689:17): [True: 0, False: 0]
  Branch (689:17): [True: 0, False: 0]
  Branch (689:17): [True: 0, False: 0]
  Branch (689:17): [True: 0, False: 0]
  Branch (689:17): [True: 0, False: 0]
  Branch (689:17): [True: 0, False: 0]
  Branch (689:17): [True: 0, False: 0]
  Branch (689:17): [True: 0, False: 0]
  Branch (689:17): [True: 0, False: 0]
  Branch (689:17): [True: 0, False: 0]
  Branch (689:17): [True: 0, False: 0]
  Branch (689:17): [True: 0, False: 0]
  Branch (689:17): [True: 0, False: 0]
  Branch (689:17): [True: 0, False: 0]
690
                /* We encounter a tree node with at least one unexpanded child.
691
                 * Expand it. By the time we hit this node again, the result of
692
                 * that child (and all earlier children) will be at the end of `results`. */
693
0
                size_t child_index = stack.back().expanded++;
694
0
                State child_state = downfn(stack.back().state, node, child_index);
695
0
                stack.emplace_back(node.subs[child_index], 0, std::move(child_state));
696
0
                continue;
697
0
            }
698
            // Invoke upfn with the last node.subs.size() elements of results as input.
699
0
            assert(results.size() >= node.subs.size());
  Branch (699:13): [True: 0, False: 0]
  Branch (699:13): [True: 0, False: 0]
  Branch (699:13): [True: 0, False: 0]
  Branch (699:13): [True: 0, False: 0]
  Branch (699:13): [True: 0, False: 0]
  Branch (699:13): [True: 0, False: 0]
  Branch (699:13): [True: 0, False: 0]
  Branch (699:13): [True: 0, False: 0]
  Branch (699:13): [True: 0, False: 0]
  Branch (699:13): [True: 0, False: 0]
  Branch (699:13): [True: 0, False: 0]
  Branch (699:13): [True: 0, False: 0]
  Branch (699:13): [True: 0, False: 0]
  Branch (699:13): [True: 0, False: 0]
  Branch (699:13): [True: 0, False: 0]
  Branch (699:13): [True: 0, False: 0]
  Branch (699:13): [True: 0, False: 0]
  Branch (699:13): [True: 0, False: 0]
700
0
            std::optional<Result> result{upfn(std::move(stack.back().state), node,
701
0
                std::span<Result>{results}.last(node.subs.size()))};
702
            // If evaluation returns std::nullopt, abort immediately.
703
0
            if (!result) return {};
  Branch (703:17): [True: 0, False: 0]
  Branch (703:17): [True: 0, False: 0]
  Branch (703:17): [True: 0, False: 0]
  Branch (703:17): [True: 0, False: 0]
  Branch (703:17): [True: 0, False: 0]
  Branch (703:17): [True: 0, False: 0]
  Branch (703:17): [True: 0, False: 0]
  Branch (703:17): [True: 0, False: 0]
  Branch (703:17): [True: 0, False: 0]
  Branch (703:17): [True: 0, False: 0]
  Branch (703:17): [True: 0, False: 0]
  Branch (703:17): [True: 0, False: 0]
  Branch (703:17): [True: 0, False: 0]
  Branch (703:17): [True: 0, False: 0]
  Branch (703:17): [True: 0, False: 0]
  Branch (703:17): [True: 0, False: 0]
  Branch (703:17): [True: 0, False: 0]
  Branch (703:17): [True: 0, False: 0]
704
            // Replace the last node.subs.size() elements of results with the new result.
705
0
            results.erase(results.end() - node.subs.size(), results.end());
706
0
            results.push_back(std::move(*result));
707
0
            stack.pop_back();
708
0
        }
709
        // The final remaining results element is the root result, return it.
710
0
        assert(results.size() >= 1);
  Branch (710:9): [True: 0, False: 0]
  Branch (710:9): [True: 0, False: 0]
  Branch (710:9): [True: 0, False: 0]
  Branch (710:9): [True: 0, False: 0]
  Branch (710:9): [True: 0, False: 0]
  Branch (710:9): [True: 0, False: 0]
  Branch (710:9): [True: 0, False: 0]
  Branch (710:9): [True: 0, False: 0]
  Branch (710:9): [True: 0, False: 0]
  Branch (710:9): [True: 0, False: 0]
  Branch (710:9): [True: 0, False: 0]
  Branch (710:9): [True: 0, False: 0]
  Branch (710:9): [True: 0, False: 0]
  Branch (710:9): [True: 0, False: 0]
  Branch (710:9): [True: 0, False: 0]
  Branch (710:9): [True: 0, False: 0]
  Branch (710:9): [True: 0, False: 0]
  Branch (710:9): [True: 0, False: 0]
711
0
        CHECK_NONFATAL(results.size() == 1);
712
0
        return std::move(results[0]);
713
0
    }
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeI7CPubKeyE13TreeEvalMaybeI7CScriptbZNKS2_8ToScriptIN12_GLOBAL__N_113ParserContextEEES4_RKT_EUlbRKS2_mE_ZNKS2_8TreeEvalIS4_bRSD_ZNKS5_IS7_EES4_SA_EUlbSC_St4spanIS4_Lm18446744073709551615EEE_EES8_T0_OT1_T2_EUlObSC_SH_E_EESt8optionalIS8_ESJ_SK_SM_
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeI7CPubKeyE13TreeEvalMaybeISt8optionalISt3setIS1_ZNKS2_17DuplicateKeyCheckIN12_GLOBAL__N_113ParserContextEEEvRKT_E4CompSaIS1_EEEZNKS2_8TreeEvalISF_ZNKS6_IS8_EEvSB_EUlRKS2_St4spanISF_Lm18446744073709551615EEE_EES9_T0_E10DummyStateZNKSG_ISF_SL_EES9_SM_EUlSN_SI_mE_ZNKSG_ISF_SL_EES9_SM_EUlSN_SI_SK_E_EES4_IS9_ESM_T1_T2_
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeI7CPubKeyE13TreeEvalMaybeINS_8internal11InputResultEZNKS2_8TreeEvalIS5_ZNKS2_12ProduceInputIN12_GLOBAL__N_116SatisfierContextEEES5_RKT_EUlRKS2_St4spanIS5_Lm18446744073709551615EEE0_EESA_T0_E10DummyStateZNKS6_IS5_SH_EESA_SI_EUlSJ_SE_mE_ZNKS6_IS5_SH_EESA_SI_EUlSJ_SE_SG_E_EESt8optionalISA_ESI_T1_T2_
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeI7CPubKeyE13TreeEvalMaybeIiZNKS2_8TreeEvalIiZNKS2_13IsSatisfiableIZN12_GLOBAL__N_18TestNodeENS_17MiniscriptContextERKSt8optionalIS2_ER18FuzzedDataProviderE3$_0EEbT_EUlRKS2_St4spanIiLm18446744073709551615EEE_EESF_T0_E10DummyStateZNKS4_IiSK_EESF_SL_EUlSM_SH_mE_ZNKS4_IiSK_EESF_SL_EUlSM_SH_SJ_E_EES8_ISF_ESL_T1_T2_
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeI7CPubKeyE13TreeEvalMaybeINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEbZNKS2_8ToStringIN12_GLOBAL__N_113ParserContextEEESt8optionalIS9_ERKT_RbEUlbRKS2_mE_ZNKSA_ISC_EESE_SH_SI_EUlbSK_St4spanIS9_Lm18446744073709551615EEE_EESD_ISF_ET0_T1_T2_
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeI7CPubKeyE13TreeEvalMaybeISt8optionalISt3setIS1_ZNKS2_17DuplicateKeyCheckIN12_GLOBAL__N_113KeyComparatorEEEvRKT_E4CompSaIS1_EEEZNKS2_8TreeEvalISF_ZNKS6_IS8_EEvSB_EUlRKS2_St4spanISF_Lm18446744073709551615EEE_EES9_T0_E10DummyStateZNKSG_ISF_SL_EES9_SM_EUlSN_SI_mE_ZNKSG_ISF_SL_EES9_SM_EUlSN_SI_SK_E_EES4_IS9_ESM_T1_T2_
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEE13TreeEvalMaybeISt8optionalISt3setIS3_ZNKS4_17DuplicateKeyCheckIS2_EEvRKT_E4CompSaIS3_EEEZNKS4_8TreeEvalISF_ZNKS8_IS2_EEvSB_EUlRKS4_St4spanISF_Lm18446744073709551615EEE_EES9_T0_E10DummyStateZNKSG_ISF_SL_EES9_SM_EUlSN_SI_mE_ZNKSG_ISF_SL_EES9_SM_EUlSN_SI_SK_E_EES6_IS9_ESM_T1_T2_
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEE13TreeEvalMaybeI7CScriptbZNKS4_8ToScriptIS2_EES6_RKT_EUlbRKS4_mE_ZNKS4_8TreeEvalIS6_bRSD_ZNKS7_IS2_EES6_SA_EUlbSC_St4spanIS6_Lm18446744073709551615EEE_EES8_T0_OT1_T2_EUlObSC_SH_E_EESt8optionalIS8_ESJ_SK_SM_
Unexecuted instantiation: descriptor.cpp:_ZNK10miniscript4NodeIjE13TreeEvalMaybeISt8optionalISt3setIjZNKS1_17DuplicateKeyCheckIN12_GLOBAL__N_19KeyParserEEEvRKT_E4CompSaIjEEEZNKS1_8TreeEvalISE_ZNKS5_IS7_EEvSA_EUlRKS1_St4spanISE_Lm18446744073709551615EEE_EES8_T0_E10DummyStateZNKSF_ISE_SK_EES8_SL_EUlSM_SH_mE_ZNKSF_ISE_SK_EES8_SL_EUlSM_SH_SJ_E_EES3_IS8_ESL_T1_T2_
Unexecuted instantiation: _ZNK10miniscript4NodeIjE13TreeEvalMaybeIPKS1_ZNKS1_8TreeEvalIS4_ZNKS1_13FindInsaneSubEvEUlRS3_St4spanIS4_Lm18446744073709551615EEE_EET_T0_E10DummyStateZNKS5_IS4_S9_EESA_SB_EUlSC_S6_mE_ZNKS5_IS4_S9_EESA_SB_EUlSC_S6_S8_E_EESt8optionalISA_ESB_T1_T2_
Unexecuted instantiation: descriptor.cpp:_ZNK10miniscript4NodeIjE13TreeEvalMaybeINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEbZNKS1_8ToStringIN12_GLOBAL__N_19KeyParserEEESt8optionalIS8_ERKT_RbEUlbRKS1_mE_ZNKS9_ISB_EESD_SG_SH_EUlbSJ_St4spanIS8_Lm18446744073709551615EEE_EESC_ISE_ET0_T1_T2_
Unexecuted instantiation: descriptor.cpp:_ZNK10miniscript4NodeIjE13TreeEvalMaybeI7CScriptbZNKS1_8ToScriptIN12_GLOBAL__N_111ScriptMakerEEES3_RKT_EUlbRKS1_mE_ZNKS1_8TreeEvalIS3_bRSC_ZNKS4_IS6_EES3_S9_EUlbSB_St4spanIS3_Lm18446744073709551615EEE_EES7_T0_OT1_T2_EUlObSB_SG_E_EESt8optionalIS7_ESI_SJ_SL_
Unexecuted instantiation: descriptor.cpp:_ZNK10miniscript4NodeIjE13TreeEvalMaybeINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEbZNKS1_8ToStringIN12_GLOBAL__N_111StringMakerEEESt8optionalIS8_ERKT_RbEUlbRKS1_mE_ZNKS9_ISB_EESD_SG_SH_EUlbSJ_St4spanIS8_Lm18446744073709551615EEE_EESC_ISE_ET0_T1_T2_
Unexecuted instantiation: _ZNK10miniscript4NodeIjE13TreeEvalMaybeIS1_ZNKS1_8TreeEvalIS1_ZNKS1_5CloneEvEUlRKS1_St4spanIS1_Lm18446744073709551615EEE_EET_T0_E10DummyStateZNKS3_IS1_S8_EES9_SA_EUlSB_S5_mE_ZNKS3_IS1_S8_EES9_SA_EUlSB_S5_S7_E_EESt8optionalIS9_ESA_T1_T2_
Unexecuted instantiation: _ZNK10miniscript4NodeI11XOnlyPubKeyE13TreeEvalMaybeISt8optionalISt3setIS1_ZNKS2_17DuplicateKeyCheckI12TapSatisfierEEvRKT_E4CompSaIS1_EEEZNKS2_8TreeEvalISE_ZNKS6_IS7_EEvSA_EUlRKS2_St4spanISE_Lm18446744073709551615EEE_EES8_T0_E10DummyStateZNKSF_ISE_SK_EES8_SL_EUlSM_SH_mE_ZNKSF_ISE_SK_EES8_SL_EUlSM_SH_SJ_E_EES4_IS8_ESL_T1_T2_
Unexecuted instantiation: _ZNK10miniscript4NodeI11XOnlyPubKeyE13TreeEvalMaybeINS_8internal11InputResultEZNKS2_8TreeEvalIS5_ZNKS2_12ProduceInputI12TapSatisfierEES5_RKT_EUlRKS2_St4spanIS5_Lm18446744073709551615EEE0_EES9_T0_E10DummyStateZNKS6_IS5_SG_EES9_SH_EUlSI_SD_mE_ZNKS6_IS5_SG_EES9_SH_EUlSI_SD_SF_E_EESt8optionalIS9_ESH_T1_T2_
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE13TreeEvalMaybeISt8optionalISt3setIS1_ZNKS2_17DuplicateKeyCheckI12WshSatisfierEEvRKT_E4CompSaIS1_EEEZNKS2_8TreeEvalISE_ZNKS6_IS7_EEvSA_EUlRKS2_St4spanISE_Lm18446744073709551615EEE_EES8_T0_E10DummyStateZNKSF_ISE_SK_EES8_SL_EUlSM_SH_mE_ZNKSF_ISE_SK_EES8_SL_EUlSM_SH_SJ_E_EES4_IS8_ESL_T1_T2_
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE13TreeEvalMaybeINS_8internal11InputResultEZNKS2_8TreeEvalIS5_ZNKS2_12ProduceInputI12WshSatisfierEES5_RKT_EUlRKS2_St4spanIS5_Lm18446744073709551615EEE0_EES9_T0_E10DummyStateZNKS6_IS5_SG_EES9_SH_EUlSI_SD_mE_ZNKS6_IS5_SG_EES9_SH_EUlSI_SD_SF_E_EESt8optionalIS9_ESH_T1_T2_
714
715
    /** Like TreeEvalMaybe, but without downfn or State type.
716
     * upfn takes (const Node&, std::span<Result>) and returns std::optional<Result>. */
717
    template<typename Result, typename UpFn>
718
    std::optional<Result> TreeEvalMaybe(UpFn upfn) const
719
    {
720
        struct DummyState {};
721
        return TreeEvalMaybe<Result>(DummyState{},
722
            [](DummyState, const Node&, size_t) { return DummyState{}; },
723
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
724
                return upfn(node, subs);
725
            }
726
        );
727
    }
728
729
    /** Like TreeEvalMaybe, but always produces a result. upfn must return Result. */
730
    template<typename Result, typename State, typename DownFn, typename UpFn>
731
    Result TreeEval(State root_state, DownFn&& downfn, UpFn upfn) const
732
0
    {
733
        // Invoke TreeEvalMaybe with upfn wrapped to return std::optional<Result>, and then
734
        // unconditionally dereference the result (it cannot be std::nullopt).
735
0
        return std::move(*TreeEvalMaybe<Result>(std::move(root_state),
736
0
            std::forward<DownFn>(downfn),
737
0
            [&upfn](State&& state, const Node& node, std::span<Result> subs) {
738
0
                Result res{upfn(std::move(state), node, subs)};
739
0
                return std::optional<Result>(std::move(res));
740
0
            }
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeI7CPubKeyE8TreeEvalI7CScriptbRZNKS2_8ToScriptIN12_GLOBAL__N_113ParserContextEEES4_RKT_EUlbRKS2_mE_ZNKS5_IS7_EES4_SA_EUlbSC_St4spanIS4_Lm18446744073709551615EEE_EES8_T0_OT1_T2_ENKUlObSC_SG_E_clESM_SC_SG_
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEE8TreeEvalI7CScriptbRZNKS4_8ToScriptIS2_EES6_RKT_EUlbRKS4_mE_ZNKS7_IS2_EES6_SA_EUlbSC_St4spanIS6_Lm18446744073709551615EEE_EES8_T0_OT1_T2_ENKUlObSC_SG_E_clESM_SC_SG_
Unexecuted instantiation: descriptor.cpp:_ZZNK10miniscript4NodeIjE8TreeEvalI7CScriptbRZNKS1_8ToScriptIN12_GLOBAL__N_111ScriptMakerEEES3_RKT_EUlbRKS1_mE_ZNKS4_IS6_EES3_S9_EUlbSB_St4spanIS3_Lm18446744073709551615EEE_EES7_T0_OT1_T2_ENKUlObSB_SF_E_clESL_SB_SF_
741
0
        ));
742
0
    }
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeI7CPubKeyE8TreeEvalI7CScriptbRZNKS2_8ToScriptIN12_GLOBAL__N_113ParserContextEEES4_RKT_EUlbRKS2_mE_ZNKS5_IS7_EES4_SA_EUlbSC_St4spanIS4_Lm18446744073709551615EEE_EES8_T0_OT1_T2_
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEE8TreeEvalI7CScriptbRZNKS4_8ToScriptIS2_EES6_RKT_EUlbRKS4_mE_ZNKS7_IS2_EES6_SA_EUlbSC_St4spanIS6_Lm18446744073709551615EEE_EES8_T0_OT1_T2_
Unexecuted instantiation: descriptor.cpp:_ZNK10miniscript4NodeIjE8TreeEvalI7CScriptbRZNKS1_8ToScriptIN12_GLOBAL__N_111ScriptMakerEEES3_RKT_EUlbRKS1_mE_ZNKS4_IS6_EES3_S9_EUlbSB_St4spanIS3_Lm18446744073709551615EEE_EES7_T0_OT1_T2_
743
744
    /** Like TreeEval, but without downfn or State type.
745
     *  upfn takes (const Node&, std::span<Result>) and returns Result. */
746
    template<typename Result, typename UpFn>
747
    Result TreeEval(UpFn upfn) const
748
0
    {
749
0
        struct DummyState {};
750
0
        return std::move(*TreeEvalMaybe<Result>(DummyState{},
751
0
            [](DummyState, const Node&, size_t) { return DummyState{}; },
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeI7CPubKeyE8TreeEvalISt8optionalISt3setIS1_ZNKS2_17DuplicateKeyCheckIN12_GLOBAL__N_113ParserContextEEEvRKT_E4CompSaIS1_EEEZNKS6_IS8_EEvSB_EUlRKS2_St4spanISF_Lm18446744073709551615EEE_EES9_T0_ENKUlZNKS3_ISF_SK_EES9_SL_E10DummyStateSH_mE_clESM_SH_m
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeI7CPubKeyE8TreeEvalINS_8internal11InputResultEZNKS2_12ProduceInputIN12_GLOBAL__N_116SatisfierContextEEES5_RKT_EUlRKS2_St4spanIS5_Lm18446744073709551615EEE0_EES9_T0_ENKUlZNKS3_IS5_SG_EES9_SH_E10DummyStateSD_mE_clESI_SD_m
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeI7CPubKeyE8TreeEvalIiZNKS2_13IsSatisfiableIZN12_GLOBAL__N_18TestNodeENS_17MiniscriptContextERKSt8optionalIS2_ER18FuzzedDataProviderE3$_0EEbT_EUlRKS2_St4spanIiLm18446744073709551615EEE_EESE_T0_ENKUlZNKS3_IiSJ_EESE_SK_E10DummyStateSG_mE_clESL_SG_m
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeI7CPubKeyE8TreeEvalISt8optionalISt3setIS1_ZNKS2_17DuplicateKeyCheckIN12_GLOBAL__N_113KeyComparatorEEEvRKT_E4CompSaIS1_EEEZNKS6_IS8_EEvSB_EUlRKS2_St4spanISF_Lm18446744073709551615EEE_EES9_T0_ENKUlZNKS3_ISF_SK_EES9_SL_E10DummyStateSH_mE_clESM_SH_m
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEE8TreeEvalISt8optionalISt3setIS3_ZNKS4_17DuplicateKeyCheckIS2_EEvRKT_E4CompSaIS3_EEEZNKS8_IS2_EEvSB_EUlRKS4_St4spanISF_Lm18446744073709551615EEE_EES9_T0_ENKUlZNKS5_ISF_SK_EES9_SL_E10DummyStateSH_mE_clESM_SH_m
Unexecuted instantiation: descriptor.cpp:_ZZNK10miniscript4NodeIjE8TreeEvalISt8optionalISt3setIjZNKS1_17DuplicateKeyCheckIN12_GLOBAL__N_19KeyParserEEEvRKT_E4CompSaIjEEEZNKS5_IS7_EEvSA_EUlRKS1_St4spanISE_Lm18446744073709551615EEE_EES8_T0_ENKUlZNKS2_ISE_SJ_EES8_SK_E10DummyStateSG_mE_clESL_SG_m
Unexecuted instantiation: _ZZNK10miniscript4NodeIjE8TreeEvalIPKS1_ZNKS1_13FindInsaneSubEvEUlRS3_St4spanIS4_Lm18446744073709551615EEE_EET_T0_ENKUlZNKS2_IS4_S8_EES9_SA_E10DummyStateS5_mE_clESB_S5_m
Unexecuted instantiation: _ZZNK10miniscript4NodeIjE8TreeEvalIS1_ZNKS1_5CloneEvEUlRKS1_St4spanIS1_Lm18446744073709551615EEE_EET_T0_ENKUlZNKS2_IS1_S7_EES8_S9_E10DummyStateS4_mE_clESA_S4_m
Unexecuted instantiation: _ZZNK10miniscript4NodeI11XOnlyPubKeyE8TreeEvalISt8optionalISt3setIS1_ZNKS2_17DuplicateKeyCheckI12TapSatisfierEEvRKT_E4CompSaIS1_EEEZNKS6_IS7_EEvSA_EUlRKS2_St4spanISE_Lm18446744073709551615EEE_EES8_T0_ENKUlZNKS3_ISE_SJ_EES8_SK_E10DummyStateSG_mE_clESL_SG_m
Unexecuted instantiation: _ZZNK10miniscript4NodeI11XOnlyPubKeyE8TreeEvalINS_8internal11InputResultEZNKS2_12ProduceInputI12TapSatisfierEES5_RKT_EUlRKS2_St4spanIS5_Lm18446744073709551615EEE0_EES8_T0_ENKUlZNKS3_IS5_SF_EES8_SG_E10DummyStateSC_mE_clESH_SC_m
Unexecuted instantiation: _ZZNK10miniscript4NodeI7CPubKeyE8TreeEvalISt8optionalISt3setIS1_ZNKS2_17DuplicateKeyCheckI12WshSatisfierEEvRKT_E4CompSaIS1_EEEZNKS6_IS7_EEvSA_EUlRKS2_St4spanISE_Lm18446744073709551615EEE_EES8_T0_ENKUlZNKS3_ISE_SJ_EES8_SK_E10DummyStateSG_mE_clESL_SG_m
Unexecuted instantiation: _ZZNK10miniscript4NodeI7CPubKeyE8TreeEvalINS_8internal11InputResultEZNKS2_12ProduceInputI12WshSatisfierEES5_RKT_EUlRKS2_St4spanIS5_Lm18446744073709551615EEE0_EES8_T0_ENKUlZNKS3_IS5_SF_EES8_SG_E10DummyStateSC_mE_clESH_SC_m
752
0
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
0
                Result res{upfn(node, subs)};
754
0
                return std::optional<Result>(std::move(res));
755
0
            }
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeI7CPubKeyE8TreeEvalISt8optionalISt3setIS1_ZNKS2_17DuplicateKeyCheckIN12_GLOBAL__N_113ParserContextEEEvRKT_E4CompSaIS1_EEEZNKS6_IS8_EEvSB_EUlRKS2_St4spanISF_Lm18446744073709551615EEE_EES9_T0_ENKUlZNKS3_ISF_SK_EES9_SL_E10DummyStateSH_SJ_E_clESM_SH_SJ_
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeI7CPubKeyE8TreeEvalINS_8internal11InputResultEZNKS2_12ProduceInputIN12_GLOBAL__N_116SatisfierContextEEES5_RKT_EUlRKS2_St4spanIS5_Lm18446744073709551615EEE0_EES9_T0_ENKUlZNKS3_IS5_SG_EES9_SH_E10DummyStateSD_SF_E_clESI_SD_SF_
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeI7CPubKeyE8TreeEvalIiZNKS2_13IsSatisfiableIZN12_GLOBAL__N_18TestNodeENS_17MiniscriptContextERKSt8optionalIS2_ER18FuzzedDataProviderE3$_0EEbT_EUlRKS2_St4spanIiLm18446744073709551615EEE_EESE_T0_ENKUlZNKS3_IiSJ_EESE_SK_E10DummyStateSG_SI_E_clESL_SG_SI_
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeI7CPubKeyE8TreeEvalISt8optionalISt3setIS1_ZNKS2_17DuplicateKeyCheckIN12_GLOBAL__N_113KeyComparatorEEEvRKT_E4CompSaIS1_EEEZNKS6_IS8_EEvSB_EUlRKS2_St4spanISF_Lm18446744073709551615EEE_EES9_T0_ENKUlZNKS3_ISF_SK_EES9_SL_E10DummyStateSH_SJ_E_clESM_SH_SJ_
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEE8TreeEvalISt8optionalISt3setIS3_ZNKS4_17DuplicateKeyCheckIS2_EEvRKT_E4CompSaIS3_EEEZNKS8_IS2_EEvSB_EUlRKS4_St4spanISF_Lm18446744073709551615EEE_EES9_T0_ENKUlZNKS5_ISF_SK_EES9_SL_E10DummyStateSH_SJ_E_clESM_SH_SJ_
Unexecuted instantiation: descriptor.cpp:_ZZNK10miniscript4NodeIjE8TreeEvalISt8optionalISt3setIjZNKS1_17DuplicateKeyCheckIN12_GLOBAL__N_19KeyParserEEEvRKT_E4CompSaIjEEEZNKS5_IS7_EEvSA_EUlRKS1_St4spanISE_Lm18446744073709551615EEE_EES8_T0_ENKUlZNKS2_ISE_SJ_EES8_SK_E10DummyStateSG_SI_E_clESL_SG_SI_
Unexecuted instantiation: _ZZNK10miniscript4NodeIjE8TreeEvalIPKS1_ZNKS1_13FindInsaneSubEvEUlRS3_St4spanIS4_Lm18446744073709551615EEE_EET_T0_ENKUlZNKS2_IS4_S8_EES9_SA_E10DummyStateS5_S7_E_clESB_S5_S7_
Unexecuted instantiation: _ZZNK10miniscript4NodeIjE8TreeEvalIS1_ZNKS1_5CloneEvEUlRKS1_St4spanIS1_Lm18446744073709551615EEE_EET_T0_ENKUlZNKS2_IS1_S7_EES8_S9_E10DummyStateS4_S6_E_clESA_S4_S6_
Unexecuted instantiation: _ZZNK10miniscript4NodeI11XOnlyPubKeyE8TreeEvalISt8optionalISt3setIS1_ZNKS2_17DuplicateKeyCheckI12TapSatisfierEEvRKT_E4CompSaIS1_EEEZNKS6_IS7_EEvSA_EUlRKS2_St4spanISE_Lm18446744073709551615EEE_EES8_T0_ENKUlZNKS3_ISE_SJ_EES8_SK_E10DummyStateSG_SI_E_clESL_SG_SI_
Unexecuted instantiation: _ZZNK10miniscript4NodeI11XOnlyPubKeyE8TreeEvalINS_8internal11InputResultEZNKS2_12ProduceInputI12TapSatisfierEES5_RKT_EUlRKS2_St4spanIS5_Lm18446744073709551615EEE0_EES8_T0_ENKUlZNKS3_IS5_SF_EES8_SG_E10DummyStateSC_SE_E_clESH_SC_SE_
Unexecuted instantiation: _ZZNK10miniscript4NodeI7CPubKeyE8TreeEvalISt8optionalISt3setIS1_ZNKS2_17DuplicateKeyCheckI12WshSatisfierEEvRKT_E4CompSaIS1_EEEZNKS6_IS7_EEvSA_EUlRKS2_St4spanISE_Lm18446744073709551615EEE_EES8_T0_ENKUlZNKS3_ISE_SJ_EES8_SK_E10DummyStateSG_SI_E_clESL_SG_SI_
Unexecuted instantiation: _ZZNK10miniscript4NodeI7CPubKeyE8TreeEvalINS_8internal11InputResultEZNKS2_12ProduceInputI12WshSatisfierEES5_RKT_EUlRKS2_St4spanIS5_Lm18446744073709551615EEE0_EES8_T0_ENKUlZNKS3_IS5_SF_EES8_SG_E10DummyStateSC_SE_E_clESH_SC_SE_
756
0
        ));
757
0
    }
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeI7CPubKeyE8TreeEvalISt8optionalISt3setIS1_ZNKS2_17DuplicateKeyCheckIN12_GLOBAL__N_113ParserContextEEEvRKT_E4CompSaIS1_EEEZNKS6_IS8_EEvSB_EUlRKS2_St4spanISF_Lm18446744073709551615EEE_EES9_T0_
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeI7CPubKeyE8TreeEvalINS_8internal11InputResultEZNKS2_12ProduceInputIN12_GLOBAL__N_116SatisfierContextEEES5_RKT_EUlRKS2_St4spanIS5_Lm18446744073709551615EEE0_EES9_T0_
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeI7CPubKeyE8TreeEvalIiZNKS2_13IsSatisfiableIZN12_GLOBAL__N_18TestNodeENS_17MiniscriptContextERKSt8optionalIS2_ER18FuzzedDataProviderE3$_0EEbT_EUlRKS2_St4spanIiLm18446744073709551615EEE_EESE_T0_
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeI7CPubKeyE8TreeEvalISt8optionalISt3setIS1_ZNKS2_17DuplicateKeyCheckIN12_GLOBAL__N_113KeyComparatorEEEvRKT_E4CompSaIS1_EEEZNKS6_IS8_EEvSB_EUlRKS2_St4spanISF_Lm18446744073709551615EEE_EES9_T0_
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEE8TreeEvalISt8optionalISt3setIS3_ZNKS4_17DuplicateKeyCheckIS2_EEvRKT_E4CompSaIS3_EEEZNKS8_IS2_EEvSB_EUlRKS4_St4spanISF_Lm18446744073709551615EEE_EES9_T0_
Unexecuted instantiation: descriptor.cpp:_ZNK10miniscript4NodeIjE8TreeEvalISt8optionalISt3setIjZNKS1_17DuplicateKeyCheckIN12_GLOBAL__N_19KeyParserEEEvRKT_E4CompSaIjEEEZNKS5_IS7_EEvSA_EUlRKS1_St4spanISE_Lm18446744073709551615EEE_EES8_T0_
Unexecuted instantiation: _ZNK10miniscript4NodeIjE8TreeEvalIPKS1_ZNKS1_13FindInsaneSubEvEUlRS3_St4spanIS4_Lm18446744073709551615EEE_EET_T0_
Unexecuted instantiation: _ZNK10miniscript4NodeIjE8TreeEvalIS1_ZNKS1_5CloneEvEUlRKS1_St4spanIS1_Lm18446744073709551615EEE_EET_T0_
Unexecuted instantiation: _ZNK10miniscript4NodeI11XOnlyPubKeyE8TreeEvalISt8optionalISt3setIS1_ZNKS2_17DuplicateKeyCheckI12TapSatisfierEEvRKT_E4CompSaIS1_EEEZNKS6_IS7_EEvSA_EUlRKS2_St4spanISE_Lm18446744073709551615EEE_EES8_T0_
Unexecuted instantiation: _ZNK10miniscript4NodeI11XOnlyPubKeyE8TreeEvalINS_8internal11InputResultEZNKS2_12ProduceInputI12TapSatisfierEES5_RKT_EUlRKS2_St4spanIS5_Lm18446744073709551615EEE0_EES8_T0_
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE8TreeEvalISt8optionalISt3setIS1_ZNKS2_17DuplicateKeyCheckI12WshSatisfierEEvRKT_E4CompSaIS1_EEEZNKS6_IS7_EEvSA_EUlRKS2_St4spanISE_Lm18446744073709551615EEE_EES8_T0_
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE8TreeEvalINS_8internal11InputResultEZNKS2_12ProduceInputI12WshSatisfierEES5_RKT_EUlRKS2_St4spanIS5_Lm18446744073709551615EEE0_EES8_T0_
758
759
    /** Compare two miniscript subtrees, using a non-recursive algorithm. */
760
    friend int Compare(const Node<Key>& node1, const Node<Key>& node2)
761
0
    {
762
0
        std::vector<std::pair<const Node<Key>&, const Node<Key>&>> queue;
763
0
        queue.emplace_back(node1, node2);
764
0
        while (!queue.empty()) {
  Branch (764:16): [True: 0, False: 0]
765
0
            const auto& [a, b] = queue.back();
766
0
            queue.pop_back();
767
0
            if (std::tie(a.fragment, a.k, a.keys, a.data) < std::tie(b.fragment, b.k, b.keys, b.data)) return -1;
  Branch (767:17): [True: 0, False: 0]
768
0
            if (std::tie(b.fragment, b.k, b.keys, b.data) < std::tie(a.fragment, a.k, a.keys, a.data)) return 1;
  Branch (768:17): [True: 0, False: 0]
769
0
            if (a.subs.size() < b.subs.size()) return -1;
  Branch (769:17): [True: 0, False: 0]
770
0
            if (b.subs.size() < a.subs.size()) return 1;
  Branch (770:17): [True: 0, False: 0]
771
0
            size_t n = a.subs.size();
772
0
            for (size_t i = 0; i < n; ++i) {
  Branch (772:32): [True: 0, False: 0]
773
0
                queue.emplace_back(a.subs[n - 1 - i], b.subs[n - 1 - i]);
774
0
            }
775
0
        }
776
0
        return 0;
777
0
    }
778
779
    //! Compute the type for this miniscript.
780
0
    Type CalcType() const {
781
0
        using namespace internal;
782
783
        // THRESH has a variable number of subexpressions
784
0
        std::vector<Type> sub_types;
785
0
        if (fragment == Fragment::THRESH) {
  Branch (785:13): [True: 0, False: 0]
  Branch (785:13): [True: 0, False: 0]
  Branch (785:13): [True: 0, False: 0]
  Branch (785:13): [True: 0, False: 0]
786
0
            for (const auto& sub : subs) sub_types.push_back(sub.GetType());
  Branch (786:34): [True: 0, False: 0]
  Branch (786:34): [True: 0, False: 0]
  Branch (786:34): [True: 0, False: 0]
  Branch (786:34): [True: 0, False: 0]
787
0
        }
788
        // All other nodes than THRESH can be computed just from the types of the 0-3 subexpressions.
789
0
        Type x = subs.size() > 0 ? subs[0].GetType() : ""_mst;
  Branch (789:18): [True: 0, False: 0]
  Branch (789:18): [True: 0, False: 0]
  Branch (789:18): [True: 0, False: 0]
  Branch (789:18): [True: 0, False: 0]
790
0
        Type y = subs.size() > 1 ? subs[1].GetType() : ""_mst;
  Branch (790:18): [True: 0, False: 0]
  Branch (790:18): [True: 0, False: 0]
  Branch (790:18): [True: 0, False: 0]
  Branch (790:18): [True: 0, False: 0]
791
0
        Type z = subs.size() > 2 ? subs[2].GetType() : ""_mst;
  Branch (791:18): [True: 0, False: 0]
  Branch (791:18): [True: 0, False: 0]
  Branch (791:18): [True: 0, False: 0]
  Branch (791:18): [True: 0, False: 0]
792
793
0
        return SanitizeType(ComputeType(fragment, x, y, z, sub_types, k, data.size(), subs.size(), keys.size(), m_script_ctx));
794
0
    }
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE8CalcTypeEv
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEE8CalcTypeEv
Unexecuted instantiation: _ZNK10miniscript4NodeIjE8CalcTypeEv
Unexecuted instantiation: _ZNK10miniscript4NodeI11XOnlyPubKeyE8CalcTypeEv
795
796
public:
797
    template<typename Ctx>
798
    CScript ToScript(const Ctx& ctx) const
799
0
    {
800
        // To construct the CScript for a Miniscript object, we use the TreeEval algorithm.
801
        // The State is a boolean: whether or not the node's script expansion is followed
802
        // by an OP_VERIFY (which may need to be combined with the last script opcode).
803
0
        auto downfn = [](bool verify, const Node& node, size_t index) {
804
            // For WRAP_V, the subexpression is certainly followed by OP_VERIFY.
805
0
            if (node.fragment == Fragment::WRAP_V) return true;
  Branch (805:17): [True: 0, False: 0]
  Branch (805:17): [True: 0, False: 0]
  Branch (805:17): [True: 0, False: 0]
806
            // The subexpression of WRAP_S, and the last subexpression of AND_V
807
            // inherit the followed-by-OP_VERIFY property from the parent.
808
0
            if (node.fragment == Fragment::WRAP_S ||
  Branch (808:17): [True: 0, False: 0]
  Branch (808:17): [True: 0, False: 0]
  Branch (808:17): [True: 0, False: 0]
809
0
                (node.fragment == Fragment::AND_V && index == 1)) return verify;
  Branch (809:18): [True: 0, False: 0]
  Branch (809:54): [True: 0, False: 0]
  Branch (809:18): [True: 0, False: 0]
  Branch (809:54): [True: 0, False: 0]
  Branch (809:18): [True: 0, False: 0]
  Branch (809:54): [True: 0, False: 0]
810
0
            return false;
811
0
        };
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeI7CPubKeyE8ToScriptIN12_GLOBAL__N_113ParserContextEEE7CScriptRKT_ENKUlbRKS2_mE_clEbSB_m
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEE8ToScriptIS2_EE7CScriptRKT_ENKUlbRKS4_mE_clEbSB_m
Unexecuted instantiation: descriptor.cpp:_ZZNK10miniscript4NodeIjE8ToScriptIN12_GLOBAL__N_111ScriptMakerEEE7CScriptRKT_ENKUlbRKS1_mE_clEbSA_m
812
        // The upward function computes for a node, given its followed-by-OP_VERIFY status
813
        // and the CScripts of its child nodes, the CScript of the node.
814
0
        const bool is_tapscript{IsTapscript(m_script_ctx)};
815
0
        auto upfn = [&ctx, is_tapscript](bool verify, const Node& node, std::span<CScript> subs) -> CScript {
816
0
            switch (node.fragment) {
  Branch (816:21): [True: 0, False: 0]
  Branch (816:21): [True: 0, False: 0]
  Branch (816:21): [True: 0, False: 0]
817
0
                case Fragment::PK_K: return BuildScript(ctx.ToPKBytes(node.keys[0]));
  Branch (817:17): [True: 0, False: 0]
  Branch (817:17): [True: 0, False: 0]
  Branch (817:17): [True: 0, False: 0]
818
0
                case Fragment::PK_H: return BuildScript(OP_DUP, OP_HASH160, ctx.ToPKHBytes(node.keys[0]), OP_EQUALVERIFY);
  Branch (818:17): [True: 0, False: 0]
  Branch (818:17): [True: 0, False: 0]
  Branch (818:17): [True: 0, False: 0]
819
0
                case Fragment::OLDER: return BuildScript(node.k, OP_CHECKSEQUENCEVERIFY);
  Branch (819:17): [True: 0, False: 0]
  Branch (819:17): [True: 0, False: 0]
  Branch (819:17): [True: 0, False: 0]
820
0
                case Fragment::AFTER: return BuildScript(node.k, OP_CHECKLOCKTIMEVERIFY);
  Branch (820:17): [True: 0, False: 0]
  Branch (820:17): [True: 0, False: 0]
  Branch (820:17): [True: 0, False: 0]
821
0
                case Fragment::SHA256: return BuildScript(OP_SIZE, 32, OP_EQUALVERIFY, OP_SHA256, node.data, verify ? OP_EQUALVERIFY : OP_EQUAL);
  Branch (821:17): [True: 0, False: 0]
  Branch (821:110): [True: 0, False: 0]
  Branch (821:17): [True: 0, False: 0]
  Branch (821:110): [True: 0, False: 0]
  Branch (821:17): [True: 0, False: 0]
  Branch (821:110): [True: 0, False: 0]
822
0
                case Fragment::RIPEMD160: return BuildScript(OP_SIZE, 32, OP_EQUALVERIFY, OP_RIPEMD160, node.data, verify ? OP_EQUALVERIFY : OP_EQUAL);
  Branch (822:17): [True: 0, False: 0]
  Branch (822:116): [True: 0, False: 0]
  Branch (822:17): [True: 0, False: 0]
  Branch (822:116): [True: 0, False: 0]
  Branch (822:17): [True: 0, False: 0]
  Branch (822:116): [True: 0, False: 0]
823
0
                case Fragment::HASH256: return BuildScript(OP_SIZE, 32, OP_EQUALVERIFY, OP_HASH256, node.data, verify ? OP_EQUALVERIFY : OP_EQUAL);
  Branch (823:17): [True: 0, False: 0]
  Branch (823:112): [True: 0, False: 0]
  Branch (823:17): [True: 0, False: 0]
  Branch (823:112): [True: 0, False: 0]
  Branch (823:17): [True: 0, False: 0]
  Branch (823:112): [True: 0, False: 0]
824
0
                case Fragment::HASH160: return BuildScript(OP_SIZE, 32, OP_EQUALVERIFY, OP_HASH160, node.data, verify ? OP_EQUALVERIFY : OP_EQUAL);
  Branch (824:17): [True: 0, False: 0]
  Branch (824:112): [True: 0, False: 0]
  Branch (824:17): [True: 0, False: 0]
  Branch (824:112): [True: 0, False: 0]
  Branch (824:17): [True: 0, False: 0]
  Branch (824:112): [True: 0, False: 0]
825
0
                case Fragment::WRAP_A: return BuildScript(OP_TOALTSTACK, subs[0], OP_FROMALTSTACK);
  Branch (825:17): [True: 0, False: 0]
  Branch (825:17): [True: 0, False: 0]
  Branch (825:17): [True: 0, False: 0]
826
0
                case Fragment::WRAP_S: return BuildScript(OP_SWAP, subs[0]);
  Branch (826:17): [True: 0, False: 0]
  Branch (826:17): [True: 0, False: 0]
  Branch (826:17): [True: 0, False: 0]
827
0
                case Fragment::WRAP_C: return BuildScript(std::move(subs[0]), verify ? OP_CHECKSIGVERIFY : OP_CHECKSIG);
  Branch (827:17): [True: 0, False: 0]
  Branch (827:79): [True: 0, False: 0]
  Branch (827:17): [True: 0, False: 0]
  Branch (827:79): [True: 0, False: 0]
  Branch (827:17): [True: 0, False: 0]
  Branch (827:79): [True: 0, False: 0]
828
0
                case Fragment::WRAP_D: return BuildScript(OP_DUP, OP_IF, subs[0], OP_ENDIF);
  Branch (828:17): [True: 0, False: 0]
  Branch (828:17): [True: 0, False: 0]
  Branch (828:17): [True: 0, False: 0]
829
0
                case Fragment::WRAP_V: {
  Branch (829:17): [True: 0, False: 0]
  Branch (829:17): [True: 0, False: 0]
  Branch (829:17): [True: 0, False: 0]
830
0
                    if (node.subs[0].GetType() << "x"_mst) {
  Branch (830:25): [True: 0, False: 0]
  Branch (830:25): [True: 0, False: 0]
  Branch (830:25): [True: 0, False: 0]
831
0
                        return BuildScript(std::move(subs[0]), OP_VERIFY);
832
0
                    } else {
833
0
                        return std::move(subs[0]);
834
0
                    }
835
0
                }
836
0
                case Fragment::WRAP_J: return BuildScript(OP_SIZE, OP_0NOTEQUAL, OP_IF, subs[0], OP_ENDIF);
  Branch (836:17): [True: 0, False: 0]
  Branch (836:17): [True: 0, False: 0]
  Branch (836:17): [True: 0, False: 0]
837
0
                case Fragment::WRAP_N: return BuildScript(std::move(subs[0]), OP_0NOTEQUAL);
  Branch (837:17): [True: 0, False: 0]
  Branch (837:17): [True: 0, False: 0]
  Branch (837:17): [True: 0, False: 0]
838
0
                case Fragment::JUST_1: return BuildScript(OP_1);
  Branch (838:17): [True: 0, False: 0]
  Branch (838:17): [True: 0, False: 0]
  Branch (838:17): [True: 0, False: 0]
839
0
                case Fragment::JUST_0: return BuildScript(OP_0);
  Branch (839:17): [True: 0, False: 0]
  Branch (839:17): [True: 0, False: 0]
  Branch (839:17): [True: 0, False: 0]
840
0
                case Fragment::AND_V: return BuildScript(std::move(subs[0]), subs[1]);
  Branch (840:17): [True: 0, False: 0]
  Branch (840:17): [True: 0, False: 0]
  Branch (840:17): [True: 0, False: 0]
841
0
                case Fragment::AND_B: return BuildScript(std::move(subs[0]), subs[1], OP_BOOLAND);
  Branch (841:17): [True: 0, False: 0]
  Branch (841:17): [True: 0, False: 0]
  Branch (841:17): [True: 0, False: 0]
842
0
                case Fragment::OR_B: return BuildScript(std::move(subs[0]), subs[1], OP_BOOLOR);
  Branch (842:17): [True: 0, False: 0]
  Branch (842:17): [True: 0, False: 0]
  Branch (842:17): [True: 0, False: 0]
843
0
                case Fragment::OR_D: return BuildScript(std::move(subs[0]), OP_IFDUP, OP_NOTIF, subs[1], OP_ENDIF);
  Branch (843:17): [True: 0, False: 0]
  Branch (843:17): [True: 0, False: 0]
  Branch (843:17): [True: 0, False: 0]
844
0
                case Fragment::OR_C: return BuildScript(std::move(subs[0]), OP_NOTIF, subs[1], OP_ENDIF);
  Branch (844:17): [True: 0, False: 0]
  Branch (844:17): [True: 0, False: 0]
  Branch (844:17): [True: 0, False: 0]
845
0
                case Fragment::OR_I: return BuildScript(OP_IF, subs[0], OP_ELSE, subs[1], OP_ENDIF);
  Branch (845:17): [True: 0, False: 0]
  Branch (845:17): [True: 0, False: 0]
  Branch (845:17): [True: 0, False: 0]
846
0
                case Fragment::ANDOR: return BuildScript(std::move(subs[0]), OP_NOTIF, subs[2], OP_ELSE, subs[1], OP_ENDIF);
  Branch (846:17): [True: 0, False: 0]
  Branch (846:17): [True: 0, False: 0]
  Branch (846:17): [True: 0, False: 0]
847
0
                case Fragment::MULTI: {
  Branch (847:17): [True: 0, False: 0]
  Branch (847:17): [True: 0, False: 0]
  Branch (847:17): [True: 0, False: 0]
848
0
                    CHECK_NONFATAL(!is_tapscript);
849
0
                    CScript script = BuildScript(node.k);
850
0
                    for (const auto& key : node.keys) {
  Branch (850:42): [True: 0, False: 0]
  Branch (850:42): [True: 0, False: 0]
  Branch (850:42): [True: 0, False: 0]
851
0
                        script = BuildScript(std::move(script), ctx.ToPKBytes(key));
852
0
                    }
853
0
                    return BuildScript(std::move(script), node.keys.size(), verify ? OP_CHECKMULTISIGVERIFY : OP_CHECKMULTISIG);
  Branch (853:77): [True: 0, False: 0]
  Branch (853:77): [True: 0, False: 0]
  Branch (853:77): [True: 0, False: 0]
854
0
                }
855
0
                case Fragment::MULTI_A: {
  Branch (855:17): [True: 0, False: 0]
  Branch (855:17): [True: 0, False: 0]
  Branch (855:17): [True: 0, False: 0]
856
0
                    CHECK_NONFATAL(is_tapscript);
857
0
                    CScript script = BuildScript(ctx.ToPKBytes(*node.keys.begin()), OP_CHECKSIG);
858
0
                    for (auto it = node.keys.begin() + 1; it != node.keys.end(); ++it) {
  Branch (858:59): [True: 0, False: 0]
  Branch (858:59): [True: 0, False: 0]
  Branch (858:59): [True: 0, False: 0]
859
0
                        script = BuildScript(std::move(script), ctx.ToPKBytes(*it), OP_CHECKSIGADD);
860
0
                    }
861
0
                    return BuildScript(std::move(script), node.k, verify ? OP_NUMEQUALVERIFY : OP_NUMEQUAL);
  Branch (861:67): [True: 0, False: 0]
  Branch (861:67): [True: 0, False: 0]
  Branch (861:67): [True: 0, False: 0]
862
0
                }
863
0
                case Fragment::THRESH: {
  Branch (863:17): [True: 0, False: 0]
  Branch (863:17): [True: 0, False: 0]
  Branch (863:17): [True: 0, False: 0]
864
0
                    CScript script = std::move(subs[0]);
865
0
                    for (size_t i = 1; i < subs.size(); ++i) {
  Branch (865:40): [True: 0, False: 0]
  Branch (865:40): [True: 0, False: 0]
  Branch (865:40): [True: 0, False: 0]
866
0
                        script = BuildScript(std::move(script), subs[i], OP_ADD);
867
0
                    }
868
0
                    return BuildScript(std::move(script), node.k, verify ? OP_EQUALVERIFY : OP_EQUAL);
  Branch (868:67): [True: 0, False: 0]
  Branch (868:67): [True: 0, False: 0]
  Branch (868:67): [True: 0, False: 0]
869
0
                }
870
0
            }
871
0
            assert(false);
  Branch (871:13): [Folded, False: 0]
  Branch (871:13): [Folded, False: 0]
  Branch (871:13): [Folded, False: 0]
872
0
        };
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeI7CPubKeyE8ToScriptIN12_GLOBAL__N_113ParserContextEEE7CScriptRKT_ENKUlbRKS2_St4spanIS6_Lm18446744073709551615EEE_clEbSB_SD_
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEE8ToScriptIS2_EE7CScriptRKT_ENKUlbRKS4_St4spanIS6_Lm18446744073709551615EEE_clEbSB_SD_
Unexecuted instantiation: descriptor.cpp:_ZZNK10miniscript4NodeIjE8ToScriptIN12_GLOBAL__N_111ScriptMakerEEE7CScriptRKT_ENKUlbRKS1_St4spanIS5_Lm18446744073709551615EEE_clEbSA_SC_
873
0
        return TreeEval<CScript>(false, downfn, upfn);
874
0
    }
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeI7CPubKeyE8ToScriptIN12_GLOBAL__N_113ParserContextEEE7CScriptRKT_
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEE8ToScriptIS2_EE7CScriptRKT_
Unexecuted instantiation: descriptor.cpp:_ZNK10miniscript4NodeIjE8ToScriptIN12_GLOBAL__N_111ScriptMakerEEE7CScriptRKT_
875
876
    template<typename CTx>
877
0
    std::optional<std::string> ToString(const CTx& ctx) const {
878
0
        bool dummy{false};
879
0
        return ToString(ctx, dummy);
880
0
    }
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeI7CPubKeyE8ToStringIN12_GLOBAL__N_113ParserContextEEESt8optionalINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEERKT_
Unexecuted instantiation: descriptor.cpp:_ZNK10miniscript4NodeIjE8ToStringIN12_GLOBAL__N_19KeyParserEEESt8optionalINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEERKT_
881
882
    template<typename CTx>
883
0
    std::optional<std::string> ToString(const CTx& ctx, bool& has_priv_key) const {
884
        // To construct the std::string representation for a Miniscript object, we use
885
        // the TreeEvalMaybe algorithm. The State is a boolean: whether the parent node is a
886
        // wrapper. If so, non-wrapper expressions must be prefixed with a ":".
887
0
        auto downfn = [](bool, const Node& node, size_t) {
888
0
            return (node.fragment == Fragment::WRAP_A || node.fragment == Fragment::WRAP_S ||
  Branch (888:21): [True: 0, False: 0]
  Branch (888:58): [True: 0, False: 0]
  Branch (888:21): [True: 0, False: 0]
  Branch (888:58): [True: 0, False: 0]
  Branch (888:21): [True: 0, False: 0]
  Branch (888:58): [True: 0, False: 0]
889
0
                    node.fragment == Fragment::WRAP_D || node.fragment == Fragment::WRAP_V ||
  Branch (889:21): [True: 0, False: 0]
  Branch (889:58): [True: 0, False: 0]
  Branch (889:21): [True: 0, False: 0]
  Branch (889:58): [True: 0, False: 0]
  Branch (889:21): [True: 0, False: 0]
  Branch (889:58): [True: 0, False: 0]
890
0
                    node.fragment == Fragment::WRAP_J || node.fragment == Fragment::WRAP_N ||
  Branch (890:21): [True: 0, False: 0]
  Branch (890:58): [True: 0, False: 0]
  Branch (890:21): [True: 0, False: 0]
  Branch (890:58): [True: 0, False: 0]
  Branch (890:21): [True: 0, False: 0]
  Branch (890:58): [True: 0, False: 0]
891
0
                    node.fragment == Fragment::WRAP_C ||
  Branch (891:21): [True: 0, False: 0]
  Branch (891:21): [True: 0, False: 0]
  Branch (891:21): [True: 0, False: 0]
892
0
                    (node.fragment == Fragment::AND_V && node.subs[1].fragment == Fragment::JUST_1) ||
  Branch (892:22): [True: 0, False: 0]
  Branch (892:58): [True: 0, False: 0]
  Branch (892:22): [True: 0, False: 0]
  Branch (892:58): [True: 0, False: 0]
  Branch (892:22): [True: 0, False: 0]
  Branch (892:58): [True: 0, False: 0]
893
0
                    (node.fragment == Fragment::OR_I && node.subs[0].fragment == Fragment::JUST_0) ||
  Branch (893:22): [True: 0, False: 0]
  Branch (893:57): [True: 0, False: 0]
  Branch (893:22): [True: 0, False: 0]
  Branch (893:57): [True: 0, False: 0]
  Branch (893:22): [True: 0, False: 0]
  Branch (893:57): [True: 0, False: 0]
894
0
                    (node.fragment == Fragment::OR_I && node.subs[1].fragment == Fragment::JUST_0));
  Branch (894:22): [True: 0, False: 0]
  Branch (894:57): [True: 0, False: 0]
  Branch (894:22): [True: 0, False: 0]
  Branch (894:57): [True: 0, False: 0]
  Branch (894:22): [True: 0, False: 0]
  Branch (894:57): [True: 0, False: 0]
895
0
        };
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeI7CPubKeyE8ToStringIN12_GLOBAL__N_113ParserContextEEESt8optionalINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEERKT_RbENKUlbRKS2_mE_clEbSJ_m
Unexecuted instantiation: descriptor.cpp:_ZZNK10miniscript4NodeIjE8ToStringIN12_GLOBAL__N_19KeyParserEEESt8optionalINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEERKT_RbENKUlbRKS1_mE_clEbSI_m
Unexecuted instantiation: descriptor.cpp:_ZZNK10miniscript4NodeIjE8ToStringIN12_GLOBAL__N_111StringMakerEEESt8optionalINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEERKT_RbENKUlbRKS1_mE_clEbSI_m
896
0
        auto toString = [&ctx, &has_priv_key](Key key) -> std::optional<std::string> {
897
0
            bool fragment_has_priv_key{false};
898
0
            auto key_str{ctx.ToString(key, fragment_has_priv_key)};
899
0
            if (key_str) has_priv_key = has_priv_key || fragment_has_priv_key;
  Branch (899:17): [True: 0, False: 0]
  Branch (899:41): [True: 0, False: 0]
  Branch (899:57): [True: 0, False: 0]
  Branch (899:17): [True: 0, False: 0]
  Branch (899:41): [True: 0, False: 0]
  Branch (899:57): [True: 0, False: 0]
  Branch (899:17): [True: 0, False: 0]
  Branch (899:41): [True: 0, False: 0]
  Branch (899:57): [True: 0, False: 0]
900
0
            return key_str;
901
0
        };
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeI7CPubKeyE8ToStringIN12_GLOBAL__N_113ParserContextEEESt8optionalINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEERKT_RbENKUlS1_E_clB5cxx11ES1_
Unexecuted instantiation: descriptor.cpp:_ZZNK10miniscript4NodeIjE8ToStringIN12_GLOBAL__N_19KeyParserEEESt8optionalINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEERKT_RbENKUljE_clB5cxx11Ej
Unexecuted instantiation: descriptor.cpp:_ZZNK10miniscript4NodeIjE8ToStringIN12_GLOBAL__N_111StringMakerEEESt8optionalINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEERKT_RbENKUljE_clB5cxx11Ej
902
        // The upward function computes for a node, given whether its parent is a wrapper,
903
        // and the string representations of its child nodes, the string representation of the node.
904
0
        const bool is_tapscript{IsTapscript(m_script_ctx)};
905
0
        auto upfn = [is_tapscript, &toString](bool wrapped, const Node& node, std::span<std::string> subs) -> std::optional<std::string> {
906
0
            std::string ret = wrapped ? ":" : "";
  Branch (906:31): [True: 0, False: 0]
  Branch (906:31): [True: 0, False: 0]
  Branch (906:31): [True: 0, False: 0]
907
908
0
            switch (node.fragment) {
909
0
                case Fragment::WRAP_A: return "a" + std::move(subs[0]);
  Branch (909:17): [True: 0, False: 0]
  Branch (909:17): [True: 0, False: 0]
  Branch (909:17): [True: 0, False: 0]
910
0
                case Fragment::WRAP_S: return "s" + std::move(subs[0]);
  Branch (910:17): [True: 0, False: 0]
  Branch (910:17): [True: 0, False: 0]
  Branch (910:17): [True: 0, False: 0]
911
0
                case Fragment::WRAP_C:
  Branch (911:17): [True: 0, False: 0]
  Branch (911:17): [True: 0, False: 0]
  Branch (911:17): [True: 0, False: 0]
912
0
                    if (node.subs[0].fragment == Fragment::PK_K) {
  Branch (912:25): [True: 0, False: 0]
  Branch (912:25): [True: 0, False: 0]
  Branch (912:25): [True: 0, False: 0]
913
                        // pk(K) is syntactic sugar for c:pk_k(K)
914
0
                        auto key_str = toString(node.subs[0].keys[0]);
915
0
                        if (!key_str) return {};
  Branch (915:29): [True: 0, False: 0]
  Branch (915:29): [True: 0, False: 0]
  Branch (915:29): [True: 0, False: 0]
916
0
                        return std::move(ret) + "pk(" + std::move(*key_str) + ")";
917
0
                    }
918
0
                    if (node.subs[0].fragment == Fragment::PK_H) {
  Branch (918:25): [True: 0, False: 0]
  Branch (918:25): [True: 0, False: 0]
  Branch (918:25): [True: 0, False: 0]
919
                        // pkh(K) is syntactic sugar for c:pk_h(K)
920
0
                        auto key_str = toString(node.subs[0].keys[0]);
921
0
                        if (!key_str) return {};
  Branch (921:29): [True: 0, False: 0]
  Branch (921:29): [True: 0, False: 0]
  Branch (921:29): [True: 0, False: 0]
922
0
                        return std::move(ret) + "pkh(" + std::move(*key_str) + ")";
923
0
                    }
924
0
                    return "c" + std::move(subs[0]);
925
0
                case Fragment::WRAP_D: return "d" + std::move(subs[0]);
  Branch (925:17): [True: 0, False: 0]
  Branch (925:17): [True: 0, False: 0]
  Branch (925:17): [True: 0, False: 0]
926
0
                case Fragment::WRAP_V: return "v" + std::move(subs[0]);
  Branch (926:17): [True: 0, False: 0]
  Branch (926:17): [True: 0, False: 0]
  Branch (926:17): [True: 0, False: 0]
927
0
                case Fragment::WRAP_J: return "j" + std::move(subs[0]);
  Branch (927:17): [True: 0, False: 0]
  Branch (927:17): [True: 0, False: 0]
  Branch (927:17): [True: 0, False: 0]
928
0
                case Fragment::WRAP_N: return "n" + std::move(subs[0]);
  Branch (928:17): [True: 0, False: 0]
  Branch (928:17): [True: 0, False: 0]
  Branch (928:17): [True: 0, False: 0]
929
0
                case Fragment::AND_V:
  Branch (929:17): [True: 0, False: 0]
  Branch (929:17): [True: 0, False: 0]
  Branch (929:17): [True: 0, False: 0]
930
                    // t:X is syntactic sugar for and_v(X,1).
931
0
                    if (node.subs[1].fragment == Fragment::JUST_1) return "t" + std::move(subs[0]);
  Branch (931:25): [True: 0, False: 0]
  Branch (931:25): [True: 0, False: 0]
  Branch (931:25): [True: 0, False: 0]
932
0
                    break;
933
0
                case Fragment::OR_I:
  Branch (933:17): [True: 0, False: 0]
  Branch (933:17): [True: 0, False: 0]
  Branch (933:17): [True: 0, False: 0]
934
0
                    if (node.subs[0].fragment == Fragment::JUST_0) return "l" + std::move(subs[1]);
  Branch (934:25): [True: 0, False: 0]
  Branch (934:25): [True: 0, False: 0]
  Branch (934:25): [True: 0, False: 0]
935
0
                    if (node.subs[1].fragment == Fragment::JUST_0) return "u" + std::move(subs[0]);
  Branch (935:25): [True: 0, False: 0]
  Branch (935:25): [True: 0, False: 0]
  Branch (935:25): [True: 0, False: 0]
936
0
                    break;
937
0
                default: break;
  Branch (937:17): [True: 0, False: 0]
  Branch (937:17): [True: 0, False: 0]
  Branch (937:17): [True: 0, False: 0]
938
0
            }
939
0
            switch (node.fragment) {
940
0
                case Fragment::PK_K: {
  Branch (940:17): [True: 0, False: 0]
  Branch (940:17): [True: 0, False: 0]
  Branch (940:17): [True: 0, False: 0]
941
0
                    auto key_str = toString(node.keys[0]);
942
0
                    if (!key_str) return {};
  Branch (942:25): [True: 0, False: 0]
  Branch (942:25): [True: 0, False: 0]
  Branch (942:25): [True: 0, False: 0]
943
0
                    return std::move(ret) + "pk_k(" + std::move(*key_str) + ")";
944
0
                }
945
0
                case Fragment::PK_H: {
  Branch (945:17): [True: 0, False: 0]
  Branch (945:17): [True: 0, False: 0]
  Branch (945:17): [True: 0, False: 0]
946
0
                    auto key_str = toString(node.keys[0]);
947
0
                    if (!key_str) return {};
  Branch (947:25): [True: 0, False: 0]
  Branch (947:25): [True: 0, False: 0]
  Branch (947:25): [True: 0, False: 0]
948
0
                    return std::move(ret) + "pk_h(" + std::move(*key_str) + ")";
949
0
                }
950
0
                case Fragment::AFTER: return std::move(ret) + "after(" + util::ToString(node.k) + ")";
  Branch (950:17): [True: 0, False: 0]
  Branch (950:17): [True: 0, False: 0]
  Branch (950:17): [True: 0, False: 0]
951
0
                case Fragment::OLDER: return std::move(ret) + "older(" + util::ToString(node.k) + ")";
  Branch (951:17): [True: 0, False: 0]
  Branch (951:17): [True: 0, False: 0]
  Branch (951:17): [True: 0, False: 0]
952
0
                case Fragment::HASH256: return std::move(ret) + "hash256(" + HexStr(node.data) + ")";
  Branch (952:17): [True: 0, False: 0]
  Branch (952:17): [True: 0, False: 0]
  Branch (952:17): [True: 0, False: 0]
953
0
                case Fragment::HASH160: return std::move(ret) + "hash160(" + HexStr(node.data) + ")";
  Branch (953:17): [True: 0, False: 0]
  Branch (953:17): [True: 0, False: 0]
  Branch (953:17): [True: 0, False: 0]
954
0
                case Fragment::SHA256: return std::move(ret) + "sha256(" + HexStr(node.data) + ")";
  Branch (954:17): [True: 0, False: 0]
  Branch (954:17): [True: 0, False: 0]
  Branch (954:17): [True: 0, False: 0]
955
0
                case Fragment::RIPEMD160: return std::move(ret) + "ripemd160(" + HexStr(node.data) + ")";
  Branch (955:17): [True: 0, False: 0]
  Branch (955:17): [True: 0, False: 0]
  Branch (955:17): [True: 0, False: 0]
956
0
                case Fragment::JUST_1: return std::move(ret) + "1";
  Branch (956:17): [True: 0, False: 0]
  Branch (956:17): [True: 0, False: 0]
  Branch (956:17): [True: 0, False: 0]
957
0
                case Fragment::JUST_0: return std::move(ret) + "0";
  Branch (957:17): [True: 0, False: 0]
  Branch (957:17): [True: 0, False: 0]
  Branch (957:17): [True: 0, False: 0]
958
0
                case Fragment::AND_V: return std::move(ret) + "and_v(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
  Branch (958:17): [True: 0, False: 0]
  Branch (958:17): [True: 0, False: 0]
  Branch (958:17): [True: 0, False: 0]
959
0
                case Fragment::AND_B: return std::move(ret) + "and_b(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
  Branch (959:17): [True: 0, False: 0]
  Branch (959:17): [True: 0, False: 0]
  Branch (959:17): [True: 0, False: 0]
960
0
                case Fragment::OR_B: return std::move(ret) + "or_b(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
  Branch (960:17): [True: 0, False: 0]
  Branch (960:17): [True: 0, False: 0]
  Branch (960:17): [True: 0, False: 0]
961
0
                case Fragment::OR_D: return std::move(ret) + "or_d(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
  Branch (961:17): [True: 0, False: 0]
  Branch (961:17): [True: 0, False: 0]
  Branch (961:17): [True: 0, False: 0]
962
0
                case Fragment::OR_C: return std::move(ret) + "or_c(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
  Branch (962:17): [True: 0, False: 0]
  Branch (962:17): [True: 0, False: 0]
  Branch (962:17): [True: 0, False: 0]
963
0
                case Fragment::OR_I: return std::move(ret) + "or_i(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
  Branch (963:17): [True: 0, False: 0]
  Branch (963:17): [True: 0, False: 0]
  Branch (963:17): [True: 0, False: 0]
964
0
                case Fragment::ANDOR:
  Branch (964:17): [True: 0, False: 0]
  Branch (964:17): [True: 0, False: 0]
  Branch (964:17): [True: 0, False: 0]
965
                    // and_n(X,Y) is syntactic sugar for andor(X,Y,0).
966
0
                    if (node.subs[2].fragment == Fragment::JUST_0) return std::move(ret) + "and_n(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
  Branch (966:25): [True: 0, False: 0]
  Branch (966:25): [True: 0, False: 0]
  Branch (966:25): [True: 0, False: 0]
967
0
                    return std::move(ret) + "andor(" + std::move(subs[0]) + "," + std::move(subs[1]) + "," + std::move(subs[2]) + ")";
968
0
                case Fragment::MULTI: {
  Branch (968:17): [True: 0, False: 0]
  Branch (968:17): [True: 0, False: 0]
  Branch (968:17): [True: 0, False: 0]
969
0
                    CHECK_NONFATAL(!is_tapscript);
970
0
                    auto str = std::move(ret) + "multi(" + util::ToString(node.k);
971
0
                    for (const auto& key : node.keys) {
  Branch (971:42): [True: 0, False: 0]
  Branch (971:42): [True: 0, False: 0]
  Branch (971:42): [True: 0, False: 0]
972
0
                        auto key_str = toString(key);
973
0
                        if (!key_str) return {};
  Branch (973:29): [True: 0, False: 0]
  Branch (973:29): [True: 0, False: 0]
  Branch (973:29): [True: 0, False: 0]
974
0
                        str += "," + std::move(*key_str);
975
0
                    }
976
0
                    return std::move(str) + ")";
977
0
                }
978
0
                case Fragment::MULTI_A: {
  Branch (978:17): [True: 0, False: 0]
  Branch (978:17): [True: 0, False: 0]
  Branch (978:17): [True: 0, False: 0]
979
0
                    CHECK_NONFATAL(is_tapscript);
980
0
                    auto str = std::move(ret) + "multi_a(" + util::ToString(node.k);
981
0
                    for (const auto& key : node.keys) {
  Branch (981:42): [True: 0, False: 0]
  Branch (981:42): [True: 0, False: 0]
  Branch (981:42): [True: 0, False: 0]
982
0
                        auto key_str = toString(key);
983
0
                        if (!key_str) return {};
  Branch (983:29): [True: 0, False: 0]
  Branch (983:29): [True: 0, False: 0]
  Branch (983:29): [True: 0, False: 0]
984
0
                        str += "," + std::move(*key_str);
985
0
                    }
986
0
                    return std::move(str) + ")";
987
0
                }
988
0
                case Fragment::THRESH: {
  Branch (988:17): [True: 0, False: 0]
  Branch (988:17): [True: 0, False: 0]
  Branch (988:17): [True: 0, False: 0]
989
0
                    auto str = std::move(ret) + "thresh(" + util::ToString(node.k);
990
0
                    for (auto& sub : subs) {
  Branch (990:36): [True: 0, False: 0]
  Branch (990:36): [True: 0, False: 0]
  Branch (990:36): [True: 0, False: 0]
991
0
                        str += "," + std::move(sub);
992
0
                    }
993
0
                    return std::move(str) + ")";
994
0
                }
995
0
                default: break;
  Branch (995:17): [True: 0, False: 0]
  Branch (995:17): [True: 0, False: 0]
  Branch (995:17): [True: 0, False: 0]
996
0
            }
997
0
            assert(false);
  Branch (997:13): [Folded, False: 0]
  Branch (997:13): [Folded, False: 0]
  Branch (997:13): [Folded, False: 0]
998
0
        };
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeI7CPubKeyE8ToStringIN12_GLOBAL__N_113ParserContextEEESt8optionalINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEERKT_RbENKUlbRKS2_St4spanISC_Lm18446744073709551615EEE_clB5cxx11EbSJ_SL_
Unexecuted instantiation: descriptor.cpp:_ZZNK10miniscript4NodeIjE8ToStringIN12_GLOBAL__N_19KeyParserEEESt8optionalINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEERKT_RbENKUlbRKS1_St4spanISB_Lm18446744073709551615EEE_clB5cxx11EbSI_SK_
Unexecuted instantiation: descriptor.cpp:_ZZNK10miniscript4NodeIjE8ToStringIN12_GLOBAL__N_111StringMakerEEESt8optionalINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEERKT_RbENKUlbRKS1_St4spanISB_Lm18446744073709551615EEE_clB5cxx11EbSI_SK_
999
1000
0
        return TreeEvalMaybe<std::string>(false, downfn, upfn);
1001
0
    }
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeI7CPubKeyE8ToStringIN12_GLOBAL__N_113ParserContextEEESt8optionalINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEERKT_Rb
Unexecuted instantiation: descriptor.cpp:_ZNK10miniscript4NodeIjE8ToStringIN12_GLOBAL__N_19KeyParserEEESt8optionalINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEERKT_Rb
Unexecuted instantiation: descriptor.cpp:_ZNK10miniscript4NodeIjE8ToStringIN12_GLOBAL__N_111StringMakerEEESt8optionalINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEERKT_Rb
1002
1003
private:
1004
0
    internal::Ops CalcOps() const {
1005
0
        switch (fragment) {
  Branch (1005:17): [True: 0, False: 0]
  Branch (1005:17): [True: 0, False: 0]
  Branch (1005:17): [True: 0, False: 0]
  Branch (1005:17): [True: 0, False: 0]
1006
0
            case Fragment::JUST_1: return {0, 0, {}};
  Branch (1006:13): [True: 0, False: 0]
  Branch (1006:13): [True: 0, False: 0]
  Branch (1006:13): [True: 0, False: 0]
  Branch (1006:13): [True: 0, False: 0]
1007
0
            case Fragment::JUST_0: return {0, {}, 0};
  Branch (1007:13): [True: 0, False: 0]
  Branch (1007:13): [True: 0, False: 0]
  Branch (1007:13): [True: 0, False: 0]
  Branch (1007:13): [True: 0, False: 0]
1008
0
            case Fragment::PK_K: return {0, 0, 0};
  Branch (1008:13): [True: 0, False: 0]
  Branch (1008:13): [True: 0, False: 0]
  Branch (1008:13): [True: 0, False: 0]
  Branch (1008:13): [True: 0, False: 0]
1009
0
            case Fragment::PK_H: return {3, 0, 0};
  Branch (1009:13): [True: 0, False: 0]
  Branch (1009:13): [True: 0, False: 0]
  Branch (1009:13): [True: 0, False: 0]
  Branch (1009:13): [True: 0, False: 0]
1010
0
            case Fragment::OLDER:
  Branch (1010:13): [True: 0, False: 0]
  Branch (1010:13): [True: 0, False: 0]
  Branch (1010:13): [True: 0, False: 0]
  Branch (1010:13): [True: 0, False: 0]
1011
0
            case Fragment::AFTER: return {1, 0, {}};
  Branch (1011:13): [True: 0, False: 0]
  Branch (1011:13): [True: 0, False: 0]
  Branch (1011:13): [True: 0, False: 0]
  Branch (1011:13): [True: 0, False: 0]
1012
0
            case Fragment::SHA256:
  Branch (1012:13): [True: 0, False: 0]
  Branch (1012:13): [True: 0, False: 0]
  Branch (1012:13): [True: 0, False: 0]
  Branch (1012:13): [True: 0, False: 0]
1013
0
            case Fragment::RIPEMD160:
  Branch (1013:13): [True: 0, False: 0]
  Branch (1013:13): [True: 0, False: 0]
  Branch (1013:13): [True: 0, False: 0]
  Branch (1013:13): [True: 0, False: 0]
1014
0
            case Fragment::HASH256:
  Branch (1014:13): [True: 0, False: 0]
  Branch (1014:13): [True: 0, False: 0]
  Branch (1014:13): [True: 0, False: 0]
  Branch (1014:13): [True: 0, False: 0]
1015
0
            case Fragment::HASH160: return {4, 0, {}};
  Branch (1015:13): [True: 0, False: 0]
  Branch (1015:13): [True: 0, False: 0]
  Branch (1015:13): [True: 0, False: 0]
  Branch (1015:13): [True: 0, False: 0]
1016
0
            case Fragment::AND_V: return {subs[0].ops.count + subs[1].ops.count, subs[0].ops.sat + subs[1].ops.sat, {}};
  Branch (1016:13): [True: 0, False: 0]
  Branch (1016:13): [True: 0, False: 0]
  Branch (1016:13): [True: 0, False: 0]
  Branch (1016:13): [True: 0, False: 0]
1017
0
            case Fragment::AND_B: {
  Branch (1017:13): [True: 0, False: 0]
  Branch (1017:13): [True: 0, False: 0]
  Branch (1017:13): [True: 0, False: 0]
  Branch (1017:13): [True: 0, False: 0]
1018
0
                const auto count{1 + subs[0].ops.count + subs[1].ops.count};
1019
0
                const auto sat{subs[0].ops.sat + subs[1].ops.sat};
1020
0
                const auto dsat{subs[0].ops.dsat + subs[1].ops.dsat};
1021
0
                return {count, sat, dsat};
1022
0
            }
1023
0
            case Fragment::OR_B: {
  Branch (1023:13): [True: 0, False: 0]
  Branch (1023:13): [True: 0, False: 0]
  Branch (1023:13): [True: 0, False: 0]
  Branch (1023:13): [True: 0, False: 0]
1024
0
                const auto count{1 + subs[0].ops.count + subs[1].ops.count};
1025
0
                const auto sat{(subs[0].ops.sat + subs[1].ops.dsat) | (subs[1].ops.sat + subs[0].ops.dsat)};
1026
0
                const auto dsat{subs[0].ops.dsat + subs[1].ops.dsat};
1027
0
                return {count, sat, dsat};
1028
0
            }
1029
0
            case Fragment::OR_D: {
  Branch (1029:13): [True: 0, False: 0]
  Branch (1029:13): [True: 0, False: 0]
  Branch (1029:13): [True: 0, False: 0]
  Branch (1029:13): [True: 0, False: 0]
1030
0
                const auto count{3 + subs[0].ops.count + subs[1].ops.count};
1031
0
                const auto sat{subs[0].ops.sat | (subs[1].ops.sat + subs[0].ops.dsat)};
1032
0
                const auto dsat{subs[0].ops.dsat + subs[1].ops.dsat};
1033
0
                return {count, sat, dsat};
1034
0
            }
1035
0
            case Fragment::OR_C: {
  Branch (1035:13): [True: 0, False: 0]
  Branch (1035:13): [True: 0, False: 0]
  Branch (1035:13): [True: 0, False: 0]
  Branch (1035:13): [True: 0, False: 0]
1036
0
                const auto count{2 + subs[0].ops.count + subs[1].ops.count};
1037
0
                const auto sat{subs[0].ops.sat | (subs[1].ops.sat + subs[0].ops.dsat)};
1038
0
                return {count, sat, {}};
1039
0
            }
1040
0
            case Fragment::OR_I: {
  Branch (1040:13): [True: 0, False: 0]
  Branch (1040:13): [True: 0, False: 0]
  Branch (1040:13): [True: 0, False: 0]
  Branch (1040:13): [True: 0, False: 0]
1041
0
                const auto count{3 + subs[0].ops.count + subs[1].ops.count};
1042
0
                const auto sat{subs[0].ops.sat | subs[1].ops.sat};
1043
0
                const auto dsat{subs[0].ops.dsat | subs[1].ops.dsat};
1044
0
                return {count, sat, dsat};
1045
0
            }
1046
0
            case Fragment::ANDOR: {
  Branch (1046:13): [True: 0, False: 0]
  Branch (1046:13): [True: 0, False: 0]
  Branch (1046:13): [True: 0, False: 0]
  Branch (1046:13): [True: 0, False: 0]
1047
0
                const auto count{3 + subs[0].ops.count + subs[1].ops.count + subs[2].ops.count};
1048
0
                const auto sat{(subs[1].ops.sat + subs[0].ops.sat) | (subs[0].ops.dsat + subs[2].ops.sat)};
1049
0
                const auto dsat{subs[0].ops.dsat + subs[2].ops.dsat};
1050
0
                return {count, sat, dsat};
1051
0
            }
1052
0
            case Fragment::MULTI: return {1, (uint32_t)keys.size(), (uint32_t)keys.size()};
  Branch (1052:13): [True: 0, False: 0]
  Branch (1052:13): [True: 0, False: 0]
  Branch (1052:13): [True: 0, False: 0]
  Branch (1052:13): [True: 0, False: 0]
1053
0
            case Fragment::MULTI_A: return {(uint32_t)keys.size() + 1, 0, 0};
  Branch (1053:13): [True: 0, False: 0]
  Branch (1053:13): [True: 0, False: 0]
  Branch (1053:13): [True: 0, False: 0]
  Branch (1053:13): [True: 0, False: 0]
1054
0
            case Fragment::WRAP_S:
  Branch (1054:13): [True: 0, False: 0]
  Branch (1054:13): [True: 0, False: 0]
  Branch (1054:13): [True: 0, False: 0]
  Branch (1054:13): [True: 0, False: 0]
1055
0
            case Fragment::WRAP_C:
  Branch (1055:13): [True: 0, False: 0]
  Branch (1055:13): [True: 0, False: 0]
  Branch (1055:13): [True: 0, False: 0]
  Branch (1055:13): [True: 0, False: 0]
1056
0
            case Fragment::WRAP_N: return {1 + subs[0].ops.count, subs[0].ops.sat, subs[0].ops.dsat};
  Branch (1056:13): [True: 0, False: 0]
  Branch (1056:13): [True: 0, False: 0]
  Branch (1056:13): [True: 0, False: 0]
  Branch (1056:13): [True: 0, False: 0]
1057
0
            case Fragment::WRAP_A: return {2 + subs[0].ops.count, subs[0].ops.sat, subs[0].ops.dsat};
  Branch (1057:13): [True: 0, False: 0]
  Branch (1057:13): [True: 0, False: 0]
  Branch (1057:13): [True: 0, False: 0]
  Branch (1057:13): [True: 0, False: 0]
1058
0
            case Fragment::WRAP_D: return {3 + subs[0].ops.count, subs[0].ops.sat, 0};
  Branch (1058:13): [True: 0, False: 0]
  Branch (1058:13): [True: 0, False: 0]
  Branch (1058:13): [True: 0, False: 0]
  Branch (1058:13): [True: 0, False: 0]
1059
0
            case Fragment::WRAP_J: return {4 + subs[0].ops.count, subs[0].ops.sat, 0};
  Branch (1059:13): [True: 0, False: 0]
  Branch (1059:13): [True: 0, False: 0]
  Branch (1059:13): [True: 0, False: 0]
  Branch (1059:13): [True: 0, False: 0]
1060
0
            case Fragment::WRAP_V: return {subs[0].ops.count + (subs[0].GetType() << "x"_mst), subs[0].ops.sat, {}};
  Branch (1060:13): [True: 0, False: 0]
  Branch (1060:13): [True: 0, False: 0]
  Branch (1060:13): [True: 0, False: 0]
  Branch (1060:13): [True: 0, False: 0]
1061
0
            case Fragment::THRESH: {
  Branch (1061:13): [True: 0, False: 0]
  Branch (1061:13): [True: 0, False: 0]
  Branch (1061:13): [True: 0, False: 0]
  Branch (1061:13): [True: 0, False: 0]
1062
0
                uint32_t count = 0;
1063
0
                auto sats = Vector(internal::MaxInt<uint32_t>(0));
1064
0
                for (const auto& sub : subs) {
  Branch (1064:38): [True: 0, False: 0]
  Branch (1064:38): [True: 0, False: 0]
  Branch (1064:38): [True: 0, False: 0]
  Branch (1064:38): [True: 0, False: 0]
1065
0
                    count += sub.ops.count + 1;
1066
0
                    auto next_sats = Vector(sats[0] + sub.ops.dsat);
1067
0
                    for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + sub.ops.dsat) | (sats[j - 1] + sub.ops.sat));
  Branch (1067:40): [True: 0, False: 0]
  Branch (1067:40): [True: 0, False: 0]
  Branch (1067:40): [True: 0, False: 0]
  Branch (1067:40): [True: 0, False: 0]
1068
0
                    next_sats.push_back(sats[sats.size() - 1] + sub.ops.sat);
1069
0
                    sats = std::move(next_sats);
1070
0
                }
1071
0
                assert(k < sats.size());
  Branch (1071:17): [True: 0, False: 0]
  Branch (1071:17): [True: 0, False: 0]
  Branch (1071:17): [True: 0, False: 0]
  Branch (1071:17): [True: 0, False: 0]
1072
0
                return {count, sats[k], sats[0]};
1073
0
            }
1074
0
        }
1075
0
        assert(false);
  Branch (1075:9): [Folded, False: 0]
  Branch (1075:9): [Folded, False: 0]
  Branch (1075:9): [Folded, False: 0]
  Branch (1075:9): [Folded, False: 0]
1076
0
    }
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE7CalcOpsEv
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEE7CalcOpsEv
Unexecuted instantiation: _ZNK10miniscript4NodeIjE7CalcOpsEv
Unexecuted instantiation: _ZNK10miniscript4NodeI11XOnlyPubKeyE7CalcOpsEv
1077
1078
0
    internal::StackSize CalcStackSize() const {
1079
0
        using namespace internal;
1080
0
        switch (fragment) {
  Branch (1080:17): [True: 0, False: 0]
  Branch (1080:17): [True: 0, False: 0]
  Branch (1080:17): [True: 0, False: 0]
  Branch (1080:17): [True: 0, False: 0]
1081
0
            case Fragment::JUST_0: return {{}, SatInfo::Push()};
  Branch (1081:13): [True: 0, False: 0]
  Branch (1081:13): [True: 0, False: 0]
  Branch (1081:13): [True: 0, False: 0]
  Branch (1081:13): [True: 0, False: 0]
1082
0
            case Fragment::JUST_1: return {SatInfo::Push(), {}};
  Branch (1082:13): [True: 0, False: 0]
  Branch (1082:13): [True: 0, False: 0]
  Branch (1082:13): [True: 0, False: 0]
  Branch (1082:13): [True: 0, False: 0]
1083
0
            case Fragment::OLDER:
  Branch (1083:13): [True: 0, False: 0]
  Branch (1083:13): [True: 0, False: 0]
  Branch (1083:13): [True: 0, False: 0]
  Branch (1083:13): [True: 0, False: 0]
1084
0
            case Fragment::AFTER: return {SatInfo::Push() + SatInfo::Nop(), {}};
  Branch (1084:13): [True: 0, False: 0]
  Branch (1084:13): [True: 0, False: 0]
  Branch (1084:13): [True: 0, False: 0]
  Branch (1084:13): [True: 0, False: 0]
1085
0
            case Fragment::PK_K: return {SatInfo::Push()};
  Branch (1085:13): [True: 0, False: 0]
  Branch (1085:13): [True: 0, False: 0]
  Branch (1085:13): [True: 0, False: 0]
  Branch (1085:13): [True: 0, False: 0]
1086
0
            case Fragment::PK_H: return {SatInfo::OP_DUP() + SatInfo::Hash() + SatInfo::Push() + SatInfo::OP_EQUALVERIFY()};
  Branch (1086:13): [True: 0, False: 0]
  Branch (1086:13): [True: 0, False: 0]
  Branch (1086:13): [True: 0, False: 0]
  Branch (1086:13): [True: 0, False: 0]
1087
0
            case Fragment::SHA256:
  Branch (1087:13): [True: 0, False: 0]
  Branch (1087:13): [True: 0, False: 0]
  Branch (1087:13): [True: 0, False: 0]
  Branch (1087:13): [True: 0, False: 0]
1088
0
            case Fragment::RIPEMD160:
  Branch (1088:13): [True: 0, False: 0]
  Branch (1088:13): [True: 0, False: 0]
  Branch (1088:13): [True: 0, False: 0]
  Branch (1088:13): [True: 0, False: 0]
1089
0
            case Fragment::HASH256:
  Branch (1089:13): [True: 0, False: 0]
  Branch (1089:13): [True: 0, False: 0]
  Branch (1089:13): [True: 0, False: 0]
  Branch (1089:13): [True: 0, False: 0]
1090
0
            case Fragment::HASH160: return {
  Branch (1090:13): [True: 0, False: 0]
  Branch (1090:13): [True: 0, False: 0]
  Branch (1090:13): [True: 0, False: 0]
  Branch (1090:13): [True: 0, False: 0]
1091
0
                SatInfo::OP_SIZE() + SatInfo::Push() + SatInfo::OP_EQUALVERIFY() + SatInfo::Hash() + SatInfo::Push() + SatInfo::OP_EQUAL(),
1092
0
                {}
1093
0
            };
1094
0
            case Fragment::ANDOR: {
  Branch (1094:13): [True: 0, False: 0]
  Branch (1094:13): [True: 0, False: 0]
  Branch (1094:13): [True: 0, False: 0]
  Branch (1094:13): [True: 0, False: 0]
1095
0
                const auto& x{subs[0].ss};
1096
0
                const auto& y{subs[1].ss};
1097
0
                const auto& z{subs[2].ss};
1098
0
                return {
1099
0
                    (x.Sat() + SatInfo::If() + y.Sat()) | (x.Dsat() + SatInfo::If() + z.Sat()),
1100
0
                    x.Dsat() + SatInfo::If() + z.Dsat()
1101
0
                };
1102
0
            }
1103
0
            case Fragment::AND_V: {
  Branch (1103:13): [True: 0, False: 0]
  Branch (1103:13): [True: 0, False: 0]
  Branch (1103:13): [True: 0, False: 0]
  Branch (1103:13): [True: 0, False: 0]
1104
0
                const auto& x{subs[0].ss};
1105
0
                const auto& y{subs[1].ss};
1106
0
                return {x.Sat() + y.Sat(), {}};
1107
0
            }
1108
0
            case Fragment::AND_B: {
  Branch (1108:13): [True: 0, False: 0]
  Branch (1108:13): [True: 0, False: 0]
  Branch (1108:13): [True: 0, False: 0]
  Branch (1108:13): [True: 0, False: 0]
1109
0
                const auto& x{subs[0].ss};
1110
0
                const auto& y{subs[1].ss};
1111
0
                return {x.Sat() + y.Sat() + SatInfo::BinaryOp(), x.Dsat() + y.Dsat() + SatInfo::BinaryOp()};
1112
0
            }
1113
0
            case Fragment::OR_B: {
  Branch (1113:13): [True: 0, False: 0]
  Branch (1113:13): [True: 0, False: 0]
  Branch (1113:13): [True: 0, False: 0]
  Branch (1113:13): [True: 0, False: 0]
1114
0
                const auto& x{subs[0].ss};
1115
0
                const auto& y{subs[1].ss};
1116
0
                return {
1117
0
                    ((x.Sat() + y.Dsat()) | (x.Dsat() + y.Sat())) + SatInfo::BinaryOp(),
1118
0
                    x.Dsat() + y.Dsat() + SatInfo::BinaryOp()
1119
0
                };
1120
0
            }
1121
0
            case Fragment::OR_C: {
  Branch (1121:13): [True: 0, False: 0]
  Branch (1121:13): [True: 0, False: 0]
  Branch (1121:13): [True: 0, False: 0]
  Branch (1121:13): [True: 0, False: 0]
1122
0
                const auto& x{subs[0].ss};
1123
0
                const auto& y{subs[1].ss};
1124
0
                return {(x.Sat() + SatInfo::If()) | (x.Dsat() + SatInfo::If() + y.Sat()), {}};
1125
0
            }
1126
0
            case Fragment::OR_D: {
  Branch (1126:13): [True: 0, False: 0]
  Branch (1126:13): [True: 0, False: 0]
  Branch (1126:13): [True: 0, False: 0]
  Branch (1126:13): [True: 0, False: 0]
1127
0
                const auto& x{subs[0].ss};
1128
0
                const auto& y{subs[1].ss};
1129
0
                return {
1130
0
                    (x.Sat() + SatInfo::OP_IFDUP(true) + SatInfo::If()) | (x.Dsat() + SatInfo::OP_IFDUP(false) + SatInfo::If() + y.Sat()),
1131
0
                    x.Dsat() + SatInfo::OP_IFDUP(false) + SatInfo::If() + y.Dsat()
1132
0
                };
1133
0
            }
1134
0
            case Fragment::OR_I: {
  Branch (1134:13): [True: 0, False: 0]
  Branch (1134:13): [True: 0, False: 0]
  Branch (1134:13): [True: 0, False: 0]
  Branch (1134:13): [True: 0, False: 0]
1135
0
                const auto& x{subs[0].ss};
1136
0
                const auto& y{subs[1].ss};
1137
0
                return {SatInfo::If() + (x.Sat() | y.Sat()), SatInfo::If() + (x.Dsat() | y.Dsat())};
1138
0
            }
1139
            // multi(k, key1, key2, ..., key_n) starts off with k+1 stack elements (a 0, plus k
1140
            // signatures), then reaches n+k+3 stack elements after pushing the n keys, plus k and
1141
            // n itself, and ends with 1 stack element (success or failure). Thus, it net removes
1142
            // k elements (from k+1 to 1), while reaching k+n+2 more than it ends with.
1143
0
            case Fragment::MULTI: return {SatInfo(k, k + keys.size() + 2)};
  Branch (1143:13): [True: 0, False: 0]
  Branch (1143:13): [True: 0, False: 0]
  Branch (1143:13): [True: 0, False: 0]
  Branch (1143:13): [True: 0, False: 0]
1144
            // multi_a(k, key1, key2, ..., key_n) starts off with n stack elements (the
1145
            // signatures), reaches 1 more (after the first key push), and ends with 1. Thus it net
1146
            // removes n-1 elements (from n to 1) while reaching n more than it ends with.
1147
0
            case Fragment::MULTI_A: return {SatInfo(keys.size() - 1, keys.size())};
  Branch (1147:13): [True: 0, False: 0]
  Branch (1147:13): [True: 0, False: 0]
  Branch (1147:13): [True: 0, False: 0]
  Branch (1147:13): [True: 0, False: 0]
1148
0
            case Fragment::WRAP_A:
  Branch (1148:13): [True: 0, False: 0]
  Branch (1148:13): [True: 0, False: 0]
  Branch (1148:13): [True: 0, False: 0]
  Branch (1148:13): [True: 0, False: 0]
1149
0
            case Fragment::WRAP_N:
  Branch (1149:13): [True: 0, False: 0]
  Branch (1149:13): [True: 0, False: 0]
  Branch (1149:13): [True: 0, False: 0]
  Branch (1149:13): [True: 0, False: 0]
1150
0
            case Fragment::WRAP_S: return subs[0].ss;
  Branch (1150:13): [True: 0, False: 0]
  Branch (1150:13): [True: 0, False: 0]
  Branch (1150:13): [True: 0, False: 0]
  Branch (1150:13): [True: 0, False: 0]
1151
0
            case Fragment::WRAP_C: return {
  Branch (1151:13): [True: 0, False: 0]
  Branch (1151:13): [True: 0, False: 0]
  Branch (1151:13): [True: 0, False: 0]
  Branch (1151:13): [True: 0, False: 0]
1152
0
                subs[0].ss.Sat() + SatInfo::OP_CHECKSIG(),
1153
0
                subs[0].ss.Dsat() + SatInfo::OP_CHECKSIG()
1154
0
            };
1155
0
            case Fragment::WRAP_D: return {
  Branch (1155:13): [True: 0, False: 0]
  Branch (1155:13): [True: 0, False: 0]
  Branch (1155:13): [True: 0, False: 0]
  Branch (1155:13): [True: 0, False: 0]
1156
0
                SatInfo::OP_DUP() + SatInfo::If() + subs[0].ss.Sat(),
1157
0
                SatInfo::OP_DUP() + SatInfo::If()
1158
0
            };
1159
0
            case Fragment::WRAP_V: return {subs[0].ss.Sat() + SatInfo::OP_VERIFY(), {}};
  Branch (1159:13): [True: 0, False: 0]
  Branch (1159:13): [True: 0, False: 0]
  Branch (1159:13): [True: 0, False: 0]
  Branch (1159:13): [True: 0, False: 0]
1160
0
            case Fragment::WRAP_J: return {
  Branch (1160:13): [True: 0, False: 0]
  Branch (1160:13): [True: 0, False: 0]
  Branch (1160:13): [True: 0, False: 0]
  Branch (1160:13): [True: 0, False: 0]
1161
0
                SatInfo::OP_SIZE() + SatInfo::OP_0NOTEQUAL() + SatInfo::If() + subs[0].ss.Sat(),
1162
0
                SatInfo::OP_SIZE() + SatInfo::OP_0NOTEQUAL() + SatInfo::If()
1163
0
            };
1164
0
            case Fragment::THRESH: {
  Branch (1164:13): [True: 0, False: 0]
  Branch (1164:13): [True: 0, False: 0]
  Branch (1164:13): [True: 0, False: 0]
  Branch (1164:13): [True: 0, False: 0]
1165
                // sats[j] is the SatInfo corresponding to all traces reaching j satisfactions.
1166
0
                auto sats = Vector(SatInfo::Empty());
1167
0
                for (size_t i = 0; i < subs.size(); ++i) {
  Branch (1167:36): [True: 0, False: 0]
  Branch (1167:36): [True: 0, False: 0]
  Branch (1167:36): [True: 0, False: 0]
  Branch (1167:36): [True: 0, False: 0]
1168
                    // Loop over the subexpressions, processing them one by one. After adding
1169
                    // element i we need to add OP_ADD (if i>0).
1170
0
                    auto add = i ? SatInfo::BinaryOp() : SatInfo::Empty();
  Branch (1170:32): [True: 0, False: 0]
  Branch (1170:32): [True: 0, False: 0]
  Branch (1170:32): [True: 0, False: 0]
  Branch (1170:32): [True: 0, False: 0]
1171
                    // Construct a variable that will become the next sats, starting with index 0.
1172
0
                    auto next_sats = Vector(sats[0] + subs[i].ss.Dsat() + add);
1173
                    // Then loop to construct next_sats[1..i].
1174
0
                    for (size_t j = 1; j < sats.size(); ++j) {
  Branch (1174:40): [True: 0, False: 0]
  Branch (1174:40): [True: 0, False: 0]
  Branch (1174:40): [True: 0, False: 0]
  Branch (1174:40): [True: 0, False: 0]
1175
0
                        next_sats.push_back(((sats[j] + subs[i].ss.Dsat()) | (sats[j - 1] + subs[i].ss.Sat())) + add);
1176
0
                    }
1177
                    // Finally construct next_sats[i+1].
1178
0
                    next_sats.push_back(sats[sats.size() - 1] + subs[i].ss.Sat() + add);
1179
                    // Switch over.
1180
0
                    sats = std::move(next_sats);
1181
0
                }
1182
                // To satisfy thresh we need k satisfactions; to dissatisfy we need 0. In both
1183
                // cases a push of k and an OP_EQUAL follow.
1184
0
                return {
1185
0
                    sats[k] + SatInfo::Push() + SatInfo::OP_EQUAL(),
1186
0
                    sats[0] + SatInfo::Push() + SatInfo::OP_EQUAL()
1187
0
                };
1188
0
            }
1189
0
        }
1190
0
        assert(false);
  Branch (1190:9): [Folded, False: 0]
  Branch (1190:9): [Folded, False: 0]
  Branch (1190:9): [Folded, False: 0]
  Branch (1190:9): [Folded, False: 0]
1191
0
    }
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE13CalcStackSizeEv
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEE13CalcStackSizeEv
Unexecuted instantiation: _ZNK10miniscript4NodeIjE13CalcStackSizeEv
Unexecuted instantiation: _ZNK10miniscript4NodeI11XOnlyPubKeyE13CalcStackSizeEv
1192
1193
0
    internal::WitnessSize CalcWitnessSize() const {
1194
0
        const uint32_t sig_size = IsTapscript(m_script_ctx) ? 1 + 65 : 1 + 72;
  Branch (1194:35): [True: 0, False: 0]
  Branch (1194:35): [True: 0, False: 0]
  Branch (1194:35): [True: 0, False: 0]
  Branch (1194:35): [True: 0, False: 0]
1195
0
        const uint32_t pubkey_size = IsTapscript(m_script_ctx) ? 1 + 32 : 1 + 33;
  Branch (1195:38): [True: 0, False: 0]
  Branch (1195:38): [True: 0, False: 0]
  Branch (1195:38): [True: 0, False: 0]
  Branch (1195:38): [True: 0, False: 0]
1196
0
        switch (fragment) {
  Branch (1196:17): [True: 0, False: 0]
  Branch (1196:17): [True: 0, False: 0]
  Branch (1196:17): [True: 0, False: 0]
  Branch (1196:17): [True: 0, False: 0]
1197
0
            case Fragment::JUST_0: return {{}, 0};
  Branch (1197:13): [True: 0, False: 0]
  Branch (1197:13): [True: 0, False: 0]
  Branch (1197:13): [True: 0, False: 0]
  Branch (1197:13): [True: 0, False: 0]
1198
0
            case Fragment::JUST_1:
  Branch (1198:13): [True: 0, False: 0]
  Branch (1198:13): [True: 0, False: 0]
  Branch (1198:13): [True: 0, False: 0]
  Branch (1198:13): [True: 0, False: 0]
1199
0
            case Fragment::OLDER:
  Branch (1199:13): [True: 0, False: 0]
  Branch (1199:13): [True: 0, False: 0]
  Branch (1199:13): [True: 0, False: 0]
  Branch (1199:13): [True: 0, False: 0]
1200
0
            case Fragment::AFTER: return {0, {}};
  Branch (1200:13): [True: 0, False: 0]
  Branch (1200:13): [True: 0, False: 0]
  Branch (1200:13): [True: 0, False: 0]
  Branch (1200:13): [True: 0, False: 0]
1201
0
            case Fragment::PK_K: return {sig_size, 1};
  Branch (1201:13): [True: 0, False: 0]
  Branch (1201:13): [True: 0, False: 0]
  Branch (1201:13): [True: 0, False: 0]
  Branch (1201:13): [True: 0, False: 0]
1202
0
            case Fragment::PK_H: return {sig_size + pubkey_size, 1 + pubkey_size};
  Branch (1202:13): [True: 0, False: 0]
  Branch (1202:13): [True: 0, False: 0]
  Branch (1202:13): [True: 0, False: 0]
  Branch (1202:13): [True: 0, False: 0]
1203
0
            case Fragment::SHA256:
  Branch (1203:13): [True: 0, False: 0]
  Branch (1203:13): [True: 0, False: 0]
  Branch (1203:13): [True: 0, False: 0]
  Branch (1203:13): [True: 0, False: 0]
1204
0
            case Fragment::RIPEMD160:
  Branch (1204:13): [True: 0, False: 0]
  Branch (1204:13): [True: 0, False: 0]
  Branch (1204:13): [True: 0, False: 0]
  Branch (1204:13): [True: 0, False: 0]
1205
0
            case Fragment::HASH256:
  Branch (1205:13): [True: 0, False: 0]
  Branch (1205:13): [True: 0, False: 0]
  Branch (1205:13): [True: 0, False: 0]
  Branch (1205:13): [True: 0, False: 0]
1206
0
            case Fragment::HASH160: return {1 + 32, {}};
  Branch (1206:13): [True: 0, False: 0]
  Branch (1206:13): [True: 0, False: 0]
  Branch (1206:13): [True: 0, False: 0]
  Branch (1206:13): [True: 0, False: 0]
1207
0
            case Fragment::ANDOR: {
  Branch (1207:13): [True: 0, False: 0]
  Branch (1207:13): [True: 0, False: 0]
  Branch (1207:13): [True: 0, False: 0]
  Branch (1207:13): [True: 0, False: 0]
1208
0
                const auto sat{(subs[0].ws.sat + subs[1].ws.sat) | (subs[0].ws.dsat + subs[2].ws.sat)};
1209
0
                const auto dsat{subs[0].ws.dsat + subs[2].ws.dsat};
1210
0
                return {sat, dsat};
1211
0
            }
1212
0
            case Fragment::AND_V: return {subs[0].ws.sat + subs[1].ws.sat, {}};
  Branch (1212:13): [True: 0, False: 0]
  Branch (1212:13): [True: 0, False: 0]
  Branch (1212:13): [True: 0, False: 0]
  Branch (1212:13): [True: 0, False: 0]
1213
0
            case Fragment::AND_B: return {subs[0].ws.sat + subs[1].ws.sat, subs[0].ws.dsat + subs[1].ws.dsat};
  Branch (1213:13): [True: 0, False: 0]
  Branch (1213:13): [True: 0, False: 0]
  Branch (1213:13): [True: 0, False: 0]
  Branch (1213:13): [True: 0, False: 0]
1214
0
            case Fragment::OR_B: {
  Branch (1214:13): [True: 0, False: 0]
  Branch (1214:13): [True: 0, False: 0]
  Branch (1214:13): [True: 0, False: 0]
  Branch (1214:13): [True: 0, False: 0]
1215
0
                const auto sat{(subs[0].ws.dsat + subs[1].ws.sat) | (subs[0].ws.sat + subs[1].ws.dsat)};
1216
0
                const auto dsat{subs[0].ws.dsat + subs[1].ws.dsat};
1217
0
                return {sat, dsat};
1218
0
            }
1219
0
            case Fragment::OR_C: return {subs[0].ws.sat | (subs[0].ws.dsat + subs[1].ws.sat), {}};
  Branch (1219:13): [True: 0, False: 0]
  Branch (1219:13): [True: 0, False: 0]
  Branch (1219:13): [True: 0, False: 0]
  Branch (1219:13): [True: 0, False: 0]
1220
0
            case Fragment::OR_D: return {subs[0].ws.sat | (subs[0].ws.dsat + subs[1].ws.sat), subs[0].ws.dsat + subs[1].ws.dsat};
  Branch (1220:13): [True: 0, False: 0]
  Branch (1220:13): [True: 0, False: 0]
  Branch (1220:13): [True: 0, False: 0]
  Branch (1220:13): [True: 0, False: 0]
1221
0
            case Fragment::OR_I: return {(subs[0].ws.sat + 1 + 1) | (subs[1].ws.sat + 1), (subs[0].ws.dsat + 1 + 1) | (subs[1].ws.dsat + 1)};
  Branch (1221:13): [True: 0, False: 0]
  Branch (1221:13): [True: 0, False: 0]
  Branch (1221:13): [True: 0, False: 0]
  Branch (1221:13): [True: 0, False: 0]
1222
0
            case Fragment::MULTI: return {k * sig_size + 1, k + 1};
  Branch (1222:13): [True: 0, False: 0]
  Branch (1222:13): [True: 0, False: 0]
  Branch (1222:13): [True: 0, False: 0]
  Branch (1222:13): [True: 0, False: 0]
1223
0
            case Fragment::MULTI_A: return {k * sig_size + static_cast<uint32_t>(keys.size()) - k, static_cast<uint32_t>(keys.size())};
  Branch (1223:13): [True: 0, False: 0]
  Branch (1223:13): [True: 0, False: 0]
  Branch (1223:13): [True: 0, False: 0]
  Branch (1223:13): [True: 0, False: 0]
1224
0
            case Fragment::WRAP_A:
  Branch (1224:13): [True: 0, False: 0]
  Branch (1224:13): [True: 0, False: 0]
  Branch (1224:13): [True: 0, False: 0]
  Branch (1224:13): [True: 0, False: 0]
1225
0
            case Fragment::WRAP_N:
  Branch (1225:13): [True: 0, False: 0]
  Branch (1225:13): [True: 0, False: 0]
  Branch (1225:13): [True: 0, False: 0]
  Branch (1225:13): [True: 0, False: 0]
1226
0
            case Fragment::WRAP_S:
  Branch (1226:13): [True: 0, False: 0]
  Branch (1226:13): [True: 0, False: 0]
  Branch (1226:13): [True: 0, False: 0]
  Branch (1226:13): [True: 0, False: 0]
1227
0
            case Fragment::WRAP_C: return subs[0].ws;
  Branch (1227:13): [True: 0, False: 0]
  Branch (1227:13): [True: 0, False: 0]
  Branch (1227:13): [True: 0, False: 0]
  Branch (1227:13): [True: 0, False: 0]
1228
0
            case Fragment::WRAP_D: return {1 + 1 + subs[0].ws.sat, 1};
  Branch (1228:13): [True: 0, False: 0]
  Branch (1228:13): [True: 0, False: 0]
  Branch (1228:13): [True: 0, False: 0]
  Branch (1228:13): [True: 0, False: 0]
1229
0
            case Fragment::WRAP_V: return {subs[0].ws.sat, {}};
  Branch (1229:13): [True: 0, False: 0]
  Branch (1229:13): [True: 0, False: 0]
  Branch (1229:13): [True: 0, False: 0]
  Branch (1229:13): [True: 0, False: 0]
1230
0
            case Fragment::WRAP_J: return {subs[0].ws.sat, 1};
  Branch (1230:13): [True: 0, False: 0]
  Branch (1230:13): [True: 0, False: 0]
  Branch (1230:13): [True: 0, False: 0]
  Branch (1230:13): [True: 0, False: 0]
1231
0
            case Fragment::THRESH: {
  Branch (1231:13): [True: 0, False: 0]
  Branch (1231:13): [True: 0, False: 0]
  Branch (1231:13): [True: 0, False: 0]
  Branch (1231:13): [True: 0, False: 0]
1232
0
                auto sats = Vector(internal::MaxInt<uint32_t>(0));
1233
0
                for (const auto& sub : subs) {
  Branch (1233:38): [True: 0, False: 0]
  Branch (1233:38): [True: 0, False: 0]
  Branch (1233:38): [True: 0, False: 0]
  Branch (1233:38): [True: 0, False: 0]
1234
0
                    auto next_sats = Vector(sats[0] + sub.ws.dsat);
1235
0
                    for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + sub.ws.dsat) | (sats[j - 1] + sub.ws.sat));
  Branch (1235:40): [True: 0, False: 0]
  Branch (1235:40): [True: 0, False: 0]
  Branch (1235:40): [True: 0, False: 0]
  Branch (1235:40): [True: 0, False: 0]
1236
0
                    next_sats.push_back(sats[sats.size() - 1] + sub.ws.sat);
1237
0
                    sats = std::move(next_sats);
1238
0
                }
1239
0
                assert(k < sats.size());
  Branch (1239:17): [True: 0, False: 0]
  Branch (1239:17): [True: 0, False: 0]
  Branch (1239:17): [True: 0, False: 0]
  Branch (1239:17): [True: 0, False: 0]
1240
0
                return {sats[k], sats[0]};
1241
0
            }
1242
0
        }
1243
0
        assert(false);
  Branch (1243:9): [Folded, False: 0]
  Branch (1243:9): [Folded, False: 0]
  Branch (1243:9): [Folded, False: 0]
  Branch (1243:9): [Folded, False: 0]
1244
0
    }
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE15CalcWitnessSizeEv
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEE15CalcWitnessSizeEv
Unexecuted instantiation: _ZNK10miniscript4NodeIjE15CalcWitnessSizeEv
Unexecuted instantiation: _ZNK10miniscript4NodeI11XOnlyPubKeyE15CalcWitnessSizeEv
1245
1246
    template<typename Ctx>
1247
0
    internal::InputResult ProduceInput(const Ctx& ctx) const {
1248
0
        using namespace internal;
1249
1250
        // Internal function which is invoked for every tree node, constructing satisfaction/dissatisfactions
1251
        // given those of its subnodes.
1252
0
        auto helper = [&ctx](const Node& node, std::span<InputResult> subres) -> InputResult {
1253
0
            switch (node.fragment) {
  Branch (1253:21): [True: 0, False: 0]
  Branch (1253:21): [True: 0, False: 0]
  Branch (1253:21): [True: 0, False: 0]
1254
0
                case Fragment::PK_K: {
  Branch (1254:17): [True: 0, False: 0]
  Branch (1254:17): [True: 0, False: 0]
  Branch (1254:17): [True: 0, False: 0]
1255
0
                    std::vector<unsigned char> sig;
1256
0
                    Availability avail = ctx.Sign(node.keys[0], sig);
1257
0
                    return {ZERO, InputStack(std::move(sig)).SetWithSig().SetAvailable(avail)};
1258
0
                }
1259
0
                case Fragment::PK_H: {
  Branch (1259:17): [True: 0, False: 0]
  Branch (1259:17): [True: 0, False: 0]
  Branch (1259:17): [True: 0, False: 0]
1260
0
                    std::vector<unsigned char> key = ctx.ToPKBytes(node.keys[0]), sig;
1261
0
                    Availability avail = ctx.Sign(node.keys[0], sig);
1262
0
                    return {ZERO + InputStack(key), (InputStack(std::move(sig)).SetWithSig() + InputStack(key)).SetAvailable(avail)};
1263
0
                }
1264
0
                case Fragment::MULTI_A: {
  Branch (1264:17): [True: 0, False: 0]
  Branch (1264:17): [True: 0, False: 0]
  Branch (1264:17): [True: 0, False: 0]
1265
                    // sats[j] represents the best stack containing j valid signatures (out of the first i keys).
1266
                    // In the loop below, these stacks are built up using a dynamic programming approach.
1267
0
                    std::vector<InputStack> sats = Vector(EMPTY);
1268
0
                    for (size_t i = 0; i < node.keys.size(); ++i) {
  Branch (1268:40): [True: 0, False: 0]
  Branch (1268:40): [True: 0, False: 0]
  Branch (1268:40): [True: 0, False: 0]
1269
                        // Get the signature for the i'th key in reverse order (the signature for the first key needs to
1270
                        // be at the top of the stack, contrary to CHECKMULTISIG's satisfaction).
1271
0
                        std::vector<unsigned char> sig;
1272
0
                        Availability avail = ctx.Sign(node.keys[node.keys.size() - 1 - i], sig);
1273
                        // Compute signature stack for just this key.
1274
0
                        auto sat = InputStack(std::move(sig)).SetWithSig().SetAvailable(avail);
1275
                        // Compute the next sats vector: next_sats[0] is a copy of sats[0] (no signatures). All further
1276
                        // next_sats[j] are equal to either the existing sats[j] + ZERO, or sats[j-1] plus a signature
1277
                        // for the current (i'th) key. The very last element needs all signatures filled.
1278
0
                        std::vector<InputStack> next_sats;
1279
0
                        next_sats.push_back(sats[0] + ZERO);
1280
0
                        for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + ZERO) | (std::move(sats[j - 1]) + sat));
  Branch (1280:44): [True: 0, False: 0]
  Branch (1280:44): [True: 0, False: 0]
  Branch (1280:44): [True: 0, False: 0]
1281
0
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(sat));
1282
                        // Switch over.
1283
0
                        sats = std::move(next_sats);
1284
0
                    }
1285
                    // The dissatisfaction consists of as many empty vectors as there are keys, which is the same as
1286
                    // satisfying 0 keys.
1287
0
                    auto& nsat{sats[0]};
1288
0
                    CHECK_NONFATAL(node.k != 0);
1289
0
                    assert(node.k < sats.size());
  Branch (1289:21): [True: 0, False: 0]
  Branch (1289:21): [True: 0, False: 0]
  Branch (1289:21): [True: 0, False: 0]
1290
0
                    return {std::move(nsat), std::move(sats[node.k])};
1291
0
                }
1292
0
                case Fragment::MULTI: {
  Branch (1292:17): [True: 0, False: 0]
  Branch (1292:17): [True: 0, False: 0]
  Branch (1292:17): [True: 0, False: 0]
1293
                    // sats[j] represents the best stack containing j valid signatures (out of the first i keys).
1294
                    // In the loop below, these stacks are built up using a dynamic programming approach.
1295
                    // sats[0] starts off being {0}, due to the CHECKMULTISIG bug that pops off one element too many.
1296
0
                    std::vector<InputStack> sats = Vector(ZERO);
1297
0
                    for (size_t i = 0; i < node.keys.size(); ++i) {
  Branch (1297:40): [True: 0, False: 0]
  Branch (1297:40): [True: 0, False: 0]
  Branch (1297:40): [True: 0, False: 0]
1298
0
                        std::vector<unsigned char> sig;
1299
0
                        Availability avail = ctx.Sign(node.keys[i], sig);
1300
                        // Compute signature stack for just the i'th key.
1301
0
                        auto sat = InputStack(std::move(sig)).SetWithSig().SetAvailable(avail);
1302
                        // Compute the next sats vector: next_sats[0] is a copy of sats[0] (no signatures). All further
1303
                        // next_sats[j] are equal to either the existing sats[j], or sats[j-1] plus a signature for the
1304
                        // current (i'th) key. The very last element needs all signatures filled.
1305
0
                        std::vector<InputStack> next_sats;
1306
0
                        next_sats.push_back(sats[0]);
1307
0
                        for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back(sats[j] | (std::move(sats[j - 1]) + sat));
  Branch (1307:44): [True: 0, False: 0]
  Branch (1307:44): [True: 0, False: 0]
  Branch (1307:44): [True: 0, False: 0]
1308
0
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(sat));
1309
                        // Switch over.
1310
0
                        sats = std::move(next_sats);
1311
0
                    }
1312
                    // The dissatisfaction consists of k+1 stack elements all equal to 0.
1313
0
                    InputStack nsat = ZERO;
1314
0
                    for (size_t i = 0; i < node.k; ++i) nsat = std::move(nsat) + ZERO;
  Branch (1314:40): [True: 0, False: 0]
  Branch (1314:40): [True: 0, False: 0]
  Branch (1314:40): [True: 0, False: 0]
1315
0
                    assert(node.k < sats.size());
  Branch (1315:21): [True: 0, False: 0]
  Branch (1315:21): [True: 0, False: 0]
  Branch (1315:21): [True: 0, False: 0]
1316
0
                    return {std::move(nsat), std::move(sats[node.k])};
1317
0
                }
1318
0
                case Fragment::THRESH: {
  Branch (1318:17): [True: 0, False: 0]
  Branch (1318:17): [True: 0, False: 0]
  Branch (1318:17): [True: 0, False: 0]
1319
                    // sats[k] represents the best stack that satisfies k out of the *last* i subexpressions.
1320
                    // In the loop below, these stacks are built up using a dynamic programming approach.
1321
                    // sats[0] starts off empty.
1322
0
                    std::vector<InputStack> sats = Vector(EMPTY);
1323
0
                    for (size_t i = 0; i < subres.size(); ++i) {
  Branch (1323:40): [True: 0, False: 0]
  Branch (1323:40): [True: 0, False: 0]
  Branch (1323:40): [True: 0, False: 0]
1324
                        // Introduce an alias for the i'th last satisfaction/dissatisfaction.
1325
0
                        auto& res = subres[subres.size() - i - 1];
1326
                        // Compute the next sats vector: next_sats[0] is sats[0] plus res.nsat (thus containing all dissatisfactions
1327
                        // so far. next_sats[j] is either sats[j] + res.nsat (reusing j earlier satisfactions) or sats[j-1] + res.sat
1328
                        // (reusing j-1 earlier satisfactions plus a new one). The very last next_sats[j] is all satisfactions.
1329
0
                        std::vector<InputStack> next_sats;
1330
0
                        next_sats.push_back(sats[0] + res.nsat);
1331
0
                        for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + res.nsat) | (std::move(sats[j - 1]) + res.sat));
  Branch (1331:44): [True: 0, False: 0]
  Branch (1331:44): [True: 0, False: 0]
  Branch (1331:44): [True: 0, False: 0]
1332
0
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(res.sat));
1333
                        // Switch over.
1334
0
                        sats = std::move(next_sats);
1335
0
                    }
1336
                    // At this point, sats[k].sat is the best satisfaction for the overall thresh() node. The best dissatisfaction
1337
                    // is computed by gathering all sats[i].nsat for i != k.
1338
0
                    InputStack nsat = INVALID;
1339
0
                    for (size_t i = 0; i < sats.size(); ++i) {
  Branch (1339:40): [True: 0, False: 0]
  Branch (1339:40): [True: 0, False: 0]
  Branch (1339:40): [True: 0, False: 0]
1340
                        // i==k is the satisfaction; i==0 is the canonical dissatisfaction;
1341
                        // the rest are non-canonical (a no-signature dissatisfaction - the i=0
1342
                        // form - is always available) and malleable (due to overcompleteness).
1343
                        // Marking the solutions malleable here is not strictly necessary, as they
1344
                        // should already never be picked in non-malleable solutions due to the
1345
                        // availability of the i=0 form.
1346
0
                        if (i != 0 && i != node.k) sats[i].SetMalleable().SetNonCanon();
  Branch (1346:29): [True: 0, False: 0]
  Branch (1346:39): [True: 0, False: 0]
  Branch (1346:29): [True: 0, False: 0]
  Branch (1346:39): [True: 0, False: 0]
  Branch (1346:29): [True: 0, False: 0]
  Branch (1346:39): [True: 0, False: 0]
1347
                        // Include all dissatisfactions (even these non-canonical ones) in nsat.
1348
0
                        if (i != node.k) nsat = std::move(nsat) | std::move(sats[i]);
  Branch (1348:29): [True: 0, False: 0]
  Branch (1348:29): [True: 0, False: 0]
  Branch (1348:29): [True: 0, False: 0]
1349
0
                    }
1350
0
                    assert(node.k < sats.size());
  Branch (1350:21): [True: 0, False: 0]
  Branch (1350:21): [True: 0, False: 0]
  Branch (1350:21): [True: 0, False: 0]
1351
0
                    return {std::move(nsat), std::move(sats[node.k])};
1352
0
                }
1353
0
                case Fragment::OLDER: {
  Branch (1353:17): [True: 0, False: 0]
  Branch (1353:17): [True: 0, False: 0]
  Branch (1353:17): [True: 0, False: 0]
1354
0
                    return {INVALID, ctx.CheckOlder(node.k) ? EMPTY : INVALID};
  Branch (1354:38): [True: 0, False: 0]
  Branch (1354:38): [True: 0, False: 0]
  Branch (1354:38): [True: 0, False: 0]
1355
0
                }
1356
0
                case Fragment::AFTER: {
  Branch (1356:17): [True: 0, False: 0]
  Branch (1356:17): [True: 0, False: 0]
  Branch (1356:17): [True: 0, False: 0]
1357
0
                    return {INVALID, ctx.CheckAfter(node.k) ? EMPTY : INVALID};
  Branch (1357:38): [True: 0, False: 0]
  Branch (1357:38): [True: 0, False: 0]
  Branch (1357:38): [True: 0, False: 0]
1358
0
                }
1359
0
                case Fragment::SHA256: {
  Branch (1359:17): [True: 0, False: 0]
  Branch (1359:17): [True: 0, False: 0]
  Branch (1359:17): [True: 0, False: 0]
1360
0
                    std::vector<unsigned char> preimage;
1361
0
                    Availability avail = ctx.SatSHA256(node.data, preimage);
1362
0
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1363
0
                }
1364
0
                case Fragment::RIPEMD160: {
  Branch (1364:17): [True: 0, False: 0]
  Branch (1364:17): [True: 0, False: 0]
  Branch (1364:17): [True: 0, False: 0]
1365
0
                    std::vector<unsigned char> preimage;
1366
0
                    Availability avail = ctx.SatRIPEMD160(node.data, preimage);
1367
0
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1368
0
                }
1369
0
                case Fragment::HASH256: {
  Branch (1369:17): [True: 0, False: 0]
  Branch (1369:17): [True: 0, False: 0]
  Branch (1369:17): [True: 0, False: 0]
1370
0
                    std::vector<unsigned char> preimage;
1371
0
                    Availability avail = ctx.SatHASH256(node.data, preimage);
1372
0
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1373
0
                }
1374
0
                case Fragment::HASH160: {
  Branch (1374:17): [True: 0, False: 0]
  Branch (1374:17): [True: 0, False: 0]
  Branch (1374:17): [True: 0, False: 0]
1375
0
                    std::vector<unsigned char> preimage;
1376
0
                    Availability avail = ctx.SatHASH160(node.data, preimage);
1377
0
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1378
0
                }
1379
0
                case Fragment::AND_V: {
  Branch (1379:17): [True: 0, False: 0]
  Branch (1379:17): [True: 0, False: 0]
  Branch (1379:17): [True: 0, False: 0]
1380
0
                    auto& x = subres[0], &y = subres[1];
1381
                    // As the dissatisfaction here only consist of a single option, it doesn't
1382
                    // actually need to be listed (it's not required for reasoning about malleability of
1383
                    // other options), and is never required (no valid miniscript relies on the ability
1384
                    // to satisfy the type V left subexpression). It's still listed here for
1385
                    // completeness, as a hypothetical (not currently implemented) satisfier that doesn't
1386
                    // care about malleability might in some cases prefer it still.
1387
0
                    return {(y.nsat + x.sat).SetNonCanon(), y.sat + x.sat};
1388
0
                }
1389
0
                case Fragment::AND_B: {
  Branch (1389:17): [True: 0, False: 0]
  Branch (1389:17): [True: 0, False: 0]
  Branch (1389:17): [True: 0, False: 0]
1390
0
                    auto& x = subres[0], &y = subres[1];
1391
                    // Note that it is not strictly necessary to mark the 2nd and 3rd dissatisfaction here
1392
                    // as malleable. While they are definitely malleable, they are also non-canonical due
1393
                    // to the guaranteed existence of a no-signature other dissatisfaction (the 1st)
1394
                    // option. Because of that, the 2nd and 3rd option will never be chosen, even if they
1395
                    // weren't marked as malleable.
1396
0
                    return {(y.nsat + x.nsat) | (y.sat + x.nsat).SetMalleable().SetNonCanon() | (y.nsat + x.sat).SetMalleable().SetNonCanon(), y.sat + x.sat};
1397
0
                }
1398
0
                case Fragment::OR_B: {
  Branch (1398:17): [True: 0, False: 0]
  Branch (1398:17): [True: 0, False: 0]
  Branch (1398:17): [True: 0, False: 0]
1399
0
                    auto& x = subres[0], &z = subres[1];
1400
                    // The (sat(Z) sat(X)) solution is overcomplete (attacker can change either into dsat).
1401
0
                    return {z.nsat + x.nsat, (z.nsat + x.sat) | (z.sat + x.nsat) | (z.sat + x.sat).SetMalleable().SetNonCanon()};
1402
0
                }
1403
0
                case Fragment::OR_C: {
  Branch (1403:17): [True: 0, False: 0]
  Branch (1403:17): [True: 0, False: 0]
  Branch (1403:17): [True: 0, False: 0]
1404
0
                    auto& x = subres[0], &z = subres[1];
1405
0
                    return {INVALID, std::move(x.sat) | (z.sat + x.nsat)};
1406
0
                }
1407
0
                case Fragment::OR_D: {
  Branch (1407:17): [True: 0, False: 0]
  Branch (1407:17): [True: 0, False: 0]
  Branch (1407:17): [True: 0, False: 0]
1408
0
                    auto& x = subres[0], &z = subres[1];
1409
0
                    return {z.nsat + x.nsat, std::move(x.sat) | (z.sat + x.nsat)};
1410
0
                }
1411
0
                case Fragment::OR_I: {
  Branch (1411:17): [True: 0, False: 0]
  Branch (1411:17): [True: 0, False: 0]
  Branch (1411:17): [True: 0, False: 0]
1412
0
                    auto& x = subres[0], &z = subres[1];
1413
0
                    return {(x.nsat + ONE) | (z.nsat + ZERO), (x.sat + ONE) | (z.sat + ZERO)};
1414
0
                }
1415
0
                case Fragment::ANDOR: {
  Branch (1415:17): [True: 0, False: 0]
  Branch (1415:17): [True: 0, False: 0]
  Branch (1415:17): [True: 0, False: 0]
1416
0
                    auto& x = subres[0], &y = subres[1], &z = subres[2];
1417
0
                    return {(y.nsat + x.sat).SetNonCanon() | (z.nsat + x.nsat), (y.sat + x.sat) | (z.sat + x.nsat)};
1418
0
                }
1419
0
                case Fragment::WRAP_A:
  Branch (1419:17): [True: 0, False: 0]
  Branch (1419:17): [True: 0, False: 0]
  Branch (1419:17): [True: 0, False: 0]
1420
0
                case Fragment::WRAP_S:
  Branch (1420:17): [True: 0, False: 0]
  Branch (1420:17): [True: 0, False: 0]
  Branch (1420:17): [True: 0, False: 0]
1421
0
                case Fragment::WRAP_C:
  Branch (1421:17): [True: 0, False: 0]
  Branch (1421:17): [True: 0, False: 0]
  Branch (1421:17): [True: 0, False: 0]
1422
0
                case Fragment::WRAP_N:
  Branch (1422:17): [True: 0, False: 0]
  Branch (1422:17): [True: 0, False: 0]
  Branch (1422:17): [True: 0, False: 0]
1423
0
                    return std::move(subres[0]);
1424
0
                case Fragment::WRAP_D: {
  Branch (1424:17): [True: 0, False: 0]
  Branch (1424:17): [True: 0, False: 0]
  Branch (1424:17): [True: 0, False: 0]
1425
0
                    auto &x = subres[0];
1426
0
                    return {ZERO, x.sat + ONE};
1427
0
                }
1428
0
                case Fragment::WRAP_J: {
  Branch (1428:17): [True: 0, False: 0]
  Branch (1428:17): [True: 0, False: 0]
  Branch (1428:17): [True: 0, False: 0]
1429
0
                    auto &x = subres[0];
1430
                    // If a dissatisfaction with a nonzero top stack element exists, an alternative dissatisfaction exists.
1431
                    // As the dissatisfaction logic currently doesn't keep track of this nonzeroness property, and thus even
1432
                    // if a dissatisfaction with a top zero element is found, we don't know whether another one with a
1433
                    // nonzero top stack element exists. Make the conservative assumption that whenever the subexpression is weakly
1434
                    // dissatisfiable, this alternative dissatisfaction exists and leads to malleability.
1435
0
                    return {InputStack(ZERO).SetMalleable(x.nsat.available != Availability::NO && !x.nsat.has_sig), std::move(x.sat)};
  Branch (1435:59): [True: 0, False: 0]
  Branch (1435:99): [True: 0, False: 0]
  Branch (1435:59): [True: 0, False: 0]
  Branch (1435:99): [True: 0, False: 0]
  Branch (1435:59): [True: 0, False: 0]
  Branch (1435:99): [True: 0, False: 0]
1436
0
                }
1437
0
                case Fragment::WRAP_V: {
  Branch (1437:17): [True: 0, False: 0]
  Branch (1437:17): [True: 0, False: 0]
  Branch (1437:17): [True: 0, False: 0]
1438
0
                    auto &x = subres[0];
1439
0
                    return {INVALID, std::move(x.sat)};
1440
0
                }
1441
0
                case Fragment::JUST_0: return {EMPTY, INVALID};
  Branch (1441:17): [True: 0, False: 0]
  Branch (1441:17): [True: 0, False: 0]
  Branch (1441:17): [True: 0, False: 0]
1442
0
                case Fragment::JUST_1: return {INVALID, EMPTY};
  Branch (1442:17): [True: 0, False: 0]
  Branch (1442:17): [True: 0, False: 0]
  Branch (1442:17): [True: 0, False: 0]
1443
0
            }
1444
0
            assert(false);
  Branch (1444:13): [Folded, False: 0]
  Branch (1444:13): [Folded, False: 0]
  Branch (1444:13): [Folded, False: 0]
1445
0
            return {INVALID, INVALID};
1446
0
        };
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeI7CPubKeyE12ProduceInputIN12_GLOBAL__N_116SatisfierContextEEENS_8internal11InputResultERKT_ENKUlRKS2_St4spanIS7_Lm18446744073709551615EEE_clESC_SE_
Unexecuted instantiation: _ZZNK10miniscript4NodeI11XOnlyPubKeyE12ProduceInputI12TapSatisfierEENS_8internal11InputResultERKT_ENKUlRKS2_St4spanIS6_Lm18446744073709551615EEE_clESB_SD_
Unexecuted instantiation: _ZZNK10miniscript4NodeI7CPubKeyE12ProduceInputI12WshSatisfierEENS_8internal11InputResultERKT_ENKUlRKS2_St4spanIS6_Lm18446744073709551615EEE_clESB_SD_
1447
1448
0
        auto tester = [&helper](const Node& node, std::span<InputResult> subres) -> InputResult {
1449
0
            auto ret = helper(node, subres);
1450
1451
            // Do a consistency check between the satisfaction code and the type checker
1452
            // (the actual satisfaction code in ProduceInputHelper does not use GetType)
1453
1454
            // For 'z' nodes, available satisfactions/dissatisfactions must have stack size 0.
1455
0
            if (node.GetType() << "z"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() == 0);
  Branch (1455:17): [True: 0, False: 0]
  Branch (1455:17): [True: 0, False: 0]
  Branch (1455:46): [True: 0, False: 0]
  Branch (1455:17): [True: 0, False: 0]
  Branch (1455:17): [True: 0, False: 0]
  Branch (1455:46): [True: 0, False: 0]
  Branch (1455:17): [True: 0, False: 0]
  Branch (1455:17): [True: 0, False: 0]
  Branch (1455:46): [True: 0, False: 0]
1456
0
            if (node.GetType() << "z"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() == 0);
  Branch (1456:17): [True: 0, False: 0]
  Branch (1456:17): [True: 0, False: 0]
  Branch (1456:46): [True: 0, False: 0]
  Branch (1456:17): [True: 0, False: 0]
  Branch (1456:17): [True: 0, False: 0]
  Branch (1456:46): [True: 0, False: 0]
  Branch (1456:17): [True: 0, False: 0]
  Branch (1456:17): [True: 0, False: 0]
  Branch (1456:46): [True: 0, False: 0]
1457
1458
            // For 'o' nodes, available satisfactions/dissatisfactions must have stack size 1.
1459
0
            if (node.GetType() << "o"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() == 1);
  Branch (1459:17): [True: 0, False: 0]
  Branch (1459:17): [True: 0, False: 0]
  Branch (1459:46): [True: 0, False: 0]
  Branch (1459:17): [True: 0, False: 0]
  Branch (1459:17): [True: 0, False: 0]
  Branch (1459:46): [True: 0, False: 0]
  Branch (1459:17): [True: 0, False: 0]
  Branch (1459:17): [True: 0, False: 0]
  Branch (1459:46): [True: 0, False: 0]
1460
0
            if (node.GetType() << "o"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() == 1);
  Branch (1460:17): [True: 0, False: 0]
  Branch (1460:17): [True: 0, False: 0]
  Branch (1460:46): [True: 0, False: 0]
  Branch (1460:17): [True: 0, False: 0]
  Branch (1460:17): [True: 0, False: 0]
  Branch (1460:46): [True: 0, False: 0]
  Branch (1460:17): [True: 0, False: 0]
  Branch (1460:17): [True: 0, False: 0]
  Branch (1460:46): [True: 0, False: 0]
1461
1462
            // For 'n' nodes, available satisfactions/dissatisfactions must have stack size 1 or larger. For satisfactions,
1463
            // the top element cannot be 0.
1464
0
            if (node.GetType() << "n"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() >= 1);
  Branch (1464:17): [True: 0, False: 0]
  Branch (1464:17): [True: 0, False: 0]
  Branch (1464:46): [True: 0, False: 0]
  Branch (1464:17): [True: 0, False: 0]
  Branch (1464:17): [True: 0, False: 0]
  Branch (1464:46): [True: 0, False: 0]
  Branch (1464:17): [True: 0, False: 0]
  Branch (1464:17): [True: 0, False: 0]
  Branch (1464:46): [True: 0, False: 0]
1465
0
            if (node.GetType() << "n"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() >= 1);
  Branch (1465:17): [True: 0, False: 0]
  Branch (1465:17): [True: 0, False: 0]
  Branch (1465:46): [True: 0, False: 0]
  Branch (1465:17): [True: 0, False: 0]
  Branch (1465:17): [True: 0, False: 0]
  Branch (1465:46): [True: 0, False: 0]
  Branch (1465:17): [True: 0, False: 0]
  Branch (1465:17): [True: 0, False: 0]
  Branch (1465:46): [True: 0, False: 0]
1466
0
            if (node.GetType() << "n"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(!ret.sat.stack.back().empty());
  Branch (1466:17): [True: 0, False: 0]
  Branch (1466:17): [True: 0, False: 0]
  Branch (1466:46): [True: 0, False: 0]
  Branch (1466:17): [True: 0, False: 0]
  Branch (1466:17): [True: 0, False: 0]
  Branch (1466:46): [True: 0, False: 0]
  Branch (1466:17): [True: 0, False: 0]
  Branch (1466:17): [True: 0, False: 0]
  Branch (1466:46): [True: 0, False: 0]
1467
1468
            // For 'd' nodes, a dissatisfaction must exist, and they must not need a signature. If it is non-malleable,
1469
            // it must be canonical.
1470
0
            if (node.GetType() << "d"_mst) CHECK_NONFATAL(ret.nsat.available != Availability::NO);
  Branch (1470:17): [True: 0, False: 0]
  Branch (1470:17): [True: 0, False: 0]
  Branch (1470:17): [True: 0, False: 0]
1471
0
            if (node.GetType() << "d"_mst) CHECK_NONFATAL(!ret.nsat.has_sig);
  Branch (1471:17): [True: 0, False: 0]
  Branch (1471:17): [True: 0, False: 0]
  Branch (1471:17): [True: 0, False: 0]
1472
0
            if (node.GetType() << "d"_mst && !ret.nsat.malleable) CHECK_NONFATAL(!ret.nsat.non_canon);
  Branch (1472:17): [True: 0, False: 0]
  Branch (1472:17): [True: 0, False: 0]
  Branch (1472:46): [True: 0, False: 0]
  Branch (1472:17): [True: 0, False: 0]
  Branch (1472:17): [True: 0, False: 0]
  Branch (1472:46): [True: 0, False: 0]
  Branch (1472:17): [True: 0, False: 0]
  Branch (1472:17): [True: 0, False: 0]
  Branch (1472:46): [True: 0, False: 0]
1473
1474
            // For 'f'/'s' nodes, dissatisfactions/satisfactions must have a signature.
1475
0
            if (node.GetType() << "f"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.has_sig);
  Branch (1475:17): [True: 0, False: 0]
  Branch (1475:17): [True: 0, False: 0]
  Branch (1475:46): [True: 0, False: 0]
  Branch (1475:17): [True: 0, False: 0]
  Branch (1475:17): [True: 0, False: 0]
  Branch (1475:46): [True: 0, False: 0]
  Branch (1475:17): [True: 0, False: 0]
  Branch (1475:17): [True: 0, False: 0]
  Branch (1475:46): [True: 0, False: 0]
1476
0
            if (node.GetType() << "s"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.has_sig);
  Branch (1476:17): [True: 0, False: 0]
  Branch (1476:17): [True: 0, False: 0]
  Branch (1476:46): [True: 0, False: 0]
  Branch (1476:17): [True: 0, False: 0]
  Branch (1476:17): [True: 0, False: 0]
  Branch (1476:46): [True: 0, False: 0]
  Branch (1476:17): [True: 0, False: 0]
  Branch (1476:17): [True: 0, False: 0]
  Branch (1476:46): [True: 0, False: 0]
1477
1478
            // For non-malleable 'e' nodes, a non-malleable dissatisfaction must exist.
1479
0
            if (node.GetType() << "me"_mst) CHECK_NONFATAL(ret.nsat.available != Availability::NO);
  Branch (1479:17): [True: 0, False: 0]
  Branch (1479:17): [True: 0, False: 0]
  Branch (1479:17): [True: 0, False: 0]
1480
0
            if (node.GetType() << "me"_mst) CHECK_NONFATAL(!ret.nsat.malleable);
  Branch (1480:17): [True: 0, False: 0]
  Branch (1480:17): [True: 0, False: 0]
  Branch (1480:17): [True: 0, False: 0]
1481
1482
            // For 'm' nodes, if a satisfaction exists, it must be non-malleable.
1483
0
            if (node.GetType() << "m"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(!ret.sat.malleable);
  Branch (1483:17): [True: 0, False: 0]
  Branch (1483:17): [True: 0, False: 0]
  Branch (1483:46): [True: 0, False: 0]
  Branch (1483:17): [True: 0, False: 0]
  Branch (1483:17): [True: 0, False: 0]
  Branch (1483:46): [True: 0, False: 0]
  Branch (1483:17): [True: 0, False: 0]
  Branch (1483:17): [True: 0, False: 0]
  Branch (1483:46): [True: 0, False: 0]
1484
1485
            // If a non-malleable satisfaction exists, it must be canonical.
1486
0
            if (ret.sat.available != Availability::NO && !ret.sat.malleable) CHECK_NONFATAL(!ret.sat.non_canon);
  Branch (1486:17): [True: 0, False: 0]
  Branch (1486:58): [True: 0, False: 0]
  Branch (1486:17): [True: 0, False: 0]
  Branch (1486:58): [True: 0, False: 0]
  Branch (1486:17): [True: 0, False: 0]
  Branch (1486:58): [True: 0, False: 0]
1487
1488
0
            return ret;
1489
0
        };
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeI7CPubKeyE12ProduceInputIN12_GLOBAL__N_116SatisfierContextEEENS_8internal11InputResultERKT_ENKUlRKS2_St4spanIS7_Lm18446744073709551615EEE0_clESC_SE_
Unexecuted instantiation: _ZZNK10miniscript4NodeI11XOnlyPubKeyE12ProduceInputI12TapSatisfierEENS_8internal11InputResultERKT_ENKUlRKS2_St4spanIS6_Lm18446744073709551615EEE0_clESB_SD_
Unexecuted instantiation: _ZZNK10miniscript4NodeI7CPubKeyE12ProduceInputI12WshSatisfierEENS_8internal11InputResultERKT_ENKUlRKS2_St4spanIS6_Lm18446744073709551615EEE0_clESB_SD_
1490
1491
0
        return TreeEval<InputResult>(tester);
1492
0
    }
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeI7CPubKeyE12ProduceInputIN12_GLOBAL__N_116SatisfierContextEEENS_8internal11InputResultERKT_
Unexecuted instantiation: _ZNK10miniscript4NodeI11XOnlyPubKeyE12ProduceInputI12TapSatisfierEENS_8internal11InputResultERKT_
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE12ProduceInputI12WshSatisfierEENS_8internal11InputResultERKT_
1493
1494
public:
1495
    /** Update duplicate key information in this Node.
1496
     *
1497
     * This uses a custom key comparator provided by the context in order to still detect duplicates
1498
     * for more complicated types.
1499
     */
1500
    template<typename Ctx> void DuplicateKeyCheck(const Ctx& ctx) const
1501
0
    {
1502
        // We cannot use a lambda here, as lambdas are non assignable, and the set operations
1503
        // below require moving the comparators around.
1504
0
        struct Comp {
1505
0
            const Ctx* ctx_ptr;
1506
0
            Comp(const Ctx& ctx) : ctx_ptr(&ctx) {}
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeI7CPubKeyE17DuplicateKeyCheckIN12_GLOBAL__N_113ParserContextEEEvRKT_EN4CompC2ERKS5_
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeI7CPubKeyE17DuplicateKeyCheckIN12_GLOBAL__N_113KeyComparatorEEEvRKT_EN4CompC2ERKS5_
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEE17DuplicateKeyCheckIS2_EEvRKT_EN4CompC2ERKS2_
Unexecuted instantiation: descriptor.cpp:_ZZNK10miniscript4NodeIjE17DuplicateKeyCheckIN12_GLOBAL__N_19KeyParserEEEvRKT_EN4CompC2ERKS4_
Unexecuted instantiation: _ZZNK10miniscript4NodeI11XOnlyPubKeyE17DuplicateKeyCheckI12TapSatisfierEEvRKT_EN4CompC2ERKS4_
Unexecuted instantiation: _ZZNK10miniscript4NodeI7CPubKeyE17DuplicateKeyCheckI12WshSatisfierEEvRKT_EN4CompC2ERKS4_
1507
0
            bool operator()(const Key& a, const Key& b) const { return ctx_ptr->KeyCompare(a, b); }
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeI7CPubKeyE17DuplicateKeyCheckIN12_GLOBAL__N_113ParserContextEEEvRKT_ENK4CompclERKS1_SB_
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeI7CPubKeyE17DuplicateKeyCheckIN12_GLOBAL__N_113KeyComparatorEEEvRKT_ENK4CompclERKS1_SB_
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEE17DuplicateKeyCheckIS2_EEvRKT_ENK4CompclERKS3_SB_
Unexecuted instantiation: descriptor.cpp:_ZZNK10miniscript4NodeIjE17DuplicateKeyCheckIN12_GLOBAL__N_19KeyParserEEEvRKT_ENK4CompclERKjSA_
Unexecuted instantiation: _ZZNK10miniscript4NodeI11XOnlyPubKeyE17DuplicateKeyCheckI12TapSatisfierEEvRKT_ENK4CompclERKS1_SA_
Unexecuted instantiation: _ZZNK10miniscript4NodeI7CPubKeyE17DuplicateKeyCheckI12WshSatisfierEEvRKT_ENK4CompclERKS1_SA_
1508
0
        };
1509
1510
        // state in the recursive computation:
1511
        // - std::nullopt means "this node has duplicates"
1512
        // - an std::set means "this node has no duplicate keys, and they are: ...".
1513
0
        using keyset = std::set<Key, Comp>;
1514
0
        using state = std::optional<keyset>;
1515
1516
0
        auto upfn = [&ctx](const Node& node, std::span<state> subs) -> state {
1517
            // If this node is already known to have duplicates, nothing left to do.
1518
0
            if (node.has_duplicate_keys.has_value() && *node.has_duplicate_keys) return {};
  Branch (1518:17): [True: 0, False: 0]
  Branch (1518:56): [True: 0, False: 0]
  Branch (1518:17): [True: 0, False: 0]
  Branch (1518:56): [True: 0, False: 0]
  Branch (1518:17): [True: 0, False: 0]
  Branch (1518:56): [True: 0, False: 0]
  Branch (1518:17): [True: 0, False: 0]
  Branch (1518:56): [True: 0, False: 0]
  Branch (1518:17): [True: 0, False: 0]
  Branch (1518:56): [True: 0, False: 0]
  Branch (1518:17): [True: 0, False: 0]
  Branch (1518:56): [True: 0, False: 0]
1519
1520
            // Check if one of the children is already known to have duplicates.
1521
0
            for (auto& sub : subs) {
  Branch (1521:28): [True: 0, False: 0]
  Branch (1521:28): [True: 0, False: 0]
  Branch (1521:28): [True: 0, False: 0]
  Branch (1521:28): [True: 0, False: 0]
  Branch (1521:28): [True: 0, False: 0]
  Branch (1521:28): [True: 0, False: 0]
1522
0
                if (!sub.has_value()) {
  Branch (1522:21): [True: 0, False: 0]
  Branch (1522:21): [True: 0, False: 0]
  Branch (1522:21): [True: 0, False: 0]
  Branch (1522:21): [True: 0, False: 0]
  Branch (1522:21): [True: 0, False: 0]
  Branch (1522:21): [True: 0, False: 0]
1523
0
                    node.has_duplicate_keys = true;
1524
0
                    return {};
1525
0
                }
1526
0
            }
1527
1528
            // Start building the set of keys involved in this node and children.
1529
            // Start by keys in this node directly.
1530
0
            size_t keys_count = node.keys.size();
1531
0
            keyset key_set{node.keys.begin(), node.keys.end(), Comp(ctx)};
1532
0
            if (key_set.size() != keys_count) {
  Branch (1532:17): [True: 0, False: 0]
  Branch (1532:17): [True: 0, False: 0]
  Branch (1532:17): [True: 0, False: 0]
  Branch (1532:17): [True: 0, False: 0]
  Branch (1532:17): [True: 0, False: 0]
  Branch (1532:17): [True: 0, False: 0]
1533
                // It already has duplicates; bail out.
1534
0
                node.has_duplicate_keys = true;
1535
0
                return {};
1536
0
            }
1537
1538
            // Merge the keys from the children into this set.
1539
0
            for (auto& sub : subs) {
  Branch (1539:28): [True: 0, False: 0]
  Branch (1539:28): [True: 0, False: 0]
  Branch (1539:28): [True: 0, False: 0]
  Branch (1539:28): [True: 0, False: 0]
  Branch (1539:28): [True: 0, False: 0]
  Branch (1539:28): [True: 0, False: 0]
1540
0
                keys_count += sub->size();
1541
                // Small optimization: std::set::merge is linear in the size of the second arg but
1542
                // logarithmic in the size of the first.
1543
0
                if (key_set.size() < sub->size()) std::swap(key_set, *sub);
  Branch (1543:21): [True: 0, False: 0]
  Branch (1543:21): [True: 0, False: 0]
  Branch (1543:21): [True: 0, False: 0]
  Branch (1543:21): [True: 0, False: 0]
  Branch (1543:21): [True: 0, False: 0]
  Branch (1543:21): [True: 0, False: 0]
1544
0
                key_set.merge(*sub);
1545
0
                if (key_set.size() != keys_count) {
  Branch (1545:21): [True: 0, False: 0]
  Branch (1545:21): [True: 0, False: 0]
  Branch (1545:21): [True: 0, False: 0]
  Branch (1545:21): [True: 0, False: 0]
  Branch (1545:21): [True: 0, False: 0]
  Branch (1545:21): [True: 0, False: 0]
1546
0
                    node.has_duplicate_keys = true;
1547
0
                    return {};
1548
0
                }
1549
0
            }
1550
1551
0
            node.has_duplicate_keys = false;
1552
0
            return key_set;
1553
0
        };
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeI7CPubKeyE17DuplicateKeyCheckIN12_GLOBAL__N_113ParserContextEEEvRKT_ENKUlRKS2_St4spanISt8optionalISt3setIS1_ZNKS3_IS5_EEvS8_E4CompSaIS1_EEELm18446744073709551615EEE_clESA_SI_
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeI7CPubKeyE17DuplicateKeyCheckIN12_GLOBAL__N_113KeyComparatorEEEvRKT_ENKUlRKS2_St4spanISt8optionalISt3setIS1_ZNKS3_IS5_EEvS8_E4CompSaIS1_EEELm18446744073709551615EEE_clESA_SI_
Unexecuted instantiation: miniscript.cpp:_ZZNK10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEE17DuplicateKeyCheckIS2_EEvRKT_ENKUlRKS4_St4spanISt8optionalISt3setIS3_ZNKS5_IS2_EEvS8_E4CompSaIS3_EEELm18446744073709551615EEE_clESA_SI_
Unexecuted instantiation: descriptor.cpp:_ZZNK10miniscript4NodeIjE17DuplicateKeyCheckIN12_GLOBAL__N_19KeyParserEEEvRKT_ENKUlRKS1_St4spanISt8optionalISt3setIjZNKS2_IS4_EEvS7_E4CompSaIjEEELm18446744073709551615EEE_clES9_SH_
Unexecuted instantiation: _ZZNK10miniscript4NodeI11XOnlyPubKeyE17DuplicateKeyCheckI12TapSatisfierEEvRKT_ENKUlRKS2_St4spanISt8optionalISt3setIS1_ZNKS3_IS4_EEvS7_E4CompSaIS1_EEELm18446744073709551615EEE_clES9_SH_
Unexecuted instantiation: _ZZNK10miniscript4NodeI7CPubKeyE17DuplicateKeyCheckI12WshSatisfierEEvRKT_ENKUlRKS2_St4spanISt8optionalISt3setIS1_ZNKS3_IS4_EEvS7_E4CompSaIS1_EEELm18446744073709551615EEE_clES9_SH_
1554
1555
0
        TreeEval<state>(upfn);
1556
0
    }
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeI7CPubKeyE17DuplicateKeyCheckIN12_GLOBAL__N_113ParserContextEEEvRKT_
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeI7CPubKeyE17DuplicateKeyCheckIN12_GLOBAL__N_113KeyComparatorEEEvRKT_
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEE17DuplicateKeyCheckIS2_EEvRKT_
Unexecuted instantiation: descriptor.cpp:_ZNK10miniscript4NodeIjE17DuplicateKeyCheckIN12_GLOBAL__N_19KeyParserEEEvRKT_
Unexecuted instantiation: _ZNK10miniscript4NodeI11XOnlyPubKeyE17DuplicateKeyCheckI12TapSatisfierEEvRKT_
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE17DuplicateKeyCheckI12WshSatisfierEEvRKT_
1557
1558
    //! Return the size of the script for this expression (faster than ToScript().size()).
1559
0
    size_t ScriptSize() const { return scriptlen; }
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE10ScriptSizeEv
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEE10ScriptSizeEv
Unexecuted instantiation: _ZNK10miniscript4NodeIjE10ScriptSizeEv
Unexecuted instantiation: _ZNK10miniscript4NodeI11XOnlyPubKeyE10ScriptSizeEv
1560
1561
    //! Return the maximum number of ops needed to satisfy this script non-malleably.
1562
0
    std::optional<uint32_t> GetOps() const {
1563
0
        if (!ops.sat.Valid()) return {};
  Branch (1563:13): [True: 0, False: 0]
  Branch (1563:13): [True: 0, False: 0]
1564
0
        return ops.count + ops.sat.Value();
1565
0
    }
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE6GetOpsEv
Unexecuted instantiation: _ZNK10miniscript4NodeIjE6GetOpsEv
1566
1567
    //! Return the number of ops in the script (not counting the dynamic ones that depend on execution).
1568
0
    uint32_t GetStaticOps() const { return ops.count; }
1569
1570
    //! Check the ops limit of this script against the consensus limit.
1571
0
    bool CheckOpsLimit() const {
1572
0
        if (IsTapscript(m_script_ctx)) return true;
  Branch (1572:13): [True: 0, False: 0]
  Branch (1572:13): [True: 0, False: 0]
1573
0
        if (const auto ops = GetOps()) return *ops <= MAX_OPS_PER_SCRIPT;
  Branch (1573:24): [True: 0, False: 0]
  Branch (1573:24): [True: 0, False: 0]
1574
0
        return true;
1575
0
    }
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE13CheckOpsLimitEv
Unexecuted instantiation: _ZNK10miniscript4NodeIjE13CheckOpsLimitEv
1576
1577
    /** Whether this node is of type B, K or W. (That is, anything but V.) */
1578
0
    bool IsBKW() const {
1579
0
        return !((GetType() & "BKW"_mst) == ""_mst);
1580
0
    }
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE5IsBKWEv
Unexecuted instantiation: _ZNK10miniscript4NodeIjE5IsBKWEv
1581
1582
    /** Return the maximum number of stack elements needed to satisfy this script non-malleably. */
1583
0
    std::optional<uint32_t> GetStackSize() const {
1584
0
        if (!ss.Sat().Valid()) return {};
  Branch (1584:13): [True: 0, False: 0]
  Branch (1584:13): [True: 0, False: 0]
1585
0
        return ss.Sat().NetDiff() + static_cast<int32_t>(IsBKW());
1586
0
    }
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE12GetStackSizeEv
Unexecuted instantiation: _ZNK10miniscript4NodeIjE12GetStackSizeEv
1587
1588
    //! Return the maximum size of the stack during execution of this script.
1589
0
    std::optional<uint32_t> GetExecStackSize() const {
1590
0
        if (!ss.Sat().Valid()) return {};
  Branch (1590:13): [True: 0, False: 0]
  Branch (1590:13): [True: 0, False: 0]
1591
0
        return ss.Sat().Exec() + static_cast<int32_t>(IsBKW());
1592
0
    }
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE16GetExecStackSizeEv
Unexecuted instantiation: _ZNK10miniscript4NodeIjE16GetExecStackSizeEv
1593
1594
    //! Check the maximum stack size for this script against the policy limit.
1595
0
    bool CheckStackSize() const {
1596
        // Since in Tapscript there is no standardness limit on the script and witness sizes, we may run
1597
        // into the maximum stack size while executing the script. Make sure it doesn't happen.
1598
0
        if (IsTapscript(m_script_ctx)) {
  Branch (1598:13): [True: 0, False: 0]
  Branch (1598:13): [True: 0, False: 0]
1599
0
            if (const auto exec_ss = GetExecStackSize()) return exec_ss <= MAX_STACK_SIZE;
  Branch (1599:28): [True: 0, False: 0]
  Branch (1599:28): [True: 0, False: 0]
1600
0
            return true;
1601
0
        }
1602
0
        if (const auto ss = GetStackSize()) return *ss <= MAX_STANDARD_P2WSH_STACK_ITEMS;
  Branch (1602:24): [True: 0, False: 0]
  Branch (1602:24): [True: 0, False: 0]
1603
0
        return true;
1604
0
    }
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE14CheckStackSizeEv
Unexecuted instantiation: _ZNK10miniscript4NodeIjE14CheckStackSizeEv
1605
1606
    //! Whether no satisfaction exists for this node.
1607
0
    bool IsNotSatisfiable() const { return !GetStackSize(); }
1608
1609
    /** Return the maximum size in bytes of a witness to satisfy this script non-malleably. Note this does
1610
     * not include the witness script push. */
1611
0
    std::optional<uint32_t> GetWitnessSize() const {
1612
0
        if (!ws.sat.Valid()) return {};
  Branch (1612:13): [True: 0, False: 0]
  Branch (1612:13): [True: 0, False: 0]
1613
0
        return ws.sat.Value();
1614
0
    }
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE14GetWitnessSizeEv
Unexecuted instantiation: _ZNK10miniscript4NodeIjE14GetWitnessSizeEv
1615
1616
    //! Return the expression type.
1617
0
    Type GetType() const { return typ; }
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE7GetTypeEv
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEE7GetTypeEv
Unexecuted instantiation: _ZNK10miniscript4NodeIjE7GetTypeEv
Unexecuted instantiation: _ZNK10miniscript4NodeI11XOnlyPubKeyE7GetTypeEv
1618
1619
    //! Return the script context for this node.
1620
0
    MiniscriptContext GetMsCtx() const { return m_script_ctx; }
1621
1622
    //! Find an insane subnode which has no insane children. Nullptr if there is none.
1623
0
    const Node* FindInsaneSub() const {
1624
0
        return TreeEval<const Node*>([](const Node& node, std::span<const Node*> subs) -> const Node* {
1625
0
            for (auto& sub: subs) if (sub) return sub;
  Branch (1625:27): [True: 0, False: 0]
  Branch (1625:39): [True: 0, False: 0]
1626
0
            if (!node.IsSaneSubexpression()) return &node;
  Branch (1626:17): [True: 0, False: 0]
1627
0
            return nullptr;
1628
0
        });
1629
0
    }
1630
1631
    //! Determine whether a Miniscript node is satisfiable. fn(node) will be invoked for all
1632
    //! key, time, and hashing nodes, and should return their satisfiability.
1633
    template<typename F>
1634
    bool IsSatisfiable(F fn) const
1635
0
    {
1636
        // TreeEval() doesn't support bool as NodeType, so use int instead.
1637
0
        return TreeEval<int>([&fn](const Node& node, std::span<int> subs) -> bool {
1638
0
            switch (node.fragment) {
1639
0
                case Fragment::JUST_0:
  Branch (1639:17): [True: 0, False: 0]
1640
0
                    return false;
1641
0
                case Fragment::JUST_1:
  Branch (1641:17): [True: 0, False: 0]
1642
0
                    return true;
1643
0
                case Fragment::PK_K:
  Branch (1643:17): [True: 0, False: 0]
1644
0
                case Fragment::PK_H:
  Branch (1644:17): [True: 0, False: 0]
1645
0
                case Fragment::MULTI:
  Branch (1645:17): [True: 0, False: 0]
1646
0
                case Fragment::MULTI_A:
  Branch (1646:17): [True: 0, False: 0]
1647
0
                case Fragment::AFTER:
  Branch (1647:17): [True: 0, False: 0]
1648
0
                case Fragment::OLDER:
  Branch (1648:17): [True: 0, False: 0]
1649
0
                case Fragment::HASH256:
  Branch (1649:17): [True: 0, False: 0]
1650
0
                case Fragment::HASH160:
  Branch (1650:17): [True: 0, False: 0]
1651
0
                case Fragment::SHA256:
  Branch (1651:17): [True: 0, False: 0]
1652
0
                case Fragment::RIPEMD160:
  Branch (1652:17): [True: 0, False: 0]
1653
0
                    return bool{fn(node)};
1654
0
                case Fragment::ANDOR:
  Branch (1654:17): [True: 0, False: 0]
1655
0
                    return (subs[0] && subs[1]) || subs[2];
  Branch (1655:29): [True: 0, False: 0]
  Branch (1655:40): [True: 0, False: 0]
  Branch (1655:52): [True: 0, False: 0]
1656
0
                case Fragment::AND_V:
  Branch (1656:17): [True: 0, False: 0]
1657
0
                case Fragment::AND_B:
  Branch (1657:17): [True: 0, False: 0]
1658
0
                    return subs[0] && subs[1];
  Branch (1658:28): [True: 0, False: 0]
  Branch (1658:39): [True: 0, False: 0]
1659
0
                case Fragment::OR_B:
  Branch (1659:17): [True: 0, False: 0]
1660
0
                case Fragment::OR_C:
  Branch (1660:17): [True: 0, False: 0]
1661
0
                case Fragment::OR_D:
  Branch (1661:17): [True: 0, False: 0]
1662
0
                case Fragment::OR_I:
  Branch (1662:17): [True: 0, False: 0]
1663
0
                    return subs[0] || subs[1];
  Branch (1663:28): [True: 0, False: 0]
  Branch (1663:39): [True: 0, False: 0]
1664
0
                case Fragment::THRESH:
  Branch (1664:17): [True: 0, False: 0]
1665
0
                    return static_cast<uint32_t>(std::count(subs.begin(), subs.end(), true)) >= node.k;
1666
0
                default: // wrappers
  Branch (1666:17): [True: 0, False: 0]
1667
0
                    assert(subs.size() >= 1);
  Branch (1667:21): [True: 0, False: 0]
1668
0
                    CHECK_NONFATAL(subs.size() == 1);
1669
0
                    return subs[0];
1670
0
            }
1671
0
        });
1672
0
    }
1673
1674
    //! Check whether this node is valid at all.
1675
0
    bool IsValid() const {
1676
0
        if (GetType() == ""_mst) return false;
  Branch (1676:13): [True: 0, False: 0]
  Branch (1676:13): [True: 0, False: 0]
  Branch (1676:13): [True: 0, False: 0]
  Branch (1676:13): [True: 0, False: 0]
1677
0
        return ScriptSize() <= internal::MaxScriptSize(m_script_ctx);
1678
0
    }
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE7IsValidEv
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEE7IsValidEv
Unexecuted instantiation: _ZNK10miniscript4NodeIjE7IsValidEv
Unexecuted instantiation: _ZNK10miniscript4NodeI11XOnlyPubKeyE7IsValidEv
1679
1680
    //! Check whether this node is valid as a script on its own.
1681
0
    bool IsValidTopLevel() const { return IsValid() && GetType() << "B"_mst; }
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE15IsValidTopLevelEv
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEE15IsValidTopLevelEv
Unexecuted instantiation: _ZNK10miniscript4NodeIjE15IsValidTopLevelEv
Unexecuted instantiation: _ZNK10miniscript4NodeI11XOnlyPubKeyE15IsValidTopLevelEv
  Branch (1681:43): [True: 0, False: 0]
  Branch (1681:56): [True: 0, False: 0]
  Branch (1681:43): [True: 0, False: 0]
  Branch (1681:56): [True: 0, False: 0]
  Branch (1681:43): [True: 0, False: 0]
  Branch (1681:56): [True: 0, False: 0]
  Branch (1681:43): [True: 0, False: 0]
  Branch (1681:56): [True: 0, False: 0]
1682
1683
    //! Check whether this script can always be satisfied in a non-malleable way.
1684
0
    bool IsNonMalleable() const { return GetType() << "m"_mst; }
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE14IsNonMalleableEv
Unexecuted instantiation: _ZNK10miniscript4NodeIjE14IsNonMalleableEv
1685
1686
    //! Check whether this script always needs a signature.
1687
0
    bool NeedsSignature() const { return GetType() << "s"_mst; }
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE14NeedsSignatureEv
Unexecuted instantiation: _ZNK10miniscript4NodeIjE14NeedsSignatureEv
1688
1689
    //! Check whether there is no satisfaction path that contains both timelocks and heightlocks
1690
0
    bool CheckTimeLocksMix() const { return GetType() << "k"_mst; }
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE17CheckTimeLocksMixEv
Unexecuted instantiation: _ZNK10miniscript4NodeIjE17CheckTimeLocksMixEv
1691
1692
    //! Check whether there is no duplicate key across this fragment and all its sub-fragments.
1693
0
    bool CheckDuplicateKey() const { return has_duplicate_keys && !*has_duplicate_keys; }
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE17CheckDuplicateKeyEv
Unexecuted instantiation: _ZNK10miniscript4NodeIjE17CheckDuplicateKeyEv
  Branch (1693:45): [True: 0, False: 0]
  Branch (1693:67): [True: 0, False: 0]
  Branch (1693:45): [True: 0, False: 0]
  Branch (1693:67): [True: 0, False: 0]
1694
1695
    //! Whether successful non-malleable satisfactions are guaranteed to be valid.
1696
0
    bool ValidSatisfactions() const { return IsValid() && CheckOpsLimit() && CheckStackSize(); }
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE18ValidSatisfactionsEv
Unexecuted instantiation: _ZNK10miniscript4NodeIjE18ValidSatisfactionsEv
  Branch (1696:46): [True: 0, False: 0]
  Branch (1696:59): [True: 0, False: 0]
  Branch (1696:78): [True: 0, False: 0]
  Branch (1696:46): [True: 0, False: 0]
  Branch (1696:59): [True: 0, False: 0]
  Branch (1696:78): [True: 0, False: 0]
1697
1698
    //! Whether the apparent policy of this node matches its script semantics. Doesn't guarantee it is a safe script on its own.
1699
0
    bool IsSaneSubexpression() const { return ValidSatisfactions() && IsNonMalleable() && CheckTimeLocksMix() && CheckDuplicateKey(); }
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE19IsSaneSubexpressionEv
Unexecuted instantiation: _ZNK10miniscript4NodeIjE19IsSaneSubexpressionEv
  Branch (1699:47): [True: 0, False: 0]
  Branch (1699:71): [True: 0, False: 0]
  Branch (1699:91): [True: 0, False: 0]
  Branch (1699:114): [True: 0, False: 0]
  Branch (1699:47): [True: 0, False: 0]
  Branch (1699:71): [True: 0, False: 0]
  Branch (1699:91): [True: 0, False: 0]
  Branch (1699:114): [True: 0, False: 0]
1700
1701
    //! Check whether this node is safe as a script on its own.
1702
0
    bool IsSane() const { return IsValidTopLevel() && IsSaneSubexpression() && NeedsSignature(); }
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE6IsSaneEv
Unexecuted instantiation: _ZNK10miniscript4NodeIjE6IsSaneEv
  Branch (1702:34): [True: 0, False: 0]
  Branch (1702:55): [True: 0, False: 0]
  Branch (1702:80): [True: 0, False: 0]
  Branch (1702:34): [True: 0, False: 0]
  Branch (1702:55): [True: 0, False: 0]
  Branch (1702:80): [True: 0, False: 0]
1703
1704
    //! Produce a witness for this script, if possible and given the information available in the context.
1705
    //! The non-malleable satisfaction is guaranteed to be valid if it exists, and ValidSatisfaction()
1706
    //! is true. If IsSane() holds, this satisfaction is guaranteed to succeed in case the node's
1707
    //! conditions are satisfied (private keys and hash preimages available, locktimes satisfied).
1708
    template<typename Ctx>
1709
0
    Availability Satisfy(const Ctx& ctx, std::vector<std::vector<unsigned char>>& stack, bool nonmalleable = true) const {
1710
0
        auto ret = ProduceInput(ctx);
1711
0
        if (nonmalleable && (ret.sat.malleable || !ret.sat.has_sig)) return Availability::NO;
  Branch (1711:13): [True: 0, False: 0]
  Branch (1711:30): [True: 0, False: 0]
  Branch (1711:51): [True: 0, False: 0]
  Branch (1711:13): [True: 0, False: 0]
  Branch (1711:30): [True: 0, False: 0]
  Branch (1711:51): [True: 0, False: 0]
  Branch (1711:13): [True: 0, False: 0]
  Branch (1711:30): [True: 0, False: 0]
  Branch (1711:51): [True: 0, False: 0]
1712
0
        stack = std::move(ret.sat.stack);
1713
0
        return ret.sat.available;
1714
0
    }
Unexecuted instantiation: miniscript.cpp:_ZNK10miniscript4NodeI7CPubKeyE7SatisfyIN12_GLOBAL__N_116SatisfierContextEEENS_12AvailabilityERKT_RSt6vectorISA_IhSaIhEESaISC_EEb
Unexecuted instantiation: _ZNK10miniscript4NodeI11XOnlyPubKeyE7SatisfyI12TapSatisfierEENS_12AvailabilityERKT_RSt6vectorIS9_IhSaIhEESaISB_EEb
Unexecuted instantiation: _ZNK10miniscript4NodeI7CPubKeyE7SatisfyI12WshSatisfierEENS_12AvailabilityERKT_RSt6vectorIS9_IhSaIhEESaISB_EEb
1715
1716
    //! Equality testing.
1717
0
    bool operator==(const Node<Key>& arg) const { return Compare(*this, arg) == 0; }
1718
1719
    // Constructors with various argument combinations, which bypass the duplicate key check.
1720
    Node(internal::NoDupCheck, MiniscriptContext script_ctx, enum Fragment nt, std::vector<Node> sub, std::vector<unsigned char> arg, uint32_t val = 0)
1721
0
        : fragment(nt), k(val), data(std::move(arg)), subs(std::move(sub)), m_script_ctx{script_ctx}, ops(CalcOps()), ss(CalcStackSize()), ws(CalcWitnessSize()), typ(CalcType()), scriptlen(CalcScriptLen()) {}
1722
    Node(internal::NoDupCheck, MiniscriptContext script_ctx, enum Fragment nt, std::vector<unsigned char> arg, uint32_t val = 0)
1723
0
        : fragment(nt), k(val), data(std::move(arg)), m_script_ctx{script_ctx}, ops(CalcOps()), ss(CalcStackSize()), ws(CalcWitnessSize()), typ(CalcType()), scriptlen(CalcScriptLen()) {}
Unexecuted instantiation: _ZN10miniscript4NodeI7CPubKeyEC2ENS_8internal10NoDupCheckENS_17MiniscriptContextENS_8FragmentESt6vectorIhSaIhEEj
Unexecuted instantiation: miniscript.cpp:_ZN10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEEC2ENS_8internal10NoDupCheckENS_17MiniscriptContextENS_8FragmentESt6vectorIhSaIhEEj
Unexecuted instantiation: _ZN10miniscript4NodeIjEC2ENS_8internal10NoDupCheckENS_17MiniscriptContextENS_8FragmentESt6vectorIhSaIhEEj
Unexecuted instantiation: _ZN10miniscript4NodeI11XOnlyPubKeyEC2ENS_8internal10NoDupCheckENS_17MiniscriptContextENS_8FragmentESt6vectorIhSaIhEEj
1724
    Node(internal::NoDupCheck, MiniscriptContext script_ctx, enum Fragment nt, std::vector<Node> sub, std::vector<Key> key, uint32_t val = 0)
1725
        : fragment(nt), k(val), keys(std::move(key)), m_script_ctx{script_ctx}, subs(std::move(sub)), ops(CalcOps()), ss(CalcStackSize()), ws(CalcWitnessSize()), typ(CalcType()), scriptlen(CalcScriptLen()) {}
1726
    Node(internal::NoDupCheck, MiniscriptContext script_ctx, enum Fragment nt, std::vector<Key> key, uint32_t val = 0)
1727
0
        : fragment(nt), k(val), keys(std::move(key)), m_script_ctx{script_ctx}, ops(CalcOps()), ss(CalcStackSize()), ws(CalcWitnessSize()), typ(CalcType()), scriptlen(CalcScriptLen()) {}
Unexecuted instantiation: _ZN10miniscript4NodeI7CPubKeyEC2ENS_8internal10NoDupCheckENS_17MiniscriptContextENS_8FragmentESt6vectorIS1_SaIS1_EEj
Unexecuted instantiation: miniscript.cpp:_ZN10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEEC2ENS_8internal10NoDupCheckENS_17MiniscriptContextENS_8FragmentESt6vectorIS3_SaIS3_EEj
Unexecuted instantiation: _ZN10miniscript4NodeIjEC2ENS_8internal10NoDupCheckENS_17MiniscriptContextENS_8FragmentESt6vectorIjSaIjEEj
Unexecuted instantiation: _ZN10miniscript4NodeI11XOnlyPubKeyEC2ENS_8internal10NoDupCheckENS_17MiniscriptContextENS_8FragmentESt6vectorIS1_SaIS1_EEj
1728
    Node(internal::NoDupCheck, MiniscriptContext script_ctx, enum Fragment nt, std::vector<Node> sub, uint32_t val = 0)
1729
0
        : fragment(nt), k(val), subs(std::move(sub)), m_script_ctx{script_ctx}, ops(CalcOps()), ss(CalcStackSize()), ws(CalcWitnessSize()), typ(CalcType()), scriptlen(CalcScriptLen()) {}
Unexecuted instantiation: _ZN10miniscript4NodeI7CPubKeyEC2ENS_8internal10NoDupCheckENS_17MiniscriptContextENS_8FragmentESt6vectorIS2_SaIS2_EEj
Unexecuted instantiation: miniscript.cpp:_ZN10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEEC2ENS_8internal10NoDupCheckENS_17MiniscriptContextENS_8FragmentESt6vectorIS4_SaIS4_EEj
Unexecuted instantiation: _ZN10miniscript4NodeIjEC2ENS_8internal10NoDupCheckENS_17MiniscriptContextENS_8FragmentESt6vectorIS1_SaIS1_EEj
Unexecuted instantiation: _ZN10miniscript4NodeI11XOnlyPubKeyEC2ENS_8internal10NoDupCheckENS_17MiniscriptContextENS_8FragmentESt6vectorIS2_SaIS2_EEj
1730
    Node(internal::NoDupCheck, MiniscriptContext script_ctx, enum Fragment nt, uint32_t val = 0)
1731
0
        : fragment(nt), k(val), m_script_ctx{script_ctx}, ops(CalcOps()), ss(CalcStackSize()), ws(CalcWitnessSize()), typ(CalcType()), scriptlen(CalcScriptLen()) {}
Unexecuted instantiation: _ZN10miniscript4NodeI7CPubKeyEC2ENS_8internal10NoDupCheckENS_17MiniscriptContextENS_8FragmentEj
Unexecuted instantiation: miniscript.cpp:_ZN10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEEC2ENS_8internal10NoDupCheckENS_17MiniscriptContextENS_8FragmentEj
Unexecuted instantiation: _ZN10miniscript4NodeIjEC2ENS_8internal10NoDupCheckENS_17MiniscriptContextENS_8FragmentEj
Unexecuted instantiation: _ZN10miniscript4NodeI11XOnlyPubKeyEC2ENS_8internal10NoDupCheckENS_17MiniscriptContextENS_8FragmentEj
1732
1733
    // Constructors with various argument combinations, which do perform the duplicate key check.
1734
    template <typename Ctx> Node(const Ctx& ctx, enum Fragment nt, std::vector<Node> sub, std::vector<unsigned char> arg, uint32_t val = 0)
1735
        : Node(internal::NoDupCheck{}, ctx.MsContext(), nt, std::move(sub), std::move(arg), val) { DuplicateKeyCheck(ctx); }
1736
    template <typename Ctx> Node(const Ctx& ctx, enum Fragment nt, std::vector<unsigned char> arg, uint32_t val = 0)
1737
        : Node(internal::NoDupCheck{}, ctx.MsContext(), nt, std::move(arg), val) { DuplicateKeyCheck(ctx);}
1738
    template <typename Ctx> Node(const Ctx& ctx, enum Fragment nt, std::vector<Node> sub, std::vector<Key> key, uint32_t val = 0)
1739
        : Node(internal::NoDupCheck{}, ctx.MsContext(), nt, std::move(sub), std::move(key), val) { DuplicateKeyCheck(ctx); }
1740
    template <typename Ctx> Node(const Ctx& ctx, enum Fragment nt, std::vector<Key> key, uint32_t val = 0)
1741
        : Node(internal::NoDupCheck{}, ctx.MsContext(), nt, std::move(key), val) { DuplicateKeyCheck(ctx); }
1742
    template <typename Ctx> Node(const Ctx& ctx, enum Fragment nt, std::vector<Node> sub, uint32_t val = 0)
1743
        : Node(internal::NoDupCheck{}, ctx.MsContext(), nt, std::move(sub), val) { DuplicateKeyCheck(ctx); }
1744
    template <typename Ctx> Node(const Ctx& ctx, enum Fragment nt, uint32_t val = 0)
1745
        : Node(internal::NoDupCheck{}, ctx.MsContext(), nt, val) { DuplicateKeyCheck(ctx); }
1746
1747
    // Delete copy constructor and assignment operator, use Clone() instead
1748
    Node(const Node&) = delete;
1749
    Node& operator=(const Node&) = delete;
1750
1751
    // subs is movable, circumventing recursion, so these are permitted.
1752
0
    Node(Node&&) noexcept = default;
Unexecuted instantiation: _ZN10miniscript4NodeI7CPubKeyEC2EOS2_
Unexecuted instantiation: miniscript.cpp:_ZN10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEEC2EOS4_
Unexecuted instantiation: _ZN10miniscript4NodeIjEC2EOS1_
Unexecuted instantiation: _ZN10miniscript4NodeI11XOnlyPubKeyEC2EOS2_
1753
0
    Node& operator=(Node&&) noexcept = default;
Unexecuted instantiation: _ZN10miniscript4NodeI7CPubKeyEaSEOS2_
Unexecuted instantiation: miniscript.cpp:_ZN10miniscript4NodeIN12_GLOBAL__N_119ScriptParserContext3KeyEEaSEOS4_
Unexecuted instantiation: _ZN10miniscript4NodeIjEaSEOS1_
Unexecuted instantiation: _ZN10miniscript4NodeI11XOnlyPubKeyEaSEOS2_
1754
};
1755
1756
namespace internal {
1757
1758
enum class ParseContext {
1759
    /** An expression which may be begin with wrappers followed by a colon. */
1760
    WRAPPED_EXPR,
1761
    /** A miniscript expression which does not begin with wrappers. */
1762
    EXPR,
1763
1764
    /** SWAP wraps the top constructed node with s: */
1765
    SWAP,
1766
    /** ALT wraps the top constructed node with a: */
1767
    ALT,
1768
    /** CHECK wraps the top constructed node with c: */
1769
    CHECK,
1770
    /** DUP_IF wraps the top constructed node with d: */
1771
    DUP_IF,
1772
    /** VERIFY wraps the top constructed node with v: */
1773
    VERIFY,
1774
    /** NON_ZERO wraps the top constructed node with j: */
1775
    NON_ZERO,
1776
    /** ZERO_NOTEQUAL wraps the top constructed node with n: */
1777
    ZERO_NOTEQUAL,
1778
    /** WRAP_U will construct an or_i(X,0) node from the top constructed node. */
1779
    WRAP_U,
1780
    /** WRAP_T will construct an and_v(X,1) node from the top constructed node. */
1781
    WRAP_T,
1782
1783
    /** AND_N will construct an andor(X,Y,0) node from the last two constructed nodes. */
1784
    AND_N,
1785
    /** AND_V will construct an and_v node from the last two constructed nodes. */
1786
    AND_V,
1787
    /** AND_B will construct an and_b node from the last two constructed nodes. */
1788
    AND_B,
1789
    /** ANDOR will construct an andor node from the last three constructed nodes. */
1790
    ANDOR,
1791
    /** OR_B will construct an or_b node from the last two constructed nodes. */
1792
    OR_B,
1793
    /** OR_C will construct an or_c node from the last two constructed nodes. */
1794
    OR_C,
1795
    /** OR_D will construct an or_d node from the last two constructed nodes. */
1796
    OR_D,
1797
    /** OR_I will construct an or_i node from the last two constructed nodes. */
1798
    OR_I,
1799
1800
    /** THRESH will read a wrapped expression, and then look for a COMMA. If
1801
     * no comma follows, it will construct a thresh node from the appropriate
1802
     * number of constructed children. Otherwise, it will recurse with another
1803
     * THRESH. */
1804
    THRESH,
1805
1806
    /** COMMA expects the next element to be ',' and fails if not. */
1807
    COMMA,
1808
    /** CLOSE_BRACKET expects the next element to be ')' and fails if not. */
1809
    CLOSE_BRACKET,
1810
};
1811
1812
int FindNextChar(std::span<const char> in, char m);
1813
1814
/** Parse a key expression fully contained within a fragment with the name given by 'func' */
1815
template<typename Key, typename Ctx>
1816
std::optional<Key> ParseKey(const std::string& func, std::span<const char>& in, const Ctx& ctx)
1817
0
{
1818
0
    std::span<const char> expr = script::Expr(in);
1819
0
    if (!script::Func(func, expr)) return {};
  Branch (1819:9): [True: 0, False: 0]
  Branch (1819:9): [True: 0, False: 0]
1820
0
    return ctx.FromString(expr);
1821
0
}
Unexecuted instantiation: miniscript.cpp:_ZN10miniscript8internal8ParseKeyI7CPubKeyN12_GLOBAL__N_113ParserContextEEESt8optionalIT_ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERSt4spanIKcLm18446744073709551615EERKT0_
Unexecuted instantiation: descriptor.cpp:_ZN10miniscript8internal8ParseKeyIjN12_GLOBAL__N_19KeyParserEEESt8optionalIT_ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERSt4spanIKcLm18446744073709551615EERKT0_
1822
1823
/** Parse a hex string fully contained within a fragment with the name given by 'func' */
1824
inline std::optional<std::vector<unsigned char>> ParseHexStr(const std::string& func, std::span<const char>& in, const size_t expected_size)
1825
0
{
1826
0
    std::span<const char> expr = script::Expr(in);
1827
0
    if (!script::Func(func, expr)) return {};
  Branch (1827:9): [True: 0, False: 0]
1828
0
    std::string val = std::string(expr.begin(), expr.end());
1829
0
    if (!IsHex(val)) return {};
  Branch (1829:9): [True: 0, False: 0]
1830
0
    auto hash = ParseHex(val);
1831
0
    if (hash.size() != expected_size) return {};
  Branch (1831:9): [True: 0, False: 0]
1832
0
    return hash;
1833
0
}
1834
1835
/** BuildBack pops the last two elements off `constructed` and wraps them in the specified Fragment */
1836
template<typename Key>
1837
void BuildBack(const MiniscriptContext script_ctx, Fragment nt, std::vector<Node<Key>>& constructed, const bool reverse = false)
1838
0
{
1839
0
    Node<Key> child{std::move(constructed.back())};
1840
0
    constructed.pop_back();
1841
0
    if (reverse) {
  Branch (1841:9): [True: 0, False: 0]
  Branch (1841:9): [True: 0, False: 0]
  Branch (1841:9): [True: 0, False: 0]
  Branch (1841:9): [True: 0, False: 0]
1842
0
        constructed.back() = Node<Key>{internal::NoDupCheck{}, script_ctx, nt, Vector(std::move(child), std::move(constructed.back()))};
1843
0
    } else {
1844
0
        constructed.back() = Node<Key>{internal::NoDupCheck{}, script_ctx, nt, Vector(std::move(constructed.back()), std::move(child))};
1845
0
    }
1846
0
}
Unexecuted instantiation: _ZN10miniscript8internal9BuildBackI7CPubKeyEEvNS_17MiniscriptContextENS_8FragmentERSt6vectorINS_4NodeIT_EESaIS8_EEb
Unexecuted instantiation: miniscript.cpp:_ZN10miniscript8internal9BuildBackIN12_GLOBAL__N_119ScriptParserContext3KeyEEEvNS_17MiniscriptContextENS_8FragmentERSt6vectorINS_4NodeIT_EESaISA_EEb
Unexecuted instantiation: _ZN10miniscript8internal9BuildBackIjEEvNS_17MiniscriptContextENS_8FragmentERSt6vectorINS_4NodeIT_EESaIS7_EEb
Unexecuted instantiation: _ZN10miniscript8internal9BuildBackI11XOnlyPubKeyEEvNS_17MiniscriptContextENS_8FragmentERSt6vectorINS_4NodeIT_EESaIS8_EEb
1847
1848
/**
1849
 * Parse a miniscript from its textual descriptor form.
1850
 * This does not check whether the script is valid, let alone sane. The caller is expected to use
1851
 * the `IsValidTopLevel()` and `IsSaneTopLevel()` to check for these properties on the node.
1852
 */
1853
template <typename Key, typename Ctx>
1854
inline std::optional<Node<Key>> Parse(std::span<const char> in, const Ctx& ctx)
1855
0
{
1856
0
    using namespace script;
1857
1858
    // Account for the minimum script size for all parsed fragments so far. It "borrows" 1
1859
    // script byte from all leaf nodes, counting it instead whenever a space for a recursive
1860
    // expression is added (through andor, and_*, or_*, thresh). This guarantees that all fragments
1861
    // increment the script_size by at least one, except for:
1862
    // - "0", "1": these leafs are only a single byte, so their subtracted-from increment is 0.
1863
    //   This is not an issue however, as "space" for them has to be created by combinators,
1864
    //   which do increment script_size.
1865
    // - "v:": the v wrapper adds nothing as in some cases it results in no opcode being added
1866
    //   (instead transforming another opcode into its VERIFY form). However, the v: wrapper has
1867
    //   to be interleaved with other fragments to be valid, so this is not a concern.
1868
0
    size_t script_size{1};
1869
0
    size_t max_size{internal::MaxScriptSize(ctx.MsContext())};
1870
1871
    // The two integers are used to hold state for thresh()
1872
0
    std::vector<std::tuple<ParseContext, int64_t, int64_t>> to_parse;
1873
0
    std::vector<Node<Key>> constructed;
1874
1875
0
    to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
1876
1877
    // Parses a multi() or multi_a() from its string representation. Returns false on parsing error.
1878
0
    const auto parse_multi_exp = [&](std::span<const char>& in, const bool is_multi_a) -> bool {
1879
0
        const auto max_keys{is_multi_a ? MAX_PUBKEYS_PER_MULTI_A : MAX_PUBKEYS_PER_MULTISIG};
  Branch (1879:29): [True: 0, False: 0]
  Branch (1879:29): [True: 0, False: 0]
1880
0
        const auto required_ctx{is_multi_a ? MiniscriptContext::TAPSCRIPT : MiniscriptContext::P2WSH};
  Branch (1880:33): [True: 0, False: 0]
  Branch (1880:33): [True: 0, False: 0]
1881
0
        if (ctx.MsContext() != required_ctx) return false;
  Branch (1881:13): [True: 0, False: 0]
  Branch (1881:13): [True: 0, False: 0]
1882
        // Get threshold
1883
0
        int next_comma = FindNextChar(in, ',');
1884
0
        if (next_comma < 1) return false;
  Branch (1884:13): [True: 0, False: 0]
  Branch (1884:13): [True: 0, False: 0]
1885
0
        const auto k_to_integral{ToIntegral<int64_t>(std::string_view(in.data(), next_comma))};
1886
0
        if (!k_to_integral.has_value()) return false;
  Branch (1886:13): [True: 0, False: 0]
  Branch (1886:13): [True: 0, False: 0]
1887
0
        const int64_t k{k_to_integral.value()};
1888
0
        in = in.subspan(next_comma + 1);
1889
        // Get keys. It is compatible for both compressed and x-only keys.
1890
0
        std::vector<Key> keys;
1891
0
        while (next_comma != -1) {
  Branch (1891:16): [True: 0, False: 0]
  Branch (1891:16): [True: 0, False: 0]
1892
0
            next_comma = FindNextChar(in, ',');
1893
0
            int key_length = (next_comma == -1) ? FindNextChar(in, ')') : next_comma;
  Branch (1893:30): [True: 0, False: 0]
  Branch (1893:30): [True: 0, False: 0]
1894
0
            if (key_length < 1) return false;
  Branch (1894:17): [True: 0, False: 0]
  Branch (1894:17): [True: 0, False: 0]
1895
0
            std::span<const char> sp{in.begin(), in.begin() + key_length};
1896
0
            auto key = ctx.FromString(sp);
1897
0
            if (!key) return false;
  Branch (1897:17): [True: 0, False: 0]
  Branch (1897:17): [True: 0, False: 0]
1898
0
            keys.push_back(std::move(*key));
1899
0
            in = in.subspan(key_length + 1);
1900
0
        }
1901
0
        if (keys.size() < 1 || keys.size() > max_keys) return false;
  Branch (1901:13): [True: 0, False: 0]
  Branch (1901:32): [True: 0, False: 0]
  Branch (1901:13): [True: 0, False: 0]
  Branch (1901:32): [True: 0, False: 0]
1902
0
        if (k < 1 || k > (int64_t)keys.size()) return false;
  Branch (1902:13): [True: 0, False: 0]
  Branch (1902:22): [True: 0, False: 0]
  Branch (1902:13): [True: 0, False: 0]
  Branch (1902:22): [True: 0, False: 0]
1903
0
        if (is_multi_a) {
  Branch (1903:13): [True: 0, False: 0]
  Branch (1903:13): [True: 0, False: 0]
1904
            // (push + xonly-key + CHECKSIG[ADD]) * n + k + OP_NUMEQUAL(VERIFY), minus one.
1905
0
            script_size += (1 + 32 + 1) * keys.size() + BuildScript(k).size();
1906
0
            constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::MULTI_A, std::move(keys), k);
1907
0
        } else {
1908
0
            script_size += 2 + (keys.size() > 16) + (k > 16) + 34 * keys.size();
1909
0
            constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::MULTI, std::move(keys), k);
1910
0
        }
1911
0
        return true;
1912
0
    };
Unexecuted instantiation: miniscript.cpp:_ZZN10miniscript8internal5ParseI7CPubKeyN12_GLOBAL__N_113ParserContextEEESt8optionalINS_4NodeIT_EEESt4spanIKcLm18446744073709551615EERKT0_ENKUlRSC_bE_clESG_b
Unexecuted instantiation: descriptor.cpp:_ZZN10miniscript8internal5ParseIjN12_GLOBAL__N_19KeyParserEEESt8optionalINS_4NodeIT_EEESt4spanIKcLm18446744073709551615EERKT0_ENKUlRSB_bE_clESF_b
1913
1914
0
    while (!to_parse.empty()) {
  Branch (1914:12): [True: 0, False: 0]
  Branch (1914:12): [True: 0, False: 0]
1915
0
        if (script_size > max_size) return {};
  Branch (1915:13): [True: 0, False: 0]
  Branch (1915:13): [True: 0, False: 0]
1916
1917
        // Get the current context we are decoding within
1918
0
        auto [cur_context, n, k] = to_parse.back();
1919
0
        to_parse.pop_back();
1920
1921
0
        switch (cur_context) {
  Branch (1921:17): [True: 0, False: 0]
  Branch (1921:17): [True: 0, False: 0]
1922
0
        case ParseContext::WRAPPED_EXPR: {
  Branch (1922:9): [True: 0, False: 0]
  Branch (1922:9): [True: 0, False: 0]
1923
0
            std::optional<size_t> colon_index{};
1924
0
            for (size_t i = 1; i < in.size(); ++i) {
  Branch (1924:32): [True: 0, False: 0]
  Branch (1924:32): [True: 0, False: 0]
1925
0
                if (in[i] == ':') {
  Branch (1925:21): [True: 0, False: 0]
  Branch (1925:21): [True: 0, False: 0]
1926
0
                    colon_index = i;
1927
0
                    break;
1928
0
                }
1929
0
                if (in[i] < 'a' || in[i] > 'z') break;
  Branch (1929:21): [True: 0, False: 0]
  Branch (1929:36): [True: 0, False: 0]
  Branch (1929:21): [True: 0, False: 0]
  Branch (1929:36): [True: 0, False: 0]
1930
0
            }
1931
            // If there is no colon, this loop won't execute
1932
0
            bool last_was_v{false};
1933
0
            for (size_t j = 0; colon_index && j < *colon_index; ++j) {
  Branch (1933:32): [True: 0, False: 0]
  Branch (1933:47): [True: 0, False: 0]
  Branch (1933:32): [True: 0, False: 0]
  Branch (1933:47): [True: 0, False: 0]
1934
0
                if (script_size > max_size) return {};
  Branch (1934:21): [True: 0, False: 0]
  Branch (1934:21): [True: 0, False: 0]
1935
0
                if (in[j] == 'a') {
  Branch (1935:21): [True: 0, False: 0]
  Branch (1935:21): [True: 0, False: 0]
1936
0
                    script_size += 2;
1937
0
                    to_parse.emplace_back(ParseContext::ALT, -1, -1);
1938
0
                } else if (in[j] == 's') {
  Branch (1938:28): [True: 0, False: 0]
  Branch (1938:28): [True: 0, False: 0]
1939
0
                    script_size += 1;
1940
0
                    to_parse.emplace_back(ParseContext::SWAP, -1, -1);
1941
0
                } else if (in[j] == 'c') {
  Branch (1941:28): [True: 0, False: 0]
  Branch (1941:28): [True: 0, False: 0]
1942
0
                    script_size += 1;
1943
0
                    to_parse.emplace_back(ParseContext::CHECK, -1, -1);
1944
0
                } else if (in[j] == 'd') {
  Branch (1944:28): [True: 0, False: 0]
  Branch (1944:28): [True: 0, False: 0]
1945
0
                    script_size += 3;
1946
0
                    to_parse.emplace_back(ParseContext::DUP_IF, -1, -1);
1947
0
                } else if (in[j] == 'j') {
  Branch (1947:28): [True: 0, False: 0]
  Branch (1947:28): [True: 0, False: 0]
1948
0
                    script_size += 4;
1949
0
                    to_parse.emplace_back(ParseContext::NON_ZERO, -1, -1);
1950
0
                } else if (in[j] == 'n') {
  Branch (1950:28): [True: 0, False: 0]
  Branch (1950:28): [True: 0, False: 0]
1951
0
                    script_size += 1;
1952
0
                    to_parse.emplace_back(ParseContext::ZERO_NOTEQUAL, -1, -1);
1953
0
                } else if (in[j] == 'v') {
  Branch (1953:28): [True: 0, False: 0]
  Branch (1953:28): [True: 0, False: 0]
1954
                    // do not permit "...vv...:"; it's not valid, and also doesn't trigger early
1955
                    // failure as script_size isn't incremented.
1956
0
                    if (last_was_v) return {};
  Branch (1956:25): [True: 0, False: 0]
  Branch (1956:25): [True: 0, False: 0]
1957
0
                    to_parse.emplace_back(ParseContext::VERIFY, -1, -1);
1958
0
                } else if (in[j] == 'u') {
  Branch (1958:28): [True: 0, False: 0]
  Branch (1958:28): [True: 0, False: 0]
1959
0
                    script_size += 4;
1960
0
                    to_parse.emplace_back(ParseContext::WRAP_U, -1, -1);
1961
0
                } else if (in[j] == 't') {
  Branch (1961:28): [True: 0, False: 0]
  Branch (1961:28): [True: 0, False: 0]
1962
0
                    script_size += 1;
1963
0
                    to_parse.emplace_back(ParseContext::WRAP_T, -1, -1);
1964
0
                } else if (in[j] == 'l') {
  Branch (1964:28): [True: 0, False: 0]
  Branch (1964:28): [True: 0, False: 0]
1965
                    // The l: wrapper is equivalent to or_i(0,X)
1966
0
                    script_size += 4;
1967
0
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_0);
1968
0
                    to_parse.emplace_back(ParseContext::OR_I, -1, -1);
1969
0
                } else {
1970
0
                    return {};
1971
0
                }
1972
0
                last_was_v = (in[j] == 'v');
1973
0
            }
1974
0
            to_parse.emplace_back(ParseContext::EXPR, -1, -1);
1975
0
            if (colon_index) in = in.subspan(*colon_index + 1);
  Branch (1975:17): [True: 0, False: 0]
  Branch (1975:17): [True: 0, False: 0]
1976
0
            break;
1977
0
        }
1978
0
        case ParseContext::EXPR: {
  Branch (1978:9): [True: 0, False: 0]
  Branch (1978:9): [True: 0, False: 0]
1979
0
            if (Const("0", in)) {
  Branch (1979:17): [True: 0, False: 0]
  Branch (1979:17): [True: 0, False: 0]
1980
0
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_0);
1981
0
            } else if (Const("1", in)) {
  Branch (1981:24): [True: 0, False: 0]
  Branch (1981:24): [True: 0, False: 0]
1982
0
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_1);
1983
0
            } else if (Const("pk(", in, /*skip=*/false)) {
  Branch (1983:24): [True: 0, False: 0]
  Branch (1983:24): [True: 0, False: 0]
1984
0
                std::optional<Key> key = ParseKey<Key, Ctx>("pk", in, ctx);
1985
0
                if (!key) return {};
  Branch (1985:21): [True: 0, False: 0]
  Branch (1985:21): [True: 0, False: 0]
1986
0
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_C, Vector(Node<Key>(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_K, Vector(std::move(*key)))));
1987
0
                script_size += IsTapscript(ctx.MsContext()) ? 33 : 34;
  Branch (1987:32): [True: 0, False: 0]
  Branch (1987:32): [True: 0, False: 0]
1988
0
            } else if (Const("pkh(", in, /*skip=*/false)) {
  Branch (1988:24): [True: 0, False: 0]
  Branch (1988:24): [True: 0, False: 0]
1989
0
                std::optional<Key> key = ParseKey<Key, Ctx>("pkh", in, ctx);
1990
0
                if (!key) return {};
  Branch (1990:21): [True: 0, False: 0]
  Branch (1990:21): [True: 0, False: 0]
1991
0
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_C, Vector(Node<Key>(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_H, Vector(std::move(*key)))));
1992
0
                script_size += 24;
1993
0
            } else if (Const("pk_k(", in, /*skip=*/false)) {
  Branch (1993:24): [True: 0, False: 0]
  Branch (1993:24): [True: 0, False: 0]
1994
0
                std::optional<Key> key = ParseKey<Key, Ctx>("pk_k", in, ctx);
1995
0
                if (!key) return {};
  Branch (1995:21): [True: 0, False: 0]
  Branch (1995:21): [True: 0, False: 0]
1996
0
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_K, Vector(std::move(*key)));
1997
0
                script_size += IsTapscript(ctx.MsContext()) ? 32 : 33;
  Branch (1997:32): [True: 0, False: 0]
  Branch (1997:32): [True: 0, False: 0]
1998
0
            } else if (Const("pk_h(", in, /*skip=*/false)) {
  Branch (1998:24): [True: 0, False: 0]
  Branch (1998:24): [True: 0, False: 0]
1999
0
                std::optional<Key> key = ParseKey<Key, Ctx>("pk_h", in, ctx);
2000
0
                if (!key) return {};
  Branch (2000:21): [True: 0, False: 0]
  Branch (2000:21): [True: 0, False: 0]
2001
0
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_H, Vector(std::move(*key)));
2002
0
                script_size += 23;
2003
0
            } else if (Const("sha256(", in, /*skip=*/false)) {
  Branch (2003:24): [True: 0, False: 0]
  Branch (2003:24): [True: 0, False: 0]
2004
0
                std::optional<std::vector<unsigned char>> hash = ParseHexStr("sha256", in, 32);
2005
0
                if (!hash) return {};
  Branch (2005:21): [True: 0, False: 0]
  Branch (2005:21): [True: 0, False: 0]
2006
0
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::SHA256, std::move(*hash));
2007
0
                script_size += 38;
2008
0
            } else if (Const("ripemd160(", in, /*skip=*/false)) {
  Branch (2008:24): [True: 0, False: 0]
  Branch (2008:24): [True: 0, False: 0]
2009
0
                std::optional<std::vector<unsigned char>> hash = ParseHexStr("ripemd160", in, 20);
2010
0
                if (!hash) return {};
  Branch (2010:21): [True: 0, False: 0]
  Branch (2010:21): [True: 0, False: 0]
2011
0
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::RIPEMD160, std::move(*hash));
2012
0
                script_size += 26;
2013
0
            } else if (Const("hash256(", in, /*skip=*/false)) {
  Branch (2013:24): [True: 0, False: 0]
  Branch (2013:24): [True: 0, False: 0]
2014
0
                std::optional<std::vector<unsigned char>> hash = ParseHexStr("hash256", in, 32);
2015
0
                if (!hash) return {};
  Branch (2015:21): [True: 0, False: 0]
  Branch (2015:21): [True: 0, False: 0]
2016
0
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::HASH256, std::move(*hash));
2017
0
                script_size += 38;
2018
0
            } else if (Const("hash160(", in, /*skip=*/false)) {
  Branch (2018:24): [True: 0, False: 0]
  Branch (2018:24): [True: 0, False: 0]
2019
0
                std::optional<std::vector<unsigned char>> hash = ParseHexStr("hash160", in, 20);
2020
0
                if (!hash) return {};
  Branch (2020:21): [True: 0, False: 0]
  Branch (2020:21): [True: 0, False: 0]
2021
0
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::HASH160, std::move(*hash));
2022
0
                script_size += 26;
2023
0
            } else if (Const("after(", in, /*skip=*/false)) {
  Branch (2023:24): [True: 0, False: 0]
  Branch (2023:24): [True: 0, False: 0]
2024
0
                auto expr = Expr(in);
2025
0
                if (!Func("after", expr)) return {};
  Branch (2025:21): [True: 0, False: 0]
  Branch (2025:21): [True: 0, False: 0]
2026
0
                const auto num{ToIntegral<int64_t>(std::string_view(expr.begin(), expr.end()))};
2027
0
                if (!num.has_value() || *num < 1 || *num >= 0x80000000L) return {};
  Branch (2027:21): [True: 0, False: 0]
  Branch (2027:41): [True: 0, False: 0]
  Branch (2027:53): [True: 0, False: 0]
  Branch (2027:21): [True: 0, False: 0]
  Branch (2027:41): [True: 0, False: 0]
  Branch (2027:53): [True: 0, False: 0]
2028
0
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::AFTER, *num);
2029
0
                script_size += 1 + (*num > 16) + (*num > 0x7f) + (*num > 0x7fff) + (*num > 0x7fffff);
2030
0
            } else if (Const("older(", in, /*skip=*/false)) {
  Branch (2030:24): [True: 0, False: 0]
  Branch (2030:24): [True: 0, False: 0]
2031
0
                auto expr = Expr(in);
2032
0
                if (!Func("older", expr)) return {};
  Branch (2032:21): [True: 0, False: 0]
  Branch (2032:21): [True: 0, False: 0]
2033
0
                const auto num{ToIntegral<int64_t>(std::string_view(expr.begin(), expr.end()))};
2034
0
                if (!num.has_value() || *num < 1 || *num >= 0x80000000L) return {};
  Branch (2034:21): [True: 0, False: 0]
  Branch (2034:41): [True: 0, False: 0]
  Branch (2034:53): [True: 0, False: 0]
  Branch (2034:21): [True: 0, False: 0]
  Branch (2034:41): [True: 0, False: 0]
  Branch (2034:53): [True: 0, False: 0]
2035
0
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::OLDER, *num);
2036
0
                script_size += 1 + (*num > 16) + (*num > 0x7f) + (*num > 0x7fff) + (*num > 0x7fffff);
2037
0
            } else if (Const("multi(", in)) {
  Branch (2037:24): [True: 0, False: 0]
  Branch (2037:24): [True: 0, False: 0]
2038
0
                if (!parse_multi_exp(in, /* is_multi_a = */false)) return {};
  Branch (2038:21): [True: 0, False: 0]
  Branch (2038:21): [True: 0, False: 0]
2039
0
            } else if (Const("multi_a(", in)) {
  Branch (2039:24): [True: 0, False: 0]
  Branch (2039:24): [True: 0, False: 0]
2040
0
                if (!parse_multi_exp(in, /* is_multi_a = */true)) return {};
  Branch (2040:21): [True: 0, False: 0]
  Branch (2040:21): [True: 0, False: 0]
2041
0
            } else if (Const("thresh(", in)) {
  Branch (2041:24): [True: 0, False: 0]
  Branch (2041:24): [True: 0, False: 0]
2042
0
                int next_comma = FindNextChar(in, ',');
2043
0
                if (next_comma < 1) return {};
  Branch (2043:21): [True: 0, False: 0]
  Branch (2043:21): [True: 0, False: 0]
2044
0
                const auto k{ToIntegral<int64_t>(std::string_view(in.data(), next_comma))};
2045
0
                if (!k.has_value() || *k < 1) return {};
  Branch (2045:21): [True: 0, False: 0]
  Branch (2045:39): [True: 0, False: 0]
  Branch (2045:21): [True: 0, False: 0]
  Branch (2045:39): [True: 0, False: 0]
2046
0
                in = in.subspan(next_comma + 1);
2047
                // n = 1 here because we read the first WRAPPED_EXPR before reaching THRESH
2048
0
                to_parse.emplace_back(ParseContext::THRESH, 1, *k);
2049
0
                to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2050
0
                script_size += 2 + (*k > 16) + (*k > 0x7f) + (*k > 0x7fff) + (*k > 0x7fffff);
2051
0
            } else if (Const("andor(", in)) {
  Branch (2051:24): [True: 0, False: 0]
  Branch (2051:24): [True: 0, False: 0]
2052
0
                to_parse.emplace_back(ParseContext::ANDOR, -1, -1);
2053
0
                to_parse.emplace_back(ParseContext::CLOSE_BRACKET, -1, -1);
2054
0
                to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2055
0
                to_parse.emplace_back(ParseContext::COMMA, -1, -1);
2056
0
                to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2057
0
                to_parse.emplace_back(ParseContext::COMMA, -1, -1);
2058
0
                to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2059
0
                script_size += 5;
2060
0
            } else {
2061
0
                if (Const("and_n(", in)) {
  Branch (2061:21): [True: 0, False: 0]
  Branch (2061:21): [True: 0, False: 0]
2062
0
                    to_parse.emplace_back(ParseContext::AND_N, -1, -1);
2063
0
                    script_size += 5;
2064
0
                } else if (Const("and_b(", in)) {
  Branch (2064:28): [True: 0, False: 0]
  Branch (2064:28): [True: 0, False: 0]
2065
0
                    to_parse.emplace_back(ParseContext::AND_B, -1, -1);
2066
0
                    script_size += 2;
2067
0
                } else if (Const("and_v(", in)) {
  Branch (2067:28): [True: 0, False: 0]
  Branch (2067:28): [True: 0, False: 0]
2068
0
                    to_parse.emplace_back(ParseContext::AND_V, -1, -1);
2069
0
                    script_size += 1;
2070
0
                } else if (Const("or_b(", in)) {
  Branch (2070:28): [True: 0, False: 0]
  Branch (2070:28): [True: 0, False: 0]
2071
0
                    to_parse.emplace_back(ParseContext::OR_B, -1, -1);
2072
0
                    script_size += 2;
2073
0
                } else if (Const("or_c(", in)) {
  Branch (2073:28): [True: 0, False: 0]
  Branch (2073:28): [True: 0, False: 0]
2074
0
                    to_parse.emplace_back(ParseContext::OR_C, -1, -1);
2075
0
                    script_size += 3;
2076
0
                } else if (Const("or_d(", in)) {
  Branch (2076:28): [True: 0, False: 0]
  Branch (2076:28): [True: 0, False: 0]
2077
0
                    to_parse.emplace_back(ParseContext::OR_D, -1, -1);
2078
0
                    script_size += 4;
2079
0
                } else if (Const("or_i(", in)) {
  Branch (2079:28): [True: 0, False: 0]
  Branch (2079:28): [True: 0, False: 0]
2080
0
                    to_parse.emplace_back(ParseContext::OR_I, -1, -1);
2081
0
                    script_size += 4;
2082
0
                } else {
2083
0
                    return {};
2084
0
                }
2085
0
                to_parse.emplace_back(ParseContext::CLOSE_BRACKET, -1, -1);
2086
0
                to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2087
0
                to_parse.emplace_back(ParseContext::COMMA, -1, -1);
2088
0
                to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2089
0
            }
2090
0
            break;
2091
0
        }
2092
0
        case ParseContext::ALT: {
  Branch (2092:9): [True: 0, False: 0]
  Branch (2092:9): [True: 0, False: 0]
2093
0
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_A, Vector(std::move(constructed.back()))};
2094
0
            break;
2095
0
        }
2096
0
        case ParseContext::SWAP: {
  Branch (2096:9): [True: 0, False: 0]
  Branch (2096:9): [True: 0, False: 0]
2097
0
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_S, Vector(std::move(constructed.back()))};
2098
0
            break;
2099
0
        }
2100
0
        case ParseContext::CHECK: {
  Branch (2100:9): [True: 0, False: 0]
  Branch (2100:9): [True: 0, False: 0]
2101
0
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_C, Vector(std::move(constructed.back()))};
2102
0
            break;
2103
0
        }
2104
0
        case ParseContext::DUP_IF: {
  Branch (2104:9): [True: 0, False: 0]
  Branch (2104:9): [True: 0, False: 0]
2105
0
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_D, Vector(std::move(constructed.back()))};
2106
0
            break;
2107
0
        }
2108
0
        case ParseContext::NON_ZERO: {
  Branch (2108:9): [True: 0, False: 0]
  Branch (2108:9): [True: 0, False: 0]
2109
0
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_J, Vector(std::move(constructed.back()))};
2110
0
            break;
2111
0
        }
2112
0
        case ParseContext::ZERO_NOTEQUAL: {
  Branch (2112:9): [True: 0, False: 0]
  Branch (2112:9): [True: 0, False: 0]
2113
0
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_N, Vector(std::move(constructed.back()))};
2114
0
            break;
2115
0
        }
2116
0
        case ParseContext::VERIFY: {
  Branch (2116:9): [True: 0, False: 0]
  Branch (2116:9): [True: 0, False: 0]
2117
0
            script_size += (constructed.back().GetType() << "x"_mst);
2118
0
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_V, Vector(std::move(constructed.back()))};
2119
0
            break;
2120
0
        }
2121
0
        case ParseContext::WRAP_U: {
  Branch (2121:9): [True: 0, False: 0]
  Branch (2121:9): [True: 0, False: 0]
2122
0
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::OR_I, Vector(std::move(constructed.back()), Node<Key>{internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_0})};
2123
0
            break;
2124
0
        }
2125
0
        case ParseContext::WRAP_T: {
  Branch (2125:9): [True: 0, False: 0]
  Branch (2125:9): [True: 0, False: 0]
2126
0
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::AND_V, Vector(std::move(constructed.back()), Node<Key>{internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_1})};
2127
0
            break;
2128
0
        }
2129
0
        case ParseContext::AND_B: {
  Branch (2129:9): [True: 0, False: 0]
  Branch (2129:9): [True: 0, False: 0]
2130
0
            BuildBack(ctx.MsContext(), Fragment::AND_B, constructed);
2131
0
            break;
2132
0
        }
2133
0
        case ParseContext::AND_N: {
  Branch (2133:9): [True: 0, False: 0]
  Branch (2133:9): [True: 0, False: 0]
2134
0
            auto mid = std::move(constructed.back());
2135
0
            constructed.pop_back();
2136
0
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::ANDOR, Vector(std::move(constructed.back()), std::move(mid), Node<Key>{internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_0})};
2137
0
            break;
2138
0
        }
2139
0
        case ParseContext::AND_V: {
  Branch (2139:9): [True: 0, False: 0]
  Branch (2139:9): [True: 0, False: 0]
2140
0
            BuildBack(ctx.MsContext(), Fragment::AND_V, constructed);
2141
0
            break;
2142
0
        }
2143
0
        case ParseContext::OR_B: {
  Branch (2143:9): [True: 0, False: 0]
  Branch (2143:9): [True: 0, False: 0]
2144
0
            BuildBack(ctx.MsContext(), Fragment::OR_B, constructed);
2145
0
            break;
2146
0
        }
2147
0
        case ParseContext::OR_C: {
  Branch (2147:9): [True: 0, False: 0]
  Branch (2147:9): [True: 0, False: 0]
2148
0
            BuildBack(ctx.MsContext(), Fragment::OR_C, constructed);
2149
0
            break;
2150
0
        }
2151
0
        case ParseContext::OR_D: {
  Branch (2151:9): [True: 0, False: 0]
  Branch (2151:9): [True: 0, False: 0]
2152
0
            BuildBack(ctx.MsContext(), Fragment::OR_D, constructed);
2153
0
            break;
2154
0
        }
2155
0
        case ParseContext::OR_I: {
  Branch (2155:9): [True: 0, False: 0]
  Branch (2155:9): [True: 0, False: 0]
2156
0
            BuildBack(ctx.MsContext(), Fragment::OR_I, constructed);
2157
0
            break;
2158
0
        }
2159
0
        case ParseContext::ANDOR: {
  Branch (2159:9): [True: 0, False: 0]
  Branch (2159:9): [True: 0, False: 0]
2160
0
            auto right = std::move(constructed.back());
2161
0
            constructed.pop_back();
2162
0
            auto mid = std::move(constructed.back());
2163
0
            constructed.pop_back();
2164
0
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::ANDOR, Vector(std::move(constructed.back()), std::move(mid), std::move(right))};
2165
0
            break;
2166
0
        }
2167
0
        case ParseContext::THRESH: {
  Branch (2167:9): [True: 0, False: 0]
  Branch (2167:9): [True: 0, False: 0]
2168
0
            if (in.size() < 1) return {};
  Branch (2168:17): [True: 0, False: 0]
  Branch (2168:17): [True: 0, False: 0]
2169
0
            if (in[0] == ',') {
  Branch (2169:17): [True: 0, False: 0]
  Branch (2169:17): [True: 0, False: 0]
2170
0
                in = in.subspan(1);
2171
0
                to_parse.emplace_back(ParseContext::THRESH, n+1, k);
2172
0
                to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2173
0
                script_size += 2;
2174
0
            } else if (in[0] == ')') {
  Branch (2174:24): [True: 0, False: 0]
  Branch (2174:24): [True: 0, False: 0]
2175
0
                if (k > n) return {};
  Branch (2175:21): [True: 0, False: 0]
  Branch (2175:21): [True: 0, False: 0]
2176
0
                in = in.subspan(1);
2177
                // Children are constructed in reverse order, so iterate from end to beginning
2178
0
                std::vector<Node<Key>> subs;
2179
0
                for (int i = 0; i < n; ++i) {
  Branch (2179:33): [True: 0, False: 0]
  Branch (2179:33): [True: 0, False: 0]
2180
0
                    subs.push_back(std::move(constructed.back()));
2181
0
                    constructed.pop_back();
2182
0
                }
2183
0
                std::reverse(subs.begin(), subs.end());
2184
0
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::THRESH, std::move(subs), k);
2185
0
            } else {
2186
0
                return {};
2187
0
            }
2188
0
            break;
2189
0
        }
2190
0
        case ParseContext::COMMA: {
  Branch (2190:9): [True: 0, False: 0]
  Branch (2190:9): [True: 0, False: 0]
2191
0
            if (in.size() < 1 || in[0] != ',') return {};
  Branch (2191:17): [True: 0, False: 0]
  Branch (2191:34): [True: 0, False: 0]
  Branch (2191:17): [True: 0, False: 0]
  Branch (2191:34): [True: 0, False: 0]
2192
0
            in = in.subspan(1);
2193
0
            break;
2194
0
        }
2195
0
        case ParseContext::CLOSE_BRACKET: {
  Branch (2195:9): [True: 0, False: 0]
  Branch (2195:9): [True: 0, False: 0]
2196
0
            if (in.size() < 1 || in[0] != ')') return {};
  Branch (2196:17): [True: 0, False: 0]
  Branch (2196:34): [True: 0, False: 0]
  Branch (2196:17): [True: 0, False: 0]
  Branch (2196:34): [True: 0, False: 0]
2197
0
            in = in.subspan(1);
2198
0
            break;
2199
0
        }
2200
0
        }
2201
0
    }
2202
2203
    // Sanity checks on the produced miniscript
2204
0
    assert(constructed.size() >= 1);
  Branch (2204:5): [True: 0, False: 0]
  Branch (2204:5): [True: 0, False: 0]
2205
0
    CHECK_NONFATAL(constructed.size() == 1);
2206
0
    assert(constructed[0].ScriptSize() == script_size);
  Branch (2206:5): [True: 0, False: 0]
  Branch (2206:5): [True: 0, False: 0]
2207
0
    if (in.size() > 0) return {};
  Branch (2207:9): [True: 0, False: 0]
  Branch (2207:9): [True: 0, False: 0]
2208
0
    Node<Key> tl_node{std::move(constructed.front())};
2209
0
    tl_node.DuplicateKeyCheck(ctx);
2210
0
    return tl_node;
2211
0
}
Unexecuted instantiation: miniscript.cpp:_ZN10miniscript8internal5ParseI7CPubKeyN12_GLOBAL__N_113ParserContextEEESt8optionalINS_4NodeIT_EEESt4spanIKcLm18446744073709551615EERKT0_
Unexecuted instantiation: descriptor.cpp:_ZN10miniscript8internal5ParseIjN12_GLOBAL__N_19KeyParserEEESt8optionalINS_4NodeIT_EEESt4spanIKcLm18446744073709551615EERKT0_
2212
2213
/** Decode a script into opcode/push pairs.
2214
 *
2215
 * Construct a vector with one element per opcode in the script, in reverse order.
2216
 * Each element is a pair consisting of the opcode, as well as the data pushed by
2217
 * the opcode (including OP_n), if any. OP_CHECKSIGVERIFY, OP_CHECKMULTISIGVERIFY,
2218
 * OP_NUMEQUALVERIFY and OP_EQUALVERIFY are decomposed into OP_CHECKSIG, OP_CHECKMULTISIG,
2219
 * OP_EQUAL and OP_NUMEQUAL respectively, plus OP_VERIFY.
2220
 */
2221
std::optional<std::vector<Opcode>> DecomposeScript(const CScript& script);
2222
2223
/** Determine whether the passed pair (created by DecomposeScript) is pushing a number. */
2224
std::optional<int64_t> ParseScriptNumber(const Opcode& in);
2225
2226
enum class DecodeContext {
2227
    /** A single expression of type B, K, or V. Specifically, this can't be an
2228
     * and_v or an expression of type W (a: and s: wrappers). */
2229
    SINGLE_BKV_EXPR,
2230
    /** Potentially multiple SINGLE_BKV_EXPRs as children of (potentially multiple)
2231
     * and_v expressions. Syntactic sugar for MAYBE_AND_V + SINGLE_BKV_EXPR. */
2232
    BKV_EXPR,
2233
    /** An expression of type W (a: or s: wrappers). */
2234
    W_EXPR,
2235
2236
    /** SWAP expects the next element to be OP_SWAP (inside a W-type expression that
2237
     * didn't end with FROMALTSTACK), and wraps the top of the constructed stack
2238
     * with s: */
2239
    SWAP,
2240
    /** ALT expects the next element to be TOALTSTACK (we must have already read a
2241
     * FROMALTSTACK earlier), and wraps the top of the constructed stack with a: */
2242
    ALT,
2243
    /** CHECK wraps the top constructed node with c: */
2244
    CHECK,
2245
    /** DUP_IF wraps the top constructed node with d: */
2246
    DUP_IF,
2247
    /** VERIFY wraps the top constructed node with v: */
2248
    VERIFY,
2249
    /** NON_ZERO wraps the top constructed node with j: */
2250
    NON_ZERO,
2251
    /** ZERO_NOTEQUAL wraps the top constructed node with n: */
2252
    ZERO_NOTEQUAL,
2253
2254
    /** MAYBE_AND_V will check if the next part of the script could be a valid
2255
     * miniscript sub-expression, and if so it will push AND_V and SINGLE_BKV_EXPR
2256
     * to decode it and construct the and_v node. This is recursive, to deal with
2257
     * multiple and_v nodes inside each other. */
2258
    MAYBE_AND_V,
2259
    /** AND_V will construct an and_v node from the last two constructed nodes. */
2260
    AND_V,
2261
    /** AND_B will construct an and_b node from the last two constructed nodes. */
2262
    AND_B,
2263
    /** ANDOR will construct an andor node from the last three constructed nodes. */
2264
    ANDOR,
2265
    /** OR_B will construct an or_b node from the last two constructed nodes. */
2266
    OR_B,
2267
    /** OR_C will construct an or_c node from the last two constructed nodes. */
2268
    OR_C,
2269
    /** OR_D will construct an or_d node from the last two constructed nodes. */
2270
    OR_D,
2271
2272
    /** In a thresh expression, all sub-expressions other than the first are W-type,
2273
     * and end in OP_ADD. THRESH_W will check for this OP_ADD and either push a W_EXPR
2274
     * or a SINGLE_BKV_EXPR and jump to THRESH_E accordingly. */
2275
    THRESH_W,
2276
    /** THRESH_E constructs a thresh node from the appropriate number of constructed
2277
     * children. */
2278
    THRESH_E,
2279
2280
    /** ENDIF signals that we are inside some sort of OP_IF structure, which could be
2281
     * or_d, or_c, or_i, andor, d:, or j: wrapper, depending on what follows. We read
2282
     * a BKV_EXPR and then deal with the next opcode case-by-case. */
2283
    ENDIF,
2284
    /** If, inside an ENDIF context, we find an OP_NOTIF before finding an OP_ELSE,
2285
     * we could either be in an or_d or an or_c node. We then check for IFDUP to
2286
     * distinguish these cases. */
2287
    ENDIF_NOTIF,
2288
    /** If, inside an ENDIF context, we find an OP_ELSE, then we could be in either an
2289
     * or_i or an andor node. Read the next BKV_EXPR and find either an OP_IF or an
2290
     * OP_NOTIF. */
2291
    ENDIF_ELSE,
2292
};
2293
2294
//! Parse a miniscript from a bitcoin script
2295
template <typename Key, typename Ctx, typename I>
2296
inline std::optional<Node<Key>> DecodeScript(I& in, I last, const Ctx& ctx)
2297
0
{
2298
    // The two integers are used to hold state for thresh()
2299
0
    std::vector<std::tuple<DecodeContext, int64_t, int64_t>> to_parse;
2300
0
    std::vector<Node<Key>> constructed;
2301
2302
    // This is the top level, so we assume the type is B
2303
    // (in particular, disallowing top level W expressions)
2304
0
    to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2305
2306
0
    while (!to_parse.empty()) {
  Branch (2306:12): [True: 0, False: 0]
  Branch (2306:12): [True: 0, False: 0]
  Branch (2306:12): [True: 0, False: 0]
  Branch (2306:12): [True: 0, False: 0]
  Branch (2306:12): [True: 0, False: 0]
2307
        // Exit early if the Miniscript is not going to be valid.
2308
0
        if (!constructed.empty() && !constructed.back().IsValid()) return {};
  Branch (2308:13): [True: 0, False: 0]
  Branch (2308:37): [True: 0, False: 0]
  Branch (2308:13): [True: 0, False: 0]
  Branch (2308:37): [True: 0, False: 0]
  Branch (2308:13): [True: 0, False: 0]
  Branch (2308:37): [True: 0, False: 0]
  Branch (2308:13): [True: 0, False: 0]
  Branch (2308:37): [True: 0, False: 0]
  Branch (2308:13): [True: 0, False: 0]
  Branch (2308:37): [True: 0, False: 0]
2309
2310
        // Get the current context we are decoding within
2311
0
        auto [cur_context, n, k] = to_parse.back();
2312
0
        to_parse.pop_back();
2313
2314
0
        switch(cur_context) {
  Branch (2314:16): [True: 0, False: 0]
  Branch (2314:16): [True: 0, False: 0]
  Branch (2314:16): [True: 0, False: 0]
  Branch (2314:16): [True: 0, False: 0]
  Branch (2314:16): [True: 0, False: 0]
2315
0
        case DecodeContext::SINGLE_BKV_EXPR: {
  Branch (2315:9): [True: 0, False: 0]
  Branch (2315:9): [True: 0, False: 0]
  Branch (2315:9): [True: 0, False: 0]
  Branch (2315:9): [True: 0, False: 0]
  Branch (2315:9): [True: 0, False: 0]
2316
0
            if (in >= last) return {};
  Branch (2316:17): [True: 0, False: 0]
  Branch (2316:17): [True: 0, False: 0]
  Branch (2316:17): [True: 0, False: 0]
  Branch (2316:17): [True: 0, False: 0]
  Branch (2316:17): [True: 0, False: 0]
2317
2318
            // Constants
2319
0
            if (in[0].first == OP_1) {
  Branch (2319:17): [True: 0, False: 0]
  Branch (2319:17): [True: 0, False: 0]
  Branch (2319:17): [True: 0, False: 0]
  Branch (2319:17): [True: 0, False: 0]
  Branch (2319:17): [True: 0, False: 0]
2320
0
                ++in;
2321
0
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_1);
2322
0
                break;
2323
0
            }
2324
0
            if (in[0].first == OP_0) {
  Branch (2324:17): [True: 0, False: 0]
  Branch (2324:17): [True: 0, False: 0]
  Branch (2324:17): [True: 0, False: 0]
  Branch (2324:17): [True: 0, False: 0]
  Branch (2324:17): [True: 0, False: 0]
2325
0
                ++in;
2326
0
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_0);
2327
0
                break;
2328
0
            }
2329
            // Public keys
2330
0
            if (in[0].second.size() == 33 || in[0].second.size() == 32) {
  Branch (2330:17): [True: 0, False: 0]
  Branch (2330:46): [True: 0, False: 0]
  Branch (2330:17): [True: 0, False: 0]
  Branch (2330:46): [True: 0, False: 0]
  Branch (2330:17): [True: 0, False: 0]
  Branch (2330:46): [True: 0, False: 0]
  Branch (2330:17): [True: 0, False: 0]
  Branch (2330:46): [True: 0, False: 0]
  Branch (2330:17): [True: 0, False: 0]
  Branch (2330:46): [True: 0, False: 0]
2331
0
                auto key = ctx.FromPKBytes(in[0].second.begin(), in[0].second.end());
2332
0
                if (!key) return {};
  Branch (2332:21): [True: 0, False: 0]
  Branch (2332:21): [True: 0, False: 0]
  Branch (2332:21): [True: 0, False: 0]
  Branch (2332:21): [True: 0, False: 0]
  Branch (2332:21): [True: 0, False: 0]
2333
0
                ++in;
2334
0
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_K, Vector(std::move(*key)));
2335
0
                break;
2336
0
            }
2337
0
            if (last - in >= 5 && in[0].first == OP_VERIFY && in[1].first == OP_EQUAL && in[3].first == OP_HASH160 && in[4].first == OP_DUP && in[2].second.size() == 20) {
  Branch (2337:17): [True: 0, False: 0]
  Branch (2337:35): [True: 0, False: 0]
  Branch (2337:63): [True: 0, False: 0]
  Branch (2337:90): [True: 0, False: 0]
  Branch (2337:119): [True: 0, False: 0]
  Branch (2337:144): [True: 0, False: 0]
  Branch (2337:17): [True: 0, False: 0]
  Branch (2337:35): [True: 0, False: 0]
  Branch (2337:63): [True: 0, False: 0]
  Branch (2337:90): [True: 0, False: 0]
  Branch (2337:119): [True: 0, False: 0]
  Branch (2337:144): [True: 0, False: 0]
  Branch (2337:17): [True: 0, False: 0]
  Branch (2337:35): [True: 0, False: 0]
  Branch (2337:63): [True: 0, False: 0]
  Branch (2337:90): [True: 0, False: 0]
  Branch (2337:119): [True: 0, False: 0]
  Branch (2337:144): [True: 0, False: 0]
  Branch (2337:17): [True: 0, False: 0]
  Branch (2337:35): [True: 0, False: 0]
  Branch (2337:63): [True: 0, False: 0]
  Branch (2337:90): [True: 0, False: 0]
  Branch (2337:119): [True: 0, False: 0]
  Branch (2337:144): [True: 0, False: 0]
  Branch (2337:17): [True: 0, False: 0]
  Branch (2337:35): [True: 0, False: 0]
  Branch (2337:63): [True: 0, False: 0]
  Branch (2337:90): [True: 0, False: 0]
  Branch (2337:119): [True: 0, False: 0]
  Branch (2337:144): [True: 0, False: 0]
2338
0
                auto key = ctx.FromPKHBytes(in[2].second.begin(), in[2].second.end());
2339
0
                if (!key) return {};
  Branch (2339:21): [True: 0, False: 0]
  Branch (2339:21): [True: 0, False: 0]
  Branch (2339:21): [True: 0, False: 0]
  Branch (2339:21): [True: 0, False: 0]
  Branch (2339:21): [True: 0, False: 0]
2340
0
                in += 5;
2341
0
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_H, Vector(std::move(*key)));
2342
0
                break;
2343
0
            }
2344
            // Time locks
2345
0
            std::optional<int64_t> num;
2346
0
            if (last - in >= 2 && in[0].first == OP_CHECKSEQUENCEVERIFY && (num = ParseScriptNumber(in[1]))) {
  Branch (2346:17): [True: 0, False: 0]
  Branch (2346:17): [True: 0, False: 0]
  Branch (2346:35): [True: 0, False: 0]
  Branch (2346:76): [True: 0, False: 0]
  Branch (2346:17): [True: 0, False: 0]
  Branch (2346:17): [True: 0, False: 0]
  Branch (2346:35): [True: 0, False: 0]
  Branch (2346:76): [True: 0, False: 0]
  Branch (2346:17): [True: 0, False: 0]
  Branch (2346:17): [True: 0, False: 0]
  Branch (2346:35): [True: 0, False: 0]
  Branch (2346:76): [True: 0, False: 0]
  Branch (2346:17): [True: 0, False: 0]
  Branch (2346:17): [True: 0, False: 0]
  Branch (2346:35): [True: 0, False: 0]
  Branch (2346:76): [True: 0, False: 0]
  Branch (2346:17): [True: 0, False: 0]
  Branch (2346:17): [True: 0, False: 0]
  Branch (2346:35): [True: 0, False: 0]
  Branch (2346:76): [True: 0, False: 0]
2347
0
                in += 2;
2348
0
                if (*num < 1 || *num > 0x7FFFFFFFL) return {};
  Branch (2348:21): [True: 0, False: 0]
  Branch (2348:33): [True: 0, False: 0]
  Branch (2348:21): [True: 0, False: 0]
  Branch (2348:33): [True: 0, False: 0]
  Branch (2348:21): [True: 0, False: 0]
  Branch (2348:33): [True: 0, False: 0]
  Branch (2348:21): [True: 0, False: 0]
  Branch (2348:33): [True: 0, False: 0]
  Branch (2348:21): [True: 0, False: 0]
  Branch (2348:33): [True: 0, False: 0]
2349
0
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::OLDER, *num);
2350
0
                break;
2351
0
            }
2352
0
            if (last - in >= 2 && in[0].first == OP_CHECKLOCKTIMEVERIFY && (num = ParseScriptNumber(in[1]))) {
  Branch (2352:17): [True: 0, False: 0]
  Branch (2352:17): [True: 0, False: 0]
  Branch (2352:35): [True: 0, False: 0]
  Branch (2352:76): [True: 0, False: 0]
  Branch (2352:17): [True: 0, False: 0]
  Branch (2352:17): [True: 0, False: 0]
  Branch (2352:35): [True: 0, False: 0]
  Branch (2352:76): [True: 0, False: 0]
  Branch (2352:17): [True: 0, False: 0]
  Branch (2352:17): [True: 0, False: 0]
  Branch (2352:35): [True: 0, False: 0]
  Branch (2352:76): [True: 0, False: 0]
  Branch (2352:17): [True: 0, False: 0]
  Branch (2352:17): [True: 0, False: 0]
  Branch (2352:35): [True: 0, False: 0]
  Branch (2352:76): [True: 0, False: 0]
  Branch (2352:17): [True: 0, False: 0]
  Branch (2352:17): [True: 0, False: 0]
  Branch (2352:35): [True: 0, False: 0]
  Branch (2352:76): [True: 0, False: 0]
2353
0
                in += 2;
2354
0
                if (num < 1 || num > 0x7FFFFFFFL) return {};
  Branch (2354:21): [True: 0, False: 0]
  Branch (2354:21): [True: 0, False: 0]
  Branch (2354:32): [True: 0, False: 0]
  Branch (2354:21): [True: 0, False: 0]
  Branch (2354:21): [True: 0, False: 0]
  Branch (2354:32): [True: 0, False: 0]
  Branch (2354:21): [True: 0, False: 0]
  Branch (2354:21): [True: 0, False: 0]
  Branch (2354:32): [True: 0, False: 0]
  Branch (2354:21): [True: 0, False: 0]
  Branch (2354:21): [True: 0, False: 0]
  Branch (2354:32): [True: 0, False: 0]
  Branch (2354:21): [True: 0, False: 0]
  Branch (2354:21): [True: 0, False: 0]
  Branch (2354:32): [True: 0, False: 0]
2355
0
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::AFTER, *num);
2356
0
                break;
2357
0
            }
2358
            // Hashes
2359
0
            if (last - in >= 7 && in[0].first == OP_EQUAL && in[3].first == OP_VERIFY && in[4].first == OP_EQUAL && (num = ParseScriptNumber(in[5])) && num == 32 && in[6].first == OP_SIZE) {
  Branch (2359:17): [True: 0, False: 0]
  Branch (2359:17): [True: 0, False: 0]
  Branch (2359:35): [True: 0, False: 0]
  Branch (2359:62): [True: 0, False: 0]
  Branch (2359:90): [True: 0, False: 0]
  Branch (2359:117): [True: 0, False: 0]
  Branch (2359:153): [True: 0, False: 0]
  Branch (2359:166): [True: 0, False: 0]
  Branch (2359:17): [True: 0, False: 0]
  Branch (2359:17): [True: 0, False: 0]
  Branch (2359:35): [True: 0, False: 0]
  Branch (2359:62): [True: 0, False: 0]
  Branch (2359:90): [True: 0, False: 0]
  Branch (2359:117): [True: 0, False: 0]
  Branch (2359:153): [True: 0, False: 0]
  Branch (2359:166): [True: 0, False: 0]
  Branch (2359:17): [True: 0, False: 0]
  Branch (2359:17): [True: 0, False: 0]
  Branch (2359:35): [True: 0, False: 0]
  Branch (2359:62): [True: 0, False: 0]
  Branch (2359:90): [True: 0, False: 0]
  Branch (2359:117): [True: 0, False: 0]
  Branch (2359:153): [True: 0, False: 0]
  Branch (2359:166): [True: 0, False: 0]
  Branch (2359:17): [True: 0, False: 0]
  Branch (2359:17): [True: 0, False: 0]
  Branch (2359:35): [True: 0, False: 0]
  Branch (2359:62): [True: 0, False: 0]
  Branch (2359:90): [True: 0, False: 0]
  Branch (2359:117): [True: 0, False: 0]
  Branch (2359:153): [True: 0, False: 0]
  Branch (2359:166): [True: 0, False: 0]
  Branch (2359:17): [True: 0, False: 0]
  Branch (2359:17): [True: 0, False: 0]
  Branch (2359:35): [True: 0, False: 0]
  Branch (2359:62): [True: 0, False: 0]
  Branch (2359:90): [True: 0, False: 0]
  Branch (2359:117): [True: 0, False: 0]
  Branch (2359:153): [True: 0, False: 0]
  Branch (2359:166): [True: 0, False: 0]
2360
0
                if (in[2].first == OP_SHA256 && in[1].second.size() == 32) {
  Branch (2360:21): [True: 0, False: 0]
  Branch (2360:49): [True: 0, False: 0]
  Branch (2360:21): [True: 0, False: 0]
  Branch (2360:49): [True: 0, False: 0]
  Branch (2360:21): [True: 0, False: 0]
  Branch (2360:49): [True: 0, False: 0]
  Branch (2360:21): [True: 0, False: 0]
  Branch (2360:49): [True: 0, False: 0]
  Branch (2360:21): [True: 0, False: 0]
  Branch (2360:49): [True: 0, False: 0]
2361
0
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::SHA256, in[1].second);
2362
0
                    in += 7;
2363
0
                    break;
2364
0
                } else if (in[2].first == OP_RIPEMD160 && in[1].second.size() == 20) {
  Branch (2364:28): [True: 0, False: 0]
  Branch (2364:59): [True: 0, False: 0]
  Branch (2364:28): [True: 0, False: 0]
  Branch (2364:59): [True: 0, False: 0]
  Branch (2364:28): [True: 0, False: 0]
  Branch (2364:59): [True: 0, False: 0]
  Branch (2364:28): [True: 0, False: 0]
  Branch (2364:59): [True: 0, False: 0]
  Branch (2364:28): [True: 0, False: 0]
  Branch (2364:59): [True: 0, False: 0]
2365
0
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::RIPEMD160, in[1].second);
2366
0
                    in += 7;
2367
0
                    break;
2368
0
                } else if (in[2].first == OP_HASH256 && in[1].second.size() == 32) {
  Branch (2368:28): [True: 0, False: 0]
  Branch (2368:57): [True: 0, False: 0]
  Branch (2368:28): [True: 0, False: 0]
  Branch (2368:57): [True: 0, False: 0]
  Branch (2368:28): [True: 0, False: 0]
  Branch (2368:57): [True: 0, False: 0]
  Branch (2368:28): [True: 0, False: 0]
  Branch (2368:57): [True: 0, False: 0]
  Branch (2368:28): [True: 0, False: 0]
  Branch (2368:57): [True: 0, False: 0]
2369
0
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::HASH256, in[1].second);
2370
0
                    in += 7;
2371
0
                    break;
2372
0
                } else if (in[2].first == OP_HASH160 && in[1].second.size() == 20) {
  Branch (2372:28): [True: 0, False: 0]
  Branch (2372:57): [True: 0, False: 0]
  Branch (2372:28): [True: 0, False: 0]
  Branch (2372:57): [True: 0, False: 0]
  Branch (2372:28): [True: 0, False: 0]
  Branch (2372:57): [True: 0, False: 0]
  Branch (2372:28): [True: 0, False: 0]
  Branch (2372:57): [True: 0, False: 0]
  Branch (2372:28): [True: 0, False: 0]
  Branch (2372:57): [True: 0, False: 0]
2373
0
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::HASH160, in[1].second);
2374
0
                    in += 7;
2375
0
                    break;
2376
0
                }
2377
0
            }
2378
            // Multi
2379
0
            if (last - in >= 3 && in[0].first == OP_CHECKMULTISIG) {
  Branch (2379:17): [True: 0, False: 0]
  Branch (2379:35): [True: 0, False: 0]
  Branch (2379:17): [True: 0, False: 0]
  Branch (2379:35): [True: 0, False: 0]
  Branch (2379:17): [True: 0, False: 0]
  Branch (2379:35): [True: 0, False: 0]
  Branch (2379:17): [True: 0, False: 0]
  Branch (2379:35): [True: 0, False: 0]
  Branch (2379:17): [True: 0, False: 0]
  Branch (2379:35): [True: 0, False: 0]
2380
0
                if (IsTapscript(ctx.MsContext())) return {};
  Branch (2380:21): [True: 0, False: 0]
  Branch (2380:21): [True: 0, False: 0]
  Branch (2380:21): [True: 0, False: 0]
  Branch (2380:21): [True: 0, False: 0]
  Branch (2380:21): [True: 0, False: 0]
2381
0
                std::vector<Key> keys;
2382
0
                const auto n = ParseScriptNumber(in[1]);
2383
0
                if (!n || last - in < 3 + *n) return {};
  Branch (2383:21): [True: 0, False: 0]
  Branch (2383:27): [True: 0, False: 0]
  Branch (2383:21): [True: 0, False: 0]
  Branch (2383:27): [True: 0, False: 0]
  Branch (2383:21): [True: 0, False: 0]
  Branch (2383:27): [True: 0, False: 0]
  Branch (2383:21): [True: 0, False: 0]
  Branch (2383:27): [True: 0, False: 0]
  Branch (2383:21): [True: 0, False: 0]
  Branch (2383:27): [True: 0, False: 0]
2384
0
                if (*n < 1 || *n > 20) return {};
  Branch (2384:21): [True: 0, False: 0]
  Branch (2384:31): [True: 0, False: 0]
  Branch (2384:21): [True: 0, False: 0]
  Branch (2384:31): [True: 0, False: 0]
  Branch (2384:21): [True: 0, False: 0]
  Branch (2384:31): [True: 0, False: 0]
  Branch (2384:21): [True: 0, False: 0]
  Branch (2384:31): [True: 0, False: 0]
  Branch (2384:21): [True: 0, False: 0]
  Branch (2384:31): [True: 0, False: 0]
2385
0
                for (int i = 0; i < *n; ++i) {
  Branch (2385:33): [True: 0, False: 0]
  Branch (2385:33): [True: 0, False: 0]
  Branch (2385:33): [True: 0, False: 0]
  Branch (2385:33): [True: 0, False: 0]
  Branch (2385:33): [True: 0, False: 0]
2386
0
                    if (in[2 + i].second.size() != 33) return {};
  Branch (2386:25): [True: 0, False: 0]
  Branch (2386:25): [True: 0, False: 0]
  Branch (2386:25): [True: 0, False: 0]
  Branch (2386:25): [True: 0, False: 0]
  Branch (2386:25): [True: 0, False: 0]
2387
0
                    auto key = ctx.FromPKBytes(in[2 + i].second.begin(), in[2 + i].second.end());
2388
0
                    if (!key) return {};
  Branch (2388:25): [True: 0, False: 0]
  Branch (2388:25): [True: 0, False: 0]
  Branch (2388:25): [True: 0, False: 0]
  Branch (2388:25): [True: 0, False: 0]
  Branch (2388:25): [True: 0, False: 0]
2389
0
                    keys.push_back(std::move(*key));
2390
0
                }
2391
0
                const auto k = ParseScriptNumber(in[2 + *n]);
2392
0
                if (!k || *k < 1 || *k > *n) return {};
  Branch (2392:21): [True: 0, False: 0]
  Branch (2392:27): [True: 0, False: 0]
  Branch (2392:37): [True: 0, False: 0]
  Branch (2392:21): [True: 0, False: 0]
  Branch (2392:27): [True: 0, False: 0]
  Branch (2392:37): [True: 0, False: 0]
  Branch (2392:21): [True: 0, False: 0]
  Branch (2392:27): [True: 0, False: 0]
  Branch (2392:37): [True: 0, False: 0]
  Branch (2392:21): [True: 0, False: 0]
  Branch (2392:27): [True: 0, False: 0]
  Branch (2392:37): [True: 0, False: 0]
  Branch (2392:21): [True: 0, False: 0]
  Branch (2392:27): [True: 0, False: 0]
  Branch (2392:37): [True: 0, False: 0]
2393
0
                in += 3 + *n;
2394
0
                std::reverse(keys.begin(), keys.end());
2395
0
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::MULTI, std::move(keys), *k);
2396
0
                break;
2397
0
            }
2398
            // Tapscript's equivalent of multi
2399
0
            if (last - in >= 4 && in[0].first == OP_NUMEQUAL) {
  Branch (2399:17): [True: 0, False: 0]
  Branch (2399:35): [True: 0, False: 0]
  Branch (2399:17): [True: 0, False: 0]
  Branch (2399:35): [True: 0, False: 0]
  Branch (2399:17): [True: 0, False: 0]
  Branch (2399:35): [True: 0, False: 0]
  Branch (2399:17): [True: 0, False: 0]
  Branch (2399:35): [True: 0, False: 0]
  Branch (2399:17): [True: 0, False: 0]
  Branch (2399:35): [True: 0, False: 0]
2400
0
                if (!IsTapscript(ctx.MsContext())) return {};
  Branch (2400:21): [True: 0, False: 0]
  Branch (2400:21): [True: 0, False: 0]
  Branch (2400:21): [True: 0, False: 0]
  Branch (2400:21): [True: 0, False: 0]
  Branch (2400:21): [True: 0, False: 0]
2401
                // The necessary threshold of signatures.
2402
0
                const auto k = ParseScriptNumber(in[1]);
2403
0
                if (!k) return {};
  Branch (2403:21): [True: 0, False: 0]
  Branch (2403:21): [True: 0, False: 0]
  Branch (2403:21): [True: 0, False: 0]
  Branch (2403:21): [True: 0, False: 0]
  Branch (2403:21): [True: 0, False: 0]
2404
0
                if (*k < 1 || *k > MAX_PUBKEYS_PER_MULTI_A) return {};
  Branch (2404:21): [True: 0, False: 0]
  Branch (2404:31): [True: 0, False: 0]
  Branch (2404:21): [True: 0, False: 0]
  Branch (2404:31): [True: 0, False: 0]
  Branch (2404:21): [True: 0, False: 0]
  Branch (2404:31): [True: 0, False: 0]
  Branch (2404:21): [True: 0, False: 0]
  Branch (2404:31): [True: 0, False: 0]
  Branch (2404:21): [True: 0, False: 0]
  Branch (2404:31): [True: 0, False: 0]
2405
0
                if (last - in < 2 + *k * 2) return {};
  Branch (2405:21): [True: 0, False: 0]
  Branch (2405:21): [True: 0, False: 0]
  Branch (2405:21): [True: 0, False: 0]
  Branch (2405:21): [True: 0, False: 0]
  Branch (2405:21): [True: 0, False: 0]
2406
0
                std::vector<Key> keys;
2407
0
                keys.reserve(*k);
2408
                // Walk through the expected (pubkey, CHECKSIG[ADD]) pairs.
2409
0
                for (int pos = 2;; pos += 2) {
2410
0
                    if (last - in < pos + 2) return {};
  Branch (2410:25): [True: 0, False: 0]
  Branch (2410:25): [True: 0, False: 0]
  Branch (2410:25): [True: 0, False: 0]
  Branch (2410:25): [True: 0, False: 0]
  Branch (2410:25): [True: 0, False: 0]
2411
                    // Make sure it's indeed an x-only pubkey and a CHECKSIG[ADD], then parse the key.
2412
0
                    if (in[pos].first != OP_CHECKSIGADD && in[pos].first != OP_CHECKSIG) return {};
  Branch (2412:25): [True: 0, False: 0]
  Branch (2412:60): [True: 0, False: 0]
  Branch (2412:25): [True: 0, False: 0]
  Branch (2412:60): [True: 0, False: 0]
  Branch (2412:25): [True: 0, False: 0]
  Branch (2412:60): [True: 0, False: 0]
  Branch (2412:25): [True: 0, False: 0]
  Branch (2412:60): [True: 0, False: 0]
  Branch (2412:25): [True: 0, False: 0]
  Branch (2412:60): [True: 0, False: 0]
2413
0
                    if (in[pos + 1].second.size() != 32) return {};
  Branch (2413:25): [True: 0, False: 0]
  Branch (2413:25): [True: 0, False: 0]
  Branch (2413:25): [True: 0, False: 0]
  Branch (2413:25): [True: 0, False: 0]
  Branch (2413:25): [True: 0, False: 0]
2414
0
                    auto key = ctx.FromPKBytes(in[pos + 1].second.begin(), in[pos + 1].second.end());
2415
0
                    if (!key) return {};
  Branch (2415:25): [True: 0, False: 0]
  Branch (2415:25): [True: 0, False: 0]
  Branch (2415:25): [True: 0, False: 0]
  Branch (2415:25): [True: 0, False: 0]
  Branch (2415:25): [True: 0, False: 0]
2416
0
                    keys.push_back(std::move(*key));
2417
                    // Make sure early we don't parse an arbitrary large expression.
2418
0
                    if (keys.size() > MAX_PUBKEYS_PER_MULTI_A) return {};
  Branch (2418:25): [True: 0, False: 0]
  Branch (2418:25): [True: 0, False: 0]
  Branch (2418:25): [True: 0, False: 0]
  Branch (2418:25): [True: 0, False: 0]
  Branch (2418:25): [True: 0, False: 0]
2419
                    // OP_CHECKSIG means it was the last one to parse.
2420
0
                    if (in[pos].first == OP_CHECKSIG) break;
  Branch (2420:25): [True: 0, False: 0]
  Branch (2420:25): [True: 0, False: 0]
  Branch (2420:25): [True: 0, False: 0]
  Branch (2420:25): [True: 0, False: 0]
  Branch (2420:25): [True: 0, False: 0]
2421
0
                }
2422
0
                if (keys.size() < (size_t)*k) return {};
  Branch (2422:21): [True: 0, False: 0]
  Branch (2422:21): [True: 0, False: 0]
  Branch (2422:21): [True: 0, False: 0]
  Branch (2422:21): [True: 0, False: 0]
  Branch (2422:21): [True: 0, False: 0]
2423
0
                in += 2 + keys.size() * 2;
2424
0
                std::reverse(keys.begin(), keys.end());
2425
0
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::MULTI_A, std::move(keys), *k);
2426
0
                break;
2427
0
            }
2428
            /** In the following wrappers, we only need to push SINGLE_BKV_EXPR rather
2429
             * than BKV_EXPR, because and_v commutes with these wrappers. For example,
2430
             * c:and_v(X,Y) produces the same script as and_v(X,c:Y). */
2431
            // c: wrapper
2432
0
            if (in[0].first == OP_CHECKSIG) {
  Branch (2432:17): [True: 0, False: 0]
  Branch (2432:17): [True: 0, False: 0]
  Branch (2432:17): [True: 0, False: 0]
  Branch (2432:17): [True: 0, False: 0]
  Branch (2432:17): [True: 0, False: 0]
2433
0
                ++in;
2434
0
                to_parse.emplace_back(DecodeContext::CHECK, -1, -1);
2435
0
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2436
0
                break;
2437
0
            }
2438
            // v: wrapper
2439
0
            if (in[0].first == OP_VERIFY) {
  Branch (2439:17): [True: 0, False: 0]
  Branch (2439:17): [True: 0, False: 0]
  Branch (2439:17): [True: 0, False: 0]
  Branch (2439:17): [True: 0, False: 0]
  Branch (2439:17): [True: 0, False: 0]
2440
0
                ++in;
2441
0
                to_parse.emplace_back(DecodeContext::VERIFY, -1, -1);
2442
0
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2443
0
                break;
2444
0
            }
2445
            // n: wrapper
2446
0
            if (in[0].first == OP_0NOTEQUAL) {
  Branch (2446:17): [True: 0, False: 0]
  Branch (2446:17): [True: 0, False: 0]
  Branch (2446:17): [True: 0, False: 0]
  Branch (2446:17): [True: 0, False: 0]
  Branch (2446:17): [True: 0, False: 0]
2447
0
                ++in;
2448
0
                to_parse.emplace_back(DecodeContext::ZERO_NOTEQUAL, -1, -1);
2449
0
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2450
0
                break;
2451
0
            }
2452
            // Thresh
2453
0
            if (last - in >= 3 && in[0].first == OP_EQUAL && (num = ParseScriptNumber(in[1]))) {
  Branch (2453:17): [True: 0, False: 0]
  Branch (2453:17): [True: 0, False: 0]
  Branch (2453:35): [True: 0, False: 0]
  Branch (2453:62): [True: 0, False: 0]
  Branch (2453:17): [True: 0, False: 0]
  Branch (2453:17): [True: 0, False: 0]
  Branch (2453:35): [True: 0, False: 0]
  Branch (2453:62): [True: 0, False: 0]
  Branch (2453:17): [True: 0, False: 0]
  Branch (2453:17): [True: 0, False: 0]
  Branch (2453:35): [True: 0, False: 0]
  Branch (2453:62): [True: 0, False: 0]
  Branch (2453:17): [True: 0, False: 0]
  Branch (2453:17): [True: 0, False: 0]
  Branch (2453:35): [True: 0, False: 0]
  Branch (2453:62): [True: 0, False: 0]
  Branch (2453:17): [True: 0, False: 0]
  Branch (2453:17): [True: 0, False: 0]
  Branch (2453:35): [True: 0, False: 0]
  Branch (2453:62): [True: 0, False: 0]
2454
0
                if (*num < 1) return {};
  Branch (2454:21): [True: 0, False: 0]
  Branch (2454:21): [True: 0, False: 0]
  Branch (2454:21): [True: 0, False: 0]
  Branch (2454:21): [True: 0, False: 0]
  Branch (2454:21): [True: 0, False: 0]
2455
0
                in += 2;
2456
0
                to_parse.emplace_back(DecodeContext::THRESH_W, 0, *num);
2457
0
                break;
2458
0
            }
2459
            // OP_ENDIF can be WRAP_J, WRAP_D, ANDOR, OR_C, OR_D, or OR_I
2460
0
            if (in[0].first == OP_ENDIF) {
  Branch (2460:17): [True: 0, False: 0]
  Branch (2460:17): [True: 0, False: 0]
  Branch (2460:17): [True: 0, False: 0]
  Branch (2460:17): [True: 0, False: 0]
  Branch (2460:17): [True: 0, False: 0]
2461
0
                ++in;
2462
0
                to_parse.emplace_back(DecodeContext::ENDIF, -1, -1);
2463
0
                to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2464
0
                break;
2465
0
            }
2466
            /** In and_b and or_b nodes, we only look for SINGLE_BKV_EXPR, because
2467
             * or_b(and_v(X,Y),Z) has script [X] [Y] [Z] OP_BOOLOR, the same as
2468
             * and_v(X,or_b(Y,Z)). In this example, the former of these is invalid as
2469
             * miniscript, while the latter is valid. So we leave the and_v "outside"
2470
             * while decoding. */
2471
            // and_b
2472
0
            if (in[0].first == OP_BOOLAND) {
  Branch (2472:17): [True: 0, False: 0]
  Branch (2472:17): [True: 0, False: 0]
  Branch (2472:17): [True: 0, False: 0]
  Branch (2472:17): [True: 0, False: 0]
  Branch (2472:17): [True: 0, False: 0]
2473
0
                ++in;
2474
0
                to_parse.emplace_back(DecodeContext::AND_B, -1, -1);
2475
0
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2476
0
                to_parse.emplace_back(DecodeContext::W_EXPR, -1, -1);
2477
0
                break;
2478
0
            }
2479
            // or_b
2480
0
            if (in[0].first == OP_BOOLOR) {
  Branch (2480:17): [True: 0, False: 0]
  Branch (2480:17): [True: 0, False: 0]
  Branch (2480:17): [True: 0, False: 0]
  Branch (2480:17): [True: 0, False: 0]
  Branch (2480:17): [True: 0, False: 0]
2481
0
                ++in;
2482
0
                to_parse.emplace_back(DecodeContext::OR_B, -1, -1);
2483
0
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2484
0
                to_parse.emplace_back(DecodeContext::W_EXPR, -1, -1);
2485
0
                break;
2486
0
            }
2487
            // Unrecognised expression
2488
0
            return {};
2489
0
        }
2490
0
        case DecodeContext::BKV_EXPR: {
  Branch (2490:9): [True: 0, False: 0]
  Branch (2490:9): [True: 0, False: 0]
  Branch (2490:9): [True: 0, False: 0]
  Branch (2490:9): [True: 0, False: 0]
  Branch (2490:9): [True: 0, False: 0]
2491
0
            to_parse.emplace_back(DecodeContext::MAYBE_AND_V, -1, -1);
2492
0
            to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2493
0
            break;
2494
0
        }
2495
0
        case DecodeContext::W_EXPR: {
  Branch (2495:9): [True: 0, False: 0]
  Branch (2495:9): [True: 0, False: 0]
  Branch (2495:9): [True: 0, False: 0]
  Branch (2495:9): [True: 0, False: 0]
  Branch (2495:9): [True: 0, False: 0]
2496
            // a: wrapper
2497
0
            if (in >= last) return {};
  Branch (2497:17): [True: 0, False: 0]
  Branch (2497:17): [True: 0, False: 0]
  Branch (2497:17): [True: 0, False: 0]
  Branch (2497:17): [True: 0, False: 0]
  Branch (2497:17): [True: 0, False: 0]
2498
0
            if (in[0].first == OP_FROMALTSTACK) {
  Branch (2498:17): [True: 0, False: 0]
  Branch (2498:17): [True: 0, False: 0]
  Branch (2498:17): [True: 0, False: 0]
  Branch (2498:17): [True: 0, False: 0]
  Branch (2498:17): [True: 0, False: 0]
2499
0
                ++in;
2500
0
                to_parse.emplace_back(DecodeContext::ALT, -1, -1);
2501
0
            } else {
2502
0
                to_parse.emplace_back(DecodeContext::SWAP, -1, -1);
2503
0
            }
2504
0
            to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2505
0
            break;
2506
0
        }
2507
0
        case DecodeContext::MAYBE_AND_V: {
  Branch (2507:9): [True: 0, False: 0]
  Branch (2507:9): [True: 0, False: 0]
  Branch (2507:9): [True: 0, False: 0]
  Branch (2507:9): [True: 0, False: 0]
  Branch (2507:9): [True: 0, False: 0]
2508
            // If we reach a potential AND_V top-level, check if the next part of the script could be another AND_V child
2509
            // These op-codes cannot end any well-formed miniscript so cannot be used in an and_v node.
2510
0
            if (in < last && in[0].first != OP_IF && in[0].first != OP_ELSE && in[0].first != OP_NOTIF && in[0].first != OP_TOALTSTACK && in[0].first != OP_SWAP) {
  Branch (2510:17): [True: 0, False: 0]
  Branch (2510:30): [True: 0, False: 0]
  Branch (2510:54): [True: 0, False: 0]
  Branch (2510:80): [True: 0, False: 0]
  Branch (2510:107): [True: 0, False: 0]
  Branch (2510:139): [True: 0, False: 0]
  Branch (2510:17): [True: 0, False: 0]
  Branch (2510:30): [True: 0, False: 0]
  Branch (2510:54): [True: 0, False: 0]
  Branch (2510:80): [True: 0, False: 0]
  Branch (2510:107): [True: 0, False: 0]
  Branch (2510:139): [True: 0, False: 0]
  Branch (2510:17): [True: 0, False: 0]
  Branch (2510:30): [True: 0, False: 0]
  Branch (2510:54): [True: 0, False: 0]
  Branch (2510:80): [True: 0, False: 0]
  Branch (2510:107): [True: 0, False: 0]
  Branch (2510:139): [True: 0, False: 0]
  Branch (2510:17): [True: 0, False: 0]
  Branch (2510:30): [True: 0, False: 0]
  Branch (2510:54): [True: 0, False: 0]
  Branch (2510:80): [True: 0, False: 0]
  Branch (2510:107): [True: 0, False: 0]
  Branch (2510:139): [True: 0, False: 0]
  Branch (2510:17): [True: 0, False: 0]
  Branch (2510:30): [True: 0, False: 0]
  Branch (2510:54): [True: 0, False: 0]
  Branch (2510:80): [True: 0, False: 0]
  Branch (2510:107): [True: 0, False: 0]
  Branch (2510:139): [True: 0, False: 0]
2511
0
                to_parse.emplace_back(DecodeContext::AND_V, -1, -1);
2512
                // BKV_EXPR can contain more AND_V nodes
2513
0
                to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2514
0
            }
2515
0
            break;
2516
0
        }
2517
0
        case DecodeContext::SWAP: {
  Branch (2517:9): [True: 0, False: 0]
  Branch (2517:9): [True: 0, False: 0]
  Branch (2517:9): [True: 0, False: 0]
  Branch (2517:9): [True: 0, False: 0]
  Branch (2517:9): [True: 0, False: 0]
2518
0
            if (in >= last || in[0].first != OP_SWAP || constructed.empty()) return {};
  Branch (2518:17): [True: 0, False: 0]
  Branch (2518:31): [True: 0, False: 0]
  Branch (2518:57): [True: 0, False: 0]
  Branch (2518:17): [True: 0, False: 0]
  Branch (2518:31): [True: 0, False: 0]
  Branch (2518:57): [True: 0, False: 0]
  Branch (2518:17): [True: 0, False: 0]
  Branch (2518:31): [True: 0, False: 0]
  Branch (2518:57): [True: 0, False: 0]
  Branch (2518:17): [True: 0, False: 0]
  Branch (2518:31): [True: 0, False: 0]
  Branch (2518:57): [True: 0, False: 0]
  Branch (2518:17): [True: 0, False: 0]
  Branch (2518:31): [True: 0, False: 0]
  Branch (2518:57): [True: 0, False: 0]
2519
0
            ++in;
2520
0
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_S, Vector(std::move(constructed.back()))};
2521
0
            break;
2522
0
        }
2523
0
        case DecodeContext::ALT: {
  Branch (2523:9): [True: 0, False: 0]
  Branch (2523:9): [True: 0, False: 0]
  Branch (2523:9): [True: 0, False: 0]
  Branch (2523:9): [True: 0, False: 0]
  Branch (2523:9): [True: 0, False: 0]
2524
0
            if (in >= last || in[0].first != OP_TOALTSTACK || constructed.empty()) return {};
  Branch (2524:17): [True: 0, False: 0]
  Branch (2524:31): [True: 0, False: 0]
  Branch (2524:63): [True: 0, False: 0]
  Branch (2524:17): [True: 0, False: 0]
  Branch (2524:31): [True: 0, False: 0]
  Branch (2524:63): [True: 0, False: 0]
  Branch (2524:17): [True: 0, False: 0]
  Branch (2524:31): [True: 0, False: 0]
  Branch (2524:63): [True: 0, False: 0]
  Branch (2524:17): [True: 0, False: 0]
  Branch (2524:31): [True: 0, False: 0]
  Branch (2524:63): [True: 0, False: 0]
  Branch (2524:17): [True: 0, False: 0]
  Branch (2524:31): [True: 0, False: 0]
  Branch (2524:63): [True: 0, False: 0]
2525
0
            ++in;
2526
0
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_A, Vector(std::move(constructed.back()))};
2527
0
            break;
2528
0
        }
2529
0
        case DecodeContext::CHECK: {
  Branch (2529:9): [True: 0, False: 0]
  Branch (2529:9): [True: 0, False: 0]
  Branch (2529:9): [True: 0, False: 0]
  Branch (2529:9): [True: 0, False: 0]
  Branch (2529:9): [True: 0, False: 0]
2530
0
            if (constructed.empty()) return {};
  Branch (2530:17): [True: 0, False: 0]
  Branch (2530:17): [True: 0, False: 0]
  Branch (2530:17): [True: 0, False: 0]
  Branch (2530:17): [True: 0, False: 0]
  Branch (2530:17): [True: 0, False: 0]
2531
0
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_C, Vector(std::move(constructed.back()))};
2532
0
            break;
2533
0
        }
2534
0
        case DecodeContext::DUP_IF: {
  Branch (2534:9): [True: 0, False: 0]
  Branch (2534:9): [True: 0, False: 0]
  Branch (2534:9): [True: 0, False: 0]
  Branch (2534:9): [True: 0, False: 0]
  Branch (2534:9): [True: 0, False: 0]
2535
0
            if (constructed.empty()) return {};
  Branch (2535:17): [True: 0, False: 0]
  Branch (2535:17): [True: 0, False: 0]
  Branch (2535:17): [True: 0, False: 0]
  Branch (2535:17): [True: 0, False: 0]
  Branch (2535:17): [True: 0, False: 0]
2536
0
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_D, Vector(std::move(constructed.back()))};
2537
0
            break;
2538
0
        }
2539
0
        case DecodeContext::VERIFY: {
  Branch (2539:9): [True: 0, False: 0]
  Branch (2539:9): [True: 0, False: 0]
  Branch (2539:9): [True: 0, False: 0]
  Branch (2539:9): [True: 0, False: 0]
  Branch (2539:9): [True: 0, False: 0]
2540
0
            if (constructed.empty()) return {};
  Branch (2540:17): [True: 0, False: 0]
  Branch (2540:17): [True: 0, False: 0]
  Branch (2540:17): [True: 0, False: 0]
  Branch (2540:17): [True: 0, False: 0]
  Branch (2540:17): [True: 0, False: 0]
2541
0
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_V, Vector(std::move(constructed.back()))};
2542
0
            break;
2543
0
        }
2544
0
        case DecodeContext::NON_ZERO: {
  Branch (2544:9): [True: 0, False: 0]
  Branch (2544:9): [True: 0, False: 0]
  Branch (2544:9): [True: 0, False: 0]
  Branch (2544:9): [True: 0, False: 0]
  Branch (2544:9): [True: 0, False: 0]
2545
0
            if (constructed.empty()) return {};
  Branch (2545:17): [True: 0, False: 0]
  Branch (2545:17): [True: 0, False: 0]
  Branch (2545:17): [True: 0, False: 0]
  Branch (2545:17): [True: 0, False: 0]
  Branch (2545:17): [True: 0, False: 0]
2546
0
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_J, Vector(std::move(constructed.back()))};
2547
0
            break;
2548
0
        }
2549
0
        case DecodeContext::ZERO_NOTEQUAL: {
  Branch (2549:9): [True: 0, False: 0]
  Branch (2549:9): [True: 0, False: 0]
  Branch (2549:9): [True: 0, False: 0]
  Branch (2549:9): [True: 0, False: 0]
  Branch (2549:9): [True: 0, False: 0]
2550
0
            if (constructed.empty()) return {};
  Branch (2550:17): [True: 0, False: 0]
  Branch (2550:17): [True: 0, False: 0]
  Branch (2550:17): [True: 0, False: 0]
  Branch (2550:17): [True: 0, False: 0]
  Branch (2550:17): [True: 0, False: 0]
2551
0
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_N, Vector(std::move(constructed.back()))};
2552
0
            break;
2553
0
        }
2554
0
        case DecodeContext::AND_V: {
  Branch (2554:9): [True: 0, False: 0]
  Branch (2554:9): [True: 0, False: 0]
  Branch (2554:9): [True: 0, False: 0]
  Branch (2554:9): [True: 0, False: 0]
  Branch (2554:9): [True: 0, False: 0]
2555
0
            if (constructed.size() < 2) return {};
  Branch (2555:17): [True: 0, False: 0]
  Branch (2555:17): [True: 0, False: 0]
  Branch (2555:17): [True: 0, False: 0]
  Branch (2555:17): [True: 0, False: 0]
  Branch (2555:17): [True: 0, False: 0]
2556
0
            BuildBack(ctx.MsContext(), Fragment::AND_V, constructed, /*reverse=*/true);
2557
0
            break;
2558
0
        }
2559
0
        case DecodeContext::AND_B: {
  Branch (2559:9): [True: 0, False: 0]
  Branch (2559:9): [True: 0, False: 0]
  Branch (2559:9): [True: 0, False: 0]
  Branch (2559:9): [True: 0, False: 0]
  Branch (2559:9): [True: 0, False: 0]
2560
0
            if (constructed.size() < 2) return {};
  Branch (2560:17): [True: 0, False: 0]
  Branch (2560:17): [True: 0, False: 0]
  Branch (2560:17): [True: 0, False: 0]
  Branch (2560:17): [True: 0, False: 0]
  Branch (2560:17): [True: 0, False: 0]
2561
0
            BuildBack(ctx.MsContext(), Fragment::AND_B, constructed, /*reverse=*/true);
2562
0
            break;
2563
0
        }
2564
0
        case DecodeContext::OR_B: {
  Branch (2564:9): [True: 0, False: 0]
  Branch (2564:9): [True: 0, False: 0]
  Branch (2564:9): [True: 0, False: 0]
  Branch (2564:9): [True: 0, False: 0]
  Branch (2564:9): [True: 0, False: 0]
2565
0
            if (constructed.size() < 2) return {};
  Branch (2565:17): [True: 0, False: 0]
  Branch (2565:17): [True: 0, False: 0]
  Branch (2565:17): [True: 0, False: 0]
  Branch (2565:17): [True: 0, False: 0]
  Branch (2565:17): [True: 0, False: 0]
2566
0
            BuildBack(ctx.MsContext(), Fragment::OR_B, constructed, /*reverse=*/true);
2567
0
            break;
2568
0
        }
2569
0
        case DecodeContext::OR_C: {
  Branch (2569:9): [True: 0, False: 0]
  Branch (2569:9): [True: 0, False: 0]
  Branch (2569:9): [True: 0, False: 0]
  Branch (2569:9): [True: 0, False: 0]
  Branch (2569:9): [True: 0, False: 0]
2570
0
            if (constructed.size() < 2) return {};
  Branch (2570:17): [True: 0, False: 0]
  Branch (2570:17): [True: 0, False: 0]
  Branch (2570:17): [True: 0, False: 0]
  Branch (2570:17): [True: 0, False: 0]
  Branch (2570:17): [True: 0, False: 0]
2571
0
            BuildBack(ctx.MsContext(), Fragment::OR_C, constructed, /*reverse=*/true);
2572
0
            break;
2573
0
        }
2574
0
        case DecodeContext::OR_D: {
  Branch (2574:9): [True: 0, False: 0]
  Branch (2574:9): [True: 0, False: 0]
  Branch (2574:9): [True: 0, False: 0]
  Branch (2574:9): [True: 0, False: 0]
  Branch (2574:9): [True: 0, False: 0]
2575
0
            if (constructed.size() < 2) return {};
  Branch (2575:17): [True: 0, False: 0]
  Branch (2575:17): [True: 0, False: 0]
  Branch (2575:17): [True: 0, False: 0]
  Branch (2575:17): [True: 0, False: 0]
  Branch (2575:17): [True: 0, False: 0]
2576
0
            BuildBack(ctx.MsContext(), Fragment::OR_D, constructed, /*reverse=*/true);
2577
0
            break;
2578
0
        }
2579
0
        case DecodeContext::ANDOR: {
  Branch (2579:9): [True: 0, False: 0]
  Branch (2579:9): [True: 0, False: 0]
  Branch (2579:9): [True: 0, False: 0]
  Branch (2579:9): [True: 0, False: 0]
  Branch (2579:9): [True: 0, False: 0]
2580
0
            if (constructed.size() < 3) return {};
  Branch (2580:17): [True: 0, False: 0]
  Branch (2580:17): [True: 0, False: 0]
  Branch (2580:17): [True: 0, False: 0]
  Branch (2580:17): [True: 0, False: 0]
  Branch (2580:17): [True: 0, False: 0]
2581
0
            Node left{std::move(constructed.back())};
2582
0
            constructed.pop_back();
2583
0
            Node right{std::move(constructed.back())};
2584
0
            constructed.pop_back();
2585
0
            Node mid{std::move(constructed.back())};
2586
0
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::ANDOR, Vector(std::move(left), std::move(mid), std::move(right))};
2587
0
            break;
2588
0
        }
2589
0
        case DecodeContext::THRESH_W: {
  Branch (2589:9): [True: 0, False: 0]
  Branch (2589:9): [True: 0, False: 0]
  Branch (2589:9): [True: 0, False: 0]
  Branch (2589:9): [True: 0, False: 0]
  Branch (2589:9): [True: 0, False: 0]
2590
0
            if (in >= last) return {};
  Branch (2590:17): [True: 0, False: 0]
  Branch (2590:17): [True: 0, False: 0]
  Branch (2590:17): [True: 0, False: 0]
  Branch (2590:17): [True: 0, False: 0]
  Branch (2590:17): [True: 0, False: 0]
2591
0
            if (in[0].first == OP_ADD) {
  Branch (2591:17): [True: 0, False: 0]
  Branch (2591:17): [True: 0, False: 0]
  Branch (2591:17): [True: 0, False: 0]
  Branch (2591:17): [True: 0, False: 0]
  Branch (2591:17): [True: 0, False: 0]
2592
0
                ++in;
2593
0
                to_parse.emplace_back(DecodeContext::THRESH_W, n+1, k);
2594
0
                to_parse.emplace_back(DecodeContext::W_EXPR, -1, -1);
2595
0
            } else {
2596
0
                to_parse.emplace_back(DecodeContext::THRESH_E, n+1, k);
2597
                // All children of thresh have type modifier d, so cannot be and_v
2598
0
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2599
0
            }
2600
0
            break;
2601
0
        }
2602
0
        case DecodeContext::THRESH_E: {
  Branch (2602:9): [True: 0, False: 0]
  Branch (2602:9): [True: 0, False: 0]
  Branch (2602:9): [True: 0, False: 0]
  Branch (2602:9): [True: 0, False: 0]
  Branch (2602:9): [True: 0, False: 0]
2603
0
            if (k < 1 || k > n || constructed.size() < static_cast<size_t>(n)) return {};
  Branch (2603:17): [True: 0, False: 0]
  Branch (2603:26): [True: 0, False: 0]
  Branch (2603:35): [True: 0, False: 0]
  Branch (2603:17): [True: 0, False: 0]
  Branch (2603:26): [True: 0, False: 0]
  Branch (2603:35): [True: 0, False: 0]
  Branch (2603:17): [True: 0, False: 0]
  Branch (2603:26): [True: 0, False: 0]
  Branch (2603:35): [True: 0, False: 0]
  Branch (2603:17): [True: 0, False: 0]
  Branch (2603:26): [True: 0, False: 0]
  Branch (2603:35): [True: 0, False: 0]
  Branch (2603:17): [True: 0, False: 0]
  Branch (2603:26): [True: 0, False: 0]
  Branch (2603:35): [True: 0, False: 0]
2604
0
            std::vector<Node<Key>> subs;
2605
0
            for (int i = 0; i < n; ++i) {
  Branch (2605:29): [True: 0, False: 0]
  Branch (2605:29): [True: 0, False: 0]
  Branch (2605:29): [True: 0, False: 0]
  Branch (2605:29): [True: 0, False: 0]
  Branch (2605:29): [True: 0, False: 0]
2606
0
                Node sub{std::move(constructed.back())};
2607
0
                constructed.pop_back();
2608
0
                subs.push_back(std::move(sub));
2609
0
            }
2610
0
            constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::THRESH, std::move(subs), k);
2611
0
            break;
2612
0
        }
2613
0
        case DecodeContext::ENDIF: {
  Branch (2613:9): [True: 0, False: 0]
  Branch (2613:9): [True: 0, False: 0]
  Branch (2613:9): [True: 0, False: 0]
  Branch (2613:9): [True: 0, False: 0]
  Branch (2613:9): [True: 0, False: 0]
2614
0
            if (in >= last) return {};
  Branch (2614:17): [True: 0, False: 0]
  Branch (2614:17): [True: 0, False: 0]
  Branch (2614:17): [True: 0, False: 0]
  Branch (2614:17): [True: 0, False: 0]
  Branch (2614:17): [True: 0, False: 0]
2615
2616
            // could be andor or or_i
2617
0
            if (in[0].first == OP_ELSE) {
  Branch (2617:17): [True: 0, False: 0]
  Branch (2617:17): [True: 0, False: 0]
  Branch (2617:17): [True: 0, False: 0]
  Branch (2617:17): [True: 0, False: 0]
  Branch (2617:17): [True: 0, False: 0]
2618
0
                ++in;
2619
0
                to_parse.emplace_back(DecodeContext::ENDIF_ELSE, -1, -1);
2620
0
                to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2621
0
            }
2622
            // could be j: or d: wrapper
2623
0
            else if (in[0].first == OP_IF) {
  Branch (2623:22): [True: 0, False: 0]
  Branch (2623:22): [True: 0, False: 0]
  Branch (2623:22): [True: 0, False: 0]
  Branch (2623:22): [True: 0, False: 0]
  Branch (2623:22): [True: 0, False: 0]
2624
0
                if (last - in >= 2 && in[1].first == OP_DUP) {
  Branch (2624:21): [True: 0, False: 0]
  Branch (2624:39): [True: 0, False: 0]
  Branch (2624:21): [True: 0, False: 0]
  Branch (2624:39): [True: 0, False: 0]
  Branch (2624:21): [True: 0, False: 0]
  Branch (2624:39): [True: 0, False: 0]
  Branch (2624:21): [True: 0, False: 0]
  Branch (2624:39): [True: 0, False: 0]
  Branch (2624:21): [True: 0, False: 0]
  Branch (2624:39): [True: 0, False: 0]
2625
0
                    in += 2;
2626
0
                    to_parse.emplace_back(DecodeContext::DUP_IF, -1, -1);
2627
0
                } else if (last - in >= 3 && in[1].first == OP_0NOTEQUAL && in[2].first == OP_SIZE) {
  Branch (2627:28): [True: 0, False: 0]
  Branch (2627:46): [True: 0, False: 0]
  Branch (2627:77): [True: 0, False: 0]
  Branch (2627:28): [True: 0, False: 0]
  Branch (2627:46): [True: 0, False: 0]
  Branch (2627:77): [True: 0, False: 0]
  Branch (2627:28): [True: 0, False: 0]
  Branch (2627:46): [True: 0, False: 0]
  Branch (2627:77): [True: 0, False: 0]
  Branch (2627:28): [True: 0, False: 0]
  Branch (2627:46): [True: 0, False: 0]
  Branch (2627:77): [True: 0, False: 0]
  Branch (2627:28): [True: 0, False: 0]
  Branch (2627:46): [True: 0, False: 0]
  Branch (2627:77): [True: 0, False: 0]
2628
0
                    in += 3;
2629
0
                    to_parse.emplace_back(DecodeContext::NON_ZERO, -1, -1);
2630
0
                }
2631
0
                else {
2632
0
                    return {};
2633
0
                }
2634
            // could be or_c or or_d
2635
0
            } else if (in[0].first == OP_NOTIF) {
  Branch (2635:24): [True: 0, False: 0]
  Branch (2635:24): [True: 0, False: 0]
  Branch (2635:24): [True: 0, False: 0]
  Branch (2635:24): [True: 0, False: 0]
  Branch (2635:24): [True: 0, False: 0]
2636
0
                ++in;
2637
0
                to_parse.emplace_back(DecodeContext::ENDIF_NOTIF, -1, -1);
2638
0
            }
2639
0
            else {
2640
0
                return {};
2641
0
            }
2642
0
            break;
2643
0
        }
2644
0
        case DecodeContext::ENDIF_NOTIF: {
  Branch (2644:9): [True: 0, False: 0]
  Branch (2644:9): [True: 0, False: 0]
  Branch (2644:9): [True: 0, False: 0]
  Branch (2644:9): [True: 0, False: 0]
  Branch (2644:9): [True: 0, False: 0]
2645
0
            if (in >= last) return {};
  Branch (2645:17): [True: 0, False: 0]
  Branch (2645:17): [True: 0, False: 0]
  Branch (2645:17): [True: 0, False: 0]
  Branch (2645:17): [True: 0, False: 0]
  Branch (2645:17): [True: 0, False: 0]
2646
0
            if (in[0].first == OP_IFDUP) {
  Branch (2646:17): [True: 0, False: 0]
  Branch (2646:17): [True: 0, False: 0]
  Branch (2646:17): [True: 0, False: 0]
  Branch (2646:17): [True: 0, False: 0]
  Branch (2646:17): [True: 0, False: 0]
2647
0
                ++in;
2648
0
                to_parse.emplace_back(DecodeContext::OR_D, -1, -1);
2649
0
            } else {
2650
0
                to_parse.emplace_back(DecodeContext::OR_C, -1, -1);
2651
0
            }
2652
            // or_c and or_d both require X to have type modifier d so, can't contain and_v
2653
0
            to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2654
0
            break;
2655
0
        }
2656
0
        case DecodeContext::ENDIF_ELSE: {
  Branch (2656:9): [True: 0, False: 0]
  Branch (2656:9): [True: 0, False: 0]
  Branch (2656:9): [True: 0, False: 0]
  Branch (2656:9): [True: 0, False: 0]
  Branch (2656:9): [True: 0, False: 0]
2657
0
            if (in >= last) return {};
  Branch (2657:17): [True: 0, False: 0]
  Branch (2657:17): [True: 0, False: 0]
  Branch (2657:17): [True: 0, False: 0]
  Branch (2657:17): [True: 0, False: 0]
  Branch (2657:17): [True: 0, False: 0]
2658
0
            if (in[0].first == OP_IF) {
  Branch (2658:17): [True: 0, False: 0]
  Branch (2658:17): [True: 0, False: 0]
  Branch (2658:17): [True: 0, False: 0]
  Branch (2658:17): [True: 0, False: 0]
  Branch (2658:17): [True: 0, False: 0]
2659
0
                ++in;
2660
0
                BuildBack(ctx.MsContext(), Fragment::OR_I, constructed, /*reverse=*/true);
2661
0
            } else if (in[0].first == OP_NOTIF) {
  Branch (2661:24): [True: 0, False: 0]
  Branch (2661:24): [True: 0, False: 0]
  Branch (2661:24): [True: 0, False: 0]
  Branch (2661:24): [True: 0, False: 0]
  Branch (2661:24): [True: 0, False: 0]
2662
0
                ++in;
2663
0
                to_parse.emplace_back(DecodeContext::ANDOR, -1, -1);
2664
                // andor requires X to have type modifier d, so it can't be and_v
2665
0
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2666
0
            } else {
2667
0
                return {};
2668
0
            }
2669
0
            break;
2670
0
        }
2671
0
        }
2672
0
    }
2673
0
    if (constructed.size() != 1) return {};
  Branch (2673:9): [True: 0, False: 0]
  Branch (2673:9): [True: 0, False: 0]
  Branch (2673:9): [True: 0, False: 0]
  Branch (2673:9): [True: 0, False: 0]
  Branch (2673:9): [True: 0, False: 0]
2674
0
    Node tl_node{std::move(constructed.front())};
2675
0
    tl_node.DuplicateKeyCheck(ctx);
2676
    // Note that due to how ComputeType works (only assign the type to the node if the
2677
    // subs' types are valid) this would fail if any node of tree is badly typed.
2678
0
    if (!tl_node.IsValidTopLevel()) return {};
  Branch (2678:9): [True: 0, False: 0]
  Branch (2678:9): [True: 0, False: 0]
  Branch (2678:9): [True: 0, False: 0]
  Branch (2678:9): [True: 0, False: 0]
  Branch (2678:9): [True: 0, False: 0]
2679
0
    return tl_node;
2680
0
}
Unexecuted instantiation: miniscript.cpp:_ZN10miniscript8internal12DecodeScriptI7CPubKeyN12_GLOBAL__N_113ParserContextEN9__gnu_cxx17__normal_iteratorIPSt4pairI10opcodetypeSt6vectorIhSaIhEEES9_ISC_SaISC_EEEEEESt8optionalINS_4NodeIT_EEERT1_SM_RKT0_
Unexecuted instantiation: miniscript.cpp:_ZN10miniscript8internal12DecodeScriptIN12_GLOBAL__N_119ScriptParserContext3KeyES3_N9__gnu_cxx17__normal_iteratorIPSt4pairI10opcodetypeSt6vectorIhSaIhEEES9_ISC_SaISC_EEEEEESt8optionalINS_4NodeIT_EEERT1_SM_RKT0_
Unexecuted instantiation: descriptor.cpp:_ZN10miniscript8internal12DecodeScriptIjN12_GLOBAL__N_19KeyParserEN9__gnu_cxx17__normal_iteratorIPSt4pairI10opcodetypeSt6vectorIhSaIhEEES8_ISB_SaISB_EEEEEESt8optionalINS_4NodeIT_EEERT1_SL_RKT0_
Unexecuted instantiation: _ZN10miniscript8internal12DecodeScriptI11XOnlyPubKey12TapSatisfierN9__gnu_cxx17__normal_iteratorIPSt4pairI10opcodetypeSt6vectorIhSaIhEEES8_ISB_SaISB_EEEEEESt8optionalINS_4NodeIT_EEERT1_SL_RKT0_
Unexecuted instantiation: _ZN10miniscript8internal12DecodeScriptI7CPubKey12WshSatisfierN9__gnu_cxx17__normal_iteratorIPSt4pairI10opcodetypeSt6vectorIhSaIhEEES8_ISB_SaISB_EEEEEESt8optionalINS_4NodeIT_EEERT1_SL_RKT0_
2681
2682
} // namespace internal
2683
2684
template <typename Ctx>
2685
inline std::optional<Node<typename Ctx::Key>> FromString(const std::string& str, const Ctx& ctx)
2686
0
{
2687
0
    return internal::Parse<typename Ctx::Key>(str, ctx);
2688
0
}
Unexecuted instantiation: miniscript.cpp:_ZN10miniscript10FromStringIN12_GLOBAL__N_113ParserContextEEESt8optionalINS_4NodeINT_3KeyEEEERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKS5_
Unexecuted instantiation: descriptor.cpp:_ZN10miniscript10FromStringIN12_GLOBAL__N_19KeyParserEEESt8optionalINS_4NodeINT_3KeyEEEERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKS5_
2689
2690
template <typename Ctx>
2691
inline std::optional<Node<typename Ctx::Key>> FromScript(const CScript& script, const Ctx& ctx)
2692
0
{
2693
0
    using namespace internal;
2694
    // A too large Script is necessarily invalid, don't bother parsing it.
2695
0
    if (script.size() > MaxScriptSize(ctx.MsContext())) return {};
  Branch (2695:9): [True: 0, False: 0]
  Branch (2695:9): [True: 0, False: 0]
  Branch (2695:9): [True: 0, False: 0]
  Branch (2695:9): [True: 0, False: 0]
  Branch (2695:9): [True: 0, False: 0]
2696
0
    auto decomposed = DecomposeScript(script);
2697
0
    if (!decomposed) return {};
  Branch (2697:9): [True: 0, False: 0]
  Branch (2697:9): [True: 0, False: 0]
  Branch (2697:9): [True: 0, False: 0]
  Branch (2697:9): [True: 0, False: 0]
  Branch (2697:9): [True: 0, False: 0]
2698
0
    auto it = decomposed->begin();
2699
0
    auto ret = DecodeScript<typename Ctx::Key>(it, decomposed->end(), ctx);
2700
0
    if (!ret) return {};
  Branch (2700:9): [True: 0, False: 0]
  Branch (2700:9): [True: 0, False: 0]
  Branch (2700:9): [True: 0, False: 0]
  Branch (2700:9): [True: 0, False: 0]
  Branch (2700:9): [True: 0, False: 0]
2701
0
    if (it != decomposed->end()) return {};
  Branch (2701:9): [True: 0, False: 0]
  Branch (2701:9): [True: 0, False: 0]
  Branch (2701:9): [True: 0, False: 0]
  Branch (2701:9): [True: 0, False: 0]
  Branch (2701:9): [True: 0, False: 0]
2702
0
    return ret;
2703
0
}
Unexecuted instantiation: miniscript.cpp:_ZN10miniscript10FromScriptIN12_GLOBAL__N_113ParserContextEEESt8optionalINS_4NodeINT_3KeyEEEERK7CScriptRKS5_
Unexecuted instantiation: miniscript.cpp:_ZN10miniscript10FromScriptIN12_GLOBAL__N_119ScriptParserContextEEESt8optionalINS_4NodeINT_3KeyEEEERK7CScriptRKS5_
Unexecuted instantiation: descriptor.cpp:_ZN10miniscript10FromScriptIN12_GLOBAL__N_19KeyParserEEESt8optionalINS_4NodeINT_3KeyEEEERK7CScriptRKS5_
Unexecuted instantiation: _ZN10miniscript10FromScriptI12TapSatisfierEESt8optionalINS_4NodeINT_3KeyEEEERK7CScriptRKS4_
Unexecuted instantiation: _ZN10miniscript10FromScriptI12WshSatisfierEESt8optionalINS_4NodeINT_3KeyEEEERK7CScriptRKS4_
2704
2705
} // namespace miniscript
2706
2707
#endif // BITCOIN_SCRIPT_MINISCRIPT_H