Skip to content

Commit 0000dca

Browse files
author
MarcoFalke
committed
fuzz: Cleanup muhash fuzz target
Can be reviewed with -W --ignore-all-space Fixes: * Calling ConsumeRandomLengthByteVector 4 times, when 2 is enough. * Slow execution speed: Finalize is expensive because it invokes division. Speed up the target by calling Finalize() at most twice per fuzz input.
1 parent 053a5fc commit 0000dca

File tree

1 file changed

+34
-39
lines changed

1 file changed

+34
-39
lines changed

src/test/fuzz/muhash.cpp

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,52 +12,47 @@
1212
FUZZ_TARGET(muhash)
1313
{
1414
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);
15+
std::vector<uint8_t> data{ConsumeRandomLengthByteVector(fuzzed_data_provider)};
16+
std::vector<uint8_t> data2{ConsumeRandomLengthByteVector(fuzzed_data_provider)};
2617

2718
MuHash3072 muhash;
2819

29-
// Test that MuHash result is consistent independent of order of operations
3020
muhash.Insert(data);
3121
muhash.Insert(data2);
3222

23+
const std::string initial_state_hash{"dd5ad2a105c2d29495f577245c357409002329b9f4d6182c0af3dc2f462555c8"};
3324
uint256 out;
34-
muhash.Finalize(out);
35-
36-
muhash = MuHash3072();
37-
muhash.Insert(data2);
38-
muhash.Insert(data);
39-
4025
uint256 out2;
41-
muhash.Finalize(out2);
42-
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+
});
4357
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);
6358
}

0 commit comments

Comments
 (0)