You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
2.1 KiB

  1. package keccak
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "testing"
  6. "github.com/ethereum/go-ethereum/crypto"
  7. qt "github.com/frankban/quicktest"
  8. )
  9. func TestKeccak(t *testing.T) {
  10. testKeccak(t, []byte("test"), "9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658")
  11. testKeccak(t, make([]byte, 32), "290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563")
  12. testKeccak(t, make([]byte, 100), "913fb9e1f6f1c6d910fd574a5cad8857aa43bfba24e401ada4f56090d4d997a7")
  13. }
  14. func testKeccak(t *testing.T, input []byte, expectedHex string) {
  15. expected := crypto.Keccak256(input)
  16. hBits := ComputeKeccak(bytesToBits(input))
  17. h := bitsToBytes(hBits)
  18. qt.Assert(t, h, qt.DeepEquals, expected)
  19. qt.Assert(t, hex.EncodeToString(h), qt.Equals, expectedHex)
  20. }
  21. func TestPad(t *testing.T) {
  22. b := make([]byte, 32)
  23. for i := 0; i < len(b); i++ {
  24. b[i] = byte(i)
  25. }
  26. fmt.Println(b)
  27. bBits := bytesToBits(b)
  28. fBits := pad(bBits)
  29. qt.Assert(t, bitsToBytes(fBits[:]), qt.DeepEquals,
  30. []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128})
  31. }
  32. func TestFinal(t *testing.T) {
  33. b := make([]byte, 32)
  34. for i := 0; i < len(b); i++ {
  35. b[i] = byte(i)
  36. }
  37. fmt.Println(b)
  38. bBits := bytesToBits(b)
  39. fBits := final(bBits)
  40. qt.Assert(t, bitsToU64Array(fBits[:]), qt.DeepEquals,
  41. []uint64{16953415415620100490, 7495738965189503699, 12723370805759944158, 3295955328722933810, 12121371508560456016, 174876831679863147, 15944933357501475584, 7502339663607726274, 12048918224562833898, 16715284461100269102, 15582559130083209842, 1743886467337678829, 2424196198791253761, 1116417308245482383, 10367365997906434042, 1849801549382613906, 13294939539683415102, 4478091053375708790, 2969967870313332958, 14618962068930014237, 2721742233407503451, 12003265593030191290, 8109318293656735684, 6346795302983965746, 12210038122000333046})
  42. }