Bitcoin Core  31.0.0
P2P Digital Currency
descriptor.h
Go to the documentation of this file.
1 // Copyright (c) 2023-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_TEST_FUZZ_UTIL_DESCRIPTOR_H
6 #define BITCOIN_TEST_FUZZ_UTIL_DESCRIPTOR_H
7 
8 #include <array>
9 #include <cinttypes>
10 #include <cstddef>
11 #include <limits>
12 #include <optional>
13 #include <span>
14 #include <string>
15 #include <string_view>
16 
24 private:
26  static constexpr uint8_t KEY_TYPES_COUNT{6};
28  static constexpr size_t TOTAL_KEYS_GENERATED{std::numeric_limits<uint8_t>::max() + 1};
30  std::array<std::string, TOTAL_KEYS_GENERATED> keys_str;
31 
32 public:
33  // We derive the type of key to generate from the 1-byte id parsed from hex.
34  bool IdIsCompPubKey(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 0; }
35  bool IdIsUnCompPubKey(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 1; }
36  bool IdIsXOnlyPubKey(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 2; }
37  bool IdIsConstPrivKey(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 3; }
38  bool IdIsXpub(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 4; }
39  bool IdIsXprv(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 5; }
40 
42  void Init();
43 
45  std::optional<uint8_t> IdxFromHex(std::string_view hex_characters) const;
46 
48  std::optional<std::string> GetDescriptor(std::string_view mocked_desc) const;
49 };
50 
52 constexpr int MAX_DEPTH{2};
53 
58 bool HasDeepDerivPath(std::span<const uint8_t> buff, int max_depth = MAX_DEPTH);
59 
61 constexpr int MAX_SUBS{1'000};
63 constexpr size_t MAX_NESTED_SUBS{10'000};
64 
69 bool HasTooManySubFrag(std::span<const uint8_t> buff, int max_subs = MAX_SUBS,
70  size_t max_nested_subs = MAX_NESTED_SUBS);
71 
73 constexpr int MAX_WRAPPERS{100};
74 
79 bool HasTooManyWrappers(std::span<const uint8_t> buff, int max_wrappers = MAX_WRAPPERS);
80 
83 constexpr uint32_t MAX_LEAF_SIZE{200};
84 
87 bool HasTooLargeLeafSize(std::span<const uint8_t> buff, uint32_t max_leaf_size = MAX_LEAF_SIZE);
88 
94 inline bool IsTooExpensive(std::span<const uint8_t> buffer)
95 {
96  // Key derivation is expensive. Deriving deep derivation paths takes a lot of compute and we'd
97  // rather spend time elsewhere in this target, like on the actual descriptor syntax. So rule
98  // out strings which could correspond to a descriptor containing a too large derivation path.
99  if (HasDeepDerivPath(buffer)) return true;
100 
101  // Some fragments can take a virtually unlimited number of sub-fragments (thresh, multi_a) but
102  // may perform quadratic operations on them. Limit the number of sub-fragments per fragment.
103  if (HasTooManySubFrag(buffer)) return true;
104 
105  // The script building logic performs quadratic copies in the number of nested wrappers. Limit
106  // the number of nested wrappers per fragment.
107  if (HasTooManyWrappers(buffer)) return true;
108 
109  // If any suspected leaf is too large, it will likely not represent a valid
110  // use-case. Also, possible base58 parsing in the leaf is quadratic. So
111  // limit the leaf size.
112  if (HasTooLargeLeafSize(buffer)) return true;
113 
114  return false;
115 }
116 #endif // BITCOIN_TEST_FUZZ_UTIL_DESCRIPTOR_H
bool IsTooExpensive(std::span< const uint8_t > buffer)
Deriving "expensive" descriptors will consume useful fuzz compute. The compute is better spent on a s...
Definition: descriptor.h:94
std::optional< uint8_t > IdxFromHex(std::string_view hex_characters) const
Parse an id in the keys vectors from a 2-characters hex string.
Definition: descriptor.cpp:52
constexpr uint32_t MAX_LEAF_SIZE
Default maximum leaf size. This should be large enough to cover an extended key, including paths "/"...
Definition: descriptor.h:83
constexpr size_t MAX_NESTED_SUBS
Maximum number of nested sub-fragments we&#39;ll allow in a descriptor.
Definition: descriptor.h:63
constexpr int MAX_DEPTH
Default maximum number of derivation indexes in a single derivation path when limiting its depth...
Definition: descriptor.h:52
static constexpr size_t TOTAL_KEYS_GENERATED
How many keys we&#39;ll generate in total.
Definition: descriptor.h:28
std::array< std::string, TOTAL_KEYS_GENERATED > keys_str
256 keys of various types.
Definition: descriptor.h:30
bool IdIsXpub(uint8_t idx) const
Definition: descriptor.h:38
bool IdIsXprv(uint8_t idx) const
Definition: descriptor.h:39
bool HasTooLargeLeafSize(std::span< const uint8_t > buff, uint32_t max_leaf_size=MAX_LEAF_SIZE)
Whether the expanded buffer (after calling GetDescriptor() in MockedDescriptorConverter) has a leaf s...
Definition: descriptor.cpp:155
bool IdIsCompPubKey(uint8_t idx) const
Definition: descriptor.h:34
bool IdIsXOnlyPubKey(uint8_t idx) const
Definition: descriptor.h:36
void Init()
When initializing the target, populate the list of keys.
Definition: descriptor.cpp:17
bool HasTooManySubFrag(std::span< const uint8_t > buff, int max_subs=MAX_SUBS, size_t max_nested_subs=MAX_NESTED_SUBS)
Whether the buffer, if it represents a valid descriptor, contains a fragment with more sub-fragments ...
Definition: descriptor.cpp:99
std::optional< std::string > GetDescriptor(std::string_view mocked_desc) const
Get an actual descriptor string from a descriptor string whose keys were mocked.
Definition: descriptor.cpp:59
Converts a mocked descriptor string to a valid one.
Definition: descriptor.h:23
constexpr int MAX_WRAPPERS
Default maximum number of wrappers per fragment.
Definition: descriptor.h:73
static constexpr uint8_t KEY_TYPES_COUNT
Types are raw (un)compressed pubkeys, raw xonly pubkeys, raw privkeys (WIF), xpubs, xprvs.
Definition: descriptor.h:26
bool IdIsConstPrivKey(uint8_t idx) const
Definition: descriptor.h:37
bool HasTooManyWrappers(std::span< const uint8_t > buff, int max_wrappers=MAX_WRAPPERS)
Whether the buffer, if it represents a valid descriptor, contains a fragment with more wrappers than ...
Definition: descriptor.cpp:123
bool HasDeepDerivPath(std::span< const uint8_t > buff, int max_depth=MAX_DEPTH)
Whether the buffer, if it represents a valid descriptor, contains a derivation path deeper than a giv...
Definition: descriptor.cpp:85
bool IdIsUnCompPubKey(uint8_t idx) const
Definition: descriptor.h:35
constexpr int MAX_SUBS
Default maximum number of sub-fragments.
Definition: descriptor.h:61