28using miniscript::operator
""_mst;
35 std::vector<Key> dummy_keys;
36 std::map<Key, int> dummy_key_idx_map;
37 std::map<CKeyID, Key> dummy_keys_map;
38 std::map<Key, std::pair<std::vector<unsigned char>,
bool>> dummy_sigs;
39 std::map<XOnlyPubKey, std::pair<std::vector<unsigned char>,
bool>> schnorr_sigs;
42 std::vector<std::vector<unsigned char>>
sha256;
43 std::vector<std::vector<unsigned char>>
ripemd160;
44 std::vector<std::vector<unsigned char>> hash256;
45 std::vector<std::vector<unsigned char>> hash160;
46 std::map<std::vector<unsigned char>, std::vector<unsigned char>> sha256_preimages;
47 std::map<std::vector<unsigned char>, std::vector<unsigned char>> ripemd160_preimages;
48 std::map<std::vector<unsigned char>, std::vector<unsigned char>> hash256_preimages;
49 std::map<std::vector<unsigned char>, std::vector<unsigned char>> hash160_preimages;
53 unsigned char keydata[32] = {1};
55 constexpr uint256 MESSAGE_HASH{
"0000000000000000f5cd94e18b6fe77dd7aca9e35c2b0c9cbd86356c80a71065"};
59 for (
size_t i = 0; i < 256; i++) {
63 const Key pubkey =
privkey.GetPubKey();
65 dummy_keys.push_back(pubkey);
66 dummy_key_idx_map.emplace(pubkey, i);
67 dummy_keys_map.insert({pubkey.GetID(), pubkey});
76 dummy_sigs.insert({pubkey, {sig, i & 1}});
81 std::vector<unsigned char> hash;
85 if (i & 1) sha256_preimages[hash] = std::vector<unsigned char>(keydata, keydata + 32);
87 hash256.push_back(hash);
88 if (i & 1) hash256_preimages[hash] = std::vector<unsigned char>(keydata, keydata + 32);
93 if (i & 1) ripemd160_preimages[hash] = std::vector<unsigned char>(keydata, keydata + 32);
95 hash160.push_back(hash);
96 if (i & 1) hash160_preimages[hash] = std::vector<unsigned char>(keydata, keydata + 32);
101 const std::pair<std::vector<unsigned char>,
bool>*
GetSig(
const MsCtx script_ctx,
const Key& key)
const {
103 const auto it = dummy_sigs.find(key);
104 if (it == dummy_sigs.end())
return nullptr;
107 const auto it = schnorr_sigs.find(
XOnlyPubKey{key});
108 if (it == schnorr_sigs.end())
return nullptr;
119struct ParserContext {
122 const MsCtx script_ctx;
124 constexpr ParserContext(
MsCtx ctx) noexcept : script_ctx(ctx) {}
126 bool KeyCompare(
const Key&
a,
const Key& b)
const {
133 auto it =
TEST_DATA.dummy_key_idx_map.find(key);
134 if (it ==
TEST_DATA.dummy_key_idx_map.end()) {
139 return HexStr(std::span{&idx, 1});
142 std::vector<unsigned char> ToPKBytes(
const Key& key)
const {
144 return {key.begin(), key.end()};
150 std::vector<unsigned char>
ToPKHBytes(
const Key& key)
const {
153 return {h.begin(), h.end()};
156 return {h.
begin(), h.end()};
159 std::optional<Key>
FromString(std::span<const char>& in)
const {
160 if (in.size() != 2)
return {};
161 auto idx =
ParseHex(std::string(in.begin(), in.end()));
162 if (idx.size() != 1)
return {};
167 std::optional<Key> FromPKBytes(I
first, I
last)
const {
170 if (key.IsValid())
return key;
180 std::optional<Key> FromPKHBytes(I
first, I
last)
const {
185 if (it ==
TEST_DATA.dummy_keys_map.end())
return {};
189 MsCtx MsContext()
const {
195struct ScriptParserContext {
196 const MsCtx script_ctx;
198 constexpr ScriptParserContext(
MsCtx ctx) noexcept : script_ctx(ctx) {}
203 std::vector<unsigned char>
data;
206 bool KeyCompare(
const Key&
a,
const Key& b)
const {
207 return a.data < b.data;
210 const std::vector<unsigned char>& ToPKBytes(
const Key& key)
const
216 std::vector<unsigned char>
ToPKHBytes(
const Key& key)
const
218 if (key.is_hash)
return key.data;
219 const auto h =
Hash160(key.data);
220 return {h.begin(), h.end()};
224 std::optional<Key> FromPKBytes(I
first, I
last)
const
233 std::optional<Key> FromPKHBytes(I
first, I
last)
const
241 MsCtx MsContext()
const {
247struct SatisfierContext : ParserContext {
249 constexpr SatisfierContext(
MsCtx ctx) noexcept : ParserContext(ctx) {}
253 bool CheckAfter(
uint32_t value)
const {
return value % 2; }
254 bool CheckOlder(
uint32_t value)
const {
return value % 2; }
267 const std::map<std::vector<unsigned char>, std::vector<unsigned char>>&
map)
const
269 const auto it =
map.find(hash);
295 const auto it =
TEST_DATA.dummy_sigs.find(key);
296 if (it ==
TEST_DATA.dummy_sigs.end())
return false;
297 return it->second.first == sig;
302 auto it =
TEST_DATA.schnorr_sigs.find(pk);
303 if (it ==
TEST_DATA.schnorr_sigs.end())
return false;
304 return std::ranges::equal(it->second.first, sig);
311const struct KeyComparator {
327 std::vector<CPubKey>
keys;
329 std::vector<unsigned char> hash;
331 std::vector<Type> subtypes;
333 NodeInfo(Fragment
frag): fragment(
frag),
k(0) {}
336 NodeInfo(Fragment
frag, std::vector<unsigned char> h): fragment(
frag),
k(0), hash(
std::move(h)) {}
337 NodeInfo(std::vector<Type>
subt, Fragment
frag): fragment(
frag),
k(0), subtypes(
std::move(
subt)) {}
343template<
typename T,
typename A>
371 if (
k == 0 ||
k >= 0x80000000)
return {};
398 return {{Fragment::JUST_0}};
401 return {{Fragment::JUST_1}};
412 return {{Fragment::OLDER, *
k}};
418 return {{Fragment::AFTER, *
k}};
439 return {{Fragment::MULTI,
k, std::move(
keys)}};
446 return {{{
"V"_mst,
type_needed}, Fragment::AND_V}};
449 return {{{
"B"_mst,
"W"_mst}, Fragment::AND_B}};
452 return {{{
"B"_mst,
"W"_mst}, Fragment::OR_B}};
455 return {{{
"B"_mst,
"V"_mst}, Fragment::OR_C}};
458 return {{{
"B"_mst,
"B"_mst}, Fragment::OR_D}};
466 if (
k == 0 ||
k >
n_subs)
return {};
467 std::vector<Type> subtypes;
469 subtypes.emplace_back(
"B"_mst);
470 for (
size_t i = 1; i <
n_subs; ++i) subtypes.emplace_back(
"W"_mst);
471 return {{std::move(subtypes), Fragment::THRESH,
k}};
475 return {{{
"B"_mst}, Fragment::WRAP_A}};
478 return {{{
"B"_mst}, Fragment::WRAP_S}};
481 return {{{
"K"_mst}, Fragment::WRAP_C}};
484 return {{{
"V"_mst}, Fragment::WRAP_D}};
487 return {{{
"B"_mst}, Fragment::WRAP_V}};
490 return {{{
"B"_mst}, Fragment::WRAP_J}};
493 return {{{
"B"_mst}, Fragment::WRAP_N}};
501 return {{Fragment::MULTI_A,
k, std::move(
keys)}};
518 using recipe = std::pair<Fragment, std::vector<Type>>;
519 std::map<Type, std::vector<recipe>> wsh_table, tap_table;
523 Init(wsh_table, MsCtx::P2WSH);
524 Init(tap_table, MsCtx::TAPSCRIPT);
527 void Init(std::map<Type, std::vector<recipe>>& table,
MsCtx script_ctx)
530 std::vector<Type> types;
531 for (
int base = 0; base < 4; ++base) {
532 Type
type_base = base == 0 ?
"B"_mst : base == 1 ?
"K"_mst : base == 2 ?
"V"_mst :
"W"_mst;
533 for (
int zo = 0;
zo < 3; ++
zo) {
534 Type
type_zo =
zo == 0 ?
"z"_mst :
zo == 1 ?
"o"_mst :
""_mst;
535 for (
int n = 0; n < 2; ++n) {
536 if (
zo == 0 && n == 1)
continue;
537 if (base == 3 && n == 1)
continue;
538 Type
type_n = n == 0 ?
""_mst :
"n"_mst;
539 for (
int d = 0; d < 2; ++d) {
540 if (base == 2 && d == 1)
continue;
541 Type
type_d = d == 0 ?
""_mst :
"d"_mst;
542 for (
int u = 0; u < 2; ++u) {
543 if (base == 2 && u == 1)
continue;
544 Type
type_u = u == 0 ?
""_mst :
"u"_mst;
546 types.push_back(type);
559 auto is_super_of = [](
const recipe&
a,
const recipe& b) {
560 if (
a.first != b.first)
return false;
561 if (
a.second.size() != b.second.size())
return false;
562 for (
size_t i = 0; i <
a.second.size(); ++i) {
563 if (!(b.second[i] <<
a.second[i]))
return false;
573 std::sort(types.begin(), types.end());
596 case Fragment::MULTI:
597 case Fragment::MULTI_A:
601 case Fragment::OLDER:
602 case Fragment::AFTER:
605 case Fragment::SHA256:
606 case Fragment::HASH256:
609 case Fragment::RIPEMD160:
610 case Fragment::HASH160:
613 case Fragment::JUST_0:
614 case Fragment::JUST_1:
616 case Fragment::WRAP_A:
617 case Fragment::WRAP_S:
618 case Fragment::WRAP_C:
619 case Fragment::WRAP_D:
620 case Fragment::WRAP_V:
621 case Fragment::WRAP_J:
622 case Fragment::WRAP_N:
625 case Fragment::AND_V:
626 case Fragment::AND_B:
633 case Fragment::ANDOR:
636 case Fragment::THRESH:
645 std::vector<Type>
subt;
648 for (Type x : types) {
649 for (Type y : types) {
650 for (Type z : types) {
653 if (subs > 0)
subt.push_back(x);
654 if (subs > 1)
subt.push_back(y);
655 if (subs > 2)
subt.push_back(z);
658 if ((
res <<
"K"_mst) + (
res <<
"V"_mst) + (
res <<
"B"_mst) + (
res <<
"W"_mst) != 1)
continue;
664 for (Type s : types) {
665 if ((
res &
"BKVWzondu"_mst) << s) {
674 if (subs <= 2)
break;
676 if (subs <= 1)
break;
678 if (subs <= 0)
break;
686 std::set<Type>
useful_types{
"B"_mst,
"V"_mst,
"K"_mst,
"W"_mst};
690 for (
const auto& [type,
recipes] : table) {
692 for (
const auto& [
_, subtypes] :
recipes) {
700 std::erase_if(table, [&](
const auto& entry) {
return !
useful_types.contains(entry.first); });
713 for (
const auto& [type,
recipes] : table) {
730 std::erase_if(
type_it->second, [&](
const recipe& rec) {
731 return !std::all_of(rec.second.begin(), rec.second.end(), known_constructible);
741 for (
auto& [type,
recipes] : table) {
746 [](
const recipe&
a,
const recipe& b) {
747 if (a.second.size() < b.second.size()) return true;
748 if (a.second.size() > b.second.size()) return false;
779 case Fragment::MULTI: {
786 case Fragment::MULTI_A: {
793 case Fragment::OLDER:
794 case Fragment::AFTER:
796 case Fragment::SHA256:
798 case Fragment::HASH256:
800 case Fragment::RIPEMD160:
802 case Fragment::HASH160:
804 case Fragment::JUST_0:
805 case Fragment::JUST_1:
806 case Fragment::WRAP_A:
807 case Fragment::WRAP_S:
808 case Fragment::WRAP_C:
809 case Fragment::WRAP_D:
810 case Fragment::WRAP_V:
811 case Fragment::WRAP_J:
812 case Fragment::WRAP_N:
813 case Fragment::AND_V:
814 case Fragment::AND_B:
819 case Fragment::ANDOR:
821 case Fragment::THRESH: {
823 if (
subt.size() < 2) {
824 children =
subt.size();
831 std::vector<Type> subs =
subt;
832 while (subs.size() < children) subs.push_back(subs.back());
833 return {{std::move(subs),
frag,
k}};
852 std::vector<Node> stack;
854 std::vector<std::pair<Type, std::optional<NodeInfo>>>
todo{{
root_type, {}}};
861 while (!
todo.empty()) {
864 if (!
todo.back().second) {
875 case Fragment::JUST_0:
876 case Fragment::JUST_1:
883 case Fragment::OLDER:
884 case Fragment::AFTER:
887 case Fragment::RIPEMD160:
888 case Fragment::SHA256:
889 case Fragment::HASH160:
890 case Fragment::HASH256:
893 case Fragment::ANDOR:
896 case Fragment::AND_V:
898 case Fragment::AND_B:
911 case Fragment::THRESH:
914 case Fragment::MULTI:
917 case Fragment::MULTI_A:
920 case Fragment::WRAP_A:
923 case Fragment::WRAP_S:
926 case Fragment::WRAP_C:
929 case Fragment::WRAP_D:
932 case Fragment::WRAP_V:
936 case Fragment::WRAP_J:
939 case Fragment::WRAP_N:
946 todo.reserve(
todo.size() + subtypes.size());
949 for (
size_t i = 0; i < subtypes.size(); ++i) {
950 todo.emplace_back(*(subtypes.rbegin() + i), std::nullopt);
957 NodeInfo& info = *
todo.back().second;
959 std::vector<Node>
sub;
960 sub.reserve(info.subtypes.size());
961 for (
size_t i = 0; i < info.subtypes.size(); ++i) {
962 sub.push_back(std::move(*(stack.end() - info.subtypes.size() + i)));
964 stack.erase(stack.end() - info.subtypes.size(), stack.end());
967 if (info.keys.empty()) {
971 assert(info.hash.empty());
975 if ((
node.GetType() &
"KVWB"_mst) ==
""_mst) {
982 if (!
node.IsValid())
return {};
984 if (
node.Fragment() == Fragment::WRAP_V &&
node.Subs()[0].GetType() <<
"x"_mst) {
993 stack.push_back(std::move(
node));
997 assert(stack.size() == 1);
998 assert(stack[0].GetStaticOps() == ops);
1000 stack[0].DuplicateKeyCheck(
KEY_COMP);
1001 return std::move(stack[0]);
1021 witness.
stack.push_back(*
builder.GetSpendData().scripts.begin()->second.begin());
1043 if (!(
node->GetType() <<
"K"_mst)) {
1049 if (!
node->IsValidTopLevel())
return;
1076 int add = std::min<int>(
1079 for (
int i = 0; i < add; ++i)
script.push_back(
OP_NOP);
1090 for (
unsigned i = 0; i < add; ++i)
script.push_back(
OP_NIP);
1101 std::vector<std::vector<unsigned char>>
stack_mal;
1131 (!
node->CheckOpsLimit() &&
serror == ScriptError::SCRIPT_ERR_OP_COUNT) ||
1132 (!
node->CheckStackSize() &&
serror == ScriptError::SCRIPT_ERR_STACK_SIZE));
1143 assert(
res ||
serror == ScriptError::SCRIPT_ERR_OP_COUNT ||
serror == ScriptError::SCRIPT_ERR_STACK_SIZE);
1146 if (
node->IsSane()) {
1160 switch (
node.Fragment()) {
1161 case Fragment::PK_K:
1162 case Fragment::PK_H:
1164 case Fragment::MULTI:
1165 case Fragment::MULTI_A: {
1166 size_t sats = std::ranges::count_if(
node.Keys(), [&](
const auto& key) {
1167 return size_t(is_key_satisfiable(key));
1171 case Fragment::OLDER:
1172 case Fragment::AFTER:
1173 return node.K() & 1;
1174 case Fragment::SHA256:
1176 case Fragment::HASH256:
1178 case Fragment::RIPEMD160:
1180 case Fragment::HASH160:
1208 for (
const auto script_ctx: {MsCtx::P2WSH, MsCtx::TAPSCRIPT}) {
1220 static constexpr std::array<Type, 4>
BASE_TYPES{
"B"_mst,
"V"_mst,
"K"_mst,
"W"_mst};
1234 if (buffer.empty())
return;
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
virtual bool CheckLockTime(const CScriptNum &nLockTime) const
virtual bool CheckSchnorrSignature(std::span< const unsigned char > sig, std::span< const unsigned char > pubkey, SigVersion sigversion, ScriptExecutionData &execdata, ScriptError *serror=nullptr) const
virtual bool CheckSequence(const CScriptNum &nSequence) const
virtual bool CheckECDSASignature(const std::vector< unsigned char > &scriptSig, const std::vector< unsigned char > &vchPubKey, const CScript &scriptCode, SigVersion sigversion) const
A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160).
CHash160 & Write(std::span< const unsigned char > input)
void Finalize(std::span< unsigned char > output)
A hasher class for Bitcoin's 256-bit hash (double SHA-256).
void Finalize(std::span< unsigned char > output)
CHash256 & Write(std::span< const unsigned char > input)
An encapsulated private key.
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
A reference to a CKey: the Hash160 of its serialized public key.
An encapsulated public key.
A hasher class for RIPEMD-160.
CRIPEMD160 & Write(const unsigned char *data, size_t len)
void Finalize(unsigned char hash[OUTPUT_SIZE])
A hasher class for SHA-256.
void Finalize(unsigned char hash[OUTPUT_SIZE])
CSHA256 & Write(const unsigned char *data, size_t len)
Serialized script, used inside transaction inputs and outputs.
RAII class initializing and deinitializing global state for elliptic curve support.
std::string ConsumeBytesAsString(size_t num_bytes)
T ConsumeIntegralInRange(T min, T max)
Utility class to construct Taproot outputs from internal key and script tree.
const unsigned char * begin() const
static const XOnlyPubKey NUMS_H
Nothing Up My Sleeve point H Used as an internal key for provably disabling the key path spend see BI...
constexpr unsigned char * begin()
A node in a miniscript expression.
This type encapsulates the miniscript type system properties.
static const uint256 ZERO
uint160 Hash160(const T1 &in1)
Compute the 160-bit hash an object.
#define T(expected, seed, data)
std::string HexStr(const std::span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, script_verify_flags flags, const BaseSignatureChecker &checker, ScriptError *serror)
constexpr uint8_t TAPROOT_LEAF_TAPSCRIPT
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.
constexpr uint32_t MaxScriptSize(MiniscriptContext ms_ctx)
The maximum size of a script depending on the context.
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.
constexpr bool IsTapscript(MiniscriptContext ms_ctx)
Whether the context Tapscript, ensuring the only other possibility is P2WSH.
std::optional< Node< typename Ctx::Key > > FromScript(const CScript &script, const Ctx &ctx)
std::optional< Node< typename Ctx::Key > > FromString(const std::string &str, const Ctx &ctx)
Fragment
The different node types in miniscript.
Internal RIPEMD-160 implementation.
Internal SHA-256 implementation.
void insert(Tdst &dst, const Tsrc &src)
Simplification of std insertion.
std::string ToString(const T &t)
Locale-independent version of std::to_string.
constexpr script_verify_flags STANDARD_SCRIPT_VERIFY_FLAGS
Standard script verification flags that standard transactions will comply with.
constexpr unsigned int MAX_STANDARD_P2WSH_SCRIPT_SIZE
The maximum size in bytes of a standard witnessScript.
constexpr int MAX_STACK_SIZE
constexpr int MAX_OPS_PER_SCRIPT
enum ScriptError_t ScriptError
constexpr unsigned int GetSizeOfCompactSize(uint64_t nSize)
Compact Size size < 253 – 1 byte size <= USHRT_MAX – 3 bytes (253 + 2 bytes) size <= UINT_MAX – 5 byt...
uint64_t GetSerializeSize(const T &t)
constexpr auto MakeUCharSpan(const V &v) -> decltype(UCharSpanCast(std::span{v}))
Like the std::span constructor, but for (const) unsigned char member types only.
std::vector< Byte > ParseHex(std::string_view hex_str)
Like TryParseHex, but returns an empty vector on invalid input.
std::vector< std::vector< unsigned char > > stack
std::vector< uint16_t > keys
FuzzedDataProvider provider
bool HasTooManySubFrag(std::span< const uint8_t > buff, const int max_subs, const size_t max_nested_subs)
Whether the buffer, if it represents a valid descriptor, contains a fragment with more sub-fragments ...
bool HasTooManyWrappers(std::span< const uint8_t > buff, const int max_wrappers)
Whether the buffer, if it represents a valid descriptor, contains a fragment with more wrappers than ...
auto ConsumeNode(FuzzedDataProvider &fuzzed_data_provider, FakeSteadyClock &clock, const std::optional< NodeId > &node_id_in=std::nullopt) noexcept
auto & PickValue(FuzzedDataProvider &fuzzed_data_provider, Collection &col)
consteval auto _(util::TranslatedLiteral str)
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
FuzzedDataProvider & fuzzed_data_provider