sha256_cryptopp.c 16 KB

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