Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2023-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#ifndef BITCOIN_TEST_FUZZ_UTIL_DESCRIPTOR_H6#define BITCOIN_TEST_FUZZ_UTIL_DESCRIPTOR_H8#include <array>9#include <cinttypes>10#include <cstddef>11#include <limits>12#include <optional>13#include <span>14#include <string>15#include <string_view>17/**18* Converts a mocked descriptor string to a valid one. Every key in a mocked descriptor is19* represented by 2 hex characters preceded by the '%' character. We parse the two hex characters20* as an index in a list of pre-generated keys. This list contains keys of the various types21* accepted in descriptor key expressions.22*/23class MockedDescriptorConverter {24private:25//! Types are raw (un)compressed pubkeys, raw xonly pubkeys, raw privkeys (WIF), xpubs, xprvs.26static constexpr uint8_t KEY_TYPES_COUNT{6};27//! How many keys we'll generate in total.28static constexpr size_t TOTAL_KEYS_GENERATED{std::numeric_limits<uint8_t>::max() + 1};29//! 256 keys of various types.30std::array<std::string, TOTAL_KEYS_GENERATED> keys_str;32public:33// We derive the type of key to generate from the 1-byte id parsed from hex.34bool IdIsCompPubKey(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 0; }35bool IdIsUnCompPubKey(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 1; }36bool IdIsXOnlyPubKey(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 2; }37bool IdIsConstPrivKey(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 3; }38bool IdIsXpub(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 4; }39bool IdIsXprv(uint8_t idx) const { return idx % KEY_TYPES_COUNT == 5; }41//! When initializing the target, populate the list of keys.42void Init();44//! Parse an id in the keys vectors from a 2-characters hex string.45std::optional<uint8_t> IdxFromHex(std::string_view hex_characters) const;47//! Get an actual descriptor string from a descriptor string whose keys were mocked.48std::optional<std::string> GetDescriptor(std::string_view mocked_desc) const;49};51//! Default maximum number of derivation indexes in a single derivation path when limiting its depth.52inline constexpr int MAX_DEPTH{2};54/**55* Whether the buffer, if it represents a valid descriptor, contains a derivation path deeper than56* a given maximum depth. Note this may also be hit for deriv paths in origins.57*/58bool HasDeepDerivPath(std::span<const uint8_t> buff, int max_depth = MAX_DEPTH);60//! Default maximum number of sub-fragments.61inline constexpr int MAX_SUBS{1'000};62//! Maximum number of nested sub-fragments we'll allow in a descriptor.63inline constexpr size_t MAX_NESTED_SUBS{10'000};65/**66* Whether the buffer, if it represents a valid descriptor, contains a fragment with more67* sub-fragments than the given maximum.68*/69bool HasTooManySubFrag(std::span<const uint8_t> buff, int max_subs = MAX_SUBS,70size_t max_nested_subs = MAX_NESTED_SUBS);72//! Default maximum number of wrappers per fragment.73inline constexpr int MAX_WRAPPERS{100};75/**76* Whether the buffer, if it represents a valid descriptor, contains a fragment with more77* wrappers than the given maximum.78*/79bool HasTooManyWrappers(std::span<const uint8_t> buff, int max_wrappers = MAX_WRAPPERS);81/// Default maximum leaf size. This should be large enough to cover an extended82/// key, including paths "/", inside and outside of "[]".83inline constexpr uint32_t MAX_LEAF_SIZE{200};85/// Whether the expanded buffer (after calling GetDescriptor() in86/// MockedDescriptorConverter) has a leaf size too large.87bool HasTooLargeLeafSize(std::span<const uint8_t> buff, uint32_t max_leaf_size = MAX_LEAF_SIZE);89/// Deriving "expensive" descriptors will consume useful fuzz compute. The90/// compute is better spent on a smaller subset of descriptors, which still91/// covers all real end-user settings.92///93/// Use this function after MockedDescriptorConverter::GetDescriptor()94inline 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'd97// rather spend time elsewhere in this target, like on the actual descriptor syntax. So rule98// out strings which could correspond to a descriptor containing a too large derivation path.99if (HasDeepDerivPath(buffer)) return true;101// Some fragments can take a virtually unlimited number of sub-fragments (thresh, multi_a) but102// may perform quadratic operations on them. Limit the number of sub-fragments per fragment.103if (HasTooManySubFrag(buffer)) return true;105// The script building logic performs quadratic copies in the number of nested wrappers. Limit106// the number of nested wrappers per fragment.107if (HasTooManyWrappers(buffer)) return true;109// If any suspected leaf is too large, it will likely not represent a valid110// use-case. Also, possible base58 parsing in the leaf is quadratic. So111// limit the leaf size.112if (HasTooLargeLeafSize(buffer)) return true;114return false;115}116#endif // BITCOIN_TEST_FUZZ_UTIL_DESCRIPTOR_H
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.