diff --git a/src/test/fuzz/bip324.cpp b/src/test/fuzz/bip324.cpp index 276d2c6300..afe3cafafe 100644 --- a/src/test/fuzz/bip324.cpp +++ b/src/test/fuzz/bip324.cpp @@ -26,19 +26,13 @@ FUZZ_TARGET_INIT(bip324_cipher_roundtrip, initialize_bip324) // Load keys from fuzzer. FuzzedDataProvider provider(buffer.data(), buffer.size()); // Initiator key - auto init_key_data = provider.ConsumeBytes(32); - init_key_data.resize(32); - CKey init_key; - init_key.Set(init_key_data.begin(), init_key_data.end(), true); + CKey init_key = ConsumePrivateKey(provider, /*compressed=*/true); if (!init_key.IsValid()) return; // Initiator entropy auto init_ent = provider.ConsumeBytes(32); init_ent.resize(32); // Responder key - auto resp_key_data = provider.ConsumeBytes(32); - resp_key_data.resize(32); - CKey resp_key; - resp_key.Set(resp_key_data.begin(), resp_key_data.end(), true); + CKey resp_key = ConsumePrivateKey(provider, /*compressed=*/true); if (!resp_key.IsValid()) return; // Responder entropy auto resp_ent = provider.ConsumeBytes(32); diff --git a/src/test/fuzz/key.cpp b/src/test/fuzz/key.cpp index bb87b49504..549e39efee 100644 --- a/src/test/fuzz/key.cpp +++ b/src/test/fuzz/key.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -264,10 +265,7 @@ FUZZ_TARGET_INIT(ellswift_roundtrip, initialize_key) { FuzzedDataProvider fdp{buffer.data(), buffer.size()}; - auto key_bytes = fdp.ConsumeBytes(32); - key_bytes.resize(32); - CKey key; - key.Set(key_bytes.begin(), key_bytes.end(), true); + CKey key = ConsumePrivateKey(fdp, /*compressed=*/true); if (!key.IsValid()) return; auto ent32 = fdp.ConsumeBytes(32); @@ -284,17 +282,11 @@ FUZZ_TARGET_INIT(bip324_ecdh, initialize_key) FuzzedDataProvider fdp{buffer.data(), buffer.size()}; // We generate private key, k1. - auto rnd32 = fdp.ConsumeBytes(32); - rnd32.resize(32); - CKey k1; - k1.Set(rnd32.begin(), rnd32.end(), true); + CKey k1 = ConsumePrivateKey(fdp, /*compressed=*/true); if (!k1.IsValid()) return; // They generate private key, k2. - rnd32 = fdp.ConsumeBytes(32); - rnd32.resize(32); - CKey k2; - k2.Set(rnd32.begin(), rnd32.end(), true); + CKey k2 = ConsumePrivateKey(fdp, /*compressed=*/true); if (!k2.IsValid()) return; // We construct an ellswift encoding for our key, k1_ellswift. diff --git a/src/test/fuzz/message.cpp b/src/test/fuzz/message.cpp index f6ce29ad48..1b0696929b 100644 --- a/src/test/fuzz/message.cpp +++ b/src/test/fuzz/message.cpp @@ -27,9 +27,7 @@ FUZZ_TARGET_INIT(message, initialize_message) FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); const std::string random_message = fuzzed_data_provider.ConsumeRandomLengthString(1024); { - const std::vector random_bytes = ConsumeRandomLengthByteVector(fuzzed_data_provider); - CKey private_key; - private_key.Set(random_bytes.begin(), random_bytes.end(), fuzzed_data_provider.ConsumeBool()); + CKey private_key = ConsumePrivateKey(fuzzed_data_provider); std::string signature; const bool message_signed = MessageSign(private_key, random_message, signature); if (private_key.IsValid()) { diff --git a/src/test/fuzz/rpc.cpp b/src/test/fuzz/rpc.cpp index 292b355d5f..79062ef49d 100644 --- a/src/test/fuzz/rpc.cpp +++ b/src/test/fuzz/rpc.cpp @@ -274,9 +274,7 @@ std::string ConsumeScalarRPCArgument(FuzzedDataProvider& fuzzed_data_provider) }, [&] { // base58 encoded key - const std::vector random_bytes = fuzzed_data_provider.ConsumeBytes(32); - CKey key; - key.Set(random_bytes.begin(), random_bytes.end(), fuzzed_data_provider.ConsumeBool()); + CKey key = ConsumePrivateKey(fuzzed_data_provider); if (!key.IsValid()) { return; } @@ -284,9 +282,7 @@ std::string ConsumeScalarRPCArgument(FuzzedDataProvider& fuzzed_data_provider) }, [&] { // hex encoded pubkey - const std::vector random_bytes = fuzzed_data_provider.ConsumeBytes(32); - CKey key; - key.Set(random_bytes.begin(), random_bytes.end(), fuzzed_data_provider.ConsumeBool()); + CKey key = ConsumePrivateKey(fuzzed_data_provider); if (!key.IsValid()) { return; } diff --git a/src/test/fuzz/script_sign.cpp b/src/test/fuzz/script_sign.cpp index 8f2159fa61..3455124134 100644 --- a/src/test/fuzz/script_sign.cpp +++ b/src/test/fuzz/script_sign.cpp @@ -78,9 +78,7 @@ FUZZ_TARGET_INIT(script_sign, initialize_script_sign) } FillableSigningProvider provider; - CKey k; - const std::vector key_data = ConsumeRandomLengthByteVector(fuzzed_data_provider); - k.Set(key_data.begin(), key_data.end(), fuzzed_data_provider.ConsumeBool()); + CKey k = ConsumePrivateKey(fuzzed_data_provider); if (k.IsValid()) { provider.AddKey(k); } diff --git a/src/test/fuzz/util.cpp b/src/test/fuzz/util.cpp index 11e9adf2dd..c30ff2d3e9 100644 --- a/src/test/fuzz/util.cpp +++ b/src/test/fuzz/util.cpp @@ -351,3 +351,13 @@ uint32_t ConsumeSequence(FuzzedDataProvider& fuzzed_data_provider) noexcept }) : fuzzed_data_provider.ConsumeIntegral(); } + +CKey ConsumePrivateKey(FuzzedDataProvider& fuzzed_data_provider, std::optional compressed) noexcept +{ + auto key_data = fuzzed_data_provider.ConsumeBytes(32); + key_data.resize(32); + CKey key; + bool compressed_value = compressed ? *compressed : fuzzed_data_provider.ConsumeBool(); + key.Set(key_data.begin(), key_data.end(), compressed_value); + return key; +} diff --git a/src/test/fuzz/util.h b/src/test/fuzz/util.h index b248f2fc1b..e49c8130b8 100644 --- a/src/test/fuzz/util.h +++ b/src/test/fuzz/util.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -250,6 +251,8 @@ template return tx_destination; } +[[nodiscard]] CKey ConsumePrivateKey(FuzzedDataProvider& fuzzed_data_provider, std::optional compressed = std::nullopt) noexcept; + template [[nodiscard]] bool MultiplicationOverflow(const T i, const T j) noexcept {