github.com/luckypickle/go-ethereum-vet@v1.14.2/crypto/signature_test.go (about) 1 // Copyright 2017 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package crypto 18 19 import ( 20 "bytes" 21 "crypto/ecdsa" 22 "reflect" 23 "testing" 24 25 "github.com/luckypickle/go-ethereum-vet/common" 26 "github.com/luckypickle/go-ethereum-vet/common/hexutil" 27 "github.com/luckypickle/go-ethereum-vet/common/math" 28 ) 29 30 var ( 31 testmsg = hexutil.MustDecode("0xce0677bb30baa8cf067c88db9811f4333d131bf8bcf12fe7065d211dce971008") 32 testsig = hexutil.MustDecode("0x90f27b8b488db00b00606796d2987f6a5f59ae62ea05effe84fef5b8b0e549984a691139ad57a3f0b906637673aa2f63d1f55cb1a69199d4009eea23ceaddc9301") 33 testpubkey = hexutil.MustDecode("0x04e32df42865e97135acfb65f3bae71bdc86f4d49150ad6a440b6f15878109880a0a2b2667f7e725ceea70c673093bf67663e0312623c8e091b13cf2c0f11ef652") 34 testpubkeyc = hexutil.MustDecode("0x02e32df42865e97135acfb65f3bae71bdc86f4d49150ad6a440b6f15878109880a") 35 ) 36 37 func TestEcrecover(t *testing.T) { 38 pubkey, err := Ecrecover(testmsg, testsig) 39 if err != nil { 40 t.Fatalf("recover error: %s", err) 41 } 42 if !bytes.Equal(pubkey, testpubkey) { 43 t.Errorf("pubkey mismatch: want: %x have: %x", testpubkey, pubkey) 44 } 45 } 46 47 func TestVerifySignature(t *testing.T) { 48 sig := testsig[:len(testsig)-1] // remove recovery id 49 if !VerifySignature(testpubkey, testmsg, sig) { 50 t.Errorf("can't verify signature with uncompressed key") 51 } 52 if !VerifySignature(testpubkeyc, testmsg, sig) { 53 t.Errorf("can't verify signature with compressed key") 54 } 55 56 if VerifySignature(nil, testmsg, sig) { 57 t.Errorf("signature valid with no key") 58 } 59 if VerifySignature(testpubkey, nil, sig) { 60 t.Errorf("signature valid with no message") 61 } 62 if VerifySignature(testpubkey, testmsg, nil) { 63 t.Errorf("nil signature valid") 64 } 65 if VerifySignature(testpubkey, testmsg, append(common.CopyBytes(sig), 1, 2, 3)) { 66 t.Errorf("signature valid with extra bytes at the end") 67 } 68 if VerifySignature(testpubkey, testmsg, sig[:len(sig)-2]) { 69 t.Errorf("signature valid even though it's incomplete") 70 } 71 wrongkey := common.CopyBytes(testpubkey) 72 wrongkey[10]++ 73 if VerifySignature(wrongkey, testmsg, sig) { 74 t.Errorf("signature valid with with wrong public key") 75 } 76 } 77 78 // This test checks that VerifySignature rejects malleable signatures with s > N/2. 79 func TestVerifySignatureMalleable(t *testing.T) { 80 sig := hexutil.MustDecode("0x638a54215d80a6713c8d523a6adc4e6e73652d859103a36b700851cb0e61b66b8ebfc1a610c57d732ec6e0a8f06a9a7a28df5051ece514702ff9cdff0b11f454") 81 key := hexutil.MustDecode("0x03ca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd3138") 82 msg := hexutil.MustDecode("0xd301ce462d3e639518f482c7f03821fec1e602018630ce621e1e7851c12343a6") 83 if VerifySignature(key, msg, sig) { 84 t.Error("VerifySignature returned true for malleable signature") 85 } 86 } 87 88 func TestDecompressPubkey(t *testing.T) { 89 key, err := DecompressPubkey(testpubkeyc) 90 if err != nil { 91 t.Fatal(err) 92 } 93 if uncompressed := FromECDSAPub(key); !bytes.Equal(uncompressed, testpubkey) { 94 t.Errorf("wrong public key result: got %x, want %x", uncompressed, testpubkey) 95 } 96 if _, err := DecompressPubkey(nil); err == nil { 97 t.Errorf("no error for nil pubkey") 98 } 99 if _, err := DecompressPubkey(testpubkeyc[:5]); err == nil { 100 t.Errorf("no error for incomplete pubkey") 101 } 102 if _, err := DecompressPubkey(append(common.CopyBytes(testpubkeyc), 1, 2, 3)); err == nil { 103 t.Errorf("no error for pubkey with extra bytes at the end") 104 } 105 } 106 107 func TestCompressPubkey(t *testing.T) { 108 key := &ecdsa.PublicKey{ 109 Curve: S256(), 110 X: math.MustParseBig256("0xe32df42865e97135acfb65f3bae71bdc86f4d49150ad6a440b6f15878109880a"), 111 Y: math.MustParseBig256("0x0a2b2667f7e725ceea70c673093bf67663e0312623c8e091b13cf2c0f11ef652"), 112 } 113 compressed := CompressPubkey(key) 114 if !bytes.Equal(compressed, testpubkeyc) { 115 t.Errorf("wrong public key result: got %x, want %x", compressed, testpubkeyc) 116 } 117 } 118 119 func TestPubkeyRandom(t *testing.T) { 120 const runs = 200 121 122 for i := 0; i < runs; i++ { 123 key, err := GenerateKey() 124 if err != nil { 125 t.Fatalf("iteration %d: %v", i, err) 126 } 127 pubkey2, err := DecompressPubkey(CompressPubkey(&key.PublicKey)) 128 if err != nil { 129 t.Fatalf("iteration %d: %v", i, err) 130 } 131 if !reflect.DeepEqual(key.PublicKey, *pubkey2) { 132 t.Fatalf("iteration %d: keys not equal", i) 133 } 134 } 135 } 136 137 func BenchmarkEcrecoverSignature(b *testing.B) { 138 for i := 0; i < b.N; i++ { 139 if _, err := Ecrecover(testmsg, testsig); err != nil { 140 b.Fatal("ecrecover error", err) 141 } 142 } 143 } 144 145 func BenchmarkVerifySignature(b *testing.B) { 146 sig := testsig[:len(testsig)-1] // remove recovery id 147 for i := 0; i < b.N; i++ { 148 if !VerifySignature(testpubkey, testmsg, sig) { 149 b.Fatal("verify error") 150 } 151 } 152 } 153 154 func BenchmarkDecompressPubkey(b *testing.B) { 155 for i := 0; i < b.N; i++ { 156 if _, err := DecompressPubkey(testpubkeyc); err != nil { 157 b.Fatal(err) 158 } 159 } 160 }