Skip to content

Commit 01297fb

Browse files
committed
fuzz: Add MuHash consistency fuzz test
1 parent b111410 commit 01297fb

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ test_fuzz_fuzz_SOURCES = \
229229
test/fuzz/locale.cpp \
230230
test/fuzz/merkleblock.cpp \
231231
test/fuzz/message.cpp \
232+
test/fuzz/muhash.cpp \
232233
test/fuzz/multiplication_overflow.cpp \
233234
test/fuzz/net.cpp \
234235
test/fuzz/net_permissions.cpp \

src/test/fuzz/muhash.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 <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+
void test_one_input(const std::vector<uint8_t>& buffer)
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+
45+
// Test that removing all added elements brings the object back to it's initial state
46+
muhash /= muhash;
47+
muhash.Finalize(out);
48+
49+
MuHash3072 muhash2;
50+
muhash2.Finalize(out2);
51+
52+
assert(out == out2);
53+
}

0 commit comments

Comments
 (0)