IsCanonicalPubKey method in Bitcoin Core source

0

In my logs I see that I keep getting:

ERROR: Non-canonical public key: compressed nor uncompressed

This error is thrown from inside the method:

bool IsCanonicalPubKey(const valtype &vchPubKey, unsigned int flags)

I can't understand what this method is checking. I understand it's checking if the Public Key is Canonical but, I don't understand what that means.

Can you please explain?

Emre Kenci

Posted 2014-08-13T06:32:13.837

Reputation: 3 008

Answers

1

bool IsCanonicalPubKey(const valtype &vchPubKey) {
    if (vchPubKey.size() < 33)
        return error("Non-canonical public key: too short");
    if (vchPubKey[0] == 0x04) {
        if (vchPubKey.size() != 65)
            return error("Non-canonical public key: invalid length for uncompressed key");
    } else if (vchPubKey[0] == 0x02 || vchPubKey[0] == 0x03) {
        if (vchPubKey.size() != 33)
            return error("Non-canonical public key: invalid length for compressed key");
    } else {
        return error("Non-canonical public key: compressed nor uncompressed");
    }
    return true;
}
  • 1) Public keys must be either 65 or 33 bytes long.
  • 2a) 65 byte long public keys must start with one byte prefix 0x04
  • 2b) 33 byte long public keys must start with one byte prefix 0x02 or 0x03
  • 3) Any other byte sequences can not be treated as public key

amaclin

Posted 2014-08-13T06:32:13.837

Reputation: 5 763

Do you think "Non-canonical public key: compressed nor uncompressed" can cause a wallet to crash?Emre Kenci 2014-08-13T13:53:55.287

I am sure - no. This check is for incoming/raw transactions. If the test failed - the client throws away this transaction. As if it not exists at all.amaclin 2014-08-13T14:48:50.943