2021-01-25 12:23:45 +01:00
|
|
|
// Copyright (c) 2020-2021 The Bitcoin Core developers
|
2020-11-24 01:35:37 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include <crypto/muhash.h>
|
|
|
|
#include <test/fuzz/FuzzedDataProvider.h>
|
|
|
|
#include <test/fuzz/fuzz.h>
|
|
|
|
#include <test/fuzz/util.h>
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
2021-01-08 09:45:55 +01:00
|
|
|
FUZZ_TARGET(muhash)
|
2020-11-24 01:35:37 +01:00
|
|
|
{
|
|
|
|
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
|
2021-09-10 17:54:38 +02:00
|
|
|
std::vector<uint8_t> data{ConsumeRandomLengthByteVector(fuzzed_data_provider)};
|
|
|
|
std::vector<uint8_t> data2{ConsumeRandomLengthByteVector(fuzzed_data_provider)};
|
2020-11-24 01:35:37 +01:00
|
|
|
|
|
|
|
MuHash3072 muhash;
|
|
|
|
|
|
|
|
muhash.Insert(data);
|
|
|
|
muhash.Insert(data2);
|
|
|
|
|
2024-08-05 14:51:47 +02:00
|
|
|
constexpr uint256 initial_state_hash{"dd5ad2a105c2d29495f577245c357409002329b9f4d6182c0af3dc2f462555c8"};
|
2020-11-24 01:35:37 +01:00
|
|
|
uint256 out;
|
|
|
|
uint256 out2;
|
2021-09-10 17:54:38 +02:00
|
|
|
CallOneOf(
|
|
|
|
fuzzed_data_provider,
|
|
|
|
[&] {
|
|
|
|
// Test that MuHash result is consistent independent of order of operations
|
|
|
|
muhash.Finalize(out);
|
|
|
|
|
|
|
|
muhash = MuHash3072();
|
|
|
|
muhash.Insert(data2);
|
|
|
|
muhash.Insert(data);
|
|
|
|
muhash.Finalize(out2);
|
|
|
|
},
|
|
|
|
[&] {
|
|
|
|
// Test that multiplication with the initial state never changes the finalized result
|
|
|
|
muhash.Finalize(out);
|
|
|
|
MuHash3072 muhash3;
|
|
|
|
muhash3 *= muhash;
|
|
|
|
muhash3.Finalize(out2);
|
|
|
|
},
|
|
|
|
[&] {
|
|
|
|
// Test that dividing a MuHash by itself brings it back to it's initial state
|
2024-06-05 20:05:39 +00:00
|
|
|
|
|
|
|
// See note about clang + self-assignment in test/uint256_tests.cpp
|
|
|
|
#if defined(__clang__)
|
|
|
|
# pragma clang diagnostic push
|
|
|
|
# pragma clang diagnostic ignored "-Wself-assign-overloaded"
|
|
|
|
#endif
|
|
|
|
|
2021-09-10 17:54:38 +02:00
|
|
|
muhash /= muhash;
|
2024-06-05 20:05:39 +00:00
|
|
|
|
|
|
|
#if defined(__clang__)
|
|
|
|
# pragma clang diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
2021-09-10 17:54:38 +02:00
|
|
|
muhash.Finalize(out);
|
2024-08-05 14:51:47 +02:00
|
|
|
out2 = initial_state_hash;
|
2021-09-10 17:54:38 +02:00
|
|
|
},
|
|
|
|
[&] {
|
|
|
|
// Test that removing all added elements brings the object back to it's initial state
|
|
|
|
muhash.Remove(data);
|
|
|
|
muhash.Remove(data2);
|
|
|
|
muhash.Finalize(out);
|
2024-08-05 14:51:47 +02:00
|
|
|
out2 = initial_state_hash;
|
2021-09-10 17:54:38 +02:00
|
|
|
});
|
2020-11-24 01:35:37 +01:00
|
|
|
assert(out == out2);
|
|
|
|
}
|