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) 2019-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#include <bech32.h>6#include <test/fuzz/fuzz.h>7#include <test/fuzz/FuzzedDataProvider.h>8#include <util/strencodings.h>10#include <cassert>11#include <cstdint>12#include <string>13#include <vector>15FUZZ_TARGET(bech32_random_decode)16{17auto limit = bech32::CharLimit::BECH32;18FuzzedDataProvider fdp(buffer.data(), buffer.size());19auto random_string = fdp.ConsumeRandomLengthString(limit + 1);20auto decoded = bech32::Decode(random_string, limit);22if (decoded.hrp.empty()) {23assert(decoded.encoding == bech32::Encoding::INVALID);24assert(decoded.data.empty());25} else {26assert(decoded.encoding != bech32::Encoding::INVALID);27auto reencoded = bech32::Encode(decoded.encoding, decoded.hrp, decoded.data);28assert(CaseInsensitiveEqual(random_string, reencoded));29}30}32// https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki and https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki33std::string GenerateRandomHRP(FuzzedDataProvider& fdp)34{35std::string hrp;36size_t length = fdp.ConsumeIntegralInRange<size_t>(1, 83);37for (size_t i = 0; i < length; ++i) {38// Generate lowercase ASCII characters in ([33-126] - ['A'-'Z']) range39char c = fdp.ConsumeBool()40? fdp.ConsumeIntegralInRange<char>(33, 'A' - 1)41: fdp.ConsumeIntegralInRange<char>('Z' + 1, 126);42hrp += c;43}44return hrp;45}47FUZZ_TARGET(bech32_roundtrip)48{49FuzzedDataProvider fdp(buffer.data(), buffer.size());50auto hrp = GenerateRandomHRP(fdp);52auto input_chars = fdp.ConsumeBytes<unsigned char>(fdp.ConsumeIntegralInRange<size_t>(0, 82));53std::vector<uint8_t> converted_input;54ConvertBits<8, 5, true>([&](auto c) { converted_input.push_back(c); }, input_chars.begin(), input_chars.end());56auto size = converted_input.size() + hrp.length() + std::string({bech32::SEPARATOR}).size() + bech32::CHECKSUM_SIZE;57if (size <= bech32::CharLimit::BECH32) {58for (auto encoding: {bech32::Encoding::BECH32, bech32::Encoding::BECH32M}) {59auto encoded = bech32::Encode(encoding, hrp, converted_input);60assert(!encoded.empty());62const auto decoded = bech32::Decode(encoded);63assert(decoded.encoding == encoding);64assert(decoded.hrp == hrp);65assert(decoded.data == converted_input);66}67}68}
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.