Coverage Report

Created: 2026-09-14 20:36

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
29.7M
    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
14.8M
    constexpr Type operator|(Type x) const { return Type(m_flags | x.m_flags); }
143
144
    //! Compute the type with the intersection of properties.
145
14.8M
    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
269M
    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
6.33M
    constexpr bool operator==(Type x) const { return m_flags == x.m_flags; }
155
156
    //! The empty type if x is false, itself otherwise.
157
99.2k
    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
887
{
201
887
    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
887
}
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
21.3M
{
261
21.3M
    switch (ms_ctx) {
262
69.8k
        case MiniscriptContext::P2WSH: return false;
263
21.2M
        case MiniscriptContext::TAPSCRIPT: return true;
264
21.3M
    }
265
21.3M
    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
6.33M
{
286
6.33M
    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
6.30M
        constexpr auto max_size{MAX_STANDARD_TX_WEIGHT - TX_BODY_LEEWAY_WEIGHT - MAX_TAPSCRIPT_SAT_SIZE};
293
6.30M
        return max_size - GetSizeOfCompactSize(max_size);
294
6.30M
    }
295
24.7k
    return MAX_STANDARD_P2WSH_SCRIPT_SIZE;
296
6.33M
}
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.41k
    InputStack() = default;
328
    //! Construct a valid single-element stack (with an element up to 75 bytes).
329
480k
    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.05k
    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.67k
    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
3.03k
    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
45.1k
    MaxInt() : valid(false), value(0) {}
372
113k
    MaxInt(I val) : valid(true), value(val) {}
373
374
2.75k
    bool Valid() const { return valid; }
375
2.74k
    I Value() const { return value; }
376
377
57.9k
    friend MaxInt<I> operator+(const MaxInt<I>& a, const MaxInt<I>& b) {
378
57.9k
        if (!a.valid || !b.valid) return {};
379
43.2k
        return a.value + b.value;
380
57.9k
    }
381
382
9.85k
    friend MaxInt<I> operator|(const MaxInt<I>& a, const MaxInt<I>& b) {
383
9.85k
        if (!a.valid) return b;
384
8.61k
        if (!b.valid) return a;
385
7.29k
        return std::max(a.value, b.value);
386
8.61k
    }
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
7.36M
    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
28.0k
    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
148k
        valid{true}, netdiff{in_netdiff}, exec{in_exec} {}
457
458
7.30k
    bool Valid() const { return valid; }
459
2.80k
    int32_t NetDiff() const { return netdiff; }
460
4.47k
    int32_t Exec() const { return exec; }
461
462
    /** Script set union. */
463
    constexpr friend SatInfo operator|(const SatInfo& a, const SatInfo& b) noexcept
464
4.92k
    {
465
        // Union with an empty set is itself.
466
4.92k
        if (!a.valid) return b;
467
4.30k
        if (!b.valid) return a;
468
        // Otherwise the netdiff and exec of the union is the maximum of the individual values.
469
3.64k
        return {std::max(a.netdiff, b.netdiff), std::max(a.exec, b.exec)};
470
4.30k
    }
471
472
    /** Script set concatenation. */
473
    constexpr friend SatInfo operator+(const SatInfo& a, const SatInfo& b) noexcept
474
84.6k
    {
475
        // Concatenation with an empty set yields an empty set.
476
84.6k
        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
71.8k
        return {a.netdiff + b.netdiff, std::max(b.exec, b.netdiff + a.exec)};
481
84.6k
    }
482
483
    /** The empty script. */
484
780
    static constexpr SatInfo Empty() noexcept { return {0, 0}; }
485
    /** A script consisting of a single push opcode. */
486
19.9k
    static constexpr SatInfo Push() noexcept { return {-1, 0}; }
487
    /** A script consisting of a single hash opcode. */
488
1.27k
    static constexpr SatInfo Hash() noexcept { return {0, 0}; }
489
    /** A script consisting of just a repurposed nop (OP_CHECKLOCKTIMEVERIFY, OP_CHECKSEQUENCEVERIFY). */
490
9.43k
    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.92k
    static constexpr SatInfo If() noexcept { return {1, 1}; }
493
    /** A script consisting of just a binary operator (OP_BOOLAND, OP_BOOLOR, OP_ADD). */
494
15.6k
    static constexpr SatInfo BinaryOp() noexcept { return {1, 1}; }
495
496
    // Scripts for specific individual opcodes.
497
1.11k
    static constexpr SatInfo OP_DUP() noexcept { return {-1, 0}; }
498
408
    static constexpr SatInfo OP_IFDUP(bool nonzero) noexcept { return {nonzero ? -1 : 0, 0}; }
499
1.27k
    static constexpr SatInfo OP_EQUALVERIFY() noexcept { return {2, 2}; }
500
1.18k
    static constexpr SatInfo OP_EQUAL() noexcept { return {1, 1}; }
501
432
    static constexpr SatInfo OP_SIZE() noexcept { return {-1, 0}; }
502
15.7k
    static constexpr SatInfo OP_CHECKSIG() noexcept { return {1, 1}; }
503
32
    static constexpr SatInfo OP_0NOTEQUAL() noexcept { return {0, 0}; }
504
2.26k
    static constexpr SatInfo OP_VERIFY() noexcept { return {1, 1}; }
505
};
506
507
class StackSize
508
{
509
    SatInfo sat, dsat;
510
511
public:
512
31.9k
    constexpr StackSize(SatInfo in_sat, SatInfo in_dsat) noexcept : sat(in_sat), dsat(in_dsat) {};
513
8.96k
    constexpr StackSize(SatInfo in_both) noexcept : sat(in_both), dsat(in_both) {};
514
515
50.6k
    const SatInfo& Sat() const { return sat; }
516
29.7k
    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.0k
    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
15.8M
    {
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
15.8M
        std::vector<std::vector<Node>> queue;
561
15.8M
        queue.push_back(std::move(subs));
562
23.1M
        do {
563
23.1M
            auto flattening{std::move(queue.back())};
564
23.1M
            queue.pop_back();
565
23.1M
            for (Node& n : flattening) {
566
7.36M
                if (!n.subs.empty()) queue.push_back(std::move(n.subs));
567
7.36M
            }
568
23.1M
        } while (!queue.empty());
569
15.8M
    }
miniscript::Node<CPubKey>::~Node()
Line
Count
Source
553
74.4k
    {
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
74.4k
        std::vector<std::vector<Node>> queue;
561
74.4k
        queue.push_back(std::move(subs));
562
91.5k
        do {
563
91.5k
            auto flattening{std::move(queue.back())};
564
91.5k
            queue.pop_back();
565
91.5k
            for (Node& n : flattening) {
566
26.4k
                if (!n.subs.empty()) queue.push_back(std::move(n.subs));
567
26.4k
            }
568
91.5k
        } while (!queue.empty());
569
74.4k
    }
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
11.2M
    {
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
11.2M
        std::vector<std::vector<Node>> queue;
561
11.2M
        queue.push_back(std::move(subs));
562
16.8M
        do {
563
16.8M
            auto flattening{std::move(queue.back())};
564
16.8M
            queue.pop_back();
565
16.8M
            for (Node& n : flattening) {
566
5.60M
                if (!n.subs.empty()) queue.push_back(std::move(n.subs));
567
5.60M
            }
568
16.8M
        } while (!queue.empty());
569
11.2M
    }
570
    // NOLINTEND(misc-no-recursion)
571
572
    Node<Key> Clone() const
573
221
    {
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
221
        return TreeEval<Node>(upfn);
585
221
    }
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.35k
    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
253
    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
7.36M
    {
620
7.36M
        size_t subsize = 0;
621
7.36M
        for (const auto& sub : subs) {
622
7.36M
            subsize += sub.ScriptSize();
623
7.36M
        }
624
7.36M
        Type sub0type = subs.size() > 0 ? subs[0].GetType() : ""_mst;
625
7.36M
        return internal::ComputeScriptLen(fragment, sub0type, subsize, k, subs.size(), keys.size(), m_script_ctx);
626
7.36M
    }
miniscript::Node<CPubKey>::CalcScriptLen() const
Line
Count
Source
619
28.7k
    {
620
28.7k
        size_t subsize = 0;
621
28.7k
        for (const auto& sub : subs) {
622
26.4k
            subsize += sub.ScriptSize();
623
26.4k
        }
624
28.7k
        Type sub0type = subs.size() > 0 ? subs[0].GetType() : ""_mst;
625
28.7k
        return internal::ComputeScriptLen(fragment, sub0type, subsize, k, subs.size(), keys.size(), m_script_ctx);
626
28.7k
    }
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
5.61M
    {
620
5.61M
        size_t subsize = 0;
621
5.61M
        for (const auto& sub : subs) {
622
5.60M
            subsize += sub.ScriptSize();
623
5.60M
        }
624
5.61M
        Type sub0type = subs.size() > 0 ? subs[0].GetType() : ""_mst;
625
5.61M
        return internal::ComputeScriptLen(fragment, sub0type, subsize, k, subs.size(), keys.size(), m_script_ctx);
626
5.61M
    }
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
20.4k
    {
654
        /** Entries of the explicit stack tracked in this algorithm. */
655
20.4k
        struct StackElem
656
20.4k
        {
657
20.4k
            const Node& node; //!< The node being evaluated.
658
20.4k
            size_t expanded; //!< How many children of this node have been expanded.
659
20.4k
            State state; //!< The state for that node.
660
661
20.4k
            StackElem(const Node& node_, size_t exp_, State&& state_) :
662
27.3M
                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
11.2M
                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
5.61M
                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
5.61M
                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.46k
                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.46k
                node(node_), expanded(exp_), state(std::move(state_)) {}
663
20.4k
        };
664
        /* Stack of tree nodes being explored. */
665
20.4k
        std::vector<StackElem> stack;
666
        /* Results of subtrees so far. Their order and mapping to tree nodes
667
         * is implicitly defined by stack. */
668
20.4k
        std::vector<Result> results;
669
20.4k
        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
54.6M
        while (stack.size()) {
688
54.6M
            const Node& node = stack.back().node;
689
54.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
27.3M
                size_t child_index = stack.back().expanded++;
694
27.3M
                State child_state = downfn(stack.back().state, node, child_index);
695
27.3M
                stack.emplace_back(node.subs[child_index], 0, std::move(child_state));
696
27.3M
                continue;
697
27.3M
            }
698
            // Invoke upfn with the last node.subs.size() elements of results as input.
699
54.6M
            assert(results.size() >= node.subs.size());
700
27.3M
            std::optional<Result> result{upfn(std::move(stack.back().state), node,
701
27.3M
                std::span<Result>{results}.last(node.subs.size()))};
702
            // If evaluation returns std::nullopt, abort immediately.
703
27.3M
            if (!result) return {};
704
            // Replace the last node.subs.size() elements of results with the new result.
705
27.3M
            results.erase(results.end() - node.subs.size(), results.end());
706
27.3M
            results.push_back(std::move(*result));
707
27.3M
            stack.pop_back();
708
27.3M
        }
709
        // The final remaining results element is the root result, return it.
710
20.4k
        assert(results.size() >= 1);
711
20.4k
        CHECK_NONFATAL(results.size() == 1);
712
20.4k
        return std::move(results[0]);
713
20.4k
    }
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
221
    {
654
        /** Entries of the explicit stack tracked in this algorithm. */
655
221
        struct StackElem
656
221
        {
657
221
            const Node& node; //!< The node being evaluated.
658
221
            size_t expanded; //!< How many children of this node have been expanded.
659
221
            State state; //!< The state for that node.
660
661
221
            StackElem(const Node& node_, size_t exp_, State&& state_) :
662
221
                node(node_), expanded(exp_), state(std::move(state_)) {}
663
221
        };
664
        /* Stack of tree nodes being explored. */
665
221
        std::vector<StackElem> stack;
666
        /* Results of subtrees so far. Their order and mapping to tree nodes
667
         * is implicitly defined by stack. */
668
221
        std::vector<Result> results;
669
221
        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
221
        assert(results.size() >= 1);
711
221
        CHECK_NONFATAL(results.size() == 1);
712
221
        return std::move(results[0]);
713
221
    }
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
877
    {
654
        /** Entries of the explicit stack tracked in this algorithm. */
655
877
        struct StackElem
656
877
        {
657
877
            const Node& node; //!< The node being evaluated.
658
877
            size_t expanded; //!< How many children of this node have been expanded.
659
877
            State state; //!< The state for that node.
660
661
877
            StackElem(const Node& node_, size_t exp_, State&& state_) :
662
877
                node(node_), expanded(exp_), state(std::move(state_)) {}
663
877
        };
664
        /* Stack of tree nodes being explored. */
665
877
        std::vector<StackElem> stack;
666
        /* Results of subtrees so far. Their order and mapping to tree nodes
667
         * is implicitly defined by stack. */
668
877
        std::vector<Result> results;
669
877
        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
877
        assert(results.size() >= 1);
711
877
        CHECK_NONFATAL(results.size() == 1);
712
877
        return std::move(results[0]);
713
877
    }
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
2.34k
    {
654
        /** Entries of the explicit stack tracked in this algorithm. */
655
2.34k
        struct StackElem
656
2.34k
        {
657
2.34k
            const Node& node; //!< The node being evaluated.
658
2.34k
            size_t expanded; //!< How many children of this node have been expanded.
659
2.34k
            State state; //!< The state for that node.
660
661
2.34k
            StackElem(const Node& node_, size_t exp_, State&& state_) :
662
2.34k
                node(node_), expanded(exp_), state(std::move(state_)) {}
663
2.34k
        };
664
        /* Stack of tree nodes being explored. */
665
2.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
2.34k
        std::vector<Result> results;
669
2.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
22.4M
        while (stack.size()) {
688
22.4M
            const Node& node = stack.back().node;
689
22.4M
            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
11.2M
                size_t child_index = stack.back().expanded++;
694
11.2M
                State child_state = downfn(stack.back().state, node, child_index);
695
11.2M
                stack.emplace_back(node.subs[child_index], 0, std::move(child_state));
696
11.2M
                continue;
697
11.2M
            }
698
            // Invoke upfn with the last node.subs.size() elements of results as input.
699
22.4M
            assert(results.size() >= node.subs.size());
700
11.2M
            std::optional<Result> result{upfn(std::move(stack.back().state), node,
701
11.2M
                std::span<Result>{results}.last(node.subs.size()))};
702
            // If evaluation returns std::nullopt, abort immediately.
703
11.2M
            if (!result) return {};
704
            // Replace the last node.subs.size() elements of results with the new result.
705
11.2M
            results.erase(results.end() - node.subs.size(), results.end());
706
11.2M
            results.push_back(std::move(*result));
707
11.2M
            stack.pop_back();
708
11.2M
        }
709
        // The final remaining results element is the root result, return it.
710
2.34k
        assert(results.size() >= 1);
711
2.34k
        CHECK_NONFATAL(results.size() == 1);
712
2.34k
        return std::move(results[0]);
713
2.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.44k
    {
654
        /** Entries of the explicit stack tracked in this algorithm. */
655
4.44k
        struct StackElem
656
4.44k
        {
657
4.44k
            const Node& node; //!< The node being evaluated.
658
4.44k
            size_t expanded; //!< How many children of this node have been expanded.
659
4.44k
            State state; //!< The state for that node.
660
661
4.44k
            StackElem(const Node& node_, size_t exp_, State&& state_) :
662
4.44k
                node(node_), expanded(exp_), state(std::move(state_)) {}
663
4.44k
        };
664
        /* Stack of tree nodes being explored. */
665
4.44k
        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.44k
        std::vector<Result> results;
669
4.44k
        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
11.2M
        while (stack.size()) {
688
11.2M
            const Node& node = stack.back().node;
689
11.2M
            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
5.60M
                size_t child_index = stack.back().expanded++;
694
5.60M
                State child_state = downfn(stack.back().state, node, child_index);
695
5.60M
                stack.emplace_back(node.subs[child_index], 0, std::move(child_state));
696
5.60M
                continue;
697
5.60M
            }
698
            // Invoke upfn with the last node.subs.size() elements of results as input.
699
11.2M
            assert(results.size() >= node.subs.size());
700
5.61M
            std::optional<Result> result{upfn(std::move(stack.back().state), node,
701
5.61M
                std::span<Result>{results}.last(node.subs.size()))};
702
            // If evaluation returns std::nullopt, abort immediately.
703
5.61M
            if (!result) return {};
704
            // Replace the last node.subs.size() elements of results with the new result.
705
5.61M
            results.erase(results.end() - node.subs.size(), results.end());
706
5.61M
            results.push_back(std::move(*result));
707
5.61M
            stack.pop_back();
708
5.61M
        }
709
        // The final remaining results element is the root result, return it.
710
4.44k
        assert(results.size() >= 1);
711
4.44k
        CHECK_NONFATAL(results.size() == 1);
712
4.44k
        return std::move(results[0]);
713
4.44k
    }
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.44k
    {
654
        /** Entries of the explicit stack tracked in this algorithm. */
655
4.44k
        struct StackElem
656
4.44k
        {
657
4.44k
            const Node& node; //!< The node being evaluated.
658
4.44k
            size_t expanded; //!< How many children of this node have been expanded.
659
4.44k
            State state; //!< The state for that node.
660
661
4.44k
            StackElem(const Node& node_, size_t exp_, State&& state_) :
662
4.44k
                node(node_), expanded(exp_), state(std::move(state_)) {}
663
4.44k
        };
664
        /* Stack of tree nodes being explored. */
665
4.44k
        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.44k
        std::vector<Result> results;
669
4.44k
        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
11.2M
        while (stack.size()) {
688
11.2M
            const Node& node = stack.back().node;
689
11.2M
            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
5.60M
                size_t child_index = stack.back().expanded++;
694
5.60M
                State child_state = downfn(stack.back().state, node, child_index);
695
5.60M
                stack.emplace_back(node.subs[child_index], 0, std::move(child_state));
696
5.60M
                continue;
697
5.60M
            }
698
            // Invoke upfn with the last node.subs.size() elements of results as input.
699
11.2M
            assert(results.size() >= node.subs.size());
700
5.61M
            std::optional<Result> result{upfn(std::move(stack.back().state), node,
701
5.61M
                std::span<Result>{results}.last(node.subs.size()))};
702
            // If evaluation returns std::nullopt, abort immediately.
703
5.61M
            if (!result) return {};
704
            // Replace the last node.subs.size() elements of results with the new result.
705
5.61M
            results.erase(results.end() - node.subs.size(), results.end());
706
5.61M
            results.push_back(std::move(*result));
707
5.61M
            stack.pop_back();
708
5.61M
        }
709
        // The final remaining results element is the root result, return it.
710
4.44k
        assert(results.size() >= 1);
711
4.44k
        CHECK_NONFATAL(results.size() == 1);
712
4.44k
        return std::move(results[0]);
713
4.44k
    }
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
268
    {
654
        /** Entries of the explicit stack tracked in this algorithm. */
655
268
        struct StackElem
656
268
        {
657
268
            const Node& node; //!< The node being evaluated.
658
268
            size_t expanded; //!< How many children of this node have been expanded.
659
268
            State state; //!< The state for that node.
660
661
268
            StackElem(const Node& node_, size_t exp_, State&& state_) :
662
268
                node(node_), expanded(exp_), state(std::move(state_)) {}
663
268
        };
664
        /* Stack of tree nodes being explored. */
665
268
        std::vector<StackElem> stack;
666
        /* Results of subtrees so far. Their order and mapping to tree nodes
667
         * is implicitly defined by stack. */
668
268
        std::vector<Result> results;
669
268
        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.93k
        while (stack.size()) {
688
6.66k
            const Node& node = stack.back().node;
689
6.66k
            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.19k
                size_t child_index = stack.back().expanded++;
694
3.19k
                State child_state = downfn(stack.back().state, node, child_index);
695
3.19k
                stack.emplace_back(node.subs[child_index], 0, std::move(child_state));
696
3.19k
                continue;
697
3.19k
            }
698
            // Invoke upfn with the last node.subs.size() elements of results as input.
699
6.66k
            assert(results.size() >= node.subs.size());
700
3.46k
            std::optional<Result> result{upfn(std::move(stack.back().state), node,
701
3.46k
                std::span<Result>{results}.last(node.subs.size()))};
702
            // If evaluation returns std::nullopt, abort immediately.
703
3.46k
            if (!result) return {};
704
            // Replace the last node.subs.size() elements of results with the new result.
705
3.46k
            results.erase(results.end() - node.subs.size(), results.end());
706
3.46k
            results.push_back(std::move(*result));
707
3.46k
            stack.pop_back();
708
3.46k
        }
709
        // The final remaining results element is the root result, return it.
710
268
        assert(results.size() >= 1);
711
268
        CHECK_NONFATAL(results.size() == 1);
712
268
        return std::move(results[0]);
713
268
    }
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
268
    {
654
        /** Entries of the explicit stack tracked in this algorithm. */
655
268
        struct StackElem
656
268
        {
657
268
            const Node& node; //!< The node being evaluated.
658
268
            size_t expanded; //!< How many children of this node have been expanded.
659
268
            State state; //!< The state for that node.
660
661
268
            StackElem(const Node& node_, size_t exp_, State&& state_) :
662
268
                node(node_), expanded(exp_), state(std::move(state_)) {}
663
268
        };
664
        /* Stack of tree nodes being explored. */
665
268
        std::vector<StackElem> stack;
666
        /* Results of subtrees so far. Their order and mapping to tree nodes
667
         * is implicitly defined by stack. */
668
268
        std::vector<Result> results;
669
268
        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.93k
        while (stack.size()) {
688
6.66k
            const Node& node = stack.back().node;
689
6.66k
            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.19k
                size_t child_index = stack.back().expanded++;
694
3.19k
                State child_state = downfn(stack.back().state, node, child_index);
695
3.19k
                stack.emplace_back(node.subs[child_index], 0, std::move(child_state));
696
3.19k
                continue;
697
3.19k
            }
698
            // Invoke upfn with the last node.subs.size() elements of results as input.
699
6.66k
            assert(results.size() >= node.subs.size());
700
3.46k
            std::optional<Result> result{upfn(std::move(stack.back().state), node,
701
3.46k
                std::span<Result>{results}.last(node.subs.size()))};
702
            // If evaluation returns std::nullopt, abort immediately.
703
3.46k
            if (!result) return {};
704
            // Replace the last node.subs.size() elements of results with the new result.
705
3.46k
            results.erase(results.end() - node.subs.size(), results.end());
706
3.46k
            results.push_back(std::move(*result));
707
3.46k
            stack.pop_back();
708
3.46k
        }
709
        // The final remaining results element is the root result, return it.
710
268
        assert(results.size() >= 1);
711
268
        CHECK_NONFATAL(results.size() == 1);
712
268
        return std::move(results[0]);
713
268
    }
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
16.0k
    {
749
16.0k
        struct DummyState {};
750
16.0k
        return std::move(*TreeEvalMaybe<Result>(DummyState{},
751
14.4M
            [](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
5.60M
            [](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
5.60M
            [](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.19k
            [](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.19k
            [](DummyState, const Node&, size_t) { return DummyState{}; },
752
14.4M
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
14.4M
                Result res{upfn(node, subs)};
754
14.4M
                return std::optional<Result>(std::move(res));
755
14.4M
            }
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
5.61M
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
5.61M
                Result res{upfn(node, subs)};
754
5.61M
                return std::optional<Result>(std::move(res));
755
5.61M
            }
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
5.61M
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
5.61M
                Result res{upfn(node, subs)};
754
5.61M
                return std::optional<Result>(std::move(res));
755
5.61M
            }
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.46k
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
3.46k
                Result res{upfn(node, subs)};
754
3.46k
                return std::optional<Result>(std::move(res));
755
3.46k
            }
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.46k
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
3.46k
                Result res{upfn(node, subs)};
754
3.46k
                return std::optional<Result>(std::move(res));
755
3.46k
            }
756
16.0k
        ));
757
16.0k
    }
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
221
    {
749
221
        struct DummyState {};
750
221
        return std::move(*TreeEvalMaybe<Result>(DummyState{},
751
221
            [](DummyState, const Node&, size_t) { return DummyState{}; },
752
221
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
221
                Result res{upfn(node, subs)};
754
221
                return std::optional<Result>(std::move(res));
755
221
            }
756
221
        ));
757
221
    }
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
877
    {
749
877
        struct DummyState {};
750
877
        return std::move(*TreeEvalMaybe<Result>(DummyState{},
751
877
            [](DummyState, const Node&, size_t) { return DummyState{}; },
752
877
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
877
                Result res{upfn(node, subs)};
754
877
                return std::optional<Result>(std::move(res));
755
877
            }
756
877
        ));
757
877
    }
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.44k
    {
749
4.44k
        struct DummyState {};
750
4.44k
        return std::move(*TreeEvalMaybe<Result>(DummyState{},
751
4.44k
            [](DummyState, const Node&, size_t) { return DummyState{}; },
752
4.44k
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
4.44k
                Result res{upfn(node, subs)};
754
4.44k
                return std::optional<Result>(std::move(res));
755
4.44k
            }
756
4.44k
        ));
757
4.44k
    }
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.44k
    {
749
4.44k
        struct DummyState {};
750
4.44k
        return std::move(*TreeEvalMaybe<Result>(DummyState{},
751
4.44k
            [](DummyState, const Node&, size_t) { return DummyState{}; },
752
4.44k
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
4.44k
                Result res{upfn(node, subs)};
754
4.44k
                return std::optional<Result>(std::move(res));
755
4.44k
            }
756
4.44k
        ));
757
4.44k
    }
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
268
    {
749
268
        struct DummyState {};
750
268
        return std::move(*TreeEvalMaybe<Result>(DummyState{},
751
268
            [](DummyState, const Node&, size_t) { return DummyState{}; },
752
268
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
268
                Result res{upfn(node, subs)};
754
268
                return std::optional<Result>(std::move(res));
755
268
            }
756
268
        ));
757
268
    }
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
268
    {
749
268
        struct DummyState {};
750
268
        return std::move(*TreeEvalMaybe<Result>(DummyState{},
751
268
            [](DummyState, const Node&, size_t) { return DummyState{}; },
752
268
            [&upfn](DummyState, const Node& node, std::span<Result> subs) {
753
268
                Result res{upfn(node, subs)};
754
268
                return std::optional<Result>(std::move(res));
755
268
            }
756
268
        ));
757
268
    }
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
7.36M
    Type CalcType() const {
781
7.36M
        using namespace internal;
782
783
        // THRESH has a variable number of subexpressions
784
7.36M
        std::vector<Type> sub_types;
785
7.36M
        if (fragment == Fragment::THRESH) {
786
1.51k
            for (const auto& sub : subs) sub_types.push_back(sub.GetType());
787
390
        }
788
        // All other nodes than THRESH can be computed just from the types of the 0-3 subexpressions.
789
7.36M
        Type x = subs.size() > 0 ? subs[0].GetType() : ""_mst;
790
7.36M
        Type y = subs.size() > 1 ? subs[1].GetType() : ""_mst;
791
7.36M
        Type z = subs.size() > 2 ? subs[2].GetType() : ""_mst;
792
793
7.36M
        return SanitizeType(ComputeType(fragment, x, y, z, sub_types, k, data.size(), subs.size(), keys.size(), m_script_ctx));
794
7.36M
    }
miniscript::Node<CPubKey>::CalcType() const
Line
Count
Source
780
28.7k
    Type CalcType() const {
781
28.7k
        using namespace internal;
782
783
        // THRESH has a variable number of subexpressions
784
28.7k
        std::vector<Type> sub_types;
785
28.7k
        if (fragment == Fragment::THRESH) {
786
679
            for (const auto& sub : subs) sub_types.push_back(sub.GetType());
787
140
        }
788
        // All other nodes than THRESH can be computed just from the types of the 0-3 subexpressions.
789
28.7k
        Type x = subs.size() > 0 ? subs[0].GetType() : ""_mst;
790
28.7k
        Type y = subs.size() > 1 ? subs[1].GetType() : ""_mst;
791
28.7k
        Type z = subs.size() > 2 ? subs[2].GetType() : ""_mst;
792
793
28.7k
        return SanitizeType(ComputeType(fragment, x, y, z, sub_types, k, data.size(), subs.size(), keys.size(), m_script_ctx));
794
28.7k
    }
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
814
            for (const auto& sub : subs) sub_types.push_back(sub.GetType());
