aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/crypto.go
diff options
context:
space:
mode:
authorGustav Simonsson <gustav.simonsson@gmail.com>2015-09-30 01:37:44 +0800
committerGustav Simonsson <gustav.simonsson@gmail.com>2015-11-30 20:43:32 +0800
commitc8ad64f33cd04fc10ac6681260ea06e464908c91 (patch)
treebd48055c50b57e2b17ca0bde4e9e5ae9ba7ca5ce /crypto/crypto.go
parent27a50c8f4bc69f98e20db361859bfbb6cf371c00 (diff)
downloadgo-tangerine-c8ad64f33cd04fc10ac6681260ea06e464908c91.tar
go-tangerine-c8ad64f33cd04fc10ac6681260ea06e464908c91.tar.gz
go-tangerine-c8ad64f33cd04fc10ac6681260ea06e464908c91.tar.bz2
go-tangerine-c8ad64f33cd04fc10ac6681260ea06e464908c91.tar.lz
go-tangerine-c8ad64f33cd04fc10ac6681260ea06e464908c91.tar.xz
go-tangerine-c8ad64f33cd04fc10ac6681260ea06e464908c91.tar.zst
go-tangerine-c8ad64f33cd04fc10ac6681260ea06e464908c91.zip
crypto, crypto/ecies, crypto/secp256k1: libsecp256k1 scalar mult
thanks to Felix Lange (fjl) for help with design & impl
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) {