@@ -20,22 +20,21 @@ import (
2020 "crypto/ecdsa"
2121 "crypto/elliptic"
2222 "crypto/rand"
23- "crypto/sha256 "
24- "fmt "
23+ "encoding/hex "
24+ "errors "
2525 "io"
2626 "io/ioutil"
2727 "math/big"
2828 "os"
2929
30- "encoding/hex"
31- "errors"
32-
3330 "github.com/ethereum/go-ethereum/common"
34- "github.com/ethereum/go-ethereum/crypto/ecies"
35- "github.com/ethereum/go-ethereum/crypto/secp256k1"
3631 "github.com/ethereum/go-ethereum/crypto/sha3"
3732 "github.com/ethereum/go-ethereum/rlp"
38- "golang.org/x/crypto/ripemd160"
33+ )
34+
35+ var (
36+ secp256k1_N , _ = new (big.Int ).SetString ("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141" , 16 )
37+ secp256k1_halfN = new (big.Int ).Div (secp256k1_N , big .NewInt (2 ))
3938)
4039
4140func Keccak256 (data ... []byte ) []byte {
@@ -56,7 +55,6 @@ func Keccak256Hash(data ...[]byte) (h common.Hash) {
5655}
5756
5857// Deprecated: For backward compatibility as other packages depend on these
59- func Sha3 (data ... []byte ) []byte { return Keccak256 (data ... ) }
6058func Sha3Hash (data ... []byte ) common.Hash { return Keccak256Hash (data ... ) }
6159
6260// Creates an ethereum address given the bytes and the nonce
@@ -65,39 +63,16 @@ func CreateAddress(b common.Address, nonce uint64) common.Address {
6563 return common .BytesToAddress (Keccak256 (data )[12 :])
6664}
6765
68- func Sha256 (data []byte ) []byte {
69- hash := sha256 .Sum256 (data )
70-
71- return hash [:]
72- }
73-
74- func Ripemd160 (data []byte ) []byte {
75- ripemd := ripemd160 .New ()
76- ripemd .Write (data )
77-
78- return ripemd .Sum (nil )
79- }
80-
81- // Ecrecover returns the public key for the private key that was used to
82- // calculate the signature.
83- //
84- // Note: secp256k1 expects the recover id to be either 0, 1. Ethereum
85- // signatures have a recover id with an offset of 27. Callers must take
86- // this into account and if "recovering" from an Ethereum signature adjust.
87- func Ecrecover (hash , sig []byte ) ([]byte , error ) {
88- return secp256k1 .RecoverPubkey (hash , sig )
89- }
90-
91- // New methods using proper ecdsa keys from the stdlib
66+ // ToECDSA creates a private key with the given D value.
9267func ToECDSA (prv []byte ) * ecdsa.PrivateKey {
9368 if len (prv ) == 0 {
9469 return nil
9570 }
9671
9772 priv := new (ecdsa.PrivateKey )
98- priv .PublicKey .Curve = secp256k1 . S256 ()
73+ priv .PublicKey .Curve = S256 ()
9974 priv .D = common .BigD (prv )
100- priv .PublicKey .X , priv .PublicKey .Y = secp256k1 . S256 () .ScalarBaseMult (prv )
75+ priv .PublicKey .X , priv .PublicKey .Y = priv . PublicKey . Curve .ScalarBaseMult (prv )
10176 return priv
10277}
10378
@@ -112,15 +87,15 @@ func ToECDSAPub(pub []byte) *ecdsa.PublicKey {
11287 if len (pub ) == 0 {
11388 return nil
11489 }
115- x , y := elliptic .Unmarshal (secp256k1 . S256 (), pub )
116- return & ecdsa.PublicKey {Curve : secp256k1 . S256 (), X : x , Y : y }
90+ x , y := elliptic .Unmarshal (S256 (), pub )
91+ return & ecdsa.PublicKey {Curve : S256 (), X : x , Y : y }
11792}
11893
11994func FromECDSAPub (pub * ecdsa.PublicKey ) []byte {
12095 if pub == nil || pub .X == nil || pub .Y == nil {
12196 return nil
12297 }
123- return elliptic .Marshal (secp256k1 . S256 (), pub .X , pub .Y )
98+ return elliptic .Marshal (S256 (), pub .X , pub .Y )
12499}
125100
126101// HexToECDSA parses a secp256k1 private key.
@@ -164,7 +139,7 @@ func SaveECDSA(file string, key *ecdsa.PrivateKey) error {
164139}
165140
166141func GenerateKey () (* ecdsa.PrivateKey , error ) {
167- return ecdsa .GenerateKey (secp256k1 . S256 (), rand .Reader )
142+ return ecdsa .GenerateKey (S256 (), rand .Reader )
168143}
169144
170145// ValidateSignatureValues verifies whether the signature values are valid with
@@ -175,49 +150,11 @@ func ValidateSignatureValues(v byte, r, s *big.Int, homestead bool) bool {
175150 }
176151 // reject upper range of s values (ECDSA malleability)
177152 // see discussion in secp256k1/libsecp256k1/include/secp256k1.h
178- if homestead && s .Cmp (secp256k1 . HalfN ) > 0 {
153+ if homestead && s .Cmp (secp256k1_halfN ) > 0 {
179154 return false
180155 }
181156 // Frontier: allow s to be in full N range
182- return r .Cmp (secp256k1 .N ) < 0 && s .Cmp (secp256k1 .N ) < 0 && (v == 0 || v == 1 )
183- }
184-
185- func SigToPub (hash , sig []byte ) (* ecdsa.PublicKey , error ) {
186- s , err := Ecrecover (hash , sig )
187- if err != nil {
188- return nil , err
189- }
190-
191- x , y := elliptic .Unmarshal (secp256k1 .S256 (), s )
192- return & ecdsa.PublicKey {Curve : secp256k1 .S256 (), X : x , Y : y }, nil
193- }
194-
195- // Sign calculates an ECDSA signature.
196- //
197- // This function is susceptible to chosen plaintext attacks that can leak
198- // information about the private key that is used for signing. Callers must
199- // be aware that the given hash cannot be chosen by an adversery. Common
200- // solution is to hash any input before calculating the signature.
201- //
202- // The produced signature is in the [R || S || V] format where V is 0 or 1.
203- func Sign (data []byte , prv * ecdsa.PrivateKey ) (sig []byte , err error ) {
204- if len (data ) != 32 {
205- return nil , fmt .Errorf ("hash is required to be exactly 32 bytes (%d)" , len (data ))
206- }
207-
208- seckey := common .LeftPadBytes (prv .D .Bytes (), prv .Params ().BitSize / 8 )
209- defer zeroBytes (seckey )
210- sig , err = secp256k1 .Sign (data , seckey )
211- return
212- }
213-
214- func Encrypt (pub * ecdsa.PublicKey , message []byte ) ([]byte , error ) {
215- return ecies .Encrypt (rand .Reader , ecies .ImportECDSAPublic (pub ), message , nil , nil )
216- }
217-
218- func Decrypt (prv * ecdsa.PrivateKey , ct []byte ) ([]byte , error ) {
219- key := ecies .ImportECDSA (prv )
220- return key .Decrypt (rand .Reader , ct , nil , nil )
157+ return r .Cmp (secp256k1_N ) < 0 && s .Cmp (secp256k1_N ) < 0 && (v == 0 || v == 1 )
221158}
222159
223160func PubkeyToAddress (p ecdsa.PublicKey ) common.Address {
0 commit comments