787
242
        }
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
5.61M
    Type CalcType() const {
781
5.61M
        using namespace internal;
782
783
        // THRESH has a variable number of subexpressions
784
5.61M
        std::vector<Type> sub_types;
785
5.61M
        if (fragment == Fragment::THRESH) {
786
24
            for (const auto& sub : subs) sub_types.push_back(sub.GetType());
787
8
        }
788
        // All other nodes than THRESH can be computed just from the types of the 0-3 subexpressions.
789
5.61M
        Type x = subs.size() > 0 ? subs[0].GetType() : ""_mst;
790
5.61M
        Type y = subs.size() > 1 ? subs[1].GetType() : ""_mst;
791
5.61M
        Type z = subs.size() > 2 ? subs[2].GetType() : ""_mst;
792
793
5.61M
        return SanitizeType(ComputeType(fragment, x, y, z, sub_types, k, data.size(), subs.size(), keys.size(), m_script_ctx));
794
5.61M
    }
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.58k
                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
125
                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.68k
                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.19k
                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
101
                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
2.35k
    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
11.2M
        auto downfn = [](bool, const Node& node, size_t) {
888
11.2M
            return (node.fragment == Fragment::WRAP_A || node.fragment == Fragment::WRAP_S ||
889
11.2M
                    node.fragment == Fragment::WRAP_D || node.fragment == Fragment::WRAP_V ||
890
11.2M
                    node.fragment == Fragment::WRAP_J || node.fragment == Fragment::WRAP_N ||
891
11.2M
                    node.fragment == Fragment::WRAP_C ||
892
11.2M
                    (node.fragment == Fragment::AND_V && node.subs[1].fragment == Fragment::JUST_1) ||
893
11.2M
                    (node.fragment == Fragment::OR_I && node.subs[0].fragment == Fragment::JUST_0) ||
894
11.2M
                    (node.fragment == Fragment::OR_I && node.subs[1].fragment == Fragment::JUST_0));
895
11.2M
        };
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
11.2M
        auto downfn = [](bool, const Node& node, size_t) {
888
11.2M
            return (node.fragment == Fragment::WRAP_A || node.fragment == Fragment::WRAP_S ||
889
11.2M
                    node.fragment == Fragment::WRAP_D || node.fragment == Fragment::WRAP_V ||
890
11.2M
                    node.fragment == Fragment::WRAP_J || node.fragment == Fragment::WRAP_N ||
891
11.2M
                    node.fragment == Fragment::WRAP_C ||
892
11.2M
                    (node.fragment == Fragment::AND_V && node.subs[1].fragment == Fragment::JUST_1) ||
893
11.2M
                    (node.fragment == Fragment::OR_I && node.subs[0].fragment == Fragment::JUST_0) ||
894
11.2M
                    (node.fragment == Fragment::OR_I && node.subs[1].fragment == Fragment::JUST_0));
895
11.2M
        };
896
10.3k
        auto toString = [&ctx, &has_priv_key](Key key) -> std::optional<std::string> {
897
10.3k
            bool fragment_has_priv_key{false};
898
10.3k
            auto key_str{ctx.ToString(key, fragment_has_priv_key)};
899
10.3k
            if (key_str) has_priv_key = has_priv_key || fragment_has_priv_key;
900
10.3k
            return key_str;
901
10.3k
        };
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
10.3k
        auto toString = [&ctx, &has_priv_key](Key key) -> std::optional<std::string> {
897
10.3k
            bool fragment_has_priv_key{false};
898
10.3k
            auto key_str{ctx.ToString(key, fragment_has_priv_key)};
899
10.3k
            if (key_str) has_priv_key = has_priv_key || fragment_has_priv_key;
900
10.3k
            return key_str;
901
10.3k
        };
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
2.35k
        const bool is_tapscript{IsTapscript(m_script_ctx)};
905
11.2M
        auto upfn = [is_tapscript, &toString](bool wrapped, const Node& node, std::span<std::string> subs) -> std::optional<std::string> {
906
11.2M
            std::string ret = wrapped ? ":" : "";
907
908
11.2M
            switch (node.fragment) {
909
1.00k
                case Fragment::WRAP_A: return "a" + std::move(subs[0]);
910
528
                case Fragment::WRAP_S: return "s" + std::move(subs[0]);
911
4.86k
                case Fragment::WRAP_C:
912
4.86k
                    if (node.subs[0].fragment == Fragment::PK_K) {
913
                        // pk(K) is syntactic sugar for c:pk_k(K)
914
3.69k
                        auto key_str = toString(node.subs[0].keys[0]);
915
3.69k
                        if (!key_str) return {};
916
3.69k
                        return std::move(ret) + "pk(" + std::move(*key_str) + ")";
917
3.69k
                    }
918
1.16k
                    if (node.subs[0].fragment == Fragment::PK_H) {
919
                        // pkh(K) is syntactic sugar for c:pk_h(K)
920
1.12k
                        auto key_str = toString(node.subs[0].keys[0]);
921
1.12k
                        if (!key_str) return {};
922
1.12k
                        return std::move(ret) + "pkh(" + std::move(*key_str) + ")";
923
1.12k
                    }
924
47
                    return "c" + std::move(subs[0]);
925
142
                case Fragment::WRAP_D: return "d" + std::move(subs[0]);
926
2.31k
                case Fragment::WRAP_V: return "v" + std::move(subs[0]);
927
0
                case Fragment::WRAP_J: return "j" + std::move(subs[0]);
928
11.2M
                case Fragment::WRAP_N: return "n" + std::move(subs[0]);
929
2.17k
                case Fragment::AND_V:
930
                    // t:X is syntactic sugar for and_v(X,1).
931
2.17k
                    if (node.subs[1].fragment == Fragment::JUST_1) return "t" + std::move(subs[0]);
932
2.16k
                    break;
933
2.16k
                case Fragment::OR_I:
934
307
                    if (node.subs[0].fragment == Fragment::JUST_0) return "l" + std::move(subs[1]);
935
172
                    if (node.subs[1].fragment == Fragment::JUST_0) return "u" + std::move(subs[0]);
936
172
                    break;
937
8.89k
                default: break;
938
11.2M
            }
939
11.2k
            switch (node.fragment) {
940
3.78k
                case Fragment::PK_K: {
941
3.78k
                    auto key_str = toString(node.keys[0]);
942
3.78k
                    if (!key_str) return {};
943
3.78k
                    return std::move(ret) + "pk_k(" + std::move(*key_str) + ")";
944
3.78k
                }
945
1.12k
                case Fragment::PK_H: {
946
1.12k
                    auto key_str = toString(node.keys[0]);
947
1.12k
                    if (!key_str) return {};
948
1.12k
                    return std::move(ret) + "pk_h(" + std::move(*key_str) + ")";
949
1.12k
                }
950
783
                case Fragment::AFTER: return std::move(ret) + "after(" + util::ToString(node.k) + ")";
951
804
                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
95
                case Fragment::HASH160: return std::move(ret) + "hash160(" + HexStr(node.data) + ")";
954
125
                case Fragment::SHA256: return std::move(ret) + "sha256(" + HexStr(node.data) + ")";
955
46
                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
2.16k
                case Fragment::AND_V: return std::move(ret) + "and_v(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
959
624
                case Fragment::AND_B: return std::move(ret) + "and_b(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
960
172
                case Fragment::OR_B: return std::move(ret) + "or_b(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
961
134
                case Fragment::OR_D: return std::move(ret) + "or_d(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
962
70
                case Fragment::OR_C: return std::move(ret) + "or_c(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
963
172
                case Fragment::OR_I: return std::move(ret) + "or_i(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
964
336
                case Fragment::ANDOR:
965
                    // and_n(X,Y) is syntactic sugar for andor(X,Y,0).
966
336
                    if (node.subs[2].fragment == Fragment::JUST_0) return std::move(ret) + "and_n(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
967
288
                    return std::move(ret) + "andor(" + std::move(subs[0]) + "," + std::move(subs[1]) + "," + std::move(subs[2]) + ")";
968
125
                case Fragment::MULTI: {
969
125
                    CHECK_NONFATAL(!is_tapscript);
970
125
                    auto str = std::move(ret) + "multi(" + util::ToString(node.k);
971
318
                    for (const auto& key : node.keys) {
972
318
                        auto key_str = toString(key);
973
318
                        if (!key_str) return {};
974
318
                        str += "," + std::move(*key_str);
975
318
                    }
976
125
                    return std::move(str) + ")";
977
125
                }
978
129
                case Fragment::MULTI_A: {
979
129
                    CHECK_NONFATAL(is_tapscript);
980
129
                    auto str = std::move(ret) + "multi_a(" + util::ToString(node.k);
981
314
                    for (const auto& key : node.keys) {
982
314
                        auto key_str = toString(key);
983
314
                        if (!key_str) return {};
984
314
                        str += "," + std::move(*key_str);
985
314
                    }
986
129
                    return std::move(str) + ")";
987
129
                }
988
312
                case Fragment::THRESH: {
989
312
                    auto str = std::move(ret) + "thresh(" + util::ToString(node.k);
990
1.05k
                    for (auto& sub : subs) {
991
1.05k
                        str += "," + std::move(sub);
992
1.05k
                    }
993
312
                    return std::move(str) + ")";
994
129
                }
995
0
                default: break;
996
11.2k
            }
997
11.2k
            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
11.2M
        auto upfn = [is_tapscript, &toString](bool wrapped, const Node& node, std::span<std::string> subs) -> std::optional<std::string> {
906
11.2M
            std::string ret = wrapped ? ":" : "";
907
908
11.2M
            switch (node.fragment) {
909
1.00k
                case Fragment::WRAP_A: return "a" + std::move(subs[0]);
910
522
                case Fragment::WRAP_S: return "s" + std::move(subs[0]);
911
4.84k
                case Fragment::WRAP_C:
912
4.84k
                    if (node.subs[0].fragment == Fragment::PK_K) {
913
                        // pk(K) is syntactic sugar for c:pk_k(K)
914
3.67k
                        auto key_str = toString(node.subs[0].keys[0]);
915
3.67k
                        if (!key_str) return {};
916
3.67k
                        return std::move(ret) + "pk(" + std::move(*key_str) + ")";
917
3.67k
                    }
918
1.16k
                    if (node.subs[0].fragment == Fragment::PK_H) {
919
                        // pkh(K) is syntactic sugar for c:pk_h(K)
920
1.11k
                        auto key_str = toString(node.subs[0].keys[0]);
921
1.11k
                        if (!key_str) return {};
922
1.11k
                        return std::move(ret) + "pkh(" + std::move(*key_str) + ")";
923
1.11k
                    }
924
45
                    return "c" + std::move(subs[0]);
925
142
                case Fragment::WRAP_D: return "d" + std::move(subs[0]);
926
2.30k
                case Fragment::WRAP_V: return "v" + std::move(subs[0]);
927
0
                case Fragment::WRAP_J: return "j" + std::move(subs[0]);
928
11.2M
                case Fragment::WRAP_N: return "n" + std::move(subs[0]);
929
2.16k
                case Fragment::AND_V:
930
                    // t:X is syntactic sugar for and_v(X,1).
931
2.16k
                    if (node.subs[1].fragment == Fragment::JUST_1) return "t" + std::move(subs[0]);
932
2.16k
                    break;
933
2.16k
                case Fragment::OR_I:
934
305
                    if (node.subs[0].fragment == Fragment::JUST_0) return "l" + std::move(subs[1]);
935
170
                    if (node.subs[1].fragment == Fragment::JUST_0) return "u" + std::move(subs[0]);
936
170
                    break;
937
8.84k
                default: break;
938
11.2M
            }
939
11.1k
            switch (node.fragment) {
940
3.76k
                case Fragment::PK_K: {
941
3.76k
                    auto key_str = toString(node.keys[0]);
942
3.76k
                    if (!key_str) return {};
943
3.76k
                    return std::move(ret) + "pk_k(" + std::move(*key_str) + ")";
944
3.76k
                }
945
1.11k
                case Fragment::PK_H: {
946
1.11k
                    auto key_str = toString(node.keys[0]);
947
1.11k
                    if (!key_str) return {};
948
1.11k
                    return std::move(ret) + "pk_h(" + std::move(*key_str) + ")";
949
1.11k
                }
950
779
                case Fragment::AFTER: return std::move(ret) + "after(" + util::ToString(node.k) + ")";
951
796
                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
95
                case Fragment::HASH160: return std::move(ret) + "hash160(" + HexStr(node.data) + ")";
954
123
                case Fragment::SHA256: return std::move(ret) + "sha256(" + HexStr(node.data) + ")";
955
45
                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
2.16k
                case Fragment::AND_V: return std::move(ret) + "and_v(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
959
616
                case Fragment::AND_B: return std::move(ret) + "and_b(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
960
168
                case Fragment::OR_B: return std::move(ret) + "or_b(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
961
134
                case Fragment::OR_D: return std::move(ret) + "or_d(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
962
70
                case Fragment::OR_C: return std::move(ret) + "or_c(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
963
170
                case Fragment::OR_I: return std::move(ret) + "or_i(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
964
334
                case Fragment::ANDOR:
965
                    // and_n(X,Y) is syntactic sugar for andor(X,Y,0).
966
334
                    if (node.subs[2].fragment == Fragment::JUST_0) return std::move(ret) + "and_n(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
967
286
                    return std::move(ret) + "andor(" + std::move(subs[0]) + "," + std::move(subs[1]) + "," + std::move(subs[2]) + ")";
968
125
                case Fragment::MULTI: {
969
125
                    CHECK_NONFATAL(!is_tapscript);
970
125
                    auto str = std::move(ret) + "multi(" + util::ToString(node.k);
971
318
                    for (const auto& key : node.keys) {
972
318
                        auto key_str = toString(key);
973
318
                        if (!key_str) return {};
974
318
                        str += "," + std::move(*key_str);
975
318
                    }
976
125
                    return std::move(str) + ")";
977
125
                }
978
129
                case Fragment::MULTI_A: {
979
129
                    CHECK_NONFATAL(is_tapscript);
980
129
                    auto str = std::move(ret) + "multi_a(" + util::ToString(node.k);
981
314
                    for (const auto& key : node.keys) {
982
314
                        auto key_str = toString(key);
983
314
                        if (!key_str) return {};
984
314
                        str += "," + std::move(*key_str);
985
314
                    }
986
129
                    return std::move(str) + ")";
987
129
                }
988
312
                case Fragment::THRESH: {
989
312
                    auto str = std::move(ret) + "thresh(" + util::ToString(node.k);
990
1.05k
                    for (auto& sub : subs) {
991
1.05k
                        str += "," + std::move(sub);
992
1.05k
                    }
993
312
                    return std::move(str) + ")";
994
129
                }
995
0
                default: break;
996
11.1k
            }
997
11.1k
            assert(false);
998
0
        };
999
1000
2.35k
        return TreeEvalMaybe<std::string>(false, downfn, upfn);
1001
2.35k
    }
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
2.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
2.34k
        auto downfn = [](bool, const Node& node, size_t) {
888
2.34k
            return (node.fragment == Fragment::WRAP_A || node.fragment == Fragment::WRAP_S ||
889
2.34k
                    node.fragment == Fragment::WRAP_D || node.fragment == Fragment::WRAP_V ||
890
2.34k
                    node.fragment == Fragment::WRAP_J || node.fragment == Fragment::WRAP_N ||
891
2.34k
                    node.fragment == Fragment::WRAP_C ||
892
2.34k
                    (node.fragment == Fragment::AND_V && node.subs[1].fragment == Fragment::JUST_1) ||
893
2.34k
                    (node.fragment == Fragment::OR_I && node.subs[0].fragment == Fragment::JUST_0) ||
894
2.34k
                    (node.fragment == Fragment::OR_I && node.subs[1].fragment == Fragment::JUST_0));
895
2.34k
        };
896
2.34k
        auto toString = [&ctx, &has_priv_key](Key key) -> std::optional<std::string> {
897
2.34k
            bool fragment_has_priv_key{false};
898
2.34k
            auto key_str{ctx.ToString(key, fragment_has_priv_key)};
899
2.34k
            if (key_str) has_priv_key = has_priv_key || fragment_has_priv_key;
900
2.34k
            return key_str;
901
2.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
2.34k
        const bool is_tapscript{IsTapscript(m_script_ctx)};
905
2.34k
        auto upfn = [is_tapscript, &toString](bool wrapped, const Node& node, std::span<std::string> subs) -> std::optional<std::string> {
906
2.34k
            std::string ret = wrapped ? ":" : "";
907
908
2.34k
            switch (node.fragment) {
909
2.34k
                case Fragment::WRAP_A: return "a" + std::move(subs[0]);
910
2.34k
                case Fragment::WRAP_S: return "s" + std::move(subs[0]);
911
2.34k
                case Fragment::WRAP_C:
912
2.34k
                    if (node.subs[0].fragment == Fragment::PK_K) {
913
                        // pk(K) is syntactic sugar for c:pk_k(K)
914
2.34k
                        auto key_str = toString(node.subs[0].keys[0]);
915
2.34k
                        if (!key_str) return {};
916
2.34k
                        return std::move(ret) + "pk(" + std::move(*key_str) + ")";
917
2.34k
                    }
918
2.34k
                    if (node.subs[0].fragment == Fragment::PK_H) {
919
                        // pkh(K) is syntactic sugar for c:pk_h(K)
920
2.34k
                        auto key_str = toString(node.subs[0].keys[0]);
921
2.34k
                        if (!key_str) return {};
922
2.34k
                        return std::move(ret) + "pkh(" + std::move(*key_str) + ")";
923
2.34k
                    }
924
2.34k
                    return "c" + std::move(subs[0]);
925
2.34k
                case Fragment::WRAP_D: return "d" + std::move(subs[0]);
926
2.34k
                case Fragment::WRAP_V: return "v" + std::move(subs[0]);
927
2.34k
                case Fragment::WRAP_J: return "j" + std::move(subs[0]);
928
2.34k
                case Fragment::WRAP_N: return "n" + std::move(subs[0]);
929
2.34k
                case Fragment::AND_V:
930
                    // t:X is syntactic sugar for and_v(X,1).
931
2.34k
                    if (node.subs[1].fragment == Fragment::JUST_1) return "t" + std::move(subs[0]);
932
2.34k
                    break;
933
2.34k
                case Fragment::OR_I:
934
2.34k
                    if (node.subs[0].fragment == Fragment::JUST_0) return "l" + std::move(subs[1]);
935
2.34k
                    if (node.subs[1].fragment == Fragment::JUST_0) return "u" + std::move(subs[0]);
936
2.34k
                    break;
937
2.34k
                default: break;
938
2.34k
            }
939
2.34k
            switch (node.fragment) {
940
2.34k
                case Fragment::PK_K: {
941
2.34k
                    auto key_str = toString(node.keys[0]);
942
2.34k
                    if (!key_str) return {};
943
2.34k
                    return std::move(ret) + "pk_k(" + std::move(*key_str) + ")";
944
2.34k
                }
945
2.34k
                case Fragment::PK_H: {
946
2.34k
                    auto key_str = toString(node.keys[0]);
947
2.34k
                    if (!key_str) return {};
948
2.34k
                    return std::move(ret) + "pk_h(" + std::move(*key_str) + ")";
949
2.34k
                }
950
2.34k
                case Fragment::AFTER: return std::move(ret) + "after(" + util::ToString(node.k) + ")";
951
2.34k
                case Fragment::OLDER: return std::move(ret) + "older(" + util::ToString(node.k) + ")";
952
2.34k
                case Fragment::HASH256: return std::move(ret) + "hash256(" + HexStr(node.data) + ")";
953
2.34k
                case Fragment::HASH160: return std::move(ret) + "hash160(" + HexStr(node.data) + ")";
954
2.34k
                case Fragment::SHA256: return std::move(ret) + "sha256(" + HexStr(node.data) + ")";
955
2.34k
                case Fragment::RIPEMD160: return std::move(ret) + "ripemd160(" + HexStr(node.data) + ")";
956
2.34k
                case Fragment::JUST_1: return std::move(ret) + "1";
957
2.34k
                case Fragment::JUST_0: return std::move(ret) + "0";
958
2.34k
                case Fragment::AND_V: return std::move(ret) + "and_v(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
959
2.34k
                case Fragment::AND_B: return std::move(ret) + "and_b(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
960
2.34k
                case Fragment::OR_B: return std::move(ret) + "or_b(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
961
2.34k
                case Fragment::OR_D: return std::move(ret) + "or_d(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
962
2.34k
                case Fragment::OR_C: return std::move(ret) + "or_c(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
963
2.34k
                case Fragment::OR_I: return std::move(ret) + "or_i(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
964
2.34k
                case Fragment::ANDOR:
965
                    // and_n(X,Y) is syntactic sugar for andor(X,Y,0).
966
2.34k
                    if (node.subs[2].fragment == Fragment::JUST_0) return std::move(ret) + "and_n(" + std::move(subs[0]) + "," + std::move(subs[1]) + ")";
967
2.34k
                    return std::move(ret) + "andor(" + std::move(subs[0]) + "," + std::move(subs[1]) + "," + std::move(subs[2]) + ")";
968
2.34k
                case Fragment::MULTI: {
969
2.34k
                    CHECK_NONFATAL(!is_tapscript);
970
2.34k
                    auto str = std::move(ret) + "multi(" + util::ToString(node.k);
971
2.34k
                    for (const auto& key : node.keys) {
972
2.34k
                        auto key_str = toString(key);
973
2.34k
                        if (!key_str) return {};
974
2.34k
                        str += "," + std::move(*key_str);
975
2.34k
                    }
976
2.34k
                    return std::move(str) + ")";
977
2.34k
                }
978
2.34k
                case Fragment::MULTI_A: {
979
2.34k
                    CHECK_NONFATAL(is_tapscript);
980
2.34k
                    auto str = std::move(ret) + "multi_a(" + util::ToString(node.k);
981
2.34k
                    for (const auto& key : node.keys) {
982
2.34k
                        auto key_str = toString(key);
983
2.34k
                        if (!key_str) return {};
984
2.34k
                        str += "," + std::move(*key_str);
985
2.34k
                    }
986
2.34k
                    return std::move(str) + ")";
987
2.34k
                }
988
2.34k
                case Fragment::THRESH: {
989
2.34k
                    auto str = std::move(ret) + "thresh(" + util::ToString(node.k);
990
2.34k
                    for (auto& sub : subs) {
991
2.34k
                        str += "," + std::move(sub);
992
2.34k
                    }
993
2.34k
                    return std::move(str) + ")";
994
2.34k
                }
995
2.34k
                default: break;
996
2.34k
            }
997
2.34k
            assert(false);
998
2.34k
        };
999
1000
2.34k
        return TreeEvalMaybe<std::string>(false, downfn, upfn);
1001
2.34k
    }
1002
1003
private:
1004
7.36M
    internal::Ops CalcOps() const {
1005
7.36M
        switch (fragment) {
1006
245
            case Fragment::JUST_1: return {0, 0, {}};
1007
709
            case Fragment::JUST_0: return {0, {}, 0};
1008
7.09k
            case Fragment::PK_K: return {0, 0, 0};
1009
874
            case Fragment::PK_H: return {3, 0, 0};
1010
7.96k
            case Fragment::OLDER:
1011
9.43k
            case Fragment::AFTER: return {1, 0, {}};
1012
110
            case Fragment::SHA256:
1013
187
            case Fragment::RIPEMD160:
1014
303
            case Fragment::HASH256:
1015
400
            case Fragment::HASH160: return {4, 0, {}};
1016
2.11k
            case Fragment::AND_V: return {subs[0].ops.count + subs[1].ops.count, subs[0].ops.sat + subs[1].ops.sat, {}};
1017
7.11k
            case Fragment::AND_B: {
1018
7.11k
                const auto count{1 + subs[0].ops.count + subs[1].ops.count};
1019
7.11k
                const auto sat{subs[0].ops.sat + subs[1].ops.sat};
1020
7.11k
                const auto dsat{subs[0].ops.dsat + subs[1].ops.dsat};
1021
7.11k
                return {count, sat, dsat};
1022
303
            }
1023
142
            case Fragment::OR_B: {
1024
142
                const auto count{1 + subs[0].ops.count + subs[1].ops.count};
1025
142
                const auto sat{(subs[0].ops.sat + subs[1].ops.dsat) | (subs[1].ops.sat + subs[0].ops.dsat)};
1026
142
                const auto dsat{subs[0].ops.dsat + subs[1].ops.dsat};
1027
142
                return {count, sat, dsat};
1028
303
            }
1029
136
            case Fragment::OR_D: {
1030
136
                const auto count{3 + subs[0].ops.count + subs[1].ops.count};
1031
136
                const auto sat{subs[0].ops.sat | (subs[1].ops.sat + subs[0].ops.dsat)};
1032
136
                const auto dsat{subs[0].ops.dsat + subs[1].ops.dsat};
1033
136
                return {count, sat, dsat};
1034
303
            }
1035
60
            case Fragment::OR_C: {
1036
60
                const auto count{2 + subs[0].ops.count + subs[1].ops.count};
1037
60
                const auto sat{subs[0].ops.sat | (subs[1].ops.sat + subs[0].ops.dsat)};
1038
60
                return {count, sat, {}};
1039
303
            }
1040
653
            case Fragment::OR_I: {
1041
653
                const auto count{3 + subs[0].ops.count + subs[1].ops.count};
1042
653
                const auto sat{subs[0].ops.sat | subs[1].ops.sat};
1043
653
                const auto dsat{subs[0].ops.dsat | subs[1].ops.dsat};
1044
653
                return {count, sat, dsat};
1045
303
            }
1046
271
            case Fragment::ANDOR: {
1047
271
                const auto count{3 + subs[0].ops.count + subs[1].ops.count + subs[2].ops.count};
1048
271
                const auto sat{(subs[1].ops.sat + subs[0].ops.sat) | (subs[0].ops.dsat + subs[2].ops.sat)};
1049
271
                const auto dsat{subs[0].ops.dsat + subs[2].ops.dsat};
1050
271
                return {count, sat, dsat};
1051
303
            }
1052
173
            case Fragment::MULTI: return {1, (uint32_t)keys.size(), (uint32_t)keys.size()};
1053
830
            case Fragment::MULTI_A: return {(uint32_t)keys.size() + 1, 0, 0};
1054
916
            case Fragment::WRAP_S:
1055
8.79k
            case Fragment::WRAP_C:
1056
7.32M
            case Fragment::WRAP_N: return {1 + subs[0].ops.count, subs[0].ops.sat, subs[0].ops.dsat};
1057
7.47k
            case Fragment::WRAP_A: return {2 + subs[0].ops.count, subs[0].ops.sat, subs[0].ops.dsat};
1058
122
            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.26k
            case Fragment::WRAP_V: return {subs[0].ops.count + (subs[0].GetType() << "x"_mst), subs[0].ops.sat, {}};
1061
390
            case Fragment::THRESH: {
1062
390
                uint32_t count = 0;
1063
390
                auto sats = Vector(internal::MaxInt<uint32_t>(0));
1064
1.51k
                for (const auto& sub : subs) {
1065
1.51k
                    count += sub.ops.count + 1;
1066
1.51k
                    auto next_sats = Vector(sats[0] + sub.ops.dsat);
1067
4.52k
                    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.51k
                    next_sats.push_back(sats[sats.size() - 1] + sub.ops.sat);
1069
1.51k
                    sats = std::move(next_sats);
1070
1.51k
                }
1071
390
                assert(k < sats.size());
1072
390
                return {count, sats[k], sats[0]};
1073
390
            }
1074
7.36M
        }
1075
7.36M
        assert(false);
1076
0
    }
