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.

83 lines
2.1 KiB

2 years ago
  1. package keccak
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "testing"
  6. qt "github.com/frankban/quicktest"
  7. )
  8. func printS(n string, s []uint64) {
  9. jS, err := json.Marshal(s)
  10. if err != nil {
  11. panic(err)
  12. }
  13. fmt.Printf("%s: %s\n", n, string(jS))
  14. }
  15. func newS() ([25 * 64]bool, [25]uint64) {
  16. var s [25 * 64]bool
  17. var sU64 [25]uint64
  18. for i := 0; i < len(s)/64; i++ {
  19. copy(s[i*64:i*64+64], u64ToBits(uint64(i)))
  20. sU64[i] = uint64(i)
  21. }
  22. return s, sU64
  23. }
  24. func TestTheta(t *testing.T) {
  25. s, sU64 := newS()
  26. s = theta(s)
  27. sU64 = thetaU64Version(sU64)
  28. qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals, sU64[:])
  29. qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals,
  30. []uint64{26, 9, 13, 29, 47, 31, 14, 8, 22, 34, 16, 3, 3,
  31. 19, 37, 21, 24, 30, 12, 56, 14, 29, 25, 9, 51})
  32. // compute again theta on the current state
  33. s = theta(s)
  34. sU64 = thetaU64Version(sU64)
  35. qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals, sU64[:])
  36. }
  37. func TestRhoPi(t *testing.T) {
  38. s, sU64 := newS()
  39. s = rhopi(s)
  40. sU64 = rhopiU64Version(sU64)
  41. qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals, sU64[:])
  42. qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals,
  43. []uint64{0, 105553116266496, 105553116266496, 37748736, 393216,
  44. 805306368, 9437184, 80, 562949953421312, 13835058055282163714,
  45. 2, 448, 436207616, 4864, 5242880, 536870912, 343597383680,
  46. 11264, 557056, 1657324662872342528, 9223372036854775808,
  47. 288230376151711744, 7696581394432, 32985348833280, 84})
  48. // compute again rhopi on the current state
  49. s = rhopi(s)
  50. sU64 = rhopiU64Version(sU64)
  51. qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals, sU64[:])
  52. }
  53. func TestChi(t *testing.T) {
  54. s, sU64 := newS()
  55. s = chi(s)
  56. sU64 = chiU64Version(sU64)
  57. qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals, sU64[:])
  58. qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals,
  59. []uint64{2, 0, 6, 3, 5, 4, 14, 6, 12, 11, 14, 10, 14, 13, 15,
  60. 14, 18, 16, 30, 3, 22, 20, 30, 19, 25})
  61. // compute again theta on the current state
  62. s = rhopi(s)
  63. sU64 = rhopiU64Version(sU64)
  64. qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals, sU64[:])
  65. s = chi(s)
  66. sU64 = chiU64Version(sU64)
  67. qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals, sU64[:])
  68. }