Bitcoin Core  27.99.0
P2P Digital Currency
Classes | Enumerations | Functions | Variables
miniscript::internal Namespace Reference

Classes

struct  InputStack
 An object representing a sequence of witness stack elements. More...
 
struct  InputResult
 A pair of a satisfaction and a dissatisfaction InputStack. More...
 
struct  MaxInt
 Class whose objects represent the maximum of a list of integers. More...
 
struct  Ops
 
struct  SatInfo
 A data structure to help the calculation of stack size limits. More...
 
struct  StackSize
 
struct  WitnessSize
 
struct  NoDupCheck
 

Enumerations

enum class  ParseContext {
  WRAPPED_EXPR , EXPR , SWAP , ALT ,
  CHECK , DUP_IF , VERIFY , NON_ZERO ,
  ZERO_NOTEQUAL , WRAP_U , WRAP_T , AND_N ,
  AND_V , AND_B , ANDOR , OR_B ,
  OR_C , OR_D , OR_I , THRESH ,
  COMMA , CLOSE_BRACKET
}
 
enum class  DecodeContext {
  SINGLE_BKV_EXPR , BKV_EXPR , W_EXPR , SWAP ,
  ALT , CHECK , DUP_IF , VERIFY ,
  NON_ZERO , ZERO_NOTEQUAL , MAYBE_AND_V , AND_V ,
  AND_B , ANDOR , OR_B , OR_C ,
  OR_D , THRESH_W , THRESH_E , ENDIF ,
  ENDIF_NOTIF , ENDIF_ELSE
}
 

Functions

Type SanitizeType (Type x)
 A helper sanitizer/checker for the output of CalcType. More...
 
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)
 Helper function for Node::CalcType. More...
 
size_t ComputeScriptLen (Fragment fragment, Type sub0typ, size_t subsize, uint32_t k, size_t n_subs, size_t n_keys, MiniscriptContext ms_ctx)
 Helper function for Node::CalcScriptLen. More...
 
InputStack operator+ (InputStack a, InputStack b)
 
InputStack operator| (InputStack a, InputStack b)
 
std::optional< std::vector< Opcode > > DecomposeScript (const CScript &script)
 Decode a script into opcode/push pairs. More...
 
std::optional< int64_t > ParseScriptNumber (const Opcode &in)
 Determine whether the passed pair (created by DecomposeScript) is pushing a number. More...
 
int FindNextChar (Span< const char > sp, const char m)
 
constexpr uint32_t MaxScriptSize (MiniscriptContext ms_ctx)
 The maximum size of a script depending on the context. More...
 
template<typename Key , typename Ctx >
std::optional< std::pair< Key, int > > ParseKeyEnd (Span< const char > in, const Ctx &ctx)
 Parse a key string ending at the end of the fragment's text representation. More...
 
template<typename Ctx >
std::optional< std::pair< std::vector< unsigned char >, int > > ParseHexStrEnd (Span< const char > in, const size_t expected_size, const Ctx &ctx)
 Parse a hex string ending at the end of the fragment's text representation. More...
 
template<typename Key >
void BuildBack (const MiniscriptContext script_ctx, Fragment nt, std::vector< NodeRef< Key >> &constructed, const bool reverse=false)
 BuildBack pops the last two elements off constructed and wraps them in the specified Fragment. More...
 
template<typename Key , typename Ctx >
NodeRef< Key > Parse (Span< const char > in, const Ctx &ctx)
 Parse a miniscript from its textual descriptor form. More...
 
template<typename Key , typename Ctx , typename I >
NodeRef< Key > DecodeScript (I &in, I last, const Ctx &ctx)
 Parse a miniscript from a bitcoin script. More...
 

Variables

static constexpr uint32_t MAX_TAPMINISCRIPT_STACK_ELEM_SIZE {65}
 The maximum size of a witness item for a Miniscript under Tapscript context. (A BIP340 signature with a sighash type byte.) More...
 
constexpr uint32_t TX_OVERHEAD {4 + 4}
 nVersion + nLockTime More...
 
constexpr uint32_t TXIN_BYTES_NO_WITNESS {36 + 4 + 1}
 prevout + nSequence + scriptSig More...
 
constexpr uint32_t P2WSH_TXOUT_BYTES {8 + 1 + 1 + 33}
 nValue + script len + OP_0 + pushdata 32. More...
 
