The argument to ec_privkey_export_der is BOOLEAN GetPrivKey call ec_privkey_export_der to use the flag
SECP256K1_EC_COMPRESSED is true SECP256K1_EC_UNCOMPRESSED is true
The argument to ec_privkey_export_der is BOOLEAN GetPrivKey call ec_privkey_export_der to use the flag
SECP256K1_EC_COMPRESSED is true SECP256K1_EC_UNCOMPRESSED is true
Is it changing ec_privkey_export_der? Delete another branch that will never be reached
Looks like a bug to me...
<!--e57a25ab6845829454e8d69fc972939a-->Note to reviewers: This pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.
Please make your description and title more descriptive.
You found out before, why not fix it?
@fingera Please re-open the PR: this is still confusing and hence unresolved :-)
169 | @@ -170,7 +170,7 @@ CPrivKey CKey::GetPrivKey() const { 170 | size_t privkeylen; 171 | privkey.resize(PRIVATE_KEY_SIZE); 172 | privkeylen = PRIVATE_KEY_SIZE; 173 | - ret = ec_privkey_export_der(secp256k1_context_sign, privkey.data(), &privkeylen, begin(), fCompressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED); 174 | + ret = ec_privkey_export_der(secp256k1_context_sign, privkey.data(), &privkeylen, begin(), fCompressed);
src/key.cpp:173:95: warning: implicit conversion bool -> 'int' [readability-implicit-bool-conversion]
169 | @@ -170,7 +170,7 @@ CPrivKey CKey::GetPrivKey() const { 170 | size_t privkeylen; 171 | privkey.resize(PRIVATE_KEY_SIZE); 172 | privkeylen = PRIVATE_KEY_SIZE; 173 | - ret = ec_privkey_export_der(secp256k1_context_sign, privkey.data(), &privkeylen, begin(), fCompressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED); 174 | + ret = ec_privkey_export_der(secp256k1_context_sign, privkey.data(), &privkeylen, begin(), fCompressed ? 1 : 0);
fCompressed is a C++ bool, this gets coerced to 1 or 0 automatically; so you can leave out the ? 1 : 0.
the type bool can be converted to int with the value false becoming 0 and true becoming 1. https://en.cppreference.com/w/cpp/language/implicit_conversion
Huh, apparently @practicalswift told you to do this. Can you explain @practicalswift ?
@laanwj Explicit is better than implicit :-) See https://clang.llvm.org/extra/clang-tidy/checks/readability-implicit-bool-conversion.html for a more technical rationale :-)
utACK (after squash), this clearly fixes a bug, ec_privkey_export_der does not take a bit field (secp256k1_ec_pubkey_serialize is what takes SECP256K1_EC_COMPRESSED=258 and SECP256K1_EC_UNCOMPRESSED=2). I'm surprised none of the tests catches this.
utACK 67e17da34de278c0b0bab0caad0ad877a053b0d6 modulo squash and a more informative commit message :-)
suggested on IRC was, to prevent the bug as well as the conversion warning without using really contorted C++:
diff --git a/src/key.cpp b/src/key.cpp
index df452cd3302ee6aff363b8fc8ea328b69d8bfb55..80d6589a3c36b823402b81d759ff43520037c43a 100644
--- a/src/key.cpp
+++ b/src/key.cpp
@@ -89,7 +89,7 @@ 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) {
+static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *privkey, size_t *privkeylen, const unsigned char *key32, bool compressed) {
assert(*privkeylen >= CKey::PRIVATE_KEY_SIZE);
secp256k1_pubkey pubkey;
size_t pubkeylen = 0;
@@ -170,7 +170,7 @@ CPrivKey CKey::GetPrivKey() const {
size_t privkeylen;
privkey.resize(PRIVATE_KEY_SIZE);
privkeylen = PRIVATE_KEY_SIZE;
- ret = ec_privkey_export_der(secp256k1_context_sign, privkey.data(), &privkeylen, begin(), fCompressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED);
+ ret = ec_privkey_export_der(secp256k1_context_sign, privkey.data(), &privkeylen, begin(), fCompressed);
assert(ret);
privkey.resize(privkeylen);
return privkey;
merged via 9a565a8282236f29334a6ec2de6a03358f5ce86e