/root/bitcoin/src/test/fuzz/muhash.cpp
Line | Count | Source (jump to first uncovered line) |
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> |
6 | | #include <test/fuzz/FuzzedDataProvider.h> |
7 | | #include <test/fuzz/fuzz.h> |
8 | | #include <test/fuzz/util.h> |
9 | | |
10 | | #include <vector> |
11 | | |
12 | | FUZZ_TARGET(muhash) |
13 | 0 | { |
14 | 0 | FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; |
15 | 0 | std::vector<uint8_t> data{ConsumeRandomLengthByteVector(fuzzed_data_provider)}; |
16 | 0 | std::vector<uint8_t> data2{ConsumeRandomLengthByteVector(fuzzed_data_provider)}; |
17 | |
|
18 | 0 | MuHash3072 muhash; |
19 | |
|
20 | 0 | muhash.Insert(data); |
21 | 0 | muhash.Insert(data2); |
22 | |
|
23 | 0 | constexpr uint256 initial_state_hash{"dd5ad2a105c2d29495f577245c357409002329b9f4d6182c0af3dc2f462555c8"}; |
24 | 0 | uint256 out; |
25 | 0 | uint256 out2; |
26 | 0 | CallOneOf( |
27 | 0 | fuzzed_data_provider, |
28 | 0 | [&] { |
29 | | // Test that MuHash result is consistent independent of order of operations |
30 | 0 | muhash.Finalize(out); |
31 | |
|
32 | 0 | muhash = MuHash3072(); |
33 | 0 | muhash.Insert(data2); |
34 | 0 | muhash.Insert(data); |
35 | 0 | muhash.Finalize(out2); |
36 | 0 | }, |
37 | 0 | [&] { |
38 | | // Test that multiplication with the initial state never changes the finalized result |
39 | 0 | muhash.Finalize(out); |
40 | 0 | MuHash3072 muhash3; |
41 | 0 | muhash3 *= muhash; |
42 | 0 | muhash3.Finalize(out2); |
43 | 0 | }, |
44 | 0 | [&] { |
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 | 0 | #if defined(__clang__) |
49 | 0 | # pragma clang diagnostic push |
50 | 0 | # pragma clang diagnostic ignored "-Wself-assign-overloaded" |
51 | 0 | #endif |
52 | |
|
53 | 0 | muhash /= muhash; |
54 | |
|
55 | 0 | #if defined(__clang__) |
56 | 0 | # pragma clang diagnostic pop |
57 | 0 | #endif |
58 | |
|
59 | 0 | muhash.Finalize(out); |
60 | 0 | out2 = initial_state_hash; |
61 | 0 | }, |
62 | 0 | [&] { |
63 | | // Test that removing all added elements brings the object back to it's initial state |
64 | 0 | muhash.Remove(data); |
65 | 0 | muhash.Remove(data2); |
66 | 0 | muhash.Finalize(out); |
67 | 0 | out2 = initial_state_hash; |
68 | 0 | }); |
69 | 0 | assert(out == out2); |
70 | 0 | } |