miniscript::Node<CPubKey>::CalcOps() const
Line
Count
Source
1004
28.7k
    internal::Ops CalcOps() const {
1005
28.7k
        switch (fragment) {
1006
232
            case Fragment::JUST_1: return {0, 0, {}};
1007
449
            case Fragment::JUST_0: return {0, {}, 0};
1008
1.79k
            case Fragment::PK_K: return {0, 0, 0};
1009
114
            case Fragment::PK_H: return {3, 0, 0};
1010
7.59k
            case Fragment::OLDER:
1011
7.98k
            case Fragment::AFTER: return {1, 0, {}};
1012
68
            case Fragment::SHA256:
1013
94
            case Fragment::RIPEMD160:
1014
134
            case Fragment::HASH256:
1015
158
            case Fragment::HASH160: return {4, 0, {}};
1016
305
            case Fragment::AND_V: return {subs[0].ops.count + subs[1].ops.count, subs[0].ops.sat + subs[1].ops.sat, {}};
1017
6.85k
            case Fragment::AND_B: {
1018
6.85k
                const auto count{1 + subs[0].ops.count + subs[1].ops.count};
1019
6.85k
                const auto sat{subs[0].ops.sat + subs[1].ops.sat};
1020
6.85k
                const auto dsat{subs[0].ops.dsat + subs[1].ops.dsat};
1021
6.85k
                return {count, sat, dsat};
1022
134
            }
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
134
            }
1029
52
            case Fragment::OR_D: {
1030
52
                const auto count{3 + subs[0].ops.count + subs[1].ops.count};
1031
52
                const auto sat{subs[0].ops.sat | (subs[1].ops.sat + subs[0].ops.dsat)};
1032
52
                const auto dsat{subs[0].ops.dsat + subs[1].ops.dsat};
1033
52
                return {count, sat, dsat};
1034
134
            }
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
134
            }
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
134
            }
1046
135
            case Fragment::ANDOR: {
1047
135
                const auto count{3 + subs[0].ops.count + subs[1].ops.count + subs[2].ops.count};
1048
135
                const auto sat{(subs[1].ops.sat + subs[0].ops.sat) | (subs[0].ops.dsat + subs[2].ops.sat)};
1049
135
                const auto dsat{subs[0].ops.dsat + subs[2].ops.dsat};
1050
135
                return {count, sat, dsat};
1051
134
            }
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
471
            case Fragment::WRAP_S:
1055
2.32k
            case Fragment::WRAP_C:
1056
2.62k
            case Fragment::WRAP_N: return {1 + subs[0].ops.count, subs[0].ops.sat, subs[0].ops.dsat};
1057
6.96k
            case Fragment::WRAP_A: return {2 + subs[0].ops.count, subs[0].ops.sat, subs[0].ops.dsat};
1058
44
            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
369
            case Fragment::WRAP_V: return {subs[0].ops.count + (subs[0].GetType() << "x"_mst), subs[0].ops.sat, {}};
1061
140
            case Fragment::THRESH: {
1062
140
                uint32_t count = 0;
1063
140
                auto sats = Vector(internal::MaxInt<uint32_t>(0));
1064
679
                for (const auto& sub : subs) {
1065
679
                    count += sub.ops.count + 1;
1066
679
                    auto next_sats = Vector(sats[0] + sub.ops.dsat);
1067
2.35k
                    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
679
                    next_sats.push_back(sats[sats.size() - 1] + sub.ops.sat);
1069
679
                    sats = std::move(next_sats);
1070
679
                }
1071
140
                assert(k < sats.size());
1072
140
                return {count, sats[k], sats[0]};
1073
140
            }
1074
28.7k
        }
1075
28.7k
        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.69k
            case Fragment::PK_K: return {0, 0, 0};
1009
475
            case Fragment::PK_H: return {3, 0, 0};
1010
330
            case Fragment::OLDER:
1011
669
            case Fragment::AFTER: return {1, 0, {}};
1012
42
            case Fragment::SHA256:
1013
93
            case Fragment::RIPEMD160:
1014
157
            case Fragment::HASH256:
1015
230
            case Fragment::HASH160: return {4, 0, {}};
1016
821
            case Fragment::AND_V: return {subs[0].ops.count + subs[1].ops.count, subs[0].ops.sat + subs[1].ops.sat, {}};
1017
249
            case Fragment::AND_B: {
1018
249
                const auto count{1 + subs[0].ops.count + subs[1].ops.count};
1019
249
                const auto sat{subs[0].ops.sat + subs[1].ops.sat};
1020
249
                const auto dsat{subs[0].ops.dsat + subs[1].ops.dsat};
1021
249
                return {count, sat, dsat};
1022
157
            }
1023
90
            case Fragment::OR_B: {
1024
90
                const auto count{1 + subs[0].ops.count + subs[1].ops.count};
1025
90
                const auto sat{(subs[0].ops.sat + subs[1].ops.dsat) | (subs[1].ops.sat + subs[0].ops.dsat)};
1026
90
                const auto dsat{subs[0].ops.dsat + subs[1].ops.dsat};
1027
90
                return {count, sat, dsat};
1028
157
            }
1029
84
            case Fragment::OR_D: {
1030
84
                const auto count{3 + subs[0].ops.count + subs[1].ops.count};
1031
84
                const auto sat{subs[0].ops.sat | (subs[1].ops.sat + subs[0].ops.dsat)};
1032
84
                const auto dsat{subs[0].ops.dsat + subs[1].ops.dsat};
1033
84
                return {count, sat, dsat};
1034
157
            }
1035
40
            case Fragment::OR_C: {
1036
40
                const auto count{2 + subs[0].ops.count + subs[1].ops.count};
1037
40
                const auto sat{subs[0].ops.sat | (subs[1].ops.sat + subs[0].ops.dsat)};
1038
40
                return {count, sat, {}};
1039
157
            }
1040
254
            case Fragment::OR_I: {
1041
254
                const auto count{3 + subs[0].ops.count + subs[1].ops.count};
1042
254
                const auto sat{subs[0].ops.sat | subs[1].ops.sat};
1043
254
                const auto dsat{subs[0].ops.dsat | subs[1].ops.dsat};
1044
254
                return {count, sat, dsat};
1045
157
            }
1046
136
            case Fragment::ANDOR: {
1047
136
                const auto count{3 + subs[0].ops.count + subs[1].ops.count + subs[2].ops.count};
1048
136
                const auto sat{(subs[1].ops.sat + subs[0].ops.sat) | (subs[0].ops.dsat + subs[2].ops.sat)};
1049
136
                const auto dsat{subs[0].ops.dsat + subs[2].ops.dsat};
1050
136
                return {count, sat, dsat};
1051
157
            }
1052
124
            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
414
            case Fragment::WRAP_S:
1055
2.55k
            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
495
            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
898
            case Fragment::WRAP_V: return {subs[0].ops.count + (subs[0].GetType() << "x"_mst), subs[0].ops.sat, {}};
1061
242
            case Fragment::THRESH: {
1062
242
                uint32_t count = 0;
1063
242
                auto sats = Vector(internal::MaxInt<uint32_t>(0));
1064
814
                for (const auto& sub : subs) {
1065
814
                    count += sub.ops.count + 1;
1066
814
                    auto next_sats = Vector(sats[0] + sub.ops.dsat);
1067
2.12k
                    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
814
                    next_sats.push_back(sats[sats.size() - 1] + sub.ops.sat);
1069
814
                    sats = std::move(next_sats);
1070
814
                }
1071
242
                assert(k < sats.size());
1072
242
                return {count, sats[k], sats[0]};
1073
242
            }
1074
1.72M
        }
1075
1.72M
        assert(false);
1076
0
    }
miniscript::Node<XOnlyPubKey>::CalcOps() const
Line
Count
Source
1004
5.61M
    internal::Ops CalcOps() const {
1005
5.61M
        switch (fragment) {
1006
0
            case Fragment::JUST_1: return {0, 0, {}};
1007
0
            case Fragment::JUST_0: return {0, {}, 0};
1008
3.60k
            case Fragment::PK_K: return {0, 0, 0};
1009
285
            case Fragment::PK_H: return {3, 0, 0};
1010
39
            case Fragment::OLDER:
1011
779
            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
989
            case Fragment::AND_V: return {subs[0].ops.count + subs[1].ops.count, subs[0].ops.sat + subs[1].ops.sat, {}};
1017
8
            case Fragment::AND_B: {
1018
8
                const auto count{1 + subs[0].ops.count + subs[1].ops.count};
1019
8
                const auto sat{subs[0].ops.sat + subs[1].ops.sat};
1020
8
                const auto dsat{subs[0].ops.dsat + subs[1].ops.dsat};
1021
8
                return {count, sat, dsat};
1022
12
            }
1023
23
            case Fragment::OR_B: {
1024
23
                const auto count{1 + subs[0].ops.count + subs[1].ops.count};
1025
23
                const auto sat{(subs[0].ops.sat + subs[1].ops.dsat) | (subs[1].ops.sat + subs[0].ops.dsat)};
1026
23
                const auto dsat{subs[0].ops.dsat + subs[1].ops.dsat};
1027
23
                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
793
            case Fragment::MULTI_A: return {(uint32_t)keys.size() + 1, 0, 0};
1054
31
            case Fragment::WRAP_S:
1055
3.92k
            case Fragment::WRAP_C:
1056
5.60M
            case Fragment::WRAP_N: return {1 + subs[0].ops.count, subs[0].ops.sat, subs[0].ops.dsat};
1057
16
            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
995
            case Fragment::WRAP_V: return {subs[0].ops.count + (subs[0].GetType() << "x"_mst), subs[0].ops.sat, {}};
1061
8
            case Fragment::THRESH: {
1062
8
                uint32_t count = 0;
1063
8
                auto sats = Vector(internal::MaxInt<uint32_t>(0));
1064
24
                for (const auto& sub : subs) {
1065
24
                    count += sub.ops.count + 1;
1066
24
                    auto next_sats = Vector(sats[0] + sub.ops.dsat);
1067
48
                    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
24
                    next_sats.push_back(sats[sats.size() - 1] + sub.ops.sat);
1069
24
                    sats = std::move(next_sats);
1070
24
                }
1071
8
                assert(k < sats.size());
1072
8
                return {count, sats[k], sats[0]};
1073
8
            }
1074
5.61M
        }
1075
5.61M
        assert(false);
1076
0
    }
1077
1078
7.36M
    internal::StackSize CalcStackSize() const {
1079
7.36M
        using namespace internal;
1080
7.36M
        switch (fragment) {
1081
709
            case Fragment::JUST_0: return {{}, SatInfo::Push()};
1082
245
            case Fragment::JUST_1: return {SatInfo::Push(), {}};
1083
7.96k
            case Fragment::OLDER:
1084
9.43k
            case Fragment::AFTER: return {SatInfo::Push() + SatInfo::Nop(), {}};
1085
7.09k
            case Fragment::PK_K: return {SatInfo::Push()};
1086
874
            case Fragment::PK_H: return {SatInfo::OP_DUP() + SatInfo::Hash() + SatInfo::Push() + SatInfo::OP_EQUALVERIFY()};
1087
110
            case Fragment::SHA256:
1088
187
            case Fragment::RIPEMD160:
1089
303
            case Fragment::HASH256:
1090
400
            case Fragment::HASH160: return {
1091
400
                SatInfo::OP_SIZE() + SatInfo::Push() + SatInfo::OP_EQUALVERIFY() + SatInfo::Hash() + SatInfo::Push() + SatInfo::OP_EQUAL(),
1092
400
                {}
1093
400
            };
1094
271
            case Fragment::ANDOR: {
1095
271
                const auto& x{subs[0].ss};
1096
271
                const auto& y{subs[1].ss};
1097
271
                const auto& z{subs[2].ss};
1098
271
                return {
1099
271
                    (x.Sat() + SatInfo::If() + y.Sat()) | (x.Dsat() + SatInfo::If() + z.Sat()),
1100
271
                    x.Dsat() + SatInfo::If() + z.Dsat()
1101
271
                };
1102
303
            }
1103
2.11k
            case Fragment::AND_V: {
1104
2.11k
                const auto& x{subs[0].ss};
1105
2.11k
                const auto& y{subs[1].ss};
1106
2.11k
                return {x.Sat() + y.Sat(), {}};
1107
303
            }
1108
7.11k
            case Fragment::AND_B: {
1109
7.11k
                const auto& x{subs[0].ss};
1110
7.11k
                const auto& y{subs[1].ss};
1111
7.11k
                return {x.Sat() + y.Sat() + SatInfo::BinaryOp(), x.Dsat() + y.Dsat() + SatInfo::BinaryOp()};
1112
303
            }
1113
142
            case Fragment::OR_B: {
1114
142
                const auto& x{subs[0].ss};
1115
142
                const auto& y{subs[1].ss};
1116
142
                return {
1117
142
                    ((x.Sat() + y.Dsat()) | (x.Dsat() + y.Sat())) + SatInfo::BinaryOp(),
1118
142
                    x.Dsat() + y.Dsat() + SatInfo::BinaryOp()
1119
142
                };
1120
303
            }
1121
60
            case Fragment::OR_C: {
1122
60
                const auto& x{subs[0].ss};
1123
60
                const auto& y{subs[1].ss};
1124
60
                return {(x.Sat() + SatInfo::If()) | (x.Dsat() + SatInfo::If() + y.Sat()), {}};
1125
303
            }
1126
136
            case Fragment::OR_D: {
1127
136
                const auto& x{subs[0].ss};
1128
136
                const auto& y{subs[1].ss};
1129
136
                return {
1130
136
                    (x.Sat() + SatInfo::OP_IFDUP(true) + SatInfo::If()) | (x.Dsat() + SatInfo::OP_IFDUP(false) + SatInfo::If() + y.Sat()),
1131
136
                    x.Dsat() + SatInfo::OP_IFDUP(false) + SatInfo::If() + y.Dsat()
1132
136
                };
1133
303
            }
1134
653
            case Fragment::OR_I: {
1135
653
                const auto& x{subs[0].ss};
1136
653
                const auto& y{subs[1].ss};
1137
653
                return {SatInfo::If() + (x.Sat() | y.Sat()), SatInfo::If() + (x.Dsat() | y.Dsat())};
1138
303
            }
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
173
            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
830
            case Fragment::MULTI_A: return {SatInfo(keys.size() - 1, keys.size())};
1148
7.47k
            case Fragment::WRAP_A:
1149
7.32M
            case Fragment::WRAP_N:
1150
7.32M
            case Fragment::WRAP_S: return subs[0].ss;
1151
7.88k
            case Fragment::WRAP_C: return {
1152
7.88k
                subs[0].ss.Sat() + SatInfo::OP_CHECKSIG(),
1153
7.88k
                subs[0].ss.Dsat() + SatInfo::OP_CHECKSIG()
1154
7.88k
            };
1155
122
            case Fragment::WRAP_D: return {
1156
122
                SatInfo::OP_DUP() + SatInfo::If() + subs[0].ss.Sat(),
1157
122
                SatInfo::OP_DUP() + SatInfo::If()
1158
122
            };
1159
2.26k
            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
390
            case Fragment::THRESH: {
1165
                // sats[j] is the SatInfo corresponding to all traces reaching j satisfactions.
1166
390
                auto sats = Vector(SatInfo::Empty());
1167
1.90k
                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.51k
                    auto add = i ? SatInfo::BinaryOp() : SatInfo::Empty();
1171
                    // Construct a variable that will become the next sats, starting with index 0.
1172
1.51k
                    auto next_sats = Vector(sats[0] + subs[i].ss.Dsat() + add);
1173
                    // Then loop to construct next_sats[1..i].
1174
4.52k
                    for (size_t j = 1; j < sats.size(); ++j) {
1175
3.01k
                        next_sats.push_back(((sats[j] + subs[i].ss.Dsat()) | (sats[j - 1] + subs[i].ss.Sat())) + add);
1176
3.01k
                    }
1177
                    // Finally construct next_sats[i+1].
1178
1.51k
                    next_sats.push_back(sats[sats.size() - 1] + subs[i].ss.Sat() + add);
1179
                    // Switch over.
1180
1.51k
                    sats = std::move(next_sats);
1181
1.51k
                }
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
390
                return {
1185
390
                    sats[k] + SatInfo::Push() + SatInfo::OP_EQUAL(),
1186
390
                    sats[0] + SatInfo::Push() + SatInfo::OP_EQUAL()
1187
390
                };
1188
7.32M
            }
1189
7.36M
        }
1190
7.36M
        assert(false);
1191
0
    }
miniscript::Node<CPubKey>::CalcStackSize() const
Line
Count
Source
1078
28.7k
    internal::StackSize CalcStackSize() const {
1079
28.7k
        using namespace internal;
1080
28.7k
        switch (fragment) {
1081
449
            case Fragment::JUST_0: return {{}, SatInfo::Push()};
1082
232
            case Fragment::JUST_1: return {SatInfo::Push(), {}};
1083
7.59k
            case Fragment::OLDER:
1084
7.98k
            case Fragment::AFTER: return {SatInfo::Push() + SatInfo::Nop(), {}};
1085
1.79k
            case Fragment::PK_K: return {SatInfo::Push()};
1086
114
            case Fragment::PK_H: return {SatInfo::OP_DUP() + SatInfo::Hash() + SatInfo::Push() + SatInfo::OP_EQUALVERIFY()};
1087
68
            case Fragment::SHA256:
1088
94
            case Fragment::RIPEMD160:
1089
134
            case Fragment::HASH256:
1090
158
            case Fragment::HASH160: return {
1091
158
                SatInfo::OP_SIZE() + SatInfo::Push() + SatInfo::OP_EQUALVERIFY() + SatInfo::Hash() + SatInfo::Push() + SatInfo::OP_EQUAL(),
1092
158
                {}
1093
158
            };
1094
135
            case Fragment::ANDOR: {
1095
135
                const auto& x{subs[0].ss};
1096
135
                const auto& y{subs[1].ss};
1097
135
                const auto& z{subs[2].ss};
1098
135
                return {
1099
135
                    (x.Sat() + SatInfo::If() + y.Sat()) | (x.Dsat() + SatInfo::If() + z.Sat()),
1100
135
                    x.Dsat() + SatInfo::If() + z.Dsat()
1101
135
                };
1102
134
            }
1103
305
            case Fragment::AND_V: {
1104
305
                const auto& x{subs[0].ss};
1105
305
                const auto& y{subs[1].ss};
1106
305
                return {x.Sat() + y.Sat(), {}};
1107
134
            }
1108
6.85k
            case Fragment::AND_B: {
1109
6.85k
                const auto& x{subs[0].ss};
1110
6.85k
                const auto& y{subs[1].ss};
1111
6.85k
                return {x.Sat() + y.Sat() + SatInfo::BinaryOp(), x.Dsat() + y.Dsat() + SatInfo::BinaryOp()};
1112
134
            }
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
134
            }
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
134
            }
1126
52
            case Fragment::OR_D: {
1127
52
                const auto& x{subs[0].ss};
1128
52
                const auto& y{subs[1].ss};
1129
52
                return {
1130
52
                    (x.Sat() + SatInfo::OP_IFDUP(true) + SatInfo::If()) | (x.Dsat() + SatInfo::OP_IFDUP(false) + SatInfo::If() + y.Sat()),
1131
52
                    x.Dsat() + SatInfo::OP_IFDUP(false) + SatInfo::If() + y.Dsat()
1132
52
                };
1133
134
            }
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
134
            }
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
6.96k
            case Fragment::WRAP_A:
1149
7.27k
            case Fragment::WRAP_N:
1150
7.74k
            case Fragment::WRAP_S: return subs[0].ss;
1151
1.85k
            case Fragment::WRAP_C: return {
1152
1.85k
                subs[0].ss.Sat() + SatInfo::OP_CHECKSIG(),
1153
1.85k
                subs[0].ss.Dsat() + SatInfo::OP_CHECKSIG()
1154
1.85k
            };
1155
44
            case Fragment::WRAP_D: return {
1156
44
                SatInfo::OP_DUP() + SatInfo::If() + subs[0].ss.Sat(),
1157
44
                SatInfo::OP_DUP() + SatInfo::If()
1158
44
            };
1159
369
            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
140
            case Fragment::THRESH: {
1165
                // sats[j] is the SatInfo corresponding to all traces reaching j satisfactions.
1166
140
                auto sats = Vector(SatInfo::Empty());
1167
819
                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
679
                    auto add = i ? SatInfo::BinaryOp() : SatInfo::Empty();
1171
                    // Construct a variable that will become the next sats, starting with index 0.
1172
679
                    auto next_sats = Vector(sats[0] + subs[i].ss.Dsat() + add);
1173
                    // Then loop to construct next_sats[1..i].
1174
2.35k
                    for (size_t j = 1; j < sats.size(); ++j) {
1175
1.67k
                        next_sats.push_back(((sats[j] + subs[i].ss.Dsat()) | (sats[j - 1] + subs[i].ss.Sat())) + add);
1176
1.67k
                    }
1177
                    // Finally construct next_sats[i+1].
1178
679
                    next_sats.push_back(sats[sats.size() - 1] + subs[i].ss.Sat() + add);
1179
                    // Switch over.
1180
679
                    sats = std::move(next_sats);
1181
679
                }
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
140
                return {
1185
140
                    sats[k] + SatInfo::Push() + SatInfo::OP_EQUAL(),
1186
140
                    sats[0] + SatInfo::Push() + SatInfo::OP_EQUAL()
1187
140
                };
1188
7.27k
            }
1189
28.7k
        }
1190
28.7k
        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
330
            case Fragment::OLDER:
1084
669
            case Fragment::AFTER: return {SatInfo::Push() + SatInfo::Nop(), {}};
1085
1.69k
            case Fragment::PK_K: return {SatInfo::Push()};
1086
475
            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
230
            case Fragment::HASH160: return {
1091
230
                SatInfo::OP_SIZE() + SatInfo::Push() + SatInfo::OP_EQUALVERIFY() + SatInfo::Hash() + SatInfo::Push() + SatInfo::OP_EQUAL(),
1092
230
                {}
1093
230
            };
1094
136
            case Fragment::ANDOR: {
1095
136
                const auto& x{subs[0].ss};
1096
136
                const auto& y{subs[1].ss};
1097
136
                const auto& z{subs[2].ss};
1098
136
                return {
1099
136
                    (x.Sat() + SatInfo::If() + y.Sat()) | (x.Dsat() + SatInfo::If() + z.Sat()),
1100
136
                    x.Dsat() + SatInfo::If() + z.Dsat()
1101
136
                };
1102
157
            }
1103
821
            case Fragment::AND_V: {
1104
821
                const auto& x{subs[0].ss};
1105
821
                const auto& y{subs[1].ss};
1106
821
                return {x.Sat() + y.Sat(), {}};
1107
157
            }
1108
249
            case Fragment::AND_B: {
1109
249
                const auto& x{subs[0].ss};
1110
249
                const auto& y{subs[1].ss};
1111
249
                return {x.Sat() + y.Sat() + SatInfo::BinaryOp(), x.Dsat() + y.Dsat() + SatInfo::BinaryOp()};
1112
157
            }
1113
90
            case Fragment::OR_B: {
1114
90
                const auto& x{subs[0].ss};
1115
90
                const auto& y{subs[1].ss};
1116
90
                return {
1117
90
                    ((x.Sat() + y.Dsat()) | (x.Dsat() + y.Sat())) + SatInfo::BinaryOp(),
1118
90
                    x.Dsat() + y.Dsat() + SatInfo::BinaryOp()
1119
90
                };
1120
157
            }
1121
40
            case Fragment::OR_C: {
1122
40
                const auto& x{subs[0].ss};
1123
40
                const auto& y{subs[1].ss};
1124
40
                return {(x.Sat() + SatInfo::If()) | (x.Dsat() + SatInfo::If() + y.Sat()), {}};
1125
157
            }
1126
84
            case Fragment::OR_D: {
1127
84
                const auto& x{subs[0].ss};
1128
84
                const auto& y{subs[1].ss};
1129
84
                return {
1130
84
                    (x.Sat() + SatInfo::OP_IFDUP(true) + SatInfo::If()) | (x.Dsat() + SatInfo::OP_IFDUP(false) + SatInfo::If() + y.Sat()),
1131
84
                    x.Dsat() + SatInfo::OP_IFDUP(false) + SatInfo::If() + y.Dsat()
1132
84
                };
1133
157
            }
1134
254
            case Fragment::OR_I: {
1135
254
                const auto& x{subs[0].ss};
1136
254
                const auto& y{subs[1].ss};
1137
254
                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
124
            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
495
            case Fragment::WRAP_A:
1149
1.71M
            case Fragment::WRAP_N:
1150
1.71M
            case Fragment::WRAP_S: return subs[0].ss;
1151
2.13k
            case Fragment::WRAP_C: return {
1152
2.13k
                subs[0].ss.Sat() + SatInfo::OP_CHECKSIG(),
1153
2.13k
                subs[0].ss.Dsat() + SatInfo::OP_CHECKSIG()
1154
2.13k
            };
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
898
            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
242
            case Fragment::THRESH: {
1165
                // sats[j] is the SatInfo corresponding to all traces reaching j satisfactions.
1166
242
                auto sats = Vector(SatInfo::Empty());
1167
1.05k
                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
814
                    auto add = i ? SatInfo::BinaryOp() : SatInfo::Empty();
1171
                    // Construct a variable that will become the next sats, starting with index 0.
1172
814
                    auto next_sats = Vector(sats[0] + subs[i].ss.Dsat() + add);
1173
                    // Then loop to construct next_sats[1..i].
1174
2.12k
                    for (size_t j = 1; j < sats.size(); ++j) {
1175
1.31k
                        next_sats.push_back(((sats[j] + subs[i].ss.Dsat()) | (sats[j - 1] + subs[i].ss.Sat())) + add);
1176
1.31k
                    }
1177
                    // Finally construct next_sats[i+1].
1178
814
                    next_sats.push_back(sats[sats.size() - 1] + subs[i].ss.Sat() + add);
1179
                    // Switch over.
1180
814
                    sats = std::move(next_sats);
1181
814
                }
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
242
                return {
1185
242
                    sats[k] + SatInfo::Push() + SatInfo::OP_EQUAL(),
1186
242
                    sats[0] + SatInfo::Push() + SatInfo::OP_EQUAL()
1187
242
                };
1188
1.71M
            }
1189
1.72M
        }
1190
1.72M
        assert(false);
1191
0
    }
