1 // Copyright (c) 2013-2014 The btcsuite developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
4 5 package btcec
6 7 import (
8 "smesh.lol/pkg/nostr/ec/secp256k1"
9 )
10 11 // These constants define the lengths of serialized public keys.
12 13 const (
14 PubKeyBytesLenCompressed = 33
15 )
16 17 const (
18 pubkeyCompressed byte = 0x2 // y_bit + x coord
19 pubkeyUncompressed byte = 0x4 // x coord + y coord
20 pubkeyHybrid byte = 0x6 // y_bit + x coord + y coord
21 )
22 23 // IsCompressedPubKey returns true the passed serialized public key has
24 // been encoded in compressed format, and false otherwise.
25 func IsCompressedPubKey(pubKey []byte) bool {
26 // The public key is only compressed if it is the correct length and
27 // the format (first byte) is one of the compressed pubkey values.
28 return len(pubKey) == PubKeyBytesLenCompressed &&
29 (pubKey[0]&^byte(0x1) == pubkeyCompressed)
30 }
31 32 // ParsePubKey parses a public key for a koblitz curve from a bytestring into a
33 // ecdsa.Publickey, verifying that it is valid. It supports compressed,
34 // uncompressed and hybrid signature formats.
35 func ParsePubKey(pubKeyStr []byte) (*PublicKey, error) {
36 return secp256k1.ParsePubKey(pubKeyStr)
37 }
38 39 // PublicKey is an ecdsa.PublicKey with additional functions to
40 // serialize in uncompressed, compressed, and hybrid formats.
41 type PublicKey = secp256k1.PublicKey
42 43 // NewPublicKey instantiates a new public key with the given x and y
44 // coordinates.
45 //
46 // It should be noted that, unlike ParsePubKey, since this accepts arbitrary x
47 // and y coordinates, it allows creation of public keys that are not valid
48 // points on the secp256k1 curve. The IsOnCurve method of the returned instance
49 // can be used to determine validity.
50 func NewPublicKey(x, y *FieldVal) *PublicKey {
51 return secp256k1.NewPublicKey(x, y)
52 }
53