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 <test/fuzz/fuzz.h>7#include <base58.h>8#include <psbt.h>9#include <span.h>10#include <test/fuzz/FuzzedDataProvider.h>11#include <util/strencodings.h>12#include <util/string.h>14#include <cassert>15#include <string>16#include <vector>17#include <ranges>19using util::TrimStringView;21FUZZ_TARGET(base58_encode_decode)22{23FuzzedDataProvider provider{buffer.data(), buffer.size()};24const auto random_string{provider.ConsumeRandomLengthString(100)};26const auto encoded{EncodeBase58(MakeUCharSpan(random_string))};27const auto decode_input{provider.ConsumeBool() ? random_string : encoded};28const int max_ret_len{provider.ConsumeIntegralInRange<int>(-1, decode_input.size() + 1)};29if (std::vector<unsigned char> decoded; DecodeBase58(decode_input, decoded, max_ret_len)) {30const auto encoded_string{EncodeBase58(decoded)};31assert(encoded_string == TrimStringView(decode_input));32if (decoded.size() > 0) {33assert(max_ret_len > 0);34assert(decoded.size() <= static_cast<size_t>(max_ret_len));35assert(!DecodeBase58(encoded_string, decoded, provider.ConsumeIntegralInRange<int>(0, decoded.size() - 1)));36}37}38}40FUZZ_TARGET(base58check_encode_decode)41{42FuzzedDataProvider provider{buffer.data(), buffer.size()};43const auto random_string{provider.ConsumeRandomLengthString(100)};45const auto encoded{EncodeBase58Check(MakeUCharSpan(random_string))};46const auto decode_input{provider.ConsumeBool() ? random_string : encoded};47const int max_ret_len{provider.ConsumeIntegralInRange<int>(-1, decode_input.size() + 1)};48if (std::vector<unsigned char> decoded; DecodeBase58Check(decode_input, decoded, max_ret_len)) {49const auto encoded_string{EncodeBase58Check(decoded)};50assert(encoded_string == TrimStringView(decode_input));51if (decoded.size() > 0) {52assert(max_ret_len > 0);53assert(decoded.size() <= static_cast<size_t>(max_ret_len));54assert(!DecodeBase58Check(encoded_string, decoded, provider.ConsumeIntegralInRange<int>(0, decoded.size() - 1)));55}56}57}59FUZZ_TARGET(base32_encode_decode)60{61const std::string random_string{buffer.begin(), buffer.end()};63// Decode/Encode roundtrip64if (auto result{DecodeBase32(random_string)}) {65const auto encoded_string{EncodeBase32(*result)};66assert(encoded_string == ToLower(TrimStringView(random_string)));67}68// Encode/Decode roundtrip69const auto encoded{EncodeBase32(buffer)};70const auto decoded{DecodeBase32(encoded)};71assert(decoded && std::ranges::equal(*decoded, buffer));72}74FUZZ_TARGET(base64_encode_decode)75{76const std::string random_string{buffer.begin(), buffer.end()};78// Decode/Encode roundtrip79if (auto result{DecodeBase64(random_string)}) {80const auto encoded_string{EncodeBase64(*result)};81assert(encoded_string == TrimStringView(random_string));82}83// Encode/Decode roundtrip84const auto encoded{EncodeBase64(buffer)};85const auto decoded{DecodeBase64(encoded)};86assert(decoded && std::ranges::equal(*decoded, buffer));87}89FUZZ_TARGET(psbt_base64_decode)90{91const std::string random_string{buffer.begin(), buffer.end()};93util::Result<PartiallySignedTransaction> psbt = DecodeBase64PSBT(random_string);94}
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.