Bitcoin Core  27.99.0
P2P Digital Currency
Go to the documentation of this file.
1 // Copyright (c) 2020 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or
5 #include <chainparams.h>
6 #include <key_io.h>
8 #include <test/fuzz/fuzz.h>
9 #include <test/fuzz/util.h>
10 #include <util/chaintype.h>
11 #include <util/message.h>
12 #include <util/strencodings.h>
14 #include <cassert>
15 #include <cstdint>
16 #include <iostream>
17 #include <string>
18 #include <vector>
21 {
22  static ECC_Context ecc_context{};
24 }
27 {
28  FuzzedDataProvider fuzzed_data_provider(, buffer.size());
29  const std::string random_message = fuzzed_data_provider.ConsumeRandomLengthString(1024);
30  {
31  CKey private_key = ConsumePrivateKey(fuzzed_data_provider);
32  std::string signature;
33  const bool message_signed = MessageSign(private_key, random_message, signature);
34  if (private_key.IsValid()) {
35  assert(message_signed);
36  const MessageVerificationResult verification_result = MessageVerify(EncodeDestination(PKHash(private_key.GetPubKey().GetID())), signature, random_message);
37  assert(verification_result == MessageVerificationResult::OK);
38  }
39  }
40  {
41  (void)MessageHash(random_message);
42  (void)MessageVerify(fuzzed_data_provider.ConsumeRandomLengthString(1024), fuzzed_data_provider.ConsumeRandomLengthString(1024), random_message);
44  }
45 }
ECC_Context ecc_context
void SelectParams(const ChainType chain)
Sets the params returned by Params() to those for the given chain type.
An encapsulated private key.
Definition: key.h:33
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:119
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:188
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:164
RAII class initializing and deinitializing global state for elliptic curve support.
Definition: key.h:250
std::string ConsumeRandomLengthString(size_t max_length)
T PickValueInArray(const T(&array)[size])
std::string EncodeDestination(const CTxDestination &dest)
Definition: key_io.cpp:287
@ OK
No error.
The result of a signed message verification.
Definition: message.h:23
@ OK
The message verification was successful.
void initialize_message()
Definition: message.cpp:20
Definition: message.cpp:26
CKey ConsumePrivateKey(FuzzedDataProvider &fuzzed_data_provider, std::optional< bool > compressed) noexcept
Definition: util.cpp:227
uint256 MessageHash(const std::string &message)
Hashes a message for signing and verification in a manner that prevents inadvertently signing a trans...
Definition: message.cpp:73
bool MessageSign(const CKey &privkey, const std::string &message, std::string &signature)
Sign a message.
Definition: message.cpp:57
std::string SigningResultString(const SigningResult res)
Definition: message.cpp:81
MessageVerificationResult MessageVerify(const std::string &address, const std::string &signature, const std::string &message)
Verify a signed message.
Definition: message.cpp:26