sha256_cryptopp.c 16 KB

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