aboutsummaryrefslogtreecommitdiffstats
path: root/crypto
diff options
context:
space:
mode:
Diffstat (limited to 'crypto')
-rw-r--r--crypto/bn256/curve.go2
-rw-r--r--crypto/crypto.go5
-rw-r--r--crypto/crypto_test.go17
-rw-r--r--crypto/secp256k1/curve.go46
-rw-r--r--crypto/secp256k1/ext.h63
-rw-r--r--crypto/secp256k1/secp256.go51
-rw-r--r--crypto/signature_cgo.go23
-rw-r--r--crypto/signature_nocgo.go36
-rw-r--r--crypto/signature_test.go130
9 files changed, 324 insertions, 49 deletions
diff --git a/crypto/bn256/curve.go b/crypto/bn256/curve.go
index 233b1f252..3e679fdc7 100644
--- a/crypto/bn256/curve.go
+++ b/crypto/bn256/curve.go
@@ -20,7 +20,7 @@ var curveB = new(big.Int).SetInt64(3)
// curveGen is the generator of G₁.
var curveGen = &curvePoint{
new(big.Int).SetInt64(1),
- new(big.Int).SetInt64(-2),
+ new(big.Int).SetInt64(2),
new(big.Int).SetInt64(1),
new(big.Int).SetInt64(1),
}
diff --git a/crypto/crypto.go b/crypto/crypto.go
index 8161769d3..e51726e62 100644
--- a/crypto/crypto.go
+++ b/crypto/crypto.go
@@ -79,7 +79,7 @@ func ToECDSA(d []byte) (*ecdsa.PrivateKey, error) {
return toECDSA(d, true)
}
-// ToECDSAUnsafe blidly converts a binary blob to a private key. It should almost
+// ToECDSAUnsafe blindly converts a binary blob to a private key. It should almost
// never be used unless you are sure the input is valid and want to avoid hitting
// errors due to bad origin encoding (0 prefixes cut off).
func ToECDSAUnsafe(d []byte) *ecdsa.PrivateKey {
@@ -98,6 +98,9 @@ func toECDSA(d []byte, strict bool) (*ecdsa.PrivateKey, error) {
}
priv.D = new(big.Int).SetBytes(d)
priv.PublicKey.X, priv.PublicKey.Y = priv.PublicKey.Curve.ScalarBaseMult(d)
+ if priv.PublicKey.X == nil {
+ return nil, errors.New("invalid private key")
+ }
return priv, nil
}
diff --git a/crypto/crypto_test.go b/crypto/crypto_test.go
index 92302948e..835035462 100644
--- a/crypto/crypto_test.go
+++ b/crypto/crypto_test.go
@@ -20,12 +20,10 @@ import (
"bytes"
"crypto/ecdsa"
"encoding/hex"
- "fmt"
"io/ioutil"
"math/big"
"os"
"testing"
- "time"
"github.com/ethereum/go-ethereum/common"
)
@@ -42,15 +40,20 @@ func TestKeccak256Hash(t *testing.T) {
checkhash(t, "Sha3-256-array", func(in []byte) []byte { h := Keccak256Hash(in); return h[:] }, msg, exp)
}
+func TestToECDSAErrors(t *testing.T) {
+ if _, err := HexToECDSA("0000000000000000000000000000000000000000000000000000000000000000"); err == nil {
+ t.Fatal("HexToECDSA should've returned error")
+ }
+ if _, err := HexToECDSA("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"); err == nil {
+ t.Fatal("HexToECDSA should've returned error")
+ }
+}
+
func BenchmarkSha3(b *testing.B) {
a := []byte("hello world")
- amount := 1000000
- start := time.Now()
- for i := 0; i < amount; i++ {
+ for i := 0; i < b.N; i++ {
Keccak256(a)
}
-
- fmt.Println(amount, ":", time.Since(start))
}
func TestSign(t *testing.T) {
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
}
diff --git a/crypto/secp256k1/ext.h b/crypto/secp256k1/ext.h
index ee759fde6..9b043c724 100644
--- a/crypto/secp256k1/ext.h
+++ b/crypto/secp256k1/ext.h
@@ -19,7 +19,7 @@ static secp256k1_context* secp256k1_context_create_sign_verify() {
return secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
}
-// secp256k1_ecdsa_recover_pubkey recovers the public key of an encoded compact signature.
+// secp256k1_ext_ecdsa_recover recovers the public key of an encoded compact signature.
//
// Returns: 1: recovery was successful
// 0: recovery was not successful
@@ -27,7 +27,7 @@ static secp256k1_context* secp256k1_context_create_sign_verify() {
// Out: pubkey_out: the serialized 65-byte public key of the signer (cannot be NULL)
// In: sigdata: pointer to a 65-byte signature with the recovery id at the end (cannot be NULL)
// msgdata: pointer to a 32-byte message (cannot be NULL)
-static int secp256k1_ecdsa_recover_pubkey(
+static int secp256k1_ext_ecdsa_recover(
const secp256k1_context* ctx,
unsigned char *pubkey_out,
const unsigned char *sigdata,
@@ -46,7 +46,62 @@ static int secp256k1_ecdsa_recover_pubkey(
return secp256k1_ec_pubkey_serialize(ctx, pubkey_out, &outputlen, &pubkey, SECP256K1_EC_UNCOMPRESSED);
}
-// secp256k1_pubkey_scalar_mul multiplies a point by a scalar in constant time.
+// secp256k1_ext_ecdsa_verify verifies an encoded compact signature.
+//
+// Returns: 1: signature is valid
+// 0: signature is invalid
+// Args: ctx: pointer to a context object (cannot be NULL)
+// In: sigdata: pointer to a 64-byte signature (cannot be NULL)
+// msgdata: pointer to a 32-byte message (cannot be NULL)
+// pubkeydata: pointer to public key data (cannot be NULL)
+// pubkeylen: length of pubkeydata
+static int secp256k1_ext_ecdsa_verify(
+ const secp256k1_context* ctx,
+ const unsigned char *sigdata,
+ const unsigned char *msgdata,
+ const unsigned char *pubkeydata,
+ size_t pubkeylen
+) {
+ secp256k1_ecdsa_signature sig;
+ secp256k1_pubkey pubkey;
+
+ if (!secp256k1_ecdsa_signature_parse_compact(ctx, &sig, sigdata)) {
+ return 0;
+ }
+ if (!secp256k1_ec_pubkey_parse(ctx, &pubkey, pubkeydata, pubkeylen)) {
+ return 0;
+ }
+ return secp256k1_ecdsa_verify(ctx, &sig, msgdata, &pubkey);
+}
+
+// secp256k1_ext_reencode_pubkey decodes then encodes a public key. It can be used to
+// convert between public key formats. The input/output formats are chosen depending on the
+// length of the input/output buffers.
+//
+// Returns: 1: conversion successful
+// 0: conversion unsuccessful
+// Args: ctx: pointer to a context object (cannot be NULL)
+// Out: out: output buffer that will contain the reencoded key (cannot be NULL)
+// In: outlen: length of out (33 for compressed keys, 65 for uncompressed keys)
+// pubkeydata: the input public key (cannot be NULL)
+// pubkeylen: length of pubkeydata
+static int secp256k1_ext_reencode_pubkey(
+ const secp256k1_context* ctx,
+ unsigned char *out,
+ size_t outlen,
+ const unsigned char *pubkeydata,
+ size_t pubkeylen
+) {
+ secp256k1_pubkey pubkey;
+
+ if (!secp256k1_ec_pubkey_parse(ctx, &pubkey, pubkeydata, pubkeylen)) {
+ return 0;
+ }
+ unsigned int flag = (outlen == 33) ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED;
+ return secp256k1_ec_pubkey_serialize(ctx, out, &outlen, &pubkey, flag);
+}
+
+// secp256k1_ext_scalar_mul multiplies a point by a scalar in constant time.
//
// Returns: 1: multiplication was successful
// 0: scalar was invalid (zero or overflow)
@@ -55,7 +110,7 @@ static int secp256k1_ecdsa_recover_pubkey(
// In: point: pointer to a 64-byte public point,
// encoded as two 256bit big-endian numbers.
// scalar: a 32-byte scalar with which to multiply the point
-int secp256k1_pubkey_scalar_mul(const secp256k1_context* ctx, unsigned char *point, const unsigned char *scalar) {
+int secp256k1_ext_scalar_mul(const secp256k1_context* ctx, unsigned char *point, const unsigned char *scalar) {
int ret = 0;
int overflow = 0;
secp256k1_fe feX, feY;
diff --git a/crypto/secp256k1/secp256.go b/crypto/secp256k1/secp256.go
index 0ffa04fe0..eefbb99ee 100644
--- a/crypto/secp256k1/secp256.go
+++ b/crypto/secp256k1/secp256.go
@@ -38,6 +38,7 @@ import "C"
import (
"errors"
+ "math/big"
"unsafe"
)
@@ -55,6 +56,7 @@ var (
ErrInvalidSignatureLen = errors.New("invalid signature length")
ErrInvalidRecoveryID = errors.New("invalid signature recovery id")
ErrInvalidKey = errors.New("invalid private key")
+ ErrInvalidPubkey = errors.New("invalid public key")
ErrSignFailed = errors.New("signing failed")
ErrRecoverFailed = errors.New("recovery failed")
)
@@ -113,12 +115,59 @@ func RecoverPubkey(msg []byte, sig []byte) ([]byte, error) {
sigdata = (*C.uchar)(unsafe.Pointer(&sig[0]))
msgdata = (*C.uchar)(unsafe.Pointer(&msg[0]))
)
- if C.secp256k1_ecdsa_recover_pubkey(context, (*C.uchar)(unsafe.Pointer(&pubkey[0])), sigdata, msgdata) == 0 {
+ if C.secp256k1_ext_ecdsa_recover(context, (*C.uchar)(unsafe.Pointer(&pubkey[0])), sigdata, msgdata) == 0 {
return nil, ErrRecoverFailed
}
return pubkey, nil
}
+// VerifySignature checks that the given pubkey created signature over message.
+// The signature should be in [R || S] format.
+func VerifySignature(pubkey, msg, signature []byte) bool {
+ if len(msg) != 32 || len(signature) != 64 || len(pubkey) == 0 {
+ return false
+ }
+ sigdata := (*C.uchar)(unsafe.Pointer(&signature[0]))
+ msgdata := (*C.uchar)(unsafe.Pointer(&msg[0]))
+ keydata := (*C.uchar)(unsafe.Pointer(&pubkey[0]))
+ return C.secp256k1_ext_ecdsa_verify(context, sigdata, msgdata, keydata, C.size_t(len(pubkey))) != 0
+}
+
+// DecompressPubkey parses a public key in the 33-byte compressed format.
+// It returns non-nil coordinates if the public key is valid.
+func DecompressPubkey(pubkey []byte) (x, y *big.Int) {
+ if len(pubkey) != 33 {
+ return nil, nil
+ }
+ var (
+ pubkeydata = (*C.uchar)(unsafe.Pointer(&pubkey[0]))
+ pubkeylen = C.size_t(len(pubkey))
+ out = make([]byte, 65)
+ outdata = (*C.uchar)(unsafe.Pointer(&out[0]))
+ outlen = C.size_t(len(out))
+ )
+ if C.secp256k1_ext_reencode_pubkey(context, outdata, outlen, pubkeydata, pubkeylen) == 0 {
+ return nil, nil
+ }
+ return new(big.Int).SetBytes(out[1:33]), new(big.Int).SetBytes(out[33:])
+}
+
+// CompressPubkey encodes a public key to 33-byte compressed format.
+func CompressPubkey(x, y *big.Int) []byte {
+ var (
+ pubkey = S256().Marshal(x, y)
+ pubkeydata = (*C.uchar)(unsafe.Pointer(&pubkey[0]))
+ pubkeylen = C.size_t(len(pubkey))
+ out = make([]byte, 33)
+ outdata = (*C.uchar)(unsafe.Pointer(&out[0]))
+ outlen = C.size_t(len(out))
+ )
+ if C.secp256k1_ext_reencode_pubkey(context, outdata, outlen, pubkeydata, pubkeylen) == 0 {
+ panic("libsecp256k1 error")
+ }
+ return out
+}
+
func checkSignature(sig []byte) error {
if len(sig) != 65 {
return ErrInvalidSignatureLen
diff --git a/crypto/signature_cgo.go b/crypto/signature_cgo.go
index feec5e7be..340bfc221 100644
--- a/crypto/signature_cgo.go
+++ b/crypto/signature_cgo.go
@@ -27,10 +27,12 @@ import (
"github.com/ethereum/go-ethereum/crypto/secp256k1"
)
+// Ecrecover returns the uncompressed public key that created the given signature.
func Ecrecover(hash, sig []byte) ([]byte, error) {
return secp256k1.RecoverPubkey(hash, sig)
}
+// SigToPub returns the public key that created the given signature.
func SigToPub(hash, sig []byte) (*ecdsa.PublicKey, error) {
s, err := Ecrecover(hash, sig)
if err != nil {
@@ -58,6 +60,27 @@ func Sign(hash []byte, prv *ecdsa.PrivateKey) (sig []byte, err error) {
return secp256k1.Sign(hash, seckey)
}
+// VerifySignature checks that the given public key created signature over hash.
+// The public key should be in compressed (33 bytes) or uncompressed (65 bytes) format.
+// The signature should have the 64 byte [R || S] format.
+func VerifySignature(pubkey, hash, signature []byte) bool {
+ return secp256k1.VerifySignature(pubkey, hash, signature)
+}
+
+// DecompressPubkey parses a public key in the 33-byte compressed format.
+func DecompressPubkey(pubkey []byte) (*ecdsa.PublicKey, error) {
+ x, y := secp256k1.DecompressPubkey(pubkey)
+ if x == nil {
+ return nil, fmt.Errorf("invalid public key")
+ }
+ return &ecdsa.PublicKey{X: x, Y: y, Curve: S256()}, nil
+}
+
+// CompressPubkey encodes a public key to the 33-byte compressed format.
+func CompressPubkey(pubkey *ecdsa.PublicKey) []byte {
+ return secp256k1.CompressPubkey(pubkey.X, pubkey.Y)
+}
+
// S256 returns an instance of the secp256k1 curve.
func S256() elliptic.Curve {
return secp256k1.S256()
diff --git a/crypto/signature_nocgo.go b/crypto/signature_nocgo.go
index a022eef9a..78b99c02b 100644
--- a/crypto/signature_nocgo.go
+++ b/crypto/signature_nocgo.go
@@ -21,11 +21,14 @@ package crypto
import (
"crypto/ecdsa"
"crypto/elliptic"
+ "errors"
"fmt"
+ "math/big"
"github.com/btcsuite/btcd/btcec"
)
+// Ecrecover returns the uncompressed public key that created the given signature.
func Ecrecover(hash, sig []byte) ([]byte, error) {
pub, err := SigToPub(hash, sig)
if err != nil {
@@ -35,6 +38,7 @@ func Ecrecover(hash, sig []byte) ([]byte, error) {
return bytes, err
}
+// SigToPub returns the public key that created the given signature.
func SigToPub(hash, sig []byte) (*ecdsa.PublicKey, error) {
// Convert to btcec input format with 'recovery id' v at the beginning.
btcsig := make([]byte, 65)
@@ -71,6 +75,38 @@ func Sign(hash []byte, prv *ecdsa.PrivateKey) ([]byte, error) {
return sig, nil
}
+// VerifySignature checks that the given public key created signature over hash.
+// The public key should be in compressed (33 bytes) or uncompressed (65 bytes) format.
+// The signature should have the 64 byte [R || S] format.
+func VerifySignature(pubkey, hash, signature []byte) bool {
+ if len(signature) != 64 {
+ return false
+ }
+ sig := &btcec.Signature{R: new(big.Int).SetBytes(signature[:32]), S: new(big.Int).SetBytes(signature[32:])}
+ key, err := btcec.ParsePubKey(pubkey, btcec.S256())
+ if err != nil {
+ return false
+ }
+ return sig.Verify(hash, key)
+}
+
+// DecompressPubkey parses a public key in the 33-byte compressed format.
+func DecompressPubkey(pubkey []byte) (*ecdsa.PublicKey, error) {
+ if len(pubkey) != 33 {
+ return nil, errors.New("invalid compressed public key length")
+ }
+ key, err := btcec.ParsePubKey(pubkey, btcec.S256())
+ if err != nil {
+ return nil, err
+ }
+ return key.ToECDSA(), nil
+}
+
+// CompressPubkey encodes a public key to the 33-byte compressed format.
+func CompressPubkey(pubkey *ecdsa.PublicKey) []byte {
+ return (*btcec.PublicKey)(pubkey).SerializeCompressed()
+}
+
// S256 returns an instance of the secp256k1 curve.
func S256() elliptic.Curve {
return btcec.S256()
diff --git a/crypto/signature_test.go b/crypto/signature_test.go
index aefd9e38d..5e2efc7e0 100644
--- a/crypto/signature_test.go
+++ b/crypto/signature_test.go
@@ -18,19 +18,133 @@ package crypto
import (
"bytes"
- "encoding/hex"
+ "crypto/ecdsa"
+ "reflect"
"testing"
+
+ "github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/common/hexutil"
+ "github.com/ethereum/go-ethereum/common/math"
+)
+
+var (
+ testmsg = hexutil.MustDecode("0xce0677bb30baa8cf067c88db9811f4333d131bf8bcf12fe7065d211dce971008")
+ testsig = hexutil.MustDecode("0x90f27b8b488db00b00606796d2987f6a5f59ae62ea05effe84fef5b8b0e549984a691139ad57a3f0b906637673aa2f63d1f55cb1a69199d4009eea23ceaddc9301")
+ testpubkey = hexutil.MustDecode("0x04e32df42865e97135acfb65f3bae71bdc86f4d49150ad6a440b6f15878109880a0a2b2667f7e725ceea70c673093bf67663e0312623c8e091b13cf2c0f11ef652")
+ testpubkeyc = hexutil.MustDecode("0x02e32df42865e97135acfb65f3bae71bdc86f4d49150ad6a440b6f15878109880a")
)
-func TestRecoverSanity(t *testing.T) {
- msg, _ := hex.DecodeString("ce0677bb30baa8cf067c88db9811f4333d131bf8bcf12fe7065d211dce971008")
- sig, _ := hex.DecodeString("90f27b8b488db00b00606796d2987f6a5f59ae62ea05effe84fef5b8b0e549984a691139ad57a3f0b906637673aa2f63d1f55cb1a69199d4009eea23ceaddc9301")
- pubkey1, _ := hex.DecodeString("04e32df42865e97135acfb65f3bae71bdc86f4d49150ad6a440b6f15878109880a0a2b2667f7e725ceea70c673093bf67663e0312623c8e091b13cf2c0f11ef652")
- pubkey2, err := Ecrecover(msg, sig)
+func TestEcrecover(t *testing.T) {
+ pubkey, err := Ecrecover(testmsg, testsig)
if err != nil {
t.Fatalf("recover error: %s", err)
}
- if !bytes.Equal(pubkey1, pubkey2) {
- t.Errorf("pubkey mismatch: want: %x have: %x", pubkey1, pubkey2)
+ if !bytes.Equal(pubkey, testpubkey) {
+ t.Errorf("pubkey mismatch: want: %x have: %x", testpubkey, pubkey)
+ }
+}
+
+func TestVerifySignature(t *testing.T) {
+ sig := testsig[:len(testsig)-1] // remove recovery id
+ if !VerifySignature(testpubkey, testmsg, sig) {
+ t.Errorf("can't verify signature with uncompressed key")
+ }
+ if !VerifySignature(testpubkeyc, testmsg, sig) {
+ t.Errorf("can't verify signature with compressed key")
+ }
+
+ if VerifySignature(nil, testmsg, sig) {
+ t.Errorf("signature valid with no key")
+ }
+ if VerifySignature(testpubkey, nil, sig) {
+ t.Errorf("signature valid with no message")
+ }
+ if VerifySignature(testpubkey, testmsg, nil) {
+ t.Errorf("nil signature valid")
+ }
+ if VerifySignature(testpubkey, testmsg, append(common.CopyBytes(sig), 1, 2, 3)) {
+ t.Errorf("signature valid with extra bytes at the end")
+ }
+ if VerifySignature(testpubkey, testmsg, sig[:len(sig)-2]) {
+ t.Errorf("signature valid even though it's incomplete")
+ }
+ wrongkey := common.CopyBytes(testpubkey)
+ wrongkey[10]++
+ if VerifySignature(wrongkey, testmsg, sig) {
+ t.Errorf("signature valid with with wrong public key")
+ }
+}
+
+func TestDecompressPubkey(t *testing.T) {
+ key, err := DecompressPubkey(testpubkeyc)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if uncompressed := FromECDSAPub(key); !bytes.Equal(uncompressed, testpubkey) {
+ t.Errorf("wrong public key result: got %x, want %x", uncompressed, testpubkey)
+ }
+ if _, err := DecompressPubkey(nil); err == nil {
+ t.Errorf("no error for nil pubkey")
+ }
+ if _, err := DecompressPubkey(testpubkeyc[:5]); err == nil {
+ t.Errorf("no error for incomplete pubkey")
+ }
+ if _, err := DecompressPubkey(append(common.CopyBytes(testpubkeyc), 1, 2, 3)); err == nil {
+ t.Errorf("no error for pubkey with extra bytes at the end")
+ }
+}
+
+func TestCompressPubkey(t *testing.T) {
+ key := &ecdsa.PublicKey{
+ Curve: S256(),
+ X: math.MustParseBig256("0xe32df42865e97135acfb65f3bae71bdc86f4d49150ad6a440b6f15878109880a"),
+ Y: math.MustParseBig256("0x0a2b2667f7e725ceea70c673093bf67663e0312623c8e091b13cf2c0f11ef652"),
+ }
+ compressed := CompressPubkey(key)
+ if !bytes.Equal(compressed, testpubkeyc) {
+ t.Errorf("wrong public key result: got %x, want %x", compressed, testpubkeyc)
+ }
+}
+
+func TestPubkeyRandom(t *testing.T) {
+ const runs = 200
+
+ for i := 0; i < runs; i++ {
+ key, err := GenerateKey()
+ if err != nil {
+ t.Fatalf("iteration %d: %v", i, err)
+ }
+ pubkey2, err := DecompressPubkey(CompressPubkey(&key.PublicKey))
+ if err != nil {
+ t.Fatalf("iteration %d: %v", i, err)
+ }
+ if !reflect.DeepEqual(key.PublicKey, *pubkey2) {
+ t.Fatalf("iteration %d: keys not equal", i)
+ }
+ }
+}
+
+func BenchmarkEcrecoverSignature(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ if _, err := Ecrecover(testmsg, testsig); err != nil {
+ b.Fatal("ecrecover error", err)
+ }
+ }
+}
+
+func BenchmarkVerifySignature(b *testing.B) {
+ sig := testsig[:len(testsig)-1] // remove recovery id
+ for i := 0; i < b.N; i++ {
+ if !VerifySignature(testpubkey, testmsg, sig) {
+ b.Fatal("verify error")
+ }
+ }
+}
+
+func BenchmarkDecompressPubkey(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ if _, err := DecompressPubkey(testpubkeyc); err != nil {
+ b.Fatal(err)
+ }
}
}