aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/crypto.go
diff options
context:
space:
mode:
authorJeffrey Wilcke <jeffrey@ethereum.org>2015-12-02 20:19:33 +0800
committerJeffrey Wilcke <jeffrey@ethereum.org>2015-12-02 20:19:33 +0800
commit888e7bc765acc6bfe0b8afc4ecd9830394b0a026 (patch)
tree5a27f4efd453c776f35ace6ae08c28c11417f9d5 /crypto/crypto.go
parent8db9d44ca9fb6baf406256cae491c475de2f4989 (diff)
parentc8ad64f33cd04fc10ac6681260ea06e464908c91 (diff)
downloaddexon-888e7bc765acc6bfe0b8afc4ecd9830394b0a026.tar
dexon-888e7bc765acc6bfe0b8afc4ecd9830394b0a026.tar.gz
dexon-888e7bc765acc6bfe0b8afc4ecd9830394b0a026.tar.bz2
dexon-888e7bc765acc6bfe0b8afc4ecd9830394b0a026.tar.lz
dexon-888e7bc765acc6bfe0b8afc4ecd9830394b0a026.tar.xz
dexon-888e7bc765acc6bfe0b8afc4ecd9830394b0a026.tar.zst
dexon-888e7bc765acc6bfe0b8afc4ecd9830394b0a026.zip
Merge pull request #1862 from Gustav-Simonsson/libsecp256k1_ecdh
crypto, crypto/secp256k1: use libsecp256k1 for scalar multiplication
Diffstat (limited to 'crypto/crypto.go')
-rw-r--r--crypto/crypto.go26
1 files changed, 9 insertions, 17 deletions
diff --git a/crypto/crypto.go b/crypto/crypto.go
index 8685d62d3..7d7623753 100644
--- a/crypto/crypto.go
+++ b/crypto/crypto.go
@@ -43,14 +43,6 @@ import (
"golang.org/x/crypto/ripemd160"
)
-var secp256k1n *big.Int
-
-func init() {
- // specify the params for the s256 curve
- ecies.AddParamsForCurve(S256(), ecies.ECIES_AES128_SHA256)
- secp256k1n = common.String2Big("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141")
-}
-
func Sha3(data ...[]byte) []byte {
d := sha3.NewKeccak256()
for _, b := range data {
@@ -99,9 +91,9 @@ func ToECDSA(prv []byte) *ecdsa.PrivateKey {
}
priv := new(ecdsa.PrivateKey)
- priv.PublicKey.Curve = S256()
+ priv.PublicKey.Curve = secp256k1.S256()
priv.D = common.BigD(prv)
- priv.PublicKey.X, priv.PublicKey.Y = S256().ScalarBaseMult(prv)
+ priv.PublicKey.X, priv.PublicKey.Y = secp256k1.S256().ScalarBaseMult(prv)
return priv
}
@@ -116,15 +108,15 @@ func ToECDSAPub(pub []byte) *ecdsa.PublicKey {
if len(pub) == 0 {
return nil
}
- x, y := elliptic.Unmarshal(S256(), pub)
- return &ecdsa.PublicKey{S256(), x, y}
+ x, y := elliptic.Unmarshal(secp256k1.S256(), pub)
+ return &ecdsa.PublicKey{secp256k1.S256(), x, y}
}
func FromECDSAPub(pub *ecdsa.PublicKey) []byte {
if pub == nil || pub.X == nil || pub.Y == nil {
return nil
}
- return elliptic.Marshal(S256(), pub.X, pub.Y)
+ return elliptic.Marshal(secp256k1.S256(), pub.X, pub.Y)
}
// HexToECDSA parses a secp256k1 private key.
@@ -168,7 +160,7 @@ func SaveECDSA(file string, key *ecdsa.PrivateKey) error {
}
func GenerateKey() (*ecdsa.PrivateKey, error) {
- return ecdsa.GenerateKey(S256(), rand.Reader)
+ return ecdsa.GenerateKey(secp256k1.S256(), rand.Reader)
}
func ValidateSignatureValues(v byte, r, s *big.Int) bool {
@@ -176,7 +168,7 @@ func ValidateSignatureValues(v byte, r, s *big.Int) bool {
return false
}
vint := uint32(v)
- if r.Cmp(secp256k1n) < 0 && s.Cmp(secp256k1n) < 0 && (vint == 27 || vint == 28) {
+ if r.Cmp(secp256k1.N) < 0 && s.Cmp(secp256k1.N) < 0 && (vint == 27 || vint == 28) {
return true
} else {
return false
@@ -189,8 +181,8 @@ func SigToPub(hash, sig []byte) (*ecdsa.PublicKey, error) {
return nil, err
}
- x, y := elliptic.Unmarshal(S256(), s)
- return &ecdsa.PublicKey{S256(), x, y}, nil
+ x, y := elliptic.Unmarshal(secp256k1.S256(), s)
+ return &ecdsa.PublicKey{secp256k1.S256(), x, y}, nil
}
func Sign(hash []byte, prv *ecdsa.PrivateKey) (sig []byte, err error) {