crypto_test.go 6.3 KB

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