You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.8 KiB
52 lines
1.8 KiB
// Copyright (c) 2013-2014 The btcsuite developers |
|
// Use of this source code is governed by an ISC |
|
// license that can be found in the LICENSE file. |
|
|
|
package btcec |
|
|
|
import ( |
|
"next.orly.dev/pkg/crypto/ec/secp256k1" |
|
) |
|
|
|
// These constants define the lengths of serialized public keys. |
|
|
|
const ( |
|
PubKeyBytesLenCompressed = 33 |
|
) |
|
|
|
const ( |
|
pubkeyCompressed byte = 0x2 // y_bit + x coord |
|
pubkeyUncompressed byte = 0x4 // x coord + y coord |
|
pubkeyHybrid byte = 0x6 // y_bit + x coord + y coord |
|
) |
|
|
|
// IsCompressedPubKey returns true 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. |
|
func ParsePubKey(pubKeyStr []byte) (*PublicKey, error) { |
|
return secp256k1.ParsePubKey(pubKeyStr) |
|
} |
|
|
|
// PublicKey is an ecdsa.PublicKey with additional functions to |
|
// serialize in uncompressed, compressed, and hybrid formats. |
|
type PublicKey = secp256k1.PublicKey |
|
|
|
// NewPublicKey instantiates a new public key with the given x and y |
|
// coordinates. |
|
// |
|
// It should be noted that, unlike ParsePubKey, since this accepts arbitrary x |
|
// and y coordinates, it allows creation of public keys that are not valid |
|
// points on the secp256k1 curve. The IsOnCurve method of the returned instance |
|
// can be used to determine validity. |
|
func NewPublicKey(x, y *FieldVal) *PublicKey { |
|
return secp256k1.NewPublicKey(x, y) |
|
}
|
|
|