<pre><code>bool IsCanonicalPubKey(const valtype &amp;vchPubKey) {
    if (vchPubKey.size() &lt; 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;
}
</code></pre>

<ul>
<li>1) Public keys must be either 65 or 33 bytes long.</li>
<li>2a) 65 byte long public keys must start with one byte prefix 0x04</li>
<li>2b) 33 byte long public keys must start with one byte prefix 0x02 or
0x03</li>
<li>3) Any other byte sequences can not be treated as public key</li>
</ul>

CC BY-SA 3.0
2014-08-13T13:53:55.287Z
Do you think "Non-canonical public key: compressed nor uncompressed" can cause a wallet to crash?
Emre K.
CC BY-SA 3.0
2014-08-13T14:48:50.943Z
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
CC BY-SA 3.0
2014-08-13T07:25:26.363Z
2014-08-13T14:49:22.783Z
2014-08-13T14:49:22.783Z
amaclin
<p>In my logs I see that I keep getting:</p>

<blockquote>
  <p>ERROR: Non-canonical public key: compressed nor uncompressed</p>
</blockquote>

<p>This error is thrown from inside the method:</p>

<blockquote>
  <p>bool IsCanonicalPubKey(const valtype &amp;vchPubKey, unsigned int flags)</p>
</blockquote>

<p>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. </p>

<p>Can you please explain?</p>

<bitcoind>
IsCanonicalPubKey method in Bitcoin Core source
Emre K.
# IsCanonicalPubKey method in Bitcoin Core source

(Asked by: username_0 on 2014-08-13)

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?

**Tags:** bitcoind


---

## 1 answers

### Answer by username_1 on 2014-08-13. Score: 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

> **Comments:**
> **username_0**: Do you think "Non-canonical public key: compressed nor uncompressed" can cause a wallet to crash?  
> **username_1**: 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.  

---

