sha256_cryptopp.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include <stdint.h>
  2. #include <stdbool.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include "miner.h"
  7. typedef uint32_t word32;
  8. static word32 rotrFixed(word32 word, unsigned int shift)
  9. {
  10. return (word >> shift) | (word << (32 - shift));
  11. }
  12. #define blk0(i) (W[i] = data[i])
  13. static const word32 SHA256_K[64] = {
  14. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  15. 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  16. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  17. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  18. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  19. 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  20. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  21. 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  22. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  23. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  24. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  25. 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  26. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  27. 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  28. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  29. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  30. };
  31. #define blk2(i) (W[i&15]+=s1(W[(i-2)&15])+W[(i-7)&15]+s0(W[(i-15)&15]))
  32. #define Ch(x,y,z) (z^(x&(y^z)))
  33. #define Maj(x,y,z) (y^((x^y)&(y^z)))
  34. #define a(i) T[(0-i)&7]
  35. #define b(i) T[(1-i)&7]
  36. #define c(i) T[(2-i)&7]
  37. #define d(i) T[(3-i)&7]
  38. #define e(i) T[(4-i)&7]
  39. #define f(i) T[(5-i)&7]
  40. #define g(i) T[(6-i)&7]
  41. #define h(i) T[(7-i)&7]
  42. #define R(i) h(i)+=S1(e(i))+Ch(e(i),f(i),g(i))+SHA256_K[i+j]+(j?blk2(i):blk0(i));\
  43. d(i)+=h(i);h(i)+=S0(a(i))+Maj(a(i),b(i),c(i))
  44. // for SHA256
  45. #define S0(x) (rotrFixed(x,2)^rotrFixed(x,13)^rotrFixed(x,22))
  46. #define S1(x) (rotrFixed(x,6)^rotrFixed(x,11)^rotrFixed(x,25))
  47. #define s0(x) (rotrFixed(x,7)^rotrFixed(x,18)^(x>>3))
  48. #define s1(x) (rotrFixed(x,17)^rotrFixed(x,19)^(x>>10))
  49. static void SHA256_Transform(word32 *state, const word32 *data)
  50. {
  51. word32 W[16];
  52. word32 T[8];
  53. unsigned int j;
  54. /* Copy context->state[] to working vars */
  55. memcpy(T, state, sizeof(T));
  56. /* 64 operations, partially loop unrolled */
  57. for (j=0; j<64; j+=16)
  58. {
  59. R( 0); R( 1); R( 2); R( 3);
  60. R( 4); R( 5); R( 6); R( 7);
  61. R( 8); R( 9); R(10); R(11);
  62. R(12); R(13); R(14); R(15);
  63. }
  64. /* Add the working vars back into context.state[] */
  65. state[0] += a(0);
  66. state[1] += b(0);
  67. state[2] += c(0);
  68. state[3] += d(0);
  69. state[4] += e(0);
  70. state[5] += f(0);
  71. state[6] += g(0);
  72. state[7] += h(0);
  73. }
  74. static void runhash(void *state, const void *input, const void *init)
  75. {
  76. memcpy(state, init, 32);
  77. SHA256_Transform(state, input);
  78. }
  79. /* suspiciously similar to ScanHash* from bitcoin */
  80. bool scanhash_cryptopp(const unsigned char *midstate, unsigned char *data,
  81. unsigned char *hash1, unsigned char *hash,
  82. unsigned long *hashes_done)
  83. {
  84. uint32_t *hash32 = (uint32_t *) hash;
  85. uint32_t *nonce = (uint32_t *)(data + 12);
  86. uint32_t n = 0;
  87. unsigned long stat_ctr = 0;
  88. while (1) {
  89. n++;
  90. *nonce = n;
  91. runhash(hash1, data, midstate);
  92. runhash(hash, hash1, sha256_init_state);
  93. stat_ctr++;
  94. if (hash32[7] == 0) {
  95. char *hexstr;
  96. hexstr = bin2hex(hash, 32);
  97. fprintf(stderr,
  98. "DBG: found zeroes in hash:\n%s\n",
  99. hexstr);
  100. free(hexstr);
  101. *hashes_done = stat_ctr;
  102. return true;
  103. }
  104. if ((n & 0xffffff) == 0) {
  105. if (opt_debug)
  106. fprintf(stderr, "DBG: end of nonce range\n");
  107. *hashes_done = stat_ctr;
  108. return false;
  109. }
  110. }
  111. }