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.

145 lines
3.2 KiB

3 years ago
  1. /*
  2. This file contains the byte & uint64 implementations that are used as reference
  3. to test the bits implementations of the other files.
  4. The methods in this file (thetaU64Version, rhopiU64Version, chiU64Version,
  5. iotU64Version) are modified versions of methods from: https://github.com/ebfe/keccak
  6. */
  7. package keccak
  8. func thetaU64Version(a [25]uint64) [25]uint64 {
  9. var c0, c1, c2, c3, c4, d uint64
  10. var r [25]uint64
  11. c0 = a[0] ^ a[5] ^ a[10] ^ a[15] ^ a[20]
  12. c1 = a[1] ^ a[6] ^ a[11] ^ a[16] ^ a[21]
  13. c2 = a[2] ^ a[7] ^ a[12] ^ a[17] ^ a[22]
  14. c3 = a[3] ^ a[8] ^ a[13] ^ a[18] ^ a[23]
  15. c4 = a[4] ^ a[9] ^ a[14] ^ a[19] ^ a[24]
  16. d = c4 ^ (c1<<1 | c1>>(64-1))
  17. r[0] = a[0] ^ d
  18. r[5] = a[5] ^ d
  19. r[10] = a[10] ^ d
  20. r[15] = a[15] ^ d
  21. r[20] = a[20] ^ d
  22. d = c0 ^ (c2<<1 | c2>>(64-1))
  23. r[1] = a[1] ^ d
  24. r[6] = a[6] ^ d
  25. r[11] = a[11] ^ d
  26. r[16] = a[16] ^ d
  27. r[21] = a[21] ^ d
  28. d = c1 ^ (c3<<1 | c3>>(64-1))
  29. r[2] = a[2] ^ d
  30. r[7] = a[7] ^ d
  31. r[12] = a[12] ^ d
  32. r[17] = a[17] ^ d
  33. r[22] = a[22] ^ d
  34. d = c2 ^ (c4<<1 | c4>>(64-1))
  35. r[3] = a[3] ^ d
  36. r[8] = a[8] ^ d
  37. r[13] = a[13] ^ d
  38. r[18] = a[18] ^ d
  39. r[23] = a[23] ^ d
  40. d = c3 ^ (c0<<1 | c0>>(64-1))
  41. r[4] = a[4] ^ d
  42. r[9] = a[9] ^ d
  43. r[14] = a[14] ^ d
  44. r[19] = a[19] ^ d
  45. r[24] = a[24] ^ d
  46. return r
  47. }
  48. func rhopiU64Version(a [25]uint64) [25]uint64 {
  49. var t uint64
  50. t = a[1]
  51. t, a[10] = a[10], t<<1|t>>(64-1)
  52. t, a[7] = a[7], t<<3|t>>(64-3)
  53. t, a[11] = a[11], t<<6|t>>(64-6)
  54. t, a[17] = a[17], t<<10|t>>(64-10)
  55. t, a[18] = a[18], t<<15|t>>(64-15)
  56. t, a[3] = a[3], t<<21|t>>(64-21)
  57. t, a[5] = a[5], t<<28|t>>(64-28)
  58. t, a[16] = a[16], t<<36|t>>(64-36)
  59. t, a[8] = a[8], t<<45|t>>(64-45)
  60. t, a[21] = a[21], t<<55|t>>(64-55)
  61. t, a[24] = a[24], t<<2|t>>(64-2)
  62. t, a[4] = a[4], t<<14|t>>(64-14)
  63. t, a[15] = a[15], t<<27|t>>(64-27)
  64. t, a[23] = a[23], t<<41|t>>(64-41)
  65. t, a[19] = a[19], t<<56|t>>(64-56)
  66. t, a[13] = a[13], t<<8|t>>(64-8)
  67. t, a[12] = a[12], t<<25|t>>(64-25)
  68. t, a[2] = a[2], t<<43|t>>(64-43)
  69. t, a[20] = a[20], t<<62|t>>(64-62)
  70. t, a[14] = a[14], t<<18|t>>(64-18)
  71. t, a[22] = a[22], t<<39|t>>(64-39)
  72. t, a[9] = a[9], t<<61|t>>(64-61)
  73. t, a[6] = a[6], t<<20|t>>(64-20)
  74. a[1] = t<<44 | t>>(64-44)
  75. return a
  76. }
  77. func chiU64Version(a [25]uint64) [25]uint64 {
  78. var bc0, bc1, bc2, bc3, bc4 uint64
  79. bc0 = a[0]
  80. bc1 = a[1]
  81. bc2 = a[2]
  82. bc3 = a[3]
  83. bc4 = a[4]
  84. a[0] ^= (^bc1) & bc2
  85. a[1] ^= (^bc2) & bc3
  86. a[2] ^= (^bc3) & bc4
  87. a[3] ^= (^bc4) & bc0
  88. a[4] ^= (^bc0) & bc1
  89. bc0 = a[5]
  90. bc1 = a[6]
  91. bc2 = a[7]
  92. bc3 = a[8]
  93. bc4 = a[9]
  94. a[5] ^= (^bc1) & bc2
  95. a[6] ^= (^bc2) & bc3
  96. a[7] ^= (^bc3) & bc4
  97. a[8] ^= (^bc4) & bc0
  98. a[9] ^= (^bc0) & bc1
  99. bc0 = a[10]
  100. bc1 = a[11]
  101. bc2 = a[12]
  102. bc3 = a[13]
  103. bc4 = a[14]
  104. a[10] ^= (^bc1) & bc2
  105. a[11] ^= (^bc2) & bc3
  106. a[12] ^= (^bc3) & bc4
  107. a[13] ^= (^bc4) & bc0
  108. a[14] ^= (^bc0) & bc1
  109. bc0 = a[15]
  110. bc1 = a[16]
  111. bc2 = a[17]
  112. bc3 = a[18]
  113. bc4 = a[19]
  114. a[15] ^= (^bc1) & bc2
  115. a[16] ^= (^bc2) & bc3
  116. a[17] ^= (^bc3) & bc4
  117. a[18] ^= (^bc4) & bc0
  118. a[19] ^= (^bc0) & bc1
  119. bc0 = a[20]
  120. bc1 = a[21]
  121. bc2 = a[22]
  122. bc3 = a[23]
  123. bc4 = a[24]
  124. a[20] ^= (^bc1) & bc2
  125. a[21] ^= (^bc2) & bc3
  126. a[22] ^= (^bc3) & bc4
  127. a[23] ^= (^bc4) & bc0
  128. a[24] ^= (^bc0) & bc1
  129. return a
  130. }
  131. func iotU64Version(a [25]uint64, r int) [25]uint64 {
  132. // iota
  133. a[0] ^= roundConstantsU64[r]
  134. return a
  135. }