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.

120 lines
3.9 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 TestTheta2(t *testing.T) {
  38. input := []uint64{26388279066651, 246290629787648, 26388279902208, 25165850, 246290605457408, 7784628352, 844424965783552, 2305843009213694083, 844432714760192, 2305843009249345539, 637534226, 14848, 641204224, 14354, 3670528, 6308236288, 2130304761856, 648518346341354496, 6309216256, 648520476645130240, 4611706359392501763, 792677514882318336, 20340965113972, 4611732197915754499, 792633534417207412}
  39. var s [25 * 64]bool
  40. var sU64 [25]uint64
  41. copy(s[:], u64ArrayToBits(input))
  42. copy(sU64[:], input)
  43. s = theta(s)
  44. sU64 = thetaU64Version(sU64)
  45. // fmt.Println(bitsToU64Array(s[:]))
  46. qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals, sU64[:])
  47. qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals,
  48. []uint64{3749081831850030700, 1297317621190464868, 10017560217643747862, 7854780639862409219, 13836147678645575967, 3749090635727681271, 1297915755455157604, 12323429615135705749, 7855062122598582297, 16141814766035214620, 3749090628446369381, 1297071330560683876, 10017586606556924438, 7854780639837253643, 13835971756788491039, 3749090634251287159, 1297070162329376100, 9369068259580659222, 7854780645071013913, 14484490034407743775, 8360757404916954740, 1801500877105239396, 10017570663003408994, 3243123208712177690, 14628605291203076459})
  49. // fmt.Println(bitsToU64(s[1*64:2*64]), s[1*64:2*64])
  50. // compute again theta on the current state
  51. s = theta(s)
  52. sU64 = thetaU64Version(sU64)
  53. qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals, sU64[:])
  54. }
  55. func TestRhoPi(t *testing.T) {
  56. s, sU64 := newS()
  57. s = rhopi(s)
  58. sU64 = rhopiU64Version(sU64)
  59. qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals, sU64[:])
  60. qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals,
  61. []uint64{0, 105553116266496, 105553116266496, 37748736, 393216,
  62. 805306368, 9437184, 80, 562949953421312, 13835058055282163714,
  63. 2, 448, 436207616, 4864, 5242880, 536870912, 343597383680,
  64. 11264, 557056, 1657324662872342528, 9223372036854775808,
  65. 288230376151711744, 7696581394432, 32985348833280, 84})
  66. // compute again rhopi on the current state
  67. s = rhopi(s)
  68. sU64 = rhopiU64Version(sU64)
  69. qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals, sU64[:])
  70. }
  71. func TestChi(t *testing.T) {
  72. s, sU64 := newS()
  73. s = chi(s)
  74. sU64 = chiU64Version(sU64)
  75. qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals, sU64[:])
  76. qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals,
  77. []uint64{2, 0, 6, 3, 5, 4, 14, 6, 12, 11, 14, 10, 14, 13, 15,
  78. 14, 18, 16, 30, 3, 22, 20, 30, 19, 25})
  79. // compute again theta on the current state
  80. s = rhopi(s)
  81. sU64 = rhopiU64Version(sU64)
  82. qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals, sU64[:])
  83. s = chi(s)
  84. sU64 = chiU64Version(sU64)
  85. qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals, sU64[:])
  86. }
  87. func TestIota(t *testing.T) {
  88. s, sU64 := newS()
  89. s = iot(s, 3)
  90. sU64 = iotU64Version(sU64, 3)
  91. qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals, sU64[:])
  92. qt.Assert(t, bitsToU64(s[0:64]), qt.Equals, uint64(9223372039002292224))
  93. // compute again theta on the current state
  94. s = iot(s, 10)
  95. sU64 = iotU64Version(sU64, 10)
  96. qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals, sU64[:])
  97. }