Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tpruvot
GitHub Repository: tpruvot/cpuminer-multi
Path: blob/linux/algo/sia.c
1201 views
1
/**
2
* Blake2-B Implementation
3
* tpruvot@github 2015-2016
4
*/
5
6
#include "miner.h"
7
8
#include <string.h>
9
#include <stdint.h>
10
11
#include <crypto/blake2b.h>
12
13
#define A 64
14
15
#ifdef MIDSTATE
16
static __thread blake2b_ctx s_midstate;
17
static __thread blake2b_ctx s_ctx;
18
#define MIDLEN 76
19
20
static void blake2b_hash_end(uint32_t *output, const uint32_t *input)
21
{
22
s_ctx.outlen = MIDLEN;
23
memcpy(&s_ctx, &s_midstate, 32 + 16 + MIDLEN);
24
blake2b_update(&s_ctx, (uint8_t*) &input[MIDLEN/4], 80 - MIDLEN);
25
blake2b_final(&s_ctx, (uint8_t*) output);
26
}
27
#endif
28
29
void blake2b_hash(void *output, const void *input)
30
{
31
uint8_t _ALIGN(A) hash[32];
32
blake2b_ctx ctx;
33
34
blake2b_init(&ctx, 32, NULL, 0);
35
blake2b_update(&ctx, input, 80);
36
blake2b_final(&ctx, hash);
37
38
memcpy(output, hash, 32);
39
}
40
41
int scanhash_blake2b(int thr_id, struct work *work, uint32_t max_nonce, uint64_t *hashes_done)
42
{
43
uint32_t _ALIGN(A) vhashcpu[8];
44
uint32_t _ALIGN(A) endiandata[20];
45
uint32_t *pdata = work->data;
46
uint32_t *ptarget = work->target;
47
48
const uint32_t Htarg = ptarget[7];
49
const uint32_t first_nonce = pdata[19];
50
51
uint32_t n = first_nonce;
52
53
for (int i=0; i < 19; i++) {
54
be32enc(&endiandata[i], pdata[i]);
55
}
56
57
// midstate (untested yet)
58
//blake2b_init(&s_midstate, 32, NULL, 0);
59
//blake2b_update(&s_midstate, (uint8_t*) endiandata, MIDLEN);
60
//memcpy(&s_ctx, &s_midstate, sizeof(blake2b_ctx));
61
62
do {
63
be32enc(&endiandata[19], n);
64
//blake2b_hash_end(vhashcpu, endiandata);
65
blake2b_hash(vhashcpu, endiandata);
66
67
if (vhashcpu[7] < Htarg && fulltest(vhashcpu, ptarget)) {
68
work_set_target_ratio(work, vhashcpu);
69
*hashes_done = n - first_nonce + 1;
70
pdata[19] = n;
71
return 1;
72
}
73
n++;
74
75
} while (n < max_nonce && !work_restart[thr_id].restart);
76
77
*hashes_done = n - first_nonce + 1;
78
pdata[19] = n;
79
80
return 0;
81
}
82
83
static inline void swab256(void *dest_p, const void *src_p)
84
{
85
uint32_t *dest = (uint32_t *)dest_p;
86
const uint32_t *src = (uint32_t *)src_p;
87
88
dest[0] = swab32(src[7]);
89
dest[1] = swab32(src[6]);
90
dest[2] = swab32(src[5]);
91
dest[3] = swab32(src[4]);
92
dest[4] = swab32(src[3]);
93
dest[5] = swab32(src[2]);
94
dest[6] = swab32(src[1]);
95
dest[7] = swab32(src[0]);
96
}
97
98
int scanhash_sia(int thr_id, struct work *work, uint32_t max_nonce, uint64_t *hashes_done)
99
{
100
uint32_t _ALIGN(A) hash[8];
101
uint32_t _ALIGN(A) vhashcpu[8];
102
uint32_t _ALIGN(A) inputdata[20];
103
uint32_t *pdata = work->data;
104
uint32_t *ptarget = work->target;
105
106
const uint32_t Htarg = ptarget[7];
107
const uint32_t first_nonce = pdata[8];
108
109
uint32_t n = first_nonce;
110
111
memcpy(inputdata, pdata, 80);
112
inputdata[11] = 0; // nbits
113
114
do {
115
inputdata[8] = n;
116
blake2b_hash(hash, inputdata);
117
if (swab32(hash[0]) < Htarg) {
118
swab256(vhashcpu, hash);
119
if (fulltest(vhashcpu, ptarget)) {
120
work_set_target_ratio(work, vhashcpu);
121
*hashes_done = n - first_nonce + 1;
122
pdata[8] = n;
123
return 1;
124
}
125
}
126
n++;
127
128
} while (n < max_nonce && !work_restart[thr_id].restart);
129
130
*hashes_done = n - first_nonce + 1;
131
pdata[8] = n;
132
133
return 0;
134
}
135
136