forked from mhssamadani/Autolykos2_AMD_Miner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpuAutolykos.cpp
executable file
·324 lines (237 loc) · 7.01 KB
/
cpuAutolykos.cpp
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include "cpuAutolykos.h"
AutolykosAlg::AutolykosAlg()
{
m_str = new char[64];
bound_str = new char[100];
m_n = new uint8_t[NUM_SIZE_8 + NONCE_SIZE_8];
p_w_m_n = new uint8_t[PK_SIZE_8 + PK_SIZE_8 + NUM_SIZE_8 + NONCE_SIZE_8];
Hinput = new uint8_t[sizeof(uint32_t) + CONST_MES_SIZE_8 + PK_SIZE_8 + NUM_SIZE_8 + PK_SIZE_8];
n_str = new char[NONCE_SIZE_4];
h_str = new char[HEIGHT_SIZE];
int tr = sizeof(unsigned long long);
for (size_t i = 0; i < CONST_MES_SIZE_8 / tr; i++)
{
unsigned long long tmp = i;
uint8_t tmp2[8];
uint8_t tmp1[8];
memcpy(tmp1, &tmp, tr);
tmp2[0] = tmp1[7];
tmp2[1] = tmp1[6];
tmp2[2] = tmp1[5];
tmp2[3] = tmp1[4];
tmp2[4] = tmp1[3];
tmp2[5] = tmp1[2];
tmp2[6] = tmp1[1];
tmp2[7] = tmp1[0];
memcpy(&CONST_MESS[i], tmp2, tr);
}
}
AutolykosAlg::~AutolykosAlg()
{
}
void AutolykosAlg::Blake2b256(const char * in,
const int len,
uint8_t * output,
char * outstr)
{
ctx_t ctx;
uint64_t aux[32];
//====================================================================//
// Initialize context
//====================================================================//
memset(ctx.b, 0, 128);
B2B_IV(ctx.h);
ctx.h[0] ^= 0x01010000 ^ NUM_SIZE_8;
memset(ctx.t, 0, 16);
ctx.c = 0;
//====================================================================//
// Hash message
//====================================================================//
for (int i = 0; i < len; ++i)
{
if (ctx.c == 128) { HOST_B2B_H(&ctx, aux); }
ctx.b[ctx.c++] = (uint8_t)(in[i]);
}
HOST_B2B_H_LAST(&ctx, aux);
for (int i = 0; i < NUM_SIZE_8; ++i)
{
output[NUM_SIZE_8 - i - 1] = (ctx.h[i >> 3] >> ((i & 7) << 3)) & 0xFF;
}
LittleEndianToHexStr(output, NUM_SIZE_8, outstr);
}
void AutolykosAlg::GenIdex(const char * in, const int len, uint32_t* index, uint64_t N_LEN)
{
int a = INDEX_SIZE_8;
int b = K_LEN;
int c = NUM_SIZE_8;
int d = NUM_SIZE_4;
uint8_t sk[NUM_SIZE_8 * 2];
char skstr[NUM_SIZE_4 + 10];
memset(sk, 0, NUM_SIZE_8 * 2);
memset(skstr, 0, NUM_SIZE_4);
Blake2b256(in, len, sk, skstr);
uint8_t beH[PK_SIZE_8];
HexStrToBigEndian(skstr, NUM_SIZE_4, beH, NUM_SIZE_8);
uint32_t* ind = index;
memcpy(sk, beH, NUM_SIZE_8);
memcpy(sk + NUM_SIZE_8, beH, NUM_SIZE_8);
uint32_t tmpInd[32];
int sliceIndex = 0;
for (int k = 0; k < K_LEN; k++)
{
uint8_t tmp[4];
memcpy(tmp, sk + sliceIndex, 4);
memcpy(&tmpInd[k], sk + sliceIndex, 4);
uint8_t tmp2[4];
tmp2[0] = tmp[3];
tmp2[1] = tmp[2];
tmp2[2] = tmp[1];
tmp2[3] = tmp[0];
memcpy(&ind[k], tmp2, 4);
ind[k] = ind[k] % N_LEN;
sliceIndex++;
}
}
void AutolykosAlg::hashFn(const char * in, const int len, uint8_t * output)
{
char *skstr = new char[len * 3];
Blake2b256(in, len, output, skstr);
uint8_t beHash[PK_SIZE_8];
HexStrToBigEndian(skstr, NUM_SIZE_4, beHash, NUM_SIZE_8);
memcpy(output, beHash, NUM_SIZE_8);
delete skstr;
}
bool AutolykosAlg::RunAlg(
uint8_t *message,
uint8_t *nonce,
uint8_t *bPool,
uint8_t *height,
uint64_t N_LEN
)
{
BigEndianToHexStr(message, NUM_SIZE_8, m_str);
uint32_t ilen = 0;
LittleEndianOf256ToDecStr((uint8_t *)bPool, bound_str, &ilen);
uint32_t index[K_LEN];
LittleEndianToHexStr(nonce, NONCE_SIZE_8, n_str);
//memcpy(n_str , "0000000000003105" , NONCE_SIZE_4);
//uint32_t tttt;
//HexStrToLittleEndian(n_str, NONCE_SIZE_4, (uint8_t *)&tttt, NONCE_SIZE_8);
BigEndianToHexStr(height, HEIGHT_SIZE, h_str);
uint8_t beN[NONCE_SIZE_8];
HexStrToBigEndian(n_str, NONCE_SIZE_8 * 2, beN, NONCE_SIZE_8);
uint8_t beH[HEIGHT_SIZE];
HexStrToBigEndian(h_str, HEIGHT_SIZE * 2, beH, HEIGHT_SIZE);
uint32_t littleH;
HexStrToLittleEndian(h_str, HEIGHT_SIZE * 2,(uint8_t *)&littleH, HEIGHT_SIZE);
uint32_t littleN;
HexStrToLittleEndian(n_str, NONCE_SIZE_8 * 2, (uint8_t *)&littleN, NONCE_SIZE_8);
uint8_t h1[NUM_SIZE_8];
memcpy(m_n, message, NUM_SIZE_8);
memcpy(m_n + NUM_SIZE_8, beN, NONCE_SIZE_8);
hashFn((const char *)m_n, NUM_SIZE_8 + NONCE_SIZE_8, (uint8_t *)h1);
uint64_t h2;
char tmpL1[8];
tmpL1[0] = h1[31];
tmpL1[1] = h1[30];
tmpL1[2] = h1[29];
tmpL1[3] = h1[28];
tmpL1[4] = h1[27];
tmpL1[5] = h1[26];
tmpL1[6] = h1[25];
tmpL1[7] = h1[24];
memcpy(&h2, tmpL1, 8);
uint64_t h3 = h2 % N_LEN;
uint8_t iii[4];
iii[0] = ((char *)(&h3))[3];
iii[1] = ((char *)(&h3))[2];
iii[2] = ((char *)(&h3))[1];
iii[3] = ((char *)(&h3))[0];
uint8_t i_h_M[HEIGHT_SIZE + HEIGHT_SIZE + CONST_MES_SIZE_8];
memcpy(i_h_M, iii, HEIGHT_SIZE);
memcpy(i_h_M + HEIGHT_SIZE, beH, HEIGHT_SIZE);
memcpy(i_h_M + HEIGHT_SIZE + HEIGHT_SIZE, CONST_MESS, CONST_MES_SIZE_8);
hashFn((const char *)i_h_M, HEIGHT_SIZE + HEIGHT_SIZE + CONST_MES_SIZE_8, (uint8_t *)h1);
uint8_t ff[NUM_SIZE_8 - 1];
memcpy(ff, h1 + 1, NUM_SIZE_8 - 1);
uint8_t seed[NUM_SIZE_8 - 1 + NUM_SIZE_8 + NONCE_SIZE_8];
memcpy(seed, ff, NUM_SIZE_8 - 1);
memcpy(seed + NUM_SIZE_8 - 1, message, NUM_SIZE_8);
memcpy(seed + NUM_SIZE_8 - 1 + NUM_SIZE_8, beN, NONCE_SIZE_8);
GenIdex((const char*)seed, NUM_SIZE_8 - 1 + NUM_SIZE_8 + NONCE_SIZE_8, index,N_LEN);
uint8_t ret[32][NUM_SIZE_8];
int ll = sizeof(uint32_t) + CONST_MES_SIZE_8 + PK_SIZE_8 + NUM_SIZE_8 + PK_SIZE_8;
BIGNUM* bigsum = BN_new();
CALL(BN_dec2bn(&bigsum, "0"), ERROR_OPENSSL);
BIGNUM* bigres = BN_new();
CALL(BN_dec2bn(&bigres, "0"), ERROR_OPENSSL);
int rep = 0;
int off = 0;
uint8_t tmp[NUM_SIZE_8 - 1];
char hesStr[64 + 1];
uint8_t tmp2[4];
uint8_t tmp1[4];
unsigned char f[32];
memset(f, 0, 32);
char *LSUMM;
char *LB;
for (rep = 0; rep < 32; rep++)
{
memset(Hinput, 0, ll);
memcpy(tmp1, &index[rep], 4);
tmp2[0] = tmp1[3];
tmp2[1] = tmp1[2];
tmp2[2] = tmp1[1];
tmp2[3] = tmp1[0];
off = 0;
memcpy(Hinput + off, tmp2, sizeof(uint32_t));
off += sizeof(uint32_t);
memcpy(Hinput + off, beH, HEIGHT_SIZE);
off += HEIGHT_SIZE;
memcpy(Hinput + off, CONST_MESS, CONST_MES_SIZE_8);
off += CONST_MES_SIZE_8;
hashFn((const char *)Hinput, off, (uint8_t *)ret[rep]);
memcpy(tmp, &(ret[rep][1]), 31);
CALL(BN_bin2bn((const unsigned char *)tmp, 31, bigres), ERROR_OPENSSL);
CALL(BN_add(bigsum, bigsum, bigres), ERROR_OPENSSL);
LB = BN_bn2dec(bigres);
BN_bn2bin(bigsum, f);
}
const char *SUMMbigEndian = BN_bn2dec(bigsum);
BN_bn2bin(bigsum, f);
char bigendian2littl[32];
for (size_t i = 0; i < 32; i++)
{
bigendian2littl[i] = f[32 - i - 1];
}
BIGNUM* littleF = BN_new();
CALL(BN_bin2bn((const unsigned char *)bigendian2littl, 32, littleF), ERROR_OPENSSL);
const char *SUMMLittleEndian = BN_bn2dec(littleF);
char hf[32];
hashFn((const char *)f, 32, (uint8_t *)hf);
BIGNUM* bigHF = BN_new();
CALL(BN_bin2bn((const unsigned char *)hf, 32, bigHF), ERROR_OPENSSL);
char littl2big[32];
for (size_t i = 0; i < 32; i++)
{
littl2big[i] = bPool[32 - i - 1];
}
BIGNUM* bigB = BN_new();
CALL(BN_bin2bn((const unsigned char *)littl2big, 32, bigB), ERROR_OPENSSL);
int cmp = BN_cmp(bigHF, bigB);
const char *chD = BN_bn2dec(bigHF);
const char *chB = BN_bn2dec(bigB);
BN_free(bigsum);
BN_free(bigres);
BN_free(littleF);
BN_free(bigHF);
BN_free(bigB);
if (cmp < 0)
{
return true;
}
else
{
return false;
}
}