constexpr uint32_t TX_BODY_LEEWAY_WEIGHT {(TX_OVERHEAD + GetSizeOfCompactSize(1) + TXIN_BYTES_NO_WITNESS + GetSizeOfCompactSize(1) + P2WSH_TXOUT_BYTES) * WITNESS_SCALE_FACTOR + 2}
 Data other than the witness in a transaction. Overhead + vin count + one vin + vout count + one vout + segwit marker. More...
 
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}
 Maximum possible stack size to spend a Taproot output (excluding the script itself). More...
 
static const auto ZERO = InputStack(std::vector<unsigned char>())
 A stack consisting of a single zero-length element (interpreted as 0 by the script interpreter in numeric context). More...
 
static const auto ZERO32 = InputStack(std::vector<unsigned char>(32, 0)).SetMalleable()
 A stack consisting of a single malleable 32-byte 0x0000...0000 element (for dissatisfying hash challenges). More...
 
static const auto ONE = InputStack(Vector((unsigned char)1))
 A stack consisting of a single 0x01 element (interpreted as 1 by the script interpreted in numeric context). More...
 
static const auto EMPTY = InputStack()
 The empty stack. More...
 
static const auto INVALID = InputStack().SetAvailable(Availability::NO)
 A stack representing the lack of any (dis)satisfactions. More...
 

Enumeration Type Documentation

◆ DecodeContext

Enumerator
SINGLE_BKV_EXPR 

A single expression of type B, K, or V.

Specifically, this can't be an and_v or an expression of type W (a: and s: wrappers).

BKV_EXPR 

Potentially multiple SINGLE_BKV_EXPRs as children of (potentially multiple) and_v expressions.

Syntactic sugar for MAYBE_AND_V + SINGLE_BKV_EXPR.

W_EXPR 

An expression of type W (a: or s: wrappers).

SWAP 

SWAP expects the next element to be OP_SWAP (inside a W-type expression that didn't end with FROMALTSTACK), and wraps the top of the constructed stack with s:

ALT 

ALT expects the next element to be TOALTSTACK (we must have already read a FROMALTSTACK earlier), and wraps the top of the constructed stack with a:

CHECK 

CHECK wraps the top constructed node with c:

DUP_IF 

DUP_IF wraps the top constructed node with d:

VERIFY 

VERIFY wraps the top constructed node with v:

NON_ZERO 

NON_ZERO wraps the top constructed node with j:

ZERO_NOTEQUAL 

ZERO_NOTEQUAL wraps the top constructed node with n:

MAYBE_AND_V 

MAYBE_AND_V will check if the next part of the script could be a valid miniscript sub-expression, and if so it will push AND_V and SINGLE_BKV_EXPR to decode it and construct the and_v node.

This is recursive, to deal with multiple and_v nodes inside each other.

AND_V 

AND_V will construct an and_v node from the last two constructed nodes.

AND_B 

AND_B will construct an and_b node from the last two constructed nodes.

ANDOR 

ANDOR will construct an andor node from the last three constructed nodes.

OR_B 

OR_B will construct an or_b node from the last two constructed nodes.

OR_C 

OR_C will construct an or_c node from the last two constructed nodes.

OR_D 

OR_D will construct an or_d node from the last two constructed nodes.

THRESH_W 

In a thresh expression, all sub-expressions other than the first are W-type, and end in OP_ADD.

THRESH_W will check for this OP_ADD and either push a W_EXPR or a SINGLE_BKV_EXPR and jump to THRESH_E accordingly.

THRESH_E 

THRESH_E constructs a thresh node from the appropriate number of constructed children.

ENDIF 

ENDIF signals that we are inside some sort of OP_IF structure, which could be or_d, or_c, or_i, andor, d:, or j: wrapper, depending on what follows.

We read a BKV_EXPR and then deal with the next opcode case-by-case.

ENDIF_NOTIF 

If, inside an ENDIF context, we find an OP_NOTIF before finding an OP_ELSE, we could either be in an or_d or an or_c node.

We then check for IFDUP to distinguish these cases.

ENDIF_ELSE 

If, inside an ENDIF context, we find an OP_ELSE, then we could be in either an or_i or an andor node.

Read the next BKV_EXPR and find either an OP_IF or an OP_NOTIF.

Definition at line 2152 of file miniscript.h.

◆ ParseContext

Enumerator
WRAPPED_EXPR 

An expression which may be begin with wrappers followed by a colon.

EXPR 

A miniscript expression which does not begin with wrappers.

SWAP 

SWAP wraps the top constructed node with s:

ALT 

ALT wraps the top constructed node with a:

CHECK 

CHECK wraps the top constructed node with c:

DUP_IF 

DUP_IF wraps the top constructed node with d:

VERIFY 

VERIFY wraps the top constructed node with v:

NON_ZERO 

NON_ZERO wraps the top constructed node with j:

ZERO_NOTEQUAL 

ZERO_NOTEQUAL wraps the top constructed node with n:

WRAP_U 

WRAP_U will construct an or_i(X,0) node from the top constructed node.

WRAP_T 

WRAP_T will construct an and_v(X,1) node from the top constructed node.

AND_N 

AND_N will construct an andor(X,Y,0) node from the last two constructed nodes.

AND_V 

AND_V will construct an and_v node from the last two constructed nodes.

AND_B 

AND_B will construct an and_b node from the last two constructed nodes.

ANDOR 

ANDOR will construct an andor node from the last three constructed nodes.

OR_B 

OR_B will construct an or_b node from the last two constructed nodes.

OR_C 

OR_C will construct an or_c node from the last two constructed nodes.

OR_D 

OR_D will construct an or_d node from the last two constructed nodes.

OR_I 

OR_I will construct an or_i node from the last two constructed nodes.

THRESH 

THRESH will read a wrapped expression, and then look for a COMMA.

If no comma follows, it will construct a thresh node from the appropriate number of constructed children. Otherwise, it will recurse with another THRESH.

COMMA 

COMMA expects the next element to be ',' and fails if not.

CLOSE_BRACKET 

CLOSE_BRACKET expects the next element to be ')' and fails if not.

Definition at line 1663 of file miniscript.h.

Function Documentation

◆ BuildBack()

template<typename Key >
void miniscript::internal::BuildBack ( const MiniscriptContext  script_ctx,
Fragment  nt,
std::vector< NodeRef< Key >> &  constructed,
const bool  reverse = false 
)

BuildBack pops the last two elements off constructed and wraps them in the specified Fragment.

Definition at line 1746 of file miniscript.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ComputeScriptLen()

size_t miniscript::internal::ComputeScriptLen ( Fragment  fragment,
Type  sub0typ,
size_t  subsize,
uint32_t  k,
size_t  n_subs,
size_t  n_keys,
MiniscriptContext  ms_ctx 
)

Helper function for Node::CalcScriptLen.

Definition at line 261 of file miniscript.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ComputeType()

Type miniscript::internal::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 
)

