findnonce.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright 2011-2012 Con Kolivas
  3. * Copyright 2012 Luke Dashjr
  4. * Copyright 2011 Nils Schneider
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. #include "config.h"
  12. #ifdef HAVE_OPENCL
  13. #include <stdio.h>
  14. #include <inttypes.h>
  15. #include <pthread.h>
  16. #include <string.h>
  17. #include "findnonce.h"
  18. #include "miner.h"
  19. #include "scrypt.h"
  20. const uint32_t SHA256_K[64] = {
  21. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  22. 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  23. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  24. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  25. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  26. 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  27. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  28. 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  29. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  30. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  31. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  32. 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  33. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  34. 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  35. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  36. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  37. };
  38. #define rotate(x,y) ((x<<y) | (x>>(sizeof(x)*8-y)))
  39. #define rotr(x,y) ((x>>y) | (x<<(sizeof(x)*8-y)))
  40. #define R(a, b, c, d, e, f, g, h, w, k) \
  41. h = h + (rotate(e, 26) ^ rotate(e, 21) ^ rotate(e, 7)) + (g ^ (e & (f ^ g))) + k + w; \
  42. d = d + h; \
  43. h = h + (rotate(a, 30) ^ rotate(a, 19) ^ rotate(a, 10)) + ((a & b) | (c & (a | b)))
  44. void precalc_hash(dev_blk_ctx *blk, uint32_t *state, uint32_t *data)
  45. {
  46. cl_uint A, B, C, D, E, F, G, H;
  47. A = state[0];
  48. B = state[1];
  49. C = state[2];
  50. D = state[3];
  51. E = state[4];
  52. F = state[5];
  53. G = state[6];
  54. H = state[7];
  55. R(A, B, C, D, E, F, G, H, data[0], SHA256_K[0]);
  56. R(H, A, B, C, D, E, F, G, data[1], SHA256_K[1]);
  57. R(G, H, A, B, C, D, E, F, data[2], SHA256_K[2]);
  58. blk->cty_a = A;
  59. blk->cty_b = B;
  60. blk->cty_c = C;
  61. blk->cty_d = D;
  62. blk->D1A = D + 0xb956c25b;
  63. blk->cty_e = E;
  64. blk->cty_f = F;
  65. blk->cty_g = G;
  66. blk->cty_h = H;
  67. blk->ctx_a = state[0];
  68. blk->ctx_b = state[1];
  69. blk->ctx_c = state[2];
  70. blk->ctx_d = state[3];
  71. blk->ctx_e = state[4];
  72. blk->ctx_f = state[5];
  73. blk->ctx_g = state[6];
  74. blk->ctx_h = state[7];
  75. blk->merkle = data[0];
  76. blk->ntime = data[1];
  77. blk->nbits = data[2];
  78. blk->W16 = blk->fW0 = data[0] + (rotr(data[1], 7) ^ rotr(data[1], 18) ^ (data[1] >> 3));
  79. blk->W17 = blk->fW1 = data[1] + (rotr(data[2], 7) ^ rotr(data[2], 18) ^ (data[2] >> 3)) + 0x01100000;
  80. blk->PreVal4 = blk->fcty_e = blk->ctx_e + (rotr(B, 6) ^ rotr(B, 11) ^ rotr(B, 25)) + (D ^ (B & (C ^ D))) + 0xe9b5dba5;
  81. blk->T1 = blk->fcty_e2 = (rotr(F, 2) ^ rotr(F, 13) ^ rotr(F, 22)) + ((F & G) | (H & (F | G)));
  82. blk->PreVal4_2 = blk->PreVal4 + blk->T1;
  83. blk->PreVal0 = blk->PreVal4 + blk->ctx_a;
  84. blk->PreW31 = 0x00000280 + (rotr(blk->W16, 7) ^ rotr(blk->W16, 18) ^ (blk->W16 >> 3));
  85. blk->PreW32 = blk->W16 + (rotr(blk->W17, 7) ^ rotr(blk->W17, 18) ^ (blk->W17 >> 3));
  86. blk->PreW18 = data[2] + (rotr(blk->W16, 17) ^ rotr(blk->W16, 19) ^ (blk->W16 >> 10));
  87. blk->PreW19 = 0x11002000 + (rotr(blk->W17, 17) ^ rotr(blk->W17, 19) ^ (blk->W17 >> 10));
  88. blk->W2 = data[2];
  89. blk->W2A = blk->W2 + (rotr(blk->W16, 19) ^ rotr(blk->W16, 17) ^ (blk->W16 >> 10));
  90. blk->W17_2 = 0x11002000 + (rotr(blk->W17, 19) ^ rotr(blk->W17, 17) ^ (blk->W17 >> 10));
  91. blk->fW2 = data[2] + (rotr(blk->fW0, 17) ^ rotr(blk->fW0, 19) ^ (blk->fW0 >> 10));
  92. blk->fW3 = 0x11002000 + (rotr(blk->fW1, 17) ^ rotr(blk->fW1, 19) ^ (blk->fW1 >> 10));
  93. blk->fW15 = 0x00000280 + (rotr(blk->fW0, 7) ^ rotr(blk->fW0, 18) ^ (blk->fW0 >> 3));
  94. blk->fW01r = blk->fW0 + (rotr(blk->fW1, 7) ^ rotr(blk->fW1, 18) ^ (blk->fW1 >> 3));
  95. blk->PreVal4addT1 = blk->PreVal4 + blk->T1;
  96. blk->T1substate0 = blk->ctx_a - blk->T1;
  97. blk->C1addK5 = blk->cty_c + SHA256_K[5];
  98. blk->B1addK6 = blk->cty_b + SHA256_K[6];
  99. blk->PreVal0addK7 = blk->PreVal0 + SHA256_K[7];
  100. blk->W16addK16 = blk->W16 + SHA256_K[16];
  101. blk->W17addK17 = blk->W17 + SHA256_K[17];
  102. blk->zeroA = blk->ctx_a + 0x98c7e2a2;
  103. blk->zeroB = blk->ctx_a + 0xfc08884d;
  104. blk->oneA = blk->ctx_b + 0x90bb1e3c;
  105. blk->twoA = blk->ctx_c + 0x50c6645b;
  106. blk->threeA = blk->ctx_d + 0x3ac42e24;
  107. blk->fourA = blk->ctx_e + SHA256_K[4];
  108. blk->fiveA = blk->ctx_f + SHA256_K[5];
  109. blk->sixA = blk->ctx_g + SHA256_K[6];
  110. blk->sevenA = blk->ctx_h + SHA256_K[7];
  111. }
  112. struct pc_data {
  113. struct thr_info *thr;
  114. struct work work;
  115. uint32_t res[MAXBUFFERS];
  116. pthread_t pth;
  117. int found;
  118. };
  119. static void *postcalc_hash(void *userdata)
  120. {
  121. struct pc_data *pcd = (struct pc_data *)userdata;
  122. struct thr_info *thr = pcd->thr;
  123. unsigned int entry = 0;
  124. pthread_detach(pthread_self());
  125. RenameThread("postcalchsh");
  126. /* To prevent corrupt values in FOUND from trying to read beyond the
  127. * end of the res[] array */
  128. if (unlikely(pcd->res[FOUND] & ~FOUND)) {
  129. applog(LOG_WARNING, "%"PRIpreprv": invalid nonce count - HW error",
  130. thr->cgpu->proc_repr);
  131. hw_errors++;
  132. thr->cgpu->hw_errors++;
  133. pcd->res[FOUND] &= FOUND;
  134. }
  135. for (entry = 0; entry < pcd->res[FOUND]; entry++) {
  136. uint32_t nonce = pcd->res[entry];
  137. applog(LOG_DEBUG, "OCL NONCE %u found in slot %d", nonce, entry);
  138. submit_nonce(thr, &pcd->work, nonce);
  139. }
  140. free(pcd);
  141. return NULL;
  142. }
  143. void postcalc_hash_async(struct thr_info *thr, struct work *work, uint32_t *res)
  144. {
  145. struct pc_data *pcd = malloc(sizeof(struct pc_data));
  146. if (unlikely(!pcd)) {
  147. applog(LOG_ERR, "Failed to malloc pc_data in postcalc_hash_async");
  148. return;
  149. }
  150. *pcd = (struct pc_data){
  151. .thr = thr,
  152. };
  153. __copy_work(&pcd->work, work);
  154. memcpy(&pcd->res, res, BUFFERSIZE);
  155. if (pthread_create(&pcd->pth, NULL, postcalc_hash, (void *)pcd)) {
  156. applog(LOG_ERR, "Failed to create postcalc_hash thread");
  157. return;
  158. }
  159. }
  160. #endif /* HAVE_OPENCL */