Bitcoin Core 28.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
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 constexpr uint256 initial_state_hash{"dd5ad2a105c2d29495f577245c357409002329b9f4d6182c0af3dc2f462555c8"};
25 uint256 out2;
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
47 // See note about clang + self-assignment in test/uint256_tests.cpp
48 #if defined(__clang__)
49 # pragma clang diagnostic push
50 # pragma clang diagnostic ignored "-Wself-assign-overloaded"
51 #endif
52
53 muhash /= muhash;
54
55 #if defined(__clang__)
56 # pragma clang diagnostic pop
57 #endif
58
59 muhash.Finalize(out);
60 out2 = initial_state_hash;
61 },
62 [&] {
63 // Test that removing all added elements brings the object back to it's initial state
64 muhash.Remove(data);
65 muhash.Remove(data2);
66 muhash.Finalize(out);
67 out2 = initial_state_hash;
68 });
69 assert(out == out2);
70}
A class representing MuHash sets.
Definition: muhash.h:91
void Finalize(uint256 &out) noexcept
Definition: muhash.cpp:314
MuHash3072 & Remove(Span< const unsigned char > in) noexcept
Definition: muhash.cpp:344
MuHash3072 & Insert(Span< const unsigned char > in) noexcept
Definition: muhash.cpp:339
256-bit opaque blob.
Definition: uint256.h:190
FUZZ_TARGET(muhash)
Definition: muhash.cpp:12
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:35
std::vector< B > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:57
assert(!tx.IsCoinBase())