secp256_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Copyright 2015 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 secp256k1
  17. import (
  18. "bytes"
  19. "encoding/hex"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/crypto/randentropy"
  22. )
  23. const TestCount = 1000
  24. func TestPrivkeyGenerate(t *testing.T) {
  25. _, seckey := GenerateKeyPair()
  26. if err := VerifySeckeyValidity(seckey); err != nil {
  27. t.Errorf("seckey not valid: %s", err)
  28. }
  29. }
  30. func TestSignatureValidity(t *testing.T) {
  31. pubkey, seckey := GenerateKeyPair()
  32. msg := randentropy.GetEntropyCSPRNG(32)
  33. sig, err := Sign(msg, seckey)
  34. if err != nil {
  35. t.Errorf("signature error: %s", err)
  36. }
  37. compactSigCheck(t, sig)
  38. if len(pubkey) != 65 {
  39. t.Errorf("pubkey length mismatch: want: 65 have: %d", len(pubkey))
  40. }
  41. if len(seckey) != 32 {
  42. t.Errorf("seckey length mismatch: want: 32 have: %d", len(seckey))
  43. }
  44. if len(sig) != 65 {
  45. t.Errorf("sig length mismatch: want: 65 have: %d", len(sig))
  46. }
  47. recid := int(sig[64])
  48. if recid > 4 || recid < 0 {
  49. t.Errorf("sig recid mismatch: want: within 0 to 4 have: %d", int(sig[64]))
  50. }
  51. }
  52. func TestInvalidRecoveryID(t *testing.T) {
  53. _, seckey := GenerateKeyPair()
  54. msg := randentropy.GetEntropyCSPRNG(32)
  55. sig, _ := Sign(msg, seckey)
  56. sig[64] = 99
  57. _, err := RecoverPubkey(msg, sig)
  58. if err != ErrInvalidRecoveryID {
  59. t.Fatalf("got %q, want %q", err, ErrInvalidRecoveryID)
  60. }
  61. }
  62. func TestSignAndRecover(t *testing.T) {
  63. pubkey1, seckey := GenerateKeyPair()
  64. msg := randentropy.GetEntropyCSPRNG(32)
  65. sig, err := Sign(msg, seckey)
  66. if err != nil {
  67. t.Errorf("signature error: %s", err)
  68. }
  69. pubkey2, err := RecoverPubkey(msg, sig)
  70. if err != nil {
  71. t.Errorf("recover error: %s", err)
  72. }
  73. if !bytes.Equal(pubkey1, pubkey2) {
  74. t.Errorf("pubkey mismatch: want: %x have: %x", pubkey1, pubkey2)
  75. }
  76. }
  77. func TestRandomMessagesWithSameKey(t *testing.T) {
  78. pubkey, seckey := GenerateKeyPair()
  79. keys := func() ([]byte, []byte) {
  80. return pubkey, seckey
  81. }
  82. signAndRecoverWithRandomMessages(t, keys)
  83. }
  84. func TestRandomMessagesWithRandomKeys(t *testing.T) {
  85. keys := func() ([]byte, []byte) {
  86. pubkey, seckey := GenerateKeyPair()
  87. return pubkey, seckey
  88. }
  89. signAndRecoverWithRandomMessages(t, keys)
  90. }
  91. func signAndRecoverWithRandomMessages(t *testing.T, keys func() ([]byte, []byte)) {
  92. for i := 0; i < TestCount; i++ {
  93. pubkey1, seckey := keys()
  94. msg := randentropy.GetEntropyCSPRNG(32)
  95. sig, err := Sign(msg, seckey)
  96. if err != nil {
  97. t.Fatalf("signature error: %s", err)
  98. }
  99. if sig == nil {
  100. t.Fatal("signature is nil")
  101. }
  102. compactSigCheck(t, sig)
  103. // TODO: why do we flip around the recovery id?
  104. sig[len(sig)-1] %= 4
  105. pubkey2, err := RecoverPubkey(msg, sig)
  106. if err != nil {
  107. t.Fatalf("recover error: %s", err)
  108. }
  109. if pubkey2 == nil {
  110. t.Error("pubkey is nil")
  111. }
  112. if !bytes.Equal(pubkey1, pubkey2) {
  113. t.Fatalf("pubkey mismatch: want: %x have: %x", pubkey1, pubkey2)
  114. }
  115. }
  116. }
  117. func TestRecoveryOfRandomSignature(t *testing.T) {
  118. pubkey1, _ := GenerateKeyPair()
  119. msg := randentropy.GetEntropyCSPRNG(32)
  120. for i := 0; i < TestCount; i++ {
  121. // recovery can sometimes work, but if so should always give wrong pubkey
  122. pubkey2, _ := RecoverPubkey(msg, randSig())
  123. if bytes.Equal(pubkey1, pubkey2) {
  124. t.Fatalf("iteration: %d: pubkey mismatch: do NOT want %x: ", i, pubkey2)
  125. }
  126. }
  127. }
  128. func randSig() []byte {
  129. sig := randentropy.GetEntropyCSPRNG(65)
  130. sig[32] &= 0x70
  131. sig[64] %= 4
  132. return sig
  133. }
  134. func TestRandomMessagesAgainstValidSig(t *testing.T) {
  135. pubkey1, seckey := GenerateKeyPair()
  136. msg := randentropy.GetEntropyCSPRNG(32)
  137. sig, _ := Sign(msg, seckey)
  138. for i := 0; i < TestCount; i++ {
  139. msg = randentropy.GetEntropyCSPRNG(32)
  140. pubkey2, _ := RecoverPubkey(msg, sig)
  141. // recovery can sometimes work, but if so should always give wrong pubkey
  142. if bytes.Equal(pubkey1, pubkey2) {
  143. t.Fatalf("iteration: %d: pubkey mismatch: do NOT want %x: ", i, pubkey2)
  144. }
  145. }
  146. }
  147. func TestZeroPrivkey(t *testing.T) {
  148. zeroedBytes := make([]byte, 32)
  149. err := VerifySeckeyValidity(zeroedBytes)
  150. if err == nil {
  151. t.Errorf("zeroed bytes should have returned error")
  152. }
  153. }
  154. // Useful when the underlying libsecp256k1 API changes to quickly
  155. // check only recover function without use of signature function
  156. func TestRecoverSanity(t *testing.T) {
  157. msg, _ := hex.DecodeString("ce0677bb30baa8cf067c88db9811f4333d131bf8bcf12fe7065d211dce971008")
  158. sig, _ := hex.DecodeString("90f27b8b488db00b00606796d2987f6a5f59ae62ea05effe84fef5b8b0e549984a691139ad57a3f0b906637673aa2f63d1f55cb1a69199d4009eea23ceaddc9301")
  159. pubkey1, _ := hex.DecodeString("04e32df42865e97135acfb65f3bae71bdc86f4d49150ad6a440b6f15878109880a0a2b2667f7e725ceea70c673093bf67663e0312623c8e091b13cf2c0f11ef652")
  160. pubkey2, err := RecoverPubkey(msg, sig)
  161. if err != nil {
  162. t.Fatalf("recover error: %s", err)
  163. }
  164. if !bytes.Equal(pubkey1, pubkey2) {
  165. t.Errorf("pubkey mismatch: want: %x have: %x", pubkey1, pubkey2)
  166. }
  167. }
  168. // tests for malleability
  169. // highest bit of signature ECDSA s value must be 0, in the 33th byte
  170. func compactSigCheck(t *testing.T, sig []byte) {
  171. var b int = int(sig[32])
  172. if b < 0 {
  173. t.Errorf("highest bit is negative: %d", b)
  174. }
  175. if ((b >> 7) == 1) != ((b & 0x80) == 0x80) {
  176. t.Errorf("highest bit: %d bit >> 7: %d", b, b>>7)
  177. }
  178. if (b & 0x80) == 0x80 {
  179. t.Errorf("highest bit: %d bit & 0x80: %d", b, b&0x80)
  180. }
  181. }
  182. // godep go test -v -run=XXX -bench=BenchmarkSign
  183. // add -benchtime=10s to benchmark longer for more accurate average
  184. // to avoid compiler optimizing the benchmarked function call
  185. var err error
  186. func BenchmarkSign(b *testing.B) {
  187. for i := 0; i < b.N; i++ {
  188. _, seckey := GenerateKeyPair()
  189. msg := randentropy.GetEntropyCSPRNG(32)
  190. b.StartTimer()
  191. _, e := Sign(msg, seckey)
  192. err = e
  193. b.StopTimer()
  194. }
  195. }
  196. //godep go test -v -run=XXX -bench=BenchmarkECRec
  197. func BenchmarkRecover(b *testing.B) {
  198. for i := 0; i < b.N; i++ {
  199. _, seckey := GenerateKeyPair()
  200. msg := randentropy.GetEntropyCSPRNG(32)
  201. sig, _ := Sign(msg, seckey)
  202. b.StartTimer()
  203. _, e := RecoverPubkey(msg, sig)
  204. err = e
  205. b.StopTimer()
  206. }
  207. }