1 /*
2 * Copyright 2011 ArtForz
3 * Copyright 2011-2013 pooler
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version. See COPYING for more details.
9 */
10
11 #include "cpuminer-config.h"
12 #include "miner.h"
13
14 #include <string.h>
15 #include <inttypes.h>
16
17 #if defined(USE_ASM) && \
18 (defined(__x86_64__) || \
19 (defined(__arm__) && defined(__APCS_32__)) || \
20 (defined(__powerpc__) || defined(__ppc__) || defined(__PPC__)))
21 #define EXTERN_SHA256
22 #endif
23
24 static const uint32_t sha256_h[8] = {
25 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
26 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
27 };
28
29 static const uint32_t sha256_k[64] = {
30 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
31 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
32 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
33 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
34 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
35 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
36 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
37 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
38 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
39 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
40 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
41 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
42 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
43 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
44 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
45 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
46 };
47
sha256_init(uint32_t * state)48 void sha256_init(uint32_t *state)
49 {
50 memcpy(state, sha256_h, 32);
51 }
52
53 /* Elementary functions used by SHA256 */
54 #define Ch(x, y, z) ((x & (y ^ z)) ^ z)
55 #define Maj(x, y, z) ((x & (y | z)) | (y & z))
56 #define ROTR(x, n) ((x >> n) | (x << (32 - n)))
57 #define S0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
58 #define S1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
59 #define s0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ (x >> 3))
60 #define s1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ (x >> 10))
61
62 /* SHA256 round function */
63 #define RND(a, b, c, d, e, f, g, h, k) \
64 do { \
65 t0 = h + S1(e) + Ch(e, f, g) + k; \
66 t1 = S0(a) + Maj(a, b, c); \
67 d += t0; \
68 h = t0 + t1; \
69 } while (0)
70
71 /* Adjusted round function for rotating state */
72 #define RNDr(S, W, i) \
73 RND(S[(64 - i) % 8], S[(65 - i) % 8], \
74 S[(66 - i) % 8], S[(67 - i) % 8], \
75 S[(68 - i) % 8], S[(69 - i) % 8], \
76 S[(70 - i) % 8], S[(71 - i) % 8], \
77 W[i] + sha256_k[i])
78
79 #ifndef EXTERN_SHA256
80
81 /*
82 * SHA256 block compression function. The 256-bit state is transformed via
83 * the 512-bit input block to produce a new state.
84 */
sha256_transform(uint32_t * state,const uint32_t * block,int swap)85 void sha256_transform(uint32_t *state, const uint32_t *block, int swap)
86 {
87 uint32_t W[64];
88 uint32_t S[8];
89 uint32_t t0, t1;
90 int i;
91
92 /* 1. Prepare message schedule W. */
93 if (swap) {
94 for (i = 0; i < 16; i++)
95 W[i] = swab32(block[i]);
96 } else
97 memcpy(W, block, 64);
98 for (i = 16; i < 64; i += 2) {
99 W[i] = s1(W[i - 2]) + W[i - 7] + s0(W[i - 15]) + W[i - 16];
100 W[i+1] = s1(W[i - 1]) + W[i - 6] + s0(W[i - 14]) + W[i - 15];
101 }
102
103 /* 2. Initialize working variables. */
104 memcpy(S, state, 32);
105
106 /* 3. Mix. */
107 RNDr(S, W, 0);
108 RNDr(S, W, 1);
109 RNDr(S, W, 2);
110 RNDr(S, W, 3);
111 RNDr(S, W, 4);
112 RNDr(S, W, 5);
113 RNDr(S, W, 6);
114 RNDr(S, W, 7);
115 RNDr(S, W, 8);
116 RNDr(S, W, 9);
117 RNDr(S, W, 10);
118 RNDr(S, W, 11);
119 RNDr(S, W, 12);
120 RNDr(S, W, 13);
121 RNDr(S, W, 14);
122 RNDr(S, W, 15);
123 RNDr(S, W, 16);
124 RNDr(S, W, 17);
125 RNDr(S, W, 18);
126 RNDr(S, W, 19);
127 RNDr(S, W, 20);
128 RNDr(S, W, 21);
129 RNDr(S, W, 22);
130 RNDr(S, W, 23);
131 RNDr(S, W, 24);
132 RNDr(S, W, 25);
133 RNDr(S, W, 26);
134 RNDr(S, W, 27);
135 RNDr(S, W, 28);
136 RNDr(S, W, 29);
137 RNDr(S, W, 30);
138 RNDr(S, W, 31);
139 RNDr(S, W, 32);
140 RNDr(S, W, 33);
141 RNDr(S, W, 34);
142 RNDr(S, W, 35);
143 RNDr(S, W, 36);
144 RNDr(S, W, 37);
145 RNDr(S, W, 38);
146 RNDr(S, W, 39);
147 RNDr(S, W, 40);
148 RNDr(S, W, 41);
149 RNDr(S, W, 42);
150 RNDr(S, W, 43);
151 RNDr(S, W, 44);
152 RNDr(S, W, 45);
153 RNDr(S, W, 46);
154 RNDr(S, W, 47);
155 RNDr(S, W, 48);
156 RNDr(S, W, 49);
157 RNDr(S, W, 50);
158 RNDr(S, W, 51);
159 RNDr(S, W, 52);
160 RNDr(S, W, 53);
161 RNDr(S, W, 54);
162 RNDr(S, W, 55);
163 RNDr(S, W, 56);
164 RNDr(S, W, 57);
165 RNDr(S, W, 58);
166 RNDr(S, W, 59);
167 RNDr(S, W, 60);
168 RNDr(S, W, 61);
169 RNDr(S, W, 62);
170 RNDr(S, W, 63);
171
172 /* 4. Mix local working variables into global state */
173 for (i = 0; i < 8; i++)
174 state[i] += S[i];
175 }
176
177 #endif /* EXTERN_SHA256 */
178
179
180 static const uint32_t sha256d_hash1[16] = {
181 0x00000000, 0x00000000, 0x00000000, 0x00000000,
182 0x00000000, 0x00000000, 0x00000000, 0x00000000,
183 0x80000000, 0x00000000, 0x00000000, 0x00000000,
184 0x00000000, 0x00000000, 0x00000000, 0x00000100
185 };
186
sha256d_80_swap(uint32_t * hash,const uint32_t * data)187 static void sha256d_80_swap(uint32_t *hash, const uint32_t *data)
188 {
189 uint32_t S[16];
190 int i;
191
192 sha256_init(S);
193 sha256_transform(S, data, 0);
194 sha256_transform(S, data + 16, 0);
195 memcpy(S + 8, sha256d_hash1 + 8, 32);
196 sha256_init(hash);
197 sha256_transform(hash, S, 0);
198 for (i = 0; i < 8; i++)
199 hash[i] = swab32(hash[i]);
200 }
201
sha256d(unsigned char * hash,const unsigned char * data,int len)202 void sha256d(unsigned char *hash, const unsigned char *data, int len)
203 {
204 uint32_t S[16], T[16];
205 int i, r;
206
207 sha256_init(S);
208 for (r = len; r > -9; r -= 64) {
209 if (r < 64)
210 memset(T, 0, 64);
211 memcpy(T, data + len - r, r > 64 ? 64 : (r < 0 ? 0 : r));
212 if (r >= 0 && r < 64)
213 ((unsigned char *)T)[r] = 0x80;
214 for (i = 0; i < 16; i++)
215 T[i] = be32dec(T + i);
216 if (r < 56)
217 T[15] = 8 * len;
218 sha256_transform(S, T, 0);
219 }
220 memcpy(S + 8, sha256d_hash1 + 8, 32);
221 sha256_init(T);
222 sha256_transform(T, S, 0);
223 for (i = 0; i < 8; i++)
224 be32enc((uint32_t *)hash + i, T[i]);
225 }
226
sha256d_preextend(uint32_t * W)227 static inline void sha256d_preextend(uint32_t *W)
228 {
229 W[16] = s1(W[14]) + W[ 9] + s0(W[ 1]) + W[ 0];
230 W[17] = s1(W[15]) + W[10] + s0(W[ 2]) + W[ 1];
231 W[18] = s1(W[16]) + W[11] + W[ 2];
232 W[19] = s1(W[17]) + W[12] + s0(W[ 4]);
233 W[20] = W[13] + s0(W[ 5]) + W[ 4];
234 W[21] = W[14] + s0(W[ 6]) + W[ 5];
235 W[22] = W[15] + s0(W[ 7]) + W[ 6];
236 W[23] = W[16] + s0(W[ 8]) + W[ 7];
237 W[24] = W[17] + s0(W[ 9]) + W[ 8];
238 W[25] = s0(W[10]) + W[ 9];
239 W[26] = s0(W[11]) + W[10];
240 W[27] = s0(W[12]) + W[11];
241 W[28] = s0(W[13]) + W[12];
242 W[29] = s0(W[14]) + W[13];
243 W[30] = s0(W[15]) + W[14];
244 W[31] = s0(W[16]) + W[15];
245 }
246
sha256d_prehash(uint32_t * S,const uint32_t * W)247 static inline void sha256d_prehash(uint32_t *S, const uint32_t *W)
248 {
249 uint32_t t0, t1;
250 RNDr(S, W, 0);
251 RNDr(S, W, 1);
252 RNDr(S, W, 2);
253 }
254
255 #ifdef EXTERN_SHA256
256
257 void sha256d_ms(uint32_t *hash, uint32_t *W,
258 const uint32_t *midstate, const uint32_t *prehash);
259
260 #else
261
sha256d_ms(uint32_t * hash,uint32_t * W,const uint32_t * midstate,const uint32_t * prehash)262 static inline void sha256d_ms(uint32_t *hash, uint32_t *W,
263 const uint32_t *midstate, const uint32_t *prehash)
264 {
265 uint32_t S[64];
266 uint32_t t0, t1;
267 int i;
268
269 S[18] = W[18];
270 S[19] = W[19];
271 S[20] = W[20];
272 S[22] = W[22];
273 S[23] = W[23];
274 S[24] = W[24];
275 S[30] = W[30];
276 S[31] = W[31];
277
278 W[18] += s0(W[3]);
279 W[19] += W[3];
280 W[20] += s1(W[18]);
281 W[21] = s1(W[19]);
282 W[22] += s1(W[20]);
283 W[23] += s1(W[21]);
284 W[24] += s1(W[22]);
285 W[25] = s1(W[23]) + W[18];
286 W[26] = s1(W[24]) + W[19];
287 W[27] = s1(W[25]) + W[20];
288 W[28] = s1(W[26]) + W[21];
289 W[29] = s1(W[27]) + W[22];
290 W[30] += s1(W[28]) + W[23];
291 W[31] += s1(W[29]) + W[24];
292 for (i = 32; i < 64; i += 2) {
293 W[i] = s1(W[i - 2]) + W[i - 7] + s0(W[i - 15]) + W[i - 16];
294 W[i+1] = s1(W[i - 1]) + W[i - 6] + s0(W[i - 14]) + W[i - 15];
295 }
296
297 memcpy(S, prehash, 32);
298
299 RNDr(S, W, 3);
300 RNDr(S, W, 4);
301 RNDr(S, W, 5);
302 RNDr(S, W, 6);
303 RNDr(S, W, 7);
304 RNDr(S, W, 8);
305 RNDr(S, W, 9);
306 RNDr(S, W, 10);
307 RNDr(S, W, 11);
308 RNDr(S, W, 12);
309 RNDr(S, W, 13);
310 RNDr(S, W, 14);
311 RNDr(S, W, 15);
312 RNDr(S, W, 16);
313 RNDr(S, W, 17);
314 RNDr(S, W, 18);
315 RNDr(S, W, 19);
316 RNDr(S, W, 20);
317 RNDr(S, W, 21);
318 RNDr(S, W, 22);
319 RNDr(S, W, 23);
320 RNDr(S, W, 24);
321 RNDr(S, W, 25);
322 RNDr(S, W, 26);
323 RNDr(S, W, 27);
324 RNDr(S, W, 28);
325 RNDr(S, W, 29);
326 RNDr(S, W, 30);
327 RNDr(S, W, 31);
328 RNDr(S, W, 32);
329 RNDr(S, W, 33);
330 RNDr(S, W, 34);
331 RNDr(S, W, 35);
332 RNDr(S, W, 36);
333 RNDr(S, W, 37);
334 RNDr(S, W, 38);
335 RNDr(S, W, 39);
336 RNDr(S, W, 40);
337 RNDr(S, W, 41);
338 RNDr(S, W, 42);
339 RNDr(S, W, 43);
340 RNDr(S, W, 44);
341 RNDr(S, W, 45);
342 RNDr(S, W, 46);
343 RNDr(S, W, 47);
344 RNDr(S, W, 48);
345 RNDr(S, W, 49);
346 RNDr(S, W, 50);
347 RNDr(S, W, 51);
348 RNDr(S, W, 52);
349 RNDr(S, W, 53);
350 RNDr(S, W, 54);
351 RNDr(S, W, 55);
352 RNDr(S, W, 56);
353 RNDr(S, W, 57);
354 RNDr(S, W, 58);
355 RNDr(S, W, 59);
356 RNDr(S, W, 60);
357 RNDr(S, W, 61);
358 RNDr(S, W, 62);
359 RNDr(S, W, 63);
360
361 for (i = 0; i < 8; i++)
362 S[i] += midstate[i];
363
364 W[18] = S[18];
365 W[19] = S[19];
366 W[20] = S[20];
367 W[22] = S[22];
368 W[23] = S[23];
369 W[24] = S[24];
370 W[30] = S[30];
371 W[31] = S[31];
372
373 memcpy(S + 8, sha256d_hash1 + 8, 32);
374 S[16] = s1(sha256d_hash1[14]) + sha256d_hash1[ 9] + s0(S[ 1]) + S[ 0];
375 S[17] = s1(sha256d_hash1[15]) + sha256d_hash1[10] + s0(S[ 2]) + S[ 1];
376 S[18] = s1(S[16]) + sha256d_hash1[11] + s0(S[ 3]) + S[ 2];
377 S[19] = s1(S[17]) + sha256d_hash1[12] + s0(S[ 4]) + S[ 3];
378 S[20] = s1(S[18]) + sha256d_hash1[13] + s0(S[ 5]) + S[ 4];
379 S[21] = s1(S[19]) + sha256d_hash1[14] + s0(S[ 6]) + S[ 5];
380 S[22] = s1(S[20]) + sha256d_hash1[15] + s0(S[ 7]) + S[ 6];
381 S[23] = s1(S[21]) + S[16] + s0(sha256d_hash1[ 8]) + S[ 7];
382 S[24] = s1(S[22]) + S[17] + s0(sha256d_hash1[ 9]) + sha256d_hash1[ 8];
383 S[25] = s1(S[23]) + S[18] + s0(sha256d_hash1[10]) + sha256d_hash1[ 9];
384 S[26] = s1(S[24]) + S[19] + s0(sha256d_hash1[11]) + sha256d_hash1[10];
385 S[27] = s1(S[25]) + S[20] + s0(sha256d_hash1[12]) + sha256d_hash1[11];
386 S[28] = s1(S[26]) + S[21] + s0(sha256d_hash1[13]) + sha256d_hash1[12];
387 S[29] = s1(S[27]) + S[22] + s0(sha256d_hash1[14]) + sha256d_hash1[13];
388 S[30] = s1(S[28]) + S[23] + s0(sha256d_hash1[15]) + sha256d_hash1[14];
389 S[31] = s1(S[29]) + S[24] + s0(S[16]) + sha256d_hash1[15];
390 for (i = 32; i < 60; i += 2) {
391 S[i] = s1(S[i - 2]) + S[i - 7] + s0(S[i - 15]) + S[i - 16];
392 S[i+1] = s1(S[i - 1]) + S[i - 6] + s0(S[i - 14]) + S[i - 15];
393 }
394 S[60] = s1(S[58]) + S[53] + s0(S[45]) + S[44];
395
396 sha256_init(hash);
397
398 RNDr(hash, S, 0);
399 RNDr(hash, S, 1);
400 RNDr(hash, S, 2);
401 RNDr(hash, S, 3);
402 RNDr(hash, S, 4);
403 RNDr(hash, S, 5);
404 RNDr(hash, S, 6);
405 RNDr(hash, S, 7);
406 RNDr(hash, S, 8);
407 RNDr(hash, S, 9);
408 RNDr(hash, S, 10);
409 RNDr(hash, S, 11);
410 RNDr(hash, S, 12);
411 RNDr(hash, S, 13);
412 RNDr(hash, S, 14);
413 RNDr(hash, S, 15);
414 RNDr(hash, S, 16);
415 RNDr(hash, S, 17);
416 RNDr(hash, S, 18);
417 RNDr(hash, S, 19);
418 RNDr(hash, S, 20);
419 RNDr(hash, S, 21);
420 RNDr(hash, S, 22);
421 RNDr(hash, S, 23);
422 RNDr(hash, S, 24);
423 RNDr(hash, S, 25);
424 RNDr(hash, S, 26);
425 RNDr(hash, S, 27);
426 RNDr(hash, S, 28);
427 RNDr(hash, S, 29);
428 RNDr(hash, S, 30);
429 RNDr(hash, S, 31);
430 RNDr(hash, S, 32);
431 RNDr(hash, S, 33);
432 RNDr(hash, S, 34);
433 RNDr(hash, S, 35);
434 RNDr(hash, S, 36);
435 RNDr(hash, S, 37);
436 RNDr(hash, S, 38);
437 RNDr(hash, S, 39);
438 RNDr(hash, S, 40);
439 RNDr(hash, S, 41);
440 RNDr(hash, S, 42);
441 RNDr(hash, S, 43);
442 RNDr(hash, S, 44);
443 RNDr(hash, S, 45);
444 RNDr(hash, S, 46);
445 RNDr(hash, S, 47);
446 RNDr(hash, S, 48);
447 RNDr(hash, S, 49);
448 RNDr(hash, S, 50);
449 RNDr(hash, S, 51);
450 RNDr(hash, S, 52);
451 RNDr(hash, S, 53);
452 RNDr(hash, S, 54);
453 RNDr(hash, S, 55);
454 RNDr(hash, S, 56);
455
456 hash[2] += hash[6] + S1(hash[3]) + Ch(hash[3], hash[4], hash[5])
457 + S[57] + sha256_k[57];
458 hash[1] += hash[5] + S1(hash[2]) + Ch(hash[2], hash[3], hash[4])
459 + S[58] + sha256_k[58];
460 hash[0] += hash[4] + S1(hash[1]) + Ch(hash[1], hash[2], hash[3])
461 + S[59] + sha256_k[59];
462 hash[7] += hash[3] + S1(hash[0]) + Ch(hash[0], hash[1], hash[2])
463 + S[60] + sha256_k[60]
464 + sha256_h[7];
465 }
466
467 #endif /* EXTERN_SHA256 */
468
469 #ifdef HAVE_SHA256_4WAY
470
471 void sha256d_ms_4way(uint32_t *hash, uint32_t *data,
472 const uint32_t *midstate, const uint32_t *prehash);
473
scanhash_sha256d_4way(int thr_id,uint32_t * pdata,const uint32_t * ptarget,uint32_t max_nonce,unsigned long * hashes_done)474 static inline int scanhash_sha256d_4way(int thr_id, uint32_t *pdata,
475 const uint32_t *ptarget, uint32_t max_nonce, unsigned long *hashes_done)
476 {
477 uint32_t data[4 * 64] __attribute__((aligned(128)));
478 uint32_t hash[4 * 8] __attribute__((aligned(32)));
479 uint32_t midstate[4 * 8] __attribute__((aligned(32)));
480 uint32_t prehash[4 * 8] __attribute__((aligned(32)));
481 uint32_t n = pdata[19] - 1;
482 const uint32_t first_nonce = pdata[19];
483 const uint32_t Htarg = ptarget[7];
484 int i, j;
485
486 memcpy(data, pdata + 16, 64);
487 sha256d_preextend(data);
488 for (i = 31; i >= 0; i--)
489 for (j = 0; j < 4; j++)
490 data[i * 4 + j] = data[i];
491
492 sha256_init(midstate);
493 sha256_transform(midstate, pdata, 0);
494 memcpy(prehash, midstate, 32);
495 sha256d_prehash(prehash, pdata + 16);
496 for (i = 7; i >= 0; i--) {
497 for (j = 0; j < 4; j++) {
498 midstate[i * 4 + j] = midstate[i];
499 prehash[i * 4 + j] = prehash[i];
500 }
501 }
502
503 do {
504 for (i = 0; i < 4; i++)
505 data[4 * 3 + i] = ++n;
506
507 sha256d_ms_4way(hash, data, midstate, prehash);
508
509 for (i = 0; i < 4; i++) {
510 if (swab32(hash[4 * 7 + i]) <= Htarg) {
511 pdata[19] = data[4 * 3 + i];
512 sha256d_80_swap(hash, pdata);
513 if (fulltest(hash, ptarget)) {
514 *hashes_done = n - first_nonce + 1;
515 return 1;
516 }
517 }
518 }
519 } while (n < max_nonce && !work_restart[thr_id].restart);
520
521 *hashes_done = n - first_nonce + 1;
522 pdata[19] = n;
523 return 0;
524 }
525
526 #endif /* HAVE_SHA256_4WAY */
527
528 #ifdef HAVE_SHA256_8WAY
529
530 void sha256d_ms_8way(uint32_t *hash, uint32_t *data,
531 const uint32_t *midstate, const uint32_t *prehash);
532
scanhash_sha256d_8way(int thr_id,uint32_t * pdata,const uint32_t * ptarget,uint32_t max_nonce,unsigned long * hashes_done)533 static inline int scanhash_sha256d_8way(int thr_id, uint32_t *pdata,
534 const uint32_t *ptarget, uint32_t max_nonce, unsigned long *hashes_done)
535 {
536 uint32_t data[8 * 64] __attribute__((aligned(128)));
537 uint32_t hash[8 * 8] __attribute__((aligned(32)));
538 uint32_t midstate[8 * 8] __attribute__((aligned(32)));
539 uint32_t prehash[8 * 8] __attribute__((aligned(32)));
540 uint32_t n = pdata[19] - 1;
541 const uint32_t first_nonce = pdata[19];
542 const uint32_t Htarg = ptarget[7];
543 int i, j;
544
545 memcpy(data, pdata + 16, 64);
546 sha256d_preextend(data);
547 for (i = 31; i >= 0; i--)
548 for (j = 0; j < 8; j++)
549 data[i * 8 + j] = data[i];
550
551 sha256_init(midstate);
552 sha256_transform(midstate, pdata, 0);
553 memcpy(prehash, midstate, 32);
554 sha256d_prehash(prehash, pdata + 16);
555 for (i = 7; i >= 0; i--) {
556 for (j = 0; j < 8; j++) {
557 midstate[i * 8 + j] = midstate[i];
558 prehash[i * 8 + j] = prehash[i];
559 }
560 }
561
562 do {
563 for (i = 0; i < 8; i++)
564 data[8 * 3 + i] = ++n;
565
566 sha256d_ms_8way(hash, data, midstate, prehash);
567
568 for (i = 0; i < 8; i++) {
569 if (swab32(hash[8 * 7 + i]) <= Htarg) {
570 pdata[19] = data[8 * 3 + i];
571 sha256d_80_swap(hash, pdata);
572 if (fulltest(hash, ptarget)) {
573 *hashes_done = n - first_nonce + 1;
574 return 1;
575 }
576 }
577 }
578 } while (n < max_nonce && !work_restart[thr_id].restart);
579
580 *hashes_done = n - first_nonce + 1;
581 pdata[19] = n;
582 return 0;
583 }
584
585 #endif /* HAVE_SHA256_8WAY */
586
scanhash_sha256d(int thr_id,uint32_t * pdata,const uint32_t * ptarget,uint32_t max_nonce,unsigned long * hashes_done)587 int scanhash_sha256d(int thr_id, uint32_t *pdata, const uint32_t *ptarget,
588 uint32_t max_nonce, unsigned long *hashes_done)
589 {
590 uint32_t data[64] __attribute__((aligned(128)));
591 uint32_t hash[8] __attribute__((aligned(32)));
592 uint32_t midstate[8] __attribute__((aligned(32)));
593 uint32_t prehash[8] __attribute__((aligned(32)));
594 uint32_t n = pdata[19] - 1;
595 const uint32_t first_nonce = pdata[19];
596 const uint32_t Htarg = ptarget[7];
597
598 #ifdef HAVE_SHA256_8WAY
599 if (sha256_use_8way())
600 return scanhash_sha256d_8way(thr_id, pdata, ptarget,
601 max_nonce, hashes_done);
602 #endif
603 #ifdef HAVE_SHA256_4WAY
604 if (sha256_use_4way())
605 return scanhash_sha256d_4way(thr_id, pdata, ptarget,
606 max_nonce, hashes_done);
607 #endif
608
609 memcpy(data, pdata + 16, 64);
610 sha256d_preextend(data);
611
612 sha256_init(midstate);
613 sha256_transform(midstate, pdata, 0);
614 memcpy(prehash, midstate, 32);
615 sha256d_prehash(prehash, pdata + 16);
616
617 do {
618 data[3] = ++n;
619 sha256d_ms(hash, data, midstate, prehash);
620 if (swab32(hash[7]) <= Htarg) {
621 pdata[19] = data[3];
622 sha256d_80_swap(hash, pdata);
623 if (fulltest(hash, ptarget)) {
624 *hashes_done = n - first_nonce + 1;
625 return 1;
626 }
627 }
628 } while (n < max_nonce && !work_restart[thr_id].restart);
629
630 *hashes_done = n - first_nonce + 1;
631 pdata[19] = n;
632 return 0;
633 }
634