sha256_altivec_4way.c 21 KB

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