// Copyright (c) 2010 Nils Schneider // Distributed under the MIT/X11 software license, see the accompanying // file license.txt or http://www.opensource.org/licenses/mit-license.php. // 4-way 128-bit SSE2 SHA-256 #ifdef FOURWAYSSE2 #include #include #include #include #include #define NPAR 32 extern void DoubleBlockSHA256(const void* pin, void* pout, const void* pinit, unsigned int hash[8][NPAR], const void* init2); static const unsigned int sha256_consts[] = { 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, /* 0 */ 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, /* 8 */ 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, /* 16 */ 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, /* 24 */ 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, /* 32 */ 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, /* 40 */ 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, /* 48 */ 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, /* 56 */ 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 }; static inline __m128i Ch(const __m128i b, const __m128i c, const __m128i d) { return (b & c) ^ (~b & d); } static inline __m128i Maj(const __m128i b, const __m128i c, const __m128i d) { return (b & c) ^ (b & d) ^ (c & d); } static inline __m128i ROTR(__m128i x, const int n) { return _mm_srli_epi32(x, n) | _mm_slli_epi32(x, 32 - n); } static inline __m128i SHR(__m128i x, const int n) { return _mm_srli_epi32(x, n); } /* SHA256 Functions */ #define BIGSIGMA0_256(x) (ROTR((x), 2) ^ ROTR((x), 13) ^ ROTR((x), 22)) #define BIGSIGMA1_256(x) (ROTR((x), 6) ^ ROTR((x), 11) ^ ROTR((x), 25)) #define SIGMA0_256(x) (ROTR((x), 7) ^ ROTR((x), 18) ^ SHR((x), 3)) #define SIGMA1_256(x) (ROTR((x), 17) ^ ROTR((x), 19) ^ SHR((x), 10)) static inline unsigned int store32(const __m128i x, int i) { union { unsigned int ret[4]; __m128i x; } box; box.x = x; return box.ret[i]; } static inline void store_epi32(const __m128i x, unsigned int *x0, unsigned int *x1, unsigned int *x2, unsigned int *x3) { union { unsigned int ret[4]; __m128i x; } box; box.x = x; *x0 = box.ret[3]; *x1 = box.ret[2]; *x2 = box.ret[1]; *x3 = box.ret[0]; } #define add4(x0, x1, x2, x3) _mm_add_epi32(_mm_add_epi32(_mm_add_epi32(x0, x1), x2), x3) #define add5(x0, x1, x2, x3, x4) _mm_add_epi32(add4(x0, x1, x2, x3), x4) #define SHA256ROUND(a, b, c, d, e, f, g, h, i, w) \ T1 = add5(h, BIGSIGMA1_256(e), Ch(e, f, g), _mm_set1_epi32(sha256_consts[i]), w); \ d = _mm_add_epi32(d, T1); \ h = _mm_add_epi32(T1, _mm_add_epi32(BIGSIGMA0_256(a), Maj(a, b, c))); static inline void dumpreg(__m128i x, char *msg) { union { unsigned int ret[4]; __m128i x; } box; box.x = x ; printf("%s %08x %08x %08x %08x\n", msg, box.ret[0], box.ret[1], box.ret[2], box.ret[3]); } #if 1 #define dumpstate(i) printf("%s: %08x %08x %08x %08x %08x %08x %08x %08x %08x\n", \ __func__, store32(w0, i), store32(a, i), store32(b, i), store32(c, i), store32(d, i), store32(e, i), store32(f, i), store32(g, i), store32(h, i)); #else #define dumpstate() #endif // Align by increasing pointer, must have extra space at end of buffer template T* alignup(T* p) { union { T* ptr; size_t n; } u; u.ptr = p; u.n = (u.n + (nBytes-1)) & ~(nBytes-1); return u.ptr; } static const unsigned int pSHA256InitState[8] = {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19}; unsigned int ScanHash_4WaySSE2(char* pmidstate, char* pdata, char* phash1, char* phash, unsigned int& nHashesDone) { unsigned int& nNonce = *(unsigned int*)(pdata + 12); for (;;) { nNonce += NPAR; unsigned int thashbuf[9][NPAR]; unsigned int (&thash)[9][NPAR] = *alignup<16>(&thashbuf); DoubleBlockSHA256(pdata, phash1, pmidstate, thash, pSHA256InitState); for (int j = 0; j < NPAR; j++) { if (thash[7][j] == 0) { for (int i = 0; i < 32/4; i++) ((unsigned int*)phash)[i] = thash[i][j]; return nNonce + j; } } if ((nNonce & 0xffff) == 0) { nHashesDone = 0xffff+1; return -1; } } } void DoubleBlockSHA256(const void* pin, void* pad, const void *pre, unsigned int thash[9][NPAR], const void *init) { unsigned int* In = (unsigned int*)pin; unsigned int* Pad = (unsigned int*)pad; unsigned int* hPre = (unsigned int*)pre; unsigned int* hInit = (unsigned int*)init; unsigned int i, j, k; /* vectors used in calculation */ __m128i w0, w1, w2, w3, w4, w5, w6, w7; __m128i w8, w9, w10, w11, w12, w13, w14, w15; __m128i T1; __m128i a, b, c, d, e, f, g, h; __m128i nonce; /* nonce offset for vector */ __m128i offset = _mm_set_epi32(0x00000003, 0x00000002, 0x00000001, 0x00000000); for(k = 0; k