sha256_cryptopp.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /*
  2. * Copyright 2010-2011 Jeff Garzik
  3. * Copyright 2002-2010 Wei Dai (released as public domain)
  4. * Copyright 2012-2013 Luke Dashjr
  5. * Copyright 2011 Con Kolivas
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 3 of the License, or (at your option)
  10. * any later version. See COPYING for more details.
  11. */
  12. #include "config.h"
  13. #include <stdint.h>
  14. #include <stdbool.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include "driver-cpu.h"
  19. #include "miner.h"
  20. typedef uint32_t word32;
  21. static word32 rotrFixed(word32 word, unsigned int shift)
  22. {
  23. return (word >> shift) | (word << (32 - shift));
  24. }
  25. #define blk0(i) (W[i] = data[i])
  26. static const word32 SHA256_K[64] = {
  27. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  28. 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  29. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  30. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  31. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  32. 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  33. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  34. 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  35. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  36. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  37. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  38. 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  39. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  40. 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  41. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  42. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  43. };
  44. #define blk2(i) (W[i&15]+=s1(W[(i-2)&15])+W[(i-7)&15]+s0(W[(i-15)&15]))
  45. #define Ch(x,y,z) (z^(x&(y^z)))
  46. #define Maj(x,y,z) (y^((x^y)&(y^z)))
  47. #define a(i) T[(0-i)&7]
  48. #define b(i) T[(1-i)&7]
  49. #define c(i) T[(2-i)&7]
  50. #define d(i) T[(3-i)&7]
  51. #define e(i) T[(4-i)&7]
  52. #define f(i) T[(5-i)&7]
  53. #define g(i) T[(6-i)&7]
  54. #define h(i) T[(7-i)&7]
  55. #define R(i) h(i)+=S1(e(i))+Ch(e(i),f(i),g(i))+SHA256_K[i+j]+(j?blk2(i):blk0(i));\
  56. d(i)+=h(i);h(i)+=S0(a(i))+Maj(a(i),b(i),c(i))
  57. // for SHA256
  58. #define S0(x) (rotrFixed(x,2)^rotrFixed(x,13)^rotrFixed(x,22))
  59. #define S1(x) (rotrFixed(x,6)^rotrFixed(x,11)^rotrFixed(x,25))
  60. #define s0(x) (rotrFixed(x,7)^rotrFixed(x,18)^(x>>3))
  61. #define s1(x) (rotrFixed(x,17)^rotrFixed(x,19)^(x>>10))
  62. static void SHA256_Transform(word32 *state, const word32 *data)
  63. {
  64. word32 W[16] = { };
  65. word32 T[8];
  66. unsigned int j;
  67. /* Copy context->state[] to working vars */
  68. memcpy(T, state, sizeof(T));
  69. /* 64 operations, partially loop unrolled */
  70. for (j=0; j<64; j+=16)
  71. {
  72. R( 0); R( 1); R( 2); R( 3);
  73. R( 4); R( 5); R( 6); R( 7);
  74. R( 8); R( 9); R(10); R(11);
  75. R(12); R(13); R(14); R(15);
  76. }
  77. /* Add the working vars back into context.state[] */
  78. state[0] += a(0);
  79. state[1] += b(0);
  80. state[2] += c(0);
  81. state[3] += d(0);
  82. state[4] += e(0);
  83. state[5] += f(0);
  84. state[6] += g(0);
  85. state[7] += h(0);
  86. }
  87. static void runhash(void *state, const void *input, const void *init)
  88. {
  89. memcpy(state, init, 32);
  90. SHA256_Transform(state, input);
  91. }
  92. /* suspiciously similar to ScanHash* from bitcoin */
  93. bool scanhash_cryptopp(struct thr_info * const thr, struct work * const work,
  94. uint32_t max_nonce, uint32_t *last_nonce,
  95. uint32_t n)
  96. {
  97. const uint8_t *midstate = work->midstate;
  98. uint8_t *data = work->data;
  99. uint8_t hash1[0x40];
  100. memcpy(hash1, hash1_init, sizeof(hash1));
  101. uint8_t * const hash = work->hash;
  102. uint32_t *hash32 = (uint32_t *) hash;
  103. uint32_t *nonce = (uint32_t *)(data + 76);
  104. data += 64;
  105. // Midstate and data are stored in little endian
  106. LOCAL_swap32le(unsigned char, midstate, 32/4)
  107. LOCAL_swap32le(unsigned char, data, 64/4)
  108. uint32_t *nonce_w = (uint32_t *)(data + 12);
  109. while (1) {
  110. *nonce_w = n;
  111. runhash(hash1, data, midstate);
  112. runhash(hash, hash1, sha256_init_state);
  113. if (unlikely((hash32[7] == 0)))
  114. {
  115. *nonce = htole32(n);
  116. *last_nonce = n;
  117. return true;
  118. }
  119. if ((n >= max_nonce) || thr->work_restart) {
  120. *nonce = htole32(n);
  121. *last_nonce = n;
  122. return false;
  123. }
  124. n++;
  125. }
  126. }
  127. #if defined(WANT_CRYPTOPP_ASM32)
  128. #define CRYPTOPP_FASTCALL
  129. #define CRYPTOPP_BOOL_X86 1
  130. #define CRYPTOPP_BOOL_X64 0
  131. #define CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE 0
  132. #ifdef CRYPTOPP_GENERATE_X64_MASM
  133. #define AS1(x) x*newline*
  134. #define AS2(x, y) x, y*newline*
  135. #define AS3(x, y, z) x, y, z*newline*
  136. #define ASS(x, y, a, b, c, d) x, y, a*64+b*16+c*4+d*newline*
  137. #define ASL(x) label##x:*newline*
  138. #define ASJ(x, y, z) x label##y*newline*
  139. #define ASC(x, y) x label##y*newline*
  140. #define AS_HEX(y) 0##y##h
  141. #elif defined(_MSC_VER) || defined(__BORLANDC__)
  142. #define CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY
  143. #define AS1(x) __asm {x}
  144. #define AS2(x, y) __asm {x, y}
  145. #define AS3(x, y, z) __asm {x, y, z}
  146. #define ASS(x, y, a, b, c, d) __asm {x, y, (a)*64+(b)*16+(c)*4+(d)}
  147. #define ASL(x) __asm {label##x:}
  148. #define ASJ(x, y, z) __asm {x label##y}
  149. #define ASC(x, y) __asm {x label##y}
  150. #define CRYPTOPP_NAKED __declspec(naked)
  151. #define AS_HEX(y) 0x##y
  152. #else
  153. #define CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY
  154. // define these in two steps to allow arguments to be expanded
  155. #define GNU_AS1(x) #x ";"
  156. #define GNU_AS2(x, y) #x ", " #y ";"
  157. #define GNU_AS3(x, y, z) #x ", " #y ", " #z ";"
  158. #define GNU_ASL(x) "\n" #x ":"
  159. #define GNU_ASJ(x, y, z) #x " " #y #z ";"
  160. #define AS1(x) GNU_AS1(x)
  161. #define AS2(x, y) GNU_AS2(x, y)
  162. #define AS3(x, y, z) GNU_AS3(x, y, z)
  163. #define ASS(x, y, a, b, c, d) #x ", " #y ", " #a "*64+" #b "*16+" #c "*4+" #d ";"
  164. #define ASL(x) GNU_ASL(x)
  165. #define ASJ(x, y, z) GNU_ASJ(x, y, z)
  166. #define ASC(x, y) #x " " #y ";"
  167. #define CRYPTOPP_NAKED
  168. #define AS_HEX(y) 0x##y
  169. #endif
  170. #define IF0(y)
  171. #define IF1(y) y
  172. #ifdef CRYPTOPP_GENERATE_X64_MASM
  173. #define ASM_MOD(x, y) ((x) MOD (y))
  174. #define XMMWORD_PTR XMMWORD PTR
  175. #else
  176. // GNU assembler doesn't seem to have mod operator
  177. #define ASM_MOD(x, y) ((x)-((x)/(y))*(y))
  178. // GAS 2.15 doesn't support XMMWORD PTR. it seems necessary only for MASM
  179. #define XMMWORD_PTR
  180. #endif
  181. #if CRYPTOPP_BOOL_X86
  182. #define AS_REG_1 ecx
  183. #define AS_REG_2 edx
  184. #define AS_REG_3 esi
  185. #define AS_REG_4 edi
  186. #define AS_REG_5 eax
  187. #define AS_REG_6 ebx
  188. #define AS_REG_7 ebp
  189. #define AS_REG_1d ecx
  190. #define AS_REG_2d edx
  191. #define AS_REG_3d esi
  192. #define AS_REG_4d edi
  193. #define AS_REG_5d eax
  194. #define AS_REG_6d ebx
  195. #define AS_REG_7d ebp
  196. #define WORD_SZ 4
  197. #define WORD_REG(x) e##x
  198. #define WORD_PTR DWORD PTR
  199. #define AS_PUSH_IF86(x) AS1(push e##x)
  200. #define AS_POP_IF86(x) AS1(pop e##x)
  201. #define AS_JCXZ jecxz
  202. #elif CRYPTOPP_BOOL_X64
  203. #ifdef CRYPTOPP_GENERATE_X64_MASM
  204. #define AS_REG_1 rcx
  205. #define AS_REG_2 rdx
  206. #define AS_REG_3 r8
  207. #define AS_REG_4 r9
  208. #define AS_REG_5 rax
  209. #define AS_REG_6 r10
  210. #define AS_REG_7 r11
  211. #define AS_REG_1d ecx
  212. #define AS_REG_2d edx
  213. #define AS_REG_3d r8d
  214. #define AS_REG_4d r9d
  215. #define AS_REG_5d eax
  216. #define AS_REG_6d r10d
  217. #define AS_REG_7d r11d
  218. #else
  219. #define AS_REG_1 rdi
  220. #define AS_REG_2 rsi
  221. #define AS_REG_3 rdx
  222. #define AS_REG_4 rcx
  223. #define AS_REG_5 r8
  224. #define AS_REG_6 r9
  225. #define AS_REG_7 r10
  226. #define AS_REG_1d edi
  227. #define AS_REG_2d esi
  228. #define AS_REG_3d edx
  229. #define AS_REG_4d ecx
  230. #define AS_REG_5d r8d
  231. #define AS_REG_6d r9d
  232. #define AS_REG_7d r10d
  233. #endif
  234. #define WORD_SZ 8
  235. #define WORD_REG(x) r##x
  236. #define WORD_PTR QWORD PTR
  237. #define AS_PUSH_IF86(x)
  238. #define AS_POP_IF86(x)
  239. #define AS_JCXZ jrcxz
  240. #endif
  241. static void CRYPTOPP_FASTCALL X86_SHA256_HashBlocks(word32 *state, const word32 *data, size_t len
  242. #if defined(_MSC_VER) && (_MSC_VER == 1200)
  243. , ... // VC60 workaround: prevent VC 6 from inlining this function
  244. #endif
  245. )
  246. {
  247. #if defined(_MSC_VER) && (_MSC_VER == 1200)
  248. AS2(mov ecx, [state])
  249. AS2(mov edx, [data])
  250. #endif
  251. #define LOCALS_SIZE 8*4 + 16*4 + 4*WORD_SZ
  252. #define H(i) [BASE+ASM_MOD(1024+7-(i),8)*4]
  253. #define G(i) H(i+1)
  254. #define F(i) H(i+2)
  255. #define E(i) H(i+3)
  256. #define D(i) H(i+4)
  257. #define C(i) H(i+5)
  258. #define B(i) H(i+6)
  259. #define A(i) H(i+7)
  260. #define Wt(i) BASE+8*4+ASM_MOD(1024+15-(i),16)*4
  261. #define Wt_2(i) Wt((i)-2)
  262. #define Wt_15(i) Wt((i)-15)
  263. #define Wt_7(i) Wt((i)-7)
  264. #define K_END [BASE+8*4+16*4+0*WORD_SZ]
  265. #define STATE_SAVE [BASE+8*4+16*4+1*WORD_SZ]
  266. #define DATA_SAVE [BASE+8*4+16*4+2*WORD_SZ]
  267. #define DATA_END [BASE+8*4+16*4+3*WORD_SZ]
  268. #define Kt(i) WORD_REG(si)+(i)*4
  269. #if CRYPTOPP_BOOL_X86
  270. #define BASE esp+4
  271. #elif defined(__GNUC__)
  272. #define BASE r8
  273. #else
  274. #define BASE rsp
  275. #endif
  276. #define RA0(i, edx, edi) \
  277. AS2( add edx, [Kt(i)] )\
  278. AS2( add edx, [Wt(i)] )\
  279. AS2( add edx, H(i) )\
  280. #define RA1(i, edx, edi)
  281. #define RB0(i, edx, edi)
  282. #define RB1(i, edx, edi) \
  283. AS2( mov AS_REG_7d, [Wt_2(i)] )\
  284. AS2( mov edi, [Wt_15(i)])\
  285. AS2( mov ebx, AS_REG_7d )\
  286. AS2( shr AS_REG_7d, 10 )\
  287. AS2( ror ebx, 17 )\
  288. AS2( xor AS_REG_7d, ebx )\
  289. AS2( ror ebx, 2 )\
  290. AS2( xor ebx, AS_REG_7d )/* s1(W_t-2) */\
  291. AS2( add ebx, [Wt_7(i)])\
  292. AS2( mov AS_REG_7d, edi )\
  293. AS2( shr AS_REG_7d, 3 )\
  294. AS2( ror edi, 7 )\
  295. AS2( add ebx, [Wt(i)])/* s1(W_t-2) + W_t-7 + W_t-16 */\
  296. AS2( xor AS_REG_7d, edi )\
  297. AS2( add edx, [Kt(i)])\
  298. AS2( ror edi, 11 )\
  299. AS2( add edx, H(i) )\
  300. AS2( xor AS_REG_7d, edi )/* s0(W_t-15) */\
  301. AS2( add AS_REG_7d, ebx )/* W_t = s1(W_t-2) + W_t-7 + s0(W_t-15) W_t-16*/\
  302. AS2( mov [Wt(i)], AS_REG_7d)\
  303. AS2( add edx, AS_REG_7d )\
  304. #define ROUND(i, r, eax, ecx, edi, edx)\
  305. /* in: edi = E */\
  306. /* unused: eax, ecx, temp: ebx, AS_REG_7d, out: edx = T1 */\
  307. AS2( mov edx, F(i) )\
  308. AS2( xor edx, G(i) )\
  309. AS2( and edx, edi )\
  310. AS2( xor edx, G(i) )/* Ch(E,F,G) = (G^(E&(F^G))) */\
  311. AS2( mov AS_REG_7d, edi )\
  312. AS2( ror edi, 6 )\
  313. AS2( ror AS_REG_7d, 25 )\
  314. RA##r(i, edx, edi )/* H + Wt + Kt + Ch(E,F,G) */\
  315. AS2( xor AS_REG_7d, edi )\
  316. AS2( ror edi, 5 )\
  317. AS2( xor AS_REG_7d, edi )/* S1(E) */\
  318. AS2( add edx, AS_REG_7d )/* T1 = S1(E) + Ch(E,F,G) + H + Wt + Kt */\
  319. RB##r(i, edx, edi )/* H + Wt + Kt + Ch(E,F,G) */\
  320. /* in: ecx = A, eax = B^C, edx = T1 */\
  321. /* unused: edx, temp: ebx, AS_REG_7d, out: eax = A, ecx = B^C, edx = E */\
  322. AS2( mov ebx, ecx )\
  323. AS2( xor ecx, B(i) )/* A^B */\
  324. AS2( and eax, ecx )\
  325. AS2( xor eax, B(i) )/* Maj(A,B,C) = B^((A^B)&(B^C) */\
  326. AS2( mov AS_REG_7d, ebx )\
  327. AS2( ror ebx, 2 )\
  328. AS2( add eax, edx )/* T1 + Maj(A,B,C) */\
  329. AS2( add edx, D(i) )\
  330. AS2( mov D(i), edx )\
  331. AS2( ror AS_REG_7d, 22 )\
  332. AS2( xor AS_REG_7d, ebx )\
  333. AS2( ror ebx, 11 )\
  334. AS2( xor AS_REG_7d, ebx )\
  335. AS2( add eax, AS_REG_7d )/* T1 + S0(A) + Maj(A,B,C) */\
  336. AS2( mov H(i), eax )\
  337. #define SWAP_COPY(i) \
  338. AS2( mov WORD_REG(bx), [WORD_REG(dx)+i*WORD_SZ])\
  339. AS1( bswap WORD_REG(bx))\
  340. AS2( mov [Wt(i*(1+CRYPTOPP_BOOL_X64)+CRYPTOPP_BOOL_X64)], WORD_REG(bx))
  341. #if defined(__GNUC__)
  342. #if CRYPTOPP_BOOL_X64
  343. FixedSizeAlignedSecBlock<byte, LOCALS_SIZE> workspace;
  344. #endif
  345. __asm__ __volatile__
  346. (
  347. #if CRYPTOPP_BOOL_X64
  348. "lea %4, %%r8;"
  349. #endif
  350. ".intel_syntax noprefix;"
  351. #elif defined(CRYPTOPP_GENERATE_X64_MASM)
  352. ALIGN 8
  353. X86_SHA256_HashBlocks PROC FRAME
  354. rex_push_reg rsi
  355. push_reg rdi
  356. push_reg rbx
  357. push_reg rbp
  358. alloc_stack(LOCALS_SIZE+8)
  359. .endprolog
  360. mov rdi, r8
  361. lea rsi, [?SHA256_K@CryptoPP@@3QBIB + 48*4]
  362. #endif
  363. #if CRYPTOPP_BOOL_X86
  364. #ifndef __GNUC__
  365. AS2( mov edi, [len])
  366. AS2( lea WORD_REG(si), [SHA256_K+48*4])
  367. #endif
  368. #if !defined(_MSC_VER) || (_MSC_VER < 1400)
  369. AS_PUSH_IF86(bx)
  370. #endif
  371. AS_PUSH_IF86(bp)
  372. AS2( mov ebx, esp)
  373. AS2( and esp, -16)
  374. AS2( sub WORD_REG(sp), LOCALS_SIZE)
  375. AS_PUSH_IF86(bx)
  376. #endif
  377. AS2( mov STATE_SAVE, WORD_REG(cx))
  378. AS2( mov DATA_SAVE, WORD_REG(dx))
  379. AS2( lea WORD_REG(ax), [WORD_REG(di) + WORD_REG(dx)])
  380. AS2( mov DATA_END, WORD_REG(ax))
  381. AS2( mov K_END, WORD_REG(si))
  382. #if CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE
  383. #if CRYPTOPP_BOOL_X86
  384. AS2( test edi, 1)
  385. ASJ( jnz, 2, f)
  386. AS1( dec DWORD PTR K_END)
  387. #endif
  388. AS2( movdqa xmm0, XMMWORD_PTR [WORD_REG(cx)+0*16])
  389. AS2( movdqa xmm1, XMMWORD_PTR [WORD_REG(cx)+1*16])
  390. #endif
  391. #if CRYPTOPP_BOOL_X86
  392. #if CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE
  393. ASJ( jmp, 0, f)
  394. #endif
  395. ASL(2) // non-SSE2
  396. AS2( mov esi, ecx)
  397. AS2( lea edi, A(0))
  398. AS2( mov ecx, 8)
  399. AS1( rep movsd)
  400. AS2( mov esi, K_END)
  401. ASJ( jmp, 3, f)
  402. #endif
  403. #if CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE
  404. ASL(0)
  405. AS2( movdqa E(0), xmm1)
  406. AS2( movdqa A(0), xmm0)
  407. #endif
  408. #if CRYPTOPP_BOOL_X86
  409. ASL(3)
  410. #endif
  411. AS2( sub WORD_REG(si), 48*4)
  412. SWAP_COPY(0) SWAP_COPY(1) SWAP_COPY(2) SWAP_COPY(3)
  413. SWAP_COPY(4) SWAP_COPY(5) SWAP_COPY(6) SWAP_COPY(7)
  414. #if CRYPTOPP_BOOL_X86
  415. SWAP_COPY(8) SWAP_COPY(9) SWAP_COPY(10) SWAP_COPY(11)
  416. SWAP_COPY(12) SWAP_COPY(13) SWAP_COPY(14) SWAP_COPY(15)
  417. #endif
  418. AS2( mov edi, E(0)) // E
  419. AS2( mov eax, B(0)) // B
  420. AS2( xor eax, C(0)) // B^C
  421. AS2( mov ecx, A(0)) // A
  422. ROUND(0, 0, eax, ecx, edi, edx)
  423. ROUND(1, 0, ecx, eax, edx, edi)
  424. ROUND(2, 0, eax, ecx, edi, edx)
  425. ROUND(3, 0, ecx, eax, edx, edi)
  426. ROUND(4, 0, eax, ecx, edi, edx)
  427. ROUND(5, 0, ecx, eax, edx, edi)
  428. ROUND(6, 0, eax, ecx, edi, edx)
  429. ROUND(7, 0, ecx, eax, edx, edi)
  430. ROUND(8, 0, eax, ecx, edi, edx)
  431. ROUND(9, 0, ecx, eax, edx, edi)
  432. ROUND(10, 0, eax, ecx, edi, edx)
  433. ROUND(11, 0, ecx, eax, edx, edi)
  434. ROUND(12, 0, eax, ecx, edi, edx)
  435. ROUND(13, 0, ecx, eax, edx, edi)
  436. ROUND(14, 0, eax, ecx, edi, edx)
  437. ROUND(15, 0, ecx, eax, edx, edi)
  438. ASL(1)
  439. AS2(add WORD_REG(si), 4*16)
  440. ROUND(0, 1, eax, ecx, edi, edx)
  441. ROUND(1, 1, ecx, eax, edx, edi)
  442. ROUND(2, 1, eax, ecx, edi, edx)
  443. ROUND(3, 1, ecx, eax, edx, edi)
  444. ROUND(4, 1, eax, ecx, edi, edx)
  445. ROUND(5, 1, ecx, eax, edx, edi)
  446. ROUND(6, 1, eax, ecx, edi, edx)
  447. ROUND(7, 1, ecx, eax, edx, edi)
  448. ROUND(8, 1, eax, ecx, edi, edx)
  449. ROUND(9, 1, ecx, eax, edx, edi)
  450. ROUND(10, 1, eax, ecx, edi, edx)
  451. ROUND(11, 1, ecx, eax, edx, edi)
  452. ROUND(12, 1, eax, ecx, edi, edx)
  453. ROUND(13, 1, ecx, eax, edx, edi)
  454. ROUND(14, 1, eax, ecx, edi, edx)
  455. ROUND(15, 1, ecx, eax, edx, edi)
  456. AS2( cmp WORD_REG(si), K_END)
  457. ASJ( jb, 1, b)
  458. AS2( mov WORD_REG(dx), DATA_SAVE)
  459. AS2( add WORD_REG(dx), 64)
  460. AS2( mov AS_REG_7, STATE_SAVE)
  461. AS2( mov DATA_SAVE, WORD_REG(dx))
  462. #if CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE
  463. #if CRYPTOPP_BOOL_X86
  464. AS2( test DWORD PTR K_END, 1)
  465. ASJ( jz, 4, f)
  466. #endif
  467. AS2( movdqa xmm1, XMMWORD_PTR [AS_REG_7+1*16])
  468. AS2( movdqa xmm0, XMMWORD_PTR [AS_REG_7+0*16])
  469. AS2( paddd xmm1, E(0))
  470. AS2( paddd xmm0, A(0))
  471. AS2( movdqa [AS_REG_7+1*16], xmm1)
  472. AS2( movdqa [AS_REG_7+0*16], xmm0)
  473. AS2( cmp WORD_REG(dx), DATA_END)
  474. ASJ( jb, 0, b)
  475. #endif
  476. #if CRYPTOPP_BOOL_X86
  477. #if CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE
  478. ASJ( jmp, 5, f)
  479. ASL(4) // non-SSE2
  480. #endif
  481. AS2( add [AS_REG_7+0*4], ecx) // A
  482. AS2( add [AS_REG_7+4*4], edi) // E
  483. AS2( mov eax, B(0))
  484. AS2( mov ebx, C(0))
  485. AS2( mov ecx, D(0))
  486. AS2( add [AS_REG_7+1*4], eax)
  487. AS2( add [AS_REG_7+2*4], ebx)
  488. AS2( add [AS_REG_7+3*4], ecx)
  489. AS2( mov eax, F(0))
  490. AS2( mov ebx, G(0))
  491. AS2( mov ecx, H(0))
  492. AS2( add [AS_REG_7+5*4], eax)
  493. AS2( add [AS_REG_7+6*4], ebx)
  494. AS2( add [AS_REG_7+7*4], ecx)
  495. AS2( mov ecx, AS_REG_7d)
  496. AS2( cmp WORD_REG(dx), DATA_END)
  497. ASJ( jb, 2, b)
  498. #if CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE
  499. ASL(5)
  500. #endif
  501. #endif
  502. AS_POP_IF86(sp)
  503. AS_POP_IF86(bp)
  504. #if !defined(_MSC_VER) || (_MSC_VER < 1400)
  505. AS_POP_IF86(bx)
  506. #endif
  507. #ifdef CRYPTOPP_GENERATE_X64_MASM
  508. add rsp, LOCALS_SIZE+8
  509. pop rbp
  510. pop rbx
  511. pop rdi
  512. pop rsi
  513. ret
  514. X86_SHA256_HashBlocks ENDP
  515. #endif
  516. #ifdef __GNUC__
  517. ".att_syntax prefix;"
  518. :
  519. : "c" (state), "d" (data), "S" (SHA256_K+48), "D" (len)
  520. #if CRYPTOPP_BOOL_X64
  521. , "m" (workspace[0])
  522. #endif
  523. : "memory", "cc", "%eax"
  524. #if CRYPTOPP_BOOL_X64
  525. , "%rbx", "%r8", "%r10"
  526. #endif
  527. );
  528. #endif
  529. }
  530. static void SHA256_Transform32(word32 *state, const word32 *data)
  531. {
  532. word32 W[16];
  533. swap32yes(W, data, 16);
  534. X86_SHA256_HashBlocks(state, W, 16 * 4);
  535. }
  536. static void runhash32(void *state, const void *input, const void *init)
  537. {
  538. memcpy(state, init, 32);
  539. SHA256_Transform32(state, input);
  540. }
  541. /* suspiciously similar to ScanHash* from bitcoin */
  542. bool scanhash_asm32(struct thr_info * const thr, struct work * const work,
  543. uint32_t max_nonce, uint32_t *last_nonce,
  544. uint32_t n)
  545. {
  546. const uint8_t * const midstate = work->midstate;
  547. uint8_t *data = work->data;
  548. uint8_t hash1[0x40];
  549. memcpy(hash1, hash1_init, sizeof(hash1));
  550. uint8_t * const hash = work->hash;
  551. uint32_t *hash32 = (uint32_t *) hash;
  552. uint32_t *nonce = (uint32_t *)(data + 76);
  553. data += 64;
  554. while (1) {
  555. *nonce = n;
  556. runhash32(hash1, data, midstate);
  557. runhash32(hash, hash1, sha256_init_state);
  558. if (unlikely(hash32[7] == 0))
  559. {
  560. *last_nonce = n;
  561. return true;
  562. }
  563. if ((n >= max_nonce) || thr->work_restart) {
  564. *last_nonce = n;
  565. return false;
  566. }
  567. ++n;
  568. }
  569. }
  570. #endif // #if defined(WANT_CRYPTOPP_ASM32)