Skip to content

Commit 1580e3b

Browse files
committed
fuzz: add ConstructPubKeyBytes function
Today, this code only has one spot where it needs well-formed pubkeys, but future PRs will want to reuse this code. Add a function which creates a well-formed byte array that can be turned into a pubkey. It is not required that the pubkey is valid, just that it can be recognized as a compressed or uncompressed pubkey. Note: while the main intent of this commit is to wrap the existing logic into a function, it also switches to `PickValueFromArray` so that we are only choosing one of 0x04, 0x06, or 0x07. The previous code, `ConsumeIntegralInRange` would have also picked 0x05, which is not definied in the context of compressed vs uncompressed keys. See https://bitcoin.stackexchange.com/questions/57855/c-secp256k1-what-do-prefixes-0x06-and-0x07-in-an-uncompressed-public-key-signif for more details.
1 parent ab42b2e commit 1580e3b

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/test/fuzz/util.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@
1414

1515
#include <memory>
1616

17+
std::vector<uint8_t> ConstructPubKeyBytes(FuzzedDataProvider& fuzzed_data_provider, Span<const uint8_t> byte_data, const bool compressed) noexcept
18+
{
19+
uint8_t pk_type;
20+
if (compressed) {
21+
pk_type = fuzzed_data_provider.PickValueInArray({0x02, 0x03});
22+
} else {
23+
pk_type = fuzzed_data_provider.PickValueInArray({0x04, 0x06, 0x07});
24+
}
25+
std::vector<uint8_t> pk_data{byte_data.begin(), byte_data.begin() + (compressed ? CPubKey::COMPRESSED_SIZE : CPubKey::SIZE)};
26+
pk_data[0] = pk_type;
27+
return pk_data;
28+
}
29+
1730
CAmount ConsumeMoney(FuzzedDataProvider& fuzzed_data_provider, const std::optional<CAmount>& max) noexcept
1831
{
1932
return fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(0, max.value_or(MAX_MONEY));
@@ -103,16 +116,12 @@ CScript ConsumeScript(FuzzedDataProvider& fuzzed_data_provider, const bool maybe
103116
// navigate the highly structured multisig format.
104117
r_script << fuzzed_data_provider.ConsumeIntegralInRange<int64_t>(0, 22);
105118
int num_data{fuzzed_data_provider.ConsumeIntegralInRange(1, 22)};
106-
std::vector<uint8_t> pubkey_comp{buffer.begin(), buffer.begin() + CPubKey::COMPRESSED_SIZE};
107-
pubkey_comp.front() = fuzzed_data_provider.ConsumeIntegralInRange(2, 3); // Set first byte for GetLen() to pass
108-
std::vector<uint8_t> pubkey_uncomp{buffer.begin(), buffer.begin() + CPubKey::SIZE};
109-
pubkey_uncomp.front() = fuzzed_data_provider.ConsumeIntegralInRange(4, 7); // Set first byte for GetLen() to pass
110119
while (num_data--) {
111-
auto& pubkey{fuzzed_data_provider.ConsumeBool() ? pubkey_uncomp : pubkey_comp};
120+
auto pubkey_bytes{ConstructPubKeyBytes(fuzzed_data_provider, buffer, fuzzed_data_provider.ConsumeBool())};
112121
if (fuzzed_data_provider.ConsumeBool()) {
113-
pubkey.back() = num_data; // Make each pubkey different
122+
pubkey_bytes.back() = num_data; // Make each pubkey different
114123
}
115-
r_script << pubkey;
124+
r_script << pubkey_bytes;
116125
}
117126
r_script << fuzzed_data_provider.ConsumeIntegralInRange<int64_t>(0, 22);
118127
},

0 commit comments

Comments
 (0)