Helper function for Node::CalcType.

Definition at line 36 of file miniscript.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ DecodeScript()

template<typename Key , typename Ctx , typename I >
NodeRef<Key> miniscript::internal::DecodeScript ( I &  in,
last,
const Ctx &  ctx 
)
inline

Parse a miniscript from a bitcoin script.

In the following wrappers, we only need to push SINGLE_BKV_EXPR rather than BKV_EXPR, because and_v commutes with these wrappers. For example, c:and_v(X,Y) produces the same script as and_v(X,c:Y).

In and_b and or_b nodes, we only look for SINGLE_BKV_EXPR, because or_b(and_v(X,Y),Z) has script [X] [Y] [Z] OP_BOOLOR, the same as and_v(X,or_b(Y,Z)). In this example, the former of these is invalid as miniscript, while the latter is valid. So we leave the and_v "outside" while decoding.

Definition at line 2222 of file miniscript.h.

Here is the call graph for this function:

◆ DecomposeScript()

std::optional< std::vector< Opcode > > miniscript::internal::DecomposeScript ( const CScript script)

Decode a script into opcode/push pairs.

Construct a vector with one element per opcode in the script, in reverse order. Each element is a pair consisting of the opcode, as well as the data pushed by the opcode (including OP_n), if any. OP_CHECKSIGVERIFY, OP_CHECKMULTISIGVERIFY, OP_NUMEQUALVERIFY and OP_EQUALVERIFY are decomposed into OP_CHECKSIG, OP_CHECKMULTISIG, OP_EQUAL and OP_NUMEQUAL respectively, plus OP_VERIFY.

Definition at line 365 of file miniscript.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ FindNextChar()

int miniscript::internal::FindNextChar ( Span< const char >  sp,
const char  m 
)

Definition at line 418 of file miniscript.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ MaxScriptSize()

constexpr uint32_t miniscript::internal::MaxScriptSize ( MiniscriptContext  ms_ctx)
constexpr

The maximum size of a script depending on the context.

Definition at line 265 of file miniscript.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator+()

InputStack miniscript::internal::operator+ ( InputStack  a,
InputStack  b 
)

Definition at line 322 of file miniscript.cpp.

◆ operator|()

InputStack miniscript::internal::operator| ( InputStack  a,
InputStack  b 
)

Definition at line 336 of file miniscript.cpp.

◆ Parse()

