forked from SlimcoinMiner/slimminer-opencl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcrypt.c
519 lines (424 loc) · 16 KB
/
dcrypt.c
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
// Copyright (c) 2013-2014 The Slimcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <string.h>
#include <stdlib.h>
#include <math.h> //pow
#include "miner.h"
#include "dcrypt.h"
//the base size for malloc/realloc will be 1KB
#define REALLOC_BASE_SZ (1024*4)
typedef struct
{
uint8_t *array;
unsigned long long actual_array_sz;
uint32_t times_realloced;
} Extend_Array;
inline void Extend_Array_init(Extend_Array *ExtArray)
{
//initial values
ExtArray->array = 0;
ExtArray->actual_array_sz = 0;
ExtArray->times_realloced = 0;
return;
}
uint32_t hex_char_to_int(uint8_t c)
{
if(c >= '0' && c <= '9')
return c - '0';
if(c >= 'a' && c <= 'f')
return 10 + c - 'a';
if(c >= 'A' && c <= 'F')
return 10 + c - 'A';
return -1;
}
inline void join_to_array(uint8_t *array, uint8_t join)
{
*(array + SHA256_LEN) = join;
return;
}
void extend_array(Extend_Array *extend_array, unsigned long long used_array_sz,
uint8_t *extend, uint32_t extend_sz, uint8_t hashed_end)
{
if(!extend_array)
return;
//make a few references
unsigned long long *actual_array_sz = &extend_array->actual_array_sz;
uint32_t *times_realloced = &extend_array->times_realloced;
//if there is not enough room
if((*actual_array_sz - used_array_sz) < (extend_sz + hashed_end))
{
//if extend_array->array has already been malloc'd
if(*times_realloced)
{
//reallocate on an exponential curve, modern computers have plenty ram
*actual_array_sz += pow(2, (*times_realloced)++) * REALLOC_BASE_SZ;
extend_array->array = (uint8_t*)realloc(extend_array->array, *actual_array_sz);
}else{
//allocate the base size
*actual_array_sz += REALLOC_BASE_SZ;
(*times_realloced)++;
extend_array->array = (uint8_t*)malloc(*actual_array_sz); //if we have not allocated anything, malloc
}
}
//copy the data to be extended
memcpy(extend_array->array + used_array_sz, extend, extend_sz);
if(hashed_end)
*(extend_array->array + used_array_sz + extend_sz) = 0; //add the final \000 of the whole string array
return;
}
uint64 mix_hashed_nums(uint8_t *hashed_nums, const uint8_t *unhashedData, size_t unhashed_sz,
uint8_t **mixed_hash, uint8_t *hash_digest)
{
uint32_t i, index = 0;
const uint32_t hashed_nums_len = SHA256_LEN;
uint64 count;
uint8_t tmp_val, tmp_array[SHA256_LEN + 2];
//initialize the class for the extend hash
Extend_Array new_hash;
Extend_Array_init(&new_hash);
//set the first hash length in the temp array to all 0xff
memset(tmp_array, 0xff, SHA256_LEN);
//set the last two bytes to \000
*(tmp_array + SHA256_LEN) = *(tmp_array + SHA256_LEN + 1) = 0;
for(count = 0;; count++)
{
//+1 to keeps a 0 value of *(hashed_nums + index) moving on
i = hex_char_to_int(*(hashed_nums + index)) + 1;
index += i;
//if we hit the end of the hash, rehash it
if(index >= hashed_nums_len)
{
index = index % hashed_nums_len;
sha256_to_str(hashed_nums, hashed_nums_len, hashed_nums, hash_digest); //rescramble
}
tmp_val = *(hashed_nums + index);
join_to_array(tmp_array, tmp_val); //plop tmp_val at the end of tmp_array
sha256_to_str(tmp_array, SHA256_LEN + 1, tmp_array, hash_digest);
//extend the expanded hash to the array
extend_array(&new_hash, count * SHA256_LEN, tmp_array, SHA256_LEN, false);
//check if the last value of hashed_nums is the same as the last value in tmp_array
if(index == hashed_nums_len - 1)
if(tmp_val == *(tmp_array + SHA256_LEN - 1))
{
//add to count since we extended the array, but break will exit the for loop and count
// will not get incremenented by the for loop
count++;
break;
}
}
//extend the unhashed data to the end and add the \000 to the end
extend_array(&new_hash, count * SHA256_LEN, (u8int*)unhashedData, unhashed_sz, true);
//assign the address of new_hash's array to mixed_hash
*mixed_hash = new_hash.array;
return count * SHA256_LEN + unhashed_sz;
}
u8int *dcrypt_buffer_alloc()
{
return malloc(DCRYPT_DIGEST_LENGTH);
}
void dcrypt(const uint8_t *data, size_t data_sz, uint8_t *hash_digest, u32int *hashRet)
{
uint8_t hashed_nums[SHA256_LEN + 1], *mix_hash;
bool allocDigest = false;
if(!hash_digest)
{
hash_digest = (uint8_t*)malloc(DCRYPT_DIGEST_LENGTH);
allocDigest = true;
}
sha256_to_str(data, data_sz, hashed_nums, hash_digest);
//mix the hashes up, magority of the time takes here
uint64 mix_hash_len = mix_hashed_nums(hashed_nums, data, data_sz, &mix_hash, hash_digest);
//apply the final hash to the output
sha256((const uint8_t*)mix_hash, mix_hash_len, hashRet);
free(mix_hash);
if(allocDigest)
free(hash_digest);
//sucess
return;
}
// CPU Optimized
void extend_array_opt(uint8_t *array, unsigned long long used_array_sz,
uint8_t *extend, uint32_t extend_sz, uint8_t hashed_end)
{
//if there is not enough room
if(REALLOC_BASE_SZ < (used_array_sz + extend_sz + hashed_end))
{
applog(LOG_ERR, "Extend array: %u, %u", used_array_sz, REALLOC_BASE_SZ);
exit(-1);
}
//copy the data to be extended
memcpy(array + used_array_sz, extend, extend_sz);
if(hashed_end)
*(array + used_array_sz + extend_sz) = 0; //add the final \000 of the whole string array
return;
}
int mix_hashed_nums_opt(uint8_t *hashed_nums, const uint8_t *unhashedData, size_t unhashed_sz,
uint8_t *mixed_hash, uint8_t *hash_digest)
{
uint32_t i, index = 0;
const uint32_t hashed_nums_len = SHA256_LEN;
uint64 count;
uint8_t tmp_val, tmp_array[SHA256_LEN + 2];
bool found = false;
for(count = 0; count < opt_depth; count++)
{
//+1 to keeps a 0 value of *(hashed_nums + index) moving on
i = hex_char_to_int(*(hashed_nums + index)) + 1;
index += i;
//if we hit the end of the hash, rehash it
if(index >= hashed_nums_len)
break;
if(index == hashed_nums_len - 1)
found = true;
}
if (!found)
return -1;
index = 0;
//initialize the class for the extend hash
//set the first hash length in the temp array to all 0xff
memset(tmp_array, 0xff, SHA256_LEN);
//set the last two bytes to \000
*(tmp_array + SHA256_LEN) = *(tmp_array + SHA256_LEN + 1) = 0;
for(count = 0;; count++)
{
//+1 to keeps a 0 value of *(hashed_nums + index) moving on
i = hex_char_to_int(*(hashed_nums + index)) + 1;
index += i;
//if we hit the end of the hash, rehash it
if(index >= hashed_nums_len)
{
applog(LOG_ERR, "Out of array: %u", index);
return -2;
// index = index % hashed_nums_len;
// sha256_to_str(hashed_nums, hashed_nums_len, hashed_nums, hash_digest); //rescramble
}
tmp_val = *(hashed_nums + index);
join_to_array(tmp_array, tmp_val); //plop tmp_val at the end of tmp_array
sha256_to_str(tmp_array, SHA256_LEN + 1, tmp_array, hash_digest);
//extend the expanded hash to the array
extend_array_opt(mixed_hash, count * SHA256_LEN, tmp_array, SHA256_LEN, false);
if (count * SHA256_LEN + SHA256_LEN >= REALLOC_BASE_SZ)
applog(LOG_INFO, "Extend array on step %u ", count);
//check if the last value of hashed_nums is the same as the last value in tmp_array
if(index == hashed_nums_len - 1) {
if(tmp_val == *(tmp_array + SHA256_LEN - 1))
{
// applog(LOG_INFO, "Last char check passed %c - %c", tmp_val, *(tmp_array + SHA256_LEN - 1));
// applog(LOG_INFO, "Found path in %u steps, %c - %c", count, tmp_val, *(tmp_array + SHA256_LEN - 1));
//add to count since we extended the array, but break will exit the for loop and count
// will not get incremenented by the for loop
count++;
break;
}
// applog(LOG_INFO, "Last char check failed %c - %c", tmp_val, *(tmp_array + SHA256_LEN - 1));
return 0;
}
}
//extend the unhashed data to the end and add the \000 to the end
extend_array_opt(mixed_hash, count * SHA256_LEN, (u8int*)unhashedData, unhashed_sz, true);
if (count * SHA256_LEN + unhashed_sz >= REALLOC_BASE_SZ)
applog(LOG_INFO, "Extend array with data on step %u ", count);
return count * SHA256_LEN + unhashed_sz;
}
int dcrypt_opt(const uint8_t *data, size_t data_sz, uint8_t *hash_digest, u32int *hashRet, uint8_t *mix_hash)
{
uint8_t hashed_nums[SHA256_LEN + 1];
sha256_to_str(data, data_sz, hashed_nums, hash_digest);
//mix the hashes up, magority of the time takes here
int mix_hash_len = mix_hashed_nums_opt(hashed_nums, data, data_sz, mix_hash, hash_digest);
//apply the final hash to the output
if (mix_hash_len > 0)
sha256((const uint8_t*)mix_hash, mix_hash_len, hashRet);
//sucess
return mix_hash_len;
}
//#define OUTPUT_SIZE 0xffffff
//#define OUTPUT_COUNTER ((OUTPUT_SIZE >> 2) - 1)
// OpenCL version
int scanhash_dcrypt_gpu(int thr_id, uint32_t *pdata, unsigned char *digest,
const uint32_t *ptarget, uint32_t max_nonce, unsigned long *hashes_done,
unsigned long *hashes_checked, GPU *gpu) {
uint32_t block[20], hash[8];
uint32_t nNonce = pdata[19] - 1;
const uint32_t first_nonce = pdata[19];
const uint32_t Htarg = ptarget[7]; //the last element in the target is the first 32 bits of the target
uint8_t *new_hash = (uint8_t*) malloc(REALLOC_BASE_SZ);
//copy the block (first 80 bytes of pdata) into block
memcpy(block, pdata, 80);
nNonce = max_nonce;
uint32_t hashes = scanhash_dcrypt_opencl(thr_id, block, &target, first_nonce, max_nonce, gpu);
uint32_t shares = gpu->output[(gpu->output_size >> 2) - 1];
if (shares >= 255) {
applog(LOG_ERR, "GPU: %02d, to many shares %u", thr_id, shares);
return 0;
}
int j;
for (j = 0; j < 19; j++)
block[j] = swab32(block[j]);
/*
int i;
for (i = 0; i < gpu->output[OUTPUT_COUNTER]; i++) {
int nonce = gpu->output[i*9];
uint8_t hash_str[SHA256_LEN + 1];
digest_to_string(&gpu->output[i*9+1], hash_str);
block[19] = nonce;
uint8_t hashed_nums[SHA256_LEN + 1];
sha256_to_str(block, 80, hashed_nums, digest);
applog(LOG_INFO, "GPU: %02d, %s", thr_id, hashed_nums);
applog(LOG_INFO, "GPU: %02d, found: %u, %s", thr_id, nonce, hash_str);
}
*/
*hashes_checked += hashes;
applog(LOG_DEBUG, "GPU: %02d, hashes: checked %u, found %u", thr_id, hashes, shares);
int i = 0;
for (; i < gpu->output[(gpu->output_size >> 2) - 1]; i++) {
uint32_t nonce = gpu->output[i];
// uint32_t last = gpu->output[i*2+1];
block[19] = nonce;
uint8_t hashed_nums[SHA256_LEN + 1];
sha256_to_str(block, 80, hashed_nums, digest);
// applog(LOG_INFO, "GPU: %02d, %s", thr_id, hashed_nums);
// int status = dcrypt_opt((u8int*) block, 80, digest, hash, new_hash);
int status = 1;
dcrypt((u8int*) block, 80, digest, hash);
applog(LOG_DEBUG,
"GPU: %02d, share found: %u, %08x%08x%08x%08x%08x%08x%08x%08x",
thr_id, nonce, hash[7], hash[6], hash[5], hash[4], hash[3],
hash[2], hash[1], hash[0]);
if (status > 0) {
//hash[7] <= Htarg just compares the first 32 bits of the hash with the target
// full_test fully compares the entire hash with the entire target
if(hash[7] <= target) {
applog(LOG_INFO,
"GPU: %02d, share validated: %u, %08x%08x%08x%08x%08x%08x%08x%08x",
thr_id, nonce, hash[7], hash[6], hash[5], hash[4], hash[3],
hash[2], hash[1], hash[0]);
shares_found++;
}
else
applog(LOG_INFO,
"GPU: %02d, share non valid: %u, %08x%08x%08x%08x%08x%08x%08x%08x",
thr_id, nonce, hash[7], hash[6], hash[5], hash[4], hash[3],
hash[2], hash[1], hash[0]);
if(hash[7] <= Htarg && fulltest(hash, ptarget)) {
applog(LOG_INFO,
"GPU: %02d, block found: %u, %08x%08x%08x%08x%08x%08x%08x%08x",
thr_id, nonce, hash[7], hash[6], hash[5], hash[4], hash[3],
hash[2], hash[1], hash[0]);
*hashes_done = nNonce - first_nonce + 1;
pdata[19] = nonce;
return true;
}
} else {
// if (status < 0)
applog(LOG_INFO, "GPU: %02d, hash skipped: %u, status=%d", thr_id, nonce, status);
}
}
*hashes_done = nNonce - pdata[19] + 1;
pdata[19] = nNonce;
//No luck yet
free(new_hash);
return 0;
}
// CPU optimized version
int scanhash_dcrypt_opt(int thr_id, uint32_t *pdata, unsigned char *digest,
const uint32_t *ptarget, uint32_t max_nonce, unsigned long *hashes_done,
unsigned long *hashes_checked) {
uint32_t block[20], hash[8], hash2[8];
uint32_t nNonce = pdata[19] - 1;
const uint32_t first_nonce = pdata[19];
const uint32_t Htarg = ptarget[7]; //the last element in the target is the first 32 bits of the target
uint8_t *new_hash = (uint8_t*) malloc(REALLOC_BASE_SZ);
uint8_t digest2[32];
//copy the block (first 80 bytes of pdata) into block
memcpy(block, pdata, 80);
int j;
for (j = 0; j < 19; j++)
block[j] = swab32(block[j]);
do {
//increment nNonce
block[19] = ++nNonce;
if (dcrypt_opt((u8int*) block, 80, digest, hash, new_hash) > 0) {
*hashes_checked += 1;
if(hash[7] <= target) {
shares_found++;
dcrypt((u8int*) block, 80, digest2, hash2);
if (hash2[7] <= target)
applog(LOG_INFO,
"GPU: %02d, share validated: %u, %08x%08x%08x%08x%08x%08x%08x%08x",
thr_id, nNonce, hash[7], hash[6], hash[5], hash[4], hash[3],
hash[2], hash[1], hash[0]);
else
applog(LOG_INFO,
"GPU: %02d, share non valid: %u, %08x%08x%08x%08x%08x%08x%08x%08x",
thr_id, nNonce, hash2[7], hash2[6], hash2[5], hash2[4], hash2[3],
hash2[2], hash2[1], hash2[0]);
}
if (hash[7] <= Htarg && fulltest(hash, ptarget)) {
*hashes_done = nNonce - pdata[19] + 1;
pdata[19] = block[19];
//found a hash!
return 1;
}
}
} while (nNonce < max_nonce && !work_restart[thr_id].restart);
*hashes_done = nNonce - pdata[19] + 1;
pdata[19] = nNonce;
//No luck yet
free(new_hash);
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////
//////////////////// Various tests
////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
/* Tests the comparison to two hashes
//Hash the word "Dog" with Dcrypt and strinify the hash
u32int ret[8];
char string[65];
dcrypt("Dog", 3, 0, ret);
digest_to_string((u8int*)ret, string);
printf("String is %s\n", string);
//hash the word "Doge" with Dcrypt and stringify the hash
u32int ret2[8];
char string2[65];
dcrypt("Doge", 4, 0, ret2);
digest_to_string((u8int*)ret2, string2);
printf("String2 is %s\n", string2);
//compare the last elements, which correspond the the uint256's first 32 bytes
if(ret[7] < ret2[7])
printf("String1 is smaller %08x < %08x\n", ret[7], ret2[7]);
else
printf("String1 is greater %08x >= %08x\n", ret[7], ret2[7]);
//Apply the full test to make sure
printf("Full test returns %d\n", fulltest(ret2, ret));
*/
/* Tests the scan feature of dcrypt
u8int digest[DCRYPT_DIGEST_LENGTH], string[65], strTarget[65];
unsigned long hDone;
u32int pdata[20], retHash[8], target[8];
//fill pdata with something
memset(pdata, 0xff, 20 * sizeof(u32int));
pdata[19] = 0; //element 19 is the beginning of where nNonce begins
//fill the target with 1's
memset(target, 0xff, 8 * sizeof(u32int));
//the last element is the uint256's first 32 bits, set the target to 0x00000ffffffffff....
target[7] = 0x000ffff;
//scan for them hashes
scanhash_dcrypt(0, pdata, digest, target, -1, &hDone);
//Get the hash of pdata
dcrypt((u8int*)pdata, 80, digest, retHash);
//stringify the returned hash and the target
digest_to_string((u8int*)retHash, string);
digest_to_string((u8int*)target, strTarget);
printf(" Hash is %s %08x\n", string, retHash[7]);
printf("Target is %s %08x\n", strTarget, target[7]);
printf("Nonce %d Hashes Done %ld\n", pdata[19], hDone);
*/
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////
//////////////////// Various tests
////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////