diff --git a/btcec/pubkey.go b/btcec/pubkey.go index f145414e..b7491771 100644 --- a/btcec/pubkey.go +++ b/btcec/pubkey.go @@ -54,6 +54,15 @@ const ( pubkeyHybrid byte = 0x6 // y_bit + x coord + y coord ) +// IsCompressedPubKey returns true the the passed serialized public key has +// been encoded in compressed format, and false otherwise. +func IsCompressedPubKey(pubKey []byte) bool { + // The public key is only compressed if it is the correct length and + // the format (first byte) is one of the compressed pubkey values. + return len(pubKey) == PubKeyBytesLenCompressed && + (pubKey[0]&^byte(0x1) == pubkeyCompressed) +} + // ParsePubKey parses a public key for a koblitz curve from a bytestring into a // ecdsa.Publickey, verifying that it is valid. It supports compressed, // uncompressed and hybrid signature formats. diff --git a/btcec/pubkey_test.go b/btcec/pubkey_test.go index 6abab43f..0a45f1c0 100644 --- a/btcec/pubkey_test.go +++ b/btcec/pubkey_test.go @@ -282,3 +282,15 @@ func TestPublicKeyIsEqual(t *testing.T) { "equal to %v", pubKey1, pubKey2) } } + +func TestIsCompressed(t *testing.T) { + for _, test := range pubKeyTests { + isCompressed := IsCompressedPubKey(test.key) + wantCompressed := (test.format == pubkeyCompressed) + if isCompressed != wantCompressed { + t.Fatalf("%s (%x) pubkey: unexpected compressed result, "+ + "got %v, want %v", test.name, test.key, + isCompressed, wantCompressed) + } + } +}