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.

144 lines
6.8 KiB

3 years ago
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 r [25 * 64]bool
  44. copy(r[0:1*64], a[0:1*64])
  45. copy(r[10*64:11*64], or(leftShift(a[1*64:2*64], 1), rightShift(a[1*64:2*64], 64-1)))
  46. copy(r[7*64:8*64], or(leftShift(a[10*64:11*64], 3), rightShift(a[10*64:11*64], 64-3)))
  47. copy(r[11*64:12*64], or(leftShift(a[7*64:8*64], 6), rightShift(a[7*64:8*64], 64-6)))
  48. copy(r[17*64:18*64], or(leftShift(a[11*64:12*64], 10), rightShift(a[11*64:12*64], 64-10)))
  49. copy(r[18*64:19*64], or(leftShift(a[17*64:18*64], 15), rightShift(a[17*64:18*64], 64-15)))
  50. copy(r[3*64:4*64], or(leftShift(a[18*64:19*64], 21), rightShift(a[18*64:19*64], 64-21)))
  51. copy(r[5*64:6*64], or(leftShift(a[3*64:4*64], 28), rightShift(a[3*64:4*64], 64-28)))
  52. copy(r[16*64:17*64], or(leftShift(a[5*64:6*64], 36), rightShift(a[5*64:6*64], 64-36)))
  53. copy(r[8*64:9*64], or(leftShift(a[16*64:17*64], 45), rightShift(a[16*64:17*64], 64-45)))
  54. copy(r[21*64:22*64], or(leftShift(a[8*64:9*64], 55), rightShift(a[8*64:9*64], 64-55)))
  55. copy(r[24*64:25*64], or(leftShift(a[21*64:22*64], 2), rightShift(a[21*64:22*64], 64-2)))
  56. copy(r[4*64:5*64], or(leftShift(a[24*64:25*64], 14), rightShift(a[24*64:25*64], 64-14)))
  57. copy(r[15*64:16*64], or(leftShift(a[4*64:5*64], 27), rightShift(a[4*64:5*64], 64-27)))
  58. copy(r[23*64:24*64], or(leftShift(a[15*64:16*64], 41), rightShift(a[15*64:16*64], 64-41)))
  59. copy(r[19*64:20*64], or(leftShift(a[23*64:24*64], 56), rightShift(a[23*64:24*64], 64-56)))
  60. copy(r[13*64:14*64], or(leftShift(a[19*64:20*64], 8), rightShift(a[19*64:20*64], 64-8)))
  61. copy(r[12*64:13*64], or(leftShift(a[13*64:14*64], 25), rightShift(a[13*64:14*64], 64-25)))
  62. copy(r[2*64:3*64], or(leftShift(a[12*64:13*64], 43), rightShift(a[12*64:13*64], 64-43)))
  63. copy(r[20*64:21*64], or(leftShift(a[2*64:3*64], 62), rightShift(a[2*64:3*64], 64-62)))
  64. copy(r[14*64:15*64], or(leftShift(a[20*64:21*64], 18), rightShift(a[20*64:21*64], 64-18)))
  65. copy(r[22*64:23*64], or(leftShift(a[14*64:15*64], 39), rightShift(a[14*64:15*64], 64-39)))
  66. copy(r[9*64:10*64], or(leftShift(a[22*64:23*64], 61), rightShift(a[22*64:23*64], 64-61)))
  67. copy(r[6*64:7*64], or(leftShift(a[9*64:10*64], 20), rightShift(a[9*64:10*64], 64-20)))
  68. copy(r[1*64:2*64], or(leftShift(a[6*64:7*64], 44), rightShift(a[6*64:7*64], 64-44)))
  69. return r
  70. }
  71. func chi(a [25 * 64]bool) [25 * 64]bool {
  72. var r [25 * 64]bool
  73. copy(r[0:1*64], xor(a[0:1*64], and(xorSingle(a[1*64:2*64]), a[2*64:3*64])))
  74. copy(r[1*64:2*64], xor(a[1*64:2*64], and(xorSingle(a[2*64:3*64]), a[3*64:4*64])))
  75. copy(r[2*64:3*64], xor(a[2*64:3*64], and(xorSingle(a[3*64:4*64]), a[4*64:5*64])))
  76. copy(r[3*64:4*64], xor(a[3*64:4*64], and(xorSingle(a[4*64:5*64]), a[0:1*64])))
  77. copy(r[4*64:5*64], xor(a[4*64:5*64], and(xorSingle(a[0:1*64]), a[1*64:2*64])))
  78. copy(r[5*64:6*64], xor(a[5*64:6*64], and(xorSingle(a[6*64:7*64]), a[7*64:8*64])))
  79. copy(r[6*64:7*64], xor(a[6*64:7*64], and(xorSingle(a[7*64:8*64]), a[8*64:9*64])))
  80. copy(r[7*64:8*64], xor(a[7*64:8*64], and(xorSingle(a[8*64:9*64]), a[9*64:10*64])))
  81. copy(r[8*64:9*64], xor(a[8*64:9*64], and(xorSingle(a[9*64:10*64]), a[5*64:6*64])))
  82. copy(r[9*64:10*64], xor(a[9*64:10*64], and(xorSingle(a[5*64:6*64]), a[6*64:7*64])))
  83. copy(r[10*64:11*64], xor(a[10*64:11*64], and(xorSingle(a[11*64:12*64]), a[12*64:13*64])))
  84. copy(r[11*64:12*64], xor(a[11*64:12*64], and(xorSingle(a[12*64:13*64]), a[13*64:14*64])))
  85. copy(r[12*64:13*64], xor(a[12*64:13*64], and(xorSingle(a[13*64:14*64]), a[14*64:15*64])))
  86. copy(r[13*64:14*64], xor(a[13*64:14*64], and(xorSingle(a[14*64:15*64]), a[10*64:11*64])))
  87. copy(r[14*64:15*64], xor(a[14*64:15*64], and(xorSingle(a[10*64:11*64]), a[11*64:12*64])))
  88. copy(r[15*64:16*64], xor(a[15*64:16*64], and(xorSingle(a[16*64:17*64]), a[17*64:18*64])))
  89. copy(r[16*64:17*64], xor(a[16*64:17*64], and(xorSingle(a[17*64:18*64]), a[18*64:19*64])))
  90. copy(r[17*64:18*64], xor(a[17*64:18*64], and(xorSingle(a[18*64:19*64]), a[19*64:20*64])))
  91. copy(r[18*64:19*64], xor(a[18*64:19*64], and(xorSingle(a[19*64:20*64]), a[15*64:16*64])))
  92. copy(r[19*64:20*64], xor(a[19*64:20*64], and(xorSingle(a[15*64:16*64]), a[16*64:17*64])))
  93. copy(r[20*64:21*64], xor(a[20*64:21*64], and(xorSingle(a[21*64:22*64]), a[22*64:23*64])))
  94. copy(r[21*64:22*64], xor(a[21*64:22*64], and(xorSingle(a[22*64:23*64]), a[23*64:24*64])))
  95. copy(r[22*64:23*64], xor(a[22*64:23*64], and(xorSingle(a[23*64:24*64]), a[24*64:25*64])))
  96. copy(r[23*64:24*64], xor(a[23*64:24*64], and(xorSingle(a[24*64:25*64]), a[20*64:21*64])))
  97. copy(r[24*64:25*64], xor(a[24*64:25*64], and(xorSingle(a[20*64:21*64]), a[21*64:22*64])))
  98. return r
  99. }
  100. func iot(a [25 * 64]bool, r int) [25 * 64]bool {
  101. // iota
  102. copy(a[0:64], xor(a[0:64], roundConstants[r*64:r*64+64]))
  103. return a
  104. }