Bitcoin Core  22.99.0
P2P Digital Currency
muhash.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-2021 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 <crypto/muhash.h>
7 #include <test/fuzz/fuzz.h>
8 #include <test/fuzz/util.h>
9 
10 #include <vector>
11 
12 FUZZ_TARGET(muhash)
13 {
14  FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
15  std::vector<uint8_t> data = ConsumeRandomLengthByteVector(fuzzed_data_provider);
16  std::vector<uint8_t> data2 = ConsumeRandomLengthByteVector(fuzzed_data_provider);
17  if (data.empty()) {
18  data.resize(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 4096), fuzzed_data_provider.ConsumeIntegral<uint8_t>());
19  }
20  if (data2.empty()) {
21  data2.resize(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 4096), fuzzed_data_provider.ConsumeIntegral<uint8_t>());
22  }
23 
24  data = ConsumeRandomLengthByteVector(fuzzed_data_provider);
25  data2 = ConsumeRandomLengthByteVector(fuzzed_data_provider);
26 
27  MuHash3072 muhash;
28 
29  // Test that MuHash result is consistent independent of order of operations
30  muhash.Insert(data);
31  muhash.Insert(data2);
32 
33  uint256 out;
34  muhash.Finalize(out);
35 
36  muhash = MuHash3072();
37  muhash.Insert(data2);
38  muhash.Insert(data);
39 
40  uint256 out2;
41  muhash.Finalize(out2);
42 
43  assert(out == out2);
44  MuHash3072 muhash3;
45  muhash3 *= muhash;
46  uint256 out3;
47  muhash3.Finalize(out3);
48  assert(out == out3);
49 
50  // Test that removing all added elements brings the object back to it's initial state
51  muhash /= muhash;
52  muhash.Finalize(out);
53 
54  MuHash3072 muhash2;
55  muhash2.Finalize(out2);
56 
57  assert(out == out2);
58 
59  muhash3.Remove(data);
60  muhash3.Remove(data2);
61  muhash3.Finalize(out3);
62  assert(out == out3);
63 }
FUZZ_TARGET
FUZZ_TARGET(muhash)
Definition: muhash.cpp:12
assert
assert(!tx.IsCoinBase())
MuHash3072::Insert
MuHash3072 & Insert(Span< const unsigned char > in) noexcept
Definition: muhash.cpp:338
util.h
MuHash3072::Remove
MuHash3072 & Remove(Span< const unsigned char > in) noexcept
Definition: muhash.cpp:343
muhash.h
FuzzedDataProvider.h
ConsumeRandomLengthByteVector
std::vector< uint8_t > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:61
uint256
256-bit opaque blob.
Definition: uint256.h:124
MuHash3072::Finalize
void Finalize(uint256 &out) noexcept
Definition: muhash.cpp:313
fuzz.h
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
MuHash3072
A class representing MuHash sets.
Definition: muhash.h:94