miniscript::Node<XOnlyPubKey>::CalcStackSize() const
Line
Count
Source
1078
5.61M
    internal::StackSize CalcStackSize() const {
1079
5.61M
        using namespace internal;
1080
5.61M
        switch (fragment) {
1081
0
            case Fragment::JUST_0: return {{}, SatInfo::Push()};
1082
0
            case Fragment::JUST_1: return {SatInfo::Push(), {}};
1083
39
            case Fragment::OLDER:
1084
779
            case Fragment::AFTER: return {SatInfo::Push() + SatInfo::Nop(), {}};
1085
3.60k
            case Fragment::PK_K: return {SatInfo::Push()};
1086
285
            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
989
            case Fragment::AND_V: {
1104
989
                const auto& x{subs[0].ss};
1105
989
                const auto& y{subs[1].ss};
1106
989
                return {x.Sat() + y.Sat(), {}};
1107
12
            }
1108
8
            case Fragment::AND_B: {
1109
8
                const auto& x{subs[0].ss};
1110
8
                const auto& y{subs[1].ss};
1111
8
                return {x.Sat() + y.Sat() + SatInfo::BinaryOp(), x.Dsat() + y.Dsat() + SatInfo::BinaryOp()};
1112
12
            }
1113
23
            case Fragment::OR_B: {
1114
23
                const auto& x{subs[0].ss};
1115
23
                const auto& y{subs[1].ss};
1116
23
                return {
1117
23
                    ((x.Sat() + y.Dsat()) | (x.Dsat() + y.Sat())) + SatInfo::BinaryOp(),
1118
23
                    x.Dsat() + y.Dsat() + SatInfo::BinaryOp()
1119
23
                };
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
793
            case Fragment::MULTI_A: return {SatInfo(keys.size() - 1, keys.size())};
1148
16
            case Fragment::WRAP_A:
1149
5.60M
            case Fragment::WRAP_N:
1150
5.60M
            case Fragment::WRAP_S: return subs[0].ss;
1151
3.89k
            case Fragment::WRAP_C: return {
1152
3.89k
                subs[0].ss.Sat() + SatInfo::OP_CHECKSIG(),
1153
3.89k
                subs[0].ss.Dsat() + SatInfo::OP_CHECKSIG()
1154
3.89k
            };
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
995
            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
8
            case Fragment::THRESH: {
1165
                // sats[j] is the SatInfo corresponding to all traces reaching j satisfactions.
1166
8
                auto sats = Vector(SatInfo::Empty());
1167
32
                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
24
                    auto add = i ? SatInfo::BinaryOp() : SatInfo::Empty();
1171
                    // Construct a variable that will become the next sats, starting with index 0.
1172
24
                    auto next_sats = Vector(sats[0] + subs[i].ss.Dsat() + add);
1173
                    // Then loop to construct next_sats[1..i].
1174
48
                    for (size_t j = 1; j < sats.size(); ++j) {
1175
24
                        next_sats.push_back(((sats[j] + subs[i].ss.Dsat()) | (sats[j - 1] + subs[i].ss.Sat())) + add);
1176
24
                    }
1177
                    // Finally construct next_sats[i+1].
1178
24
                    next_sats.push_back(sats[sats.size() - 1] + subs[i].ss.Sat() + add);
1179
                    // Switch over.
1180
24
                    sats = std::move(next_sats);
1181
24
                }
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
8
                return {
1185
8
                    sats[k] + SatInfo::Push() + SatInfo::OP_EQUAL(),
1186
8
                    sats[0] + SatInfo::Push() + SatInfo::OP_EQUAL()
1187
8
                };
1188
5.60M
            }
1189
5.61M
        }
1190
5.61M
        assert(false);
1191
0
    }
1192
1193
7.36M
    internal::WitnessSize CalcWitnessSize() const {
1194
7.36M
        const uint32_t sig_size = IsTapscript(m_script_ctx) ? 1 + 65 : 1 + 72;
1195
7.36M
        const uint32_t pubkey_size = IsTapscript(m_script_ctx) ? 1 + 32 : 1 + 33;
1196
7.36M
        switch (fragment) {
1197
709
            case Fragment::JUST_0: return {{}, 0};
1198
245
            case Fragment::JUST_1:
1199
8.20k
            case Fragment::OLDER:
1200
9.67k
            case Fragment::AFTER: return {0, {}};
1201
7.09k
            case Fragment::PK_K: return {sig_size, 1};
1202
874
            case Fragment::PK_H: return {sig_size + pubkey_size, 1 + pubkey_size};
1203
110
            case Fragment::SHA256:
1204
187
            case Fragment::RIPEMD160:
1205
303
            case Fragment::HASH256:
1206
400
            case Fragment::HASH160: return {1 + 32, {}};
1207
271
            case Fragment::ANDOR: {
1208
271
                const auto sat{(subs[0].ws.sat + subs[1].ws.sat) | (subs[0].ws.dsat + subs[2].ws.sat)};
1209
271
                const auto dsat{subs[0].ws.dsat + subs[2].ws.dsat};
1210
271
                return {sat, dsat};
1211
303
            }
1212
2.11k
            case Fragment::AND_V: return {subs[0].ws.sat + subs[1].ws.sat, {}};
1213
7.11k
            case Fragment::AND_B: return {subs[0].ws.sat + subs[1].ws.sat, subs[0].ws.dsat + subs[1].ws.dsat};
1214
142
            case Fragment::OR_B: {
1215
142
                const auto sat{(subs[0].ws.dsat + subs[1].ws.sat) | (subs[0].ws.sat + subs[1].ws.dsat)};
1216
142
                const auto dsat{subs[0].ws.dsat + subs[1].ws.dsat};
1217
142
                return {sat, dsat};
1218
303
            }
1219
60
            case Fragment::OR_C: return {subs[0].ws.sat | (subs[0].ws.dsat + subs[1].ws.sat), {}};
1220
136
            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
653
            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
173
            case Fragment::MULTI: return {k * sig_size + 1, k + 1};
1223
830
            case Fragment::MULTI_A: return {k * sig_size + static_cast<uint32_t>(keys.size()) - k, static_cast<uint32_t>(keys.size())};
1224
7.47k
            case Fragment::WRAP_A:
1225
7.32M
            case Fragment::WRAP_N:
1226
7.32M
            case Fragment::WRAP_S:
1227
7.33M
            case Fragment::WRAP_C: return subs[0].ws;
1228
122
            case Fragment::WRAP_D: return {1 + 1 + subs[0].ws.sat, 1};
1229
2.26k
            case Fragment::WRAP_V: return {subs[0].ws.sat, {}};
1230
16
            case Fragment::WRAP_J: return {subs[0].ws.sat, 1};
1231
390
            case Fragment::THRESH: {
1232
390
                auto sats = Vector(internal::MaxInt<uint32_t>(0));
1233
1.51k
                for (const auto& sub : subs) {
1234
1.51k
                    auto next_sats = Vector(sats[0] + sub.ws.dsat);
1235
4.52k
                    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.51k
                    next_sats.push_back(sats[sats.size() - 1] + sub.ws.sat);
1237
1.51k
                    sats = std::move(next_sats);
1238
1.51k
                }
1239
390
                assert(k < sats.size());
1240
390
                return {sats[k], sats[0]};
1241
390
            }
1242
7.36M
        }
1243
7.36M
        assert(false);
1244
0
    }
miniscript::Node<CPubKey>::CalcWitnessSize() const
Line
Count
Source
1193
28.7k
    internal::WitnessSize CalcWitnessSize() const {
1194
28.7k
        const uint32_t sig_size = IsTapscript(m_script_ctx) ? 1 + 65 : 1 + 72;
1195
28.7k
        const uint32_t pubkey_size = IsTapscript(m_script_ctx) ? 1 + 32 : 1 + 33;
1196
28.7k
        switch (fragment) {
1197
449
            case Fragment::JUST_0: return {{}, 0};
1198
232
            case Fragment::JUST_1:
1199
7.82k
            case Fragment::OLDER:
1200
8.21k
            case Fragment::AFTER: return {0, {}};
1201
1.79k
            case Fragment::PK_K: return {sig_size, 1};
1202
114
            case Fragment::PK_H: return {sig_size + pubkey_size, 1 + pubkey_size};
1203
68
            case Fragment::SHA256:
1204
94
            case Fragment::RIPEMD160:
1205
134
            case Fragment::HASH256:
1206
158
            case Fragment::HASH160: return {1 + 32, {}};
1207
135
            case Fragment::ANDOR: {
1208
135
                const auto sat{(subs[0].ws.sat + subs[1].ws.sat) | (subs[0].ws.dsat + subs[2].ws.sat)};
1209
135
                const auto dsat{subs[0].ws.dsat + subs[2].ws.dsat};
1210
135
                return {sat, dsat};
1211
134
            }
1212
305
            case Fragment::AND_V: return {subs[0].ws.sat + subs[1].ws.sat, {}};
1213
6.85k
            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
134
            }
1219
20
            case Fragment::OR_C: return {subs[0].ws.sat | (subs[0].ws.dsat + subs[1].ws.sat), {}};
1220
52
            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
6.96k
            case Fragment::WRAP_A:
1225
7.27k
            case Fragment::WRAP_N:
1226
7.74k
            case Fragment::WRAP_S:
1227
9.59k
            case Fragment::WRAP_C: return subs[0].ws;
1228
44
            case Fragment::WRAP_D: return {1 + 1 + subs[0].ws.sat, 1};
1229
369
            case Fragment::WRAP_V: return {subs[0].ws.sat, {}};
1230
16
            case Fragment::WRAP_J: return {subs[0].ws.sat, 1};
1231
140
            case Fragment::THRESH: {
1232
140
                auto sats = Vector(internal::MaxInt<uint32_t>(0));
1233
679
                for (const auto& sub : subs) {
1234
679
                    auto next_sats = Vector(sats[0] + sub.ws.dsat);
1235
2.35k
                    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
679
                    next_sats.push_back(sats[sats.size() - 1] + sub.ws.sat);
1237
679
                    sats = std::move(next_sats);
1238
679
                }
1239
140
                assert(k < sats.size());
1240
140
                return {sats[k], sats[0]};
1241
140
            }
1242
28.7k
        }
1243
28.7k
        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
343
            case Fragment::OLDER:
1200
682
            case Fragment::AFTER: return {0, {}};
1201
1.69k
            case Fragment::PK_K: return {sig_size, 1};
1202
475
            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
230
            case Fragment::HASH160: return {1 + 32, {}};
1207
136
            case Fragment::ANDOR: {
1208
136
                const auto sat{(subs[0].ws.sat + subs[1].ws.sat) | (subs[0].ws.dsat + subs[2].ws.sat)};
1209
136
                const auto dsat{subs[0].ws.dsat + subs[2].ws.dsat};
1210
136
                return {sat, dsat};
1211
157
            }
1212
821
            case Fragment::AND_V: return {subs[0].ws.sat + subs[1].ws.sat, {}};
1213
249
            case Fragment::AND_B: return {subs[0].ws.sat + subs[1].ws.sat, subs[0].ws.dsat + subs[1].ws.dsat};
1214
90
            case Fragment::OR_B: {
1215
90
                const auto sat{(subs[0].ws.dsat + subs[1].ws.sat) | (subs[0].ws.sat + subs[1].ws.dsat)};
1216
90
                const auto dsat{subs[0].ws.dsat + subs[1].ws.dsat};
1217
90
                return {sat, dsat};
1218
157
            }
1219
40
            case Fragment::OR_C: return {subs[0].ws.sat | (subs[0].ws.dsat + subs[1].ws.sat), {}};
1220
84
            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
254
            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
124
            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
495
            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
898
            case Fragment::WRAP_V: return {subs[0].ws.sat, {}};
1230
0
            case Fragment::WRAP_J: return {subs[0].ws.sat, 1};
1231
242
            case Fragment::THRESH: {
1232
242
                auto sats = Vector(internal::MaxInt<uint32_t>(0));
1233
814
                for (const auto& sub : subs) {
1234
814
                    auto next_sats = Vector(sats[0] + sub.ws.dsat);
1235
2.12k
                    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
814
                    next_sats.push_back(sats[sats.size() - 1] + sub.ws.sat);
1237
814
                    sats = std::move(next_sats);
1238
814
                }
1239
242
                assert(k < sats.size());
1240
242
                return {sats[k], sats[0]};
1241
242
            }
1242
1.72M
        }
1243
1.72M
        assert(false);
1244
0
    }
miniscript::Node<XOnlyPubKey>::CalcWitnessSize() const
Line
Count
Source
1193
5.61M
    internal::WitnessSize CalcWitnessSize() const {
1194
5.61M
        const uint32_t sig_size = IsTapscript(m_script_ctx) ? 1 + 65 : 1 + 72;
1195
5.61M
        const uint32_t pubkey_size = IsTapscript(m_script_ctx) ? 1 + 32 : 1 + 33;
1196
5.61M
        switch (fragment) {
1197
0
            case Fragment::JUST_0: return {{}, 0};
1198
0
            case Fragment::JUST_1:
1199
39
            case Fragment::OLDER:
1200
779
            case Fragment::AFTER: return {0, {}};
1201
3.60k
            case Fragment::PK_K: return {sig_size, 1};
1202
285
            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
989
            case Fragment::AND_V: return {subs[0].ws.sat + subs[1].ws.sat, {}};
1213
8
            case Fragment::AND_B: return {subs[0].ws.sat + subs[1].ws.sat, subs[0].ws.dsat + subs[1].ws.dsat};
1214
23
            case Fragment::OR_B: {
1215
23
                const auto sat{(subs[0].ws.dsat + subs[1].ws.sat) | (subs[0].ws.sat + subs[1].ws.dsat)};
1216
23
                const auto dsat{subs[0].ws.dsat + subs[1].ws.dsat};
1217
23
                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
793
            case Fragment::MULTI_A: return {k * sig_size + static_cast<uint32_t>(keys.size()) - k, static_cast<uint32_t>(keys.size())};
1224
16
            case Fragment::WRAP_A:
1225
5.60M
            case Fragment::WRAP_N:
1226
5.60M
            case Fragment::WRAP_S:
1227
5.60M
            case Fragment::WRAP_C: return subs[0].ws;
1228
6
            case Fragment::WRAP_D: return {1 + 1 + subs[0].ws.sat, 1};
1229
995
            case Fragment::WRAP_V: return {subs[0].ws.sat, {}};
1230
0
            case Fragment::WRAP_J: return {subs[0].ws.sat, 1};
1231
8
            case Fragment::THRESH: {
1232
8
                auto sats = Vector(internal::MaxInt<uint32_t>(0));
1233
24
                for (const auto& sub : subs) {
1234
24
                    auto next_sats = Vector(sats[0] + sub.ws.dsat);
1235
48
                    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
24
                    next_sats.push_back(sats[sats.size() - 1] + sub.ws.sat);
1237
24
                    sats = std::move(next_sats);
1238
24
                }
1239
8
                assert(k < sats.size());
1240
8
                return {sats[k], sats[0]};
1241
8
            }
1242
5.61M
        }
1243
5.61M
        assert(false);
1244
0
    }
1245
1246
    template<typename Ctx>
1247
9.53k
    internal::InputResult ProduceInput(const Ctx& ctx) const {
1248
9.53k
        using namespace internal;
1249
1250
        // Internal function which is invoked for every tree node, constructing satisfaction/dissatisfactions
1251
        // given those of its subnodes.
1252
7.23M
        auto helper = [&ctx](const Node& node, std::span<InputResult> subres) -> InputResult {
1253
7.23M
            switch (node.fragment) {
1254
379k
                case Fragment::PK_K: {
1255
379k
                    std::vector<unsigned char> sig;
1256
379k
                    Availability avail = ctx.Sign(node.keys[0], sig);
1257
379k
                    return {ZERO, InputStack(std::move(sig)).SetWithSig().SetAvailable(avail)};
1258
0
                }
1259
1.05k
                case Fragment::PK_H: {
1260
1.05k
                    std::vector<unsigned char> key = ctx.ToPKBytes(node.keys[0]), sig;
1261
1.05k
                    Availability avail = ctx.Sign(node.keys[0], sig);
1262
1.05k
                    return {ZERO + InputStack(key), (InputStack(std::move(sig)).SetWithSig() + InputStack(key)).SetAvailable(avail)};
1263
0
                }
1264
949
                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
949
                    std::vector<InputStack> sats = Vector(EMPTY);
1268
93.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
92.3k
                        std::vector<unsigned char> sig;
1272
92.3k
                        Availability avail = ctx.Sign(node.keys[node.keys.size() - 1 - i], sig);
1273
                        // Compute signature stack for just this key.
1274
92.3k
                        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
92.3k
                        std::vector<InputStack> next_sats;
1279
92.3k
                        next_sats.push_back(sats[0] + ZERO);
1280
43.9M
                        for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + ZERO) | (std::move(sats[j - 1]) + sat));
1281
92.3k
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(sat));
1282
                        // Switch over.
1283
92.3k
                        sats = std::move(next_sats);
1284
92.3k
                    }
1285
                    // The dissatisfaction consists of as many empty vectors as there are keys, which is the same as
1286
                    // satisfying 0 keys.
1287
949
                    auto& nsat{sats[0]};
1288
949
                    CHECK_NONFATAL(node.k != 0);
1289
949
                    assert(node.k < sats.size());
1290
949
                    return {std::move(nsat), std::move(sats[node.k])};
1291
949
                }
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
485
                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
485
                    std::vector<InputStack> sats = Vector(EMPTY);
1323
2.19k
                    for (size_t i = 0; i < subres.size(); ++i) {
1324
                        // Introduce an alias for the i'th last satisfaction/dissatisfaction.
1325
1.70k
                        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.70k
                        std::vector<InputStack> next_sats;
1330
1.70k
                        next_sats.push_back(sats[0] + res.nsat);
1331
4.45k
                        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.70k
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(res.sat));
1333
                        // Switch over.
1334
1.70k
                        sats = std::move(next_sats);
1335
1.70k
                    }
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
485
                    InputStack nsat = INVALID;
1339
2.67k
                    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.19k
                        if (i != 0 && i != node.k) sats[i].SetMalleable().SetNonCanon();
1347
                        // Include all dissatisfactions (even these non-canonical ones) in nsat.
1348
2.19k
                        if (i != node.k) nsat = std::move(nsat) | std::move(sats[i]);
1349
2.19k
                    }
1350
485
                    assert(node.k < sats.size());
1351
485
                    return {std::move(nsat), std::move(sats[node.k])};
1352
485
                }
1353
37.0k
                case Fragment::OLDER: {
1354
37.0k
                    return {INVALID, ctx.CheckOlder(node.k) ? EMPTY : INVALID};
1355
485
                }
1356
2.30k
                case Fragment::AFTER: {
1357
2.30k
                    return {INVALID, ctx.CheckAfter(node.k) ? EMPTY : INVALID};
1358
485
                }
1359
529
                case Fragment::SHA256: {
1360
529
                    std::vector<unsigned char> preimage;
1361
529
                    Availability avail = ctx.SatSHA256(node.data, preimage);
1362
529
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1363
485
                }
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
485
                }
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
485
                }
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
485
                }
1379
2.47k
                case Fragment::AND_V: {
1380
2.47k
                    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.47k
                    return {(y.nsat + x.sat).SetNonCanon(), y.sat + x.sat};
1388
485
                }
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
485
                }
1398
167
                case Fragment::OR_B: {
1399
167
                    auto& x = subres[0], &z = subres[1];
1400
                    // The (sat(Z) sat(X)) solution is overcomplete (attacker can change either into dsat).
1401
167
                    return {z.nsat + x.nsat, (z.nsat + x.sat) | (z.sat + x.nsat) | (z.sat + x.sat).SetMalleable().SetNonCanon()};
1402
485
                }
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
485
                }
1407
326
                case Fragment::OR_D: {
1408
326
                    auto& x = subres[0], &z = subres[1];
1409
326
                    return {z.nsat + x.nsat, std::move(x.sat) | (z.sat + x.nsat)};
1410
485
                }
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
485
                }
1415
741
                case Fragment::ANDOR: {
1416
741
                    auto& x = subres[0], &y = subres[1], &z = subres[2];
1417
741
                    return {(y.nsat + x.sat).SetNonCanon() | (z.nsat + x.nsat), (y.sat + x.sat) | (z.sat + x.nsat)};
1418
485
                }
1419
408k
                case Fragment::WRAP_A:
1420
409k
                case Fragment::WRAP_S:
1421
788k
                case Fragment::WRAP_C:
1422
6.38M
                case Fragment::WRAP_N:
1423
6.38M
                    return std::move(subres[0]);
1424
133
                case Fragment::WRAP_D: {
1425
133
                    auto &x = subres[0];
1426
133
                    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.81k
                case Fragment::WRAP_V: {
1438
2.81k
                    auto &x = subres[0];
1439
2.81k
                    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
7.23M
            }
1444
7.23M
            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
5.61M
        auto helper = [&ctx](const Node& node, std::span<InputResult> subres) -> InputResult {
1253
5.61M
            switch (node.fragment) {
1254
3.60k
                case Fragment::PK_K: {
1255
3.60k
                    std::vector<unsigned char> sig;
1256
3.60k
                    Availability avail = ctx.Sign(node.keys[0], sig);
1257
3.60k
                    return {ZERO, InputStack(std::move(sig)).SetWithSig().SetAvailable(avail)};
1258
0
                }
1259
285
                case Fragment::PK_H: {
1260
285
                    std::vector<unsigned char> key = ctx.ToPKBytes(node.keys[0]), sig;
1261
285
                    Availability avail = ctx.Sign(node.keys[0], sig);
1262
285
                    return {ZERO + InputStack(key), (InputStack(std::move(sig)).SetWithSig() + InputStack(key)).SetAvailable(avail)};
1263
0
                }
1264
793
                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
793
                    std::vector<InputStack> sats = Vector(EMPTY);
1268
90.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
89.5k
                        std::vector<unsigned char> sig;
1272
89.5k
                        Availability avail = ctx.Sign(node.keys[node.keys.size() - 1 - i], sig);
1273
                        // Compute signature stack for just this key.
1274
89.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
89.5k
                        std::vector<InputStack> next_sats;
1279
89.5k
                        next_sats.push_back(sats[0] + ZERO);
1280
43.9M
                        for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + ZERO) | (std::move(sats[j - 1]) + sat));
1281
89.5k
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(sat));
1282
                        // Switch over.
1283
89.5k
                        sats = std::move(next_sats);
1284
89.5k
                    }
1285
                    // The dissatisfaction consists of as many empty vectors as there are keys, which is the same as
1286
                    // satisfying 0 keys.
1287
793
                    auto& nsat{sats[0]};
1288
793
                    CHECK_NONFATAL(node.k != 0);
1289
793
                    assert(node.k < sats.size());
1290
793
                    return {std::move(nsat), std::move(sats[node.k])};
1291
793
                }
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
8
                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
8
                    std::vector<InputStack> sats = Vector(EMPTY);
1323
32
                    for (size_t i = 0; i < subres.size(); ++i) {
1324
                        // Introduce an alias for the i'th last satisfaction/dissatisfaction.
1325
24
                        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
24
                        std::vector<InputStack> next_sats;
1330
24
                        next_sats.push_back(sats[0] + res.nsat);
1331
48
                        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
24
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(res.sat));
1333
                        // Switch over.
1334
24
                        sats = std::move(next_sats);
1335
24
                    }
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
8
                    InputStack nsat = INVALID;
1339
40
                    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
32
                        if (i != 0 && i != node.k) sats[i].SetMalleable().SetNonCanon();
1347
                        // Include all dissatisfactions (even these non-canonical ones) in nsat.
1348
32
                        if (i != node.k) nsat = std::move(nsat) | std::move(sats[i]);
1349
32
                    }
1350
8
                    assert(node.k < sats.size());
1351
8
                    return {std::move(nsat), std::move(sats[node.k])};
1352
8
                }
1353
39
                case Fragment::OLDER: {
1354
39
                    return {INVALID, ctx.CheckOlder(node.k) ? EMPTY : INVALID};
1355
8
                }
1356
740
                case Fragment::AFTER: {
1357
740
                    return {INVALID, ctx.CheckAfter(node.k) ? EMPTY : INVALID};
1358
8
                }
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
8
                }
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
8
                }
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
8
                }
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
8
                }
1379
989
                case Fragment::AND_V: {
1380
989
                    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
989
                    return {(y.nsat + x.sat).SetNonCanon(), y.sat + x.sat};
1388
8
                }
1389
8
                case Fragment::AND_B: {
1390
8
                    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
8
                    return {(y.nsat + x.nsat) | (y.sat + x.nsat).SetMalleable().SetNonCanon() | (y.nsat + x.sat).SetMalleable().SetNonCanon(), y.sat + x.sat};
1397
8
                }
1398
23
                case Fragment::OR_B: {
1399
23
                    auto& x = subres[0], &z = subres[1];
1400
                    // The (sat(Z) sat(X)) solution is overcomplete (attacker can change either into dsat).
1401
23
                    return {z.nsat + x.nsat, (z.nsat + x.sat) | (z.sat + x.nsat) | (z.sat + x.sat).SetMalleable().SetNonCanon()};
1402
8
                }
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
8
                }
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
8
                }
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
8
                }
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
8
                }
1419
16
                case Fragment::WRAP_A:
1420
47
                case Fragment::WRAP_S:
1421
3.93k
                case Fragment::WRAP_C:
1422
5.60M
                case Fragment::WRAP_N:
1423
5.60M
                    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
3.93k
                }
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
3.93k
                }
1437
995
                case Fragment::WRAP_V: {
1438
995
                    auto &x = subres[0];
1439
995
                    return {INVALID, std::move(x.sat)};
1440
3.93k
                }
1441
0
                case Fragment::JUST_0: return {EMPTY, INVALID};
1442
0
                case Fragment::JUST_1: return {INVALID, EMPTY};
1443
5.61M
            }
1444
5.61M
            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.46k
        auto helper = [&ctx](const Node& node, std::span<InputResult> subres) -> InputResult {
1253
3.46k
            switch (node.fragment) {
1254
573
                case Fragment::PK_K: {
1255
573
                    std::vector<unsigned char> sig;
1256
573
                    Availability avail = ctx.Sign(node.keys[0], sig);
1257
573
                    return {ZERO, InputStack(std::move(sig)).SetWithSig().SetAvailable(avail)};
1258
0
                }
1259
60
                case Fragment::PK_H: {
1260
60
                    std::vector<unsigned char> key = ctx.ToPKBytes(node.keys[0]), sig;
1261
60
                    Availability avail = ctx.Sign(node.keys[0], sig);
1262
60
                    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
105
                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
105
                    std::vector<InputStack> sats = Vector(EMPTY);
1323
682
                    for (size_t i = 0; i < subres.size(); ++i) {
1324
                        // Introduce an alias for the i'th last satisfaction/dissatisfaction.
1325
577
                        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
577
                        std::vector<InputStack> next_sats;
1330
577
                        next_sats.push_back(sats[0] + res.nsat);
1331
2.14k
                        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
577
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(res.sat));
1333
                        // Switch over.
1334
577
                        sats = std::move(next_sats);
1335
577
                    }
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
105
                    InputStack nsat = INVALID;
1339
787
                    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
682
                        if (i != 0 && i != node.k) sats[i].SetMalleable().SetNonCanon();
1347
                        // Include all dissatisfactions (even these non-canonical ones) in nsat.
1348
682
                        if (i != node.k) nsat = std::move(nsat) | std::move(sats[i]);
1349
682
                    }
1350
105
                    assert(node.k < sats.size());
1351
105
                    return {std::move(nsat), std::move(sats[node.k])};
1352
105
                }
1353
75
                case Fragment::OLDER: {
1354
75
                    return {INVALID, ctx.CheckOlder(node.k) ? EMPTY : INVALID};
1355
105
                }
1356
253
                case Fragment::AFTER: {
1357
253
                    return {INVALID, ctx.CheckAfter(node.k) ? EMPTY : INVALID};
1358
105
                }
1359
25
                case Fragment::SHA256: {
1360
25
                    std::vector<unsigned char> preimage;
1361
25
                    Availability avail = ctx.SatSHA256(node.data, preimage);
1362
25
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1363
105
                }
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
105
                }
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
105
                }
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
105
                }
1379
158
                case Fragment::AND_V: {
1380
158
                    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
158
                    return {(y.nsat + x.sat).SetNonCanon(), y.sat + x.sat};
1388
105
                }
1389
8
                case Fragment::AND_B: {
1390
8
                    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
8
                    return {(y.nsat + x.nsat) | (y.sat + x.nsat).SetMalleable().SetNonCanon() | (y.nsat + x.sat).SetMalleable().SetNonCanon(), y.sat + x.sat};
1397
105
                }
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
105
                }
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
105
                }
1407
14
                case Fragment::OR_D: {
1408
14
                    auto& x = subres[0], &z = subres[1];
1409
14
                    return {z.nsat + x.nsat, std::move(x.sat) | (z.sat + x.nsat)};
1410
105
                }
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
105
                }
