signature_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. package crypto
  17. import (
  18. "bytes"
  19. "testing"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/common/hexutil"
  22. )
  23. var (
  24. testmsg = hexutil.MustDecode("0xce0677bb30baa8cf067c88db9811f4333d131bf8bcf12fe7065d211dce971008")
  25. testsig = hexutil.MustDecode("0x90f27b8b488db00b00606796d2987f6a5f59ae62ea05effe84fef5b8b0e549984a691139ad57a3f0b906637673aa2f63d1f55cb1a69199d4009eea23ceaddc9301")
  26. testpubkey = hexutil.MustDecode("0x04e32df42865e97135acfb65f3bae71bdc86f4d49150ad6a440b6f15878109880a0a2b2667f7e725ceea70c673093bf67663e0312623c8e091b13cf2c0f11ef652")
  27. testpubkeyc = hexutil.MustDecode("0x02e32df42865e97135acfb65f3bae71bdc86f4d49150ad6a440b6f15878109880a")
  28. )
  29. func TestEcrecover(t *testing.T) {
  30. pubkey, err := Ecrecover(testmsg, testsig)
  31. if err != nil {
  32. t.Fatalf("recover error: %s", err)
  33. }
  34. if !bytes.Equal(pubkey, testpubkey) {
  35. t.Errorf("pubkey mismatch: want: %x have: %x", testpubkey, pubkey)
  36. }
  37. }
  38. func TestVerifySignature(t *testing.T) {
  39. sig := testsig[:len(testsig)-1] // remove recovery id
  40. if !VerifySignature(testpubkey, testmsg, sig) {
  41. t.Errorf("can't verify signature with uncompressed key")
  42. }
  43. if !VerifySignature(testpubkeyc, testmsg, sig) {
  44. t.Errorf("can't verify signature with compressed key")
  45. }
  46. if VerifySignature(nil, testmsg, sig) {
  47. t.Errorf("signature valid with no key")
  48. }
  49. if VerifySignature(testpubkey, nil, sig) {
  50. t.Errorf("signature valid with no message")
  51. }
  52. if VerifySignature(testpubkey, testmsg, nil) {
  53. t.Errorf("nil signature valid")
  54. }
  55. if VerifySignature(testpubkey, testmsg, append(common.CopyBytes(sig), 1, 2, 3)) {
  56. t.Errorf("signature valid with extra bytes at the end")
  57. }
  58. if VerifySignature(testpubkey, testmsg, sig[:len(sig)-2]) {
  59. t.Errorf("signature valid even though it's incomplete")
  60. }
  61. }
  62. func TestDecompressPubkey(t *testing.T) {
  63. key, err := DecompressPubkey(testpubkeyc)
  64. if err != nil {
  65. t.Fatal(err)
  66. }
  67. if uncompressed := FromECDSAPub(key); !bytes.Equal(uncompressed, testpubkey) {
  68. t.Errorf("wrong public key result: got %x, want %x", uncompressed, testpubkey)
  69. }
  70. if _, err := DecompressPubkey(nil); err == nil {
  71. t.Errorf("no error for nil pubkey")
  72. }
  73. if _, err := DecompressPubkey(testpubkeyc[:5]); err == nil {
  74. t.Errorf("no error for incomplete pubkey")
  75. }
  76. if _, err := DecompressPubkey(append(common.CopyBytes(testpubkeyc), 1, 2, 3)); err == nil {
  77. t.Errorf("no error for pubkey with extra bytes at the end")
  78. }
  79. }
  80. func BenchmarkEcrecoverSignature(b *testing.B) {
  81. for i := 0; i < b.N; i++ {
  82. if _, err := Ecrecover(testmsg, testsig); err != nil {
  83. b.Fatal("ecrecover error", err)
  84. }
  85. }
  86. }
  87. func BenchmarkVerifySignature(b *testing.B) {
  88. sig := testsig[:len(testsig)-1] // remove recovery id
  89. for i := 0; i < b.N; i++ {
  90. if !VerifySignature(testpubkey, testmsg, sig) {
  91. b.Fatal("verify error")
  92. }
  93. }
  94. }
  95. func BenchmarkDecompressPubkey(b *testing.B) {
  96. for i := 0; i < b.N; i++ {
  97. if _, err := DecompressPubkey(testpubkeyc); err != nil {
  98. b.Fatal(err)
  99. }
  100. }
  101. }