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.

334 lines
6.6 KiB

  1. // Package keccak implements the Keccak (SHA-3) hash algorithm.
  2. // http://keccak.noekeon.org / FIPS 202 draft.
  3. package cryptonight
  4. // import "fmt"
  5. import "hash"
  6. const (
  7. domainNone = 1
  8. domainSHA3 = 0x06
  9. domainSHAKE = 0x1f
  10. )
  11. const rounds = 24
  12. var roundConstants = []uint64{
  13. 0x0000000000000001, 0x0000000000008082,
  14. 0x800000000000808A, 0x8000000080008000,
  15. 0x000000000000808B, 0x0000000080000001,
  16. 0x8000000080008081, 0x8000000000008009,
  17. 0x000000000000008A, 0x0000000000000088,
  18. 0x0000000080008009, 0x000000008000000A,
  19. 0x000000008000808B, 0x800000000000008B,
  20. 0x8000000000008089, 0x8000000000008003,
  21. 0x8000000000008002, 0x8000000000000080,
  22. 0x000000000000800A, 0x800000008000000A,
  23. 0x8000000080008081, 0x8000000000008080,
  24. 0x0000000080000001, 0x8000000080008008,
  25. }
  26. type keccak struct {
  27. S [25]uint64
  28. size int
  29. blockSize int
  30. buf []byte
  31. domain byte
  32. }
  33. func newKeccak(capacity, output int, domain byte) hash.Hash {
  34. var h keccak
  35. h.size = output / 8
  36. h.blockSize = (200 - capacity/8)
  37. h.domain = domain
  38. return &h
  39. }
  40. func New224() hash.Hash {
  41. return newKeccak(224*2, 224, domainNone)
  42. }
  43. func New256() hash.Hash {
  44. return newKeccak(256*2, 256, domainNone)
  45. }
  46. func New384() hash.Hash {
  47. return newKeccak(384*2, 384, domainNone)
  48. }
  49. func New512() hash.Hash {
  50. return newKeccak(512*2, 512, domainNone)
  51. }
  52. func (k *keccak) Write(b []byte) (int, error) {
  53. n := len(b)
  54. if len(k.buf) > 0 {
  55. x := k.blockSize - len(k.buf)
  56. if x > len(b) {
  57. x = len(b)
  58. }
  59. k.buf = append(k.buf, b[:x]...)
  60. b = b[x:]
  61. if len(k.buf) < k.blockSize {
  62. return n, nil
  63. }
  64. k.absorb(k.buf)
  65. k.buf = nil
  66. }
  67. for len(b) >= k.blockSize {
  68. k.absorb(b[:k.blockSize])
  69. b = b[k.blockSize:]
  70. }
  71. k.buf = b
  72. return n, nil
  73. }
  74. func (k0 *keccak) Sum(b []byte) []byte {
  75. k := *k0
  76. k.final()
  77. return k.squeeze(b)
  78. }
  79. func (k *keccak) Reset() {
  80. for i := range k.S {
  81. k.S[i] = 0
  82. }
  83. k.buf = nil
  84. }
  85. func (k *keccak) Size() int {
  86. return k.size
  87. }
  88. func (k *keccak) BlockSize() int {
  89. return k.blockSize
  90. }
  91. func (k *keccak) absorb(block []byte) {
  92. if len(block) != k.blockSize {
  93. panic("absorb() called with invalid block size")
  94. }
  95. for i := 0; i < k.blockSize/8; i++ {
  96. k.S[i] ^= uint64le(block[i*8:])
  97. }
  98. keccakf(&k.S)
  99. }
  100. func (k *keccak) pad(block []byte) []byte {
  101. padded := make([]byte, k.blockSize)
  102. copy(padded, k.buf)
  103. padded[len(k.buf)] = k.domain
  104. padded[len(padded)-1] |= 0x80
  105. return padded
  106. }
  107. func (k *keccak) final() {
  108. last := k.pad(k.buf)
  109. k.absorb(last)
  110. }
  111. func (k *keccak) squeeze(b []byte) []byte {
  112. buf := make([]byte, 8*len(k.S))
  113. n := k.size
  114. for {
  115. for i := range k.S {
  116. putUint64le(buf[i*8:], k.S[i])
  117. }
  118. if n <= k.blockSize {
  119. b = append(b, buf[:n]...)
  120. break
  121. }
  122. b = append(b, buf[:k.blockSize]...)
  123. n -= k.blockSize
  124. keccakf(&k.S)
  125. }
  126. return b
  127. }
  128. func keccakf(S *[25]uint64) {
  129. var bc0, bc1, bc2, bc3, bc4 uint64
  130. var S0, S1, S2, S3, S4 uint64
  131. var S5, S6, S7, S8, S9 uint64
  132. var S10, S11, S12, S13, S14 uint64
  133. var S15, S16, S17, S18, S19 uint64
  134. var S20, S21, S22, S23, S24 uint64
  135. var tmp uint64
  136. //S[0]=0x2073692073696854;
  137. //S[1]=0x1747365742061
  138. // S[16]=0x8000000000000000;
  139. /* for i :=0; i< 25;i++{
  140. fmt.Printf("%2d %X\n", i, S[i])
  141. }
  142. */
  143. S0, S1, S2, S3, S4 = S[0], S[1], S[2], S[3], S[4]
  144. S5, S6, S7, S8, S9 = S[5], S[6], S[7], S[8], S[9]
  145. S10, S11, S12, S13, S14 = S[10], S[11], S[12], S[13], S[14]
  146. S15, S16, S17, S18, S19 = S[15], S[16], S[17], S[18], S[19]
  147. S20, S21, S22, S23, S24 = S[20], S[21], S[22], S[23], S[24]
  148. for r := 0; r < rounds; r++ {
  149. // theta
  150. bc0 = S0 ^ S5 ^ S10 ^ S15 ^ S20
  151. bc1 = S1 ^ S6 ^ S11 ^ S16 ^ S21
  152. bc2 = S2 ^ S7 ^ S12 ^ S17 ^ S22
  153. bc3 = S3 ^ S8 ^ S13 ^ S18 ^ S23
  154. bc4 = S4 ^ S9 ^ S14 ^ S19 ^ S24
  155. tmp = bc4 ^ (bc1<<1 | bc1>>(64-1))
  156. S0 ^= tmp
  157. S5 ^= tmp
  158. S10 ^= tmp
  159. S15 ^= tmp
  160. S20 ^= tmp
  161. tmp = bc0 ^ (bc2<<1 | bc2>>(64-1))
  162. S1 ^= tmp
  163. S6 ^= tmp
  164. S11 ^= tmp
  165. S16 ^= tmp
  166. S21 ^= tmp
  167. tmp = bc1 ^ (bc3<<1 | bc3>>(64-1))
  168. S2 ^= tmp
  169. S7 ^= tmp
  170. S12 ^= tmp
  171. S17 ^= tmp
  172. S22 ^= tmp
  173. tmp = bc2 ^ (bc4<<1 | bc4>>(64-1))
  174. S3 ^= tmp
  175. S8 ^= tmp
  176. S13 ^= tmp
  177. S18 ^= tmp
  178. S23 ^= tmp
  179. tmp = bc3 ^ (bc0<<1 | bc0>>(64-1))
  180. S4 ^= tmp
  181. S9 ^= tmp
  182. S14 ^= tmp
  183. S19 ^= tmp
  184. S24 ^= tmp
  185. // rho phi
  186. tmp = S1
  187. tmp, S10 = S10, tmp<<1|tmp>>(64-1)
  188. tmp, S7 = S7, tmp<<3|tmp>>(64-3)
  189. tmp, S11 = S11, tmp<<6|tmp>>(64-6)
  190. tmp, S17 = S17, tmp<<10|tmp>>(64-10)
  191. tmp, S18 = S18, tmp<<15|tmp>>(64-15)
  192. tmp, S3 = S3, tmp<<21|tmp>>(64-21)
  193. tmp, S5 = S5, tmp<<28|tmp>>(64-28)
  194. tmp, S16 = S16, tmp<<36|tmp>>(64-36)
  195. tmp, S8 = S8, tmp<<45|tmp>>(64-45)
  196. tmp, S21 = S21, tmp<<55|tmp>>(64-55)
  197. tmp, S24 = S24, tmp<<2|tmp>>(64-2)
  198. tmp, S4 = S4, tmp<<14|tmp>>(64-14)
  199. tmp, S15 = S15, tmp<<27|tmp>>(64-27)
  200. tmp, S23 = S23, tmp<<41|tmp>>(64-41)
  201. tmp, S19 = S19, tmp<<56|tmp>>(64-56)
  202. tmp, S13 = S13, tmp<<8|tmp>>(64-8)
  203. tmp, S12 = S12, tmp<<25|tmp>>(64-25)
  204. tmp, S2 = S2, tmp<<43|tmp>>(64-43)
  205. tmp, S20 = S20, tmp<<62|tmp>>(64-62)
  206. tmp, S14 = S14, tmp<<18|tmp>>(64-18)
  207. tmp, S22 = S22, tmp<<39|tmp>>(64-39)
  208. tmp, S9 = S9, tmp<<61|tmp>>(64-61)
  209. tmp, S6 = S6, tmp<<20|tmp>>(64-20)
  210. S1 = tmp<<44 | tmp>>(64-44)
  211. // chi
  212. bc0 = S0
  213. bc1 = S1
  214. bc2 = S2
  215. bc3 = S3
  216. bc4 = S4
  217. S0 ^= (^bc1) & bc2
  218. S1 ^= (^bc2) & bc3
  219. S2 ^= (^bc3) & bc4
  220. S3 ^= (^bc4) & bc0
  221. S4 ^= (^bc0) & bc1
  222. bc0 = S5
  223. bc1 = S6
  224. bc2 = S7
  225. bc3 = S8
  226. bc4 = S9
  227. S5 ^= (^bc1) & bc2
  228. S6 ^= (^bc2) & bc3
  229. S7 ^= (^bc3) & bc4
  230. S8 ^= (^bc4) & bc0
  231. S9 ^= (^bc0) & bc1
  232. bc0 = S10
  233. bc1 = S11
  234. bc2 = S12
  235. bc3 = S13
  236. bc4 = S14
  237. S10 ^= (^bc1) & bc2
  238. S11 ^= (^bc2) & bc3
  239. S12 ^= (^bc3) & bc4
  240. S13 ^= (^bc4) & bc0
  241. S14 ^= (^bc0) & bc1
  242. bc0 = S15
  243. bc1 = S16
  244. bc2 = S17
  245. bc3 = S18
  246. bc4 = S19
  247. S15 ^= (^bc1) & bc2
  248. S16 ^= (^bc2) & bc3
  249. S17 ^= (^bc3) & bc4
  250. S18 ^= (^bc4) & bc0
  251. S19 ^= (^bc0) & bc1
  252. bc0 = S20
  253. bc1 = S21
  254. bc2 = S22
  255. bc3 = S23
  256. bc4 = S24
  257. S20 ^= (^bc1) & bc2
  258. S21 ^= (^bc2) & bc3
  259. S22 ^= (^bc3) & bc4
  260. S23 ^= (^bc4) & bc0
  261. S24 ^= (^bc0) & bc1
  262. // iota
  263. S0 ^= roundConstants[r]
  264. }
  265. S[0], S[1], S[2], S[3], S[4] = S0, S1, S2, S3, S4
  266. S[5], S[6], S[7], S[8], S[9] = S5, S6, S7, S8, S9
  267. S[10], S[11], S[12], S[13], S[14] = S10, S11, S12, S13, S14
  268. S[15], S[16], S[17], S[18], S[19] = S15, S16, S17, S18, S19
  269. S[20], S[21], S[22], S[23], S[24] = S20, S21, S22, S23, S24
  270. }
  271. func uint64le(v []byte) uint64 {
  272. return uint64(v[0]) |
  273. uint64(v[1])<<8 |
  274. uint64(v[2])<<16 |
  275. uint64(v[3])<<24 |
  276. uint64(v[4])<<32 |
  277. uint64(v[5])<<40 |
  278. uint64(v[6])<<48 |
  279. uint64(v[7])<<56
  280. }
  281. func putUint64le(v []byte, x uint64) {
  282. v[0] = byte(x)
  283. v[1] = byte(x >> 8)
  284. v[2] = byte(x >> 16)
  285. v[3] = byte(x >> 24)
  286. v[4] = byte(x >> 32)
  287. v[5] = byte(x >> 40)
  288. v[6] = byte(x >> 48)
  289. v[7] = byte(x >> 56)
  290. }