From 65feec33e0bf0db092129101f66dddc86dae3683 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Wed, 26 Apr 2017 16:21:40 -0700 Subject: [PATCH] btcec: add new IsCompressedPubKey function This commit adds a new function to btcec: IsCompressedPubKey. This function returns true iff the passed serialized public key is encoded in compressed format. --- btcec/pubkey.go | 9 +++++++++ btcec/pubkey_test.go | 12 ++++++++++++ 2 files changed, 21 insertions(+) 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) + } + } +}