Skip to content

Commit 2f14e6c

Browse files
committed
Fuzz tests for CPubKey<->EllSwift
1 parent 9adf6d4 commit 2f14e6c

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/test/fuzz/key.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
#include <script/signingprovider.h>
1717
#include <script/standard.h>
1818
#include <streams.h>
19+
#include <test/fuzz/FuzzedDataProvider.h>
1920
#include <test/fuzz/fuzz.h>
2021
#include <util/strencodings.h>
2122

23+
#include <array>
2224
#include <cassert>
2325
#include <cstdint>
2426
#include <numeric>
@@ -306,3 +308,34 @@ FUZZ_TARGET_INIT(key, initialize_key)
306308
}
307309
}
308310
}
311+
312+
FUZZ_TARGET_INIT(ellswift, initialize_key)
313+
{
314+
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
315+
auto pubkey_bytes = fuzzed_data_provider.ConsumeBytes<uint8_t>(CPubKey::COMPRESSED_SIZE);
316+
pubkey_bytes.resize(CPubKey::COMPRESSED_SIZE);
317+
CPubKey pubkey(pubkey_bytes.begin(), pubkey_bytes.end());
318+
319+
if (!pubkey.IsFullyValid()) {
320+
return;
321+
}
322+
323+
auto rnd32 = fuzzed_data_provider.ConsumeBytes<uint8_t>(32);
324+
rnd32.resize(32);
325+
std::array<uint8_t, 32> rnd32_array;
326+
std::copy(rnd32.begin(), rnd32.end(), rnd32_array.begin());
327+
auto ellswift_pubkey = pubkey.EllSwiftEncode(rnd32_array);
328+
329+
// only even pubkeys can be ellswift encoded
330+
assert(ellswift_pubkey.has_value() == (pubkey.data()[0] == 0x02));
331+
332+
if (!ellswift_pubkey.has_value()) {
333+
return;
334+
}
335+
assert(ellswift_pubkey->size() == ELLSWIFT_ENCODED_SIZE);
336+
337+
CPubKey decoded_pubkey{ellswift_pubkey.value()};
338+
assert(decoded_pubkey.IsFullyValid());
339+
340+
assert(pubkey == decoded_pubkey);
341+
}

0 commit comments

Comments
 (0)