1415
69
                case Fragment::ANDOR: {
1416
69
                    auto& x = subres[0], &y = subres[1], &z = subres[2];
1417
69
                    return {(y.nsat + x.sat).SetNonCanon() | (z.nsat + x.nsat), (y.sat + x.sat) | (z.sat + x.nsat)};
1418
105
                }
1419
40
                case Fragment::WRAP_A:
1420
480
                case Fragment::WRAP_S:
1421
1.09k
                case Fragment::WRAP_C:
1422
1.37k
                case Fragment::WRAP_N:
1423
1.37k
                    return std::move(subres[0]);
1424
31
                case Fragment::WRAP_D: {
1425
31
                    auto &x = subres[0];
1426
31
                    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
189
                case Fragment::WRAP_V: {
1438
189
                    auto &x = subres[0];
1439
189
                    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.46k
            }
1444
3.46k
            assert(false);
1445
0
            return {INVALID, INVALID};
1446
0
        };
1447
1448
7.23M
        auto tester = [&helper](const Node& node, std::span<InputResult> subres) -> InputResult {
1449
7.23M
            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
7.23M
            if (node.GetType() << "z"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() == 0);
1456
7.23M
            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
7.23M
            if (node.GetType() << "o"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() == 1);
1460
7.23M
            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
7.23M
            if (node.GetType() << "n"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() >= 1);
1465
7.23M
            if (node.GetType() << "n"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() >= 1);
1466
7.23M
            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
7.23M
            if (node.GetType() << "d"_mst) CHECK_NONFATAL(ret.nsat.available != Availability::NO);
1471
7.23M
            if (node.GetType() << "d"_mst) CHECK_NONFATAL(!ret.nsat.has_sig);
1472
7.23M
            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
7.23M
            if (node.GetType() << "f"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.has_sig);
1476
7.23M
            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
7.23M
            if (node.GetType() << "me"_mst) CHECK_NONFATAL(ret.nsat.available != Availability::NO);
1480
7.23M
            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
7.23M
            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
7.23M
            if (ret.sat.available != Availability::NO && !ret.sat.malleable) CHECK_NONFATAL(!ret.sat.non_canon);
1487
1488
7.23M
            return ret;
1489
7.23M
        };
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
5.61M
        auto tester = [&helper](const Node& node, std::span<InputResult> subres) -> InputResult {
1449
5.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
5.61M
            if (node.GetType() << "z"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() == 0);
1456
5.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
5.61M
            if (node.GetType() << "o"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() == 1);
1460
5.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
5.61M
            if (node.GetType() << "n"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() >= 1);
1465
5.61M
            if (node.GetType() << "n"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() >= 1);
1466
5.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
5.61M
            if (node.GetType() << "d"_mst) CHECK_NONFATAL(ret.nsat.available != Availability::NO);
1471
5.61M
            if (node.GetType() << "d"_mst) CHECK_NONFATAL(!ret.nsat.has_sig);
1472
5.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
5.61M
            if (node.GetType() << "f"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.has_sig);
1476
5.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
5.61M
            if (node.GetType() << "me"_mst) CHECK_NONFATAL(ret.nsat.available != Availability::NO);
1480
5.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
5.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
5.61M
            if (ret.sat.available != Availability::NO && !ret.sat.malleable) CHECK_NONFATAL(!ret.sat.non_canon);
1487
1488
5.61M
            return ret;
1489
5.61M
        };
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.46k
        auto tester = [&helper](const Node& node, std::span<InputResult> subres) -> InputResult {
1449
3.46k
            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.46k
            if (node.GetType() << "z"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() == 0);
1456
3.46k
            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.46k
            if (node.GetType() << "o"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() == 1);
1460
3.46k
            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.46k
            if (node.GetType() << "n"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() >= 1);
1465
3.46k
            if (node.GetType() << "n"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() >= 1);
1466
3.46k
            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.46k
            if (node.GetType() << "d"_mst) CHECK_NONFATAL(ret.nsat.available != Availability::NO);
1471
3.46k
            if (node.GetType() << "d"_mst) CHECK_NONFATAL(!ret.nsat.has_sig);
1472
3.46k
            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.46k
            if (node.GetType() << "f"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.has_sig);
1476
3.46k
            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.46k
            if (node.GetType() << "me"_mst) CHECK_NONFATAL(ret.nsat.available != Availability::NO);
1480
3.46k
            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.46k
            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.46k
            if (ret.sat.available != Availability::NO && !ret.sat.malleable) CHECK_NONFATAL(!ret.sat.non_canon);
1487
1488
3.46k
            return ret;
1489
3.46k
        };
1490
1491
9.53k
        return TreeEval<InputResult>(tester);
1492
9.53k
    }
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.44k
    internal::InputResult ProduceInput(const Ctx& ctx) const {
1248
4.44k
        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.44k
        auto helper = [&ctx](const Node& node, std::span<InputResult> subres) -> InputResult {
1253
4.44k
            switch (node.fragment) {
1254
4.44k
                case Fragment::PK_K: {
1255
4.44k
                    std::vector<unsigned char> sig;
1256
4.44k
                    Availability avail = ctx.Sign(node.keys[0], sig);
1257
4.44k
                    return {ZERO, InputStack(std::move(sig)).SetWithSig().SetAvailable(avail)};
1258
4.44k
                }
1259
4.44k
                case Fragment::PK_H: {
1260
4.44k
                    std::vector<unsigned char> key = ctx.ToPKBytes(node.keys[0]), sig;
1261
4.44k
                    Availability avail = ctx.Sign(node.keys[0], sig);
1262
4.44k
                    return {ZERO + InputStack(key), (InputStack(std::move(sig)).SetWithSig() + InputStack(key)).SetAvailable(avail)};
1263
4.44k
                }
1264
4.44k
                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.44k
                    std::vector<InputStack> sats = Vector(EMPTY);
1268
4.44k
                    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.44k
                        std::vector<unsigned char> sig;
1272
4.44k
                        Availability avail = ctx.Sign(node.keys[node.keys.size() - 1 - i], sig);
1273
                        // Compute signature stack for just this key.
1274
4.44k
                        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.44k
                        std::vector<InputStack> next_sats;
1279
4.44k
                        next_sats.push_back(sats[0] + ZERO);
1280
4.44k
                        for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + ZERO) | (std::move(sats[j - 1]) + sat));
1281
4.44k
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(sat));
1282
                        // Switch over.
1283
4.44k
                        sats = std::move(next_sats);
1284
4.44k
                    }
1285
                    // The dissatisfaction consists of as many empty vectors as there are keys, which is the same as
1286
                    // satisfying 0 keys.
1287
4.44k
                    auto& nsat{sats[0]};
1288
4.44k
                    CHECK_NONFATAL(node.k != 0);
1289
4.44k
                    assert(node.k < sats.size());
1290
4.44k
                    return {std::move(nsat), std::move(sats[node.k])};
1291
4.44k
                }
1292
4.44k
                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.44k
                    std::vector<InputStack> sats = Vector(ZERO);
1297
4.44k
                    for (size_t i = 0; i < node.keys.size(); ++i) {
1298
4.44k
                        std::vector<unsigned char> sig;
1299
4.44k
                        Availability avail = ctx.Sign(node.keys[i], sig);
1300
                        // Compute signature stack for just the i'th key.
1301
4.44k
                        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.44k
                        std::vector<InputStack> next_sats;
1306
4.44k
                        next_sats.push_back(sats[0]);
1307
4.44k
                        for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back(sats[j] | (std::move(sats[j - 1]) + sat));
1308
4.44k
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(sat));
1309
                        // Switch over.
1310
4.44k
                        sats = std::move(next_sats);
1311
4.44k
                    }
1312
                    // The dissatisfaction consists of k+1 stack elements all equal to 0.
1313
4.44k
                    InputStack nsat = ZERO;
1314
4.44k
                    for (size_t i = 0; i < node.k; ++i) nsat = std::move(nsat) + ZERO;
1315
4.44k
                    assert(node.k < sats.size());
1316
4.44k
                    return {std::move(nsat), std::move(sats[node.k])};
1317
4.44k
                }
1318
4.44k
                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.44k
                    std::vector<InputStack> sats = Vector(EMPTY);
1323
4.44k
                    for (size_t i = 0; i < subres.size(); ++i) {
1324
                        // Introduce an alias for the i'th last satisfaction/dissatisfaction.
1325
4.44k
                        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.44k
                        std::vector<InputStack> next_sats;
1330
4.44k
                        next_sats.push_back(sats[0] + res.nsat);
1331
4.44k
                        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.44k
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(res.sat));
1333
                        // Switch over.
1334
4.44k
                        sats = std::move(next_sats);
1335
4.44k
                    }
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.44k
                    InputStack nsat = INVALID;
1339
4.44k
                    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.44k
                        if (i != 0 && i != node.k) sats[i].SetMalleable().SetNonCanon();
1347
                        // Include all dissatisfactions (even these non-canonical ones) in nsat.
1348
4.44k
                        if (i != node.k) nsat = std::move(nsat) | std::move(sats[i]);
1349
4.44k
                    }
1350
4.44k
                    assert(node.k < sats.size());
1351
4.44k
                    return {std::move(nsat), std::move(sats[node.k])};
1352
4.44k
                }
1353
4.44k
                case Fragment::OLDER: {
1354
4.44k
                    return {INVALID, ctx.CheckOlder(node.k) ? EMPTY : INVALID};
1355
4.44k
                }
1356
4.44k
                case Fragment::AFTER: {
1357
4.44k
                    return {INVALID, ctx.CheckAfter(node.k) ? EMPTY : INVALID};
1358
4.44k
                }
1359
4.44k
                case Fragment::SHA256: {
1360
4.44k
                    std::vector<unsigned char> preimage;
1361
4.44k
                    Availability avail = ctx.SatSHA256(node.data, preimage);
1362
4.44k
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1363
4.44k
                }
1364
4.44k
                case Fragment::RIPEMD160: {
1365
4.44k
                    std::vector<unsigned char> preimage;
1366
4.44k
                    Availability avail = ctx.SatRIPEMD160(node.data, preimage);
1367
4.44k
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1368
4.44k
                }
1369
4.44k
                case Fragment::HASH256: {
1370
4.44k
                    std::vector<unsigned char> preimage;
1371
4.44k
                    Availability avail = ctx.SatHASH256(node.data, preimage);
1372
4.44k
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1373
4.44k
                }
1374
4.44k
                case Fragment::HASH160: {
1375
4.44k
                    std::vector<unsigned char> preimage;
1376
4.44k
                    Availability avail = ctx.SatHASH160(node.data, preimage);
1377
4.44k
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1378
4.44k
                }
1379
4.44k
                case Fragment::AND_V: {
1380
4.44k
                    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.44k
                    return {(y.nsat + x.sat).SetNonCanon(), y.sat + x.sat};
1388
4.44k
                }
1389
4.44k
                case Fragment::AND_B: {
1390
4.44k
                    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.44k
                    return {(y.nsat + x.nsat) | (y.sat + x.nsat).SetMalleable().SetNonCanon() | (y.nsat + x.sat).SetMalleable().SetNonCanon(), y.sat + x.sat};
1397
4.44k
                }
1398
4.44k
                case Fragment::OR_B: {
1399
4.44k
                    auto& x = subres[0], &z = subres[1];
1400
                    // The (sat(Z) sat(X)) solution is overcomplete (attacker can change either into dsat).
1401
4.44k
                    return {z.nsat + x.nsat, (z.nsat + x.sat) | (z.sat + x.nsat) | (z.sat + x.sat).SetMalleable().SetNonCanon()};
1402
4.44k
                }
1403
4.44k
                case Fragment::OR_C: {
1404
4.44k
                    auto& x = subres[0], &z = subres[1];
1405
4.44k
                    return {INVALID, std::move(x.sat) | (z.sat + x.nsat)};
1406
4.44k
                }
1407
4.44k
                case Fragment::OR_D: {
1408
4.44k
                    auto& x = subres[0], &z = subres[1];
1409
4.44k
                    return {z.nsat + x.nsat, std::move(x.sat) | (z.sat + x.nsat)};
1410
4.44k
                }
1411
4.44k
                case Fragment::OR_I: {
1412
4.44k
                    auto& x = subres[0], &z = subres[1];
1413
4.44k
                    return {(x.nsat + ONE) | (z.nsat + ZERO), (x.sat + ONE) | (z.sat + ZERO)};
1414
4.44k
                }
1415
4.44k
                case Fragment::ANDOR: {
1416
4.44k
                    auto& x = subres[0], &y = subres[1], &z = subres[2];
1417
4.44k
                    return {(y.nsat + x.sat).SetNonCanon() | (z.nsat + x.nsat), (y.sat + x.sat) | (z.sat + x.nsat)};
1418
4.44k
                }
1419
4.44k
                case Fragment::WRAP_A:
1420
4.44k
                case Fragment::WRAP_S:
1421
4.44k
                case Fragment::WRAP_C:
1422
4.44k
                case Fragment::WRAP_N:
1423
4.44k
                    return std::move(subres[0]);
1424
4.44k
                case Fragment::WRAP_D: {
1425
4.44k
                    auto &x = subres[0];
1426
4.44k
                    return {ZERO, x.sat + ONE};
1427
4.44k
                }
1428
4.44k
                case Fragment::WRAP_J: {
1429
4.44k
                    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.44k
                    return {InputStack(ZERO).SetMalleable(x.nsat.available != Availability::NO && !x.nsat.has_sig), std::move(x.sat)};
1436
4.44k
                }
1437
4.44k
                case Fragment::WRAP_V: {
1438
4.44k
                    auto &x = subres[0];
1439
4.44k
                    return {INVALID, std::move(x.sat)};
1440
4.44k
                }
1441
4.44k
                case Fragment::JUST_0: return {EMPTY, INVALID};
1442
4.44k
                case Fragment::JUST_1: return {INVALID, EMPTY};
1443
4.44k
            }
1444
4.44k
            assert(false);
1445
4.44k
            return {INVALID, INVALID};
1446
4.44k
        };
1447
1448
4.44k
        auto tester = [&helper](const Node& node, std::span<InputResult> subres) -> InputResult {
1449
4.44k
            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.44k
            if (node.GetType() << "z"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() == 0);
1456
4.44k
            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.44k
            if (node.GetType() << "o"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() == 1);
1460
4.44k
            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.44k
            if (node.GetType() << "n"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() >= 1);
1465
4.44k
            if (node.GetType() << "n"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() >= 1);
1466
4.44k
            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.44k
            if (node.GetType() << "d"_mst) CHECK_NONFATAL(ret.nsat.available != Availability::NO);
1471
4.44k
            if (node.GetType() << "d"_mst) CHECK_NONFATAL(!ret.nsat.has_sig);
1472
4.44k
            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.44k
            if (node.GetType() << "f"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.has_sig);
1476
4.44k
            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.44k
            if (node.GetType() << "me"_mst) CHECK_NONFATAL(ret.nsat.available != Availability::NO);
1480
4.44k
            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.44k
            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.44k
            if (ret.sat.available != Availability::NO && !ret.sat.malleable) CHECK_NONFATAL(!ret.sat.non_canon);
1487
1488
4.44k
            return ret;
1489
4.44k
        };
1490
1491
4.44k
        return TreeEval<InputResult>(tester);
1492
4.44k
    }
miniscript::internal::InputResult miniscript::Node<CPubKey>::ProduceInput<WshSatisfier>(WshSatisfier const&) const
Line
Count
Source
1247
268
    internal::InputResult ProduceInput(const Ctx& ctx) const {
1248
268
        using namespace internal;
1249
1250
        // Internal function which is invoked for every tree node, constructing satisfaction/dissatisfactions
1251
        // given those of its subnodes.
1252
268
        auto helper = [&ctx](const Node& node, std::span<InputResult> subres) -> InputResult {
1253
268
            switch (node.fragment) {
1254
268
                case Fragment::PK_K: {
1255
268
                    std::vector<unsigned char> sig;
1256
268
                    Availability avail = ctx.Sign(node.keys[0], sig);
1257
268
                    return {ZERO, InputStack(std::move(sig)).SetWithSig().SetAvailable(avail)};
1258
268
                }
1259
268
                case Fragment::PK_H: {
1260
268
                    std::vector<unsigned char> key = ctx.ToPKBytes(node.keys[0]), sig;
1261
268
                    Availability avail = ctx.Sign(node.keys[0], sig);
1262
268
                    return {ZERO + InputStack(key), (InputStack(std::move(sig)).SetWithSig() + InputStack(key)).SetAvailable(avail)};
1263
268
                }
1264
268
                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
268
                    std::vector<InputStack> sats = Vector(EMPTY);
1268
268
                    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
268
                        std::vector<unsigned char> sig;
1272
268
                        Availability avail = ctx.Sign(node.keys[node.keys.size() - 1 - i], sig);
1273
                        // Compute signature stack for just this key.
1274
268
                        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
268
                        std::vector<InputStack> next_sats;
1279
268
                        next_sats.push_back(sats[0] + ZERO);
1280
268
                        for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back((sats[j] + ZERO) | (std::move(sats[j - 1]) + sat));
1281
268
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(sat));
1282
                        // Switch over.
1283
268
                        sats = std::move(next_sats);
1284
268
                    }
1285
                    // The dissatisfaction consists of as many empty vectors as there are keys, which is the same as
1286
                    // satisfying 0 keys.
1287
268
                    auto& nsat{sats[0]};
1288
268
                    CHECK_NONFATAL(node.k != 0);
1289
268
                    assert(node.k < sats.size());
1290
268
                    return {std::move(nsat), std::move(sats[node.k])};
1291
268
                }
1292
268
                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
268
                    std::vector<InputStack> sats = Vector(ZERO);
1297
268
                    for (size_t i = 0; i < node.keys.size(); ++i) {
1298
268
                        std::vector<unsigned char> sig;
1299
268
                        Availability avail = ctx.Sign(node.keys[i], sig);
1300
                        // Compute signature stack for just the i'th key.
1301
268
                        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
268
                        std::vector<InputStack> next_sats;
1306
268
                        next_sats.push_back(sats[0]);
1307
268
                        for (size_t j = 1; j < sats.size(); ++j) next_sats.push_back(sats[j] | (std::move(sats[j - 1]) + sat));
1308
268
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(sat));
1309
                        // Switch over.
1310
268
                        sats = std::move(next_sats);
1311
268
                    }
1312
                    // The dissatisfaction consists of k+1 stack elements all equal to 0.
1313
268
                    InputStack nsat = ZERO;
1314
268
                    for (size_t i = 0; i < node.k; ++i) nsat = std::move(nsat) + ZERO;
1315
268
                    assert(node.k < sats.size());
1316
268
                    return {std::move(nsat), std::move(sats[node.k])};
1317
268
                }
1318
268
                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
268
                    std::vector<InputStack> sats = Vector(EMPTY);
1323
268
                    for (size_t i = 0; i < subres.size(); ++i) {
1324
                        // Introduce an alias for the i'th last satisfaction/dissatisfaction.
1325
268
                        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
268
                        std::vector<InputStack> next_sats;
1330
268
                        next_sats.push_back(sats[0] + res.nsat);
1331
268
                        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
268
                        next_sats.push_back(std::move(sats[sats.size() - 1]) + std::move(res.sat));
1333
                        // Switch over.
1334
268
                        sats = std::move(next_sats);
1335
268
                    }
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
268
                    InputStack nsat = INVALID;
1339
268
                    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
268
                        if (i != 0 && i != node.k) sats[i].SetMalleable().SetNonCanon();
1347
                        // Include all dissatisfactions (even these non-canonical ones) in nsat.
1348
268
                        if (i != node.k) nsat = std::move(nsat) | std::move(sats[i]);
1349
268
                    }
1350
268
                    assert(node.k < sats.size());
1351
268
                    return {std::move(nsat), std::move(sats[node.k])};
1352
268
                }
1353
268
                case Fragment::OLDER: {
1354
268
                    return {INVALID, ctx.CheckOlder(node.k) ? EMPTY : INVALID};
1355
268
                }
1356
268
                case Fragment::AFTER: {
1357
268
                    return {INVALID, ctx.CheckAfter(node.k) ? EMPTY : INVALID};
1358
268
                }
1359
268
                case Fragment::SHA256: {
1360
268
                    std::vector<unsigned char> preimage;
1361
268
                    Availability avail = ctx.SatSHA256(node.data, preimage);
1362
268
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1363
268
                }
1364
268
                case Fragment::RIPEMD160: {
1365
268
                    std::vector<unsigned char> preimage;
1366
268
                    Availability avail = ctx.SatRIPEMD160(node.data, preimage);
1367
268
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1368
268
                }
1369
268
                case Fragment::HASH256: {
1370
268
                    std::vector<unsigned char> preimage;
1371
268
                    Availability avail = ctx.SatHASH256(node.data, preimage);
1372
268
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1373
268
                }
1374
268
                case Fragment::HASH160: {
1375
268
                    std::vector<unsigned char> preimage;
1376
268
                    Availability avail = ctx.SatHASH160(node.data, preimage);
1377
268
                    return {ZERO32, InputStack(std::move(preimage)).SetAvailable(avail)};
1378
268
                }
1379
268
                case Fragment::AND_V: {
1380
268
                    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
268
                    return {(y.nsat + x.sat).SetNonCanon(), y.sat + x.sat};
1388
268
                }
1389
268
                case Fragment::AND_B: {
1390
268
                    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
268
                    return {(y.nsat + x.nsat) | (y.sat + x.nsat).SetMalleable().SetNonCanon() | (y.nsat + x.sat).SetMalleable().SetNonCanon(), y.sat + x.sat};
1397
268
                }
1398
268
                case Fragment::OR_B: {
1399
268
                    auto& x = subres[0], &z = subres[1];
1400
                    // The (sat(Z) sat(X)) solution is overcomplete (attacker can change either into dsat).
1401
268
                    return {z.nsat + x.nsat, (z.nsat + x.sat) | (z.sat + x.nsat) | (z.sat + x.sat).SetMalleable().SetNonCanon()};
1402
268
                }
1403
268
                case Fragment::OR_C: {
1404
268
                    auto& x = subres[0], &z = subres[1];
1405
268
                    return {INVALID, std::move(x.sat) | (z.sat + x.nsat)};
1406
268
                }
1407
268
                case Fragment::OR_D: {
1408
268
                    auto& x = subres[0], &z = subres[1];
1409
268
                    return {z.nsat + x.nsat, std::move(x.sat) | (z.sat + x.nsat)};
1410
268
                }
1411
268
                case Fragment::OR_I: {
1412
268
                    auto& x = subres[0], &z = subres[1];
1413
268
                    return {(x.nsat + ONE) | (z.nsat + ZERO), (x.sat + ONE) | (z.sat + ZERO)};
1414
268
                }
1415
268
                case Fragment::ANDOR: {
1416
268
                    auto& x = subres[0], &y = subres[1], &z = subres[2];
1417
268
                    return {(y.nsat + x.sat).SetNonCanon() | (z.nsat + x.nsat), (y.sat + x.sat) | (z.sat + x.nsat)};
1418
268
                }
1419
268
                case Fragment::WRAP_A:
1420
268
                case Fragment::WRAP_S:
1421
268
                case Fragment::WRAP_C:
1422
268
                case Fragment::WRAP_N:
1423
268
                    return std::move(subres[0]);
1424
268
                case Fragment::WRAP_D: {
1425
268
                    auto &x = subres[0];
1426
268
                    return {ZERO, x.sat + ONE};
1427
268
                }
1428
268
                case Fragment::WRAP_J: {
1429
268
                    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
268
                    return {InputStack(ZERO).SetMalleable(x.nsat.available != Availability::NO && !x.nsat.has_sig), std::move(x.sat)};
1436
268
                }
1437
268
                case Fragment::WRAP_V: {
1438
268
                    auto &x = subres[0];
1439
268
                    return {INVALID, std::move(x.sat)};
1440
268
                }
1441
268
                case Fragment::JUST_0: return {EMPTY, INVALID};
1442
268
                case Fragment::JUST_1: return {INVALID, EMPTY};
1443
268
            }
1444
268
            assert(false);
1445
268
            return {INVALID, INVALID};
1446
268
        };
1447
1448
268
        auto tester = [&helper](const Node& node, std::span<InputResult> subres) -> InputResult {
1449
268
            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
268
            if (node.GetType() << "z"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() == 0);
1456
268
            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
268
            if (node.GetType() << "o"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() == 1);
1460
268
            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
268
            if (node.GetType() << "n"_mst && ret.sat.available != Availability::NO) CHECK_NONFATAL(ret.sat.stack.size() >= 1);
1465
268
            if (node.GetType() << "n"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.stack.size() >= 1);
1466
268
            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
268
            if (node.GetType() << "d"_mst) CHECK_NONFATAL(ret.nsat.available != Availability::NO);
1471
268
            if (node.GetType() << "d"_mst) CHECK_NONFATAL(!ret.nsat.has_sig);
1472
268
            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
268
            if (node.GetType() << "f"_mst && ret.nsat.available != Availability::NO) CHECK_NONFATAL(ret.nsat.has_sig);
1476
268
            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
268
            if (node.GetType() << "me"_mst) CHECK_NONFATAL(ret.nsat.available != Availability::NO);
1480
268
            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
268
            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
268
            if (ret.sat.available != Availability::NO && !ret.sat.malleable) CHECK_NONFATAL(!ret.sat.non_canon);
1487
1488
268
            return ret;
1489
268
        };
1490
1491
268
        return TreeEval<InputResult>(tester);
1492
268
    }
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.89k
    {
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.89k
        struct Comp {
1505
5.89k
            const Ctx* ctx_ptr;
1506
6.63M
            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
5.61M
            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.46k
            Comp(const Ctx& ctx) : ctx_ptr(&ctx) {}
1507
328k
            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.43k
            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
315k
            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.29k
            bool operator()(const Key& a, const Key& b) const { return ctx_ptr->KeyCompare(a, b); }
1508
5.89k
        };
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.89k
        using keyset = std::set<Key, Comp>;
1514
5.89k
        using state = std::optional<keyset>;
1515
1516
6.63M
        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
6.63M
            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
6.63M
            for (auto& sub : subs) {
1522
6.62M
                if (!sub.has_value()) {
1523
0
                    node.has_duplicate_keys = true;
1524
0
                    return {};
1525
0
                }
1526
6.62M
            }
1527
1528
            // Start building the set of keys involved in this node and children.
1529
            // Start by keys in this node directly.
1530
6.63M
            size_t keys_count = node.keys.size();
1531
6.63M
            keyset key_set{node.keys.begin(), node.keys.end(), Comp(ctx)};
1532
6.63M
            if (key_set.size() != keys_count) {
1533
                // It already has duplicates; bail out.
1534
88
                node.has_duplicate_keys = true;
1535
88
                return {};
1536
88
            }
1537
1538
            // Merge the keys from the children into this set.
1539
6.63M
            for (auto& sub : subs) {
1540
6.62M
                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
6.62M
                if (key_set.size() < sub->size()) std::swap(key_set, *sub);
1544
6.62M
                key_set.merge(*sub);
1545
6.62M
                if (key_set.size() != keys_count) {
1546
10
                    node.has_duplicate_keys = true;
1547
10
                    return {};
1548
10
                }
1549
6.62M
            }
1550
1551
6.63M
            node.has_duplicate_keys = false;
1552
6.63M
            return key_set;
1553
6.63M
        };
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
5.61M
        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
5.61M
            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
5.61M
            for (auto& sub : subs) {
1522
5.60M
                if (!sub.has_value()) {
1523
0
                    node.has_duplicate_keys = true;
1524
0
                    return {};
1525
0
                }
1526
5.60M
            }
1527
1528
            // Start building the set of keys involved in this node and children.
1529
            // Start by keys in this node directly.
1530
5.61M
            size_t keys_count = node.keys.size();
