Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export der always compressed #14102

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/key.cpp
Expand Up @@ -89,15 +89,15 @@ static int ec_privkey_import_der(const secp256k1_context* ctx, unsigned char *ou
* 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 >= CKey::PRIVATE_KEY_SIZE);
static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *privkey, size_t *privkeylen, const unsigned char *key32, unsigned int compressed) {
secp256k1_pubkey pubkey;
size_t pubkeylen = 0;
if (!secp256k1_ec_pubkey_create(ctx, &pubkey, key32)) {
*privkeylen = 0;
return 0;
}
if (compressed) {
if (compressed == SECP256K1_EC_COMPRESSED) {
assert(*privkeylen >= CKey::COMPRESSED_PRIVATE_KEY_SIZE);
static const unsigned char begin[] = {
0x30,0x81,0xD3,0x02,0x01,0x01,0x04,0x20
};
Expand All @@ -117,11 +117,13 @@ static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *pr
memcpy(ptr, key32, 32); ptr += 32;
memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
pubkeylen = CPubKey::COMPRESSED_PUBLIC_KEY_SIZE;
secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED);
secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, compressed);
ptr += pubkeylen;
*privkeylen = ptr - privkey;
assert(*privkeylen == CKey::COMPRESSED_PRIVATE_KEY_SIZE);
} else {
assert(compressed == SECP256K1_EC_UNCOMPRESSED);
assert(*privkeylen >= CKey::PRIVATE_KEY_SIZE);
static const unsigned char begin[] = {
0x30,0x82,0x01,0x13,0x02,0x01,0x01,0x04,0x20
};
Expand All @@ -143,7 +145,7 @@ static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *pr
memcpy(ptr, key32, 32); ptr += 32;
memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
pubkeylen = CPubKey::PUBLIC_KEY_SIZE;
secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_UNCOMPRESSED);
secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, compressed);
ptr += pubkeylen;
*privkeylen = ptr - privkey;
assert(*privkeylen == CKey::PRIVATE_KEY_SIZE);
Expand Down
9 changes: 9 additions & 0 deletions src/test/key_tests.cpp
Expand Up @@ -188,4 +188,13 @@ BOOST_AUTO_TEST_CASE(key_signature_tests)
BOOST_CHECK(found_small);
}

BOOST_AUTO_TEST_CASE(key_priv_compress_tests)
{
CKey key;
key.MakeNewKey(false);
BOOST_CHECK(key.GetPrivKey().size() == CKey::PRIVATE_KEY_SIZE);
key.MakeNewKey(true);
BOOST_CHECK(key.GetPrivKey().size() == CKey::COMPRESSED_PRIVATE_KEY_SIZE);
}

BOOST_AUTO_TEST_SUITE_END()