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