Skip to content

ec_privkey_export_der() seems to be called as compressed mode ALWAYS. Is it on purpose? #10041

@kobake

Description

@kobake

What version of bitcoin-core are you using?

GitHub latest master. (3192e52)

Describe the issue

"compressed" argument of ec_privkey_export_der() seems to be bool-typed.

ec_privkey_export_der function has a "compressed" argument and it accepts bool-typed value (0 or not 0) as far as I see the implementation of the function.

https://github.com/bitcoin/bitcoin/blob/master/src/key.cpp#L70
https://github.com/bitcoin/bitcoin/blob/master/src/secp256k1/contrib/lax_der_privatekey_parsing.c#L56

if (compressed) {
    ....
} else {
    ....
}

Passed values for the "compressed" are NOT ZERO ALWAYS.

Actually, however, the passed values for the argument "compressed" are SECP256K1_EC_COMPRESSED or SECP256K1_EC_UNCOMPRESSED only, and Both of them are NOT ZERO.

  • SECP256K1_EC_COMPRESSED equals 0x102. (NOT ZERO)
  • SECP256K1_EC_UNCOMPRESSED equals 0x2. (NOT ZERO)

https://github.com/bitcoin/bitcoin/blob/master/src/key.cpp#L149

ret = ec_privkey_export_der(secp256k1_context_sign,
  (unsigned char*)&privkey[0], &privkeylen, begin(),
  fCompressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED);

https://github.com/bitcoin/bitcoin/blob/master/src/secp256k1/include/secp256k1.h#L147

#define SECP256K1_FLAGS_TYPE_COMPRESSION (1 << 1)
#define SECP256K1_FLAGS_BIT_COMPRESSION (1 << 8)
#define SECP256K1_EC_COMPRESSED (SECP256K1_FLAGS_TYPE_COMPRESSION | SECP256K1_FLAGS_BIT_COMPRESSION)
#define SECP256K1_EC_UNCOMPRESSED (SECP256K1_FLAGS_TYPE_COMPRESSION)

My idea: Ignoring fCompressed variable.

I think fCompressed makes no sense when calling ec_privkey_export_der(). Though, to keep compatibility, ec_privkey_export_der() should be called as compressed mode only as in the past, I think.

I also think the fCompressed variable on ec_privkey_export_der() calling is now only noise and should be removed. I'd suggest my idea that calling ec_privkey_export_der() without considering fCompressed, as follows.

// idea1
ret = ec_privkey_export_der(secp256k1_context_sign,
  (unsigned char*)&privkey[0], &privkeylen, begin(), SECP256K1_EC_COMPRESSED);

// idea2
ret = ec_privkey_export_der(secp256k1_context_sign,
  (unsigned char*)&privkey[0], &privkeylen, begin(), 1);

What do you think of that?

BTW: The implementation of ec_privkey_export_der() seems to be the copy of the library code, so I thought it's not good to change it. Actually is that right?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions