scrypt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*-
  2. * Copyright 2009 Colin Percival, 2011 ArtForz
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. * SUCH DAMAGE.
  25. *
  26. * This file was originally written by Colin Percival as part of the Tarsnap
  27. * online backup system.
  28. */
  29. #include "config.h"
  30. #include "miner.h"
  31. #include <stdlib.h>
  32. #include <stdint.h>
  33. #include <string.h>
  34. typedef struct SHA256Context {
  35. uint32_t state[8];
  36. uint32_t buf[16];
  37. } SHA256_CTX;
  38. /*
  39. * Encode a length len/4 vector of (uint32_t) into a length len vector of
  40. * (unsigned char) in big-endian form. Assumes len is a multiple of 4.
  41. */
  42. static inline void
  43. be32enc_vect(uint32_t *dst, const uint32_t *src, uint32_t len)
  44. {
  45. uint32_t i;
  46. for (i = 0; i < len; i++)
  47. dst[i] = htobe32(src[i]);
  48. }
  49. /* Elementary functions used by SHA256 */
  50. #define Ch(x, y, z) ((x & (y ^ z)) ^ z)
  51. #define Maj(x, y, z) ((x & (y | z)) | (y & z))
  52. #define SHR(x, n) (x >> n)
  53. #define ROTR(x, n) ((x >> n) | (x << (32 - n)))
  54. #define S0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
  55. #define S1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
  56. #define s0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHR(x, 3))
  57. #define s1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHR(x, 10))
  58. /* SHA256 round function */
  59. #define RND(a, b, c, d, e, f, g, h, k) \
  60. t0 = h + S1(e) + Ch(e, f, g) + k; \
  61. t1 = S0(a) + Maj(a, b, c); \
  62. d += t0; \
  63. h = t0 + t1;
  64. /* Adjusted round function for rotating state */
  65. #define RNDr(S, W, i, k) \
  66. RND(S[(64 - i) % 8], S[(65 - i) % 8], \
  67. S[(66 - i) % 8], S[(67 - i) % 8], \
  68. S[(68 - i) % 8], S[(69 - i) % 8], \
  69. S[(70 - i) % 8], S[(71 - i) % 8], \
  70. W[i] + k)
  71. /*
  72. * SHA256 block compression function. The 256-bit state is transformed via
  73. * the 512-bit input block to produce a new state.
  74. */
  75. static void
  76. SHA256_Transform(uint32_t * state, const uint32_t block[16], int swap)
  77. {
  78. uint32_t W[64];
  79. uint32_t S[8];
  80. uint32_t t0, t1;
  81. int i;
  82. /* 1. Prepare message schedule W. */
  83. if(swap)
  84. for (i = 0; i < 16; i++)
  85. W[i] = htobe32(block[i]);
  86. else
  87. memcpy(W, block, 64);
  88. for (i = 16; i < 64; i += 2) {
  89. W[i] = s1(W[i - 2]) + W[i - 7] + s0(W[i - 15]) + W[i - 16];
  90. W[i+1] = s1(W[i - 1]) + W[i - 6] + s0(W[i - 14]) + W[i - 15];
  91. }
  92. /* 2. Initialize working variables. */
  93. memcpy(S, state, 32);
  94. /* 3. Mix. */
  95. RNDr(S, W, 0, 0x428a2f98);
  96. RNDr(S, W, 1, 0x71374491);
  97. RNDr(S, W, 2, 0xb5c0fbcf);
  98. RNDr(S, W, 3, 0xe9b5dba5);
  99. RNDr(S, W, 4, 0x3956c25b);
  100. RNDr(S, W, 5, 0x59f111f1);
  101. RNDr(S, W, 6, 0x923f82a4);
  102. RNDr(S, W, 7, 0xab1c5ed5);
  103. RNDr(S, W, 8, 0xd807aa98);
  104. RNDr(S, W, 9, 0x12835b01);
  105. RNDr(S, W, 10, 0x243185be);
  106. RNDr(S, W, 11, 0x550c7dc3);
  107. RNDr(S, W, 12, 0x72be5d74);
  108. RNDr(S, W, 13, 0x80deb1fe);
  109. RNDr(S, W, 14, 0x9bdc06a7);
  110. RNDr(S, W, 15, 0xc19bf174);
  111. RNDr(S, W, 16, 0xe49b69c1);
  112. RNDr(S, W, 17, 0xefbe4786);
  113. RNDr(S, W, 18, 0x0fc19dc6);
  114. RNDr(S, W, 19, 0x240ca1cc);
  115. RNDr(S, W, 20, 0x2de92c6f);
  116. RNDr(S, W, 21, 0x4a7484aa);
  117. RNDr(S, W, 22, 0x5cb0a9dc);
  118. RNDr(S, W, 23, 0x76f988da);
  119. RNDr(S, W, 24, 0x983e5152);
  120. RNDr(S, W, 25, 0xa831c66d);
  121. RNDr(S, W, 26, 0xb00327c8);
  122. RNDr(S, W, 27, 0xbf597fc7);
  123. RNDr(S, W, 28, 0xc6e00bf3);
  124. RNDr(S, W, 29, 0xd5a79147);
  125. RNDr(S, W, 30, 0x06ca6351);
  126. RNDr(S, W, 31, 0x14292967);
  127. RNDr(S, W, 32, 0x27b70a85);
  128. RNDr(S, W, 33, 0x2e1b2138);
  129. RNDr(S, W, 34, 0x4d2c6dfc);
  130. RNDr(S, W, 35, 0x53380d13);
  131. RNDr(S, W, 36, 0x650a7354);
  132. RNDr(S, W, 37, 0x766a0abb);
  133. RNDr(S, W, 38, 0x81c2c92e);
  134. RNDr(S, W, 39, 0x92722c85);
  135. RNDr(S, W, 40, 0xa2bfe8a1);
  136. RNDr(S, W, 41, 0xa81a664b);
  137. RNDr(S, W, 42, 0xc24b8b70);
  138. RNDr(S, W, 43, 0xc76c51a3);
  139. RNDr(S, W, 44, 0xd192e819);
  140. RNDr(S, W, 45, 0xd6990624);
  141. RNDr(S, W, 46, 0xf40e3585);
  142. RNDr(S, W, 47, 0x106aa070);
  143. RNDr(S, W, 48, 0x19a4c116);
  144. RNDr(S, W, 49, 0x1e376c08);
  145. RNDr(S, W, 50, 0x2748774c);
  146. RNDr(S, W, 51, 0x34b0bcb5);
  147. RNDr(S, W, 52, 0x391c0cb3);
  148. RNDr(S, W, 53, 0x4ed8aa4a);
  149. RNDr(S, W, 54, 0x5b9cca4f);
  150. RNDr(S, W, 55, 0x682e6ff3);
  151. RNDr(S, W, 56, 0x748f82ee);
  152. RNDr(S, W, 57, 0x78a5636f);
  153. RNDr(S, W, 58, 0x84c87814);
  154. RNDr(S, W, 59, 0x8cc70208);
  155. RNDr(S, W, 60, 0x90befffa);
  156. RNDr(S, W, 61, 0xa4506ceb);
  157. RNDr(S, W, 62, 0xbef9a3f7);
  158. RNDr(S, W, 63, 0xc67178f2);
  159. /* 4. Mix local working variables into global state */
  160. for (i = 0; i < 8; i++)
  161. state[i] += S[i];
  162. }
  163. static inline void
  164. SHA256_InitState(uint32_t * state)
  165. {
  166. /* Magic initialization constants */
  167. state[0] = 0x6A09E667;
  168. state[1] = 0xBB67AE85;
  169. state[2] = 0x3C6EF372;
  170. state[3] = 0xA54FF53A;
  171. state[4] = 0x510E527F;
  172. state[5] = 0x9B05688C;
  173. state[6] = 0x1F83D9AB;
  174. state[7] = 0x5BE0CD19;
  175. }
  176. static const uint32_t passwdpad[12] = {0x00000080, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x80020000};
  177. static const uint32_t outerpad[8] = {0x80000000, 0, 0, 0, 0, 0, 0, 0x00000300};
  178. /**
  179. * PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen):
  180. * Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and
  181. * write the output to buf. The value dkLen must be at most 32 * (2^32 - 1).
  182. */
  183. static inline void
  184. PBKDF2_SHA256_80_128(const uint32_t * passwd, uint32_t * buf)
  185. {
  186. SHA256_CTX PShictx, PShoctx;
  187. uint32_t tstate[8];
  188. uint32_t ihash[8];
  189. uint32_t i;
  190. uint32_t pad[16];
  191. static const uint32_t innerpad[11] = {0x00000080, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xa0040000};
  192. /* If Klen > 64, the key is really SHA256(K). */
  193. SHA256_InitState(tstate);
  194. SHA256_Transform(tstate, passwd, 1);
  195. memcpy(pad, passwd+16, 16);
  196. memcpy(pad+4, passwdpad, 48);
  197. SHA256_Transform(tstate, pad, 1);
  198. memcpy(ihash, tstate, 32);
  199. SHA256_InitState(PShictx.state);
  200. for (i = 0; i < 8; i++)
  201. pad[i] = ihash[i] ^ 0x36363636;
  202. for (; i < 16; i++)
  203. pad[i] = 0x36363636;
  204. SHA256_Transform(PShictx.state, pad, 0);
  205. SHA256_Transform(PShictx.state, passwd, 1);
  206. be32enc_vect(PShictx.buf, passwd+16, 4);
  207. be32enc_vect(PShictx.buf+5, innerpad, 11);
  208. SHA256_InitState(PShoctx.state);
  209. for (i = 0; i < 8; i++)
  210. pad[i] = ihash[i] ^ 0x5c5c5c5c;
  211. for (; i < 16; i++)
  212. pad[i] = 0x5c5c5c5c;
  213. SHA256_Transform(PShoctx.state, pad, 0);
  214. memcpy(PShoctx.buf+8, outerpad, 32);
  215. /* Iterate through the blocks. */
  216. for (i = 0; i < 4; i++) {
  217. uint32_t istate[8];
  218. uint32_t ostate[8];
  219. memcpy(istate, PShictx.state, 32);
  220. PShictx.buf[4] = i + 1;
  221. SHA256_Transform(istate, PShictx.buf, 0);
  222. memcpy(PShoctx.buf, istate, 32);
  223. memcpy(ostate, PShoctx.state, 32);
  224. SHA256_Transform(ostate, PShoctx.buf, 0);
  225. be32enc_vect(buf+i*8, ostate, 8);
  226. }
  227. }
  228. static inline void
  229. PBKDF2_SHA256_80_128_32(const uint32_t * passwd, const uint32_t * salt, uint32_t *ostate)
  230. {
  231. uint32_t tstate[8];
  232. uint32_t ihash[8];
  233. uint32_t i;
  234. /* Compute HMAC state after processing P and S. */
  235. uint32_t pad[16];
  236. static const uint32_t ihash_finalblk[16] = {0x00000001,0x80000000,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0x00000620};
  237. /* If Klen > 64, the key is really SHA256(K). */
  238. SHA256_InitState(tstate);
  239. SHA256_Transform(tstate, passwd, 1);
  240. memcpy(pad, passwd+16, 16);
  241. memcpy(pad+4, passwdpad, 48);
  242. SHA256_Transform(tstate, pad, 1);
  243. memcpy(ihash, tstate, 32);
  244. SHA256_InitState(ostate);
  245. for (i = 0; i < 8; i++)
  246. pad[i] = ihash[i] ^ 0x5c5c5c5c;
  247. for (; i < 16; i++)
  248. pad[i] = 0x5c5c5c5c;
  249. SHA256_Transform(ostate, pad, 0);
  250. SHA256_InitState(tstate);
  251. for (i = 0; i < 8; i++)
  252. pad[i] = ihash[i] ^ 0x36363636;
  253. for (; i < 16; i++)
  254. pad[i] = 0x36363636;
  255. SHA256_Transform(tstate, pad, 0);
  256. SHA256_Transform(tstate, salt, 1);
  257. SHA256_Transform(tstate, salt+16, 1);
  258. SHA256_Transform(tstate, ihash_finalblk, 0);
  259. memcpy(pad, tstate, 32);
  260. memcpy(pad+8, outerpad, 32);
  261. /* Feed the inner hash to the outer SHA256 operation. */
  262. SHA256_Transform(ostate, pad, 0);
  263. }
  264. /**
  265. * salsa20_8(B):
  266. * Apply the salsa20/8 core to the provided block.
  267. */
  268. static inline void
  269. salsa20_8(uint32_t B[16], const uint32_t Bx[16])
  270. {
  271. uint32_t x00,x01,x02,x03,x04,x05,x06,x07,x08,x09,x10,x11,x12,x13,x14,x15;
  272. size_t i;
  273. x00 = (B[ 0] ^= Bx[ 0]);
  274. x01 = (B[ 1] ^= Bx[ 1]);
  275. x02 = (B[ 2] ^= Bx[ 2]);
  276. x03 = (B[ 3] ^= Bx[ 3]);
  277. x04 = (B[ 4] ^= Bx[ 4]);
  278. x05 = (B[ 5] ^= Bx[ 5]);
  279. x06 = (B[ 6] ^= Bx[ 6]);
  280. x07 = (B[ 7] ^= Bx[ 7]);
  281. x08 = (B[ 8] ^= Bx[ 8]);
  282. x09 = (B[ 9] ^= Bx[ 9]);
  283. x10 = (B[10] ^= Bx[10]);
  284. x11 = (B[11] ^= Bx[11]);
  285. x12 = (B[12] ^= Bx[12]);
  286. x13 = (B[13] ^= Bx[13]);
  287. x14 = (B[14] ^= Bx[14]);
  288. x15 = (B[15] ^= Bx[15]);
  289. for (i = 0; i < 8; i += 2) {
  290. #define R(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
  291. /* Operate on columns. */
  292. x04 ^= R(x00+x12, 7); x09 ^= R(x05+x01, 7); x14 ^= R(x10+x06, 7); x03 ^= R(x15+x11, 7);
  293. x08 ^= R(x04+x00, 9); x13 ^= R(x09+x05, 9); x02 ^= R(x14+x10, 9); x07 ^= R(x03+x15, 9);
  294. x12 ^= R(x08+x04,13); x01 ^= R(x13+x09,13); x06 ^= R(x02+x14,13); x11 ^= R(x07+x03,13);
  295. x00 ^= R(x12+x08,18); x05 ^= R(x01+x13,18); x10 ^= R(x06+x02,18); x15 ^= R(x11+x07,18);
  296. /* Operate on rows. */
  297. x01 ^= R(x00+x03, 7); x06 ^= R(x05+x04, 7); x11 ^= R(x10+x09, 7); x12 ^= R(x15+x14, 7);
  298. x02 ^= R(x01+x00, 9); x07 ^= R(x06+x05, 9); x08 ^= R(x11+x10, 9); x13 ^= R(x12+x15, 9);
  299. x03 ^= R(x02+x01,13); x04 ^= R(x07+x06,13); x09 ^= R(x08+x11,13); x14 ^= R(x13+x12,13);
  300. x00 ^= R(x03+x02,18); x05 ^= R(x04+x07,18); x10 ^= R(x09+x08,18); x15 ^= R(x14+x13,18);
  301. #undef R
  302. }
  303. B[ 0] += x00;
  304. B[ 1] += x01;
  305. B[ 2] += x02;
  306. B[ 3] += x03;
  307. B[ 4] += x04;
  308. B[ 5] += x05;
  309. B[ 6] += x06;
  310. B[ 7] += x07;
  311. B[ 8] += x08;
  312. B[ 9] += x09;
  313. B[10] += x10;
  314. B[11] += x11;
  315. B[12] += x12;
  316. B[13] += x13;
  317. B[14] += x14;
  318. B[15] += x15;
  319. }
  320. /* cpu and memory intensive function to transform a 80 byte buffer into a 32 byte output
  321. scratchpad size needs to be at least 63 + (128 * r * p) + (256 * r + 64) + (128 * r * N) bytes
  322. */
  323. static void scrypt_1024_1_1_256_sp(const uint32_t* input, char* scratchpad, uint32_t *ostate)
  324. {
  325. uint32_t * V;
  326. uint32_t X[32];
  327. uint32_t i;
  328. uint32_t j;
  329. uint32_t k;
  330. uint64_t *p1, *p2;
  331. p1 = (uint64_t *)X;
  332. V = (uint32_t *)(((uintptr_t)(scratchpad) + 63) & ~ (uintptr_t)(63));
  333. PBKDF2_SHA256_80_128(input, X);
  334. for (i = 0; i < 1024; i += 2) {
  335. memcpy(&V[i * 32], X, 128);
  336. salsa20_8(&X[0], &X[16]);
  337. salsa20_8(&X[16], &X[0]);
  338. memcpy(&V[(i + 1) * 32], X, 128);
  339. salsa20_8(&X[0], &X[16]);
  340. salsa20_8(&X[16], &X[0]);
  341. }
  342. for (i = 0; i < 1024; i += 2) {
  343. j = X[16] & 1023;
  344. p2 = (uint64_t *)(&V[j * 32]);
  345. for(k = 0; k < 16; k++)
  346. p1[k] ^= p2[k];
  347. salsa20_8(&X[0], &X[16]);
  348. salsa20_8(&X[16], &X[0]);
  349. j = X[16] & 1023;
  350. p2 = (uint64_t *)(&V[j * 32]);
  351. for(k = 0; k < 16; k++)
  352. p1[k] ^= p2[k];
  353. salsa20_8(&X[0], &X[16]);
  354. salsa20_8(&X[16], &X[0]);
  355. }
  356. PBKDF2_SHA256_80_128_32(input, X, ostate);
  357. }
  358. /* 131583 rounded up to 4 byte alignment */
  359. #define SCRATCHBUF_SIZE (131584)
  360. void scrypt_regenhash(struct work *work)
  361. {
  362. uint32_t data[20];
  363. char *scratchbuf;
  364. uint32_t *nonce = (uint32_t *)(work->data + 76);
  365. uint32_t *ohash = (uint32_t *)(work->hash);
  366. be32enc_vect(data, (const uint32_t *)work->data, 19);
  367. data[19] = htobe32(*nonce);
  368. scratchbuf = alloca(SCRATCHBUF_SIZE);
  369. scrypt_1024_1_1_256_sp(data, scratchbuf, ohash);
  370. flip32(ohash, ohash);
  371. }
  372. static const uint32_t diff1targ = 0x0000ffff;
  373. /* Used externally as confirmation of correct OCL code */
  374. int scrypt_test(unsigned char *pdata, const unsigned char *ptarget, uint32_t nonce)
  375. {
  376. uint32_t tmp_hash7, Htarg = le32toh(((const uint32_t *)ptarget)[7]);
  377. uint32_t data[20], ohash[8];
  378. char *scratchbuf;
  379. be32enc_vect(data, (const uint32_t *)pdata, 19);
  380. data[19] = htobe32(nonce);
  381. scratchbuf = alloca(SCRATCHBUF_SIZE);
  382. scrypt_1024_1_1_256_sp(data, scratchbuf, ohash);
  383. tmp_hash7 = be32toh(ohash[7]);
  384. applog(LOG_DEBUG, "htarget %08lx diff1 %08lx hash %08lx",
  385. (long unsigned int)Htarg,
  386. (long unsigned int)diff1targ,
  387. (long unsigned int)tmp_hash7);
  388. if (tmp_hash7 > diff1targ)
  389. return -1;
  390. if (tmp_hash7 > Htarg)
  391. return 0;
  392. return 1;
  393. }
  394. bool scanhash_scrypt(struct thr_info *thr, const unsigned char __maybe_unused *pmidstate,
  395. unsigned char *pdata, unsigned char __maybe_unused *phash1,
  396. unsigned char __maybe_unused *phash, const unsigned char *ptarget,
  397. uint32_t max_nonce, uint32_t *last_nonce, uint32_t n)
  398. {
  399. uint32_t *nonce = (uint32_t *)(pdata + 76);
  400. char *scratchbuf;
  401. uint32_t data[20];
  402. uint32_t tmp_hash7;
  403. uint32_t Htarg = le32toh(((const uint32_t *)ptarget)[7]);
  404. bool ret = false;
  405. be32enc_vect(data, (const uint32_t *)pdata, 19);
  406. scratchbuf = malloc(SCRATCHBUF_SIZE);
  407. if (unlikely(!scratchbuf)) {
  408. applog(LOG_ERR, "Failed to malloc scratchbuf in scanhash_scrypt");
  409. return ret;
  410. }
  411. while(1) {
  412. uint32_t ostate[8];
  413. *nonce = ++n;
  414. data[19] = htobe32(n);
  415. scrypt_1024_1_1_256_sp(data, scratchbuf, ostate);
  416. tmp_hash7 = be32toh(ostate[7]);
  417. if (unlikely(tmp_hash7 <= Htarg)) {
  418. ((uint32_t *)pdata)[19] = htobe32(n);
  419. *last_nonce = n;
  420. ret = true;
  421. break;
  422. }
  423. if (unlikely((n >= max_nonce) || thr->work_restart)) {
  424. *last_nonce = n;
  425. break;
  426. }
  427. }
  428. free(scratchbuf);;
  429. return ret;
  430. }