Line data Source code
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 4 : FUZZ_TARGET(muhash) 13 : { 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 : const std::string initial_state_hash{"dd5ad2a105c2d29495f577245c357409002329b9f4d6182c0af3dc2f462555c8"}; 24 0 : uint256 out; 25 2 : uint256 out2; 26 0 : CallOneOf( 27 : 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 0 : muhash /= muhash; 47 0 : muhash.Finalize(out); 48 0 : out2 = uint256S(initial_state_hash); 49 0 : }, 50 0 : [&] { 51 : // Test that removing all added elements brings the object back to it's initial state 52 0 : muhash.Remove(data); 53 0 : muhash.Remove(data2); 54 0 : muhash.Finalize(out); 55 0 : out2 = uint256S(initial_state_hash); 56 0 : }); 57 0 : assert(out == out2); 58 0 : }