crypto_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Copyright 2014 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. "crypto/ecdsa"
  20. "encoding/hex"
  21. "io/ioutil"
  22. "math/big"
  23. "os"
  24. "testing"
  25. "github.com/ethereum/go-ethereum/common"
  26. )
  27. var testAddrHex = "970e8128ab834e8eac17ab8e3812f010678cf791"
  28. var testPrivHex = "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032"
  29. // These tests are sanity checks.
  30. // They should ensure that we don't e.g. use Sha3-224 instead of Sha3-256
  31. // and that the sha3 library uses keccak-f permutation.
  32. func TestKeccak256Hash(t *testing.T) {
  33. msg := []byte("abc")
  34. exp, _ := hex.DecodeString("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45")
  35. checkhash(t, "Sha3-256-array", func(in []byte) []byte { h := Keccak256Hash(in); return h[:] }, msg, exp)
  36. }
  37. func TestToECDSAErrors(t *testing.T) {
  38. if _, err := HexToECDSA("0000000000000000000000000000000000000000000000000000000000000000"); err == nil {
  39. t.Fatal("HexToECDSA should've returned error")
  40. }
  41. if _, err := HexToECDSA("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"); err == nil {
  42. t.Fatal("HexToECDSA should've returned error")
  43. }
  44. }
  45. func BenchmarkSha3(b *testing.B) {
  46. a := []byte("hello world")
  47. for i := 0; i < b.N; i++ {
  48. Keccak256(a)
  49. }
  50. }
  51. func TestSign(t *testing.T) {
  52. key, _ := HexToECDSA(testPrivHex)
  53. addr := common.HexToAddress(testAddrHex)
  54. msg := Keccak256([]byte("foo"))
  55. sig, err := Sign(msg, key)
  56. if err != nil {
  57. t.Errorf("Sign error: %s", err)
  58. }
  59. recoveredPub, err := Ecrecover(msg, sig)
  60. if err != nil {
  61. t.Errorf("ECRecover error: %s", err)
  62. }
  63. pubKey := ToECDSAPub(recoveredPub)
  64. recoveredAddr := PubkeyToAddress(*pubKey)
  65. if addr != recoveredAddr {
  66. t.Errorf("Address mismatch: want: %x have: %x", addr, recoveredAddr)
  67. }
  68. // should be equal to SigToPub
  69. recoveredPub2, err := SigToPub(msg, sig)
  70. if err != nil {
  71. t.Errorf("ECRecover error: %s", err)
  72. }
  73. recoveredAddr2 := PubkeyToAddress(*recoveredPub2)
  74. if addr != recoveredAddr2 {
  75. t.Errorf("Address mismatch: want: %x have: %x", addr, recoveredAddr2)
  76. }
  77. }
  78. func TestInvalidSign(t *testing.T) {
  79. if _, err := Sign(make([]byte, 1), nil); err == nil {
  80. t.Errorf("expected sign with hash 1 byte to error")
  81. }
  82. if _, err := Sign(make([]byte, 33), nil); err == nil {
  83. t.Errorf("expected sign with hash 33 byte to error")
  84. }
  85. }
  86. func TestNewContractAddress(t *testing.T) {
  87. key, _ := HexToECDSA(testPrivHex)
  88. addr := common.HexToAddress(testAddrHex)
  89. genAddr := PubkeyToAddress(key.PublicKey)
  90. // sanity check before using addr to create contract address
  91. checkAddr(t, genAddr, addr)
  92. caddr0 := CreateAddress(addr, 0)
  93. caddr1 := CreateAddress(addr, 1)
  94. caddr2 := CreateAddress(addr, 2)
  95. checkAddr(t, common.HexToAddress("333c3310824b7c685133f2bedb2ca4b8b4df633d"), caddr0)
  96. checkAddr(t, common.HexToAddress("8bda78331c916a08481428e4b07c96d3e916d165"), caddr1)
  97. checkAddr(t, common.HexToAddress("c9ddedf451bc62ce88bf9292afb13df35b670699"), caddr2)
  98. }
  99. func TestLoadECDSAFile(t *testing.T) {
  100. keyBytes := common.FromHex(testPrivHex)
  101. fileName0 := "test_key0"
  102. fileName1 := "test_key1"
  103. checkKey := func(k *ecdsa.PrivateKey) {
  104. checkAddr(t, PubkeyToAddress(k.PublicKey), common.HexToAddress(testAddrHex))
  105. loadedKeyBytes := FromECDSA(k)
  106. if !bytes.Equal(loadedKeyBytes, keyBytes) {
  107. t.Fatalf("private key mismatch: want: %x have: %x", keyBytes, loadedKeyBytes)
  108. }
  109. }
  110. ioutil.WriteFile(fileName0, []byte(testPrivHex), 0600)
  111. defer os.Remove(fileName0)
  112. key0, err := LoadECDSA(fileName0)
  113. if err != nil {
  114. t.Fatal(err)
  115. }
  116. checkKey(key0)
  117. // again, this time with SaveECDSA instead of manual save:
  118. err = SaveECDSA(fileName1, key0)
  119. if err != nil {
  120. t.Fatal(err)
  121. }
  122. defer os.Remove(fileName1)
  123. key1, err := LoadECDSA(fileName1)
  124. if err != nil {
  125. t.Fatal(err)
  126. }
  127. checkKey(key1)
  128. }
  129. func TestValidateSignatureValues(t *testing.T) {
  130. check := func(expected bool, v byte, r, s *big.Int) {
  131. if ValidateSignatureValues(v, r, s, false) != expected {
  132. t.Errorf("mismatch for v: %d r: %d s: %d want: %v", v, r, s, expected)
  133. }
  134. }
  135. minusOne := big.NewInt(-1)
  136. one := common.Big1
  137. zero := common.Big0
  138. secp256k1nMinus1 := new(big.Int).Sub(secp256k1N, common.Big1)
  139. // correct v,r,s
  140. check(true, 0, one, one)
  141. check(true, 1, one, one)
  142. // incorrect v, correct r,s,
  143. check(false, 2, one, one)
  144. check(false, 3, one, one)
  145. // incorrect v, combinations of incorrect/correct r,s at lower limit
  146. check(false, 2, zero, zero)
  147. check(false, 2, zero, one)
  148. check(false, 2, one, zero)
  149. check(false, 2, one, one)
  150. // correct v for any combination of incorrect r,s
  151. check(false, 0, zero, zero)
  152. check(false, 0, zero, one)
  153. check(false, 0, one, zero)
  154. check(false, 1, zero, zero)
  155. check(false, 1, zero, one)
  156. check(false, 1, one, zero)
  157. // correct sig with max r,s
  158. check(true, 0, secp256k1nMinus1, secp256k1nMinus1)
  159. // correct v, combinations of incorrect r,s at upper limit
  160. check(false, 0, secp256k1N, secp256k1nMinus1)
  161. check(false, 0, secp256k1nMinus1, secp256k1N)
  162. check(false, 0, secp256k1N, secp256k1N)
  163. // current callers ensures r,s cannot be negative, but let's test for that too
  164. // as crypto package could be used stand-alone
  165. check(false, 0, minusOne, one)
  166. check(false, 0, one, minusOne)
  167. }
  168. func checkhash(t *testing.T, name string, f func([]byte) []byte, msg, exp []byte) {
  169. sum := f(msg)
  170. if !bytes.Equal(exp, sum) {
  171. t.Fatalf("hash %s mismatch: want: %x have: %x", name, exp, sum)
  172. }
  173. }
  174. func checkAddr(t *testing.T, addr0, addr1 common.Address) {
  175. if addr0 != addr1 {
  176. t.Fatalf("address mismatch: want: %x have: %x", addr0, addr1)
  177. }
  178. }
  179. // test to help Python team with integration of libsecp256k1
  180. // skip but keep it after they are done
  181. func TestPythonIntegration(t *testing.T) {
  182. kh := "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032"
  183. k0, _ := HexToECDSA(kh)
  184. msg0 := Keccak256([]byte("foo"))
  185. sig0, _ := Sign(msg0, k0)
  186. msg1 := common.FromHex("00000000000000000000000000000000")
  187. sig1, _ := Sign(msg0, k0)
  188. t.Logf("msg: %x, privkey: %s sig: %x\n", msg0, kh, sig0)
  189. t.Logf("msg: %x, privkey: %s sig: %x\n", msg1, kh, sig1)
  190. }