sha256_cryptopp.c 16 KB

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