crypto_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum 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. // go-ethereum 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 go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package crypto
  17. import (
  18. "bytes"
  19. "encoding/hex"
  20. "fmt"
  21. "testing"
  22. "time"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/crypto/secp256k1"
  25. )
  26. // These tests are sanity checks.
  27. // They should ensure that we don't e.g. use Sha3-224 instead of Sha3-256
  28. // and that the sha3 library uses keccak-f permutation.
  29. func TestSha3(t *testing.T) {
  30. msg := []byte("abc")
  31. exp, _ := hex.DecodeString("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45")
  32. checkhash(t, "Sha3-256", func(in []byte) []byte { return Sha3(in) }, msg, exp)
  33. }
  34. func TestSha3Hash(t *testing.T) {
  35. msg := []byte("abc")
  36. exp, _ := hex.DecodeString("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45")
  37. checkhash(t, "Sha3-256-array", func(in []byte) []byte { h := Sha3Hash(in); return h[:] }, msg, exp)
  38. }
  39. func TestSha256(t *testing.T) {
  40. msg := []byte("abc")
  41. exp, _ := hex.DecodeString("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")
  42. checkhash(t, "Sha256", Sha256, msg, exp)
  43. }
  44. func TestRipemd160(t *testing.T) {
  45. msg := []byte("abc")
  46. exp, _ := hex.DecodeString("8eb208f7e05d987a9b044a8e98c6b087f15a0bfc")
  47. checkhash(t, "Ripemd160", Ripemd160, msg, exp)
  48. }
  49. func checkhash(t *testing.T, name string, f func([]byte) []byte, msg, exp []byte) {
  50. sum := f(msg)
  51. if bytes.Compare(exp, sum) != 0 {
  52. t.Errorf("hash %s returned wrong result.\ngot: %x\nwant: %x", name, sum, exp)
  53. }
  54. }
  55. func BenchmarkSha3(b *testing.B) {
  56. a := []byte("hello world")
  57. amount := 1000000
  58. start := time.Now()
  59. for i := 0; i < amount; i++ {
  60. Sha3(a)
  61. }
  62. fmt.Println(amount, ":", time.Since(start))
  63. }
  64. func Test0Key(t *testing.T) {
  65. t.Skip()
  66. key := common.Hex2Bytes("1111111111111111111111111111111111111111111111111111111111111111")
  67. p, err := secp256k1.GeneratePubKey(key)
  68. addr := Sha3(p[1:])[12:]
  69. fmt.Printf("%x\n", p)
  70. fmt.Printf("%v %x\n", err, addr)
  71. }
  72. func TestInvalidSign(t *testing.T) {
  73. _, err := Sign(make([]byte, 1), nil)
  74. if err == nil {
  75. t.Errorf("expected sign with hash 1 byte to error")
  76. }
  77. _, err = Sign(make([]byte, 33), nil)
  78. if err == nil {
  79. t.Errorf("expected sign with hash 33 byte to error")
  80. }
  81. }