Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tpruvot
GitHub Repository: tpruvot/cpuminer-multi
Path: blob/linux/algo/nist5.c
1201 views
1
#include "miner.h"
2
3
#include <stdlib.h>
4
#include <stdint.h>
5
#include <string.h>
6
#include <stdio.h>
7
8
#include "sha3/sph_blake.h"
9
#include "sha3/sph_groestl.h"
10
#include "sha3/sph_skein.h"
11
#include "sha3/sph_jh.h"
12
#include "sha3/sph_keccak.h"
13
14
void nist5hash(void *output, const void *input)
15
{
16
sph_blake512_context ctx_blake;
17
sph_groestl512_context ctx_groestl;
18
sph_jh512_context ctx_jh;
19
sph_keccak512_context ctx_keccak;
20
sph_skein512_context ctx_skein;
21
22
uint8_t hash[64];
23
24
sph_blake512_init(&ctx_blake);
25
sph_blake512(&ctx_blake, input, 80);
26
sph_blake512_close(&ctx_blake, (void*) hash);
27
28
sph_groestl512_init(&ctx_groestl);
29
sph_groestl512(&ctx_groestl, (const void*) hash, 64);
30
sph_groestl512_close(&ctx_groestl, (void*) hash);
31
32
sph_jh512_init(&ctx_jh);
33
sph_jh512(&ctx_jh, (const void*) hash, 64);
34
sph_jh512_close(&ctx_jh, (void*) hash);
35
36
sph_keccak512_init(&ctx_keccak);
37
sph_keccak512(&ctx_keccak, (const void*) hash, 64);
38
sph_keccak512_close(&ctx_keccak, (void*) hash);
39
40
sph_skein512_init(&ctx_skein);
41
sph_skein512(&ctx_skein, (const void*) hash, 64);
42
sph_skein512_close(&ctx_skein, (void*) hash);
43
44
memcpy(output, hash, 32);
45
}
46
47
int scanhash_nist5(int thr_id, struct work *work, uint32_t max_nonce, uint64_t *hashes_done)
48
{
49
uint32_t _ALIGN(128) hash32[8];
50
uint32_t _ALIGN(128) endiandata[20];
51
uint32_t *pdata = work->data;
52
uint32_t *ptarget = work->target;
53
const uint32_t first_nonce = pdata[19];
54
const uint32_t Htarg = ptarget[7];
55
uint32_t n = pdata[19] - 1;
56
57
uint64_t htmax[] = {
58
0,
59
0xF,
60
0xFF,
61
0xFFF,
62
0xFFFF,
63
0x10000000
64
};
65
uint32_t masks[] = {
66
0xFFFFFFFF,
67
0xFFFFFFF0,
68
0xFFFFFF00,
69
0xFFFFF000,
70
0xFFFF0000,
71
0
72
};
73
74
// we need bigendian data...
75
for (int i=0; i < 19; i++) {
76
be32enc(&endiandata[i], pdata[i]);
77
}
78
79
#ifdef DEBUG_ALGO
80
printf("[%d] Htarg=%X\n", thr_id, Htarg);
81
#endif
82
for (int m=0; m < 6; m++) {
83
if (Htarg <= htmax[m]) {
84
uint32_t mask = masks[m];
85
do {
86
pdata[19] = ++n;
87
be32enc(&endiandata[19], n);
88
nist5hash(hash32, endiandata);
89
#ifndef DEBUG_ALGO
90
if ((!(hash32[7] & mask)) && fulltest(hash32, ptarget)) {
91
work_set_target_ratio(work, hash32);
92
*hashes_done = n - first_nonce + 1;
93
return 1;
94
}
95
#else
96
if (!(n % 0x1000) && !thr_id) printf(".");
97
if (!(hash32[7] & mask)) {
98
printf("[%d]",thr_id);
99
if (fulltest(hash32, ptarget)) {
100
work_set_target_ratio(work, hash32);
101
*hashes_done = n - first_nonce + 1;
102
return 1;
103
}
104
}
105
#endif
106
} while (n < max_nonce && !work_restart[thr_id].restart);
107
// see blake.c if else to understand the loop on htmax => mask
108
break;
109
}
110
}
111
112
*hashes_done = n - first_nonce + 1;
113
pdata[19] = n;
114
return 0;
115
}
116
117