Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tpruvot
GitHub Repository: tpruvot/cpuminer-multi
Path: blob/linux/algo/blake2.c
1201 views
1
/**
2
* Blake2-S 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/blake2s.h"
12
13
static __thread blake2s_state s_midstate;
14
static __thread blake2s_state s_ctx;
15
#define MIDLEN 76
16
#define A 64
17
18
void blake2s_hash(void *output, const void *input)
19
{
20
uint8_t _ALIGN(A) hash[BLAKE2S_OUTBYTES];
21
blake2s_state blake2_ctx;
22
23
blake2s_init(&blake2_ctx, BLAKE2S_OUTBYTES);
24
blake2s_update(&blake2_ctx, input, 80);
25
blake2s_final(&blake2_ctx, hash, BLAKE2S_OUTBYTES);
26
27
memcpy(output, hash, 32);
28
}
29
30
static void blake2s_hash_end(uint32_t *output, const uint32_t *input)
31
{
32
s_ctx.buflen = MIDLEN;
33
memcpy(&s_ctx, &s_midstate, 32 + 16 + MIDLEN);
34
blake2s_update(&s_ctx, (uint8_t*) &input[MIDLEN/4], 80 - MIDLEN);
35
blake2s_final(&s_ctx, (uint8_t*) output, BLAKE2S_OUTBYTES);
36
}
37
38
int scanhash_blake2s(int thr_id, struct work *work, uint32_t max_nonce, uint64_t *hashes_done)
39
{
40
uint32_t _ALIGN(A) vhashcpu[8];
41
uint32_t _ALIGN(A) endiandata[20];
42
uint32_t *pdata = work->data;
43
uint32_t *ptarget = work->target;
44
45
const uint32_t Htarg = ptarget[7];
46
const uint32_t first_nonce = pdata[19];
47
48
uint32_t n = first_nonce;
49
50
for (int i=0; i < 19; i++) {
51
be32enc(&endiandata[i], pdata[i]);
52
}
53
54
// midstate
55
blake2s_init(&s_midstate, BLAKE2S_OUTBYTES);
56
blake2s_update(&s_midstate, (uint8_t*) endiandata, MIDLEN);
57
memcpy(&s_ctx, &s_midstate, sizeof(blake2s_state));
58
59
do {
60
be32enc(&endiandata[19], n);
61
blake2s_hash_end(vhashcpu, endiandata);
62
63
//blake2s_hash(vhashcpu, endiandata);
64
if (vhashcpu[7] < Htarg && fulltest(vhashcpu, ptarget)) {
65
work_set_target_ratio(work, vhashcpu);
66
*hashes_done = n - first_nonce + 1;
67
pdata[19] = n;
68
return 1;
69
}
70
n++;
71
72
} while (n < max_nonce && !work_restart[thr_id].restart);
73
74
*hashes_done = n - first_nonce + 1;
75
pdata[19] = n;
76
77
return 0;
78
}
79
80