aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/signature_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/signature_test.go')
-rw-r--r--crypto/signature_test.go92
1 files changed, 84 insertions, 8 deletions
diff --git a/crypto/signature_test.go b/crypto/signature_test.go
index aefd9e38d..abcab425b 100644
--- a/crypto/signature_test.go
+++ b/crypto/signature_test.go
@@ -18,19 +18,95 @@ package crypto
import (
"bytes"
- "encoding/hex"
"testing"
+
+ "github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/common/hexutil"
+)
+
+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")
+ }
+}
+
+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 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)
+ }
}
}