This repository has been archived by the owner on Oct 27, 2021. It is now read-only.
forked from magi-project/obsolete-magi-minerd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashblock.h
415 lines (343 loc) · 11.2 KB
/
hashblock.h
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Copyright (c) 2014 The Mini-Blockchain Project
#ifndef HASHBLOCK_H
#define HASHBLOCK_H
#include "uint256.h"
#include "util.h"
#include "magimath.h"
#include "hash/sph_sha2.h"
#include "hash/sph_keccak.h" //sha3
#include "hash/sph_haval.h"
#include "hash/sph_tiger.h"
#include "hash/sph_whirlpool.h"
#include "hash/sph_ripemd.h"
#ifndef QT_NO_DEBUG
#include <string>
#endif
#ifdef GLOBALDEFINED
#define GLOBAL
#else
#define GLOBAL extern
#endif
GLOBAL sph_sha256_context z_sha256;
GLOBAL sph_sha512_context z_sha512;
GLOBAL sph_keccak512_context z_keccak;
GLOBAL sph_whirlpool_context z_whirlpool;
GLOBAL sph_haval256_5_context z_haval;
GLOBAL sph_tiger_context z_tiger;
GLOBAL sph_ripemd160_context z_ripemd;
#define fillz() do { \
sph_sha512_init(&z_sha512); \
sph_sha256_init(&z_sha256); \
sph_keccak512_init(&z_keccak); \
sph_whirlpool_init(&z_whirlpool); \
sph_haval256_5_init(&z_haval); \
sph_tiger_init(&z_tiger); \
sph_ripemd160_init(&z_ripemd); \
} while (0)
#define ZSHA256 (memcpy(&ctx_sha256, &z_sha256, sizeof(z_sha256)))
#define ZSHA512 (memcpy(&ctx_sha512, &z_sha512, sizeof(z_sha512)))
#define ZKECCAK (memcpy(&ctx_keccak, &z_keccak, sizeof(z_keccak)))
#define ZWHIRLPOOL (memcpy(&ctx_whirlpool, &z_whirlpool, sizeof(z_whirlpool)))
#define ZHAVAL (memcpy(&ctx_haval, &z_haval, sizeof(z_haval)))
#define ZTIGER (memcpy(&ctx_tiger, &z_tiger, sizeof(z_tiger)))
#define ZRIPEMD (memcpy(&ctx_ripemd, &z_ripemd, sizeof(z_ripemd)))
struct hash_context {
sph_sha256_context ctx_sha256;
sph_sha512_context ctx_sha512;
sph_keccak512_context ctx_keccak;
sph_whirlpool_context ctx_whirlpool;
sph_haval256_5_context ctx_haval;
sph_tiger_context ctx_tiger;
sph_ripemd160_context ctx_ripemd;
mpz_t bns[8];
mpz_t product;
char data[4096];
};
void HashInit(hash_context &h){
for(int i=0; i < 8; i++){
mpz_init(h.bns[i]);
}
mpz_init(h.product);
}
#define BITS_PER_DIGIT 3.32192809488736234787
#define EPS (std::numeric_limits<double>::epsilon())
#define NM7M 5
#define SW_DIVS 5
template<typename T1>
inline uint256 hash_M7M(hash_context &h, const T1 pbegin, const T1 pend, const unsigned int nnNonce)
{
static unsigned char pblank[1];
int bytes;
unsigned int nnNonce1 = nnNonce/2;
uint512 hash[7];
uint256 finalhash;
for(int i=0; i < 7; i++)
hash[i] = 0;
const void* ptr = (pbegin == pend ? pblank : static_cast<const void*>(&pbegin[0]));
size_t sz = (pend - pbegin) * sizeof(pbegin[0]);
// size_t sz = 80;
sph_sha256_init(&h.ctx_sha256);
// ZSHA256;
sph_sha256 (&h.ctx_sha256, ptr, sz);
sph_sha256_close(&h.ctx_sha256, static_cast<void*>(&hash[0]));
sph_sha512_init(&h.ctx_sha512);
// ZSHA512;
sph_sha512 (&h.ctx_sha512, ptr, sz);
sph_sha512_close(&h.ctx_sha512, static_cast<void*>(&hash[1]));
sph_keccak512_init(&h.ctx_keccak);
// ZKECCAK;
sph_keccak512 (&h.ctx_keccak, ptr, sz);
sph_keccak512_close(&h.ctx_keccak, static_cast<void*>(&hash[2]));
sph_whirlpool_init(&h.ctx_whirlpool);
// ZWHIRLPOOL;
sph_whirlpool (&h.ctx_whirlpool, ptr, sz);
sph_whirlpool_close(&h.ctx_whirlpool, static_cast<void*>(&hash[3]));
sph_haval256_5_init(&h.ctx_haval);
// ZHAVAL;
sph_haval256_5 (&h.ctx_haval, ptr, sz);
sph_haval256_5_close(&h.ctx_haval, static_cast<void*>(&hash[4]));
sph_tiger_init(&h.ctx_tiger);
// ZTIGER;
sph_tiger (&h.ctx_tiger, ptr, sz);
sph_tiger_close(&h.ctx_tiger, static_cast<void*>(&hash[5]));
sph_ripemd160_init(&h.ctx_ripemd);
// ZRIPEMD;
sph_ripemd160 (&h.ctx_ripemd, ptr, sz);
sph_ripemd160_close(&h.ctx_ripemd, static_cast<void*>(&hash[6]));
//printf("%s\n", hash[6].GetHex().c_str());
//Take care of zeros and load gmp
for(int i=0; i < 7; i++){
if(hash[i]==0)
hash[i] = 1;
mpz_set_uint512(h.bns[i],hash[i]);
}
mpz_set_ui(h.bns[7],0);
for(int i=0; i < 7; i++)
mpz_add(h.bns[7], h.bns[7], h.bns[i]);
// mpz_pow_ui(h.bns[7], h.bns[7], 2);
mpz_set_ui(h.product,1);
for(int i=0; i < 8; i++){
mpz_mul(h.product,h.product,h.bns[i]);
}
mpz_pow_ui(h.product, h.product, 2);
bytes = mpz_sizeinbase(h.product, 256);
char *adata = (char*)malloc(bytes);
mpz_export(adata, NULL, -1, 1, 0, 0, h.product);
sph_sha256_init(&h.ctx_sha256);
// ZSHA256;
// sph_sha256 (&h.ctx_sha256, h.data,bytes);
sph_sha256 (&h.ctx_sha256, adata, bytes);
sph_sha256_close(&h.ctx_sha256, static_cast<void*>(&finalhash));
free(adata);
for(int i=0; i < NM7M; i++)
{
if(finalhash==0) finalhash = 1;
mpz_set_uint256(h.bns[0],finalhash);
mpz_add(h.bns[7], h.bns[7], h.bns[0]);
mpz_mul(h.product, h.product, h.bns[7]);
mpz_cdiv_q (h.product, h.product, h.bns[0]);
if (mpz_sgn(h.product) <= 0) mpz_set_ui(h.product,1);
bytes = mpz_sizeinbase(h.product, 256);
// printf("M7M data space: %iB\n", bytes);
char *bdata = (char*)malloc(bytes);
mpz_export(bdata, NULL, -1, 1, 0, 0, h.product);
sph_sha256_init(&h.ctx_sha256);
// ZSHA256;
sph_sha256 (&h.ctx_sha256, bdata, bytes);
sph_sha256_close(&h.ctx_sha256, static_cast<void*>(&finalhash));
free(bdata);
// printf("finalhash = %s\n", finalhash.GetHex().c_str());
}
return finalhash;
}
template<typename T1>
inline uint256 hash_M7M_v2(const T1 pbegin, const T1 pend, const unsigned int nnNonce)
{
sph_sha256_context ctx_sha256;
sph_sha512_context ctx_sha512;
sph_keccak512_context ctx_keccak;
sph_whirlpool_context ctx_whirlpool;
sph_haval256_5_context ctx_haval;
sph_tiger_context ctx_tiger;
sph_ripemd160_context ctx_ripemd;
static unsigned char pblank[1];
int bytes, nnNonce2 = (int)(nnNonce/2);
uint512 hash[7];
uint256 finalhash;
for(int i=0; i < 7; i++)
hash[i] = 0;
const void* ptr = (pbegin == pend ? pblank : static_cast<const void*>(&pbegin[0]));
size_t sz = (pend - pbegin) * sizeof(pbegin[0]);
sph_sha256_init(&ctx_sha256);
// ZSHA256;
sph_sha256 (&ctx_sha256, ptr, sz);
sph_sha256_close(&ctx_sha256, static_cast<void*>(&hash[0]));
sph_sha512_init(&ctx_sha512);
// ZSHA512;
sph_sha512 (&ctx_sha512, ptr, sz);
sph_sha512_close(&ctx_sha512, static_cast<void*>(&hash[1]));
sph_keccak512_init(&ctx_keccak);
// ZKECCAK;
sph_keccak512 (&ctx_keccak, ptr, sz);
sph_keccak512_close(&ctx_keccak, static_cast<void*>(&hash[2]));
sph_whirlpool_init(&ctx_whirlpool);
// ZWHIRLPOOL;
sph_whirlpool (&ctx_whirlpool, ptr, sz);
sph_whirlpool_close(&ctx_whirlpool, static_cast<void*>(&hash[3]));
sph_haval256_5_init(&ctx_haval);
// ZHAVAL;
sph_haval256_5 (&ctx_haval, ptr, sz);
sph_haval256_5_close(&ctx_haval, static_cast<void*>(&hash[4]));
sph_tiger_init(&ctx_tiger);
// ZTIGER;
sph_tiger (&ctx_tiger, ptr, sz);
sph_tiger_close(&ctx_tiger, static_cast<void*>(&hash[5]));
sph_ripemd160_init(&ctx_ripemd);
// ZRIPEMD;
sph_ripemd160 (&ctx_ripemd, ptr, sz);
sph_ripemd160_close(&ctx_ripemd, static_cast<void*>(&hash[6]));
// printf("%s\n", hash[6].GetHex().c_str());
mpz_t bns[8];
//Take care of zeros and load gmp
for(int i=0; i < 7; i++){
if(hash[i]==0)
hash[i] = 1;
mpz_init(bns[i]);
mpz_set_uint512(bns[i],hash[i]);
}
mpz_init(bns[7]);
mpz_set_ui(bns[7],0);
for(int i=0; i < 7; i++)
mpz_add(bns[7], bns[7], bns[i]);
mpz_t product;
mpz_init(product);
mpz_set_ui(product,1);
// mpz_pow_ui(bns[7], bns[7], 2);
for(int i=0; i < 8; i++){
mpz_mul(product,product,bns[i]);
}
mpz_pow_ui(product, product, 2);
bytes = mpz_sizeinbase(product, 256);
// printf("M7M data space: %iB\n", bytes);
char *data = (char*)malloc(bytes);
mpz_export(data, NULL, -1, 1, 0, 0, product);
sph_sha256_init(&ctx_sha256);
// ZSHA256;
sph_sha256 (&ctx_sha256, data, bytes);
sph_sha256_close(&ctx_sha256, static_cast<void*>(&finalhash));
// printf("finalhash = %s\n", hash[6].GetHex().c_str());
free(data);
int digits=(int)((sqrt((double)(nnNonce2))*(1.+EPS))/9000+75);
// int iterations=(int)((sqrt((double)(nnNonce2))+EPS)/500+350); // <= 500
// int digits=100;
int iterations=20; // <= 500
mpf_set_default_prec((long int)(digits*BITS_PER_DIGIT+16));
mpz_t magipi;
mpz_t magisw;
mpf_t magifpi;
mpf_t mpa1, mpb1, mpt1, mpp1;
mpf_t mpa2, mpb2, mpt2, mpp2;
mpf_t mpsft;
mpz_init(magipi);
mpz_init(magisw);
mpf_init(magifpi);
mpf_init(mpsft);
mpf_init(mpa1);
mpf_init(mpb1);
mpf_init(mpt1);
mpf_init(mpp1);
mpf_init(mpa2);
mpf_init(mpb2);
mpf_init(mpt2);
mpf_init(mpp2);
uint32_t usw_;
usw_ = sw_(nnNonce2, SW_DIVS);
if (usw_ < 1) usw_ = 1;
// if(fDebugMagi) printf("usw_: %d\n", usw_);
mpz_set_ui(magisw, usw_);
uint32_t mpzscale=mpz_size(magisw);
for(int i=0; i < NM7M; i++)
{
if (mpzscale > 1000) {
mpzscale = 1000;
}
else if (mpzscale < 1) {
mpzscale = 1;
}
// if(fDebugMagi) printf("mpzscale: %d\n", mpzscale);
mpf_set_ui(mpa1, 1);
mpf_set_ui(mpb1, 2);
mpf_set_d(mpt1, 0.25*mpzscale);
mpf_set_ui(mpp1, 1);
mpf_sqrt(mpb1, mpb1);
mpf_ui_div(mpb1, 1, mpb1);
mpf_set_ui(mpsft, 10);
for(int i=0; i <= iterations; i++)
{
mpf_add(mpa2, mpa1, mpb1);
mpf_div_ui(mpa2, mpa2, 2);
mpf_mul(mpb2, mpa1, mpb1);
mpf_abs(mpb2, mpb2);
mpf_sqrt(mpb2, mpb2);
mpf_sub(mpt2, mpa1, mpa2);
mpf_abs(mpt2, mpt2);
mpf_sqrt(mpt2, mpt2);
mpf_mul(mpt2, mpt2, mpp1);
mpf_sub(mpt2, mpt1, mpt2);
mpf_mul_ui(mpp2, mpp1, 2);
mpf_swap(mpa1, mpa2);
mpf_swap(mpb1, mpb2);
mpf_swap(mpt1, mpt2);
mpf_swap(mpp1, mpp2);
}
mpf_add(magifpi, mpa1, mpb1);
mpf_pow_ui(magifpi, magifpi, 2);
mpf_div_ui(magifpi, magifpi, 4);
mpf_abs(mpt1, mpt1);
mpf_div(magifpi, magifpi, mpt1);
// mpf_out_str(stdout, 10, digits+2, magifpi);
mpf_pow_ui(mpsft, mpsft, digits/2);
mpf_mul(magifpi, magifpi, mpsft);
mpz_set_f(magipi, magifpi);
//mpz_set_ui(magipi,1);
mpz_add(product,product,magipi);
mpz_add(product,product,magisw);
if(finalhash==0) finalhash = 1;
mpz_set_uint256(bns[0],finalhash);
mpz_add(bns[7], bns[7], bns[0]);
mpz_mul(product,product,bns[7]);
mpz_cdiv_q (product, product, bns[0]);
if (mpz_sgn(product) <= 0) mpz_set_ui(product,1);
bytes = mpz_sizeinbase(product, 256);
mpzscale=bytes;
// printf("M7M data space: %iB\n", bytes);
char *bdata = (char*)malloc(bytes);
mpz_export(bdata, NULL, -1, 1, 0, 0, product);
sph_sha256_init(&ctx_sha256);
// ZSHA256;
sph_sha256 (&ctx_sha256, bdata, bytes);
sph_sha256_close(&ctx_sha256, static_cast<void*>(&finalhash));
free(bdata);
// printf("finalhash = %s\n", finalhash.GetHex().c_str());
}
//Free the memory
for(int i=0; i < 8; i++){
mpz_clear(bns[i]);
}
// mpz_clear(dSpectralWeight);
mpz_clear(product);
mpz_clear(magipi);
mpz_clear(magisw);
mpf_clear(magifpi);
mpf_clear(mpsft);
mpf_clear(mpa1);
mpf_clear(mpb1);
mpf_clear(mpt1);
mpf_clear(mpp1);
mpf_clear(mpa2);
mpf_clear(mpb2);
mpf_clear(mpt2);
mpf_clear(mpp2);
return finalhash;
}
#endif // HASHBLOCK_H