|
15 | 15 | #include <script/sign.h>
|
16 | 16 | #include <script/signingprovider.h>
|
17 | 17 | #include <script/standard.h>
|
| 18 | +#include <secp256k1.h> |
| 19 | +#include <secp256k1_ellswift.h> |
18 | 20 | #include <streams.h>
|
| 21 | +#include <test/fuzz/FuzzedDataProvider.h> |
19 | 22 | #include <test/fuzz/fuzz.h>
|
20 | 23 | #include <util/strencodings.h>
|
21 | 24 |
|
| 25 | +#include <array> |
22 | 26 | #include <cassert>
|
23 | 27 | #include <cstdint>
|
24 | 28 | #include <numeric>
|
@@ -303,3 +307,40 @@ FUZZ_TARGET_INIT(key, initialize_key)
|
303 | 307 | }
|
304 | 308 | }
|
305 | 309 | }
|
| 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