1531
5.61M
            keyset key_set{node.keys.begin(), node.keys.end(), Comp(ctx)};
1532
5.61M
            if (key_set.size() != keys_count) {
1533
                // It already has duplicates; bail out.
1534
88
                node.has_duplicate_keys = true;
1535
88
                return {};
1536
88
            }
1537
1538
            // Merge the keys from the children into this set.
1539
5.61M
            for (auto& sub : subs) {
1540
5.60M
                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
5.60M
                if (key_set.size() < sub->size()) std::swap(key_set, *sub);
1544
5.60M
                key_set.merge(*sub);
1545
5.60M
                if (key_set.size() != keys_count) {
1546
0
                    node.has_duplicate_keys = true;
1547
0
                    return {};
1548
0
                }
1549
5.60M
            }
1550
1551
5.61M
            node.has_duplicate_keys = false;
1552
5.61M
            return key_set;
1553
5.61M
        };
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.46k
        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.46k
            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.46k
            for (auto& sub : subs) {
1522
3.19k
                if (!sub.has_value()) {
1523
0
                    node.has_duplicate_keys = true;
1524
0
                    return {};
1525
0
                }
1526
3.19k
            }
1527
1528
            // Start building the set of keys involved in this node and children.
1529
            // Start by keys in this node directly.
1530
3.46k
            size_t keys_count = node.keys.size();
1531
3.46k
            keyset key_set{node.keys.begin(), node.keys.end(), Comp(ctx)};
1532
3.46k
            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.46k
            for (auto& sub : subs) {
1540
3.19k
                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.19k
                if (key_set.size() < sub->size()) std::swap(key_set, *sub);
1544
3.19k
                key_set.merge(*sub);
1545
3.19k
                if (key_set.size() != keys_count) {
1546
0
                    node.has_duplicate_keys = true;
1547
0
                    return {};
1548
0
                }
1549
3.19k
            }
1550
1551
3.46k
            node.has_duplicate_keys = false;
1552
3.46k
            return key_set;
1553
3.46k
        };
1554
1555
5.89k
        TreeEval<state>(upfn);
1556
5.89k
    }
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
877
    {
1502
        // We cannot use a lambda here, as lambdas are non assignable, and the set operations
1503
        // below require moving the comparators around.
1504
877
        struct Comp {
1505
877
            const Ctx* ctx_ptr;
1506
877
            Comp(const Ctx& ctx) : ctx_ptr(&ctx) {}
1507
877
            bool operator()(const Key& a, const Key& b) const { return ctx_ptr->KeyCompare(a, b); }
1508
877
        };
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
877
        using keyset = std::set<Key, Comp>;
1514
877
        using state = std::optional<keyset>;
1515
1516
877
        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
877
            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
877
            for (auto& sub : subs) {
1522
877
                if (!sub.has_value()) {
1523
877
                    node.has_duplicate_keys = true;
1524
877
                    return {};
1525
877
                }
1526
877
            }
1527
1528
            // Start building the set of keys involved in this node and children.
1529
            // Start by keys in this node directly.
1530
877
            size_t keys_count = node.keys.size();
1531
877
            keyset key_set{node.keys.begin(), node.keys.end(), Comp(ctx)};
1532
877
            if (key_set.size() != keys_count) {
1533
                // It already has duplicates; bail out.
1534
877
                node.has_duplicate_keys = true;
1535
877
                return {};
1536
877
            }
1537
1538
            // Merge the keys from the children into this set.
1539
877
            for (auto& sub : subs) {
1540
877
                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
877
                if (key_set.size() < sub->size()) std::swap(key_set, *sub);
1544
877
                key_set.merge(*sub);
1545
877
                if (key_set.size() != keys_count) {
1546
877
                    node.has_duplicate_keys = true;
1547
877
                    return {};
1548
877
                }
1549
877
            }
1550
1551
877
            node.has_duplicate_keys = false;
1552
877
            return key_set;
1553
877
        };
1554
1555
877
        TreeEval<state>(upfn);
1556
877
    }
void miniscript::Node<XOnlyPubKey>::DuplicateKeyCheck<TapSatisfier>(TapSatisfier const&) const
Line
Count
Source
1501
4.44k
    {
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.44k
        struct Comp {
1505
4.44k
            const Ctx* ctx_ptr;
1506
4.44k
            Comp(const Ctx& ctx) : ctx_ptr(&ctx) {}
1507
4.44k
            bool operator()(const Key& a, const Key& b) const { return ctx_ptr->KeyCompare(a, b); }
1508
4.44k
        };
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.44k
        using keyset = std::set<Key, Comp>;
1514
4.44k
        using state = std::optional<keyset>;
1515
1516
4.44k
        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.44k
            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.44k
            for (auto& sub : subs) {
1522
4.44k
                if (!sub.has_value()) {
1523
4.44k
                    node.has_duplicate_keys = true;
1524
4.44k
                    return {};
1525
4.44k
                }
1526
4.44k
            }
1527
1528
            // Start building the set of keys involved in this node and children.
1529
            // Start by keys in this node directly.
1530
4.44k
            size_t keys_count = node.keys.size();
1531
4.44k
            keyset key_set{node.keys.begin(), node.keys.end(), Comp(ctx)};
1532
4.44k
            if (key_set.size() != keys_count) {
1533
                // It already has duplicates; bail out.
1534
4.44k
                node.has_duplicate_keys = true;
1535
4.44k
                return {};
1536
4.44k
            }
1537
1538
            // Merge the keys from the children into this set.
1539
4.44k
            for (auto& sub : subs) {
1540
4.44k
                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.44k
                if (key_set.size() < sub->size()) std::swap(key_set, *sub);
1544
4.44k
                key_set.merge(*sub);
1545
4.44k
                if (key_set.size() != keys_count) {
1546
4.44k
                    node.has_duplicate_keys = true;
1547
4.44k
                    return {};
1548
4.44k
                }
1549
4.44k
            }
1550
1551
4.44k
            node.has_duplicate_keys = false;
1552
4.44k
            return key_set;
1553
4.44k
        };
1554
1555
4.44k
        TreeEval<state>(upfn);
1556
4.44k
    }
void miniscript::Node<CPubKey>::DuplicateKeyCheck<WshSatisfier>(WshSatisfier const&) const
Line
Count
Source
1501
268
    {
1502
        // We cannot use a lambda here, as lambdas are non assignable, and the set operations
1503
        // below require moving the comparators around.
1504
268
        struct Comp {
1505
268
            const Ctx* ctx_ptr;
1506
268
            Comp(const Ctx& ctx) : ctx_ptr(&ctx) {}
1507
268
            bool operator()(const Key& a, const Key& b) const { return ctx_ptr->KeyCompare(a, b); }
1508
268
        };
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
268
        using keyset = std::set<Key, Comp>;
1514
268
        using state = std::optional<keyset>;
1515
1516
268
        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
268
            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
268
            for (auto& sub : subs) {
1522
268
                if (!sub.has_value()) {
1523
268
                    node.has_duplicate_keys = true;
1524
268
                    return {};
1525
268
                }
1526
268
            }
1527
1528
            // Start building the set of keys involved in this node and children.
1529
            // Start by keys in this node directly.
1530
268
            size_t keys_count = node.keys.size();
1531
268
            keyset key_set{node.keys.begin(), node.keys.end(), Comp(ctx)};
1532
268
            if (key_set.size() != keys_count) {
1533
                // It already has duplicates; bail out.
1534
268
                node.has_duplicate_keys = true;
1535
268
                return {};
1536
268
            }
1537
1538
            // Merge the keys from the children into this set.
1539
268
            for (auto& sub : subs) {
1540
268
                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
268
                if (key_set.size() < sub->size()) std::swap(key_set, *sub);
1544
268
                key_set.merge(*sub);
1545
268
                if (key_set.size() != keys_count) {
1546
268
                    node.has_duplicate_keys = true;
1547
268
                    return {};
1548
268
                }
1549
268
            }
1550
1551
268
            node.has_duplicate_keys = false;
1552
268
            return key_set;
1553
268
        };
1554
1555
268
        TreeEval<state>(upfn);
1556
268
    }
1557
1558
    //! Return the size of the script for this expression (faster than ToScript().size()).
1559
13.6M
    size_t ScriptSize() const { return scriptlen; }
miniscript::Node<CPubKey>::ScriptSize() const
Line
Count
Source
1559
58.4k
    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
11.2M
    size_t ScriptSize() const { return scriptlen; }
1560
1561
    //! Return the maximum number of ops needed to satisfy this script non-malleably.
1562
2.22k
    std::optional<uint32_t> GetOps() const {
1563
2.22k
        if (!ops.sat.Valid()) return {};
1564
2.21k
        return ops.count + ops.sat.Value();
1565
2.22k
    }
miniscript::Node<CPubKey>::GetOps() const
Line
Count
Source
1562
1.62k
    std::optional<uint32_t> GetOps() const {
1563
1.62k
        if (!ops.sat.Valid()) return {};
1564
1.61k
        return ops.count + ops.sat.Value();
1565
1.62k
    }
miniscript::Node<unsigned int>::GetOps() const
Line
Count
Source
1562
598
    std::optional<uint32_t> GetOps() const {
1563
598
        if (!ops.sat.Valid()) return {};
1564
595
        return ops.count + ops.sat.Value();
1565
598
    }
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.46k
    bool CheckOpsLimit() const {
1572
6.46k
        if (IsTapscript(m_script_ctx)) return true;
1573
2.10k
        if (const auto ops = GetOps()) return *ops <= MAX_OPS_PER_SCRIPT;
1574
12
        return true;
1575
2.10k
    }
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
986
    bool CheckOpsLimit() const {
1572
986
        if (IsTapscript(m_script_ctx)) return true;
1573
598
        if (const auto ops = GetOps()) return *ops <= MAX_OPS_PER_SCRIPT;
1574
3
        return true;
1575
598
    }
1576
1577
    /** Whether this node is of type B, K or W. (That is, anything but V.) */
1578
7.28k
    bool IsBKW() const {
1579
7.28k
        return !((GetType() & "BKW"_mst) == ""_mst);
1580
7.28k
    }
miniscript::Node<CPubKey>::IsBKW() const
Line
Count
Source
1578
5.97k
    bool IsBKW() const {
1579
5.97k
        return !((GetType() & "BKW"_mst) == ""_mst);
1580
5.97k
    }
miniscript::Node<unsigned int>::IsBKW() const
Line
Count
Source
1578
1.30k
    bool IsBKW() const {
1579
1.30k
        return !((GetType() & "BKW"_mst) == ""_mst);
1580
1.30k
    }
1581
1582
    /** Return the maximum number of stack elements needed to satisfy this script non-malleably. */
1583
2.81k
    std::optional<uint32_t> GetStackSize() const {
1584
2.81k
        if (!ss.Sat().Valid()) return {};
1585
2.80k
        return ss.Sat().NetDiff() + static_cast<int32_t>(IsBKW());
1586
2.81k
    }
miniscript::Node<CPubKey>::GetStackSize() const
Line
Count
Source
1583
1.89k
    std::optional<uint32_t> GetStackSize() const {
1584
1.89k
        if (!ss.Sat().Valid()) return {};
1585
1.88k
        return ss.Sat().NetDiff() + static_cast<int32_t>(IsBKW());
1586
1.89k
    }
miniscript::Node<unsigned int>::GetStackSize() const
Line
Count
Source
1583
925
    std::optional<uint32_t> GetStackSize() const {
1584
925
        if (!ss.Sat().Valid()) return {};
1585
921
        return ss.Sat().NetDiff() + static_cast<int32_t>(IsBKW());
1586
925
    }
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.47k
        return ss.Sat().Exec() + static_cast<int32_t>(IsBKW());
1592
4.48k
    }
miniscript::Node<CPubKey>::GetExecStackSize() const
Line
Count
Source
1589
4.09k
    std::optional<uint32_t> GetExecStackSize() const {
1590
4.09k
        if (!ss.Sat().Valid()) return {};
1591
4.09k
        return ss.Sat().Exec() + static_cast<int32_t>(IsBKW());
1592
4.09k
    }
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.46k
    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.46k
        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.10k
        if (const auto ss = GetStackSize()) return *ss <= MAX_STANDARD_P2WSH_STACK_ITEMS;
1603
12
        return true;
1604
2.10k
    }
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
986
    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
986
        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
598
        if (const auto ss = GetStackSize()) return *ss <= MAX_STANDARD_P2WSH_STACK_ITEMS;
1603
3
        return true;
1604
598
    }
1605
1606
    //! Whether no satisfaction exists for this node.
1607
191
    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
526
    std::optional<uint32_t> GetWitnessSize() const {
1612
526
        if (!ws.sat.Valid()) return {};
1613
526
        return ws.sat.Value();
1614
526
    }
miniscript::Node<CPubKey>::GetWitnessSize() const
Line
Count
Source
1611
372
    std::optional<uint32_t> GetWitnessSize() const {
1612
372
        if (!ws.sat.Valid()) return {};
1613
372
        return ws.sat.Value();
1614
372
    }
miniscript::Node<unsigned int>::GetWitnessSize() const
Line
Count
Source
1611
154
    std::optional<uint32_t> GetWitnessSize() const {
1612
154
        if (!ws.sat.Valid()) return {};
1613
154
        return ws.sat.Value();
1614
154
    }
1615
1616
    //! Return the expression type.
1617
129M
    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
101M
    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
6.32M
    bool IsValid() const {
1676
6.32M
        if (GetType() == ""_mst) return false;
1677
6.32M
        return ScriptSize() <= internal::MaxScriptSize(m_script_ctx);
1678
6.32M
    }
miniscript::Node<CPubKey>::IsValid() const
Line
Count
Source
1675
31.7k
    bool IsValid() const {
1676
31.7k
        if (GetType() == ""_mst) return false;
1677
31.7k
        return ScriptSize() <= internal::MaxScriptSize(m_script_ctx);
1678
31.7k
    }
miniscript::Node<unsigned int>::IsValid() const
Line
Count
Source
1675
674k
    bool IsValid() const {
1676
674k
        if (GetType() == ""_mst) return false;
1677
674k
        return ScriptSize() <= internal::MaxScriptSize(m_script_ctx);
1678
674k
    }
miniscript::Node<XOnlyPubKey>::IsValid() const
Line
Count
Source
1675
5.62M
    bool IsValid() const {
1676
5.62M
        if (GetType() == ""_mst) return false;
1677
5.62M
        return ScriptSize() <= internal::MaxScriptSize(m_script_ctx);
1678
5.62M
    }
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.72k
    bool IsValidTopLevel() const { return IsValid() && GetType() << "B"_mst; }
miniscript::Node<unsigned int>::IsValidTopLevel() const
Line
Count
Source
1681
1.55k
    bool IsValidTopLevel() const { return IsValid() && GetType() << "B"_mst; }
miniscript::Node<XOnlyPubKey>::IsValidTopLevel() const
Line
Count
Source
1681
4.44k
    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.31k
    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
997
    bool IsNonMalleable() const { return GetType() << "m"_mst; }
1685
1686
    //! Check whether this script always needs a signature.
1687
4.85k
    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
872
    bool NeedsSignature() const { return GetType() << "s"_mst; }
1688
1689
    //! Check whether there is no satisfaction path that contains both timelocks and heightlocks
1690
5.14k
    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
986
    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.87k
    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
978
    bool CheckDuplicateKey() const { return has_duplicate_keys && !*has_duplicate_keys; }
1694
1695
    //! Whether successful non-malleable satisfactions are guaranteed to be valid.
1696
6.46k
    bool ValidSatisfactions() const { return IsValid() && CheckOpsLimit() && CheckStackSize(); }
miniscript::Node<CPubKey>::ValidSatisfactions() const
Line
Count
Source
1696
5.47k
    bool ValidSatisfactions() const { return IsValid() && CheckOpsLimit() && CheckStackSize(); }
miniscript::Node<unsigned int>::ValidSatisfactions() const
Line
Count
Source
1696
990
    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.20k
    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
990
    bool IsSaneSubexpression() const { return ValidSatisfactions() && IsNonMalleable() && CheckTimeLocksMix() && CheckDuplicateKey(); }
1700
1701
    //! Check whether this node is safe as a script on its own.
1702
6.09k
    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
885
    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.53k
    Availability Satisfy(const Ctx& ctx, std::vector<std::vector<unsigned char>>& stack, bool nonmalleable = true) const {
1710
9.53k
        auto ret = ProduceInput(ctx);
1711
9.53k
        if (nonmalleable && (ret.sat.malleable || !ret.sat.has_sig)) return Availability::NO;
1712
3.47k
        stack = std::move(ret.sat.stack);
1713
3.47k
        return ret.sat.available;
1714
9.53k
    }
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.67k
        stack = std::move(ret.sat.stack);
1713
2.67k
        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.44k
    Availability Satisfy(const Ctx& ctx, std::vector<std::vector<unsigned char>>& stack, bool nonmalleable = true) const {
1710
4.44k
        auto ret = ProduceInput(ctx);
1711
4.44k
        if (nonmalleable && (ret.sat.malleable || !ret.sat.has_sig)) return Availability::NO;
1712
680
        stack = std::move(ret.sat.stack);
1713
680
        return ret.sat.available;
1714
4.44k
    }
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
268
    Availability Satisfy(const Ctx& ctx, std::vector<std::vector<unsigned char>>& stack, bool nonmalleable = true) const {
1710
268
        auto ret = ProduceInput(ctx);
1711
268
        if (nonmalleable && (ret.sat.malleable || !ret.sat.has_sig)) return Availability::NO;
1712
119
        stack = std::move(ret.sat.stack);
1713
119
        return ret.sat.available;
1714
268
    }
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
363
        : 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
158
        : 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
193
        : 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.52k
        : 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.96k
        : 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
1.87k
        : 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.68k
        : 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
6.81M
        : 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.9k
        : 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
5.60M
        : 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.66k
        : 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
773
        : 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
779
        : 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
8.46M
    Node(Node&&) noexcept = default;
miniscript::Node<CPubKey>::Node(miniscript::Node<CPubKey>&&)
Line
Count
Source
1752
45.6k
    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
5.62M
    Node(Node&&) noexcept = default;
1753
6.81M
    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.1k
    Node& operator=(Node&&) noexcept = default;
miniscript::Node<XOnlyPubKey>::operator=(miniscript::Node<XOnlyPubKey>&&)
Line
Count
Source
1753
5.60M
    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.20k
    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
417
{
1818
417
    std::span<const char> expr = script::Expr(in);
1819
417
    if (!script::Func(func, expr)) return {};
1820
415
    return ctx.FromString(expr);
1821
417
}
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.85k
{
1839
9.85k
    Node<Key> child{std::move(constructed.back())};
1840
9.85k
    constructed.pop_back();
1841
9.85k
    if (reverse) {
1842
4.96k
        constructed.back() = Node<Key>{internal::NoDupCheck{}, script_ctx, nt, Vector(std::move(child), std::move(constructed.back()))};
1843
4.96k
    } 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.85k
}
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.59k
{
1839
7.59k
    Node<Key> child{std::move(constructed.back())};
1840
7.59k
    constructed.pop_back();
1841
7.59k
    if (reverse) {
1842
3.00k
        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.59k
}
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.23k
{
1839
1.23k
    Node<Key> child{std::move(constructed.back())};
1840
1.23k
    constructed.pop_back();
1841
1.23k
    if (reverse) {
1842
946
        constructed.back() = Node<Key>{internal::NoDupCheck{}, script_ctx, nt, Vector(std::move(child), std::move(constructed.back()))};
1843
946
    } else {
1844
288
        constructed.back() = Node<Key>{internal::NoDupCheck{}, script_ctx, nt, Vector(std::move(constructed.back()), std::move(child))};
1845
288
    }
1846
1.23k
}
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.02k
{
1839
1.02k
    Node<Key> child{std::move(constructed.back())};
1840
1.02k
    constructed.pop_back();
1841
1.02k
    if (reverse) {
1842
1.02k
        constructed.back() = Node<Key>{internal::NoDupCheck{}, script_ctx, nt, Vector(std::move(child), std::move(constructed.back()))};
1843
1.02k
    } else {
1844
0
        constructed.back() = Node<Key>{internal::NoDupCheck{}, script_ctx, nt, Vector(std::move(constructed.back()), std::move(child))};
1845
0
    }
1846
1.02k
}
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
794
{
1856
794
    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
794
    size_t script_size{1};
1869
794
    size_t max_size{internal::MaxScriptSize(ctx.MsContext())};
1870
1871
    // The two integers are used to hold state for thresh()
1872
794
    std::vector<std::tuple<ParseContext, int64_t, int64_t>> to_parse;
1873
794
    std::vector<Node<Key>> constructed;
1874
1875
794
    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
794
    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
85
                    script_size += 1;
1940
85
                    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
528
                } else if (Const("or_b(", in)) {
2071
58
                    to_parse.emplace_back(ParseContext::OR_B, -1, -1);
2072
58
                    script_size += 2;
2073
470
                } else if (Const("or_c(", in)) {
2074
28
                    to_parse.emplace_back(ParseContext::OR_C, -1, -1);
2075
28
                    script_size += 3;
2076
442
                } else if (Const("or_d(", in)) {
2077
42
                    to_parse.emplace_back(ParseContext::OR_D, -1, -1);
2078
42
                    script_size += 4;
2079
400
                } else if (Const("or_i(", in)) {
2080
45
                    to_parse.emplace_back(ParseContext::OR_I, -1, -1);
2081
45
                    script_size += 4;
2082
355
                } else {
2083
355
                    return {};
2084
355
                }
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
85
        case ParseContext::SWAP: {
2097
85
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_S, Vector(std::move(constructed.back()))};
2098
85
            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
57
        case ParseContext::OR_B: {
2144
57
            BuildBack(ctx.MsContext(), Fragment::OR_B, constructed);
2145
57
            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.67k
        case ParseContext::COMMA: {
2191
6.67k
            if (in.size() < 1 || in[0] != ',') return {};
2192
6.67k
            in = in.subspan(1);
2193
6.67k
            break;
2194
6.67k
        }
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
794
    assert(constructed.size() >= 1);
2205
400
    CHECK_NONFATAL(constructed.size() == 1);
2206
400
    assert(constructed[0].ScriptSize() == script_size);
2207
400
    if (in.size() > 0) return {};
2208
397
    Node<Key> tl_node{std::move(constructed.front())};
2209
397
    tl_node.DuplicateKeyCheck(ctx);
2210
397
    return tl_node;
2211
400
}
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
574
{
1856
574
    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
574
    size_t script_size{1};
1869
574
    size_t max_size{internal::MaxScriptSize(ctx.MsContext())};
1870
1871
    // The two integers are used to hold state for thresh()
1872
574
    std::vector<std::tuple<ParseContext, int64_t, int64_t>> to_parse;
1873
574
    std::vector<Node<Key>> constructed;
1874
1875
574
    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
574
    const auto parse_multi_exp = [&](std::span<const char>& in, const bool is_multi_a) -> bool {
1879
574
        const auto max_keys{is_multi_a ? MAX_PUBKEYS_PER_MULTI_A : MAX_PUBKEYS_PER_MULTISIG};
1880
574
        const auto required_ctx{is_multi_a ? MiniscriptContext::TAPSCRIPT : MiniscriptContext::P2WSH};
1881
574
        if (ctx.MsContext() != required_ctx) return false;
1882
        // Get threshold
1883
574
        int next_comma = FindNextChar(in, ',');
1884
574
        if (next_comma < 1) return false;
1885
574
        const auto k_to_integral{ToIntegral<int64_t>(std::string_view(in.data(), next_comma))};
1886
574
        if (!k_to_integral.has_value()) return false;
1887
574
        const int64_t k{k_to_integral.value()};
1888
574
        in = in.subspan(next_comma + 1);
1889
        // Get keys. It is compatible for both compressed and x-only keys.
1890
574
        std::vector<Key> keys;
1891
574
        while (next_comma != -1) {
1892
574
            next_comma = FindNextChar(in, ',');
1893
574
            int key_length = (next_comma == -1) ? FindNextChar(in, ')') : next_comma;
1894
574
            if (key_length < 1) return false;
1895
574
            std::span<const char> sp{in.begin(), in.begin() + key_length};
1896
574
            auto key = ctx.FromString(sp);
1897
574
            if (!key) return false;
1898
574
            keys.push_back(std::move(*key));
1899
574
            in = in.subspan(key_length + 1);
1900
574
        }
1901
574
        if (keys.size() < 1 || keys.size() > max_keys) return false;
1902
574
        if (k < 1 || k > (int64_t)keys.size()) return false;
1903
574
        if (is_multi_a) {
1904
            // (push + xonly-key + CHECKSIG[ADD]) * n + k + OP_NUMEQUAL(VERIFY), minus one.
1905
574
            script_size += (1 + 32 + 1) * keys.size() + BuildScript(k).size();
1906
574
            constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::MULTI_A, std::move(keys), k);
1907
574
        } else {
1908
574
            script_size += 2 + (keys.size() > 16) + (k > 16) + 34 * keys.size();
1909
574
            constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::MULTI, std::move(keys), k);
1910
574
        }
1911
574
        return true;
1912
574
    };
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.31k
        case ParseContext::WRAPPED_EXPR: {
1923
1.31k
            std::optional<size_t> colon_index{};
1924
662k
            for (size_t i = 1; i < in.size(); ++i) {
1925
662k
                if (in[i] == ':') {
1926
340
                    colon_index = i;
1927
340
                    break;
1928
340
                }
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.31k
            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
64
                    script_size += 1;
1940
64
                    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.31k
            to_parse.emplace_back(ParseContext::EXPR, -1, -1);
1975
1.31k
            if (colon_index) in = in.subspan(*colon_index + 1);
1976
1.31k
            break;
1977
1.31k
        }
1978
1.31k
        case ParseContext::EXPR: {
1979
1.31k
            if (Const("0", in)) {
1980
3
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::JUST_0);
1981
1.31k
            } 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
307
                std::optional<Key> key = ParseKey<Key, Ctx>("pk", in, ctx);
1985
307
                if (!key) return {};
1986
305
                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
305
                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
923
            } 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
898
            } 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
895
            } 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
887
            } 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
879
            } 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
871
            } 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
855
            } 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
806
            } 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
729
            } else if (Const("multi(", in)) {
2038
18
                if (!parse_multi_exp(in, /* is_multi_a = */false)) return {};
2039
711
            } else if (Const("multi_a(", in)) {
2040
14
                if (!parse_multi_exp(in, /* is_multi_a = */true)) return {};
2041
697
            } 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
664
            } 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
639
            } else {
2061
639
                if (Const("and_n(", in)) {
2062
8
                    to_parse.emplace_back(ParseContext::AND_N, -1, -1);
2063
8
                    script_size += 5;
2064
631
                } else if (Const("and_b(", in)) {
2065
41
                    to_parse.emplace_back(ParseContext::AND_B, -1, -1);
2066
41
                    script_size += 2;
2067
590
                } else if (Const("and_v(", in)) {
2068
159
                    to_parse.emplace_back(ParseContext::AND_V, -1, -1);
2069
159
                    script_size += 1;
2070
431
                } else if (Const("or_b(", in)) {
2071
36
                    to_parse.emplace_back(ParseContext::OR_B, -1, -1);
2072
36
                    script_size += 2;
2073
395
                } else if (Const("or_c(", in)) {
2074
12
                    to_parse.emplace_back(ParseContext::OR_C, -1, -1);
2075
12
                    script_size += 3;
2076
383
                } else if (Const("or_d(", in)) {
2077
18
                    to_parse.emplace_back(ParseContext::OR_D, -1, -1);
2078
18
                    script_size += 4;
2079
365
                } else if (Const("or_i(", in)) {
2080
10
                    to_parse.emplace_back(ParseContext::OR_I, -1, -1);
2081
10
                    script_size += 4;
2082
355
                } else {
2083
355
                    return {};
2084
355
                }
2085
284
                to_parse.emplace_back(ParseContext::CLOSE_BRACKET, -1, -1);
2086
284
                to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2087
284
                to_parse.emplace_back(ParseContext::COMMA, -1, -1);
2088
284
                to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1);