template<typename Key , typename Ctx >
NodeRef<Key> miniscript::internal::Parse ( Span< const char >  in,
const Ctx &  ctx 
)
inline

Parse a miniscript from its textual descriptor form.

This does not check whether the script is valid, let alone sane. The caller is expected to use the IsValidTopLevel() and IsSaneTopLevel() to check for these properties on the node.

Definition at line 1763 of file miniscript.h.

Here is the call graph for this function:

◆ ParseHexStrEnd()

template<typename Ctx >
std::optional<std::pair<std::vector<unsigned char>, int> > miniscript::internal::ParseHexStrEnd ( Span< const char >  in,
const size_t  expected_size,
const Ctx &  ctx 
)

Parse a hex string ending at the end of the fragment's text representation.

Definition at line 1732 of file miniscript.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseKeyEnd()

template<typename Key , typename Ctx >
std::optional<std::pair<Key, int> > miniscript::internal::ParseKeyEnd ( Span< const char >  in,
const Ctx &  ctx 
)

Parse a key string ending at the end of the fragment's text representation.

Definition at line 1721 of file miniscript.h.

Here is the call graph for this function:

◆ ParseScriptNumber()

std::optional< int64_t > miniscript::internal::ParseScriptNumber ( const Opcode in)

Determine whether the passed pair (created by DecomposeScript) is pushing a number.

Definition at line 405 of file miniscript.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SanitizeType()

Type miniscript::internal::SanitizeType ( Type  e)

A helper sanitizer/checker for the output of CalcType.

Definition at line 16 of file miniscript.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ EMPTY

const auto miniscript::internal::EMPTY = InputStack()
static

The empty stack.

Definition at line 332 of file miniscript.h.

◆ INVALID

const auto miniscript::internal::INVALID = InputStack().SetAvailable(Availability::NO)
static

A stack representing the lack of any (dis)satisfactions.

Definition at line 334 of file miniscript.h.

◆ MAX_TAPMINISCRIPT_STACK_ELEM_SIZE

constexpr uint32_t miniscript::internal::MAX_TAPMINISCRIPT_STACK_ELEM_SIZE {65}
staticconstexpr

The maximum size of a witness item for a Miniscript under Tapscript context. (A BIP340 signature with a sighash type byte.)

Definition at line 252 of file miniscript.h.

◆ MAX_TAPSCRIPT_SAT_SIZE

Maximum possible stack size to spend a Taproot output (excluding the script itself).

Definition at line 263 of file miniscript.h.

◆ ONE

const auto miniscript::internal::ONE = InputStack(Vector((unsigned char)1))
static

A stack consisting of a single 0x01 element (interpreted as 1 by the script interpreted in numeric context).

Definition at line 330 of file miniscript.h.

◆ P2WSH_TXOUT_BYTES

constexpr uint32_t miniscript::internal::P2WSH_TXOUT_BYTES {8 + 1 + 1 + 33}
constexpr

nValue + script len + OP_0 + pushdata 32.

Definition at line 259 of file miniscript.h.

◆ TX_BODY_LEEWAY_WEIGHT

constexpr uint32_t miniscript::internal::TX_BODY_LEEWAY_WEIGHT {(TX_OVERHEAD + GetSizeOfCompactSize(1) + TXIN_BYTES_NO_WITNESS + GetSizeOfCompactSize(1) + P2WSH_TXOUT_BYTES) * WITNESS_SCALE_FACTOR + 2}
constexpr

Data other than the witness in a transaction. Overhead + vin count + one vin + vout count + one vout + segwit marker.

Definition at line 261 of file miniscript.h.

◆ TX_OVERHEAD

constexpr uint32_t miniscript::internal::TX_OVERHEAD {4 + 4}
constexpr

nVersion + nLockTime

Definition at line 255 of file miniscript.h.

◆ TXIN_BYTES_NO_WITNESS

constexpr uint32_t miniscript::internal::TXIN_BYTES_NO_WITNESS {36 + 4 + 1}
constexpr

prevout + nSequence + scriptSig

Definition at line 257 of file miniscript.h.

◆ ZERO

const auto miniscript::internal::ZERO = InputStack(std::vector<unsigned char>())
static

A stack consisting of a single zero-length element (interpreted as 0 by the script interpreter in numeric context).

Definition at line 326 of file miniscript.h.

◆ ZERO32

const auto miniscript::internal::ZERO32 = InputStack(std::vector<unsigned char>(32, 0)).SetMalleable()
static

A stack consisting of a single malleable 32-byte 0x0000...0000 element (for dissatisfying hash challenges).

Definition at line 328 of file miniscript.h.