sha256_altivec_4way.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. // Copyright (c) 2010 Satoshi Nakamoto
  2. // Copyright (c) 2011 Gilles Risch
  3. // Distributed under the MIT/X11 software license, see the accompanying
  4. // file license.txt or http://www.opensource.org/licenses/mit-license.php.
  5. // 4-way 128-bit Altivec SHA-256,
  6. // based on tcatm's 4-way 128-bit SSE2 SHA-256
  7. //
  8. #include "device-cpu.h"
  9. #ifdef WANT_ALTIVEC_4WAY
  10. #include <string.h>
  11. #include <assert.h>
  12. //#include <altivec.h>
  13. #include <stdint.h>
  14. #include <stdio.h>
  15. #define NPAR 32
  16. static void DoubleBlockSHA256(const void* pin, void* pout, const void* pinit, unsigned int hash[8][NPAR], const void* init2);
  17. static const unsigned int sha256_consts[] = {
  18. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, /* 0 */
  19. 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  20. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, /* 8 */
  21. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  22. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, /* 16 */
  23. 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  24. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, /* 24 */
  25. 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  26. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, /* 32 */
  27. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  28. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, /* 40 */
  29. 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  30. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, /* 48 */
  31. 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  32. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, /* 56 */
  33. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  34. };
  35. static inline vector unsigned int Ch(const vector unsigned int b, const vector unsigned int c, const vector unsigned int d) {
  36. return vec_sel(d,c,b);
  37. }
  38. static inline vector unsigned int Maj(const vector unsigned int b, const vector unsigned int c, const vector unsigned int d) {
  39. return vec_sel(b,c, vec_xor(b,d));
  40. }
  41. /* RotateRight(x, n) := RotateLeft(x, 32-n) */
  42. /* SHA256 Functions */
  43. #define BIGSIGMA0_256(x) (vec_xor(vec_xor(vec_rl((x), (vector unsigned int)(32-2)),vec_rl((x), (vector unsigned int)(32-13))),vec_rl((x), (vector unsigned int)(32-22))))
  44. #define BIGSIGMA1_256(x) (vec_xor(vec_xor(vec_rl((x), (vector unsigned int)(32-6)),vec_rl((x), (vector unsigned int)(32-11))),vec_rl((x), (vector unsigned int)(32-25))))
  45. #define SIGMA0_256(x) (vec_xor(vec_xor(vec_rl((x), (vector unsigned int)(32- 7)),vec_rl((x), (vector unsigned int)(32-18))), vec_sr((x), (vector unsigned int)(3 ))))
  46. #define SIGMA1_256(x) (vec_xor(vec_xor(vec_rl((x), (vector unsigned int)(32-17)),vec_rl((x), (vector unsigned int)(32-19))), vec_sr((x), (vector unsigned int)(10))))
  47. #define add4(x0, x1, x2, x3) vec_add(vec_add(x0, x1),vec_add( x2,x3))
  48. #define add5(x0, x1, x2, x3, x4) vec_add(add4(x0, x1, x2, x3), x4)
  49. #define SHA256ROUND(a, b, c, d, e, f, g, h, i, w) \
  50. T1 = add5(h, BIGSIGMA1_256(e), Ch(e, f, g), (vector unsigned int)(sha256_consts[i],sha256_consts[i],sha256_consts[i],sha256_consts[i]), w); \
  51. d = vec_add(d, T1); \
  52. h = vec_add(T1, vec_add(BIGSIGMA0_256(a), Maj(a, b, c)));
  53. static const unsigned int pSHA256InitState[8] =
  54. {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
  55. bool ScanHash_altivec_4way(int thr_id, const unsigned char *pmidstate,
  56. unsigned char *pdata,
  57. unsigned char *phash1, unsigned char *phash,
  58. const unsigned char *ptarget,
  59. uint32_t max_nonce, uint32_t *last_nonce,
  60. uint32_t nonce)
  61. {
  62. unsigned int *nNonce_p = (unsigned int*)(pdata + 76);
  63. pdata += 64;
  64. work_restart[thr_id].restart = 0;
  65. for (;;)
  66. {
  67. unsigned int thash[9][NPAR] __attribute__((aligned(128)));
  68. int j;
  69. *nNonce_p = nonce;
  70. DoubleBlockSHA256(pdata, phash1, pmidstate, thash, pSHA256InitState);
  71. for (j = 0; j < NPAR; j++)
  72. {
  73. if (unlikely(thash[7][j] == 0))
  74. {
  75. int i;
  76. for (i = 0; i < 32/4; i++)
  77. ((unsigned int*)phash)[i] = thash[i][j];
  78. if (fulltest(phash, ptarget)) {
  79. nonce += j;
  80. *last_nonce = nonce;
  81. *nNonce_p = nonce;
  82. return true;
  83. }
  84. }
  85. }
  86. if ((nonce >= max_nonce) || work_restart[thr_id].restart)
  87. {
  88. *last_nonce = nonce;
  89. return false;
  90. }
  91. nonce += NPAR;
  92. }
  93. }
  94. static void DoubleBlockSHA256(const void* pin, void* pad, const void *pre, unsigned int thash[9][NPAR], const void *init)
  95. {
  96. unsigned int* In = (unsigned int*)pin;
  97. unsigned int* Pad = (unsigned int*)pad;
  98. unsigned int* hPre = (unsigned int*)pre;
  99. unsigned int* hInit = (unsigned int*)init;
  100. unsigned int /* i, j, */ k;
  101. /* vectors used in calculation */
  102. vector unsigned int w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14, w15;
  103. vector unsigned int T1;
  104. vector unsigned int a, b, c, d, e, f, g, h;
  105. vector unsigned int nonce, preNonce;
  106. /* nonce offset for vector */
  107. vector unsigned int offset = (vector unsigned int)(0, 1, 2, 3);
  108. preNonce = vec_add((vector unsigned int)(In[3],In[3],In[3],In[3]), offset);
  109. for(k = 0; k<NPAR; k+=4)
  110. {
  111. w0 = (vector unsigned int)(In[0],In[0],In[0],In[0]);
  112. w1 = (vector unsigned int)(In[1],In[1],In[1],In[1]);
  113. w2 = (vector unsigned int)(In[2],In[2],In[2],In[2]);
  114. //w3 = (vector unsigned int)(In[3],In[3],In[3],In[3]); nonce will be later hacked into the hash
  115. w4 = (vector unsigned int)(In[4],In[4],In[4],In[4]);
  116. w5 = (vector unsigned int)(In[5],In[5],In[5],In[5]);
  117. w6 = (vector unsigned int)(In[6],In[6],In[6],In[6]);
  118. w7 = (vector unsigned int)(In[7],In[7],In[7],In[7]);
  119. w8 = (vector unsigned int)(In[8],In[8],In[8],In[8]);
  120. w9 = (vector unsigned int)(In[9],In[9],In[9],In[9]);
  121. w10 = (vector unsigned int)(In[10],In[10],In[10],In[10]);
  122. w11 = (vector unsigned int)(In[11],In[11],In[11],In[11]);
  123. w12 = (vector unsigned int)(In[12],In[12],In[12],In[12]);
  124. w13 = (vector unsigned int)(In[13],In[13],In[13],In[13]);
  125. w14 = (vector unsigned int)(In[14],In[14],In[14],In[14]);
  126. w15 = (vector unsigned int)(In[15],In[15],In[15],In[15]);
  127. /* hack nonce into lowest byte of w3 */
  128. nonce = vec_add(preNonce, (vector unsigned int)(k,k,k,k));
  129. w3 = nonce;
  130. //printf ("W3: %08vlx\n", w3);
  131. a = (vector unsigned int)(hPre[0],hPre[0],hPre[0],hPre[0]);
  132. b = (vector unsigned int)(hPre[1],hPre[1],hPre[1],hPre[1]);
  133. c = (vector unsigned int)(hPre[2],hPre[2],hPre[2],hPre[2]);
  134. d = (vector unsigned int)(hPre[3],hPre[3],hPre[3],hPre[3]);
  135. e = (vector unsigned int)(hPre[4],hPre[4],hPre[4],hPre[4]);
  136. f = (vector unsigned int)(hPre[5],hPre[5],hPre[5],hPre[5]);
  137. g = (vector unsigned int)(hPre[6],hPre[6],hPre[6],hPre[6]);
  138. h = (vector unsigned int)(hPre[7],hPre[7],hPre[7],hPre[7]);
  139. SHA256ROUND(a, b, c, d, e, f, g, h, 0, w0);
  140. SHA256ROUND(h, a, b, c, d, e, f, g, 1, w1);
  141. SHA256ROUND(g, h, a, b, c, d, e, f, 2, w2);
  142. SHA256ROUND(f, g, h, a, b, c, d, e, 3, w3);
  143. SHA256ROUND(e, f, g, h, a, b, c, d, 4, w4);
  144. SHA256ROUND(d, e, f, g, h, a, b, c, 5, w5);
  145. SHA256ROUND(c, d, e, f, g, h, a, b, 6, w6);
  146. SHA256ROUND(b, c, d, e, f, g, h, a, 7, w7);
  147. SHA256ROUND(a, b, c, d, e, f, g, h, 8, w8);
  148. SHA256ROUND(h, a, b, c, d, e, f, g, 9, w9);
  149. SHA256ROUND(g, h, a, b, c, d, e, f, 10, w10);
  150. SHA256ROUND(f, g, h, a, b, c, d, e, 11, w11);
  151. SHA256ROUND(e, f, g, h, a, b, c, d, 12, w12);
  152. SHA256ROUND(d, e, f, g, h, a, b, c, 13, w13);
  153. SHA256ROUND(c, d, e, f, g, h, a, b, 14, w14);
  154. SHA256ROUND(b, c, d, e, f, g, h, a, 15, w15);
  155. w0 = add4(SIGMA1_256(w14), w9, SIGMA0_256(w1), w0);
  156. SHA256ROUND(a, b, c, d, e, f, g, h, 16, w0);
  157. w1 = add4(SIGMA1_256(w15), w10, SIGMA0_256(w2), w1);
  158. SHA256ROUND(h, a, b, c, d, e, f, g, 17, w1);
  159. w2 = add4(SIGMA1_256(w0), w11, SIGMA0_256(w3), w2);
  160. SHA256ROUND(g, h, a, b, c, d, e, f, 18, w2);
  161. w3 = add4(SIGMA1_256(w1), w12, SIGMA0_256(w4), w3);
  162. SHA256ROUND(f, g, h, a, b, c, d, e, 19, w3);
  163. w4 = add4(SIGMA1_256(w2), w13, SIGMA0_256(w5), w4);
  164. SHA256ROUND(e, f, g, h, a, b, c, d, 20, w4);
  165. w5 = add4(SIGMA1_256(w3), w14, SIGMA0_256(w6), w5);
  166. SHA256ROUND(d, e, f, g, h, a, b, c, 21, w5);
  167. w6 = add4(SIGMA1_256(w4), w15, SIGMA0_256(w7), w6);
  168. SHA256ROUND(c, d, e, f, g, h, a, b, 22, w6);
  169. w7 = add4(SIGMA1_256(w5), w0, SIGMA0_256(w8), w7);
  170. SHA256ROUND(b, c, d, e, f, g, h, a, 23, w7);
  171. w8 = add4(SIGMA1_256(w6), w1, SIGMA0_256(w9), w8);
  172. SHA256ROUND(a, b, c, d, e, f, g, h, 24, w8);
  173. w9 = add4(SIGMA1_256(w7), w2, SIGMA0_256(w10), w9);
  174. SHA256ROUND(h, a, b, c, d, e, f, g, 25, w9);
  175. w10 = add4(SIGMA1_256(w8), w3, SIGMA0_256(w11), w10);
  176. SHA256ROUND(g, h, a, b, c, d, e, f, 26, w10);
  177. w11 = add4(SIGMA1_256(w9), w4, SIGMA0_256(w12), w11);
  178. SHA256ROUND(f, g, h, a, b, c, d, e, 27, w11);
  179. w12 = add4(SIGMA1_256(w10), w5, SIGMA0_256(w13), w12);
  180. SHA256ROUND(e, f, g, h, a, b, c, d, 28, w12);
  181. w13 = add4(SIGMA1_256(w11), w6, SIGMA0_256(w14), w13);
  182. SHA256ROUND(d, e, f, g, h, a, b, c, 29, w13);
  183. w14 = add4(SIGMA1_256(w12), w7, SIGMA0_256(w15), w14);
  184. SHA256ROUND(c, d, e, f, g, h, a, b, 30, w14);
  185. w15 = add4(SIGMA1_256(w13), w8, SIGMA0_256(w0), w15);
  186. SHA256ROUND(b, c, d, e, f, g, h, a, 31, w15);
  187. w0 = add4(SIGMA1_256(w14), w9, SIGMA0_256(w1), w0);
  188. SHA256ROUND(a, b, c, d, e, f, g, h, 32, w0);
  189. w1 = add4(SIGMA1_256(w15), w10, SIGMA0_256(w2), w1);
  190. SHA256ROUND(h, a, b, c, d, e, f, g, 33, w1);
  191. w2 = add4(SIGMA1_256(w0), w11, SIGMA0_256(w3), w2);
  192. SHA256ROUND(g, h, a, b, c, d, e, f, 34, w2);
  193. w3 = add4(SIGMA1_256(w1), w12, SIGMA0_256(w4), w3);
  194. SHA256ROUND(f, g, h, a, b, c, d, e, 35, w3);
  195. w4 = add4(SIGMA1_256(w2), w13, SIGMA0_256(w5), w4);
  196. SHA256ROUND(e, f, g, h, a, b, c, d, 36, w4);
  197. w5 = add4(SIGMA1_256(w3), w14, SIGMA0_256(w6), w5);
  198. SHA256ROUND(d, e, f, g, h, a, b, c, 37, w5);
  199. w6 = add4(SIGMA1_256(w4), w15, SIGMA0_256(w7), w6);
  200. SHA256ROUND(c, d, e, f, g, h, a, b, 38, w6);
  201. w7 = add4(SIGMA1_256(w5), w0, SIGMA0_256(w8), w7);
  202. SHA256ROUND(b, c, d, e, f, g, h, a, 39, w7);
  203. w8 = add4(SIGMA1_256(w6), w1, SIGMA0_256(w9), w8);
  204. SHA256ROUND(a, b, c, d, e, f, g, h, 40, w8);
  205. w9 = add4(SIGMA1_256(w7), w2, SIGMA0_256(w10), w9);
  206. SHA256ROUND(h, a, b, c, d, e, f, g, 41, w9);
  207. w10 = add4(SIGMA1_256(w8), w3, SIGMA0_256(w11), w10);
  208. SHA256ROUND(g, h, a, b, c, d, e, f, 42, w10);
  209. w11 = add4(SIGMA1_256(w9), w4, SIGMA0_256(w12), w11);
  210. SHA256ROUND(f, g, h, a, b, c, d, e, 43, w11);
  211. w12 = add4(SIGMA1_256(w10), w5, SIGMA0_256(w13), w12);
  212. SHA256ROUND(e, f, g, h, a, b, c, d, 44, w12);
  213. w13 = add4(SIGMA1_256(w11), w6, SIGMA0_256(w14), w13);
  214. SHA256ROUND(d, e, f, g, h, a, b, c, 45, w13);
  215. w14 = add4(SIGMA1_256(w12), w7, SIGMA0_256(w15), w14);
  216. SHA256ROUND(c, d, e, f, g, h, a, b, 46, w14);
  217. w15 = add4(SIGMA1_256(w13), w8, SIGMA0_256(w0), w15);
  218. SHA256ROUND(b, c, d, e, f, g, h, a, 47, w15);
  219. w0 = add4(SIGMA1_256(w14), w9, SIGMA0_256(w1), w0);
  220. SHA256ROUND(a, b, c, d, e, f, g, h, 48, w0);
  221. w1 = add4(SIGMA1_256(w15), w10, SIGMA0_256(w2), w1);
  222. SHA256ROUND(h, a, b, c, d, e, f, g, 49, w1);
  223. w2 = add4(SIGMA1_256(w0), w11, SIGMA0_256(w3), w2);
  224. SHA256ROUND(g, h, a, b, c, d, e, f, 50, w2);
  225. w3 = add4(SIGMA1_256(w1), w12, SIGMA0_256(w4), w3);
  226. SHA256ROUND(f, g, h, a, b, c, d, e, 51, w3);
  227. w4 = add4(SIGMA1_256(w2), w13, SIGMA0_256(w5), w4);
  228. SHA256ROUND(e, f, g, h, a, b, c, d, 52, w4);
  229. w5 = add4(SIGMA1_256(w3), w14, SIGMA0_256(w6), w5);
  230. SHA256ROUND(d, e, f, g, h, a, b, c, 53, w5);
  231. w6 = add4(SIGMA1_256(w4), w15, SIGMA0_256(w7), w6);
  232. SHA256ROUND(c, d, e, f, g, h, a, b, 54, w6);
  233. w7 = add4(SIGMA1_256(w5), w0, SIGMA0_256(w8), w7);
  234. SHA256ROUND(b, c, d, e, f, g, h, a, 55, w7);
  235. w8 = add4(SIGMA1_256(w6), w1, SIGMA0_256(w9), w8);
  236. SHA256ROUND(a, b, c, d, e, f, g, h, 56, w8);
  237. w9 = add4(SIGMA1_256(w7), w2, SIGMA0_256(w10), w9);
  238. SHA256ROUND(h, a, b, c, d, e, f, g, 57, w9);
  239. w10 = add4(SIGMA1_256(w8), w3, SIGMA0_256(w11), w10);
  240. SHA256ROUND(g, h, a, b, c, d, e, f, 58, w10);
  241. w11 = add4(SIGMA1_256(w9), w4, SIGMA0_256(w12), w11);
  242. SHA256ROUND(f, g, h, a, b, c, d, e, 59, w11);
  243. w12 = add4(SIGMA1_256(w10), w5, SIGMA0_256(w13), w12);
  244. SHA256ROUND(e, f, g, h, a, b, c, d, 60, w12);
  245. w13 = add4(SIGMA1_256(w11), w6, SIGMA0_256(w14), w13);
  246. SHA256ROUND(d, e, f, g, h, a, b, c, 61, w13);
  247. w14 = add4(SIGMA1_256(w12), w7, SIGMA0_256(w15), w14);
  248. SHA256ROUND(c, d, e, f, g, h, a, b, 62, w14);
  249. w15 = add4(SIGMA1_256(w13), w8, SIGMA0_256(w0), w15);
  250. SHA256ROUND(b, c, d, e, f, g, h, a, 63, w15);
  251. #define store_load(x, i, dest) \
  252. T1 = (vector unsigned int)((hPre)[i],(hPre)[i],(hPre)[i],(hPre)[i]); \
  253. dest = vec_add(T1, x);
  254. store_load(a, 0, w0);
  255. store_load(b, 1, w1);
  256. store_load(c, 2, w2);
  257. store_load(d, 3, w3);
  258. store_load(e, 4, w4);
  259. store_load(f, 5, w5);
  260. store_load(g, 6, w6);
  261. store_load(h, 7, w7);
  262. /* end of first SHA256 round */
  263. w8 = (vector unsigned int)(Pad[8],Pad[8],Pad[8],Pad[8]);
  264. w9 = (vector unsigned int)(Pad[9],Pad[9],Pad[9],Pad[9]);
  265. w10 = (vector unsigned int)(Pad[10],Pad[10],Pad[10],Pad[10]);
  266. w11 = (vector unsigned int)(Pad[11],Pad[11],Pad[11],Pad[11]);
  267. w12 = (vector unsigned int)(Pad[12],Pad[12],Pad[12],Pad[12]);
  268. w13 = (vector unsigned int)(Pad[13],Pad[13],Pad[13],Pad[13]);
  269. w14 = (vector unsigned int)(Pad[14],Pad[14],Pad[14],Pad[14]);
  270. w15 = (vector unsigned int)(Pad[15],Pad[15],Pad[15],Pad[15]);
  271. a = (vector unsigned int)(hInit[0],hInit[0],hInit[0],hInit[0]);
  272. b = (vector unsigned int)(hInit[1],hInit[1],hInit[1],hInit[1]);
  273. c = (vector unsigned int)(hInit[2],hInit[2],hInit[2],hInit[2]);
  274. d = (vector unsigned int)(hInit[3],hInit[3],hInit[3],hInit[3]);
  275. e = (vector unsigned int)(hInit[4],hInit[4],hInit[4],hInit[4]);
  276. f = (vector unsigned int)(hInit[5],hInit[5],hInit[5],hInit[5]);
  277. g = (vector unsigned int)(hInit[6],hInit[6],hInit[6],hInit[6]);
  278. h = (vector unsigned int)(hInit[7],hInit[7],hInit[7],hInit[7]);
  279. SHA256ROUND(a, b, c, d, e, f, g, h, 0, w0);
  280. SHA256ROUND(h, a, b, c, d, e, f, g, 1, w1);
  281. SHA256ROUND(g, h, a, b, c, d, e, f, 2, w2);
  282. SHA256ROUND(f, g, h, a, b, c, d, e, 3, w3);
  283. SHA256ROUND(e, f, g, h, a, b, c, d, 4, w4);
  284. SHA256ROUND(d, e, f, g, h, a, b, c, 5, w5);
  285. SHA256ROUND(c, d, e, f, g, h, a, b, 6, w6);
  286. SHA256ROUND(b, c, d, e, f, g, h, a, 7, w7);
  287. SHA256ROUND(a, b, c, d, e, f, g, h, 8, w8);
  288. SHA256ROUND(h, a, b, c, d, e, f, g, 9, w9);
  289. SHA256ROUND(g, h, a, b, c, d, e, f, 10, w10);
  290. SHA256ROUND(f, g, h, a, b, c, d, e, 11, w11);
  291. SHA256ROUND(e, f, g, h, a, b, c, d, 12, w12);
  292. SHA256ROUND(d, e, f, g, h, a, b, c, 13, w13);
  293. SHA256ROUND(c, d, e, f, g, h, a, b, 14, w14);
  294. SHA256ROUND(b, c, d, e, f, g, h, a, 15, w15);
  295. w0 = add4(SIGMA1_256(w14), w9, SIGMA0_256(w1), w0);
  296. SHA256ROUND(a, b, c, d, e, f, g, h, 16, w0);
  297. w1 = add4(SIGMA1_256(w15), w10, SIGMA0_256(w2), w1);
  298. SHA256ROUND(h, a, b, c, d, e, f, g, 17, w1);
  299. w2 = add4(SIGMA1_256(w0), w11, SIGMA0_256(w3), w2);
  300. SHA256ROUND(g, h, a, b, c, d, e, f, 18, w2);
  301. w3 = add4(SIGMA1_256(w1), w12, SIGMA0_256(w4), w3);
  302. SHA256ROUND(f, g, h, a, b, c, d, e, 19, w3);
  303. w4 = add4(SIGMA1_256(w2), w13, SIGMA0_256(w5), w4);
  304. SHA256ROUND(e, f, g, h, a, b, c, d, 20, w4);
  305. w5 = add4(SIGMA1_256(w3), w14, SIGMA0_256(w6), w5);
  306. SHA256ROUND(d, e, f, g, h, a, b, c, 21, w5);
  307. w6 = add4(SIGMA1_256(w4), w15, SIGMA0_256(w7), w6);
  308. SHA256ROUND(c, d, e, f, g, h, a, b, 22, w6);
  309. w7 = add4(SIGMA1_256(w5), w0, SIGMA0_256(w8), w7);
  310. SHA256ROUND(b, c, d, e, f, g, h, a, 23, w7);
  311. w8 = add4(SIGMA1_256(w6), w1, SIGMA0_256(w9), w8);
  312. SHA256ROUND(a, b, c, d, e, f, g, h, 24, w8);
  313. w9 = add4(SIGMA1_256(w7), w2, SIGMA0_256(w10), w9);
  314. SHA256ROUND(h, a, b, c, d, e, f, g, 25, w9);
  315. w10 = add4(SIGMA1_256(w8), w3, SIGMA0_256(w11), w10);
  316. SHA256ROUND(g, h, a, b, c, d, e, f, 26, w10);
  317. w11 = add4(SIGMA1_256(w9), w4, SIGMA0_256(w12), w11);
  318. SHA256ROUND(f, g, h, a, b, c, d, e, 27, w11);
  319. w12 = add4(SIGMA1_256(w10), w5, SIGMA0_256(w13), w12);
  320. SHA256ROUND(e, f, g, h, a, b, c, d, 28, w12);
  321. w13 = add4(SIGMA1_256(w11), w6, SIGMA0_256(w14), w13);
  322. SHA256ROUND(d, e, f, g, h, a, b, c, 29, w13);
  323. w14 = add4(SIGMA1_256(w12), w7, SIGMA0_256(w15), w14);
  324. SHA256ROUND(c, d, e, f, g, h, a, b, 30, w14);
  325. w15 = add4(SIGMA1_256(w13), w8, SIGMA0_256(w0), w15);
  326. SHA256ROUND(b, c, d, e, f, g, h, a, 31, w15);
  327. w0 = add4(SIGMA1_256(w14), w9, SIGMA0_256(w1), w0);
  328. SHA256ROUND(a, b, c, d, e, f, g, h, 32, w0);
  329. w1 = add4(SIGMA1_256(w15), w10, SIGMA0_256(w2), w1);
  330. SHA256ROUND(h, a, b, c, d, e, f, g, 33, w1);
  331. w2 = add4(SIGMA1_256(w0), w11, SIGMA0_256(w3), w2);
  332. SHA256ROUND(g, h, a, b, c, d, e, f, 34, w2);
  333. w3 = add4(SIGMA1_256(w1), w12, SIGMA0_256(w4), w3);
  334. SHA256ROUND(f, g, h, a, b, c, d, e, 35, w3);
  335. w4 = add4(SIGMA1_256(w2), w13, SIGMA0_256(w5), w4);
  336. SHA256ROUND(e, f, g, h, a, b, c, d, 36, w4);
  337. w5 = add4(SIGMA1_256(w3), w14, SIGMA0_256(w6), w5);
  338. SHA256ROUND(d, e, f, g, h, a, b, c, 37, w5);
  339. w6 = add4(SIGMA1_256(w4), w15, SIGMA0_256(w7), w6);
  340. SHA256ROUND(c, d, e, f, g, h, a, b, 38, w6);
  341. w7 = add4(SIGMA1_256(w5), w0, SIGMA0_256(w8), w7);
  342. SHA256ROUND(b, c, d, e, f, g, h, a, 39, w7);
  343. w8 = add4(SIGMA1_256(w6), w1, SIGMA0_256(w9), w8);
  344. SHA256ROUND(a, b, c, d, e, f, g, h, 40, w8);
  345. w9 = add4(SIGMA1_256(w7), w2, SIGMA0_256(w10), w9);
  346. SHA256ROUND(h, a, b, c, d, e, f, g, 41, w9);
  347. w10 = add4(SIGMA1_256(w8), w3, SIGMA0_256(w11), w10);
  348. SHA256ROUND(g, h, a, b, c, d, e, f, 42, w10);
  349. w11 = add4(SIGMA1_256(w9), w4, SIGMA0_256(w12), w11);
  350. SHA256ROUND(f, g, h, a, b, c, d, e, 43, w11);
  351. w12 = add4(SIGMA1_256(w10), w5, SIGMA0_256(w13), w12);
  352. SHA256ROUND(e, f, g, h, a, b, c, d, 44, w12);
  353. w13 = add4(SIGMA1_256(w11), w6, SIGMA0_256(w14), w13);
  354. SHA256ROUND(d, e, f, g, h, a, b, c, 45, w13);
  355. w14 = add4(SIGMA1_256(w12), w7, SIGMA0_256(w15), w14);
  356. SHA256ROUND(c, d, e, f, g, h, a, b, 46, w14);
  357. w15 = add4(SIGMA1_256(w13), w8, SIGMA0_256(w0), w15);
  358. SHA256ROUND(b, c, d, e, f, g, h, a, 47, w15);
  359. w0 = add4(SIGMA1_256(w14), w9, SIGMA0_256(w1), w0);
  360. SHA256ROUND(a, b, c, d, e, f, g, h, 48, w0);
  361. w1 = add4(SIGMA1_256(w15), w10, SIGMA0_256(w2), w1);
  362. SHA256ROUND(h, a, b, c, d, e, f, g, 49, w1);
  363. w2 = add4(SIGMA1_256(w0), w11, SIGMA0_256(w3), w2);
  364. SHA256ROUND(g, h, a, b, c, d, e, f, 50, w2);
  365. w3 = add4(SIGMA1_256(w1), w12, SIGMA0_256(w4), w3);
  366. SHA256ROUND(f, g, h, a, b, c, d, e, 51, w3);
  367. w4 = add4(SIGMA1_256(w2), w13, SIGMA0_256(w5), w4);
  368. SHA256ROUND(e, f, g, h, a, b, c, d, 52, w4);
  369. w5 = add4(SIGMA1_256(w3), w14, SIGMA0_256(w6), w5);
  370. SHA256ROUND(d, e, f, g, h, a, b, c, 53, w5);
  371. w6 = add4(SIGMA1_256(w4), w15, SIGMA0_256(w7), w6);
  372. SHA256ROUND(c, d, e, f, g, h, a, b, 54, w6);
  373. w7 = add4(SIGMA1_256(w5), w0, SIGMA0_256(w8), w7);
  374. SHA256ROUND(b, c, d, e, f, g, h, a, 55, w7);
  375. w8 = add4(SIGMA1_256(w6), w1, SIGMA0_256(w9), w8);
  376. SHA256ROUND(a, b, c, d, e, f, g, h, 56, w8);
  377. w9 = add4(SIGMA1_256(w7), w2, SIGMA0_256(w10), w9);
  378. SHA256ROUND(h, a, b, c, d, e, f, g, 57, w9);
  379. w10 = add4(SIGMA1_256(w8), w3, SIGMA0_256(w11), w10);
  380. SHA256ROUND(g, h, a, b, c, d, e, f, 58, w10);
  381. w11 = add4(SIGMA1_256(w9), w4, SIGMA0_256(w12), w11);
  382. SHA256ROUND(f, g, h, a, b, c, d, e, 59, w11);
  383. w12 = add4(SIGMA1_256(w10), w5, SIGMA0_256(w13), w12);
  384. SHA256ROUND(e, f, g, h, a, b, c, d, 60, w12);
  385. /* Skip last 3-rounds; not necessary for H==0 */
  386. /*#if 0
  387. w13 = add4(SIGMA1_256(w11), w6, SIGMA0_256(w14), w13);
  388. SHA256ROUND(d, e, f, g, h, a, b, c, 61, w13);
  389. w14 = add4(SIGMA1_256(w12), w7, SIGMA0_256(w15), w14);
  390. SHA256ROUND(c, d, e, f, g, h, a, b, 62, w14);
  391. w15 = add4(SIGMA1_256(w13), w8, SIGMA0_256(w0), w15);
  392. SHA256ROUND(b, c, d, e, f, g, h, a, 63, w15);
  393. #endif*/
  394. /* store resulsts directly in thash */
  395. #define store_2(x,i) \
  396. w0 = (vector unsigned int)(hInit[i],hInit[i],hInit[i],hInit[i]); \
  397. vec_st(vec_add(w0, x), 0 ,&thash[i][k]);
  398. store_2(a, 0);
  399. store_2(b, 1);
  400. store_2(c, 2);
  401. store_2(d, 3);
  402. store_2(e, 4);
  403. store_2(f, 5);
  404. store_2(g, 6);
  405. store_2(h, 7);
  406. vec_st(nonce, 0 ,&thash[8][k]);
  407. /* writing the results into the array is time intensive */
  408. /* -> try if it´s faster to compare the results with the target inside this function */
  409. }
  410. }
  411. #endif /* WANT_ALTIVEC_4WAY */