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.

432 lines
13 KiB

  1. // Copyright 2017-2018 DERO Project. All rights reserved.
  2. // Use of this source code in any form is governed by RESEARCH license.
  3. // license can be found in the LICENSE file.
  4. // GPG: 0F39 E425 8C65 3947 702A 8234 08B2 0360 A03A 9DE8
  5. //
  6. //
  7. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  8. // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  9. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  10. // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  11. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  12. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  13. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  14. // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  15. // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. package cryptonight
  17. type size_t uint32
  18. type __locale_data struct {
  19. }
  20. type __locale_t struct {
  21. __locales [][]__locale_data
  22. __ctype_b []uint16
  23. __ctype_tolower []int
  24. __ctype_toupper []int
  25. __names [][]byte
  26. }
  27. type locale_t struct {
  28. __locales [][]__locale_data
  29. __ctype_b []uint16
  30. __ctype_tolower []int
  31. __ctype_toupper []int
  32. __names [][]byte
  33. }
  34. type ptrdiff_t int32
  35. type wchar_t int
  36. type max_align_t struct {
  37. __clang_max_align_nonce1 int64
  38. __clang_max_align_nonce2 float64
  39. }
  40. var sbox []uint8 = []uint8{99, 124, 119, 123, 242, 107, 111, 197, 48, 1, 103, 43, 254, 215, 171, 118, 202, 130, 201, 125, 250, 89, 71, 240, 173, 212, 162, 175, 156, 164, 114, 192, 183, 253, 147, 38, 54, 63, 247, 204, 52, 165, 229, 241, 113, 216, 49, 21, 4, 199, 35, 195, 24, 150, 5, 154, 7, 18, 128, 226, 235, 39, 178, 117, 9, 131, 44, 26, 27, 110, 90, 160, 82, 59, 214, 179, 41, 227, 47, 132, 83, 209, 0, 237, 32, 252, 177, 91, 106, 203, 190, 57, 74, 76, 88, 207, 208, 239, 170, 251, 67, 77, 51, 133, 69, 249, 2, 127, 80, 60, 159, 168, 81, 163, 64, 143, 146, 157, 56, 245, 188, 182, 218, 33, 16, 255, 243, 210, 205, 12, 19, 236, 95, 151, 68, 23, 196, 167, 126, 61, 100, 93, 25, 115, 96, 129, 79, 220, 34, 42, 144, 136, 70, 238, 184, 20, 222, 94, 11, 219, 224, 50, 58, 10, 73, 6, 36, 92, 194, 211, 172, 98, 145, 149, 228, 121, 231, 200, 55, 109, 141, 213, 78, 169, 108, 86, 244, 234, 101, 122, 174, 8, 186, 120, 37, 46, 28, 166, 180, 198, 232, 221, 116, 31, 75, 189, 139, 138, 112, 62, 181, 102, 72, 3, 246, 14, 97, 53, 87, 185, 134, 193, 29, 158, 225, 248, 152, 17, 105, 217, 142, 148, 155, 30, 135, 233, 206, 85, 40, 223, 140, 161, 137, 13, 191, 230, 66, 104, 65, 153, 45, 15, 176, 84, 187, 22}
  41. var mul2 []uint8 = []uint8{0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182, 184, 186, 188, 190, 192, 194, 196, 198, 200, 202, 204, 206, 208, 210, 212, 214, 216, 218, 220, 222, 224, 226, 228, 230, 232, 234, 236, 238, 240, 242, 244, 246, 248, 250, 252, 254, 27, 25, 31, 29, 19, 17, 23, 21, 11, 9, 15, 13, 3, 1, 7, 5, 59, 57, 63, 61, 51, 49, 55, 53, 43, 41, 47, 45, 35, 33, 39, 37, 91, 89, 95, 93, 83, 81, 87, 85, 75, 73, 79, 77, 67, 65, 71, 69, 123, 121, 127, 125, 115, 113, 119, 117, 107, 105, 111, 109, 99, 97, 103, 101, 155, 153, 159, 157, 147, 145, 151, 149, 139, 137, 143, 141, 131, 129, 135, 133, 187, 185, 191, 189, 179, 177, 183, 181, 171, 169, 175, 173, 163, 161, 167, 165, 219, 217, 223, 221, 211, 209, 215, 213, 203, 201, 207, 205, 195, 193, 199, 197, 251, 249, 255, 253, 243, 241, 247, 245, 235, 233, 239, 237, 227, 225, 231, 229}
  42. func mix_bytes(i0 uint8, i1 uint8, i2 uint8, i3 uint8, i4 uint8, i5 uint8, i6 uint8, i7 uint8, output []uint8) {
  43. var t0 uint8
  44. var t1 uint8
  45. var t2 uint8
  46. var t3 uint8
  47. var t4 uint8
  48. var t5 uint8
  49. var t6 uint8
  50. var t7 uint8
  51. var x0 uint8
  52. var x1 uint8
  53. var x2 uint8
  54. var x3 uint8
  55. var x4 uint8
  56. var x5 uint8
  57. var x6 uint8
  58. var x7 uint8
  59. var y0 uint8
  60. var y1 uint8
  61. var y2 uint8
  62. var y3 uint8
  63. var y4 uint8
  64. var y5 uint8
  65. var y6 uint8
  66. var y7 uint8
  67. t0 = i0 ^ i1
  68. t1 = i1 ^ i2
  69. t2 = i2 ^ i3
  70. t3 = i3 ^ i4
  71. t4 = i4 ^ i5
  72. t5 = i5 ^ i6
  73. t6 = i6 ^ i7
  74. t7 = i7 ^ i0
  75. x0 = t0 ^ t3
  76. x1 = t1 ^ t4
  77. x2 = t2 ^ t5
  78. x3 = t3 ^ t6
  79. x4 = t4 ^ t7
  80. x5 = t5 ^ t0
  81. x6 = t6 ^ t1
  82. x7 = t7 ^ t2
  83. y0 = t0 ^ t2 ^ i6
  84. y1 = t1 ^ t3 ^ i7
  85. y2 = t2 ^ t4 ^ i0
  86. y3 = t3 ^ t5 ^ i1
  87. y4 = t4 ^ t6 ^ i2
  88. y5 = t5 ^ t7 ^ i3
  89. y6 = t6 ^ t0 ^ i4
  90. y7 = t7 ^ t1 ^ i5
  91. x3 = func() uint8 {
  92. if (x3 & 128) != 0 {
  93. return (x3 << uint64(1)) ^ 27
  94. } else {
  95. return (x3 << uint64(1))
  96. }
  97. }()
  98. x0 = func() uint8 {
  99. if (x0 & 128) != 0 {
  100. return (x0 << uint64(1)) ^ 27
  101. } else {
  102. return (x0 << uint64(1))
  103. }
  104. }()
  105. t0 = x3 ^ y7
  106. t0 = func() uint8 {
  107. if (t0 & 128) != 0 {
  108. return (t0 << uint64(1)) ^ 27
  109. } else {
  110. return (t0 << uint64(1))
  111. }
  112. }()
  113. t5 = x0 ^ y4
  114. t5 = func() uint8 {
  115. if (t5 & 128) != 0 {
  116. return (t5 << uint64(1)) ^ 27
  117. } else {
  118. return (t5 << uint64(1))
  119. }
  120. }()
  121. output[0] = t0 ^ y4
  122. output[5] = t5 ^ y1
  123. output[1] = mul2[mul2[x4]^y0] ^ y5
  124. output[2] = mul2[mul2[x5]^y1] ^ y6
  125. output[3] = mul2[mul2[x6]^y2] ^ y7
  126. output[4] = mul2[mul2[x7]^y3] ^ y0
  127. output[6] = mul2[mul2[x1]^y5] ^ y2
  128. output[7] = mul2[mul2[x2]^y6] ^ y3
  129. }
  130. func perm_P(input []uint8, output []uint8) {
  131. var r0 uint8
  132. var r1 uint8
  133. var r2 uint8
  134. var r3 uint8
  135. var r4 uint8
  136. var r5 uint8
  137. var r6 uint8
  138. var r7 uint8
  139. var round uint8
  140. var state []uint8 = make([]uint8, 64, 64)
  141. var write []uint8 = state
  142. var read []uint8 = input
  143. var p_tmp []uint8
  144. for {
  145. break
  146. }
  147. {
  148. for round = uint8(0); round < 10; func() uint8 {
  149. round += 1
  150. return round
  151. }() {
  152. r0 = sbox[read[0]^round]
  153. r1 = sbox[read[9]]
  154. r2 = sbox[read[18]]
  155. r3 = sbox[read[27]]
  156. r4 = sbox[read[36]]
  157. r5 = sbox[read[45]]
  158. r6 = sbox[read[54]]
  159. r7 = sbox[read[63]]
  160. mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write)
  161. r0 = sbox[read[8]^round^16]
  162. r1 = sbox[read[17]]
  163. r2 = sbox[read[26]]
  164. r3 = sbox[read[35]]
  165. r4 = sbox[read[44]]
  166. r5 = sbox[read[53]]
  167. r6 = sbox[read[62]]
  168. r7 = sbox[read[7]]
  169. mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write[8:])
  170. r0 = sbox[read[16]^round^32]
  171. r1 = sbox[read[25]]
  172. r2 = sbox[read[34]]
  173. r3 = sbox[read[43]]
  174. r4 = sbox[read[52]]
  175. r5 = sbox[read[61]]
  176. r6 = sbox[read[6]]
  177. r7 = sbox[read[15]]
  178. mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write[16:])
  179. r0 = sbox[read[24]^round^48]
  180. r1 = sbox[read[33]]
  181. r2 = sbox[read[42]]
  182. r3 = sbox[read[51]]
  183. r4 = sbox[read[60]]
  184. r5 = sbox[read[5]]
  185. r6 = sbox[read[14]]
  186. r7 = sbox[read[23]]
  187. mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write[24:])
  188. r0 = sbox[read[32]^round^64]
  189. r1 = sbox[read[41]]
  190. r2 = sbox[read[50]]
  191. r3 = sbox[read[59]]
  192. r4 = sbox[read[4]]
  193. r5 = sbox[read[13]]
  194. r6 = sbox[read[22]]
  195. r7 = sbox[read[31]]
  196. mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write[32:])
  197. r0 = sbox[read[40]^round^80]
  198. r1 = sbox[read[49]]
  199. r2 = sbox[read[58]]
  200. r3 = sbox[read[3]]
  201. r4 = sbox[read[12]]
  202. r5 = sbox[read[21]]
  203. r6 = sbox[read[30]]
  204. r7 = sbox[read[39]]
  205. mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write[40:])
  206. r0 = sbox[read[48]^round^96]
  207. r1 = sbox[read[57]]
  208. r2 = sbox[read[2]]
  209. r3 = sbox[read[11]]
  210. r4 = sbox[read[20]]
  211. r5 = sbox[read[29]]
  212. r6 = sbox[read[38]]
  213. r7 = sbox[read[47]]
  214. mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write[48:])
  215. r0 = sbox[read[56]^round^112]
  216. r1 = sbox[read[1]]
  217. r2 = sbox[read[10]]
  218. r3 = sbox[read[19]]
  219. r4 = sbox[read[28]]
  220. r5 = sbox[read[37]]
  221. r6 = sbox[read[46]]
  222. r7 = sbox[read[55]]
  223. mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write[56:])
  224. if round == uint8(0) {
  225. read = output
  226. }
  227. p_tmp = read
  228. read = write
  229. write = p_tmp
  230. }
  231. }
  232. }
  233. func perm_Q(input []uint8, output []uint8) {
  234. var r0 uint8
  235. var r1 uint8
  236. var r2 uint8
  237. var r3 uint8
  238. var r4 uint8
  239. var r5 uint8
  240. var r6 uint8
  241. var r7 uint8
  242. var round uint8
  243. var state []uint8 = make([]uint8, 64, 64)
  244. var write []uint8 = state
  245. var read []uint8 = input
  246. var p_tmp []uint8
  247. for {
  248. break
  249. }
  250. {
  251. for round = uint8(0); round < 10; func() uint8 {
  252. round += 1
  253. return round
  254. }() {
  255. r0 = sbox[read[8]^255]
  256. r1 = sbox[read[25]^255]
  257. r2 = sbox[read[42]^255]
  258. r3 = sbox[read[59]^255]
  259. r4 = sbox[read[4]^255]
  260. r5 = sbox[read[21]^255]
  261. r6 = sbox[read[38]^255]
  262. r7 = sbox[read[55]^159^round]
  263. mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write)
  264. r0 = sbox[read[16]^255]
  265. r1 = sbox[read[33]^255]
  266. r2 = sbox[read[50]^255]
  267. r3 = sbox[read[3]^255]
  268. r4 = sbox[read[12]^255]
  269. r5 = sbox[read[29]^255]
  270. r6 = sbox[read[46]^255]
  271. r7 = sbox[read[63]^143^round]
  272. mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write[8:])
  273. r0 = sbox[read[24]^255]
  274. r1 = sbox[read[41]^255]
  275. r2 = sbox[read[58]^255]
  276. r3 = sbox[read[11]^255]
  277. r4 = sbox[read[20]^255]
  278. r5 = sbox[read[37]^255]
  279. r6 = sbox[read[54]^255]
  280. r7 = sbox[read[7]^255^round]
  281. mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write[16:])
  282. r0 = sbox[read[32]^255]
  283. r1 = sbox[read[49]^255]
  284. r2 = sbox[read[2]^255]
  285. r3 = sbox[read[19]^255]
  286. r4 = sbox[read[28]^255]
  287. r5 = sbox[read[45]^255]
  288. r6 = sbox[read[62]^255]
  289. r7 = sbox[read[15]^239^round]
  290. mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write[24:])
  291. r0 = sbox[read[40]^255]
  292. r1 = sbox[read[57]^255]
  293. r2 = sbox[read[10]^255]
  294. r3 = sbox[read[27]^255]
  295. r4 = sbox[read[36]^255]
  296. r5 = sbox[read[53]^255]
  297. r6 = sbox[read[6]^255]
  298. r7 = sbox[read[23]^223^round]
  299. mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write[32:])
  300. r0 = sbox[read[48]^255]
  301. r1 = sbox[read[1]^255]
  302. r2 = sbox[read[18]^255]
  303. r3 = sbox[read[35]^255]
  304. r4 = sbox[read[44]^255]
  305. r5 = sbox[read[61]^255]
  306. r6 = sbox[read[14]^255]
  307. r7 = sbox[read[31]^207^round]
  308. mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write[40:])
  309. r0 = sbox[read[56]^255]
  310. r1 = sbox[read[9]^255]
  311. r2 = sbox[read[26]^255]
  312. r3 = sbox[read[43]^255]
  313. r4 = sbox[read[52]^255]
  314. r5 = sbox[read[5]^255]
  315. r6 = sbox[read[22]^255]
  316. r7 = sbox[read[39]^191^round]
  317. mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write[48:])
  318. r0 = sbox[read[0]^255]
  319. r1 = sbox[read[17]^255]
  320. r2 = sbox[read[34]^255]
  321. r3 = sbox[read[51]^255]
  322. r4 = sbox[read[60]^255]
  323. r5 = sbox[read[13]^255]
  324. r6 = sbox[read[30]^255]
  325. r7 = sbox[read[47]^175^round]
  326. mix_bytes(r0, r1, r2, r3, r4, r5, r6, r7, write[56:])
  327. if round == uint8(0) {
  328. read = output
  329. }
  330. p_tmp = read
  331. read = write
  332. write = p_tmp
  333. }
  334. }
  335. }
  336. func crypto_hash(out []uint8, in []uint8, inlen uint64) int {
  337. if inlen >= (1 << uint64(16)) {
  338. return -1
  339. }
  340. var msg_len uint32 = uint32(uint32(inlen))
  341. var padded_len uint32 = ((msg_len+9-1)/64)*64 + 64
  342. var pad_block_len uint8 = uint8(padded_len - msg_len)
  343. var pad_block []uint8 = make([]uint8, pad_block_len, pad_block_len)
  344. pad_block[0] = uint8(128)
  345. var blocks uint32 = uint32((padded_len >> uint64(6)))
  346. pad_block[pad_block_len-1] = (uint8(blocks) & 255)
  347. var h_state []uint8 = make([]uint8, 64, 64)
  348. var p_state []uint8 = make([]uint8, 64, 64)
  349. var q_state []uint8 = make([]uint8, 64, 64)
  350. var x_state []uint8 = make([]uint8, 64, 64)
  351. var buf []uint8 = make([]uint8, 64, 64)
  352. h_state[62] = uint8(1)
  353. var i uint8
  354. var block uint32
  355. var remaining uint32 = uint32(0)
  356. var message_left_len uint32 = msg_len
  357. for {
  358. break
  359. }
  360. {
  361. for block = uint32(0); block < blocks; func() uint32 {
  362. block += 1
  363. return block
  364. }() {
  365. if block*64+64 < msg_len {
  366. //memcpy(buf, in+64*block, uint32(64))
  367. copy(buf, in[64*block:64*block+64]) //copy full block
  368. message_left_len -= 64
  369. } else {
  370. if message_left_len > 0 {
  371. remaining = uint32(64 - message_left_len)
  372. //memcpy(buf, in+64*block, message_left_len)
  373. copy(buf, in[64*block:64*block+message_left_len])
  374. //memcpy(buf+message_left_len, pad_block, uint32(remaining))
  375. copy(buf[message_left_len:], pad_block[:remaining])
  376. message_left_len = uint32(0)
  377. } else {
  378. //memcpy(buf, pad_block+remaining, uint32(64))
  379. copy(buf, pad_block[remaining:remaining+64])
  380. }
  381. }
  382. for {
  383. break
  384. }
  385. {
  386. for i = uint8(0); i < 64; func() uint8 {
  387. i += 1
  388. return i
  389. }() {
  390. x_state[i] = buf[i] ^ h_state[i]
  391. }
  392. }
  393. perm_P(x_state, p_state)
  394. perm_Q(buf, q_state)
  395. for {
  396. break
  397. }
  398. {
  399. for i = uint8(0); i < 64; func() uint8 {
  400. i += 1
  401. return i
  402. }() {
  403. h_state[i] ^= p_state[i] ^ q_state[i]
  404. }
  405. }
  406. }
  407. }
  408. perm_P(h_state, p_state)
  409. for {
  410. break
  411. }
  412. {
  413. for i = uint8(32); i < 64; func() uint8 {
  414. i += 1
  415. return i
  416. }() {
  417. out[i-32] = h_state[i] ^ p_state[i]
  418. }
  419. }
  420. return 0
  421. }
  422. func __init() {
  423. }