2089
284
            }
2090
958
            break;
2091
1.31k
        }
2092
958
        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.31k
        }
2096
64
        case ParseContext::SWAP: {
2097
64
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_S, Vector(std::move(constructed.back()))};
2098
64
            break;
2099
1.31k
        }
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.31k
        }
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.31k
        }
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.31k
        }
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.31k
        }
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.31k
        }
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.31k
        }
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.31k
        }
2129
41
        case ParseContext::AND_B: {
2130
41
            BuildBack(ctx.MsContext(), Fragment::AND_B, constructed);
2131
41
            break;
2132
1.31k
        }
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.31k
        }
2139
155
        case ParseContext::AND_V: {
2140
155
            BuildBack(ctx.MsContext(), Fragment::AND_V, constructed);
2141
155
            break;
2142
1.31k
        }
2143
36
        case ParseContext::OR_B: {
2144
36
            BuildBack(ctx.MsContext(), Fragment::OR_B, constructed);
2145
36
            break;
2146
1.31k
        }
2147
12
        case ParseContext::OR_C: {
2148
12
            BuildBack(ctx.MsContext(), Fragment::OR_C, constructed);
2149
12
            break;
2150
1.31k
        }
2151
18
        case ParseContext::OR_D: {
2152
18
            BuildBack(ctx.MsContext(), Fragment::OR_D, constructed);
2153
18
            break;
2154
1.31k
        }
2155
26
        case ParseContext::OR_I: {
2156
26
            BuildBack(ctx.MsContext(), Fragment::OR_I, constructed);
2157
26
            break;
2158
1.31k
        }
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.31k
        }
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
332
        case ParseContext::COMMA: {
2191
332
            if (in.size() < 1 || in[0] != ',') return {};
2192
332
            in = in.subspan(1);
2193
332
            break;
2194
332
        }
2195
303
        case ParseContext::CLOSE_BRACKET: {
2196
303
            if (in.size() < 1 || in[0] != ')') return {};
2197
303
            in = in.subspan(1);
2198
303
            break;
2199
303
        }
2200
333k
        }
2201
333k
    }
2202
2203
    // Sanity checks on the produced miniscript
2204
574
    assert(constructed.size() >= 1);
2205
212
    CHECK_NONFATAL(constructed.size() == 1);
2206
212
    assert(constructed[0].ScriptSize() == script_size);
2207
212
    if (in.size() > 0) return {};
2208
209
    Node<Key> tl_node{std::move(constructed.front())};
2209
209
    tl_node.DuplicateKeyCheck(ctx);
2210
209
    return tl_node;
2211
212
}
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.52k
{
2298
    // The two integers are used to hold state for thresh()
2299
5.52k
    std::vector<std::tuple<DecodeContext, int64_t, int64_t>> to_parse;
2300
5.52k
    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.52k
    to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2305
2306
12.5M
    while (!to_parse.empty()) {
2307
        // Exit early if the Miniscript is not going to be valid.
2308
12.5M
        if (!constructed.empty() && !constructed.back().IsValid()) return {};
2309
2310
        // Get the current context we are decoding within
2311
12.5M
        auto [cur_context, n, k] = to_parse.back();
2312
12.5M
        to_parse.pop_back();
2313
2314
12.5M
        switch(cur_context) {
2315
6.28M
        case DecodeContext::SINGLE_BKV_EXPR: {
2316
6.28M
            if (in >= last) return {};
2317
2318
            // Constants
2319
6.28M
            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
6.28M
            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
6.28M
            if (in[0].second.size() == 33 || in[0].second.size() == 32) {
2331
5.68k
                auto key = ctx.FromPKBytes(in[0].second.begin(), in[0].second.end());
2332
5.68k
                if (!key) return {};
2333
5.67k
                ++in;
2334
5.67k
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_K, Vector(std::move(*key)));
2335
5.67k
                break;
2336
5.68k
            }
2337
6.27M
            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
670
                auto key = ctx.FromPKHBytes(in[2].second.begin(), in[2].second.end());
2339
670
                if (!key) return {};
2340
667
                in += 5;
2341
667
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_H, Vector(std::move(*key)));
2342
667
                break;
2343
670
            }
2344
            // Time locks
2345
6.27M
            std::optional<int64_t> num;
2346
6.27M
            if (last - in >= 2 && in[0].first == OP_CHECKSEQUENCEVERIFY && (num = ParseScriptNumber(in[1]))) {
2347
2.34k
                in += 2;
2348
2.34k
                if (*num < 1 || *num > 0x7FFFFFFFL) return {};
2349
2.34k
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::OLDER, *num);
2350
2.34k
                break;
2351
2.34k
            }
2352
6.27M
            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
6.27M
            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
274
                if (in[2].first == OP_SHA256 && in[1].second.size() == 32) {
2361
74
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::SHA256, in[1].second);
2362
74
                    in += 7;
2363
74
                    break;
2364
200
                } 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
145
                } 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
59
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::HASH160, in[1].second);
2374
59
                    in += 7;
2375
59
                    break;
2376
59
                }
2377
274
            }
2378
            // Multi
2379
6.27M
            if (last - in >= 3 && in[0].first == OP_CHECKMULTISIG) {
2380
126
                if (IsTapscript(ctx.MsContext())) return {};
2381
126
                std::vector<Key> keys;
2382
126
                const auto n = ParseScriptNumber(in[1]);
2383
126
                if (!n || last - in < 3 + *n) return {};
2384
126
                if (*n < 1 || *n > 20) return {};
2385
419
                for (int i = 0; i < *n; ++i) {
2386
293
                    if (in[2 + i].second.size() != 33) return {};
2387
293
                    auto key = ctx.FromPKBytes(in[2 + i].second.begin(), in[2 + i].second.end());
2388
293
                    if (!key) return {};
2389
293
                    keys.push_back(std::move(*key));
2390
293
                }
2391
126
                const auto k = ParseScriptNumber(in[2 + *n]);
2392
126
                if (!k || *k < 1 || *k > *n) return {};
2393
126
                in += 3 + *n;
2394
126
                std::reverse(keys.begin(), keys.end());
2395
126
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::MULTI, std::move(keys), *k);
2396
126
                break;
2397
126
            }
2398
            // Tapscript's equivalent of multi
2399
6.27M
            if (last - in >= 4 && in[0].first == OP_NUMEQUAL) {
2400
801
                if (!IsTapscript(ctx.MsContext())) return {};
2401
                // The necessary threshold of signatures.
2402
801
                const auto k = ParseScriptNumber(in[1]);
2403
801
                if (!k) return {};
2404
801
                if (*k < 1 || *k > MAX_PUBKEYS_PER_MULTI_A) return {};
2405
801
                if (last - in < 2 + *k * 2) return {};
2406
801
                std::vector<Key> keys;
2407
801
                keys.reserve(*k);
2408
                // Walk through the expected (pubkey, CHECKSIG[ADD]) pairs.
2409
89.6k
                for (int pos = 2;; pos += 2) {
2410
89.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
89.5k
                    if (in[pos].first != OP_CHECKSIGADD && in[pos].first != OP_CHECKSIG) return {};
2413
89.5k
                    if (in[pos + 1].second.size() != 32) return {};
2414
89.5k
                    auto key = ctx.FromPKBytes(in[pos + 1].second.begin(), in[pos + 1].second.end());
2415
89.5k
                    if (!key) return {};
2416
89.5k
                    keys.push_back(std::move(*key));
2417
                    // Make sure early we don't parse an arbitrary large expression.
2418
89.5k
                    if (keys.size() > MAX_PUBKEYS_PER_MULTI_A) return {};
2419
                    // OP_CHECKSIG means it was the last one to parse.
2420
89.5k
                    if (in[pos].first == OP_CHECKSIG) break;
2421
89.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
6.27M
            if (in[0].first == OP_CHECKSIG) {
2433
6.30k
                ++in;
2434
6.30k
                to_parse.emplace_back(DecodeContext::CHECK, -1, -1);
2435
6.30k
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2436
6.30k
                break;
2437
6.30k
            }
2438
            // v: wrapper
2439
6.26M
            if (in[0].first == OP_VERIFY) {
2440
1.81k
                ++in;
2441
1.81k
                to_parse.emplace_back(DecodeContext::VERIFY, -1, -1);
2442
1.81k
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2443
1.81k
                break;
2444
1.81k
            }
2445
            // n: wrapper
2446
6.26M
            if (in[0].first == OP_0NOTEQUAL) {
2447
6.25M
                ++in;
2448
6.25M
                to_parse.emplace_back(DecodeContext::ZERO_NOTEQUAL, -1, -1);
2449
6.25M
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2450
6.25M
                break;
2451
6.25M
            }
2452
            // Thresh
2453
3.85k
            if (last - in >= 3 && in[0].first == OP_EQUAL && (num = ParseScriptNumber(in[1]))) {
2454
303
                if (*num < 1) return {};
2455
303
                in += 2;
2456
303
                to_parse.emplace_back(DecodeContext::THRESH_W, 0, *num);
2457
303
                break;
2458
303
            }
2459
            // OP_ENDIF can be WRAP_J, WRAP_D, ANDOR, OR_C, OR_D, or OR_I
2460
3.54k
            if (in[0].first == OP_ENDIF) {
2461
876
                ++in;
2462
876
                to_parse.emplace_back(DecodeContext::ENDIF, -1, -1);
2463
876
                to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2464
876
                break;
2465
876
            }
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.67k
            if (in[0].first == OP_BOOLAND) {
2473
2.61k
                ++in;
2474
2.61k
                to_parse.emplace_back(DecodeContext::AND_B, -1, -1);
2475
2.61k
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2476
2.61k
                to_parse.emplace_back(DecodeContext::W_EXPR, -1, -1);
2477
2.61k
                break;
2478
2.61k
            }
2479
            // or_b
2480
63
            if (in[0].first == OP_BOOLOR) {
2481
53
                ++in;
2482
53
                to_parse.emplace_back(DecodeContext::OR_B, -1, -1);
2483
53
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2484
53
                to_parse.emplace_back(DecodeContext::W_EXPR, -1, -1);
2485
53
                break;
2486
53
            }
2487
            // Unrecognised expression
2488
10
            return {};
2489
63
        }
2490
12.3k
        case DecodeContext::BKV_EXPR: {
2491
12.3k
            to_parse.emplace_back(DecodeContext::MAYBE_AND_V, -1, -1);
2492
12.3k
            to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2493
12.3k
            break;
2494
63
        }
2495
3.59k
        case DecodeContext::W_EXPR: {
2496
            // a: wrapper
2497
3.59k
            if (in >= last) return {};
2498
3.59k
            if (in[0].first == OP_FROMALTSTACK) {
2499
2.83k
                ++in;
2500
2.83k
                to_parse.emplace_back(DecodeContext::ALT, -1, -1);
2501
2.83k
            } else {
2502
763
                to_parse.emplace_back(DecodeContext::SWAP, -1, -1);
2503
763
            }
2504
3.59k
            to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2505
3.59k
            break;
2506
3.59k
        }
2507
12.3k
        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.3k
            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.71k
                to_parse.emplace_back(DecodeContext::AND_V, -1, -1);
2512
                // BKV_EXPR can contain more AND_V nodes
2513
1.71k
                to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2514
1.71k
            }
2515
12.3k
            break;
2516
3.59k
        }
2517
763
        case DecodeContext::SWAP: {
2518
763
            if (in >= last || in[0].first != OP_SWAP || constructed.empty()) return {};
2519
763
            ++in;
2520
763
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_S, Vector(std::move(constructed.back()))};
2521
763
            break;
2522
763
        }
2523
2.83k
        case DecodeContext::ALT: {
2524
2.83k
            if (in >= last || in[0].first != OP_TOALTSTACK || constructed.empty()) return {};
2525
2.83k
            ++in;
2526
2.83k
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_A, Vector(std::move(constructed.back()))};
2527
2.83k
            break;
2528
2.83k
        }
2529
6.29k
        case DecodeContext::CHECK: {
2530
6.29k
            if (constructed.empty()) return {};
2531
6.29k
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_C, Vector(std::move(constructed.back()))};
2532
6.29k
            break;
2533
6.29k
        }
2534
94
        case DecodeContext::DUP_IF: {
2535
94
            if (constructed.empty()) return {};
2536
94
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_D, Vector(std::move(constructed.back()))};
2537
94
            break;
2538
94
        }
2539
1.81k
        case DecodeContext::VERIFY: {
2540
1.81k
            if (constructed.empty()) return {};
2541
1.81k
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_V, Vector(std::move(constructed.back()))};
2542
1.81k
            break;
2543
1.81k
        }
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
6.25M
        case DecodeContext::ZERO_NOTEQUAL: {
2550
6.25M
            if (constructed.empty()) return {};
2551
6.25M
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_N, Vector(std::move(constructed.back()))};
2552
6.25M
            break;
2553
6.25M
        }
2554
1.71k
        case DecodeContext::AND_V: {
2555
1.71k
            if (constructed.size() < 2) return {};
2556
1.71k
            BuildBack(ctx.MsContext(), Fragment::AND_V, constructed, /*reverse=*/true);
2557
1.71k
            break;
2558
1.71k
        }
2559
2.61k
        case DecodeContext::AND_B: {
2560
2.61k
            if (constructed.size() < 2) return {};
2561
2.61k
            BuildBack(ctx.MsContext(), Fragment::AND_B, constructed, /*reverse=*/true);
2562
2.61k
            break;
2563
2.61k
        }
2564
53
        case DecodeContext::OR_B: {
2565
53
            if (constructed.size() < 2) return {};
2566
53
            BuildBack(ctx.MsContext(), Fragment::OR_B, constructed, /*reverse=*/true);
2567
53
            break;
2568
53
        }
2569
22
        case DecodeContext::OR_C: {
2570
22
            if (constructed.size() < 2) return {};
2571
22
            BuildBack(ctx.MsContext(), Fragment::OR_C, constructed, /*reverse=*/true);
2572
22
            break;
2573
22
        }
2574
72
        case DecodeContext::OR_D: {
2575
72
            if (constructed.size() < 2) return {};
2576
72
            BuildBack(ctx.MsContext(), Fragment::OR_D, constructed, /*reverse=*/true);
2577
72
            break;
2578
72
        }
2579
181
        case DecodeContext::ANDOR: {
2580
181
            if (constructed.size() < 3) return {};
2581
181
            Node left{std::move(constructed.back())};
2582
181
            constructed.pop_back();
2583
181
            Node right{std::move(constructed.back())};
2584
181
            constructed.pop_back();
2585
181
            Node mid{std::move(constructed.back())};
2586
181
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::ANDOR, Vector(std::move(left), std::move(mid), std::move(right))};
2587
181
            break;
2588
181
        }
2589
1.23k
        case DecodeContext::THRESH_W: {
2590
1.23k
            if (in >= last) return {};
2591
1.23k
            if (in[0].first == OP_ADD) {
2592
936
                ++in;
2593
936
                to_parse.emplace_back(DecodeContext::THRESH_W, n+1, k);
2594
936
                to_parse.emplace_back(DecodeContext::W_EXPR, -1, -1);
2595
936
            } else {
2596
303
                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
303
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2599
303
            }
2600
1.23k
            break;
2601
1.23k
        }
2602
303
        case DecodeContext::THRESH_E: {
2603
303
            if (k < 1 || k > n || constructed.size() < static_cast<size_t>(n)) return {};
2604
303
            std::vector<Node<Key>> subs;
2605
1.54k
            for (int i = 0; i < n; ++i) {
2606
1.23k
                Node sub{std::move(constructed.back())};
2607
1.23k
                constructed.pop_back();
2608
1.23k
                subs.push_back(std::move(sub));
2609
1.23k
            }
2610
303
            constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::THRESH, std::move(subs), k);
2611
303
            break;
2612
303
        }
2613
875
        case DecodeContext::ENDIF: {
2614
875
            if (in >= last) return {};
2615
2616
            // could be andor or or_i
2617
875
            if (in[0].first == OP_ELSE) {
2618
679
                ++in;
2619
679
                to_parse.emplace_back(DecodeContext::ENDIF_ELSE, -1, -1);
2620
679
                to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2621
679
            }
2622
            // could be j: or d: wrapper
2623
196
            else if (in[0].first == OP_IF) {
2624
102
                if (last - in >= 2 && in[1].first == OP_DUP) {
2625
94
                    in += 2;
2626
94
                    to_parse.emplace_back(DecodeContext::DUP_IF, -1, -1);
2627
94
                } 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
102
            } else if (in[0].first == OP_NOTIF) {
2636
94
                ++in;
2637
94
                to_parse.emplace_back(DecodeContext::ENDIF_NOTIF, -1, -1);
2638
94
            }
2639
0
            else {
2640
0
                return {};
2641
0
            }
2642
875
            break;
2643
875
        }
2644
875
        case DecodeContext::ENDIF_NOTIF: {
2645
94
            if (in >= last) return {};
2646
94
            if (in[0].first == OP_IFDUP) {
2647
72
                ++in;
2648
72
                to_parse.emplace_back(DecodeContext::OR_D, -1, -1);
2649
72
            } else {
2650
22
                to_parse.emplace_back(DecodeContext::OR_C, -1, -1);
2651
22
            }
2652
            // or_c and or_d both require X to have type modifier d so, can't contain and_v
2653
94
            to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2654
94
            break;
2655
94
        }
2656
679
        case DecodeContext::ENDIF_ELSE: {
2657
679
            if (in >= last) return {};
2658
679
            if (in[0].first == OP_IF) {
2659
498
                ++in;
2660
498
                BuildBack(ctx.MsContext(), Fragment::OR_I, constructed, /*reverse=*/true);
2661
498
            } else if (in[0].first == OP_NOTIF) {
2662
181
                ++in;
2663
181
                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
181
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2666
181
            } else {
2667
0
                return {};
2668
0
            }
2669
679
            break;
2670
679
        }
2671
12.5M
        }
2672
12.5M
    }
2673
5.50k
    if (constructed.size() != 1) return {};
2674
5.50k
    Node tl_node{std::move(constructed.front())};
2675
5.50k
    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.50k
    if (!tl_node.IsValidTopLevel()) return {};
2679
5.50k
    return tl_node;
2680
5.50k
}
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
680
{
2298
    // The two integers are used to hold state for thresh()
2299
680
    std::vector<std::tuple<DecodeContext, int64_t, int64_t>> to_parse;
2300
680
    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
680
    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
663k
        case DecodeContext::SINGLE_BKV_EXPR: {
2316
663k
            if (in >= last) return {};
2317
2318
            // Constants
2319
663k
            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
663k
            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
663k
            if (in[0].second.size() == 33 || in[0].second.size() == 32) {
2331
1.04k
                auto key = ctx.FromPKBytes(in[0].second.begin(), in[0].second.end());
2332
1.04k
                if (!key) return {};
2333
1.04k
                ++in;
2334
1.04k
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_K, Vector(std::move(*key)));
2335
1.04k
                break;
2336
1.04k
            }
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
298
                auto key = ctx.FromPKHBytes(in[2].second.begin(), in[2].second.end());
2339
298
                if (!key) return {};
2340
296
                in += 5;
2341
296
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_H, Vector(std::move(*key)));
2342
296
                break;
2343
298
            }
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
188
                in += 2;
2348
188
                if (*num < 1 || *num > 0x7FFFFFFFL) return {};
2349
188
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::OLDER, *num);
2350
188
                break;
2351
188
            }
2352
662k
            if (last - in >= 2 && in[0].first == OP_CHECKLOCKTIMEVERIFY && (num = ParseScriptNumber(in[1]))) {
2353
230
                in += 2;
2354
230
                if (num < 1 || num > 0x7FFFFFFFL) return {};
2355
230
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::AFTER, *num);
2356
230
                break;
2357
230
            }
2358
            // Hashes
2359
661k
            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
153
                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
125
                } 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
89
                } 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
41
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::HASH160, in[1].second);
2374
41
                    in += 7;
2375
41
                    break;
2376
41
                }
2377
153
            }
2378
            // Multi
2379
661k
            if (last - in >= 3 && in[0].first == OP_CHECKMULTISIG) {
2380
90
                if (IsTapscript(ctx.MsContext())) return {};
2381
90
                std::vector<Key> keys;
2382
90
                const auto n = ParseScriptNumber(in[1]);
2383
90
                if (!n || last - in < 3 + *n) return {};
2384
90
                if (*n < 1 || *n > 20) return {};
2385
312
                for (int i = 0; i < *n; ++i) {
2386
222
                    if (in[2 + i].second.size() != 33) return {};
2387
222
                    auto key = ctx.FromPKBytes(in[2 + i].second.begin(), in[2 + i].second.end());
2388
222
                    if (!key) return {};
2389
222
                    keys.push_back(std::move(*key));
2390
222
                }
2391
90
                const auto k = ParseScriptNumber(in[2 + *n]);
2392
90
                if (!k || *k < 1 || *k > *n) return {};
2393
90
                in += 3 + *n;
2394
90
                std::reverse(keys.begin(), keys.end());
2395
90
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::MULTI, std::move(keys), *k);
2396
90
                break;
2397
90
            }
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.32k
                ++in;
2434
1.32k
                to_parse.emplace_back(DecodeContext::CHECK, -1, -1);
2435
1.32k
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2436
1.32k
                break;
2437
1.32k
            }
2438
            // v: wrapper
2439
660k
            if (in[0].first == OP_VERIFY) {
2440
552
                ++in;
2441
552
                to_parse.emplace_back(DecodeContext::VERIFY, -1, -1);
2442
552
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2443
552
                break;
2444
552
            }
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
763
            if (last - in >= 3 && in[0].first == OP_EQUAL && (num = ParseScriptNumber(in[1]))) {
2454
174
                if (*num < 1) return {};
2455
174
                in += 2;
2456
174
                to_parse.emplace_back(DecodeContext::THRESH_W, 0, *num);
2457
174
                break;
2458
174
            }
2459
            // OP_ENDIF can be WRAP_J, WRAP_D, ANDOR, OR_C, OR_D, or OR_I
2460
589
            if (in[0].first == OP_ENDIF) {
2461
383
                ++in;
2462
383
                to_parse.emplace_back(DecodeContext::ENDIF, -1, -1);
2463
383
                to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2464
383
                break;
2465
383
            }
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
206
            if (in[0].first == OP_BOOLAND) {
2473
176
                ++in;
2474
176
                to_parse.emplace_back(DecodeContext::AND_B, -1, -1);
2475
176
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2476
176
                to_parse.emplace_back(DecodeContext::W_EXPR, -1, -1);
2477
176
                break;
2478
176
            }
2479
            // or_b
2480
30
            if (in[0].first == OP_BOOLOR) {
2481
22
                ++in;
2482
22
                to_parse.emplace_back(DecodeContext::OR_B, -1, -1);
2483
22
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2484
22
                to_parse.emplace_back(DecodeContext::W_EXPR, -1, -1);
2485
22
                break;
2486
22
            }
2487
            // Unrecognised expression
2488
8
            return {};
2489
30
        }
2490
2.45k
        case DecodeContext::BKV_EXPR: {
2491
2.45k
            to_parse.emplace_back(DecodeContext::MAYBE_AND_V, -1, -1);
2492
2.45k
            to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2493
2.45k
            break;
2494
30
        }
2495
616
        case DecodeContext::W_EXPR: {
2496
            // a: wrapper
2497
616
            if (in >= last) return {};
2498
616
            if (in[0].first == OP_FROMALTSTACK) {
2499
334
                ++in;
2500
334
                to_parse.emplace_back(DecodeContext::ALT, -1, -1);
2501
334
            } else {
2502
282
                to_parse.emplace_back(DecodeContext::SWAP, -1, -1);
2503
282
            }
2504
616
            to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2505
616
            break;
2506
616
        }
2507
2.44k
        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.44k
            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
503
                to_parse.emplace_back(DecodeContext::AND_V, -1, -1);
2512
                // BKV_EXPR can contain more AND_V nodes
2513
503
                to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2514
503
            }
2515
2.44k
            break;
2516
616
        }
2517
282
        case DecodeContext::SWAP: {
2518
282
            if (in >= last || in[0].first != OP_SWAP || constructed.empty()) return {};
2519
282
            ++in;
2520
282
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_S, Vector(std::move(constructed.back()))};
2521
282
            break;
2522
282
        }
2523
334
        case DecodeContext::ALT: {
2524
334
            if (in >= last || in[0].first != OP_TOALTSTACK || constructed.empty()) return {};
2525
334
            ++in;
2526
334
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_A, Vector(std::move(constructed.back()))};
2527
334
            break;
2528
334
        }
2529
1.32k
        case DecodeContext::CHECK: {
2530
1.32k
            if (constructed.empty()) return {};
2531
1.32k
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_C, Vector(std::move(constructed.back()))};
2532
1.32k
            break;
2533
1.32k
        }
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
552
        case DecodeContext::VERIFY: {
2540
552
            if (constructed.empty()) return {};
2541
552
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_V, Vector(std::move(constructed.back()))};
2542
552
            break;
2543
552
        }
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
501
        case DecodeContext::AND_V: {
2555
501
            if (constructed.size() < 2) return {};
2556
501
            BuildBack(ctx.MsContext(), Fragment::AND_V, constructed, /*reverse=*/true);
2557
501
            break;
2558
501
        }
2559
176
        case DecodeContext::AND_B: {
2560
176
            if (constructed.size() < 2) return {};
2561
176
            BuildBack(ctx.MsContext(), Fragment::AND_B, constructed, /*reverse=*/true);
2562
176
            break;
2563
176
        }
2564
22
        case DecodeContext::OR_B: {
2565
22
            if (constructed.size() < 2) return {};
2566
22
            BuildBack(ctx.MsContext(), Fragment::OR_B, constructed, /*reverse=*/true);
2567
22
            break;
2568
22
        }
2569
16
        case DecodeContext::OR_C: {
2570
16
            if (constructed.size() < 2) return {};
2571
16
            BuildBack(ctx.MsContext(), Fragment::OR_C, constructed, /*reverse=*/true);
2572
16
            break;
2573
16
        }
2574
43
        case DecodeContext::OR_D: {
2575
43
            if (constructed.size() < 2) return {};
2576
43
            BuildBack(ctx.MsContext(), Fragment::OR_D, constructed, /*reverse=*/true);
2577
43
            break;
2578
43
        }
2579
83
        case DecodeContext::ANDOR: {
2580
83
            if (constructed.size() < 3) return {};
2581
83
            Node left{std::move(constructed.back())};
2582
83
            constructed.pop_back();
2583
83
            Node right{std::move(constructed.back())};
2584
83
            constructed.pop_back();
2585
83
            Node mid{std::move(constructed.back())};
2586
83
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::ANDOR, Vector(std::move(left), std::move(mid), std::move(right))};
2587
83
            break;
2588
83
        }
2589
592
        case DecodeContext::THRESH_W: {
2590
592
            if (in >= last) return {};
2591
592
            if (in[0].first == OP_ADD) {
2592
418
                ++in;
2593
418
                to_parse.emplace_back(DecodeContext::THRESH_W, n+1, k);
2594
418
                to_parse.emplace_back(DecodeContext::W_EXPR, -1, -1);
2595
418
            } else {
2596
174
                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
174
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2599
174
            }
2600
592
            break;
2601
592
        }
2602
174
        case DecodeContext::THRESH_E: {
2603
174
            if (k < 1 || k > n || constructed.size() < static_cast<size_t>(n)) return {};
2604
174
            std::vector<Node<Key>> subs;
2605
766
            for (int i = 0; i < n; ++i) {
2606
592
                Node sub{std::move(constructed.back())};
2607
592
                constructed.pop_back();
2608
592
                subs.push_back(std::move(sub));
2609
592
            }
2610
174
            constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::THRESH, std::move(subs), k);
