github.com/turingchain2020/turingchain@v1.1.21/system/crypto/secp256k1/secp256k1.go (about) 1 // Copyright Turing Corp. 2018 All Rights Reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package secp256k1 secp256k1系统加密包 6 package secp256k1 7 8 import ( 9 "bytes" 10 "errors" 11 "fmt" 12 13 "github.com/turingchain2020/turingchain/common/crypto" 14 secp256k1 "github.com/btcsuite/btcd/btcec" 15 ) 16 17 //Driver 驱动 18 type Driver struct{} 19 20 //GenKey 生成私钥 21 func (d Driver) GenKey() (crypto.PrivKey, error) { 22 privKeyBytes := [32]byte{} 23 copy(privKeyBytes[:], crypto.CRandBytes(32)) 24 priv, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKeyBytes[:]) 25 copy(privKeyBytes[:], priv.Serialize()) 26 return PrivKeySecp256k1(privKeyBytes), nil 27 } 28 29 //PrivKeyFromBytes 字节转为私钥 30 func (d Driver) PrivKeyFromBytes(b []byte) (privKey crypto.PrivKey, err error) { 31 if len(b) != 32 { 32 return nil, errors.New("invalid priv key byte") 33 } 34 privKeyBytes := new([32]byte) 35 copy(privKeyBytes[:], b[:32]) 36 priv, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKeyBytes[:]) 37 copy(privKeyBytes[:], priv.Serialize()) 38 return PrivKeySecp256k1(*privKeyBytes), nil 39 } 40 41 //PubKeyFromBytes 字节转为公钥 42 func (d Driver) PubKeyFromBytes(b []byte) (pubKey crypto.PubKey, err error) { 43 if len(b) != 33 { 44 return nil, errors.New("invalid pub key byte") 45 } 46 pubKeyBytes := new([33]byte) 47 copy(pubKeyBytes[:], b[:]) 48 return PubKeySecp256k1(*pubKeyBytes), nil 49 } 50 51 //SignatureFromBytes 字节转为签名 52 func (d Driver) SignatureFromBytes(b []byte) (sig crypto.Signature, err error) { 53 return SignatureSecp256k1(b), nil 54 } 55 56 // Validate validate msg and signature 57 func (d Driver) Validate(msg, pub, sig []byte) error { 58 return crypto.BasicValidation(d, msg, pub, sig) 59 } 60 61 //PrivKeySecp256k1 PrivKey 62 type PrivKeySecp256k1 [32]byte 63 64 //Bytes 字节格式 65 func (privKey PrivKeySecp256k1) Bytes() []byte { 66 s := make([]byte, 32) 67 copy(s, privKey[:]) 68 return s 69 } 70 71 //Sign 签名 72 func (privKey PrivKeySecp256k1) Sign(msg []byte) crypto.Signature { 73 priv, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey[:]) 74 sig, err := priv.Sign(crypto.Sha256(msg)) 75 if err != nil { 76 panic("Error signing secp256k1" + err.Error()) 77 } 78 return SignatureSecp256k1(sig.Serialize()) 79 } 80 81 //PubKey 私钥生成公钥 82 func (privKey PrivKeySecp256k1) PubKey() crypto.PubKey { 83 _, pub := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey[:]) 84 var pubSecp256k1 PubKeySecp256k1 85 copy(pubSecp256k1[:], pub.SerializeCompressed()) 86 return pubSecp256k1 87 } 88 89 //Equals 私钥是否相等 90 func (privKey PrivKeySecp256k1) Equals(other crypto.PrivKey) bool { 91 if otherSecp, ok := other.(PrivKeySecp256k1); ok { 92 return bytes.Equal(privKey[:], otherSecp[:]) 93 } 94 return false 95 96 } 97 98 func (privKey PrivKeySecp256k1) String() string { 99 return fmt.Sprintf("PrivKeySecp256k1{*****}") 100 } 101 102 // PubKey 103 104 //PubKeySecp256k1 Compressed pubkey (just the x-cord), 105 // prefixed with 0x02 or 0x03, depending on the y-cord. 106 type PubKeySecp256k1 [33]byte 107 108 //Bytes 字节格式 109 func (pubKey PubKeySecp256k1) Bytes() []byte { 110 s := make([]byte, 33) 111 copy(s, pubKey[:]) 112 return s 113 } 114 115 //VerifyBytes 验证字节 116 func (pubKey PubKeySecp256k1) VerifyBytes(msg []byte, sig crypto.Signature) bool { 117 // unwrap if needed 118 if wrap, ok := sig.(SignatureS); ok { 119 sig = wrap.Signature 120 } 121 // and assert same algorithm to sign and verify 122 sigSecp256k1, ok := sig.(SignatureSecp256k1) 123 if !ok { 124 return false 125 } 126 127 pub, err := secp256k1.ParsePubKey(pubKey[:], secp256k1.S256()) 128 if err != nil { 129 return false 130 } 131 sig2, err := secp256k1.ParseDERSignature(sigSecp256k1[:], secp256k1.S256()) 132 if err != nil { 133 return false 134 } 135 return sig2.Verify(crypto.Sha256(msg), pub) 136 } 137 138 func (pubKey PubKeySecp256k1) String() string { 139 return fmt.Sprintf("PubKeySecp256k1{%X}", pubKey[:]) 140 } 141 142 //KeyString Must return the full bytes in hex. 143 // Used for map keying, etc. 144 func (pubKey PubKeySecp256k1) KeyString() string { 145 return fmt.Sprintf("%X", pubKey[:]) 146 } 147 148 //Equals 公钥相等 149 func (pubKey PubKeySecp256k1) Equals(other crypto.PubKey) bool { 150 if otherSecp, ok := other.(PubKeySecp256k1); ok { 151 return bytes.Equal(pubKey[:], otherSecp[:]) 152 } 153 return false 154 155 } 156 157 //SignatureSecp256k1 Signature 158 type SignatureSecp256k1 []byte 159 160 //SignatureS 签名 161 type SignatureS struct { 162 crypto.Signature 163 } 164 165 //Bytes 字节格式 166 func (sig SignatureSecp256k1) Bytes() []byte { 167 s := make([]byte, len(sig)) 168 copy(s, sig[:]) 169 return s 170 } 171 172 //IsZero 是否是0 173 func (sig SignatureSecp256k1) IsZero() bool { return len(sig) == 0 } 174 175 func (sig SignatureSecp256k1) String() string { 176 fingerprint := make([]byte, len(sig[:])) 177 copy(fingerprint, sig[:]) 178 return fmt.Sprintf("/%X.../", fingerprint) 179 180 } 181 182 //Equals 相等 183 func (sig SignatureSecp256k1) Equals(other crypto.Signature) bool { 184 if otherEd, ok := other.(SignatureSecp256k1); ok { 185 return bytes.Equal(sig[:], otherEd[:]) 186 } 187 return false 188 189 } 190 191 //const 192 const ( 193 Name = "secp256k1" 194 ID = 1 195 ) 196 197 func init() { 198 crypto.Register(Name, &Driver{}, crypto.WithOptionTypeID(ID)) 199 }