github.com/Gessiux/neatchain@v1.3.1/utilities/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 "crypto/ecdsa" 21 "crypto/elliptic" 22 "crypto/rand" 23 "crypto/sha256" 24 "encoding/hex" 25 "errors" 26 "fmt" 27 "hash" 28 "io" 29 "io/ioutil" 30 "math/big" 31 "os" 32 "strings" 33 34 "github.com/Gessiux/neatchain/utilities/common" 35 "github.com/Gessiux/neatchain/utilities/common/math" 36 "github.com/Gessiux/neatchain/utilities/rlp" 37 "github.com/btcsuite/btcutil/base58" 38 "golang.org/x/crypto/ripemd160" 39 "golang.org/x/crypto/sha3" 40 ) 41 42 var ( 43 pubkeyVersion = byte(0x00) 44 scriptVersion = byte(0x42) 45 addressPrefix = "NEA" 46 bs58Str = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" 47 secp256k1N, _ = new(big.Int).SetString("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141", 16) 48 secp256k1halfN = new(big.Int).Div(secp256k1N, big.NewInt(2)) 49 ) 50 51 var errInvalidPubkey = errors.New("invalid secp256k1 public key") 52 53 // Keccak256 calculates and returns the Keccak256 hash of the input data. 54 func Keccak256(data ...[]byte) []byte { 55 d := sha3.NewLegacyKeccak256() 56 for _, b := range data { 57 d.Write(b) 58 } 59 return d.Sum(nil) 60 } 61 62 // Keccak256Hash calculates and returns the Keccak256 hash of the input data, 63 // converting it to an internal Hash data structure. 64 func Keccak256Hash(data ...[]byte) (h common.Hash) { 65 d := sha3.NewLegacyKeccak256() 66 for _, b := range data { 67 d.Write(b) 68 } 69 d.Sum(h[:0]) 70 return h 71 } 72 73 // Keccak512 calculates and returns the Keccak512 hash of the input data. 74 func Keccak512(data ...[]byte) []byte { 75 d := sha3.NewLegacyKeccak512() 76 for _, b := range data { 77 d.Write(b) 78 } 79 return d.Sum(nil) 80 } 81 82 // Creates an ethereum address given the bytes and the nonce 83 //func CreateAddress(b common.Address, nonce uint64) common.Address { 84 // data, _ := rlp.EncodeToBytes([]interface{}{b, nonce}) 85 // return common.BytesToAddress(Keccak256(data)[12:]) 86 //} 87 88 // 89 func CreateAddress(b common.Address, nonce uint64) common.Address { 90 data, _ := rlp.EncodeToBytes([]interface{}{b, nonce}) 91 return common.StringToAddress(NewNEATScriptAddr(data)) 92 } 93 94 // CreateAddress2 creates an ethereum address given the address bytes, initial 95 // contract code and a salt. 96 //func CreateAddress2(b common.Address, salt [32]byte, code []byte) common.Address { 97 // return common.BytesToAddress(Keccak256([]byte{0xff}, b.Bytes(), salt[:], Keccak256(code))[12:]) 98 //} 99 100 func CreateAddress2(b common.Address, salt [32]byte, inithash []byte) common.Address { 101 return common.BytesToAddress([]byte(NewNEATScriptAddr(Keccak256([]byte{0xff}, b.Bytes(), salt[:], inithash)))) 102 } 103 104 // ToECDSA creates a private key with the given D value. 105 func ToECDSA(d []byte) (*ecdsa.PrivateKey, error) { 106 return toECDSA(d, true) 107 } 108 109 // ToECDSAUnsafe blindly converts a binary blob to a private key. It should almost 110 // never be used unless you are sure the input is valid and want to avoid hitting 111 // errors due to bad origin encoding (0 prefixes cut off). 112 func ToECDSAUnsafe(d []byte) *ecdsa.PrivateKey { 113 priv, _ := toECDSA(d, false) 114 return priv 115 } 116 117 // toECDSA creates a private key with the given D value. The strict parameter 118 // controls whether the key's length should be enforced at the curve size or 119 // it can also accept legacy encodings (0 prefixes). 120 func toECDSA(d []byte, strict bool) (*ecdsa.PrivateKey, error) { 121 priv := new(ecdsa.PrivateKey) 122 priv.PublicKey.Curve = S256() 123 if strict && 8*len(d) != priv.Params().BitSize { 124 return nil, fmt.Errorf("invalid length, need %d bits", priv.Params().BitSize) 125 } 126 priv.D = new(big.Int).SetBytes(d) 127 128 // The priv.D must < N 129 if priv.D.Cmp(secp256k1N) >= 0 { 130 return nil, fmt.Errorf("invalid private key, >=N") 131 } 132 // The priv.D must not be zero or negative. 133 if priv.D.Sign() <= 0 { 134 return nil, fmt.Errorf("invalid private key, zero or negative") 135 } 136 137 priv.PublicKey.X, priv.PublicKey.Y = priv.PublicKey.Curve.ScalarBaseMult(d) 138 if priv.PublicKey.X == nil { 139 return nil, errors.New("invalid private key") 140 } 141 return priv, nil 142 } 143 144 // FromECDSA exports a private key into a binary dump. 145 func FromECDSA(priv *ecdsa.PrivateKey) []byte { 146 if priv == nil { 147 return nil 148 } 149 return math.PaddedBigBytes(priv.D, priv.Params().BitSize/8) 150 } 151 152 func ToECDSAPub(pub []byte) *ecdsa.PublicKey { 153 if len(pub) == 0 { 154 return nil 155 } 156 x, y := elliptic.Unmarshal(S256(), pub) 157 return &ecdsa.PublicKey{Curve: S256(), X: x, Y: y} 158 } 159 160 // UnmarshalPubkey converts bytes to a secp256k1 public key. 161 func UnmarshalPubkey(pub []byte) (*ecdsa.PublicKey, error) { 162 x, y := elliptic.Unmarshal(S256(), pub) 163 if x == nil { 164 return nil, errInvalidPubkey 165 } 166 return &ecdsa.PublicKey{Curve: S256(), X: x, Y: y}, nil 167 } 168 169 func FromECDSAPub(pub *ecdsa.PublicKey) []byte { 170 if pub == nil || pub.X == nil || pub.Y == nil { 171 return nil 172 } 173 return elliptic.Marshal(S256(), pub.X, pub.Y) 174 //return Marshal(S256(), pub.X, pub.Y) 175 } 176 177 // HexToECDSA parses a secp256k1 private key. 178 func HexToECDSA(hexkey string) (*ecdsa.PrivateKey, error) { 179 b, err := hex.DecodeString(hexkey) 180 if err != nil { 181 return nil, errors.New("invalid hex string") 182 } 183 return ToECDSA(b) 184 } 185 186 // LoadECDSA loads a secp256k1 private key from the given file. 187 func LoadECDSA(file string) (*ecdsa.PrivateKey, error) { 188 buf := make([]byte, 64) 189 fd, err := os.Open(file) 190 if err != nil { 191 return nil, err 192 } 193 defer fd.Close() 194 if _, err := io.ReadFull(fd, buf); err != nil { 195 return nil, err 196 } 197 198 key, err := hex.DecodeString(string(buf)) 199 if err != nil { 200 return nil, err 201 } 202 return ToECDSA(key) 203 } 204 205 // SaveECDSA saves a secp256k1 private key to the given file with 206 // restrictive permissions. The key data is saved hex-encoded. 207 func SaveECDSA(file string, key *ecdsa.PrivateKey) error { 208 k := hex.EncodeToString(FromECDSA(key)) 209 return ioutil.WriteFile(file, []byte(k), 0600) 210 } 211 212 func GenerateKey() (*ecdsa.PrivateKey, error) { 213 return ecdsa.GenerateKey(S256(), rand.Reader) 214 } 215 216 // ValidateSignatureValues verifies whether the signature values are valid with 217 // the given chain rules. The v value is assumed to be either 0 or 1. 218 func ValidateSignatureValues(v byte, r, s *big.Int, homestead bool) bool { 219 if r.Cmp(common.Big1) < 0 || s.Cmp(common.Big1) < 0 { 220 return false 221 } 222 // reject upper range of s values (ECDSA malleability) 223 // see discussion in secp256k1/libsecp256k1/include/secp256k1.h 224 if homestead && s.Cmp(secp256k1halfN) > 0 { 225 return false 226 } 227 // Frontier: allow s to be in full N range 228 return r.Cmp(secp256k1N) < 0 && s.Cmp(secp256k1N) < 0 && (v == 0 || v == 1) 229 } 230 231 //func PubkeyToAddress(p ecdsa.PublicKey) common.Address { 232 // pubBytes := FromECDSAPub(&p) 233 // return common.BytesToAddress(Keccak256(pubBytes[1:])[12:]) 234 //} 235 236 func PubkeyToAddress(p ecdsa.PublicKey) common.Address { 237 pubBytes := FromECDSAPub(&p) 238 return common.StringToAddress(NewNEATScriptAddr(pubBytes)) 239 } 240 241 func zeroBytes(bytes []byte) { 242 for i := range bytes { 243 bytes[i] = 0 244 } 245 } 246 247 // P2PH 248 func NewNEATPubkeyAddr(pubkey []byte) string { 249 input := Hash160(pubkey) 250 251 return encodeAddress(input, pubkeyVersion) 252 } 253 254 // P2SH 255 func NewNEATScriptAddr(script []byte) string { 256 input := Hash160(script) 257 strArray := strings.Split(encodeAddress(input, scriptVersion), "") 258 return addressPrefix + strings.Join(strArray[:29], "") 259 } 260 261 // check NEAT address is validate or not 262 func ValidateNEATAddr(input string) bool { 263 inputByte := []byte(input) 264 if len(inputByte) != 32 { 265 return false 266 } 267 268 if inputByte[0] != 'N' || inputByte[1] != 'E' || inputByte[2] != 'A' || inputByte[3] != 'T' { 269 return false 270 } 271 272 inputArray := strings.Split(input, "")[3:] 273 274 for _, v := range inputArray { 275 if !strings.Contains(bs58Str, v) { 276 return false 277 } 278 } 279 280 return true 281 } 282 283 // encodeAddress returns a human-readable payment address given a ripemd160 hash 284 // and netID which encodes the bitcoin network and address type. It is used 285 // in both pay-to-pubkey-hash (P2PKH) and pay-to-script-hash (P2SH) address 286 // encoding. 287 func encodeAddress(hash160 []byte, version byte) string { 288 // Format is 1 byte for a network and address class (i.e. P2PKH vs 289 // P2SH), 20 bytes for a RIPEMD160 hash, and 4 bytes of checksum. 290 return base58.CheckEncode(hash160, version) 291 } 292 293 // Calculate the hash of hasher over buf. 294 func calcHash(buf []byte, hasher hash.Hash) []byte { 295 hasher.Write(buf) 296 return hasher.Sum(nil) 297 } 298 299 // Hash160 calculates the hash ripemd160(sha256(b)). 300 func Hash160(buf []byte) []byte { 301 return calcHash(calcHash(buf, sha256.New()), ripemd160.New()) 302 }