5 #include <chainparams.h> 43 const CKey key = [&] {
45 k.Set(buffer.begin(), buffer.end(),
true);
61 assert(!(invalid_key == key));
68 CKey uncompressed_key;
69 uncompressed_key.
Set(buffer.begin(), buffer.end(),
false);
70 assert(!(uncompressed_key == key));
88 const bool ok = key.
Derive(child_key, child_chaincode, 0, random_uint256);
91 assert(!(child_key == key));
92 assert(child_chaincode != random_uint256);
115 assert(pubkey_deserialized == pubkey);
128 assert(!tx_multisig_script.IsPayToScriptHash());
129 assert(!tx_multisig_script.IsPayToWitnessScriptHash());
130 assert(!tx_multisig_script.IsPushOnly());
131 assert(!tx_multisig_script.IsUnspendable());
132 assert(tx_multisig_script.HasValidOps());
133 assert(tx_multisig_script.size() == 37);
138 assert(fillable_signing_provider.GetKeys().size() == 0);
139 assert(!fillable_signing_provider.HaveKey(pubkey.
GetID()));
141 const bool ok_add_key = fillable_signing_provider.AddKey(key);
143 assert(fillable_signing_provider.HaveKey(pubkey.
GetID()));
148 const bool ok_add_key_pubkey = fillable_signing_provider_pub.
AddKeyPubKey(key, pubkey);
149 assert(ok_add_key_pubkey);
153 const bool is_standard_tx_pubkey =
IsStandard(tx_pubkey_script, which_type_tx_pubkey);
154 assert(is_standard_tx_pubkey);
158 const bool is_standard_tx_multisig =
IsStandard(tx_multisig_script, which_type_tx_multisig);
159 assert(is_standard_tx_multisig);
162 std::vector<std::vector<unsigned char>> v_solutions_ret_tx_pubkey;
163 const TxoutType outtype_tx_pubkey =
Solver(tx_pubkey_script, v_solutions_ret_tx_pubkey);
165 assert(v_solutions_ret_tx_pubkey.size() == 1);
166 assert(v_solutions_ret_tx_pubkey[0].size() == 33);
168 std::vector<std::vector<unsigned char>> v_solutions_ret_tx_multisig;
169 const TxoutType outtype_tx_multisig =
Solver(tx_multisig_script, v_solutions_ret_tx_multisig);
171 assert(v_solutions_ret_tx_multisig.size() == 3);
172 assert(v_solutions_ret_tx_multisig[0].size() == 1);
173 assert(v_solutions_ret_tx_multisig[1].size() == 33);
174 assert(v_solutions_ret_tx_multisig[2].size() == 1);
180 assert(
PKHash{pubkey} == *std::get_if<PKHash>(&tx_destination));
194 const bool ok_get_pubkey = fillable_signing_provider.GetPubKey(key_id, pubkey_out);
198 const bool ok_get_key = fillable_signing_provider.GetKey(key_id, key_out);
200 assert(fillable_signing_provider.GetKeys().size() == 1);
201 assert(fillable_signing_provider.HaveKey(key_id));
204 const bool ok_get_key_origin = fillable_signing_provider.GetKeyOrigin(key_id, key_origin_info);
205 assert(!ok_get_key_origin);
209 const std::vector<unsigned char> vch_pubkey{pubkey.
begin(), pubkey.
end()};
213 const CPubKey pubkey_ctor_1{vch_pubkey};
214 assert(pubkey == pubkey_ctor_1);
216 const CPubKey pubkey_ctor_2{vch_pubkey.
begin(), vch_pubkey.end()};
217 assert(pubkey == pubkey_ctor_2);
220 pubkey_set.
Set(vch_pubkey.begin(), vch_pubkey.end());
221 assert(pubkey == pubkey_set);
225 const CPubKey invalid_pubkey{};
226 assert(!invalid_pubkey.IsValid());
227 assert(!invalid_pubkey.IsFullyValid());
228 assert(!(pubkey == invalid_pubkey));
229 assert(pubkey != invalid_pubkey);
230 assert(pubkey < invalid_pubkey);
235 unsigned int sum = 0;
236 for (
size_t i = 0; i < pubkey.
size(); ++i) {
243 CPubKey decompressed_pubkey = pubkey;
246 const bool ok = decompressed_pubkey.
Decompress();
253 std::vector<unsigned char> vch_sig;
254 const bool ok = key.
Sign(random_uint256, vch_sig,
false);
259 const std::vector<unsigned char> vch_invalid_sig{vch_sig.begin(), vch_sig.begin() + vch_sig.size() - 1};
260 assert(!pubkey.
Verify(random_uint256, vch_invalid_sig));
265 std::vector<unsigned char> vch_compact_sig;
266 const bool ok_sign_compact = key.
SignCompact(random_uint256, vch_compact_sig);
270 const bool ok_recover_compact = recover_pubkey.
RecoverCompact(random_uint256, vch_compact_sig);
271 assert(ok_recover_compact);
272 assert(recover_pubkey == pubkey);
278 const bool ok = pubkey.
Derive(child_pubkey, child_chaincode, 0, random_uint256);
280 assert(child_pubkey != pubkey);
285 assert(child_chaincode != random_uint256);
291 for (
const bool skip_check : {
true,
false}) {
293 const bool ok = loaded_key.
Load(priv_key, pubkey, skip_check);
295 assert(key == loaded_key);
307 auto ent32 = fdp.ConsumeBytes<std::byte>(32);
311 auto decoded_pubkey = encoded_ellswift.
Decode();
314 std::vector<unsigned char> sig;
316 assert(decoded_pubkey.Verify(hash, sig));
332 auto ent32_1 = fdp.ConsumeBytes<std::byte>(32);
337 auto ent32_2 = fdp.ConsumeBytes<std::byte>(32);
342 auto ent32_2_bad = fdp.ConsumeBytes<std::byte>(32);
343 ent32_2_bad.resize(32);
345 assert((ent32_2_bad == ent32_2) == (k2_ellswift_bad == k2_ellswift));
348 bool initiating = fdp.ConsumeBool();
355 assert(ecdh_secret_1 == ecdh_secret_2);
357 if (k1_ellswift != k2_ellswift) {
360 assert(ecdh_secret_bad != ecdh_secret_1);
363 if (k2_ellswift_bad != k2_ellswift) {
366 assert(ecdh_secret_bad != ecdh_secret_1);
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
CPrivKey GetPrivKey() const
Convert the private key to a CPrivKey (serialized OpenSSL private key data).
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination corresponds to one with an address.
RAII class initializing and deinitializing global state for elliptic curve support.
bool IsPayToScriptHash() const
bool VerifyPubKey(const CPubKey &vchPubKey) const
Verify thoroughly whether a private key and a public key match.
CPubKey GetPubKey() const
Compute the public key from a private key.
TxoutType Solver(const CScript &scriptPubKey, std::vector< std::vector< unsigned char >> &vSolutionsRet)
Parse a scriptPubKey and identify script type for standard scripts.
void Set(const T pbegin, const T pend)
Initialize a public key using begin/end iterators to byte data.
uint256 GetHash() const
Get the 256-bit hash of this public key.
CPubKey HexToPubKey(const std::string &hex_in)
static bool CheckLowS(const std::vector< unsigned char > &vchSig)
Check whether a signature is normalized (lower-S).
CKeyID GetKeyForDestination(const SigningProvider &store, const CTxDestination &dest)
Return the CKeyID of the key involved in a script (if there is a unique one).
const std::byte * end() const
void Unserialize(Stream &s)
bool HasValidOps() const
Check if the script contains valid OP_CODES.
ECDHSecret ComputeBIP324ECDHSecret(const EllSwiftPubKey &their_ellswift, const EllSwiftPubKey &our_ellswift, bool initiating) const
Compute a BIP324-style ECDH shared secret.
bool IsSegWitOutput(const SigningProvider &provider, const CScript &script)
Check whether a scriptPubKey is known to be segwit.
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
const unsigned char * begin() const
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack...
bool SignCompact(const uint256 &hash, std::vector< unsigned char > &vchSig) const
Create a compact signature (65 bytes), which allows reconstructing the used public key...
void Serialize(Stream &s) const
Implement serialization, as if this was a byte vector.
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
CPrivKey is a serialized private key, with all parameters included (SIZE bytes)
bool Sign(const uint256 &hash, std::vector< unsigned char > &vchSig, bool grind=true, uint32_t test_case=0) const
Create a DER-serialized signature.
static bool ValidSize(const std::vector< unsigned char > &vch)
bool IsFullyValid() const
fully validate whether this is a valid public key (more expensive than IsValid()) ...
const unsigned char * end() const
bool IsPushOnly(const_iterator pc) const
Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical).
Double ended buffer combining vector and stream-like interfaces.
bool RecoverCompact(const uint256 &hash, const std::vector< unsigned char > &vchSig)
Recover a public key from a compact signature.
bool Derive(CPubKey &pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc, uint256 *bip32_tweak_out=nullptr) const
Derive BIP32 child pubkey.
An encapsulated public key.
Fillable signing provider that keeps keys in an address->secret map.
unsigned int size() const
Simple read-only vector-like interface.
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
constexpr bool IsNull() const
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
EllSwiftPubKey EllSwiftCreate(std::span< const std::byte > entropy) const
Create an ellswift-encoded public key for this key, with specified entropy.
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
Verify a DER signature (~72 bytes).
bool Derive(CKey &keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc) const
Derive BIP32 child key.
Serialized script, used inside transaction inputs and outputs.
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
const std::byte * begin() const
A reference to a CKey: the Hash160 of its serialized public key.
FUZZ_TARGET(key,.init=initialize_key)
std::string EncodeDestination(const CTxDestination &dest)
CScript GetScriptForRawPubKey(const CPubKey &pubKey)
Generate a P2PK script for the given pubkey.
virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
const unsigned char * data() const
An encapsulated private key.
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
CKey DecodeSecret(const std::string &str)
Seed with a compile time constant of zeros.
CTxDestination DecodeDestination(const std::string &str, std::string &error_msg, std::vector< int > *error_locations)
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
std::string EncodeSecret(const CKey &key)
CPubKey Decode() const
Decode to normal compressed CPubKey (for debugging purposes).
bool IsStandard(const CScript &scriptPubKey, TxoutType &whichType)
std::string HexStr(const std::span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
void SelectParams(const ChainType chain)
Sets the params returned by Params() to those for the given chain type.
CKey ConsumePrivateKey(FuzzedDataProvider &fuzzed_data_provider, std::optional< bool > compressed) noexcept
bool Decompress()
Turn this public key into an uncompressed public key.
bool IsPayToWitnessScriptHash() const
bool Load(const CPrivKey &privkey, const CPubKey &vchPubKey, bool fSkipCheck)
Load private key and check that public key matches.
bool IsValid() const
Check whether this private key is valid.
bool IsCompressed() const
Check whether this is a compressed public key.
virtual bool HaveKey(const CKeyID &address) const override