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