Files

184 lines
6.3 KiB
C++
Raw Permalink Normal View History

2017-11-09 19:34:51 +01:00
/*
* This file is part of the Flowee project
* Copyright (C) 2009-2010 Satoshi Nakamoto
* Copyright (C) 2009-2015 The Bitcoin Core developers
2024-05-01 16:00:05 +02:00
* Copyright (C) 2022-2024 Tom Zander <tom@flowee.org>
2017-11-09 19:34:51 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2013-04-13 00:13:08 -05:00
2023-11-24 18:16:32 +01:00
#ifndef FLOWEE_PRIVATEKEY_H
#define FLOWEE_PRIVATEKEY_H
2010-07-14 15:54:31 +00:00
2023-11-24 18:01:36 +01:00
#include "primitives/PublicKey.h"
2015-01-22 15:02:44 -05:00
#include "support/allocators/secure.h"
2011-06-26 16:11:56 +02:00
#include "uint256.h"
2013-04-13 00:13:08 -05:00
#include <vector>
2011-06-26 16:11:56 +02:00
/**
* secp256k1:
* const unsigned int PRIVATE_KEY_SIZE = 279;
* const unsigned int PUBLIC_KEY_SIZE = 65;
* const unsigned int SIGNATURE_SIZE = 72;
*
* see www.keylength.com
* script supports up to 75 for single byte push
*/
2010-07-14 15:54:31 +00:00
/**
* secure_allocator is defined in allocators.h
* CPrivKey is a serialized private key, with all parameters included (279 bytes)
*/
2011-05-15 09:11:04 +02:00
typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
2010-07-14 15:54:31 +00:00
2013-05-01 06:52:05 +02:00
/** An encapsulated private key. */
2022-07-06 22:12:33 +02:00
class PrivateKey
{
2013-05-01 06:52:05 +02:00
private:
//! Whether this private key is valid. We check for correctness when modifying the key
//! data, so fValid should always correspond to the actual state.
2013-05-01 06:52:05 +02:00
bool fValid;
2011-11-21 02:46:28 +01:00
//! Whether the public key corresponding to this private key is (to be) compressed.
2013-05-01 06:52:05 +02:00
bool fCompressed;
//! The actual byte data
2022-04-06 10:24:16 +02:00
std::vector<uint8_t, secure_allocator<uint8_t>> keydata;
2013-05-01 06:52:05 +02:00
//! Check whether the 32-byte array pointed to be vch is valid keydata.
2023-12-05 17:10:49 +01:00
bool static check(const uint8_t *vch);
2011-11-21 02:46:28 +01:00
public:
//! Construct an invalid private key.
2022-07-06 22:12:33 +02:00
PrivateKey() : fValid(false), fCompressed(false)
{
2022-04-06 10:24:16 +02:00
keydata.resize(32);
2013-05-01 06:52:05 +02:00
}
2010-07-14 15:54:31 +00:00
2022-07-06 22:12:33 +02:00
PrivateKey(const PrivateKey &other) = default;
2022-07-06 14:10:23 +02:00
2024-05-01 16:00:05 +02:00
/// Create a privatekey object from wif-encoded (base58) private key.
static PrivateKey fromBase58(const std::string &wif);
2022-07-06 22:12:33 +02:00
friend bool operator==(const PrivateKey& a, const PrivateKey& b)
{
return a.fCompressed == b.fCompressed && a.size() == b.size() &&
2022-04-06 10:24:16 +02:00
memcmp(a.keydata.data(), b.keydata.data(), a.size()) == 0;
2013-07-15 01:05:25 +02:00
}
//! Initialize using begin and end iterators to byte data.
template <typename T>
2022-05-11 13:12:33 +02:00
void set(const T pbegin, const T pend, bool compressed = true)
{
2022-04-06 10:24:16 +02:00
assert(keydata.size() == 32);
2013-05-01 06:52:05 +02:00
if (pend - pbegin != 32) {
fValid = false;
}
2022-07-06 22:12:33 +02:00
else if (check(&pbegin[0])) {
2022-04-06 10:24:16 +02:00
memcpy(keydata.data(), (unsigned char*)&pbegin[0], 32);
2013-05-01 06:52:05 +02:00
fValid = true;
2022-05-11 13:12:33 +02:00
fCompressed = compressed;
2013-05-01 06:52:05 +02:00
} else {
fValid = false;
}
2022-05-11 13:12:33 +02:00
if (!fValid) // Be internally consistent, remove the old data
std::memset(keydata.data(), 0, 32);
2013-05-01 06:52:05 +02:00
}
2010-07-14 15:54:31 +00:00
2023-12-05 17:10:49 +01:00
inline void set(const Streaming::ConstBuffer &data) {
set(reinterpret_cast<const uint8_t*>(data.begin()),
reinterpret_cast<const uint8_t*>(data.end()), true);
}
//! Simple read-only vector-like interface.
2013-05-01 06:52:05 +02:00
unsigned int size() const { return (fValid ? 32 : 0); }
2022-04-06 10:24:16 +02:00
const unsigned char* begin() const { return keydata.data(); }
const unsigned char* end() const { return keydata.data() + size(); }
2010-07-14 15:54:31 +00:00
//! Check whether this private key is valid.
2022-05-11 13:46:15 +02:00
bool isValid() const { return fValid; }
2013-05-01 06:52:05 +02:00
//! Check whether the public key corresponding to this private key is (to be) compressed.
2022-05-11 13:46:15 +02:00
bool isCompressed() const { return fCompressed; }
2013-05-01 06:52:05 +02:00
//! Initialize from a CPrivKey (serialized OpenSSL private key data).
2022-05-11 13:46:15 +02:00
bool setPrivKey(const CPrivKey& vchPrivKey, bool fCompressed);
2013-05-01 06:52:05 +02:00
//! Generate a new private key using a cryptographic PRNG.
2022-05-11 13:46:15 +02:00
void makeNewKey(bool fCompressed = true);
2013-05-01 06:52:05 +02:00
/**
* Convert the private key to a CPrivKey (serialized OpenSSL private key data).
2022-04-06 10:24:16 +02:00
* This is expensive.
*/
2022-05-11 13:46:15 +02:00
CPrivKey getPrivKey() const;
2013-05-01 06:52:05 +02:00
/**
* Compute the public key from a private key.
* This is expensive.
*/
2022-07-06 21:56:34 +02:00
PublicKey getPubKey() const;
2010-07-14 15:54:31 +00:00
2014-11-06 06:54:50 -08:00
/**
* Create a DER-serialized signature.
* The test_case parameter tweaks the deterministic nonce.
2014-11-06 06:54:50 -08:00
*/
2021-04-19 15:45:02 +02:00
bool signECDSA(const uint256& hash, std::vector<unsigned char>& vchSig, uint32_t test_case = 0) const;
2010-07-14 15:54:31 +00:00
2021-04-19 15:49:56 +02:00
/**
* Create a Schnorr signature.
* The test_case parameter tweaks the deterministic nonce.
*/
bool signSchnorr(const uint256 &hash, std::vector<uint8_t> &vchSig, uint32_t test_case = 0) const;
/**
* Create a compact signature (65 bytes), which allows reconstructing the used public key.
* The format is one header byte, followed by two times 32 bytes for the serialized r and s values.
* The header byte: 0x1B = first key with even y, 0x1C = first key with odd y,
* 0x1D = second key with even y, 0x1E = second key with odd y,
* add 0x04 for compressed keys.
*/
2022-05-11 13:46:15 +02:00
bool signCompact(const uint256& hash, std::vector<unsigned char>& vchSig) const;
2013-07-15 01:05:25 +02:00
//! Derive BIP32 child key.
2022-07-06 22:12:33 +02:00
bool derive(PrivateKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
2014-11-06 01:17:48 -08:00
/**
* Verify thoroughly whether a private key and a public key match.
* This is done using a different mechanism than just regenerating it.
*/
2022-07-06 21:56:34 +02:00
bool verifyPubKey(const PublicKey& vchPubKey) const;
2014-11-06 01:17:48 -08:00
//! Load private key and check that public key matches.
2022-07-06 21:56:34 +02:00
bool load(CPrivKey& privkey, PublicKey& vchPubKey, bool fSkipCheck);
//! Check whether an element of a signature (r or s) is valid.
2022-05-11 13:46:15 +02:00
static bool checkSignatureElement(const unsigned char* vch, int len, bool half);
2022-07-06 22:12:33 +02:00
PrivateKey &operator=(const PrivateKey &other);
2013-07-15 01:05:25 +02:00
};
2015-04-22 14:28:26 -07:00
/** Initialize the elliptic curve support. May not be called twice without calling ECC_Stop first. */
void ECC_Start(void);
/** Deinitialize the elliptic curve support. No-op if ECC_Start wasn't called first. */
void ECC_Stop(void);
/** Check that required EC support is available at runtime. */
bool ECC_InitSanityCheck(void);
2018-01-16 10:47:52 +00:00
#endif