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) 2021-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 <minisketch.h>7#include <test/fuzz/FuzzedDataProvider.h>8#include <test/fuzz/fuzz.h>9#include <test/fuzz/util.h>10#include <util/check.h>12#include <limits>13#include <map>14#include <numeric>15#include <utility>17namespace {19Minisketch MakeFuzzMinisketch32(size_t capacity, uint32_t impl)20{21return Assert(Minisketch(32, impl, capacity));22}24} // namespace26FUZZ_TARGET(minisketch)27{28FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};30const auto capacity{fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 200)};31const uint32_t impl{fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(0, Minisketch::MaxImplementation())};32if (!Minisketch::ImplementationSupported(32, impl)) return;34Minisketch sketch_a{MakeFuzzMinisketch32(capacity, impl)};35Minisketch sketch_b{MakeFuzzMinisketch32(capacity, impl)};36sketch_a.SetSeed(fuzzed_data_provider.ConsumeIntegral<uint64_t>());37sketch_b.SetSeed(fuzzed_data_provider.ConsumeIntegral<uint64_t>());39// Fill two sets and keep the difference in a map40std::map<uint32_t, bool> diff;41LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 10000) {42const auto entry{fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(1, std::numeric_limits<uint32_t>::max() - 1)};43const auto KeepDiff{[&] {44bool& mut{diff[entry]};45mut = !mut;46}};47CallOneOf(48fuzzed_data_provider,49[&] {50sketch_a.Add(entry);51KeepDiff();52},53[&] {54sketch_b.Add(entry);55KeepDiff();56},57[&] {58sketch_a.Add(entry);59sketch_b.Add(entry);60});61}62const auto num_diff{std::accumulate(diff.begin(), diff.end(), size_t{0}, [](auto n, const auto& e) { return n + e.second; })};64Minisketch sketch_ar{MakeFuzzMinisketch32(capacity, impl)};65Minisketch sketch_br{MakeFuzzMinisketch32(capacity, impl)};66sketch_ar.SetSeed(fuzzed_data_provider.ConsumeIntegral<uint64_t>());67sketch_br.SetSeed(fuzzed_data_provider.ConsumeIntegral<uint64_t>());69sketch_ar.Deserialize(sketch_a.Serialize());70sketch_br.Deserialize(sketch_b.Serialize());72Minisketch sketch_diff{std::move(fuzzed_data_provider.ConsumeBool() ? sketch_a : sketch_ar)};73sketch_diff.Merge(fuzzed_data_provider.ConsumeBool() ? sketch_b : sketch_br);75if (capacity >= num_diff) {76const auto max_elements{fuzzed_data_provider.ConsumeIntegralInRange<size_t>(num_diff, capacity)};77const auto dec{*Assert(sketch_diff.Decode(max_elements))};78Assert(dec.size() == num_diff);79for (auto d : dec) {80Assert(diff.at(d));81}82}83}
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.