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) 2020-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 <blockfilter.h>6#include <serialize.h>7#include <streams.h>8#include <test/fuzz/FuzzedDataProvider.h>9#include <test/fuzz/fuzz.h>10#include <test/fuzz/util.h>11#include <test/util/random.h>12#include <util/bytevectorhash.h>13#include <util/golombrice.h>15#include <algorithm>16#include <cassert>17#include <cstdint>18#include <iosfwd>19#include <unordered_set>20#include <vector>22namespace {24uint64_t HashToRange(const std::vector<uint8_t>& element, const uint64_t f)25{26const uint64_t hash = CSipHasher(0x0706050403020100ULL, 0x0F0E0D0C0B0A0908ULL)27.Write(element)28.Finalize();29return FastRange64(hash, f);30}32std::vector<uint64_t> BuildHashedSet(const std::unordered_set<std::vector<uint8_t>, ByteVectorHash>& elements, const uint64_t f)33{34std::vector<uint64_t> hashed_elements;35hashed_elements.reserve(elements.size());36for (const std::vector<uint8_t>& element : elements) {37hashed_elements.push_back(HashToRange(element, f));38}39std::sort(hashed_elements.begin(), hashed_elements.end());40return hashed_elements;41}42} // namespace44FUZZ_TARGET(golomb_rice)45{46SeedRandomStateForTest(SeedRand::ZEROS);47FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());48std::vector<uint8_t> golomb_rice_data;49std::vector<uint64_t> encoded_deltas;50{51std::unordered_set<std::vector<uint8_t>, ByteVectorHash> elements;52const int n = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 512);53for (int i = 0; i < n; ++i) {54elements.insert(ConsumeRandomLengthByteVector(fuzzed_data_provider, 16));55}56VectorWriter stream{golomb_rice_data, 0};57WriteCompactSize(stream, static_cast<uint32_t>(elements.size()));58BitStreamWriter bitwriter{stream};59if (!elements.empty()) {60uint64_t last_value = 0;61for (const uint64_t value : BuildHashedSet(elements, static_cast<uint64_t>(elements.size()) * static_cast<uint64_t>(BASIC_FILTER_M))) {62const uint64_t delta = value - last_value;63encoded_deltas.push_back(delta);64GolombRiceEncode(bitwriter, BASIC_FILTER_P, delta);65last_value = value;66}67}68bitwriter.Flush();69}71std::vector<uint64_t> decoded_deltas;72{73SpanReader stream{golomb_rice_data};74BitStreamReader bitreader{stream};75const uint32_t n = static_cast<uint32_t>(ReadCompactSize(stream));76for (uint32_t i = 0; i < n; ++i) {77decoded_deltas.push_back(GolombRiceDecode(bitreader, BASIC_FILTER_P));78}79}81assert(encoded_deltas == decoded_deltas);83{84const std::vector<uint8_t> random_bytes = ConsumeRandomLengthByteVector(fuzzed_data_provider, 1024);85SpanReader stream{random_bytes};86uint32_t n;87try {88n = static_cast<uint32_t>(ReadCompactSize(stream));89} catch (const std::ios_base::failure&) {90return;91}92BitStreamReader bitreader{stream};93for (uint32_t i = 0; i < std::min<uint32_t>(n, 1024); ++i) {94try {95(void)GolombRiceDecode(bitreader, BASIC_FILTER_P);96} catch (const std::ios_base::failure&) {97}98}99}100}
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.