-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathCPUAlgos_mtrlt.cpp
463 lines (384 loc) · 11.1 KB
/
CPUAlgos_mtrlt.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
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#include "Global.h"
#include "CPUMiner.h"
#include "CPUAlgos.h"
#include "Util.h"
#include "SHA256.h"
#include "Sieve.h"
#include "Primes.h"
#include "CPUAlgos_global.h"
//void CPU_Got_share(Reap_CPU_param* state, uchar* tempdata, vector<uchar>& target, uint serverid);
bool CPU_Hash_Below_Target(uchar* hash, uchar* target);
#define nFractionalBits 24
#define TARGET_FRACTIONAL_MASK ((1u << nFractionalBits) - 1)
#define TARGET_LENGTH_MASK (~TARGET_FRACTIONAL_MASK)
//A bunch of helper functions
static
void TargetIncrementLength(unsigned int *pnBits)
{
*pnBits += (1 << nFractionalBits);
}
static
unsigned int TargetFromInt(unsigned int nLength)
{
return (nLength << nFractionalBits);
}
// Check Fermat probable primality test (2-PRP): 2 ** (n-1) = 1 (mod n)
// true: n is probable prime
// false: n is composite; set fractional length in the nLength output
static
bool PrimeTest(mpz_t *n, unsigned int *pnLength)
{
//fast tests
/*{
for(uint i=MAX_SIEVE_AMOUNT; i<MAX_SIEVE_AMOUNT+100; ++i)
{
if (mpz_divisible_ui_p(*n,Primes::v[i]))
{
return false;
}
}
}*/
mpz_t a, e, r;
mpz_init_set_ui(a, 2); // base; Fermat witness
mpz_init(e);
mpz_sub_ui(e, *n, 1);
mpz_init(r);
mpz_powm(r, a, e, *n); // r = (2**(n-1))%n
mpz_clear(a);
mpz_clear(e);
if (!mpz_cmp_ui(r, 1)) //if r is 1, this is a witness!
{
mpz_clear(r);
return true;
}
// Failed Fermat test, calculate fractional length
// nFractionalLength = ( (n-r) << nFractionalBits ) / n
mpz_sub(r, *n, r);
mpz_mul_2exp(r, r, nFractionalBits);
mpz_fdiv_q(r, r, *n);
unsigned int nFractionalLength = mpz_get_ui(r);
mpz_clear(r);
if (nFractionalLength >= (1 << nFractionalBits))
{
cout << "PrimeTest() : fractional assert" << endl;
return false;
}
*pnLength = (*pnLength & TARGET_LENGTH_MASK) | nFractionalLength;
return false;
}
//This function determines the primality of a number of the form 2p+-1
//Sophie Germain is 2p+1, the other kind is 2p-1.
//Euler-Gandalf-Dipshit test
/*
Form 2p+1 (Sophie Germain):
n%8 == 7
OR
n%8 == 3
Form 2p-1
n%8 == 1
OR
n%8 == 5
*/
// disabling this, it's not any faster or more accurate than the Fermat test. It is indeed equivalent to the Fermat test.
/*
static
bool SpecialPrimeTest(mpz_t *n, bool fSophieGermain, unsigned int *pnLength)
{
mpz_t e, r;
mpz_init(e);
mpz_sub_ui(e, *n, 1);
mpz_fdiv_q_2exp(e, e, 1); //e = (n-1)/2;
mpz_t a; mpz_init_set_ui(a, 2); //a = 2;
mpz_init(r);
mpz_powm(r, a, e, *n); //r = (2**((n-1)/2))%n;
mpz_clear(a);
mpz_clear(e);
unsigned nMod8 = mpz_fdiv_ui(*n, 8);//nMod8 = n%8;
bool fPassedTest = false;
if (fSophieGermain && (nMod8 == 7)) // Euler & Lagrange
fPassedTest = !mpz_cmp_ui(r, 1);
else if (nMod8 == (fSophieGermain ? 3 : 5)) // Lifchitz
{
mpz_t mp;
mpz_init_set_ui(mp, 1); //mp=1
mpz_add(mp, r, mp); //mp = r+1;
fPassedTest = !mpz_cmp(mp, *n); //r+1==n i.e. r%n == -1
mpz_clear(mp);
}
else if ((!fSophieGermain) && (nMod8 == 1)) // LifChitz
fPassedTest = !mpz_cmp_ui(r, 1);
else
{
mpz_clear(r);
cout << "ELLP Test: invalid n%%8 = " << nMod8 << ", " << (fSophieGermain?"First kind":"Second kind") << endl;
return false;
//return error("EulerLagrangeLifchitzPrimalityTest() : invalid n %% 8 = %d, %s", nMod8, (fSophieGermain? "first kind" : "second kind"));
}
if (fPassedTest)
{
mpz_clear(r);
return true;
}
// Failed test, calculate fractional length
// derive Fermat test remainder
mpz_mul(r, r, r);
mpz_fdiv_r(r, r, *n);
// nFractionalLength = ( (n-r) << nFractionalBits ) / n
mpz_sub(r, *n, r);
mpz_mul_2exp(r, r, nFractionalBits);
mpz_fdiv_q(r, r, *n);
unsigned int nFractionalLength = mpz_get_ui(r);
mpz_clear(r);
if (nFractionalLength >= (1 << nFractionalBits))
{
cout << "EulerLagrangeLifchitzPrimalityTest() : fractional assert" << endl;
return false;
//return error("EulerLagrangeLifchitzPrimalityTest() : fractional assert");
}
*pnLength = (*pnLength & TARGET_LENGTH_MASK) | nFractionalLength;
return false;
}
*/
// Test Probable Cunningham Chain for: n
// fSophieGermain:
// true - Test for Cunningham Chain of first kind (n, 2n+1, 4n+3, ...)
// false - Test for Cunningham Chain of second kind (n, 2n-1, 4n-3, ...)
// Return value:
// true - Probable Cunningham Chain found (length at least 2)
// false - Not Cunningham Chain
static
bool CunnChainTest(mpz_t *n, bool fSophieGermain, bool fFermatTest, unsigned int *pnProbableChainLength)
{
*pnProbableChainLength = 0;
mpz_t N;
mpz_init_set(N, *n);
// Fermat test for n first
if (!PrimeTest(&N, pnProbableChainLength))
{
mpz_clear(N);
return false;
}
// Euler-Lagrange-Lifchitz test for the following numbers in chain
while (true)
{
TargetIncrementLength(pnProbableChainLength);
mpz_add(N, N, N);
if (fSophieGermain)
mpz_add_ui(N, N, 1);
else
mpz_sub_ui(N, N, 1);
//disabled, see SpecialPrimeTest() comments for further information
/*if (fFermatTest)
{
if (!PrimeTest(&N, pnProbableChainLength))
break;
}
else
{
if (!SpecialPrimeTest(&N, fSophieGermain, pnProbableChainLength))
break;
}*/
if (!PrimeTest(&N, pnProbableChainLength))
break;
}
mpz_clear(N);
#ifdef SUPERDEBUG
printf("PCCT => %u (%u)\n", TargetGetLength(*pnProbableChainLength), *pnProbableChainLength);
#endif
return (TargetGetLength(*pnProbableChainLength) >= 2);
}
// Test probable prime chain for: nOrigin
// Return value:
// true - Probable prime chain found (one of nChainLength meeting target)
// false - prime chain too short (none of nChainLength meeting target)
static
bool AllChainTest(mpz_t *bnPrimeChainOrigin, unsigned int nBits, bool fFermatTest, unsigned int *pnChainLengthCunningham1, unsigned int *pnChainLengthCunningham2, unsigned int *pnChainLengthBiTwin, unsigned int sievenumber)
{
*pnChainLengthCunningham1 = 0;
*pnChainLengthCunningham2 = 0;
*pnChainLengthBiTwin = 0;
mpz_t mp;
mpz_init(mp);
// Test for Cunningham Chain of first kind
if (sievenumber&1)
{
mpz_sub_ui(mp, *bnPrimeChainOrigin, 1);
CunnChainTest(&mp, true, fFermatTest, pnChainLengthCunningham1);
}
// Test for Cunningham Chain of second kind
if (sievenumber&2)
{
mpz_add_ui(mp, *bnPrimeChainOrigin, 1);
CunnChainTest(&mp, false, fFermatTest, pnChainLengthCunningham2);
}
mpz_clear(mp);
// Figure out BiTwin Chain length
// BiTwin Chain allows a single prime at the end for odd length chain
*pnChainLengthBiTwin = (TargetGetLength(*pnChainLengthCunningham1) > TargetGetLength(*pnChainLengthCunningham2)) ? (*pnChainLengthCunningham2 + TargetFromInt(TargetGetLength(*pnChainLengthCunningham2)+1)) : (*pnChainLengthCunningham1 + TargetFromInt(TargetGetLength(*pnChainLengthCunningham1)));
return (*pnChainLengthCunningham1 >= nBits || *pnChainLengthCunningham2 >= nBits || *pnChainLengthBiTwin >= nBits);
}
ullint chainspersec[20] = {};
ullint totalpersec = 0;
uint found0=0,foundtotal=0;
bool MinePrime(Reap_CPU_param* state, Work& tempwork)
{
uchar* tempdata = &tempwork.data[0];
uchar hash[32];
mysha256(hash,tempdata,80);
mysha256(hash,hash,32);
//does this need byte flipping?
uint bits = *(uint*)&tempdata[72];
if (!(hash[31] & 0x80))
return false; //hash is too small, abort
Mpz_w hashnum;
set_mpz_to_hash(&hashnum.n, hash);
if (mpz_fdiv_ui(hashnum.n, 2*3) != 0)
return false;
bool found=false;
//5431526412865007455
mpz_t factor; mpz_init_set_str(factor, "5431526412865007455", 10);
mpz_mul(hashnum.n,hashnum.n,factor);
uint remainders[MAX_SIEVE_AMOUNT] = {};
for(int i=0; i<MAX_SIEVE_AMOUNT; ++i)
{
remainders[i] = mpz_fdiv_ui(hashnum.n,Primes::v[i]);
}
mpz_t newhashnum; mpz_init(newhashnum);
for(uint h=1; h<500; ++h)
{
mpz_add(newhashnum,newhashnum,hashnum.n);
uint c1=0,c2=0,tw=0;
uint sievenumber = 0;
for(uint i=16; i<MAX_SIEVE_AMOUNT; ++i)
{
sievenumber |= Sieve::Get(i,remainders[i]*h%Primes::v[i])^3;
}
sievenumber ^= 3;
//cout << sievenumber << endl;;
if (sievenumber == 0)
continue;
//TODO: fix second parameter, it should be bits!
AllChainTest(&newhashnum, 0, true, &c1, &c2, &tw, sievenumber);
uint c1_i = TargetGetLength(c1);
uint c2_i = TargetGetLength(c2);
uint tw_i = TargetGetLength(tw);
const int minlength=5;
/*{
if (c1_i >= minlength)
cout << "First kind: " << c1_i << endl;
if (c2_i >= minlength)
cout << "Second kind: " << c2_i << endl;
if (tw_i >= minlength)
cout << "Twin kind: " << tw_i << endl;
}*/
++chainspersec[c1_i];
++chainspersec[c2_i];
++chainspersec[tw_i];
++totalpersec;
found = (c1_i >= minlength || c2_i >= minlength || tw_i >= minlength);
if (found)
{
mpz_mul_ui(factor,factor,h);
vector<uchar> auxdata = XPM_create_auxdata(&factor);
Share share;
CPU_Got_share(state,tempwork,auxdata);//tempdata,tempwork.target_share,current_server_id,tempwork.dataid,auxdata);
}
}
return found;
}
void* Reap_CPU_XPM_mtrlt(void* param)
{
Reap_CPU_param* state = (Reap_CPU_param*)param;
Work tempwork;
tempwork.time = 13371337;
//uchar tempdata[80];
//memset(tempdata, 0, 80);
uchar finalhash[32];
uchar temphash[32];
uchar hash_results[1] = {};
uint current_server_id;
uint starttime = ticker();
uint currenttime = starttime;
uint foundprimes=0;
while(!shutdown_now)
{
if (current_work.old)
{
Wait_ms(20);
continue;
}
if (tempwork.time != current_work.time)
{
pthread_mutex_lock(¤t_work_mutex);
tempwork = current_work;
pthread_mutex_unlock(¤t_work_mutex);
//memcpy(tempdata, &tempwork.data[0], 80);
*(uint*)&tempwork.data[76] = state->thread_id<<28;
current_server_id = tempwork.server_id;
}
uint trues=0;
for(uint h=0; h<CPU_BATCH_SIZE; ++h)
{
bool result = MinePrime(state,tempwork);
if (result)
{
foundprimes++;
}
++*(uint*)&tempwork.data[76];
}
//cout << "Every " << double(CPU_BATCH_SIZE)/double(trues) << "th num is true" << endl;
state->hashes += CPU_BATCH_SIZE;
}
pthread_exit(NULL);
return NULL;
}
/*
TEMPLATE OF A CPU MINER
void* Reap_CPU_V1(void* param)
{
Reap_CPU_param* state = (Reap_CPU_param*)param;
Work tempwork;
tempwork.time = 13371337;
uchar tempdata[512];
memset(tempdata, 0, 512);
uchar finalhash[32];
uchar hash_results[1] = {};
uint current_server_id;
while(!shutdown_now)
{
if (current_work.old)
{
Wait_ms(20);
continue;
}
if (tempwork.time != current_work.time)
{
pthread_mutex_lock(¤t_work_mutex);
tempwork = current_work;
pthread_mutex_unlock(¤t_work_mutex);
memcpy(tempdata, &tempwork.data[0], 128);
*(uint*)&tempdata[100] = state->thread_id;
current_server_id = tempwork.server_id;
}
*(ullint*)&tempdata[76] = tempwork.ntime_at_getwork + (ticker()-tempwork.time)/1000;
for(uint h=0; h<CPU_BATCH_SIZE; ++h)
{
BlockHash_1_mine_V1(tempdata, finalhash, hash_results);
if (hash_results[0])
{
BlockHash_1(tempdata, finalhash);
if (finalhash[30] != 0 || finalhash[31] != 0)
cpu_shares_hwinvalid++;
else
cpu_shares_hwvalid++;
if (CPU_Hash_Below_Target(finalhash, &tempwork.target_share[0]))
CPU_Got_share(state,tempdata,tempwork.target_share,current_server_id);
}
++*(uint*)&tempdata[108];
}
state->hashes += CPU_BATCH_SIZE;
}
pthread_exit(NULL);
return NULL;
}
*/