From 3f12e96292be21b0771079417fd8e213ab727107 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 13 Feb 2014 10:36:35 +0100 Subject: [PATCH 01/15] VS2010 build: Fixed compatibility with non-MSVS compilers. --- ccan/opt/helpers.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ccan/opt/helpers.c b/ccan/opt/helpers.c index 6b7218a0..0cfd3f62 100644 --- a/ccan/opt/helpers.c +++ b/ccan/opt/helpers.c @@ -74,10 +74,10 @@ char *opt_set_floatval(const char *arg, float *f) char *endp; errno = 0; -#if (_MSC_VER >= 1800) - *f = strtof(arg, &endp); -#else +#if defined (_MSC_VER) && (_MSC_VER < 1800) *f = strtod(arg, &endp); +#else + *f = strtof(arg, &endp); #endif if (*endp || !arg[0]) return arg_bad("'%s' is not a number", arg); From 28bde58d66d611bd59c04adc4fb570b510cbffe9 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 13 Feb 2014 18:49:40 +0100 Subject: [PATCH 02/15] VS2010 build: Cosmetic changes in project configuration. --- winbuild/sgminer.vcxproj | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/winbuild/sgminer.vcxproj b/winbuild/sgminer.vcxproj index 7bcb78db..fc95e6c1 100644 --- a/winbuild/sgminer.vcxproj +++ b/winbuild/sgminer.vcxproj @@ -65,22 +65,22 @@ false $(SolutionDir)output\x86\$(Configuration)\ - $(SolutionDir)output\x86\$(Configuration)\obj\ + $(SolutionDir)output\x86\obj\ false $(SolutionDir)output\x64\$(Configuration)\ - $(SolutionDir)output\x64\$(Configuration)\obj\ + $(SolutionDir)output\x64\obj\ false $(SolutionDir)output\x86\$(Configuration)\ - $(SolutionDir)output\x86\$(Configuration)\obj\ + $(SolutionDir)output\x86\obj\ false $(SolutionDir)output\x64\$(Configuration)\ - $(SolutionDir)output\x64\$(Configuration)\obj\ + $(SolutionDir)output\x64\obj\ From 47cb1f7d55c0e460dee72e861a28abe6c362c19d Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 16 Feb 2014 20:28:20 +0100 Subject: [PATCH 03/15] Initial NSCRYPT support commit. --- driver-opencl.c | 53 +- kernel/nscrypt.cl | 982 +++++++++++++++++++++++++++++++++ miner.h | 2 + ocl.c | 9 +- scrypt.c | 29 +- sgminer.c | 7 + util.c | 42 ++ util.h | 2 + winbuild/dist/include/config.h | 1 + 9 files changed, 1106 insertions(+), 21 deletions(-) create mode 100644 kernel/nscrypt.cl diff --git a/driver-opencl.c b/driver-opencl.c index 4c57a3c6..dbfb5697 100644 --- a/driver-opencl.c +++ b/driver-opencl.c @@ -206,6 +206,8 @@ static enum cl_kernels select_kernel(char *arg) return KL_ZUIKKIS; if (!strcmp(arg, PSW_KERNNAME)) return KL_PSW; + if (!strcmp(arg, NSCRYPT_KERNNAME)) + return KL_NSCRYPT; return KL_NONE; } @@ -1019,6 +1021,13 @@ static cl_int queue_scrypt_kernel(_clState *clState, dev_blk_ctx *blk, __maybe_u unsigned int num = 0; cl_uint le_target; cl_int status = 0; + uint32_t timestamp; + cl_uint nfactor = 10; // scrypt default + + if (opt_nscrypt) { + timestamp = bswap_32(*((uint32_t *)(blk->work->data + 17*4))); + nfactor = vert_GetNfactor(timestamp) + 1; + } le_target = *(cl_uint *)(blk->work->device_target + 28); clState->cldata = blk->work->data; @@ -1030,6 +1039,9 @@ static cl_int queue_scrypt_kernel(_clState *clState, dev_blk_ctx *blk, __maybe_u CL_SET_VARG(4, &midstate[0]); CL_SET_VARG(4, &midstate[16]); CL_SET_ARG(le_target); + if (opt_nscrypt) { + CL_SET_ARG(nfactor); + } return status; } @@ -1038,19 +1050,30 @@ static void set_threads_hashes(unsigned int vectors, unsigned int compute_shader unsigned int minthreads, __maybe_unused int *intensity, __maybe_unused int *xintensity, __maybe_unused int *rawintensity) { unsigned int threads = 0; - while (threads < minthreads) { - if (*rawintensity > 0) { - threads = *rawintensity; - } else if (*xintensity > 0) { - threads = compute_shaders * *xintensity; - } else { - threads = 1 << *intensity; - } - if (threads < minthreads) { - if (likely(*intensity < MAX_INTENSITY)) - (*intensity)++; - else - threads = minthreads; + if (opt_nscrypt) { + // new intensity calculation based on shader count + threads = (compute_shaders * minthreads << (MAX_INTENSITY-19)) >> (MAX_INTENSITY - *intensity); + + if (threads < minthreads) + threads = minthreads; + else if (threads % minthreads) + threads += minthreads - (threads % minthreads); + } + else { + while (threads < minthreads) { + if (*rawintensity > 0) { + threads = *rawintensity; + } else if (*xintensity > 0) { + threads = compute_shaders * *xintensity; + } else { + threads = 1 << *intensity; + } + if (threads < minthreads) { + if (likely(*intensity < MAX_INTENSITY)) + (*intensity)++; + else + threads = minthreads; + } } } @@ -1322,6 +1345,9 @@ static bool opencl_thread_prepare(struct thr_info *thr) case KL_PSW: cgpu->kname = PSW_KERNNAME; break; + case KL_NSCRYPT: + cgpu->kname = NSCRYPT_KERNNAME; + break; default: break; } @@ -1355,6 +1381,7 @@ static bool opencl_thread_init(struct thr_info *thr) case KL_CKOLIVAS: case KL_PSW: case KL_ZUIKKIS: + case KL_NSCRYPT: thrdata->queue_kernel_parameters = &queue_scrypt_kernel; break; default: diff --git a/kernel/nscrypt.cl b/kernel/nscrypt.cl new file mode 100644 index 00000000..159abaa2 --- /dev/null +++ b/kernel/nscrypt.cl @@ -0,0 +1,982 @@ +/*- + * Copyright 2009 Colin Percival, 2011 ArtForz, 2011 pooler, 2012 mtrlt, + * 2012-2013 Con Kolivas. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file was originally written by Colin Percival as part of the Tarsnap + * online backup system. + */ + +__constant uint N[] ={ + 0x00000000U, + 0x00000001U, + 0x00000003U, + 0x00000007U, + 0x0000000FU, + 0x0000001FU, + 0x0000003FU, + 0x0000007FU, + 0x000000FFU, + 0x000001FFU, + 0x000003FFU, //2^10 - 1 -> nFactor 1024 + 0x000007FFU, + 0x00000FFFU, + 0x00001FFFU, + 0x00003FFFU, + 0x00007FFFU, + 0x0000FFFFU, + 0x0001FFFFU, + 0x0003FFFFU, + 0x0000FFFFU, + 0x000FFFFFU +}; + +__constant uint K[] = { + 0x428a2f98U, + 0x71374491U, + 0xb5c0fbcfU, + 0xe9b5dba5U, + 0x3956c25bU, + 0x59f111f1U, + 0x923f82a4U, + 0xab1c5ed5U, + 0xd807aa98U, + 0x12835b01U, + 0x243185beU, // 10 + 0x550c7dc3U, + 0x72be5d74U, + 0x80deb1feU, + 0x9bdc06a7U, + 0xe49b69c1U, + 0xefbe4786U, + 0x0fc19dc6U, + 0x240ca1ccU, + 0x2de92c6fU, + 0x4a7484aaU, // 20 + 0x5cb0a9dcU, + 0x76f988daU, + 0x983e5152U, + 0xa831c66dU, + 0xb00327c8U, + 0xbf597fc7U, + 0xc6e00bf3U, + 0xd5a79147U, + 0x06ca6351U, + 0x14292967U, // 30 + 0x27b70a85U, + 0x2e1b2138U, + 0x4d2c6dfcU, + 0x53380d13U, + 0x650a7354U, + 0x766a0abbU, + 0x81c2c92eU, + 0x92722c85U, + 0xa2bfe8a1U, + 0xa81a664bU, // 40 + 0xc24b8b70U, + 0xc76c51a3U, + 0xd192e819U, + 0xd6990624U, + 0xf40e3585U, + 0x106aa070U, + 0x19a4c116U, + 0x1e376c08U, + 0x2748774cU, + 0x34b0bcb5U, // 50 + 0x391c0cb3U, + 0x4ed8aa4aU, + 0x5b9cca4fU, + 0x682e6ff3U, + 0x748f82eeU, + 0x78a5636fU, + 0x84c87814U, + 0x8cc70208U, + 0x90befffaU, + 0xa4506cebU, // 60 + 0xbef9a3f7U, + 0xc67178f2U, + 0x98c7e2a2U, + 0xfc08884dU, + 0xcd2a11aeU, + 0x510e527fU, + 0x9b05688cU, + 0xC3910C8EU, + 0xfb6feee7U, + 0x2a01a605U, // 70 + 0x0c2e12e0U, + 0x4498517BU, + 0x6a09e667U, + 0xa4ce148bU, + 0x95F61999U, + 0xc19bf174U, + 0xBB67AE85U, + 0x3C6EF372U, + 0xA54FF53AU, + 0x1F83D9ABU, // 80 + 0x5BE0CD19U, + 0x5C5C5C5CU, + 0x36363636U, + 0x80000000U, + 0x000007FFU, + 0x00000280U, + 0x000004a0U, + 0x00000300U +}; + +#define rotl(x,y) rotate(x,y) +#define Ch(x,y,z) bitselect(z,y,x) +#define Maj(x,y,z) Ch((x^z),y,z) +#define mod2(x,y) (x & (y-1)) +#define mod4(x) (x & 3) + +#define EndianSwap(n) (rotl(n&0x00FF00FF,24U)|rotl(n&0xFF00FF00,8U)) + +#define Tr2(x) (rotl(x, 30U) ^ rotl(x, 19U) ^ rotl(x, 10U)) +#define Tr1(x) (rotl(x, 26U) ^ rotl(x, 21U) ^ rotl(x, 7U)) +#define Wr2(x) (rotl(x, 25U) ^ rotl(x, 14U) ^ (x>>3U)) +#define Wr1(x) (rotl(x, 15U) ^ rotl(x, 13U) ^ (x>>10U)) + +#define RND(a, b, c, d, e, f, g, h, k) \ + h += Tr1(e) + Ch(e, f, g) + k; \ + d += h; \ + h += Tr2(a) + Maj(a, b, c); + +#define WUpdate(i) { \ + uint4 tmp1 = (uint4) (W[mod4(i)].y, W[mod4(i)].z, W[mod4(i)].w, W[mod4(i+1)].x); \ + uint4 tmp2 = (uint4) (W[mod4(i+2)].y, W[mod4(i+2)].z, W[mod4(i+2)].w, W[mod4(i+3)].x); \ + uint4 tmp3 = (uint4) (W[mod4(i+3)].z, W[mod4(i+3)].w, 0, 0); \ + W[mod4(i)] += tmp2 + Wr2(tmp1) + Wr1(tmp3); \ + W[mod4(i)] += Wr1((uint4) (0, 0, W[mod4(i)].x, W[mod4(i)].y)); \ + } + + +void SHA256(uint4*restrict state0,uint4*restrict state1, const uint4 block0, const uint4 block1, const uint4 block2, const uint4 block3) +{ + uint4 S0 = *state0; + uint4 S1 = *state1; + +#define A S0.x +#define B S0.y +#define C S0.z +#define D S0.w +#define E S1.x +#define F S1.y +#define G S1.z +#define H S1.w + + uint4 W[4] = {block0, block1, block2, block3}; + + RND(A,B,C,D,E,F,G,H, W[0].x+ K[0]); + RND(H,A,B,C,D,E,F,G, W[0].y+ K[1]); + RND(G,H,A,B,C,D,E,F, W[0].z+ K[2]); + RND(F,G,H,A,B,C,D,E, W[0].w+ K[3]); + + RND(E,F,G,H,A,B,C,D, W[1].x+ K[4]); + RND(D,E,F,G,H,A,B,C, W[1].y+ K[5]); + RND(C,D,E,F,G,H,A,B, W[1].z+ K[6]); + RND(B,C,D,E,F,G,H,A, W[1].w+ K[7]); + + RND(A,B,C,D,E,F,G,H, W[2].x+ K[8]); + RND(H,A,B,C,D,E,F,G, W[2].y+ K[9]); + RND(G,H,A,B,C,D,E,F, W[2].z+ K[10]); + RND(F,G,H,A,B,C,D,E, W[2].w+ K[11]); + + RND(E,F,G,H,A,B,C,D, W[3].x+ K[12]); + RND(D,E,F,G,H,A,B,C, W[3].y+ K[13]); + RND(C,D,E,F,G,H,A,B, W[3].z+ K[14]); + RND(B,C,D,E,F,G,H,A, W[3].w+ K[76]); + + WUpdate (0); + RND(A,B,C,D,E,F,G,H, W[0].x+ K[15]); + RND(H,A,B,C,D,E,F,G, W[0].y+ K[16]); + RND(G,H,A,B,C,D,E,F, W[0].z+ K[17]); + RND(F,G,H,A,B,C,D,E, W[0].w+ K[18]); + + WUpdate (1); + RND(E,F,G,H,A,B,C,D, W[1].x+ K[19]); + RND(D,E,F,G,H,A,B,C, W[1].y+ K[20]); + RND(C,D,E,F,G,H,A,B, W[1].z+ K[21]); + RND(B,C,D,E,F,G,H,A, W[1].w+ K[22]); + + WUpdate (2); + RND(A,B,C,D,E,F,G,H, W[2].x+ K[23]); + RND(H,A,B,C,D,E,F,G, W[2].y+ K[24]); + RND(G,H,A,B,C,D,E,F, W[2].z+ K[25]); + RND(F,G,H,A,B,C,D,E, W[2].w+ K[26]); + + WUpdate (3); + RND(E,F,G,H,A,B,C,D, W[3].x+ K[27]); + RND(D,E,F,G,H,A,B,C, W[3].y+ K[28]); + RND(C,D,E,F,G,H,A,B, W[3].z+ K[29]); + RND(B,C,D,E,F,G,H,A, W[3].w+ K[30]); + + WUpdate (0); + RND(A,B,C,D,E,F,G,H, W[0].x+ K[31]); + RND(H,A,B,C,D,E,F,G, W[0].y+ K[32]); + RND(G,H,A,B,C,D,E,F, W[0].z+ K[33]); + RND(F,G,H,A,B,C,D,E, W[0].w+ K[34]); + + WUpdate (1); + RND(E,F,G,H,A,B,C,D, W[1].x+ K[35]); + RND(D,E,F,G,H,A,B,C, W[1].y+ K[36]); + RND(C,D,E,F,G,H,A,B, W[1].z+ K[37]); + RND(B,C,D,E,F,G,H,A, W[1].w+ K[38]); + + WUpdate (2); + RND(A,B,C,D,E,F,G,H, W[2].x+ K[39]); + RND(H,A,B,C,D,E,F,G, W[2].y+ K[40]); + RND(G,H,A,B,C,D,E,F, W[2].z+ K[41]); + RND(F,G,H,A,B,C,D,E, W[2].w+ K[42]); + + WUpdate (3); + RND(E,F,G,H,A,B,C,D, W[3].x+ K[43]); + RND(D,E,F,G,H,A,B,C, W[3].y+ K[44]); + RND(C,D,E,F,G,H,A,B, W[3].z+ K[45]); + RND(B,C,D,E,F,G,H,A, W[3].w+ K[46]); + + WUpdate (0); + RND(A,B,C,D,E,F,G,H, W[0].x+ K[47]); + RND(H,A,B,C,D,E,F,G, W[0].y+ K[48]); + RND(G,H,A,B,C,D,E,F, W[0].z+ K[49]); + RND(F,G,H,A,B,C,D,E, W[0].w+ K[50]); + + WUpdate (1); + RND(E,F,G,H,A,B,C,D, W[1].x+ K[51]); + RND(D,E,F,G,H,A,B,C, W[1].y+ K[52]); + RND(C,D,E,F,G,H,A,B, W[1].z+ K[53]); + RND(B,C,D,E,F,G,H,A, W[1].w+ K[54]); + + WUpdate (2); + RND(A,B,C,D,E,F,G,H, W[2].x+ K[55]); + RND(H,A,B,C,D,E,F,G, W[2].y+ K[56]); + RND(G,H,A,B,C,D,E,F, W[2].z+ K[57]); + RND(F,G,H,A,B,C,D,E, W[2].w+ K[58]); + + WUpdate (3); + RND(E,F,G,H,A,B,C,D, W[3].x+ K[59]); + RND(D,E,F,G,H,A,B,C, W[3].y+ K[60]); + RND(C,D,E,F,G,H,A,B, W[3].z+ K[61]); + RND(B,C,D,E,F,G,H,A, W[3].w+ K[62]); + +#undef A +#undef B +#undef C +#undef D +#undef E +#undef F +#undef G +#undef H + + *state0 += S0; + *state1 += S1; +} + +void SHA256_fresh(uint4*restrict state0,uint4*restrict state1, const uint4 block0, const uint4 block1, const uint4 block2, const uint4 block3) +{ +#define A (*state0).x +#define B (*state0).y +#define C (*state0).z +#define D (*state0).w +#define E (*state1).x +#define F (*state1).y +#define G (*state1).z +#define H (*state1).w + + uint4 W[4] = {block0, block1, block2, block3}; + + D= K[63] +W[0].x; + H= K[64] +W[0].x; + + C= K[65] +Tr1(D)+Ch(D, K[66], K[67])+W[0].y; + G= K[68] +C+Tr2(H)+Ch(H, K[69] ,K[70]); + + B= K[71] +Tr1(C)+Ch(C,D,K[66])+W[0].z; + F= K[72] +B+Tr2(G)+Maj(G,H, K[73]); + + A= K[74] +Tr1(B)+Ch(B,C,D)+W[0].w; + E= K[75] +A+Tr2(F)+Maj(F,G,H); + + RND(E,F,G,H,A,B,C,D, W[1].x+ K[4]); + RND(D,E,F,G,H,A,B,C, W[1].y+ K[5]); + RND(C,D,E,F,G,H,A,B, W[1].z+ K[6]); + RND(B,C,D,E,F,G,H,A, W[1].w+ K[7]); + + RND(A,B,C,D,E,F,G,H, W[2].x+ K[8]); + RND(H,A,B,C,D,E,F,G, W[2].y+ K[9]); + RND(G,H,A,B,C,D,E,F, W[2].z+ K[10]); + RND(F,G,H,A,B,C,D,E, W[2].w+ K[11]); + + RND(E,F,G,H,A,B,C,D, W[3].x+ K[12]); + RND(D,E,F,G,H,A,B,C, W[3].y+ K[13]); + RND(C,D,E,F,G,H,A,B, W[3].z+ K[14]); + RND(B,C,D,E,F,G,H,A, W[3].w+ K[76]); + + WUpdate (0); + RND(A,B,C,D,E,F,G,H, W[0].x+ K[15]); + RND(H,A,B,C,D,E,F,G, W[0].y+ K[16]); + RND(G,H,A,B,C,D,E,F, W[0].z+ K[17]); + RND(F,G,H,A,B,C,D,E, W[0].w+ K[18]); + + WUpdate (1); + RND(E,F,G,H,A,B,C,D, W[1].x+ K[19]); + RND(D,E,F,G,H,A,B,C, W[1].y+ K[20]); + RND(C,D,E,F,G,H,A,B, W[1].z+ K[21]); + RND(B,C,D,E,F,G,H,A, W[1].w+ K[22]); + + WUpdate (2); + RND(A,B,C,D,E,F,G,H, W[2].x+ K[23]); + RND(H,A,B,C,D,E,F,G, W[2].y+ K[24]); + RND(G,H,A,B,C,D,E,F, W[2].z+ K[25]); + RND(F,G,H,A,B,C,D,E, W[2].w+ K[26]); + + WUpdate (3); + RND(E,F,G,H,A,B,C,D, W[3].x+ K[27]); + RND(D,E,F,G,H,A,B,C, W[3].y+ K[28]); + RND(C,D,E,F,G,H,A,B, W[3].z+ K[29]); + RND(B,C,D,E,F,G,H,A, W[3].w+ K[30]); + + WUpdate (0); + RND(A,B,C,D,E,F,G,H, W[0].x+ K[31]); + RND(H,A,B,C,D,E,F,G, W[0].y+ K[32]); + RND(G,H,A,B,C,D,E,F, W[0].z+ K[33]); + RND(F,G,H,A,B,C,D,E, W[0].w+ K[34]); + + WUpdate (1); + RND(E,F,G,H,A,B,C,D, W[1].x+ K[35]); + RND(D,E,F,G,H,A,B,C, W[1].y+ K[36]); + RND(C,D,E,F,G,H,A,B, W[1].z+ K[37]); + RND(B,C,D,E,F,G,H,A, W[1].w+ K[38]); + + WUpdate (2); + RND(A,B,C,D,E,F,G,H, W[2].x+ K[39]); + RND(H,A,B,C,D,E,F,G, W[2].y+ K[40]); + RND(G,H,A,B,C,D,E,F, W[2].z+ K[41]); + RND(F,G,H,A,B,C,D,E, W[2].w+ K[42]); + + WUpdate (3); + RND(E,F,G,H,A,B,C,D, W[3].x+ K[43]); + RND(D,E,F,G,H,A,B,C, W[3].y+ K[44]); + RND(C,D,E,F,G,H,A,B, W[3].z+ K[45]); + RND(B,C,D,E,F,G,H,A, W[3].w+ K[46]); + + WUpdate (0); + RND(A,B,C,D,E,F,G,H, W[0].x+ K[47]); + RND(H,A,B,C,D,E,F,G, W[0].y+ K[48]); + RND(G,H,A,B,C,D,E,F, W[0].z+ K[49]); + RND(F,G,H,A,B,C,D,E, W[0].w+ K[50]); + + WUpdate (1); + RND(E,F,G,H,A,B,C,D, W[1].x+ K[51]); + RND(D,E,F,G,H,A,B,C, W[1].y+ K[52]); + RND(C,D,E,F,G,H,A,B, W[1].z+ K[53]); + RND(B,C,D,E,F,G,H,A, W[1].w+ K[54]); + + WUpdate (2); + RND(A,B,C,D,E,F,G,H, W[2].x+ K[55]); + RND(H,A,B,C,D,E,F,G, W[2].y+ K[56]); + RND(G,H,A,B,C,D,E,F, W[2].z+ K[57]); + RND(F,G,H,A,B,C,D,E, W[2].w+ K[58]); + + WUpdate (3); + RND(E,F,G,H,A,B,C,D, W[3].x+ K[59]); + RND(D,E,F,G,H,A,B,C, W[3].y+ K[60]); + RND(C,D,E,F,G,H,A,B, W[3].z+ K[61]); + RND(B,C,D,E,F,G,H,A, W[3].w+ K[62]); + + /* + W[0].x += Wr1(W[3].z) + W[2].y + Wr2(W[0].y); + RND(A,B,C,D,E,F,G,H, W[0].x+ K[15]); + + W[0].y += Wr1(W[3].w) + W[2].z + Wr2(W[0].z); + RND(H,A,B,C,D,E,F,G, W[0].y+ K[16]); + + W[0].z += Wr1(W[0].x) + W[2].w + Wr2(W[0].w); + RND(G,H,A,B,C,D,E,F, W[0].z+ K[17]); + + W[0].w += Wr1(W[0].y) + W[3].x + Wr2(W[1].x); + RND(F,G,H,A,B,C,D,E, W[0].w+ K[18]); + + W[1].x += Wr1(W[0].z) + W[3].y + Wr2(W[1].y); + RND(E,F,G,H,A,B,C,D, W[1].x+ K[19]); + + W[1].y += Wr1(W[0].w) + W[3].z + Wr2(W[1].z); + RND(D,E,F,G,H,A,B,C, W[1].y+ K[20]); + + W[1].z += Wr1(W[1].x) + W[3].w + Wr2(W[1].w); + RND(C,D,E,F,G,H,A,B, W[1].z+ K[21]); + + W[1].w += Wr1(W[1].y) + W[0].x + Wr2(W[2].x); + RND(B,C,D,E,F,G,H,A, W[1].w+ K[22]); + + W[2].x += Wr1(W[1].z) + W[0].y + Wr2(W[2].y); + RND(A,B,C,D,E,F,G,H, W[2].x+ K[23]); + + W[2].y += Wr1(W[1].w) + W[0].z + Wr2(W[2].z); + RND(H,A,B,C,D,E,F,G, W[2].y+ K[24]); + + W[2].z += Wr1(W[2].x) + W[0].w + Wr2(W[2].w); + RND(G,H,A,B,C,D,E,F, W[2].z+ K[25]); + + W[2].w += Wr1(W[2].y) + W[1].x + Wr2(W[3].x); + RND(F,G,H,A,B,C,D,E, W[2].w+ K[26]); + + W[3].x += Wr1(W[2].z) + W[1].y + Wr2(W[3].y); + RND(E,F,G,H,A,B,C,D, W[3].x+ K[27]); + + W[3].y += Wr1(W[2].w) + W[1].z + Wr2(W[3].z); + RND(D,E,F,G,H,A,B,C, W[3].y+ K[28]); + + W[3].z += Wr1(W[3].x) + W[1].w + Wr2(W[3].w); + RND(C,D,E,F,G,H,A,B, W[3].z+ K[29]); + + W[3].w += Wr1(W[3].y) + W[2].x + Wr2(W[0].x); + RND(B,C,D,E,F,G,H,A, W[3].w+ K[30]); + + W[0].x += Wr1(W[3].z) + W[2].y + Wr2(W[0].y); + RND(A,B,C,D,E,F,G,H, W[0].x+ K[31]); + + W[0].y += Wr1(W[3].w) + W[2].z + Wr2(W[0].z); + RND(H,A,B,C,D,E,F,G, W[0].y+ K[32]); + + W[0].z += Wr1(W[0].x) + W[2].w + Wr2(W[0].w); + RND(G,H,A,B,C,D,E,F, W[0].z+ K[33]); + + W[0].w += Wr1(W[0].y) + W[3].x + Wr2(W[1].x); + RND(F,G,H,A,B,C,D,E, W[0].w+ K[34]); + + W[1].x += Wr1(W[0].z) + W[3].y + Wr2(W[1].y); + RND(E,F,G,H,A,B,C,D, W[1].x+ K[35]); + + W[1].y += Wr1(W[0].w) + W[3].z + Wr2(W[1].z); + RND(D,E,F,G,H,A,B,C, W[1].y+ K[36]); + + W[1].z += Wr1(W[1].x) + W[3].w + Wr2(W[1].w); + RND(C,D,E,F,G,H,A,B, W[1].z+ K[37]); + + W[1].w += Wr1(W[1].y) + W[0].x + Wr2(W[2].x); + RND(B,C,D,E,F,G,H,A, W[1].w+ K[38]); + + W[2].x += Wr1(W[1].z) + W[0].y + Wr2(W[2].y); + RND(A,B,C,D,E,F,G,H, W[2].x+ K[39]); + + W[2].y += Wr1(W[1].w) + W[0].z + Wr2(W[2].z); + RND(H,A,B,C,D,E,F,G, W[2].y+ K[40]); + + W[2].z += Wr1(W[2].x) + W[0].w + Wr2(W[2].w); + RND(G,H,A,B,C,D,E,F, W[2].z+ K[41]); + + W[2].w += Wr1(W[2].y) + W[1].x + Wr2(W[3].x); + RND(F,G,H,A,B,C,D,E, W[2].w+ K[42]); + + W[3].x += Wr1(W[2].z) + W[1].y + Wr2(W[3].y); + RND(E,F,G,H,A,B,C,D, W[3].x+ K[43]); + + W[3].y += Wr1(W[2].w) + W[1].z + Wr2(W[3].z); + RND(D,E,F,G,H,A,B,C, W[3].y+ K[44]); + + W[3].z += Wr1(W[3].x) + W[1].w + Wr2(W[3].w); + RND(C,D,E,F,G,H,A,B, W[3].z+ K[45]); + + W[3].w += Wr1(W[3].y) + W[2].x + Wr2(W[0].x); + RND(B,C,D,E,F,G,H,A, W[3].w+ K[46]); + + W[0].x += Wr1(W[3].z) + W[2].y + Wr2(W[0].y); + RND(A,B,C,D,E,F,G,H, W[0].x+ K[47]); + + W[0].y += Wr1(W[3].w) + W[2].z + Wr2(W[0].z); + RND(H,A,B,C,D,E,F,G, W[0].y+ K[48]); + + W[0].z += Wr1(W[0].x) + W[2].w + Wr2(W[0].w); + RND(G,H,A,B,C,D,E,F, W[0].z+ K[49]); + + W[0].w += Wr1(W[0].y) + W[3].x + Wr2(W[1].x); + RND(F,G,H,A,B,C,D,E, W[0].w+ K[50]); + + W[1].x += Wr1(W[0].z) + W[3].y + Wr2(W[1].y); + RND(E,F,G,H,A,B,C,D, W[1].x+ K[51]); + + W[1].y += Wr1(W[0].w) + W[3].z + Wr2(W[1].z); + RND(D,E,F,G,H,A,B,C, W[1].y+ K[52]); + + W[1].z += Wr1(W[1].x) + W[3].w + Wr2(W[1].w); + RND(C,D,E,F,G,H,A,B, W[1].z+ K[53]); + + W[1].w += Wr1(W[1].y) + W[0].x + Wr2(W[2].x); + RND(B,C,D,E,F,G,H,A, W[1].w+ K[54]); + + W[2].x += Wr1(W[1].z) + W[0].y + Wr2(W[2].y); + RND(A,B,C,D,E,F,G,H, W[2].x+ K[55]); + + W[2].y += Wr1(W[1].w) + W[0].z + Wr2(W[2].z); + RND(H,A,B,C,D,E,F,G, W[2].y+ K[56]); + + W[2].z += Wr1(W[2].x) + W[0].w + Wr2(W[2].w); + RND(G,H,A,B,C,D,E,F, W[2].z+ K[57]); + + W[2].w += Wr1(W[2].y) + W[1].x + Wr2(W[3].x); + RND(F,G,H,A,B,C,D,E, W[2].w+ K[58]); + + W[3].x += Wr1(W[2].z) + W[1].y + Wr2(W[3].y); + RND(E,F,G,H,A,B,C,D, W[3].x+ K[59]); + + W[3].y += Wr1(W[2].w) + W[1].z + Wr2(W[3].z); + RND(D,E,F,G,H,A,B,C, W[3].y+ K[60]); + + W[3].z += Wr1(W[3].x) + W[1].w + Wr2(W[3].w); + RND(C,D,E,F,G,H,A,B, W[3].z+ K[61]); + + W[3].w += Wr1(W[3].y) + W[2].x + Wr2(W[0].x); + RND(B,C,D,E,F,G,H,A, W[3].w+ K[62]); + */ + +#undef A +#undef B +#undef C +#undef D +#undef E +#undef F +#undef G +#undef H + + *state0 += (uint4)(K[73], K[77], K[78], K[79]); + *state1 += (uint4)(K[66], K[67], K[80], K[81]); +} + +__constant uint fixedW[64] = +{ + 0x428a2f99,0xf1374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5, + 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf794, + 0xf59b89c2,0x73924787,0x23c6886e,0xa42ca65c,0x15ed3627,0x4d6edcbf,0xe28217fc,0xef02488f, + 0xb707775c,0x0468c23f,0xe7e72b4c,0x49e1f1a2,0x4b99c816,0x926d1570,0xaa0fc072,0xadb36e2c, + 0xad87a3ea,0xbcb1d3a3,0x7b993186,0x562b9420,0xbff3ca0c,0xda4b0c23,0x6cd8711a,0x8f337caa, + 0xc91b1417,0xc359dce1,0xa83253a7,0x3b13c12d,0x9d3d725d,0xd9031a84,0xb1a03340,0x16f58012, + 0xe64fb6a2,0xe84d923a,0xe93a5730,0x09837686,0x078ff753,0x29833341,0xd5de0b7e,0x6948ccf4, + 0xe0a1adbe,0x7c728e11,0x511c78e4,0x315b45bd,0xfca71413,0xea28f96a,0x79703128,0x4e1ef848, +}; + +void SHA256_fixed(uint4*restrict state0,uint4*restrict state1) +{ + uint4 S0 = *state0; + uint4 S1 = *state1; + +#define A S0.x +#define B S0.y +#define C S0.z +#define D S0.w +#define E S1.x +#define F S1.y +#define G S1.z +#define H S1.w + + RND(A,B,C,D,E,F,G,H, fixedW[0]); + RND(H,A,B,C,D,E,F,G, fixedW[1]); + RND(G,H,A,B,C,D,E,F, fixedW[2]); + RND(F,G,H,A,B,C,D,E, fixedW[3]); + RND(E,F,G,H,A,B,C,D, fixedW[4]); + RND(D,E,F,G,H,A,B,C, fixedW[5]); + RND(C,D,E,F,G,H,A,B, fixedW[6]); + RND(B,C,D,E,F,G,H,A, fixedW[7]); + RND(A,B,C,D,E,F,G,H, fixedW[8]); + RND(H,A,B,C,D,E,F,G, fixedW[9]); + RND(G,H,A,B,C,D,E,F, fixedW[10]); + RND(F,G,H,A,B,C,D,E, fixedW[11]); + RND(E,F,G,H,A,B,C,D, fixedW[12]); + RND(D,E,F,G,H,A,B,C, fixedW[13]); + RND(C,D,E,F,G,H,A,B, fixedW[14]); + RND(B,C,D,E,F,G,H,A, fixedW[15]); + RND(A,B,C,D,E,F,G,H, fixedW[16]); + RND(H,A,B,C,D,E,F,G, fixedW[17]); + RND(G,H,A,B,C,D,E,F, fixedW[18]); + RND(F,G,H,A,B,C,D,E, fixedW[19]); + RND(E,F,G,H,A,B,C,D, fixedW[20]); + RND(D,E,F,G,H,A,B,C, fixedW[21]); + RND(C,D,E,F,G,H,A,B, fixedW[22]); + RND(B,C,D,E,F,G,H,A, fixedW[23]); + RND(A,B,C,D,E,F,G,H, fixedW[24]); + RND(H,A,B,C,D,E,F,G, fixedW[25]); + RND(G,H,A,B,C,D,E,F, fixedW[26]); + RND(F,G,H,A,B,C,D,E, fixedW[27]); + RND(E,F,G,H,A,B,C,D, fixedW[28]); + RND(D,E,F,G,H,A,B,C, fixedW[29]); + RND(C,D,E,F,G,H,A,B, fixedW[30]); + RND(B,C,D,E,F,G,H,A, fixedW[31]); + RND(A,B,C,D,E,F,G,H, fixedW[32]); + RND(H,A,B,C,D,E,F,G, fixedW[33]); + RND(G,H,A,B,C,D,E,F, fixedW[34]); + RND(F,G,H,A,B,C,D,E, fixedW[35]); + RND(E,F,G,H,A,B,C,D, fixedW[36]); + RND(D,E,F,G,H,A,B,C, fixedW[37]); + RND(C,D,E,F,G,H,A,B, fixedW[38]); + RND(B,C,D,E,F,G,H,A, fixedW[39]); + RND(A,B,C,D,E,F,G,H, fixedW[40]); + RND(H,A,B,C,D,E,F,G, fixedW[41]); + RND(G,H,A,B,C,D,E,F, fixedW[42]); + RND(F,G,H,A,B,C,D,E, fixedW[43]); + RND(E,F,G,H,A,B,C,D, fixedW[44]); + RND(D,E,F,G,H,A,B,C, fixedW[45]); + RND(C,D,E,F,G,H,A,B, fixedW[46]); + RND(B,C,D,E,F,G,H,A, fixedW[47]); + RND(A,B,C,D,E,F,G,H, fixedW[48]); + RND(H,A,B,C,D,E,F,G, fixedW[49]); + RND(G,H,A,B,C,D,E,F, fixedW[50]); + RND(F,G,H,A,B,C,D,E, fixedW[51]); + RND(E,F,G,H,A,B,C,D, fixedW[52]); + RND(D,E,F,G,H,A,B,C, fixedW[53]); + RND(C,D,E,F,G,H,A,B, fixedW[54]); + RND(B,C,D,E,F,G,H,A, fixedW[55]); + RND(A,B,C,D,E,F,G,H, fixedW[56]); + RND(H,A,B,C,D,E,F,G, fixedW[57]); + RND(G,H,A,B,C,D,E,F, fixedW[58]); + RND(F,G,H,A,B,C,D,E, fixedW[59]); + RND(E,F,G,H,A,B,C,D, fixedW[60]); + RND(D,E,F,G,H,A,B,C, fixedW[61]); + RND(C,D,E,F,G,H,A,B, fixedW[62]); + RND(B,C,D,E,F,G,H,A, fixedW[63]); + +#undef A +#undef B +#undef C +#undef D +#undef E +#undef F +#undef G +#undef H + + *state0 += S0; + *state1 += S1; +} + +void shittify(uint4 B[8]) +{ + uint4 tmp[8]; + tmp[0] = (uint4)(B[1].x,B[2].y,B[3].z,B[0].w); + tmp[1] = (uint4)(B[2].x,B[3].y,B[0].z,B[1].w); + tmp[2] = (uint4)(B[3].x,B[0].y,B[1].z,B[2].w); + tmp[3] = (uint4)(B[0].x,B[1].y,B[2].z,B[3].w); + tmp[4] = (uint4)(B[5].x,B[6].y,B[7].z,B[4].w); + tmp[5] = (uint4)(B[6].x,B[7].y,B[4].z,B[5].w); + tmp[6] = (uint4)(B[7].x,B[4].y,B[5].z,B[6].w); + tmp[7] = (uint4)(B[4].x,B[5].y,B[6].z,B[7].w); + +#pragma unroll 8 + for(uint i=0; i<8; ++i) + B[i] = EndianSwap(tmp[i]); +} + +void unshittify(uint4 B[8]) +{ + uint4 tmp[8]; + tmp[0] = (uint4)(B[3].x,B[2].y,B[1].z,B[0].w); + tmp[1] = (uint4)(B[0].x,B[3].y,B[2].z,B[1].w); + tmp[2] = (uint4)(B[1].x,B[0].y,B[3].z,B[2].w); + tmp[3] = (uint4)(B[2].x,B[1].y,B[0].z,B[3].w); + tmp[4] = (uint4)(B[7].x,B[6].y,B[5].z,B[4].w); + tmp[5] = (uint4)(B[4].x,B[7].y,B[6].z,B[5].w); + tmp[6] = (uint4)(B[5].x,B[4].y,B[7].z,B[6].w); + tmp[7] = (uint4)(B[6].x,B[5].y,B[4].z,B[7].w); + +#pragma unroll 8 + for(uint i=0; i<8; ++i) + B[i] = EndianSwap(tmp[i]); +} + +void salsa(uint4 B[8]) +{ + uint i; + uint4 w[4]; + +#pragma unroll 4 + for(i=0; i<4; ++i) + w[i] = (B[i]^=B[i+4]); + +#pragma unroll 4 + for(i=0; i<4; ++i) + { + w[0] ^= rotl(w[3] +w[2] , 7U); + w[1] ^= rotl(w[0] +w[3] , 9U); + w[2] ^= rotl(w[1] +w[0] ,13U); + w[3] ^= rotl(w[2] +w[1] ,18U); + w[2] ^= rotl(w[3].wxyz+w[0].zwxy, 7U); + w[1] ^= rotl(w[2].wxyz+w[3].zwxy, 9U); + w[0] ^= rotl(w[1].wxyz+w[2].zwxy,13U); + w[3] ^= rotl(w[0].wxyz+w[1].zwxy,18U); + } + +#pragma unroll 4 + for(i=0; i<4; ++i) + w[i] = (B[i+4]^=(B[i]+=w[i])); + +#pragma unroll 4 + for(i=0; i<4; ++i) + { + w[0] ^= rotl(w[3] +w[2] , 7U); + w[1] ^= rotl(w[0] +w[3] , 9U); + w[2] ^= rotl(w[1] +w[0] ,13U); + w[3] ^= rotl(w[2] +w[1] ,18U); + w[2] ^= rotl(w[3].wxyz+w[0].zwxy, 7U); + w[1] ^= rotl(w[2].wxyz+w[3].zwxy, 9U); + w[0] ^= rotl(w[1].wxyz+w[2].zwxy,13U); + w[3] ^= rotl(w[0].wxyz+w[1].zwxy,18U); + } + +#pragma unroll 4 + for(i=0; i<4; ++i) + B[i+4] += w[i]; +} + +void salsa_double(uint4 B[8]) +{ + uint i; + uint4 w[4]; + +#pragma unroll 4 + for(i=0; i<4; ++i) + w[i] = (B[i]^=B[i+4]); + +#pragma unroll 4 + for(i=0; i<4; ++i) + { + w[0] ^= rotl(w[3] +w[2] , 7U); + w[1] ^= rotl(w[0] +w[3] , 9U); + w[2] ^= rotl(w[1] +w[0] ,13U); + w[3] ^= rotl(w[2] +w[1] ,18U); + w[2] ^= rotl(w[3].wxyz+w[0].zwxy, 7U); + w[1] ^= rotl(w[2].wxyz+w[3].zwxy, 9U); + w[0] ^= rotl(w[1].wxyz+w[2].zwxy,13U); + w[3] ^= rotl(w[0].wxyz+w[1].zwxy,18U); + } + +#pragma unroll 4 + for(i=0; i<4; ++i) + w[i] = (B[i+4]^=(B[i]+=w[i])); + +#pragma unroll 4 + for(i=0; i<4; ++i) + { + w[0] ^= rotl(w[3] +w[2] , 7U); + w[1] ^= rotl(w[0] +w[3] , 9U); + w[2] ^= rotl(w[1] +w[0] ,13U); + w[3] ^= rotl(w[2] +w[1] ,18U); + w[2] ^= rotl(w[3].wxyz+w[0].zwxy, 7U); + w[1] ^= rotl(w[2].wxyz+w[3].zwxy, 9U); + w[0] ^= rotl(w[1].wxyz+w[2].zwxy,13U); + w[3] ^= rotl(w[0].wxyz+w[1].zwxy,18U); + } + +#pragma unroll 4 + for(i=0; i<4; ++i) + B[i+4] += w[i]; + +#pragma unroll 4 + for(i=0; i<4; ++i) + w[i] = (B[i]^=B[i+4]); + +#pragma unroll 4 + for(i=0; i<4; ++i) + { + w[0] ^= rotl(w[3] +w[2] , 7U); + w[1] ^= rotl(w[0] +w[3] , 9U); + w[2] ^= rotl(w[1] +w[0] ,13U); + w[3] ^= rotl(w[2] +w[1] ,18U); + w[2] ^= rotl(w[3].wxyz+w[0].zwxy, 7U); + w[1] ^= rotl(w[2].wxyz+w[3].zwxy, 9U); + w[0] ^= rotl(w[1].wxyz+w[2].zwxy,13U); + w[3] ^= rotl(w[0].wxyz+w[1].zwxy,18U); + } + +#pragma unroll 4 + for(i=0; i<4; ++i) + w[i] = (B[i+4]^=(B[i]+=w[i])); + +#pragma unroll 4 + for(i=0; i<4; ++i) + { + w[0] ^= rotl(w[3] +w[2] , 7U); + w[1] ^= rotl(w[0] +w[3] , 9U); + w[2] ^= rotl(w[1] +w[0] ,13U); + w[3] ^= rotl(w[2] +w[1] ,18U); + w[2] ^= rotl(w[3].wxyz+w[0].zwxy, 7U); + w[1] ^= rotl(w[2].wxyz+w[3].zwxy, 9U); + w[0] ^= rotl(w[1].wxyz+w[2].zwxy,13U); + w[3] ^= rotl(w[0].wxyz+w[1].zwxy,18U); + } + +#pragma unroll 4 + for(i=0; i<4; ++i) + B[i+4] += w[i]; +} + + +//#define Coord(x,y,z) z+x*(z ## SIZE)+y*(x ## SIZE)*(z ## SIZE) +//#define CO Coord(x,y,z) +// z + x * (z ## SIZE) + y * (x ## SIZE) * (z ## SIZE) +//#define Coord(y,z) z + ((coor ## SIZE).w << (coor ## SIZE).z) + y * ((coor ## SIZE).x << (coor ## SIZE).z) +//#define CO Coord(y,z) + +void scrypt_core(const uint gid, uint4 X[8], __global uint4*restrict lookup, const uint n) +{ + const uint4 coorSIZE = (uint4)(CONCURRENT_THREADS, ((N[n]+1)/LOOKUP_GAP+((N[n]+1)%LOOKUP_GAP>0)), 3, (gid%CONCURRENT_THREADS)); + uint4 V[8]; + uint i=0, y=0, z=0; + uint COx=coorSIZE.w<<3U; + uint COy=coorSIZE.x<<3U; + uint COz=0; +#if (LOOKUP_GAP > 2) + uint j = 0; +#endif + + shittify(X); + + // write lookup table to memory + do { + COz = COx; +#pragma unroll 8 + for(z=0; z<(1<>1)); +#else + do { + salsa(&X); + } while (++i < LOOKUP_GAP); +#endif + COx += COy; + } while (++y < coorSIZE.y); + + COy = coorSIZE.w<<3U; + + // write something more +#if (LOOKUP_GAP != 1) && (LOOKUP_GAP != 2) && (LOOKUP_GAP != 4) && (LOOKUP_GAP != 8) + { + y = ((N[n]+1)/LOOKUP_GAP); + COx = COy + (coorSIZE.w*y)<<3U; +#pragma unroll 8 + for(z=0; z<(1<> (LOOKUP_GAP>>1)); +#else + y = ((X[7].x & N[n])/LOOKUP_GAP); +#endif + + COz = COy + ((coorSIZE.x*y) << 3U); + +#pragma unroll 8 + for(z=0; z<(1<work_size = 256; break; + case KL_NSCRYPT: + applog(LOG_WARNING, "Kernel nscrypt is experimental."); + strcpy(filename, NSCRYPT_KERNNAME".cl"); + strcpy(binaryfilename, NSCRYPT_KERNNAME); + break; case KL_NONE: /* Shouldn't happen */ break; } @@ -777,7 +782,9 @@ built: return NULL; } - size_t ipt = (1024 / cgpu->lookup_gap + (1024 % cgpu->lookup_gap > 0)); + cl_uint bsize = opt_nscrypt ? 2048 : 1024; + + size_t ipt = (bsize / cgpu->lookup_gap + (bsize % cgpu->lookup_gap > 0)); size_t bufsize = 128 * ipt * cgpu->thread_concurrency; /* Use the max alloc value which has been rounded to a power of diff --git a/scrypt.c b/scrypt.c index d135a72d..ea3f5a39 100644 --- a/scrypt.c +++ b/scrypt.c @@ -356,7 +356,7 @@ salsa20_8(uint32_t B[16], const uint32_t Bx[16]) /* cpu and memory intensive function to transform a 80 byte buffer into a 32 byte output scratchpad size needs to be at least 63 + (128 * r * p) + (256 * r + 64) + (128 * r * N) bytes */ -static void scrypt_1024_1_1_256_sp(const uint32_t* input, char* scratchpad, uint32_t *ostate) +static void scrypt_1024_1_1_256_sp(const uint32_t* input, char* scratchpad, uint32_t *ostate, const cl_uint n = 1024) { uint32_t * V; uint32_t X[32]; @@ -370,7 +370,7 @@ static void scrypt_1024_1_1_256_sp(const uint32_t* input, char* scratchpad, uint PBKDF2_SHA256_80_128(input, X); - for (i = 0; i < 1024; i += 2) { + for (i = 0; i < n; i += 2) { memcpy(&V[i * 32], X, 128); salsa20_8(&X[0], &X[16]); @@ -381,8 +381,8 @@ static void scrypt_1024_1_1_256_sp(const uint32_t* input, char* scratchpad, uint salsa20_8(&X[0], &X[16]); salsa20_8(&X[16], &X[0]); } - for (i = 0; i < 1024; i += 2) { - j = X[16] & 1023; + for (i = 0; i < n; i += 2) { + j = X[16] & (n-1); p2 = (uint64_t *)(&V[j * 32]); for(k = 0; k < 16; k++) p1[k] ^= p2[k]; @@ -390,7 +390,7 @@ static void scrypt_1024_1_1_256_sp(const uint32_t* input, char* scratchpad, uint salsa20_8(&X[0], &X[16]); salsa20_8(&X[16], &X[0]); - j = X[16] & 1023; + j = X[16] & (n-1); p2 = (uint64_t *)(&V[j * 32]); for(k = 0; k < 16; k++) p1[k] ^= p2[k]; @@ -409,13 +409,28 @@ void scrypt_regenhash(struct work *work) { uint32_t data[20]; char *scratchbuf; + uint32_t timestamp; + cl_uint nfactor = 10; // scrypt default uint32_t *nonce = (uint32_t *)(work->data + 76); uint32_t *ohash = (uint32_t *)(work->hash); + if (opt_nscrypt) { + timestamp = bswap_32(*((uint32_t *)(work->data + 17*4))); + nfactor = vert_GetNfactor(timestamp) + 1; + } + be32enc_vect(data, (const uint32_t *)work->data, 19); data[19] = htobe32(*nonce); - scratchbuf = (char *)alloca(SCRATCHBUF_SIZE); - scrypt_1024_1_1_256_sp(data, scratchbuf, ohash); + + if (opt_nscrypt) { + scratchbuf = (char *)alloca((1 << nfactor) * 128 + 512); + scrypt_1024_1_1_256_sp(data, scratchbuf, ohash, (1 << nfactor)); + } + else { + scratchbuf = (char *)alloca(SCRATCHBUF_SIZE); + scrypt_1024_1_1_256_sp(data, scratchbuf, ohash, 1024); + } + flip32(ohash, ohash); } diff --git a/sgminer.c b/sgminer.c index 0ec85559..7bcba15a 100644 --- a/sgminer.c +++ b/sgminer.c @@ -101,6 +101,7 @@ int opt_dynamic_interval = 7; int opt_g_threads = -1; int gpu_threads; bool opt_restart = true; +bool opt_nscrypt = false; struct list_head scan_devices; static bool devices_enabled[MAX_DEVICES]; @@ -1269,6 +1270,9 @@ static struct opt_table opt_config_table[] = { OPT_WITH_ARG("--sched-stop", set_schedtime, NULL, &schedstop, "Set a time of day in HH:MM to stop mining (will quit without a start time)"), + OPT_WITHOUT_ARG("--nscrypt", + opt_set_bool, &opt_nscrypt, + "Use the scrypt-vert algorithm for mining (NO scyrpt!)"), OPT_WITH_ARG("--shaders", set_shaders, NULL, NULL, "GPU shaders per card for tuning scrypt, comma separated"), @@ -4210,6 +4214,9 @@ void write_config(FILE *fcfg) case KL_ZUIKKIS: fprintf(fcfg, ZUIKKIS_KERNNAME); break; + case KL_NSCRYPT: + fprintf(fcfg, NSCRYPT_KERNNAME); + break; } } diff --git a/util.c b/util.c index 4ed978ba..a16885a3 100644 --- a/util.c +++ b/util.c @@ -2719,3 +2719,45 @@ bool cg_completion_timeout(void *fn, void *fnarg, int timeout) pthread_cancel(pthread); return !ret; } + +const unsigned char minNfactor = 10; +const unsigned char maxNfactor = 30; +const unsigned int vert_nChainStartTime = 1389306217; + +unsigned char vert_GetNfactor(const long int nTimestamp) { + int l, n; + long int s; + + l = 0; + + if (nTimestamp <= vert_nChainStartTime) { + return minNfactor; + } + + s = nTimestamp - vert_nChainStartTime; + while ((s >> 1) > 3) { + l += 1; + s >>= 1; + } + + s &= 3; + n = (l * 158 + s * 28 - 2670) / 100; + + if (n < 0) + n = 0; + + if (n > 255) + printf( "GetNfactor(%ld) - something wrong(n == %d)\n", nTimestamp, n ); + + unsigned char N = ((unsigned char) n); + //printf("GetNfactor: %d -> %d %d : %d / %d\n", nTimestamp - nChainStartTime, l, s, n, min(max(N, minNfactor), maxNfactor)); + + if (N < minNfactor) { + return minNfactor; + } else if (N > maxNfactor) { + return maxNfactor; + } + + return N; + //return min(max(N, minNfactor), maxNfactor); +} \ No newline at end of file diff --git a/util.h b/util.h index 23199d86..a923399d 100644 --- a/util.h +++ b/util.h @@ -142,6 +142,8 @@ void cgsem_reset(cgsem_t *cgsem); void cgsem_destroy(cgsem_t *cgsem); bool cg_completion_timeout(void *fn, void *fnarg, int timeout); +unsigned char vert_GetNfactor(const long int nTimestamp); + #define cgsem_init(_sem) _cgsem_init(_sem, __FILE__, __func__, __LINE__) #define cgsem_post(_sem) _cgsem_post(_sem, __FILE__, __func__, __LINE__) #define cgsem_wait(_sem) _cgsem_wait(_sem, __FILE__, __func__, __LINE__) diff --git a/winbuild/dist/include/config.h b/winbuild/dist/include/config.h index 791c60ae..0c1a071a 100644 --- a/winbuild/dist/include/config.h +++ b/winbuild/dist/include/config.h @@ -8,6 +8,7 @@ #define CKOLIVAS_KERNNAME "ckolivas" #define ZUIKKIS_KERNNAME "zuikkis" #define PSW_KERNNAME "psw" +#define NSCRYPT_KERNNAME "nscrypt" #if defined(_MSC_VER) From 854e1089d77027985cc5b9e5e915eefe42f0af26 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 17 Feb 2014 00:06:29 +0100 Subject: [PATCH 04/15] Moved kernels config to separate file. --- kernels.h | 6 ++++++ winbuild/dist/include/config.h | 8 +------- winbuild/sgminer.vcxproj | 1 + winbuild/sgminer.vcxproj.filters | 3 +++ 4 files changed, 11 insertions(+), 7 deletions(-) create mode 100644 kernels.h diff --git a/kernels.h b/kernels.h new file mode 100644 index 00000000..479cfdf0 --- /dev/null +++ b/kernels.h @@ -0,0 +1,6 @@ +#define ALEXKARNEW_KERNNAME "alexkarnew" +#define ALEXKAROLD_KERNNAME "alexkarold" +#define CKOLIVAS_KERNNAME "ckolivas" +#define ZUIKKIS_KERNNAME "zuikkis" +#define PSW_KERNNAME "psw" +#define NSCRYPT_KERNNAME "nscrypt" \ No newline at end of file diff --git a/winbuild/dist/include/config.h b/winbuild/dist/include/config.h index 0c1a071a..dc6092e2 100644 --- a/winbuild/dist/include/config.h +++ b/winbuild/dist/include/config.h @@ -3,13 +3,6 @@ #define HAVE_STDINT_H -#define ALEXKARNEW_KERNNAME "alexkarnew" -#define ALEXKAROLD_KERNNAME "alexkarold" -#define CKOLIVAS_KERNNAME "ckolivas" -#define ZUIKKIS_KERNNAME "zuikkis" -#define PSW_KERNNAME "psw" -#define NSCRYPT_KERNNAME "nscrypt" - #if defined(_MSC_VER) #define HAVE_LIBCURL 1 @@ -84,6 +77,7 @@ #define SGMINER_PREFIX "" +#include "kernels.h" #include "gitversion.h" #include "winbuild.h" diff --git a/winbuild/sgminer.vcxproj b/winbuild/sgminer.vcxproj index fc95e6c1..7e59b595 100644 --- a/winbuild/sgminer.vcxproj +++ b/winbuild/sgminer.vcxproj @@ -280,6 +280,7 @@ exit 0 + diff --git a/winbuild/sgminer.vcxproj.filters b/winbuild/sgminer.vcxproj.filters index 0ed5cb60..29250318 100644 --- a/winbuild/sgminer.vcxproj.filters +++ b/winbuild/sgminer.vcxproj.filters @@ -163,6 +163,9 @@ Header Files + + Header Files + From f8579404bc6aac1310c58595c006ab224c81189e Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 17 Feb 2014 00:08:37 +0100 Subject: [PATCH 05/15] Removed --nscrypt option and introduced use_nscrypt flag that is kernel dependant. --- driver-opencl.c | 6 +++--- miner.h | 2 +- ocl.c | 3 ++- scrypt.c | 4 ++-- sgminer.c | 5 +---- 5 files changed, 9 insertions(+), 11 deletions(-) diff --git a/driver-opencl.c b/driver-opencl.c index dbfb5697..006be853 100644 --- a/driver-opencl.c +++ b/driver-opencl.c @@ -1024,7 +1024,7 @@ static cl_int queue_scrypt_kernel(_clState *clState, dev_blk_ctx *blk, __maybe_u uint32_t timestamp; cl_uint nfactor = 10; // scrypt default - if (opt_nscrypt) { + if (use_nscrypt) { timestamp = bswap_32(*((uint32_t *)(blk->work->data + 17*4))); nfactor = vert_GetNfactor(timestamp) + 1; } @@ -1039,7 +1039,7 @@ static cl_int queue_scrypt_kernel(_clState *clState, dev_blk_ctx *blk, __maybe_u CL_SET_VARG(4, &midstate[0]); CL_SET_VARG(4, &midstate[16]); CL_SET_ARG(le_target); - if (opt_nscrypt) { + if (use_nscrypt) { CL_SET_ARG(nfactor); } @@ -1050,7 +1050,7 @@ static void set_threads_hashes(unsigned int vectors, unsigned int compute_shader unsigned int minthreads, __maybe_unused int *intensity, __maybe_unused int *xintensity, __maybe_unused int *rawintensity) { unsigned int threads = 0; - if (opt_nscrypt) { + if (use_nscrypt) { // new intensity calculation based on shader count threads = (compute_shaders * minthreads << (MAX_INTENSITY-19)) >> (MAX_INTENSITY - *intensity); diff --git a/miner.h b/miner.h index 197da518..8a532792 100644 --- a/miner.h +++ b/miner.h @@ -981,7 +981,7 @@ extern bool opt_restart; extern bool opt_worktime; extern int swork_id; extern int opt_tcp_keepalive; -extern bool opt_nscrypt; +extern bool use_nscrypt; #if LOCK_TRACKING extern pthread_mutex_t lockstat_lock; diff --git a/ocl.c b/ocl.c index 6e71b22a..0105321f 100644 --- a/ocl.c +++ b/ocl.c @@ -458,6 +458,7 @@ _clState *initCl(unsigned int gpu, char *name, size_t nameSize) applog(LOG_WARNING, "Kernel nscrypt is experimental."); strcpy(filename, NSCRYPT_KERNNAME".cl"); strcpy(binaryfilename, NSCRYPT_KERNNAME); + use_nscrypt = true; break; case KL_NONE: /* Shouldn't happen */ break; @@ -782,7 +783,7 @@ built: return NULL; } - cl_uint bsize = opt_nscrypt ? 2048 : 1024; + cl_uint bsize = use_nscrypt ? 2048 : 1024; size_t ipt = (bsize / cgpu->lookup_gap + (bsize % cgpu->lookup_gap > 0)); size_t bufsize = 128 * ipt * cgpu->thread_concurrency; diff --git a/scrypt.c b/scrypt.c index ea3f5a39..34cc5db0 100644 --- a/scrypt.c +++ b/scrypt.c @@ -414,7 +414,7 @@ void scrypt_regenhash(struct work *work) uint32_t *nonce = (uint32_t *)(work->data + 76); uint32_t *ohash = (uint32_t *)(work->hash); - if (opt_nscrypt) { + if (use_nscrypt) { timestamp = bswap_32(*((uint32_t *)(work->data + 17*4))); nfactor = vert_GetNfactor(timestamp) + 1; } @@ -422,7 +422,7 @@ void scrypt_regenhash(struct work *work) be32enc_vect(data, (const uint32_t *)work->data, 19); data[19] = htobe32(*nonce); - if (opt_nscrypt) { + if (use_nscrypt) { scratchbuf = (char *)alloca((1 << nfactor) * 128 + 512); scrypt_1024_1_1_256_sp(data, scratchbuf, ohash, (1 << nfactor)); } diff --git a/sgminer.c b/sgminer.c index 7bcba15a..c9e6a107 100644 --- a/sgminer.c +++ b/sgminer.c @@ -101,7 +101,7 @@ int opt_dynamic_interval = 7; int opt_g_threads = -1; int gpu_threads; bool opt_restart = true; -bool opt_nscrypt = false; +bool use_nscrypt = false; struct list_head scan_devices; static bool devices_enabled[MAX_DEVICES]; @@ -1270,9 +1270,6 @@ static struct opt_table opt_config_table[] = { OPT_WITH_ARG("--sched-stop", set_schedtime, NULL, &schedstop, "Set a time of day in HH:MM to stop mining (will quit without a start time)"), - OPT_WITHOUT_ARG("--nscrypt", - opt_set_bool, &opt_nscrypt, - "Use the scrypt-vert algorithm for mining (NO scyrpt!)"), OPT_WITH_ARG("--shaders", set_shaders, NULL, NULL, "GPU shaders per card for tuning scrypt, comma separated"), From 2f531fddec49861fb3f453ac655bc9ba2c440fe4 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 17 Feb 2014 09:13:05 +0100 Subject: [PATCH 06/15] Moved kernels.h include to miner.h --- miner.h | 1 + winbuild/dist/include/config.h | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/miner.h b/miner.h index 8a532792..190cb75f 100644 --- a/miner.h +++ b/miner.h @@ -2,6 +2,7 @@ #define __MINER_H__ #include "config.h" +#include "kernels.h" #include #include diff --git a/winbuild/dist/include/config.h b/winbuild/dist/include/config.h index dc6092e2..dcf71023 100644 --- a/winbuild/dist/include/config.h +++ b/winbuild/dist/include/config.h @@ -77,7 +77,6 @@ #define SGMINER_PREFIX "" -#include "kernels.h" #include "gitversion.h" #include "winbuild.h" From 6285c6563054dba3a277afdabdeb632abb108336 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 17 Feb 2014 09:14:11 +0100 Subject: [PATCH 07/15] Fixed scrypt_1024_1_1_256_sp() for better cross-platform compatibility --- scrypt.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/scrypt.c b/scrypt.c index 34cc5db0..de8ba929 100644 --- a/scrypt.c +++ b/scrypt.c @@ -356,7 +356,10 @@ salsa20_8(uint32_t B[16], const uint32_t Bx[16]) /* cpu and memory intensive function to transform a 80 byte buffer into a 32 byte output scratchpad size needs to be at least 63 + (128 * r * p) + (256 * r + 64) + (128 * r * N) bytes */ -static void scrypt_1024_1_1_256_sp(const uint32_t* input, char* scratchpad, uint32_t *ostate, const cl_uint n = 1024) + +#define scrypt_1024_1_1_256_sp(input, scratchpad, ostate) scrypt_n_1_1_256_sp(input, scratchpad, ostate, 1024) + +static void scrypt_n_1_1_256_sp(const uint32_t* input, char* scratchpad, uint32_t *ostate, const cl_uint n) { uint32_t * V; uint32_t X[32]; From f846f387136d4145188dde15aaac3885c09e6beb Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 17 Feb 2014 09:33:19 +0100 Subject: [PATCH 08/15] scrypt_n_1_1_256_sp() fix. --- scrypt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scrypt.c b/scrypt.c index de8ba929..47201782 100644 --- a/scrypt.c +++ b/scrypt.c @@ -427,7 +427,7 @@ void scrypt_regenhash(struct work *work) if (use_nscrypt) { scratchbuf = (char *)alloca((1 << nfactor) * 128 + 512); - scrypt_1024_1_1_256_sp(data, scratchbuf, ohash, (1 << nfactor)); + scrypt_n_1_1_256_sp(data, scratchbuf, ohash, (1 << nfactor)); } else { scratchbuf = (char *)alloca(SCRATCHBUF_SIZE); From 850f5149ddc98dd952ce27964a3b587f8d8ce511 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 17 Feb 2014 12:35:10 +0100 Subject: [PATCH 09/15] Another scrypt_1024_1_1_256_sp() fix. --- scrypt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scrypt.c b/scrypt.c index 47201782..b026f313 100644 --- a/scrypt.c +++ b/scrypt.c @@ -431,7 +431,7 @@ void scrypt_regenhash(struct work *work) } else { scratchbuf = (char *)alloca(SCRATCHBUF_SIZE); - scrypt_1024_1_1_256_sp(data, scratchbuf, ohash, 1024); + scrypt_1024_1_1_256_sp(data, scratchbuf, ohash); } flip32(ohash, ohash); From a7e46721f6a3979f7904176c0f0fcf09a989f132 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 18 Feb 2014 10:27:18 +0100 Subject: [PATCH 10/15] TC adjustments for nscrypt. --- ocl.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/ocl.c b/ocl.c index 0105321f..93ac720d 100644 --- a/ocl.c +++ b/ocl.c @@ -484,20 +484,28 @@ _clState *initCl(unsigned int gpu, char *name, size_t nameSize) } else cgpu->lookup_gap = cgpu->opt_lg; + // TODO: check if this params can be the same for both scrypt and nscrypt + unsigned int sixtyfours = use_nscrypt ? ((cgpu->max_alloc*cgpu->lookup_gap) / (2048*128) / 64 - 1) : (cgpu->max_alloc / 131072 / 64 - 1); if (!cgpu->opt_tc) { - unsigned int sixtyfours; - - sixtyfours = cgpu->max_alloc / 131072 / 64 - 1; cgpu->thread_concurrency = sixtyfours * 64; if (cgpu->shaders && cgpu->thread_concurrency > cgpu->shaders) { cgpu->thread_concurrency -= cgpu->thread_concurrency % cgpu->shaders; - if (cgpu->thread_concurrency > cgpu->shaders * 5) - cgpu->thread_concurrency = cgpu->shaders * 5; + size_t tc_limit = cgpu->shaders * use_nscrypt ? 11 : 5; + if (cgpu->thread_concurrency > tc_limit) + cgpu->thread_concurrency = tc_limit; } applog(LOG_DEBUG, "GPU %d: selecting thread concurrency of %d", gpu, (int)(cgpu->thread_concurrency)); - } else + } else { cgpu->thread_concurrency = cgpu->opt_tc; + } + // TODO: check if this works with standard scrypt, too + if (use_nscrypt) { + if (((cgpu->thread_concurrency * (2048*128)) / cgpu->lookup_gap) > cgpu->max_alloc) { + cgpu->thread_concurrency = sixtyfours * 64; + applog(LOG_INFO, "GPU %d: thread concurrency too high, set to %d", gpu, (int)(cgpu->thread_concurrency)); + } + } FILE *binaryfile; size_t *binary_sizes; From e82f938afad8b289c21be231f0ea0052c493f131 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 19 Feb 2014 00:27:29 +0100 Subject: [PATCH 11/15] Allow use of config provided shaders count. --- ocl.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/ocl.c b/ocl.c index 93ac720d..3eb4f344 100644 --- a/ocl.c +++ b/ocl.c @@ -383,9 +383,15 @@ _clState *initCl(unsigned int gpu, char *name, size_t nameSize) applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_COMPUTE_UNITS", status); return NULL; } - // AMD architechture got 64 compute shaders per compute unit. - // Source: http://www.amd.com/us/Documents/GCN_Architecture_whitepaper.pdf - clState->compute_shaders = compute_units * 64; + + if (cgpu->shaders) { + clState->compute_shaders = cgpu->shaders; + } + else { + // AMD architechture got 64 compute shaders per compute unit. + // Source: http://www.amd.com/us/Documents/GCN_Architecture_whitepaper.pdf + clState->compute_shaders = compute_units * 64; + } applog(LOG_DEBUG, "Max shaders calculated %d", (int)(clState->compute_shaders)); status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_MEM_ALLOC_SIZE , sizeof(cl_ulong), (void *)&cgpu->max_alloc, NULL); From c8e4ec33dd40d4022b21b165d1b2f0d14169f85f Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 19 Feb 2014 22:54:16 +0100 Subject: [PATCH 12/15] Fixed tc_limit calculation. --- ocl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ocl.c b/ocl.c index 3eb4f344..73ea6bda 100644 --- a/ocl.c +++ b/ocl.c @@ -496,7 +496,7 @@ _clState *initCl(unsigned int gpu, char *name, size_t nameSize) cgpu->thread_concurrency = sixtyfours * 64; if (cgpu->shaders && cgpu->thread_concurrency > cgpu->shaders) { cgpu->thread_concurrency -= cgpu->thread_concurrency % cgpu->shaders; - size_t tc_limit = cgpu->shaders * use_nscrypt ? 11 : 5; + size_t tc_limit = cgpu->shaders * (use_nscrypt ? 11 : 5); if (cgpu->thread_concurrency > tc_limit) cgpu->thread_concurrency = tc_limit; } From 614d7402e5f3a79fbb18a7d7a28fe7dab598c8a1 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 20 Feb 2014 10:46:13 +0100 Subject: [PATCH 13/15] scrypt/nscrypt code optimization and cleanup. --- scrypt.c | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/scrypt.c b/scrypt.c index b026f313..f602beef 100644 --- a/scrypt.c +++ b/scrypt.c @@ -357,8 +357,6 @@ salsa20_8(uint32_t B[16], const uint32_t Bx[16]) scratchpad size needs to be at least 63 + (128 * r * p) + (256 * r + 64) + (128 * r * N) bytes */ -#define scrypt_1024_1_1_256_sp(input, scratchpad, ostate) scrypt_n_1_1_256_sp(input, scratchpad, ostate, 1024) - static void scrypt_n_1_1_256_sp(const uint32_t* input, char* scratchpad, uint32_t *ostate, const cl_uint n) { uint32_t * V; @@ -405,34 +403,24 @@ static void scrypt_n_1_1_256_sp(const uint32_t* input, char* scratchpad, uint32_ PBKDF2_SHA256_80_128_32(input, X, ostate); } -/* 131583 rounded up to 4 byte alignment */ -#define SCRATCHBUF_SIZE (131584) - void scrypt_regenhash(struct work *work) { uint32_t data[20]; char *scratchbuf; - uint32_t timestamp; cl_uint nfactor = 10; // scrypt default uint32_t *nonce = (uint32_t *)(work->data + 76); uint32_t *ohash = (uint32_t *)(work->hash); if (use_nscrypt) { - timestamp = bswap_32(*((uint32_t *)(work->data + 17*4))); + uint32_t timestamp = bswap_32(*((uint32_t *)(work->data + 17*4))); nfactor = vert_GetNfactor(timestamp) + 1; } be32enc_vect(data, (const uint32_t *)work->data, 19); data[19] = htobe32(*nonce); - if (use_nscrypt) { - scratchbuf = (char *)alloca((1 << nfactor) * 128 + 512); - scrypt_n_1_1_256_sp(data, scratchbuf, ohash, (1 << nfactor)); - } - else { - scratchbuf = (char *)alloca(SCRATCHBUF_SIZE); - scrypt_1024_1_1_256_sp(data, scratchbuf, ohash); - } + scratchbuf = (char *)alloca((1 << nfactor) * 128 + 512); + scrypt_n_1_1_256_sp(data, scratchbuf, ohash, (1 << nfactor)); flip32(ohash, ohash); } @@ -445,11 +433,13 @@ int scrypt_test(unsigned char *pdata, const unsigned char *ptarget, uint32_t non uint32_t tmp_hash7, Htarg = le32toh(((const uint32_t *)ptarget)[7]); uint32_t data[20], ohash[8]; char *scratchbuf; + cl_uint nfactor = 10; // scrypt default be32enc_vect(data, (const uint32_t *)pdata, 19); data[19] = htobe32(nonce); - scratchbuf = (char *)alloca(SCRATCHBUF_SIZE); - scrypt_1024_1_1_256_sp(data, scratchbuf, ohash); + + scratchbuf = (char *)alloca((1 << nfactor) * 128 + 512); + scrypt_n_1_1_256_sp(data, scratchbuf, ohash, (1 << nfactor)); tmp_hash7 = be32toh(ohash[7]); applog(LOG_DEBUG, "htarget %08lx diff1 %08lx hash %08lx", @@ -474,10 +464,11 @@ bool scanhash_scrypt(struct thr_info *thr, const unsigned char __maybe_unused *p uint32_t tmp_hash7; uint32_t Htarg = le32toh(((const uint32_t *)ptarget)[7]); bool ret = false; + cl_uint nfactor = 10; // scrypt default be32enc_vect(data, (const uint32_t *)pdata, 19); - scratchbuf = (char *)malloc(SCRATCHBUF_SIZE); + scratchbuf = (char *)alloca((1 << nfactor) * 128 + 512); if (unlikely(!scratchbuf)) { applog(LOG_ERR, "Failed to malloc scratchbuf in scanhash_scrypt"); return ret; @@ -488,7 +479,7 @@ bool scanhash_scrypt(struct thr_info *thr, const unsigned char __maybe_unused *p *nonce = ++n; data[19] = htobe32(n); - scrypt_1024_1_1_256_sp(data, scratchbuf, ostate); + scrypt_n_1_1_256_sp(data, scratchbuf, ohash, (1 << nfactor)); tmp_hash7 = be32toh(ostate[7]); if (unlikely(tmp_hash7 <= Htarg)) { From 8c1f7e272b1d484c2c3e05cbbc100626493c8c62 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 20 Feb 2014 10:47:19 +0100 Subject: [PATCH 14/15] Use shaders param if provided. --- driver-opencl.c | 10 +++++----- ocl.c | 5 +---- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/driver-opencl.c b/driver-opencl.c index 006be853..f2ee1bb9 100644 --- a/driver-opencl.c +++ b/driver-opencl.c @@ -1046,13 +1046,13 @@ static cl_int queue_scrypt_kernel(_clState *clState, dev_blk_ctx *blk, __maybe_u return status; } -static void set_threads_hashes(unsigned int vectors, unsigned int compute_shaders, int64_t *hashes, size_t *globalThreads, +static void set_threads_hashes(unsigned int vectors, size_t shaders, int64_t *hashes, size_t *globalThreads, unsigned int minthreads, __maybe_unused int *intensity, __maybe_unused int *xintensity, __maybe_unused int *rawintensity) { unsigned int threads = 0; - if (use_nscrypt) { + if (use_nscrypt && shaders) { // new intensity calculation based on shader count - threads = (compute_shaders * minthreads << (MAX_INTENSITY-19)) >> (MAX_INTENSITY - *intensity); + threads = (shaders * minthreads << (MAX_INTENSITY-19)) >> (MAX_INTENSITY - *intensity); if (threads < minthreads) threads = minthreads; @@ -1064,7 +1064,7 @@ static void set_threads_hashes(unsigned int vectors, unsigned int compute_shader if (*rawintensity > 0) { threads = *rawintensity; } else if (*xintensity > 0) { - threads = compute_shaders * *xintensity; + threads = shaders * *xintensity; } else { threads = 1 << *intensity; } @@ -1455,7 +1455,7 @@ static int64_t opencl_scanhash(struct thr_info *thr, struct work *work, gpu->intervals = 0; } - set_threads_hashes(clState->vwidth, clState->compute_shaders, &hashes, globalThreads, localThreads[0], + set_threads_hashes(clState->vwidth, gpu->shaders ? gpu->shaders : clState->compute_shaders, &hashes, globalThreads, localThreads[0], &gpu->intensity, &gpu->xintensity, &gpu->rawintensity); if (hashes > gpu->max_hashes) gpu->max_hashes = hashes; diff --git a/ocl.c b/ocl.c index 73ea6bda..50336f1c 100644 --- a/ocl.c +++ b/ocl.c @@ -384,10 +384,7 @@ _clState *initCl(unsigned int gpu, char *name, size_t nameSize) return NULL; } - if (cgpu->shaders) { - clState->compute_shaders = cgpu->shaders; - } - else { + if (!cgpu->shaders) { // AMD architechture got 64 compute shaders per compute unit. // Source: http://www.amd.com/us/Documents/GCN_Architecture_whitepaper.pdf clState->compute_shaders = compute_units * 64; From 32125026cfad6e37f8e2fb0566b2c0871102a13d Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 21 Feb 2014 09:17:24 +0100 Subject: [PATCH 15/15] Fixed typo. --- scrypt.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scrypt.c b/scrypt.c index f602beef..f0304344 100644 --- a/scrypt.c +++ b/scrypt.c @@ -479,7 +479,7 @@ bool scanhash_scrypt(struct thr_info *thr, const unsigned char __maybe_unused *p *nonce = ++n; data[19] = htobe32(n); - scrypt_n_1_1_256_sp(data, scratchbuf, ohash, (1 << nfactor)); + scrypt_n_1_1_256_sp(data, scratchbuf, ostate, (1 << nfactor)); tmp_hash7 = be32toh(ostate[7]); if (unlikely(tmp_hash7 <= Htarg)) { @@ -495,6 +495,6 @@ bool scanhash_scrypt(struct thr_info *thr, const unsigned char __maybe_unused *p } } - free(scratchbuf);; + free(scratchbuf); return ret; }