pubkey.go raw
1 // Copyright (c) 2013-2014 The btcsuite developers
2 // Copyright (c) 2015-2020 The Decred developers
3 // Use of this source code is governed by an ISC
4 // license that can be found in the LICENSE file.
5
6 package schnorr
7
8 import (
9 "fmt"
10
11 "github.com/decred/dcrd/dcrec/secp256k1/v4"
12 )
13
14 // These constants define the lengths of serialized public keys.
15 const (
16 PubKeyBytesLen = 33
17 )
18
19 const (
20 // pubkeyCompressed is the header byte for a compressed secp256k1 pubkey.
21 pubkeyCompressed byte = 0x2 // y_bit + x coord
22 )
23
24 // ParsePubKey parses a public key for a koblitz curve from a bytestring into a
25 // ecdsa.Publickey, verifying that it is valid. It supports compressed signature
26 // formats only.
27 func ParsePubKey(pubKeyStr []byte) (key *secp256k1.PublicKey, err error) {
28 if pubKeyStr == nil {
29 err = fmt.Errorf("nil pubkey byte string")
30 return
31 }
32 if len(pubKeyStr) != PubKeyBytesLen {
33 err = fmt.Errorf("bad pubkey byte string size (want %v, have %v)",
34 PubKeyBytesLen, len(pubKeyStr))
35 return
36 }
37 format := pubKeyStr[0]
38 format &= ^byte(0x1)
39 if format != pubkeyCompressed {
40 err = fmt.Errorf("wrong pubkey type (not compressed)")
41 return
42 }
43
44 return secp256k1.ParsePubKey(pubKeyStr)
45 }
46