1 // Copyright (c) 2020 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 <util/bytevectorhash.h>
12 #include <util/golombrice.h>
13 
14 #include <algorithm>
15 #include <cassert>
16 #include <cstdint>
17 #include <iosfwd>
18 #include <unordered_set>
19 #include <vector>
20 
21 namespace {
MapIntoRange(const uint64_t x,const uint64_t n)22 uint64_t MapIntoRange(const uint64_t x, const uint64_t n)
23 {
24     const uint64_t x_hi = x >> 32;
25     const uint64_t x_lo = x & 0xFFFFFFFF;
26     const uint64_t n_hi = n >> 32;
27     const uint64_t n_lo = n & 0xFFFFFFFF;
28     const uint64_t ac = x_hi * n_hi;
29     const uint64_t ad = x_hi * n_lo;
30     const uint64_t bc = x_lo * n_hi;
31     const uint64_t bd = x_lo * n_lo;
32     const uint64_t mid34 = (bd >> 32) + (bc & 0xFFFFFFFF) + (ad & 0xFFFFFFFF);
33     const uint64_t upper64 = ac + (bc >> 32) + (ad >> 32) + (mid34 >> 32);
34     return upper64;
35 }
36 
HashToRange(const std::vector<uint8_t> & element,const uint64_t f)37 uint64_t HashToRange(const std::vector<uint8_t>& element, const uint64_t f)
38 {
39     const uint64_t hash = CSipHasher(0x0706050403020100ULL, 0x0F0E0D0C0B0A0908ULL)
40                               .Write(element.data(), element.size())
41                               .Finalize();
42     return MapIntoRange(hash, f);
43 }
44 
BuildHashedSet(const std::unordered_set<std::vector<uint8_t>,ByteVectorHash> & elements,const uint64_t f)45 std::vector<uint64_t> BuildHashedSet(const std::unordered_set<std::vector<uint8_t>, ByteVectorHash>& elements, const uint64_t f)
46 {
47     std::vector<uint64_t> hashed_elements;
48     hashed_elements.reserve(elements.size());
49     for (const std::vector<uint8_t>& element : elements) {
50         hashed_elements.push_back(HashToRange(element, f));
51     }
52     std::sort(hashed_elements.begin(), hashed_elements.end());
53     return hashed_elements;
54 }
55 } // namespace
56 
test_one_input(const std::vector<uint8_t> & buffer)57 void test_one_input(const std::vector<uint8_t>& buffer)
58 {
59     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
60     std::vector<uint8_t> golomb_rice_data;
61     std::vector<uint64_t> encoded_deltas;
62     {
63         std::unordered_set<std::vector<uint8_t>, ByteVectorHash> elements;
64         const int n = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 512);
65         for (int i = 0; i < n; ++i) {
66             elements.insert(ConsumeRandomLengthByteVector(fuzzed_data_provider, 16));
67         }
68         CVectorWriter stream(SER_NETWORK, 0, golomb_rice_data, 0);
69         WriteCompactSize(stream, static_cast<uint32_t>(elements.size()));
70         BitStreamWriter<CVectorWriter> bitwriter(stream);
71         if (!elements.empty()) {
72             uint64_t last_value = 0;
73             for (const uint64_t value : BuildHashedSet(elements, static_cast<uint64_t>(elements.size()) * static_cast<uint64_t>(BASIC_FILTER_M))) {
74                 const uint64_t delta = value - last_value;
75                 encoded_deltas.push_back(delta);
76                 GolombRiceEncode(bitwriter, BASIC_FILTER_P, delta);
77                 last_value = value;
78             }
79         }
80         bitwriter.Flush();
81     }
82 
83     std::vector<uint64_t> decoded_deltas;
84     {
85         VectorReader stream{SER_NETWORK, 0, golomb_rice_data, 0};
86         BitStreamReader<VectorReader> bitreader(stream);
87         const uint32_t n = static_cast<uint32_t>(ReadCompactSize(stream));
88         for (uint32_t i = 0; i < n; ++i) {
89             decoded_deltas.push_back(GolombRiceDecode(bitreader, BASIC_FILTER_P));
90         }
91     }
92 
93     assert(encoded_deltas == decoded_deltas);
94 
95     {
96         const std::vector<uint8_t> random_bytes = ConsumeRandomLengthByteVector(fuzzed_data_provider, 1024);
97         VectorReader stream{SER_NETWORK, 0, random_bytes, 0};
98         uint32_t n;
99         try {
100             n = static_cast<uint32_t>(ReadCompactSize(stream));
101         } catch (const std::ios_base::failure&) {
102             return;
103         }
104         BitStreamReader<VectorReader> bitreader(stream);
105         for (uint32_t i = 0; i < std::min<uint32_t>(n, 1024); ++i) {
106             try {
107                 (void)GolombRiceDecode(bitreader, BASIC_FILTER_P);
108             } catch (const std::ios_base::failure&) {
109             }
110         }
111     }
112 }
113