diff options
author | Felix Lange <fjl@users.noreply.github.com> | 2017-12-15 17:40:09 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-15 17:40:09 +0800 |
commit | c6069a627c42c21fc02d0770d39db9a9be45b180 (patch) | |
tree | 45b43eef0ead6f3bf83e0f0bec1c48b02eefc374 /crypto/secp256k1/curve.go | |
parent | 1f2176dedc369f31f77927c2743b64868bf26b3e (diff) | |
download | go-tangerine-c6069a627c42c21fc02d0770d39db9a9be45b180.tar go-tangerine-c6069a627c42c21fc02d0770d39db9a9be45b180.tar.gz go-tangerine-c6069a627c42c21fc02d0770d39db9a9be45b180.tar.bz2 go-tangerine-c6069a627c42c21fc02d0770d39db9a9be45b180.tar.lz go-tangerine-c6069a627c42c21fc02d0770d39db9a9be45b180.tar.xz go-tangerine-c6069a627c42c21fc02d0770d39db9a9be45b180.tar.zst go-tangerine-c6069a627c42c21fc02d0770d39db9a9be45b180.zip |
crypto, crypto/secp256k1: add CompressPubkey (#15626)
This adds the inverse to DecompressPubkey and improves a few minor
details in crypto/secp256k1.
Diffstat (limited to 'crypto/secp256k1/curve.go')
-rw-r--r-- | crypto/secp256k1/curve.go | 46 |
1 files changed, 19 insertions, 27 deletions
diff --git a/crypto/secp256k1/curve.go b/crypto/secp256k1/curve.go index ec6d266ce..df8048185 100644 --- a/crypto/secp256k1/curve.go +++ b/crypto/secp256k1/curve.go @@ -34,7 +34,6 @@ package secp256k1 import ( "crypto/elliptic" "math/big" - "sync" "unsafe" "github.com/ethereum/go-ethereum/common/math" @@ -42,7 +41,7 @@ import ( /* #include "libsecp256k1/include/secp256k1.h" -extern int secp256k1_pubkey_scalar_mul(const secp256k1_context* ctx, const unsigned char *point, const unsigned char *scalar); +extern int secp256k1_ext_scalar_mul(const secp256k1_context* ctx, const unsigned char *point, const unsigned char *scalar); */ import "C" @@ -236,7 +235,7 @@ func (BitCurve *BitCurve) ScalarMult(Bx, By *big.Int, scalar []byte) (*big.Int, math.ReadBits(By, point[32:]) pointPtr := (*C.uchar)(unsafe.Pointer(&point[0])) scalarPtr := (*C.uchar)(unsafe.Pointer(&scalar[0])) - res := C.secp256k1_pubkey_scalar_mul(context, pointPtr, scalarPtr) + res := C.secp256k1_ext_scalar_mul(context, pointPtr, scalarPtr) // Unpack the result and clear temporaries. x := new(big.Int).SetBytes(point[:32]) @@ -263,14 +262,10 @@ func (BitCurve *BitCurve) ScalarBaseMult(k []byte) (*big.Int, *big.Int) { // X9.62. func (BitCurve *BitCurve) Marshal(x, y *big.Int) []byte { byteLen := (BitCurve.BitSize + 7) >> 3 - ret := make([]byte, 1+2*byteLen) - ret[0] = 4 // uncompressed point - - xBytes := x.Bytes() - copy(ret[1+byteLen-len(xBytes):], xBytes) - yBytes := y.Bytes() - copy(ret[1+2*byteLen-len(yBytes):], yBytes) + ret[0] = 4 // uncompressed point flag + math.ReadBits(x, ret[1:1+byteLen]) + math.ReadBits(y, ret[1+byteLen:]) return ret } @@ -289,24 +284,21 @@ func (BitCurve *BitCurve) Unmarshal(data []byte) (x, y *big.Int) { return } -var ( - initonce sync.Once - theCurve *BitCurve -) +var theCurve = new(BitCurve) + +func init() { + // See SEC 2 section 2.7.1 + // curve parameters taken from: + // http://www.secg.org/collateral/sec2_final.pdf + theCurve.P, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 16) + theCurve.N, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16) + theCurve.B, _ = new(big.Int).SetString("0000000000000000000000000000000000000000000000000000000000000007", 16) + theCurve.Gx, _ = new(big.Int).SetString("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", 16) + theCurve.Gy, _ = new(big.Int).SetString("483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8", 16) + theCurve.BitSize = 256 +} -// S256 returns a BitCurve which implements secp256k1 (see SEC 2 section 2.7.1) +// S256 returns a BitCurve which implements secp256k1. func S256() *BitCurve { - initonce.Do(func() { - // See SEC 2 section 2.7.1 - // curve parameters taken from: - // http://www.secg.org/collateral/sec2_final.pdf - theCurve = new(BitCurve) - theCurve.P, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 16) - theCurve.N, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16) - theCurve.B, _ = new(big.Int).SetString("0000000000000000000000000000000000000000000000000000000000000007", 16) - theCurve.Gx, _ = new(big.Int).SetString("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", 16) - theCurve.Gy, _ = new(big.Int).SetString("483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8", 16) - theCurve.BitSize = 256 - }) return theCurve } |