Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tpruvot
GitHub Repository: tpruvot/cpuminer-multi
Path: blob/linux/algo/decred.c
1201 views
1
#include "miner.h"
2
3
#include "sha3/sph_blake.h"
4
5
#include <string.h>
6
#include <stdint.h>
7
#include <memory.h>
8
9
static __thread sph_blake256_context blake_mid;
10
static __thread bool ctx_midstate_done = false;
11
12
void decred_hash(void *state, const void *input)
13
{
14
#define MIDSTATE_LEN 128
15
sph_blake256_context ctx;
16
17
uint8_t *ending = (uint8_t*) input;
18
ending += MIDSTATE_LEN;
19
20
if (!ctx_midstate_done) {
21
sph_blake256_init(&blake_mid);
22
sph_blake256(&blake_mid, input, MIDSTATE_LEN);
23
ctx_midstate_done = true;
24
}
25
memcpy(&ctx, &blake_mid, sizeof(blake_mid));
26
27
sph_blake256(&ctx, ending, (180 - MIDSTATE_LEN));
28
sph_blake256_close(&ctx, state);
29
}
30
31
void decred_hash_simple(void *state, const void *input)
32
{
33
sph_blake256_context ctx;
34
sph_blake256_init(&ctx);
35
sph_blake256(&ctx, input, 180);
36
sph_blake256_close(&ctx, state);
37
}
38
39
int scanhash_decred(int thr_id, struct work *work, uint32_t max_nonce, uint64_t *hashes_done)
40
{
41
uint32_t _ALIGN(128) endiandata[48];
42
uint32_t _ALIGN(128) hash32[8];
43
uint32_t *pdata = work->data;
44
uint32_t *ptarget = work->target;
45
46
#define DCR_NONCE_OFT32 35
47
48
const uint32_t first_nonce = pdata[DCR_NONCE_OFT32];
49
const uint32_t HTarget = opt_benchmark ? 0x7f : ptarget[7];
50
51
uint32_t n = first_nonce;
52
53
ctx_midstate_done = false;
54
55
#if 1
56
memcpy(endiandata, pdata, 180);
57
#else
58
for (int k=0; k < (180/4); k++)
59
be32enc(&endiandata[k], pdata[k]);
60
#endif
61
62
#ifdef DEBUG_ALGO
63
if (!thr_id) applog(LOG_DEBUG,"[%d] Target=%08x %08x", thr_id, ptarget[6], ptarget[7]);
64
#endif
65
66
do {
67
//be32enc(&endiandata[DCR_NONCE_OFT32], n);
68
endiandata[DCR_NONCE_OFT32] = n;
69
decred_hash(hash32, endiandata);
70
71
if (hash32[7] <= HTarget && fulltest(hash32, ptarget)) {
72
work_set_target_ratio(work, hash32);
73
*hashes_done = n - first_nonce + 1;
74
#ifdef DEBUG_ALGO
75
applog(LOG_BLUE, "Nonce : %08x %08x", n, swab32(n));
76
applog_hash(ptarget);
77
applog_compare_hash(hash32, ptarget);
78
#endif
79
pdata[DCR_NONCE_OFT32] = n;
80
return 1;
81
}
82
83
n++;
84
85
} while (n < max_nonce && !work_restart[thr_id].restart);
86
87
*hashes_done = n - first_nonce + 1;
88
pdata[DCR_NONCE_OFT32] = n;
89
return 0;
90
}
91
92