forked from todcnst1453/trueminer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfchainhash.cu
305 lines (267 loc) · 5.52 KB
/
fchainhash.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include "gpu_var.h"
#include "hash.h"
#include "sha3.h"
__device__ uint64 gFoundIdx = uint64(-1);
__constant__ int kXor[256];
int genLookupTable(uint64 *plookup, uint32 *ptable)
{
uint64 *ptbl = plookup;
int lkt_wz = (DATA_LENGTH) / 64;
int lkt_sz = DATA_LENGTH*lkt_wz;
int idx = 0;
for (int k = 0; k < 16; k++)
{
uint64 *plkt = plookup+k*lkt_sz;
uint32 *ptbl = ptable + k*DATA_LENGTH*PMT_SIZE;
for (int x = 0; x < DATA_LENGTH; x++)
{
if (x == 0 && k == 13)
x = x;
for (int y = 0; y < PMT_SIZE; y++)
{
int val = *ptable;
if (val == 0xFFF)
{
ptable++;
continue;
}
int v_i = val / 64;
int v_r = val % 64;
plkt[v_i] |= ((uint64)1 << v_r);
ptable++;
}
plkt += lkt_wz;
}
//printf("\n");
}
return 0;
}
__device__ int xor64(uint64 val)
{
int r = 0;
for (int k = 0; k < 8 && val; k++)
{
r ^= kXor[val & 0xFF];
val >>= 8;
}
return r;
}
__device__ int muliple(uint64 input[32], uint64 *prow)
{
int r = 0;
for (int k = 0; k < 32; k++)
{
if (input[k] != 0 && prow[k] != 0)
r ^= xor64(input[k] & prow[k]);
}
return r;
}
__device__ int MatMuliple(uint64 input[32], uint64 output[32], uint64 *pmat)
{
uint64 *prow = pmat;
for (int k = 0; k < 2048; k++)
{
int k_i = k / 64;
int k_r = k % 64;
unsigned int temp;
temp = muliple(input, prow);
output[k_i] |= ((uint64)temp << k_r);
prow += 32;
}
return 0;
}
__device__ int shift2048(uint64 in[32], int sf)
{
int sf_i = sf / 64;
int sf_r = sf % 64;
uint64 mask = ((uint64)1 << sf_r) - 1;
int bits = (64 - sf_r);
uint64 res;
if (sf_i == 1)
{
uint64 val = in[0];
for (int k = 0; k < 31; k++)
{
in[k] = in[k + 1];
}
in[31] = val;
}
res = (in[0] & mask) << bits;
for (int k = 0; k < 31; k++)
{
uint64 val = (in[k + 1] & mask) << bits;
in[k] = (in[k] >> sf_r) + val;
}
in[31] = (in[31] >> sf_r) + res;
return 0;
}
__device__ int scramble(uint64 *permute_in)
{
uint64 *ptbl;
uint64 permute_out[32] = { 0 };
for (int k = 0; k < 64; k++)
{
int sf, bs;
sf = permute_in[0] & 0x7f;
bs = permute_in[31] >> 60;
ptbl = gTable + bs * 2048 * 32;
MatMuliple(permute_in, permute_out, ptbl);
shift2048(permute_out, sf);
for (int k = 0; k < 32; k++)
{
permute_in[k] = permute_out[k];
permute_out[k] = 0;
}
}
return 0;
}
__device__ int byteReverse(byte sha512_out[64])
{
for (int k = 0; k < 32; k++)
{
byte temp = sha512_out[k];
sha512_out[k] = sha512_out[63 - k];
sha512_out[63 - k] = temp;
}
return 0;
}
int convertLE(byte header[HEAD_SIZE])
{
int wz = HEAD_SIZE / 4;
for (int k = 0; k < wz; k++)
{
byte temp[4];
temp[0] = header[k * 4 + 3];
temp[1] = header[k * 4 + 2];
temp[2] = header[k * 4 + 1];
temp[3] = header[k * 4 + 0];
header[k * 4 + 0] = temp[0];
header[k * 4 + 1] = temp[1];
header[k * 4 + 2] = temp[2];
header[k * 4 + 3] = temp[3];
}
return 0;
}
__device__ int convertWD(byte header[HEAD_SIZE])
{
byte temp[HEAD_SIZE];
int wz = HEAD_SIZE / 4;
for (int k = 0; k < wz; k++)
{
int i = 7 - k;
temp[k * 4] = header[i * 4];
temp[k * 4 + 1] = header[i * 4 + 1];
temp[k * 4 + 2] = header[i * 4 + 2];
temp[k * 4 + 3] = header[i * 4 + 3];
}
for (int k = 0; k < HEAD_SIZE; k++)
{
header[k] = temp[k];
}
return 0;
}
__device__ int compare(byte dgst[DGST_SIZE], byte target1[TARG_SIZE], byte target2[TARG_SIZE])
{
for (int k = TARG_SIZE - 1; k >= 0; k--)
{
int dif = (int)dgst[k] - (int)target1[k];
if (dif > 0)
return 0;
if (dif < 0)
return 1;
}
for (int k = TARG_SIZE - 1; k >= 0; k--)
{
int dif = (int)dgst[k + 16] - (int)target2[k];
if (dif > 0)
return 0;
if (dif < 0)
return 1;
}
return 0;
}
__global__ void compute(uint64 nonce_start)
{
byte digs[DGST_SIZE];
const uint64 offset = gridDim.x * blockDim.x;
nonce_start += threadIdx.x + blockIdx.x * blockDim.x;
while (nonce_start < gFoundIdx)
{
fchainhash(nonce_start, digs);
if (compare(digs, kTarget1, kTarget2) == 1)
{
atomicMin((unsigned long long int*)&gFoundIdx, unsigned long long int(nonce_start));
break;
}
// Get result here
printf("Current nonce : %llu\n", nonce_start);
nonce_start += offset;
}
}
__device__ void fchainhash(uint64 nonce, byte digs[DGST_SIZE])
{
byte seed[64] = { 0 };
byte output[DGST_SIZE] = { 0 };
uint32 val0 = (uint32)(nonce & 0xFFFFFFFF);
uint32 val1 = (uint32)(nonce >> 32);
for (int k = 3; k >= 0; k--)
{
seed[k] = val0 & 0xFF;
val0 >>= 8;
}
for (int k = 7; k >= 4; k--)
{
seed[k] = val1 & 0xFF;
val1 >>= 8;
}
for (int k = 0; k < HEAD_SIZE; k++)
{
seed[k+8] = kInput[k];
}
byte sha512_out[64];
sha3(seed, 64, sha512_out, 64);
byteReverse(sha512_out);
uint64 permute_in[32] = { 0 }, permute_out[32] = {0};
for (int k = 0; k < 8; k++)
{
for (int x = 0; x < 8; x++)
{
int sft = x * 8;
uint64 val = ((uint64)sha512_out[k*8+x] << sft);
permute_in[k] += val;
}
}
for (int k = 1; k < 4; k++)
{
for (int x = 0; x < 8; x++)
permute_in[k * 8 + x] = permute_in[x];
}
scramble(permute_in);
byte dat_in[256];
for (int k = 0; k < 32; k++)
{
uint64 val = permute_in[k];
for (int x = 0; x < 8; x++)
{
dat_in[k * 8 + x] = val & 0xFF;
val = val >> 8;
}
}
for (int k = 0; k < 64; k++)
{
byte temp;
temp = dat_in[k * 4];
dat_in[k * 4] = dat_in[k * 4 + 3];
dat_in[k * 4 + 3] = temp;
temp = dat_in[k * 4 + 1];
dat_in[k * 4 + 1] = dat_in[k * 4 + 2];
dat_in[k * 4 + 2] = temp;
}
//unsigned char output[64];
sha3(dat_in, 256, output, 32);
// reverse byte
for (int k = 0; k < DGST_SIZE; k++)
{
digs[k] = output[DGST_SIZE - k - 1];
}
}