20 const unsigned char *end = privkey + privkeylen;
25 if (end < privkey+1 || *privkey != 0x30) {
30 if (end < privkey+1 || !(*privkey & 0x80)) {
33 lenb = *privkey & ~0x80; privkey++;
34 if (lenb < 1 || lenb > 2) {
37 if (end < privkey+lenb) {
41 len = privkey[lenb-1] | (lenb > 1 ? privkey[lenb-2] << 8 : 0);
43 if (end < privkey+len) {
47 if (end < privkey+3 || privkey[0] != 0x02 || privkey[1] != 0x01 || privkey[2] != 0x01) {
52 if (end < privkey+2 || privkey[0] != 0x04 || privkey[1] > 0x20 || end < privkey+2+privkey[1]) {
55 memcpy(out32 + 32 - privkey[1], privkey + 2, privkey[1]);
71 static const unsigned char begin[] = {
72 0x30,0x81,0xD3,0x02,0x01,0x01,0x04,0x20
74 static const unsigned char middle[] = {
75 0xA0,0x81,0x85,0x30,0x81,0x82,0x02,0x01,0x01,0x30,0x2C,0x06,0x07,0x2A,0x86,0x48,
76 0xCE,0x3D,0x01,0x01,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
77 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
78 0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F,0x30,0x06,0x04,0x01,0x00,0x04,0x01,0x07,0x04,
79 0x21,0x02,0x79,0xBE,0x66,0x7E,0xF9,0xDC,0xBB,0xAC,0x55,0xA0,0x62,0x95,0xCE,0x87,
80 0x0B,0x07,0x02,0x9B,0xFC,0xDB,0x2D,0xCE,0x28,0xD9,0x59,0xF2,0x81,0x5B,0x16,0xF8,
81 0x17,0x98,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
82 0xFF,0xFF,0xFF,0xFF,0xFE,0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,0xBF,0xD2,0x5E,
83 0x8C,0xD0,0x36,0x41,0x41,0x02,0x01,0x01,0xA1,0x24,0x03,0x22,0x00
85 unsigned char *ptr = privkey;
86 memcpy(ptr, begin,
sizeof(begin)); ptr +=
sizeof(begin);
87 memcpy(ptr, key32, 32); ptr += 32;
88 memcpy(ptr, middle,
sizeof(middle)); ptr +=
sizeof(middle);
92 *privkeylen = ptr - privkey;
94 static const unsigned char begin[] = {
95 0x30,0x82,0x01,0x13,0x02,0x01,0x01,0x04,0x20
97 static const unsigned char middle[] = {
98 0xA0,0x81,0xA5,0x30,0x81,0xA2,0x02,0x01,0x01,0x30,0x2C,0x06,0x07,0x2A,0x86,0x48,
99 0xCE,0x3D,0x01,0x01,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
100 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
101 0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F,0x30,0x06,0x04,0x01,0x00,0x04,0x01,0x07,0x04,
102 0x41,0x04,0x79,0xBE,0x66,0x7E,0xF9,0xDC,0xBB,0xAC,0x55,0xA0,0x62,0x95,0xCE,0x87,
103 0x0B,0x07,0x02,0x9B,0xFC,0xDB,0x2D,0xCE,0x28,0xD9,0x59,0xF2,0x81,0x5B,0x16,0xF8,
104 0x17,0x98,0x48,0x3A,0xDA,0x77,0x26,0xA3,0xC4,0x65,0x5D,0xA4,0xFB,0xFC,0x0E,0x11,
105 0x08,0xA8,0xFD,0x17,0xB4,0x48,0xA6,0x85,0x54,0x19,0x9C,0x47,0xD0,0x8F,0xFB,0x10,
106 0xD4,0xB8,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
107 0xFF,0xFF,0xFF,0xFF,0xFE,0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,0xBF,0xD2,0x5E,
108 0x8C,0xD0,0x36,0x41,0x41,0x02,0x01,0x01,0xA1,0x44,0x03,0x42,0x00
110 unsigned char *ptr = privkey;
111 memcpy(ptr, begin,
sizeof(begin)); ptr +=
sizeof(begin);
112 memcpy(ptr, key32, 32); ptr += 32;
113 memcpy(ptr, middle,
sizeof(middle)); ptr +=
sizeof(middle);
117 *privkeylen = ptr - privkey;
152 privkey.resize(privkeylen);
164 assert(
result.size() == clen);
169 bool CKey::Sign(
const uint256 &hash, std::vector<unsigned char>& vchSig, uint32_t test_case)
const {
174 unsigned char extra_entropy[32] = {0};
180 vchSig.resize(nSigLen);
188 unsigned char rnd[8];
189 std::string str =
"Bitcoin key verification\n";
192 CHash256().
Write((
unsigned char*)str.data(), str.size()).Write(rnd,
sizeof(rnd)).Finalize(hash.
begin());
193 std::vector<unsigned char> vchSig;
195 return pubkey.
Verify(hash, vchSig);
228 unsigned char out[64];
230 if ((nChild >> 31) == 0) {
232 assert(pubkey.
begin() + 33 == pubkey.
end());
256 static const unsigned char hashkey[] = {
'B',
'i',
't',
'c',
'o',
'i',
'n',
' ',
's',
'e',
'e',
'd'};
257 unsigned char out[64];
281 code[5] = (
nChild >> 24) & 0xFF; code[6] = (
nChild >> 16) & 0xFF;
282 code[7] = (
nChild >> 8) & 0xFF; code[8] = (
nChild >> 0) & 0xFF;
292 nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8];
294 key.
Set(code+42, code+74,
true);
299 key.MakeNewKey(
true);
301 return key.VerifyPubKey(pubkey);
312 unsigned char seed[32];
void SetMaster(const unsigned char *seed, unsigned int nSeedLen)
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
bool SetPrivKey(const CPrivKey &vchPrivKey, bool fCompressed)
Initialize from a CPrivKey (serialized OpenSSL private key data).
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(secp256k1_context *ctx, const unsigned char *seed32) SECP256K1_ARG_NONNULL(1)
void RandAddSeedPerfmon()
bool fCompressed
Whether the public key corresponding to this private key is (to be) compressed.
bool Derive(CExtKey &out, unsigned int nChild) const
static void WriteLE32(unsigned char *ptr, uint32_t x)
bool VerifyPubKey(const CPubKey &vchPubKey) const
CHMAC_SHA512 & Write(const unsigned char *data, size_t len)
static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *privkey, size_t *privkeylen, const unsigned char *key32, int compressed)
bool Sign(const uint256 &hash, std::vector< unsigned char > &vchSig, uint32_t test_case=0) const
SECP256K1_API int secp256k1_ec_pubkey_serialize(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey *pubkey, unsigned int flags) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
#define SECP256K1_CONTEXT_SIGN
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_add(const secp256k1_context *ctx, unsigned char *seckey, const unsigned char *tweak) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
void UnlockObject(const T &t)
CPrivKey GetPrivKey() const
SECP256K1_API const secp256k1_nonce_function secp256k1_nonce_function_rfc6979
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx)
bool Derive(CKey &keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc) const
Derive BIP32 child key.
void Encode(unsigned char code[74]) const
bool Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck)
Load private key and check that public key matches.
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
#define SECP256K1_EC_UNCOMPRESSED
#define SECP256K1_EC_COMPRESSED
const unsigned char * begin() const
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_verify(const secp256k1_context *ctx, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2)
CExtPubKey Neuter() const
unsigned char vchFingerprint[4]
void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
static secp256k1_context * ctx
SECP256K1_API int secp256k1_ecdsa_sign(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sig, const unsigned char *msg32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void *ndata) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
SECP256K1_API int secp256k1_ecdsa_sign_recoverable(const secp256k1_context *ctx, secp256k1_ecdsa_recoverable_signature *sig, const unsigned char *msg32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void *ndata) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
unsigned char vchFingerprint[4]
SECP256K1_API int secp256k1_ecdsa_recoverable_signature_serialize_compact(const secp256k1_context *ctx, unsigned char *output64, int *recid, const secp256k1_ecdsa_recoverable_signature *sig) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
static int ec_privkey_import_der(const secp256k1_context *ctx, unsigned char *out32, const unsigned char *privkey, size_t privkeylen)
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
const unsigned char * end() const
void Decode(const unsigned char code[74])
const unsigned char * begin() const
static secp256k1_context * secp256k1_context_sign
static bool Check(const unsigned char *vch)
Check whether the 32-byte array pointed to be vch is valid keydata.
void Finalize(unsigned char hash[OUTPUT_SIZE])
SECP256K1_API int secp256k1_ecdsa_signature_serialize_der(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_ecdsa_signature *sig) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
bool IsCompressed() const
Check whether this is a compressed public key.
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
void * memcpy(void *a, const void *b, size_t c)
const unsigned char * end() const
CPubKey GetPubKey() const
unsigned char vch[32]
The actual byte data.
void GetRandBytes(unsigned char *buf, int num)
unsigned int size() const
Simple read-only vector-like interface.
bool IsValid() const
Check whether this private key is valid.
bool ECC_InitSanityCheck()
bool SignCompact(const uint256 &hash, std::vector< unsigned char > &vchSig) const
SECP256K1_API secp256k1_context * secp256k1_context_create(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
void LockObject(const T &t)
CHash256 & Write(const unsigned char *data, size_t len)