merge bitcoin#19055: Add MuHash3072 implementation

contains:
- b111410914041b72961536c3e4037eba103a8085
- 01297fb3ca57e4b8cbc5a89fc7c6367de33b0bc6

completes:
- c7eb44a911 (from dash#4704)
This commit is contained in:
Kittywhiskers Van Gogh 2023-08-26 20:05:13 +05:30 committed by PastaPastaPasta
parent 04fabaa1bd
commit 3b25c5e84e
3 changed files with 67 additions and 0 deletions

View File

@ -278,6 +278,7 @@ test_fuzz_fuzz_SOURCES = \
test/fuzz/locale.cpp \ test/fuzz/locale.cpp \
test/fuzz/merkleblock.cpp \ test/fuzz/merkleblock.cpp \
test/fuzz/message.cpp \ test/fuzz/message.cpp \
test/fuzz/muhash.cpp \
test/fuzz/multiplication_overflow.cpp \ test/fuzz/multiplication_overflow.cpp \
test/fuzz/net.cpp \ test/fuzz/net.cpp \
test/fuzz/net_permissions.cpp \ test/fuzz/net_permissions.cpp \

View File

@ -4,6 +4,7 @@
#include <crypto/hmac_sha256.h> #include <crypto/hmac_sha256.h>
#include <crypto/hmac_sha512.h> #include <crypto/hmac_sha512.h>
#include <crypto/muhash.h>
#include <crypto/ripemd160.h> #include <crypto/ripemd160.h>
#include <crypto/sha1.h> #include <crypto/sha1.h>
#include <crypto/sha256.h> #include <crypto/sha256.h>
@ -39,6 +40,7 @@ FUZZ_TARGET(crypto)
CSHA512 sha512; CSHA512 sha512;
SHA3_256 sha3; SHA3_256 sha3;
CSipHasher sip_hasher{fuzzed_data_provider.ConsumeIntegral<uint64_t>(), fuzzed_data_provider.ConsumeIntegral<uint64_t>()}; CSipHasher sip_hasher{fuzzed_data_provider.ConsumeIntegral<uint64_t>(), fuzzed_data_provider.ConsumeIntegral<uint64_t>()};
MuHash3072 muhash;
while (--limit_max_ops >= 0 && fuzzed_data_provider.ConsumeBool()) { while (--limit_max_ops >= 0 && fuzzed_data_provider.ConsumeBool()) {
CallOneOf( CallOneOf(
@ -65,6 +67,12 @@ FUZZ_TARGET(crypto)
(void)Hash160(data); (void)Hash160(data);
(void)Hash160(data.begin(), data.end()); (void)Hash160(data.begin(), data.end());
(void)sha512.Size(); (void)sha512.Size();
if (fuzzed_data_provider.ConsumeBool()) {
muhash *= MuHash3072(data);
} else {
muhash /= MuHash3072(data);
}
}, },
[&] { [&] {
(void)hash160.Reset(); (void)hash160.Reset();
@ -74,6 +82,7 @@ FUZZ_TARGET(crypto)
(void)sha256.Reset(); (void)sha256.Reset();
(void)sha3.Reset(); (void)sha3.Reset();
(void)sha512.Reset(); (void)sha512.Reset();
muhash = MuHash3072();
}, },
[&] { [&] {
CallOneOf( CallOneOf(
@ -117,6 +126,10 @@ FUZZ_TARGET(crypto)
[&] { [&] {
data.resize(SHA3_256::OUTPUT_SIZE); data.resize(SHA3_256::OUTPUT_SIZE);
sha3.Finalize(data); sha3.Finalize(data);
},
[&] {
uint256 out;
muhash.Finalize(out);
}); });
}); });
} }

53
src/test/fuzz/muhash.cpp Normal file
View File

@ -0,0 +1,53 @@
// Copyright (c) 2020 The Bitcoin Core developers
// 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>
void test_one_input(const std::vector<uint8_t>& buffer)
{
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
std::vector<uint8_t> data = ConsumeRandomLengthByteVector(fuzzed_data_provider);
std::vector<uint8_t> data2 = ConsumeRandomLengthByteVector(fuzzed_data_provider);
if (data.empty()) {
data.resize(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 4096), fuzzed_data_provider.ConsumeIntegral<uint8_t>());
}
if (data2.empty()) {
data2.resize(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 4096), fuzzed_data_provider.ConsumeIntegral<uint8_t>());
}
data = ConsumeRandomLengthByteVector(fuzzed_data_provider);
data2 = ConsumeRandomLengthByteVector(fuzzed_data_provider);
MuHash3072 muhash;
// Test that MuHash result is consistent independent of order of operations
muhash.Insert(data);
muhash.Insert(data2);
uint256 out;
muhash.Finalize(out);
muhash = MuHash3072();
muhash.Insert(data2);
muhash.Insert(data);
uint256 out2;
muhash.Finalize(out2);
assert(out == out2);
// Test that removing all added elements brings the object back to it's initial state
muhash /= muhash;
muhash.Finalize(out);
MuHash3072 muhash2;
muhash2.Finalize(out2);
assert(out == out2);
}