Coverage Report

Created: 2026-09-21 19:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/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
12.6M
    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
6.29M
    constexpr Type operator|(Type x) const { return Type(m_flags | x.m_flags); }
143
144
    //! Compute the type with the intersection of properties.
145
6.25M
    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
111M
    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
2.05M
    constexpr bool operator==(Type x) const { return m_flags == x.m_flags; }
155
156
    //! The empty type if x is false, itself otherwise.
157
100k
    constexpr Type If(bool x) const { return Type(x ? m_flags : 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
911
{
201
911
    std::vector<std::reference_wrapper<const Node<Key>>> stack{root};
202
997k
    while (!stack.empty()) {
203
996k
        const Node<Key>& node = stack.back();
204
996k
        std::invoke(fn, node);
205
996k
        stack.pop_back();
206
996k
        for (const auto& sub : node.Subs()) {
207
995k
            stack.emplace_back(sub);
208
995k
        }
209
996k
    }
210
911
}
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
8.45M
{
261
8.45M
    switch (ms_ctx) {
262
71.0k
        case MiniscriptContext::P2WSH: return false;
263
8.38M
        case MiniscriptContext::TAPSCRIPT: return true;
264
8.45M
    }
265
8.45M
    assert(false);
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
2.05M
{
286
2.05M
    if (IsTapscript(ms_ctx)) {
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
2.02M
        constexpr auto max_size{MAX_STANDARD_TX_WEIGHT - TX_BODY_LEEWAY_WEIGHT - MAX_TAPSCRIPT_SAT_SIZE};
293
2.02M
        return max_size - GetSizeOfCompactSize(max_size);
294
2.02M
    }
295
25.5k
    return MAX_STANDARD_P2WSH_SCRIPT_SIZE;
296
2.05M
}
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
1.40k
    InputStack() = default;
328
    //! Construct a valid single-element stack (with an element up to 75 bytes).
329
484k
    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
841k
    InputResult(A&& in_nsat, B&& in_sat) : nsat(std::forward<A>(in_nsat)), sat(std::forward<B>(in_sat)) {}
miniscript::internal::InputResult::InputResult<miniscript::internal::InputStack const&, miniscript::internal::InputStack&>(miniscript::internal::InputStack const&, miniscript::internal::InputStack&)
Line
Count
Source
360
380k
    InputResult(A&& in_nsat, B&& in_sat) : nsat(std::forward<A>(in_nsat)), sat(std::forward<B>(in_sat)) {}
miniscript::internal::InputResult::InputResult<miniscript::internal::InputStack, miniscript::internal::InputStack&>(miniscript::internal::InputStack&&, miniscript::internal::InputStack&)
Line
Count
Source
360
1.12k
    InputResult(A&& in_nsat, B&& in_sat) : nsat(std::forward<A>(in_nsat)), sat(std::forward<B>(in_sat)) {}
miniscript::internal::InputResult::InputResult<miniscript::internal::InputStack, miniscript::internal::InputStack>(miniscript::internal::InputStack&&, miniscript::internal::InputStack&&)
Line
Count
Source
360
412k
    InputResult(A&& in_nsat, B&& in_sat) : nsat(std::forward<A>(in_nsat)), sat(std::forward<B>(in_sat)) {}
miniscript::internal::InputResult::InputResult<miniscript::internal::InputStack const&, miniscript::internal::InputStack const&>(miniscript::internal::InputStack const&, miniscript::internal::InputStack const&)
Line
Count
Source
360
42.0k
    InputResult(A&& in_nsat, B&& in_sat) : nsat(std::forward<A>(in_nsat)), sat(std::forward<B>(in_sat)) {}
miniscript::internal::InputResult::InputResult<miniscript::internal::InputStack&, miniscript::internal::InputStack>(miniscript::internal::InputStack&, miniscript::internal::InputStack&&)
Line
Count
Source
360
2.61k
    InputResult(A&& in_nsat, B&& in_sat) : nsat(std::forward<A>(in_nsat)), sat(std::forward<B>(in_sat)) {}
miniscript::internal::InputResult::InputResult<miniscript::internal::InputStack const&, miniscript::internal::InputStack>(miniscript::internal::InputStack const&, miniscript::internal::InputStack&&)
Line
Count
Source
360
2.95k
    InputResult(A&& in_nsat, B&& in_sat) : nsat(std::forward<A>(in_nsat)), sat(std::forward<B>(in_sat)) {}
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
44.9k
    MaxInt() : valid(false), value(0) {}
372
115k
    MaxInt(I val) : valid(true), value(val) {}
373
374
2.80k
    bool Valid() const { return valid; }
375
2.79k
    I Value() const { return value; }
376
377
59.4k
    friend MaxInt<I> operator+(const MaxInt<I>& a, const MaxInt<I>& b) {
378
59.4k
        if (!a.valid || !b.valid) return {};
379
44.7k
        return a.value + b.value;
380
59.4k
    }
381
382
10.1k
    friend MaxInt<I> operator|(const MaxInt<I>& a, const MaxInt<I>& b) {
383
10.1k
        if (!a.valid) return b;
384
8.88k
        if (!b.valid) return a;
385
7.53k
        return std::max(a.value, b.value);
386
8.88k
    }
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
3.08M
    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
27.9k
    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
152k
        valid{true}, netdiff{in_netdiff}, exec{in_exec} {}
457
458
7.36k
    bool Valid() const { return valid; }
459
2.86k
    int32_t NetDiff() const { return netdiff; }
460
4.48k
    int32_t Exec() const { return exec; }
461
462
    /** Script set union. */
463
    constexpr friend SatInfo operator|(const SatInfo& a, const SatInfo& b) noexcept
464
5.06k
    {
465
        // Union with an empty set is itself.
466
5.06k
        if (!a.valid) return b;
467
4.44k
        if (!b.valid) return a;
468
        // Otherwise the netdiff and exec of the union is the maximum of the individual values.
469
3.76k
        return {std::max(a.netdiff, b.netdiff), std::max(a.exec, b.exec)};
470
4.44k
    }
471
472
    /** Script set concatenation. */
473
    constexpr friend SatInfo operator+(const SatInfo& a, const SatInfo& b) noexcept
474
86.5k
    {
475
        // Concatenation with an empty set yields an empty set.
476
86.5k
        if (!a.valid || !b.valid) return {};
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
73.7k
        return {a.netdiff + b.netdiff, std::max(b.exec, b.netdiff + a.exec)};
481
86.5k
    }
482
483
    /** The empty script. */
484
856
    static constexpr SatInfo Empty() noexcept { return {0, 0}; }
485
    /** A script consisting of a single push opcode. */
486
20.1k
    static constexpr SatInfo Push() noexcept { return {-1, 0}; }
487
    /** A script consisting of a single hash opcode. */
488
1.38k
    static constexpr SatInfo Hash() noexcept { return {0, 0}; }
489
    /** A script consisting of just a repurposed nop (OP_CHECKLOCKTIMEVERIFY, OP_CHECKSEQUENCEVERIFY). */
490
9.45k
    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
2.86k
    static constexpr SatInfo If() noexcept { return {1, 1}; }
493
    /** A script consisting of just a binary operator (OP_BOOLAND, OP_BOOLOR, OP_ADD). */
494
16.0k
    static constexpr SatInfo BinaryOp() noexcept { return {1, 1}; }
495
496
    // Scripts for specific individual opcodes.
497
1.21k
    static constexpr SatInfo OP_DUP() noexcept { return {-1, 0}; }
498
390
    static constexpr SatInfo OP_IFDUP(bool nonzero) noexcept { return {nonzero ? -1 : 0, 0}; }
499
1.38k
    static constexpr SatInfo OP_EQUALVERIFY() noexcept { return {2, 2}; }
500
1.25k
    static constexpr SatInfo OP_EQUAL() noexcept { return {1, 1}; }
501
428
    static constexpr SatInfo OP_SIZE() noexcept { return {-1, 0}; }
502
15.9k
    static constexpr SatInfo OP_CHECKSIG() noexcept { return {1, 1}; }
503
32
    static constexpr SatInfo OP_0NOTEQUAL() noexcept { return {0, 0}; }
504
2.21k
    static constexpr SatInfo OP_VERIFY() noexcept { return {1, 1}; }
505
};
506
507
class StackSize
508
{
509
    SatInfo sat, dsat;
510
511
public:
512
32.1k
    constexpr StackSize(SatInfo in_sat, SatInfo in_dsat) noexcept : sat(in_sat), dsat(in_dsat) {};
513
9.07k
    constexpr StackSize(SatInfo in_both) noexcept : sat(in_both), dsat(in_both) {};
514
515
51.2k
    const SatInfo& Sat() const { return sat; }
516
30.4k
    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
33.2k
    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
7.27M
    {
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
7.27M
        std::vector<std::vector<Node>> queue;
561
7.27M
        queue.push_back(std::move(subs));
562
10.3M
        do {
563
10.3M
            auto flattening{std::move(queue.back())};
564
10.3M
            queue.pop_back();
565
10.3M
            for (Node& n : flattening) {
566
3.07M
                if (!n.subs.empty()) queue.push_back(std::move(n.subs));
567
3.07M
            }
568
10.3M
        } while (!queue.empty());
569
7.27M
    }
miniscript::Node<CPubKey>::~Node()
Line
Count
Source
553
73.8k
    {
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
73.8k
        std::vector<std::vector<Node>> queue;
561
73.8k
        queue.push_back(std::move(subs));
562
90.9k
        do {
563
90.9k
            auto flattening{std::move(queue.back())};
564
90.9k
            queue.pop_back();
565
90.9k
            for (Node& n : flattening) {
566
26.2k
                if (!n.subs.empty()) queue.push_back(std::move(n.subs));
567
26.2k
            }
568
90.9k
        } while (!queue.empty());
569
73.8k
    }
miniscript::Node<unsigned int>::~Node()
Line
Count
Source
553
4.52M
    {
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
4.52M
        std::vector<std::vector<Node>> queue;
561
4.52M
        queue.push_back(std::move(subs));
562
6.24M
        do {
563
6.24M
            auto flattening{std::move(queue.back())};
564
6.24M
            queue.pop_back();
565
6.24M
            for (Node& n : flattening) {
566
1.72M
                if (!n.subs.empty()) queue.push_back(std::move(n.subs));
567
1.72M
            }
568
6.24M
        } while (!queue.empty());
569
4.52M
    }
miniscript::Node<XOnlyPubKey>::~Node()
Line
Count
Source
553
2.67M
    {
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
2.67M
        std::vector<std::vector<Node>> queue;
561
2.67M
        queue.push_back(std::move(subs));
562
3.99M
        do {
563
3.99M
            auto flattening{std::move(queue.back())};
564
3.99M
            queue.pop_back();
565
3.99M
            for (Node& n : flattening) {
566
1.32M
                if (!n.subs.empty()) queue.push_back(std::move(n.subs));
567
1.32M
            }
568
3.99M
        } while (!queue.empty());
569
2.67M
    }
570
    // NOLINTEND(misc-no-recursion)
571
572
    Node<Key> Clone() const
573
223
    {
574
        // Use TreeEval() to avoid a stack-overflow due to recursion
575
531k
        auto upfn = [](const Node& node, std::span<Node> children) {
576
531k
            std::vector<Node> new_subs;
577
531k
            for (auto& child : children) {
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
531k
                new_subs.push_back(std::move(child));
581
531k
            }
582
531k
            return Node{internal::NoDupCheck{}, node.m_script_ctx, node.fragment, std::move(new_subs), node.keys, node.data, node.k};
583
531k
        };
584
223
        return TreeEval<Node>(upfn);
585
223
    }
586
587
1.00M
    enum Fragment Fragment() const { return fragment; }
miniscript::Node<CPubKey>::Fragment() const
Line
Count
Source
587
8.47k
    enum Fragment Fragment() const { return fragment; }
miniscript::Node<unsigned int>::Fragment() const
Line
Count
Source
587
996k
    enum Fragment Fragment() const { return fragment; }
588
2.37k
    uint32_t K() const { return k; }
miniscript::Node<CPubKey>::K() const
Line
Count
Source
588
2.10k
    uint32_t K() const { return k; }
miniscript::Node<unsigned int>::K() const
Line
Count
Source
588
267
    uint32_t K() const { return k; }
589
8.47k
    const std::vector<Key>& Keys() const { return keys; }
590
48
    const std::vector<unsigned char>& Data() const { return data; }
591
2.20M
    const std::vector<Node>& Subs() const { return subs; }
miniscript::Node<CPubKey>::Subs() const
Line
Count
Source
591
8.47k
    const std::vector<Node>& Subs() const { return subs; }
miniscript::Node<unsigned int>::Subs() const
Line
Count
Source
591
2.19M
    const std::vector<Node>& Subs() const { return subs; }
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
531k
        : 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
3.08M
    {
620
3.08M
        size_t subsize = 0;
621
3.08M
        for (const auto& sub : subs) {
622
3.07M
            subsize += sub.ScriptSize();
623
3.07M
        }
624
3.08M
        Type sub0type = subs.size() > 0 ? subs[0].GetType() : ""_mst;
625
3.08M
        return internal::ComputeScriptLen(fragment, sub0type, subsize, k, subs.size(), keys.size(), m_script_ctx);
626
3.08M
    }
miniscript::Node<CPubKey>::CalcScriptLen() const
Line
Count
Source
619
28.5k
    {
620
28.5k
        size_t subsize = 0;
621
28.5k
        for (const auto& sub : subs) {
622
26.2k
            subsize += sub.ScriptSize();
623
26.2k
        }
624
28.5k
        Type sub0type = subs.size() > 0 ? subs[0].GetType() : ""_mst;
625
28.5k
        return internal::ComputeScriptLen(fragment, sub0type, subsize, k, subs.size(), keys.size(), m_script_ctx);
626
28.5k
    }
miniscript::Node<unsigned int>::CalcScriptLen() const
Line
Count
Source
619
1.72M
    {
620
1.72M
        size_t subsize = 0;
621
1.72M
        for (const auto& sub : subs) {
622
1.72M
            subsize += sub.ScriptSize();
623
1.72M
        }
624
1.72M
        Type sub0type = subs.size() > 0 ? subs[0].GetType() : ""_mst;
625
1.72M
        return internal::ComputeScriptLen(fragment, sub0type, subsize, k, subs.size(), keys.size(), m_script_ctx);
626
1.72M
    }
miniscript::Node<XOnlyPubKey>::CalcScriptLen() const
Line
Count
Source
619
1.32M
    {
620
1.32M
        size_t subsize = 0;
621
1.32M
        for (const auto& sub : subs) {
622
1.32M
            subsize += sub.ScriptSize();
623
1.32M
        }
624
1.32M
        Type sub0type = subs.size() > 0 ? subs[0].GetType() : ""_mst;
625
1.32M
        return internal::ComputeScriptLen(fragment, sub0type, subsize, k, subs.size(), keys.size(), m_script_ctx);
626
1.32M
    }
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
19.3k
    {
654
        /** Entries of the explicit stack tracked in this algorithm. */
655
19.3k
        struct StackElem
656
19.3k
        {
657
19.3k
            const Node& node; //!< The node being evaluated.
658
19.3k
            size_t expanded; //!< How many children of this node have been expanded.
659
19.3k
            State state; //!< The state for that node.
660
661
19.3k
            StackElem(const Node& node_, size_t exp_, State&& state_) :
662
10.8M
                node(node_), expanded(exp_), state(std::move(state_)) {}
miniscript_tests.cpp:std::optional<(anonymous namespace)::KeyConverter> miniscript::Node<CPubKey>::TreeEvalMaybe<CScript, bool, CScript miniscript::Node<CPubKey>::ToScript<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, unsigned long), (anonymous namespace)::KeyConverter miniscript::Node<CPubKey>::TreeEval<CScript, bool, CScript miniscript::Node<CPubKey>::ToScript<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, unsigned long)&, CScript miniscript::Node<CPubKey>::ToScript<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, std::span<CScript, 18446744073709551615ul>)>(bool, CScript miniscript::Node<CPubKey>::ToScript<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, unsigned long)&, CScript miniscript::Node<CPubKey>::ToScript<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, std::span<CScript, 18446744073709551615ul>)) const::'lambda'(bool&&, miniscript::Node<CPubKey> const&, std::span<CScript, 18446744073709551615ul>)>(bool, CScript miniscript::Node<CPubKey>::ToScript<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, unsigned long)&, CScript miniscript::Node<CPubKey>::ToScript<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, std::span<CScript, 18446744073709551615ul>)) const::StackElem::StackElem(miniscript::Node<CPubKey> const&, unsigned long, bool&&)
Line
Count
Source
662
25.4k
                node(node_), expanded(exp_), state(std::move(state_)) {}
miniscript_tests.cpp:std::optional<(anonymous namespace)::Satisfier> miniscript::Node<CPubKey>::TreeEvalMaybe<miniscript::internal::InputResult, (anonymous namespace)::Satisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, (anonymous namespace)::Satisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::Satisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long), (anonymous namespace)::Satisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::Satisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>), (anonymous namespace)::Satisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::Satisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long), (anonymous namespace)::Satisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::Satisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::StackElem::StackElem(miniscript::Node<CPubKey> const&, unsigned long, (anonymous namespace)::Satisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState&&)
Line
Count
Source
662
1.61M
                node(node_), expanded(exp_), state(std::move(state_)) {}
miniscript_tests.cpp:std::optional<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)> miniscript::Node<CPubKey>::TreeEvalMaybe<int, (anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&) miniscript::Node<CPubKey>::TreeEval<int, bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)>(bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)) const::DummyState, (anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&) miniscript::Node<CPubKey>::TreeEval<int, bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)>(bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&) miniscript::Node<CPubKey>::TreeEval<int, bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)>(bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long), (anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&) miniscript::Node<CPubKey>::TreeEval<int, bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)>(bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&) miniscript::Node<CPubKey>::TreeEval<int, bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)>(bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)>(bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>), (anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&) miniscript::Node<CPubKey>::TreeEval<int, bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)>(bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&) miniscript::Node<CPubKey>::TreeEval<int, bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)>(bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long), (anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&) miniscript::Node<CPubKey>::TreeEval<int, bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)>(bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&) miniscript::Node<CPubKey>::TreeEval<int, bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)>(bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)) const::StackElem::StackElem(miniscript::Node<CPubKey> const&, unsigned long, (anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&) miniscript::Node<CPubKey>::TreeEval<int, bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)>(bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)) const::DummyState&&)
Line
Count
Source
662
25.4k
                node(node_), expanded(exp_), state(std::move(state_)) {}
miniscript_tests.cpp:std::optional<(anonymous namespace)::KeyConverter> miniscript::Node<CPubKey>::TreeEvalMaybe<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, (anonymous namespace)::KeyConverter miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::DummyState, (anonymous namespace)::KeyConverter miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::KeyConverter miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long), (anonymous namespace)::KeyConverter miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::KeyConverter miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>), (anonymous namespace)::KeyConverter miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::KeyConverter miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long), (anonymous namespace)::KeyConverter miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::KeyConverter miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::StackElem::StackElem(miniscript::Node<CPubKey> const&, unsigned long, (anonymous namespace)::KeyConverter miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::DummyState&&)
Line
Count
Source
662
23.2k
                node(node_), expanded(exp_), state(std::move(state_)) {}
std::optional<miniscript::Node<CPubKey> const*> miniscript::Node<CPubKey>::TreeEvalMaybe<miniscript::Node<CPubKey> const*, miniscript::Node<CPubKey> const* miniscript::Node<CPubKey>::TreeEval<miniscript::Node<CPubKey> const*, miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)>(miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const* miniscript::Node<CPubKey>::TreeEval<miniscript::Node<CPubKey> const*, miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)>(miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)) const::'lambda'(miniscript::Node<CPubKey> const* miniscript::Node<CPubKey>::TreeEval<miniscript::Node<CPubKey> const*, miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)>(miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long), miniscript::Node<CPubKey> const* miniscript::Node<CPubKey>::TreeEval<miniscript::Node<CPubKey> const*, miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)>(miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)) const::'lambda'(miniscript::Node<CPubKey> const* miniscript::Node<CPubKey>::TreeEval<miniscript::Node<CPubKey> const*, miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)>(miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)>(miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>), miniscript::Node<CPubKey> const* miniscript::Node<CPubKey>::TreeEval<miniscript::Node<CPubKey> const*, miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)>(miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)) const::'lambda'(miniscript::Node<CPubKey> const* miniscript::Node<CPubKey>::TreeEval<miniscript::Node<CPubKey> const*, miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)>(miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long), miniscript::Node<CPubKey> const* miniscript::Node<CPubKey>::TreeEval<miniscript::Node<CPubKey> const*, miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)>(miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)) const::'lambda'(miniscript::Node<CPubKey> const* miniscript::Node<CPubKey>::TreeEval<miniscript::Node<CPubKey> const*, miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)>(miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)) const::StackElem::StackElem(miniscript::Node<CPubKey> const&, unsigned long, miniscript::Node<CPubKey> const* miniscript::Node<CPubKey>::TreeEval<miniscript::Node<CPubKey> const*, miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)>(miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)) const::DummyState&&)
Line
Count
Source
662
7
                node(node_), expanded(exp_), state(std::move(state_)) {}
miniscript_tests.cpp:std::optional<(anonymous namespace)::KeyConverter> miniscript::Node<CPubKey>::TreeEvalMaybe<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, bool, std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<CPubKey>::ToString<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&, bool&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, unsigned long), std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<CPubKey>::ToString<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&, bool&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, std::span<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, 18446744073709551615ul>)>(bool, std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<CPubKey>::ToString<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&, bool&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, unsigned long), std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<CPubKey>::ToString<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&, bool&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, std::span<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, 18446744073709551615ul>)) const::StackElem::StackElem(miniscript::Node<CPubKey> const&, unsigned long, bool&&)
Line
Count
Source
662
4
                node(node_), expanded(exp_), state(std::move(state_)) {}
std::optional<miniscript::Node<unsigned int>> miniscript::Node<unsigned int>::TreeEvalMaybe<miniscript::Node<unsigned int>, miniscript::Node<unsigned int> miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int>, miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int>, miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)) const::'lambda'(miniscript::Node<unsigned int> miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int>, miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, unsigned long), miniscript::Node<unsigned int> miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int>, miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)) const::'lambda'(miniscript::Node<unsigned int> miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int>, miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>), miniscript::Node<unsigned int> miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int>, miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)) const::'lambda'(miniscript::Node<unsigned int> miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int>, miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, unsigned long), miniscript::Node<unsigned int> miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int>, miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)) const::'lambda'(miniscript::Node<unsigned int> miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int>, miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)) const::StackElem::StackElem(miniscript::Node<unsigned int> const&, unsigned long, miniscript::Node<unsigned int> miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int>, miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)) const::DummyState&&)
Line
Count
Source
662
531k
                node(node_), expanded(exp_), state(std::move(state_)) {}
descriptor.cpp:std::optional<(anonymous namespace)::KeyParser> miniscript::Node<unsigned int>::TreeEvalMaybe<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, (anonymous namespace)::KeyParser miniscript::Node<unsigned int>::TreeEval<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)>(void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)) const::DummyState, (anonymous namespace)::KeyParser miniscript::Node<unsigned int>::TreeEval<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)>(void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::KeyParser miniscript::Node<unsigned int>::TreeEval<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)>(void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, unsigned long), (anonymous namespace)::KeyParser miniscript::Node<unsigned int>::TreeEval<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)>(void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::KeyParser miniscript::Node<unsigned int>::TreeEval<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)>(void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)>(void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>), (anonymous namespace)::KeyParser miniscript::Node<unsigned int>::TreeEval<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)>(void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::KeyParser miniscript::Node<unsigned int>::TreeEval<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)>(void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, unsigned long), (anonymous namespace)::KeyParser miniscript::Node<unsigned int>::TreeEval<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)>(void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::KeyParser miniscript::Node<unsigned int>::TreeEval<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)>(void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)) const::StackElem::StackElem(miniscript::Node<unsigned int> const&, unsigned long, (anonymous namespace)::KeyParser miniscript::Node<unsigned int>::TreeEval<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)>(void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)) const::DummyState&&)
Line
Count
Source
662
996k
                node(node_), expanded(exp_), state(std::move(state_)) {}
std::optional<miniscript::Node<unsigned int> const*> miniscript::Node<unsigned int>::TreeEvalMaybe<miniscript::Node<unsigned int> const*, miniscript::Node<unsigned int> const* miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int> const*, miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const* miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int> const*, miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)) const::'lambda'(miniscript::Node<unsigned int> const* miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int> const*, miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, unsigned long), miniscript::Node<unsigned int> const* miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int> const*, miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)) const::'lambda'(miniscript::Node<unsigned int> const* miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int> const*, miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>), miniscript::Node<unsigned int> const* miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int> const*, miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)) const::'lambda'(miniscript::Node<unsigned int> const* miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int> const*, miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, unsigned long), miniscript::Node<unsigned int> const* miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int> const*, miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)) const::'lambda'(miniscript::Node<unsigned int> const* miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int> const*, miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)) const::StackElem::StackElem(miniscript::Node<unsigned int> const&, unsigned long, miniscript::Node<unsigned int> const* miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int> const*, miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)) const::DummyState&&)
Line
Count
Source
662
119
                node(node_), expanded(exp_), state(std::move(state_)) {}
descriptor.cpp:std::optional<(anonymous namespace)::KeyParser> miniscript::Node<unsigned int>::TreeEvalMaybe<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, bool, std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<unsigned int>::ToString<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&, bool&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, unsigned long), std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<unsigned int>::ToString<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&, bool&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, std::span<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, 18446744073709551615ul>)>(bool, std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<unsigned int>::ToString<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&, bool&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, unsigned long), std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<unsigned int>::ToString<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&, bool&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, std::span<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, 18446744073709551615ul>)) const::StackElem::StackElem(miniscript::Node<unsigned int> const&, unsigned long, bool&&)
Line
Count
Source
662
91
                node(node_), expanded(exp_), state(std::move(state_)) {}
descriptor.cpp:std::optional<(anonymous namespace)::ScriptMaker> miniscript::Node<unsigned int>::TreeEvalMaybe<CScript, bool, CScript miniscript::Node<unsigned int>::ToScript<(anonymous namespace)::ScriptMaker>((anonymous namespace)::ScriptMaker const&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, unsigned long), (anonymous namespace)::ScriptMaker miniscript::Node<unsigned int>::TreeEval<CScript, bool, CScript miniscript::Node<unsigned int>::ToScript<(anonymous namespace)::ScriptMaker>((anonymous namespace)::ScriptMaker const&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, unsigned long)&, CScript miniscript::Node<unsigned int>::ToScript<(anonymous namespace)::ScriptMaker>((anonymous namespace)::ScriptMaker const&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, std::span<CScript, 18446744073709551615ul>)>(bool, CScript miniscript::Node<unsigned int>::ToScript<(anonymous namespace)::ScriptMaker>((anonymous namespace)::ScriptMaker const&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, unsigned long)&, CScript miniscript::Node<unsigned int>::ToScript<(anonymous namespace)::ScriptMaker>((anonymous namespace)::ScriptMaker const&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, std::span<CScript, 18446744073709551615ul>)) const::'lambda'(bool&&, miniscript::Node<unsigned int> const&, std::span<CScript, 18446744073709551615ul>)>(bool, CScript miniscript::Node<unsigned int>::ToScript<(anonymous namespace)::ScriptMaker>((anonymous namespace)::ScriptMaker const&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, unsigned long)&, CScript miniscript::Node<unsigned int>::ToScript<(anonymous namespace)::ScriptMaker>((anonymous namespace)::ScriptMaker const&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, std::span<CScript, 18446744073709551615ul>)) const::StackElem::StackElem(miniscript::Node<unsigned int> const&, unsigned long, bool&&)
Line
Count
Source
662
1.66M
                node(node_), expanded(exp_), state(std::move(state_)) {}
descriptor.cpp:std::optional<(anonymous namespace)::StringMaker> miniscript::Node<unsigned int>::TreeEvalMaybe<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, bool, std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<unsigned int>::ToString<(anonymous namespace)::StringMaker>((anonymous namespace)::StringMaker const&, bool&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, unsigned long), std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<unsigned int>::ToString<(anonymous namespace)::StringMaker>((anonymous namespace)::StringMaker const&, bool&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, std::span<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, 18446744073709551615ul>)>(bool, std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<unsigned int>::ToString<(anonymous namespace)::StringMaker>((anonymous namespace)::StringMaker const&, bool&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, unsigned long), std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<unsigned int>::ToString<(anonymous namespace)::StringMaker>((anonymous namespace)::StringMaker const&, bool&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, std::span<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, 18446744073709551615ul>)) const::StackElem::StackElem(miniscript::Node<unsigned int> const&, unsigned long, bool&&)
Line
Count
Source
662
3.30M
                node(node_), expanded(exp_), state(std::move(state_)) {}
std::optional<TapSatisfier> miniscript::Node<XOnlyPubKey>::TreeEvalMaybe<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)) const::DummyState, TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)) const::'lambda'(TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<XOnlyPubKey> const&, unsigned long), TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)) const::'lambda'(TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>), TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)) const::'lambda'(TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<XOnlyPubKey> const&, unsigned long), TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)) const::'lambda'(TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)) const::StackElem::StackElem(miniscript::Node<XOnlyPubKey> const&, unsigned long, TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)) const::DummyState&&)
Line
Count
Source
662
1.32M
                node(node_), expanded(exp_), state(std::move(state_)) {}
std::optional<TapSatisfier> miniscript::Node<XOnlyPubKey>::TreeEvalMaybe<miniscript::internal::InputResult, TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::'lambda'(TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<XOnlyPubKey> const&, unsigned long), TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::'lambda'(TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>), TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::'lambda'(TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<XOnlyPubKey> const&, unsigned long), TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::'lambda'(TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::StackElem::StackElem(miniscript::Node<XOnlyPubKey> const&, unsigned long, TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState&&)
Line
Count
Source
662
1.32M
                node(node_), expanded(exp_), state(std::move(state_)) {}
std::optional<WshSatisfier> miniscript::Node<CPubKey>::TreeEvalMaybe<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, WshSatisfier miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::DummyState, WshSatisfier miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::'lambda'(WshSatisfier miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long), WshSatisfier miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::'lambda'(WshSatisfier miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>), WshSatisfier miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::'lambda'(WshSatisfier miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long), WshSatisfier miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::'lambda'(WshSatisfier miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::StackElem::StackElem(miniscript::Node<CPubKey> const&, unsigned long, WshSatisfier miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::DummyState&&)
Line
Count
Source
662
3.28k
                node(node_), expanded(exp_), state(std::move(state_)) {}
std::optional<WshSatisfier> miniscript::Node<CPubKey>::TreeEvalMaybe<miniscript::internal::InputResult, WshSatisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, WshSatisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::'lambda'(WshSatisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long), WshSatisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::'lambda'(WshSatisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>), WshSatisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::'lambda'(WshSatisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long), WshSatisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::'lambda'(WshSatisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::StackElem::StackElem(miniscript::Node<CPubKey> const&, unsigned long, WshSatisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState&&)
Line
Count
Source
662
3.28k
                node(node_), expanded(exp_), state(std::move(state_)) {}
663
19.3k
        };
664
        /* Stack of tree nodes being explored. */
665
19.3k
        std::vector<StackElem> stack;
666
        /* Results of subtrees so far. Their order and mapping to tree nodes
667
         * is implicitly defined by stack. */
668
19.3k
        std::vector<Result> results;
669
19.3k
        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
21.7M
        while (stack.size()) {
688
21.6M
            const Node& node = stack.back().node;
689
21.6M
            if (stack.back().expanded < node.subs.size()) {
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
10.8M
                size_t child_index = stack.back().expanded++;
694
10.8M
                State child_state = downfn(stack.back().state, node, child_index);
695
10.8M
                stack.emplace_back(node.subs[child_index], 0, std::move(child_state));
696
10.8M
                continue;
697
10.8M
            }
698
            // Invoke upfn with the last node.subs.size() elements of results as input.
699
21.6M
            assert(results.size() >= node.subs.size());
700
10.8M
            std::optional<Result> result{upfn(std::move(stack.back().state), node,
701
10.8M
                std::span<Result>{results}.last(node.subs.size()))};
702
            // If evaluation returns std::nullopt, abort immediately.
703
10.8M
            if (!result) return {};
704
            // Replace the last node.subs.size() elements of results with the new result.
705
10.8M
            results.erase(results.end() - node.subs.size(), results.end());
706
10.8M
            results.push_back(std::move(*result));
707
10.8M
            stack.pop_back();
708
10.8M
        }
709
        // The final remaining results element is the root result, return it.
710
19.3k
        assert(results.size() >= 1);
711
19.3k
        CHECK_NONFATAL(results.size() == 1);
712
19.3k
        return std::move(results[0]);
713
19.3k
    }
miniscript_tests.cpp:std::optional<(anonymous namespace)::KeyConverter> miniscript::Node<CPubKey>::TreeEvalMaybe<CScript, bool, CScript miniscript::Node<CPubKey>::ToScript<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, unsigned long), (anonymous namespace)::KeyConverter miniscript::Node<CPubKey>::TreeEval<CScript, bool, CScript miniscript::Node<CPubKey>::ToScript<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, unsigned long)&, CScript miniscript::Node<CPubKey>::ToScript<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, std::span<CScript, 18446744073709551615ul>)>(bool, CScript miniscript::Node<CPubKey>::ToScript<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, unsigned long)&, CScript miniscript::Node<CPubKey>::ToScript<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, std::span<CScript, 18446744073709551615ul>)) const::'lambda'(bool&&, miniscript::Node<CPubKey> const&, std::span<CScript, 18446744073709551615ul>)>(bool, CScript miniscript::Node<CPubKey>::ToScript<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, unsigned long)&, CScript miniscript::Node<CPubKey>::ToScript<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, std::span<CScript, 18446744073709551615ul>)) const
Line
Count
Source
653
375
    {
654
        /** Entries of the explicit stack tracked in this algorithm. */
655
375
        struct StackElem
656
375
        {
657
375
            const Node& node; //!< The node being evaluated.
658
375
            size_t expanded; //!< How many children of this node have been expanded.
659
375
            State state; //!< The state for that node.
660
661
375
            StackElem(const Node& node_, size_t exp_, State&& state_) :
662
375
                node(node_), expanded(exp_), state(std::move(state_)) {}
663
375
        };
664
        /* Stack of tree nodes being explored. */
665
375
        std::vector<StackElem> stack;
666
        /* Results of subtrees so far. Their order and mapping to tree nodes
667
         * is implicitly defined by stack. */
668
375
        std::vector<Result> results;
669
375
        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
50.8k
        while (stack.size()) {
688
50.4k
            const Node& node = stack.back().node;
689
50.4k
            if (stack.back().expanded < node.subs.size()) {
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
25.0k
                size_t child_index = stack.back().expanded++;
694
25.0k
                State child_state = downfn(stack.back().state, node, child_index);
695
25.0k
                stack.emplace_back(node.subs[child_index], 0, std::move(child_state));
696
25.0k
                continue;
697
25.0k
            }
698
            // Invoke upfn with the last node.subs.size() elements of results as input.
699
50.4k
            assert(results.size() >= node.subs.size());
700
25.4k
            std::optional<Result> result{upfn(std::move(stack.back().state), node,
701
25.4k
                std::span<Result>{results}.last(node.subs.size()))};
702
            // If evaluation returns std::nullopt, abort immediately.
703
25.4k
            if (!result) return {};
704
            // Replace the last node.subs.size() elements of results with the new result.
705
25.4k
            results.erase(results.end() - node.subs.size(), results.end());
706
25.4k
            results.push_back(std::move(*result));
707
25.4k
            stack.pop_back();
708
25.4k
        }
709
        // The final remaining results element is the root result, return it.
710
375
        assert(results.size() >= 1);
711
375
        CHECK_NONFATAL(results.size() == 1);
712
375
        return std::move(results[0]);
713
375
    }
miniscript_tests.cpp:std::optional<(anonymous namespace)::Satisfier> miniscript::Node<CPubKey>::TreeEvalMaybe<miniscript::internal::InputResult, (anonymous namespace)::Satisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, (anonymous namespace)::Satisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::Satisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long), (anonymous namespace)::Satisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::Satisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>), (anonymous namespace)::Satisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::Satisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long), (anonymous namespace)::Satisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::Satisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const
Line
Count
Source
653
4.82k
    {
654
        /** Entries of the explicit stack tracked in this algorithm. */
655
4.82k
        struct StackElem
656
4.82k
        {
657
4.82k
            const Node& node; //!< The node being evaluated.
658
4.82k
            size_t expanded; //!< How many children of this node have been expanded.
659
4.82k
            State state; //!< The state for that node.
660
661
4.82k
            StackElem(const Node& node_, size_t exp_, State&& state_) :
662
4.82k
                node(node_), expanded(exp_), state(std::move(state_)) {}
663
4.82k
        };
664
        /* Stack of tree nodes being explored. */
665
4.82k
        std::vector<StackElem> stack;
666
        /* Results of subtrees so far. Their order and mapping to tree nodes
667
         * is implicitly defined by stack. */
668
4.82k
        std::vector<Result> results;
669
4.82k
        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
3.23M
        while (stack.size()) {
688
3.22M
            const Node& node = stack.back().node;
689
3.22M
            if (stack.back().expanded < node.subs.size()) {
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
1.61M
                size_t child_index = stack.back().expanded++;
694
1.61M
                State child_state = downfn(stack.back().state, node, child_index);
695
1.61M
                stack.emplace_back(node.subs[child_index], 0, std::move(child_state));
696
1.61M
                continue;
697
1.61M
            }
698
            // Invoke upfn with the last node.subs.size() elements of results as input.
699
3.22M
            assert(results.size() >= node.subs.size());
700
1.61M
            std::optional<Result> result{upfn(std::move(stack.back().state), node,
701
1.61M
                std::span<Result>{results}.last(node.subs.size()))};
702
            // If evaluation returns std::nullopt, abort immediately.
703
1.61M
            if (!result) return {};
704
            // Replace the last node.subs.size() elements of results with the new result.
705
1.61M
            results.erase(results.end() - node.subs.size(), results.end());
706
1.61M
            results.push_back(std::move(*result));
707
1.61M
            stack.pop_back();
708
1.61M
        }
709
        // The final remaining results element is the root result, return it.
710
4.82k
        assert(results.size() >= 1);
711
4.82k
        CHECK_NONFATAL(results.size() == 1);
712
4.82k
        return std::move(results[0]);
713
4.82k
    }
miniscript_tests.cpp:std::optional<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)> miniscript::Node<CPubKey>::TreeEvalMaybe<int, (anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&) miniscript::Node<CPubKey>::TreeEval<int, bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)>(bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)) const::DummyState, (anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&) miniscript::Node<CPubKey>::TreeEval<int, bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)>(bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&) miniscript::Node<CPubKey>::TreeEval<int, bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)>(bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long), (anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&) miniscript::Node<CPubKey>::TreeEval<int, bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)>(bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&) miniscript::Node<CPubKey>::TreeEval<int, bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)>(bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)>(bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>), (anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&) miniscript::Node<CPubKey>::TreeEval<int, bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)>(bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&) miniscript::Node<CPubKey>::TreeEval<int, bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)>(bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long), (anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&) miniscript::Node<CPubKey>::TreeEval<int, bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)>(bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&) miniscript::Node<CPubKey>::TreeEval<int, bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)>(bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)) const
Line
Count
Source
653
375
    {
654
        /** Entries of the explicit stack tracked in this algorithm. */
655
375
        struct StackElem
656
375
        {
657
375
            const Node& node; //!< The node being evaluated.
658
375
            size_t expanded; //!< How many children of this node have been expanded.
659
375
            State state; //!< The state for that node.
660
661
375
            StackElem(const Node& node_, size_t exp_, State&& state_) :
662
375
                node(node_), expanded(exp_), state(std::move(state_)) {}
663
375
        };
664
        /* Stack of tree nodes being explored. */
665
375
        std::vector<StackElem> stack;
666
        /* Results of subtrees so far. Their order and mapping to tree nodes
667
         * is implicitly defined by stack. */
668
375
        std::vector<Result> results;
669
375
        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
50.8k
        while (stack.size()) {
688
50.4k
            const Node& node = stack.back().node;
689
50.4k
            if (stack.back().expanded < node.subs.size()) {
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
25.0k
                size_t child_index = stack.back().expanded++;
694
25.0k
                State child_state = downfn(stack.back().state, node, child_index);
695
25.0k
                stack.emplace_back(node.subs[child_index], 0, std::move(child_state));
696
25.0k
                continue;
697
25.0k
            }
698
            // Invoke upfn with the last node.subs.size() elements of results as input.
699
50.4k
            assert(results.size() >= node.subs.size());
700
25.4k
            std::optional<Result> result{upfn(std::move(stack.back().state), node,
701
25.4k
                std::span<Result>{results}.last(node.subs.size()))};
702
            // If evaluation returns std::nullopt, abort immediately.
703
25.4k
            if (!result) return {};
704
            // Replace the last node.subs.size() elements of results with the new result.
705
25.4k
            results.erase(results.end() - node.subs.size(), results.end());
706
25.4k
            results.push_back(std::move(*result));
707
25.4k
            stack.pop_back();
708
25.4k
        }
709
        // The final remaining results element is the root result, return it.
710
375
        assert(results.size() >= 1);
711
375
        CHECK_NONFATAL(results.size() == 1);
712
375
        return std::move(results[0]);
713
375
    }
miniscript_tests.cpp:std::optional<(anonymous namespace)::KeyConverter> miniscript::Node<CPubKey>::TreeEvalMaybe<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, (anonymous namespace)::KeyConverter miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::DummyState, (anonymous namespace)::KeyConverter miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::KeyConverter miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long), (anonymous namespace)::KeyConverter miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::KeyConverter miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>), (anonymous namespace)::KeyConverter miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::KeyConverter miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long), (anonymous namespace)::KeyConverter miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::KeyConverter miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const
Line
Count
Source
653
313
    {
654
        /** Entries of the explicit stack tracked in this algorithm. */
655
313
        struct StackElem
656
313
        {
657
313
            const Node& node; //!< The node being evaluated.
658
313
            size_t expanded; //!< How many children of this node have been expanded.
659
313
            State state; //!< The state for that node.
660
661
313
            StackElem(const Node& node_, size_t exp_, State&& state_) :
662
313
                node(node_), expanded(exp_), state(std::move(state_)) {}
663
313
        };
664
        /* Stack of tree nodes being explored. */
665
313
        std::vector<StackElem> stack;
666
        /* Results of subtrees so far. Their order and mapping to tree nodes
667
         * is implicitly defined by stack. */
668
313
        std::vector<Result> results;
669
313
        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
46.5k
        while (stack.size()) {
688
46.1k
            const Node& node = stack.back().node;
689
46.1k
            if (stack.back().expanded < node.subs.size()) {
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
22.9k
                size_t child_index = stack.back().expanded++;
694
22.9k
                State child_state = downfn(stack.back().state, node, child_index);
695
22.9k
                stack.emplace_back(node.subs[child_index], 0, std::move(child_state));
696
22.9k
                continue;
697
22.9k
            }
698
            // Invoke upfn with the last node.subs.size() elements of results as input.
699
46.1k
            assert(results.size() >= node.subs.size());
700
23.2k
            std::optional<Result> result{upfn(std::move(stack.back().state), node,
701
23.2k
                std::span<Result>{results}.last(node.subs.size()))};
702
            // If evaluation returns std::nullopt, abort immediately.
703
23.2k
            if (!result) return {};
704
            // Replace the last node.subs.size() elements of results with the new result.
705
23.2k
            results.erase(results.end() - node.subs.size(), results.end());
706
23.2k
            results.push_back(std::move(*result));
707
23.2k
            stack.pop_back();
708
23.2k
        }
709
        // The final remaining results element is the root result, return it.
710
313
        assert(results.size() >= 1);
711
313
        CHECK_NONFATAL(results.size() == 1);
712
313
        return std::move(results[0]);
713
313
    }
std::optional<miniscript::Node<CPubKey> const*> miniscript::Node<CPubKey>::TreeEvalMaybe<miniscript::Node<CPubKey> const*, miniscript::Node<CPubKey> const* miniscript::Node<CPubKey>::TreeEval<miniscript::Node<CPubKey> const*, miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)>(miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const* miniscript::Node<CPubKey>::TreeEval<miniscript::Node<CPubKey> const*, miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)>(miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)) const::'lambda'(miniscript::Node<CPubKey> const* miniscript::Node<CPubKey>::TreeEval<miniscript::Node<CPubKey> const*, miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)>(miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long), miniscript::Node<CPubKey> const* miniscript::Node<CPubKey>::TreeEval<miniscript::Node<CPubKey> const*, miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)>(miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)) const::'lambda'(miniscript::Node<CPubKey> const* miniscript::Node<CPubKey>::TreeEval<miniscript::Node<CPubKey> const*, miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)>(miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)>(miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>), miniscript::Node<CPubKey> const* miniscript::Node<CPubKey>::TreeEval<miniscript::Node<CPubKey> const*, miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)>(miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)) const::'lambda'(miniscript::Node<CPubKey> const* miniscript::Node<CPubKey>::TreeEval<miniscript::Node<CPubKey> const*, miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)>(miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long), miniscript::Node<CPubKey> const* miniscript::Node<CPubKey>::TreeEval<miniscript::Node<CPubKey> const*, miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)>(miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)) const::'lambda'(miniscript::Node<CPubKey> const* miniscript::Node<CPubKey>::TreeEval<miniscript::Node<CPubKey> const*, miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)>(miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)) const
Line
Count
Source
653
1
    {
654
        /** Entries of the explicit stack tracked in this algorithm. */
655
1
        struct StackElem
656
1
        {
657
1
            const Node& node; //!< The node being evaluated.
658
1
            size_t expanded; //!< How many children of this node have been expanded.
659
1
            State state; //!< The state for that node.
660
661
1
            StackElem(const Node& node_, size_t exp_, State&& state_) :
662
1
                node(node_), expanded(exp_), state(std::move(state_)) {}
663
1
        };
664
        /* Stack of tree nodes being explored. */
665
1
        std::vector<StackElem> stack;
666
        /* Results of subtrees so far. Their order and mapping to tree nodes
667
         * is implicitly defined by stack. */
668
1
        std::vector<Result> results;
669
1
        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
14
        while (stack.size()) {
688
13
            const Node& node = stack.back().node;
689
13
            if (stack.back().expanded < node.subs.size()) {
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
6
                size_t child_index = stack.back().expanded++;
694
6
                State child_state = downfn(stack.back().state, node, child_index);
695
6
                stack.emplace_back(node.subs[child_index], 0, std::move(child_state));
696
6
                continue;
697
6
            }
698
            // Invoke upfn with the last node.subs.size() elements of results as input.
699
13
            assert(results.size() >= node.subs.size());
700
7
            std::optional<Result> result{upfn(std::move(stack.back().state), node,
701
7
                std::span<Result>{results}.last(node.subs.size()))};
702
            // If evaluation returns std::nullopt, abort immediately.
703
7
            if (!result) return {};
704
            // Replace the last node.subs.size() elements of results with the new result.
705
7
            results.erase(results.end() - node.subs.size(), results.end());
706
7
            results.push_back(std::move(*result));
707
7
            stack.pop_back();
708
7
        }
709
        // The final remaining results element is the root result, return it.
710
1
        assert(results.size() >= 1);
711
1
        CHECK_NONFATAL(results.size() == 1);
712
1
        return std::move(results[0]);
713
1
    }
miniscript_tests.cpp:std::optional<(anonymous namespace)::KeyConverter> miniscript::Node<CPubKey>::TreeEvalMaybe<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, bool, std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<CPubKey>::ToString<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&, bool&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, unsigned long), std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<CPubKey>::ToString<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&, bool&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, std::span<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, 18446744073709551615ul>)>(bool, std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<CPubKey>::ToString<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&, bool&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, unsigned long), std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<CPubKey>::ToString<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&, bool&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, std::span<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, 18446744073709551615ul>)) const
Line
Count
Source
653
1
    {
654
        /** Entries of the explicit stack tracked in this algorithm. */
655
1
        struct StackElem
656
1
        {
657
1
            const Node& node; //!< The node being evaluated.
658
1
            size_t expanded; //!< How many children of this node have been expanded.
659
1
            State state; //!< The state for that node.
660
661
1
            StackElem(const Node& node_, size_t exp_, State&& state_) :
662
1
                node(node_), expanded(exp_), state(std::move(state_)) {}
663
1
        };
664
        /* Stack of tree nodes being explored. */
665
1
        std::vector<StackElem> stack;
666
        /* Results of subtrees so far. Their order and mapping to tree nodes
667
         * is implicitly defined by stack. */
668
1
        std::vector<Result> results;
669
1
        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
8
        while (stack.size()) {
688
7
            const Node& node = stack.back().node;
689
7
            if (stack.back().expanded < node.subs.size()) {
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
3
                size_t child_index = stack.back().expanded++;
694
3
                State child_state = downfn(stack.back().state, node, child_index);
695
3
                stack.emplace_back(node.subs[child_index], 0, std::move(child_state));
696
3
                continue;
697
3
            }
698
            // Invoke upfn with the last node.subs.size() elements of results as input.
699
7
            assert(results.size() >= node.subs.size());
700
4
            std::optional<Result> result{upfn(std::move(stack.back().state), node,
701
4
                std::span<Result>{results}.last(node.subs.size()))};
702
            // If evaluation returns std::nullopt, abort immediately.
703
4
            if (!result) return {};
704
            // Replace the last node.subs.size() elements of results with the new result.
705
4
            results.erase(results.end() - node.subs.size(), results.end());
706
4
            results.push_back(std::move(*result));
707
4
            stack.pop_back();
708
4
        }
709
        // The final remaining results element is the root result, return it.
710
1
        assert(results.size() >= 1);
711
1
        CHECK_NONFATAL(results.size() == 1);
712
1
        return std::move(results[0]);
713
1
    }
std::optional<miniscript::Node<unsigned int>> miniscript::Node<unsigned int>::TreeEvalMaybe<miniscript::Node<unsigned int>, miniscript::Node<unsigned int> miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int>, miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int>, miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)) const::'lambda'(miniscript::Node<unsigned int> miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int>, miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, unsigned long), miniscript::Node<unsigned int> miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int>, miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)) const::'lambda'(miniscript::Node<unsigned int> miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int>, miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>), miniscript::Node<unsigned int> miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int>, miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)) const::'lambda'(miniscript::Node<unsigned int> miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int>, miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, unsigned long), miniscript::Node<unsigned int> miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int>, miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)) const::'lambda'(miniscript::Node<unsigned int> miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int>, miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)) const
Line
Count
Source
653
223
    {
654
        /** Entries of the explicit stack tracked in this algorithm. */
655
223
        struct StackElem
656
223
        {
657
223
            const Node& node; //!< The node being evaluated.
658
223
            size_t expanded; //!< How many children of this node have been expanded.
659
223
            State state; //!< The state for that node.
660
661
223
            StackElem(const Node& node_, size_t exp_, State&& state_) :
662
223
                node(node_), expanded(exp_), state(std::move(state_)) {}
663
223
        };
664
        /* Stack of tree nodes being explored. */
665
223
        std::vector<StackElem> stack;
666
        /* Results of subtrees so far. Their order and mapping to tree nodes
667
         * is implicitly defined by stack. */
668
223
        std::vector<Result> results;
669
223
        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
1.06M
        while (stack.size()) {
688
1.06M
            const Node& node = stack.back().node;
689
1.06M
            if (stack.back().expanded < node.subs.size()) {
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
531k
                size_t child_index = stack.back().expanded++;
694
531k
                State child_state = downfn(stack.back().state, node, child_index);
695
531k
                stack.emplace_back(node.subs[child_index], 0, std::move(child_state));
696
531k
                continue;
697
531k
            }
698
            // Invoke upfn with the last node.subs.size() elements of results as input.
699
1.06M
            assert(results.size() >= node.subs.size());
700
531k
            std::optional<Result> result{upfn(std::move(stack.back().state), node,
701
531k
                std::span<Result>{results}.last(node.subs.size()))};
702
            // If evaluation returns std::nullopt, abort immediately.
703
531k
            if (!result) return {};
704
            // Replace the last node.subs.size() elements of results with the new result.
705
531k
            results.erase(results.end() - node.subs.size(), results.end());
706
531k
            results.push_back(std::move(*result));
707
531k
            stack.pop_back();
708
531k
        }
709
        // The final remaining results element is the root result, return it.
710
223
        assert(results.size() >= 1);
711
223
        CHECK_NONFATAL(results.size() == 1);
712
223
        return std::move(results[0]);
713
223
    }
descriptor.cpp:std::optional<(anonymous namespace)::KeyParser> miniscript::Node<unsigned int>::TreeEvalMaybe<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, (anonymous namespace)::KeyParser miniscript::Node<unsigned int>::TreeEval<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)>(void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)) const::DummyState, (anonymous namespace)::KeyParser miniscript::Node<unsigned int>::TreeEval<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)>(void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::KeyParser miniscript::Node<unsigned int>::TreeEval<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)>(void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, unsigned long), (anonymous namespace)::KeyParser miniscript::Node<unsigned int>::TreeEval<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)>(void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::KeyParser miniscript::Node<unsigned int>::TreeEval<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)>(void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)>(void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>), (anonymous namespace)::KeyParser miniscript::Node<unsigned int>::TreeEval<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)>(void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::KeyParser miniscript::Node<unsigned int>::TreeEval<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)>(void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, unsigned long), (anonymous namespace)::KeyParser miniscript::Node<unsigned int>::TreeEval<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)>(void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::KeyParser miniscript::Node<unsigned int>::TreeEval<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)>(void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)) const
Line
Count
Source
653
901
    {
654
        /** Entries of the explicit stack tracked in this algorithm. */
655
901
        struct StackElem
656
901
        {
657
901
            const Node& node; //!< The node being evaluated.
658
901
            size_t expanded; //!< How many children of this node have been expanded.
659
901
            State state; //!< The state for that node.
660
661
901
            StackElem(const Node& node_, size_t exp_, State&& state_) :
662
901
                node(node_), expanded(exp_), state(std::move(state_)) {}
663
901
        };
664
        /* Stack of tree nodes being explored. */
665
901
        std::vector<StackElem> stack;
666
        /* Results of subtrees so far. Their order and mapping to tree nodes
667
         * is implicitly defined by stack. */
668
901
        std::vector<Result> results;
669
901
        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
1.99M
        while (stack.size()) {
688
1.99M
            const Node& node = stack.back().node;
689
1.99M
            if (stack.back().expanded < node.subs.size()) {
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
995k
                size_t child_index = stack.back().expanded++;
694
995k
                State child_state = downfn(stack.back().state, node, child_index);
695
995k
                stack.emplace_back(node.subs[child_index], 0, std::move(child_state));
696
995k
                continue;
697
995k
            }
698
            // Invoke upfn with the last node.subs.size() elements of results as input.
699
1.99M
            assert(results.size() >= node.subs.size());
700
996k
            std::optional<Result> result{upfn(std::move(stack.back().state), node,
701
996k
                std::span<Result>{results}.last(node.subs.size()))};
702
            // If evaluation returns std::nullopt, abort immediately.
703
996k
            if (!result) return {};
704
            // Replace the last node.subs.size() elements of results with the new result.
705
996k
            results.erase(results.end() - node.subs.size(), results.end());
706
996k
            results.push_back(std::move(*result));
707
996k
            stack.pop_back();
708
996k
        }
709
        // The final remaining results element is the root result, return it.
710
901
        assert(results.size() >= 1);
711
901
        CHECK_NONFATAL(results.size() == 1);
712
901
        return std::move(results[0]);
713
901
    }
std::optional<miniscript::Node<unsigned int> const*> miniscript::Node<unsigned int>::TreeEvalMaybe<miniscript::Node<unsigned int> const*, miniscript::Node<unsigned int> const* miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int> const*, miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const* miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int> const*, miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)) const::'lambda'(miniscript::Node<unsigned int> const* miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int> const*, miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, unsigned long), miniscript::Node<unsigned int> const* miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int> const*, miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)) const::'lambda'(miniscript::Node<unsigned int> const* miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int> const*, miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>), miniscript::Node<unsigned int> const* miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int> const*, miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)) const::'lambda'(miniscript::Node<unsigned int> const* miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int> const*, miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, unsigned long), miniscript::Node<unsigned int> const* miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int> const*, miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)) const::'lambda'(miniscript::Node<unsigned int> const* miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int> const*, miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)) const
Line
Count
Source
653
16
    {
654
        /** Entries of the explicit stack tracked in this algorithm. */
655
16
        struct StackElem
656
16
        {
657
16
            const Node& node; //!< The node being evaluated.
658
16
            size_t expanded; //!< How many children of this node have been expanded.
659
16
            State state; //!< The state for that node.
660
661
16
            StackElem(const Node& node_, size_t exp_, State&& state_) :
662
16
                node(node_), expanded(exp_), state(std::move(state_)) {}
663
16
        };
664
        /* Stack of tree nodes being explored. */
665
16
        std::vector<StackElem> stack;
666
        /* Results of subtrees so far. Their order and mapping to tree nodes
667
         * is implicitly defined by stack. */
668
16
        std::vector<Result> results;
669
16
        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
238
        while (stack.size()) {
688
222
            const Node& node = stack.back().node;
689
222
            if (stack.back().expanded < node.subs.size()) {
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
103
                size_t child_index = stack.back().expanded++;
694
103
                State child_state = downfn(stack.back().state, node, child_index);
695
103
                stack.emplace_back(node.subs[child_index], 0, std::move(child_state));
696
103
                continue;
697
103
            }
698
            // Invoke upfn with the last node.subs.size() elements of results as input.
699
222
            assert(results.size() >= node.subs.size());
700
119
            std::optional<Result> result{upfn(std::move(stack.back().state), node,
701
119
                std::span<Result>{results}.last(node.subs.size()))};
702
            // If evaluation returns std::nullopt, abort immediately.
703
119
            if (!result) return {};
704
            // Replace the last node.subs.size() elements of results with the new result.
705
119
            results.erase(results.end() - node.subs.size(), results.end());
706
119
            results.push_back(std::move(*result));
707
119
            stack.pop_back();
708
119
        }
709
        // The final remaining results element is the root result, return it.
710
16
        assert(results.size() >= 1);
711
16
        CHECK_NONFATAL(results.size() == 1);
712
16
        return std::move(results[0]);
713
16
    }
descriptor.cpp:std::optional<(anonymous namespace)::KeyParser> miniscript::Node<unsigned int>::TreeEvalMaybe<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, bool, std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<unsigned int>::ToString<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&, bool&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, unsigned long), std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<unsigned int>::ToString<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&, bool&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, std::span<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, 18446744073709551615ul>)>(bool, std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<unsigned int>::ToString<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&, bool&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, unsigned long), std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<unsigned int>::ToString<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&, bool&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, std::span<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, 18446744073709551615ul>)) const
Line
Count
Source
653
16
    {
654
        /** Entries of the explicit stack tracked in this algorithm. */
655
16
        struct StackElem
656
16
        {
657
16
            const Node& node; //!< The node being evaluated.
658
16
            size_t expanded; //!< How many children of this node have been expanded.
659
16
            State state; //!< The state for that node.
660
661
16
            StackElem(const Node& node_, size_t exp_, State&& state_) :
662
16
                node(node_), expanded(exp_), state(std::move(state_)) {}
663
16
        };
664
        /* Stack of tree nodes being explored. */
665
16
        std::vector<StackElem> stack;
666
        /* Results of subtrees so far. Their order and mapping to tree nodes
667
         * is implicitly defined by stack. */
668
16
        std::vector<Result> results;
669
16
        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
182
        while (stack.size()) {
688
166
            const Node& node = stack.back().node;
689
166
            if (stack.back().expanded < node.subs.size()) {
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
75
                size_t child_index = stack.back().expanded++;
694
75
                State child_state = downfn(stack.back().state, node, child_index);
695
75
                stack.emplace_back(node.subs[child_index], 0, std::move(child_state));
696
75
                continue;
697
75
            }
698
            // Invoke upfn with the last node.subs.size() elements of results as input.
699
166
            assert(results.size() >= node.subs.size());
700
91
            std::optional<Result> result{upfn(std::move(stack.back().state), node,
701
91
                std::span<Result>{results}.last(node.subs.size()))};
702
            // If evaluation returns std::nullopt, abort immediately.
703
91
            if (!result) return {};
704
            // Replace the last node.subs.size() elements of results with the new result.
705
91
            results.erase(results.end() - node.subs.size(), results.end());
706
91
            results.push_back(std::move(*result));
707
91
            stack.pop_back();
708
91
        }
709
        // The final remaining results element is the root result, return it.
710
16
        assert(results.size() >= 1);
711
16
        CHECK_NONFATAL(results.size() == 1);
712
16
        return std::move(results[0]);
713
16
    }
descriptor.cpp:std::optional<(anonymous namespace)::ScriptMaker> miniscript::Node<unsigned int>::TreeEvalMaybe<CScript, bool, CScript miniscript::Node<unsigned int>::ToScript<(anonymous namespace)::ScriptMaker>((anonymous namespace)::ScriptMaker const&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, unsigned long), (anonymous namespace)::ScriptMaker miniscript::Node<unsigned int>::TreeEval<CScript, bool, CScript miniscript::Node<unsigned int>::ToScript<(anonymous namespace)::ScriptMaker>((anonymous namespace)::ScriptMaker const&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, unsigned long)&, CScript miniscript::Node<unsigned int>::ToScript<(anonymous namespace)::ScriptMaker>((anonymous namespace)::ScriptMaker const&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, std::span<CScript, 18446744073709551615ul>)>(bool, CScript miniscript::Node<unsigned int>::ToScript<(anonymous namespace)::ScriptMaker>((anonymous namespace)::ScriptMaker const&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, unsigned long)&, CScript miniscript::Node<unsigned int>::ToScript<(anonymous namespace)::ScriptMaker>((anonymous namespace)::ScriptMaker const&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, std::span<CScript, 18446744073709551615ul>)) const::'lambda'(bool&&, miniscript::Node<unsigned int> const&, std::span<CScript, 18446744073709551615ul>)>(bool, CScript miniscript::Node<unsigned int>::ToScript<(anonymous namespace)::ScriptMaker>((anonymous namespace)::ScriptMaker const&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, unsigned long)&, CScript miniscript::Node<unsigned int>::ToScript<(anonymous namespace)::ScriptMaker>((anonymous namespace)::ScriptMaker const&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, std::span<CScript, 18446744073709551615ul>)) const
Line
Count
Source
653
1.69k
    {
654
        /** Entries of the explicit stack tracked in this algorithm. */
655
1.69k
        struct StackElem
656
1.69k
        {
657
1.69k
            const Node& node; //!< The node being evaluated.
658
1.69k
            size_t expanded; //!< How many children of this node have been expanded.
659
1.69k
            State state; //!< The state for that node.
660
661
1.69k
            StackElem(const Node& node_, size_t exp_, State&& state_) :
662
1.69k
                node(node_), expanded(exp_), state(std::move(state_)) {}
663
1.69k
        };
664
        /* Stack of tree nodes being explored. */
665
1.69k
        std::vector<StackElem> stack;
666
        /* Results of subtrees so far. Their order and mapping to tree nodes
667
         * is implicitly defined by stack. */
668
1.69k
        std::vector<Result> results;
669
1.69k
        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
3.32M
        while (stack.size()) {
688
3.32M
            const Node& node = stack.back().node;
689
3.32M
            if (stack.back().expanded < node.subs.size()) {
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
1.66M
                size_t child_index = stack.back().expanded++;
694
1.66M
                State child_state = downfn(stack.back().state, node, child_index);
695
1.66M
                stack.emplace_back(node.subs[child_index], 0, std::move(child_state));
696
1.66M
                continue;
697
1.66M
            }
698
            // Invoke upfn with the last node.subs.size() elements of results as input.
699
3.32M
            assert(results.size() >= node.subs.size());
700
1.66M
            std::optional<Result> result{upfn(std::move(stack.back().state), node,
701
1.66M
                std::span<Result>{results}.last(node.subs.size()))};
702
            // If evaluation returns std::nullopt, abort immediately.
703
1.66M
            if (!result) return {};
704
            // Replace the last node.subs.size() elements of results with the new result.
705
1.66M
            results.erase(results.end() - node.subs.size(), results.end());
706
1.66M
            results.push_back(std::move(*result));
707
1.66M
            stack.pop_back();
708
1.66M
        }
709
        // The final remaining results element is the root result, return it.
710
1.69k
        assert(results.size() >= 1);
711
1.69k
        CHECK_NONFATAL(results.size() == 1);
712
1.69k
        return std::move(results[0]);
713
1.69k
    }
descriptor.cpp:std::optional<(anonymous namespace)::StringMaker> miniscript::Node<unsigned int>::TreeEvalMaybe<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, bool, std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<unsigned int>::ToString<(anonymous namespace)::StringMaker>((anonymous namespace)::StringMaker const&, bool&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, unsigned long), std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<unsigned int>::ToString<(anonymous namespace)::StringMaker>((anonymous namespace)::StringMaker const&, bool&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, std::span<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, 18446744073709551615ul>)>(bool, std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<unsigned int>::ToString<(anonymous namespace)::StringMaker>((anonymous namespace)::StringMaker const&, bool&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, unsigned long), std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<unsigned int>::ToString<(anonymous namespace)::StringMaker>((anonymous namespace)::StringMaker const&, bool&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, std::span<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, 18446744073709551615ul>)) const
Line
Count
Source
653
1.34k
    {
654
        /** Entries of the explicit stack tracked in this algorithm. */
655
1.34k
        struct StackElem
656
1.34k
        {
657
1.34k
            const Node& node; //!< The node being evaluated.
658
1.34k
            size_t expanded; //!< How many children of this node have been expanded.
659
1.34k
            State state; //!< The state for that node.
660
661
1.34k
            StackElem(const Node& node_, size_t exp_, State&& state_) :
662
1.34k
                node(node_), expanded(exp_), state(std::move(state_)) {}
663
1.34k
        };
664
        /* Stack of tree nodes being explored. */
665
1.34k
        std::vector<StackElem> stack;
666
        /* Results of subtrees so far. Their order and mapping to tree nodes
667
         * is implicitly defined by stack. */
668
1.34k
        std::vector<Result> results;
669
1.34k
        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
6.61M
        while (stack.size()) {
688
6.61M
            const Node& node = stack.back().node;
689
6.61M
            if (stack.back().expanded < node.subs.size()) {
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
3.30M
                size_t child_index = stack.back().expanded++;
694
3.30M
                State child_state = downfn(stack.back().state, node, child_index);
695
3.30M
                stack.emplace_back(node.subs[child_index], 0, std::move(child_state));
696
3.30M
                continue;
697
3.30M
            }
698
            // Invoke upfn with the last node.subs.size() elements of results as input.
699
6.61M
            assert(results.size() >= node.subs.size());
700
3.30M
            std::optional<Result> result{upfn(std::move(stack.back().state), node,
701
3.30M
                std::span<Result>{results}.last(node.subs.size()))};
702
            // If evaluation returns std::nullopt, abort immediately.
703
3.30M
            if (!result) return {};
704
            // Replace the last node.subs.size() elements of results with the new result.
705
3.30M
            results.erase(results.end() - node.subs.size(), results.end());
706
3.30M
            results.push_back(std::move(*result));
707
3.30M
            stack.pop_back();
708
3.30M
        }
709
        // The final remaining results element is the root result, return it.
710
1.34k
        assert(results.size() >= 1);
711
1.34k
        CHECK_NONFATAL(results.size() == 1);
712
1.34k
        return std::move(results[0]);
713
1.34k
    }
std::optional<TapSatisfier> miniscript::Node<XOnlyPubKey>::TreeEvalMaybe<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)) const::DummyState, TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)) const::'lambda'(TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<XOnlyPubKey> const&, unsigned long), TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)) const::'lambda'(TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>), TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)) const::'lambda'(TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<XOnlyPubKey> const&, unsigned long), TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)) const::'lambda'(TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)) const
Line
Count
Source
653
4.41k
    {
654
        /** Entries of the explicit stack tracked in this algorithm. */
655
4.41k
        struct StackElem
656
4.41k
        {
657
4.41k
            const Node& node; //!< The node being evaluated.
658
4.41k
            size_t expanded; //!< How many children of this node have been expanded.
659
4.41k
            State state; //!< The state for that node.
660
661
4.41k
            StackElem(const Node& node_, size_t exp_, State&& state_) :
662
4.41k
                node(node_), expanded(exp_), state(std::move(state_)) {}
663
4.41k
        };
664
        /* Stack of tree nodes being explored. */
665
4.41k
        std::vector<StackElem> stack;
666
        /* Results of subtrees so far. Their order and mapping to tree nodes
667
         * is implicitly defined by stack. */
668
4.41k
        std::vector<Result> results;
669
4.41k
        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
2.65M
        while (stack.size()) {
688
2.65M
            const Node& node = stack.back().node;
689
2.65M
            if (stack.back().expanded < node.subs.size()) {
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
1.32M
                size_t child_index = stack.back().expanded++;
694
1.32M
                State child_state = downfn(stack.back().state, node, child_index);
695
1.32M
                stack.emplace_back(node.subs[child_index], 0, std::move(child_state));
696
1.32M
                continue;
697
1.32M
            }
698
            // Invoke upfn with the last node.subs.size() elements of results as input.
699
2.65M
            assert(results.size() >= node.subs.size());
700
1.32M
            std::optional<Result> result{upfn(std::move(stack.back().state), node,
701
1.32M
                std::span<Result>{results}.last(node.subs.size()))};
702
            // If evaluation returns std::nullopt, abort immediately.
703
1.32M
            if (!result) return {};
704
            // Replace the last node.subs.size() elements of results with the new result.
705
1.32M
            results.erase(results.end() - node.subs.size(), results.end());
706
1.32M
            results.push_back(std::move(*result));
707
1.32M
            stack.pop_back();
708
1.32M
        }
709
        // The final remaining results element is the root result, return it.
710
4.41k
        assert(results.size() >= 1);
711
4.41k
        CHECK_NONFATAL(results.size() == 1);
712
4.41k
        return std::move(results[0]);
713
4.41k
    }
std::optional<TapSatisfier> miniscript::Node<XOnlyPubKey>::TreeEvalMaybe<miniscript::internal::InputResult, TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::'lambda'(TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<XOnlyPubKey> const&, unsigned long), TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::'lambda'(TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>), TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::'lambda'(TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<XOnlyPubKey> const&, unsigned long), TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::'lambda'(TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const
Line
Count
Source
653
4.41k
    {
654
        /** Entries of the explicit stack tracked in this algorithm. */
655
4.41k
        struct StackElem
656
4.41k
        {
657
4.41k
            const Node& node; //!< The node being evaluated.
658
4.41k
            size_t expanded; //!< How many children of this node have been expanded.
659
4.41k
            State state; //!< The state for that node.
660
661
4.41k
            StackElem(const Node& node_, size_t exp_, State&& state_) :
662
4.41k
                node(node_), expanded(exp_), state(std::move(state_)) {}
663
4.41k
        };
664
        /* Stack of tree nodes being explored. */
665
4.41k
        std::vector<StackElem> stack;
666
        /* Results of subtrees so far. Their order and mapping to tree nodes
667
         * is implicitly defined by stack. */
668
4.41k
        std::vector<Result> results;
669
4.41k
        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
2.65M
        while (stack.size()) {
688
2.65M
            const Node& node = stack.back().node;
689
2.65M
            if (stack.back().expanded < node.subs.size()) {
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
1.32M
                size_t child_index = stack.back().expanded++;
694
1.32M
                State child_state = downfn(stack.back().state, node, child_index);
695
1.32M
                stack.emplace_back(node.subs[child_index], 0, std::move(child_state));
696
1.32M
                continue;
697
1.32M
            }
698
            // Invoke upfn with the last node.subs.size() elements of results as input.
699
2.65M
            assert(results.size() >= node.subs.size());
700
1.32M
            std::optional<Result> result{upfn(std::move(stack.back().state), node,
701
1.32M
                std::span<Result>{results}.last(node.subs.size()))};
702
            // If evaluation returns std::nullopt, abort immediately.
703
1.32M
            if (!result) return {};
704
            // Replace the last node.subs.size() elements of results with the new result.
705
1.32M
            results.erase(results.end() - node.subs.size(), results.end());
706
1.32M
            results.push_back(std::move(*result));
707
1.32M
            stack.pop_back();
708
1.32M
        }
709
        // The final remaining results element is the root result, return it.
710
4.41k
        assert(results.size() >= 1);
711
4.41k
        CHECK_NONFATAL(results.size() == 1);
712
4.41k
        return std::move(results[0]);
713
4.41k
    }
std::optional<WshSatisfier> miniscript::Node<CPubKey>::TreeEvalMaybe<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, WshSatisfier miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::DummyState, WshSatisfier miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::'lambda'(WshSatisfier miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long), WshSatisfier miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::'lambda'(WshSatisfier miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>), WshSatisfier miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::'lambda'(WshSatisfier miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long), WshSatisfier miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::'lambda'(WshSatisfier miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const
Line
Count
Source
653
234
    {
654
        /** Entries of the explicit stack tracked in this algorithm. */
655
234
        struct StackElem
656
234
        {
657
234
            const Node& node; //!< The node being evaluated.
658
234
            size_t expanded; //!< How many children of this node have been expanded.
659
234
            State state; //!< The state for that node.
660
661
234
            StackElem(const Node& node_, size_t exp_, State&& state_) :
662
234
                node(node_), expanded(exp_), state(std::move(state_)) {}
663
234
        };
664
        /* Stack of tree nodes being explored. */
665
234
        std::vector<StackElem> stack;
666
        /* Results of subtrees so far. Their order and mapping to tree nodes
667
         * is implicitly defined by stack. */
668
234
        std::vector<Result> results;
669
234
        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
6.57k
        while (stack.size()) {
688
6.34k
            const Node& node = stack.back().node;
689
6.34k
            if (stack.back().expanded < node.subs.size()) {
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
3.05k
                size_t child_index = stack.back().expanded++;
694
3.05k
                State child_state = downfn(stack.back().state, node, child_index);
695
3.05k
                stack.emplace_back(node.subs[child_index], 0, std::move(child_state));
696
3.05k
                continue;
697
3.05k
            }
698
            // Invoke upfn with the last node.subs.size() elements of results as input.
699
6.34k
            assert(results.size() >= node.subs.size());
700
3.28k
            std::optional<Result> result{upfn(std::move(stack.back().state), node,
701
3.28k
                std::span<Result>{results}.last(node.subs.size()))};
702
            // If evaluation returns std::nullopt, abort immediately.
703
3.28k
            if (!result) return {};
704
            // Replace the last node.subs.size() elements of results with the new result.
705
3.28k
            results.erase(results.end() - node.subs.size(), results.end());
706
3.28k
            results.push_back(std::move(*result));
707
3.28k
            stack.pop_back();
708
3.28k
        }
709
        // The final remaining results element is the root result, return it.
710
234
        assert(results.size() >= 1);
711
234
        CHECK_NONFATAL(results.size() == 1);
712
234
        return std::move(results[0]);
713
234
    }
std::optional<WshSatisfier> miniscript::Node<CPubKey>::TreeEvalMaybe<miniscript::internal::InputResult, WshSatisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, WshSatisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::'lambda'(WshSatisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long), WshSatisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::'lambda'(WshSatisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>), WshSatisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::'lambda'(WshSatisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long), WshSatisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::'lambda'(WshSatisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const
Line
Count
Source
653
234
    {
654
        /** Entries of the explicit stack tracked in this algorithm. */
655
234
        struct StackElem
656
234
        {
657
234
            const Node& node; //!< The node being evaluated.
658
234
            size_t expanded; //!< How many children of this node have been expanded.
659
234
            State state; //!< The state for that node.
660
661
234
            StackElem(const Node& node_, size_t exp_, State&& state_) :
662
234
                node(node_), expanded(exp_), state(std::move(state_)) {}
663
234
        };
664
        /* Stack of tree nodes being explored. */
665
234
        std::vector<StackElem> stack;
666
        /* Results of subtrees so far. Their order and mapping to tree nodes
667
         * is implicitly defined by stack. */
668
234
        std::vector<Result> results;
669
234
        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
6.57k
        while (stack.size()) {
688
6.34k
            const Node& node = stack.back().node;
689
6.34k
            if (stack.back().expanded < node.subs.size()) {
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
3.05k
                size_t child_index = stack.back().expanded++;
694
3.05k
                State child_state = downfn(stack.back().state, node, child_index);
695
3.05k
                stack.emplace_back(node.subs[child_index], 0, std::move(child_state));
696
3.05k
                continue;
697
3.05k
            }
698
            // Invoke upfn with the last node.subs.size() elements of results as input.
699
6.34k
            assert(results.size() >= node.subs.size());
700
3.28k
            std::optional<Result> result{upfn(std::move(stack.back().state), node,
701
3.28k
                std::span<Result>{results}.last(node.subs.size()))};
702
            // If evaluation returns std::nullopt, abort immediately.
703
3.28k
            if (!result) return {};
704
            // Replace the last node.subs.size() elements of results with the new result.
705
3.28k
            results.erase(results.end() - node.subs.size(), results.end());
706
3.28k
            results.push_back(std::move(*result));
707
3.28k
            stack.pop_back();
708
3.28k
        }
709
        // The final remaining results element is the root result, return it.
710
234
        assert(results.size() >= 1);
711
234
        CHECK_NONFATAL(results.size() == 1);
712
234
        return std::move(results[0]);
713
234
    }
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
2.07k
    {
733
        // Invoke TreeEvalMaybe with upfn wrapped to return std::optional<Result>, and then
734
        // unconditionally dereference the result (it cannot be std::nullopt).
735
2.07k
        return std::move(*TreeEvalMaybe<Result>(std::move(root_state),
736
2.07k
            std::forward<DownFn>(downfn),
737
1.68M
            [&upfn](State&& state, const Node& node, std::span<Result> subs) {
738
1.68M
                Result res{upfn(std::move(state), node, subs)};
739
1.68M
                return std::optional<Result>(std::move(res));
740
1.68M
            }
miniscript_tests.cpp:(anonymous namespace)::KeyConverter miniscript::Node<CPubKey>::TreeEval<CScript, bool, CScript miniscript::Node<CPubKey>::ToScript<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, unsigned long)&, CScript miniscript::Node<CPubKey>::ToScript<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, std::span<CScript, 18446744073709551615ul>)>(bool, CScript miniscript::Node<CPubKey>::ToScript<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, unsigned long)&, CScript miniscript::Node<CPubKey>::ToScript<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, std::span<CScript, 18446744073709551615ul>)) const::'lambda'(bool&&, miniscript::Node<CPubKey> const&, std::span<CScript, 18446744073709551615ul>)::operator()(bool&&, miniscript::Node<CPubKey> const&, std::span<CScript, 18446744073709551615ul>) const
Line
Count
Source
737
25.4k
            [&upfn](State&& state, const Node& node, std::span<Result> subs) {
738
25.4k
                Result res{upfn(std::move(state), node, subs)};
739
25.4k
                return std::optional<Result>(std::move(res));
740
25.4k
            }
descriptor.cpp:(anonymous namespace)::ScriptMaker miniscript::Node<unsigned int>::TreeEval<CScript, bool, CScript miniscript::Node<unsigned int>::ToScript<(anonymous namespace)::ScriptMaker>((anonymous namespace)::ScriptMaker const&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, unsigned long)&, CScript miniscript::Node<unsigned int>::ToScript<(anonymous namespace)::ScriptMaker>((anonymous namespace)::ScriptMaker const&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, std::span<CScript, 18446744073709551615ul>)>(bool, CScript miniscript::Node<unsigned int>::ToScript<(anonymous namespace)::ScriptMaker>((anonymous namespace)::ScriptMaker const&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, unsigned long)&, CScript miniscript::Node<unsigned int>::ToScript<(anonymous namespace)::ScriptMaker>((anonymous namespace)::ScriptMaker const&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, std::span<CScript, 18446744073709551615ul>)) const::'lambda'(bool&&, miniscript::Node<unsigned int> const&, std::span<CScript, 18446744073709551615ul>)::operator()(bool&&, miniscript::Node<unsigned int> const&, std::span<CScript, 18446744073709551615ul>) const
Line
Count
Source
737
1.66M
            [&upfn](State&& state, const Node& node, std::span<Result> subs) {
738
1.66M
                Result res{upfn(std::move(state), node, subs)};
739
1.66M
                return std::optional<Result>(std::move(res));
740
1.66M
            }
741
2.07k
        ));
742
2.07k
    }
miniscript_tests.cpp:(anonymous namespace)::KeyConverter miniscript::Node<CPubKey>::TreeEval<CScript, bool, CScript miniscript::Node<CPubKey>::ToScript<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, unsigned long)&, CScript miniscript::Node<CPubKey>::ToScript<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, std::span<CScript, 18446744073709551615ul>)>(bool, CScript miniscript::Node<CPubKey>::ToScript<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, unsigned long)&, CScript miniscript::Node<CPubKey>::ToScript<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, std::span<CScript, 18446744073709551615ul>)) const
Line
Count
Source
732
375
    {
733
        // Invoke TreeEvalMaybe with upfn wrapped to return std::optional<Result>, and then
734
        // unconditionally dereference the result (it cannot be std::nullopt).
735
375
        return std::move(*TreeEvalMaybe<Result>(std::move(root_state),
736
375
            std::forward<DownFn>(downfn),
737
375
            [&upfn](State&& state, const Node& node, std::span<Result> subs) {
738
375
                Result res{upfn(std::move(state), node, subs)};
739
375
                return std::optional<Result>(std::move(res));
740
375
            }
741
375
        ));
742
375
    }
descriptor.cpp:(anonymous namespace)::ScriptMaker miniscript::Node<unsigned int>::TreeEval<CScript, bool, CScript miniscript::Node<unsigned int>::ToScript<(anonymous namespace)::ScriptMaker>((anonymous namespace)::ScriptMaker const&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, unsigned long)&, CScript miniscript::Node<unsigned int>::ToScript<(anonymous namespace)::ScriptMaker>((anonymous namespace)::ScriptMaker const&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, std::span<CScript, 18446744073709551615ul>)>(bool, CScript miniscript::Node<unsigned int>::ToScript<(anonymous namespace)::ScriptMaker>((anonymous namespace)::ScriptMaker const&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, unsigned long)&, CScript miniscript::Node<unsigned int>::ToScript<(anonymous namespace)::ScriptMaker>((anonymous namespace)::ScriptMaker const&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, std::span<CScript, 18446744073709551615ul>)) const
Line
Count
Source
732
1.69k
    {
733
        // Invoke TreeEvalMaybe with upfn wrapped to return std::optional<Result>, and then
734
        // unconditionally dereference the result (it cannot be std::nullopt).
735
1.69k
        return std::move(*TreeEvalMaybe<Result>(std::move(root_state),
736
1.69k
            std::forward<DownFn>(downfn),
737
1.69k
            [&upfn](State&& state, const Node& node, std::span<Result> subs) {
738
1.69k
                Result res{upfn(std::move(state), node, subs)};
739
1.69k
                return std::optional<Result>(std::move(res));
740
1.69k
            }
741
1.69k
        ));
742
1.69k
    }
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
15.9k
    {
749
15.9k
        struct DummyState {};
750
15.9k
        return std::move(*TreeEvalMaybe<Result>(DummyState{},
751
5.84M
            [](DummyState, const Node&, size_t) { return DummyState{}; },
miniscript_tests.cpp:(anonymous namespace)::Satisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::Satisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long)::operator()((anonymous namespace)::Satisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long) const
Line
Count
Source
751
1.61M
            [](DummyState, const Node&, size_t) { return DummyState{}; },
miniscript_tests.cpp:(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&) miniscript::Node<CPubKey>::TreeEval<int, bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)>(bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&) miniscript::Node<CPubKey>::TreeEval<int, bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)>(bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long)::operator()((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&) miniscript::Node<CPubKey>::TreeEval<int, bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)>(bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long) const
Line
Count
Source
751
25.0k
            [](DummyState, const Node&, size_t) { return DummyState{}; },
miniscript_tests.cpp:(anonymous namespace)::KeyConverter miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::KeyConverter miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long)::operator()((anonymous namespace)::KeyConverter miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long) const
Line
Count
Source
751
22.9k
            [](DummyState, const Node&, size_t) { return DummyState{}; },
miniscript::Node<CPubKey> const* miniscript::Node<CPubKey>::TreeEval<miniscript::Node<CPubKey> const*, miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)>(miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)) const::'lambda'(miniscript::Node<CPubKey> const* miniscript::Node<CPubKey>::TreeEval<miniscript::Node<CPubKey> const*, miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)>(miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long)::operator()(miniscript::Node<CPubKey> const* miniscript::Node<CPubKey>::TreeEval<miniscript::Node<CPubKey> const*, miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)>(miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long) const
Line
Count
Source
751
6
            [](DummyState, const Node&, size_t) { return DummyState{}; },
miniscript::Node<unsigned int> miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int>, miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)) const::'lambda'(miniscript::Node<unsigned int> miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int>, miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, unsigned long)::operator()(miniscript::Node<unsigned int> miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int>, miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, unsigned long) const
Line
Count
Source
751
531k
            [](DummyState, const Node&, size_t) { return DummyState{}; },
descriptor.cpp:(anonymous namespace)::KeyParser miniscript::Node<unsigned int>::TreeEval<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)>(void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::KeyParser miniscript::Node<unsigned int>::TreeEval<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)>(void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, unsigned long)::operator()((anonymous namespace)::KeyParser miniscript::Node<unsigned int>::TreeEval<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)>(void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, unsigned long) const
Line
Count
Source
751
995k
            [](DummyState, const Node&, size_t) { return DummyState{}; },
miniscript::Node<unsigned int> const* miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int> const*, miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)) const::'lambda'(miniscript::Node<unsigned int> const* miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int> const*, miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, unsigned long)::operator()(miniscript::Node<unsigned int> const* miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int> const*, miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, unsigned long) const
Line
Count
Source
751
103
            [](DummyState, const Node&, size_t) { return DummyState{}; },
TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)) const::'lambda'(TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<XOnlyPubKey> const&, unsigned long)::operator()(TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<XOnlyPubKey> const&, unsigned long) const
Line
Count
Source
751
1.32M
            [](DummyState, const Node&, size_t) { return DummyState{}; },
TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::'lambda'(TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<XOnlyPubKey> const&, unsigned long)::operator()(TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<XOnlyPubKey> const&, unsigned long) const
Line
Count
Source
751
1.32M
            [](DummyState, const Node&, size_t) { return DummyState{}; },
WshSatisfier miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::'lambda'(WshSatisfier miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long)::operator()(WshSatisfier miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long) const
Line
Count
Source
751
3.05k
            [](DummyState, const Node&, size_t) { return DummyState{}; },
WshSatisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::'lambda'(WshSatisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long)::operator()(WshSatisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, unsigned long) const
Line
Count
Source
751
3.05k
            [](DummyState, const Node&, size_t) { return DummyState{}; },
752
5.85M
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
5.85M
                Result res{upfn(node, subs)};
754
5.85M
                return std::optional<Result>(std::move(res));
755
5.85M
            }
miniscript_tests.cpp:(anonymous namespace)::Satisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::Satisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)::operator()((anonymous namespace)::Satisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>) const
Line
Count
Source
752
1.61M
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
1.61M
                Result res{upfn(node, subs)};
754
1.61M
                return std::optional<Result>(std::move(res));
755
1.61M
            }
miniscript_tests.cpp:(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&) miniscript::Node<CPubKey>::TreeEval<int, bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)>(bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&) miniscript::Node<CPubKey>::TreeEval<int, bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)>(bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)::operator()((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&) miniscript::Node<CPubKey>::TreeEval<int, bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)>(bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>) const
Line
Count
Source
752
25.4k
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
25.4k
                Result res{upfn(node, subs)};
754
25.4k
                return std::optional<Result>(std::move(res));
755
25.4k
            }
miniscript_tests.cpp:(anonymous namespace)::KeyConverter miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::KeyConverter miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)::operator()((anonymous namespace)::KeyConverter miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>) const
Line
Count
Source
752
23.2k
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
23.2k
                Result res{upfn(node, subs)};
754
23.2k
                return std::optional<Result>(std::move(res));
755
23.2k
            }
miniscript::Node<CPubKey> const* miniscript::Node<CPubKey>::TreeEval<miniscript::Node<CPubKey> const*, miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)>(miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)) const::'lambda'(miniscript::Node<CPubKey> const* miniscript::Node<CPubKey>::TreeEval<miniscript::Node<CPubKey> const*, miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)>(miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)::operator()(miniscript::Node<CPubKey> const* miniscript::Node<CPubKey>::TreeEval<miniscript::Node<CPubKey> const*, miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)>(miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>) const
Line
Count
Source
752
7
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
7
                Result res{upfn(node, subs)};
754
7
                return std::optional<Result>(std::move(res));
755
7
            }
miniscript::Node<unsigned int> miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int>, miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)) const::'lambda'(miniscript::Node<unsigned int> miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int>, miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)::operator()(miniscript::Node<unsigned int> miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int>, miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>) const
Line
Count
Source
752
531k
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
531k
                Result res{upfn(node, subs)};
754
531k
                return std::optional<Result>(std::move(res));
755
531k
            }
descriptor.cpp:(anonymous namespace)::KeyParser miniscript::Node<unsigned int>::TreeEval<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)>(void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)) const::'lambda'((anonymous namespace)::KeyParser miniscript::Node<unsigned int>::TreeEval<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)>(void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)::operator()((anonymous namespace)::KeyParser miniscript::Node<unsigned int>::TreeEval<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)>(void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>) const
Line
Count
Source
752
996k
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
996k
                Result res{upfn(node, subs)};
754
996k
                return std::optional<Result>(std::move(res));
755
996k
            }
miniscript::Node<unsigned int> const* miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int> const*, miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)) const::'lambda'(miniscript::Node<unsigned int> const* miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int> const*, miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)::operator()(miniscript::Node<unsigned int> const* miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int> const*, miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)) const::DummyState, miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>) const
Line
Count
Source
752
119
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
119
                Result res{upfn(node, subs)};
754
119
                return std::optional<Result>(std::move(res));
755
119
            }
TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)) const::'lambda'(TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)::operator()(TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>) const
Line
Count
Source
752
1.32M
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
1.32M
                Result res{upfn(node, subs)};
754
1.32M
                return std::optional<Result>(std::move(res));
755
1.32M
            }
TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::'lambda'(TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)::operator()(TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>) const
Line
Count
Source
752
1.32M
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
1.32M
                Result res{upfn(node, subs)};
754
1.32M
                return std::optional<Result>(std::move(res));
755
1.32M
            }
WshSatisfier miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::'lambda'(WshSatisfier miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)::operator()(WshSatisfier miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>) const
Line
Count
Source
752
3.28k
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
3.28k
                Result res{upfn(node, subs)};
754
3.28k
                return std::optional<Result>(std::move(res));
755
3.28k
            }
WshSatisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::'lambda'(WshSatisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)::operator()(WshSatisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const::DummyState, miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>) const
Line
Count
Source
752
3.28k
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
3.28k
                Result res{upfn(node, subs)};
754
3.28k
                return std::optional<Result>(std::move(res));
755
3.28k
            }
756
15.9k
        ));
757
15.9k
    }
miniscript_tests.cpp:(anonymous namespace)::Satisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const
Line
Count
Source
748
4.82k
    {
749
4.82k
        struct DummyState {};
750
4.82k
        return std::move(*TreeEvalMaybe<Result>(DummyState{},
751
4.82k
            [](DummyState, const Node&, size_t) { return DummyState{}; },
752
4.82k
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
4.82k
                Result res{upfn(node, subs)};
754
4.82k
                return std::optional<Result>(std::move(res));
755
4.82k
            }
756
4.82k
        ));
757
4.82k
    }
miniscript_tests.cpp:(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&) miniscript::Node<CPubKey>::TreeEval<int, bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)>(bool miniscript::Node<CPubKey>::IsSatisfiable<(anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)>((anonymous namespace)::MiniScriptTest::TestSatisfy((anonymous namespace)::KeyConverter const&, miniscript::Node<CPubKey> const&)::'lambda'(miniscript::Node<CPubKey> const&)) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<int, 18446744073709551615ul>)) const
Line
Count
Source
748
375
    {
749
375
        struct DummyState {};
750
375
        return std::move(*TreeEvalMaybe<Result>(DummyState{},
751
375
            [](DummyState, const Node&, size_t) { return DummyState{}; },
752
375
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
375
                Result res{upfn(node, subs)};
754
375
                return std::optional<Result>(std::move(res));
755
375
            }
756
375
        ));
757
375
    }
miniscript_tests.cpp:(anonymous namespace)::KeyConverter miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const
Line
Count
Source
748
313
    {
749
313
        struct DummyState {};
750
313
        return std::move(*TreeEvalMaybe<Result>(DummyState{},
751
313
            [](DummyState, const Node&, size_t) { return DummyState{}; },
752
313
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
313
                Result res{upfn(node, subs)};
754
313
                return std::optional<Result>(std::move(res));
755
313
            }
756
313
        ));
757
313
    }
miniscript::Node<CPubKey> const* miniscript::Node<CPubKey>::TreeEval<miniscript::Node<CPubKey> const*, miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)>(miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)) const
Line
Count
Source
748
1
    {
749
1
        struct DummyState {};
750
1
        return std::move(*TreeEvalMaybe<Result>(DummyState{},
751
1
            [](DummyState, const Node&, size_t) { return DummyState{}; },
752
1
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
1
                Result res{upfn(node, subs)};
754
1
                return std::optional<Result>(std::move(res));
755
1
            }
756
1
        ));
757
1
    }
miniscript::Node<unsigned int> miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int>, miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::Clone() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int>, 18446744073709551615ul>)) const
Line
Count
Source
748
223
    {
749
223
        struct DummyState {};
750
223
        return std::move(*TreeEvalMaybe<Result>(DummyState{},
751
223
            [](DummyState, const Node&, size_t) { return DummyState{}; },
752
223
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
223
                Result res{upfn(node, subs)};
754
223
                return std::optional<Result>(std::move(res));
755
223
            }
756
223
        ));
757
223
    }
descriptor.cpp:(anonymous namespace)::KeyParser miniscript::Node<unsigned int>::TreeEval<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)>(void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)) const
Line
Count
Source
748
901
    {
749
901
        struct DummyState {};
750
901
        return std::move(*TreeEvalMaybe<Result>(DummyState{},
751
901
            [](DummyState, const Node&, size_t) { return DummyState{}; },
752
901
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
901
                Result res{upfn(node, subs)};
754
901
                return std::optional<Result>(std::move(res));
755
901
            }
756
901
        ));
757
901
    }
miniscript::Node<unsigned int> const* miniscript::Node<unsigned int>::TreeEval<miniscript::Node<unsigned int> const*, miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)>(miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)) const
Line
Count
Source
748
16
    {
749
16
        struct DummyState {};
750
16
        return std::move(*TreeEvalMaybe<Result>(DummyState{},
751
16
            [](DummyState, const Node&, size_t) { return DummyState{}; },
752
16
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
16
                Result res{upfn(node, subs)};
754
16
                return std::optional<Result>(std::move(res));
755
16
            }
756
16
        ));
757
16
    }
TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)) const
Line
Count
Source
748
4.41k
    {
749
4.41k
        struct DummyState {};
750
4.41k
        return std::move(*TreeEvalMaybe<Result>(DummyState{},
751
4.41k
            [](DummyState, const Node&, size_t) { return DummyState{}; },
752
4.41k
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
4.41k
                Result res{upfn(node, subs)};
754
4.41k
                return std::optional<Result>(std::move(res));
755
4.41k
            }
756
4.41k
        ));
757
4.41k
    }
TapSatisfier miniscript::Node<XOnlyPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const
Line
Count
Source
748
4.41k
    {
749
4.41k
        struct DummyState {};
750
4.41k
        return std::move(*TreeEvalMaybe<Result>(DummyState{},
751
4.41k
            [](DummyState, const Node&, size_t) { return DummyState{}; },
752
4.41k
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
4.41k
                Result res{upfn(node, subs)};
754
4.41k
                return std::optional<Result>(std::move(res));
755
4.41k
            }
756
4.41k
        ));
757
4.41k
    }
WshSatisfier miniscript::Node<CPubKey>::TreeEval<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)>(void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)) const
Line
Count
Source
748
234
    {
749
234
        struct DummyState {};
750
234
        return std::move(*TreeEvalMaybe<Result>(DummyState{},
751
234
            [](DummyState, const Node&, size_t) { return DummyState{}; },
752
234
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
234
                Result res{upfn(node, subs)};
754
234
                return std::optional<Result>(std::move(res));
755
234
            }
756
234
        ));
757
234
    }
WshSatisfier miniscript::Node<CPubKey>::TreeEval<miniscript::internal::InputResult, miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)>(miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)) const
Line
Count
Source
748
234
    {
749
234
        struct DummyState {};
750
234
        return std::move(*TreeEvalMaybe<Result>(DummyState{},
751
234
            [](DummyState, const Node&, size_t) { return DummyState{}; },
752
234
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
234
                Result res{upfn(node, subs)};
754
234
                return std::optional<Result>(std::move(res));
755
234
            }
756
234
        ));
757
234
    }
758
759
    /** Compare two miniscript subtrees, using a non-recursive algorithm. */
760
    friend int Compare(const Node<Key>& node1, const Node<Key>& node2)
761
    {
762
        std::vector<std::pair<const Node<Key>&, const Node<Key>&>> queue;
763
        queue.emplace_back(node1, node2);
764
        while (!queue.empty()) {
765
            const auto& [a, b] = queue.back();
766
            queue.pop_back();
767
            if (std::tie(a.fragment, a.k, a.keys, a.data) < std::tie(b.fragment, b.k, b.keys, b.data)) return -1;
768
            if (std::tie(b.fragment, b.k, b.keys, b.data) < std::tie(a.fragment, a.k, a.keys, a.data)) return 1;
769
            if (a.subs.size() < b.subs.size()) return -1;
770
            if (b.subs.size() < a.subs.size()) return 1;
771
            size_t n = a.subs.size();
772
            for (size_t i = 0; i < n; ++i) {
773
                queue.emplace_back(a.subs[n - 1 - i], b.subs[n - 1 - i]);
774
            }
775
        }
776
        return 0;
777
    }
778
779
    //! Compute the type for this miniscript.
780
3.08M
    Type CalcType() const {
781
3.08M
        using namespace internal;
782
783
        // THRESH has a variable number of subexpressions
784
3.08M
        std::vector<Type> sub_types;
785
3.08M
        if (fragment == Fragment::THRESH) {
786
1.63k
            for (const auto& sub : subs) sub_types.push_back(sub.GetType());
787
428
        }
788
        // All other nodes than THRESH can be computed just from the types of the 0-3 subexpressions.
789
3.08M
        Type x = subs.size() > 0 ? subs[0].GetType() : ""_mst;
790
3.08M
        Type y = subs.size() > 1 ? subs[1].GetType() : ""_mst;
791
3.08M
        Type z = subs.size() > 2 ? subs[2].GetType() : ""_mst;
792
793
3.08M
        return SanitizeType(ComputeType(fragment, x, y, z, sub_types, k, data.size(), subs.size(), keys.size(), m_script_ctx));
794
3.08M
    }
miniscript::Node<CPubKey>::CalcType() const
Line
Count
Source
780
28.5k
    Type CalcType() const {
781
28.5k
        using namespace internal;
782
783
        // THRESH has a variable number of subexpressions
784
28.5k
        std::vector<Type> sub_types;
785
28.5k
        if (fragment == Fragment::THRESH) {
786
703
            for (const auto& sub : subs) sub_types.push_back(sub.GetType());
787
148
        }
788
        // All other nodes than THRESH can be computed just from the types of the 0-3 subexpressions.
789
28.5k
        Type x = subs.size() > 0 ? subs[0].GetType() : ""_mst;
790
28.5k
        Type y = subs.size() > 1 ? subs[1].GetType() : ""_mst;
791
28.5k
        Type z = subs.size() > 2 ? subs[2].GetType() : ""_mst;
792
793
28.5k
        return SanitizeType(ComputeType(fragment, x, y, z, sub_types, k, data.size(), subs.size(), keys.size(), m_script_ctx));
794
28.5k
    }
miniscript::Node<unsigned int>::CalcType() const
Line
Count
Source
780
1.72M
    Type CalcType() const {
781
1.72M
        using namespace internal;
782
783
        // THRESH has a variable number of subexpressions
784
1.72M
        std::vector<Type> sub_types;
785
1.72M
        if (fragment == Fragment::THRESH) {
786
860
            for (const auto& sub : subs) sub_types.push_back(sub.GetType());
787
256
        }
788
        // All other nodes than THRESH can be computed just from the types of the 0-3 subexpressions.
789
1.72M
        Type x = subs.size() > 0 ? subs[0].GetType() : ""_mst;
790
1.72M
        Type y = subs.size() > 1 ? subs[1].GetType() : ""_mst;
791
1.72M
        Type z = subs.size() > 2 ? subs[2].GetType() : ""_mst;
792
793
1.72M
        return SanitizeType(ComputeType(fragment, x, y, z, sub_types, k, data.size(), subs.size(), keys.size(), m_script_ctx));
794
1.72M
    }
miniscript::Node<XOnlyPubKey>::CalcType() const
Line
Count
Source
780
1.32M
    Type CalcType() const {
781
1.32M
        using namespace internal;
782
783
        // THRESH has a variable number of subexpressions
784
1.32M
        std::vector<Type> sub_types;
785
1.32M
        if (fragment == Fragment::THRESH) {
786
72
            for (const auto& sub : subs) sub_types.push_back(sub.GetType());
787
24
        }
788
        // All other nodes than THRESH can be computed just from the types of the 0-3 subexpressions.
789
1.32M
        Type x = subs.size() > 0 ? subs[0].GetType() : ""_mst;
790
1.32M
        Type y = subs.size() > 1 ? subs[1].GetType() : ""_mst;
791
1.32M
        Type z = subs.size() > 2 ? subs[2].GetType() : ""_mst;
792
793
1.32M
        return SanitizeType(ComputeType(fragment, x, y, z, sub_types, k, data.size(), subs.size(), keys.size(), m_script_ctx));
794
1.32M
    }
795
796
public:
797
    template<typename Ctx>
798
    CScript ToScript(const Ctx& ctx) const
799
2.07k
    {
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
1.68M
        auto downfn = [](bool verify, const Node& node, size_t index) {
804
            // For WRAP_V, the subexpression is certainly followed by OP_VERIFY.
805
1.68M
            if (node.fragment == Fragment::WRAP_V) return true;
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
1.68M
            if (node.fragment == Fragment::WRAP_S ||
809
1.68M
                (node.fragment == Fragment::AND_V && index == 1)) return verify;
810
1.68M
            return false;
811
1.68M
        };
miniscript_tests.cpp:CScript miniscript::Node<CPubKey>::ToScript<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, unsigned long)::operator()(bool, miniscript::Node<CPubKey> const&, unsigned long) const
Line
Count
Source
803
25.0k
        auto downfn = [](bool verify, const Node& node, size_t index) {
804
            // For WRAP_V, the subexpression is certainly followed by OP_VERIFY.
805
25.0k
            if (node.fragment == Fragment::WRAP_V) return true;
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
24.7k
            if (node.fragment == Fragment::WRAP_S ||
809
24.7k
                (node.fragment == Fragment::AND_V && index == 1)) return verify;
810
24.5k
            return false;
811
24.7k
        };
descriptor.cpp:CScript miniscript::Node<unsigned int>::ToScript<(anonymous namespace)::ScriptMaker>((anonymous namespace)::ScriptMaker const&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, unsigned long)::operator()(bool, miniscript::Node<unsigned int> const&, unsigned long) const
Line
Count
Source
803
1.66M
        auto downfn = [](bool verify, const Node& node, size_t index) {
804
            // For WRAP_V, the subexpression is certainly followed by OP_VERIFY.
805
1.66M
            if (node.fragment == Fragment::WRAP_V) return true;
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
1.66M
            if (node.fragment == Fragment::WRAP_S ||
809
1.66M
                (node.fragment == Fragment::AND_V && index == 1)) return verify;
810
1.65M
            return false;
811
1.66M
        };
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
2.07k
        const bool is_tapscript{IsTapscript(m_script_ctx)};
815
1.68M
        auto upfn = [&ctx, is_tapscript](bool verify, const Node& node, std::span<CScript> subs) -> CScript {
816
1.68M
            switch (node.fragment) {
817
4.05k
                case Fragment::PK_K: return BuildScript(ctx.ToPKBytes(node.keys[0]));
818
590
                case Fragment::PK_H: return BuildScript(OP_DUP, OP_HASH160, ctx.ToPKHBytes(node.keys[0]), OP_EQUALVERIFY);
819
6.56k
                case Fragment::OLDER: return BuildScript(node.k, OP_CHECKSEQUENCEVERIFY);
820
1.13k
                case Fragment::AFTER: return BuildScript(node.k, OP_CHECKLOCKTIMEVERIFY);
821
133
                case Fragment::SHA256: return BuildScript(OP_SIZE, 32, OP_EQUALVERIFY, OP_SHA256, node.data, verify ? OP_EQUALVERIFY : OP_EQUAL);
822
113
                case Fragment::RIPEMD160: return BuildScript(OP_SIZE, 32, OP_EQUALVERIFY, OP_RIPEMD160, node.data, verify ? OP_EQUALVERIFY : OP_EQUAL);
823
162
                case Fragment::HASH256: return BuildScript(OP_SIZE, 32, OP_EQUALVERIFY, OP_HASH256, node.data, verify ? OP_EQUALVERIFY : OP_EQUAL);
824
117
                case Fragment::HASH160: return BuildScript(OP_SIZE, 32, OP_EQUALVERIFY, OP_HASH160, node.data, verify ? OP_EQUALVERIFY : OP_EQUAL);
825
7.82k
                case Fragment::WRAP_A: return BuildScript(OP_TOALTSTACK, subs[0], OP_FROMALTSTACK);
826
1.53k
                case Fragment::WRAP_S: return BuildScript(OP_SWAP, subs[0]);
827
4.59k
                case Fragment::WRAP_C: return BuildScript(std::move(subs[0]), verify ? OP_CHECKSIGVERIFY : OP_CHECKSIG);
828
145
                case Fragment::WRAP_D: return BuildScript(OP_DUP, OP_IF, subs[0], OP_ENDIF);
829
1.46k
                case Fragment::WRAP_V: {
830
1.46k
                    if (node.subs[0].GetType() << "x"_mst) {
831
352
                        return BuildScript(std::move(subs[0]), OP_VERIFY);
832
1.11k
                    } else {
833
1.11k
                        return std::move(subs[0]);
834
1.11k
                    }
835
1.46k
                }
836
24
                case Fragment::WRAP_J: return BuildScript(OP_SIZE, OP_0NOTEQUAL, OP_IF, subs[0], OP_ENDIF);
837
1.64M
                case Fragment::WRAP_N: return BuildScript(std::move(subs[0]), OP_0NOTEQUAL);
838
236
                case Fragment::JUST_1: return BuildScript(OP_1);
839
1.14k
                case Fragment::JUST_0: return BuildScript(OP_0);
840
1.28k
                case Fragment::AND_V: return BuildScript(std::move(subs[0]), subs[1]);
841
7.42k
                case Fragment::AND_B: return BuildScript(std::move(subs[0]), subs[1], OP_BOOLAND);
842
127
                case Fragment::OR_B: return BuildScript(std::move(subs[0]), subs[1], OP_BOOLOR);
843
150
                case Fragment::OR_D: return BuildScript(std::move(subs[0]), OP_IFDUP, OP_NOTIF, subs[1], OP_ENDIF);
844
57
                case Fragment::OR_C: return BuildScript(std::move(subs[0]), OP_NOTIF, subs[1], OP_ENDIF);
845
1.05k
                case Fragment::OR_I: return BuildScript(OP_IF, subs[0], OP_ELSE, subs[1], OP_ENDIF);
846
262
                case Fragment::ANDOR: return BuildScript(std::move(subs[0]), OP_NOTIF, subs[2], OP_ELSE, subs[1], OP_ENDIF);
847
212
                case Fragment::MULTI: {
848
212
                    CHECK_NONFATAL(!is_tapscript);
849
212
                    CScript script = BuildScript(node.k);
850
445
                    for (const auto& key : node.keys) {
851
445
                        script = BuildScript(std::move(script), ctx.ToPKBytes(key));
852
445
                    }
853
212
                    return BuildScript(std::move(script), node.keys.size(), verify ? OP_CHECKMULTISIGVERIFY : OP_CHECKMULTISIG);
854
1.46k
                }
855
52
                case Fragment::MULTI_A: {
856
52
                    CHECK_NONFATAL(is_tapscript);
857
52
                    CScript script = BuildScript(ctx.ToPKBytes(*node.keys.begin()), OP_CHECKSIG);
858
197
                    for (auto it = node.keys.begin() + 1; it != node.keys.end(); ++it) {
859
145
                        script = BuildScript(std::move(script), ctx.ToPKBytes(*it), OP_CHECKSIGADD);
860
145
                    }
861
52
                    return BuildScript(std::move(script), node.k, verify ? OP_NUMEQUALVERIFY : OP_NUMEQUAL);
862
1.46k
                }
863
548
                case Fragment::THRESH: {
864
548
                    CScript script = std::move(subs[0]);
865
2.35k
                    for (size_t i = 1; i < subs.size(); ++i) {
866
1.80k
                        script = BuildScript(std::move(script), subs[i], OP_ADD);
867
1.80k
                    }
868
548
                    return BuildScript(std::move(script), node.k, verify ? OP_EQUALVERIFY : OP_EQUAL);
869
1.46k
                }
870
1.68M
            }
871
1.68M
            assert(false);
872
0
        };
miniscript_tests.cpp:CScript miniscript::Node<CPubKey>::ToScript<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, std::span<CScript, 18446744073709551615ul>)::operator()(bool, miniscript::Node<CPubKey> const&, std::span<CScript, 18446744073709551615ul>) const
Line
Count
Source
815
25.4k
        auto upfn = [&ctx, is_tapscript](bool verify, const Node& node, std::span<CScript> subs) -> CScript {
816
25.4k
            switch (node.fragment) {
817
1.36k
                case Fragment::PK_K: return BuildScript(ctx.ToPKBytes(node.keys[0]));
818
78
                case Fragment::PK_H: return BuildScript(OP_DUP, OP_HASH160, ctx.ToPKHBytes(node.keys[0]), OP_EQUALVERIFY);
819
6.11k
                case Fragment::OLDER: return BuildScript(node.k, OP_CHECKSEQUENCEVERIFY);
820
195
                case Fragment::AFTER: return BuildScript(node.k, OP_CHECKLOCKTIMEVERIFY);
821
63
                case Fragment::SHA256: return BuildScript(OP_SIZE, 32, OP_EQUALVERIFY, OP_SHA256, node.data, verify ? OP_EQUALVERIFY : OP_EQUAL);
822
21
                case Fragment::RIPEMD160: return BuildScript(OP_SIZE, 32, OP_EQUALVERIFY, OP_RIPEMD160, node.data, verify ? OP_EQUALVERIFY : OP_EQUAL);
823
42
                case Fragment::HASH256: return BuildScript(OP_SIZE, 32, OP_EQUALVERIFY, OP_HASH256, node.data, verify ? OP_EQUALVERIFY : OP_EQUAL);
824
18
                case Fragment::HASH160: return BuildScript(OP_SIZE, 32, OP_EQUALVERIFY, OP_HASH160, node.data, verify ? OP_EQUALVERIFY : OP_EQUAL);
825
7.33k
                case Fragment::WRAP_A: return BuildScript(OP_TOALTSTACK, subs[0], OP_FROMALTSTACK);
826
30
                case Fragment::WRAP_S: return BuildScript(OP_SWAP, subs[0]);
827
1.39k
                case Fragment::WRAP_C: return BuildScript(std::move(subs[0]), verify ? OP_CHECKSIGVERIFY : OP_CHECKSIG);
828
15
                case Fragment::WRAP_D: return BuildScript(OP_DUP, OP_IF, subs[0], OP_ENDIF);
829
243
                case Fragment::WRAP_V: {
830
243
                    if (node.subs[0].GetType() << "x"_mst) {
831
192
                        return BuildScript(std::move(subs[0]), OP_VERIFY);
832
192
                    } else {
833
51
                        return std::move(subs[0]);
834
51
                    }
835
243
                }
836
24
                case Fragment::WRAP_J: return BuildScript(OP_SIZE, OP_0NOTEQUAL, OP_IF, subs[0], OP_ENDIF);
837
45
                case Fragment::WRAP_N: return BuildScript(std::move(subs[0]), OP_0NOTEQUAL);
838
231
                case Fragment::JUST_1: return BuildScript(OP_1);
839
249
                case Fragment::JUST_0: return BuildScript(OP_0);
840
198
                case Fragment::AND_V: return BuildScript(std::move(subs[0]), subs[1]);
841
7.25k
                case Fragment::AND_B: return BuildScript(std::move(subs[0]), subs[1], OP_BOOLAND);
842
24
                case Fragment::OR_B: return BuildScript(std::move(subs[0]), subs[1], OP_BOOLOR);
843
45
                case Fragment::OR_D: return BuildScript(std::move(subs[0]), OP_IFDUP, OP_NOTIF, subs[1], OP_ENDIF);
844
18
                case Fragment::OR_C: return BuildScript(std::move(subs[0]), OP_NOTIF, subs[1], OP_ENDIF);
845
237
                case Fragment::OR_I: return BuildScript(OP_IF, subs[0], OP_ELSE, subs[1], OP_ENDIF);
846
87
                case Fragment::ANDOR: return BuildScript(std::move(subs[0]), OP_NOTIF, subs[2], OP_ELSE, subs[1], OP_ENDIF);
847
36
                case Fragment::MULTI: {
848
36
                    CHECK_NONFATAL(!is_tapscript);
849
36
                    CScript script = BuildScript(node.k);
850
69
                    for (const auto& key : node.keys) {
851
69
                        script = BuildScript(std::move(script), ctx.ToPKBytes(key));
852
69
                    }
853
36
                    return BuildScript(std::move(script), node.keys.size(), verify ? OP_CHECKMULTISIGVERIFY : OP_CHECKMULTISIG);
854
243
                }
855
6
                case Fragment::MULTI_A: {
856
6
                    CHECK_NONFATAL(is_tapscript);
857
6
                    CScript script = BuildScript(ctx.ToPKBytes(*node.keys.begin()), OP_CHECKSIG);
858
69
                    for (auto it = node.keys.begin() + 1; it != node.keys.end(); ++it) {
859
63
                        script = BuildScript(std::move(script), ctx.ToPKBytes(*it), OP_CHECKSIGADD);
860
63
                    }
861
6
                    return BuildScript(std::move(script), node.k, verify ? OP_NUMEQUALVERIFY : OP_NUMEQUAL);
862
243
                }
863
48
                case Fragment::THRESH: {
864
48
                    CScript script = std::move(subs[0]);
865
138
                    for (size_t i = 1; i < subs.size(); ++i) {
866
90
                        script = BuildScript(std::move(script), subs[i], OP_ADD);
867
90
                    }
868
48
                    return BuildScript(std::move(script), node.k, verify ? OP_EQUALVERIFY : OP_EQUAL);
869
243
                }
870
25.4k
            }
871
25.4k
            assert(false);
872
0
        };
descriptor.cpp:CScript miniscript::Node<unsigned int>::ToScript<(anonymous namespace)::ScriptMaker>((anonymous namespace)::ScriptMaker const&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, std::span<CScript, 18446744073709551615ul>)::operator()(bool, miniscript::Node<unsigned int> const&, std::span<CScript, 18446744073709551615ul>) const
Line
Count
Source
815
1.66M
        auto upfn = [&ctx, is_tapscript](bool verify, const Node& node, std::span<CScript> subs) -> CScript {
816
1.66M
            switch (node.fragment) {
817
2.69k
                case Fragment::PK_K: return BuildScript(ctx.ToPKBytes(node.keys[0]));
818
512
                case Fragment::PK_H: return BuildScript(OP_DUP, OP_HASH160, ctx.ToPKHBytes(node.keys[0]), OP_EQUALVERIFY);
819
448
                case Fragment::OLDER: return BuildScript(node.k, OP_CHECKSEQUENCEVERIFY);
820
935
                case Fragment::AFTER: return BuildScript(node.k, OP_CHECKLOCKTIMEVERIFY);
821
70
                case Fragment::SHA256: return BuildScript(OP_SIZE, 32, OP_EQUALVERIFY, OP_SHA256, node.data, verify ? OP_EQUALVERIFY : OP_EQUAL);
822
92
                case Fragment::RIPEMD160: return BuildScript(OP_SIZE, 32, OP_EQUALVERIFY, OP_RIPEMD160, node.data, verify ? OP_EQUALVERIFY : OP_EQUAL);
823
120
                case Fragment::HASH256: return BuildScript(OP_SIZE, 32, OP_EQUALVERIFY, OP_HASH256, node.data, verify ? OP_EQUALVERIFY : OP_EQUAL);
824
99
                case Fragment::HASH160: return BuildScript(OP_SIZE, 32, OP_EQUALVERIFY, OP_HASH160, node.data, verify ? OP_EQUALVERIFY : OP_EQUAL);
825
485
                case Fragment::WRAP_A: return BuildScript(OP_TOALTSTACK, subs[0], OP_FROMALTSTACK);
826
1.50k
                case Fragment::WRAP_S: return BuildScript(OP_SWAP, subs[0]);
827
3.20k
                case Fragment::WRAP_C: return BuildScript(std::move(subs[0]), verify ? OP_CHECKSIGVERIFY : OP_CHECKSIG);
828
130
                case Fragment::WRAP_D: return BuildScript(OP_DUP, OP_IF, subs[0], OP_ENDIF);
829
1.21k
                case Fragment::WRAP_V: {
830
1.21k
                    if (node.subs[0].GetType() << "x"_mst) {
831
160
                        return BuildScript(std::move(subs[0]), OP_VERIFY);
832
1.05k
                    } else {
833
1.05k
                        return std::move(subs[0]);
834
1.05k
                    }
835
1.21k
                }
836
0
                case Fragment::WRAP_J: return BuildScript(OP_SIZE, OP_0NOTEQUAL, OP_IF, subs[0], OP_ENDIF);
837
1.64M
                case Fragment::WRAP_N: return BuildScript(std::move(subs[0]), OP_0NOTEQUAL);
838
5
                case Fragment::JUST_1: return BuildScript(OP_1);
839
893
                case Fragment::JUST_0: return BuildScript(OP_0);
840
1.08k
                case Fragment::AND_V: return BuildScript(std::move(subs[0]), subs[1]);
841
172
                case Fragment::AND_B: return BuildScript(std::move(subs[0]), subs[1], OP_BOOLAND);
842
103
                case Fragment::OR_B: return BuildScript(std::move(subs[0]), subs[1], OP_BOOLOR);
843
105
                case Fragment::OR_D: return BuildScript(std::move(subs[0]), OP_IFDUP, OP_NOTIF, subs[1], OP_ENDIF);
844
39
                case Fragment::OR_C: return BuildScript(std::move(subs[0]), OP_NOTIF, subs[1], OP_ENDIF);
845
816
                case Fragment::OR_I: return BuildScript(OP_IF, subs[0], OP_ELSE, subs[1], OP_ENDIF);
846
175
                case Fragment::ANDOR: return BuildScript(std::move(subs[0]), OP_NOTIF, subs[2], OP_ELSE, subs[1], OP_ENDIF);
847
176
                case Fragment::MULTI: {
848
176
                    CHECK_NONFATAL(!is_tapscript);
849
176
                    CScript script = BuildScript(node.k);
850
376
                    for (const auto& key : node.keys) {
851
376
                        script = BuildScript(std::move(script), ctx.ToPKBytes(key));
852
376
                    }
853
176
                    return BuildScript(std::move(script), node.keys.size(), verify ? OP_CHECKMULTISIGVERIFY : OP_CHECKMULTISIG);
854
1.21k
                }
855
46
                case Fragment::MULTI_A: {
856
46
                    CHECK_NONFATAL(is_tapscript);
857
46
                    CScript script = BuildScript(ctx.ToPKBytes(*node.keys.begin()), OP_CHECKSIG);
858
128
                    for (auto it = node.keys.begin() + 1; it != node.keys.end(); ++it) {
859
82
                        script = BuildScript(std::move(script), ctx.ToPKBytes(*it), OP_CHECKSIGADD);
860
82
                    }
861
46
                    return BuildScript(std::move(script), node.k, verify ? OP_NUMEQUALVERIFY : OP_NUMEQUAL);
862
1.21k
                }
863
500
                case Fragment::THRESH: {
864
500
                    CScript script = std::move(subs[0]);
865
2.21k
                    for (size_t i = 1; i < subs.size(); ++i) {
866
1.71k
                        script = BuildScript(std::move(script), subs[i], OP_ADD);
867
1.71k
                    }
868
500
                    return BuildScript(std::move(script), node.k, verify ? OP_EQUALVERIFY : OP_EQUAL);
869
1.21k
                }
870
1.66M
            }
871
1.66M
            assert(false);
872
0
        };
873
2.07k
        return TreeEval<CScript>(false, downfn, upfn);
874
2.07k
    }
miniscript_tests.cpp:CScript miniscript::Node<CPubKey>::ToScript<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const
Line
Count
Source
799
375
    {
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
375
        auto downfn = [](bool verify, const Node& node, size_t index) {
804
            // For WRAP_V, the subexpression is certainly followed by OP_VERIFY.
805
375
            if (node.fragment == Fragment::WRAP_V) return true;
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
375
            if (node.fragment == Fragment::WRAP_S ||
809
375
                (node.fragment == Fragment::AND_V && index == 1)) return verify;
810
375
            return false;
811
375
        };
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
375
        const bool is_tapscript{IsTapscript(m_script_ctx)};
815
375
        auto upfn = [&ctx, is_tapscript](bool verify, const Node& node, std::span<CScript> subs) -> CScript {
816
375
            switch (node.fragment) {
817
375
                case Fragment::PK_K: return BuildScript(ctx.ToPKBytes(node.keys[0]));
818
375
                case Fragment::PK_H: return BuildScript(OP_DUP, OP_HASH160, ctx.ToPKHBytes(node.keys[0]), OP_EQUALVERIFY);
819
375
                case Fragment::OLDER: return BuildScript(node.k, OP_CHECKSEQUENCEVERIFY);
820
375
                case Fragment::AFTER: return BuildScript(node.k, OP_CHECKLOCKTIMEVERIFY);
821
375
                case Fragment::SHA256: return BuildScript(OP_SIZE, 32, OP_EQUALVERIFY, OP_SHA256, node.data, verify ? OP_EQUALVERIFY : OP_EQUAL);
822
375
                case Fragment::RIPEMD160: return BuildScript(OP_SIZE, 32, OP_EQUALVERIFY, OP_RIPEMD160, node.data, verify ? OP_EQUALVERIFY : OP_EQUAL);
823
375
                case Fragment::HASH256: return BuildScript(OP_SIZE, 32, OP_EQUALVERIFY, OP_HASH256, node.data, verify ? OP_EQUALVERIFY : OP_EQUAL);
824
375
                case Fragment::HASH160: return BuildScript(OP_SIZE, 32, OP_EQUALVERIFY, OP_HASH160, node.data, verify ? OP_EQUALVERIFY : OP_EQUAL);
825
375
                case Fragment::WRAP_A: return BuildScript(OP_TOALTSTACK, subs[0], OP_FROMALTSTACK);
826
375
                case Fragment::WRAP_S: return BuildScript(OP_SWAP, subs[0]);
827
375
                case Fragment::WRAP_C: return BuildScript(std::move(subs[0]), verify ? OP_CHECKSIGVERIFY : OP_CHECKSIG);
828
375
                case Fragment::WRAP_D: return BuildScript(OP_DUP, OP_IF, subs[0], OP_ENDIF);
829
375
                case Fragment::WRAP_V: {
830
375
                    if (node.subs[0].GetType() << "x"_mst) {
831
375
                        return BuildScript(std::move(subs[0]), OP_VERIFY);
832
375
                    } else {
833
375
                        return std::move(subs[0]);
834
375
                    }
835
375
                }
836
375
                case Fragment::WRAP_J: return BuildScript(OP_SIZE, OP_0NOTEQUAL, OP_IF, subs[0], OP_ENDIF);
837
375
                case Fragment::WRAP_N: return BuildScript(std::move(subs[0]), OP_0NOTEQUAL);
838
375
                case Fragment::JUST_1: return BuildScript(OP_1);
839
375
                case Fragment::JUST_0: return BuildScript(OP_0);
840
375
                case Fragment::AND_V: return BuildScript(std::move(subs[0]), subs[1]);
841
375
                case Fragment::AND_B: return BuildScript(std::move(subs[0]), subs[1], OP_BOOLAND);
842
375
                case Fragment::OR_B: return BuildScript(std::move(subs[0]), subs[1], OP_BOOLOR);
843
375
                case Fragment::OR_D: return BuildScript(std::move(subs[0]), OP_IFDUP, OP_NOTIF, subs[1], OP_ENDIF);
844
375
                case Fragment::OR_C: return BuildScript(std::move(subs[0]), OP_NOTIF, subs[1], OP_ENDIF);
845
375
                case Fragment::OR_I: return BuildScript(OP_IF, subs[0], OP_ELSE, subs[1], OP_ENDIF);
846
375
                case Fragment::ANDOR: return BuildScript(std::move(subs[0]), OP_NOTIF, subs[2], OP_ELSE, subs[1], OP_ENDIF);
847
375
                case Fragment::MULTI: {
848
375
                    CHECK_NONFATAL(!is_tapscript);
849
375
                    CScript script = BuildScript(node.k);
850
375
                    for (const auto& key : node.keys) {
851
375
                        script = BuildScript(std::move(script), ctx.ToPKBytes(key));
852
375
                    }
853
375
                    return BuildScript(std::move(script), node.keys.size(), verify ? OP_CHECKMULTISIGVERIFY : OP_CHECKMULTISIG);
854
375
                }
855
375
                case Fragment::MULTI_A: {
856
375
                    CHECK_NONFATAL(is_tapscript);
857
375
                    CScript script = BuildScript(ctx.ToPKBytes(*node.keys.begin()), OP_CHECKSIG);
858
375
                    for (auto it = node.keys.begin() + 1; it != node.keys.end(); ++it) {
859
375
                        script = BuildScript(std::move(script), ctx.ToPKBytes(*it), OP_CHECKSIGADD);
860
375
                    }
861
375
                    return BuildScript(std::move(script), node.k, verify ? OP_NUMEQUALVERIFY : OP_NUMEQUAL);
862
375
                }
863
375
                case Fragment::THRESH: {
864
375
                    CScript script = std::move(subs[0]);
865
375
                    for (size_t i = 1; i < subs.size(); ++i) {
866
375
                        script = BuildScript(std::move(script), subs[i], OP_ADD);
867
375
                    }
868
375
                    return BuildScript(std::move(script), node.k, verify ? OP_EQUALVERIFY : OP_EQUAL);
869
375
                }
870
375
            }
871
375
            assert(false);
872
375
        };
873
375
        return TreeEval<CScript>(false, downfn, upfn);
874
375
    }
descriptor.cpp:CScript miniscript::Node<unsigned int>::ToScript<(anonymous namespace)::ScriptMaker>((anonymous namespace)::ScriptMaker const&) const
Line
Count
Source
799
1.69k
    {
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
1.69k
        auto downfn = [](bool verify, const Node& node, size_t index) {
804
            // For WRAP_V, the subexpression is certainly followed by OP_VERIFY.
805
1.69k
            if (node.fragment == Fragment::WRAP_V) return true;
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
1.69k
            if (node.fragment == Fragment::WRAP_S ||
809
1.69k
                (node.fragment == Fragment::AND_V && index == 1)) return verify;
810
1.69k
            return false;
811
1.69k
        };
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
1.69k
        const bool is_tapscript{IsTapscript(m_script_ctx)};
815
1.69k
        auto upfn = [&ctx, is_tapscript](bool verify, const Node& node, std::span<CScript> subs) -> CScript {
816
1.69k
            switch (node.fragment) {
817
1.69k
                case Fragment::PK_K: return BuildScript(ctx.ToPKBytes(node.keys[0]));
818
1.69k
                case Fragment::PK_H: return BuildScript(OP_DUP, OP_HASH160, ctx.ToPKHBytes(node.keys[0]), OP_EQUALVERIFY);
819
1.69k
                case Fragment::OLDER: return BuildScript(node.k, OP_CHECKSEQUENCEVERIFY);
820
1.69k
                case Fragment::AFTER: return BuildScript(node.k, OP_CHECKLOCKTIMEVERIFY);
821
1.69k
                case Fragment::SHA256: return BuildScript(OP_SIZE, 32, OP_EQUALVERIFY, OP_SHA256, node.data, verify ? OP_EQUALVERIFY : OP_EQUAL);
822
1.69k
                case Fragment::RIPEMD160: return BuildScript(OP_SIZE, 32, OP_EQUALVERIFY, OP_RIPEMD160, node.data, verify ? OP_EQUALVERIFY : OP_EQUAL);
823
1.69k
                case Fragment::HASH256: return BuildScript(OP_SIZE, 32, OP_EQUALVERIFY, OP_HASH256, node.data, verify ? OP_EQUALVERIFY : OP_EQUAL);
824
1.69k
                case Fragment::HASH160: return BuildScript(OP_SIZE, 32, OP_EQUALVERIFY, OP_HASH160, node.data, verify ? OP_EQUALVERIFY : OP_EQUAL);
825
1.69k
                case Fragment::WRAP_A: return BuildScript(OP_TOALTSTACK, subs[0], OP_FROMALTSTACK);
826
1.69k
                case Fragment::WRAP_S: return BuildScript(OP_SWAP, subs[0]);
827
1.69k
                case Fragment::WRAP_C: return BuildScript(std::move(subs[0]), verify ? OP_CHECKSIGVERIFY : OP_CHECKSIG);
828
1.69k
                case Fragment::WRAP_D: return BuildScript(OP_DUP, OP_IF, subs[0], OP_ENDIF);
829
1.69k
                case Fragment::WRAP_V: {
830
1.69k
                    if (node.subs[0].GetType() << "x"_mst) {
831
1.69k
                        return BuildScript(std::move(subs[0]), OP_VERIFY);
832
1.69k
                    } else {
833
1.69k
                        return std::move(subs[0]);
834
1.69k
                    }
835
1.69k
                }
836
1.69k
                case Fragment::WRAP_J: return BuildScript(OP_SIZE, OP_0NOTEQUAL, OP_IF, subs[0], OP_ENDIF);
837
1.69k
                case Fragment::WRAP_N: return BuildScript(std::move(subs[0]), OP_0NOTEQUAL);
838
1.69k
                case Fragment::JUST_1: return BuildScript(OP_1);
839
1.69k
                case Fragment::JUST_0: return BuildScript(OP_0);
840
1.69k
                case Fragment::AND_V: return BuildScript(std::move(subs[0]), subs[1]);
841
1.69k
                case Fragment::AND_B: return BuildScript(std::move(subs[0]), subs[1], OP_BOOLAND);
842
1.69k
                case Fragment::OR_B: return BuildScript(std::move(subs[0]), subs[1], OP_BOOLOR);
843
1.69k
                case Fragment::OR_D: return BuildScript(std::move(subs[0]), OP_IFDUP, OP_NOTIF, subs[1], OP_ENDIF);
844
1.69k
                case Fragment::OR_C: return BuildScript(std::move(subs[0]), OP_NOTIF, subs[1], OP_ENDIF);
845
1.69k
                case Fragment::OR_I: return BuildScript(OP_IF, subs[0], OP_ELSE, subs[1], OP_ENDIF);
846
1.69k
                case Fragment::ANDOR: return BuildScript(std::move(subs[0]), OP_NOTIF, subs[2], OP_ELSE, subs[1], OP_ENDIF);
847
1.69k
                case Fragment::MULTI: {
848
1.69k
                    CHECK_NONFATAL(!is_tapscript);
849
1.69k
                    CScript script = BuildScript(node.k);
850
1.69k
                    for (const auto& key : node.keys) {
851
1.69k
                        script = BuildScript(std::move(script), ctx.ToPKBytes(key));
852
1.69k
                    }
853
1.69k
                    return BuildScript(std::move(script), node.keys.size(), verify ? OP_CHECKMULTISIGVERIFY : OP_CHECKMULTISIG);
854
1.69k
                }
855
1.69k
                case Fragment::MULTI_A: {
856
1.69k
                    CHECK_NONFATAL(is_tapscript);
857
1.69k
                    CScript script = BuildScript(ctx.ToPKBytes(*node.keys.begin()), OP_CHECKSIG);
858
1.69k
                    for (auto it = node.keys.begin() + 1; it != node.keys.end(); ++it) {
859
1.69k
                        script = BuildScript(std::move(script), ctx.ToPKBytes(*it), OP_CHECKSIGADD);
860
1.69k
                    }
861
1.69k
                    return BuildScript(std::move(script), node.k, verify ? OP_NUMEQUALVERIFY : OP_NUMEQUAL);
862
1.69k
                }
863
1.69k
                case Fragment::THRESH: {
864
1.69k
                    CScript script = std::move(subs[0]);
865
1.69k
                    for (size_t i = 1; i < subs.size(); ++i) {
866
1.69k
                        script = BuildScript(std::move(script), subs[i], OP_ADD);
867
1.69k
                    }
868
1.69k
                    return BuildScript(std::move(script), node.k, verify ? OP_EQUALVERIFY : OP_EQUAL);
869
1.69k
                }
870
1.69k
            }
871
1.69k
            assert(false);
872
1.69k
        };
873
1.69k
        return TreeEval<CScript>(false, downfn, upfn);
874
1.69k
    }
875
876
    template<typename CTx>
877
17
    std::optional<std::string> ToString(const CTx& ctx) const {
878
17
        bool dummy{false};
879
17
        return ToString(ctx, dummy);
880
17
    }
miniscript_tests.cpp:std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<CPubKey>::ToString<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const
Line
Count
Source
877
1
    std::optional<std::string> ToString(const CTx& ctx) const {
878
1
        bool dummy{false};
879
1
        return ToString(ctx, dummy);
880
1
    }
descriptor.cpp:std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<unsigned int>::ToString<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const
Line
Count
Source
877
16
    std::optional<std::string> ToString(const CTx& ctx) const {
878
16
        bool dummy{false};
879
16
        return ToString(ctx, dummy);
880
16
    }
881
882
    template<typename CTx>
883
1.36k
    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
3.30M
        auto downfn = [](bool, const Node& node, size_t) {
888
3.30M
            return (node.fragment == Fragment::WRAP_A || node.fragment == Fragment::WRAP_S ||
889
3.30M
                    node.fragment == Fragment::WRAP_D || node.fragment == Fragment::WRAP_V ||
890
3.30M
                    node.fragment == Fragment::WRAP_J || node.fragment == Fragment::WRAP_N ||
891
3.30M
                    node.fragment == Fragment::WRAP_C ||
892
3.30M
                    (node.fragment == Fragment::AND_V && node.subs[1].fragment == Fragment::JUST_1) ||
893
3.30M
                    (node.fragment == Fragment::OR_I && node.subs[0].fragment == Fragment::JUST_0) ||
894
3.30M
                    (node.fragment == Fragment::OR_I && node.subs[1].fragment == Fragment::JUST_0));
895
3.30M
        };
miniscript_tests.cpp:std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<CPubKey>::ToString<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&, bool&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, unsigned long)::operator()(bool, miniscript::Node<CPubKey> const&, unsigned long) const
Line
Count
Source
887
3
        auto downfn = [](bool, const Node& node, size_t) {
888
3
            return (node.fragment == Fragment::WRAP_A || node.fragment == Fragment::WRAP_S ||
889
3
                    node.fragment == Fragment::WRAP_D || node.fragment == Fragment::WRAP_V ||
890
3
                    node.fragment == Fragment::WRAP_J || node.fragment == Fragment::WRAP_N ||
891
3
                    node.fragment == Fragment::WRAP_C ||
892
3
                    (node.fragment == Fragment::AND_V && node.subs[1].fragment == Fragment::JUST_1) ||
893
3
                    (node.fragment == Fragment::OR_I && node.subs[0].fragment == Fragment::JUST_0) ||
894
3
                    (node.fragment == Fragment::OR_I && node.subs[1].fragment == Fragment::JUST_0));
895
3
        };
descriptor.cpp:std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<unsigned int>::ToString<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&, bool&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, unsigned long)::operator()(bool, miniscript::Node<unsigned int> const&, unsigned long) const
Line
Count
Source
887
75
        auto downfn = [](bool, const Node& node, size_t) {
888
75
            return (node.fragment == Fragment::WRAP_A || node.fragment == Fragment::WRAP_S ||
889
75
                    node.fragment == Fragment::WRAP_D || node.fragment == Fragment::WRAP_V ||
890
75
                    node.fragment == Fragment::WRAP_J || node.fragment == Fragment::WRAP_N ||
891
75
                    node.fragment == Fragment::WRAP_C ||
892
75
                    (node.fragment == Fragment::AND_V && node.subs[1].fragment == Fragment::JUST_1) ||
893
75
                    (node.fragment == Fragment::OR_I && node.subs[0].fragment == Fragment::JUST_0) ||
894
75
                    (node.fragment == Fragment::OR_I && node.subs[1].fragment == Fragment::JUST_0));
895
75
        };
descriptor.cpp:std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<unsigned int>::ToString<(anonymous namespace)::StringMaker>((anonymous namespace)::StringMaker const&, bool&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, unsigned long)::operator()(bool, miniscript::Node<unsigned int> const&, unsigned long) const
Line
Count
Source
887
3.30M
        auto downfn = [](bool, const Node& node, size_t) {
888
3.30M
            return (node.fragment == Fragment::WRAP_A || node.fragment == Fragment::WRAP_S ||
889
3.30M
                    node.fragment == Fragment::WRAP_D || node.fragment == Fragment::WRAP_V ||
890
3.30M
                    node.fragment == Fragment::WRAP_J || node.fragment == Fragment::WRAP_N ||
891
3.30M
                    node.fragment == Fragment::WRAP_C ||
892
3.30M
                    (node.fragment == Fragment::AND_V && node.subs[1].fragment == Fragment::JUST_1) ||
893
3.30M
                    (node.fragment == Fragment::OR_I && node.subs[0].fragment == Fragment::JUST_0) ||
894
3.30M
                    (node.fragment == Fragment::OR_I && node.subs[1].fragment == Fragment::JUST_0));
895
3.30M
        };
896
6.12k
        auto toString = [&ctx, &has_priv_key](Key key) -> std::optional<std::string> {
897
6.12k
            bool fragment_has_priv_key{false};
898
6.12k
            auto key_str{ctx.ToString(key, fragment_has_priv_key)};
899
6.12k
            if (key_str) has_priv_key = has_priv_key || fragment_has_priv_key;
900
6.12k
            return key_str;
901
6.12k
        };
Unexecuted instantiation: miniscript_tests.cpp:std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<CPubKey>::ToString<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&, bool&) const::'lambda'(CPubKey)::operator()[abi:cxx11](CPubKey) const
descriptor.cpp:std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<unsigned int>::ToString<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&, bool&) const::'lambda'(unsigned int)::operator()[abi:cxx11](unsigned int) const
Line
Count
Source
896
38
        auto toString = [&ctx, &has_priv_key](Key key) -> std::optional<std::string> {
897
38
            bool fragment_has_priv_key{false};
898
38
            auto key_str{ctx.ToString(key, fragment_has_priv_key)};
899
38
            if (key_str) has_priv_key = has_priv_key || fragment_has_priv_key;
900
38
            return key_str;
901
38
        };
descriptor.cpp:std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<unsigned int>::ToString<(anonymous namespace)::StringMaker>((anonymous namespace)::StringMaker const&, bool&) const::'lambda'(unsigned int)::operator()[abi:cxx11](unsigned int) const
Line
Count
Source
896
6.08k
        auto toString = [&ctx, &has_priv_key](Key key) -> std::optional<std::string> {
897
6.08k
            bool fragment_has_priv_key{false};
898
6.08k
            auto key_str{ctx.ToString(key, fragment_has_priv_key)};
899
6.08k
            if (key_str) has_priv_key = has_priv_key || fragment_has_priv_key;
900
6.08k
            return key_str;
901
6.08k
        };
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
1.36k
        const bool is_tapscript{IsTapscript(m_script_ctx)};
905
3.30M
        auto upfn = [is_tapscript, &toString](bool wrapped, const Node& node, std::span<std::string> subs) -> std::optional<std::string> {
906
3.30M
            std::string ret = wrapped ? ":" : "";
907
908
3.30M
            switch (node.fragment) {
909
650
                case Fragment::WRAP_A: return "a" + std::move(subs[0]);
910
400
                case Fragment::WRAP_S: return "s" + std::move(subs[0]);
911
2.85k
                case Fragment::WRAP_C:
912
2.85k
                    if (node.subs[0].fragment == Fragment::PK_K) {
913
                        // pk(K) is syntactic sugar for c:pk_k(K)
914
2.15k
                        auto key_str = toString(node.subs[0].keys[0]);
915
2.15k
                        if (!key_str) return {};
916
2.15k
                        return std::move(ret) + "pk(" + std::move(*key_str) + ")";
917
2.15k
                    }
918
700
                    if (node.subs[0].fragment == Fragment::PK_H) {
919
                        // pkh(K) is syntactic sugar for c:pk_h(K)
920
677
                        auto key_str = toString(node.subs[0].keys[0]);
921
677
                        if (!key_str) return {};
922
677
                        return std::move(ret) + "pkh(" + std::move(*key_str) + ")";
923
677
                    }
924
23
                    return "c" + std::move(subs[0]);
925
94
                case Fragment::WRAP_D: return "d" + std::move(subs[0]);
926
1.29k
                case Fragment::WRAP_V: return "v" + std::move(subs[0]);
927
0
                case Fragment::WRAP_J: return "j" + std::move(subs[0]);
928
3.29M
                case Fragment::WRAP_N: return "n" + std::move(subs[0]);
929
1.19k
                case Fragment::AND_V:
930
                    // t:X is syntactic sugar for and_v(X,1).
931
1.19k
                    if (node.subs[1].fragment == Fragment::JUST_1) return "t" + std::move(subs[0]);
932
1.19k
                    break;
933
1.19k
                case Fragment::OR_I:
934
247
                    if (node.subs[0].fragment == Fragment::JUST_0) return "l" + std::move(subs[1]);
935
112
                    if (node.subs[1].fragment == Fragment::JUST_0) return "u" + std::move(subs[0]);
936
112
                    break;
937
5.44k
                default: break;
938
3.30M
            }
939
6.74k
            switch (node.fragment) {
940
2.19k
                case Fragment::PK_K: {
941
2.19k
                    auto key_str = toString(node.keys[0]);
942
2.19k
                    if (!key_str) return {};
943
2.19k
                    return std::move(ret) + "pk_k(" + std::move(*key_str) + ")";
944
2.19k
                }
945
677
                case Fragment::PK_H: {
946
677
                    auto key_str = toString(node.keys[0]);
947
677
                    if (!key_str) return {};
948
677
                    return std::move(ret) + "pk_h(" + std::move(*key_str) + ")";
949
677
                }
950
483
                case Fragment::AFTER: return std::move(ret) + "after(" + util::ToString(node.k) + ")";
951
448
                case Fragment::OLDER: return std::move(ret) + "older(" + util::ToString(node.k) + ")";
952
38
                case Fragment::HASH256: return std::move(ret) + "hash256(" + HexStr(node.data) + ")";
953
71
                case Fragment::HASH160: return std::move(ret) + "hash160(" + HexStr(node.data) + ")";
954
77
                case Fragment::SHA256: return std::move(ret) + "sha256(" + HexStr(node.data) + ")";
955
42
                case Fragment::RIPEMD160: return std::move(ret) + "ripemd160(" + HexStr(node.data) + ")";
956
6
                case Fragment::JUST_1: return std::move(ret) + "1";
957
184
                case Fragment::JUST_0: return std::move(ret) + "0";
958
1.19k
                case Fragment::AND_V: return std::move(ret) + "and_v(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
959
384
                case Fragment::AND_B: return std::move(ret) + "and_b(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
960
104
                case Fragment::OR_B: return std::move(ret) + "or_b(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
961
86
                case Fragment::OR_D: return std::move(ret) + "or_d(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
962
46
                case Fragment::OR_C: return std::move(ret) + "or_c(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
963
112
                case Fragment::OR_I: return std::move(ret) + "or_i(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
964
198
                case Fragment::ANDOR:
965
                    // and_n(X,Y) is syntactic sugar for andor(X,Y,0).
966
198
                    if (node.subs[2].fragment == Fragment::JUST_0) return std::move(ret) + "and_n(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
967
150
                    return std::move(ret) + "andor(" + std::move(subs[0]) + "," + std::move(subs[1]) + "," + std::move(subs[2]) + ")";
968
109
                case Fragment::MULTI: {
969
109
                    CHECK_NONFATAL(!is_tapscript);
970
109
                    auto str = std::move(ret) + "multi(" + util::ToString(node.k);
971
274
                    for (const auto& key : node.keys) {
972
274
                        auto key_str = toString(key);
973
274
                        if (!key_str) return {};
974
274
                        str += "," + std::move(*key_str);
975
274
                    }
976
109
                    return std::move(str) + ")";
977
109
                }
978
57
                case Fragment::MULTI_A: {
979
57
                    CHECK_NONFATAL(is_tapscript);
980
57
                    auto str = std::move(ret) + "multi_a(" + util::ToString(node.k);
981
146
                    for (const auto& key : node.keys) {
982
146
                        auto key_str = toString(key);
983
146
                        if (!key_str) return {};
984
146
                        str += "," + std::move(*key_str);
985
146
                    }
986
57
                    return std::move(str) + ")";
987
57
                }
988
234
                case Fragment::THRESH: {
989
234
                    auto str = std::move(ret) + "thresh(" + util::ToString(node.k);
990
798
                    for (auto& sub : subs) {
991
798
                        str += "," + std::move(sub);
992
798
                    }
993
234
                    return std::move(str) + ")";
994
57
                }
995
0
                default: break;
996
6.74k
            }
997
6.74k
            assert(false);
998
0
        };
miniscript_tests.cpp:std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<CPubKey>::ToString<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&, bool&) const::'lambda'(bool, miniscript::Node<CPubKey> const&, std::span<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, 18446744073709551615ul>)::operator()[abi:cxx11](bool, miniscript::Node<CPubKey> const&, std::span<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, 18446744073709551615ul>) const
Line
Count
Source
905
4
        auto upfn = [is_tapscript, &toString](bool wrapped, const Node& node, std::span<std::string> subs) -> std::optional<std::string> {
906
4
            std::string ret = wrapped ? ":" : "";
907
908
4
            switch (node.fragment) {
909
1
                case Fragment::WRAP_A: return "a" + std::move(subs[0]);
910
0
                case Fragment::WRAP_S: return "s" + std::move(subs[0]);
911
0
                case Fragment::WRAP_C:
912
0
                    if (node.subs[0].fragment == Fragment::PK_K) {
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 {};
916
0
                        return std::move(ret) + "pk(" + std::move(*key_str) + ")";
917
0
                    }
918
0
                    if (node.subs[0].fragment == Fragment::PK_H) {
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 {};
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]);
926
0
                case Fragment::WRAP_V: return "v" + std::move(subs[0]);
927
0
                case Fragment::WRAP_J: return "j" + std::move(subs[0]);
928
0
                case Fragment::WRAP_N: return "n" + std::move(subs[0]);
929
0
                case Fragment::AND_V:
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]);
932
0
                    break;
933
0
                case Fragment::OR_I:
934
0
                    if (node.subs[0].fragment == Fragment::JUST_0) return "l" + std::move(subs[1]);
935
0
                    if (node.subs[1].fragment == Fragment::JUST_0) return "u" + std::move(subs[0]);
936
0
                    break;
937
3
                default: break;
938
4
            }
939
3
            switch (node.fragment) {
940
0
                case Fragment::PK_K: {
941
0
                    auto key_str = toString(node.keys[0]);
942
0
                    if (!key_str) return {};
943
0
                    return std::move(ret) + "pk_k(" + std::move(*key_str) + ")";
944
0
                }
945
0
                case Fragment::PK_H: {
946
0
                    auto key_str = toString(node.keys[0]);
947
0
                    if (!key_str) return {};
948
0
                    return std::move(ret) + "pk_h(" + std::move(*key_str) + ")";
949
0
                }
950
2
                case Fragment::AFTER: return std::move(ret) + "after(" + util::ToString(node.k) + ")";
951
0
                case Fragment::OLDER: return std::move(ret) + "older(" + util::ToString(node.k) + ")";
952
0
                case Fragment::HASH256: return std::move(ret) + "hash256(" + HexStr(node.data) + ")";
953
0
                case Fragment::HASH160: return std::move(ret) + "hash160(" + HexStr(node.data) + ")";
954
0
                case Fragment::SHA256: return std::move(ret) + "sha256(" + HexStr(node.data) + ")";
955
0
                case Fragment::RIPEMD160: return std::move(ret) + "ripemd160(" + HexStr(node.data) + ")";
956
0
                case Fragment::JUST_1: return std::move(ret) + "1";
957
0
                case Fragment::JUST_0: return std::move(ret) + "0";
958
0
                case Fragment::AND_V: return std::move(ret) + "and_v(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
959
1
                case Fragment::AND_B: return std::move(ret) + "and_b(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
960
0
                case Fragment::OR_B: return std::move(ret) + "or_b(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
961
0
                case Fragment::OR_D: return std::move(ret) + "or_d(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
962
0
                case Fragment::OR_C: return std::move(ret) + "or_c(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
963
0
                case Fragment::OR_I: return std::move(ret) + "or_i(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
964
0
                case Fragment::ANDOR:
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]) + ")";
967
0
                    return std::move(ret) + "andor(" + std::move(subs[0]) + "," + std::move(subs[1]) + "," + std::move(subs[2]) + ")";
968
0
                case Fragment::MULTI: {
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) {
972
0
                        auto key_str = toString(key);
973
0
                        if (!key_str) return {};
974
0
                        str += "," + std::move(*key_str);
975
0
                    }
976
0
                    return std::move(str) + ")";
977
0
                }
978
0
                case Fragment::MULTI_A: {
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) {
982
0
                        auto key_str = toString(key);
983
0
                        if (!key_str) return {};
984
0
                        str += "," + std::move(*key_str);
985
0
                    }
986
0
                    return std::move(str) + ")";
987
0
                }
988
0
                case Fragment::THRESH: {
989
0
                    auto str = std::move(ret) + "thresh(" + util::ToString(node.k);
990
0
                    for (auto& sub : subs) {
991
0
                        str += "," + std::move(sub);
992
0
                    }
993
0
                    return std::move(str) + ")";
994
0
                }
995
0
                default: break;
996
3
            }
997
3
            assert(false);
998
0
        };
descriptor.cpp:std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<unsigned int>::ToString<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&, bool&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, std::span<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, 18446744073709551615ul>)::operator()[abi:cxx11](bool, miniscript::Node<unsigned int> const&, std::span<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, 18446744073709551615ul>) const
Line
Count
Source
905
91
        auto upfn = [is_tapscript, &toString](bool wrapped, const Node& node, std::span<std::string> subs) -> std::optional<std::string> {
906
91
            std::string ret = wrapped ? ":" : "";
907
908
91
            switch (node.fragment) {
909
3
                case Fragment::WRAP_A: return "a" + std::move(subs[0]);
910
6
                case Fragment::WRAP_S: return "s" + std::move(subs[0]);
911
18
                case Fragment::WRAP_C:
912
18
                    if (node.subs[0].fragment == Fragment::PK_K) {
913
                        // pk(K) is syntactic sugar for c:pk_k(K)
914
14
                        auto key_str = toString(node.subs[0].keys[0]);
915
14
                        if (!key_str) return {};
916
14
                        return std::move(ret) + "pk(" + std::move(*key_str) + ")";
917
14
                    }
918
4
                    if (node.subs[0].fragment == Fragment::PK_H) {
919
                        // pkh(K) is syntactic sugar for c:pk_h(K)
920
2
                        auto key_str = toString(node.subs[0].keys[0]);
921
2
                        if (!key_str) return {};
922
2
                        return std::move(ret) + "pkh(" + std::move(*key_str) + ")";
923
2
                    }
924
2
                    return "c" + std::move(subs[0]);
925
0
                case Fragment::WRAP_D: return "d" + std::move(subs[0]);
926
8
                case Fragment::WRAP_V: return "v" + std::move(subs[0]);
927
0
                case Fragment::WRAP_J: return "j" + std::move(subs[0]);
928
0
                case Fragment::WRAP_N: return "n" + std::move(subs[0]);
929
4
                case Fragment::AND_V:
930
                    // t:X is syntactic sugar for and_v(X,1).
931
4
                    if (node.subs[1].fragment == Fragment::JUST_1) return "t" + std::move(subs[0]);
932
4
                    break;
933
4
                case Fragment::OR_I:
934
2
                    if (node.subs[0].fragment == Fragment::JUST_0) return "l" + std::move(subs[1]);
935
2
                    if (node.subs[1].fragment == Fragment::JUST_0) return "u" + std::move(subs[0]);
936
2
                    break;
937
50
                default: break;
938
91
            }
939
56
            switch (node.fragment) {
940
20
                case Fragment::PK_K: {
941
20
                    auto key_str = toString(node.keys[0]);
942
20
                    if (!key_str) return {};
943
20
                    return std::move(ret) + "pk_k(" + std::move(*key_str) + ")";
944
20
                }
945
2
                case Fragment::PK_H: {
946
2
                    auto key_str = toString(node.keys[0]);
947
2
                    if (!key_str) return {};
948
2
                    return std::move(ret) + "pk_h(" + std::move(*key_str) + ")";
949
2
                }
950
2
                case Fragment::AFTER: return std::move(ret) + "after(" + util::ToString(node.k) + ")";
951
8
                case Fragment::OLDER: return std::move(ret) + "older(" + util::ToString(node.k) + ")";
952
0
                case Fragment::HASH256: return std::move(ret) + "hash256(" + HexStr(node.data) + ")";
953
0
                case Fragment::HASH160: return std::move(ret) + "hash160(" + HexStr(node.data) + ")";
954
2
                case Fragment::SHA256: return std::move(ret) + "sha256(" + HexStr(node.data) + ")";
955
1
                case Fragment::RIPEMD160: return std::move(ret) + "ripemd160(" + HexStr(node.data) + ")";
956
1
                case Fragment::JUST_1: return std::move(ret) + "1";
957
1
                case Fragment::JUST_0: return std::move(ret) + "0";
958
4
                case Fragment::AND_V: return std::move(ret) + "and_v(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
959
7
                case Fragment::AND_B: return std::move(ret) + "and_b(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
960
4
                case Fragment::OR_B: return std::move(ret) + "or_b(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
961
0
                case Fragment::OR_D: return std::move(ret) + "or_d(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
962
0
                case Fragment::OR_C: return std::move(ret) + "or_c(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
963
2
                case Fragment::OR_I: return std::move(ret) + "or_i(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
964
2
                case Fragment::ANDOR:
965
                    // and_n(X,Y) is syntactic sugar for andor(X,Y,0).
966
2
                    if (node.subs[2].fragment == Fragment::JUST_0) return std::move(ret) + "and_n(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
967
2
                    return std::move(ret) + "andor(" + std::move(subs[0]) + "," + std::move(subs[1]) + "," + std::move(subs[2]) + ")";
968
0
                case Fragment::MULTI: {
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) {
972
0
                        auto key_str = toString(key);
973
0
                        if (!key_str) return {};
974
0
                        str += "," + std::move(*key_str);
975
0
                    }
976
0
                    return std::move(str) + ")";
977
0
                }
978
0
                case Fragment::MULTI_A: {
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) {
982
0
                        auto key_str = toString(key);
983
0
                        if (!key_str) return {};
984
0
                        str += "," + std::move(*key_str);
985
0
                    }
986
0
                    return std::move(str) + ")";
987
0
                }
988
0
                case Fragment::THRESH: {
989
0
                    auto str = std::move(ret) + "thresh(" + util::ToString(node.k);
990
0
                    for (auto& sub : subs) {
991
0
                        str += "," + std::move(sub);
992
0
                    }
993
0
                    return std::move(str) + ")";
994
0
                }
995
0
                default: break;
996
56
            }
997
56
            assert(false);
998
0
        };
descriptor.cpp:std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<unsigned int>::ToString<(anonymous namespace)::StringMaker>((anonymous namespace)::StringMaker const&, bool&) const::'lambda'(bool, miniscript::Node<unsigned int> const&, std::span<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, 18446744073709551615ul>)::operator()[abi:cxx11](bool, miniscript::Node<unsigned int> const&, std::span<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, 18446744073709551615ul>) const
Line
Count
Source
905
3.30M
        auto upfn = [is_tapscript, &toString](bool wrapped, const Node& node, std::span<std::string> subs) -> std::optional<std::string> {
906
3.30M
            std::string ret = wrapped ? ":" : "";
907
908
3.30M
            switch (node.fragment) {
909
646
                case Fragment::WRAP_A: return "a" + std::move(subs[0]);
910
394
                case Fragment::WRAP_S: return "s" + std::move(subs[0]);
911
2.83k
                case Fragment::WRAP_C:
912
2.83k
                    if (node.subs[0].fragment == Fragment::PK_K) {
913
                        // pk(K) is syntactic sugar for c:pk_k(K)
914
2.13k
                        auto key_str = toString(node.subs[0].keys[0]);
915
2.13k
                        if (!key_str) return {};
916
2.13k
                        return std::move(ret) + "pk(" + std::move(*key_str) + ")";
917
2.13k
                    }
918
696
                    if (node.subs[0].fragment == Fragment::PK_H) {
919
                        // pkh(K) is syntactic sugar for c:pk_h(K)
920
675
                        auto key_str = toString(node.subs[0].keys[0]);
921
675
                        if (!key_str) return {};
922
675
                        return std::move(ret) + "pkh(" + std::move(*key_str) + ")";
923
675
                    }
924
21
                    return "c" + std::move(subs[0]);
925
94
                case Fragment::WRAP_D: return "d" + std::move(subs[0]);
926
1.28k
                case Fragment::WRAP_V: return "v" + std::move(subs[0]);
927
0
                case Fragment::WRAP_J: return "j" + std::move(subs[0]);
928
3.29M
                case Fragment::WRAP_N: return "n" + std::move(subs[0]);
929
1.19k
                case Fragment::AND_V:
930
                    // t:X is syntactic sugar for and_v(X,1).
931
1.19k
                    if (node.subs[1].fragment == Fragment::JUST_1) return "t" + std::move(subs[0]);
932
1.18k
                    break;
933
1.18k
                case Fragment::OR_I:
934
245
                    if (node.subs[0].fragment == Fragment::JUST_0) return "l" + std::move(subs[1]);
935
110
                    if (node.subs[1].fragment == Fragment::JUST_0) return "u" + std::move(subs[0]);
936
110
                    break;
937
5.39k
                default: break;
938
3.30M
            }
939
6.68k
            switch (node.fragment) {
940
2.17k
                case Fragment::PK_K: {
941
2.17k
                    auto key_str = toString(node.keys[0]);
942
2.17k
                    if (!key_str) return {};
943
2.17k
                    return std::move(ret) + "pk_k(" + std::move(*key_str) + ")";
944
2.17k
                }
945
675
                case Fragment::PK_H: {
946
675
                    auto key_str = toString(node.keys[0]);
947
675
                    if (!key_str) return {};
948
675
                    return std::move(ret) + "pk_h(" + std::move(*key_str) + ")";
949
675
                }
950
479
                case Fragment::AFTER: return std::move(ret) + "after(" + util::ToString(node.k) + ")";
951
440
                case Fragment::OLDER: return std::move(ret) + "older(" + util::ToString(node.k) + ")";
952
38
                case Fragment::HASH256: return std::move(ret) + "hash256(" + HexStr(node.data) + ")";
953
71
                case Fragment::HASH160: return std::move(ret) + "hash160(" + HexStr(node.data) + ")";
954
75
                case Fragment::SHA256: return std::move(ret) + "sha256(" + HexStr(node.data) + ")";
955
41
                case Fragment::RIPEMD160: return std::move(ret) + "ripemd160(" + HexStr(node.data) + ")";
956
5
                case Fragment::JUST_1: return std::move(ret) + "1";
957
183
                case Fragment::JUST_0: return std::move(ret) + "0";
958
1.18k
                case Fragment::AND_V: return std::move(ret) + "and_v(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
959
376
                case Fragment::AND_B: return std::move(ret) + "and_b(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
960
100
                case Fragment::OR_B: return std::move(ret) + "or_b(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
961
86
                case Fragment::OR_D: return std::move(ret) + "or_d(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
962
46
                case Fragment::OR_C: return std::move(ret) + "or_c(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
963
110
                case Fragment::OR_I: return std::move(ret) + "or_i(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
964
196
                case Fragment::ANDOR:
965
                    // and_n(X,Y) is syntactic sugar for andor(X,Y,0).
966
196
                    if (node.subs[2].fragment == Fragment::JUST_0) return std::move(ret) + "and_n(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
967
148
                    return std::move(ret) + "andor(" + std::move(subs[0]) + "," + std::move(subs[1]) + "," + std::move(subs[2]) + ")";
968
109
                case Fragment::MULTI: {
969
109
                    CHECK_NONFATAL(!is_tapscript);
970
109
                    auto str = std::move(ret) + "multi(" + util::ToString(node.k);
971
274
                    for (const auto& key : node.keys) {
972
274
                        auto key_str = toString(key);
973
274
                        if (!key_str) return {};
974
274
                        str += "," + std::move(*key_str);
975
274
                    }
976
109
                    return std::move(str) + ")";
977
109
                }
978
57
                case Fragment::MULTI_A: {
979
57
                    CHECK_NONFATAL(is_tapscript);
980
57
                    auto str = std::move(ret) + "multi_a(" + util::ToString(node.k);
981
146
                    for (const auto& key : node.keys) {
982
146
                        auto key_str = toString(key);
983
146
                        if (!key_str) return {};
984
146
                        str += "," + std::move(*key_str);
985
146
                    }
986
57
                    return std::move(str) + ")";
987
57
                }
988
234
                case Fragment::THRESH: {
989
234
                    auto str = std::move(ret) + "thresh(" + util::ToString(node.k);
990
798
                    for (auto& sub : subs) {
991
798
                        str += "," + std::move(sub);
992
798
                    }
993
234
                    return std::move(str) + ")";
994
57
                }
995
0
                default: break;
996
6.68k
            }
997
6.68k
            assert(false);
998
0
        };
999
1000
1.36k
        return TreeEvalMaybe<std::string>(false, downfn, upfn);
1001
1.36k
    }
miniscript_tests.cpp:std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<CPubKey>::ToString<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&, bool&) const
Line
Count
Source
883
1
    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
1
        auto downfn = [](bool, const Node& node, size_t) {
888
1
            return (node.fragment == Fragment::WRAP_A || node.fragment == Fragment::WRAP_S ||
889
1
                    node.fragment == Fragment::WRAP_D || node.fragment == Fragment::WRAP_V ||
890
1
                    node.fragment == Fragment::WRAP_J || node.fragment == Fragment::WRAP_N ||
891
1
                    node.fragment == Fragment::WRAP_C ||
892
1
                    (node.fragment == Fragment::AND_V && node.subs[1].fragment == Fragment::JUST_1) ||
893
1
                    (node.fragment == Fragment::OR_I && node.subs[0].fragment == Fragment::JUST_0) ||
894
1
                    (node.fragment == Fragment::OR_I && node.subs[1].fragment == Fragment::JUST_0));
895
1
        };
896
1
        auto toString = [&ctx, &has_priv_key](Key key) -> std::optional<std::string> {
897
1
            bool fragment_has_priv_key{false};
898
1
            auto key_str{ctx.ToString(key, fragment_has_priv_key)};
899
1
            if (key_str) has_priv_key = has_priv_key || fragment_has_priv_key;
900
1
            return key_str;
901
1
        };
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
1
        const bool is_tapscript{IsTapscript(m_script_ctx)};
905
1
        auto upfn = [is_tapscript, &toString](bool wrapped, const Node& node, std::span<std::string> subs) -> std::optional<std::string> {
906
1
            std::string ret = wrapped ? ":" : "";
907
908
1
            switch (node.fragment) {
909
1
                case Fragment::WRAP_A: return "a" + std::move(subs[0]);
910
1
                case Fragment::WRAP_S: return "s" + std::move(subs[0]);
911
1
                case Fragment::WRAP_C:
912
1
                    if (node.subs[0].fragment == Fragment::PK_K) {
913
                        // pk(K) is syntactic sugar for c:pk_k(K)
914
1
                        auto key_str = toString(node.subs[0].keys[0]);
915
1
                        if (!key_str) return {};
916
1
                        return std::move(ret) + "pk(" + std::move(*key_str) + ")";
917
1
                    }
918
1
                    if (node.subs[0].fragment == Fragment::PK_H) {
919
                        // pkh(K) is syntactic sugar for c:pk_h(K)
920
1
                        auto key_str = toString(node.subs[0].keys[0]);
921
1
                        if (!key_str) return {};
922
1
                        return std::move(ret) + "pkh(" + std::move(*key_str) + ")";
923
1
                    }
924
1
                    return "c" + std::move(subs[0]);
925
1
                case Fragment::WRAP_D: return "d" + std::move(subs[0]);
926
1
                case Fragment::WRAP_V: return "v" + std::move(subs[0]);
927
1
                case Fragment::WRAP_J: return "j" + std::move(subs[0]);
928
1
                case Fragment::WRAP_N: return "n" + std::move(subs[0]);
929
1
                case Fragment::AND_V:
930
                    // t:X is syntactic sugar for and_v(X,1).
931
1
                    if (node.subs[1].fragment == Fragment::JUST_1) return "t" + std::move(subs[0]);
932
1
                    break;
933
1
                case Fragment::OR_I:
934
1
                    if (node.subs[0].fragment == Fragment::JUST_0) return "l" + std::move(subs[1]);
935
1
                    if (node.subs[1].fragment == Fragment::JUST_0) return "u" + std::move(subs[0]);
936
1
                    break;
937
1
                default: break;
938
1
            }
939
1
            switch (node.fragment) {
940
1
                case Fragment::PK_K: {
941
1
                    auto key_str = toString(node.keys[0]);
942
1
                    if (!key_str) return {};
943
1
                    return std::move(ret) + "pk_k(" + std::move(*key_str) + ")";
944
1
                }
945
1
                case Fragment::PK_H: {
946
1
                    auto key_str = toString(node.keys[0]);
947
1
                    if (!key_str) return {};
948
1
                    return std::move(ret) + "pk_h(" + std::move(*key_str) + ")";
949
1
                }
950
1
                case Fragment::AFTER: return std::move(ret) + "after(" + util::ToString(node.k) + ")";
951
1
                case Fragment::OLDER: return std::move(ret) + "older(" + util::ToString(node.k) + ")";
952
1
                case Fragment::HASH256: return std::move(ret) + "hash256(" + HexStr(node.data) + ")";
953
1
                case Fragment::HASH160: return std::move(ret) + "hash160(" + HexStr(node.data) + ")";
954
1
                case Fragment::SHA256: return std::move(ret) + "sha256(" + HexStr(node.data) + ")";
955
1
                case Fragment::RIPEMD160: return std::move(ret) + "ripemd160(" + HexStr(node.data) + ")";
956
1
                case Fragment::JUST_1: return std::move(ret) + "1";
957
1
                case Fragment::JUST_0: return std::move(ret) + "0";
958
1
                case Fragment::AND_V: return std::move(ret) + "and_v(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
959
1
                case Fragment::AND_B: return std::move(ret) + "and_b(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
960
1
                case Fragment::OR_B: return std::move(ret) + "or_b(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
961
1
                case Fragment::OR_D: return std::move(ret) + "or_d(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
962
1
                case Fragment::OR_C: return std::move(ret) + "or_c(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
963
1
                case Fragment::OR_I: return std::move(ret) + "or_i(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
964
1
                case Fragment::ANDOR:
965
                    // and_n(X,Y) is syntactic sugar for andor(X,Y,0).
966
1
                    if (node.subs[2].fragment == Fragment::JUST_0) return std::move(ret) + "and_n(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
967
1
                    return std::move(ret) + "andor(" + std::move(subs[0]) + "," + std::move(subs[1]) + "," + std::move(subs[2]) + ")";
968
1
                case Fragment::MULTI: {
969
1
                    CHECK_NONFATAL(!is_tapscript);
970
1
                    auto str = std::move(ret) + "multi(" + util::ToString(node.k);
971
1
                    for (const auto& key : node.keys) {
972
1
                        auto key_str = toString(key);
973
1
                        if (!key_str) return {};
974
1
                        str += "," + std::move(*key_str);
975
1
                    }
976
1
                    return std::move(str) + ")";
977
1
                }
978
1
                case Fragment::MULTI_A: {
979
1
                    CHECK_NONFATAL(is_tapscript);
980
1
                    auto str = std::move(ret) + "multi_a(" + util::ToString(node.k);
981
1
                    for (const auto& key : node.keys) {
982
1
                        auto key_str = toString(key);
983
1
                        if (!key_str) return {};
984
1
                        str += "," + std::move(*key_str);
985
1
                    }
986
1
                    return std::move(str) + ")";
987
1
                }
988
1
                case Fragment::THRESH: {
989
1
                    auto str = std::move(ret) + "thresh(" + util::ToString(node.k);
990
1
                    for (auto& sub : subs) {
991
1
                        str += "," + std::move(sub);
992
1
                    }
993
1
                    return std::move(str) + ")";
994
1
                }
995
1
                default: break;
996
1
            }
997
1
            assert(false);
998
1
        };
999
1000
1
        return TreeEvalMaybe<std::string>(false, downfn, upfn);
1001
1
    }
descriptor.cpp:std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<unsigned int>::ToString<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&, bool&) const
Line
Count
Source
883
16
    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
16
        auto downfn = [](bool, const Node& node, size_t) {
888
16
            return (node.fragment == Fragment::WRAP_A || node.fragment == Fragment::WRAP_S ||
889
16
                    node.fragment == Fragment::WRAP_D || node.fragment == Fragment::WRAP_V ||
890
16
                    node.fragment == Fragment::WRAP_J || node.fragment == Fragment::WRAP_N ||
891
16
                    node.fragment == Fragment::WRAP_C ||
892
16
                    (node.fragment == Fragment::AND_V && node.subs[1].fragment == Fragment::JUST_1) ||
893
16
                    (node.fragment == Fragment::OR_I && node.subs[0].fragment == Fragment::JUST_0) ||
894
16
                    (node.fragment == Fragment::OR_I && node.subs[1].fragment == Fragment::JUST_0));
895
16
        };
896
16
        auto toString = [&ctx, &has_priv_key](Key key) -> std::optional<std::string> {
897
16
            bool fragment_has_priv_key{false};
898
16
            auto key_str{ctx.ToString(key, fragment_has_priv_key)};
899
16
            if (key_str) has_priv_key = has_priv_key || fragment_has_priv_key;
900
16
            return key_str;
901
16
        };
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
16
        const bool is_tapscript{IsTapscript(m_script_ctx)};
905
16
        auto upfn = [is_tapscript, &toString](bool wrapped, const Node& node, std::span<std::string> subs) -> std::optional<std::string> {
906
16
            std::string ret = wrapped ? ":" : "";
907
908
16
            switch (node.fragment) {
909
16
                case Fragment::WRAP_A: return "a" + std::move(subs[0]);
910
16
                case Fragment::WRAP_S: return "s" + std::move(subs[0]);
911
16
                case Fragment::WRAP_C:
912
16
                    if (node.subs[0].fragment == Fragment::PK_K) {
913
                        // pk(K) is syntactic sugar for c:pk_k(K)
914
16
                        auto key_str = toString(node.subs[0].keys[0]);
915
16
                        if (!key_str) return {};
916
16
                        return std::move(ret) + "pk(" + std::move(*key_str) + ")";
917
16
                    }
918
16
                    if (node.subs[0].fragment == Fragment::PK_H) {
919
                        // pkh(K) is syntactic sugar for c:pk_h(K)
920
16
                        auto key_str = toString(node.subs[0].keys[0]);
921
16
                        if (!key_str) return {};
922
16
                        return std::move(ret) + "pkh(" + std::move(*key_str) + ")";
923
16
                    }
924
16
                    return "c" + std::move(subs[0]);
925
16
                case Fragment::WRAP_D: return "d" + std::move(subs[0]);
926
16
                case Fragment::WRAP_V: return "v" + std::move(subs[0]);
927
16
                case Fragment::WRAP_J: return "j" + std::move(subs[0]);
928
16
                case Fragment::WRAP_N: return "n" + std::move(subs[0]);
929
16
                case Fragment::AND_V:
930
                    // t:X is syntactic sugar for and_v(X,1).
931
16
                    if (node.subs[1].fragment == Fragment::JUST_1) return "t" + std::move(subs[0]);
932
16
                    break;
933
16
                case Fragment::OR_I:
934
16
                    if (node.subs[0].fragment == Fragment::JUST_0) return "l" + std::move(subs[1]);
935
16
                    if (node.subs[1].fragment == Fragment::JUST_0) return "u" + std::move(subs[0]);
936
16
                    break;
937
16
                default: break;
938
16
            }
939
16
            switch (node.fragment) {
940
16
                case Fragment::PK_K: {
941
16
                    auto key_str = toString(node.keys[0]);
942
16
                    if (!key_str) return {};
943
16
                    return std::move(ret) + "pk_k(" + std::move(*key_str) + ")";
944
16
                }
945
16
                case Fragment::PK_H: {
946
16
                    auto key_str = toString(node.keys[0]);
947
16
                    if (!key_str) return {};
948
16
                    return std::move(ret) + "pk_h(" + std::move(*key_str) + ")";
949
16
                }
950
16
                case Fragment::AFTER: return std::move(ret) + "after(" + util::ToString(node.k) + ")";
951
16
                case Fragment::OLDER: return std::move(ret) + "older(" + util::ToString(node.k) + ")";
952
16
                case Fragment::HASH256: return std::move(ret) + "hash256(" + HexStr(node.data) + ")";
953
16
                case Fragment::HASH160: return std::move(ret) + "hash160(" + HexStr(node.data) + ")";
954
16
                case Fragment::SHA256: return std::move(ret) + "sha256(" + HexStr(node.data) + ")";
955
16
                case Fragment::RIPEMD160: return std::move(ret) + "ripemd160(" + HexStr(node.data) + ")";
956
16
                case Fragment::JUST_1: return std::move(ret) + "1";
957
16
                case Fragment::JUST_0: return std::move(ret) + "0";
958
16
                case Fragment::AND_V: return std::move(ret) + "and_v(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
959
16
                case Fragment::AND_B: return std::move(ret) + "and_b(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
960
16
                case Fragment::OR_B: return std::move(ret) + "or_b(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
961
16
                case Fragment::OR_D: return std::move(ret) + "or_d(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
962
16
                case Fragment::OR_C: return std::move(ret) + "or_c(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
963
16
                case Fragment::OR_I: return std::move(ret) + "or_i(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
964
16
                case Fragment::ANDOR:
965
                    // and_n(X,Y) is syntactic sugar for andor(X,Y,0).
966
16
                    if (node.subs[2].fragment == Fragment::JUST_0) return std::move(ret) + "and_n(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
967
16
                    return std::move(ret) + "andor(" + std::move(subs[0]) + "," + std::move(subs[1]) + "," + std::move(subs[2]) + ")";
968
16
                case Fragment::MULTI: {
969
16
                    CHECK_NONFATAL(!is_tapscript);
970
16
                    auto str = std::move(ret) + "multi(" + util::ToString(node.k);
971
16
                    for (const auto& key : node.keys) {
972
16
                        auto key_str = toString(key);
973
16
                        if (!key_str) return {};
974
16
                        str += "," + std::move(*key_str);
975
16
                    }
976
16
                    return std::move(str) + ")";
977
16
                }
978
16
                case Fragment::MULTI_A: {
979
16
                    CHECK_NONFATAL(is_tapscript);
980
16
                    auto str = std::move(ret) + "multi_a(" + util::ToString(node.k);
981
16
                    for (const auto& key : node.keys) {
982
16
                        auto key_str = toString(key);
983
16
                        if (!key_str) return {};
984
16
                        str += "," + std::move(*key_str);
985
16
                    }
986
16
                    return std::move(str) + ")";
987
16
                }
988
16
                case Fragment::THRESH: {
989
16
                    auto str = std::move(ret) + "thresh(" + util::ToString(node.k);
990
16
                    for (auto& sub : subs) {
991
16
                        str += "," + std::move(sub);
992
16
                    }
993
16
                    return std::move(str) + ")";
994
16
                }
995
16
                default: break;
996
16
            }
997
16
            assert(false);
998
16
        };
999
1000
16
        return TreeEvalMaybe<std::string>(false, downfn, upfn);
1001
16
    }
descriptor.cpp:std::optional<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> miniscript::Node<unsigned int>::ToString<(anonymous namespace)::StringMaker>((anonymous namespace)::StringMaker const&, bool&) const
Line
Count
Source
883
1.34k
    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
1.34k
        auto downfn = [](bool, const Node& node, size_t) {
888
1.34k
            return (node.fragment == Fragment::WRAP_A || node.fragment == Fragment::WRAP_S ||
889
1.34k
                    node.fragment == Fragment::WRAP_D || node.fragment == Fragment::WRAP_V ||
890
1.34k
                    node.fragment == Fragment::WRAP_J || node.fragment == Fragment::WRAP_N ||
891
1.34k
                    node.fragment == Fragment::WRAP_C ||
892
1.34k
                    (node.fragment == Fragment::AND_V && node.subs[1].fragment == Fragment::JUST_1) ||
893
1.34k
                    (node.fragment == Fragment::OR_I && node.subs[0].fragment == Fragment::JUST_0) ||
894
1.34k
                    (node.fragment == Fragment::OR_I && node.subs[1].fragment == Fragment::JUST_0));
895
1.34k
        };
896
1.34k
        auto toString = [&ctx, &has_priv_key](Key key) -> std::optional<std::string> {
897
1.34k
            bool fragment_has_priv_key{false};
898
1.34k
            auto key_str{ctx.ToString(key, fragment_has_priv_key)};
899
1.34k
            if (key_str) has_priv_key = has_priv_key || fragment_has_priv_key;
900
1.34k
            return key_str;
901
1.34k
        };
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
1.34k
        const bool is_tapscript{IsTapscript(m_script_ctx)};
905
1.34k
        auto upfn = [is_tapscript, &toString](bool wrapped, const Node& node, std::span<std::string> subs) -> std::optional<std::string> {
906
1.34k
            std::string ret = wrapped ? ":" : "";
907
908
1.34k
            switch (node.fragment) {
909
1.34k
                case Fragment::WRAP_A: return "a" + std::move(subs[0]);
910
1.34k
                case Fragment::WRAP_S: return "s" + std::move(subs[0]);
911
1.34k
                case Fragment::WRAP_C:
912
1.34k
                    if (node.subs[0].fragment == Fragment::PK_K) {
913
                        // pk(K) is syntactic sugar for c:pk_k(K)
914
1.34k
                        auto key_str = toString(node.subs[0].keys[0]);
915
1.34k
                        if (!key_str) return {};
916
1.34k
                        return std::move(ret) + "pk(" + std::move(*key_str) + ")";
917
1.34k
                    }
918
1.34k
                    if (node.subs[0].fragment == Fragment::PK_H) {
919
                        // pkh(K) is syntactic sugar for c:pk_h(K)
920
1.34k
                        auto key_str = toString(node.subs[0].keys[0]);
921
1.34k
                        if (!key_str) return {};
922
1.34k
                        return std::move(ret) + "pkh(" + std::move(*key_str) + ")";
923
1.34k
                    }
924
1.34k
                    return "c" + std::move(subs[0]);
925
1.34k
                case Fragment::WRAP_D: return "d" + std::move(subs[0]);
926
1.34k
                case Fragment::WRAP_V: return "v" + std::move(subs[0]);
927
1.34k
                case Fragment::WRAP_J: return "j" + std::move(subs[0]);
928
1.34k
                case Fragment::WRAP_N: return "n" + std::move(subs[0]);
929
1.34k
                case Fragment::AND_V:
930
                    // t:X is syntactic sugar for and_v(X,1).
931
1.34k
                    if (node.subs[1].fragment == Fragment::JUST_1) return "t" + std::move(subs[0]);
932
1.34k
                    break;
933
1.34k
                case Fragment::OR_I:
934
1.34k
                    if (node.subs[0].fragment == Fragment::JUST_0) return "l" + std::move(subs[1]);
935
1.34k
                    if (node.subs[1].fragment == Fragment::JUST_0) return "u" + std::move(subs[0]);
936
1.34k
                    break;
937
1.34k
                default: break;
938
1.34k
            }
939
1.34k
            switch (node.fragment) {
940
1.34k
                case Fragment::PK_K: {
941
1.34k
                    auto key_str = toString(node.keys[0]);
942
1.34k
                    if (!key_str) return {};
943
1.34k
                    return std::move(ret) + "pk_k(" + std::move(*key_str) + ")";
944
1.34k
                }
945
1.34k
                case Fragment::PK_H: {
946
1.34k
                    auto key_str = toString(node.keys[0]);
947
1.34k
                    if (!key_str) return {};
948
1.34k
                    return std::move(ret) + "pk_h(" + std::move(*key_str) + ")";
949
1.34k
                }
950
1.34k
                case Fragment::AFTER: return std::move(ret) + "after(" + util::ToString(node.k) + ")";
951
1.34k
                case Fragment::OLDER: return std::move(ret) + "older(" + util::ToString(node.k) + ")";
952
1.34k
                case Fragment::HASH256: return std::move(ret) + "hash256(" + HexStr(node.data) + ")";
953
1.34k
                case Fragment::HASH160: return std::move(ret) + "hash160(" + HexStr(node.data) + ")";
954
1.34k
                case Fragment::SHA256: return std::move(ret) + "sha256(" + HexStr(node.data) + ")";
955
1.34k
                case Fragment::RIPEMD160: return std::move(ret) + "ripemd160(" + HexStr(node.data) + ")";
956
1.34k
                case Fragment::JUST_1: return std::move(ret) + "1";
957
1.34k
                case Fragment::JUST_0: return std::move(ret) + "0";
958
1.34k
                case Fragment::AND_V: return std::move(ret) + "and_v(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
959
1.34k
                case Fragment::AND_B: return std::move(ret) + "and_b(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
960
1.34k
                case Fragment::OR_B: return std::move(ret) + "or_b(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
961
1.34k
                case Fragment::OR_D: return std::move(ret) + "or_d(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
962
1.34k
                case Fragment::OR_C: return std::move(ret) + "or_c(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
963
1.34k
                case Fragment::OR_I: return std::move(ret) + "or_i(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
964
1.34k
                case Fragment::ANDOR:
965
                    // and_n(X,Y) is syntactic sugar for andor(X,Y,0).
966
1.34k
                    if (node.subs[2].fragment == Fragment::JUST_0) return std::move(ret) + "and_n(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
967
1.34k
                    return std::move(ret) + "andor(" + std::move(subs[0]) + "," + std::move(subs[1]) + "," + std::move(subs[2]) + ")";
968
1.34k
                case Fragment::MULTI: {
969
1.34k
                    CHECK_NONFATAL(!is_tapscript);
970
1.34k
                    auto str = std::move(ret) + "multi(" + util::ToString(node.k);
971
1.34k
                    for (const auto& key : node.keys) {
972
1.34k
                        auto key_str = toString(key);
973
1.34k
                        if (!key_str) return {};
974
1.34k
                        str += "," + std::move(*key_str);
975
1.34k
                    }
976
1.34k
                    return std::move(str) + ")";
977
1.34k
                }
978
1.34k
                case Fragment::MULTI_A: {
979
1.34k
                    CHECK_NONFATAL(is_tapscript);
980
1.34k
                    auto str = std::move(ret) + "multi_a(" + util::ToString(node.k);
981
1.34k
                    for (const auto& key : node.keys) {
982
1.34k
                        auto key_str = toString(key);
983
1.34k
                        if (!key_str) return {};
984
1.34k
                        str += "," + std::move(*key_str);
985
1.34k
                    }
986
1.34k
                    return std::move(str) + ")";
987
1.34k
                }
988
1.34k
                case Fragment::THRESH: {
989
1.34k
                    auto str = std::move(ret) + "thresh(" + util::ToString(node.k);
990
1.34k
                    for (auto& sub : subs) {
991
1.34k
                        str += "," + std::move(sub);
992
1.34k
                    }
993
1.34k
                    return std::move(str) + ")";
994
1.34k
                }
995
1.34k
                default: break;
996
1.34k
            }
997
1.34k
            assert(false);
998
1.34k
        };
999
1000
1.34k
        return TreeEvalMaybe<std::string>(false, downfn, upfn);
1001
1.34k
    }
1002
1003
private:
1004
3.08M
    internal::Ops CalcOps() const {
1005
3.08M
        switch (fragment) {
1006
245
            case Fragment::JUST_1: return {0, 0, {}};
1007
709
            case Fragment::JUST_0: return {0, {}, 0};
1008
7.06k
            case Fragment::PK_K: return {0, 0, 0};
1009
987
            case Fragment::PK_H: return {3, 0, 0};
1010
7.98k
            case Fragment::OLDER:
1011
9.45k
            case Fragment::AFTER: return {1, 0, {}};
1012
102
            case Fragment::SHA256:
1013
179
            case Fragment::RIPEMD160:
1014
295
            case Fragment::HASH256:
1015
396
            case Fragment::HASH160: return {4, 0, {}};
1016
2.07k
            case Fragment::AND_V: return {subs[0].ops.count + subs[1].ops.count, subs[0].ops.sat + subs[1].ops.sat, {}};
1017
7.24k
            case Fragment::AND_B: {
1018
7.24k
                const auto count{1 + subs[0].ops.count + subs[1].ops.count};
1019
7.24k
                const auto sat{subs[0].ops.sat + subs[1].ops.sat};
1020
7.24k
                const auto dsat{subs[0].ops.dsat + subs[1].ops.dsat};
1021
7.24k
                return {count, sat, dsat};
1022
295
            }
1023
153
            case Fragment::OR_B: {
1024
153
                const auto count{1 + subs[0].ops.count + subs[1].ops.count};
1025
153
                const auto sat{(subs[0].ops.sat + subs[1].ops.dsat) | (subs[1].ops.sat + subs[0].ops.dsat)};
1026
153
                const auto dsat{subs[0].ops.dsat + subs[1].ops.dsat};
1027
153
                return {count, sat, dsat};
1028
295
            }
1029
130
            case Fragment::OR_D: {
1030
130
                const auto count{3 + subs[0].ops.count + subs[1].ops.count};
1031
130
                const auto sat{subs[0].ops.sat | (subs[1].ops.sat + subs[0].ops.dsat)};
1032
130
                const auto dsat{subs[0].ops.dsat + subs[1].ops.dsat};
1033
130
                return {count, sat, dsat};
1034
295
            }
1035
64
            case Fragment::OR_C: {
1036
64
                const auto count{2 + subs[0].ops.count + subs[1].ops.count};
1037
64
                const auto sat{subs[0].ops.sat | (subs[1].ops.sat + subs[0].ops.dsat)};
1038
64
                return {count, sat, {}};
1039
295
            }
1040
663
            case Fragment::OR_I: {
1041
663
                const auto count{3 + subs[0].ops.count + subs[1].ops.count};
1042
663
                const auto sat{subs[0].ops.sat | subs[1].ops.sat};
1043
663
                const auto dsat{subs[0].ops.dsat | subs[1].ops.dsat};
1044
663
                return {count, sat, dsat};
1045
295
            }
1046
253
            case Fragment::ANDOR: {
1047
253
                const auto count{3 + subs[0].ops.count + subs[1].ops.count + subs[2].ops.count};
1048
253
                const auto sat{(subs[1].ops.sat + subs[0].ops.sat) | (subs[0].ops.dsat + subs[2].ops.sat)};
1049
253
                const auto dsat{subs[0].ops.dsat + subs[2].ops.dsat};
1050
253
                return {count, sat, dsat};
1051
295
            }
1052
181
            case Fragment::MULTI: return {1, (uint32_t)keys.size(), (uint32_t)keys.size()};
1053
837
            case Fragment::MULTI_A: return {(uint32_t)keys.size() + 1, 0, 0};
1054
961
            case Fragment::WRAP_S:
1055
8.94k
            case Fragment::WRAP_C:
1056
3.04M
            case Fragment::WRAP_N: return {1 + subs[0].ops.count, subs[0].ops.sat, subs[0].ops.dsat};
1057
7.66k
            case Fragment::WRAP_A: return {2 + subs[0].ops.count, subs[0].ops.sat, subs[0].ops.dsat};
1058
114
            case Fragment::WRAP_D: return {3 + subs[0].ops.count, subs[0].ops.sat, 0};
1059
16
            case Fragment::WRAP_J: return {4 + subs[0].ops.count, subs[0].ops.sat, 0};
1060
2.21k
            case Fragment::WRAP_V: return {subs[0].ops.count + (subs[0].GetType() << "x"_mst), subs[0].ops.sat, {}};
1061
428
            case Fragment::THRESH: {
1062
428
                uint32_t count = 0;
1063
428
                auto sats = Vector(internal::MaxInt<uint32_t>(0));
1064
1.63k
                for (const auto& sub : subs) {
1065
1.63k
                    count += sub.ops.count + 1;
1066
1.63k
                    auto next_sats = Vector(sats[0] + sub.ops.dsat);
1067
4.77k
                    for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + sub.ops.dsat) | (sats[j - 1] + sub.ops.sat));
1068
1.63k
                    next_sats.push_back(sats[sats.size() - 1] + sub.ops.sat);
1069
1.63k
                    sats = std::move(next_sats);
1070
1.63k
                }
1071
428
                assert(k < sats.size());
1072
428
                return {count, sats[k], sats[0]};
1073
428
            }
1074
3.08M
        }
1075
3.08M
        assert(false);
1076
0
    }
miniscript::Node<CPubKey>::CalcOps() const
Line
Count
Source
1004
28.5k
    internal::Ops CalcOps() const {
1005
28.5k
        switch (fragment) {
1006
232
            case Fragment::JUST_1: return {0, 0, {}};
1007
449
            case Fragment::JUST_0: return {0, {}, 0};
1008
1.70k
            case Fragment::PK_K: return {0, 0, 0};
1009
137
            case Fragment::PK_H: return {3, 0, 0};
1010
7.58k
            case Fragment::OLDER:
1011
7.97k
            case Fragment::AFTER: return {1, 0, {}};
1012
60
            case Fragment::SHA256:
1013
86
            case Fragment::RIPEMD160:
1014
126
            case Fragment::HASH256:
1015
150
            case Fragment::HASH160: return {4, 0, {}};
1016
265
            case Fragment::AND_V: return {subs[0].ops.count + subs[1].ops.count, subs[0].ops.sat + subs[1].ops.sat, {}};
1017
6.88k
            case Fragment::AND_B: {
1018
6.88k
                const auto count{1 + subs[0].ops.count + subs[1].ops.count};
1019
6.88k
                const auto sat{subs[0].ops.sat + subs[1].ops.sat};
1020
6.88k
                const auto dsat{subs[0].ops.dsat + subs[1].ops.dsat};
1021
6.88k
                return {count, sat, dsat};
1022
126
            }
1023
29
            case Fragment::OR_B: {
1024
29
                const auto count{1 + subs[0].ops.count + subs[1].ops.count};
1025
29
                const auto sat{(subs[0].ops.sat + subs[1].ops.dsat) | (subs[1].ops.sat + subs[0].ops.dsat)};
1026
29
                const auto dsat{subs[0].ops.dsat + subs[1].ops.dsat};
1027
29
                return {count, sat, dsat};
1028
126
            }
1029
42
            case Fragment::OR_D: {
1030
42
                const auto count{3 + subs[0].ops.count + subs[1].ops.count};
1031
42
                const auto sat{subs[0].ops.sat | (subs[1].ops.sat + subs[0].ops.dsat)};
1032
42
                const auto dsat{subs[0].ops.dsat + subs[1].ops.dsat};
1033
42
                return {count, sat, dsat};
1034
126
            }
1035
20
            case Fragment::OR_C: {
1036
20
                const auto count{2 + subs[0].ops.count + subs[1].ops.count};
1037
20
                const auto sat{subs[0].ops.sat | (subs[1].ops.sat + subs[0].ops.dsat)};
1038
20
                return {count, sat, {}};
1039
126
            }
1040
399
            case Fragment::OR_I: {
1041
399
                const auto count{3 + subs[0].ops.count + subs[1].ops.count};
1042
399
                const auto sat{subs[0].ops.sat | subs[1].ops.sat};
1043
399
                const auto dsat{subs[0].ops.dsat | subs[1].ops.dsat};
1044
399
                return {count, sat, dsat};
1045
126
            }
1046
113
            case Fragment::ANDOR: {
1047
113
                const auto count{3 + subs[0].ops.count + subs[1].ops.count + subs[2].ops.count};
1048
113
                const auto sat{(subs[1].ops.sat + subs[0].ops.sat) | (subs[0].ops.dsat + subs[2].ops.sat)};
1049
113
                const auto dsat{subs[0].ops.dsat + subs[2].ops.dsat};
1050
113
                return {count, sat, dsat};
1051
126
            }
1052
49
            case Fragment::MULTI: return {1, (uint32_t)keys.size(), (uint32_t)keys.size()};
1053
5
            case Fragment::MULTI_A: return {(uint32_t)keys.size() + 1, 0, 0};
1054
479
            case Fragment::WRAP_S:
1055
2.28k
            case Fragment::WRAP_C:
1056
2.57k
            case Fragment::WRAP_N: return {1 + subs[0].ops.count, subs[0].ops.sat, subs[0].ops.dsat};
1057
7.00k
            case Fragment::WRAP_A: return {2 + subs[0].ops.count, subs[0].ops.sat, subs[0].ops.dsat};
1058
36
            case Fragment::WRAP_D: return {3 + subs[0].ops.count, subs[0].ops.sat, 0};
1059
16
            case Fragment::WRAP_J: return {4 + subs[0].ops.count, subs[0].ops.sat, 0};
1060
321
            case Fragment::WRAP_V: return {subs[0].ops.count + (subs[0].GetType() << "x"_mst), subs[0].ops.sat, {}};
1061
148
            case Fragment::THRESH: {
1062
148
                uint32_t count = 0;
1063
148
                auto sats = Vector(internal::MaxInt<uint32_t>(0));
1064
703
                for (const auto& sub : subs) {
1065
703
                    count += sub.ops.count + 1;
1066
703
                    auto next_sats = Vector(sats[0] + sub.ops.dsat);
1067
2.40k
                    for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + sub.ops.dsat) | (sats[j - 1] + sub.ops.sat));
1068
703
                    next_sats.push_back(sats[sats.size() - 1] + sub.ops.sat);
1069
703
                    sats = std::move(next_sats);
1070
703
                }
1071
148
                assert(k < sats.size());
1072
148
                return {count, sats[k], sats[0]};
1073
148
            }
1074
28.5k
        }
1075
28.5k
        assert(false);
1076
0
    }
miniscript::Node<unsigned int>::CalcOps() const
Line
Count
Source
1004
1.72M
    internal::Ops CalcOps() const {
1005
1.72M
        switch (fragment) {
1006
13
            case Fragment::JUST_1: return {0, 0, {}};
1007
260
            case Fragment::JUST_0: return {0, {}, 0};
1008
1.76k
            case Fragment::PK_K: return {0, 0, 0};
1009
521
            case Fragment::PK_H: return {3, 0, 0};
1010
344
            case Fragment::OLDER:
1011
687
            case Fragment::AFTER: return {1, 0, {}};
1012
42
            case Fragment::SHA256:
1013
93
            case Fragment::RIPEMD160:
1014
157
            case Fragment::HASH256:
1015
234
            case Fragment::HASH160: return {4, 0, {}};
1016
843
            case Fragment::AND_V: return {subs[0].ops.count + subs[1].ops.count, subs[0].ops.sat + subs[1].ops.sat, {}};
1017
289
            case Fragment::AND_B: {
1018
289
                const auto count{1 + subs[0].ops.count + subs[1].ops.count};
1019
289
                const auto sat{subs[0].ops.sat + subs[1].ops.sat};
1020
289
                const auto dsat{subs[0].ops.dsat + subs[1].ops.dsat};
1021
289
                return {count, sat, dsat};
1022
157
            }
1023
98
            case Fragment::OR_B: {
1024
98
                const auto count{1 + subs[0].ops.count + subs[1].ops.count};
1025
98
                const auto sat{(subs[0].ops.sat + subs[1].ops.dsat) | (subs[1].ops.sat + subs[0].ops.dsat)};
1026
98
                const auto dsat{subs[0].ops.dsat + subs[1].ops.dsat};
1027
98
                return {count, sat, dsat};
1028
157
            }
1029
88
            case Fragment::OR_D: {
1030
88
                const auto count{3 + subs[0].ops.count + subs[1].ops.count};
1031
88
                const auto sat{subs[0].ops.sat | (subs[1].ops.sat + subs[0].ops.dsat)};
1032
88
                const auto dsat{subs[0].ops.dsat + subs[1].ops.dsat};
1033
88
                return {count, sat, dsat};
1034
157
            }
1035
44
            case Fragment::OR_C: {
1036
44
                const auto count{2 + subs[0].ops.count + subs[1].ops.count};
1037
44
                const auto sat{subs[0].ops.sat | (subs[1].ops.sat + subs[0].ops.dsat)};
1038
44
                return {count, sat, {}};
1039
157
            }
1040
264
            case Fragment::OR_I: {
1041
264
                const auto count{3 + subs[0].ops.count + subs[1].ops.count};
1042
264
                const auto sat{subs[0].ops.sat | subs[1].ops.sat};
1043
264
                const auto dsat{subs[0].ops.dsat | subs[1].ops.dsat};
1044
264
                return {count, sat, dsat};
1045
157
            }
1046
140
            case Fragment::ANDOR: {
1047
140
                const auto count{3 + subs[0].ops.count + subs[1].ops.count + subs[2].ops.count};
1048
140
                const auto sat{(subs[1].ops.sat + subs[0].ops.sat) | (subs[0].ops.dsat + subs[2].ops.sat)};
1049
140
                const auto dsat{subs[0].ops.dsat + subs[2].ops.dsat};
1050
140
                return {count, sat, dsat};
1051
157
            }
1052
132
            case Fragment::MULTI: return {1, (uint32_t)keys.size(), (uint32_t)keys.size()};
1053
32
            case Fragment::MULTI_A: return {(uint32_t)keys.size() + 1, 0, 0};
1054
432
            case Fragment::WRAP_S:
1055
2.69k
            case Fragment::WRAP_C:
1056
1.72M
            case Fragment::WRAP_N: return {1 + subs[0].ops.count, subs[0].ops.sat, subs[0].ops.dsat};
1057
557
            case Fragment::WRAP_A: return {2 + subs[0].ops.count, subs[0].ops.sat, subs[0].ops.dsat};
1058
72
            case Fragment::WRAP_D: return {3 + subs[0].ops.count, subs[0].ops.sat, 0};
1059
0
            case Fragment::WRAP_J: return {4 + subs[0].ops.count, subs[0].ops.sat, 0};
1060
920
            case Fragment::WRAP_V: return {subs[0].ops.count + (subs[0].GetType() << "x"_mst), subs[0].ops.sat, {}};
1061
256
            case Fragment::THRESH: {
1062
256
                uint32_t count = 0;
1063
256
                auto sats = Vector(internal::MaxInt<uint32_t>(0));
1064
860
                for (const auto& sub : subs) {
1065
860
                    count += sub.ops.count + 1;
1066
860
                    auto next_sats = Vector(sats[0] + sub.ops.dsat);
1067
2.22k
                    for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + sub.ops.dsat) | (sats[j - 1] + sub.ops.sat));
1068
860
                    next_sats.push_back(sats[sats.size() - 1] + sub.ops.sat);
1069
860
                    sats = std::move(next_sats);
1070
860
                }
1071
256
                assert(k < sats.size());
1072
256
                return {count, sats[k], sats[0]};
1073
256
            }
1074
1.72M
        }
1075
1.72M
        assert(false);
1076
0
    }
miniscript::Node<XOnlyPubKey>::CalcOps() const
Line
Count
Source
1004
1.32M
    internal::Ops CalcOps() const {
1005
1.32M
        switch (fragment) {
1006
0
            case Fragment::JUST_1: return {0, 0, {}};
1007
0
            case Fragment::JUST_0: return {0, {}, 0};
1008
3.59k
            case Fragment::PK_K: return {0, 0, 0};
1009
329
            case Fragment::PK_H: return {3, 0, 0};
1010
55
            case Fragment::OLDER:
1011
796
            case Fragment::AFTER: return {1, 0, {}};
1012
0
            case Fragment::SHA256:
1013
0
            case Fragment::RIPEMD160:
1014
12
            case Fragment::HASH256:
1015
12
            case Fragment::HASH160: return {4, 0, {}};
1016
969
            case Fragment::AND_V: return {subs[0].ops.count + subs[1].ops.count, subs[0].ops.sat + subs[1].ops.sat, {}};
1017
72
            case Fragment::AND_B: {
1018
72
                const auto count{1 + subs[0].ops.count + subs[1].ops.count};
1019
72
                const auto sat{subs[0].ops.sat + subs[1].ops.sat};
1020
72
                const auto dsat{subs[0].ops.dsat + subs[1].ops.dsat};
1021
72
                return {count, sat, dsat};
1022
12
            }
1023
26
            case Fragment::OR_B: {
1024
26
                const auto count{1 + subs[0].ops.count + subs[1].ops.count};
1025
26
                const auto sat{(subs[0].ops.sat + subs[1].ops.dsat) | (subs[1].ops.sat + subs[0].ops.dsat)};
1026
26
                const auto dsat{subs[0].ops.dsat + subs[1].ops.dsat};
1027
26
                return {count, sat, dsat};
1028
12
            }
1029
0
            case Fragment::OR_D: {
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
12
            }
1035
0
            case Fragment::OR_C: {
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
12
            }
1040
0
            case Fragment::OR_I: {
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
12
            }
1046
0
            case Fragment::ANDOR: {
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
12
            }
1052
0
            case Fragment::MULTI: return {1, (uint32_t)keys.size(), (uint32_t)keys.size()};
1053
800
            case Fragment::MULTI_A: return {(uint32_t)keys.size() + 1, 0, 0};
1054
50
            case Fragment::WRAP_S:
1055
3.97k
            case Fragment::WRAP_C:
1056
1.32M
            case Fragment::WRAP_N: return {1 + subs[0].ops.count, subs[0].ops.sat, subs[0].ops.dsat};
1057
96
            case Fragment::WRAP_A: return {2 + subs[0].ops.count, subs[0].ops.sat, subs[0].ops.dsat};
1058
6
            case Fragment::WRAP_D: return {3 + subs[0].ops.count, subs[0].ops.sat, 0};
1059
0
            case Fragment::WRAP_J: return {4 + subs[0].ops.count, subs[0].ops.sat, 0};
1060
975
            case Fragment::WRAP_V: return {subs[0].ops.count + (subs[0].GetType() << "x"_mst), subs[0].ops.sat, {}};
1061
24
            case Fragment::THRESH: {
1062
24
                uint32_t count = 0;
1063
24
                auto sats = Vector(internal::MaxInt<uint32_t>(0));
1064
72
                for (const auto& sub : subs) {
1065
72
                    count += sub.ops.count + 1;
1066
72
                    auto next_sats = Vector(sats[0] + sub.ops.dsat);
1067
144
                    for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + sub.ops.dsat) | (sats[j - 1] + sub.ops.sat));
1068
72
                    next_sats.push_back(sats[sats.size() - 1] + sub.ops.sat);
1069
72
                    sats = std::move(next_sats);
1070
72
                }
1071
24
                assert(k < sats.size());
1072
24
                return {count, sats[k], sats[0]};
1073
24
            }
1074
1.32M
        }
1075
1.32M
        assert(false);
1076
0
    }
1077
1078
3.08M
    internal::StackSize CalcStackSize() const {
1079
3.08M
        using namespace internal;
1080
3.08M
        switch (fragment) {
1081
709
            case Fragment::JUST_0: return {{}, SatInfo::Push()};
1082
245
            case Fragment::JUST_1: return {SatInfo::Push(), {}};
1083
7.98k
            case Fragment::OLDER:
1084
9.45k
            case Fragment::AFTER: return {SatInfo::Push() + SatInfo::Nop(), {}};
1085
7.06k
            case Fragment::PK_K: return {SatInfo::Push()};
1086
987
            case Fragment::PK_H: return {SatInfo::OP_DUP() + SatInfo::Hash() + SatInfo::Push() + SatInfo::OP_EQUALVERIFY()};
1087
102
            case Fragment::SHA256:
1088
179
            case Fragment::RIPEMD160:
1089
295
            case Fragment::HASH256:
1090
396
            case Fragment::HASH160: return {
1091
396
                SatInfo::OP_SIZE() + SatInfo::Push() + SatInfo::OP_EQUALVERIFY() + SatInfo::Hash() + SatInfo::Push() + SatInfo::OP_EQUAL(),
1092
396
                {}
1093
396
            };
1094
253
            case Fragment::ANDOR: {
1095
253
                const auto& x{subs[0].ss};
1096
253
                const auto& y{subs[1].ss};
1097
253
                const auto& z{subs[2].ss};
1098
253
                return {
1099
253
                    (x.Sat() + SatInfo::If() + y.Sat()) | (x.Dsat() + SatInfo::If() + z.Sat()),
1100
253
                    x.Dsat() + SatInfo::If() + z.Dsat()
1101
253
                };
1102
295
            }
1103
2.07k
            case Fragment::AND_V: {
1104
2.07k
                const auto& x{subs[0].ss};
1105
2.07k
                const auto& y{subs[1].ss};
1106
2.07k
                return {x.Sat() + y.Sat(), {}};
1107
295
            }
1108
7.24k
            case Fragment::AND_B: {
1109
7.24k
                const auto& x{subs[0].ss};
1110
7.24k
                const auto& y{subs[1].ss};
1111
7.24k
                return {x.Sat() + y.Sat() + SatInfo::BinaryOp(), x.Dsat() + y.Dsat() + SatInfo::BinaryOp()};
1112
295
            }
1113
153
            case Fragment::OR_B: {
1114
153
                const auto& x{subs[0].ss};
1115
153
                const auto& y{subs[1].ss};
1116
153
                return {
1117
153
                    ((x.Sat() + y.Dsat()) | (x.Dsat() + y.Sat())) + SatInfo::BinaryOp(),
1118
153
                    x.Dsat() + y.Dsat() + SatInfo::BinaryOp()
1119
153
                };
1120
295
            }
1121
64
            case Fragment::OR_C: {
1122
64
                const auto& x{subs[0].ss};
1123
64
                const auto& y{subs[1].ss};
1124
64
                return {(x.Sat() + SatInfo::If()) | (x.Dsat() + SatInfo::If() + y.Sat()), {}};
1125
295
            }
1126
130
            case Fragment::OR_D: {
1127
130
                const auto& x{subs[0].ss};
1128
130
                const auto& y{subs[1].ss};
1129
130
                return {
1130
130
                    (x.Sat() + SatInfo::OP_IFDUP(true) + SatInfo::If()) | (x.Dsat() + SatInfo::OP_IFDUP(false) + SatInfo::If() + y.Sat()),
1131
130
                    x.Dsat() + SatInfo::OP_IFDUP(false) + SatInfo::If() + y.Dsat()
1132
130
                };
1133
295
            }
1134
663
            case Fragment::OR_I: {
1135
663
                const auto& x{subs[0].ss};
1136
663
                const auto& y{subs[1].ss};
1137
663
                return {SatInfo::If() + (x.Sat() | y.Sat()), SatInfo::If() + (x.Dsat() | y.Dsat())};
1138
295
            }
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
181
            case Fragment::MULTI: return {SatInfo(k, k + keys.size() + 2)};
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
837
            case Fragment::MULTI_A: return {SatInfo(keys.size() - 1, keys.size())};
1148
7.66k
            case Fragment::WRAP_A:
1149
3.04M
            case Fragment::WRAP_N:
1150
3.04M
            case Fragment::WRAP_S: return subs[0].ss;
1151
7.98k
            case Fragment::WRAP_C: return {
1152
7.98k
                subs[0].ss.Sat() + SatInfo::OP_CHECKSIG(),
1153
7.98k
                subs[0].ss.Dsat() + SatInfo::OP_CHECKSIG()
1154
7.98k
            };
1155
114
            case Fragment::WRAP_D: return {
1156
114
                SatInfo::OP_DUP() + SatInfo::If() + subs[0].ss.Sat(),
1157
114
                SatInfo::OP_DUP() + SatInfo::If()
1158
114
            };
1159
2.21k
            case Fragment::WRAP_V: return {subs[0].ss.Sat() + SatInfo::OP_VERIFY(), {}};
1160
16
            case Fragment::WRAP_J: return {
1161
16
                SatInfo::OP_SIZE() + SatInfo::OP_0NOTEQUAL() + SatInfo::If() + subs[0].ss.Sat(),
1162
16
                SatInfo::OP_SIZE() + SatInfo::OP_0NOTEQUAL() + SatInfo::If()
1163
16
            };
1164
428
            case Fragment::THRESH: {
1165
                // sats[j] is the SatInfo corresponding to all traces reaching j satisfactions.
1166
428
                auto sats = Vector(SatInfo::Empty());
1167
2.06k
                for (size_t i = 0; i < subs.size(); ++i) {
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
1.63k
                    auto add = i ? SatInfo::BinaryOp() : SatInfo::Empty();
1171
                    // Construct a variable that will become the next sats, starting with index 0.
1172
1.63k
                    auto next_sats = Vector(sats[0] + subs[i].ss.Dsat() + add);
1173
                    // Then loop to construct next_sats[1..i].
1174
4.77k
                    for (size_t j = 1; j < sats.size(); ++j) {
1175
3.13k
                        next_sats.push_back(((sats[j] + subs[i].ss.Dsat()) | (sats[j - 1] + subs[i].ss.Sat())) + add);
1176
3.13k
                    }
1177
                    // Finally construct next_sats[i+1].
1178
1.63k
                    next_sats.push_back(sats[sats.size() - 1] + subs[i].ss.Sat() + add);
1179
                    // Switch over.
1180
1.63k
                    sats = std::move(next_sats);
1181
1.63k
                }
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
428
                return {
1185
428
                    sats[k] + SatInfo::Push() + SatInfo::OP_EQUAL(),
1186
428
                    sats[0] + SatInfo::Push() + SatInfo::OP_EQUAL()
1187
428
                };
1188
3.04M
            }
1189
3.08M
        }
1190
3.08M
        assert(false);
1191
0
    }
miniscript::Node<CPubKey>::CalcStackSize() const
Line
Count
Source
1078
28.5k
    internal::StackSize CalcStackSize() const {
1079
28.5k
        using namespace internal;
1080
28.5k
        switch (fragment) {
1081
449
            case Fragment::JUST_0: return {{}, SatInfo::Push()};
1082
232
            case Fragment::JUST_1: return {SatInfo::Push(), {}};
1083
7.58k
            case Fragment::OLDER:
1084
7.97k
            case Fragment::AFTER: return {SatInfo::Push() + SatInfo::Nop(), {}};
1085
1.70k
            case Fragment::PK_K: return {SatInfo::Push()};
1086
137
            case Fragment::PK_H: return {SatInfo::OP_DUP() + SatInfo::Hash() + SatInfo::Push() + SatInfo::OP_EQUALVERIFY()};
1087
60
            case Fragment::SHA256:
1088
86
            case Fragment::RIPEMD160:
1089
126
            case Fragment::HASH256:
1090
150
            case Fragment::HASH160: return {
1091
150
                SatInfo::OP_SIZE() + SatInfo::Push() + SatInfo::OP_EQUALVERIFY() + SatInfo::Hash() + SatInfo::Push() + SatInfo::OP_EQUAL(),
1092
150
                {}
1093
150
            };
1094
113
            case Fragment::ANDOR: {
1095
113
                const auto& x{subs[0].ss};
1096
113
                const auto& y{subs[1].ss};
1097
113
                const auto& z{subs[2].ss};
1098
113
                return {
1099
113
                    (x.Sat() + SatInfo::If() + y.Sat()) | (x.Dsat() + SatInfo::If() + z.Sat()),
1100
113
                    x.Dsat() + SatInfo::If() + z.Dsat()
1101
113
                };
1102
126
            }
1103
265
            case Fragment::AND_V: {
1104
265
                const auto& x{subs[0].ss};
1105
265
                const auto& y{subs[1].ss};
1106
265
                return {x.Sat() + y.Sat(), {}};
1107
126
            }
1108
6.88k
            case Fragment::AND_B: {
1109
6.88k
                const auto& x{subs[0].ss};
1110
6.88k
                const auto& y{subs[1].ss};
1111
6.88k
                return {x.Sat() + y.Sat() + SatInfo::BinaryOp(), x.Dsat() + y.Dsat() + SatInfo::BinaryOp()};
1112
126
            }
1113
29
            case Fragment::OR_B: {
1114
29
                const auto& x{subs[0].ss};
1115
29
                const auto& y{subs[1].ss};
1116
29
                return {
1117
29
                    ((x.Sat() + y.Dsat()) | (x.Dsat() + y.Sat())) + SatInfo::BinaryOp(),
1118
29
                    x.Dsat() + y.Dsat() + SatInfo::BinaryOp()
1119
29
                };
1120
126
            }
1121
20
            case Fragment::OR_C: {
1122
20
                const auto& x{subs[0].ss};
1123
20
                const auto& y{subs[1].ss};
1124
20
                return {(x.Sat() + SatInfo::If()) | (x.Dsat() + SatInfo::If() + y.Sat()), {}};
1125
126
            }
1126
42
            case Fragment::OR_D: {
1127
42
                const auto& x{subs[0].ss};
1128
42
                const auto& y{subs[1].ss};
1129
42
                return {
1130
42
                    (x.Sat() + SatInfo::OP_IFDUP(true) + SatInfo::If()) | (x.Dsat() + SatInfo::OP_IFDUP(false) + SatInfo::If() + y.Sat()),
1131
42
                    x.Dsat() + SatInfo::OP_IFDUP(false) + SatInfo::If() + y.Dsat()
1132
42
                };
1133
126
            }
1134
399
            case Fragment::OR_I: {
1135
399
                const auto& x{subs[0].ss};
1136
399
                const auto& y{subs[1].ss};
1137
399
                return {SatInfo::If() + (x.Sat() | y.Sat()), SatInfo::If() + (x.Dsat() | y.Dsat())};
1138
126
            }
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
49
            case Fragment::MULTI: return {SatInfo(k, k + keys.size() + 2)};
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
5
            case Fragment::MULTI_A: return {SatInfo(keys.size() - 1, keys.size())};
1148
7.00k
            case Fragment::WRAP_A:
1149
7.30k
            case Fragment::WRAP_N:
1150
7.78k
            case Fragment::WRAP_S: return subs[0].ss;
1151
1.80k
            case Fragment::WRAP_C: return {
1152
1.80k
                subs[0].ss.Sat() + SatInfo::OP_CHECKSIG(),
1153
1.80k
                subs[0].ss.Dsat() + SatInfo::OP_CHECKSIG()
1154
1.80k
            };
1155
36
            case Fragment::WRAP_D: return {
1156
36
                SatInfo::OP_DUP() + SatInfo::If() + subs[0].ss.Sat(),
1157
36
                SatInfo::OP_DUP() + SatInfo::If()
1158
36
            };
1159
321
            case Fragment::WRAP_V: return {subs[0].ss.Sat() + SatInfo::OP_VERIFY(), {}};
1160
16
            case Fragment::WRAP_J: return {
1161
16
                SatInfo::OP_SIZE() + SatInfo::OP_0NOTEQUAL() + SatInfo::If() + subs[0].ss.Sat(),
1162
16
                SatInfo::OP_SIZE() + SatInfo::OP_0NOTEQUAL() + SatInfo::If()
1163
16
            };
1164
148
            case Fragment::THRESH: {
1165
                // sats[j] is the SatInfo corresponding to all traces reaching j satisfactions.
1166
148
                auto sats = Vector(SatInfo::Empty());
1167
851
                for (size_t i = 0; i < subs.size(); ++i) {
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
703
                    auto add = i ? SatInfo::BinaryOp() : SatInfo::Empty();
1171
                    // Construct a variable that will become the next sats, starting with index 0.
1172
703
                    auto next_sats = Vector(sats[0] + subs[i].ss.Dsat() + add);
1173
                    // Then loop to construct next_sats[1..i].
1174
2.40k
                    for (size_t j = 1; j < sats.size(); ++j) {
1175
1.69k
                        next_sats.push_back(((sats[j] + subs[i].ss.Dsat()) | (sats[j - 1] + subs[i].ss.Sat())) + add);
1176
1.69k
                    }
1177
                    // Finally construct next_sats[i+1].
1178
703
                    next_sats.push_back(sats[sats.size() - 1] + subs[i].ss.Sat() + add);
1179
                    // Switch over.
1180
703
                    sats = std::move(next_sats);
1181
703
                }
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
148
                return {
1185
148
                    sats[k] + SatInfo::Push() + SatInfo::OP_EQUAL(),
1186
148
                    sats[0] + SatInfo::Push() + SatInfo::OP_EQUAL()
1187
148
                };
1188
7.30k
            }
1189
28.5k
        }
1190
28.5k
        assert(false);
1191
0
    }
miniscript::Node<unsigned int>::CalcStackSize() const
Line
Count
Source
1078
1.72M
    internal::StackSize CalcStackSize() const {
1079
1.72M
        using namespace internal;
1080
1.72M
        switch (fragment) {
1081
260
            case Fragment::JUST_0: return {{}, SatInfo::Push()};
1082
13
            case Fragment::JUST_1: return {SatInfo::Push(), {}};
1083
344
            case Fragment::OLDER:
1084
687
            case Fragment::AFTER: return {SatInfo::Push() + SatInfo::Nop(), {}};
1085
1.76k
            case Fragment::PK_K: return {SatInfo::Push()};
1086
521
            case Fragment::PK_H: return {SatInfo::OP_DUP() + SatInfo::Hash() + SatInfo::Push() + SatInfo::OP_EQUALVERIFY()};
1087
42
            case Fragment::SHA256:
1088
93
            case Fragment::RIPEMD160:
1089
157
            case Fragment::HASH256:
1090
234
            case Fragment::HASH160: return {
1091
234
                SatInfo::OP_SIZE() + SatInfo::Push() + SatInfo::OP_EQUALVERIFY() + SatInfo::Hash() + SatInfo::Push() + SatInfo::OP_EQUAL(),
1092
234
                {}
1093
234
            };
1094
140
            case Fragment::ANDOR: {
1095
140
                const auto& x{subs[0].ss};
1096
140
                const auto& y{subs[1].ss};
1097
140
                const auto& z{subs[2].ss};
1098
140
                return {
1099
140
                    (x.Sat() + SatInfo::If() + y.Sat()) | (x.Dsat() + SatInfo::If() + z.Sat()),
1100
140
                    x.Dsat() + SatInfo::If() + z.Dsat()
1101
140
                };
1102
157
            }
1103
843
            case Fragment::AND_V: {
1104
843
                const auto& x{subs[0].ss};
1105
843
                const auto& y{subs[1].ss};
1106
843
                return {x.Sat() + y.Sat(), {}};
1107
157
            }
1108
289
            case Fragment::AND_B: {
1109
289
                const auto& x{subs[0].ss};
1110
289
                const auto& y{subs[1].ss};
1111
289
                return {x.Sat() + y.Sat() + SatInfo::BinaryOp(), x.Dsat() + y.Dsat() + SatInfo::BinaryOp()};
1112
157
            }
1113
98
            case Fragment::OR_B: {
1114
98
                const auto& x{subs[0].ss};
1115
98
                const auto& y{subs[1].ss};
1116
98
                return {
1117
98
                    ((x.Sat() + y.Dsat()) | (x.Dsat() + y.Sat())) + SatInfo::BinaryOp(),
1118
98
                    x.Dsat() + y.Dsat() + SatInfo::BinaryOp()
1119
98
                };
1120
157
            }
1121
44
            case Fragment::OR_C: {
1122
44
                const auto& x{subs[0].ss};
1123
44
                const auto& y{subs[1].ss};
1124
44
                return {(x.Sat() + SatInfo::If()) | (x.Dsat() + SatInfo::If() + y.Sat()), {}};
1125
157
            }
1126
88
            case Fragment::OR_D: {
1127
88
                const auto& x{subs[0].ss};
1128
88
                const auto& y{subs[1].ss};
1129
88
                return {
1130
88
                    (x.Sat() + SatInfo::OP_IFDUP(true) + SatInfo::If()) | (x.Dsat() + SatInfo::OP_IFDUP(false) + SatInfo::If() + y.Sat()),
1131
88
                    x.Dsat() + SatInfo::OP_IFDUP(false) + SatInfo::If() + y.Dsat()
1132
88
                };
1133
157
            }
1134
264
            case Fragment::OR_I: {
1135
264
                const auto& x{subs[0].ss};
1136
264
                const auto& y{subs[1].ss};
1137
264
                return {SatInfo::If() + (x.Sat() | y.Sat()), SatInfo::If() + (x.Dsat() | y.Dsat())};
1138
157
            }
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
132
            case Fragment::MULTI: return {SatInfo(k, k + keys.size() + 2)};
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
32
            case Fragment::MULTI_A: return {SatInfo(keys.size() - 1, keys.size())};
1148
557
            case Fragment::WRAP_A:
1149
1.71M
            case Fragment::WRAP_N:
1150
1.71M
            case Fragment::WRAP_S: return subs[0].ss;
1151
2.26k
            case Fragment::WRAP_C: return {
1152
2.26k
                subs[0].ss.Sat() + SatInfo::OP_CHECKSIG(),
1153
2.26k
                subs[0].ss.Dsat() + SatInfo::OP_CHECKSIG()
1154
2.26k
            };
1155
72
            case Fragment::WRAP_D: return {
1156
72
                SatInfo::OP_DUP() + SatInfo::If() + subs[0].ss.Sat(),
1157
72
                SatInfo::OP_DUP() + SatInfo::If()
1158
72
            };
1159
920
            case Fragment::WRAP_V: return {subs[0].ss.Sat() + SatInfo::OP_VERIFY(), {}};
1160
0
            case Fragment::WRAP_J: return {
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
256
            case Fragment::THRESH: {
1165
                // sats[j] is the SatInfo corresponding to all traces reaching j satisfactions.
1166
256
                auto sats = Vector(SatInfo::Empty());
1167
1.11k
                for (size_t i = 0; i < subs.size(); ++i) {
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
860
                    auto add = i ? SatInfo::BinaryOp() : SatInfo::Empty();
1171
                    // Construct a variable that will become the next sats, starting with index 0.
1172
860
                    auto next_sats = Vector(sats[0] + subs[i].ss.Dsat() + add);
1173
                    // Then loop to construct next_sats[1..i].
1174
2.22k
                    for (size_t j = 1; j < sats.size(); ++j) {
1175
1.36k
                        next_sats.push_back(((sats[j] + subs[i].ss.Dsat()) | (sats[j - 1] + subs[i].ss.Sat())) + add);
1176
1.36k
                    }
1177
                    // Finally construct next_sats[i+1].
1178
860
                    next_sats.push_back(sats[sats.size() - 1] + subs[i].ss.Sat() + add);
1179
                    // Switch over.
1180
860
                    sats = std::move(next_sats);
1181
860
                }
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
256
                return {
1185
256
                    sats[k] + SatInfo::Push() + SatInfo::OP_EQUAL(),
1186
256
                    sats[0] + SatInfo::Push() + SatInfo::OP_EQUAL()
1187
256
                };
1188
1.71M
            }
1189
1.72M
        }
1190
1.72M
        assert(false);
1191
0
    }
miniscript::Node<XOnlyPubKey>::CalcStackSize() const
Line
Count
Source
1078
1.32M
    internal::StackSize CalcStackSize() const {
1079
1.32M
        using namespace internal;
1080
1.32M
        switch (fragment) {
1081
0
            case Fragment::JUST_0: return {{}, SatInfo::Push()};
1082
0
            case Fragment::JUST_1: return {SatInfo::Push(), {}};
1083
55
            case Fragment::OLDER:
1084
796
            case Fragment::AFTER: return {SatInfo::Push() + SatInfo::Nop(), {}};
1085
3.59k
            case Fragment::PK_K: return {SatInfo::Push()};
1086
329
            case Fragment::PK_H: return {SatInfo::OP_DUP() + SatInfo::Hash() + SatInfo::Push() + SatInfo::OP_EQUALVERIFY()};
1087
0
            case Fragment::SHA256:
1088
0
            case Fragment::RIPEMD160:
1089
12
            case Fragment::HASH256:
1090
12
            case Fragment::HASH160: return {
1091
12
                SatInfo::OP_SIZE() + SatInfo::Push() + SatInfo::OP_EQUALVERIFY() + SatInfo::Hash() + SatInfo::Push() + SatInfo::OP_EQUAL(),
1092
12
                {}
1093
12
            };
1094
0
            case Fragment::ANDOR: {
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
12
            }
1103
969
            case Fragment::AND_V: {
1104
969
                const auto& x{subs[0].ss};
1105
969
                const auto& y{subs[1].ss};
1106
969
                return {x.Sat() + y.Sat(), {}};
1107
12
            }
1108
72
            case Fragment::AND_B: {
1109
72
                const auto& x{subs[0].ss};
1110
72
                const auto& y{subs[1].ss};
1111
72
                return {x.Sat() + y.Sat() + SatInfo::BinaryOp(), x.Dsat() + y.Dsat() + SatInfo::BinaryOp()};
1112
12
            }
1113
26
            case Fragment::OR_B: {
1114
26
                const auto& x{subs[0].ss};
1115
26
                const auto& y{subs[1].ss};
1116
26
                return {
1117
26
                    ((x.Sat() + y.Dsat()) | (x.Dsat() + y.Sat())) + SatInfo::BinaryOp(),
1118
26
                    x.Dsat() + y.Dsat() + SatInfo::BinaryOp()
1119
26
                };
1120
12
            }
1121
0
            case Fragment::OR_C: {
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
12
            }
1126
0
            case Fragment::OR_D: {
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
12
            }
1134
0
            case Fragment::OR_I: {
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
12
            }
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)};
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
800
            case Fragment::MULTI_A: return {SatInfo(keys.size() - 1, keys.size())};
1148
96
            case Fragment::WRAP_A:
1149
1.31M
            case Fragment::WRAP_N:
1150
1.31M
            case Fragment::WRAP_S: return subs[0].ss;
1151
3.92k
            case Fragment::WRAP_C: return {
1152
3.92k
                subs[0].ss.Sat() + SatInfo::OP_CHECKSIG(),
1153
3.92k
                subs[0].ss.Dsat() + SatInfo::OP_CHECKSIG()
1154
3.92k
            };
1155
6
            case Fragment::WRAP_D: return {
1156
6
                SatInfo::OP_DUP() + SatInfo::If() + subs[0].ss.Sat(),
1157
6
                SatInfo::OP_DUP() + SatInfo::If()
1158
6
            };
1159
975
            case Fragment::WRAP_V: return {subs[0].ss.Sat() + SatInfo::OP_VERIFY(), {}};
1160
0
            case Fragment::WRAP_J: return {
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
24
            case Fragment::THRESH: {
1165
                // sats[j] is the SatInfo corresponding to all traces reaching j satisfactions.
1166
24
                auto sats = Vector(SatInfo::Empty());
1167
96
                for (size_t i = 0; i < subs.size(); ++i) {
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
72
                    auto add = i ? SatInfo::BinaryOp() : SatInfo::Empty();
1171
                    // Construct a variable that will become the next sats, starting with index 0.
1172
72
                    auto next_sats = Vector(sats[0] + subs[i].ss.Dsat() + add);
1173
                    // Then loop to construct next_sats[1..i].
1174
144
                    for (size_t j = 1; j < sats.size(); ++j) {
1175
72
                        next_sats.push_back(((sats[j] + subs[i].ss.Dsat()) | (sats[j - 1] + subs[i].ss.Sat())) + add);
1176
72
                    }
1177
                    // Finally construct next_sats[i+1].
1178
72
                    next_sats.push_back(sats[sats.size() - 1] + subs[i].ss.Sat() + add);
1179
                    // Switch over.
1180
72
                    sats = std::move(next_sats);
1181
72
                }
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
24
                return {
1185
24
                    sats[k] + SatInfo::Push() + SatInfo::OP_EQUAL(),
1186
24
                    sats[0] + SatInfo::Push() + SatInfo::OP_EQUAL()
1187
24
                };
1188
1.31M
            }
1189
1.32M
        }
1190
1.32M
        assert(false);
1191
0
    }
1192
1193
3.08M
    internal::WitnessSize CalcWitnessSize() const {
1194
3.08M
        const uint32_t sig_size = IsTapscript(m_script_ctx) ? 1 + 65 : 1 + 72;
1195
3.08M
        const uint32_t pubkey_size = IsTapscript(m_script_ctx) ? 1 + 32 : 1 + 33;
1196
3.08M
        switch (fragment) {
1197
709
            case Fragment::JUST_0: return {{}, 0};
1198
245
            case Fragment::JUST_1:
1199
8.23k
            case Fragment::OLDER:
1200
9.70k
            case Fragment::AFTER: return {0, {}};
1201
7.06k
            case Fragment::PK_K: return {sig_size, 1};
1202
987
            case Fragment::PK_H: return {sig_size + pubkey_size, 1 + pubkey_size};
1203
102
            case Fragment::SHA256:
1204
179
            case Fragment::RIPEMD160:
1205
295
            case Fragment::HASH256:
1206
396
            case Fragment::HASH160: return {1 + 32, {}};
1207
253
            case Fragment::ANDOR: {
1208
253
                const auto sat{(subs[0].ws.sat + subs[1].ws.sat) | (subs[0].ws.dsat + subs[2].ws.sat)};
1209
253
                const auto dsat{subs[0].ws.dsat + subs[2].ws.dsat};
1210
253
                return {sat, dsat};
1211
295
            }
1212
2.07k
            case Fragment::AND_V: return {subs[0].ws.sat + subs[1].ws.sat, {}};
1213
7.24k
            case Fragment::AND_B: return {subs[0].ws.sat + subs[1].ws.sat, subs[0].ws.dsat + subs[1].ws.dsat};
1214
153
            case Fragment::OR_B: {
1215
153
                const auto sat{(subs[0].ws.dsat + subs[1].ws.sat) | (subs[0].ws.sat + subs[1].ws.dsat)};
1216
153
                const auto dsat{subs[0].ws.dsat + subs[1].ws.dsat};
1217
153
                return {sat, dsat};
1218
295
            }
1219
64
            case Fragment::OR_C: return {subs[0].ws.sat | (subs[0].ws.dsat + subs[1].ws.sat), {}};
1220
130
            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};
1221
663
            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)};
1222
181
            case Fragment::MULTI: return {k * sig_size + 1, k + 1};
1223
837
            case Fragment::MULTI_A: return {k * sig_size + static_cast<uint32_t>(keys.size()) - k, static_cast<uint32_t>(keys.size())};
1224
7.66k
            case Fragment::WRAP_A:
1225
3.04M
            case Fragment::WRAP_N:
1226
3.04M
            case Fragment::WRAP_S:
1227
3.05M
            case Fragment::WRAP_C: return subs[0].ws;
1228
114
            case Fragment::WRAP_D: return {1 + 1 + subs[0].ws.sat, 1};
1229
2.21k
            case Fragment::WRAP_V: return {subs[0].ws.sat, {}};
1230
16
            case Fragment::WRAP_J: return {subs[0].ws.sat, 1};
1231
428
            case Fragment::THRESH: {
1232
428
                auto sats = Vector(internal::MaxInt<uint32_t>(0));
1233
1.63k
                for (const auto& sub : subs) {
1234
1.63k
                    auto next_sats = Vector(sats[0] + sub.ws.dsat);
1235
4.77k
                    for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + sub.ws.dsat) | (sats[j - 1] + sub.ws.sat));
1236
1.63k
                    next_sats.push_back(sats[sats.size() - 1] + sub.ws.sat);
1237
1.63k
                    sats = std::move(next_sats);
1238
1.63k
                }
1239
428
                assert(k < sats.size());
1240
428
                return {sats[k], sats[0]};
1241
428
            }
1242
3.08M
        }
1243
3.08M
        assert(false);
1244
0
    }
miniscript::Node<CPubKey>::CalcWitnessSize() const
Line
Count
Source
1193
28.5k
    internal::WitnessSize CalcWitnessSize() const {
1194
28.5k
        const uint32_t sig_size = IsTapscript(m_script_ctx) ? 1 + 65 : 1 + 72;
1195
28.5k
        const uint32_t pubkey_size = IsTapscript(m_script_ctx) ? 1 + 32 : 1 + 33;
1196
28.5k
        switch (fragment) {
1197
449
            case Fragment::JUST_0: return {{}, 0};
1198
232
            case Fragment::JUST_1:
1199
7.81k
            case Fragment::OLDER:
1200
8.20k
            case Fragment::AFTER: return {0, {}};
1201
1.70k
            case Fragment::PK_K: return {sig_size, 1};
1202
137
            case Fragment::PK_H: return {sig_size + pubkey_size, 1 + pubkey_size};
1203
60
            case Fragment::SHA256:
1204
86
            case Fragment::RIPEMD160:
1205
126
            case Fragment::HASH256:
1206
150
            case Fragment::HASH160: return {1 + 32, {}};
1207
113
            case Fragment::ANDOR: {
1208
113
                const auto sat{(subs[0].ws.sat + subs[1].ws.sat) | (subs[0].ws.dsat + subs[2].ws.sat)};
1209
113
                const auto dsat{subs[0].ws.dsat + subs[2].ws.dsat};
1210
113
                return {sat, dsat};
1211
126
            }
1212
265
            case Fragment::AND_V: return {subs[0].ws.sat + subs[1].ws.sat, {}};
1213
6.88k
            case Fragment::AND_B: return {subs[0].ws.sat + subs[1].ws.sat, subs[0].ws.dsat + subs[1].ws.dsat};
1214
29
            case Fragment::OR_B: {
1215
29
                const auto sat{(subs[0].ws.dsat + subs[1].ws.sat) | (subs[0].ws.sat + subs[1].ws.dsat)};
1216
29
                const auto dsat{subs[0].ws.dsat + subs[1].ws.dsat};
1217
29
                return {sat, dsat};
1218
126
            }
1219
20
            case Fragment::OR_C: return {subs[0].ws.sat | (subs[0].ws.dsat + subs[1].ws.sat), {}};
1220
42
            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};
1221
399
            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)};
1222
49
            case Fragment::MULTI: return {k * sig_size + 1, k + 1};
1223
5
            case Fragment::MULTI_A: return {k * sig_size + static_cast<uint32_t>(keys.size()) - k, static_cast<uint32_t>(keys.size())};
1224
7.00k
            case Fragment::WRAP_A:
1225
7.30k
            case Fragment::WRAP_N:
1226
7.78k
            case Fragment::WRAP_S:
1227
9.58k
            case Fragment::WRAP_C: return subs[0].ws;
1228
36
            case Fragment::WRAP_D: return {1 + 1 + subs[0].ws.sat, 1};
1229
321
            case Fragment::WRAP_V: return {subs[0].ws.sat, {}};
1230
16
            case Fragment::WRAP_J: return {subs[0].ws.sat, 1};
1231
148
            case Fragment::THRESH: {
1232
148
                auto sats = Vector(internal::MaxInt<uint32_t>(0));
1233
703
                for (const auto& sub : subs) {
1234
703
                    auto next_sats = Vector(sats[0] + sub.ws.dsat);
1235
2.40k
                    for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + sub.ws.dsat) | (sats[j - 1] + sub.ws.sat));
1236
703
                    next_sats.push_back(sats[sats.size() - 1] + sub.ws.sat);
1237
703
                    sats = std::move(next_sats);
1238
703
                }
1239
148
                assert(k < sats.size());
1240
148
                return {sats[k], sats[0]};
1241
148
            }
1242
28.5k
        }
1243
28.5k
        assert(false);
1244
0
    }
miniscript::Node<unsigned int>::CalcWitnessSize() const
Line
Count
Source
1193
1.72M
    internal::WitnessSize CalcWitnessSize() const {
1194
1.72M
        const uint32_t sig_size = IsTapscript(m_script_ctx) ? 1 + 65 : 1 + 72;
1195
1.72M
        const uint32_t pubkey_size = IsTapscript(m_script_ctx) ? 1 + 32 : 1 + 33;
1196
1.72M
        switch (fragment) {
1197
260
            case Fragment::JUST_0: return {{}, 0};
1198
13
            case Fragment::JUST_1:
1199
357
            case Fragment::OLDER:
1200
700
            case Fragment::AFTER: return {0, {}};
1201
1.76k
            case Fragment::PK_K: return {sig_size, 1};
1202
521
            case Fragment::PK_H: return {sig_size + pubkey_size, 1 + pubkey_size};
1203
42
            case Fragment::SHA256:
1204
93
            case Fragment::RIPEMD160:
1205
157
            case Fragment::HASH256:
1206
234
            case Fragment::HASH160: return {1 + 32, {}};
1207
140
            case Fragment::ANDOR: {
1208
140
                const auto sat{(subs[0].ws.sat + subs[1].ws.sat) | (subs[0].ws.dsat + subs[2].ws.sat)};
1209
140
                const auto dsat{subs[0].ws.dsat + subs[2].ws.dsat};
1210
140
                return {sat, dsat};
1211
157
            }
1212
843
            case Fragment::AND_V: return {subs[0].ws.sat + subs[1].ws.sat, {}};
1213
289
            case Fragment::AND_B: return {subs[0].ws.sat + subs[1].ws.sat, subs[0].ws.dsat + subs[1].ws.dsat};
1214
98
            case Fragment::OR_B: {
1215
98
                const auto sat{(subs[0].ws.dsat + subs[1].ws.sat) | (subs[0].ws.sat + subs[1].ws.dsat)};
1216
98
                const auto dsat{subs[0].ws.dsat + subs[1].ws.dsat};
1217
98
                return {sat, dsat};
1218
157
            }
1219
44
            case Fragment::OR_C: return {subs[0].ws.sat | (subs[0].ws.dsat + subs[1].ws.sat), {}};
1220
88
            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};
1221
264
            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)};
1222
132
            case Fragment::MULTI: return {k * sig_size + 1, k + 1};
1223
32
            case Fragment::MULTI_A: return {k * sig_size + static_cast<uint32_t>(keys.size()) - k, static_cast<uint32_t>(keys.size())};
1224
557
            case Fragment::WRAP_A:
1225
1.71M
            case Fragment::WRAP_N:
1226
1.71M
            case Fragment::WRAP_S:
1227
1.72M
            case Fragment::WRAP_C: return subs[0].ws;
1228
72
            case Fragment::WRAP_D: return {1 + 1 + subs[0].ws.sat, 1};
1229
920
            case Fragment::WRAP_V: return {subs[0].ws.sat, {}};
1230
0
            case Fragment::WRAP_J: return {subs[0].ws.sat, 1};
1231
256
            case Fragment::THRESH: {
1232
256
                auto sats = Vector(internal::MaxInt<uint32_t>(0));
1233
860
                for (const auto& sub : subs) {
1234
860
                    auto next_sats = Vector(sats[0] + sub.ws.dsat);
1235
2.22k
                    for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + sub.ws.dsat) | (sats[j - 1] + sub.ws.sat));
1236
860
                    next_sats.push_back(sats[sats.size() - 1] + sub.ws.sat);
1237
860
                    sats = std::move(next_sats);
1238
860
                }
1239
256
                assert(k < sats.size());
1240
256
                return {sats[k], sats[0]};
1241
256
            }
1242
1.72M
        }
1243
1.72M
        assert(false);
1244
0
    }
miniscript::Node<XOnlyPubKey>::CalcWitnessSize() const
Line
Count
Source
1193
1.32M
    internal::WitnessSize CalcWitnessSize() const {
1194
1.32M
        const uint32_t sig_size = IsTapscript(m_script_ctx) ? 1 + 65 : 1 + 72;
1195
1.32M
        const uint32_t pubkey_size = IsTapscript(m_script_ctx) ? 1 + 32 : 1 + 33;
1196
1.32M
        switch (fragment) {
1197
0
            case Fragment::JUST_0: return {{}, 0};
1198
0
            case Fragment::JUST_1:
1199
55
            case Fragment::OLDER:
1200
796
            case Fragment::AFTER: return {0, {}};
1201
3.59k
            case Fragment::PK_K: return {sig_size, 1};
1202
329
            case Fragment::PK_H: return {sig_size + pubkey_size, 1 + pubkey_size};
1203
0
            case Fragment::SHA256:
1204
0
            case Fragment::RIPEMD160:
1205
12
            case Fragment::HASH256:
1206
12
            case Fragment::HASH160: return {1 + 32, {}};
1207
0
            case Fragment::ANDOR: {
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
12
            }
1212
969
            case Fragment::AND_V: return {subs[0].ws.sat + subs[1].ws.sat, {}};
1213
72
            case Fragment::AND_B: return {subs[0].ws.sat + subs[1].ws.sat, subs[0].ws.dsat + subs[1].ws.dsat};
1214
26
            case Fragment::OR_B: {
1215
26
                const auto sat{(subs[0].ws.dsat + subs[1].ws.sat) | (subs[0].ws.sat + subs[1].ws.dsat)};
1216
26
                const auto dsat{subs[0].ws.dsat + subs[1].ws.dsat};
1217
26
                return {sat, dsat};
1218
12
            }
1219
0
            case Fragment::OR_C: return {subs[0].ws.sat | (subs[0].ws.dsat + subs[1].ws.sat), {}};
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};
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)};
1222
0
            case Fragment::MULTI: return {k * sig_size + 1, k + 1};
1223
800
            case Fragment::MULTI_A: return {k * sig_size + static_cast<uint32_t>(keys.size()) - k, static_cast<uint32_t>(keys.size())};
1224
96
            case Fragment::WRAP_A:
1225
1.31M
            case Fragment::WRAP_N:
1226
1.31M
            case Fragment::WRAP_S:
1227
1.32M
            case Fragment::WRAP_C: return subs[0].ws;
1228
6
            case Fragment::WRAP_D: return {1 + 1 + subs[0].ws.sat, 1};
1229
975
            case Fragment::WRAP_V: return {subs[0].ws.sat, {}};
1230
0
            case Fragment::WRAP_J: return {subs[0].ws.sat, 1};
1231
24
            case Fragment::THRESH: {
1232
24
                auto sats = Vector(internal::MaxInt<uint32_t>(0));
1233
72
                for (const auto& sub : subs) {
1234
72
                    auto next_sats = Vector(sats[0] + sub.ws.dsat);
1235
144
                    for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + sub.ws.dsat) | (sats[j - 1] + sub.ws.sat));
1236
72
                    next_sats.push_back(sats[sats.size() - 1] + sub.ws.sat);
1237
72
                    sats = std::move(next_sats);
1238
72
                }
1239
24
                assert(k < sats.size());
1240
24
                return {sats[k], sats[0]};
1241
24
            }
1242
1.32M
        }
1243
1.32M
        assert(false);
1244
0
    }
1245
1246
    template<typename Ctx>
1247
9.47k
    internal::InputResult ProduceInput(const Ctx& ctx) const {
1248
9.47k
        using namespace internal;
1249
1250
        // Internal function which is invoked for every tree node, constructing satisfaction/dissatisfactions
1251
        // given those of its subnodes.
1252
2.94M
        auto helper = [&ctx](const Node& node, std::span<InputResult> subres) -> InputResult {
1253
2.94M
            switch (node.fragment) {
1254
378k
                case Fragment::PK_K: {
1255
378k
                    std::vector<unsigned char> sig;
1256
378k
                    Availability avail = ctx.Sign(node.keys[0], sig);
1257
378k
                    return {ZERO, InputStack(std::move(sig)).SetWithSig().SetAvailable(avail)};
1258
0
                }
1259
1.12k
                case Fragment::PK_H: {
1260
1.12k
                    std::vector<unsigned char> key = ctx.ToPKBytes(node.keys[0]), sig;
1261
1.12k
                    Availability avail = ctx.Sign(node.keys[0], sig);
1262
1.12k
                    return {ZERO + InputStack(key), (InputStack(std::move(sig)).SetWithSig() + InputStack(key)).SetAvailable(avail)};
1263
0
                }
1264
956
                case Fragment::MULTI_A: {
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
956
                    std::vector<InputStack> sats = Vector(EMPTY);
1268
97.3k
                    for (size_t i = 0; i < node.keys.size(); ++i) {
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
96.4k
                        std::vector<unsigned char> sig;
1272
96.4k
                        Availability avail = ctx.Sign(node.keys[node.keys.size() - 1 - i], sig);
1273
                        // Compute signature stack for just this key.
1274
96.4k
                        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
96.4k
                        std::vector<InputStack> next_sats;
1279
96.4k
                        next_sats.push_back(sats[0] + ZERO);
1280
45.9M
                        for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + ZERO) | (std::move(sats[j - 1]) + sat));
1281
96.4k
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(sat));
1282
                        // Switch over.
1283
96.4k
                        sats = std::move(next_sats);
1284
96.4k
                    }
1285
                    // The dissatisfaction consists of as many empty vectors as there are keys, which is the same as
1286
                    // satisfying 0 keys.
1287
956
                    auto& nsat{sats[0]};
1288
956
                    CHECK_NONFATAL(node.k != 0);
1289
956
                    assert(node.k < sats.size());
1290
956
                    return {std::move(nsat), std::move(sats[node.k])};
1291
956
                }
1292
384
                case Fragment::MULTI: {
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
384
                    std::vector<InputStack> sats = Vector(ZERO);
1297
1.14k
                    for (size_t i = 0; i < node.keys.size(); ++i) {
1298
756
                        std::vector<unsigned char> sig;
1299
756
                        Availability avail = ctx.Sign(node.keys[i], sig);
1300
                        // Compute signature stack for just the i'th key.
1301
756
                        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
756
                        std::vector<InputStack> next_sats;
1306
756
                        next_sats.push_back(sats[0]);
1307
1.20k
                        for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back(sats[j] | (std::move(sats[j - 1]) + sat));
1308
756
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(sat));
1309
                        // Switch over.
1310
756
                        sats = std::move(next_sats);
1311
756
                    }
1312
                    // The dissatisfaction consists of k+1 stack elements all equal to 0.
1313
384
                    InputStack nsat = ZERO;
1314
1.11k
                    for (size_t i = 0; i < node.k; ++i) nsat = std::move(nsat) + ZERO;
1315
384
                    assert(node.k < sats.size());
1316
384
                    return {std::move(nsat), std::move(sats[node.k])};
1317
384
                }
1318
509
                case Fragment::THRESH: {
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
509
                    std::vector<InputStack> sats = Vector(EMPTY);
1323
2.28k
                    for (size_t i = 0; i < subres.size(); ++i) {
1324
                        // Introduce an alias for the i'th last satisfaction/dissatisfaction.
1325
1.77k
                        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
1.77k
                        std::vector<InputStack> next_sats;
1330
1.77k
                        next_sats.push_back(sats[0] + res.nsat);
1331
4.59k
                        for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + res.nsat) | (std::move(sats[j - 1]) + res.sat));
1332
1.77k
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(res.sat));
1333
                        // Switch over.
1334
1.77k
                        sats = std::move(next_sats);
1335
1.77k
                    }
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
509
                    InputStack nsat = INVALID;
1339
2.79k
                    for (size_t i = 0; i < sats.size(); ++i) {
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
2.28k
                        if (i != 0 && i != node.k) sats[i].SetMalleable().SetNonCanon();
1347
                        // Include all dissatisfactions (even these non-canonical ones) in nsat.
1348
2.28k
                        if (i != node.k) nsat = std::move(nsat) | std::move(sats[i]);
1349
2.28k
                    }
1350
509
                    assert(node.k < sats.size());
1351
509
                    return {std::move(nsat), std::move(sats[node.k])};
1352
509
                }
1353
37.0k
                case Fragment::OLDER: {
1354
37.0k
                    return {INVALID, ctx.CheckOlder(node.k) ? EMPTY : INVALID};
1355
509
                }
1356
2.29k
                case Fragment::AFTER: {
1357
2.29k
                    return {INVALID, ctx.CheckAfter(node.k) ? EMPTY : INVALID};
1358
509
                }
1359
521
                case Fragment::SHA256: {
1360
521
                    std::vector<unsigned char> preimage;
1361
521
                    Availability avail = ctx.SatSHA256(node.data, preimage);
1362
521
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1363
509
                }
1364
222
                case Fragment::RIPEMD160: {
1365
222
                    std::vector<unsigned char> preimage;
1366
222
                    Availability avail = ctx.SatRIPEMD160(node.data, preimage);
1367
222
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1368
509
                }
1369
396
                case Fragment::HASH256: {
1370
396
                    std::vector<unsigned char> preimage;
1371
396
                    Availability avail = ctx.SatHASH256(node.data, preimage);
1372
396
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1373
509
                }
1374
168
                case Fragment::HASH160: {
1375
168
                    std::vector<unsigned char> preimage;
1376
168
                    Availability avail = ctx.SatHASH160(node.data, preimage);
1377
168
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1378
509
                }
1379
2.41k
                case Fragment::AND_V: {
1380
2.41k
                    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
2.41k
                    return {(y.nsat + x.sat).SetNonCanon(), y.sat + x.sat};
1388
509
                }
1389
407k
                case Fragment::AND_B: {
1390
407k
                    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
407k
                    return {(y.nsat + x.nsat) | (y.sat + x.nsat).SetMalleable().SetNonCanon() | (y.nsat + x.sat).SetMalleable().SetNonCanon(), y.sat + x.sat};
1397
509
                }
1398
170
                case Fragment::OR_B: {
1399
170
                    auto& x = subres[0], &z = subres[1];
1400
                    // The (sat(Z) sat(X)) solution is overcomplete (attacker can change either into dsat).
1401
170
                    return {z.nsat + x.nsat, (z.nsat + x.sat) | (z.sat + x.nsat) | (z.sat + x.sat).SetMalleable().SetNonCanon()};
1402
509
                }
1403
90
                case Fragment::OR_C: {
1404
90
                    auto& x = subres[0], &z = subres[1];
1405
90
                    return {INVALID, std::move(x.sat) | (z.sat + x.nsat)};
1406
509
                }
1407
316
                case Fragment::OR_D: {
1408
316
                    auto& x = subres[0], &z = subres[1];
1409
316
                    return {z.nsat + x.nsat, std::move(x.sat) | (z.sat + x.nsat)};
1410
509
                }
1411
1.82k
                case Fragment::OR_I: {
1412
1.82k
                    auto& x = subres[0], &z = subres[1];
1413
1.82k
                    return {(x.nsat + ONE) | (z.nsat + ZERO), (x.sat + ONE) | (z.sat + ZERO)};
1414
509
                }
1415
719
                case Fragment::ANDOR: {
1416
719
                    auto& x = subres[0], &y = subres[1], &z = subres[2];
1417
719
                    return {(y.nsat + x.sat).SetNonCanon() | (z.nsat + x.nsat), (y.sat + x.sat) | (z.sat + x.nsat)};
1418
509
                }
1419
408k
                case Fragment::WRAP_A:
1420
409k
                case Fragment::WRAP_S:
1421
788k
                case Fragment::WRAP_C:
1422
2.10M
                case Fragment::WRAP_N:
1423
2.10M
                    return std::move(subres[0]);
1424
125
                case Fragment::WRAP_D: {
1425
125
                    auto &x = subres[0];
1426
125
                    return {ZERO, x.sat + ONE};
1427
788k
                }
1428
198
                case Fragment::WRAP_J: {
1429
198
                    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
198
                    return {InputStack(ZERO).SetMalleable(x.nsat.available != Availability::NO && !x.nsat.has_sig), std::move(x.sat)};
1436
788k
                }
1437
2.74k
                case Fragment::WRAP_V: {
1438
2.74k
                    auto &x = subres[0];
1439
2.74k
                    return {INVALID, std::move(x.sat)};
1440
788k
                }
1441
1.74k
                case Fragment::JUST_0: return {EMPTY, INVALID};
1442
972
                case Fragment::JUST_1: return {INVALID, EMPTY};
1443
2.94M
            }
1444
2.94M
            assert(false);
1445
0
            return {INVALID, INVALID};
1446
0
        };
miniscript_tests.cpp:miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)::operator()(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>) const
Line
Count
Source
1252
1.61M
        auto helper = [&ctx](const Node& node, std::span<InputResult> subres) -> InputResult {
1253
1.61M
            switch (node.fragment) {
1254
374k
                case Fragment::PK_K: {
1255
374k
                    std::vector<unsigned char> sig;
1256
374k
                    Availability avail = ctx.Sign(node.keys[0], sig);
1257
374k
                    return {ZERO, InputStack(std::move(sig)).SetWithSig().SetAvailable(avail)};
1258
0
                }
1259
708
                case Fragment::PK_H: {
1260
708
                    std::vector<unsigned char> key = ctx.ToPKBytes(node.keys[0]), sig;
1261
708
                    Availability avail = ctx.Sign(node.keys[0], sig);
1262
708
                    return {ZERO + InputStack(key), (InputStack(std::move(sig)).SetWithSig() + InputStack(key)).SetAvailable(avail)};
1263
0
                }
1264
156
                case Fragment::MULTI_A: {
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
156
                    std::vector<InputStack> sats = Vector(EMPTY);
1268
2.97k
                    for (size_t i = 0; i < node.keys.size(); ++i) {
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
2.82k
                        std::vector<unsigned char> sig;
1272
2.82k
                        Availability avail = ctx.Sign(node.keys[node.keys.size() - 1 - i], sig);
1273
                        // Compute signature stack for just this key.
1274
2.82k
                        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
2.82k
                        std::vector<InputStack> next_sats;
1279
2.82k
                        next_sats.push_back(sats[0] + ZERO);
1280
30.5k
                        for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + ZERO) | (std::move(sats[j - 1]) + sat));
1281
2.82k
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(sat));
1282
                        // Switch over.
1283
2.82k
                        sats = std::move(next_sats);
1284
2.82k
                    }
1285
                    // The dissatisfaction consists of as many empty vectors as there are keys, which is the same as
1286
                    // satisfying 0 keys.
1287
156
                    auto& nsat{sats[0]};
1288
156
                    CHECK_NONFATAL(node.k != 0);
1289
156
                    assert(node.k < sats.size());
1290
156
                    return {std::move(nsat), std::move(sats[node.k])};
1291
156
                }
1292
360
                case Fragment::MULTI: {
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
360
                    std::vector<InputStack> sats = Vector(ZERO);
1297
1.06k
                    for (size_t i = 0; i < node.keys.size(); ++i) {
1298
708
                        std::vector<unsigned char> sig;
1299
708
                        Availability avail = ctx.Sign(node.keys[i], sig);
1300
                        // Compute signature stack for just the i'th key.
1301
708
                        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
708
                        std::vector<InputStack> next_sats;
1306
708
                        next_sats.push_back(sats[0]);
1307
1.12k
                        for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back(sats[j] | (std::move(sats[j - 1]) + sat));
1308
708
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(sat));
1309
                        // Switch over.
1310
708
                        sats = std::move(next_sats);
1311
708
                    }
1312
                    // The dissatisfaction consists of k+1 stack elements all equal to 0.
1313
360
                    InputStack nsat = ZERO;
1314
1.06k
                    for (size_t i = 0; i < node.k; ++i) nsat = std::move(nsat) + ZERO;
1315
360
                    assert(node.k < sats.size());
1316
360
                    return {std::move(nsat), std::move(sats[node.k])};
1317
360
                }
1318
372
                case Fragment::THRESH: {
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
372
                    std::vector<InputStack> sats = Vector(EMPTY);
1323
1.47k
                    for (size_t i = 0; i < subres.size(); ++i) {
1324
                        // Introduce an alias for the i'th last satisfaction/dissatisfaction.
1325
1.10k
                        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
1.10k
                        std::vector<InputStack> next_sats;
1330
1.10k
                        next_sats.push_back(sats[0] + res.nsat);
1331
2.25k
                        for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + res.nsat) | (std::move(sats[j - 1]) + res.sat));
1332
1.10k
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(res.sat));
1333
                        // Switch over.
1334
1.10k
                        sats = std::move(next_sats);
1335
1.10k
                    }
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
372
                    InputStack nsat = INVALID;
1339
1.84k
                    for (size_t i = 0; i < sats.size(); ++i) {
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
1.47k
                        if (i != 0 && i != node.k) sats[i].SetMalleable().SetNonCanon();
1347
                        // Include all dissatisfactions (even these non-canonical ones) in nsat.
1348
1.47k
                        if (i != node.k) nsat = std::move(nsat) | std::move(sats[i]);
1349
1.47k
                    }
1350
372
                    assert(node.k < sats.size());
1351
372
                    return {std::move(nsat), std::move(sats[node.k])};
1352
372
                }
1353
36.9k
                case Fragment::OLDER: {
1354
36.9k
                    return {INVALID, ctx.CheckOlder(node.k) ? EMPTY : INVALID};
1355
372
                }
1356
1.30k
                case Fragment::AFTER: {
1357
1.30k
                    return {INVALID, ctx.CheckAfter(node.k) ? EMPTY : INVALID};
1358
372
                }
1359
504
                case Fragment::SHA256: {
1360
504
                    std::vector<unsigned char> preimage;
1361
504
                    Availability avail = ctx.SatSHA256(node.data, preimage);
1362
504
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1363
372
                }
1364
210
                case Fragment::RIPEMD160: {
1365
210
                    std::vector<unsigned char> preimage;
1366
210
                    Availability avail = ctx.SatRIPEMD160(node.data, preimage);
1367
210
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1368
372
                }
1369
372
                case Fragment::HASH256: {
1370
372
                    std::vector<unsigned char> preimage;
1371
372
                    Availability avail = ctx.SatHASH256(node.data, preimage);
1372
372
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1373
372
                }
1374
156
                case Fragment::HASH160: {
1375
156
                    std::vector<unsigned char> preimage;
1376
156
                    Availability avail = ctx.SatHASH160(node.data, preimage);
1377
156
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1378
372
                }
1379
1.32k
                case Fragment::AND_V: {
1380
1.32k
                    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
1.32k
                    return {(y.nsat + x.sat).SetNonCanon(), y.sat + x.sat};
1388
372
                }
1389
407k
                case Fragment::AND_B: {
1390
407k
                    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
407k
                    return {(y.nsat + x.nsat) | (y.sat + x.nsat).SetMalleable().SetNonCanon() | (y.nsat + x.sat).SetMalleable().SetNonCanon(), y.sat + x.sat};
1397
372
                }
1398
144
                case Fragment::OR_B: {
1399
144
                    auto& x = subres[0], &z = subres[1];
1400
                    // The (sat(Z) sat(X)) solution is overcomplete (attacker can change either into dsat).
1401
144
                    return {z.nsat + x.nsat, (z.nsat + x.sat) | (z.sat + x.nsat) | (z.sat + x.sat).SetMalleable().SetNonCanon()};
1402
372
                }
1403
90
                case Fragment::OR_C: {
1404
90
                    auto& x = subres[0], &z = subres[1];
1405
90
                    return {INVALID, std::move(x.sat) | (z.sat + x.nsat)};
1406
372
                }
1407
312
                case Fragment::OR_D: {
1408
312
                    auto& x = subres[0], &z = subres[1];
1409
312
                    return {z.nsat + x.nsat, std::move(x.sat) | (z.sat + x.nsat)};
1410
372
                }
1411
1.59k
                case Fragment::OR_I: {
1412
1.59k
                    auto& x = subres[0], &z = subres[1];
1413
1.59k
                    return {(x.nsat + ONE) | (z.nsat + ZERO), (x.sat + ONE) | (z.sat + ZERO)};
1414
372
                }
1415
672
                case Fragment::ANDOR: {
1416
672
                    auto& x = subres[0], &y = subres[1], &z = subres[2];
1417
672
                    return {(y.nsat + x.sat).SetNonCanon() | (z.nsat + x.nsat), (y.sat + x.sat) | (z.sat + x.nsat)};
1418
372
                }
1419
408k
                case Fragment::WRAP_A:
1420
408k
                case Fragment::WRAP_S:
1421
783k
                case Fragment::WRAP_C:
1422
783k
                case Fragment::WRAP_N:
1423
783k
                    return std::move(subres[0]);
1424
96
                case Fragment::WRAP_D: {
1425
96
                    auto &x = subres[0];
1426
96
                    return {ZERO, x.sat + ONE};
1427
783k
                }
1428
198
                case Fragment::WRAP_J: {
1429
198
                    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
198
                    return {InputStack(ZERO).SetMalleable(x.nsat.available != Availability::NO && !x.nsat.has_sig), std::move(x.sat)};
1436
783k
                }
1437
1.62k
                case Fragment::WRAP_V: {
1438
1.62k
                    auto &x = subres[0];
1439
1.62k
                    return {INVALID, std::move(x.sat)};
1440
783k
                }
1441
1.50k
                case Fragment::JUST_0: return {EMPTY, INVALID};
1442
972
                case Fragment::JUST_1: return {INVALID, EMPTY};
1443
1.61M
            }
1444
1.61M
            assert(false);
1445
0
            return {INVALID, INVALID};
1446
0
        };
miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)::operator()(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>) const
Line
Count
Source
1252
1.32M
        auto helper = [&ctx](const Node& node, std::span<InputResult> subres) -> InputResult {
1253
1.32M
            switch (node.fragment) {
1254
3.59k
                case Fragment::PK_K: {
1255
3.59k
                    std::vector<unsigned char> sig;
1256
3.59k
                    Availability avail = ctx.Sign(node.keys[0], sig);
1257
3.59k
                    return {ZERO, InputStack(std::move(sig)).SetWithSig().SetAvailable(avail)};
1258
0
                }
1259
329
                case Fragment::PK_H: {
1260
329
                    std::vector<unsigned char> key = ctx.ToPKBytes(node.keys[0]), sig;
1261
329
                    Availability avail = ctx.Sign(node.keys[0], sig);
1262
329
                    return {ZERO + InputStack(key), (InputStack(std::move(sig)).SetWithSig() + InputStack(key)).SetAvailable(avail)};
1263
0
                }
1264
800
                case Fragment::MULTI_A: {
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
800
                    std::vector<InputStack> sats = Vector(EMPTY);
1268
94.3k
                    for (size_t i = 0; i < node.keys.size(); ++i) {
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
93.5k
                        std::vector<unsigned char> sig;
1272
93.5k
                        Availability avail = ctx.Sign(node.keys[node.keys.size() - 1 - i], sig);
1273
                        // Compute signature stack for just this key.
1274
93.5k
                        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
93.5k
                        std::vector<InputStack> next_sats;
1279
93.5k
                        next_sats.push_back(sats[0] + ZERO);
1280
45.9M
                        for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + ZERO) | (std::move(sats[j - 1]) + sat));
1281
93.5k
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(sat));
1282
                        // Switch over.
1283
93.5k
                        sats = std::move(next_sats);
1284
93.5k
                    }
1285
                    // The dissatisfaction consists of as many empty vectors as there are keys, which is the same as
1286
                    // satisfying 0 keys.
1287
800
                    auto& nsat{sats[0]};
1288
800
                    CHECK_NONFATAL(node.k != 0);
1289
800
                    assert(node.k < sats.size());
1290
800
                    return {std::move(nsat), std::move(sats[node.k])};
1291
800
                }
1292
0
                case Fragment::MULTI: {
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) {
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));
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;
1315
0
                    assert(node.k < sats.size());
1316
0
                    return {std::move(nsat), std::move(sats[node.k])};
1317
0
                }
1318
24
                case Fragment::THRESH: {
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
24
                    std::vector<InputStack> sats = Vector(EMPTY);
1323
96
                    for (size_t i = 0; i < subres.size(); ++i) {
1324
                        // Introduce an alias for the i'th last satisfaction/dissatisfaction.
1325
72
                        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
72
                        std::vector<InputStack> next_sats;
1330
72
                        next_sats.push_back(sats[0] + res.nsat);
1331
144
                        for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + res.nsat) | (std::move(sats[j - 1]) + res.sat));
1332
72
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(res.sat));
1333
                        // Switch over.
1334
72
                        sats = std::move(next_sats);
1335
72
                    }
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
24
                    InputStack nsat = INVALID;
1339
120
                    for (size_t i = 0; i < sats.size(); ++i) {
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
96
                        if (i != 0 && i != node.k) sats[i].SetMalleable().SetNonCanon();
1347
                        // Include all dissatisfactions (even these non-canonical ones) in nsat.
1348
96
                        if (i != node.k) nsat = std::move(nsat) | std::move(sats[i]);
1349
96
                    }
1350
24
                    assert(node.k < sats.size());
1351
24
                    return {std::move(nsat), std::move(sats[node.k])};
1352
24
                }
1353
55
                case Fragment::OLDER: {
1354
55
                    return {INVALID, ctx.CheckOlder(node.k) ? EMPTY : INVALID};
1355
24
                }
1356
741
                case Fragment::AFTER: {
1357
741
                    return {INVALID, ctx.CheckAfter(node.k) ? EMPTY : INVALID};
1358
24
                }
1359
0
                case Fragment::SHA256: {
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
24
                }
1364
0
                case Fragment::RIPEMD160: {
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
24
                }
1369
12
                case Fragment::HASH256: {
1370
12
                    std::vector<unsigned char> preimage;
1371
12
                    Availability avail = ctx.SatHASH256(node.data, preimage);
1372
12
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1373
24
                }
1374
0
                case Fragment::HASH160: {
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
24
                }
1379
969
                case Fragment::AND_V: {
1380
969
                    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
969
                    return {(y.nsat + x.sat).SetNonCanon(), y.sat + x.sat};
1388
24
                }
1389
72
                case Fragment::AND_B: {
1390
72
                    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
72
                    return {(y.nsat + x.nsat) | (y.sat + x.nsat).SetMalleable().SetNonCanon() | (y.nsat + x.sat).SetMalleable().SetNonCanon(), y.sat + x.sat};
1397
24
                }
1398
26
                case Fragment::OR_B: {
1399
26
                    auto& x = subres[0], &z = subres[1];
1400
                    // The (sat(Z) sat(X)) solution is overcomplete (attacker can change either into dsat).
1401
26
                    return {z.nsat + x.nsat, (z.nsat + x.sat) | (z.sat + x.nsat) | (z.sat + x.sat).SetMalleable().SetNonCanon()};
1402
24
                }
1403
0
                case Fragment::OR_C: {
1404
0
                    auto& x = subres[0], &z = subres[1];
1405
0
                    return {INVALID, std::move(x.sat) | (z.sat + x.nsat)};
1406
24
                }
1407
0
                case Fragment::OR_D: {
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
24
                }
1411
0
                case Fragment::OR_I: {
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
24
                }
1415
0
                case Fragment::ANDOR: {
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
24
                }
1419
96
                case Fragment::WRAP_A:
1420
146
                case Fragment::WRAP_S:
1421
4.06k
                case Fragment::WRAP_C:
1422
1.32M
                case Fragment::WRAP_N:
1423
1.32M
                    return std::move(subres[0]);
1424
6
                case Fragment::WRAP_D: {
1425
6
                    auto &x = subres[0];
1426
6
                    return {ZERO, x.sat + ONE};
1427
4.06k
                }
1428
0
                case Fragment::WRAP_J: {
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)};
1436
4.06k
                }
1437
975
                case Fragment::WRAP_V: {
1438
975
                    auto &x = subres[0];
1439
975
                    return {INVALID, std::move(x.sat)};
1440
4.06k
                }
1441
0
                case Fragment::JUST_0: return {EMPTY, INVALID};
1442
0
                case Fragment::JUST_1: return {INVALID, EMPTY};
1443
1.32M
            }
1444
1.32M
            assert(false);
1445
0
            return {INVALID, INVALID};
1446
0
        };
miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)::operator()(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>) const
Line
Count
Source
1252
3.28k
        auto helper = [&ctx](const Node& node, std::span<InputResult> subres) -> InputResult {
1253
3.28k
            switch (node.fragment) {
1254
488
                case Fragment::PK_K: {
1255
488
                    std::vector<unsigned char> sig;
1256
488
                    Availability avail = ctx.Sign(node.keys[0], sig);
1257
488
                    return {ZERO, InputStack(std::move(sig)).SetWithSig().SetAvailable(avail)};
1258
0
                }
1259
83
                case Fragment::PK_H: {
1260
83
                    std::vector<unsigned char> key = ctx.ToPKBytes(node.keys[0]), sig;
1261
83
                    Availability avail = ctx.Sign(node.keys[0], sig);
1262
83
                    return {ZERO + InputStack(key), (InputStack(std::move(sig)).SetWithSig() + InputStack(key)).SetAvailable(avail)};
1263
0
                }
1264
0
                case Fragment::MULTI_A: {
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) {
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));
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());
1290
0
                    return {std::move(nsat), std::move(sats[node.k])};
1291
0
                }
1292
24
                case Fragment::MULTI: {
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
24
                    std::vector<InputStack> sats = Vector(ZERO);
1297
72
                    for (size_t i = 0; i < node.keys.size(); ++i) {
1298
48
                        std::vector<unsigned char> sig;
1299
48
                        Availability avail = ctx.Sign(node.keys[i], sig);
1300
                        // Compute signature stack for just the i'th key.
1301
48
                        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
48
                        std::vector<InputStack> next_sats;
1306
48
                        next_sats.push_back(sats[0]);
1307
72
                        for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back(sats[j] | (std::move(sats[j - 1]) + sat));
1308
48
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(sat));
1309
                        // Switch over.
1310
48
                        sats = std::move(next_sats);
1311
48
                    }
1312
                    // The dissatisfaction consists of k+1 stack elements all equal to 0.
1313
24
                    InputStack nsat = ZERO;
1314
48
                    for (size_t i = 0; i < node.k; ++i) nsat = std::move(nsat) + ZERO;
1315
24
                    assert(node.k < sats.size());
1316
24
                    return {std::move(nsat), std::move(sats[node.k])};
1317
24
                }
1318
113
                case Fragment::THRESH: {
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
113
                    std::vector<InputStack> sats = Vector(EMPTY);
1323
714
                    for (size_t i = 0; i < subres.size(); ++i) {
1324
                        // Introduce an alias for the i'th last satisfaction/dissatisfaction.
1325
601
                        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
601
                        std::vector<InputStack> next_sats;
1330
601
                        next_sats.push_back(sats[0] + res.nsat);
1331
2.19k
                        for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + res.nsat) | (std::move(sats[j - 1]) + res.sat));
1332
601
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(res.sat));
1333
                        // Switch over.
1334
601
                        sats = std::move(next_sats);
1335
601
                    }
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
113
                    InputStack nsat = INVALID;
1339
827
                    for (size_t i = 0; i < sats.size(); ++i) {
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
714
                        if (i != 0 && i != node.k) sats[i].SetMalleable().SetNonCanon();
1347
                        // Include all dissatisfactions (even these non-canonical ones) in nsat.
1348
714
                        if (i != node.k) nsat = std::move(nsat) | std::move(sats[i]);
1349
714
                    }
1350
113
                    assert(node.k < sats.size());
1351
113
                    return {std::move(nsat), std::move(sats[node.k])};
1352
113
                }
1353
69
                case Fragment::OLDER: {
1354
69
                    return {INVALID, ctx.CheckOlder(node.k) ? EMPTY : INVALID};
1355
113
                }
1356
249
                case Fragment::AFTER: {
1357
249
                    return {INVALID, ctx.CheckAfter(node.k) ? EMPTY : INVALID};
1358
113
                }
1359
17
                case Fragment::SHA256: {
1360
17
                    std::vector<unsigned char> preimage;
1361
17
                    Availability avail = ctx.SatSHA256(node.data, preimage);
1362
17
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1363
113
                }
1364
12
                case Fragment::RIPEMD160: {
1365
12
                    std::vector<unsigned char> preimage;
1366
12
                    Availability avail = ctx.SatRIPEMD160(node.data, preimage);
1367
12
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1368
113
                }
1369
12
                case Fragment::HASH256: {
1370
12
                    std::vector<unsigned char> preimage;
1371
12
                    Availability avail = ctx.SatHASH256(node.data, preimage);
1372
12
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1373
113
                }
1374
12
                case Fragment::HASH160: {
1375
12
                    std::vector<unsigned char> preimage;
1376
12
                    Availability avail = ctx.SatHASH160(node.data, preimage);
1377
12
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1378
113
                }
1379
118
                case Fragment::AND_V: {
1380
118
                    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
118
                    return {(y.nsat + x.sat).SetNonCanon(), y.sat + x.sat};
1388
113
                }
1389
40
                case Fragment::AND_B: {
1390
40
                    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
40
                    return {(y.nsat + x.nsat) | (y.sat + x.nsat).SetMalleable().SetNonCanon() | (y.nsat + x.sat).SetMalleable().SetNonCanon(), y.sat + x.sat};
1397
113
                }
1398
0
                case Fragment::OR_B: {
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
113
                }
1403
0
                case Fragment::OR_C: {
1404
0
                    auto& x = subres[0], &z = subres[1];
1405
0
                    return {INVALID, std::move(x.sat) | (z.sat + x.nsat)};
1406
113
                }
1407
4
                case Fragment::OR_D: {
1408
4
                    auto& x = subres[0], &z = subres[1];
1409
4
                    return {z.nsat + x.nsat, std::move(x.sat) | (z.sat + x.nsat)};
1410
113
                }
1411
231
                case Fragment::OR_I: {
1412
231
                    auto& x = subres[0], &z = subres[1];
1413
231
                    return {(x.nsat + ONE) | (z.nsat + ZERO), (x.sat + ONE) | (z.sat + ZERO)};
1414
113
                }
1415
47
                case Fragment::ANDOR: {
1416
47
                    auto& x = subres[0], &y = subres[1], &z = subres[2];
1417
47
                    return {(y.nsat + x.sat).SetNonCanon() | (z.nsat + x.nsat), (y.sat + x.sat) | (z.sat + x.nsat)};
1418
113
                }
1419
80
                case Fragment::WRAP_A:
1420
528
                case Fragment::WRAP_S:
1421
1.09k
                case Fragment::WRAP_C:
1422
1.36k
                case Fragment::WRAP_N:
1423
1.36k
                    return std::move(subres[0]);
1424
23
                case Fragment::WRAP_D: {
1425
23
                    auto &x = subres[0];
1426
23
                    return {ZERO, x.sat + ONE};
1427
1.09k
                }
1428
0
                case Fragment::WRAP_J: {
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)};
1436
1.09k
                }
1437
141
                case Fragment::WRAP_V: {
1438
141
                    auto &x = subres[0];
1439
141
                    return {INVALID, std::move(x.sat)};
1440
1.09k
                }
1441
243
                case Fragment::JUST_0: return {EMPTY, INVALID};
1442
0
                case Fragment::JUST_1: return {INVALID, EMPTY};
1443
3.28k
            }
1444
3.28k
            assert(false);
1445
0
            return {INVALID, INVALID};
1446
0
        };
1447
1448
2.94M
        auto tester = [&helper](const Node& node, std::span<InputResult> subres) -> InputResult {
1449
2.94M
            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
2.94M
            if (node.GetType() << "z"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() == 0);
1456
2.94M
            if (node.GetType() << "z"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() == 0);
1457
1458
            // For 'o' nodes, available satisfactions/dissatisfactions must have stack size 1.
1459
2.94M
            if (node.GetType() << "o"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() == 1);
1460
2.94M
            if (node.GetType() << "o"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() == 1);
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
2.94M
            if (node.GetType() << "n"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() >= 1);
1465
2.94M
            if (node.GetType() << "n"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() >= 1);
1466
2.94M
            if (node.GetType() << "n"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(!ret.sat.stack.back().empty());
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
2.94M
            if (node.GetType() << "d"_mst) CHECK_NONFATAL(ret.nsat.available != Availability::NO);
1471
2.94M
            if (node.GetType() << "d"_mst) CHECK_NONFATAL(!ret.nsat.has_sig);
1472
2.94M
            if (node.GetType() << "d"_mst && !ret.nsat.malleable) CHECK_NONFATAL(!ret.nsat.non_canon);
1473
1474
            // For 'f'/'s' nodes, dissatisfactions/satisfactions must have a signature.
1475
2.94M
            if (node.GetType() << "f"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.has_sig);
1476
2.94M
            if (node.GetType() << "s"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.has_sig);
1477
1478
            // For non-malleable 'e' nodes, a non-malleable dissatisfaction must exist.
1479
2.94M
            if (node.GetType() << "me"_mst) CHECK_NONFATAL(ret.nsat.available != Availability::NO);
1480
2.94M
            if (node.GetType() << "me"_mst) CHECK_NONFATAL(!ret.nsat.malleable);
1481
1482
            // For 'm' nodes, if a satisfaction exists, it must be non-malleable.
1483
2.94M
            if (node.GetType() << "m"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(!ret.sat.malleable);
1484
1485
            // If a non-malleable satisfaction exists, it must be canonical.
1486
2.94M
            if (ret.sat.available != Availability::NO && !ret.sat.malleable) CHECK_NONFATAL(!ret.sat.non_canon);
1487
1488
2.94M
            return ret;
1489
2.94M
        };
miniscript_tests.cpp:miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)::operator()(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>) const
Line
Count
Source
1448
1.61M
        auto tester = [&helper](const Node& node, std::span<InputResult> subres) -> InputResult {
1449
1.61M
            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
1.61M
            if (node.GetType() << "z"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() == 0);
1456
1.61M
            if (node.GetType() << "z"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() == 0);
1457
1458
            // For 'o' nodes, available satisfactions/dissatisfactions must have stack size 1.
1459
1.61M
            if (node.GetType() << "o"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() == 1);
1460
1.61M
            if (node.GetType() << "o"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() == 1);
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
1.61M
            if (node.GetType() << "n"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() >= 1);
1465
1.61M
            if (node.GetType() << "n"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() >= 1);
1466
1.61M
            if (node.GetType() << "n"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(!ret.sat.stack.back().empty());
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
1.61M
            if (node.GetType() << "d"_mst) CHECK_NONFATAL(ret.nsat.available != Availability::NO);
1471
1.61M
            if (node.GetType() << "d"_mst) CHECK_NONFATAL(!ret.nsat.has_sig);
1472
1.61M
            if (node.GetType() << "d"_mst && !ret.nsat.malleable) CHECK_NONFATAL(!ret.nsat.non_canon);
1473
1474
            // For 'f'/'s' nodes, dissatisfactions/satisfactions must have a signature.
1475
1.61M
            if (node.GetType() << "f"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.has_sig);
1476
1.61M
            if (node.GetType() << "s"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.has_sig);
1477
1478
            // For non-malleable 'e' nodes, a non-malleable dissatisfaction must exist.
1479
1.61M
            if (node.GetType() << "me"_mst) CHECK_NONFATAL(ret.nsat.available != Availability::NO);
1480
1.61M
            if (node.GetType() << "me"_mst) CHECK_NONFATAL(!ret.nsat.malleable);
1481
1482
            // For 'm' nodes, if a satisfaction exists, it must be non-malleable.
1483
1.61M
            if (node.GetType() << "m"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(!ret.sat.malleable);
1484
1485
            // If a non-malleable satisfaction exists, it must be canonical.
1486
1.61M
            if (ret.sat.available != Availability::NO && !ret.sat.malleable) CHECK_NONFATAL(!ret.sat.non_canon);
1487
1488
1.61M
            return ret;
1489
1.61M
        };
miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const::'lambda0'(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)::operator()(miniscript::Node<XOnlyPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>) const
Line
Count
Source
1448
1.32M
        auto tester = [&helper](const Node& node, std::span<InputResult> subres) -> InputResult {
1449
1.32M
            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
1.32M
            if (node.GetType() << "z"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() == 0);
1456
1.32M
            if (node.GetType() << "z"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() == 0);
1457
1458
            // For 'o' nodes, available satisfactions/dissatisfactions must have stack size 1.
1459
1.32M
            if (node.GetType() << "o"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() == 1);
1460
1.32M
            if (node.GetType() << "o"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() == 1);
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
1.32M
            if (node.GetType() << "n"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() >= 1);
1465
1.32M
            if (node.GetType() << "n"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() >= 1);
1466
1.32M
            if (node.GetType() << "n"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(!ret.sat.stack.back().empty());
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
1.32M
            if (node.GetType() << "d"_mst) CHECK_NONFATAL(ret.nsat.available != Availability::NO);
1471
1.32M
            if (node.GetType() << "d"_mst) CHECK_NONFATAL(!ret.nsat.has_sig);
1472
1.32M
            if (node.GetType() << "d"_mst && !ret.nsat.malleable) CHECK_NONFATAL(!ret.nsat.non_canon);
1473
1474
            // For 'f'/'s' nodes, dissatisfactions/satisfactions must have a signature.
1475
1.32M
            if (node.GetType() << "f"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.has_sig);
1476
1.32M
            if (node.GetType() << "s"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.has_sig);
1477
1478
            // For non-malleable 'e' nodes, a non-malleable dissatisfaction must exist.
1479
1.32M
            if (node.GetType() << "me"_mst) CHECK_NONFATAL(ret.nsat.available != Availability::NO);
1480
1.32M
            if (node.GetType() << "me"_mst) CHECK_NONFATAL(!ret.nsat.malleable);
1481
1482
            // For 'm' nodes, if a satisfaction exists, it must be non-malleable.
1483
1.32M
            if (node.GetType() << "m"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(!ret.sat.malleable);
1484
1485
            // If a non-malleable satisfaction exists, it must be canonical.
1486
1.32M
            if (ret.sat.available != Availability::NO && !ret.sat.malleable) CHECK_NONFATAL(!ret.sat.non_canon);
1487
1488
1.32M
            return ret;
1489
1.32M
        };
miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const::'lambda0'(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>)::operator()(miniscript::Node<CPubKey> const&, std::span<miniscript::internal::InputResult, 18446744073709551615ul>) const
Line
Count
Source
1448
3.28k
        auto tester = [&helper](const Node& node, std::span<InputResult> subres) -> InputResult {
1449
3.28k
            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
3.28k
            if (node.GetType() << "z"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() == 0);
1456
3.28k
            if (node.GetType() << "z"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() == 0);
1457
1458
            // For 'o' nodes, available satisfactions/dissatisfactions must have stack size 1.
1459
3.28k
            if (node.GetType() << "o"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() == 1);
1460
3.28k
            if (node.GetType() << "o"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() == 1);
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
3.28k
            if (node.GetType() << "n"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() >= 1);
1465
3.28k
            if (node.GetType() << "n"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() >= 1);
1466
3.28k
            if (node.GetType() << "n"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(!ret.sat.stack.back().empty());
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
3.28k
            if (node.GetType() << "d"_mst) CHECK_NONFATAL(ret.nsat.available != Availability::NO);
1471
3.28k
            if (node.GetType() << "d"_mst) CHECK_NONFATAL(!ret.nsat.has_sig);
1472
3.28k
            if (node.GetType() << "d"_mst && !ret.nsat.malleable) CHECK_NONFATAL(!ret.nsat.non_canon);
1473
1474
            // For 'f'/'s' nodes, dissatisfactions/satisfactions must have a signature.
1475
3.28k
            if (node.GetType() << "f"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.has_sig);
1476
3.28k
            if (node.GetType() << "s"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.has_sig);
1477
1478
            // For non-malleable 'e' nodes, a non-malleable dissatisfaction must exist.
1479
3.28k
            if (node.GetType() << "me"_mst) CHECK_NONFATAL(ret.nsat.available != Availability::NO);
1480
3.28k
            if (node.GetType() << "me"_mst) CHECK_NONFATAL(!ret.nsat.malleable);
1481
1482
            // For 'm' nodes, if a satisfaction exists, it must be non-malleable.
1483
3.28k
            if (node.GetType() << "m"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(!ret.sat.malleable);
1484
1485
            // If a non-malleable satisfaction exists, it must be canonical.
1486
3.28k
            if (ret.sat.available != Availability::NO && !ret.sat.malleable) CHECK_NONFATAL(!ret.sat.non_canon);
1487
1488
3.28k
            return ret;
1489
3.28k
        };
1490
1491
9.47k
        return TreeEval<InputResult>(tester);
1492
9.47k
    }
miniscript_tests.cpp:miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&) const
Line
Count
Source
1247
4.82k
    internal::InputResult ProduceInput(const Ctx& ctx) const {
1248
4.82k
        using namespace internal;
1249
1250
        // Internal function which is invoked for every tree node, constructing satisfaction/dissatisfactions
1251
        // given those of its subnodes.
1252
4.82k
        auto helper = [&ctx](const Node& node, std::span<InputResult> subres) -> InputResult {
1253
4.82k
            switch (node.fragment) {
1254
4.82k
                case Fragment::PK_K: {
1255
4.82k
                    std::vector<unsigned char> sig;
1256
4.82k
                    Availability avail = ctx.Sign(node.keys[0], sig);
1257
4.82k
                    return {ZERO, InputStack(std::move(sig)).SetWithSig().SetAvailable(avail)};
1258
4.82k
                }
1259
4.82k
                case Fragment::PK_H: {
1260
4.82k
                    std::vector<unsigned char> key = ctx.ToPKBytes(node.keys[0]), sig;
1261
4.82k
                    Availability avail = ctx.Sign(node.keys[0], sig);
1262
4.82k
                    return {ZERO + InputStack(key), (InputStack(std::move(sig)).SetWithSig() + InputStack(key)).SetAvailable(avail)};
1263
4.82k
                }
1264
4.82k
                case Fragment::MULTI_A: {
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
4.82k
                    std::vector<InputStack> sats = Vector(EMPTY);
1268
4.82k
                    for (size_t i = 0; i < node.keys.size(); ++i) {
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
4.82k
                        std::vector<unsigned char> sig;
1272
4.82k
                        Availability avail = ctx.Sign(node.keys[node.keys.size() - 1 - i], sig);
1273
                        // Compute signature stack for just this key.
1274
4.82k
                        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
4.82k
                        std::vector<InputStack> next_sats;
1279
4.82k
                        next_sats.push_back(sats[0] + ZERO);
1280
4.82k
                        for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + ZERO) | (std::move(sats[j - 1]) + sat));
1281
4.82k
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(sat));
1282
                        // Switch over.
1283
4.82k
                        sats = std::move(next_sats);
1284
4.82k
                    }
1285
                    // The dissatisfaction consists of as many empty vectors as there are keys, which is the same as
1286
                    // satisfying 0 keys.
1287
4.82k
                    auto& nsat{sats[0]};
1288
4.82k
                    CHECK_NONFATAL(node.k != 0);
1289
4.82k
                    assert(node.k < sats.size());
1290
4.82k
                    return {std::move(nsat), std::move(sats[node.k])};
1291
4.82k
                }
1292
4.82k
                case Fragment::MULTI: {
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
4.82k
                    std::vector<InputStack> sats = Vector(ZERO);
1297
4.82k
                    for (size_t i = 0; i < node.keys.size(); ++i) {
1298
4.82k
                        std::vector<unsigned char> sig;
1299
4.82k
                        Availability avail = ctx.Sign(node.keys[i], sig);
1300
                        // Compute signature stack for just the i'th key.
1301
4.82k
                        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
4.82k
                        std::vector<InputStack> next_sats;
1306
4.82k
                        next_sats.push_back(sats[0]);
1307
4.82k
                        for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back(sats[j] | (std::move(sats[j - 1]) + sat));
1308
4.82k
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(sat));
1309
                        // Switch over.
1310
4.82k
                        sats = std::move(next_sats);
1311
4.82k
                    }
1312
                    // The dissatisfaction consists of k+1 stack elements all equal to 0.
1313
4.82k
                    InputStack nsat = ZERO;
1314
4.82k
                    for (size_t i = 0; i < node.k; ++i) nsat = std::move(nsat) + ZERO;
1315
4.82k
                    assert(node.k < sats.size());
1316
4.82k
                    return {std::move(nsat), std::move(sats[node.k])};
1317
4.82k
                }
1318
4.82k
                case Fragment::THRESH: {
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
4.82k
                    std::vector<InputStack> sats = Vector(EMPTY);
1323
4.82k
                    for (size_t i = 0; i < subres.size(); ++i) {
1324
                        // Introduce an alias for the i'th last satisfaction/dissatisfaction.
1325
4.82k
                        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
4.82k
                        std::vector<InputStack> next_sats;
1330
4.82k
                        next_sats.push_back(sats[0] + res.nsat);
1331
4.82k
                        for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + res.nsat) | (std::move(sats[j - 1]) + res.sat));
1332
4.82k
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(res.sat));
1333
                        // Switch over.
1334
4.82k
                        sats = std::move(next_sats);
1335
4.82k
                    }
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
4.82k
                    InputStack nsat = INVALID;
1339
4.82k
                    for (size_t i = 0; i < sats.size(); ++i) {
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
4.82k
                        if (i != 0 && i != node.k) sats[i].SetMalleable().SetNonCanon();
1347
                        // Include all dissatisfactions (even these non-canonical ones) in nsat.
1348
4.82k
                        if (i != node.k) nsat = std::move(nsat) | std::move(sats[i]);
1349
4.82k
                    }
1350
4.82k
                    assert(node.k < sats.size());
1351
4.82k
                    return {std::move(nsat), std::move(sats[node.k])};
1352
4.82k
                }
1353
4.82k
                case Fragment::OLDER: {
1354
4.82k
                    return {INVALID, ctx.CheckOlder(node.k) ? EMPTY : INVALID};
1355
4.82k
                }
1356
4.82k
                case Fragment::AFTER: {
1357
4.82k
                    return {INVALID, ctx.CheckAfter(node.k) ? EMPTY : INVALID};
1358
4.82k
                }
1359
4.82k
                case Fragment::SHA256: {
1360
4.82k
                    std::vector<unsigned char> preimage;
1361
4.82k
                    Availability avail = ctx.SatSHA256(node.data, preimage);
1362
4.82k
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1363
4.82k
                }
1364
4.82k
                case Fragment::RIPEMD160: {
1365
4.82k
                    std::vector<unsigned char> preimage;
1366
4.82k
                    Availability avail = ctx.SatRIPEMD160(node.data, preimage);
1367
4.82k
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1368
4.82k
                }
1369
4.82k
                case Fragment::HASH256: {
1370
4.82k
                    std::vector<unsigned char> preimage;
1371
4.82k
                    Availability avail = ctx.SatHASH256(node.data, preimage);
1372
4.82k
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1373
4.82k
                }
1374
4.82k
                case Fragment::HASH160: {
1375
4.82k
                    std::vector<unsigned char> preimage;
1376
4.82k
                    Availability avail = ctx.SatHASH160(node.data, preimage);
1377
4.82k
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1378
4.82k
                }
1379
4.82k
                case Fragment::AND_V: {
1380
4.82k
                    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
4.82k
                    return {(y.nsat + x.sat).SetNonCanon(), y.sat + x.sat};
1388
4.82k
                }
1389
4.82k
                case Fragment::AND_B: {
1390
4.82k
                    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
4.82k
                    return {(y.nsat + x.nsat) | (y.sat + x.nsat).SetMalleable().SetNonCanon() | (y.nsat + x.sat).SetMalleable().SetNonCanon(), y.sat + x.sat};
1397
4.82k
                }
1398
4.82k
                case Fragment::OR_B: {
1399
4.82k
                    auto& x = subres[0], &z = subres[1];
1400
                    // The (sat(Z) sat(X)) solution is overcomplete (attacker can change either into dsat).
1401
4.82k
                    return {z.nsat + x.nsat, (z.nsat + x.sat) | (z.sat + x.nsat) | (z.sat + x.sat).SetMalleable().SetNonCanon()};
1402
4.82k
                }
1403
4.82k
                case Fragment::OR_C: {
1404
4.82k
                    auto& x = subres[0], &z = subres[1];
1405
4.82k
                    return {INVALID, std::move(x.sat) | (z.sat + x.nsat)};
1406
4.82k
                }
1407
4.82k
                case Fragment::OR_D: {
1408
4.82k
                    auto& x = subres[0], &z = subres[1];
1409
4.82k
                    return {z.nsat + x.nsat, std::move(x.sat) | (z.sat + x.nsat)};
1410
4.82k
                }
1411
4.82k
                case Fragment::OR_I: {
1412
4.82k
                    auto& x = subres[0], &z = subres[1];
1413
4.82k
                    return {(x.nsat + ONE) | (z.nsat + ZERO), (x.sat + ONE) | (z.sat + ZERO)};
1414
4.82k
                }
1415
4.82k
                case Fragment::ANDOR: {
1416
4.82k
                    auto& x = subres[0], &y = subres[1], &z = subres[2];
1417
4.82k
                    return {(y.nsat + x.sat).SetNonCanon() | (z.nsat + x.nsat), (y.sat + x.sat) | (z.sat + x.nsat)};
1418
4.82k
                }
1419
4.82k
                case Fragment::WRAP_A:
1420
4.82k
                case Fragment::WRAP_S:
1421
4.82k
                case Fragment::WRAP_C:
1422
4.82k
                case Fragment::WRAP_N:
1423
4.82k
                    return std::move(subres[0]);
1424
4.82k
                case Fragment::WRAP_D: {
1425
4.82k
                    auto &x = subres[0];
1426
4.82k
                    return {ZERO, x.sat + ONE};
1427
4.82k
                }
1428
4.82k
                case Fragment::WRAP_J: {
1429
4.82k
                    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
4.82k
                    return {InputStack(ZERO).SetMalleable(x.nsat.available != Availability::NO && !x.nsat.has_sig), std::move(x.sat)};
1436
4.82k
                }
1437
4.82k
                case Fragment::WRAP_V: {
1438
4.82k
                    auto &x = subres[0];
1439
4.82k
                    return {INVALID, std::move(x.sat)};
1440
4.82k
                }
1441
4.82k
                case Fragment::JUST_0: return {EMPTY, INVALID};
1442
4.82k
                case Fragment::JUST_1: return {INVALID, EMPTY};
1443
4.82k
            }
1444
4.82k
            assert(false);
1445
4.82k
            return {INVALID, INVALID};
1446
4.82k
        };
1447
1448
4.82k
        auto tester = [&helper](const Node& node, std::span<InputResult> subres) -> InputResult {
1449
4.82k
            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
4.82k
            if (node.GetType() << "z"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() == 0);
1456
4.82k
            if (node.GetType() << "z"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() == 0);
1457
1458
            // For 'o' nodes, available satisfactions/dissatisfactions must have stack size 1.
1459
4.82k
            if (node.GetType() << "o"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() == 1);
1460
4.82k
            if (node.GetType() << "o"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() == 1);
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
4.82k
            if (node.GetType() << "n"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() >= 1);
1465
4.82k
            if (node.GetType() << "n"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() >= 1);
1466
4.82k
            if (node.GetType() << "n"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(!ret.sat.stack.back().empty());
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
4.82k
            if (node.GetType() << "d"_mst) CHECK_NONFATAL(ret.nsat.available != Availability::NO);
1471
4.82k
            if (node.GetType() << "d"_mst) CHECK_NONFATAL(!ret.nsat.has_sig);
1472
4.82k
            if (node.GetType() << "d"_mst && !ret.nsat.malleable) CHECK_NONFATAL(!ret.nsat.non_canon);
1473
1474
            // For 'f'/'s' nodes, dissatisfactions/satisfactions must have a signature.
1475
4.82k
            if (node.GetType() << "f"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.has_sig);
1476
4.82k
            if (node.GetType() << "s"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.has_sig);
1477
1478
            // For non-malleable 'e' nodes, a non-malleable dissatisfaction must exist.
1479
4.82k
            if (node.GetType() << "me"_mst) CHECK_NONFATAL(ret.nsat.available != Availability::NO);
1480
4.82k
            if (node.GetType() << "me"_mst) CHECK_NONFATAL(!ret.nsat.malleable);
1481
1482
            // For 'm' nodes, if a satisfaction exists, it must be non-malleable.
1483
4.82k
            if (node.GetType() << "m"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(!ret.sat.malleable);
1484
1485
            // If a non-malleable satisfaction exists, it must be canonical.
1486
4.82k
            if (ret.sat.available != Availability::NO && !ret.sat.malleable) CHECK_NONFATAL(!ret.sat.non_canon);
1487
1488
4.82k
            return ret;
1489
4.82k
        };
1490
1491
4.82k
        return TreeEval<InputResult>(tester);
1492
4.82k
    }
miniscript::internal::InputResult miniscript::Node<XOnlyPubKey>::ProduceInput<TapSatisfier>(TapSatisfier const&) const
Line
Count
Source
1247
4.41k
    internal::InputResult ProduceInput(const Ctx& ctx) const {
1248
4.41k
        using namespace internal;
1249
1250
        // Internal function which is invoked for every tree node, constructing satisfaction/dissatisfactions
1251
        // given those of its subnodes.
1252
4.41k
        auto helper = [&ctx](const Node& node, std::span<InputResult> subres) -> InputResult {
1253
4.41k
            switch (node.fragment) {
1254
4.41k
                case Fragment::PK_K: {
1255
4.41k
                    std::vector<unsigned char> sig;
1256
4.41k
                    Availability avail = ctx.Sign(node.keys[0], sig);
1257
4.41k
                    return {ZERO, InputStack(std::move(sig)).SetWithSig().SetAvailable(avail)};
1258
4.41k
                }
1259
4.41k
                case Fragment::PK_H: {
1260
4.41k
                    std::vector<unsigned char> key = ctx.ToPKBytes(node.keys[0]), sig;
1261
4.41k
                    Availability avail = ctx.Sign(node.keys[0], sig);
1262
4.41k
                    return {ZERO + InputStack(key), (InputStack(std::move(sig)).SetWithSig() + InputStack(key)).SetAvailable(avail)};
1263
4.41k
                }
1264
4.41k
                case Fragment::MULTI_A: {
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
4.41k
                    std::vector<InputStack> sats = Vector(EMPTY);
1268
4.41k
                    for (size_t i = 0; i < node.keys.size(); ++i) {
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
4.41k
                        std::vector<unsigned char> sig;
1272
4.41k
                        Availability avail = ctx.Sign(node.keys[node.keys.size() - 1 - i], sig);
1273
                        // Compute signature stack for just this key.
1274
4.41k
                        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
4.41k
                        std::vector<InputStack> next_sats;
1279
4.41k
                        next_sats.push_back(sats[0] + ZERO);
1280
4.41k
                        for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + ZERO) | (std::move(sats[j - 1]) + sat));
1281
4.41k
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(sat));
1282
                        // Switch over.
1283
4.41k
                        sats = std::move(next_sats);
1284
4.41k
                    }
1285
                    // The dissatisfaction consists of as many empty vectors as there are keys, which is the same as
1286
                    // satisfying 0 keys.
1287
4.41k
                    auto& nsat{sats[0]};
1288
4.41k
                    CHECK_NONFATAL(node.k != 0);
1289
4.41k
                    assert(node.k < sats.size());
1290
4.41k
                    return {std::move(nsat), std::move(sats[node.k])};
1291
4.41k
                }
1292
4.41k
                case Fragment::MULTI: {
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
4.41k
                    std::vector<InputStack> sats = Vector(ZERO);
1297
4.41k
                    for (size_t i = 0; i < node.keys.size(); ++i) {
1298
4.41k
                        std::vector<unsigned char> sig;
1299
4.41k
                        Availability avail = ctx.Sign(node.keys[i], sig);
1300
                        // Compute signature stack for just the i'th key.
1301
4.41k
                        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
4.41k
                        std::vector<InputStack> next_sats;
1306
4.41k
                        next_sats.push_back(sats[0]);
1307
4.41k
                        for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back(sats[j] | (std::move(sats[j - 1]) + sat));
1308
4.41k
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(sat));
1309
                        // Switch over.
1310
4.41k
                        sats = std::move(next_sats);
1311
4.41k
                    }
1312
                    // The dissatisfaction consists of k+1 stack elements all equal to 0.
1313
4.41k
                    InputStack nsat = ZERO;
1314
4.41k
                    for (size_t i = 0; i < node.k; ++i) nsat = std::move(nsat) + ZERO;
1315
4.41k
                    assert(node.k < sats.size());
1316
4.41k
                    return {std::move(nsat), std::move(sats[node.k])};
1317
4.41k
                }
1318
4.41k
                case Fragment::THRESH: {
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
4.41k
                    std::vector<InputStack> sats = Vector(EMPTY);
1323
4.41k
                    for (size_t i = 0; i < subres.size(); ++i) {
1324
                        // Introduce an alias for the i'th last satisfaction/dissatisfaction.
1325
4.41k
                        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
4.41k
                        std::vector<InputStack> next_sats;
1330
4.41k
                        next_sats.push_back(sats[0] + res.nsat);
1331
4.41k
                        for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + res.nsat) | (std::move(sats[j - 1]) + res.sat));
1332
4.41k
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(res.sat));
1333
                        // Switch over.
1334
4.41k
                        sats = std::move(next_sats);
1335
4.41k
                    }
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
4.41k
                    InputStack nsat = INVALID;
1339
4.41k
                    for (size_t i = 0; i < sats.size(); ++i) {
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
4.41k
                        if (i != 0 && i != node.k) sats[i].SetMalleable().SetNonCanon();
1347
                        // Include all dissatisfactions (even these non-canonical ones) in nsat.
1348
4.41k
                        if (i != node.k) nsat = std::move(nsat) | std::move(sats[i]);
1349
4.41k
                    }
1350
4.41k
                    assert(node.k < sats.size());
1351
4.41k
                    return {std::move(nsat), std::move(sats[node.k])};
1352
4.41k
                }
1353
4.41k
                case Fragment::OLDER: {
1354
4.41k
                    return {INVALID, ctx.CheckOlder(node.k) ? EMPTY : INVALID};
1355
4.41k
                }
1356
4.41k
                case Fragment::AFTER: {
1357
4.41k
                    return {INVALID, ctx.CheckAfter(node.k) ? EMPTY : INVALID};
1358
4.41k
                }
1359
4.41k
                case Fragment::SHA256: {
1360
4.41k
                    std::vector<unsigned char> preimage;
1361
4.41k
                    Availability avail = ctx.SatSHA256(node.data, preimage);
1362
4.41k
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1363
4.41k
                }
1364
4.41k
                case Fragment::RIPEMD160: {
1365
4.41k
                    std::vector<unsigned char> preimage;
1366
4.41k
                    Availability avail = ctx.SatRIPEMD160(node.data, preimage);
1367
4.41k
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1368
4.41k
                }
1369
4.41k
                case Fragment::HASH256: {
1370
4.41k
                    std::vector<unsigned char> preimage;
1371
4.41k
                    Availability avail = ctx.SatHASH256(node.data, preimage);
1372
4.41k
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1373
4.41k
                }
1374
4.41k
                case Fragment::HASH160: {
1375
4.41k
                    std::vector<unsigned char> preimage;
1376
4.41k
                    Availability avail = ctx.SatHASH160(node.data, preimage);
1377
4.41k
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1378
4.41k
                }
1379
4.41k
                case Fragment::AND_V: {
1380
4.41k
                    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
4.41k
                    return {(y.nsat + x.sat).SetNonCanon(), y.sat + x.sat};
1388
4.41k
                }
1389
4.41k
                case Fragment::AND_B: {
1390
4.41k
                    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
4.41k
                    return {(y.nsat + x.nsat) | (y.sat + x.nsat).SetMalleable().SetNonCanon() | (y.nsat + x.sat).SetMalleable().SetNonCanon(), y.sat + x.sat};
1397
4.41k
                }
1398
4.41k
                case Fragment::OR_B: {
1399
4.41k
                    auto& x = subres[0], &z = subres[1];
1400
                    // The (sat(Z) sat(X)) solution is overcomplete (attacker can change either into dsat).
1401
4.41k
                    return {z.nsat + x.nsat, (z.nsat + x.sat) | (z.sat + x.nsat) | (z.sat + x.sat).SetMalleable().SetNonCanon()};
1402
4.41k
                }
1403
4.41k
                case Fragment::OR_C: {
1404
4.41k
                    auto& x = subres[0], &z = subres[1];
1405
4.41k
                    return {INVALID, std::move(x.sat) | (z.sat + x.nsat)};
1406
4.41k
                }
1407
4.41k
                case Fragment::OR_D: {
1408
4.41k
                    auto& x = subres[0], &z = subres[1];
1409
4.41k
                    return {z.nsat + x.nsat, std::move(x.sat) | (z.sat + x.nsat)};
1410
4.41k
                }
1411
4.41k
                case Fragment::OR_I: {
1412
4.41k
                    auto& x = subres[0], &z = subres[1];
1413
4.41k
                    return {(x.nsat + ONE) | (z.nsat + ZERO), (x.sat + ONE) | (z.sat + ZERO)};
1414
4.41k
                }
1415
4.41k
                case Fragment::ANDOR: {
1416
4.41k
                    auto& x = subres[0], &y = subres[1], &z = subres[2];
1417
4.41k
                    return {(y.nsat + x.sat).SetNonCanon() | (z.nsat + x.nsat), (y.sat + x.sat) | (z.sat + x.nsat)};
1418
4.41k
                }
1419
4.41k
                case Fragment::WRAP_A:
1420
4.41k
                case Fragment::WRAP_S:
1421
4.41k
                case Fragment::WRAP_C:
1422
4.41k
                case Fragment::WRAP_N:
1423
4.41k
                    return std::move(subres[0]);
1424
4.41k
                case Fragment::WRAP_D: {
1425
4.41k
                    auto &x = subres[0];
1426
4.41k
                    return {ZERO, x.sat + ONE};
1427
4.41k
                }
1428
4.41k
                case Fragment::WRAP_J: {
1429
4.41k
                    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
4.41k
                    return {InputStack(ZERO).SetMalleable(x.nsat.available != Availability::NO && !x.nsat.has_sig), std::move(x.sat)};
1436
4.41k
                }
1437
4.41k
                case Fragment::WRAP_V: {
1438
4.41k
                    auto &x = subres[0];
1439
4.41k
                    return {INVALID, std::move(x.sat)};
1440
4.41k
                }
1441
4.41k
                case Fragment::JUST_0: return {EMPTY, INVALID};
1442
4.41k
                case Fragment::JUST_1: return {INVALID, EMPTY};
1443
4.41k
            }
1444
4.41k
            assert(false);
1445
4.41k
            return {INVALID, INVALID};
1446
4.41k
        };
1447
1448
4.41k
        auto tester = [&helper](const Node& node, std::span<InputResult> subres) -> InputResult {
1449
4.41k
            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
4.41k
            if (node.GetType() << "z"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() == 0);
1456
4.41k
            if (node.GetType() << "z"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() == 0);
1457
1458
            // For 'o' nodes, available satisfactions/dissatisfactions must have stack size 1.
1459
4.41k
            if (node.GetType() << "o"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() == 1);
1460
4.41k
            if (node.GetType() << "o"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() == 1);
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
4.41k
            if (node.GetType() << "n"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() >= 1);
1465
4.41k
            if (node.GetType() << "n"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() >= 1);
1466
4.41k
            if (node.GetType() << "n"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(!ret.sat.stack.back().empty());
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
4.41k
            if (node.GetType() << "d"_mst) CHECK_NONFATAL(ret.nsat.available != Availability::NO);
1471
4.41k
            if (node.GetType() << "d"_mst) CHECK_NONFATAL(!ret.nsat.has_sig);
1472
4.41k
            if (node.GetType() << "d"_mst && !ret.nsat.malleable) CHECK_NONFATAL(!ret.nsat.non_canon);
1473
1474
            // For 'f'/'s' nodes, dissatisfactions/satisfactions must have a signature.
1475
4.41k
            if (node.GetType() << "f"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.has_sig);
1476
4.41k
            if (node.GetType() << "s"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.has_sig);
1477
1478
            // For non-malleable 'e' nodes, a non-malleable dissatisfaction must exist.
1479
4.41k
            if (node.GetType() << "me"_mst) CHECK_NONFATAL(ret.nsat.available != Availability::NO);
1480
4.41k
            if (node.GetType() << "me"_mst) CHECK_NONFATAL(!ret.nsat.malleable);
1481
1482
            // For 'm' nodes, if a satisfaction exists, it must be non-malleable.
1483
4.41k
            if (node.GetType() << "m"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(!ret.sat.malleable);
1484
1485
            // If a non-malleable satisfaction exists, it must be canonical.
1486
4.41k
            if (ret.sat.available != Availability::NO && !ret.sat.malleable) CHECK_NONFATAL(!ret.sat.non_canon);
1487
1488
4.41k
            return ret;
1489
4.41k
        };
1490
1491
4.41k
        return TreeEval<InputResult>(tester);
1492
4.41k
    }
miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const
Line
Count
Source
1247
234
    internal::InputResult ProduceInput(const Ctx& ctx) const {
1248
234
        using namespace internal;
1249
1250
        // Internal function which is invoked for every tree node, constructing satisfaction/dissatisfactions
1251
        // given those of its subnodes.
1252
234
        auto helper = [&ctx](const Node& node, std::span<InputResult> subres) -> InputResult {
1253
234
            switch (node.fragment) {
1254
234
                case Fragment::PK_K: {
1255
234
                    std::vector<unsigned char> sig;
1256
234
                    Availability avail = ctx.Sign(node.keys[0], sig);
1257
234
                    return {ZERO, InputStack(std::move(sig)).SetWithSig().SetAvailable(avail)};
1258
234
                }
1259
234
                case Fragment::PK_H: {
1260
234
                    std::vector<unsigned char> key = ctx.ToPKBytes(node.keys[0]), sig;
1261
234
                    Availability avail = ctx.Sign(node.keys[0], sig);
1262
234
                    return {ZERO + InputStack(key), (InputStack(std::move(sig)).SetWithSig() + InputStack(key)).SetAvailable(avail)};
1263
234
                }
1264
234
                case Fragment::MULTI_A: {
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
234
                    std::vector<InputStack> sats = Vector(EMPTY);
1268
234
                    for (size_t i = 0; i < node.keys.size(); ++i) {
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
234
                        std::vector<unsigned char> sig;
1272
234
                        Availability avail = ctx.Sign(node.keys[node.keys.size() - 1 - i], sig);
1273
                        // Compute signature stack for just this key.
1274
234
                        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
234
                        std::vector<InputStack> next_sats;
1279
234
                        next_sats.push_back(sats[0] + ZERO);
1280
234
                        for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + ZERO) | (std::move(sats[j - 1]) + sat));
1281
234
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(sat));
1282
                        // Switch over.
1283
234
                        sats = std::move(next_sats);
1284
234
                    }
1285
                    // The dissatisfaction consists of as many empty vectors as there are keys, which is the same as
1286
                    // satisfying 0 keys.
1287
234
                    auto& nsat{sats[0]};
1288
234
                    CHECK_NONFATAL(node.k != 0);
1289
234
                    assert(node.k < sats.size());
1290
234
                    return {std::move(nsat), std::move(sats[node.k])};
1291
234
                }
1292
234
                case Fragment::MULTI: {
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
234
                    std::vector<InputStack> sats = Vector(ZERO);
1297
234
                    for (size_t i = 0; i < node.keys.size(); ++i) {
1298
234
                        std::vector<unsigned char> sig;
1299
234
                        Availability avail = ctx.Sign(node.keys[i], sig);
1300
                        // Compute signature stack for just the i'th key.
1301
234
                        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
234
                        std::vector<InputStack> next_sats;
1306
234
                        next_sats.push_back(sats[0]);
1307
234
                        for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back(sats[j] | (std::move(sats[j - 1]) + sat));
1308
234
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(sat));
1309
                        // Switch over.
1310
234
                        sats = std::move(next_sats);
1311
234
                    }
1312
                    // The dissatisfaction consists of k+1 stack elements all equal to 0.
1313
234
                    InputStack nsat = ZERO;
1314
234
                    for (size_t i = 0; i < node.k; ++i) nsat = std::move(nsat) + ZERO;
1315
234
                    assert(node.k < sats.size());
1316
234
                    return {std::move(nsat), std::move(sats[node.k])};
1317
234
                }
1318
234
                case Fragment::THRESH: {
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
234
                    std::vector<InputStack> sats = Vector(EMPTY);
1323
234
                    for (size_t i = 0; i < subres.size(); ++i) {
1324
                        // Introduce an alias for the i'th last satisfaction/dissatisfaction.
1325
234
                        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
234
                        std::vector<InputStack> next_sats;
1330
234
                        next_sats.push_back(sats[0] + res.nsat);
1331
234
                        for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + res.nsat) | (std::move(sats[j - 1]) + res.sat));
1332
234
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(res.sat));
1333
                        // Switch over.
1334
234
                        sats = std::move(next_sats);
1335
234
                    }
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
234
                    InputStack nsat = INVALID;
1339
234
                    for (size_t i = 0; i < sats.size(); ++i) {
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
234
                        if (i != 0 && i != node.k) sats[i].SetMalleable().SetNonCanon();
1347
                        // Include all dissatisfactions (even these non-canonical ones) in nsat.
1348
234
                        if (i != node.k) nsat = std::move(nsat) | std::move(sats[i]);
1349
234
                    }
1350
234
                    assert(node.k < sats.size());
1351
234
                    return {std::move(nsat), std::move(sats[node.k])};
1352
234
                }
1353
234
                case Fragment::OLDER: {
1354
234
                    return {INVALID, ctx.CheckOlder(node.k) ? EMPTY : INVALID};
1355
234
                }
1356
234
                case Fragment::AFTER: {
1357
234
                    return {INVALID, ctx.CheckAfter(node.k) ? EMPTY : INVALID};
1358
234
                }
1359
234
                case Fragment::SHA256: {
1360
234
                    std::vector<unsigned char> preimage;
1361
234
                    Availability avail = ctx.SatSHA256(node.data, preimage);
1362
234
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1363
234
                }
1364
234
                case Fragment::RIPEMD160: {
1365
234
                    std::vector<unsigned char> preimage;
1366
234
                    Availability avail = ctx.SatRIPEMD160(node.data, preimage);
1367
234
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1368
234
                }
1369
234
                case Fragment::HASH256: {
1370
234
                    std::vector<unsigned char> preimage;
1371
234
                    Availability avail = ctx.SatHASH256(node.data, preimage);
1372
234
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1373
234
                }
1374
234
                case Fragment::HASH160: {
1375
234
                    std::vector<unsigned char> preimage;
1376
234
                    Availability avail = ctx.SatHASH160(node.data, preimage);
1377
234
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1378
234
                }
1379
234
                case Fragment::AND_V: {
1380
234
                    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
234
                    return {(y.nsat + x.sat).SetNonCanon(), y.sat + x.sat};
1388
234
                }
1389
234
                case Fragment::AND_B: {
1390
234
                    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
234
                    return {(y.nsat + x.nsat) | (y.sat + x.nsat).SetMalleable().SetNonCanon() | (y.nsat + x.sat).SetMalleable().SetNonCanon(), y.sat + x.sat};
1397
234
                }
1398
234
                case Fragment::OR_B: {
1399
234
                    auto& x = subres[0], &z = subres[1];
1400
                    // The (sat(Z) sat(X)) solution is overcomplete (attacker can change either into dsat).
1401
234
                    return {z.nsat + x.nsat, (z.nsat + x.sat) | (z.sat + x.nsat) | (z.sat + x.sat).SetMalleable().SetNonCanon()};
1402
234
                }
1403
234
                case Fragment::OR_C: {
1404
234
                    auto& x = subres[0], &z = subres[1];
1405
234
                    return {INVALID, std::move(x.sat) | (z.sat + x.nsat)};
1406
234
                }
1407
234
                case Fragment::OR_D: {
1408
234
                    auto& x = subres[0], &z = subres[1];
1409
234
                    return {z.nsat + x.nsat, std::move(x.sat) | (z.sat + x.nsat)};
1410
234
                }
1411
234
                case Fragment::OR_I: {
1412
234
                    auto& x = subres[0], &z = subres[1];
1413
234
                    return {(x.nsat + ONE) | (z.nsat + ZERO), (x.sat + ONE) | (z.sat + ZERO)};
1414
234
                }
1415
234
                case Fragment::ANDOR: {
1416
234
                    auto& x = subres[0], &y = subres[1], &z = subres[2];
1417
234
                    return {(y.nsat + x.sat).SetNonCanon() | (z.nsat + x.nsat), (y.sat + x.sat) | (z.sat + x.nsat)};
1418
234
                }
1419
234
                case Fragment::WRAP_A:
1420
234
                case Fragment::WRAP_S:
1421
234
                case Fragment::WRAP_C:
1422
234
                case Fragment::WRAP_N:
1423
234
                    return std::move(subres[0]);
1424
234
                case Fragment::WRAP_D: {
1425
234
                    auto &x = subres[0];
1426
234
                    return {ZERO, x.sat + ONE};
1427
234
                }
1428
234
                case Fragment::WRAP_J: {
1429
234
                    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
234
                    return {InputStack(ZERO).SetMalleable(x.nsat.available != Availability::NO && !x.nsat.has_sig), std::move(x.sat)};
1436
234
                }
1437
234
                case Fragment::WRAP_V: {
1438
234
                    auto &x = subres[0];
1439
234
                    return {INVALID, std::move(x.sat)};
1440
234
                }
1441
234
                case Fragment::JUST_0: return {EMPTY, INVALID};
1442
234
                case Fragment::JUST_1: return {INVALID, EMPTY};
1443
234
            }
1444
234
            assert(false);
1445
234
            return {INVALID, INVALID};
1446
234
        };
1447
1448
234
        auto tester = [&helper](const Node& node, std::span<InputResult> subres) -> InputResult {
1449
234
            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
234
            if (node.GetType() << "z"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() == 0);
1456
234
            if (node.GetType() << "z"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() == 0);
1457
1458
            // For 'o' nodes, available satisfactions/dissatisfactions must have stack size 1.
1459
234
            if (node.GetType() << "o"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() == 1);
1460
234
            if (node.GetType() << "o"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() == 1);
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
234
            if (node.GetType() << "n"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() >= 1);
1465
234
            if (node.GetType() << "n"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() >= 1);
1466
234
            if (node.GetType() << "n"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(!ret.sat.stack.back().empty());
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
234
            if (node.GetType() << "d"_mst) CHECK_NONFATAL(ret.nsat.available != Availability::NO);
1471
234
            if (node.GetType() << "d"_mst) CHECK_NONFATAL(!ret.nsat.has_sig);
1472
234
            if (node.GetType() << "d"_mst && !ret.nsat.malleable) CHECK_NONFATAL(!ret.nsat.non_canon);
1473
1474
            // For 'f'/'s' nodes, dissatisfactions/satisfactions must have a signature.
1475
234
            if (node.GetType() << "f"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.has_sig);
1476
234
            if (node.GetType() << "s"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.has_sig);
1477
1478
            // For non-malleable 'e' nodes, a non-malleable dissatisfaction must exist.
1479
234
            if (node.GetType() << "me"_mst) CHECK_NONFATAL(ret.nsat.available != Availability::NO);
1480
234
            if (node.GetType() << "me"_mst) CHECK_NONFATAL(!ret.nsat.malleable);
1481
1482
            // For 'm' nodes, if a satisfaction exists, it must be non-malleable.
1483
234
            if (node.GetType() << "m"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(!ret.sat.malleable);
1484
1485
            // If a non-malleable satisfaction exists, it must be canonical.
1486
234
            if (ret.sat.available != Availability::NO && !ret.sat.malleable) CHECK_NONFATAL(!ret.sat.non_canon);
1487
1488
234
            return ret;
1489
234
        };
1490
1491
234
        return TreeEval<InputResult>(tester);
1492
234
    }
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
5.86k
    {
1502
        // We cannot use a lambda here, as lambdas are non assignable, and the set operations
1503
        // below require moving the comparators around.
1504
5.86k
        struct Comp {
1505
5.86k
            const Ctx* ctx_ptr;
1506
2.35M
            Comp(const Ctx& ctx) : ctx_ptr(&ctx) {}
miniscript_tests.cpp:void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp::Comp((anonymous namespace)::KeyConverter const&)
Line
Count
Source
1506
23.2k
            Comp(const Ctx& ctx) : ctx_ptr(&ctx) {}
descriptor.cpp:void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp::Comp((anonymous namespace)::KeyParser const&)
Line
Count
Source
1506
996k
            Comp(const Ctx& ctx) : ctx_ptr(&ctx) {}
void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp::Comp(TapSatisfier const&)
Line
Count
Source
1506
1.32M
            Comp(const Ctx& ctx) : ctx_ptr(&ctx) {}
void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp::Comp(WshSatisfier const&)
Line
Count
Source
1506
3.28k
            Comp(const Ctx& ctx) : ctx_ptr(&ctx) {}
1507
323k
            bool operator()(const Key& a, const Key& b) const { return ctx_ptr->KeyCompare(a, b); }
miniscript_tests.cpp:void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp::operator()(CPubKey const&, CPubKey const&) const
Line
Count
Source
1507
6.98k
            bool operator()(const Key& a, const Key& b) const { return ctx_ptr->KeyCompare(a, b); }
descriptor.cpp:void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp::operator()(unsigned int const&, unsigned int const&) const
Line
Count
Source
1507
4.97k
            bool operator()(const Key& a, const Key& b) const { return ctx_ptr->KeyCompare(a, b); }
void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp::operator()(XOnlyPubKey const&, XOnlyPubKey const&) const
Line
Count
Source
1507
309k
            bool operator()(const Key& a, const Key& b) const { return ctx_ptr->KeyCompare(a, b); }
void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp::operator()(CPubKey const&, CPubKey const&) const
Line
Count
Source
1507
1.25k
            bool operator()(const Key& a, const Key& b) const { return ctx_ptr->KeyCompare(a, b); }
1508
5.86k
        };
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
5.86k
        using keyset = std::set<Key, Comp>;
1514
5.86k
        using state = std::optional<keyset>;
1515
1516
2.35M
        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
2.35M
            if (node.has_duplicate_keys.has_value() && *node.has_duplicate_keys) return {};
1519
1520
            // Check if one of the children is already known to have duplicates.
1521
2.35M
            for (auto& sub : subs) {
1522
2.34M
                if (!sub.has_value()) {
1523
0
                    node.has_duplicate_keys = true;
1524
0
                    return {};
1525
0
                }
1526
2.34M
            }
1527
1528
            // Start building the set of keys involved in this node and children.
1529
            // Start by keys in this node directly.
1530
2.35M
            size_t keys_count = node.keys.size();
1531
2.35M
            keyset key_set{node.keys.begin(), node.keys.end(), Comp(ctx)};
1532
2.35M
            if (key_set.size() != keys_count) {
1533
                // It already has duplicates; bail out.
1534
92
                node.has_duplicate_keys = true;
1535
92
                return {};
1536
92
            }
1537
1538
            // Merge the keys from the children into this set.
1539
2.35M
            for (auto& sub : subs) {
1540
2.34M
                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
2.34M
                if (key_set.size() < sub->size()) std::swap(key_set, *sub);
1544
2.34M
                key_set.merge(*sub);
1545
2.34M
                if (key_set.size() != keys_count) {
1546
10
                    node.has_duplicate_keys = true;
1547
10
                    return {};
1548
10
                }
1549
2.34M
            }
1550
1551
2.35M
            node.has_duplicate_keys = false;
1552
2.35M
            return key_set;
1553
2.35M
        };
miniscript_tests.cpp:void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)::operator()(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>) const
Line
Count
Source
1516
23.2k
        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
23.2k
            if (node.has_duplicate_keys.has_value() && *node.has_duplicate_keys) return {};
1519
1520
            // Check if one of the children is already known to have duplicates.
1521
23.2k
            for (auto& sub : subs) {
1522
22.9k
                if (!sub.has_value()) {
1523
0
                    node.has_duplicate_keys = true;
1524
0
                    return {};
1525
0
                }
1526
22.9k
            }
1527
1528
            // Start building the set of keys involved in this node and children.
1529
            // Start by keys in this node directly.
1530
23.2k
            size_t keys_count = node.keys.size();
1531
23.2k
            keyset key_set{node.keys.begin(), node.keys.end(), Comp(ctx)};
1532
23.2k
            if (key_set.size() != keys_count) {
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
23.2k
            for (auto& sub : subs) {
1540
22.9k
                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
22.9k
                if (key_set.size() < sub->size()) std::swap(key_set, *sub);
1544
22.9k
                key_set.merge(*sub);
1545
22.9k
                if (key_set.size() != keys_count) {
1546
6
                    node.has_duplicate_keys = true;
1547
6
                    return {};
1548
6
                }
1549
22.9k
            }
1550
1551
23.2k
            node.has_duplicate_keys = false;
1552
23.2k
            return key_set;
1553
23.2k
        };
descriptor.cpp:void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::'lambda'(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>)::operator()(miniscript::Node<unsigned int> const&, std::span<std::optional<std::set<unsigned int, void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const::Comp, std::allocator<unsigned int>>>, 18446744073709551615ul>) const
Line
Count
Source
1516
996k
        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
996k
            if (node.has_duplicate_keys.has_value() && *node.has_duplicate_keys) return {};
1519
1520
            // Check if one of the children is already known to have duplicates.
1521
996k
            for (auto& sub : subs) {
1522
995k
                if (!sub.has_value()) {
1523
0
                    node.has_duplicate_keys = true;
1524
0
                    return {};
1525
0
                }
1526
995k
            }
1527
1528
            // Start building the set of keys involved in this node and children.
1529
            // Start by keys in this node directly.
1530
996k
            size_t keys_count = node.keys.size();
1531
996k
            keyset key_set{node.keys.begin(), node.keys.end(), Comp(ctx)};
1532
996k
            if (key_set.size() != keys_count) {
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
996k
            for (auto& sub : subs) {
1540
995k
                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
995k
                if (key_set.size() < sub->size()) std::swap(key_set, *sub);
1544
995k
                key_set.merge(*sub);
1545
995k
                if (key_set.size() != keys_count) {
1546
4
                    node.has_duplicate_keys = true;
1547
4
                    return {};
1548
4
                }
1549
995k
            }
1550
1551
996k
            node.has_duplicate_keys = false;
1552
996k
            return key_set;
1553
996k
        };
void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::'lambda'(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>)::operator()(miniscript::Node<XOnlyPubKey> const&, std::span<std::optional<std::set<XOnlyPubKey, void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const::Comp, std::allocator<XOnlyPubKey>>>, 18446744073709551615ul>) const
Line
Count
Source
1516
1.32M
        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
1.32M
            if (node.has_duplicate_keys.has_value() && *node.has_duplicate_keys) return {};
1519
1520
            // Check if one of the children is already known to have duplicates.
1521
1.32M
            for (auto& sub : subs) {
1522
1.32M
                if (!sub.has_value()) {
1523
0
                    node.has_duplicate_keys = true;
1524
0
                    return {};
1525
0
                }
1526
1.32M
            }
1527
1528
            // Start building the set of keys involved in this node and children.
1529
            // Start by keys in this node directly.
1530
1.32M
            size_t keys_count = node.keys.size();
1531
1.32M
            keyset key_set{node.keys.begin(), node.keys.end(), Comp(ctx)};
1532
1.32M
            if (key_set.size() != keys_count) {
1533
                // It already has duplicates; bail out.
1534
92
                node.has_duplicate_keys = true;
1535
92
                return {};
1536
92
            }
1537
1538
            // Merge the keys from the children into this set.
1539
1.32M
            for (auto& sub : subs) {
1540
1.32M
                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
1.32M
                if (key_set.size() < sub->size()) std::swap(key_set, *sub);
1544
1.32M
                key_set.merge(*sub);
1545
1.32M
                if (key_set.size() != keys_count) {
1546
0
                    node.has_duplicate_keys = true;
1547
0
                    return {};
1548
0
                }
1549
1.32M
            }
1550
1551
1.32M
            node.has_duplicate_keys = false;
1552
1.32M
            return key_set;
1553
1.32M
        };
void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::'lambda'(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>)::operator()(miniscript::Node<CPubKey> const&, std::span<std::optional<std::set<CPubKey, void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const::Comp, std::allocator<CPubKey>>>, 18446744073709551615ul>) const
Line
Count
Source
1516
3.28k
        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
3.28k
            if (node.has_duplicate_keys.has_value() && *node.has_duplicate_keys) return {};
1519
1520
            // Check if one of the children is already known to have duplicates.
1521
3.28k
            for (auto& sub : subs) {
1522
3.05k
                if (!sub.has_value()) {
1523
0
                    node.has_duplicate_keys = true;
1524
0
                    return {};
1525
0
                }
1526
3.05k
            }
1527
1528
            // Start building the set of keys involved in this node and children.
1529
            // Start by keys in this node directly.
1530
3.28k
            size_t keys_count = node.keys.size();
1531
3.28k
            keyset key_set{node.keys.begin(), node.keys.end(), Comp(ctx)};
1532
3.28k
            if (key_set.size() != keys_count) {
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
3.28k
            for (auto& sub : subs) {
1540
3.05k
                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
3.05k
                if (key_set.size() < sub->size()) std::swap(key_set, *sub);
1544
3.05k
                key_set.merge(*sub);
1545
3.05k
                if (key_set.size() != keys_count) {
1546
0
                    node.has_duplicate_keys = true;
1547
0
                    return {};
1548
0
                }
1549
3.05k
            }
1550
1551
3.28k
            node.has_duplicate_keys = false;
1552
3.28k
            return key_set;
1553
3.28k
        };
1554
1555
5.86k
        TreeEval<state>(upfn);
1556
5.86k
    }
miniscript_tests.cpp:void miniscript::Node<CPubKey>::DuplicateKeyCheck<(anonymous namespace)::KeyConverter>((anonymous namespace)::KeyConverter const&) const
Line
Count
Source
1501
313
    {
1502
        // We cannot use a lambda here, as lambdas are non assignable, and the set operations
1503
        // below require moving the comparators around.
1504
313
        struct Comp {
1505
313
            const Ctx* ctx_ptr;
1506
313
            Comp(const Ctx& ctx) : ctx_ptr(&ctx) {}
1507
313
            bool operator()(const Key& a, const Key& b) const { return ctx_ptr->KeyCompare(a, b); }
1508
313
        };
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
313
        using keyset = std::set<Key, Comp>;
1514
313
        using state = std::optional<keyset>;
1515
1516
313
        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
313
            if (node.has_duplicate_keys.has_value() && *node.has_duplicate_keys) return {};
1519
1520
            // Check if one of the children is already known to have duplicates.
1521
313
            for (auto& sub : subs) {
1522
313
                if (!sub.has_value()) {
1523
313
                    node.has_duplicate_keys = true;
1524
313
                    return {};
1525
313
                }
1526
313
            }
1527
1528
            // Start building the set of keys involved in this node and children.
1529
            // Start by keys in this node directly.
1530
313
            size_t keys_count = node.keys.size();
1531
313
            keyset key_set{node.keys.begin(), node.keys.end(), Comp(ctx)};
1532
313
            if (key_set.size() != keys_count) {
1533
                // It already has duplicates; bail out.
1534
313
                node.has_duplicate_keys = true;
1535
313
                return {};
1536
313
            }
1537
1538
            // Merge the keys from the children into this set.
1539
313
            for (auto& sub : subs) {
1540
313
                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
313
                if (key_set.size() < sub->size()) std::swap(key_set, *sub);
1544
313
                key_set.merge(*sub);
1545
313
                if (key_set.size() != keys_count) {
1546
313
                    node.has_duplicate_keys = true;
1547
313
                    return {};
1548
313
                }
1549
313
            }
1550
1551
313
            node.has_duplicate_keys = false;
1552
313
            return key_set;
1553
313
        };
1554
1555
313
        TreeEval<state>(upfn);
1556
313
    }
descriptor.cpp:void miniscript::Node<unsigned int>::DuplicateKeyCheck<(anonymous namespace)::KeyParser>((anonymous namespace)::KeyParser const&) const
Line
Count
Source
1501
901
    {
1502
        // We cannot use a lambda here, as lambdas are non assignable, and the set operations
1503
        // below require moving the comparators around.
1504
901
        struct Comp {
1505
901
            const Ctx* ctx_ptr;
1506
901
            Comp(const Ctx& ctx) : ctx_ptr(&ctx) {}
1507
901
            bool operator()(const Key& a, const Key& b) const { return ctx_ptr->KeyCompare(a, b); }
1508
901
        };
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
901
        using keyset = std::set<Key, Comp>;
1514
901
        using state = std::optional<keyset>;
1515
1516
901
        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
901
            if (node.has_duplicate_keys.has_value() && *node.has_duplicate_keys) return {};
1519
1520
            // Check if one of the children is already known to have duplicates.
1521
901
            for (auto& sub : subs) {
1522
901
                if (!sub.has_value()) {
1523
901
                    node.has_duplicate_keys = true;
1524
901
                    return {};
1525
901
                }
1526
901
            }
1527
1528
            // Start building the set of keys involved in this node and children.
1529
            // Start by keys in this node directly.
1530
901
            size_t keys_count = node.keys.size();
1531
901
            keyset key_set{node.keys.begin(), node.keys.end(), Comp(ctx)};
1532
901
            if (key_set.size() != keys_count) {
1533
                // It already has duplicates; bail out.
1534
901
                node.has_duplicate_keys = true;
1535
901
                return {};
1536
901
            }
1537
1538
            // Merge the keys from the children into this set.
1539
901
            for (auto& sub : subs) {
1540
901
                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
901
                if (key_set.size() < sub->size()) std::swap(key_set, *sub);
1544
901
                key_set.merge(*sub);
1545
901
                if (key_set.size() != keys_count) {
1546
901
                    node.has_duplicate_keys = true;
1547
901
                    return {};
1548
901
                }
1549
901
            }
1550
1551
901
            node.has_duplicate_keys = false;
1552
901
            return key_set;
1553
901
        };
1554
1555
901
        TreeEval<state>(upfn);
1556
901
    }
void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const
Line
Count
Source
1501
4.41k
    {
1502
        // We cannot use a lambda here, as lambdas are non assignable, and the set operations
1503
        // below require moving the comparators around.
1504
4.41k
        struct Comp {
1505
4.41k
            const Ctx* ctx_ptr;
1506
4.41k
            Comp(const Ctx& ctx) : ctx_ptr(&ctx) {}
1507
4.41k
            bool operator()(const Key& a, const Key& b) const { return ctx_ptr->KeyCompare(a, b); }
1508
4.41k
        };
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
4.41k
        using keyset = std::set<Key, Comp>;
1514
4.41k
        using state = std::optional<keyset>;
1515
1516
4.41k
        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
4.41k
            if (node.has_duplicate_keys.has_value() && *node.has_duplicate_keys) return {};
1519
1520
            // Check if one of the children is already known to have duplicates.
1521
4.41k
            for (auto& sub : subs) {
1522
4.41k
                if (!sub.has_value()) {
1523
4.41k
                    node.has_duplicate_keys = true;
1524
4.41k
                    return {};
1525
4.41k
                }
1526
4.41k
            }
1527
1528
            // Start building the set of keys involved in this node and children.
1529
            // Start by keys in this node directly.
1530
4.41k
            size_t keys_count = node.keys.size();
1531
4.41k
            keyset key_set{node.keys.begin(), node.keys.end(), Comp(ctx)};
1532
4.41k
            if (key_set.size() != keys_count) {
1533
                // It already has duplicates; bail out.
1534
4.41k
                node.has_duplicate_keys = true;
1535
4.41k
                return {};
1536
4.41k
            }
1537
1538
            // Merge the keys from the children into this set.
1539
4.41k
            for (auto& sub : subs) {
1540
4.41k
                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
4.41k
                if (key_set.size() < sub->size()) std::swap(key_set, *sub);
1544
4.41k
                key_set.merge(*sub);
1545
4.41k
                if (key_set.size() != keys_count) {
1546
4.41k
                    node.has_duplicate_keys = true;
1547
4.41k
                    return {};
1548
4.41k
                }
1549
4.41k
            }
1550
1551
4.41k
            node.has_duplicate_keys = false;
1552
4.41k
            return key_set;
1553
4.41k
        };
1554
1555
4.41k
        TreeEval<state>(upfn);
1556
4.41k
    }
void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const
Line
Count
Source
1501
234
    {
1502
        // We cannot use a lambda here, as lambdas are non assignable, and the set operations
1503
        // below require moving the comparators around.
1504
234
        struct Comp {
1505
234
            const Ctx* ctx_ptr;
1506
234
            Comp(const Ctx& ctx) : ctx_ptr(&ctx) {}
1507
234
            bool operator()(const Key& a, const Key& b) const { return ctx_ptr->KeyCompare(a, b); }
1508
234
        };
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
234
        using keyset = std::set<Key, Comp>;
1514
234
        using state = std::optional<keyset>;
1515
1516
234
        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
234
            if (node.has_duplicate_keys.has_value() && *node.has_duplicate_keys) return {};
1519
1520
            // Check if one of the children is already known to have duplicates.
1521
234
            for (auto& sub : subs) {
1522
234
                if (!sub.has_value()) {
1523
234
                    node.has_duplicate_keys = true;
1524
234
                    return {};
1525
234
                }
1526
234
            }
1527
1528
            // Start building the set of keys involved in this node and children.
1529
            // Start by keys in this node directly.
1530
234
            size_t keys_count = node.keys.size();
1531
234
            keyset key_set{node.keys.begin(), node.keys.end(), Comp(ctx)};
1532
234
            if (key_set.size() != keys_count) {
1533
                // It already has duplicates; bail out.
1534
234
                node.has_duplicate_keys = true;
1535
234
                return {};
1536
234
            }
1537
1538
            // Merge the keys from the children into this set.
1539
234
            for (auto& sub : subs) {
1540
234
                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
234
                if (key_set.size() < sub->size()) std::swap(key_set, *sub);
1544
234
                key_set.merge(*sub);
1545
234
                if (key_set.size() != keys_count) {
1546
234
                    node.has_duplicate_keys = true;
1547
234
                    return {};
1548
234
                }
1549
234
            }
1550
1551
234
            node.has_duplicate_keys = false;
1552
234
            return key_set;
1553
234
        };
1554
1555
234
        TreeEval<state>(upfn);
1556
234
    }
1557
1558
    //! Return the size of the script for this expression (faster than ToScript().size()).
1559
5.12M
    size_t ScriptSize() const { return scriptlen; }
miniscript::Node<CPubKey>::ScriptSize() const
Line
Count
Source
1559
57.9k
    size_t ScriptSize() const { return scriptlen; }
miniscript::Node<unsigned int>::ScriptSize() const
Line
Count
Source
1559
2.40M
    size_t ScriptSize() const { return scriptlen; }
miniscript::Node<XOnlyPubKey>::ScriptSize() const
Line
Count
Source
1559
2.66M
    size_t ScriptSize() const { return scriptlen; }
1560
1561
    //! Return the maximum number of ops needed to satisfy this script non-malleably.
1562
2.25k
    std::optional<uint32_t> GetOps() const {
1563
2.25k
        if (!ops.sat.Valid()) return {};
1564
2.24k
        return ops.count + ops.sat.Value();
1565
2.25k
    }
miniscript::Node<CPubKey>::GetOps() const
Line
Count
Source
1562
1.63k
    std::optional<uint32_t> GetOps() const {
1563
1.63k
        if (!ops.sat.Valid()) return {};
1564
1.62k
        return ops.count + ops.sat.Value();
1565
1.63k
    }
miniscript::Node<unsigned int>::GetOps() const
Line
Count
Source
1562
622
    std::optional<uint32_t> GetOps() const {
1563
622
        if (!ops.sat.Valid()) return {};
1564
619
        return ops.count + ops.sat.Value();
1565
622
    }
1566
1567
    //! Return the number of ops in the script (not counting the dynamic ones that depend on execution).
1568
    uint32_t GetStaticOps() const { return ops.count; }
1569
1570
    //! Check the ops limit of this script against the consensus limit.
1571
6.49k
    bool CheckOpsLimit() const {
1572
6.49k
        if (IsTapscript(m_script_ctx)) return true;
1573
2.13k
        if (const auto ops = GetOps()) return *ops <= MAX_OPS_PER_SCRIPT;
1574
12
        return true;
1575
2.13k
    }
miniscript::Node<CPubKey>::CheckOpsLimit() const
Line
Count
Source
1571
5.48k
    bool CheckOpsLimit() const {
1572
5.48k
        if (IsTapscript(m_script_ctx)) return true;
1573
1.50k
        if (const auto ops = GetOps()) return *ops <= MAX_OPS_PER_SCRIPT;
1574
9
        return true;
1575
1.50k
    }
miniscript::Node<unsigned int>::CheckOpsLimit() const
Line
Count
Source
1571
1.01k
    bool CheckOpsLimit() const {
1572
1.01k
        if (IsTapscript(m_script_ctx)) return true;
1573
622
        if (const auto ops = GetOps()) return *ops <= MAX_OPS_PER_SCRIPT;
1574
3
        return true;
1575
622
    }
1576
1577
    /** Whether this node is of type B, K or W. (That is, anything but V.) */
1578
7.34k
    bool IsBKW() const {
1579
7.34k
        return !((GetType() & "BKW"_mst) == ""_mst);
1580
7.34k
    }
miniscript::Node<CPubKey>::IsBKW() const
Line
Count
Source
1578
5.98k
    bool IsBKW() const {
1579
5.98k
        return !((GetType() & "BKW"_mst) == ""_mst);
1580
5.98k
    }
miniscript::Node<unsigned int>::IsBKW() const
Line
Count
Source
1578
1.35k
    bool IsBKW() const {
1579
1.35k
        return !((GetType() & "BKW"_mst) == ""_mst);
1580
1.35k
    }
1581
1582
    /** Return the maximum number of stack elements needed to satisfy this script non-malleably. */
1583
2.87k
    std::optional<uint32_t> GetStackSize() const {
1584
2.87k
        if (!ss.Sat().Valid()) return {};
1585
2.86k
        return ss.Sat().NetDiff() + static_cast<int32_t>(IsBKW());
1586
2.87k
    }
miniscript::Node<CPubKey>::GetStackSize() const
Line
Count
Source
1583
1.90k
    std::optional<uint32_t> GetStackSize() const {
1584
1.90k
        if (!ss.Sat().Valid()) return {};
1585
1.89k
        return ss.Sat().NetDiff() + static_cast<int32_t>(IsBKW());
1586
1.90k
    }
miniscript::Node<unsigned int>::GetStackSize() const
Line
Count
Source
1583
973
    std::optional<uint32_t> GetStackSize() const {
1584
973
        if (!ss.Sat().Valid()) return {};
1585
969
        return ss.Sat().NetDiff() + static_cast<int32_t>(IsBKW());
1586
973
    }
1587
1588
    //! Return the maximum size of the stack during execution of this script.
1589
4.48k
    std::optional<uint32_t> GetExecStackSize() const {
1590
4.48k
        if (!ss.Sat().Valid()) return {};
1591
4.48k
        return ss.Sat().Exec() + static_cast<int32_t>(IsBKW());
1592
4.48k
    }
miniscript::Node<CPubKey>::GetExecStackSize() const
Line
Count
Source
1589
4.10k
    std::optional<uint32_t> GetExecStackSize() const {
1590
4.10k
        if (!ss.Sat().Valid()) return {};
1591
4.09k
        return ss.Sat().Exec() + static_cast<int32_t>(IsBKW());
1592
4.10k
    }
miniscript::Node<unsigned int>::GetExecStackSize() const
Line
Count
Source
1589
388
    std::optional<uint32_t> GetExecStackSize() const {
1590
388
        if (!ss.Sat().Valid()) return {};
1591
388
        return ss.Sat().Exec() + static_cast<int32_t>(IsBKW());
1592
388
    }
1593
1594
    //! Check the maximum stack size for this script against the policy limit.
1595
6.49k
    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
6.49k
        if (IsTapscript(m_script_ctx)) {
1599
4.36k
            if (const auto exec_ss = GetExecStackSize()) return exec_ss <= MAX_STACK_SIZE;
1600
9
            return true;
1601
4.36k
        }
1602
2.13k
        if (const auto ss = GetStackSize()) return *ss <= MAX_STANDARD_P2WSH_STACK_ITEMS;
1603
12
        return true;
1604
2.13k
    }
miniscript::Node<CPubKey>::CheckStackSize() const
Line
Count
Source
1595
5.48k
    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
5.48k
        if (IsTapscript(m_script_ctx)) {
1599
3.97k
            if (const auto exec_ss = GetExecStackSize()) return exec_ss <= MAX_STACK_SIZE;
1600
9
            return true;
1601
3.97k
        }
1602
1.50k
        if (const auto ss = GetStackSize()) return *ss <= MAX_STANDARD_P2WSH_STACK_ITEMS;
1603
9
        return true;
1604
1.50k
    }
miniscript::Node<unsigned int>::CheckStackSize() const
Line
Count
Source
1595
1.01k
    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
1.01k
        if (IsTapscript(m_script_ctx)) {
1599
388
            if (const auto exec_ss = GetExecStackSize()) return exec_ss <= MAX_STACK_SIZE;
1600
0
            return true;
1601
388
        }
1602
622
        if (const auto ss = GetStackSize()) return *ss <= MAX_STANDARD_P2WSH_STACK_ITEMS;
1603
3
        return true;
1604
622
    }
1605
1606
    //! Whether no satisfaction exists for this node.
1607
193
    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
554
    std::optional<uint32_t> GetWitnessSize() const {
1612
554
        if (!ws.sat.Valid()) return {};
1613
554
        return ws.sat.Value();
1614
554
    }
miniscript::Node<CPubKey>::GetWitnessSize() const
Line
Count
Source
1611
378
    std::optional<uint32_t> GetWitnessSize() const {
1612
378
        if (!ws.sat.Valid()) return {};
1613
378
        return ws.sat.Value();
1614
378
    }
miniscript::Node<unsigned int>::GetWitnessSize() const
Line
Count
Source
1611
176
    std::optional<uint32_t> GetWitnessSize() const {
1612
176
        if (!ws.sat.Valid()) return {};
1613
176
        return ws.sat.Value();
1614
176
    }
1615
1616
    //! Return the expression type.
1617
52.4M
    Type GetType() const { return typ; }
miniscript::Node<CPubKey>::GetType() const
Line
Count
Source
1617
24.3M
    Type GetType() const { return typ; }
miniscript::Node<unsigned int>::GetType() const
Line
Count
Source
1617
4.13M
    Type GetType() const { return typ; }
miniscript::Node<XOnlyPubKey>::GetType() const
Line
Count
Source
1617
23.9M
    Type GetType() const { return typ; }
1618
1619
    //! Return the script context for this node.
1620
1.69k
    MiniscriptContext GetMsCtx() const { return m_script_ctx; }
1621
1622
    //! Find an insane subnode which has no insane children. Nullptr if there is none.
1623
17
    const Node* FindInsaneSub() const {
1624
126
        return TreeEval<const Node*>([](const Node& node, std::span<const Node*> subs) -> const Node* {
1625
126
            for (auto& sub: subs) if (sub) return sub;
1626
115
            if (!node.IsSaneSubexpression()) return &node;
1627
102
            return nullptr;
1628
115
        });
miniscript::Node<CPubKey>::FindInsaneSub() const::'lambda'(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>)::operator()(miniscript::Node<CPubKey> const&, std::span<miniscript::Node<CPubKey> const*, 18446744073709551615ul>) const
Line
Count
Source
1624
7
        return TreeEval<const Node*>([](const Node& node, std::span<const Node*> subs) -> const Node* {
1625
7
            for (auto& sub: subs) if (sub) return sub;
1626
6
            if (!node.IsSaneSubexpression()) return &node;
1627
5
            return nullptr;
1628
6
        });
miniscript::Node<unsigned int>::FindInsaneSub() const::'lambda'(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>)::operator()(miniscript::Node<unsigned int> const&, std::span<miniscript::Node<unsigned int> const*, 18446744073709551615ul>) const
Line
Count
Source
1624
119
        return TreeEval<const Node*>([](const Node& node, std::span<const Node*> subs) -> const Node* {
1625
119
            for (auto& sub: subs) if (sub) return sub;
1626
109
            if (!node.IsSaneSubexpression()) return &node;
1627
97
            return nullptr;
1628
109
        });
1629
17
    }
miniscript::Node<CPubKey>::FindInsaneSub() const
Line
Count
Source
1623
1
    const Node* FindInsaneSub() const {
1624
1
        return TreeEval<const Node*>([](const Node& node, std::span<const Node*> subs) -> const Node* {
1625
1
            for (auto& sub: subs) if (sub) return sub;
1626
1
            if (!node.IsSaneSubexpression()) return &node;
1627
1
            return nullptr;
1628
1
        });
1629
1
    }
miniscript::Node<unsigned int>::FindInsaneSub() const
Line
Count
Source
1623
16
    const Node* FindInsaneSub() const {
1624
16
        return TreeEval<const Node*>([](const Node& node, std::span<const Node*> subs) -> const Node* {
1625
16
            for (auto& sub: subs) if (sub) return sub;
1626
16
            if (!node.IsSaneSubexpression()) return &node;
1627
16
            return nullptr;
1628
16
        });
1629
16
    }
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
375
    {
1636
        // TreeEval() doesn't support bool as NodeType, so use int instead.
1637
25.4k
        return TreeEval<int>([&fn](const Node& node, std::span<int> subs) -> bool {
1638
25.4k
            switch (node.fragment) {
1639
249
                case Fragment::JUST_0:
1640
249
                    return false;
1641
231
                case Fragment::JUST_1:
1642
231
                    return true;
1643
1.36k
                case Fragment::PK_K:
1644
1.44k
                case Fragment::PK_H:
1645
1.47k
                case Fragment::MULTI:
1646
1.48k
                case Fragment::MULTI_A:
1647
1.67k
                case Fragment::AFTER:
1648
7.79k
                case Fragment::OLDER:
1649
7.83k
                case Fragment::HASH256:
1650
7.85k
                case Fragment::HASH160:
1651
7.91k
                case Fragment::SHA256:
1652
7.93k
                case Fragment::RIPEMD160:
1653
7.93k
                    return bool{fn(node)};
1654
87
                case Fragment::ANDOR:
1655
87
                    return (subs[0] && subs[1]) || subs[2];
1656
198
                case Fragment::AND_V:
1657
7.45k
                case Fragment::AND_B:
1658
7.45k
                    return subs[0] && subs[1];
1659
24
                case Fragment::OR_B:
1660
42
                case Fragment::OR_C:
1661
87
                case Fragment::OR_D:
1662
324
                case Fragment::OR_I:
1663
324
                    return subs[0] || subs[1];
1664
48
                case Fragment::THRESH:
1665
48
                    return static_cast<uint32_t>(std::count(subs.begin(), subs.end(), true)) >= node.k;
1666
9.08k
                default: // wrappers
1667
9.08k
                    assert(subs.size() >= 1);
1668
9.08k
                    CHECK_NONFATAL(subs.size() == 1);
1669
9.08k
                    return subs[0];
1670
25.4k
            }
1671
25.4k
        });
1672
375
    }
1673
1674
    //! Check whether this node is valid at all.
1675
2.04M
    bool IsValid() const {
1676
2.04M
        if (GetType() == ""_mst) return false;
1677
2.04M
        return ScriptSize() <= internal::MaxScriptSize(m_script_ctx);
1678
2.04M
    }
miniscript::Node<CPubKey>::IsValid() const
Line
Count
Source
1675
31.4k
    bool IsValid() const {
1676
31.4k
        if (GetType() == ""_mst) return false;
1677
31.3k
        return ScriptSize() <= internal::MaxScriptSize(m_script_ctx);
1678
31.4k
    }
miniscript::Node<unsigned int>::IsValid() const
Line
Count
Source
1675
675k
    bool IsValid() const {
1676
675k
        if (GetType() == ""_mst) return false;
1677
675k
        return ScriptSize() <= internal::MaxScriptSize(m_script_ctx);
1678
675k
    }
miniscript::Node<XOnlyPubKey>::IsValid() const
Line
Count
Source
1675
1.33M
    bool IsValid() const {
1676
1.33M
        if (GetType() == ""_mst) return false;
1677
1.33M
        return ScriptSize() <= internal::MaxScriptSize(m_script_ctx);
1678
1.33M
    }
1679
1680
    //! Check whether this node is valid as a script on its own.
1681
11.7k
    bool IsValidTopLevel() const { return IsValid() && GetType() << "B"_mst; }
miniscript::Node<CPubKey>::IsValidTopLevel() const
Line
Count
Source
1681
5.68k
    bool IsValidTopLevel() const { return IsValid() && GetType() << "B"_mst; }
miniscript::Node<unsigned int>::IsValidTopLevel() const
Line
Count
Source
1681
1.59k
    bool IsValidTopLevel() const { return IsValid() && GetType() << "B"_mst; }
miniscript::Node<XOnlyPubKey>::IsValidTopLevel() const
Line
Count
Source
1681
4.41k
    bool IsValidTopLevel() const { return IsValid() && GetType() << "B"_mst; }
1682
1683
    //! Check whether this script can always be satisfied in a non-malleable way.
1684
6.33k
    bool IsNonMalleable() const { return GetType() << "m"_mst; }
miniscript::Node<CPubKey>::IsNonMalleable() const
Line
Count
Source
1684
5.31k
    bool IsNonMalleable() const { return GetType() << "m"_mst; }
miniscript::Node<unsigned int>::IsNonMalleable() const
Line
Count
Source
1684
1.02k
    bool IsNonMalleable() const { return GetType() << "m"_mst; }
1685
1686
    //! Check whether this script always needs a signature.
1687
4.88k
    bool NeedsSignature() const { return GetType() << "s"_mst; }
miniscript::Node<CPubKey>::NeedsSignature() const
Line
Count
Source
1687
3.98k
    bool NeedsSignature() const { return GetType() << "s"_mst; }
miniscript::Node<unsigned int>::NeedsSignature() const
Line
Count
Source
1687
896
    bool NeedsSignature() const { return GetType() << "s"_mst; }
1688
1689
    //! Check whether there is no satisfaction path that contains both timelocks and heightlocks
1690
5.16k
    bool CheckTimeLocksMix() const { return GetType() << "k"_mst; }
miniscript::Node<CPubKey>::CheckTimeLocksMix() const
Line
Count
Source
1690
4.15k
    bool CheckTimeLocksMix() const { return GetType() << "k"_mst; }
miniscript::Node<unsigned int>::CheckTimeLocksMix() const
Line
Count
Source
1690
1.01k
    bool CheckTimeLocksMix() const { return GetType() << "k"_mst; }
1691
1692
    //! Check whether there is no duplicate key across this fragment and all its sub-fragments.
1693
4.90k
    bool CheckDuplicateKey() const { return has_duplicate_keys && !*has_duplicate_keys; }
miniscript::Node<CPubKey>::CheckDuplicateKey() const
Line
Count
Source
1693
3.89k
    bool CheckDuplicateKey() const { return has_duplicate_keys && !*has_duplicate_keys; }
miniscript::Node<unsigned int>::CheckDuplicateKey() const
Line
Count
Source
1693
1.00k
    bool CheckDuplicateKey() const { return has_duplicate_keys && !*has_duplicate_keys; }
1694
1695
    //! Whether successful non-malleable satisfactions are guaranteed to be valid.
1696
6.49k
    bool ValidSatisfactions() const { return IsValid() && CheckOpsLimit() && CheckStackSize(); }
miniscript::Node<CPubKey>::ValidSatisfactions() const
Line
Count
Source
1696
5.48k
    bool ValidSatisfactions() const { return IsValid() && CheckOpsLimit() && CheckStackSize(); }
miniscript::Node<unsigned int>::ValidSatisfactions() const
Line
Count
Source
1696
1.01k
    bool ValidSatisfactions() const { return IsValid() && CheckOpsLimit() && CheckStackSize(); }
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
6.22k
    bool IsSaneSubexpression() const { return ValidSatisfactions() && IsNonMalleable() && CheckTimeLocksMix() && CheckDuplicateKey(); }
miniscript::Node<CPubKey>::IsSaneSubexpression() const
Line
Count
Source
1699
5.21k
    bool IsSaneSubexpression() const { return ValidSatisfactions() && IsNonMalleable() && CheckTimeLocksMix() && CheckDuplicateKey(); }
miniscript::Node<unsigned int>::IsSaneSubexpression() const
Line
Count
Source
1699
1.01k
    bool IsSaneSubexpression() const { return ValidSatisfactions() && IsNonMalleable() && CheckTimeLocksMix() && CheckDuplicateKey(); }
1700
1701
    //! Check whether this node is safe as a script on its own.
1702
6.11k
    bool IsSane() const { return IsValidTopLevel() && IsSaneSubexpression() && NeedsSignature(); }
miniscript::Node<CPubKey>::IsSane() const
Line
Count
Source
1702
5.20k
    bool IsSane() const { return IsValidTopLevel() && IsSaneSubexpression() && NeedsSignature(); }
miniscript::Node<unsigned int>::IsSane() const
Line
Count
Source
1702
909
    bool IsSane() const { return IsValidTopLevel() && IsSaneSubexpression() && NeedsSignature(); }
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
9.47k
    Availability Satisfy(const Ctx& ctx, std::vector<std::vector<unsigned char>>& stack, bool nonmalleable = true) const {
1710
9.47k
        auto ret = ProduceInput(ctx);
1711
9.47k
        if (nonmalleable && (ret.sat.malleable || !ret.sat.has_sig)) return Availability::NO;
1712
3.48k
        stack = std::move(ret.sat.stack);
1713
3.48k
        return ret.sat.available;
1714
9.47k
    }
miniscript_tests.cpp:miniscript::Availability miniscript::Node<CPubKey>::Satisfy<(anonymous namespace)::Satisfier>((anonymous namespace)::Satisfier const&, std::vector<std::vector<unsigned char, std::allocator<unsigned char>>, std::allocator<std::vector<unsigned char, std::allocator<unsigned char>>>>&, bool) const
Line
Count
Source
1709
4.82k
    Availability Satisfy(const Ctx& ctx, std::vector<std::vector<unsigned char>>& stack, bool nonmalleable = true) const {
1710
4.82k
        auto ret = ProduceInput(ctx);
1711
4.82k
        if (nonmalleable && (ret.sat.malleable || !ret.sat.has_sig)) return Availability::NO;
1712
2.68k
        stack = std::move(ret.sat.stack);
1713
2.68k
        return ret.sat.available;
1714
4.82k
    }
miniscript::Availability miniscript::Node<XOnlyPubKey>::Satisfy<TapSatisfier>(TapSatisfier const&, std::vector<std::vector<unsigned char, std::allocator<unsigned char>>, std::allocator<std::vector<unsigned char, std::allocator<unsigned char>>>>&, bool) const
Line
Count
Source
1709
4.41k
    Availability Satisfy(const Ctx& ctx, std::vector<std::vector<unsigned char>>& stack, bool nonmalleable = true) const {
1710
4.41k
        auto ret = ProduceInput(ctx);
1711
4.41k
        if (nonmalleable && (ret.sat.malleable || !ret.sat.has_sig)) return Availability::NO;
1712
705
        stack = std::move(ret.sat.stack);
1713
705
        return ret.sat.available;
1714
4.41k
    }
miniscript::Availability miniscript::Node<CPubKey>::Satisfy<WshSatisfier>(WshSatisfier const&, std::vector<std::vector<unsigned char, std::allocator<unsigned char>>, std::allocator<std::vector<unsigned char, std::allocator<unsigned char>>>>&, bool) const
Line
Count
Source
1709
234
    Availability Satisfy(const Ctx& ctx, std::vector<std::vector<unsigned char>>& stack, bool nonmalleable = true) const {
1710
234
        auto ret = ProduceInput(ctx);
1711
234
        if (nonmalleable && (ret.sat.malleable || !ret.sat.has_sig)) return Availability::NO;
1712
96
        stack = std::move(ret.sat.stack);
1713
96
        return ret.sat.available;
1714
234
    }
1715
1716
    //! Equality testing.
1717
    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
        : 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
359
        : fragment(nt), k(val), data(std::move(arg)), m_script_ctx{script_ctx}, ops(CalcOps()), ss(CalcStackSize()), ws(CalcWitnessSize()), typ(CalcType()), scriptlen(CalcScriptLen()) {}
miniscript::Node<CPubKey>::Node(miniscript::internal::NoDupCheck, miniscript::MiniscriptContext, miniscript::Fragment, std::vector<unsigned char, std::allocator<unsigned char>>, unsigned int)
Line
Count
Source
1723
150
        : fragment(nt), k(val), data(std::move(arg)), m_script_ctx{script_ctx}, ops(CalcOps()), ss(CalcStackSize()), ws(CalcWitnessSize()), typ(CalcType()), scriptlen(CalcScriptLen()) {}
miniscript::Node<unsigned int>::Node(miniscript::internal::NoDupCheck, miniscript::MiniscriptContext, miniscript::Fragment, std::vector<unsigned char, std::allocator<unsigned char>>, unsigned int)
Line
Count
Source
1723
197
        : fragment(nt), k(val), data(std::move(arg)), m_script_ctx{script_ctx}, ops(CalcOps()), ss(CalcStackSize()), ws(CalcWitnessSize()), typ(CalcType()), scriptlen(CalcScriptLen()) {}
miniscript::Node<XOnlyPubKey>::Node(miniscript::internal::NoDupCheck, miniscript::MiniscriptContext, miniscript::Fragment, std::vector<unsigned char, std::allocator<unsigned char>>, unsigned int)
Line
Count
Source
1723
12
        : fragment(nt), k(val), data(std::move(arg)), m_script_ctx{script_ctx}, ops(CalcOps()), ss(CalcStackSize()), ws(CalcWitnessSize()), typ(CalcType()), scriptlen(CalcScriptLen()) {}
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
8.62k
        : fragment(nt), k(val), keys(std::move(key)), m_script_ctx{script_ctx}, ops(CalcOps()), ss(CalcStackSize()), ws(CalcWitnessSize()), typ(CalcType()), scriptlen(CalcScriptLen()) {}
miniscript::Node<CPubKey>::Node(miniscript::internal::NoDupCheck, miniscript::MiniscriptContext, miniscript::Fragment, std::vector<CPubKey, std::allocator<CPubKey>>, unsigned int)
Line
Count
Source
1727
1.89k
        : fragment(nt), k(val), keys(std::move(key)), m_script_ctx{script_ctx}, ops(CalcOps()), ss(CalcStackSize()), ws(CalcWitnessSize()), typ(CalcType()), scriptlen(CalcScriptLen()) {}
miniscript::Node<unsigned int>::Node(miniscript::internal::NoDupCheck, miniscript::MiniscriptContext, miniscript::Fragment, std::vector<unsigned int, std::allocator<unsigned int>>, unsigned int)
Line
Count
Source
1727
2.00k
        : fragment(nt), k(val), keys(std::move(key)), m_script_ctx{script_ctx}, ops(CalcOps()), ss(CalcStackSize()), ws(CalcWitnessSize()), typ(CalcType()), scriptlen(CalcScriptLen()) {}
miniscript::Node<XOnlyPubKey>::Node(miniscript::internal::NoDupCheck, miniscript::MiniscriptContext, miniscript::Fragment, std::vector<XOnlyPubKey, std::allocator<XOnlyPubKey>>, unsigned int)
Line
Count
Source
1727
4.72k
        : fragment(nt), k(val), keys(std::move(key)), m_script_ctx{script_ctx}, ops(CalcOps()), ss(CalcStackSize()), ws(CalcWitnessSize()), typ(CalcType()), scriptlen(CalcScriptLen()) {}
1728
    Node(internal::NoDupCheck, MiniscriptContext script_ctx, enum Fragment nt, std::vector<Node> sub, uint32_t val = 0)
1729
2.53M
        : fragment(nt), k(val), subs(std::move(sub)), m_script_ctx{script_ctx}, ops(CalcOps()), ss(CalcStackSize()), ws(CalcWitnessSize()), typ(CalcType()), scriptlen(CalcScriptLen()) {}
miniscript::Node<CPubKey>::Node(miniscript::internal::NoDupCheck, miniscript::MiniscriptContext, miniscript::Fragment, std::vector<miniscript::Node<CPubKey>, std::allocator<miniscript::Node<CPubKey>>>, unsigned int)
Line
Count
Source
1729
17.8k
        : fragment(nt), k(val), subs(std::move(sub)), m_script_ctx{script_ctx}, ops(CalcOps()), ss(CalcStackSize()), ws(CalcWitnessSize()), typ(CalcType()), scriptlen(CalcScriptLen()) {}
miniscript::Node<unsigned int>::Node(miniscript::internal::NoDupCheck, miniscript::MiniscriptContext, miniscript::Fragment, std::vector<miniscript::Node<unsigned int>, std::allocator<miniscript::Node<unsigned int>>>, unsigned int)
Line
Count
Source
1729
1.19M
        : fragment(nt), k(val), subs(std::move(sub)), m_script_ctx{script_ctx}, ops(CalcOps()), ss(CalcStackSize()), ws(CalcWitnessSize()), typ(CalcType()), scriptlen(CalcScriptLen()) {}
miniscript::Node<XOnlyPubKey>::Node(miniscript::internal::NoDupCheck, miniscript::MiniscriptContext, miniscript::Fragment, std::vector<miniscript::Node<XOnlyPubKey>, std::allocator<miniscript::Node<XOnlyPubKey>>>, unsigned int)
Line
Count
Source
1729
1.32M
        : fragment(nt), k(val), subs(std::move(sub)), m_script_ctx{script_ctx}, ops(CalcOps()), ss(CalcStackSize()), ws(CalcWitnessSize()), typ(CalcType()), scriptlen(CalcScriptLen()) {}
1730
    Node(internal::NoDupCheck, MiniscriptContext script_ctx, enum Fragment nt, uint32_t val = 0)
1731
10.2k
        : fragment(nt), k(val), m_script_ctx{script_ctx}, ops(CalcOps()), ss(CalcStackSize()), ws(CalcWitnessSize()), typ(CalcType()), scriptlen(CalcScriptLen()) {}
miniscript::Node<CPubKey>::Node(miniscript::internal::NoDupCheck, miniscript::MiniscriptContext, miniscript::Fragment, unsigned int)
Line
Count
Source
1731
8.65k
        : fragment(nt), k(val), m_script_ctx{script_ctx}, ops(CalcOps()), ss(CalcStackSize()), ws(CalcWitnessSize()), typ(CalcType()), scriptlen(CalcScriptLen()) {}
miniscript::Node<unsigned int>::Node(miniscript::internal::NoDupCheck, miniscript::MiniscriptContext, miniscript::Fragment, unsigned int)
Line
Count
Source
1731
791
        : fragment(nt), k(val), m_script_ctx{script_ctx}, ops(CalcOps()), ss(CalcStackSize()), ws(CalcWitnessSize()), typ(CalcType()), scriptlen(CalcScriptLen()) {}
miniscript::Node<XOnlyPubKey>::Node(miniscript::internal::NoDupCheck, miniscript::MiniscriptContext, miniscript::Fragment, unsigned int)
Line
Count
Source
1731
796
        : fragment(nt), k(val), m_script_ctx{script_ctx}, ops(CalcOps()), ss(CalcStackSize()), ws(CalcWitnessSize()), typ(CalcType()), scriptlen(CalcScriptLen()) {}
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
4.18M
    Node(Node&&) noexcept = default;
miniscript::Node<CPubKey>::Node(miniscript::Node<CPubKey>&&)
Line
Count
Source
1752
45.3k
    Node(Node&&) noexcept = default;
miniscript::Node<unsigned int>::Node(miniscript::Node<unsigned int>&&)
Line
Count
Source
1752
2.79M
    Node(Node&&) noexcept = default;
miniscript::Node<XOnlyPubKey>::Node(miniscript::Node<XOnlyPubKey>&&)
Line
Count
Source
1752
1.34M
    Node(Node&&) noexcept = default;
1753
2.53M
    Node& operator=(Node&&) noexcept = default;
miniscript::Node<unsigned int>::operator=(miniscript::Node<unsigned int>&&)
Line
Count
Source
1753
1.19M
    Node& operator=(Node&&) noexcept = default;
miniscript::Node<CPubKey>::operator=(miniscript::Node<CPubKey>&&)
Line
Count
Source
1753
17.0k
    Node& operator=(Node&&) noexcept = default;
miniscript::Node<XOnlyPubKey>::operator=(miniscript::Node<XOnlyPubKey>&&)
Line
Count
Source
1753
1.32M
    Node& operator=(Node&&) noexcept = default;
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
1.21k
{
1818
1.21k
    std::span<const char> expr = script::Expr(in);
1819
1.21k
    if (!script::Func(func, expr)) return {};
1820
1.21k
    return ctx.FromString(expr);
1821
1.21k
}
miniscript_tests.cpp:std::optional<CPubKey> miniscript::internal::ParseKey<CPubKey, (anonymous namespace)::KeyConverter>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::span<char const, 18446744073709551615ul>&, (anonymous namespace)::KeyConverter const&)
Line
Count
Source
1817
794
{
1818
794
    std::span<const char> expr = script::Expr(in);
1819
794
    if (!script::Func(func, expr)) return {};
1820
794
    return ctx.FromString(expr);
1821
794
}
descriptor.cpp:std::optional<unsigned int> miniscript::internal::ParseKey<unsigned int, (anonymous namespace)::KeyParser>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::span<char const, 18446744073709551615ul>&, (anonymous namespace)::KeyParser const&)
Line
Count
Source
1817
421
{
1818
421
    std::span<const char> expr = script::Expr(in);
1819
421
    if (!script::Func(func, expr)) return {};
1820
419
    return ctx.FromString(expr);
1821
421
}
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
89
{
1826
89
    std::span<const char> expr = script::Expr(in);
1827
89
    if (!script::Func(func, expr)) return {};
1828
89
    std::string val = std::string(expr.begin(), expr.end());
1829
89
    if (!IsHex(val)) return {};
1830
89
    auto hash = ParseHex(val);
1831
89
    if (hash.size() != expected_size) return {};
1832
89
    return hash;
1833
89
}
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
9.96k
{
1839
9.96k
    Node<Key> child{std::move(constructed.back())};
1840
9.96k
    constructed.pop_back();
1841
9.96k
    if (reverse) {
1842
5.08k
        constructed.back() = Node<Key>{internal::NoDupCheck{}, script_ctx, nt, Vector(std::move(child), std::move(constructed.back()))};
1843
5.08k
    } else {
1844
4.88k
        constructed.back() = Node<Key>{internal::NoDupCheck{}, script_ctx, nt, Vector(std::move(constructed.back()), std::move(child))};
1845
4.88k
    }
1846
9.96k
}
void miniscript::internal::BuildBack<CPubKey>(miniscript::MiniscriptContext, miniscript::Fragment, std::vector<miniscript::Node<CPubKey>, std::allocator<miniscript::Node<CPubKey>>>&, bool)
Line
Count
Source
1838
7.58k
{
1839
7.58k
    Node<Key> child{std::move(constructed.back())};
1840
7.58k
    constructed.pop_back();
1841
7.58k
    if (reverse) {
1842
2.98k
        constructed.back() = Node<Key>{internal::NoDupCheck{}, script_ctx, nt, Vector(std::move(child), std::move(constructed.back()))};
1843
4.59k
    } else {
1844
4.59k
        constructed.back() = Node<Key>{internal::NoDupCheck{}, script_ctx, nt, Vector(std::move(constructed.back()), std::move(child))};
1845
4.59k
    }
1846
7.58k
}
void miniscript::internal::BuildBack<unsigned int>(miniscript::MiniscriptContext, miniscript::Fragment, std::vector<miniscript::Node<unsigned int>, std::allocator<miniscript::Node<unsigned int>>>&, bool)
Line
Count
Source
1838
1.32k
{
1839
1.32k
    Node<Key> child{std::move(constructed.back())};
1840
1.32k
    constructed.pop_back();
1841
1.32k
    if (reverse) {
1842
1.03k
        constructed.back() = Node<Key>{internal::NoDupCheck{}, script_ctx, nt, Vector(std::move(child), std::move(constructed.back()))};
1843
1.03k
    } else {
1844
290
        constructed.back() = Node<Key>{internal::NoDupCheck{}, script_ctx, nt, Vector(std::move(constructed.back()), std::move(child))};
1845
290
    }
1846
1.32k
}
void miniscript::internal::BuildBack<XOnlyPubKey>(miniscript::MiniscriptContext, miniscript::Fragment, std::vector<miniscript::Node<XOnlyPubKey>, std::allocator<miniscript::Node<XOnlyPubKey>>>&, bool)
Line
Count
Source
1838
1.06k
{
1839
1.06k
    Node<Key> child{std::move(constructed.back())};
1840
1.06k
    constructed.pop_back();
1841
1.06k
    if (reverse) {
1842
1.06k
        constructed.back() = Node<Key>{internal::NoDupCheck{}, script_ctx, nt, Vector(std::move(child), std::move(constructed.back()))};
1843
1.06k
    } else {
1844
0
        constructed.back() = Node<Key>{internal::NoDupCheck{}, script_ctx, nt, Vector(std::move(constructed.back()), std::move(child))};
1845
0
    }
1846
1.06k
}
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
796
{
1856
796
    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
796
    size_t script_size{1};
1869
796
    size_t max_size{internal::MaxScriptSize(ctx.MsContext())};
1870
1871
    // The two integers are used to hold state for thresh()
1872
796
    std::vector<std::tuple<ParseContext, int64_t, int64_t>> to_parse;
1873
796
    std::vector<Node<Key>> constructed;
1874
1875
796
    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
796
    const auto parse_multi_exp = [&](std::span<const char>& in, const bool is_multi_a) -> bool {
1879
59
        const auto max_keys{is_multi_a ? MAX_PUBKEYS_PER_MULTI_A : MAX_PUBKEYS_PER_MULTISIG};
1880
59
        const auto required_ctx{is_multi_a ? MiniscriptContext::TAPSCRIPT : MiniscriptContext::P2WSH};
1881
59
        if (ctx.MsContext() != required_ctx) return false;
1882
        // Get threshold
1883
47
        int next_comma = FindNextChar(in, ',');
1884
47
        if (next_comma < 1) return false;
1885
47
        const auto k_to_integral{ToIntegral<int64_t>(std::string_view(in.data(), next_comma))};
1886
47
        if (!k_to_integral.has_value()) return false;
1887
46
        const int64_t k{k_to_integral.value()};
1888
46
        in = in.subspan(next_comma + 1);
1889
        // Get keys. It is compatible for both compressed and x-only keys.
1890
46
        std::vector<Key> keys;
1891
175
        while (next_comma != -1) {
1892
129
            next_comma = FindNextChar(in, ',');
1893
129
            int key_length = (next_comma == -1) ? FindNextChar(in, ')') : next_comma;
1894
129
            if (key_length < 1) return false;
1895
129
            std::span<const char> sp{in.begin(), in.begin() + key_length};
1896
129
            auto key = ctx.FromString(sp);
1897
129
            if (!key) return false;
1898
129
            keys.push_back(std::move(*key));
1899
129
            in = in.subspan(key_length + 1);
1900
129
        }
1901
46
        if (keys.size() < 1 || keys.size() > max_keys) return false;
1902
46
        if (k < 1 || k > (int64_t)keys.size()) return false;
1903
46
        if (is_multi_a) {
1904
            // (push + xonly-key + CHECKSIG[ADD]) * n + k + OP_NUMEQUAL(VERIFY), minus one.
1905
16
            script_size += (1 + 32 + 1) * keys.size() + BuildScript(k).size();
1906
16
            constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::MULTI_A, std::move(keys), k);
1907
30
        } else {
1908
30
            script_size += 2 + (keys.size() > 16) + (k > 16) + 34 * keys.size();
1909
30
            constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::MULTI, std::move(keys), k);
1910
30
        }
1911
46
        return true;
1912
46
    };
miniscript_tests.cpp:std::optional<miniscript::Node<CPubKey>> miniscript::internal::Parse<CPubKey, (anonymous namespace)::KeyConverter>(std::span<char const, 18446744073709551615ul>, (anonymous namespace)::KeyConverter const&)::'lambda'(std::span<char const, 18446744073709551615ul>&, bool)::operator()(std::span<char const, 18446744073709551615ul>&, bool) const
Line
Count
Source
1878
27
    const auto parse_multi_exp = [&](std::span<const char>& in, const bool is_multi_a) -> bool {
1879
27
        const auto max_keys{is_multi_a ? MAX_PUBKEYS_PER_MULTI_A : MAX_PUBKEYS_PER_MULTISIG};
1880
27
        const auto required_ctx{is_multi_a ? MiniscriptContext::TAPSCRIPT : MiniscriptContext::P2WSH};
1881
27
        if (ctx.MsContext() != required_ctx) return false;
1882
        // Get threshold
1883
16
        int next_comma = FindNextChar(in, ',');
1884
16
        if (next_comma < 1) return false;
1885
16
        const auto k_to_integral{ToIntegral<int64_t>(std::string_view(in.data(), next_comma))};
1886
16
        if (!k_to_integral.has_value()) return false;
1887
15
        const int64_t k{k_to_integral.value()};
1888
15
        in = in.subspan(next_comma + 1);
1889
        // Get keys. It is compatible for both compressed and x-only keys.
1890
15
        std::vector<Key> keys;
1891
64
        while (next_comma != -1) {
1892
49
            next_comma = FindNextChar(in, ',');
1893
49
            int key_length = (next_comma == -1) ? FindNextChar(in, ')') : next_comma;
1894
49
            if (key_length < 1) return false;
1895
49
            std::span<const char> sp{in.begin(), in.begin() + key_length};
1896
49
            auto key = ctx.FromString(sp);
1897
49
            if (!key) return false;
1898
49
            keys.push_back(std::move(*key));
1899
49
            in = in.subspan(key_length + 1);
1900
49
        }
1901
15
        if (keys.size() < 1 || keys.size() > max_keys) return false;
1902
15
        if (k < 1 || k > (int64_t)keys.size()) return false;
1903
15
        if (is_multi_a) {
1904
            // (push + xonly-key + CHECKSIG[ADD]) * n + k + OP_NUMEQUAL(VERIFY), minus one.
1905
2
            script_size += (1 + 32 + 1) * keys.size() + BuildScript(k).size();
1906
2
            constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::MULTI_A, std::move(keys), k);
1907
13
        } else {
1908
13
            script_size += 2 + (keys.size() > 16) + (k > 16) + 34 * keys.size();
1909
13
            constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::MULTI, std::move(keys), k);
1910
13
        }
1911
15
        return true;
1912
15
    };
descriptor.cpp:std::optional<miniscript::Node<unsigned int>> miniscript::internal::Parse<unsigned int, (anonymous namespace)::KeyParser>(std::span<char const, 18446744073709551615ul>, (anonymous namespace)::KeyParser const&)::'lambda'(std::span<char const, 18446744073709551615ul>&, bool)::operator()(std::span<char const, 18446744073709551615ul>&, bool) const
Line
Count
Source
1878
32
    const auto parse_multi_exp = [&](std::span<const char>& in, const bool is_multi_a) -> bool {
1879
32
        const auto max_keys{is_multi_a ? MAX_PUBKEYS_PER_MULTI_A : MAX_PUBKEYS_PER_MULTISIG};
1880
32
        const auto required_ctx{is_multi_a ? MiniscriptContext::TAPSCRIPT : MiniscriptContext::P2WSH};
1881
32
        if (ctx.MsContext() != required_ctx) return false;
1882
        // Get threshold
1883
31
        int next_comma = FindNextChar(in, ',');
1884
31
        if (next_comma < 1) return false;
1885
31
        const auto k_to_integral{ToIntegral<int64_t>(std::string_view(in.data(), next_comma))};
1886
31
        if (!k_to_integral.has_value()) return false;
1887
31
        const int64_t k{k_to_integral.value()};
1888
31
        in = in.subspan(next_comma + 1);
1889
        // Get keys. It is compatible for both compressed and x-only keys.
1890
31
        std::vector<Key> keys;
1891
111
        while (next_comma != -1) {
1892
80
            next_comma = FindNextChar(in, ',');
1893
80
            int key_length = (next_comma == -1) ? FindNextChar(in, ')') : next_comma;
1894
80
            if (key_length < 1) return false;
1895
80
            std::span<const char> sp{in.begin(), in.begin() + key_length};
1896
80
            auto key = ctx.FromString(sp);
1897
80
            if (!key) return false;
1898
80
            keys.push_back(std::move(*key));
1899
80
            in = in.subspan(key_length + 1);
1900
80
        }
1901
31
        if (keys.size() < 1 || keys.size() > max_keys) return false;
1902
31
        if (k < 1 || k > (int64_t)keys.size()) return false;
1903
31
        if (is_multi_a) {
1904
            // (push + xonly-key + CHECKSIG[ADD]) * n + k + OP_NUMEQUAL(VERIFY), minus one.
1905
14
            script_size += (1 + 32 + 1) * keys.size() + BuildScript(k).size();
1906
14
            constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::MULTI_A, std::move(keys), k);
1907
17
        } else {
1908
17
            script_size += 2 + (keys.size() > 16) + (k > 16) + 34 * keys.size();
1909
17
            constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::MULTI, std::move(keys), k);
1910
17
        }
1911
31
        return true;
1912
31
    };
1913
1914
380k
    while (!to_parse.empty()) {
1915
379k
        if (script_size > max_size) return {};
1916
1917
        // Get the current context we are decoding within
1918
379k
        auto [cur_context, n, k] = to_parse.back();
1919
379k
        to_parse.pop_back();
1920
1921
379k
        switch (cur_context) {
1922
14.2k
        case ParseContext::WRAPPED_EXPR: {
1923
14.2k
            std::optional<size_t> colon_index{};
1924
698k
            for (size_t i = 1; i < in.size(); ++i) {
1925
698k
                if (in[i] == ':') {
1926
6.76k
                    colon_index = i;
1927
6.76k
                    break;
1928
6.76k
                }
1929
692k
                if (in[i] < 'a' || in[i] > 'z') break;
1930
692k
            }
1931
            // If there is no colon, this loop won't execute
1932
14.2k
            bool last_was_v{false};
1933
680k
            for (size_t j = 0; colon_index && j < *colon_index; ++j) {
1934
665k
                if (script_size > max_size) return {};
1935
665k
                if (in[j] == 'a') {
1936
6.28k
                    script_size += 2;
1937
6.28k
                    to_parse.emplace_back(ParseContext::ALT, -1, -1);
1938
659k
                } else if (in[j] == 's') {
1939
87
                    script_size += 1;
1940
87
                    to_parse.emplace_back(ParseContext::SWAP, -1, -1);
1941
659k
                } else if (in[j] == 'c') {
1942
72
                    script_size += 1;
1943
72
                    to_parse.emplace_back(ParseContext::CHECK, -1, -1);
1944
659k
                } else if (in[j] == 'd') {
1945
18
                    script_size += 3;
1946
18
                    to_parse.emplace_back(ParseContext::DUP_IF, -1, -1);
1947
659k
                } else if (in[j] == 'j') {
1948
10
                    script_size += 4;
1949
10
                    to_parse.emplace_back(ParseContext::NON_ZERO, -1, -1);
1950
659k
                } else if (in[j] == 'n') {
1951
658k
                    script_size += 1;
1952
658k
                    to_parse.emplace_back(ParseContext::ZERO_NOTEQUAL, -1, -1);
1953
658k
                } else if (in[j] == 'v') {
1954
                    // do not permit "...vv...:"; it's not valid, and also doesn't trigger early
1955
                    // failure as script_size isn't incremented.
1956
278
                    if (last_was_v) return {};
1957
278
                    to_parse.emplace_back(ParseContext::VERIFY, -1, -1);
1958
278
                } else if (in[j] == 'u') {
1959
23
                    script_size += 4;
1960
23
                    to_parse.emplace_back(ParseContext::WRAP_U, -1, -1);
1961
105
                } else if (in[j] == 't') {
1962
46
                    script_size += 1;
1963
46
                    to_parse.emplace_back(ParseContext::WRAP_T, -1, -1);
1964
59
                } else if (in[j] == 'l') {
1965
                    // The l: wrapper is equivalent to or_i(0,X)
1966
59
                    script_size += 4;
1967
59
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_0);
1968
59
                    to_parse.emplace_back(ParseContext::OR_I, -1, -1);
1969
59
                } else {
1970
0
                    return {};
1971
0
                }
1972
665k
                last_was_v = (in[j] == 'v');
1973
665k
            }
1974
14.2k
            to_parse.emplace_back(ParseContext::EXPR, -1, -1);
1975
14.2k
            if (colon_index) in = in.subspan(*colon_index + 1);
1976
14.2k
            break;
1977
14.2k
        }
1978
14.2k
        case ParseContext::EXPR: {
1979
14.2k
            if (Const("0", in)) {
1980
59
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_0);
1981
14.2k
            } else if (Const("1", in)) {
1982
115
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_1);
1983
14.1k
            } else if (Const("pk(", in, /*skip=*/false)) {
1984
1.02k
                std::optional<Key> key = ParseKey<Key, Ctx>("pk", in, ctx);
1985
1.02k
                if (!key) return {};
1986
1.02k
                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
1.02k
                script_size += IsTapscript(ctx.MsContext()) ? 33 : 34;
1988
13.0k
            } else if (Const("pkh(", in, /*skip=*/false)) {
1989
85
                std::optional<Key> key = ParseKey<Key, Ctx>("pkh", in, ctx);
1990
85
                if (!key) return {};
1991
85
                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
85
                script_size += 24;
1993
12.9k
            } else if (Const("pk_k(", in, /*skip=*/false)) {
1994
76
                std::optional<Key> key = ParseKey<Key, Ctx>("pk_k", in, ctx);
1995
76
                if (!key) return {};
1996
74
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_K, Vector(std::move(*key)));
1997
74
                script_size += IsTapscript(ctx.MsContext()) ? 32 : 33;
1998
12.9k
            } else if (Const("pk_h(", in, /*skip=*/false)) {
1999
28
                std::optional<Key> key = ParseKey<Key, Ctx>("pk_h", in, ctx);
2000
28
                if (!key) return {};
2001
28
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_H, Vector(std::move(*key)));
2002
28
                script_size += 23;
2003
12.8k
            } else if (Const("sha256(", in, /*skip=*/false)) {
2004
30
                std::optional<std::vector<unsigned char>> hash = ParseHexStr("sha256", in, 32);
2005
30
                if (!hash) return {};
2006
30
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::SHA256, std::move(*hash));
2007
30
                script_size += 38;
2008
12.8k
            } else if (Const("ripemd160(", in, /*skip=*/false)) {
2009
15
                std::optional<std::vector<unsigned char>> hash = ParseHexStr("ripemd160", in, 20);
2010
15
                if (!hash) return {};
2011
15
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::RIPEMD160, std::move(*hash));
2012
15
                script_size += 26;
2013
12.8k
            } else if (Const("hash256(", in, /*skip=*/false)) {
2014
22
                std::optional<std::vector<unsigned char>> hash = ParseHexStr("hash256", in, 32);
2015
22
                if (!hash) return {};
2016
22
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::HASH256, std::move(*hash));
2017
22
                script_size += 38;
2018
12.8k
            } else if (Const("hash160(", in, /*skip=*/false)) {
2019
22
                std::optional<std::vector<unsigned char>> hash = ParseHexStr("hash160", in, 20);
2020
22
                if (!hash) return {};
2021
22
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::HASH160, std::move(*hash));
2022
22
                script_size += 26;
2023
12.8k
            } else if (Const("after(", in, /*skip=*/false)) {
2024
128
                auto expr = Expr(in);
2025
128
                if (!Func("after", expr)) return {};
2026
128
                const auto num{ToIntegral<int64_t>(std::string_view(expr.begin(), expr.end()))};
2027
128
                if (!num.has_value() || *num < 1 || *num >= 0x80000000L) return {};
2028
122
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::AFTER, *num);
2029
122
                script_size += 1 + (*num > 16) + (*num > 0x7f) + (*num > 0x7fff) + (*num > 0x7fffff);
2030
12.6k
            } else if (Const("older(", in, /*skip=*/false)) {
2031
5.56k
                auto expr = Expr(in);
2032
5.56k
                if (!Func("older", expr)) return {};
2033
5.56k
                const auto num{ToIntegral<int64_t>(std::string_view(expr.begin(), expr.end()))};
2034
5.56k
                if (!num.has_value() || *num < 1 || *num >= 0x80000000L) return {};
2035
5.55k
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::OLDER, *num);
2036
5.55k
                script_size += 1 + (*num > 16) + (*num > 0x7f) + (*num > 0x7fff) + (*num > 0x7fffff);
2037
7.11k
            } else if (Const("multi(", in)) {
2038
41
                if (!parse_multi_exp(in, /* is_multi_a = */false)) return {};
2039
7.07k
            } else if (Const("multi_a(", in)) {
2040
18
                if (!parse_multi_exp(in, /* is_multi_a = */true)) return {};
2041
7.05k
            } else if (Const("thresh(", in)) {
2042
58
                int next_comma = FindNextChar(in, ',');
2043
58
                if (next_comma < 1) return {};
2044
58
                const auto k{ToIntegral<int64_t>(std::string_view(in.data(), next_comma))};
2045
58
                if (!k.has_value() || *k < 1) return {};
2046
55
                in = in.subspan(next_comma + 1);
2047
                // n = 1 here because we read the first WRAPPED_EXPR before reaching THRESH
2048
55
                to_parse.emplace_back(ParseContext::THRESH, 1, *k);
2049
55
                to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2050
55
                script_size += 2 + (*k > 16) + (*k > 0x7f) + (*k > 0x7fff) + (*k > 0x7fffff);
2051
6.99k
            } else if (Const("andor(", in)) {
2052
55
                to_parse.emplace_back(ParseContext::ANDOR, -1, -1);
2053
55
                to_parse.emplace_back(ParseContext::CLOSE_BRACKET, -1, -1);
2054
55
                to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2055
55
                to_parse.emplace_back(ParseContext::COMMA, -1, -1);
2056
55
                to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2057
55
                to_parse.emplace_back(ParseContext::COMMA, -1, -1);
2058
55
                to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2059
55
                script_size += 5;
2060
6.94k
            } else {
2061
6.94k
                if (Const("and_n(", in)) {
2062
16
                    to_parse.emplace_back(ParseContext::AND_N, -1, -1);
2063
16
                    script_size += 5;
2064
6.92k
                } else if (Const("and_b(", in)) {
2065
6.19k
                    to_parse.emplace_back(ParseContext::AND_B, -1, -1);
2066
6.19k
                    script_size += 2;
2067
6.19k
                } else if (Const("and_v(", in)) {
2068
202
                    to_parse.emplace_back(ParseContext::AND_V, -1, -1);
2069
202
                    script_size += 1;
2070
531
                } else if (Const("or_b(", in)) {
2071
60
                    to_parse.emplace_back(ParseContext::OR_B, -1, -1);
2072
60
                    script_size += 2;
2073
471
                } else if (Const("or_c(", in)) {
2074
28
                    to_parse.emplace_back(ParseContext::OR_C, -1, -1);
2075
28
                    script_size += 3;
2076
443
                } else if (Const("or_d(", in)) {
2077
42
                    to_parse.emplace_back(ParseContext::OR_D, -1, -1);
2078
42
                    script_size += 4;
2079
401
                } else if (Const("or_i(", in)) {
2080
45
                    to_parse.emplace_back(ParseContext::OR_I, -1, -1);
2081
45
                    script_size += 4;
2082
356
                } else {
2083
356
                    return {};
2084
356
                }
2085
6.58k
                to_parse.emplace_back(ParseContext::CLOSE_BRACKET, -1, -1);
2086
6.58k
                to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2087
6.58k
                to_parse.emplace_back(ParseContext::COMMA, -1, -1);
2088
6.58k
                to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2089
6.58k
            }
2090
13.8k
            break;
2091
14.2k
        }
2092
13.8k
        case ParseContext::ALT: {
2093
4.56k
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_A, Vector(std::move(constructed.back()))};
2094
4.56k
            break;
2095
14.2k
        }
2096
87
        case ParseContext::SWAP: {
2097
87
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_S, Vector(std::move(constructed.back()))};
2098
87
            break;
2099
14.2k
        }
2100
68
        case ParseContext::CHECK: {
2101
68
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_C, Vector(std::move(constructed.back()))};
2102
68
            break;
2103
14.2k
        }
2104
18
        case ParseContext::DUP_IF: {
2105
18
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_D, Vector(std::move(constructed.back()))};
2106
18
            break;
2107
14.2k
        }
2108
8
        case ParseContext::NON_ZERO: {
2109
8
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_J, Vector(std::move(constructed.back()))};
2110
8
            break;
2111
14.2k
        }
2112
329k
        case ParseContext::ZERO_NOTEQUAL: {
2113
329k
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_N, Vector(std::move(constructed.back()))};
2114
329k
            break;
2115
14.2k
        }
2116
272
        case ParseContext::VERIFY: {
2117
272
            script_size += (constructed.back().GetType() << "x"_mst);
2118
272
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_V, Vector(std::move(constructed.back()))};
2119
272
            break;
2120
14.2k
        }
2121
16
        case ParseContext::WRAP_U: {
2122
16
            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
16
            break;
2124
14.2k
        }
2125
45
        case ParseContext::WRAP_T: {
2126
45
            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
45
            break;
2128
14.2k
        }
2129
4.46k
        case ParseContext::AND_B: {
2130
4.46k
            BuildBack(ctx.MsContext(), Fragment::AND_B, constructed);
2131
4.46k
            break;
2132
14.2k
        }
2133
16
        case ParseContext::AND_N: {
2134
16
            auto mid = std::move(constructed.back());
2135
16
            constructed.pop_back();
2136
16
            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
16
            break;
2138
14.2k
        }
2139
193
        case ParseContext::AND_V: {
2140
193
            BuildBack(ctx.MsContext(), Fragment::AND_V, constructed);
2141
193
            break;
2142
14.2k
        }
2143
59
        case ParseContext::OR_B: {
2144
59
            BuildBack(ctx.MsContext(), Fragment::OR_B, constructed);
2145
59
            break;
2146
14.2k
        }
2147
26
        case ParseContext::OR_C: {
2148
26
            BuildBack(ctx.MsContext(), Fragment::OR_C, constructed);
2149
26
            break;
2150
14.2k
        }
2151
41
        case ParseContext::OR_D: {
2152
41
            BuildBack(ctx.MsContext(), Fragment::OR_D, constructed);
2153
41
            break;
2154
14.2k
        }
2155
99
        case ParseContext::OR_I: {
2156
99
            BuildBack(ctx.MsContext(), Fragment::OR_I, constructed);
2157
99
            break;
2158
14.2k
        }
2159
52
        case ParseContext::ANDOR: {
2160
52
            auto right = std::move(constructed.back());
2161
52
            constructed.pop_back();
2162
52
            auto mid = std::move(constructed.back());
2163
52
            constructed.pop_back();
2164
52
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::ANDOR, Vector(std::move(constructed.back()), std::move(mid), std::move(right))};
2165
52
            break;
2166
14.2k
        }
2167
164
        case ParseContext::THRESH: {
2168
164
            if (in.size() < 1) return {};
2169
164
            if (in[0] == ',') {
2170
110
                in = in.subspan(1);
2171
110
                to_parse.emplace_back(ParseContext::THRESH, n+1, k);
2172
110
                to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2173
110
                script_size += 2;
2174
110
            } else if (in[0] == ')') {
2175
54
                if (k > n) return {};
2176
52
                in = in.subspan(1);
2177
                // Children are constructed in reverse order, so iterate from end to beginning
2178
52
                std::vector<Node<Key>> subs;
2179
212
                for (int i = 0; i < n; ++i) {
2180
160
                    subs.push_back(std::move(constructed.back()));
2181
160
                    constructed.pop_back();
2182
160
                }
2183
52
                std::reverse(subs.begin(), subs.end());
2184
52
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::THRESH, std::move(subs), k);
2185
52
            } else {
2186
0
                return {};
2187
0
            }
2188
162
            break;
2189
164
        }
2190
6.68k
        case ParseContext::COMMA: {
2191
6.68k
            if (in.size() < 1 || in[0] != ',') return {};
2192
6.68k
            in = in.subspan(1);
2193
6.68k
            break;
2194
6.68k
        }
2195
4.89k
        case ParseContext::CLOSE_BRACKET: {
2196
4.89k
            if (in.size() < 1 || in[0] != ')') return {};
2197
4.89k
            in = in.subspan(1);
2198
4.89k
            break;
2199
4.89k
        }
2200
379k
        }
2201
379k
    }
2202
2203
    // Sanity checks on the produced miniscript
2204
796
    assert(constructed.size() >= 1);
2205
402
    CHECK_NONFATAL(constructed.size() == 1);
2206
402
    assert(constructed[0].ScriptSize() == script_size);
2207
402
    if (in.size() > 0) return {};
2208
399
    Node<Key> tl_node{std::move(constructed.front())};
2209
399
    tl_node.DuplicateKeyCheck(ctx);
2210
399
    return tl_node;
2211
402
}
miniscript_tests.cpp:std::optional<miniscript::Node<CPubKey>> miniscript::internal::Parse<CPubKey, (anonymous namespace)::KeyConverter>(std::span<char const, 18446744073709551615ul>, (anonymous namespace)::KeyConverter const&)
Line
Count
Source
1855
220
{
1856
220
    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
220
    size_t script_size{1};
1869
220
    size_t max_size{internal::MaxScriptSize(ctx.MsContext())};
1870
1871
    // The two integers are used to hold state for thresh()
1872
220
    std::vector<std::tuple<ParseContext, int64_t, int64_t>> to_parse;
1873
220
    std::vector<Node<Key>> constructed;
1874
1875
220
    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
220
    const auto parse_multi_exp = [&](std::span<const char>& in, const bool is_multi_a) -> bool {
1879
220
        const auto max_keys{is_multi_a ? MAX_PUBKEYS_PER_MULTI_A : MAX_PUBKEYS_PER_MULTISIG};
1880
220
        const auto required_ctx{is_multi_a ? MiniscriptContext::TAPSCRIPT : MiniscriptContext::P2WSH};
1881
220
        if (ctx.MsContext() != required_ctx) return false;
1882
        // Get threshold
1883
220
        int next_comma = FindNextChar(in, ',');
1884
220
        if (next_comma < 1) return false;
1885
220
        const auto k_to_integral{ToIntegral<int64_t>(std::string_view(in.data(), next_comma))};
1886
220
        if (!k_to_integral.has_value()) return false;
1887
220
        const int64_t k{k_to_integral.value()};
1888
220
        in = in.subspan(next_comma + 1);
1889
        // Get keys. It is compatible for both compressed and x-only keys.
1890
220
        std::vector<Key> keys;
1891
220
        while (next_comma != -1) {
1892
220
            next_comma = FindNextChar(in, ',');
1893
220
            int key_length = (next_comma == -1) ? FindNextChar(in, ')') : next_comma;
1894
220
            if (key_length < 1) return false;
1895
220
            std::span<const char> sp{in.begin(), in.begin() + key_length};
1896
220
            auto key = ctx.FromString(sp);
1897
220
            if (!key) return false;
1898
220
            keys.push_back(std::move(*key));
1899
220
            in = in.subspan(key_length + 1);
1900
220
        }
1901
220
        if (keys.size() < 1 || keys.size() > max_keys) return false;
1902
220
        if (k < 1 || k > (int64_t)keys.size()) return false;
1903
220
        if (is_multi_a) {
1904
            // (push + xonly-key + CHECKSIG[ADD]) * n + k + OP_NUMEQUAL(VERIFY), minus one.
1905
220
            script_size += (1 + 32 + 1) * keys.size() + BuildScript(k).size();
1906
220
            constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::MULTI_A, std::move(keys), k);
1907
220
        } else {
1908
220
            script_size += 2 + (keys.size() > 16) + (k > 16) + 34 * keys.size();
1909
220
            constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::MULTI, std::move(keys), k);
1910
220
        }
1911
220
        return true;
1912
220
    };
1913
1914
46.4k
    while (!to_parse.empty()) {
1915
46.2k
        if (script_size > max_size) return {};
1916
1917
        // Get the current context we are decoding within
1918
46.2k
        auto [cur_context, n, k] = to_parse.back();
1919
46.2k
        to_parse.pop_back();
1920
1921
46.2k
        switch (cur_context) {
1922
12.9k
        case ParseContext::WRAPPED_EXPR: {
1923
12.9k
            std::optional<size_t> colon_index{};
1924
36.2k
            for (size_t i = 1; i < in.size(); ++i) {
1925
36.2k
                if (in[i] == ':') {
1926
6.42k
                    colon_index = i;
1927
6.42k
                    break;
1928
6.42k
                }
1929
29.8k
                if (in[i] < 'a' || in[i] > 'z') break;
1930
29.8k
            }
1931
            // If there is no colon, this loop won't execute
1932
12.9k
            bool last_was_v{false};
1933
19.4k
            for (size_t j = 0; colon_index && j < *colon_index; ++j) {
1934
6.52k
                if (script_size > max_size) return {};
1935
6.52k
                if (in[j] == 'a') {
1936
6.20k
                    script_size += 2;
1937
6.20k
                    to_parse.emplace_back(ParseContext::ALT, -1, -1);
1938
6.20k
                } else if (in[j] == 's') {
1939
21
                    script_size += 1;
1940
21
                    to_parse.emplace_back(ParseContext::SWAP, -1, -1);
1941
303
                } else if (in[j] == 'c') {
1942
56
                    script_size += 1;
1943
56
                    to_parse.emplace_back(ParseContext::CHECK, -1, -1);
1944
247
                } else if (in[j] == 'd') {
1945
8
                    script_size += 3;
1946
8
                    to_parse.emplace_back(ParseContext::DUP_IF, -1, -1);
1947
239
                } else if (in[j] == 'j') {
1948
10
                    script_size += 4;
1949
10
                    to_parse.emplace_back(ParseContext::NON_ZERO, -1, -1);
1950
229
                } else if (in[j] == 'n') {
1951
16
                    script_size += 1;
1952
16
                    to_parse.emplace_back(ParseContext::ZERO_NOTEQUAL, -1, -1);
1953
213
                } else if (in[j] == 'v') {
1954
                    // do not permit "...vv...:"; it's not valid, and also doesn't trigger early
1955
                    // failure as script_size isn't incremented.
1956
103
                    if (last_was_v) return {};
1957
103
                    to_parse.emplace_back(ParseContext::VERIFY, -1, -1);
1958
110
                } else if (in[j] == 'u') {
1959
23
                    script_size += 4;
1960
23
                    to_parse.emplace_back(ParseContext::WRAP_U, -1, -1);
1961
87
                } else if (in[j] == 't') {
1962
44
                    script_size += 1;
1963
44
                    to_parse.emplace_back(ParseContext::WRAP_T, -1, -1);
1964
44
                } else if (in[j] == 'l') {
1965
                    // The l: wrapper is equivalent to or_i(0,X)
1966
43
                    script_size += 4;
1967
43
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_0);
1968
43
                    to_parse.emplace_back(ParseContext::OR_I, -1, -1);
1969
43
                } else {
1970
0
                    return {};
1971
0
                }
1972
6.52k
                last_was_v = (in[j] == 'v');
1973
6.52k
            }
1974
12.9k
            to_parse.emplace_back(ParseContext::EXPR, -1, -1);
1975
12.9k
            if (colon_index) in = in.subspan(*colon_index + 1);
1976
12.9k
            break;
1977
12.9k
        }
1978
12.9k
        case ParseContext::EXPR: {
1979
12.9k
            if (Const("0", in)) {
1980
56
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_0);
1981
12.9k
            } else if (Const("1", in)) {
1982
112
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_1);
1983
12.7k
            } else if (Const("pk(", in, /*skip=*/false)) {
1984
715
                std::optional<Key> key = ParseKey<Key, Ctx>("pk", in, ctx);
1985
715
                if (!key) return {};
1986
715
                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
715
                script_size += IsTapscript(ctx.MsContext()) ? 33 : 34;
1988
12.0k
            } else if (Const("pkh(", in, /*skip=*/false)) {
1989
3
                std::optional<Key> key = ParseKey<Key, Ctx>("pkh", in, ctx);
1990
3
                if (!key) return {};
1991
3
                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
3
                script_size += 24;
1993
12.0k
            } else if (Const("pk_k(", in, /*skip=*/false)) {
1994
51
                std::optional<Key> key = ParseKey<Key, Ctx>("pk_k", in, ctx);
1995
51
                if (!key) return {};
1996
51
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_K, Vector(std::move(*key)));
1997
51
                script_size += IsTapscript(ctx.MsContext()) ? 32 : 33;
1998
12.0k
            } else if (Const("pk_h(", in, /*skip=*/false)) {
1999
25
                std::optional<Key> key = ParseKey<Key, Ctx>("pk_h", in, ctx);
2000
25
                if (!key) return {};
2001
25
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_H, Vector(std::move(*key)));
2002
25
                script_size += 23;
2003
11.9k
            } else if (Const("sha256(", in, /*skip=*/false)) {
2004
22
                std::optional<std::vector<unsigned char>> hash = ParseHexStr("sha256", in, 32);
2005
22
                if (!hash) return {};
2006
22
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::SHA256, std::move(*hash));
2007
22
                script_size += 38;
2008
11.9k
            } else if (Const("ripemd160(", in, /*skip=*/false)) {
2009
7
                std::optional<std::vector<unsigned char>> hash = ParseHexStr("ripemd160", in, 20);
2010
7
                if (!hash) return {};
2011
7
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::RIPEMD160, std::move(*hash));
2012
7
                script_size += 26;
2013
11.9k
            } else if (Const("hash256(", in, /*skip=*/false)) {
2014
14
                std::optional<std::vector<unsigned char>> hash = ParseHexStr("hash256", in, 32);
2015
14
                if (!hash) return {};
2016
14
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::HASH256, std::move(*hash));
2017
14
                script_size += 38;
2018
11.9k
            } else if (Const("hash160(", in, /*skip=*/false)) {
2019
6
                std::optional<std::vector<unsigned char>> hash = ParseHexStr("hash160", in, 20);
2020
6
                if (!hash) return {};
2021
6
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::HASH160, std::move(*hash));
2022
6
                script_size += 26;
2023
11.9k
            } else if (Const("after(", in, /*skip=*/false)) {
2024
79
                auto expr = Expr(in);
2025
79
                if (!Func("after", expr)) return {};
2026
79
                const auto num{ToIntegral<int64_t>(std::string_view(expr.begin(), expr.end()))};
2027
79
                if (!num.has_value() || *num < 1 || *num >= 0x80000000L) return {};
2028
73
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::AFTER, *num);
2029
73
                script_size += 1 + (*num > 16) + (*num > 0x7f) + (*num > 0x7fff) + (*num > 0x7fffff);
2030
11.8k
            } else if (Const("older(", in, /*skip=*/false)) {
2031
5.48k
                auto expr = Expr(in);
2032
5.48k
                if (!Func("older", expr)) return {};
2033
5.48k
                const auto num{ToIntegral<int64_t>(std::string_view(expr.begin(), expr.end()))};
2034
5.48k
                if (!num.has_value() || *num < 1 || *num >= 0x80000000L) return {};
2035
5.47k
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::OLDER, *num);
2036
5.47k
                script_size += 1 + (*num > 16) + (*num > 0x7f) + (*num > 0x7fff) + (*num > 0x7fffff);
2037
6.38k
            } else if (Const("multi(", in)) {
2038
23
                if (!parse_multi_exp(in, /* is_multi_a = */false)) return {};
2039
6.36k
            } else if (Const("multi_a(", in)) {
2040
4
                if (!parse_multi_exp(in, /* is_multi_a = */true)) return {};
2041
6.35k
            } else if (Const("thresh(", in)) {
2042
25
                int next_comma = FindNextChar(in, ',');
2043
25
                if (next_comma < 1) return {};
2044
25
                const auto k{ToIntegral<int64_t>(std::string_view(in.data(), next_comma))};
2045
25
                if (!k.has_value() || *k < 1) return {};
2046
22
                in = in.subspan(next_comma + 1);
2047
                // n = 1 here because we read the first WRAPPED_EXPR before reaching THRESH
2048
22
                to_parse.emplace_back(ParseContext::THRESH, 1, *k);
2049
22
                to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2050
22
                script_size += 2 + (*k > 16) + (*k > 0x7f) + (*k > 0x7fff) + (*k > 0x7fffff);
2051
6.33k
            } else if (Const("andor(", in)) {
2052
30
                to_parse.emplace_back(ParseContext::ANDOR, -1, -1);
2053
30
                to_parse.emplace_back(ParseContext::CLOSE_BRACKET, -1, -1);
2054
30
                to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2055
30
                to_parse.emplace_back(ParseContext::COMMA, -1, -1);
2056
30
                to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2057
30
                to_parse.emplace_back(ParseContext::COMMA, -1, -1);
2058
30
                to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2059
30
                script_size += 5;
2060
6.30k
            } else {
2061
6.30k
                if (Const("and_n(", in)) {
2062
8
                    to_parse.emplace_back(ParseContext::AND_N, -1, -1);
2063
8
                    script_size += 5;
2064
6.29k
                } else if (Const("and_b(", in)) {
2065
6.15k
                    to_parse.emplace_back(ParseContext::AND_B, -1, -1);
2066
6.15k
                    script_size += 2;
2067
6.15k
                } else if (Const("and_v(", in)) {
2068
43
                    to_parse.emplace_back(ParseContext::AND_V, -1, -1);
2069
43
                    script_size += 1;
2070
97
                } else if (Const("or_b(", in)) {
2071
22
                    to_parse.emplace_back(ParseContext::OR_B, -1, -1);
2072
22
                    script_size += 2;
2073
75
                } else if (Const("or_c(", in)) {
2074
16
                    to_parse.emplace_back(ParseContext::OR_C, -1, -1);
2075
16
                    script_size += 3;
2076
59
                } else if (Const("or_d(", in)) {
2077
24
                    to_parse.emplace_back(ParseContext::OR_D, -1, -1);
2078
24
                    script_size += 4;
2079
35
                } else if (Const("or_i(", in)) {
2080
35
                    to_parse.emplace_back(ParseContext::OR_I, -1, -1);
2081
35
                    script_size += 4;
2082
35
                } else {
2083
0
                    return {};
2084
0
                }
2085
6.30k
                to_parse.emplace_back(ParseContext::CLOSE_BRACKET, -1, -1);
2086
6.30k
                to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2087
6.30k
                to_parse.emplace_back(ParseContext::COMMA, -1, -1);
2088
6.30k
                to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2089
6.30k
            }
2090
12.9k
            break;
2091
12.9k
        }
2092
12.9k
        case ParseContext::ALT: {
2093
4.48k
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_A, Vector(std::move(constructed.back()))};
2094
4.48k
            break;
2095
12.9k
        }
2096
21
        case ParseContext::SWAP: {
2097
21
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_S, Vector(std::move(constructed.back()))};
2098
21
            break;
2099
12.9k
        }
2100
54
        case ParseContext::CHECK: {
2101
54
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_C, Vector(std::move(constructed.back()))};
2102
54
            break;
2103
12.9k
        }
2104
8
        case ParseContext::DUP_IF: {
2105
8
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_D, Vector(std::move(constructed.back()))};
2106
8
            break;
2107
12.9k
        }
2108
8
        case ParseContext::NON_ZERO: {
2109
8
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_J, Vector(std::move(constructed.back()))};
2110
8
            break;
2111
12.9k
        }
2112
15
        case ParseContext::ZERO_NOTEQUAL: {
2113
15
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_N, Vector(std::move(constructed.back()))};
2114
15
            break;
2115
12.9k
        }
2116
99
        case ParseContext::VERIFY: {
2117
99
            script_size += (constructed.back().GetType() << "x"_mst);
2118
99
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_V, Vector(std::move(constructed.back()))};
2119
99
            break;
2120
12.9k
        }
2121
16
        case ParseContext::WRAP_U: {
2122
16
            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
16
            break;
2124
12.9k
        }
2125
43
        case ParseContext::WRAP_T: {
2126
43
            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
43
            break;
2128
12.9k
        }
2129
4.42k
        case ParseContext::AND_B: {
2130
4.42k
            BuildBack(ctx.MsContext(), Fragment::AND_B, constructed);
2131
4.42k
            break;
2132
12.9k
        }
2133
8
        case ParseContext::AND_N: {
2134
8
            auto mid = std::move(constructed.back());
2135
8
            constructed.pop_back();
2136
8
            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
8
            break;
2138
12.9k
        }
2139
38
        case ParseContext::AND_V: {
2140
38
            BuildBack(ctx.MsContext(), Fragment::AND_V, constructed);
2141
38
            break;
2142
12.9k
        }
2143
21
        case ParseContext::OR_B: {
2144
21
            BuildBack(ctx.MsContext(), Fragment::OR_B, constructed);
2145
21
            break;
2146
12.9k
        }
2147
14
        case ParseContext::OR_C: {
2148
14
            BuildBack(ctx.MsContext(), Fragment::OR_C, constructed);
2149
14
            break;
2150
12.9k
        }
2151
23
        case ParseContext::OR_D: {
2152
23
            BuildBack(ctx.MsContext(), Fragment::OR_D, constructed);
2153
23
            break;
2154
12.9k
        }
2155
73
        case ParseContext::OR_I: {
2156
73
            BuildBack(ctx.MsContext(), Fragment::OR_I, constructed);
2157
73
            break;
2158
12.9k
        }
2159
29
        case ParseContext::ANDOR: {
2160
29
            auto right = std::move(constructed.back());
2161
29
            constructed.pop_back();
2162
29
            auto mid = std::move(constructed.back());
2163
29
            constructed.pop_back();
2164
29
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::ANDOR, Vector(std::move(constructed.back()), std::move(mid), std::move(right))};
2165
29
            break;
2166
12.9k
        }
2167
60
        case ParseContext::THRESH: {
2168
60
            if (in.size() < 1) return {};
2169
60
            if (in[0] == ',') {
2170
39
                in = in.subspan(1);
2171
39
                to_parse.emplace_back(ParseContext::THRESH, n+1, k);
2172
39
                to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2173
39
                script_size += 2;
2174
39
            } else if (in[0] == ')') {
2175
21
                if (k > n) return {};
2176
19
                in = in.subspan(1);
2177
                // Children are constructed in reverse order, so iterate from end to beginning
2178
19
                std::vector<Node<Key>> subs;
2179
75
                for (int i = 0; i < n; ++i) {
2180
56
                    subs.push_back(std::move(constructed.back()));
2181
56
                    constructed.pop_back();
2182
56
                }
2183
19
                std::reverse(subs.begin(), subs.end());
2184
19
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::THRESH, std::move(subs), k);
2185
19
            } else {
2186
0
                return {};
2187
0
            }
2188
58
            break;
2189
60
        }
2190
6.34k
        case ParseContext::COMMA: {
2191
6.34k
            if (in.size() < 1 || in[0] != ',') return {};
2192
6.34k
            in = in.subspan(1);
2193
6.34k
            break;
2194
6.34k
        }
2195
4.59k
        case ParseContext::CLOSE_BRACKET: {
2196
4.59k
            if (in.size() < 1 || in[0] != ')') return {};
2197
4.59k
            in = in.subspan(1);
2198
4.59k
            break;
2199
4.59k
        }
2200
46.2k
        }
2201
46.2k
    }
2202
2203
    // Sanity checks on the produced miniscript
2204
220
    assert(constructed.size() >= 1);
2205
188
    CHECK_NONFATAL(constructed.size() == 1);
2206
188
    assert(constructed[0].ScriptSize() == script_size);
2207
188
    if (in.size() > 0) return {};
2208
188
    Node<Key> tl_node{std::move(constructed.front())};
2209
188
    tl_node.DuplicateKeyCheck(ctx);
2210
188
    return tl_node;
2211
188
}
descriptor.cpp:std::optional<miniscript::Node<unsigned int>> miniscript::internal::Parse<unsigned int, (anonymous namespace)::KeyParser>(std::span<char const, 18446744073709551615ul>, (anonymous namespace)::KeyParser const&)
Line
Count
Source
1855
576
{
1856
576
    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
576
    size_t script_size{1};
1869
576
    size_t max_size{internal::MaxScriptSize(ctx.MsContext())};
1870
1871
    // The two integers are used to hold state for thresh()
1872
576
    std::vector<std::tuple<ParseContext, int64_t, int64_t>> to_parse;
1873
576
    std::vector<Node<Key>> constructed;
1874
1875
576
    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
576
    const auto parse_multi_exp = [&](std::span<const char>& in, const bool is_multi_a) -> bool {
1879
576
        const auto max_keys{is_multi_a ? MAX_PUBKEYS_PER_MULTI_A : MAX_PUBKEYS_PER_MULTISIG};
1880
576
        const auto required_ctx{is_multi_a ? MiniscriptContext::TAPSCRIPT : MiniscriptContext::P2WSH};
1881
576
        if (ctx.MsContext() != required_ctx) return false;
1882
        // Get threshold
1883
576
        int next_comma = FindNextChar(in, ',');
1884
576
        if (next_comma < 1) return false;
1885
576
        const auto k_to_integral{ToIntegral<int64_t>(std::string_view(in.data(), next_comma))};
1886
576
        if (!k_to_integral.has_value()) return false;
1887
576
        const int64_t k{k_to_integral.value()};
1888
576
        in = in.subspan(next_comma + 1);
1889
        // Get keys. It is compatible for both compressed and x-only keys.
1890
576
        std::vector<Key> keys;
1891
576
        while (next_comma != -1) {
1892
576
            next_comma = FindNextChar(in, ',');
1893
576
            int key_length = (next_comma == -1) ? FindNextChar(in, ')') : next_comma;
1894
576
            if (key_length < 1) return false;
1895
576
            std::span<const char> sp{in.begin(), in.begin() + key_length};
1896
576
            auto key = ctx.FromString(sp);
1897
576
            if (!key) return false;
1898
576
            keys.push_back(std::move(*key));
1899
576
            in = in.subspan(key_length + 1);
1900
576
        }
1901
576
        if (keys.size() < 1 || keys.size() > max_keys) return false;
1902
576
        if (k < 1 || k > (int64_t)keys.size()) return false;
1903
576
        if (is_multi_a) {
1904
            // (push + xonly-key + CHECKSIG[ADD]) * n + k + OP_NUMEQUAL(VERIFY), minus one.
1905
576
            script_size += (1 + 32 + 1) * keys.size() + BuildScript(k).size();
1906
576
            constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::MULTI_A, std::move(keys), k);
1907
576
        } else {
1908
576
            script_size += 2 + (keys.size() > 16) + (k > 16) + 34 * keys.size();
1909
576
            constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::MULTI, std::move(keys), k);
1910
576
        }
1911
576
        return true;
1912
576
    };
1913
1914
333k
    while (!to_parse.empty()) {
1915
333k
        if (script_size > max_size) return {};
1916
1917
        // Get the current context we are decoding within
1918
333k
        auto [cur_context, n, k] = to_parse.back();
1919
333k
        to_parse.pop_back();
1920
1921
333k
        switch (cur_context) {
1922
1.32k
        case ParseContext::WRAPPED_EXPR: {
1923
1.32k
            std::optional<size_t> colon_index{};
1924
662k
            for (size_t i = 1; i < in.size(); ++i) {
1925
662k
                if (in[i] == ':') {
1926
342
                    colon_index = i;
1927
342
                    break;
1928
342
                }
1929
662k
                if (in[i] < 'a' || in[i] > 'z') break;
1930
662k
            }
1931
            // If there is no colon, this loop won't execute
1932
1.32k
            bool last_was_v{false};
1933
660k
            for (size_t j = 0; colon_index && j < *colon_index; ++j) {
1934
659k
                if (script_size > max_size) return {};
1935
659k
                if (in[j] == 'a') {
1936
82
                    script_size += 2;
1937
82
                    to_parse.emplace_back(ParseContext::ALT, -1, -1);
1938
659k
                } else if (in[j] == 's') {
1939
66
                    script_size += 1;
1940
66
                    to_parse.emplace_back(ParseContext::SWAP, -1, -1);
1941
659k
                } else if (in[j] == 'c') {
1942
16
                    script_size += 1;
1943
16
                    to_parse.emplace_back(ParseContext::CHECK, -1, -1);
1944
659k
                } else if (in[j] == 'd') {
1945
10
                    script_size += 3;
1946
10
                    to_parse.emplace_back(ParseContext::DUP_IF, -1, -1);
1947
659k
                } else if (in[j] == 'j') {
1948
0
                    script_size += 4;
1949
0
                    to_parse.emplace_back(ParseContext::NON_ZERO, -1, -1);
1950
659k
                } else if (in[j] == 'n') {
1951
658k
                    script_size += 1;
1952
658k
                    to_parse.emplace_back(ParseContext::ZERO_NOTEQUAL, -1, -1);
1953
658k
                } else if (in[j] == 'v') {
1954
                    // do not permit "...vv...:"; it's not valid, and also doesn't trigger early
1955
                    // failure as script_size isn't incremented.
1956
175
                    if (last_was_v) return {};
1957
175
                    to_parse.emplace_back(ParseContext::VERIFY, -1, -1);
1958
175
                } else if (in[j] == 'u') {
1959
0
                    script_size += 4;
1960
0
                    to_parse.emplace_back(ParseContext::WRAP_U, -1, -1);
1961
18
                } else if (in[j] == 't') {
1962
2
                    script_size += 1;
1963
2
                    to_parse.emplace_back(ParseContext::WRAP_T, -1, -1);
1964
16
                } else if (in[j] == 'l') {
1965
                    // The l: wrapper is equivalent to or_i(0,X)
1966
16
                    script_size += 4;
1967
16
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_0);
1968
16
                    to_parse.emplace_back(ParseContext::OR_I, -1, -1);
1969
16
                } else {
1970
0
                    return {};
1971
0
                }
1972
659k
                last_was_v = (in[j] == 'v');
1973
659k
            }
1974
1.32k
            to_parse.emplace_back(ParseContext::EXPR, -1, -1);
1975
1.32k
            if (colon_index) in = in.subspan(*colon_index + 1);
1976
1.32k
            break;
1977
1.32k
        }
1978
1.32k
        case ParseContext::EXPR: {
1979
1.32k
            if (Const("0", in)) {
1980
3
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_0);
1981
1.32k
            } else if (Const("1", in)) {
1982
3
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_1);
1983
1.31k
            } else if (Const("pk(", in, /*skip=*/false)) {
1984
311
                std::optional<Key> key = ParseKey<Key, Ctx>("pk", in, ctx);
1985
311
                if (!key) return {};
1986
309
                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
309
                script_size += IsTapscript(ctx.MsContext()) ? 33 : 34;
1988
1.00k
            } else if (Const("pkh(", in, /*skip=*/false)) {
1989
82
                std::optional<Key> key = ParseKey<Key, Ctx>("pkh", in, ctx);
1990
82
                if (!key) return {};
1991
82
                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
82
                script_size += 24;
1993
926
            } else if (Const("pk_k(", in, /*skip=*/false)) {
1994
25
                std::optional<Key> key = ParseKey<Key, Ctx>("pk_k", in, ctx);
1995
25
                if (!key) return {};
1996
23
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_K, Vector(std::move(*key)));
1997
23
                script_size += IsTapscript(ctx.MsContext()) ? 32 : 33;
1998
901
            } else if (Const("pk_h(", in, /*skip=*/false)) {
1999
3
                std::optional<Key> key = ParseKey<Key, Ctx>("pk_h", in, ctx);
2000
3
                if (!key) return {};
2001
3
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_H, Vector(std::move(*key)));
2002
3
                script_size += 23;
2003
898
            } else if (Const("sha256(", in, /*skip=*/false)) {
2004
8
                std::optional<std::vector<unsigned char>> hash = ParseHexStr("sha256", in, 32);
2005
8
                if (!hash) return {};
2006
8
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::SHA256, std::move(*hash));
2007
8
                script_size += 38;
2008
890
            } else if (Const("ripemd160(", in, /*skip=*/false)) {
2009
8
                std::optional<std::vector<unsigned char>> hash = ParseHexStr("ripemd160", in, 20);
2010
8
                if (!hash) return {};
2011
8
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::RIPEMD160, std::move(*hash));
2012
8
                script_size += 26;
2013
882
            } else if (Const("hash256(", in, /*skip=*/false)) {
2014
8
                std::optional<std::vector<unsigned char>> hash = ParseHexStr("hash256", in, 32);
2015
8
                if (!hash) return {};
2016
8
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::HASH256, std::move(*hash));
2017
8
                script_size += 38;
2018
874
            } else if (Const("hash160(", in, /*skip=*/false)) {
2019
16
                std::optional<std::vector<unsigned char>> hash = ParseHexStr("hash160", in, 20);
2020
16
                if (!hash) return {};
2021
16
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::HASH160, std::move(*hash));
2022
16
                script_size += 26;
2023
858
            } else if (Const("after(", in, /*skip=*/false)) {
2024
49
                auto expr = Expr(in);
2025
49
                if (!Func("after", expr)) return {};
2026
49
                const auto num{ToIntegral<int64_t>(std::string_view(expr.begin(), expr.end()))};
2027
49
                if (!num.has_value() || *num < 1 || *num >= 0x80000000L) return {};
2028
49
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::AFTER, *num);
2029
49
                script_size += 1 + (*num > 16) + (*num > 0x7f) + (*num > 0x7fff) + (*num > 0x7fffff);
2030
809
            } else if (Const("older(", in, /*skip=*/false)) {
2031
77
                auto expr = Expr(in);
2032
77
                if (!Func("older", expr)) return {};
2033
77
                const auto num{ToIntegral<int64_t>(std::string_view(expr.begin(), expr.end()))};
2034
77
                if (!num.has_value() || *num < 1 || *num >= 0x80000000L) return {};
2035
77
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::OLDER, *num);
2036
77
                script_size += 1 + (*num > 16) + (*num > 0x7f) + (*num > 0x7fff) + (*num > 0x7fffff);
2037
732
            } else if (Const("multi(", in)) {
2038
18
                if (!parse_multi_exp(in, /* is_multi_a = */false)) return {};
2039
714
            } else if (Const("multi_a(", in)) {
2040
14
                if (!parse_multi_exp(in, /* is_multi_a = */true)) return {};
2041
700
            } else if (Const("thresh(", in)) {
2042
33
                int next_comma = FindNextChar(in, ',');
2043
33
                if (next_comma < 1) return {};
2044
33
                const auto k{ToIntegral<int64_t>(std::string_view(in.data(), next_comma))};
2045
33
                if (!k.has_value() || *k < 1) return {};
2046
33
                in = in.subspan(next_comma + 1);
2047
                // n = 1 here because we read the first WRAPPED_EXPR before reaching THRESH
2048
33
                to_parse.emplace_back(ParseContext::THRESH, 1, *k);
2049
33
                to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2050
33
                script_size += 2 + (*k > 16) + (*k > 0x7f) + (*k > 0x7fff) + (*k > 0x7fffff);
2051
667
            } else if (Const("andor(", in)) {
2052
25
                to_parse.emplace_back(ParseContext::ANDOR, -1, -1);
2053
25
                to_parse.emplace_back(ParseContext::CLOSE_BRACKET, -1, -1);
2054
25
                to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2055
25
                to_parse.emplace_back(ParseContext::COMMA, -1, -1);
2056
25
                to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2057
25
                to_parse.emplace_back(ParseContext::COMMA, -1, -1);
2058
25
                to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2059
25
                script_size += 5;
2060
642
            } else {
2061
642
                if (Const("and_n(", in)) {
2062
8
                    to_parse.emplace_back(ParseContext::AND_N, -1, -1);
2063
8
                    script_size += 5;
2064
634
                } else if (Const("and_b(", in)) {
2065
41
                    to_parse.emplace_back(ParseContext::AND_B, -1, -1);
2066
41
                    script_size += 2;
2067
593
                } else if (Const("and_v(", in)) {
2068
159
                    to_parse.emplace_back(ParseContext::AND_V, -1, -1);
2069
159
                    script_size += 1;
2070
434
                } else if (Const("or_b(", in)) {
2071
38
                    to_parse.emplace_back(ParseContext::OR_B, -1, -1);
2072
38
                    script_size += 2;
2073
396
                } else if (Const("or_c(", in)) {
2074
12
                    to_parse.emplace_back(ParseContext::OR_C, -1, -1);
2075
12
                    script_size += 3;
2076
384
                } else if (Const("or_d(", in)) {
2077
18
                    to_parse.emplace_back(ParseContext::OR_D, -1, -1);
2078
18
                    script_size += 4;
2079
366
                } else if (Const("or_i(", in)) {
2080
10
                    to_parse.emplace_back(ParseContext::OR_I, -1, -1);
2081
10
                    script_size += 4;
2082
356
                } else {
2083
356
                    return {};
2084
356
                }
2085
286
                to_parse.emplace_back(ParseContext::CLOSE_BRACKET, -1, -1);
2086
286
                to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2087
286
                to_parse.emplace_back(ParseContext::COMMA, -1, -1);
2088
286
                to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2089
286
            }
2090
964
            break;
2091
1.32k
        }
2092
964
        case ParseContext::ALT: {
2093
82
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_A, Vector(std::move(constructed.back()))};
2094
82
            break;
2095
1.32k
        }
2096
66
        case ParseContext::SWAP: {
2097
66
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_S, Vector(std::move(constructed.back()))};
2098
66
            break;
2099
1.32k
        }
2100
14
        case ParseContext::CHECK: {
2101
14
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_C, Vector(std::move(constructed.back()))};
2102
14
            break;
2103
1.32k
        }
2104
10
        case ParseContext::DUP_IF: {
2105
10
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_D, Vector(std::move(constructed.back()))};
2106
10
            break;
2107
1.32k
        }
2108
0
        case ParseContext::NON_ZERO: {
2109
0
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_J, Vector(std::move(constructed.back()))};
2110
0
            break;
2111
1.32k
        }
2112
329k
        case ParseContext::ZERO_NOTEQUAL: {
2113
329k
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_N, Vector(std::move(constructed.back()))};
2114
329k
            break;
2115
1.32k
        }
2116
173
        case ParseContext::VERIFY: {
2117
173
            script_size += (constructed.back().GetType() << "x"_mst);
2118
173
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_V, Vector(std::move(constructed.back()))};
2119
173
            break;
2120
1.32k
        }
2121
0
        case ParseContext::WRAP_U: {
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
1.32k
        }
2125
2
        case ParseContext::WRAP_T: {
2126
2
            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
2
            break;
2128
1.32k
        }
2129
41
        case ParseContext::AND_B: {
2130
41
            BuildBack(ctx.MsContext(), Fragment::AND_B, constructed);
2131
41
            break;
2132
1.32k
        }
2133
8
        case ParseContext::AND_N: {
2134
8
            auto mid = std::move(constructed.back());
2135
8
            constructed.pop_back();
2136
8
            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
8
            break;
2138
1.32k
        }
2139
155
        case ParseContext::AND_V: {
2140
155
            BuildBack(ctx.MsContext(), Fragment::AND_V, constructed);
2141
155
            break;
2142
1.32k
        }
2143
38
        case ParseContext::OR_B: {
2144
38
            BuildBack(ctx.MsContext(), Fragment::OR_B, constructed);
2145
38
            break;
2146
1.32k
        }
2147
12
        case ParseContext::OR_C: {
2148
12
            BuildBack(ctx.MsContext(), Fragment::OR_C, constructed);
2149
12
            break;
2150
1.32k
        }
2151
18
        case ParseContext::OR_D: {
2152
18
            BuildBack(ctx.MsContext(), Fragment::OR_D, constructed);
2153
18
            break;
2154
1.32k
        }
2155
26
        case ParseContext::OR_I: {
2156
26
            BuildBack(ctx.MsContext(), Fragment::OR_I, constructed);
2157
26
            break;
2158
1.32k
        }
2159
23
        case ParseContext::ANDOR: {
2160
23
            auto right = std::move(constructed.back());
2161
23
            constructed.pop_back();
2162
23
            auto mid = std::move(constructed.back());
2163
23
            constructed.pop_back();
2164
23
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::ANDOR, Vector(std::move(constructed.back()), std::move(mid), std::move(right))};
2165
23
            break;
2166
1.32k
        }
2167
104
        case ParseContext::THRESH: {
2168
104
            if (in.size() < 1) return {};
2169
104
            if (in[0] == ',') {
2170
71
                in = in.subspan(1);
2171
71
                to_parse.emplace_back(ParseContext::THRESH, n+1, k);
2172
71
                to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2173
71
                script_size += 2;
2174
71
            } else if (in[0] == ')') {
2175
33
                if (k > n) return {};
2176
33
                in = in.subspan(1);
2177
                // Children are constructed in reverse order, so iterate from end to beginning
2178
33
                std::vector<Node<Key>> subs;
2179
137
                for (int i = 0; i < n; ++i) {
2180
104
                    subs.push_back(std::move(constructed.back()));
2181
104
                    constructed.pop_back();
2182
104
                }
2183
33
                std::reverse(subs.begin(), subs.end());
2184
33
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::THRESH, std::move(subs), k);
2185
33
            } else {
2186
0
                return {};
2187
0
            }
2188
104
            break;
2189
104
        }
2190
334
        case ParseContext::COMMA: {
2191
334
            if (in.size() < 1 || in[0] != ',') return {};
2192
334
            in = in.subspan(1);
2193
334
            break;
2194
334
        }
2195
305
        case ParseContext::CLOSE_BRACKET: {
2196
305
            if (in.size() < 1 || in[0] != ')') return {};
2197
305
            in = in.subspan(1);
2198
305
            break;
2199
305
        }
2200
333k
        }
2201
333k
    }
2202
2203
    // Sanity checks on the produced miniscript
2204
576
    assert(constructed.size() >= 1);
2205
214
    CHECK_NONFATAL(constructed.size() == 1);
2206
214
    assert(constructed[0].ScriptSize() == script_size);
2207
214
    if (in.size() > 0) return {};
2208
211
    Node<Key> tl_node{std::move(constructed.front())};
2209
211
    tl_node.DuplicateKeyCheck(ctx);
2210
211
    return tl_node;
2211
214
}
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
5.48k
{
2298
    // The two integers are used to hold state for thresh()
2299
5.48k
    std::vector<std::tuple<DecodeContext, int64_t, int64_t>> to_parse;
2300
5.48k
    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
5.48k
    to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2305
2306
4.03M
    while (!to_parse.empty()) {
2307
        // Exit early if the Miniscript is not going to be valid.
2308
4.02M
        if (!constructed.empty() && !constructed.back().IsValid()) return {};
2309
2310
        // Get the current context we are decoding within
2311
4.02M
        auto [cur_context, n, k] = to_parse.back();
2312
4.02M
        to_parse.pop_back();
2313
2314
4.02M
        switch(cur_context) {
2315
2.00M
        case DecodeContext::SINGLE_BKV_EXPR: {
2316
2.00M
            if (in >= last) return {};
2317
2318
            // Constants
2319
2.00M
            if (in[0].first == OP_1) {
2320
80
                ++in;
2321
80
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_1);
2322
80
                break;
2323
80
            }
2324
2.00M
            if (in[0].first == OP_0) {
2325
519
                ++in;
2326
519
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_0);
2327
519
                break;
2328
519
            }
2329
            // Public keys
2330
2.00M
            if (in[0].second.size() == 33 || in[0].second.size() == 32) {
2331
5.65k
                auto key = ctx.FromPKBytes(in[0].second.begin(), in[0].second.end());
2332
5.65k
                if (!key) return {};
2333
5.64k
                ++in;
2334
5.64k
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_K, Vector(std::move(*key)));
2335
5.64k
                break;
2336
5.65k
            }
2337
1.99M
            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) {
2338
783
                auto key = ctx.FromPKHBytes(in[2].second.begin(), in[2].second.end());
2339
783
                if (!key) return {};
2340
780
                in += 5;
2341
780
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_H, Vector(std::move(*key)));
2342
780
                break;
2343
783
            }
2344
            // Time locks
2345
1.99M
            std::optional<int64_t> num;
2346
1.99M
            if (last - in >= 2 && in[0].first == OP_CHECKSEQUENCEVERIFY && (num = ParseScriptNumber(in[1]))) {
2347
2.36k
                in += 2;
2348
2.36k
                if (*num < 1 || *num > 0x7FFFFFFFL) return {};
2349
2.36k
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::OLDER, *num);
2350
2.36k
                break;
2351
2.36k
            }
2352
1.99M
            if (last - in >= 2 && in[0].first == OP_CHECKLOCKTIMEVERIFY && (num = ParseScriptNumber(in[1]))) {
2353
1.28k
                in += 2;
2354
1.28k
                if (num < 1 || num > 0x7FFFFFFFL) return {};
2355
1.28k
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::AFTER, *num);
2356
1.28k
                break;
2357
1.28k
            }
2358
            // Hashes
2359
1.99M
            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) {
2360
270
                if (in[2].first == OP_SHA256 && in[1].second.size() == 32) {
2361
66
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::SHA256, in[1].second);
2362
66
                    in += 7;
2363
66
                    break;
2364
204
                } else if (in[2].first == OP_RIPEMD160 && in[1].second.size() == 20) {
2365
55
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::RIPEMD160, in[1].second);
2366
55
                    in += 7;
2367
55
                    break;
2368
149
                } else if (in[2].first == OP_HASH256 && in[1].second.size() == 32) {
2369
86
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::HASH256, in[1].second);
2370
86
                    in += 7;
2371
86
                    break;
2372
86
                } else if (in[2].first == OP_HASH160 && in[1].second.size() == 20) {
2373
63
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::HASH160, in[1].second);
2374
63
                    in += 7;
2375
63
                    break;
2376
63
                }
2377
270
            }
2378
            // Multi
2379
1.99M
            if (last - in >= 3 && in[0].first == OP_CHECKMULTISIG) {
2380
134
                if (IsTapscript(ctx.MsContext())) return {};
2381
134
                std::vector<Key> keys;
2382
134
                const auto n = ParseScriptNumber(in[1]);
2383
134
                if (!n || last - in < 3 + *n) return {};
2384
134
                if (*n < 1 || *n > 20) return {};
2385
451
                for (int i = 0; i < *n; ++i) {
2386
317
                    if (in[2 + i].second.size() != 33) return {};
2387
317
                    auto key = ctx.FromPKBytes(in[2 + i].second.begin(), in[2 + i].second.end());
2388
317
                    if (!key) return {};
2389
317
                    keys.push_back(std::move(*key));
2390
317
                }
2391
134
                const auto k = ParseScriptNumber(in[2 + *n]);
2392
134
                if (!k || *k < 1 || *k > *n) return {};
2393
134
                in += 3 + *n;
2394
134
                std::reverse(keys.begin(), keys.end());
2395
134
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::MULTI, std::move(keys), *k);
2396
134
                break;
2397
134
            }
2398
            // Tapscript's equivalent of multi
2399
1.99M
            if (last - in >= 4 && in[0].first == OP_NUMEQUAL) {
2400
808
                if (!IsTapscript(ctx.MsContext())) return {};
2401
                // The necessary threshold of signatures.
2402
808
                const auto k = ParseScriptNumber(in[1]);
2403
808
                if (!k) return {};
2404
808
                if (*k < 1 || *k > MAX_PUBKEYS_PER_MULTI_A) return {};
2405
808
                if (last - in < 2 + *k * 2) return {};
2406
808
                std::vector<Key> keys;
2407
808
                keys.reserve(*k);
2408
                // Walk through the expected (pubkey, CHECKSIG[ADD]) pairs.
2409
93.6k
                for (int pos = 2;; pos += 2) {
2410
93.6k
                    if (last - in < pos + 2) return {};
2411
                    // Make sure it's indeed an x-only pubkey and a CHECKSIG[ADD], then parse the key.
2412
93.6k
                    if (in[pos].first != OP_CHECKSIGADD && in[pos].first != OP_CHECKSIG) return {};
2413
93.6k
                    if (in[pos + 1].second.size() != 32) return {};
2414
93.6k
                    auto key = ctx.FromPKBytes(in[pos + 1].second.begin(), in[pos + 1].second.end());
2415
93.6k
                    if (!key) return {};
2416
93.6k
                    keys.push_back(std::move(*key));
2417
                    // Make sure early we don't parse an arbitrary large expression.
2418
93.6k
                    if (keys.size() > MAX_PUBKEYS_PER_MULTI_A) return {};
2419
                    // OP_CHECKSIG means it was the last one to parse.
2420
93.6k
                    if (in[pos].first == OP_CHECKSIG) break;
2421
93.6k
                }
2422
807
                if (keys.size() < (size_t)*k) return {};
2423
807
                in += 2 + keys.size() * 2;
2424
807
                std::reverse(keys.begin(), keys.end());
2425
807
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::MULTI_A, std::move(keys), *k);
2426
807
                break;
2427
807
            }
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
1.98M
            if (in[0].first == OP_CHECKSIG) {
2433
6.39k
                ++in;
2434
6.39k
                to_parse.emplace_back(DecodeContext::CHECK, -1, -1);
2435
6.39k
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2436
6.39k
                break;
2437
6.39k
            }
2438
            // v: wrapper
2439
1.98M
            if (in[0].first == OP_VERIFY) {
2440
1.77k
                ++in;
2441
1.77k
                to_parse.emplace_back(DecodeContext::VERIFY, -1, -1);
2442
1.77k
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2443
1.77k
                break;
2444
1.77k
            }
2445
            // n: wrapper
2446
1.98M
            if (in[0].first == OP_0NOTEQUAL) {
2447
1.97M
                ++in;
2448
1.97M
                to_parse.emplace_back(DecodeContext::ZERO_NOTEQUAL, -1, -1);
2449
1.97M
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2450
1.97M
                break;
2451
1.97M
            }
2452
            // Thresh
2453
4.01k
            if (last - in >= 3 && in[0].first == OP_EQUAL && (num = ParseScriptNumber(in[1]))) {
2454
341
                if (*num < 1) return {};
2455
341
                in += 2;
2456
341
                to_parse.emplace_back(DecodeContext::THRESH_W, 0, *num);
2457
341
                break;
2458
341
            }
2459
            // OP_ENDIF can be WRAP_J, WRAP_D, ANDOR, OR_C, OR_D, or OR_I
2460
3.67k
            if (in[0].first == OP_ENDIF) {
2461
858
                ++in;
2462
858
                to_parse.emplace_back(DecodeContext::ENDIF, -1, -1);
2463
858
                to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2464
858
                break;
2465
858
            }
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
2.81k
            if (in[0].first == OP_BOOLAND) {
2473
2.74k
                ++in;
2474
2.74k
                to_parse.emplace_back(DecodeContext::AND_B, -1, -1);
2475
2.74k
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2476
2.74k
                to_parse.emplace_back(DecodeContext::W_EXPR, -1, -1);
2477
2.74k
                break;
2478
2.74k
            }
2479
            // or_b
2480
70
            if (in[0].first == OP_BOOLOR) {
2481
60
                ++in;
2482
60
                to_parse.emplace_back(DecodeContext::OR_B, -1, -1);
2483
60
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2484
60
                to_parse.emplace_back(DecodeContext::W_EXPR, -1, -1);
2485
60
                break;
2486
60
            }
2487
            // Unrecognised expression
2488
10
            return {};
2489
70
        }
2490
12.5k
        case DecodeContext::BKV_EXPR: {
2491
12.5k
            to_parse.emplace_back(DecodeContext::MAYBE_AND_V, -1, -1);
2492
12.5k
            to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2493
12.5k
            break;
2494
70
        }
2495
3.82k
        case DecodeContext::W_EXPR: {
2496
            // a: wrapper
2497
3.82k
            if (in >= last) return {};
2498
3.82k
            if (in[0].first == OP_FROMALTSTACK) {
2499
3.01k
                ++in;
2500
3.01k
                to_parse.emplace_back(DecodeContext::ALT, -1, -1);
2501
3.01k
            } else {
2502
804
                to_parse.emplace_back(DecodeContext::SWAP, -1, -1);
2503
804
            }
2504
3.82k
            to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2505
3.82k
            break;
2506
3.82k
        }
2507
12.4k
        case DecodeContext::MAYBE_AND_V: {
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
12.4k
            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) {
2511
1.67k
                to_parse.emplace_back(DecodeContext::AND_V, -1, -1);
2512
                // BKV_EXPR can contain more AND_V nodes
2513
1.67k
                to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2514
1.67k
            }
2515
12.4k
            break;
2516
3.82k
        }
2517
804
        case DecodeContext::SWAP: {
2518
804
            if (in >= last || in[0].first != OP_SWAP || constructed.empty()) return {};
2519
804
            ++in;
2520
804
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_S, Vector(std::move(constructed.back()))};
2521
804
            break;
2522
804
        }
2523
3.01k
        case DecodeContext::ALT: {
2524
3.01k
            if (in >= last || in[0].first != OP_TOALTSTACK || constructed.empty()) return {};
2525
3.01k
            ++in;
2526
3.01k
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_A, Vector(std::move(constructed.back()))};
2527
3.01k
            break;
2528
3.01k
        }
2529
6.39k
        case DecodeContext::CHECK: {
2530
6.39k
            if (constructed.empty()) return {};
2531
6.39k
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_C, Vector(std::move(constructed.back()))};
2532
6.39k
            break;
2533
6.39k
        }
2534
86
        case DecodeContext::DUP_IF: {
2535
86
            if (constructed.empty()) return {};
2536
86
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_D, Vector(std::move(constructed.back()))};
2537
86
            break;
2538
86
        }
2539
1.77k
        case DecodeContext::VERIFY: {
2540
1.77k
            if (constructed.empty()) return {};
2541
1.77k
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_V, Vector(std::move(constructed.back()))};
2542
1.77k
            break;
2543
1.77k
        }
2544
8
        case DecodeContext::NON_ZERO: {
2545
8
            if (constructed.empty()) return {};
2546
8
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_J, Vector(std::move(constructed.back()))};
2547
8
            break;
2548
8
        }
2549
1.97M
        case DecodeContext::ZERO_NOTEQUAL: {
2550
1.97M
            if (constructed.empty()) return {};
2551
1.97M
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_N, Vector(std::move(constructed.back()))};
2552
1.97M
            break;
2553
1.97M
        }
2554
1.67k
        case DecodeContext::AND_V: {
2555
1.67k
            if (constructed.size() < 2) return {};
2556
1.67k
            BuildBack(ctx.MsContext(), Fragment::AND_V, constructed, /*reverse=*/true);
2557
1.67k
            break;
2558
1.67k
        }
2559
2.74k
        case DecodeContext::AND_B: {
2560
2.74k
            if (constructed.size() < 2) return {};
2561
2.74k
            BuildBack(ctx.MsContext(), Fragment::AND_B, constructed, /*reverse=*/true);
2562
2.74k
            break;
2563
2.74k
        }
2564
60
        case DecodeContext::OR_B: {
2565
60
            if (constructed.size() < 2) return {};
2566
60
            BuildBack(ctx.MsContext(), Fragment::OR_B, constructed, /*reverse=*/true);
2567
60
            break;
2568
60
        }
2569
26
        case DecodeContext::OR_C: {
2570
26
            if (constructed.size() < 2) return {};
2571
26
            BuildBack(ctx.MsContext(), Fragment::OR_C, constructed, /*reverse=*/true);
2572
26
            break;
2573
26
        }
2574
66
        case DecodeContext::OR_D: {
2575
66
            if (constructed.size() < 2) return {};
2576
66
            BuildBack(ctx.MsContext(), Fragment::OR_D, constructed, /*reverse=*/true);
2577
66
            break;
2578
66
        }
2579
163
        case DecodeContext::ANDOR: {
2580
163
            if (constructed.size() < 3) return {};
2581
163
            Node left{std::move(constructed.back())};
2582
163
            constructed.pop_back();
2583
163
            Node right{std::move(constructed.back())};
2584
163
            constructed.pop_back();
2585
163
            Node mid{std::move(constructed.back())};
2586
163
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::ANDOR, Vector(std::move(left), std::move(mid), std::move(right))};
2587
163
            break;
2588
163
        }
2589
1.35k
        case DecodeContext::THRESH_W: {
2590
1.35k
            if (in >= last) return {};
2591
1.35k
            if (in[0].first == OP_ADD) {
2592
1.01k
                ++in;
2593
1.01k
                to_parse.emplace_back(DecodeContext::THRESH_W, n+1, k);
2594
1.01k
                to_parse.emplace_back(DecodeContext::W_EXPR, -1, -1);
2595
1.01k
            } else {
2596
341
                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
341
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2599
341
            }
2600
1.35k
            break;
2601
1.35k
        }
2602
341
        case DecodeContext::THRESH_E: {
2603
341
            if (k < 1 || k > n || constructed.size() < static_cast<size_t>(n)) return {};
2604
341
            std::vector<Node<Key>> subs;
2605
1.69k
            for (int i = 0; i < n; ++i) {
2606
1.35k
                Node sub{std::move(constructed.back())};
2607
1.35k
                constructed.pop_back();
2608
1.35k
                subs.push_back(std::move(sub));
2609
1.35k
            }
2610
341
            constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::THRESH, std::move(subs), k);
2611
341
            break;
2612
341
        }
2613
857
        case DecodeContext::ENDIF: {
2614
857
            if (in >= last) return {};
2615
2616
            // could be andor or or_i
2617
857
            if (in[0].first == OP_ELSE) {
2618
671
                ++in;
2619
671
                to_parse.emplace_back(DecodeContext::ENDIF_ELSE, -1, -1);
2620
671
                to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2621
671
            }
2622
            // could be j: or d: wrapper
2623
186
            else if (in[0].first == OP_IF) {
2624
94
                if (last - in >= 2 && in[1].first == OP_DUP) {
2625
86
                    in += 2;
2626
86
                    to_parse.emplace_back(DecodeContext::DUP_IF, -1, -1);
2627
86
                } else if (last - in >= 3 && in[1].first == OP_0NOTEQUAL && in[2].first == OP_SIZE) {
2628
8
                    in += 3;
2629
8
                    to_parse.emplace_back(DecodeContext::NON_ZERO, -1, -1);
2630
8
                }
2631
0
                else {
2632
0
                    return {};
2633
0
                }
2634
            // could be or_c or or_d
2635
94
            } else if (in[0].first == OP_NOTIF) {
2636
92
                ++in;
2637
92
                to_parse.emplace_back(DecodeContext::ENDIF_NOTIF, -1, -1);
2638
92
            }
2639
0
            else {
2640
0
                return {};
2641
0
            }
2642
857
            break;
2643
857
        }
2644
857
        case DecodeContext::ENDIF_NOTIF: {
2645
92
            if (in >= last) return {};
2646
92
            if (in[0].first == OP_IFDUP) {
2647
66
                ++in;
2648
66
                to_parse.emplace_back(DecodeContext::OR_D, -1, -1);
2649
66
            } else {
2650
26
                to_parse.emplace_back(DecodeContext::OR_C, -1, -1);
2651
26
            }
2652
            // or_c and or_d both require X to have type modifier d so, can't contain and_v
2653
92
            to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2654
92
            break;
2655
92
        }
2656
671
        case DecodeContext::ENDIF_ELSE: {
2657
671
            if (in >= last) return {};
2658
671
            if (in[0].first == OP_IF) {
2659
508
                ++in;
2660
508
                BuildBack(ctx.MsContext(), Fragment::OR_I, constructed, /*reverse=*/true);
2661
508
            } else if (in[0].first == OP_NOTIF) {
2662
163
                ++in;
2663
163
                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
163
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2666
163
            } else {
2667
0
                return {};
2668
0
            }
2669
671
            break;
2670
671
        }
2671
4.02M
        }
2672
4.02M
    }
2673
5.46k
    if (constructed.size() != 1) return {};
2674
5.46k
    Node tl_node{std::move(constructed.front())};
2675
5.46k
    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
5.46k
    if (!tl_node.IsValidTopLevel()) return {};
2679
5.46k
    return tl_node;
2680
5.46k
}
miniscript_tests.cpp:std::optional<miniscript::Node<CPubKey>> miniscript::internal::DecodeScript<CPubKey, (anonymous namespace)::KeyConverter, __gnu_cxx::__normal_iterator<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>*, std::vector<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>, std::allocator<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>>>>>(__gnu_cxx::__normal_iterator<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>*, std::vector<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>, std::allocator<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>>>>&, __gnu_cxx::__normal_iterator<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>*, std::vector<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>, std::allocator<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>>>>, (anonymous namespace)::KeyConverter const&)
Line
Count
Source
2297
128
{
2298
    // The two integers are used to hold state for thresh()
2299
128
    std::vector<std::tuple<DecodeContext, int64_t, int64_t>> to_parse;
2300
128
    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
128
    to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2305
2306
20.2k
    while (!to_parse.empty()) {
2307
        // Exit early if the Miniscript is not going to be valid.
2308
20.1k
        if (!constructed.empty() && !constructed.back().IsValid()) return {};
2309
2310
        // Get the current context we are decoding within
2311
20.1k
        auto [cur_context, n, k] = to_parse.back();
2312
20.1k
        to_parse.pop_back();
2313
2314
20.1k
        switch(cur_context) {
2315
5.95k
        case DecodeContext::SINGLE_BKV_EXPR: {
2316
5.95k
            if (in >= last) return {};
2317
2318
            // Constants
2319
5.95k
            if (in[0].first == OP_1) {
2320
77
                ++in;
2321
77
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_1);
2322
77
                break;
2323
77
            }
2324
5.87k
            if (in[0].first == OP_0) {
2325
83
                ++in;
2326
83
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_0);
2327
83
                break;
2328
83
            }
2329
            // Public keys
2330
5.79k
            if (in[0].second.size() == 33 || in[0].second.size() == 32) {
2331
454
                auto key = ctx.FromPKBytes(in[0].second.begin(), in[0].second.end());
2332
454
                if (!key) return {};
2333
454
                ++in;
2334
454
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_K, Vector(std::move(*key)));
2335
454
                break;
2336
454
            }
2337
5.33k
            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) {
2338
26
                auto key = ctx.FromPKHBytes(in[2].second.begin(), in[2].second.end());
2339
26
                if (!key) return {};
2340
26
                in += 5;
2341
26
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_H, Vector(std::move(*key)));
2342
26
                break;
2343
26
            }
2344
            // Time locks
2345
5.31k
            std::optional<int64_t> num;
2346
5.31k
            if (last - in >= 2 && in[0].first == OP_CHECKSEQUENCEVERIFY && (num = ParseScriptNumber(in[1]))) {
2347
2.03k
                in += 2;
2348
2.03k
                if (*num < 1 || *num > 0x7FFFFFFFL) return {};
2349
2.03k
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::OLDER, *num);
2350
2.03k
                break;
2351
2.03k
            }
2352
3.27k
            if (last - in >= 2 && in[0].first == OP_CHECKLOCKTIMEVERIFY && (num = ParseScriptNumber(in[1]))) {
2353
65
                in += 2;
2354
65
                if (num < 1 || num > 0x7FFFFFFFL) return {};
2355
65
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::AFTER, *num);
2356
65
                break;
2357
65
            }
2358
            // Hashes
2359
3.21k
            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) {
2360
48
                if (in[2].first == OP_SHA256 && in[1].second.size() == 32) {
2361
21
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::SHA256, in[1].second);
2362
21
                    in += 7;
2363
21
                    break;
2364
27
                } else if (in[2].first == OP_RIPEMD160 && in[1].second.size() == 20) {
2365
7
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::RIPEMD160, in[1].second);
2366
7
                    in += 7;
2367
7
                    break;
2368
20
                } else if (in[2].first == OP_HASH256 && in[1].second.size() == 32) {
2369
14
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::HASH256, in[1].second);
2370
14
                    in += 7;
2371
14
                    break;
2372
14
                } else if (in[2].first == OP_HASH160 && in[1].second.size() == 20) {
2373
6
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::HASH160, in[1].second);
2374
6
                    in += 7;
2375
6
                    break;
2376
6
                }
2377
48
            }
2378
            // Multi
2379
3.16k
            if (last - in >= 3 && in[0].first == OP_CHECKMULTISIG) {
2380
12
                if (IsTapscript(ctx.MsContext())) return {};
2381
12
                std::vector<Key> keys;
2382
12
                const auto n = ParseScriptNumber(in[1]);
2383
12
                if (!n || last - in < 3 + *n) return {};
2384
12
                if (*n < 1 || *n > 20) return {};
2385
35
                for (int i = 0; i < *n; ++i) {
2386
23
                    if (in[2 + i].second.size() != 33) return {};
2387
23
                    auto key = ctx.FromPKBytes(in[2 + i].second.begin(), in[2 + i].second.end());
2388
23
                    if (!key) return {};
2389
23
                    keys.push_back(std::move(*key));
2390
23
                }
2391
12
                const auto k = ParseScriptNumber(in[2 + *n]);
2392
12
                if (!k || *k < 1 || *k > *n) return {};
2393
12
                in += 3 + *n;
2394
12
                std::reverse(keys.begin(), keys.end());
2395
12
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::MULTI, std::move(keys), *k);
2396
12
                break;
2397
12
            }
2398
            // Tapscript's equivalent of multi
2399
3.15k
            if (last - in >= 4 && in[0].first == OP_NUMEQUAL) {
2400
4
                if (!IsTapscript(ctx.MsContext())) return {};
2401
                // The necessary threshold of signatures.
2402
4
                const auto k = ParseScriptNumber(in[1]);
2403
4
                if (!k) return {};
2404
4
                if (*k < 1 || *k > MAX_PUBKEYS_PER_MULTI_A) return {};
2405
4
                if (last - in < 2 + *k * 2) return {};
2406
4
                std::vector<Key> keys;
2407
4
                keys.reserve(*k);
2408
                // Walk through the expected (pubkey, CHECKSIG[ADD]) pairs.
2409
27
                for (int pos = 2;; pos += 2) {
2410
27
                    if (last - in < pos + 2) return {};
2411
                    // Make sure it's indeed an x-only pubkey and a CHECKSIG[ADD], then parse the key.
2412
26
                    if (in[pos].first != OP_CHECKSIGADD && in[pos].first != OP_CHECKSIG) return {};
2413
26
                    if (in[pos + 1].second.size() != 32) return {};
2414
26
                    auto key = ctx.FromPKBytes(in[pos + 1].second.begin(), in[pos + 1].second.end());
2415
26
                    if (!key) return {};
2416
26
                    keys.push_back(std::move(*key));
2417
                    // Make sure early we don't parse an arbitrary large expression.
2418
26
                    if (keys.size() > MAX_PUBKEYS_PER_MULTI_A) return {};
2419
                    // OP_CHECKSIG means it was the last one to parse.
2420
26
                    if (in[pos].first == OP_CHECKSIG) break;
2421
26
                }
2422
3
                if (keys.size() < (size_t)*k) return {};
2423
3
                in += 2 + keys.size() * 2;
2424
3
                std::reverse(keys.begin(), keys.end());
2425
3
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::MULTI_A, std::move(keys), *k);
2426
3
                break;
2427
3
            }
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
3.14k
            if (in[0].first == OP_CHECKSIG) {
2433
465
                ++in;
2434
465
                to_parse.emplace_back(DecodeContext::CHECK, -1, -1);
2435
465
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2436
465
                break;
2437
465
            }
2438
            // v: wrapper
2439
2.68k
            if (in[0].first == OP_VERIFY) {
2440
81
                ++in;
2441
81
                to_parse.emplace_back(DecodeContext::VERIFY, -1, -1);
2442
81
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2443
81
                break;
2444
81
            }
2445
            // n: wrapper
2446
2.60k
            if (in[0].first == OP_0NOTEQUAL) {
2447
15
                ++in;
2448
15
                to_parse.emplace_back(DecodeContext::ZERO_NOTEQUAL, -1, -1);
2449
15
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2450
15
                break;
2451
15
            }
2452
            // Thresh
2453
2.58k
            if (last - in >= 3 && in[0].first == OP_EQUAL && (num = ParseScriptNumber(in[1]))) {
2454
16
                if (*num < 1) return {};
2455
16
                in += 2;
2456
16
                to_parse.emplace_back(DecodeContext::THRESH_W, 0, *num);
2457
16
                break;
2458
16
            }
2459
            // OP_ENDIF can be WRAP_J, WRAP_D, ANDOR, OR_C, OR_D, or OR_I
2460
2.56k
            if (in[0].first == OP_ENDIF) {
2461
142
                ++in;
2462
142
                to_parse.emplace_back(DecodeContext::ENDIF, -1, -1);
2463
142
                to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2464
142
                break;
2465
142
            }
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
2.42k
            if (in[0].first == OP_BOOLAND) {
2473
2.41k
                ++in;
2474
2.41k
                to_parse.emplace_back(DecodeContext::AND_B, -1, -1);
2475
2.41k
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2476
2.41k
                to_parse.emplace_back(DecodeContext::W_EXPR, -1, -1);
2477
2.41k
                break;
2478
2.41k
            }
2479
            // or_b
2480
9
            if (in[0].first == OP_BOOLOR) {
2481
8
                ++in;
2482
8
                to_parse.emplace_back(DecodeContext::OR_B, -1, -1);
2483
8
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2484
8
                to_parse.emplace_back(DecodeContext::W_EXPR, -1, -1);
2485
8
                break;
2486
8
            }
2487
            // Unrecognised expression
2488
1
            return {};
2489
9
        }
2490
2.90k
        case DecodeContext::BKV_EXPR: {
2491
2.90k
            to_parse.emplace_back(DecodeContext::MAYBE_AND_V, -1, -1);
2492
2.90k
            to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2493
2.90k
            break;
2494
9
        }
2495
2.45k
        case DecodeContext::W_EXPR: {
2496
            // a: wrapper
2497
2.45k
            if (in >= last) return {};
2498
2.45k
            if (in[0].first == OP_FROMALTSTACK) {
2499
2.44k
                ++in;
2500
2.44k
                to_parse.emplace_back(DecodeContext::ALT, -1, -1);
2501
2.44k
            } else {
2502
10
                to_parse.emplace_back(DecodeContext::SWAP, -1, -1);
2503
10
            }
2504
2.45k
            to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2505
2.45k
            break;
2506
2.45k
        }
2507
2.89k
        case DecodeContext::MAYBE_AND_V: {
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
2.89k
            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) {
2511
67
                to_parse.emplace_back(DecodeContext::AND_V, -1, -1);
2512
                // BKV_EXPR can contain more AND_V nodes
2513
67
                to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2514
67
            }
2515
2.89k
            break;
2516
2.45k
        }
2517
10
        case DecodeContext::SWAP: {
2518
10
            if (in >= last || in[0].first != OP_SWAP || constructed.empty()) return {};
2519
10
            ++in;
2520
10
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_S, Vector(std::move(constructed.back()))};
2521
10
            break;
2522
10
        }
2523
2.44k
        case DecodeContext::ALT: {
2524
2.44k
            if (in >= last || in[0].first != OP_TOALTSTACK || constructed.empty()) return {};
2525
2.44k
            ++in;
2526
2.44k
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_A, Vector(std::move(constructed.back()))};
2527
2.44k
            break;
2528
2.44k
        }
2529
464
        case DecodeContext::CHECK: {
2530
464
            if (constructed.empty()) return {};
2531
464
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_C, Vector(std::move(constructed.back()))};
2532
464
            break;
2533
464
        }
2534
5
        case DecodeContext::DUP_IF: {
2535
5
            if (constructed.empty()) return {};
2536
5
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_D, Vector(std::move(constructed.back()))};
2537
5
            break;
2538
5
        }
2539
81
        case DecodeContext::VERIFY: {
2540
81
            if (constructed.empty()) return {};
2541
81
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_V, Vector(std::move(constructed.back()))};
2542
81
            break;
2543
81
        }
2544
8
        case DecodeContext::NON_ZERO: {
2545
8
            if (constructed.empty()) return {};
2546
8
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_J, Vector(std::move(constructed.back()))};
2547
8
            break;
2548
8
        }
2549
15
        case DecodeContext::ZERO_NOTEQUAL: {
2550
15
            if (constructed.empty()) return {};
2551
15
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_N, Vector(std::move(constructed.back()))};
2552
15
            break;
2553
15
        }
2554
66
        case DecodeContext::AND_V: {
2555
66
            if (constructed.size() < 2) return {};
2556
66
            BuildBack(ctx.MsContext(), Fragment::AND_V, constructed, /*reverse=*/true);
2557
66
            break;
2558
66
        }
2559
2.41k
        case DecodeContext::AND_B: {
2560
2.41k
            if (constructed.size() < 2) return {};
2561
2.41k
            BuildBack(ctx.MsContext(), Fragment::AND_B, constructed, /*reverse=*/true);
2562
2.41k
            break;
2563
2.41k
        }
2564
8
        case DecodeContext::OR_B: {
2565
8
            if (constructed.size() < 2) return {};
2566
8
            BuildBack(ctx.MsContext(), Fragment::OR_B, constructed, /*reverse=*/true);
2567
8
            break;
2568
8
        }
2569
6
        case DecodeContext::OR_C: {
2570
6
            if (constructed.size() < 2) return {};
2571
6
            BuildBack(ctx.MsContext(), Fragment::OR_C, constructed, /*reverse=*/true);
2572
6
            break;
2573
6
        }
2574
15
        case DecodeContext::OR_D: {
2575
15
            if (constructed.size() < 2) return {};
2576
15
            BuildBack(ctx.MsContext(), Fragment::OR_D, constructed, /*reverse=*/true);
2577
15
            break;
2578
15
        }
2579
29
        case DecodeContext::ANDOR: {
2580
29
            if (constructed.size() < 3) return {};
2581
29
            Node left{std::move(constructed.back())};
2582
29
            constructed.pop_back();
2583
29
            Node right{std::move(constructed.back())};
2584
29
            constructed.pop_back();
2585
29
            Node mid{std::move(constructed.back())};
2586
29
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::ANDOR, Vector(std::move(left), std::move(mid), std::move(right))};
2587
29
            break;
2588
29
        }
2589
46
        case DecodeContext::THRESH_W: {
2590
46
            if (in >= last) return {};
2591
46
            if (in[0].first == OP_ADD) {
2592
30
                ++in;
2593
30
                to_parse.emplace_back(DecodeContext::THRESH_W, n+1, k);
2594
30
                to_parse.emplace_back(DecodeContext::W_EXPR, -1, -1);
2595
30
            } else {
2596
16
                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
16
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2599
16
            }
2600
46
            break;
2601
46
        }
2602
16
        case DecodeContext::THRESH_E: {
2603
16
            if (k < 1 || k > n || constructed.size() < static_cast<size_t>(n)) return {};
2604
16
            std::vector<Node<Key>> subs;
2605
62
            for (int i = 0; i < n; ++i) {
2606
46
                Node sub{std::move(constructed.back())};
2607
46
                constructed.pop_back();
2608
46
                subs.push_back(std::move(sub));
2609
46
            }
2610
16
            constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::THRESH, std::move(subs), k);
2611
16
            break;
2612
16
        }
2613
142
        case DecodeContext::ENDIF: {
2614
142
            if (in >= last) return {};
2615
2616
            // could be andor or or_i
2617
142
            if (in[0].first == OP_ELSE) {
2618
108
                ++in;
2619
108
                to_parse.emplace_back(DecodeContext::ENDIF_ELSE, -1, -1);
2620
108
                to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2621
108
            }
2622
            // could be j: or d: wrapper
2623
34
            else if (in[0].first == OP_IF) {
2624
13
                if (last - in >= 2 && in[1].first == OP_DUP) {
2625
5
                    in += 2;
2626
5
                    to_parse.emplace_back(DecodeContext::DUP_IF, -1, -1);
2627
8
                } else if (last - in >= 3 && in[1].first == OP_0NOTEQUAL && in[2].first == OP_SIZE) {
2628
8
                    in += 3;
2629
8
                    to_parse.emplace_back(DecodeContext::NON_ZERO, -1, -1);
2630
8
                }
2631
0
                else {
2632
0
                    return {};
2633
0
                }
2634
            // could be or_c or or_d
2635
21
            } else if (in[0].first == OP_NOTIF) {
2636
21
                ++in;
2637
21
                to_parse.emplace_back(DecodeContext::ENDIF_NOTIF, -1, -1);
2638
21
            }
2639
0
            else {
2640
0
                return {};
2641
0
            }
2642
142
            break;
2643
142
        }
2644
142
        case DecodeContext::ENDIF_NOTIF: {
2645
21
            if (in >= last) return {};
2646
21
            if (in[0].first == OP_IFDUP) {
2647
15
                ++in;
2648
15
                to_parse.emplace_back(DecodeContext::OR_D, -1, -1);
2649
15
            } else {
2650
6
                to_parse.emplace_back(DecodeContext::OR_C, -1, -1);
2651
6
            }
2652
            // or_c and or_d both require X to have type modifier d so, can't contain and_v
2653
21
            to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2654
21
            break;
2655
21
        }
2656
108
        case DecodeContext::ENDIF_ELSE: {
2657
108
            if (in >= last) return {};
2658
108
            if (in[0].first == OP_IF) {
2659
79
                ++in;
2660
79
                BuildBack(ctx.MsContext(), Fragment::OR_I, constructed, /*reverse=*/true);
2661
79
            } else if (in[0].first == OP_NOTIF) {
2662
29
                ++in;
2663
29
                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
29
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2666
29
            } else {
2667
0
                return {};
2668
0
            }
2669
108
            break;
2670
108
        }
2671
20.1k
        }
2672
20.1k
    }
2673
125
    if (constructed.size() != 1) return {};
2674
125
    Node tl_node{std::move(constructed.front())};
2675
125
    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
125
    if (!tl_node.IsValidTopLevel()) return {};
2679
125
    return tl_node;
2680
125
}
descriptor.cpp:std::optional<miniscript::Node<unsigned int>> miniscript::internal::DecodeScript<unsigned int, (anonymous namespace)::KeyParser, __gnu_cxx::__normal_iterator<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>*, std::vector<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>, std::allocator<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>>>>>(__gnu_cxx::__normal_iterator<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>*, std::vector<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>, std::allocator<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>>>>&, __gnu_cxx::__normal_iterator<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>*, std::vector<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>, std::allocator<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>>>>, (anonymous namespace)::KeyParser const&)
Line
Count
Source
2297
702
{
2298
    // The two integers are used to hold state for thresh()
2299
702
    std::vector<std::tuple<DecodeContext, int64_t, int64_t>> to_parse;
2300
702
    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
702
    to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2305
2306
1.33M
    while (!to_parse.empty()) {
2307
        // Exit early if the Miniscript is not going to be valid.
2308
1.33M
        if (!constructed.empty() && !constructed.back().IsValid()) return {};
2309
2310
        // Get the current context we are decoding within
2311
1.33M
        auto [cur_context, n, k] = to_parse.back();
2312
1.33M
        to_parse.pop_back();
2313
2314
1.33M
        switch(cur_context) {
2315
664k
        case DecodeContext::SINGLE_BKV_EXPR: {
2316
664k
            if (in >= last) return {};
2317
2318
            // Constants
2319
664k
            if (in[0].first == OP_1) {
2320
3
                ++in;
2321
3
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_1);
2322
3
                break;
2323
3
            }
2324
664k
            if (in[0].first == OP_0) {
2325
193
                ++in;
2326
193
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_0);
2327
193
                break;
2328
193
            }
2329
            // Public keys
2330
664k
            if (in[0].second.size() == 33 || in[0].second.size() == 32) {
2331
1.11k
                auto key = ctx.FromPKBytes(in[0].second.begin(), in[0].second.end());
2332
1.11k
                if (!key) return {};
2333
1.11k
                ++in;
2334
1.11k
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_K, Vector(std::move(*key)));
2335
1.11k
                break;
2336
1.11k
            }
2337
662k
            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) {
2338
344
                auto key = ctx.FromPKHBytes(in[2].second.begin(), in[2].second.end());
2339
344
                if (!key) return {};
2340
342
                in += 5;
2341
342
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_H, Vector(std::move(*key)));
2342
342
                break;
2343
344
            }
2344
            // Time locks
2345
662k
            std::optional<int64_t> num;
2346
662k
            if (last - in >= 2 && in[0].first == OP_CHECKSEQUENCEVERIFY && (num = ParseScriptNumber(in[1]))) {
2347
202
                in += 2;
2348
202
                if (*num < 1 || *num > 0x7FFFFFFFL) return {};
2349
202
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::OLDER, *num);
2350
202
                break;
2351
202
            }
2352
662k
            if (last - in >= 2 && in[0].first == OP_CHECKLOCKTIMEVERIFY && (num = ParseScriptNumber(in[1]))) {
2353
234
                in += 2;
2354
234
                if (num < 1 || num > 0x7FFFFFFFL) return {};
2355
234
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::AFTER, *num);
2356
234
                break;
2357
234
            }
2358
            // Hashes
2359
662k
            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) {
2360
157
                if (in[2].first == OP_SHA256 && in[1].second.size() == 32) {
2361
28
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::SHA256, in[1].second);
2362
28
                    in += 7;
2363
28
                    break;
2364
129
                } else if (in[2].first == OP_RIPEMD160 && in[1].second.size() == 20) {
2365
36
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::RIPEMD160, in[1].second);
2366
36
                    in += 7;
2367
36
                    break;
2368
93
                } else if (in[2].first == OP_HASH256 && in[1].second.size() == 32) {
2369
48
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::HASH256, in[1].second);
2370
48
                    in += 7;
2371
48
                    break;
2372
48
                } else if (in[2].first == OP_HASH160 && in[1].second.size() == 20) {
2373
45
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::HASH160, in[1].second);
2374
45
                    in += 7;
2375
45
                    break;
2376
45
                }
2377
157
            }
2378
            // Multi
2379
662k
            if (last - in >= 3 && in[0].first == OP_CHECKMULTISIG) {
2380
98
                if (IsTapscript(ctx.MsContext())) return {};
2381
98
                std::vector<Key> keys;
2382
98
                const auto n = ParseScriptNumber(in[1]);
2383
98
                if (!n || last - in < 3 + *n) return {};
2384
98
                if (*n < 1 || *n > 20) return {};
2385
344
                for (int i = 0; i < *n; ++i) {
2386
246
                    if (in[2 + i].second.size() != 33) return {};
2387
246
                    auto key = ctx.FromPKBytes(in[2 + i].second.begin(), in[2 + i].second.end());
2388
246
                    if (!key) return {};
2389
246
                    keys.push_back(std::move(*key));
2390
246
                }
2391
98
                const auto k = ParseScriptNumber(in[2 + *n]);
2392
98
                if (!k || *k < 1 || *k > *n) return {};
2393
98
                in += 3 + *n;
2394
98
                std::reverse(keys.begin(), keys.end());
2395
98
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::MULTI, std::move(keys), *k);
2396
98
                break;
2397
98
            }
2398
            // Tapscript's equivalent of multi
2399
661k
            if (last - in >= 4 && in[0].first == OP_NUMEQUAL) {
2400
4
                if (!IsTapscript(ctx.MsContext())) return {};
2401
                // The necessary threshold of signatures.
2402
4
                const auto k = ParseScriptNumber(in[1]);
2403
4
                if (!k) return {};
2404
4
                if (*k < 1 || *k > MAX_PUBKEYS_PER_MULTI_A) return {};
2405
4
                if (last - in < 2 + *k * 2) return {};
2406
4
                std::vector<Key> keys;
2407
4
                keys.reserve(*k);
2408
                // Walk through the expected (pubkey, CHECKSIG[ADD]) pairs.
2409
8
                for (int pos = 2;; pos += 2) {
2410
8
                    if (last - in < pos + 2) return {};
2411
                    // Make sure it's indeed an x-only pubkey and a CHECKSIG[ADD], then parse the key.
2412
8
                    if (in[pos].first != OP_CHECKSIGADD && in[pos].first != OP_CHECKSIG) return {};
2413
8
                    if (in[pos + 1].second.size() != 32) return {};
2414
8
                    auto key = ctx.FromPKBytes(in[pos + 1].second.begin(), in[pos + 1].second.end());
2415
8
                    if (!key) return {};
2416
8
                    keys.push_back(std::move(*key));
2417
                    // Make sure early we don't parse an arbitrary large expression.
2418
8
                    if (keys.size() > MAX_PUBKEYS_PER_MULTI_A) return {};
2419
                    // OP_CHECKSIG means it was the last one to parse.
2420
8
                    if (in[pos].first == OP_CHECKSIG) break;
2421
8
                }
2422
4
                if (keys.size() < (size_t)*k) return {};
2423
4
                in += 2 + keys.size() * 2;
2424
4
                std::reverse(keys.begin(), keys.end());
2425
4
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::MULTI_A, std::move(keys), *k);
2426
4
                break;
2427
4
            }
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
661k
            if (in[0].first == OP_CHECKSIG) {
2433
1.44k
                ++in;
2434
1.44k
                to_parse.emplace_back(DecodeContext::CHECK, -1, -1);
2435
1.44k
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2436
1.44k
                break;
2437
1.44k
            }
2438
            // v: wrapper
2439
660k
            if (in[0].first == OP_VERIFY) {
2440
574
                ++in;
2441
574
                to_parse.emplace_back(DecodeContext::VERIFY, -1, -1);
2442
574
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2443
574
                break;
2444
574
            }
2445
            // n: wrapper
2446
659k
            if (in[0].first == OP_0NOTEQUAL) {
2447
659k
                ++in;
2448
659k
                to_parse.emplace_back(DecodeContext::ZERO_NOTEQUAL, -1, -1);
2449
659k
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2450
659k
                break;
2451
659k
            }
2452
            // Thresh
2453
843
            if (last - in >= 3 && in[0].first == OP_EQUAL && (num = ParseScriptNumber(in[1]))) {
2454
188
                if (*num < 1) return {};
2455
188
                in += 2;
2456
188
                to_parse.emplace_back(DecodeContext::THRESH_W, 0, *num);
2457
188
                break;
2458
188
            }
2459
            // OP_ENDIF can be WRAP_J, WRAP_D, ANDOR, OR_C, OR_D, or OR_I
2460
655
            if (in[0].first == OP_ENDIF) {
2461
405
                ++in;
2462
405
                to_parse.emplace_back(DecodeContext::ENDIF, -1, -1);
2463
405
                to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2464
405
                break;
2465
405
            }
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
250
            if (in[0].first == OP_BOOLAND) {
2473
216
                ++in;
2474
216
                to_parse.emplace_back(DecodeContext::AND_B, -1, -1);
2475
216
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2476
216
                to_parse.emplace_back(DecodeContext::W_EXPR, -1, -1);
2477
216
                break;
2478
216
            }
2479
            // or_b
2480
34
            if (in[0].first == OP_BOOLOR) {
2481
26
                ++in;
2482
26
                to_parse.emplace_back(DecodeContext::OR_B, -1, -1);
2483
26
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2484
26
                to_parse.emplace_back(DecodeContext::W_EXPR, -1, -1);
2485
26
                break;
2486
26
            }
2487
            // Unrecognised expression
2488
8
            return {};
2489
34
        }
2490
2.60k
        case DecodeContext::BKV_EXPR: {
2491
2.60k
            to_parse.emplace_back(DecodeContext::MAYBE_AND_V, -1, -1);
2492
2.60k
            to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2493
2.60k
            break;
2494
34
        }
2495
692
        case DecodeContext::W_EXPR: {
2496
            // a: wrapper
2497
692
            if (in >= last) return {};
2498
692
            if (in[0].first == OP_FROMALTSTACK) {
2499
396
                ++in;
2500
396
                to_parse.emplace_back(DecodeContext::ALT, -1, -1);
2501
396
            } else {
2502
296
                to_parse.emplace_back(DecodeContext::SWAP, -1, -1);
2503
296
            }
2504
692
            to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2505
692
            break;
2506
692
        }
2507
2.59k
        case DecodeContext::MAYBE_AND_V: {
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
2.59k
            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) {
2511
525
                to_parse.emplace_back(DecodeContext::AND_V, -1, -1);
2512
                // BKV_EXPR can contain more AND_V nodes
2513
525
                to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2514
525
            }
2515
2.59k
            break;
2516
692
        }
2517
296
        case DecodeContext::SWAP: {
2518
296
            if (in >= last || in[0].first != OP_SWAP || constructed.empty()) return {};
2519
296
            ++in;
2520
296
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_S, Vector(std::move(constructed.back()))};
2521
296
            break;
2522
296
        }
2523
396
        case DecodeContext::ALT: {
2524
396
            if (in >= last || in[0].first != OP_TOALTSTACK || constructed.empty()) return {};
2525
396
            ++in;
2526
396
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_A, Vector(std::move(constructed.back()))};
2527
396
            break;
2528
396
        }
2529
1.44k
        case DecodeContext::CHECK: {
2530
1.44k
            if (constructed.empty()) return {};
2531
1.44k
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_C, Vector(std::move(constructed.back()))};
2532
1.44k
            break;
2533
1.44k
        }
2534
52
        case DecodeContext::DUP_IF: {
2535
52
            if (constructed.empty()) return {};
2536
52
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_D, Vector(std::move(constructed.back()))};
2537
52
            break;
2538
52
        }
2539
574
        case DecodeContext::VERIFY: {
2540
574
            if (constructed.empty()) return {};
2541
574
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_V, Vector(std::move(constructed.back()))};
2542
574
            break;
2543
574
        }
2544
0
        case DecodeContext::NON_ZERO: {
2545
0
            if (constructed.empty()) return {};
2546
0
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_J, Vector(std::move(constructed.back()))};
2547
0
            break;
2548
0
        }
2549
659k
        case DecodeContext::ZERO_NOTEQUAL: {
2550
659k
            if (constructed.empty()) return {};
2551
659k
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_N, Vector(std::move(constructed.back()))};
2552
659k
            break;
2553
659k
        }
2554
523
        case DecodeContext::AND_V: {
2555
523
            if (constructed.size() < 2) return {};
2556
523
            BuildBack(ctx.MsContext(), Fragment::AND_V, constructed, /*reverse=*/true);
2557
523
            break;
2558
523
        }
2559
216
        case DecodeContext::AND_B: {
2560
216
            if (constructed.size() < 2) return {};
2561
216
            BuildBack(ctx.MsContext(), Fragment::AND_B, constructed, /*reverse=*/true);
2562
216
            break;
2563
216
        }
2564
26
        case DecodeContext::OR_B: {
2565
26
            if (constructed.size() < 2) return {};
2566
26
            BuildBack(ctx.MsContext(), Fragment::OR_B, constructed, /*reverse=*/true);
2567
26
            break;
2568
26
        }
2569
20
        case DecodeContext::OR_C: {
2570
20
            if (constructed.size() < 2) return {};
2571
20
            BuildBack(ctx.MsContext(), Fragment::OR_C, constructed, /*reverse=*/true);
2572
20
            break;
2573
20
        }
2574
47
        case DecodeContext::OR_D: {
2575
47
            if (constructed.size() < 2) return {};
2576
47
            BuildBack(ctx.MsContext(), Fragment::OR_D, constructed, /*reverse=*/true);
2577
47
            break;
2578
47
        }
2579
87
        case DecodeContext::ANDOR: {
2580
87
            if (constructed.size() < 3) return {};
2581
87
            Node left{std::move(constructed.back())};
2582
87
            constructed.pop_back();
2583
87
            Node right{std::move(constructed.back())};
2584
87
            constructed.pop_back();
2585
87
            Node mid{std::move(constructed.back())};
2586
87
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::ANDOR, Vector(std::move(left), std::move(mid), std::move(right))};
2587
87
            break;
2588
87
        }
2589
638
        case DecodeContext::THRESH_W: {
2590
638
            if (in >= last) return {};
2591
638
            if (in[0].first == OP_ADD) {
2592
450
                ++in;
2593
450
                to_parse.emplace_back(DecodeContext::THRESH_W, n+1, k);
2594
450
                to_parse.emplace_back(DecodeContext::W_EXPR, -1, -1);
2595
450
            } else {
2596
188
                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
188
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2599
188
            }
2600
638
            break;
2601
638
        }
2602
188
        case DecodeContext::THRESH_E: {
2603
188
            if (k < 1 || k > n || constructed.size() < static_cast<size_t>(n)) return {};
2604
188
            std::vector<Node<Key>> subs;
2605
826
            for (int i = 0; i < n; ++i) {
2606
638
                Node sub{std::move(constructed.back())};
2607
638
                constructed.pop_back();
2608
638
                subs.push_back(std::move(sub));
2609
638
            }
2610
188
            constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::THRESH, std::move(subs), k);
2611
188
            break;
2612
188
        }
2613
404
        case DecodeContext::ENDIF: {
2614
404
            if (in >= last) return {};
2615
2616
            // could be andor or or_i
2617
404
            if (in[0].first == OP_ELSE) {
2618
285
                ++in;
2619
285
                to_parse.emplace_back(DecodeContext::ENDIF_ELSE, -1, -1);
2620
285
                to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2621
285
            }
2622
            // could be j: or d: wrapper
2623
119
            else if (in[0].first == OP_IF) {
2624
52
                if (last - in >= 2 && in[1].first == OP_DUP) {
2625
52
                    in += 2;
2626
52
                    to_parse.emplace_back(DecodeContext::DUP_IF, -1, -1);
2627
52
                } else if (last - in >= 3 && in[1].first == OP_0NOTEQUAL && in[2].first == OP_SIZE) {
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
67
            } else if (in[0].first == OP_NOTIF) {
2636
67
                ++in;
2637
67
                to_parse.emplace_back(DecodeContext::ENDIF_NOTIF, -1, -1);
2638
67
            }
2639
0
            else {
2640
0
                return {};
2641
0
            }
2642
404
            break;
2643
404
        }
2644
404
        case DecodeContext::ENDIF_NOTIF: {
2645
67
            if (in >= last) return {};
2646
67
            if (in[0].first == OP_IFDUP) {
2647
47
                ++in;
2648
47
                to_parse.emplace_back(DecodeContext::OR_D, -1, -1);
2649
47
            } else {
2650
20
                to_parse.emplace_back(DecodeContext::OR_C, -1, -1);
2651
20
            }
2652
            // or_c and or_d both require X to have type modifier d so, can't contain and_v
2653
67
            to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2654
67
            break;
2655
67
        }
2656
285
        case DecodeContext::ENDIF_ELSE: {
2657
285
            if (in >= last) return {};
2658
285
            if (in[0].first == OP_IF) {
2659
198
                ++in;
2660
198
                BuildBack(ctx.MsContext(), Fragment::OR_I, constructed, /*reverse=*/true);
2661
198
            } else if (in[0].first == OP_NOTIF) {
2662
87
                ++in;
2663
87
                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
87
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2666
87
            } else {
2667
0
                return {};
2668
0
            }
2669
285
            break;
2670
285
        }
2671
1.33M
        }
2672
1.33M
    }
2673
690
    if (constructed.size() != 1) return {};
2674
690
    Node tl_node{std::move(constructed.front())};
2675
690
    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
690
    if (!tl_node.IsValidTopLevel()) return {};
2679
689
    return tl_node;
2680
690
}
std::optional<miniscript::Node<XOnlyPubKey>> miniscript::internal::DecodeScript<XOnlyPubKey, TapSatisfier, __gnu_cxx::__normal_iterator<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>*, std::vector<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>, std::allocator<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>>>>>(__gnu_cxx::__normal_iterator<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>*, std::vector<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>, std::allocator<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>>>>&, __gnu_cxx::__normal_iterator<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>*, std::vector<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>, std::allocator<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>>>>, TapSatisfier const&)
Line
Count
Source
2297
4.41k
{
2298
    // The two integers are used to hold state for thresh()
2299
4.41k
    std::vector<std::tuple<DecodeContext, int64_t, int64_t>> to_parse;
2300
4.41k
    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
4.41k
    to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2305
2306
2.66M
    while (!to_parse.empty()) {
2307
        // Exit early if the Miniscript is not going to be valid.
2308
2.66M
        if (!constructed.empty() && !constructed.back().IsValid()) return {};
2309
2310
        // Get the current context we are decoding within
2311
2.66M
        auto [cur_context, n, k] = to_parse.back();
2312
2.66M
        to_parse.pop_back();
2313
2314
2.66M
        switch(cur_context) {
2315
1.32M
        case DecodeContext::SINGLE_BKV_EXPR: {
2316
1.32M
            if (in >= last) return {};
2317
2318
            // Constants
2319
1.32M
            if (in[0].first == OP_1) {
2320
0
                ++in;
2321
0
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_1);
2322
0
                break;
2323
0
            }
2324
1.32M
            if (in[0].first == OP_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
1.32M
            if (in[0].second.size() == 33 || in[0].second.size() == 32) {
2331
3.59k
                auto key = ctx.FromPKBytes(in[0].second.begin(), in[0].second.end());
2332
3.59k
                if (!key) return {};
2333
3.59k
                ++in;
2334
3.59k
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_K, Vector(std::move(*key)));
2335
3.59k
                break;
2336
3.59k
            }
2337
1.32M
            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) {
2338
329
                auto key = ctx.FromPKHBytes(in[2].second.begin(), in[2].second.end());
2339
329
                if (!key) return {};
2340
329
                in += 5;
2341
329
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_H, Vector(std::move(*key)));
2342
329
                break;
2343
329
            }
2344
            // Time locks
2345
1.32M
            std::optional<int64_t> num;
2346
1.32M
            if (last - in >= 2 && in[0].first == OP_CHECKSEQUENCEVERIFY && (num = ParseScriptNumber(in[1]))) {
2347
55
                in += 2;
2348
55
                if (*num < 1 || *num > 0x7FFFFFFFL) return {};
2349
55
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::OLDER, *num);
2350
55
                break;
2351
55
            }
2352
1.32M
            if (last - in >= 2 && in[0].first == OP_CHECKLOCKTIMEVERIFY && (num = ParseScriptNumber(in[1]))) {
2353
741
                in += 2;
2354
741
                if (num < 1 || num > 0x7FFFFFFFL) return {};
2355
741
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::AFTER, *num);
2356
741
                break;
2357
741
            }
2358
            // Hashes
2359
1.32M
            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) {
2360
12
                if (in[2].first == OP_SHA256 && in[1].second.size() == 32) {
2361
0
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::SHA256, in[1].second);
2362
0
                    in += 7;
2363
0
                    break;
2364
12
                } else if (in[2].first == OP_RIPEMD160 && in[1].second.size() == 20) {
2365
0
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::RIPEMD160, in[1].second);
2366
0
                    in += 7;
2367
0
                    break;
2368
12
                } else if (in[2].first == OP_HASH256 && in[1].second.size() == 32) {
2369
12
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::HASH256, in[1].second);
2370
12
                    in += 7;
2371
12
                    break;
2372
12
                } else if (in[2].first == OP_HASH160 && in[1].second.size() == 20) {
2373
0
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::HASH160, in[1].second);
2374
0
                    in += 7;
2375
0
                    break;
2376
0
                }
2377
12
            }
2378
            // Multi
2379
1.32M
            if (last - in >= 3 && in[0].first == OP_CHECKMULTISIG) {
2380
0
                if (IsTapscript(ctx.MsContext())) return {};
2381
0
                std::vector<Key> keys;
2382
0
                const auto n = ParseScriptNumber(in[1]);
2383
0
                if (!n || last - in < 3 + *n) return {};
2384
0
                if (*n < 1 || *n > 20) return {};
2385
0
                for (int i = 0; i < *n; ++i) {
2386
0
                    if (in[2 + i].second.size() != 33) return {};
2387
0
                    auto key = ctx.FromPKBytes(in[2 + i].second.begin(), in[2 + i].second.end());
2388
0
                    if (!key) return {};
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 {};
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
1.32M
            if (last - in >= 4 && in[0].first == OP_NUMEQUAL) {
2400
800
                if (!IsTapscript(ctx.MsContext())) return {};
2401
                // The necessary threshold of signatures.
2402
800
                const auto k = ParseScriptNumber(in[1]);
2403
800
                if (!k) return {};
2404
800
                if (*k < 1 || *k > MAX_PUBKEYS_PER_MULTI_A) return {};
2405
800
                if (last - in < 2 + *k * 2) return {};
2406
800
                std::vector<Key> keys;
2407
800
                keys.reserve(*k);
2408
                // Walk through the expected (pubkey, CHECKSIG[ADD]) pairs.
2409
93.5k
                for (int pos = 2;; pos += 2) {
2410
93.5k
                    if (last - in < pos + 2) return {};
2411
                    // Make sure it's indeed an x-only pubkey and a CHECKSIG[ADD], then parse the key.
2412
93.5k
                    if (in[pos].first != OP_CHECKSIGADD && in[pos].first != OP_CHECKSIG) return {};
2413
93.5k
                    if (in[pos + 1].second.size() != 32) return {};
2414
93.5k
                    auto key = ctx.FromPKBytes(in[pos + 1].second.begin(), in[pos + 1].second.end());
2415
93.5k
                    if (!key) return {};
2416
93.5k
                    keys.push_back(std::move(*key));
2417
                    // Make sure early we don't parse an arbitrary large expression.
2418
93.5k
                    if (keys.size() > MAX_PUBKEYS_PER_MULTI_A) return {};
2419
                    // OP_CHECKSIG means it was the last one to parse.
2420
93.5k
                    if (in[pos].first == OP_CHECKSIG) break;
2421
93.5k
                }
2422
800
                if (keys.size() < (size_t)*k) return {};
2423
800
                in += 2 + keys.size() * 2;
2424
800
                std::reverse(keys.begin(), keys.end());
2425
800
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::MULTI_A, std::move(keys), *k);
2426
800
                break;
2427
800
            }
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
1.32M
            if (in[0].first == OP_CHECKSIG) {
2433
3.92k
                ++in;
2434
3.92k
                to_parse.emplace_back(DecodeContext::CHECK, -1, -1);
2435
3.92k
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2436
3.92k
                break;
2437
3.92k
            }
2438
            // v: wrapper
2439
1.31M
            if (in[0].first == OP_VERIFY) {
2440
975
                ++in;
2441
975
                to_parse.emplace_back(DecodeContext::VERIFY, -1, -1);
2442
975
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2443
975
                break;
2444
975
            }
2445
            // n: wrapper
2446
1.31M
            if (in[0].first == OP_0NOTEQUAL) {
2447
1.31M
                ++in;
2448
1.31M
                to_parse.emplace_back(DecodeContext::ZERO_NOTEQUAL, -1, -1);
2449
1.31M
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2450
1.31M
                break;
2451
1.31M
            }
2452
            // Thresh
2453
128
            if (last - in >= 3 && in[0].first == OP_EQUAL && (num = ParseScriptNumber(in[1]))) {
2454
24
                if (*num < 1) return {};
2455
24
                in += 2;
2456
24
                to_parse.emplace_back(DecodeContext::THRESH_W, 0, *num);
2457
24
                break;
2458
24
            }
2459
            // OP_ENDIF can be WRAP_J, WRAP_D, ANDOR, OR_C, OR_D, or OR_I
2460
104
            if (in[0].first == OP_ENDIF) {
2461
6
                ++in;
2462
6
                to_parse.emplace_back(DecodeContext::ENDIF, -1, -1);
2463
6
                to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2464
6
                break;
2465
6
            }
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
98
            if (in[0].first == OP_BOOLAND) {
2473
72
                ++in;
2474
72
                to_parse.emplace_back(DecodeContext::AND_B, -1, -1);
2475
72
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2476
72
                to_parse.emplace_back(DecodeContext::W_EXPR, -1, -1);
2477
72
                break;
2478
72
            }
2479
            // or_b
2480
26
            if (in[0].first == OP_BOOLOR) {
2481
26
                ++in;
2482
26
                to_parse.emplace_back(DecodeContext::OR_B, -1, -1);
2483
26
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2484
26
                to_parse.emplace_back(DecodeContext::W_EXPR, -1, -1);
2485
26
                break;
2486
26
            }
2487
            // Unrecognised expression
2488
0
            return {};
2489
26
        }
2490
5.53k
        case DecodeContext::BKV_EXPR: {
2491
5.53k
            to_parse.emplace_back(DecodeContext::MAYBE_AND_V, -1, -1);
2492
5.53k
            to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2493
5.53k
            break;
2494
26
        }
2495
146
        case DecodeContext::W_EXPR: {
2496
            // a: wrapper
2497
146
            if (in >= last) return {};
2498
146
            if (in[0].first == OP_FROMALTSTACK) {
2499
96
                ++in;
2500
96
                to_parse.emplace_back(DecodeContext::ALT, -1, -1);
2501
96
            } else {
2502
50
                to_parse.emplace_back(DecodeContext::SWAP, -1, -1);
2503
50
            }
2504
146
            to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2505
146
            break;
2506
146
        }
2507
5.53k
        case DecodeContext::MAYBE_AND_V: {
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
5.53k
            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) {
2511
969
                to_parse.emplace_back(DecodeContext::AND_V, -1, -1);
2512
                // BKV_EXPR can contain more AND_V nodes
2513
969
                to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2514
969
            }
2515
5.53k
            break;
2516
146
        }
2517
50
        case DecodeContext::SWAP: {
2518
50
            if (in >= last || in[0].first != OP_SWAP || constructed.empty()) return {};
2519
50
            ++in;
2520
50
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_S, Vector(std::move(constructed.back()))};
2521
50
            break;
2522
50
        }
2523
96
        case DecodeContext::ALT: {
2524
96
            if (in >= last || in[0].first != OP_TOALTSTACK || constructed.empty()) return {};
2525
96
            ++in;
2526
96
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_A, Vector(std::move(constructed.back()))};
2527
96
            break;
2528
96
        }
2529
3.92k
        case DecodeContext::CHECK: {
2530
3.92k
            if (constructed.empty()) return {};
2531
3.92k
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_C, Vector(std::move(constructed.back()))};
2532
3.92k
            break;
2533
3.92k
        }
2534
6
        case DecodeContext::DUP_IF: {
2535
6
            if (constructed.empty()) return {};
2536
6
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_D, Vector(std::move(constructed.back()))};
2537
6
            break;
2538
6
        }
2539
975
        case DecodeContext::VERIFY: {
2540
975
            if (constructed.empty()) return {};
2541
975
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_V, Vector(std::move(constructed.back()))};
2542
975
            break;
2543
975
        }
2544
0
        case DecodeContext::NON_ZERO: {
2545
0
            if (constructed.empty()) return {};
2546
0
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_J, Vector(std::move(constructed.back()))};
2547
0
            break;
2548
0
        }
2549
1.31M
        case DecodeContext::ZERO_NOTEQUAL: {
2550
1.31M
            if (constructed.empty()) return {};
2551
1.31M
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_N, Vector(std::move(constructed.back()))};
2552
1.31M
            break;
2553
1.31M
        }
2554
969
        case DecodeContext::AND_V: {
2555
969
            if (constructed.size() < 2) return {};
2556
969
            BuildBack(ctx.MsContext(), Fragment::AND_V, constructed, /*reverse=*/true);
2557
969
            break;
2558
969
        }
2559
72
        case DecodeContext::AND_B: {
2560
72
            if (constructed.size() < 2) return {};
2561
72
            BuildBack(ctx.MsContext(), Fragment::AND_B, constructed, /*reverse=*/true);
2562
72
            break;
2563
72
        }
2564
26
        case DecodeContext::OR_B: {
2565
26
            if (constructed.size() < 2) return {};
2566
26
            BuildBack(ctx.MsContext(), Fragment::OR_B, constructed, /*reverse=*/true);
2567
26
            break;
2568
26
        }
2569
0
        case DecodeContext::OR_C: {
2570
0
            if (constructed.size() < 2) return {};
2571
0
            BuildBack(ctx.MsContext(), Fragment::OR_C, constructed, /*reverse=*/true);
2572
0
            break;
2573
0
        }
2574
0
        case DecodeContext::OR_D: {
2575
0
            if (constructed.size() < 2) return {};
2576
0
            BuildBack(ctx.MsContext(), Fragment::OR_D, constructed, /*reverse=*/true);
2577
0
            break;
2578
0
        }
2579
0
        case DecodeContext::ANDOR: {
2580
0
            if (constructed.size() < 3) return {};
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
72
        case DecodeContext::THRESH_W: {
2590
72
            if (in >= last) return {};
2591
72
            if (in[0].first == OP_ADD) {
2592
48
                ++in;
2593
48
                to_parse.emplace_back(DecodeContext::THRESH_W, n+1, k);
2594
48
                to_parse.emplace_back(DecodeContext::W_EXPR, -1, -1);
2595
48
            } else {
2596
24
                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
24
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2599
24
            }
2600
72
            break;
2601
72
        }
2602
24
        case DecodeContext::THRESH_E: {
2603
24
            if (k < 1 || k > n || constructed.size() < static_cast<size_t>(n)) return {};
2604
24
            std::vector<Node<Key>> subs;
2605
96
            for (int i = 0; i < n; ++i) {
2606
72
                Node sub{std::move(constructed.back())};
2607
72
                constructed.pop_back();
2608
72
                subs.push_back(std::move(sub));
2609
72
            }
2610
24
            constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::THRESH, std::move(subs), k);
2611
24
            break;
2612
24
        }
2613
6
        case DecodeContext::ENDIF: {
2614
6
            if (in >= last) return {};
2615
2616
            // could be andor or or_i
2617
6
            if (in[0].first == OP_ELSE) {
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
6
            else if (in[0].first == OP_IF) {
2624
6
                if (last - in >= 2 && in[1].first == OP_DUP) {
2625
6
                    in += 2;
2626
6
                    to_parse.emplace_back(DecodeContext::DUP_IF, -1, -1);
2627
6
                } else if (last - in >= 3 && in[1].first == OP_0NOTEQUAL && in[2].first == OP_SIZE) {
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
6
            } else if (in[0].first == OP_NOTIF) {
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
6
            break;
2643
6
        }
2644
6
        case DecodeContext::ENDIF_NOTIF: {
2645
0
            if (in >= last) return {};
2646
0
            if (in[0].first == OP_IFDUP) {
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: {
2657
0
            if (in >= last) return {};
2658
0
            if (in[0].first == OP_IF) {
2659
0
                ++in;
2660
0
                BuildBack(ctx.MsContext(), Fragment::OR_I, constructed, /*reverse=*/true);
2661
0
            } else if (in[0].first == OP_NOTIF) {
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
2.66M
        }
2672
2.66M
    }
2673
4.41k
    if (constructed.size() != 1) return {};
2674
4.41k
    Node tl_node{std::move(constructed.front())};
2675
4.41k
    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
4.41k
    if (!tl_node.IsValidTopLevel()) return {};
2679
4.41k
    return tl_node;
2680
4.41k
}
std::optional<miniscript::Node<CPubKey>> miniscript::internal::DecodeScript<CPubKey, WshSatisfier, __gnu_cxx::__normal_iterator<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>*, std::vector<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>, std::allocator<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>>>>>(__gnu_cxx::__normal_iterator<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>*, std::vector<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>, std::allocator<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>>>>&, __gnu_cxx::__normal_iterator<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>*, std::vector<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>, std::allocator<std::pair<opcodetype, std::vector<unsigned char, std::allocator<unsigned char>>>>>>, WshSatisfier const&)
Line
Count
Source
2297
237
{
2298
    // The two integers are used to hold state for thresh()
2299
237
    std::vector<std::tuple<DecodeContext, int64_t, int64_t>> to_parse;
2300
237
    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
237
    to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2305
2306
9.37k
    while (!to_parse.empty()) {
2307
        // Exit early if the Miniscript is not going to be valid.
2308
9.13k
        if (!constructed.empty() && !constructed.back().IsValid()) return {};
2309
2310
        // Get the current context we are decoding within
2311
9.13k
        auto [cur_context, n, k] = to_parse.back();
2312
9.13k
        to_parse.pop_back();
2313
2314
9.13k
        switch(cur_context) {
2315
2.64k
        case DecodeContext::SINGLE_BKV_EXPR: {
2316
2.64k
            if (in >= last) return {};
2317
2318
            // Constants
2319
2.64k
            if (in[0].first == OP_1) {
2320
0
                ++in;
2321
0
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_1);
2322
0
                break;
2323
0
            }
2324
2.64k
            if (in[0].first == OP_0) {
2325
243
                ++in;
2326
243
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_0);
2327
243
                break;
2328
243
            }
2329
            // Public keys
2330
2.40k
            if (in[0].second.size() == 33 || in[0].second.size() == 32) {
2331
489
                auto key = ctx.FromPKBytes(in[0].second.begin(), in[0].second.end());
2332
489
                if (!key) return {};
2333
488
                ++in;
2334
488
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_K, Vector(std::move(*key)));
2335
488
                break;
2336
489
            }
2337
1.91k
            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) {
2338
84
                auto key = ctx.FromPKHBytes(in[2].second.begin(), in[2].second.end());
2339
84
                if (!key) return {};
2340
83
                in += 5;
2341
83
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_H, Vector(std::move(*key)));
2342
83
                break;
2343
84
            }
2344
            // Time locks
2345
1.82k
            std::optional<int64_t> num;
2346
1.82k
            if (last - in >= 2 && in[0].first == OP_CHECKSEQUENCEVERIFY && (num = ParseScriptNumber(in[1]))) {
2347
69
                in += 2;
2348
69
                if (*num < 1 || *num > 0x7FFFFFFFL) return {};
2349
69
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::OLDER, *num);
2350
69
                break;
2351
69
            }
2352
1.76k
            if (last - in >= 2 && in[0].first == OP_CHECKLOCKTIMEVERIFY && (num = ParseScriptNumber(in[1]))) {
2353
249
                in += 2;
2354
249
                if (num < 1 || num > 0x7FFFFFFFL) return {};
2355
249
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::AFTER, *num);
2356
249
                break;
2357
249
            }
2358
            // Hashes
2359
1.51k
            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) {
2360
53
                if (in[2].first == OP_SHA256 && in[1].second.size() == 32) {
2361
17
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::SHA256, in[1].second);
2362
17
                    in += 7;
2363
17
                    break;
2364
36
                } else if (in[2].first == OP_RIPEMD160 && in[1].second.size() == 20) {
2365
12
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::RIPEMD160, in[1].second);
2366
12
                    in += 7;
2367
12
                    break;
2368
24
                } else if (in[2].first == OP_HASH256 && in[1].second.size() == 32) {
2369
12
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::HASH256, in[1].second);
2370
12
                    in += 7;
2371
12
                    break;
2372
12
                } else if (in[2].first == OP_HASH160 && in[1].second.size() == 20) {
2373
12
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::HASH160, in[1].second);
2374
12
                    in += 7;
2375
12
                    break;
2376
12
                }
2377
53
            }
2378
            // Multi
2379
1.45k
            if (last - in >= 3 && in[0].first == OP_CHECKMULTISIG) {
2380
24
                if (IsTapscript(ctx.MsContext())) return {};
2381
24
                std::vector<Key> keys;
2382
24
                const auto n = ParseScriptNumber(in[1]);
2383
24
                if (!n || last - in < 3 + *n) return {};
2384
24
                if (*n < 1 || *n > 20) return {};
2385
72
                for (int i = 0; i < *n; ++i) {
2386
48
                    if (in[2 + i].second.size() != 33) return {};
2387
48
                    auto key = ctx.FromPKBytes(in[2 + i].second.begin(), in[2 + i].second.end());
2388
48
                    if (!key) return {};
2389
48
                    keys.push_back(std::move(*key));
2390
48
                }
2391
24
                const auto k = ParseScriptNumber(in[2 + *n]);
2392
24
                if (!k || *k < 1 || *k > *n) return {};
2393
24
                in += 3 + *n;
2394
24
                std::reverse(keys.begin(), keys.end());
2395
24
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::MULTI, std::move(keys), *k);
2396
24
                break;
2397
24
            }
2398
            // Tapscript's equivalent of multi
2399
1.43k
            if (last - in >= 4 && in[0].first == OP_NUMEQUAL) {
2400
0
                if (!IsTapscript(ctx.MsContext())) return {};
2401
                // The necessary threshold of signatures.
2402
0
                const auto k = ParseScriptNumber(in[1]);
2403
0
                if (!k) return {};
2404
0
                if (*k < 1 || *k > MAX_PUBKEYS_PER_MULTI_A) return {};
2405
0
                if (last - in < 2 + *k * 2) return {};
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 {};
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 {};
2413
0
                    if (in[pos + 1].second.size() != 32) return {};
2414
0
                    auto key = ctx.FromPKBytes(in[pos + 1].second.begin(), in[pos + 1].second.end());
2415
0
                    if (!key) return {};
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 {};
2419
                    // OP_CHECKSIG means it was the last one to parse.
2420
0
                    if (in[pos].first == OP_CHECKSIG) break;
2421
0
                }
2422
0
                if (keys.size() < (size_t)*k) return {};
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
1.43k
            if (in[0].first == OP_CHECKSIG) {
2433
568
                ++in;
2434
568
                to_parse.emplace_back(DecodeContext::CHECK, -1, -1);
2435
568
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2436
568
                break;
2437
568
            }
2438
            // v: wrapper
2439
866
            if (in[0].first == OP_VERIFY) {
2440
141
                ++in;
2441
141
                to_parse.emplace_back(DecodeContext::VERIFY, -1, -1);
2442
141
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2443
141
                break;
2444
141
            }
2445
            // n: wrapper
2446
725
            if (in[0].first == OP_0NOTEQUAL) {
2447
266
                ++in;
2448
266
                to_parse.emplace_back(DecodeContext::ZERO_NOTEQUAL, -1, -1);
2449
266
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2450
266
                break;
2451
266
            }
2452
            // Thresh
2453
459
            if (last - in >= 3 && in[0].first == OP_EQUAL && (num = ParseScriptNumber(in[1]))) {
2454
113
                if (*num < 1) return {};
2455
113
                in += 2;
2456
113
                to_parse.emplace_back(DecodeContext::THRESH_W, 0, *num);
2457
113
                break;
2458
113
            }
2459
            // OP_ENDIF can be WRAP_J, WRAP_D, ANDOR, OR_C, OR_D, or OR_I
2460
346
            if (in[0].first == OP_ENDIF) {
2461
305
                ++in;
2462
305
                to_parse.emplace_back(DecodeContext::ENDIF, -1, -1);
2463
305
                to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2464
305
                break;
2465
305
            }
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
41
            if (in[0].first == OP_BOOLAND) {
2473
40
                ++in;
2474
40
                to_parse.emplace_back(DecodeContext::AND_B, -1, -1);
2475
40
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2476
40
                to_parse.emplace_back(DecodeContext::W_EXPR, -1, -1);
2477
40
                break;
2478
40
            }
2479
            // or_b
2480
1
            if (in[0].first == OP_BOOLOR) {
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
1
            return {};
2489
1
        }
2490
1.46k
        case DecodeContext::BKV_EXPR: {
2491
1.46k
            to_parse.emplace_back(DecodeContext::MAYBE_AND_V, -1, -1);
2492
1.46k
            to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2493
1.46k
            break;
2494
1
        }
2495
528
        case DecodeContext::W_EXPR: {
2496
            // a: wrapper
2497
528
            if (in >= last) return {};
2498
528
            if (in[0].first == OP_FROMALTSTACK) {
2499
80
                ++in;
2500
80
                to_parse.emplace_back(DecodeContext::ALT, -1, -1);
2501
448
            } else {
2502
448
                to_parse.emplace_back(DecodeContext::SWAP, -1, -1);
2503
448
            }
2504
528
            to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2505
528
            break;
2506
528
        }
2507
1.46k
        case DecodeContext::MAYBE_AND_V: {
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
1.46k
            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) {
2511
118
                to_parse.emplace_back(DecodeContext::AND_V, -1, -1);
2512
                // BKV_EXPR can contain more AND_V nodes
2513
118
                to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2514
118
            }
2515
1.46k
            break;
2516
528
        }
2517
448
        case DecodeContext::SWAP: {
2518
448
            if (in >= last || in[0].first != OP_SWAP || constructed.empty()) return {};
2519
448
            ++in;
2520
448
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_S, Vector(std::move(constructed.back()))};
2521
448
            break;
2522
448
        }
2523
80
        case DecodeContext::ALT: {
2524
80
            if (in >= last || in[0].first != OP_TOALTSTACK || constructed.empty()) return {};
2525
80
            ++in;
2526
80
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_A, Vector(std::move(constructed.back()))};
2527
80
            break;
2528
80
        }
2529
567
        case DecodeContext::CHECK: {
2530
567
            if (constructed.empty()) return {};
2531
567
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_C, Vector(std::move(constructed.back()))};
2532
567
            break;
2533
567
        }
2534
23
        case DecodeContext::DUP_IF: {
2535
23
            if (constructed.empty()) return {};
2536
23
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_D, Vector(std::move(constructed.back()))};
2537
23
            break;
2538
23
        }
2539
141
        case DecodeContext::VERIFY: {
2540
141
            if (constructed.empty()) return {};
2541
141
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_V, Vector(std::move(constructed.back()))};
2542
141
            break;
2543
141
        }
2544
0
        case DecodeContext::NON_ZERO: {
2545
0
            if (constructed.empty()) return {};
2546
0
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_J, Vector(std::move(constructed.back()))};
2547
0
            break;
2548
0
        }
2549
266
        case DecodeContext::ZERO_NOTEQUAL: {
2550
266
            if (constructed.empty()) return {};
2551
266
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_N, Vector(std::move(constructed.back()))};
2552
266
            break;
2553
266
        }
2554
118
        case DecodeContext::AND_V: {
2555
118
            if (constructed.size() < 2) return {};
2556
118
            BuildBack(ctx.MsContext(), Fragment::AND_V, constructed, /*reverse=*/true);
2557
118
            break;
2558
118
        }
2559
40
        case DecodeContext::AND_B: {
2560
40
            if (constructed.size() < 2) return {};
2561
40
            BuildBack(ctx.MsContext(), Fragment::AND_B, constructed, /*reverse=*/true);
2562
40
            break;
2563
40
        }
2564
0
        case DecodeContext::OR_B: {
2565
0
            if (constructed.size() < 2) return {};
2566
0
            BuildBack(ctx.MsContext(), Fragment::OR_B, constructed, /*reverse=*/true);
2567
0
            break;
2568
0
        }
2569
0
        case DecodeContext::OR_C: {
2570
0
            if (constructed.size() < 2) return {};
2571
0
            BuildBack(ctx.MsContext(), Fragment::OR_C, constructed, /*reverse=*/true);
2572
0
            break;
2573
0
        }
2574
4
        case DecodeContext::OR_D: {
2575
4
            if (constructed.size() < 2) return {};
2576
4
            BuildBack(ctx.MsContext(), Fragment::OR_D, constructed, /*reverse=*/true);
2577
4
            break;
2578
4
        }
2579
47
        case DecodeContext::ANDOR: {
2580
47
            if (constructed.size() < 3) return {};
2581
47
            Node left{std::move(constructed.back())};
2582
47
            constructed.pop_back();
2583
47
            Node right{std::move(constructed.back())};
2584
47
            constructed.pop_back();
2585
47
            Node mid{std::move(constructed.back())};
2586
47
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::ANDOR, Vector(std::move(left), std::move(mid), std::move(right))};
2587
47
            break;
2588
47
        }
2589
601
        case DecodeContext::THRESH_W: {
2590
601
            if (in >= last) return {};
2591
601
            if (in[0].first == OP_ADD) {
2592
488
                ++in;
2593
488
                to_parse.emplace_back(DecodeContext::THRESH_W, n+1, k);
2594
488
                to_parse.emplace_back(DecodeContext::W_EXPR, -1, -1);
2595
488
            } else {
2596
113
                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
113
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2599
113
            }
2600
601
            break;
2601
601
        }
2602
113
        case DecodeContext::THRESH_E: {
2603
113
            if (k < 1 || k > n || constructed.size() < static_cast<size_t>(n)) return {};
2604
113
            std::vector<Node<Key>> subs;
2605
714
            for (int i = 0; i < n; ++i) {
2606
601
                Node sub{std::move(constructed.back())};
2607
601
                constructed.pop_back();
2608
601
                subs.push_back(std::move(sub));
2609
601
            }
2610
113
            constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::THRESH, std::move(subs), k);
2611
113
            break;
2612
113
        }
2613
305
        case DecodeContext::ENDIF: {
2614
305
            if (in >= last) return {};
2615
2616
            // could be andor or or_i
2617
305
            if (in[0].first == OP_ELSE) {
2618
278
                ++in;
2619
278
                to_parse.emplace_back(DecodeContext::ENDIF_ELSE, -1, -1);
2620
278
                to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2621
278
            }
2622
            // could be j: or d: wrapper
2623
27
            else if (in[0].first == OP_IF) {
2624
23
                if (last - in >= 2 && in[1].first == OP_DUP) {
2625
23
                    in += 2;
2626
23
                    to_parse.emplace_back(DecodeContext::DUP_IF, -1, -1);
2627
23
                } else if (last - in >= 3 && in[1].first == OP_0NOTEQUAL && in[2].first == OP_SIZE) {
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
23
            } else if (in[0].first == OP_NOTIF) {
2636
4
                ++in;
2637
4
                to_parse.emplace_back(DecodeContext::ENDIF_NOTIF, -1, -1);
2638
4
            }
2639
0
            else {
2640
0
                return {};
2641
0
            }
2642
305
            break;
2643
305
        }
2644
305
        case DecodeContext::ENDIF_NOTIF: {
2645
4
            if (in >= last) return {};
2646
4
            if (in[0].first == OP_IFDUP) {
2647
4
                ++in;
2648
4
                to_parse.emplace_back(DecodeContext::OR_D, -1, -1);
2649
4
            } 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
4
            to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2654
4
            break;
2655
4
        }
2656
278
        case DecodeContext::ENDIF_ELSE: {
2657
278
            if (in >= last) return {};
2658
278
            if (in[0].first == OP_IF) {
2659
231
                ++in;
2660
231
                BuildBack(ctx.MsContext(), Fragment::OR_I, constructed, /*reverse=*/true);
2661
231
            } else if (in[0].first == OP_NOTIF) {
2662
47
                ++in;
2663
47
                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
47
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2666
47
            } else {
2667
0
                return {};
2668
0
            }
2669
278
            break;
2670
278
        }
2671
9.13k
        }
2672
9.13k
    }
2673
234
    if (constructed.size() != 1) return {};
2674
234
    Node tl_node{std::move(constructed.front())};
2675
234
    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
234
    if (!tl_node.IsValidTopLevel()) return {};
2679
234
    return tl_node;
2680
234
}
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
796
{
2687
796
    return internal::Parse<typename Ctx::Key>(str, ctx);
2688
796
}
miniscript_tests.cpp:std::optional<miniscript::Node<(anonymous namespace)::KeyConverter::Key>> miniscript::FromString<(anonymous namespace)::KeyConverter>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, (anonymous namespace)::KeyConverter const&)
Line
Count
Source
2686
220
{
2687
220
    return internal::Parse<typename Ctx::Key>(str, ctx);
2688
220
}
descriptor.cpp:std::optional<miniscript::Node<(anonymous namespace)::KeyParser::Key>> miniscript::FromString<(anonymous namespace)::KeyParser>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, (anonymous namespace)::KeyParser const&)
Line
Count
Source
2686
576
{
2687
576
    return internal::Parse<typename Ctx::Key>(str, ctx);
2688
576
}
2689
2690
template <typename Ctx>
2691
inline std::optional<Node<typename Ctx::Key>> FromScript(const CScript& script, const Ctx& ctx)
2692
5.48k
{
2693
5.48k
    using namespace internal;
2694
    // A too large Script is necessarily invalid, don't bother parsing it.
2695
5.48k
    if (script.size() > MaxScriptSize(ctx.MsContext())) return {};
2696
5.48k
    auto decomposed = DecomposeScript(script);
2697
5.48k
    if (!decomposed) return {};
2698
5.48k
    auto it = decomposed->begin();
2699
5.48k
    auto ret = DecodeScript<typename Ctx::Key>(it, decomposed->end(), ctx);
2700
5.48k
    if (!ret) return {};
2701
5.46k
    if (it != decomposed->end()) return {};
2702
5.46k
    return ret;
2703
5.46k
}
miniscript_tests.cpp:std::optional<miniscript::Node<(anonymous namespace)::KeyConverter::Key>> miniscript::FromScript<(anonymous namespace)::KeyConverter>(CScript const&, (anonymous namespace)::KeyConverter const&)
Line
Count
Source
2692
132
{
2693
132
    using namespace internal;
2694
    // A too large Script is necessarily invalid, don't bother parsing it.
2695
132
    if (script.size() > MaxScriptSize(ctx.MsContext())) return {};
2696
132
    auto decomposed = DecomposeScript(script);
2697
132
    if (!decomposed) return {};
2698
128
    auto it = decomposed->begin();
2699
128
    auto ret = DecodeScript<typename Ctx::Key>(it, decomposed->end(), ctx);
2700
128
    if (!ret) return {};
2701
125
    if (it != decomposed->end()) return {};
2702
125
    return ret;
2703
125
}
descriptor.cpp:std::optional<miniscript::Node<(anonymous namespace)::KeyParser::Key>> miniscript::FromScript<(anonymous namespace)::KeyParser>(CScript const&, (anonymous namespace)::KeyParser const&)
Line
Count
Source
2692
702
{
2693
702
    using namespace internal;
2694
    // A too large Script is necessarily invalid, don't bother parsing it.
2695
702
    if (script.size() > MaxScriptSize(ctx.MsContext())) return {};
2696
702
    auto decomposed = DecomposeScript(script);
2697
702
    if (!decomposed) return {};
2698
702
    auto it = decomposed->begin();
2699
702
    auto ret = DecodeScript<typename Ctx::Key>(it, decomposed->end(), ctx);
2700
702
    if (!ret) return {};
2701
689
    if (it != decomposed->end()) return {};
2702
689
    return ret;
2703
689
}
std::optional<miniscript::Node<TapSatisfier::Key>> miniscript::FromScript<TapSatisfier>(CScript const&, TapSatisfier const&)
Line
Count
Source
2692
4.41k
{
2693
4.41k
    using namespace internal;
2694
    // A too large Script is necessarily invalid, don't bother parsing it.
2695
4.41k
    if (script.size() > MaxScriptSize(ctx.MsContext())) return {};
2696
4.41k
    auto decomposed = DecomposeScript(script);
2697
4.41k
    if (!decomposed) return {};
2698
4.41k
    auto it = decomposed->begin();
2699
4.41k
    auto ret = DecodeScript<typename Ctx::Key>(it, decomposed->end(), ctx);
2700
4.41k
    if (!ret) return {};
2701
4.41k
    if (it != decomposed->end()) return {};
2702
4.41k
    return ret;
2703
4.41k
}
std::optional<miniscript::Node<WshSatisfier::Key>> miniscript::FromScript<WshSatisfier>(CScript const&, WshSatisfier const&)
Line
Count
Source
2692
237
{
2693
237
    using namespace internal;
2694
    // A too large Script is necessarily invalid, don't bother parsing it.
2695
237
    if (script.size() > MaxScriptSize(ctx.MsContext())) return {};
2696
237
    auto decomposed = DecomposeScript(script);
2697
237
    if (!decomposed) return {};
2698
237
    auto it = decomposed->begin();
2699
237
    auto ret = DecodeScript<typename Ctx::Key>(it, decomposed->end(), ctx);
2700
237
    if (!ret) return {};
2701
234
    if (it != decomposed->end()) return {};
2702
234
    return ret;
2703
234
}
2704
2705
} // namespace miniscript
2706
2707
#endif // BITCOIN_SCRIPT_MINISCRIPT_H