forked from ckolivas/cgminer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathX11.c
168 lines (137 loc) · 4.56 KB
/
X11.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
/* X11 Hashing algorithm for cgminer
by: XePeleato
Thanks to:
Colin Percival (ArtForz)
lucasjones
*/
//#include "X11.h"
#include "miner.h"
#include "config.h"
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
// * X11 Functions
#include "sha3/sph_blake.h"
#include "sha3/sph_bmw.h"
#include "sha3/sph_groestl.h"
#include "sha3/sph_jh.h"
#include "sha3/sph_keccak.h"
#include "sha3/sph_skein.h"
#include "sha3/sph_luffa.h"
#include "sha3/sph_cubehash.h"
#include "sha3/sph_shavite.h"
#include "sha3/sph_simd.h"
#include "sha3/sph_echo.h"
typedef struct X11_context_holder {
sph_blake512_context blake;
sph_bmw512_context bmw;
sph_groestl512_context groestl;
sph_skein512_context skein;
sph_jh512_context jh;
sph_keccak512_context keccak;
sph_luffa512_context luffa;
sph_cubehash512_context cubehash;
sph_shavite512_context shavite;
sph_simd512_context simd;
sph_echo512_context echo;
uint32_t h[8];
}X11_context_holder;
X11_context_holder base_contexts;
void init_X11_contexts()
{
sph_blake512_init(&base_contexts.blake);
sph_bmw512_init(&base_contexts.bmw);
sph_groestl512_init(&base_contexts.groestl);
sph_skein512_init(&base_contexts.skein);
sph_jh512_init(&base_contexts.jh);
sph_keccak512_init(&base_contexts.keccak);
sph_luffa512_init(&base_contexts.luffa);
sph_cubehash512_init(&base_contexts.cubehash);
sph_shavite512_init(&base_contexts.shavite);
sph_simd512_init(&base_contexts.simd);
sph_echo512_init(&base_contexts.echo);
}
// Thanks to Colin Percival (sgminer) for this:
inline void be32enc_vect(uint32_t *dst, const uint32_t *src, uint32_t len)
{
uint32_t i;
for (i = 0; i < len; i++)
dst[i] = htobe32(src[i]);
}
// Inline will speed it up
inline void X11_Hash(const void *input, void *state)
{
init_X11_contexts();
X11_context_holder ctx;
uint32_t hashA[16], hashB[16];
//Order: Blake > Bmw > Groestl > Sken > Jh > Meccak > Luffa > Cubehash > Shivite > Simd > Echo
memcpy(&ctx, &base_contexts, sizeof(base_contexts));
sph_blake512 (&ctx.blake, input, 80);
sph_blake512_close (&ctx.blake, hashA);
sph_bmw512 (&ctx.bmw, hashA, 64);
sph_bmw512_close(&ctx.bmw, hashB);
sph_groestl512 (&ctx.groestl, hashB, 64);
sph_groestl512_close(&ctx.groestl, hashA);
sph_skein512 (&ctx.skein, hashA, 64);
sph_skein512_close(&ctx.skein, hashB);
sph_jh512 (&ctx.jh, hashB, 64);
sph_jh512_close(&ctx.jh, hashA);
sph_keccak512 (&ctx.keccak, hashA, 64);
sph_keccak512_close(&ctx.keccak, hashB);
sph_luffa512 (&ctx.luffa, hashB, 64);
sph_luffa512_close (&ctx.luffa, hashA);
sph_cubehash512 (&ctx.cubehash, hashA, 64);
sph_cubehash512_close(&ctx.cubehash, hashB);
sph_shavite512 (&ctx.shavite, hashB, 64);
sph_shavite512_close(&ctx.shavite, hashA);
sph_simd512 (&ctx.simd, hashA, 64);
sph_simd512_close(&ctx.simd, hashB);
sph_echo512 (&ctx.echo, hashB, 64);
sph_echo512_close(&ctx.echo, hashA);
memcpy(state, hashA, 32);
}
static const uint32_t diff1targ = 0x0000ffff;
void X11_RegenHash(struct work *work)
{
uint32_t data[20];
char *scratchbuf;
uint32_t *nonce = (uint32_t *)(work->data + 76);
uint32_t *ohash = (uint32_t *)(work->hash);
be32enc_vect(data, (const uint32_t *)work->data, 19);
data[19] = htobe32(*nonce);
X11_Hash(ohash, data);
}
bool X11_ScanHash(struct thr_info *thr, const unsigned char __maybe_unused *pmidstate,
unsigned char *pdata, unsigned char __maybe_unused *phash1,
unsigned char __maybe_unused *phash, const unsigned char *ptarget,
uint32_t max_nonce, uint32_t *last_nonce, uint32_t n)
{
uint32_t *nonce = (uint32_t *)(pdata + 76);
char *scratchbuf;
uint32_t data[20];
uint32_t tmp_hash7;
uint32_t Htarg = le32toh(((const uint32_t *)ptarget)[7]);
bool ret = false;
be32enc_vect(data, (const uint32_t *)pdata, 19);
while(1) {
uint32_t ostate[8];
*nonce = ++n;
data[19] = (n);
X11_Hash(ostate, data);
tmp_hash7 = (ostate[7]);
applog(LOG_INFO, "data7 %08lx",
(long unsigned int)data[7]);
if (unlikely(tmp_hash7 <= Htarg)) {
((uint32_t *)pdata)[19] = htobe32(n);
*last_nonce = n;
ret = true;
break;
}
if (unlikely((n >= max_nonce) || thr->work_restart)) {
*last_nonce = n;
break;
}
}
return ret;
}