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