Skip to content

Commit ae4b695

Browse files
committed
Fuzz tests for CKey->EllSwift
1 parent 697a237 commit ae4b695

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/test/fuzz/key.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@
1515
#include <script/sign.h>
1616
#include <script/signingprovider.h>
1717
#include <script/standard.h>
18+
#include <secp256k1.h>
19+
#include <secp256k1_ellswift.h>
1820
#include <streams.h>
21+
#include <test/fuzz/FuzzedDataProvider.h>
1922
#include <test/fuzz/fuzz.h>
2023
#include <util/strencodings.h>
2124

25+
#include <array>
2226
#include <cassert>
2327
#include <cstdint>
2428
#include <numeric>
@@ -303,3 +307,40 @@ FUZZ_TARGET_INIT(key, initialize_key)
303307
}
304308
}
305309
}
310+
311+
CPubKey EllSwiftDecode(const EllSwiftPubKey& encoded_pubkey)
312+
{
313+
secp256k1_pubkey pubkey;
314+
secp256k1_ellswift_decode(secp256k1_context_static, &pubkey, reinterpret_cast<const unsigned char*>(encoded_pubkey.data()));
315+
316+
size_t sz = CPubKey::COMPRESSED_SIZE;
317+
std::array<uint8_t, CPubKey::COMPRESSED_SIZE> vch_bytes;
318+
319+
secp256k1_ec_pubkey_serialize(secp256k1_context_static, vch_bytes.data(), &sz, &pubkey, SECP256K1_EC_COMPRESSED);
320+
321+
return CPubKey{vch_bytes.begin(), vch_bytes.end()};
322+
}
323+
324+
FUZZ_TARGET_INIT(ellswift, initialize_key)
325+
{
326+
FuzzedDataProvider fdp{buffer.data(), buffer.size()};
327+
auto key_bytes = fdp.ConsumeBytes<uint8_t>(32);
328+
key_bytes.resize(32);
329+
CKey key;
330+
key.Set(key_bytes.begin(), key_bytes.end(), fdp.ConsumeBool());
331+
if (!key.IsValid()) {
332+
return;
333+
}
334+
335+
auto r32_vec = fdp.ConsumeBytes<std::byte>(32);
336+
r32_vec.resize(32);
337+
std::array<std::byte, 32> rnd32;
338+
memcpy(rnd32.data(), r32_vec.data(), r32_vec.size());
339+
340+
auto encoded_ellswift = key.EllSwiftEncode(rnd32);
341+
auto decoded_pubkey = EllSwiftDecode(encoded_ellswift);
342+
if (!key.IsCompressed()) {
343+
decoded_pubkey.Decompress();
344+
}
345+
assert(key.VerifyPubKey(decoded_pubkey));
346+
}

0 commit comments

Comments
 (0)