Bitcoin Core  0.18.99
P2P Digital Currency
key.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2018 The Bitcoin Core developers
2 // Copyright (c) 2017 The Zcash developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include <key.h>
7 
8 #include <arith_uint256.h>
9 #include <crypto/common.h>
10 #include <crypto/hmac_sha512.h>
11 #include <random.h>
12 
13 #include <secp256k1.h>
14 #include <secp256k1_recovery.h>
15 
16 static secp256k1_context* secp256k1_context_sign = nullptr;
17 
35 static int ec_privkey_import_der(const secp256k1_context* ctx, unsigned char *out32, const unsigned char *privkey, size_t privkeylen) {
36  const unsigned char *end = privkey + privkeylen;
37  memset(out32, 0, 32);
38  /* sequence header */
39  if (end - privkey < 1 || *privkey != 0x30u) {
40  return 0;
41  }
42  privkey++;
43  /* sequence length constructor */
44  if (end - privkey < 1 || !(*privkey & 0x80u)) {
45  return 0;
46  }
47  ptrdiff_t lenb = *privkey & ~0x80u; privkey++;
48  if (lenb < 1 || lenb > 2) {
49  return 0;
50  }
51  if (end - privkey < lenb) {
52  return 0;
53  }
54  /* sequence length */
55  ptrdiff_t len = privkey[lenb-1] | (lenb > 1 ? privkey[lenb-2] << 8 : 0u);
56  privkey += lenb;
57  if (end - privkey < len) {
58  return 0;
59  }
60  /* sequence element 0: version number (=1) */
61  if (end - privkey < 3 || privkey[0] != 0x02u || privkey[1] != 0x01u || privkey[2] != 0x01u) {
62  return 0;
63  }
64  privkey += 3;
65  /* sequence element 1: octet string, up to 32 bytes */
66  if (end - privkey < 2 || privkey[0] != 0x04u) {
67  return 0;
68  }
69  ptrdiff_t oslen = privkey[1];
70  privkey += 2;
71  if (oslen > 32 || end - privkey < oslen) {
72  return 0;
73  }
74  memcpy(out32 + (32 - oslen), privkey, oslen);
75  if (!secp256k1_ec_seckey_verify(ctx, out32)) {
76  memset(out32, 0, 32);
77  return 0;
78  }
79  return 1;
80 }
81 
92 static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *privkey, size_t *privkeylen, const unsigned char *key32, bool compressed) {
93  assert(*privkeylen >= CKey::PRIVATE_KEY_SIZE);
94  secp256k1_pubkey pubkey;
95  size_t pubkeylen = 0;
96  if (!secp256k1_ec_pubkey_create(ctx, &pubkey, key32)) {
97  *privkeylen = 0;
98  return 0;
99  }
100  if (compressed) {
101  static const unsigned char begin[] = {
102  0x30,0x81,0xD3,0x02,0x01,0x01,0x04,0x20
103  };
104  static const unsigned char middle[] = {
105  0xA0,0x81,0x85,0x30,0x81,0x82,0x02,0x01,0x01,0x30,0x2C,0x06,0x07,0x2A,0x86,0x48,
106  0xCE,0x3D,0x01,0x01,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
107  0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
108  0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F,0x30,0x06,0x04,0x01,0x00,0x04,0x01,0x07,0x04,
109  0x21,0x02,0x79,0xBE,0x66,0x7E,0xF9,0xDC,0xBB,0xAC,0x55,0xA0,0x62,0x95,0xCE,0x87,
110  0x0B,0x07,0x02,0x9B,0xFC,0xDB,0x2D,0xCE,0x28,0xD9,0x59,0xF2,0x81,0x5B,0x16,0xF8,
111  0x17,0x98,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
112  0xFF,0xFF,0xFF,0xFF,0xFE,0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,0xBF,0xD2,0x5E,
113  0x8C,0xD0,0x36,0x41,0x41,0x02,0x01,0x01,0xA1,0x24,0x03,0x22,0x00
114  };
115  unsigned char *ptr = privkey;
116  memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin);
117  memcpy(ptr, key32, 32); ptr += 32;
118  memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
120  secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED);
121  ptr += pubkeylen;
122  *privkeylen = ptr - privkey;
123  assert(*privkeylen == CKey::COMPRESSED_PRIVATE_KEY_SIZE);
124  } else {
125  static const unsigned char begin[] = {
126  0x30,0x82,0x01,0x13,0x02,0x01,0x01,0x04,0x20
127  };
128  static const unsigned char middle[] = {
129  0xA0,0x81,0xA5,0x30,0x81,0xA2,0x02,0x01,0x01,0x30,0x2C,0x06,0x07,0x2A,0x86,0x48,
130  0xCE,0x3D,0x01,0x01,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
131  0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
132  0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F,0x30,0x06,0x04,0x01,0x00,0x04,0x01,0x07,0x04,
133  0x41,0x04,0x79,0xBE,0x66,0x7E,0xF9,0xDC,0xBB,0xAC,0x55,0xA0,0x62,0x95,0xCE,0x87,
134  0x0B,0x07,0x02,0x9B,0xFC,0xDB,0x2D,0xCE,0x28,0xD9,0x59,0xF2,0x81,0x5B,0x16,0xF8,
135  0x17,0x98,0x48,0x3A,0xDA,0x77,0x26,0xA3,0xC4,0x65,0x5D,0xA4,0xFB,0xFC,0x0E,0x11,
136  0x08,0xA8,0xFD,0x17,0xB4,0x48,0xA6,0x85,0x54,0x19,0x9C,0x47,0xD0,0x8F,0xFB,0x10,
137  0xD4,0xB8,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
138  0xFF,0xFF,0xFF,0xFF,0xFE,0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,0xBF,0xD2,0x5E,
139  0x8C,0xD0,0x36,0x41,0x41,0x02,0x01,0x01,0xA1,0x44,0x03,0x42,0x00
140  };
141  unsigned char *ptr = privkey;
142  memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin);
143  memcpy(ptr, key32, 32); ptr += 32;
144  memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
145  pubkeylen = CPubKey::PUBLIC_KEY_SIZE;
146  secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_UNCOMPRESSED);
147  ptr += pubkeylen;
148  *privkeylen = ptr - privkey;
149  assert(*privkeylen == CKey::PRIVATE_KEY_SIZE);
150  }
151  return 1;
152 }
153 
154 bool CKey::Check(const unsigned char *vch) {
155  return secp256k1_ec_seckey_verify(secp256k1_context_sign, vch);
156 }
157 
158 void CKey::MakeNewKey(bool fCompressedIn) {
159  do {
160  GetStrongRandBytes(keydata.data(), keydata.size());
161  } while (!Check(keydata.data()));
162  fValid = true;
163  fCompressed = fCompressedIn;
164 }
165 
167 {
168  assert(fValid);
169  return secp256k1_ec_privkey_negate(secp256k1_context_sign, keydata.data());
170 }
171 
173  assert(fValid);
174  CPrivKey privkey;
175  int ret;
176  size_t privkeylen;
177  privkey.resize(PRIVATE_KEY_SIZE);
178  privkeylen = PRIVATE_KEY_SIZE;
179  ret = ec_privkey_export_der(secp256k1_context_sign, privkey.data(), &privkeylen, begin(), fCompressed);
180  assert(ret);
181  privkey.resize(privkeylen);
182  return privkey;
183 }
184 
186  assert(fValid);
187  secp256k1_pubkey pubkey;
188  size_t clen = CPubKey::PUBLIC_KEY_SIZE;
189  CPubKey result;
190  int ret = secp256k1_ec_pubkey_create(secp256k1_context_sign, &pubkey, begin());
191  assert(ret);
192  secp256k1_ec_pubkey_serialize(secp256k1_context_sign, (unsigned char*)result.begin(), &clen, &pubkey, fCompressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED);
193  assert(result.size() == clen);
194  assert(result.IsValid());
195  return result;
196 }
197 
198 // Check that the sig has a low R value and will be less than 71 bytes
200 {
201  unsigned char compact_sig[64];
202  secp256k1_ecdsa_signature_serialize_compact(secp256k1_context_sign, compact_sig, sig);
203 
204  // In DER serialization, all values are interpreted as big-endian, signed integers. The highest bit in the integer indicates
205  // its signed-ness; 0 is positive, 1 is negative. When the value is interpreted as a negative integer, it must be converted
206  // to a positive value by prepending a 0x00 byte so that the highest bit is 0. We can avoid this prepending by ensuring that
207  // our highest bit is always 0, and thus we must check that the first byte is less than 0x80.
208  return compact_sig[0] < 0x80;
209 }
210 
211 bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, bool grind, uint32_t test_case) const {
212  if (!fValid)
213  return false;
214  vchSig.resize(CPubKey::SIGNATURE_SIZE);
215  size_t nSigLen = CPubKey::SIGNATURE_SIZE;
216  unsigned char extra_entropy[32] = {0};
217  WriteLE32(extra_entropy, test_case);
219  uint32_t counter = 0;
220  int ret = secp256k1_ecdsa_sign(secp256k1_context_sign, &sig, hash.begin(), begin(), secp256k1_nonce_function_rfc6979, (!grind && test_case) ? extra_entropy : nullptr);
221 
222  // Grind for low R
223  while (ret && !SigHasLowR(&sig) && grind) {
224  WriteLE32(extra_entropy, ++counter);
225  ret = secp256k1_ecdsa_sign(secp256k1_context_sign, &sig, hash.begin(), begin(), secp256k1_nonce_function_rfc6979, extra_entropy);
226  }
227  assert(ret);
228  secp256k1_ecdsa_signature_serialize_der(secp256k1_context_sign, vchSig.data(), &nSigLen, &sig);
229  vchSig.resize(nSigLen);
230  return true;
231 }
232 
233 bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
234  if (pubkey.IsCompressed() != fCompressed) {
235  return false;
236  }
237  unsigned char rnd[8];
238  std::string str = "Bitcoin key verification\n";
239  GetRandBytes(rnd, sizeof(rnd));
240  uint256 hash;
241  CHash256().Write((unsigned char*)str.data(), str.size()).Write(rnd, sizeof(rnd)).Finalize(hash.begin());
242  std::vector<unsigned char> vchSig;
243  Sign(hash, vchSig);
244  return pubkey.Verify(hash, vchSig);
245 }
246 
247 bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
248  if (!fValid)
249  return false;
250  vchSig.resize(CPubKey::COMPACT_SIGNATURE_SIZE);
251  int rec = -1;
253  int ret = secp256k1_ecdsa_sign_recoverable(secp256k1_context_sign, &sig, hash.begin(), begin(), secp256k1_nonce_function_rfc6979, nullptr);
254  assert(ret);
255  ret = secp256k1_ecdsa_recoverable_signature_serialize_compact(secp256k1_context_sign, &vchSig[1], &rec, &sig);
256  assert(ret);
257  assert(rec != -1);
258  vchSig[0] = 27 + rec + (fCompressed ? 4 : 0);
259  return true;
260 }
261 
262 bool CKey::Load(const CPrivKey &privkey, const CPubKey &vchPubKey, bool fSkipCheck=false) {
263  if (!ec_privkey_import_der(secp256k1_context_sign, (unsigned char*)begin(), privkey.data(), privkey.size()))
264  return false;
265  fCompressed = vchPubKey.IsCompressed();
266  fValid = true;
267 
268  if (fSkipCheck)
269  return true;
270 
271  return VerifyPubKey(vchPubKey);
272 }
273 
274 bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const {
275  assert(IsValid());
276  assert(IsCompressed());
277  std::vector<unsigned char, secure_allocator<unsigned char>> vout(64);
278  if ((nChild >> 31) == 0) {
279  CPubKey pubkey = GetPubKey();
280  assert(pubkey.size() == CPubKey::COMPRESSED_PUBLIC_KEY_SIZE);
281  BIP32Hash(cc, nChild, *pubkey.begin(), pubkey.begin()+1, vout.data());
282  } else {
283  assert(size() == 32);
284  BIP32Hash(cc, nChild, 0, begin(), vout.data());
285  }
286  memcpy(ccChild.begin(), vout.data()+32, 32);
287  memcpy((unsigned char*)keyChild.begin(), begin(), 32);
288  bool ret = secp256k1_ec_privkey_tweak_add(secp256k1_context_sign, (unsigned char*)keyChild.begin(), vout.data());
289  keyChild.fCompressed = true;
290  keyChild.fValid = ret;
291  return ret;
292 }
293 
294 bool CExtKey::Derive(CExtKey &out, unsigned int _nChild) const {
295  out.nDepth = nDepth + 1;
296  CKeyID id = key.GetPubKey().GetID();
297  memcpy(&out.vchFingerprint[0], &id, 4);
298  out.nChild = _nChild;
299  return key.Derive(out.key, out.chaincode, _nChild, chaincode);
300 }
301 
302 void CExtKey::SetSeed(const unsigned char *seed, unsigned int nSeedLen) {
303  static const unsigned char hashkey[] = {'B','i','t','c','o','i','n',' ','s','e','e','d'};
304  std::vector<unsigned char, secure_allocator<unsigned char>> vout(64);
305  CHMAC_SHA512(hashkey, sizeof(hashkey)).Write(seed, nSeedLen).Finalize(vout.data());
306  key.Set(vout.data(), vout.data() + 32, true);
307  memcpy(chaincode.begin(), vout.data() + 32, 32);
308  nDepth = 0;
309  nChild = 0;
310  memset(vchFingerprint, 0, sizeof(vchFingerprint));
311 }
312 
314  CExtPubKey ret;
315  ret.nDepth = nDepth;
316  memcpy(&ret.vchFingerprint[0], &vchFingerprint[0], 4);
317  ret.nChild = nChild;
318  ret.pubkey = key.GetPubKey();
319  ret.chaincode = chaincode;
320  return ret;
321 }
322 
323 void CExtKey::Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const {
324  code[0] = nDepth;
325  memcpy(code+1, vchFingerprint, 4);
326  code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF;
327  code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF;
328  memcpy(code+9, chaincode.begin(), 32);
329  code[41] = 0;
330  assert(key.size() == 32);
331  memcpy(code+42, key.begin(), 32);
332 }
333 
334 void CExtKey::Decode(const unsigned char code[BIP32_EXTKEY_SIZE]) {
335  nDepth = code[0];
336  memcpy(vchFingerprint, code+1, 4);
337  nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8];
338  memcpy(chaincode.begin(), code+9, 32);
339  key.Set(code+42, code+BIP32_EXTKEY_SIZE, true);
340 }
341 
343  CKey key;
344  key.MakeNewKey(true);
345  CPubKey pubkey = key.GetPubKey();
346  return key.VerifyPubKey(pubkey);
347 }
348 
349 void ECC_Start() {
350  assert(secp256k1_context_sign == nullptr);
351 
353  assert(ctx != nullptr);
354 
355  {
356  // Pass in a random blinding seed to the secp256k1 context.
357  std::vector<unsigned char, secure_allocator<unsigned char>> vseed(32);
358  GetRandBytes(vseed.data(), 32);
359  bool ret = secp256k1_context_randomize(ctx, vseed.data());
360  assert(ret);
361  }
362 
363  secp256k1_context_sign = ctx;
364 }
365 
366 void ECC_Stop() {
367  secp256k1_context *ctx = secp256k1_context_sign;
368  secp256k1_context_sign = nullptr;
369 
370  if (ctx) {
372  }
373 }
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: hmac_sha512.cpp:29
CHMAC_SHA512 & Write(const unsigned char *data, size_t len)
Definition: hmac_sha512.h:24
void ECC_Start()
Initialize the elliptic curve support.
Definition: key.cpp:349
CPrivKey GetPrivKey() const
Convert the private key to a CPrivKey (serialized OpenSSL private key data).
Definition: key.cpp:172
unsigned char vchFingerprint[4]
Definition: pubkey.h:208
SECP256K1_API int secp256k1_ecdsa_signature_serialize_compact(const secp256k1_context *ctx, unsigned char *output64, const secp256k1_ecdsa_signature *sig) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Serialize an ECDSA signature in compact (64 byte) format.
Definition: secp256k1.c:261
CKey key
Definition: key.h:149
bool Negate()
Negate private key.
Definition: key.cpp:166
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(secp256k1_context *ctx, const unsigned char *seed32) SECP256K1_ARG_NONNULL(1)
Updates the context randomization to protect against side-channel leakage.
Definition: secp256k1.c:547
bool Derive(CExtKey &out, unsigned int nChild) const
Definition: key.cpp:294
bool VerifyPubKey(const CPubKey &vchPubKey) const
Verify thoroughly whether a private key and a public key match.
Definition: key.cpp:233
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:185
CHash256 & Write(const unsigned char *data, size_t len)
Definition: hash.h:34
static constexpr unsigned int PUBLIC_KEY_SIZE
secp256k1:
Definition: pubkey.h:36
static const unsigned int COMPRESSED_PRIVATE_KEY_SIZE
Definition: key.h:34
Definition: key.h:144
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const
Definition: key.cpp:323
Opaque data structured that holds a parsed ECDSA signature, supporting pubkey recovery.
unsigned char vchFingerprint[4]
Definition: key.h:146
unsigned char nDepth
Definition: pubkey.h:207
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)
Serialize a pubkey object into a serialized byte sequence.
Definition: secp256k1.c:165
A hasher class for Bitcoin&#39;s 256-bit hash (double SHA-256).
Definition: hash.h:22
void GetRandBytes(unsigned char *buf, int num) noexcept
Overall design of the RNG and entropy sources.
Definition: random.cpp:659
ChainCode chaincode
Definition: pubkey.h:210
const unsigned char * begin() const
Definition: key.h:89
#define SECP256K1_CONTEXT_SIGN
Definition: secp256k1.h:155
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)
Tweak a private key by adding tweak to it.
Definition: secp256k1.c:454
unsigned char * begin()
Definition: uint256.h:55
bool fValid
see www.keylength.com script supports up to 75 for single byte push
Definition: key.h:41
unsigned int nChild
Definition: pubkey.h:209
const unsigned char * begin() const
Definition: pubkey.h:111
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_negate(const secp256k1_context *ctx, unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2)
Negates a private key in place.
Definition: secp256k1.c:427
SECP256K1_API const secp256k1_nonce_function secp256k1_nonce_function_rfc6979
An implementation of RFC6979 (using HMAC-SHA256) as nonce generation function.
Definition: secp256k1.c:342
static constexpr unsigned int COMPRESSED_PUBLIC_KEY_SIZE
Definition: pubkey.h:37
bool SignCompact(const uint256 &hash, std::vector< unsigned char > &vchSig) const
Create a compact signature (65 bytes), which allows reconstructing the used public key...
Definition: key.cpp:247
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx)
Destroy a secp256k1 context object.
Definition: secp256k1.c:92
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)
Compute the public key for a secret key.
Definition: secp256k1.c:404
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
secure_allocator is defined in allocators.h CPrivKey is a serialized private key, with all parameters...
Definition: key.h:24
bool Sign(const uint256 &hash, std::vector< unsigned char > &vchSig, bool grind=true, uint32_t test_case=0) const
Create a DER-serialized signature.
Definition: key.cpp:211
#define SECP256K1_EC_UNCOMPRESSED
Definition: secp256k1.h:160
#define SECP256K1_EC_COMPRESSED
Flag to pass to secp256k1_ec_pubkey_serialize and secp256k1_ec_privkey_export.
Definition: secp256k1.h:159
static const unsigned int PRIVATE_KEY_SIZE
secp256k1:
Definition: key.h:33
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)
Verify an ECDSA secret key.
Definition: secp256k1.c:391
void GetStrongRandBytes(unsigned char *buf, int num) noexcept
Gather entropy from various sources, feed it into the internal PRNG, and generate random data using i...
Definition: random.cpp:660
bool SigHasLowR(const secp256k1_ecdsa_signature *sig)
Definition: key.cpp:199
void ECC_Stop()
Deinitialize the elliptic curve support.
Definition: key.cpp:366
unsigned char nDepth
Definition: key.h:145
void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
Definition: hash.cpp:71
bool IsValid() const
Definition: pubkey.h:171
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)
Create an ECDSA signature.
Definition: secp256k1.c:345
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)
Create a recoverable ECDSA signature.
Definition: main_impl.h:123
An encapsulated public key.
Definition: pubkey.h:30
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:158
unsigned int nChild
Definition: key.h:147
unsigned int size() const
Simple read-only vector-like interface.
Definition: key.h:88
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
Definition: pubkey.h:109
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:96
Opaque data structured that holds a parsed ECDSA signature.
Definition: secp256k1.h:66
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)
Serialize an ECDSA signature in compact format (64 bytes + recovery id).
Definition: main_impl.h:60
static constexpr unsigned int COMPACT_SIGNATURE_SIZE
Definition: pubkey.h:39
ChainCode chaincode
Definition: key.h:148
void SetSeed(const unsigned char *seed, unsigned int nSeedLen)
Definition: key.cpp:302
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE])
Definition: key.cpp:334
256-bit opaque blob.
Definition: uint256.h:121
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)
Serialize an ECDSA signature in DER format.
Definition: secp256k1.c:249
CExtPubKey Neuter() const
Definition: key.cpp:313
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
Verify a DER signature (~72 bytes).
Definition: pubkey.cpp:169
bool Derive(CKey &keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc) const
Derive BIP32 child key.
Definition: key.cpp:274
void * memcpy(void *a, const void *b, size_t c)
const unsigned int BIP32_EXTKEY_SIZE
Definition: pubkey.h:17
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:20
bool fCompressed
Whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:49
CPubKey pubkey
Definition: pubkey.h:211
std::vector< unsigned char, secure_allocator< unsigned char > > keydata
The actual byte data.
Definition: key.h:52
static bool Check(const unsigned char *vch)
Check whether the 32-byte array pointed to by vch is valid keydata.
Definition: key.cpp:154
An encapsulated private key.
Definition: key.h:27
bool ECC_InitSanityCheck()
Check that required EC support is available at runtime.
Definition: key.cpp:342
static constexpr unsigned int SIGNATURE_SIZE
Definition: pubkey.h:38
SECP256K1_API secp256k1_context * secp256k1_context_create(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
Create a secp256k1 context object.
Definition: secp256k1.c:58
bool Load(const CPrivKey &privkey, const CPubKey &vchPubKey, bool fSkipCheck)
Load private key and check that public key matches.
Definition: key.cpp:262
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:93
Opaque data structure that holds a parsed and valid public key.
Definition: secp256k1.h:53
bool IsCompressed() const
Check whether this is a compressed public key.
Definition: pubkey.h:180
A hasher class for HMAC-SHA-512.
Definition: hmac_sha512.h:14