findnonce.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright 2011 Con Kolivas
  3. * Copyright 2011 Nils Schneider
  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. #include "config.h"
  11. #ifdef HAVE_OPENCL
  12. #include <stdio.h>
  13. #include <inttypes.h>
  14. #include <pthread.h>
  15. #include <string.h>
  16. #include "ocl.h"
  17. #include "findnonce.h"
  18. #include "miner.h"
  19. const uint32_t SHA256_K[64] = {
  20. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  21. 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  22. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  23. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  24. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  25. 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  26. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  27. 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  28. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  29. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  30. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  31. 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  32. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  33. 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  34. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  35. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  36. };
  37. inline uint32_t ByteReverse(uint32_t value)
  38. {
  39. __asm__ ("bswap %0" : "=r" (value) : "0" (value));
  40. return value;
  41. }
  42. #define rotate(x,y) ((x<<y) | (x>>(sizeof(x)*8-y)))
  43. #define rotr(x,y) ((x>>y) | (x<<(sizeof(x)*8-y)))
  44. #define R(a, b, c, d, e, f, g, h, w, k) \
  45. h = h + (rotate(e, 26) ^ rotate(e, 21) ^ rotate(e, 7)) + (g ^ (e & (f ^ g))) + k + w; \
  46. d = d + h; \
  47. h = h + (rotate(a, 30) ^ rotate(a, 19) ^ rotate(a, 10)) + ((a & b) | (c & (a | b)))
  48. void precalc_hash(dev_blk_ctx *blk, uint32_t *state, uint32_t *data) {
  49. cl_uint A, B, C, D, E, F, G, H;
  50. A = state[0];
  51. B = state[1];
  52. C = state[2];
  53. D = state[3];
  54. E = state[4];
  55. F = state[5];
  56. G = state[6];
  57. H = state[7];
  58. R(A, B, C, D, E, F, G, H, data[0], SHA256_K[0]);
  59. R(H, A, B, C, D, E, F, G, data[1], SHA256_K[1]);
  60. R(G, H, A, B, C, D, E, F, data[2], SHA256_K[2]);
  61. blk->cty_a = A;
  62. blk->cty_b = B;
  63. blk->cty_c = C;
  64. blk->cty_d = D;
  65. blk->cty_e = E;
  66. blk->cty_f = F;
  67. blk->cty_g = G;
  68. blk->cty_h = H;
  69. blk->ctx_a = state[0];
  70. blk->ctx_b = state[1];
  71. blk->ctx_c = state[2];
  72. blk->ctx_d = state[3];
  73. blk->ctx_e = state[4];
  74. blk->ctx_f = state[5];
  75. blk->ctx_g = state[6];
  76. blk->ctx_h = state[7];
  77. blk->merkle = data[0];
  78. blk->ntime = data[1];
  79. blk->nbits = data[2];
  80. blk->W16 = blk->fW0 = data[0] + (rotr(data[1], 7) ^ rotr(data[1], 18) ^ (data[1] >> 3));
  81. blk->W17 = blk->fW1 = data[1] + (rotr(data[2], 7) ^ rotr(data[2], 18) ^ (data[2] >> 3)) + 0x01100000;
  82. blk->W2 = data[2];
  83. blk->fW2 = data[2] + (rotr(blk->fW0, 17) ^ rotr(blk->fW0, 19) ^ (blk->fW0 >> 10));
  84. blk->fW3 = 0x11002000 + (rotr(blk->fW1, 17) ^ rotr(blk->fW1, 19) ^ (blk->fW1 >> 10));
  85. blk->fW15 = 0x00000280 + (rotr(blk->fW0, 7) ^ rotr(blk->fW0, 18) ^ (blk->fW0 >> 3));
  86. blk->fW01r = blk->fW0 + (rotr(blk->fW1, 7) ^ rotr(blk->fW1, 18) ^ (blk->fW1 >> 3));
  87. blk->PreVal4 = blk->fcty_e = E + (rotr(B, 6) ^ rotr(B, 11) ^ rotr(B, 25)) + (D ^ (B & (C ^ D))) + 0xe9b5dba5;
  88. blk->T1 = blk->fcty_e2 = (rotr(F, 2) ^ rotr(F, 13) ^ rotr(F, 22)) + ((F & G) | (H & (F | G)));
  89. }
  90. #define P(t) (W[(t)&0xF] = W[(t-16)&0xF] + (rotate(W[(t-15)&0xF], 25) ^ rotate(W[(t-15)&0xF], 14) ^ (W[(t-15)&0xF] >> 3)) + W[(t-7)&0xF] + (rotate(W[(t-2)&0xF], 15) ^ rotate(W[(t-2)&0xF], 13) ^ (W[(t-2)&0xF] >> 10)))
  91. #define IR(u) \
  92. R(A, B, C, D, E, F, G, H, W[u+0], SHA256_K[u+0]); \
  93. R(H, A, B, C, D, E, F, G, W[u+1], SHA256_K[u+1]); \
  94. R(G, H, A, B, C, D, E, F, W[u+2], SHA256_K[u+2]); \
  95. R(F, G, H, A, B, C, D, E, W[u+3], SHA256_K[u+3]); \
  96. R(E, F, G, H, A, B, C, D, W[u+4], SHA256_K[u+4]); \
  97. R(D, E, F, G, H, A, B, C, W[u+5], SHA256_K[u+5]); \
  98. R(C, D, E, F, G, H, A, B, W[u+6], SHA256_K[u+6]); \
  99. R(B, C, D, E, F, G, H, A, W[u+7], SHA256_K[u+7])
  100. #define FR(u) \
  101. R(A, B, C, D, E, F, G, H, P(u+0), SHA256_K[u+0]); \
  102. R(H, A, B, C, D, E, F, G, P(u+1), SHA256_K[u+1]); \
  103. R(G, H, A, B, C, D, E, F, P(u+2), SHA256_K[u+2]); \
  104. R(F, G, H, A, B, C, D, E, P(u+3), SHA256_K[u+3]); \
  105. R(E, F, G, H, A, B, C, D, P(u+4), SHA256_K[u+4]); \
  106. R(D, E, F, G, H, A, B, C, P(u+5), SHA256_K[u+5]); \
  107. R(C, D, E, F, G, H, A, B, P(u+6), SHA256_K[u+6]); \
  108. R(B, C, D, E, F, G, H, A, P(u+7), SHA256_K[u+7])
  109. #define PIR(u) \
  110. R(F, G, H, A, B, C, D, E, W[u+3], SHA256_K[u+3]); \
  111. R(E, F, G, H, A, B, C, D, W[u+4], SHA256_K[u+4]); \
  112. R(D, E, F, G, H, A, B, C, W[u+5], SHA256_K[u+5]); \
  113. R(C, D, E, F, G, H, A, B, W[u+6], SHA256_K[u+6]); \
  114. R(B, C, D, E, F, G, H, A, W[u+7], SHA256_K[u+7])
  115. #define PFR(u) \
  116. R(A, B, C, D, E, F, G, H, P(u+0), SHA256_K[u+0]); \
  117. R(H, A, B, C, D, E, F, G, P(u+1), SHA256_K[u+1]); \
  118. R(G, H, A, B, C, D, E, F, P(u+2), SHA256_K[u+2]); \
  119. R(F, G, H, A, B, C, D, E, P(u+3), SHA256_K[u+3]); \
  120. R(E, F, G, H, A, B, C, D, P(u+4), SHA256_K[u+4]); \
  121. R(D, E, F, G, H, A, B, C, P(u+5), SHA256_K[u+5])
  122. struct pc_data {
  123. struct thr_info *thr;
  124. struct work *work;
  125. uint32_t res[MAXBUFFERS];
  126. pthread_t pth;
  127. };
  128. static void *postcalc_hash(void *userdata)
  129. {
  130. struct pc_data *pcd = (struct pc_data *)userdata;
  131. struct thr_info *thr = pcd->thr;
  132. dev_blk_ctx *blk = &pcd->work->blk;
  133. struct work *work = pcd->work;
  134. uint32_t start = 0;
  135. cl_uint A, B, C, D, E, F, G, H;
  136. cl_uint W[16];
  137. cl_uint nonce;
  138. uint32_t end;
  139. int entry = 0;
  140. cycle:
  141. while (entry < MAXBUFFERS) {
  142. if (pcd->res[entry]) {
  143. start = pcd->res[entry++];
  144. break;
  145. }
  146. entry++;
  147. }
  148. if (entry == MAXBUFFERS)
  149. goto out;
  150. end = start + 1026;
  151. for (nonce = start; nonce != end; nonce+=1) {
  152. A = blk->cty_a; B = blk->cty_b;
  153. C = blk->cty_c; D = blk->cty_d;
  154. E = blk->cty_e; F = blk->cty_f;
  155. G = blk->cty_g; H = blk->cty_h;
  156. W[0] = blk->merkle; W[1] = blk->ntime;
  157. W[2] = blk->nbits; W[3] = nonce;;
  158. W[4] = 0x80000000; W[5] = 0x00000000; W[6] = 0x00000000; W[7] = 0x00000000;
  159. W[8] = 0x00000000; W[9] = 0x00000000; W[10] = 0x00000000; W[11] = 0x00000000;
  160. W[12] = 0x00000000; W[13] = 0x00000000; W[14] = 0x00000000; W[15] = 0x00000280;
  161. PIR(0); IR(8);
  162. FR(16); FR(24);
  163. FR(32); FR(40);
  164. FR(48); FR(56);
  165. W[0] = A + blk->ctx_a; W[1] = B + blk->ctx_b;
  166. W[2] = C + blk->ctx_c; W[3] = D + blk->ctx_d;
  167. W[4] = E + blk->ctx_e; W[5] = F + blk->ctx_f;
  168. W[6] = G + blk->ctx_g; W[7] = H + blk->ctx_h;
  169. W[8] = 0x80000000; W[9] = 0x00000000; W[10] = 0x00000000; W[11] = 0x00000000;
  170. W[12] = 0x00000000; W[13] = 0x00000000; W[14] = 0x00000000; W[15] = 0x00000100;
  171. A = 0x6a09e667; B = 0xbb67ae85;
  172. C = 0x3c6ef372; D = 0xa54ff53a;
  173. E = 0x510e527f; F = 0x9b05688c;
  174. G = 0x1f83d9ab; H = 0x5be0cd19;
  175. IR(0); IR(8);
  176. FR(16); FR(24);
  177. FR(32); FR(40);
  178. FR(48); PFR(56);
  179. if (unlikely(H == 0xA41F32E7)) {
  180. G += 0x1f83d9ab;
  181. G = ByteReverse(G);
  182. if (G < thr->best_g) {
  183. if (unlikely(submit_nonce(thr, work, nonce) == false)) {
  184. applog(LOG_ERR, "Failed to submit work, exiting");
  185. break;
  186. }
  187. thr->best_g = G;
  188. }
  189. }
  190. }
  191. if (unlikely(thr->best_g == ~0)) {
  192. if (opt_debug)
  193. applog(LOG_DEBUG, "No best_g found! Error in OpenCL code?");
  194. hw_errors++;
  195. thr->cgpu->hw_errors++;
  196. }
  197. if (entry < MAXBUFFERS)
  198. goto cycle;
  199. out:
  200. pthread_detach(pthread_self());
  201. free(pcd);
  202. return NULL;
  203. }
  204. void postcalc_hash_async(struct thr_info *thr, struct work *work, uint32_t *res)
  205. {
  206. struct pc_data *pcd = malloc(sizeof(struct pc_data));
  207. if (unlikely(!pcd)) {
  208. applog(LOG_ERR, "Failed to malloc pc_data in postcalc_hash_async");
  209. return;
  210. }
  211. pcd->work = calloc(1, sizeof(struct work));
  212. if (unlikely(!pcd->work)) {
  213. applog(LOG_ERR, "Failed to malloc work in postcalc_hash_async");
  214. return;
  215. }
  216. pcd->thr = thr;
  217. memcpy(pcd->work, work, sizeof(struct work));
  218. memcpy(&pcd->res, res, BUFFERSIZE);
  219. if (pthread_create(&pcd->pth, NULL, postcalc_hash, (void *)pcd)) {
  220. applog(LOG_ERR, "Failed to create postcalc_hash thread");
  221. return;
  222. }
  223. }
  224. #endif /* HAVE_OPENCL */