|
|
|
@ -87,9 +87,13 @@ static int ec_privkey_import_der(const secp256k1_context* ctx, unsigned char *ou
@@ -87,9 +87,13 @@ static int ec_privkey_import_der(const secp256k1_context* ctx, unsigned char *ou
|
|
|
|
|
* <http://www.secg.org/sec1-v2.pdf>. The optional parameters and publicKey fields are
|
|
|
|
|
* included. |
|
|
|
|
* |
|
|
|
|
* privkey must point to an output buffer of length at least PRIVATE_KEY_SIZE bytes. |
|
|
|
|
* privkeylen must initially be set to the size of the privkey buffer. Upon return it |
|
|
|
|
* will be set to the number of bytes used in the buffer. |
|
|
|
|
* key32 must point to a 32-byte raw private key. |
|
|
|
|
*/ |
|
|
|
|
static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *privkey, size_t *privkeylen, const unsigned char *key32, int compressed) { |
|
|
|
|
assert(*privkeylen >= PRIVATE_KEY_SIZE); |
|
|
|
|
secp256k1_pubkey pubkey; |
|
|
|
|
size_t pubkeylen = 0; |
|
|
|
|
if (!secp256k1_ec_pubkey_create(ctx, &pubkey, key32)) { |
|
|
|
@ -115,10 +119,11 @@ static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *pr
@@ -115,10 +119,11 @@ static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *pr
|
|
|
|
|
memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin); |
|
|
|
|
memcpy(ptr, key32, 32); ptr += 32; |
|
|
|
|
memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle); |
|
|
|
|
pubkeylen = 33; |
|
|
|
|
pubkeylen = COMPRESSED_PUBLIC_KEY_SIZE; |
|
|
|
|
secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED); |
|
|
|
|
ptr += pubkeylen; |
|
|
|
|
*privkeylen = ptr - privkey; |
|
|
|
|
assert(*privkeylen == COMPRESSED_PRIVATE_KEY_SIZE); |
|
|
|
|
} else { |
|
|
|
|
static const unsigned char begin[] = { |
|
|
|
|
0x30,0x82,0x01,0x13,0x02,0x01,0x01,0x04,0x20 |
|
|
|
@ -140,10 +145,11 @@ static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *pr
@@ -140,10 +145,11 @@ static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *pr
|
|
|
|
|
memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin); |
|
|
|
|
memcpy(ptr, key32, 32); ptr += 32; |
|
|
|
|
memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle); |
|
|
|
|
pubkeylen = 65; |
|
|
|
|
pubkeylen = PUBLIC_KEY_SIZE; |
|
|
|
|
secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_UNCOMPRESSED); |
|
|
|
|
ptr += pubkeylen; |
|
|
|
|
*privkeylen = ptr - privkey; |
|
|
|
|
assert(*privkeylen == PRIVATE_KEY_SIZE); |
|
|
|
|
} |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
@ -165,8 +171,8 @@ CPrivKey CKey::GetPrivKey() const {
@@ -165,8 +171,8 @@ CPrivKey CKey::GetPrivKey() const {
|
|
|
|
|
CPrivKey privkey; |
|
|
|
|
int ret; |
|
|
|
|
size_t privkeylen; |
|
|
|
|
privkey.resize(279); |
|
|
|
|
privkeylen = 279; |
|
|
|
|
privkey.resize(PRIVATE_KEY_SIZE); |
|
|
|
|
privkeylen = PRIVATE_KEY_SIZE; |
|
|
|
|
ret = ec_privkey_export_der(secp256k1_context_sign, (unsigned char*) privkey.data(), &privkeylen, begin(), fCompressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED); |
|
|
|
|
assert(ret); |
|
|
|
|
privkey.resize(privkeylen); |
|
|
|
@ -176,7 +182,7 @@ CPrivKey CKey::GetPrivKey() const {
@@ -176,7 +182,7 @@ CPrivKey CKey::GetPrivKey() const {
|
|
|
|
|
CPubKey CKey::GetPubKey() const { |
|
|
|
|
assert(fValid); |
|
|
|
|
secp256k1_pubkey pubkey; |
|
|
|
|
size_t clen = 65; |
|
|
|
|
size_t clen = PUBLIC_KEY_SIZE; |
|
|
|
|
CPubKey result; |
|
|
|
|
int ret = secp256k1_ec_pubkey_create(secp256k1_context_sign, &pubkey, begin()); |
|
|
|
|
assert(ret); |
|
|
|
@ -189,8 +195,8 @@ CPubKey CKey::GetPubKey() const {
@@ -189,8 +195,8 @@ CPubKey CKey::GetPubKey() const {
|
|
|
|
|
bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, uint32_t test_case) const { |
|
|
|
|
if (!fValid) |
|
|
|
|
return false; |
|
|
|
|
vchSig.resize(72); |
|
|
|
|
size_t nSigLen = 72; |
|
|
|
|
vchSig.resize(SIGNATURE_SIZE); |
|
|
|
|
size_t nSigLen = SIGNATURE_SIZE; |
|
|
|
|
unsigned char extra_entropy[32] = {0}; |
|
|
|
|
WriteLE32(extra_entropy, test_case); |
|
|
|
|
secp256k1_ecdsa_signature sig; |
|
|
|
@ -218,7 +224,7 @@ bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
@@ -218,7 +224,7 @@ bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
|
|
|
|
|
bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig) const { |
|
|
|
|
if (!fValid) |
|
|
|
|
return false; |
|
|
|
|
vchSig.resize(65); |
|
|
|
|
vchSig.resize(COMPACT_SIGNATURE_SIZE); |
|
|
|
|
int rec = -1; |
|
|
|
|
secp256k1_ecdsa_recoverable_signature sig; |
|
|
|
|
int ret = secp256k1_ecdsa_sign_recoverable(secp256k1_context_sign, &sig, hash.begin(), begin(), secp256k1_nonce_function_rfc6979, NULL); |
|
|
|
@ -248,7 +254,7 @@ bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const
@@ -248,7 +254,7 @@ bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const
|
|
|
|
|
std::vector<unsigned char, secure_allocator<unsigned char>> vout(64); |
|
|
|
|
if ((nChild >> 31) == 0) { |
|
|
|
|
CPubKey pubkey = GetPubKey(); |
|
|
|
|
assert(pubkey.size() == 33); |
|
|
|
|
assert(pubkey.size() == COMPRESSED_PUBLIC_KEY_SIZE); |
|
|
|
|
BIP32Hash(cc, nChild, *pubkey.begin(), pubkey.begin()+1, vout.data()); |
|
|
|
|
} else { |
|
|
|
|
assert(size() == 32); |
|
|
|
|