Bitcoin Core
30.99.0
P2P Digital Currency
src
test
fuzz
message.cpp
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 http://www.opensource.org/licenses/mit-license.php.
4
5
#include <chainparams.h>
6
#include <
common/signmessage.h
>
7
#include <
key_io.h
>
8
#include <
test/fuzz/FuzzedDataProvider.h
>
9
#include <
test/fuzz/fuzz.h
>
10
#include <
test/fuzz/util.h
>
11
#include <
util/chaintype.h
>
12
#include <
util/strencodings.h
>
13
14
#include <cassert>
15
#include <cstdint>
16
#include <iostream>
17
#include <string>
18
#include <vector>
19
20
void
initialize_message
()
21
{
22
static
ECC_Context
ecc_context
{};
23
SelectParams
(
ChainType::REGTEST
);
24
}
25
26
FUZZ_TARGET
(message, .
init
=
initialize_message
)
27
{
28
FuzzedDataProvider
fuzzed_data_provider
(buffer.data(), 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
auto
address =
fuzzed_data_provider
.
ConsumeRandomLengthString
(1024);
43
auto
signature =
fuzzed_data_provider
.
ConsumeRandomLengthString
(1024);
44
(void)
MessageVerify
(address, signature, random_message);
45
(void)
SigningResultString
(
fuzzed_data_provider
.
PickValueInArray
({
SigningResult::OK
,
SigningResult::PRIVATE_KEY_NOT_AVAILABLE
,
SigningResult::SIGNING_FAILED
}));
46
}
47
}
FuzzedDataProvider.h
ecc_context
ECC_Context ecc_context
Definition:
bitcoin-wallet.cpp:126
SelectParams
void SelectParams(const ChainType chain)
Sets the params returned by Params() to those for the given chain type.
Definition:
chainparams.cpp:140
chaintype.h
ChainType::REGTEST
@ REGTEST
CKey
An encapsulated private key.
Definition:
key.h:36
ECC_Context
RAII class initializing and deinitializing global state for elliptic curve support.
Definition:
key.h:326
FuzzedDataProvider
Definition:
FuzzedDataProvider.h:32
FuzzedDataProvider::ConsumeRandomLengthString
std::string ConsumeRandomLengthString(size_t max_length)
Definition:
FuzzedDataProvider.h:153
FuzzedDataProvider::PickValueInArray
T PickValueInArray(const T(&array)[size])
Definition:
FuzzedDataProvider.h:304
MessageHash
uint256 MessageHash(const std::string &message)
Hashes a message for signing and verification in a manner that prevents inadvertently signing a trans...
Definition:
signmessage.cpp:73
MessageSign
bool MessageSign(const CKey &privkey, const std::string &message, std::string &signature)
Sign a message.
Definition:
signmessage.cpp:57
SigningResultString
std::string SigningResultString(const SigningResult res)
Definition:
signmessage.cpp:81
MessageVerify
MessageVerificationResult MessageVerify(const std::string &address, const std::string &signature, const std::string &message)
Verify a signed message.
Definition:
signmessage.cpp:26
fuzz.h
EncodeDestination
std::string EncodeDestination(const CTxDestination &dest)
Definition:
key_io.cpp:294
key_io.h
initialize_message
void initialize_message()
Definition:
message.cpp:20
FUZZ_TARGET
FUZZ_TARGET(message,.init=initialize_message)
Definition:
message.cpp:26
init
Definition:
bitcoin-gui.cpp:17
tests_wycheproof_generate_ecdh.private_key
def private_key
Definition:
tests_wycheproof_generate_ecdh.py:92
signmessage.h
SigningResult::PRIVATE_KEY_NOT_AVAILABLE
@ PRIVATE_KEY_NOT_AVAILABLE
SigningResult::SIGNING_FAILED
@ SIGNING_FAILED
SigningResult::OK
@ OK
No error.
MessageVerificationResult
MessageVerificationResult
The result of a signed message verification.
Definition:
signmessage.h:23
MessageVerificationResult::OK
@ OK
The message verification was successful.
strencodings.h
PKHash
Definition:
addresstype.h:48
ConsumePrivateKey
CKey ConsumePrivateKey(FuzzedDataProvider &fuzzed_data_provider, std::optional< bool > compressed) noexcept
Definition:
util.cpp:230
util.h
assert
assert(!tx.IsCoinBase())
fuzzed_data_provider
FuzzedDataProvider & fuzzed_data_provider
Definition:
fees.cpp:38
Generated on Tue Nov 11 2025 20:00:31 for Bitcoin Core by
1.9.4