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.

218 lines
7.7 KiB

3 years ago
  1. package keccak
  2. func theta(a [25 * 64]bool) [25 * 64]bool {
  3. var c0, c1, c2, c3, c4, d [64]bool
  4. var r [25 * 64]bool
  5. 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]))
  6. 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]))
  7. 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]))
  8. 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]))
  9. 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]))
  10. copy(d[:], xor(c4[:], or(leftShift(c1[:], 1), rightShift(c1[:], (64-1)))))
  11. copy(r[0:1*64], xor(a[0:1*64], d[:]))
  12. copy(r[5*64:6*64], xor(a[5*64:6*64], d[:]))
  13. copy(r[10*64:11*64], xor(a[10*64:11*64], d[:]))
  14. copy(r[15*64:16*64], xor(a[15*64:16*64], d[:]))
  15. copy(r[20*64:21*64], xor(a[20*64:21*64], d[:]))
  16. copy(d[:], xor(c0[:], or(leftShift(c2[:], 1), rightShift(c2[:], (64-1)))))
  17. copy(r[1*64:2*64], xor(a[1*64:2*64], d[:]))
  18. copy(r[6*64:7*64], xor(a[6*64:7*64], d[:]))
  19. copy(r[11*64:12*64], xor(a[11*64:12*64], d[:]))
  20. copy(r[16*64:17*64], xor(a[16*64:17*64], d[:]))
  21. copy(r[21*64:22*64], xor(a[21*64:22*64], d[:]))
  22. copy(d[:], xor(c1[:], or(leftShift(c3[:], 1), rightShift(c3[:], (64-1)))))
  23. copy(r[2*64:3*64], xor(a[2*64:3*64], d[:]))
  24. copy(r[7*64:8*64], xor(a[7*64:8*64], d[:]))
  25. copy(r[12*64:13*64], xor(a[12*64:13*64], d[:]))
  26. copy(r[17*64:18*64], xor(a[17*64:18*64], d[:]))
  27. copy(r[22*64:23*64], xor(a[22*64:23*64], d[:]))
  28. copy(d[:], xor(c2[:], or(leftShift(c4[:], 1), rightShift(c4[:], (64-1)))))
  29. copy(r[3*64:4*64], xor(a[3*64:4*64], d[:]))
  30. copy(r[8*64:9*64], xor(a[8*64:9*64], d[:]))
  31. copy(r[13*64:14*64], xor(a[13*64:14*64], d[:]))
  32. copy(r[18*64:19*64], xor(a[18*64:19*64], d[:]))
  33. copy(r[23*64:24*64], xor(a[23*64:24*64], d[:]))
  34. copy(d[:], xor(c3[:], or(leftShift(c0[:], 1), rightShift(c0[:], (64-1)))))
  35. copy(r[4*64:5*64], xor(a[4*64:5*64], d[:]))
  36. copy(r[9*64:10*64], xor(a[9*64:10*64], d[:]))
  37. copy(r[14*64:15*64], xor(a[14*64:15*64], d[:]))
  38. copy(r[19*64:20*64], xor(a[19*64:20*64], d[:]))
  39. copy(r[24*64:25*64], xor(a[24*64:25*64], d[:]))
  40. return r
  41. }
  42. func rhopi(a [25 * 64]bool) [25 * 64]bool {
  43. var t, tAux [64]bool
  44. var r [25 * 64]bool
  45. copy(r[0:1*64], a[0:1*64])
  46. copy(t[:], a[1*64:2*64])
  47. copy(tAux[:], a[10*64:11*64])
  48. copy(r[10*64:11*64], or(leftShift(t[:], 1), rightShift(t[:], 64-1)))
  49. copy(t[:], tAux[:])
  50. copy(tAux[:], a[7*64:8*64])
  51. copy(r[7*64:8*64], or(leftShift(t[:], 3), rightShift(t[:], 64-3)))
  52. copy(t[:], tAux[:])
  53. copy(tAux[:], a[11*64:12*64])
  54. copy(r[11*64:12*64], or(leftShift(t[:], 6), rightShift(t[:], 64-6)))
  55. copy(t[:], tAux[:])
  56. copy(tAux[:], a[17*64:18*64])
  57. copy(r[17*64:18*64], or(leftShift(t[:], 10), rightShift(t[:], 64-10)))
  58. copy(t[:], tAux[:])
  59. copy(tAux[:], a[18*64:19*64])
  60. copy(r[18*64:19*64], or(leftShift(t[:], 15), rightShift(t[:], 64-15)))
  61. copy(t[:], tAux[:])
  62. copy(tAux[:], a[3*64:4*64])
  63. copy(r[3*64:4*64], or(leftShift(t[:], 21), rightShift(t[:], 64-21)))
  64. copy(t[:], tAux[:])
  65. copy(tAux[:], a[5*64:6*64])
  66. copy(r[5*64:6*64], or(leftShift(t[:], 28), rightShift(t[:], 64-28)))
  67. copy(t[:], tAux[:])
  68. copy(tAux[:], a[16*64:17*64])
  69. copy(r[16*64:17*64], or(leftShift(t[:], 36), rightShift(t[:], 64-36)))
  70. copy(t[:], tAux[:])
  71. copy(tAux[:], a[8*64:9*64])
  72. copy(r[8*64:9*64], or(leftShift(t[:], 45), rightShift(t[:], 64-45)))
  73. copy(t[:], tAux[:])
  74. copy(tAux[:], a[21*64:22*64])
  75. copy(r[21*64:22*64], or(leftShift(t[:], 55), rightShift(t[:], 64-55)))
  76. copy(t[:], tAux[:])
  77. copy(tAux[:], a[24*64:25*64])
  78. copy(r[24*64:25*64], or(leftShift(t[:], 2), rightShift(t[:], 64-2)))
  79. copy(t[:], tAux[:])
  80. copy(tAux[:], a[4*64:5*64])
  81. copy(r[4*64:5*64], or(leftShift(t[:], 14), rightShift(t[:], 64-14)))
  82. copy(t[:], tAux[:])
  83. copy(tAux[:], a[15*64:16*64])
  84. copy(r[15*64:16*64], or(leftShift(t[:], 27), rightShift(t[:], 64-27)))
  85. copy(t[:], tAux[:])
  86. copy(tAux[:], a[23*64:24*64])
  87. copy(r[23*64:24*64], or(leftShift(t[:], 41), rightShift(t[:], 64-41)))
  88. copy(t[:], tAux[:])
  89. copy(tAux[:], a[19*64:20*64])
  90. copy(r[19*64:20*64], or(leftShift(t[:], 56), rightShift(t[:], 64-56)))
  91. copy(t[:], tAux[:])
  92. copy(tAux[:], a[13*64:14*64])
  93. copy(r[13*64:14*64], or(leftShift(t[:], 8), rightShift(t[:], 64-8)))
  94. copy(t[:], tAux[:])
  95. copy(tAux[:], a[12*64:13*64])
  96. copy(r[12*64:13*64], or(leftShift(t[:], 25), rightShift(t[:], 64-25)))
  97. copy(t[:], tAux[:])
  98. copy(tAux[:], a[2*64:3*64])
  99. copy(r[2*64:3*64], or(leftShift(t[:], 43), rightShift(t[:], 64-43)))
  100. copy(t[:], tAux[:])
  101. copy(tAux[:], a[20*64:21*64])
  102. copy(r[20*64:21*64], or(leftShift(t[:], 62), rightShift(t[:], 64-62)))
  103. copy(t[:], tAux[:])
  104. copy(tAux[:], a[14*64:15*64])
  105. copy(r[14*64:15*64], or(leftShift(t[:], 18), rightShift(t[:], 64-18)))
  106. copy(t[:], tAux[:])
  107. copy(tAux[:], a[22*64:23*64])
  108. copy(r[22*64:23*64], or(leftShift(t[:], 39), rightShift(t[:], 64-39)))
  109. copy(t[:], tAux[:])
  110. copy(tAux[:], a[9*64:10*64])
  111. copy(r[9*64:10*64], or(leftShift(t[:], 61), rightShift(t[:], 64-61)))
  112. copy(t[:], tAux[:])
  113. copy(tAux[:], a[6*64:7*64])
  114. copy(r[6*64:7*64], or(leftShift(t[:], 20), rightShift(t[:], 64-20)))
  115. copy(t[:], tAux[:])
  116. copy(r[1*64:2*64], or(leftShift(t[:], 44), rightShift(t[:], 64-44)))
  117. return r
  118. }
  119. func chi(a [25 * 64]bool) [25 * 64]bool {
  120. var c0, c1, c2, c3, c4 [64]bool
  121. var r [25 * 64]bool
  122. copy(c0[:], a[0:1*64])
  123. copy(c1[:], a[1*64:2*64])
  124. copy(c2[:], a[2*64:3*64])
  125. copy(c3[:], a[3*64:4*64])
  126. copy(c4[:], a[4*64:5*64])
  127. copy(r[0:1*64], xor(a[0:1*64], and(xorSingle(c1[:]), c2[:])))
  128. copy(r[1*64:2*64], xor(a[1*64:2*64], and(xorSingle(c2[:]), c3[:])))
  129. copy(r[2*64:3*64], xor(a[2*64:3*64], and(xorSingle(c3[:]), c4[:])))
  130. copy(r[3*64:4*64], xor(a[3*64:4*64], and(xorSingle(c4[:]), c0[:])))
  131. copy(r[4*64:5*64], xor(a[4*64:5*64], and(xorSingle(c0[:]), c1[:])))
  132. copy(c0[:], a[5*64:6*64])
  133. copy(c1[:], a[6*64:7*64])
  134. copy(c2[:], a[7*64:8*64])
  135. copy(c3[:], a[8*64:9*64])
  136. copy(c4[:], a[9*64:10*64])
  137. copy(r[5*64:6*64], xor(a[5*64:6*64], and(xorSingle(c1[:]), c2[:])))
  138. copy(r[6*64:7*64], xor(a[6*64:7*64], and(xorSingle(c2[:]), c3[:])))
  139. copy(r[7*64:8*64], xor(a[7*64:8*64], and(xorSingle(c3[:]), c4[:])))
  140. copy(r[8*64:9*64], xor(a[8*64:9*64], and(xorSingle(c4[:]), c0[:])))
  141. copy(r[9*64:10*64], xor(a[9*64:10*64], and(xorSingle(c0[:]), c1[:])))
  142. copy(c0[:], a[10*64:11*64])
  143. copy(c1[:], a[11*64:12*64])
  144. copy(c2[:], a[12*64:13*64])
  145. copy(c3[:], a[13*64:14*64])
  146. copy(c4[:], a[14*64:15*64])
  147. copy(r[10*64:11*64], xor(a[10*64:11*64], and(xorSingle(c1[:]), c2[:])))
  148. copy(r[11*64:12*64], xor(a[11*64:12*64], and(xorSingle(c2[:]), c3[:])))
  149. copy(r[12*64:13*64], xor(a[12*64:13*64], and(xorSingle(c3[:]), c4[:])))
  150. copy(r[13*64:14*64], xor(a[13*64:14*64], and(xorSingle(c4[:]), c0[:])))
  151. copy(r[14*64:15*64], xor(a[14*64:15*64], and(xorSingle(c0[:]), c1[:])))
  152. copy(c0[:], a[15*64:16*64])
  153. copy(c1[:], a[16*64:17*64])
  154. copy(c2[:], a[17*64:18*64])
  155. copy(c3[:], a[18*64:19*64])
  156. copy(c4[:], a[19*64:20*64])
  157. copy(r[15*64:16*64], xor(a[15*64:16*64], and(xorSingle(c1[:]), c2[:])))
  158. copy(r[16*64:17*64], xor(a[16*64:17*64], and(xorSingle(c2[:]), c3[:])))
  159. copy(r[17*64:18*64], xor(a[17*64:18*64], and(xorSingle(c3[:]), c4[:])))
  160. copy(r[18*64:19*64], xor(a[18*64:19*64], and(xorSingle(c4[:]), c0[:])))
  161. copy(r[19*64:20*64], xor(a[19*64:20*64], and(xorSingle(c0[:]), c1[:])))
  162. copy(c0[:], a[20*64:21*64])
  163. copy(c1[:], a[21*64:22*64])
  164. copy(c2[:], a[22*64:23*64])
  165. copy(c3[:], a[23*64:24*64])
  166. copy(c4[:], a[24*64:25*64])
  167. copy(r[20*64:21*64], xor(a[20*64:21*64], and(xorSingle(c1[:]), c2[:])))
  168. copy(r[21*64:22*64], xor(a[21*64:22*64], and(xorSingle(c2[:]), c3[:])))
  169. copy(r[22*64:23*64], xor(a[22*64:23*64], and(xorSingle(c3[:]), c4[:])))
  170. copy(r[23*64:24*64], xor(a[23*64:24*64], and(xorSingle(c4[:]), c0[:])))
  171. copy(r[24*64:25*64], xor(a[24*64:25*64], and(xorSingle(c0[:]), c1[:])))
  172. return r
  173. }