secp256_test.go 6.5 KB

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