/root/bitcoin/src/test/fuzz/golomb_rice.cpp
| Line | Count | Source | 
| 1 |  | // Copyright (c) 2020-2022 The Bitcoin Core developers | 
| 2 |  | // Distributed under the MIT software license, see the accompanying | 
| 3 |  | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | 
| 4 |  |  | 
| 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> | 
| 14 |  |  | 
| 15 |  | #include <algorithm> | 
| 16 |  | #include <cassert> | 
| 17 |  | #include <cstdint> | 
| 18 |  | #include <iosfwd> | 
| 19 |  | #include <unordered_set> | 
| 20 |  | #include <vector> | 
| 21 |  |  | 
| 22 |  | namespace { | 
| 23 |  |  | 
| 24 |  | uint64_t HashToRange(const std::vector<uint8_t>& element, const uint64_t f) | 
| 25 | 0 | { | 
| 26 | 0 |     const uint64_t hash = CSipHasher(0x0706050403020100ULL, 0x0F0E0D0C0B0A0908ULL) | 
| 27 | 0 |                               .Write(element) | 
| 28 | 0 |                               .Finalize(); | 
| 29 | 0 |     return FastRange64(hash, f); | 
| 30 | 0 | } | 
| 31 |  |  | 
| 32 |  | std::vector<uint64_t> BuildHashedSet(const std::unordered_set<std::vector<uint8_t>, ByteVectorHash>& elements, const uint64_t f) | 
| 33 | 0 | { | 
| 34 | 0 |     std::vector<uint64_t> hashed_elements; | 
| 35 | 0 |     hashed_elements.reserve(elements.size()); | 
| 36 | 0 |     for (const std::vector<uint8_t>& element : elements) { | 
| 37 | 0 |         hashed_elements.push_back(HashToRange(element, f)); | 
| 38 | 0 |     } | 
| 39 | 0 |     std::sort(hashed_elements.begin(), hashed_elements.end()); | 
| 40 | 0 |     return hashed_elements; | 
| 41 | 0 | } | 
| 42 |  | } // namespace | 
| 43 |  |  | 
| 44 |  | FUZZ_TARGET(golomb_rice) | 
| 45 | 0 | { | 
| 46 | 0 |     SeedRandomStateForTest(SeedRand::ZEROS); | 
| 47 | 0 |     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); | 
| 48 | 0 |     std::vector<uint8_t> golomb_rice_data; | 
| 49 | 0 |     std::vector<uint64_t> encoded_deltas; | 
| 50 | 0 |     { | 
| 51 | 0 |         std::unordered_set<std::vector<uint8_t>, ByteVectorHash> elements; | 
| 52 | 0 |         const int n = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 512); | 
| 53 | 0 |         for (int i = 0; i < n; ++i) { | 
| 54 | 0 |             elements.insert(ConsumeRandomLengthByteVector(fuzzed_data_provider, 16)); | 
| 55 | 0 |         } | 
| 56 | 0 |         VectorWriter stream{golomb_rice_data, 0}; | 
| 57 | 0 |         WriteCompactSize(stream, static_cast<uint32_t>(elements.size())); | 
| 58 | 0 |         BitStreamWriter bitwriter{stream}; | 
| 59 | 0 |         if (!elements.empty()) { | 
| 60 | 0 |             uint64_t last_value = 0; | 
| 61 | 0 |             for (const uint64_t value : BuildHashedSet(elements, static_cast<uint64_t>(elements.size()) * static_cast<uint64_t>(BASIC_FILTER_M))) { | 
| 62 | 0 |                 const uint64_t delta = value - last_value; | 
| 63 | 0 |                 encoded_deltas.push_back(delta); | 
| 64 | 0 |                 GolombRiceEncode(bitwriter, BASIC_FILTER_P, delta); | 
| 65 | 0 |                 last_value = value; | 
| 66 | 0 |             } | 
| 67 | 0 |         } | 
| 68 | 0 |         bitwriter.Flush(); | 
| 69 | 0 |     } | 
| 70 |  | 
 | 
| 71 | 0 |     std::vector<uint64_t> decoded_deltas; | 
| 72 | 0 |     { | 
| 73 | 0 |         SpanReader stream{golomb_rice_data}; | 
| 74 | 0 |         BitStreamReader bitreader{stream}; | 
| 75 | 0 |         const uint32_t n = static_cast<uint32_t>(ReadCompactSize(stream)); | 
| 76 | 0 |         for (uint32_t i = 0; i < n; ++i) { | 
| 77 | 0 |             decoded_deltas.push_back(GolombRiceDecode(bitreader, BASIC_FILTER_P)); | 
| 78 | 0 |         } | 
| 79 | 0 |     } | 
| 80 |  | 
 | 
| 81 | 0 |     assert(encoded_deltas == decoded_deltas); | 
| 82 |  |  | 
| 83 | 0 |     { | 
| 84 | 0 |         const std::vector<uint8_t> random_bytes = ConsumeRandomLengthByteVector(fuzzed_data_provider, 1024); | 
| 85 | 0 |         SpanReader stream{random_bytes}; | 
| 86 | 0 |         uint32_t n; | 
| 87 | 0 |         try { | 
| 88 | 0 |             n = static_cast<uint32_t>(ReadCompactSize(stream)); | 
| 89 | 0 |         } catch (const std::ios_base::failure&) { | 
| 90 | 0 |             return; | 
| 91 | 0 |         } | 
| 92 | 0 |         BitStreamReader bitreader{stream}; | 
| 93 | 0 |         for (uint32_t i = 0; i < std::min<uint32_t>(n, 1024); ++i) { | 
| 94 | 0 |             try { | 
| 95 | 0 |                 (void)GolombRiceDecode(bitreader, BASIC_FILTER_P); | 
| 96 | 0 |             } catch (const std::ios_base::failure&) { | 
| 97 | 0 |             } | 
| 98 | 0 |         } | 
| 99 | 0 |     } | 
| 100 | 0 | } |