Bitcoin Core  24.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 
18  MuHash3072 muhash;
19 
20  muhash.Insert(data);
21  muhash.Insert(data2);
22 
23  const std::string initial_state_hash{"dd5ad2a105c2d29495f577245c357409002329b9f4d6182c0af3dc2f462555c8"};
24  uint256 out;
25  uint256 out2;
26  CallOneOf(
27  fuzzed_data_provider,
28  [&] {
29  // Test that MuHash result is consistent independent of order of operations
30  muhash.Finalize(out);
31 
32  muhash = MuHash3072();
33  muhash.Insert(data2);
34  muhash.Insert(data);
35  muhash.Finalize(out2);
36  },
37  [&] {
38  // Test that multiplication with the initial state never changes the finalized result
39  muhash.Finalize(out);
40  MuHash3072 muhash3;
41  muhash3 *= muhash;
42  muhash3.Finalize(out2);
43  },
44  [&] {
45  // Test that dividing a MuHash by itself brings it back to it's initial state
46  muhash /= muhash;
47  muhash.Finalize(out);
48  out2 = uint256S(initial_state_hash);
49  },
50  [&] {
51  // Test that removing all added elements brings the object back to it's initial state
52  muhash.Remove(data);
53  muhash.Remove(data2);
54  muhash.Finalize(out);
55  out2 = uint256S(initial_state_hash);
56  });
57  assert(out == out2);
58 }
A class representing MuHash sets.
Definition: muhash.h:95
void Finalize(uint256 &out) noexcept
Definition: muhash.cpp:313
MuHash3072 & Remove(Span< const unsigned char > in) noexcept
Definition: muhash.cpp:343
MuHash3072 & Insert(Span< const unsigned char > in) noexcept
Definition: muhash.cpp:338
256-bit opaque blob.
Definition: uint256.h:119
FUZZ_TARGET(muhash)
Definition: muhash.cpp:12
std::vector< uint8_t > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:110
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:89
uint256 uint256S(const char *str)
Definition: uint256.h:132
assert(!tx.IsCoinBase())