github.com/phillinzzz/newBsc@v1.1.6/crypto/crypto.go (about) 1 // Copyright 2014 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package crypto 18 19 import ( 20 "bufio" 21 "crypto/ecdsa" 22 "crypto/elliptic" 23 "crypto/rand" 24 "encoding/hex" 25 "errors" 26 "fmt" 27 "hash" 28 "io" 29 "io/ioutil" 30 "math/big" 31 "os" 32 "sync" 33 34 "github.com/VictoriaMetrics/fastcache" 35 36 "github.com/phillinzzz/newBsc/common" 37 "github.com/phillinzzz/newBsc/common/math" 38 "github.com/phillinzzz/newBsc/rlp" 39 "golang.org/x/crypto/sha3" 40 ) 41 42 //SignatureLength indicates the byte length required to carry a signature with recovery id. 43 const SignatureLength = 64 + 1 // 64 bytes ECDSA signature + 1 byte recovery id 44 45 // RecoveryIDOffset points to the byte offset within the signature that contains the recovery id. 46 const RecoveryIDOffset = 64 47 48 // DigestLength sets the signature digest exact length 49 const DigestLength = 32 50 51 var ( 52 secp256k1N, _ = new(big.Int).SetString("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141", 16) 53 secp256k1halfN = new(big.Int).Div(secp256k1N, big.NewInt(2)) 54 55 keccakState256Cache = fastcache.New(100 * 1024 * 1024) 56 ) 57 58 var errInvalidPubkey = errors.New("invalid secp256k1 public key") 59 60 var keccakState256Pool = sync.Pool{ 61 New: func() interface{} { 62 return sha3.NewLegacyKeccak256().(KeccakState) 63 }} 64 65 // KeccakState wraps sha3.state. In addition to the usual hash methods, it also supports 66 // Read to get a variable amount of data from the hash state. Read is faster than Sum 67 // because it doesn't copy the internal state, but also modifies the internal state. 68 type KeccakState interface { 69 hash.Hash 70 Read([]byte) (int, error) 71 } 72 73 // NewKeccakState creates a new KeccakState 74 func NewKeccakState() KeccakState { 75 return sha3.NewLegacyKeccak256().(KeccakState) 76 } 77 78 // HashData hashes the provided data using the KeccakState and returns a 32 byte hash 79 func HashData(kh KeccakState, data []byte) (h common.Hash) { 80 if hash, ok := keccakState256Cache.HasGet(nil, data); ok { 81 return common.BytesToHash(hash) 82 } 83 kh.Reset() 84 kh.Write(data) 85 kh.Read(h[:]) 86 keccakState256Cache.Set(data, h.Bytes()) 87 return h 88 } 89 90 // Keccak256 calculates and returns the Keccak256 hash of the input data. 91 func Keccak256(data ...[]byte) []byte { 92 if len(data) == 1 { 93 if hash, ok := keccakState256Cache.HasGet(nil, data[0]); ok { 94 return hash 95 } 96 } 97 b := make([]byte, 32) 98 d := keccakState256Pool.Get().(KeccakState) 99 defer keccakState256Pool.Put(d) 100 d.Reset() 101 for _, b := range data { 102 d.Write(b) 103 } 104 d.Read(b) 105 if len(data) == 1 { 106 keccakState256Cache.Set(data[0], b) 107 } 108 return b 109 } 110 111 // Keccak256Hash calculates and returns the Keccak256 hash of the input data, 112 // converting it to an internal Hash data structure. 113 func Keccak256Hash(data ...[]byte) (h common.Hash) { 114 if len(data) == 1 { 115 if hash, ok := keccakState256Cache.HasGet(nil, data[0]); ok { 116 return common.BytesToHash(hash) 117 } 118 } 119 d := keccakState256Pool.Get().(KeccakState) 120 defer keccakState256Pool.Put(d) 121 d.Reset() 122 for _, b := range data { 123 d.Write(b) 124 } 125 d.Read(h[:]) 126 if len(data) == 1 { 127 keccakState256Cache.Set(data[0], h.Bytes()) 128 } 129 return h 130 } 131 132 // Keccak512 calculates and returns the Keccak512 hash of the input data. 133 func Keccak512(data ...[]byte) []byte { 134 d := sha3.NewLegacyKeccak512() 135 for _, b := range data { 136 d.Write(b) 137 } 138 return d.Sum(nil) 139 } 140 141 // CreateAddress creates an ethereum address given the bytes and the nonce 142 func CreateAddress(b common.Address, nonce uint64) common.Address { 143 data, _ := rlp.EncodeToBytes([]interface{}{b, nonce}) 144 return common.BytesToAddress(Keccak256(data)[12:]) 145 } 146 147 // CreateAddress2 creates an ethereum address given the address bytes, initial 148 // contract code hash and a salt. 149 func CreateAddress2(b common.Address, salt [32]byte, inithash []byte) common.Address { 150 return common.BytesToAddress(Keccak256([]byte{0xff}, b.Bytes(), salt[:], inithash)[12:]) 151 } 152 153 // ToECDSA creates a private key with the given D value. 154 func ToECDSA(d []byte) (*ecdsa.PrivateKey, error) { 155 return toECDSA(d, true) 156 } 157 158 // ToECDSAUnsafe blindly converts a binary blob to a private key. It should almost 159 // never be used unless you are sure the input is valid and want to avoid hitting 160 // errors due to bad origin encoding (0 prefixes cut off). 161 func ToECDSAUnsafe(d []byte) *ecdsa.PrivateKey { 162 priv, _ := toECDSA(d, false) 163 return priv 164 } 165 166 // toECDSA creates a private key with the given D value. The strict parameter 167 // controls whether the key's length should be enforced at the curve size or 168 // it can also accept legacy encodings (0 prefixes). 169 func toECDSA(d []byte, strict bool) (*ecdsa.PrivateKey, error) { 170 priv := new(ecdsa.PrivateKey) 171 priv.PublicKey.Curve = S256() 172 if strict && 8*len(d) != priv.Params().BitSize { 173 return nil, fmt.Errorf("invalid length, need %d bits", priv.Params().BitSize) 174 } 175 priv.D = new(big.Int).SetBytes(d) 176 177 // The priv.D must < N 178 if priv.D.Cmp(secp256k1N) >= 0 { 179 return nil, fmt.Errorf("invalid private key, >=N") 180 } 181 // The priv.D must not be zero or negative. 182 if priv.D.Sign() <= 0 { 183 return nil, fmt.Errorf("invalid private key, zero or negative") 184 } 185 186 priv.PublicKey.X, priv.PublicKey.Y = priv.PublicKey.Curve.ScalarBaseMult(d) 187 if priv.PublicKey.X == nil { 188 return nil, errors.New("invalid private key") 189 } 190 return priv, nil 191 } 192 193 // FromECDSA exports a private key into a binary dump. 194 func FromECDSA(priv *ecdsa.PrivateKey) []byte { 195 if priv == nil { 196 return nil 197 } 198 return math.PaddedBigBytes(priv.D, priv.Params().BitSize/8) 199 } 200 201 // UnmarshalPubkey converts bytes to a secp256k1 public key. 202 func UnmarshalPubkey(pub []byte) (*ecdsa.PublicKey, error) { 203 x, y := elliptic.Unmarshal(S256(), pub) 204 if x == nil { 205 return nil, errInvalidPubkey 206 } 207 return &ecdsa.PublicKey{Curve: S256(), X: x, Y: y}, nil 208 } 209 210 func FromECDSAPub(pub *ecdsa.PublicKey) []byte { 211 if pub == nil || pub.X == nil || pub.Y == nil { 212 return nil 213 } 214 return elliptic.Marshal(S256(), pub.X, pub.Y) 215 } 216 217 // HexToECDSA parses a secp256k1 private key. 218 func HexToECDSA(hexkey string) (*ecdsa.PrivateKey, error) { 219 b, err := hex.DecodeString(hexkey) 220 if byteErr, ok := err.(hex.InvalidByteError); ok { 221 return nil, fmt.Errorf("invalid hex character %q in private key", byte(byteErr)) 222 } else if err != nil { 223 return nil, errors.New("invalid hex data for private key") 224 } 225 return ToECDSA(b) 226 } 227 228 // LoadECDSA loads a secp256k1 private key from the given file. 229 func LoadECDSA(file string) (*ecdsa.PrivateKey, error) { 230 fd, err := os.Open(file) 231 if err != nil { 232 return nil, err 233 } 234 defer fd.Close() 235 236 r := bufio.NewReader(fd) 237 buf := make([]byte, 64) 238 n, err := readASCII(buf, r) 239 if err != nil { 240 return nil, err 241 } else if n != len(buf) { 242 return nil, fmt.Errorf("key file too short, want 64 hex characters") 243 } 244 if err := checkKeyFileEnd(r); err != nil { 245 return nil, err 246 } 247 248 return HexToECDSA(string(buf)) 249 } 250 251 // readASCII reads into 'buf', stopping when the buffer is full or 252 // when a non-printable control character is encountered. 253 func readASCII(buf []byte, r *bufio.Reader) (n int, err error) { 254 for ; n < len(buf); n++ { 255 buf[n], err = r.ReadByte() 256 switch { 257 case err == io.EOF || buf[n] < '!': 258 return n, nil 259 case err != nil: 260 return n, err 261 } 262 } 263 return n, nil 264 } 265 266 // checkKeyFileEnd skips over additional newlines at the end of a key file. 267 func checkKeyFileEnd(r *bufio.Reader) error { 268 for i := 0; ; i++ { 269 b, err := r.ReadByte() 270 switch { 271 case err == io.EOF: 272 return nil 273 case err != nil: 274 return err 275 case b != '\n' && b != '\r': 276 return fmt.Errorf("invalid character %q at end of key file", b) 277 case i >= 2: 278 return errors.New("key file too long, want 64 hex characters") 279 } 280 } 281 } 282 283 // SaveECDSA saves a secp256k1 private key to the given file with 284 // restrictive permissions. The key data is saved hex-encoded. 285 func SaveECDSA(file string, key *ecdsa.PrivateKey) error { 286 k := hex.EncodeToString(FromECDSA(key)) 287 return ioutil.WriteFile(file, []byte(k), 0600) 288 } 289 290 // GenerateKey generates a new private key. 291 func GenerateKey() (*ecdsa.PrivateKey, error) { 292 return ecdsa.GenerateKey(S256(), rand.Reader) 293 } 294 295 // ValidateSignatureValues verifies whether the signature values are valid with 296 // the given chain rules. The v value is assumed to be either 0 or 1. 297 func ValidateSignatureValues(v byte, r, s *big.Int, homestead bool) bool { 298 if r.Cmp(common.Big1) < 0 || s.Cmp(common.Big1) < 0 { 299 return false 300 } 301 // reject upper range of s values (ECDSA malleability) 302 // see discussion in secp256k1/libsecp256k1/include/secp256k1.h 303 if homestead && s.Cmp(secp256k1halfN) > 0 { 304 return false 305 } 306 // Frontier: allow s to be in full N range 307 return r.Cmp(secp256k1N) < 0 && s.Cmp(secp256k1N) < 0 && (v == 0 || v == 1) 308 } 309 310 func PubkeyToAddress(p ecdsa.PublicKey) common.Address { 311 pubBytes := FromECDSAPub(&p) 312 return common.BytesToAddress(Keccak256(pubBytes[1:])[12:]) 313 } 314 315 func zeroBytes(bytes []byte) { 316 for i := range bytes { 317 bytes[i] = 0 318 } 319 }