diff --git a/src/script/sigencoding.cpp b/src/script/sigencoding.cpp --- a/src/script/sigencoding.cpp +++ b/src/script/sigencoding.cpp @@ -135,34 +135,28 @@ } static bool IsCompressedOrUncompressedPubKey(const valtype &vchPubKey) { - if (vchPubKey.size() < 33) { - // Non-canonical public key: too short - return false; - } - if (vchPubKey[0] == 0x04) { - if (vchPubKey.size() != 65) { - // Non-canonical public key: invalid length for uncompressed key - return false; - } - } else if (vchPubKey[0] == 0x02 || vchPubKey[0] == 0x03) { - if (vchPubKey.size() != 33) { - // Non-canonical public key: invalid length for compressed key + switch (vchPubKey.size()) { + case 33: + // Compressed public key: must start with 0x02 or 0x03. + return vchPubKey[0] == 0x02 || vchPubKey[0] == 0x03; + + case 65: + // Non-compressed public key: must start with 0x04. + return vchPubKey[0] == 0x04; + + default: + // Non-canonical public key: invalid size. return false; - } - } else { - // Non-canonical public key: neither compressed nor uncompressed - return false; } - return true; } static bool IsCompressedPubKey(const valtype &vchPubKey) { if (vchPubKey.size() != 33) { - // Non-canonical public key: invalid length for compressed key + // Non-canonical public key: invalid length for compressed key return false; } if (vchPubKey[0] != 0x02 && vchPubKey[0] != 0x03) { - // Non-canonical public key: invalid prefix for compressed key + // Non-canonical public key: invalid prefix for compressed key return false; } return true; @@ -170,7 +164,7 @@ bool CheckPubKeyEncoding(const valtype &vchPubKey, uint32_t flags, ScriptError *serror) { - if ((flags & SCRIPT_VERIFY_STRICTENC) != 0 && + if ((flags & SCRIPT_VERIFY_STRICTENC) && !IsCompressedOrUncompressedPubKey(vchPubKey)) { return set_error(serror, SCRIPT_ERR_PUBKEYTYPE); } diff --git a/src/test/sigencoding_tests.cpp b/src/test/sigencoding_tests.cpp --- a/src/test/sigencoding_tests.cpp +++ b/src/test/sigencoding_tests.cpp @@ -32,6 +32,11 @@ // Degenerate keys. {}, {0x00}, + {0x01}, + {0x02}, + {0x03}, + {0x04}, + {0x05}, {0x42}, {0xff}, // Invalid first byte 0x00.