gitee.com/lh-her-team/common@v1.5.1/helper/libp2pcrypto/secp256k1.go (about) 1 package libp2pcrypto 2 3 import ( 4 "fmt" 5 "io" 6 7 "gitee.com/lh-her-team/common/helper/libp2pcrypto/pb" 8 9 btcec "github.com/btcsuite/btcd/btcec" 10 sha256 "github.com/minio/sha256-simd" 11 ) 12 13 // Secp256k1PrivateKey is an Secp256k1 private key 14 type Secp256k1PrivateKey btcec.PrivateKey 15 16 // Secp256k1PublicKey is an Secp256k1 public key 17 type Secp256k1PublicKey btcec.PublicKey 18 19 // GenerateSecp256k1Key generates a new Secp256k1 private and public key pair 20 func GenerateSecp256k1Key(src io.Reader) (PrivKey, PubKey, error) { 21 privk, err := btcec.NewPrivateKey(btcec.S256()) 22 if err != nil { 23 return nil, nil, err 24 } 25 k := (*Secp256k1PrivateKey)(privk) 26 return k, k.GetPublic(), nil 27 } 28 29 // UnmarshalSecp256k1PrivateKey returns a private key from bytes 30 func UnmarshalSecp256k1PrivateKey(data []byte) (PrivKey, error) { 31 if len(data) != btcec.PrivKeyBytesLen { 32 return nil, fmt.Errorf("expected secp256k1 data size to be %d", btcec.PrivKeyBytesLen) 33 } 34 privk, _ := btcec.PrivKeyFromBytes(btcec.S256(), data) 35 return (*Secp256k1PrivateKey)(privk), nil 36 } 37 38 // UnmarshalSecp256k1PublicKey returns a public key from bytes 39 func UnmarshalSecp256k1PublicKey(data []byte) (PubKey, error) { 40 k, err := btcec.ParsePubKey(data, btcec.S256()) 41 if err != nil { 42 return nil, err 43 } 44 return (*Secp256k1PublicKey)(k), nil 45 } 46 47 // Bytes returns protobuf bytes from a private key 48 func (k *Secp256k1PrivateKey) Bytes() ([]byte, error) { 49 return MarshalPrivateKey(k) 50 } 51 52 // Type returns the private key type 53 func (k *Secp256k1PrivateKey) Type() pb.KeyType { 54 return pb.KeyType_Secp256k1 55 } 56 57 // Raw returns the bytes of the key 58 func (k *Secp256k1PrivateKey) Raw() ([]byte, error) { 59 return (*btcec.PrivateKey)(k).Serialize(), nil 60 } 61 62 // Equals compares two private keys 63 func (k *Secp256k1PrivateKey) Equals(o Key) bool { 64 sk, ok := o.(*Secp256k1PrivateKey) 65 if !ok { 66 return basicEquals(k, o) 67 } 68 return k.GetPublic().Equals(sk.GetPublic()) 69 } 70 71 // Sign returns a signature from input data 72 func (k *Secp256k1PrivateKey) Sign(data []byte) ([]byte, error) { 73 hash := sha256.Sum256(data) 74 sig, err := (*btcec.PrivateKey)(k).Sign(hash[:]) 75 if err != nil { 76 return nil, err 77 } 78 return sig.Serialize(), nil 79 } 80 81 // GetPublic returns a public key 82 func (k *Secp256k1PrivateKey) GetPublic() PubKey { 83 return (*Secp256k1PublicKey)((*btcec.PrivateKey)(k).PubKey()) 84 } 85 86 // Bytes returns protobuf bytes from a public key 87 func (k *Secp256k1PublicKey) Bytes() ([]byte, error) { 88 return MarshalPublicKey(k) 89 } 90 91 // Type returns the public key type 92 func (k *Secp256k1PublicKey) Type() pb.KeyType { 93 return pb.KeyType_Secp256k1 94 } 95 96 // Raw returns the bytes of the key 97 func (k *Secp256k1PublicKey) Raw() ([]byte, error) { 98 return (*btcec.PublicKey)(k).SerializeCompressed(), nil 99 } 100 101 // Equals compares two public keys 102 func (k *Secp256k1PublicKey) Equals(o Key) bool { 103 sk, ok := o.(*Secp256k1PublicKey) 104 if !ok { 105 return basicEquals(k, o) 106 } 107 return (*btcec.PublicKey)(k).IsEqual((*btcec.PublicKey)(sk)) 108 } 109 110 // Verify compares a signature against the input data 111 func (k *Secp256k1PublicKey) Verify(data []byte, sigStr []byte) (bool, error) { 112 sig, err := btcec.ParseDERSignature(sigStr, btcec.S256()) 113 if err != nil { 114 return false, err 115 } 116 hash := sha256.Sum256(data) 117 return sig.Verify(hash[:], (*btcec.PublicKey)(k)), nil 118 }