12 #include "algo-gate-api.h" 17 #if defined(USE_ASM) && defined(__arm__) && defined(__APCS_32__) 21 static const uint32_t sha256_h[8] = {
22 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
23 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
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
47 memcpy(state, sha256_h, 32);
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)) 60 #define RND(a, b, c, d, e, f, g, h, k) \ 62 t0 = h + S1(e) + Ch(e, f, g) + k; \ 63 t1 = S0(a) + Maj(a, b, c); \ 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], \ 91 for (i = 0; i < 16; i++)
92 W[i] = swab32(block[i]);
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];
170 for (i = 0; i < 8; i++)
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
184 static void sha256d_80_swap(uint32_t *hash,
const uint32_t *data)
192 memcpy(S + 8, sha256d_hash1 + 8, 32);
195 for (i = 0; i < 8; i++)
196 hash[i] = swab32(hash[i]);
199 extern void sha256d(
unsigned char *hash,
const unsigned char *data,
int len)
201 uint32_t
S[16], T[16];
205 for (r = len; r > -9; r -= 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);
217 memcpy(S + 8, sha256d_hash1 + 8, 32);
220 for (i = 0; i < 8; i++)
221 be32enc((uint32_t *)hash + i, T[i]);
224 static inline void sha256d_preextend(uint32_t *W)
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];
244 static inline void sha256d_prehash(uint32_t *
S,
const uint32_t *W)
254 void sha256d_ms(uint32_t *hash, uint32_t *W,
255 const uint32_t *midstate,
const uint32_t *prehash);
259 static inline void sha256d_ms(uint32_t *hash, uint32_t *W,
260 const uint32_t *midstate,
const uint32_t *prehash)
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];
358 for (i = 0; i < 8; i++)
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];
391 S[60] =
s1(S[58]) + S[53] +
s0(S[45]) + S[44];
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]
466 #ifdef HAVE_SHA256_4WAY 468 void sha256d_ms_4way(uint32_t *hash, uint32_t *data,
469 const uint32_t *midstate,
const uint32_t *prehash);
471 static inline int scanhash_sha256d_4way(
int thr_id,
struct work *work,
472 uint32_t max_nonce, uint64_t *hashes_done)
474 uint32_t *pdata = work->data;
475 uint32_t *ptarget = work->target;
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];
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];
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];
504 for (i = 0; i < 4; i++)
505 data[4 * 3 + i] = ++n;
507 sha256d_ms_4way(hash, data, midstate, prehash);
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;
519 }
while (n < max_nonce && !work_restart[thr_id].restart);
521 *hashes_done = n - first_nonce + 1;
528 #ifdef HAVE_SHA256_8WAY 530 void sha256d_ms_8way(uint32_t *hash, uint32_t *data,
531 const uint32_t *midstate,
const uint32_t *prehash);
533 static inline int scanhash_sha256d_8way(
int thr_id,
struct work *work,
534 uint32_t max_nonce, uint64_t *hashes_done)
536 uint32_t *pdata = work->data;
537 uint32_t *ptarget = work->target;
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];
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];
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];
566 for (i = 0; i < 8; i++)
567 data[8 * 3 + i] = ++n;
569 sha256d_ms_8way(hash, data, midstate, prehash);
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;
581 }
while (n < max_nonce && !work_restart[thr_id].restart);
583 *hashes_done = n - first_nonce + 1;
591 uint32_t max_nonce, uint64_t *hashes_done)
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];
603 #ifdef HAVE_SHA256_8WAY 604 if (sha256_use_8way())
605 return scanhash_sha256d_8way(thr_id, work,
606 max_nonce, hashes_done);
608 #ifdef HAVE_SHA256_4WAY 609 if (sha256_use_4way())
610 return scanhash_sha256d_4way(thr_id, work,
611 max_nonce, hashes_done);
614 memcpy(data, pdata + 16, 64);
615 sha256d_preextend(data);
619 memcpy(prehash, midstate, 32);
620 sha256d_prehash(prehash, pdata + 16);
624 sha256d_ms(hash, data, midstate, prehash);
625 if (unlikely(swab32(hash[7]) <= Htarg)) {
627 sha256d_80_swap(hash, pdata);
628 if (fulltest(hash, ptarget)) {
629 *hashes_done = n - first_nonce + 1;
633 }
while (likely(n < max_nonce && !work_restart[thr_id].restart));
635 *hashes_done = n - first_nonce + 1;
643 gate->hash_alt = (
void*)&
sha256d;
bool register_sha256d_algo(algo_gate_t *gate)
void sha256d(unsigned char *hash, const unsigned char *data, int len)
void sha256_transform(uint32_t *state, const uint32_t *block, int swap)
int scanhash_sha256d(int thr_id, struct work *work, uint32_t max_nonce, uint64_t *hashes_done)
#define S(x0, x1, x2, x3, cb, r)
void sha256_init(uint32_t *state)
void * memcpy(void *a, const void *b, size_t c)