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.

237 lines
8.4 KiB

3 years ago
  1. package keccak
  2. var roundConstantsU64 = []uint64{
  3. 0x0000000000000001, 0x0000000000008082, 0x800000000000808A,
  4. 0x8000000080008000, 0x000000000000808B, 0x0000000080000001,
  5. 0x8000000080008081, 0x8000000000008009, 0x000000000000008A,
  6. 0x0000000000000088, 0x0000000080008009, 0x000000008000000A,
  7. 0x000000008000808B, 0x800000000000008B, 0x8000000000008089,
  8. 0x8000000000008003, 0x8000000000008002, 0x8000000000000080,
  9. 0x000000000000800A, 0x800000008000000A, 0x8000000080008081,
  10. 0x8000000000008080, 0x0000000080000001, 0x8000000080008008,
  11. }
  12. var roundConstants = u64ArrayToBits(roundConstantsU64)
  13. func theta(a [25 * 64]bool) [25 * 64]bool {
  14. var c0, c1, c2, c3, c4, d [64]bool
  15. var r [25 * 64]bool
  16. copy(c0[:], xor(xor(xor(xor(a[0:1*64], a[5*64:6*64]), a[10*64:11*64]), a[15*64:16*64]), a[20*64:21*64]))
  17. copy(c1[:], xor(xor(xor(xor(a[1*64:2*64], a[6*64:7*64]), a[11*64:12*64]), a[16*64:17*64]), a[21*64:22*64]))
  18. copy(c2[:], xor(xor(xor(xor(a[2*64:3*64], a[7*64:8*64]), a[12*64:13*64]), a[17*64:18*64]), a[22*64:23*64]))
  19. copy(c3[:], xor(xor(xor(xor(a[3*64:4*64], a[8*64:9*64]), a[13*64:14*64]), a[18*64:19*64]), a[23*64:24*64]))
  20. copy(c4[:], xor(xor(xor(xor(a[4*64:5*64], a[9*64:10*64]), a[14*64:15*64]), a[19*64:20*64]), a[24*64:25*64]))
  21. copy(d[:], xor(c4[:], or(leftShift(c1[:], 1), rightShift(c1[:], (64-1)))))
  22. copy(r[0:1*64], xor(a[0:1*64], d[:]))
  23. copy(r[5*64:6*64], xor(a[5*64:6*64], d[:]))
  24. copy(r[10*64:11*64], xor(a[10*64:11*64], d[:]))
  25. copy(r[15*64:16*64], xor(a[15*64:16*64], d[:]))
  26. copy(r[20*64:21*64], xor(a[20*64:21*64], d[:]))
  27. copy(d[:], xor(c0[:], or(leftShift(c2[:], 1), rightShift(c2[:], (64-1)))))
  28. copy(r[1*64:2*64], xor(a[1*64:2*64], d[:]))
  29. copy(r[6*64:7*64], xor(a[6*64:7*64], d[:]))
  30. copy(r[11*64:12*64], xor(a[11*64:12*64], d[:]))
  31. copy(r[16*64:17*64], xor(a[16*64:17*64], d[:]))
  32. copy(r[21*64:22*64], xor(a[21*64:22*64], d[:]))
  33. copy(d[:], xor(c1[:], or(leftShift(c3[:], 1), rightShift(c3[:], (64-1)))))
  34. copy(r[2*64:3*64], xor(a[2*64:3*64], d[:]))
  35. copy(r[7*64:8*64], xor(a[7*64:8*64], d[:]))
  36. copy(r[12*64:13*64], xor(a[12*64:13*64], d[:]))
  37. copy(r[17*64:18*64], xor(a[17*64:18*64], d[:]))
  38. copy(r[22*64:23*64], xor(a[22*64:23*64], d[:]))
  39. copy(d[:], xor(c2[:], or(leftShift(c4[:], 1), rightShift(c4[:], (64-1)))))
  40. copy(r[3*64:4*64], xor(a[3*64:4*64], d[:]))
  41. copy(r[8*64:9*64], xor(a[8*64:9*64], d[:]))
  42. copy(r[13*64:14*64], xor(a[13*64:14*64], d[:]))
  43. copy(r[18*64:19*64], xor(a[18*64:19*64], d[:]))
  44. copy(r[23*64:24*64], xor(a[23*64:24*64], d[:]))
  45. copy(d[:], xor(c3[:], or(leftShift(c0[:], 1), rightShift(c0[:], (64-1)))))
  46. copy(r[4*64:5*64], xor(a[4*64:5*64], d[:]))
  47. copy(r[9*64:10*64], xor(a[9*64:10*64], d[:]))
  48. copy(r[14*64:15*64], xor(a[14*64:15*64], d[:]))
  49. copy(r[19*64:20*64], xor(a[19*64:20*64], d[:]))
  50. copy(r[24*64:25*64], xor(a[24*64:25*64], d[:]))
  51. return r
  52. }
  53. func rhopi(a [25 * 64]bool) [25 * 64]bool {
  54. var t, tAux [64]bool
  55. var r [25 * 64]bool
  56. copy(r[0:1*64], a[0:1*64])
  57. copy(t[:], a[1*64:2*64])
  58. copy(tAux[:], a[10*64:11*64])
  59. copy(r[10*64:11*64], or(leftShift(t[:], 1), rightShift(t[:], 64-1)))
  60. copy(t[:], tAux[:])
  61. copy(tAux[:], a[7*64:8*64])
  62. copy(r[7*64:8*64], or(leftShift(t[:], 3), rightShift(t[:], 64-3)))
  63. copy(t[:], tAux[:])
  64. copy(tAux[:], a[11*64:12*64])
  65. copy(r[11*64:12*64], or(leftShift(t[:], 6), rightShift(t[:], 64-6)))
  66. copy(t[:], tAux[:])
  67. copy(tAux[:], a[17*64:18*64])
  68. copy(r[17*64:18*64], or(leftShift(t[:], 10), rightShift(t[:], 64-10)))
  69. copy(t[:], tAux[:])
  70. copy(tAux[:], a[18*64:19*64])
  71. copy(r[18*64:19*64], or(leftShift(t[:], 15), rightShift(t[:], 64-15)))
  72. copy(t[:], tAux[:])
  73. copy(tAux[:], a[3*64:4*64])
  74. copy(r[3*64:4*64], or(leftShift(t[:], 21), rightShift(t[:], 64-21)))
  75. copy(t[:], tAux[:])
  76. copy(tAux[:], a[5*64:6*64])
  77. copy(r[5*64:6*64], or(leftShift(t[:], 28), rightShift(t[:], 64-28)))
  78. copy(t[:], tAux[:])
  79. copy(tAux[:], a[16*64:17*64])
  80. copy(r[16*64:17*64], or(leftShift(t[:], 36), rightShift(t[:], 64-36)))
  81. copy(t[:], tAux[:])
  82. copy(tAux[:], a[8*64:9*64])
  83. copy(r[8*64:9*64], or(leftShift(t[:], 45), rightShift(t[:], 64-45)))
  84. copy(t[:], tAux[:])
  85. copy(tAux[:], a[21*64:22*64])
  86. copy(r[21*64:22*64], or(leftShift(t[:], 55), rightShift(t[:], 64-55)))
  87. copy(t[:], tAux[:])
  88. copy(tAux[:], a[24*64:25*64])
  89. copy(r[24*64:25*64], or(leftShift(t[:], 2), rightShift(t[:], 64-2)))
  90. copy(t[:], tAux[:])
  91. copy(tAux[:], a[4*64:5*64])
  92. copy(r[4*64:5*64], or(leftShift(t[:], 14), rightShift(t[:], 64-14)))
  93. copy(t[:], tAux[:])
  94. copy(tAux[:], a[15*64:16*64])
  95. copy(r[15*64:16*64], or(leftShift(t[:], 27), rightShift(t[:], 64-27)))
  96. copy(t[:], tAux[:])
  97. copy(tAux[:], a[23*64:24*64])
  98. copy(r[23*64:24*64], or(leftShift(t[:], 41), rightShift(t[:], 64-41)))
  99. copy(t[:], tAux[:])
  100. copy(tAux[:], a[19*64:20*64])
  101. copy(r[19*64:20*64], or(leftShift(t[:], 56), rightShift(t[:], 64-56)))
  102. copy(t[:], tAux[:])
  103. copy(tAux[:], a[13*64:14*64])
  104. copy(r[13*64:14*64], or(leftShift(t[:], 8), rightShift(t[:], 64-8)))
  105. copy(t[:], tAux[:])
  106. copy(tAux[:], a[12*64:13*64])
  107. copy(r[12*64:13*64], or(leftShift(t[:], 25), rightShift(t[:], 64-25)))
  108. copy(t[:], tAux[:])
  109. copy(tAux[:], a[2*64:3*64])
  110. copy(r[2*64:3*64], or(leftShift(t[:], 43), rightShift(t[:], 64-43)))
  111. copy(t[:], tAux[:])
  112. copy(tAux[:], a[20*64:21*64])
  113. copy(r[20*64:21*64], or(leftShift(t[:], 62), rightShift(t[:], 64-62)))
  114. copy(t[:], tAux[:])
  115. copy(tAux[:], a[14*64:15*64])
  116. copy(r[14*64:15*64], or(leftShift(t[:], 18), rightShift(t[:], 64-18)))
  117. copy(t[:], tAux[:])
  118. copy(tAux[:], a[22*64:23*64])
  119. copy(r[22*64:23*64], or(leftShift(t[:], 39), rightShift(t[:], 64-39)))
  120. copy(t[:], tAux[:])
  121. copy(tAux[:], a[9*64:10*64])
  122. copy(r[9*64:10*64], or(leftShift(t[:], 61), rightShift(t[:], 64-61)))
  123. copy(t[:], tAux[:])
  124. copy(tAux[:], a[6*64:7*64])
  125. copy(r[6*64:7*64], or(leftShift(t[:], 20), rightShift(t[:], 64-20)))
  126. copy(t[:], tAux[:])
  127. copy(r[1*64:2*64], or(leftShift(t[:], 44), rightShift(t[:], 64-44)))
  128. return r
  129. }
  130. func chi(a [25 * 64]bool) [25 * 64]bool {
  131. var c0, c1, c2, c3, c4 [64]bool
  132. var r [25 * 64]bool
  133. copy(c0[:], a[0:1*64])
  134. copy(c1[:], a[1*64:2*64])
  135. copy(c2[:], a[2*64:3*64])
  136. copy(c3[:], a[3*64:4*64])
  137. copy(c4[:], a[4*64:5*64])
  138. copy(r[0:1*64], xor(a[0:1*64], and(xorSingle(c1[:]), c2[:])))
  139. copy(r[1*64:2*64], xor(a[1*64:2*64], and(xorSingle(c2[:]), c3[:])))
  140. copy(r[2*64:3*64], xor(a[2*64:3*64], and(xorSingle(c3[:]), c4[:])))
  141. copy(r[3*64:4*64], xor(a[3*64:4*64], and(xorSingle(c4[:]), c0[:])))
  142. copy(r[4*64:5*64], xor(a[4*64:5*64], and(xorSingle(c0[:]), c1[:])))
  143. copy(c0[:], a[5*64:6*64])
  144. copy(c1[:], a[6*64:7*64])
  145. copy(c2[:], a[7*64:8*64])
  146. copy(c3[:], a[8*64:9*64])
  147. copy(c4[:], a[9*64:10*64])
  148. copy(r[5*64:6*64], xor(a[5*64:6*64], and(xorSingle(c1[:]), c2[:])))
  149. copy(r[6*64:7*64], xor(a[6*64:7*64], and(xorSingle(c2[:]), c3[:])))
  150. copy(r[7*64:8*64], xor(a[7*64:8*64], and(xorSingle(c3[:]), c4[:])))
  151. copy(r[8*64:9*64], xor(a[8*64:9*64], and(xorSingle(c4[:]), c0[:])))
  152. copy(r[9*64:10*64], xor(a[9*64:10*64], and(xorSingle(c0[:]), c1[:])))
  153. copy(c0[:], a[10*64:11*64])
  154. copy(c1[:], a[11*64:12*64])
  155. copy(c2[:], a[12*64:13*64])
  156. copy(c3[:], a[13*64:14*64])
  157. copy(c4[:], a[14*64:15*64])
  158. copy(r[10*64:11*64], xor(a[10*64:11*64], and(xorSingle(c1[:]), c2[:])))
  159. copy(r[11*64:12*64], xor(a[11*64:12*64], and(xorSingle(c2[:]), c3[:])))
  160. copy(r[12*64:13*64], xor(a[12*64:13*64], and(xorSingle(c3[:]), c4[:])))
  161. copy(r[13*64:14*64], xor(a[13*64:14*64], and(xorSingle(c4[:]), c0[:])))
  162. copy(r[14*64:15*64], xor(a[14*64:15*64], and(xorSingle(c0[:]), c1[:])))
  163. copy(c0[:], a[15*64:16*64])
  164. copy(c1[:], a[16*64:17*64])
  165. copy(c2[:], a[17*64:18*64])
  166. copy(c3[:], a[18*64:19*64])
  167. copy(c4[:], a[19*64:20*64])
  168. copy(r[15*64:16*64], xor(a[15*64:16*64], and(xorSingle(c1[:]), c2[:])))
  169. copy(r[16*64:17*64], xor(a[16*64:17*64], and(xorSingle(c2[:]), c3[:])))
  170. copy(r[17*64:18*64], xor(a[17*64:18*64], and(xorSingle(c3[:]), c4[:])))
  171. copy(r[18*64:19*64], xor(a[18*64:19*64], and(xorSingle(c4[:]), c0[:])))
  172. copy(r[19*64:20*64], xor(a[19*64:20*64], and(xorSingle(c0[:]), c1[:])))
  173. copy(c0[:], a[20*64:21*64])
  174. copy(c1[:], a[21*64:22*64])
  175. copy(c2[:], a[22*64:23*64])
  176. copy(c3[:], a[23*64:24*64])
  177. copy(c4[:], a[24*64:25*64])
  178. copy(r[20*64:21*64], xor(a[20*64:21*64], and(xorSingle(c1[:]), c2[:])))
  179. copy(r[21*64:22*64], xor(a[21*64:22*64], and(xorSingle(c2[:]), c3[:])))
  180. copy(r[22*64:23*64], xor(a[22*64:23*64], and(xorSingle(c3[:]), c4[:])))
  181. copy(r[23*64:24*64], xor(a[23*64:24*64], and(xorSingle(c4[:]), c0[:])))
  182. copy(r[24*64:25*64], xor(a[24*64:25*64], and(xorSingle(c0[:]), c1[:])))
  183. return r
  184. }
  185. func iot(a [25 * 64]bool, r int) [25 * 64]bool {
  186. // iota
  187. copy(a[0:64], xor(a[0:64], roundConstants[r*64:r*64+64]))
  188. return a
  189. }