2611
174
            break;
2612
174
        }
2613
382
        case DecodeContext::ENDIF: {
2614
382
            if (in >= last) return {};
2615
2616
            // could be andor or or_i
2617
382
            if (in[0].first == OP_ELSE) {
2618
271
                ++in;
2619
271
                to_parse.emplace_back(DecodeContext::ENDIF_ELSE, -1, -1);
2620
271
                to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2621
271
            }
2622
            // could be j: or d: wrapper
2623
111
            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
59
            } else if (in[0].first == OP_NOTIF) {
2636
59
                ++in;
2637
59
                to_parse.emplace_back(DecodeContext::ENDIF_NOTIF, -1, -1);
2638
59
            }
2639
0
            else {
2640
0
                return {};
2641
0
            }
2642
382
            break;
2643
382
        }
2644
382
        case DecodeContext::ENDIF_NOTIF: {
2645
59
            if (in >= last) return {};
2646
59
            if (in[0].first == OP_IFDUP) {
2647
43
                ++in;
2648
43
                to_parse.emplace_back(DecodeContext::OR_D, -1, -1);
2649
43
            } else {
2650
16
                to_parse.emplace_back(DecodeContext::OR_C, -1, -1);
2651
16
            }
2652
            // or_c and or_d both require X to have type modifier d so, can't contain and_v
2653
59
            to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2654
59
            break;
2655
59
        }
2656
271
        case DecodeContext::ENDIF_ELSE: {
2657
271
            if (in >= last) return {};
2658
271
            if (in[0].first == OP_IF) {
2659
188
                ++in;
2660
188
                BuildBack(ctx.MsContext(), Fragment::OR_I, constructed, /*reverse=*/true);
2661
188
            } else if (in[0].first == OP_NOTIF) {
2662
83
                ++in;
2663
83
                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
83
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2666
83
            } else {
2667
0
                return {};
2668
0
            }
2669
271
            break;
2670
271
        }
2671
1.33M
        }
2672
1.33M
    }
2673
668
    if (constructed.size() != 1) return {};
2674
668
    Node tl_node{std::move(constructed.front())};
2675
668
    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
668
    if (!tl_node.IsValidTopLevel()) return {};
2679
667
    return tl_node;
2680
668
}
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.44k
{
2298
    // The two integers are used to hold state for thresh()
2299
4.44k
    std::vector<std::tuple<DecodeContext, int64_t, int64_t>> to_parse;
2300
4.44k
    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.44k
    to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2305
2306
11.2M
    while (!to_parse.empty()) {
2307
        // Exit early if the Miniscript is not going to be valid.
2308
11.2M
        if (!constructed.empty() && !constructed.back().IsValid()) return {};
2309
2310
        // Get the current context we are decoding within
2311
11.2M
        auto [cur_context, n, k] = to_parse.back();
2312
11.2M
        to_parse.pop_back();
2313
2314
11.2M
        switch(cur_context) {
2315
5.61M
        case DecodeContext::SINGLE_BKV_EXPR: {
2316
5.61M
            if (in >= last) return {};
2317
2318
            // Constants
2319
5.61M
            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
5.61M
            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
5.61M
            if (in[0].second.size() == 33 || in[0].second.size() == 32) {
2331
3.60k
                auto key = ctx.FromPKBytes(in[0].second.begin(), in[0].second.end());
2332
3.60k
                if (!key) return {};
2333
3.60k
                ++in;
2334
3.60k
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_K, Vector(std::move(*key)));
2335
3.60k
                break;
2336
3.60k
            }
2337
5.60M
            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
285
                auto key = ctx.FromPKHBytes(in[2].second.begin(), in[2].second.end());
2339
285
                if (!key) return {};
2340
285
                in += 5;
2341
285
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_H, Vector(std::move(*key)));
2342
285
                break;
2343
285
            }
2344
            // Time locks
2345
5.60M
            std::optional<int64_t> num;
2346
5.60M
            if (last - in >= 2 && in[0].first == OP_CHECKSEQUENCEVERIFY && (num = ParseScriptNumber(in[1]))) {
2347
39
                in += 2;
2348
39
                if (*num < 1 || *num > 0x7FFFFFFFL) return {};
2349
39
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::OLDER, *num);
2350
39
                break;
2351
39
            }
2352
5.60M
            if (last - in >= 2 && in[0].first == OP_CHECKLOCKTIMEVERIFY && (num = ParseScriptNumber(in[1]))) {
2353
740
                in += 2;
2354
740
                if (num < 1 || num > 0x7FFFFFFFL) return {};
2355
740
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::AFTER, *num);
2356
740
                break;
2357
740
            }
2358
            // Hashes
2359
5.60M
            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
5.60M
            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
5.60M
            if (last - in >= 4 && in[0].first == OP_NUMEQUAL) {
2400
793
                if (!IsTapscript(ctx.MsContext())) return {};
2401
                // The necessary threshold of signatures.
2402
793
                const auto k = ParseScriptNumber(in[1]);
2403
793
                if (!k) return {};
2404
793
                if (*k < 1 || *k > MAX_PUBKEYS_PER_MULTI_A) return {};
2405
793
                if (last - in < 2 + *k * 2) return {};
2406
793
                std::vector<Key> keys;
2407
793
                keys.reserve(*k);
2408
                // Walk through the expected (pubkey, CHECKSIG[ADD]) pairs.
2409
89.5k
                for (int pos = 2;; pos += 2) {
2410
89.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
89.5k
                    if (in[pos].first != OP_CHECKSIGADD && in[pos].first != OP_CHECKSIG) return {};
2413
89.5k
                    if (in[pos + 1].second.size() != 32) return {};
2414
89.5k
                    auto key = ctx.FromPKBytes(in[pos + 1].second.begin(), in[pos + 1].second.end());
2415
89.5k
                    if (!key) return {};
2416
89.5k
                    keys.push_back(std::move(*key));
2417
                    // Make sure early we don't parse an arbitrary large expression.
2418
89.5k
                    if (keys.size() > MAX_PUBKEYS_PER_MULTI_A) return {};
2419
                    // OP_CHECKSIG means it was the last one to parse.
2420
89.5k
                    if (in[pos].first == OP_CHECKSIG) break;
2421
89.5k
                }
2422
793
                if (keys.size() < (size_t)*k) return {};
2423
793
                in += 2 + keys.size() * 2;
2424
793
                std::reverse(keys.begin(), keys.end());
2425
793
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::MULTI_A, std::move(keys), *k);
2426
793
                break;
2427
793
            }
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
5.60M
            if (in[0].first == OP_CHECKSIG) {
2433
3.89k
                ++in;
2434
3.89k
                to_parse.emplace_back(DecodeContext::CHECK, -1, -1);
2435
3.89k
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2436
3.89k
                break;
2437
3.89k
            }
2438
            // v: wrapper
2439
5.60M
            if (in[0].first == OP_VERIFY) {
2440
995
                ++in;
2441
995
                to_parse.emplace_back(DecodeContext::VERIFY, -1, -1);
2442
995
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2443
995
                break;
2444
995
            }
2445
            // n: wrapper
2446
5.60M
            if (in[0].first == OP_0NOTEQUAL) {
2447
5.60M
                ++in;
2448
5.60M
                to_parse.emplace_back(DecodeContext::ZERO_NOTEQUAL, -1, -1);
2449
5.60M
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2450
5.60M
                break;
2451
5.60M
            }
2452
            // Thresh
2453
45
            if (last - in >= 3 && in[0].first == OP_EQUAL && (num = ParseScriptNumber(in[1]))) {
2454
8
                if (*num < 1) return {};
2455
8
                in += 2;
2456
8
                to_parse.emplace_back(DecodeContext::THRESH_W, 0, *num);
2457
8
                break;
2458
8
            }
2459
            // OP_ENDIF can be WRAP_J, WRAP_D, ANDOR, OR_C, OR_D, or OR_I
2460
37
            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
31
            if (in[0].first == OP_BOOLAND) {
2473
8
                ++in;
2474
8
                to_parse.emplace_back(DecodeContext::AND_B, -1, -1);
2475
8
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2476
8
                to_parse.emplace_back(DecodeContext::W_EXPR, -1, -1);
2477
8
                break;
2478
8
            }
2479
            // or_b
2480
23
            if (in[0].first == OP_BOOLOR) {
2481
23
                ++in;
2482
23
                to_parse.emplace_back(DecodeContext::OR_B, -1, -1);
2483
23
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2484
23
                to_parse.emplace_back(DecodeContext::W_EXPR, -1, -1);
2485
23
                break;
2486
23
            }
2487
            // Unrecognised expression
2488
0
            return {};
2489
23
        }
2490
5.48k
        case DecodeContext::BKV_EXPR: {
2491
5.48k
            to_parse.emplace_back(DecodeContext::MAYBE_AND_V, -1, -1);
2492
5.48k
            to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2493
5.48k
            break;
2494
23
        }
2495
47
        case DecodeContext::W_EXPR: {
2496
            // a: wrapper
2497
47
            if (in >= last) return {};
2498
47
            if (in[0].first == OP_FROMALTSTACK) {
2499
16
                ++in;
2500
16
                to_parse.emplace_back(DecodeContext::ALT, -1, -1);
2501
31
            } else {
2502
31
                to_parse.emplace_back(DecodeContext::SWAP, -1, -1);
2503
31
            }
2504
47
            to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2505
47
            break;
2506
47
        }
2507
5.48k
        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.48k
            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
989
                to_parse.emplace_back(DecodeContext::AND_V, -1, -1);
2512
                // BKV_EXPR can contain more AND_V nodes
2513
989
                to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2514
989
            }
2515
5.48k
            break;
2516
47
        }
2517
31
        case DecodeContext::SWAP: {
2518
31
            if (in >= last || in[0].first != OP_SWAP || constructed.empty()) return {};
2519
31
            ++in;
2520
31
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_S, Vector(std::move(constructed.back()))};
2521
31
            break;
2522
31
        }
2523
16
        case DecodeContext::ALT: {
2524
16
            if (in >= last || in[0].first != OP_TOALTSTACK || constructed.empty()) return {};
2525
16
            ++in;
2526
16
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_A, Vector(std::move(constructed.back()))};
2527
16
            break;
2528
16
        }
2529
3.89k
        case DecodeContext::CHECK: {
2530
3.89k
            if (constructed.empty()) return {};
2531
3.89k
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_C, Vector(std::move(constructed.back()))};
2532
3.89k
            break;
2533
3.89k
        }
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
995
        case DecodeContext::VERIFY: {
2540
995
            if (constructed.empty()) return {};
2541
995
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_V, Vector(std::move(constructed.back()))};
2542
995
            break;
2543
995
        }
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
5.60M
        case DecodeContext::ZERO_NOTEQUAL: {
2550
5.60M
            if (constructed.empty()) return {};
2551
5.60M
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_N, Vector(std::move(constructed.back()))};
2552
5.60M
            break;
2553
5.60M
        }
2554
989
        case DecodeContext::AND_V: {
2555
989
            if (constructed.size() < 2) return {};
2556
989
            BuildBack(ctx.MsContext(), Fragment::AND_V, constructed, /*reverse=*/true);
2557
989
            break;
2558
989
        }
2559
8
        case DecodeContext::AND_B: {
2560
8
            if (constructed.size() < 2) return {};
2561
8
            BuildBack(ctx.MsContext(), Fragment::AND_B, constructed, /*reverse=*/true);
2562
8
            break;
2563
8
        }
2564
23
        case DecodeContext::OR_B: {
2565
23
            if (constructed.size() < 2) return {};
2566
23
            BuildBack(ctx.MsContext(), Fragment::OR_B, constructed, /*reverse=*/true);
2567
23
            break;
2568
23
        }
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
24
        case DecodeContext::THRESH_W: {
2590
24
            if (in >= last) return {};
2591
24
            if (in[0].first == OP_ADD) {
2592
16
                ++in;
2593
16
                to_parse.emplace_back(DecodeContext::THRESH_W, n+1, k);
2594
16
                to_parse.emplace_back(DecodeContext::W_EXPR, -1, -1);
2595
16
            } else {
2596
8
                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
8
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2599
8
            }
2600
24
            break;
2601
24
        }
2602
8
        case DecodeContext::THRESH_E: {
2603
8
            if (k < 1 || k > n || constructed.size() < static_cast<size_t>(n)) return {};
2604
8
            std::vector<Node<Key>> subs;
2605
32
            for (int i = 0; i < n; ++i) {
2606
24
                Node sub{std::move(constructed.back())};
2607
24
                constructed.pop_back();
2608
24
                subs.push_back(std::move(sub));
2609
24
            }
2610
8
            constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::THRESH, std::move(subs), k);
2611
8
            break;
2612
8
        }
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
11.2M
        }
2672
11.2M
    }
2673
4.44k
    if (constructed.size() != 1) return {};
2674
4.44k
    Node tl_node{std::move(constructed.front())};
2675
4.44k
    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.44k
    if (!tl_node.IsValidTopLevel()) return {};
2679
4.44k
    return tl_node;
2680
4.44k
}
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
271
{
2298
    // The two integers are used to hold state for thresh()
2299
271
    std::vector<std::tuple<DecodeContext, int64_t, int64_t>> to_parse;
2300
271
    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
271
    to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2305
2306
9.86k
    while (!to_parse.empty()) {
2307
        // Exit early if the Miniscript is not going to be valid.
2308
9.59k
        if (!constructed.empty() && !constructed.back().IsValid()) return {};
2309
2310
        // Get the current context we are decoding within
2311
9.59k
        auto [cur_context, n, k] = to_parse.back();
2312
9.59k
        to_parse.pop_back();
2313
2314
9.59k
        switch(cur_context) {
2315
2.83k
        case DecodeContext::SINGLE_BKV_EXPR: {
2316
2.83k
            if (in >= last) return {};
2317
2318
            // Constants
2319
2.83k
            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.83k
            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.58k
            if (in[0].second.size() == 33 || in[0].second.size() == 32) {
2331
574
                auto key = ctx.FromPKBytes(in[0].second.begin(), in[0].second.end());
2332
574
                if (!key) return {};
2333
573
                ++in;
2334
573
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_K, Vector(std::move(*key)));
2335
573
                break;
2336
574
            }
2337
2.01k
            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
61
                auto key = ctx.FromPKHBytes(in[2].second.begin(), in[2].second.end());
2339
61
                if (!key) return {};
2340
60
                in += 5;
2341
60
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::PK_H, Vector(std::move(*key)));
2342
60
                break;
2343
61
            }
2344
            // Time locks
2345
1.95k
            std::optional<int64_t> num;
2346
1.95k
            if (last - in >= 2 && in[0].first == OP_CHECKSEQUENCEVERIFY && (num = ParseScriptNumber(in[1]))) {
2347
75
                in += 2;
2348
75
                if (*num < 1 || *num > 0x7FFFFFFFL) return {};
2349
75
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::OLDER, *num);
2350
75
                break;
2351
75
            }
2352
1.87k
            if (last - in >= 2 && in[0].first == OP_CHECKLOCKTIMEVERIFY && (num = ParseScriptNumber(in[1]))) {
2353
253
                in += 2;
2354
253
                if (num < 1 || num > 0x7FFFFFFFL) return {};
2355
253
                constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::AFTER, *num);
2356
253
                break;
2357
253
            }
2358
            // Hashes
2359
1.62k
            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
61
                if (in[2].first == OP_SHA256 && in[1].second.size() == 32) {
2361
25
                    constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::SHA256, in[1].second);
2362
25
                    in += 7;
2363
25
                    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
61
            }
2378
            // Multi
2379
1.56k
            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.54k
            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.54k
            if (in[0].first == OP_CHECKSIG) {
2433
618
                ++in;
2434
618
                to_parse.emplace_back(DecodeContext::CHECK, -1, -1);
2435
618
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2436
618
                break;
2437
618
            }
2438
            // v: wrapper
2439
922
            if (in[0].first == OP_VERIFY) {
2440
189
                ++in;
2441
189
                to_parse.emplace_back(DecodeContext::VERIFY, -1, -1);
2442
189
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2443
189
                break;
2444
189
            }
2445
            // n: wrapper
2446
733
            if (in[0].first == OP_0NOTEQUAL) {
2447
274
                ++in;
2448
274
                to_parse.emplace_back(DecodeContext::ZERO_NOTEQUAL, -1, -1);
2449
274
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2450
274
                break;
2451
274
            }
2452
            // Thresh
2453
459
            if (last - in >= 3 && in[0].first == OP_EQUAL && (num = ParseScriptNumber(in[1]))) {
2454
105
                if (*num < 1) return {};
2455
105
                in += 2;
2456
105
                to_parse.emplace_back(DecodeContext::THRESH_W, 0, *num);
2457
105
                break;
2458
105
            }
2459
            // OP_ENDIF can be WRAP_J, WRAP_D, ANDOR, OR_C, OR_D, or OR_I
2460
354
            if (in[0].first == OP_ENDIF) {
2461
345
                ++in;
2462
345
                to_parse.emplace_back(DecodeContext::ENDIF, -1, -1);
2463
345
                to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2464
345
                break;
2465
345
            }
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
9
            if (in[0].first == OP_BOOLAND) {
2473
8
                ++in;
2474
8
                to_parse.emplace_back(DecodeContext::AND_B, -1, -1);
2475
8
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2476
8
                to_parse.emplace_back(DecodeContext::W_EXPR, -1, -1);
2477
8
                break;
2478
8
            }
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.55k
        case DecodeContext::BKV_EXPR: {
2491
1.55k
            to_parse.emplace_back(DecodeContext::MAYBE_AND_V, -1, -1);
2492
1.55k
            to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2493
1.55k
            break;
2494
1
        }
2495
480
        case DecodeContext::W_EXPR: {
2496
            // a: wrapper
2497
480
            if (in >= last) return {};
2498
480
            if (in[0].first == OP_FROMALTSTACK) {
2499
40
                ++in;
2500
40
                to_parse.emplace_back(DecodeContext::ALT, -1, -1);
2501
440
            } else {
2502
440
                to_parse.emplace_back(DecodeContext::SWAP, -1, -1);
2503
440
            }
2504
480
            to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2505
480
            break;
2506
480
        }
2507
1.55k
        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.55k
            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
158
                to_parse.emplace_back(DecodeContext::AND_V, -1, -1);
2512
                // BKV_EXPR can contain more AND_V nodes
2513
158
                to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2514
158
            }
2515
1.55k
            break;
2516
480
        }
2517
440
        case DecodeContext::SWAP: {
2518
440
            if (in >= last || in[0].first != OP_SWAP || constructed.empty()) return {};
2519
440
            ++in;
2520
440
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_S, Vector(std::move(constructed.back()))};
2521
440
            break;
2522
440
        }
2523
40
        case DecodeContext::ALT: {
2524
40
            if (in >= last || in[0].first != OP_TOALTSTACK || constructed.empty()) return {};
2525
40
            ++in;
2526
40
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_A, Vector(std::move(constructed.back()))};
2527
40
            break;
2528
40
        }
2529
617
        case DecodeContext::CHECK: {
2530
617
            if (constructed.empty()) return {};
2531
617
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_C, Vector(std::move(constructed.back()))};
2532
617
            break;
2533
617
        }
2534
31
        case DecodeContext::DUP_IF: {
2535
31
            if (constructed.empty()) return {};
2536
31
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_D, Vector(std::move(constructed.back()))};
2537
31
            break;
2538
31
        }
2539
189
        case DecodeContext::VERIFY: {
2540
189
            if (constructed.empty()) return {};
2541
189
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_V, Vector(std::move(constructed.back()))};
2542
189
            break;
2543
189
        }
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
274
        case DecodeContext::ZERO_NOTEQUAL: {
2550
274
            if (constructed.empty()) return {};
2551
274
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::WRAP_N, Vector(std::move(constructed.back()))};
2552
274
            break;
2553
274
        }
2554
158
        case DecodeContext::AND_V: {
2555
158
            if (constructed.size() < 2) return {};
2556
158
            BuildBack(ctx.MsContext(), Fragment::AND_V, constructed, /*reverse=*/true);
2557
158
            break;
2558
158
        }
2559
8
        case DecodeContext::AND_B: {
2560
8
            if (constructed.size() < 2) return {};
2561
8
            BuildBack(ctx.MsContext(), Fragment::AND_B, constructed, /*reverse=*/true);
2562
8
            break;
2563
8
        }
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
14
        case DecodeContext::OR_D: {
2575
14
            if (constructed.size() < 2) return {};
2576
14
            BuildBack(ctx.MsContext(), Fragment::OR_D, constructed, /*reverse=*/true);
2577
14
            break;
2578
14
        }
2579
69
        case DecodeContext::ANDOR: {
2580
69
            if (constructed.size() < 3) return {};
2581
69
            Node left{std::move(constructed.back())};
2582
69
            constructed.pop_back();
2583
69
            Node right{std::move(constructed.back())};
2584
69
            constructed.pop_back();
2585
69
            Node mid{std::move(constructed.back())};
2586
69
            constructed.back() = Node{internal::NoDupCheck{}, ctx.MsContext(), Fragment::ANDOR, Vector(std::move(left), std::move(mid), std::move(right))};
2587
69
            break;
2588
69
        }
2589
577
        case DecodeContext::THRESH_W: {
2590
577
            if (in >= last) return {};
2591
577
            if (in[0].first == OP_ADD) {
2592
472
                ++in;
2593
472
                to_parse.emplace_back(DecodeContext::THRESH_W, n+1, k);
2594
472
                to_parse.emplace_back(DecodeContext::W_EXPR, -1, -1);
2595
472
            } else {
2596
105
                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
105
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2599
105
            }
2600
577
            break;
2601
577
        }
2602
105
        case DecodeContext::THRESH_E: {
2603
105
            if (k < 1 || k > n || constructed.size() < static_cast<size_t>(n)) return {};
2604
105
            std::vector<Node<Key>> subs;
2605
682
            for (int i = 0; i < n; ++i) {
2606
577
                Node sub{std::move(constructed.back())};
2607
577
                constructed.pop_back();
2608
577
                subs.push_back(std::move(sub));
2609
577
            }
2610
105
            constructed.emplace_back(internal::NoDupCheck{}, ctx.MsContext(), Fragment::THRESH, std::move(subs), k);
2611
105
            break;
2612
105
        }
2613
345
        case DecodeContext::ENDIF: {
2614
345
            if (in >= last) return {};
2615
2616
            // could be andor or or_i
2617
345
            if (in[0].first == OP_ELSE) {
2618
300
                ++in;
2619
300
                to_parse.emplace_back(DecodeContext::ENDIF_ELSE, -1, -1);
2620
300
                to_parse.emplace_back(DecodeContext::BKV_EXPR, -1, -1);
2621
300
            }
2622
            // could be j: or d: wrapper
2623
45
            else if (in[0].first == OP_IF) {
2624
31
                if (last - in >= 2 && in[1].first == OP_DUP) {
2625
31
                    in += 2;
2626
31
                    to_parse.emplace_back(DecodeContext::DUP_IF, -1, -1);
2627
31
                } 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
31
            } else if (in[0].first == OP_NOTIF) {
2636
14
                ++in;
2637
14
                to_parse.emplace_back(DecodeContext::ENDIF_NOTIF, -1, -1);
2638
14
            }
2639
0
            else {
2640
0
                return {};
2641
0
            }
2642
345
            break;
2643
345
        }
2644
345
        case DecodeContext::ENDIF_NOTIF: {
2645
14
            if (in >= last) return {};
2646
14
            if (in[0].first == OP_IFDUP) {
2647
14
                ++in;
2648
14
                to_parse.emplace_back(DecodeContext::OR_D, -1, -1);
2649
14
            } 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
14
            to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2654
14
            break;
2655
14
        }
2656
300
        case DecodeContext::ENDIF_ELSE: {
2657
300
            if (in >= last) return {};
2658
300
            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
69
                ++in;
2663
69
                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
69
                to_parse.emplace_back(DecodeContext::SINGLE_BKV_EXPR, -1, -1);
2666
69
            } else {
2667
0
                return {};
2668
0
            }
2669
300
            break;
2670
300
        }
2671
9.59k
        }
2672
9.59k
    }
2673
268
    if (constructed.size() != 1) return {};
2674
268
    Node tl_node{std::move(constructed.front())};
2675
268
    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
268
    if (!tl_node.IsValidTopLevel()) return {};
2679
268
    return tl_node;
2680
268
}
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
794
{
2687
794
    return internal::Parse<typename Ctx::Key>(str, ctx);
2688
794
}
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
574
{
2687
574
    return internal::Parse<typename Ctx::Key>(str, ctx);
2688
574
}
2689
2690
template <typename Ctx>
2691
inline std::optional<Node<typename Ctx::Key>> FromScript(const CScript& script, const Ctx& ctx)
2692
5.52k
{
2693
5.52k
    using namespace internal;
2694
    // A too large Script is necessarily invalid, don't bother parsing it.
2695
5.52k
    if (script.size() > MaxScriptSize(ctx.MsContext())) return {};
2696
5.52k
    auto decomposed = DecomposeScript(script);
2697
5.52k
    if (!decomposed) return {};
2698
5.52k
    auto it = decomposed->begin();
2699
5.52k
    auto ret = DecodeScript<typename Ctx::Key>(it, decomposed->end(), ctx);
2700
5.52k
    if (!ret) return {};
2701
5.50k
    if (it != decomposed->end()) return {};
2702
5.50k
    return ret;
2703
5.50k
}
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
680
{
2693
680
    using namespace internal;
2694
    // A too large Script is necessarily invalid, don't bother parsing it.
2695
680
    if (script.size() > MaxScriptSize(ctx.MsContext())) return {};
2696
680
    auto decomposed = DecomposeScript(script);
2697
680
    if (!decomposed) return {};
2698
680
    auto it = decomposed->begin();
2699
680
    auto ret = DecodeScript<typename Ctx::Key>(it, decomposed->end(), ctx);
2700
680
    if (!ret) return {};
2701
667
    if (it != decomposed->end()) return {};
2702
667
    return ret;
2703
667
}
std::optional<miniscript::Node<TapSatisfier::Key>> miniscript::FromScript<TapSatisfier>(CScript const&, TapSatisfier const&)
Line
Count
Source
2692
4.44k
{
2693
4.44k
    using namespace internal;
2694
    // A too large Script is necessarily invalid, don't bother parsing it.
2695
4.44k
    if (script.size() > MaxScriptSize(ctx.MsContext())) return {};
2696
4.44k
    auto decomposed = DecomposeScript(script);
2697
4.44k
    if (!decomposed) return {};
2698
4.44k
    auto it = decomposed->begin();
2699
4.44k
    auto ret = DecodeScript<typename Ctx::Key>(it, decomposed->end(), ctx);
2700
4.44k
    if (!ret) return {};
2701
4.44k
    if (it != decomposed->end()) return {};
2702
4.44k
    return ret;
2703
4.44k
}
std::optional<miniscript::Node<WshSatisfier::Key>> miniscript::FromScript<WshSatisfier>(CScript const&, WshSatisfier const&)
Line
Count
Source
2692
271
{
2693
271
    using namespace internal;
2694
    // A too large Script is necessarily invalid, don't bother parsing it.
2695
271
    if (script.size() > MaxScriptSize(ctx.MsContext())) return {};
2696
271
    auto decomposed = DecomposeScript(script);
2697
271
    if (!decomposed) return {};
2698
271
    auto it = decomposed->begin();
2699
271
    auto ret = DecodeScript<typename Ctx::Key>(it, decomposed->end(), ctx);
2700
271
    if (!ret) return {};
2701
268
    if (it != decomposed->end()) return {};
2702
268
    return ret;
2703
268
}
2704
2705
} // namespace miniscript
2706
2707
#endif // BITCOIN_SCRIPT_MINISCRIPT_H