prime.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. #include "config.h"
  2. #include <stdbool.h>
  3. #include <stddef.h>
  4. #include <stdint.h>
  5. #include <stdio.h>
  6. #include <sys/time.h>
  7. #include <gmp.h>
  8. #include "compat.h"
  9. #include "miner.h"
  10. #define nMaxSieveSize 1000000u
  11. #define nPrimeTableLimit nMaxSieveSize
  12. #define nPrimorialTableLimit 100000u
  13. #define PRIME_COUNT 78498
  14. #define PRIMORIAL_COUNT 9592
  15. static
  16. unsigned vPrimes[PRIME_COUNT];
  17. mpz_t bnTwoInverses[PRIME_COUNT];
  18. mpz_t vPrimorials[PRIMORIAL_COUNT];
  19. struct prime_longterms {
  20. unsigned int nPrimorialHashFactor;
  21. int64_t nTimeExpected; // time expected to prime chain (micro-second)
  22. int64_t nTimeExpectedPrev; // time expected to prime chain last time
  23. bool fIncrementPrimorial; // increase or decrease primorial factor
  24. unsigned current_prime;
  25. int64_t nHPSTimerStart;
  26. int64_t nLogTime;
  27. int64_t nPrimeCounter;
  28. int64_t nTestCounter;
  29. #ifdef USE_WEAVE_CHEMISIST
  30. unsigned timeouts;
  31. unsigned completed;
  32. int sieveBuildTime;
  33. #endif
  34. };
  35. static struct prime_longterms *get_prime_longterms();
  36. static
  37. int64_t GetTimeMicros()
  38. {
  39. struct timeval tv;
  40. cgtime(&tv);
  41. return ((int64_t)tv.tv_sec * 1000000) + tv.tv_usec;
  42. }
  43. static
  44. int64_t GetTimeMillis()
  45. {
  46. return GetTimeMicros() / 1000;
  47. }
  48. static
  49. int64_t GetTime()
  50. {
  51. return GetTimeMicros() / 1000000;
  52. }
  53. static
  54. bool error(const char *fmt, ...)
  55. {
  56. puts(fmt); // FIXME
  57. return false;
  58. }
  59. mpz_t bnTwo;
  60. void GeneratePrimeTable()
  61. {
  62. mpz_init_set_ui(bnTwo, 2);
  63. mpz_t bnOne;
  64. mpz_init_set_ui(bnOne, 1);
  65. mpz_t *bnLastPrimorial = &bnOne;
  66. unsigned i = 0;
  67. // Generate prime table using sieve of Eratosthenes
  68. bool vfComposite[nPrimeTableLimit] = {false};
  69. for (unsigned int nFactor = 2; nFactor * nFactor < nPrimeTableLimit; nFactor++)
  70. {
  71. if (vfComposite[nFactor])
  72. continue;
  73. for (unsigned int nComposite = nFactor * nFactor; nComposite < nPrimeTableLimit; nComposite += nFactor)
  74. vfComposite[nComposite] = true;
  75. }
  76. for (unsigned int n = 2; n < nPrimeTableLimit; n++)
  77. if (!vfComposite[n])
  78. {
  79. vPrimes[i] = n;
  80. if (n > 2)
  81. {
  82. // bnOne isn't 1 here, which is okay since it is no longer needed as 1 after prime 2
  83. mpz_init(bnTwoInverses[i]);
  84. mpz_set_ui(bnOne, n);
  85. if (!mpz_invert(bnTwoInverses[i], bnTwo, bnOne))
  86. quit(1, "mpz_invert of 2 failed for prime %u", n);
  87. }
  88. if (n < nPrimorialTableLimit)
  89. {
  90. mpz_init(vPrimorials[i]);
  91. mpz_mul_ui(vPrimorials[i], *bnLastPrimorial, n);
  92. bnLastPrimorial = &vPrimorials[i];
  93. }
  94. ++i;
  95. }
  96. mpz_clear(bnOne);
  97. applog(LOG_DEBUG, "GeneratePrimeTable() : prime table [1, %d] generated with %lu primes", nPrimeTableLimit, (unsigned long)i);
  98. }
  99. #define nFractionalBits 24
  100. #define TARGET_FRACTIONAL_MASK ((1u << nFractionalBits) - 1)
  101. #define TARGET_LENGTH_MASK (~TARGET_FRACTIONAL_MASK)
  102. // Check Fermat probable primality test (2-PRP): 2 ** (n-1) = 1 (mod n)
  103. // true: n is probable prime
  104. // false: n is composite; set fractional length in the nLength output
  105. static
  106. bool FermatProbablePrimalityTest(mpz_t *n, unsigned int *pnLength)
  107. {
  108. mpz_t a, e, r;
  109. mpz_init_set_ui(a, 2); // base; Fermat witness
  110. mpz_init(e);
  111. mpz_sub_ui(e, *n, 1);
  112. mpz_init(r);
  113. mpz_powm(r, a, e, *n);
  114. mpz_clear(a);
  115. mpz_clear(e);
  116. if (!mpz_cmp_ui(r, 1))
  117. {
  118. mpz_clear(r);
  119. return true;
  120. }
  121. // Failed Fermat test, calculate fractional length
  122. // nFractionalLength = ( (n-r) << nFractionalBits ) / n
  123. mpz_sub(r, *n, r);
  124. mpz_mul_2exp(r, r, nFractionalBits);
  125. mpz_fdiv_q(r, r, *n);
  126. unsigned int nFractionalLength = mpz_get_ui(r);
  127. mpz_clear(r);
  128. if (nFractionalLength >= (1 << nFractionalBits))
  129. return error("FermatProbablePrimalityTest() : fractional assert");
  130. *pnLength = (*pnLength & TARGET_LENGTH_MASK) | nFractionalLength;
  131. return false;
  132. }
  133. static
  134. unsigned int TargetGetLength(unsigned int nBits)
  135. {
  136. return ((nBits & TARGET_LENGTH_MASK) >> nFractionalBits);
  137. }
  138. static
  139. void TargetIncrementLength(unsigned int *pnBits)
  140. {
  141. *pnBits += (1 << nFractionalBits);
  142. }
  143. // Test probable primality of n = 2p +/- 1 based on Euler, Lagrange and Lifchitz
  144. // fSophieGermain:
  145. // true: n = 2p+1, p prime, aka Cunningham Chain of first kind
  146. // false: n = 2p-1, p prime, aka Cunningham Chain of second kind
  147. // Return values
  148. // true: n is probable prime
  149. // false: n is composite; set fractional length in the nLength output
  150. static
  151. bool EulerLagrangeLifchitzPrimalityTest(mpz_t *n, bool fSophieGermain, unsigned int *pnLength)
  152. {
  153. mpz_t a, e, r;
  154. mpz_init_set_ui(a, 2);
  155. mpz_init(e);
  156. mpz_sub_ui(e, *n, 1);
  157. mpz_fdiv_q_2exp(e, e, 1);
  158. mpz_init(r);
  159. mpz_powm(r, a, e, *n);
  160. mpz_clear(a);
  161. mpz_clear(e);
  162. unsigned nMod8 = mpz_fdiv_ui(*n, 8);
  163. bool fPassedTest = false;
  164. if (fSophieGermain && (nMod8 == 7)) // Euler & Lagrange
  165. fPassedTest = !mpz_cmp_ui(r, 1);
  166. else if (nMod8 == (fSophieGermain ? 3 : 5)) // Lifchitz
  167. {
  168. mpz_t mp;
  169. mpz_init_set_ui(mp, 1);
  170. mpz_add(mp, r, mp);
  171. fPassedTest = !mpz_cmp(mp, *n);
  172. mpz_clear(mp);
  173. }
  174. else if ((!fSophieGermain) && (nMod8 == 1)) // LifChitz
  175. fPassedTest = !mpz_cmp_ui(r, 1);
  176. else
  177. {
  178. mpz_clear(r);
  179. return error("EulerLagrangeLifchitzPrimalityTest() : invalid n %% 8 = %d, %s", nMod8, (fSophieGermain? "first kind" : "second kind"));
  180. }
  181. if (fPassedTest)
  182. {
  183. mpz_clear(r);
  184. return true;
  185. }
  186. // Failed test, calculate fractional length
  187. // derive Fermat test remainder
  188. mpz_mul(r, r, r);
  189. mpz_fdiv_r(r, r, *n);
  190. // nFractionalLength = ( (n-r) << nFractionalBits ) / n
  191. mpz_sub(r, *n, r);
  192. mpz_mul_2exp(r, r, nFractionalBits);
  193. mpz_fdiv_q(r, r, *n);
  194. unsigned int nFractionalLength = mpz_get_ui(r);
  195. mpz_clear(r);
  196. if (nFractionalLength >= (1 << nFractionalBits))
  197. return error("EulerLagrangeLifchitzPrimalityTest() : fractional assert");
  198. *pnLength = (*pnLength & TARGET_LENGTH_MASK) | nFractionalLength;
  199. return false;
  200. }
  201. // Test Probable Cunningham Chain for: n
  202. // fSophieGermain:
  203. // true - Test for Cunningham Chain of first kind (n, 2n+1, 4n+3, ...)
  204. // false - Test for Cunningham Chain of second kind (n, 2n-1, 4n-3, ...)
  205. // Return value:
  206. // true - Probable Cunningham Chain found (length at least 2)
  207. // false - Not Cunningham Chain
  208. static
  209. bool ProbableCunninghamChainTest(mpz_t *n, bool fSophieGermain, bool fFermatTest, unsigned int *pnProbableChainLength)
  210. {
  211. #ifdef SUPERDEBUG
  212. printf("ProbableCunninghamChainTest(");
  213. mpz_out_str(stdout, 0x10, *n);
  214. printf(", %d, %d, %u)\n", (int)fSophieGermain, (int)fFermatTest, *pnProbableChainLength);
  215. #endif
  216. *pnProbableChainLength = 0;
  217. mpz_t N;
  218. mpz_init_set(N, *n);
  219. // Fermat test for n first
  220. if (!FermatProbablePrimalityTest(&N, pnProbableChainLength))
  221. {
  222. mpz_clear(N);
  223. return false;
  224. }
  225. #ifdef SUPERDEBUG
  226. printf("N=");
  227. mpz_out_str(stdout, 0x10, N);
  228. printf("\n");
  229. #endif
  230. // Euler-Lagrange-Lifchitz test for the following numbers in chain
  231. while (true)
  232. {
  233. TargetIncrementLength(pnProbableChainLength);
  234. mpz_add(N, N, N);
  235. if (fSophieGermain)
  236. mpz_add_ui(N, N, 1);
  237. else
  238. mpz_sub_ui(N, N, 1);
  239. if (fFermatTest)
  240. {
  241. if (!FermatProbablePrimalityTest(&N, pnProbableChainLength))
  242. break;
  243. }
  244. else
  245. {
  246. #ifdef SUPERDEBUG
  247. if (!fSophieGermain)
  248. {
  249. printf("EulerLagrangeLifchitzPrimalityTest(");
  250. mpz_out_str(stdout, 0x10, N);
  251. printf(", 1, %d)\n", *pnProbableChainLength);
  252. }
  253. #endif
  254. if (!EulerLagrangeLifchitzPrimalityTest(&N, fSophieGermain, pnProbableChainLength))
  255. break;
  256. }
  257. }
  258. mpz_clear(N);
  259. #ifdef SUPERDEBUG
  260. printf("PCCT => %u (%u)\n", TargetGetLength(*pnProbableChainLength), *pnProbableChainLength);
  261. #endif
  262. return (TargetGetLength(*pnProbableChainLength) >= 2);
  263. }
  264. static
  265. unsigned int TargetFromInt(unsigned int nLength)
  266. {
  267. return (nLength << nFractionalBits);
  268. }
  269. // Test probable prime chain for: nOrigin
  270. // Return value:
  271. // true - Probable prime chain found (one of nChainLength meeting target)
  272. // false - prime chain too short (none of nChainLength meeting target)
  273. static
  274. bool ProbablePrimeChainTest(mpz_t *bnPrimeChainOrigin, unsigned int nBits, bool fFermatTest, unsigned int *pnChainLengthCunningham1, unsigned int *pnChainLengthCunningham2, unsigned int *pnChainLengthBiTwin)
  275. {
  276. *pnChainLengthCunningham1 = 0;
  277. *pnChainLengthCunningham2 = 0;
  278. *pnChainLengthBiTwin = 0;
  279. mpz_t mp;
  280. mpz_init(mp);
  281. // Test for Cunningham Chain of first kind
  282. mpz_sub_ui(mp, *bnPrimeChainOrigin, 1);
  283. ProbableCunninghamChainTest(&mp, true, fFermatTest, pnChainLengthCunningham1);
  284. // Test for Cunningham Chain of second kind
  285. mpz_add_ui(mp, *bnPrimeChainOrigin, 1);
  286. ProbableCunninghamChainTest(&mp, false, fFermatTest, pnChainLengthCunningham2);
  287. mpz_clear(mp);
  288. // Figure out BiTwin Chain length
  289. // BiTwin Chain allows a single prime at the end for odd length chain
  290. *pnChainLengthBiTwin = (TargetGetLength(*pnChainLengthCunningham1) > TargetGetLength(*pnChainLengthCunningham2)) ? (*pnChainLengthCunningham2 + TargetFromInt(TargetGetLength(*pnChainLengthCunningham2)+1)) : (*pnChainLengthCunningham1 + TargetFromInt(TargetGetLength(*pnChainLengthCunningham1)));
  291. return (*pnChainLengthCunningham1 >= nBits || *pnChainLengthCunningham2 >= nBits || *pnChainLengthBiTwin >= nBits);
  292. }
  293. struct SieveOfEratosthenes {
  294. bool valid;
  295. unsigned int nSieveSize; // size of the sieve
  296. unsigned int nBits; // target of the prime chain to search for
  297. mpz_t hashBlockHeader; // block header hash
  298. mpz_t bnFixedFactor; // fixed factor to derive the chain
  299. // bitmaps of the sieve, index represents the variable part of multiplier
  300. bool vfCompositeCunningham1[1000000];
  301. bool vfCompositeCunningham2[1000000];
  302. bool vfCompositeBiTwin[1000000];
  303. unsigned int nPrimeSeq; // prime sequence number currently being processed
  304. unsigned int nCandidateMultiplier; // current candidate for power test
  305. };
  306. static
  307. void psieve_reset(struct SieveOfEratosthenes *psieve)
  308. {
  309. mpz_clear(psieve->hashBlockHeader);
  310. mpz_clear(psieve->bnFixedFactor);
  311. psieve->valid = false;
  312. }
  313. static
  314. void psieve_init(struct SieveOfEratosthenes *psieve, unsigned nSieveSize, unsigned nBits, mpz_t *hashBlockHeader, mpz_t *bnFixedMultiplier)
  315. {
  316. assert(!psieve->valid);
  317. *psieve = (struct SieveOfEratosthenes){
  318. .valid = true,
  319. .nSieveSize = nSieveSize,
  320. .nBits = nBits,
  321. };
  322. mpz_init_set(psieve->hashBlockHeader, *hashBlockHeader);
  323. mpz_init(psieve->bnFixedFactor);
  324. mpz_mul(psieve->bnFixedFactor, *bnFixedMultiplier, *hashBlockHeader);
  325. }
  326. #ifdef USE_WEAVE_CHEMISIST
  327. #define TESTING_FREQUENCY 1000
  328. static
  329. void Weave_Chemisist(struct thr_info *thr, struct SieveOfEratosthenes *psieve) {
  330. struct prime_longterms *pl = get_prime_longterms();
  331. int64_t nStart = GetTimeMicros(), nCurrent = GetTimeMicros();
  332. mpz_t bnFixedInverse, p;
  333. mpz_init(bnFixedInverse);
  334. mpz_init(p);
  335. unsigned int nChainLength = TargetGetLength(psieve->nBits);
  336. unsigned int nChainLength2 = 2*nChainLength;
  337. unsigned int nSolvedMultiplier, nVariableMultiplier, nBiTwinSeq, uP;
  338. unsigned int nPrimeSeqMax;
  339. mpz_t *pbnTwoInverse;
  340. if(vPrimes[PRIME_COUNT-1] < psieve->nSieveSize) {
  341. nPrimeSeqMax = PRIME_COUNT;
  342. } else {
  343. for(nPrimeSeqMax = 0; nPrimeSeqMax < PRIME_COUNT && vPrimes[nPrimeSeqMax] < psieve->nSieveSize; nPrimeSeqMax++) ;
  344. }
  345. // create no new variables during the loop to eliminate all malloc() operations
  346. for(psieve->nPrimeSeq = 0; psieve->nPrimeSeq < nPrimeSeqMax; psieve->nPrimeSeq++) {
  347. uP = vPrimes[psieve->nPrimeSeq]; // aka nPrime
  348. mpz_set_ui(p, uP);
  349. if (mpz_fdiv_ui(psieve->bnFixedFactor, uP) == 0)
  350. {
  351. // Nothing in the sieve is divisible by this prime
  352. continue;
  353. }
  354. // Find the modulo inverse of fixed factor
  355. if (!mpz_invert(bnFixedInverse, psieve->bnFixedFactor, p))
  356. {
  357. // TODO: mpz_clear
  358. error("CSieveOfEratosthenes::Weave(): BN_mod_inverse of fixed factor failed for prime #%u=%u", psieve->nPrimeSeq, uP);
  359. return;
  360. }
  361. pbnTwoInverse = &bnTwoInverses[psieve->nPrimeSeq];
  362. // calling the GetTimeMicros() method and the additional boolean testing ends up taking a while, so the speed can be increased by just calculating it every so often.
  363. if(psieve->nPrimeSeq % TESTING_FREQUENCY == 0)
  364. {
  365. nCurrent = GetTimeMicros() - nStart;
  366. if(nCurrent > (pl->sieveBuildTime))
  367. return;
  368. }
  369. for (nBiTwinSeq = 0; nBiTwinSeq < nChainLength; nBiTwinSeq++)
  370. {
  371. if((nBiTwinSeq & 1u) == 0)
  372. {
  373. mpz_mul_ui(p, bnFixedInverse, uP + 1);
  374. nSolvedMultiplier = mpz_fdiv_ui(p, uP);
  375. for (nVariableMultiplier = nSolvedMultiplier; nVariableMultiplier < psieve->nSieveSize; nVariableMultiplier += uP)
  376. psieve->vfCompositeCunningham1[nVariableMultiplier] = true;
  377. }
  378. else
  379. {
  380. mpz_mul_ui(p, bnFixedInverse, uP - 1);
  381. nSolvedMultiplier = mpz_fdiv_ui(p, uP);
  382. mpz_mul(bnFixedInverse, bnFixedInverse, *pbnTwoInverse); // for next number in chain
  383. for (nVariableMultiplier = nSolvedMultiplier; nVariableMultiplier < psieve->nSieveSize; nVariableMultiplier += uP)
  384. psieve->vfCompositeCunningham2[nVariableMultiplier] = true;
  385. }
  386. for (nVariableMultiplier = nSolvedMultiplier; nVariableMultiplier < psieve->nSieveSize; nVariableMultiplier += uP)
  387. psieve->vfCompositeBiTwin[nVariableMultiplier] = true;
  388. }
  389. // continue loop without the composite_bi_twin
  390. for (; nBiTwinSeq < nChainLength2; nBiTwinSeq++)
  391. {
  392. if((nBiTwinSeq & 1u) == 0)
  393. {
  394. mpz_mul_ui(p, bnFixedInverse, uP + 1);
  395. nSolvedMultiplier = mpz_fdiv_ui(p, uP);
  396. for (nVariableMultiplier = nSolvedMultiplier; nVariableMultiplier < psieve->nSieveSize; nVariableMultiplier += uP)
  397. psieve->vfCompositeCunningham1[nVariableMultiplier] = true;
  398. }
  399. else
  400. {
  401. mpz_mul_ui(p, bnFixedInverse, uP - 1);
  402. nSolvedMultiplier = mpz_fdiv_ui(p, uP);
  403. mpz_mul(bnFixedInverse, bnFixedInverse, *pbnTwoInverse); // for next number in chain
  404. for (nVariableMultiplier = nSolvedMultiplier; nVariableMultiplier < psieve->nSieveSize; nVariableMultiplier += uP)
  405. psieve->vfCompositeCunningham2[nVariableMultiplier] = true;
  406. }
  407. }
  408. }
  409. }
  410. #else
  411. // Weave sieve for the next prime in table
  412. // Return values:
  413. // True - weaved another prime; nComposite - number of composites removed
  414. // False - sieve already completed
  415. static
  416. bool psieve_Weave(struct SieveOfEratosthenes *psieve)
  417. {
  418. unsigned nPrime = vPrimes[psieve->nPrimeSeq];
  419. if (psieve->nPrimeSeq >= PRIME_COUNT || nPrime >= psieve->nSieveSize)
  420. return false; // sieve has been completed
  421. if (mpz_fdiv_ui(psieve->bnFixedFactor, nPrime) == 0)
  422. {
  423. // Nothing in the sieve is divisible by this prime
  424. ++psieve->nPrimeSeq;
  425. return true;
  426. }
  427. // Find the modulo inverse of fixed factor
  428. mpz_t bnFixedInverse, p;
  429. mpz_init(bnFixedInverse);
  430. mpz_init_set_ui(p, nPrime);
  431. if (!mpz_invert(bnFixedInverse, psieve->bnFixedFactor, p))
  432. {
  433. mpz_clear(p);
  434. mpz_clear(bnFixedInverse);
  435. return error("CSieveOfEratosthenes::Weave(): BN_mod_inverse of fixed factor failed for prime #%u=%u", psieve->nPrimeSeq, nPrime);
  436. }
  437. mpz_t *pbnTwoInverse = &bnTwoInverses[psieve->nPrimeSeq];
  438. // Weave the sieve for the prime
  439. unsigned int nChainLength = TargetGetLength(psieve->nBits);
  440. for (unsigned int nBiTwinSeq = 0; nBiTwinSeq < 2 * nChainLength; nBiTwinSeq++)
  441. {
  442. bool b = nBiTwinSeq & 1;
  443. // Find the first number that's divisible by this prime
  444. int nDelta = (b ? 1 : -1);
  445. mpz_mul_ui(p, bnFixedInverse, nPrime - nDelta);
  446. unsigned int nSolvedMultiplier = mpz_fdiv_ui(p, nPrime);
  447. if (b)
  448. mpz_mul(bnFixedInverse, bnFixedInverse, *pbnTwoInverse); // for next number in chain
  449. if (nBiTwinSeq < nChainLength)
  450. for (unsigned int nVariableMultiplier = nSolvedMultiplier; nVariableMultiplier < psieve->nSieveSize; nVariableMultiplier += nPrime)
  451. psieve->vfCompositeBiTwin[nVariableMultiplier] = true;
  452. if (!b)
  453. {
  454. for (unsigned int nVariableMultiplier = nSolvedMultiplier; nVariableMultiplier < psieve->nSieveSize; nVariableMultiplier += nPrime)
  455. psieve->vfCompositeCunningham1[nVariableMultiplier] = true;
  456. }
  457. else
  458. for (unsigned int nVariableMultiplier = nSolvedMultiplier; nVariableMultiplier < psieve->nSieveSize; nVariableMultiplier += nPrime)
  459. psieve->vfCompositeCunningham2[nVariableMultiplier] = true;
  460. }
  461. mpz_clear(p);
  462. mpz_clear(bnFixedInverse);
  463. ++psieve->nPrimeSeq;
  464. return true;
  465. }
  466. #endif
  467. static
  468. bool psieve_GetNextCandidateMultiplier(struct SieveOfEratosthenes *psieve, unsigned int *pnVariableMultiplier)
  469. {
  470. while (true)
  471. {
  472. psieve->nCandidateMultiplier++;
  473. if (psieve->nCandidateMultiplier >= psieve->nSieveSize)
  474. {
  475. psieve->nCandidateMultiplier = 0;
  476. return false;
  477. }
  478. if (!psieve->vfCompositeCunningham1[psieve->nCandidateMultiplier] ||
  479. !psieve->vfCompositeCunningham2[psieve->nCandidateMultiplier] ||
  480. !psieve->vfCompositeBiTwin[psieve->nCandidateMultiplier])
  481. {
  482. *pnVariableMultiplier = psieve->nCandidateMultiplier;
  483. return true;
  484. }
  485. }
  486. }
  487. // Get total number of candidates for power test
  488. static
  489. unsigned int psieve_GetCandidateCount(struct SieveOfEratosthenes *psieve)
  490. {
  491. unsigned int nCandidates = 0;
  492. for (unsigned int nMultiplier = 0; nMultiplier < psieve->nSieveSize; nMultiplier++)
  493. {
  494. if (!psieve->vfCompositeCunningham1[nMultiplier] || !psieve->vfCompositeCunningham2[nMultiplier] || !psieve->vfCompositeBiTwin[nMultiplier])
  495. nCandidates++;
  496. }
  497. return nCandidates;
  498. }
  499. // Get progress percentage of the sieve
  500. static
  501. unsigned psieve_GetProgressPercentage(struct SieveOfEratosthenes *psieve)
  502. {
  503. unsigned rv;
  504. if (psieve->nPrimeSeq >= PRIME_COUNT)
  505. rv = nPrimeTableLimit;
  506. else
  507. rv = vPrimes[psieve->nPrimeSeq];
  508. rv = rv * 100 / psieve->nSieveSize;
  509. if (rv > 100)
  510. rv = 100;
  511. return rv;
  512. }
  513. // Mine probable prime chain of form: n = h * p# +/- 1
  514. bool MineProbablePrimeChain(struct thr_info *thr, struct SieveOfEratosthenes *psieve, const uint8_t *header, mpz_t *hash, mpz_t *bnFixedMultiplier, bool *pfNewBlock, unsigned *pnTriedMultiplier, unsigned *pnProbableChainLength, unsigned *pnTests, unsigned *pnPrimesHit, struct work *work)
  515. {
  516. const char *proc_repr = thr->cgpu->proc_repr;
  517. #ifdef USE_WEAVE_CHEMISIST
  518. struct prime_longterms *pl = get_prime_longterms();
  519. #endif
  520. const uint32_t *pnbits = (void*)&header[72];
  521. *pnProbableChainLength = 0;
  522. *pnTests = 0;
  523. *pnPrimesHit = 0;
  524. if (*pfNewBlock && psieve->valid)
  525. {
  526. // Must rebuild the sieve
  527. psieve_reset(psieve);
  528. }
  529. *pfNewBlock = false;
  530. int64_t nStart, nCurrent; // microsecond timer
  531. #ifdef USE_WEAVE_CHEMISIST
  532. int64_t nSearch;
  533. #endif
  534. if (!psieve->valid)
  535. {
  536. // Build sieve
  537. nStart = GetTimeMicros();
  538. #ifdef SUPERDEBUG
  539. fprintf(stderr, "psieve_init(?, %u, %08x, ", nMaxSieveSize, *pnbits);
  540. mpz_out_str(stderr, 0x10, *hash);
  541. fprintf(stderr, ", ");
  542. mpz_out_str(stderr, 0x10, *bnFixedMultiplier);
  543. fprintf(stderr, ")\n");
  544. #endif
  545. psieve_init(psieve, nMaxSieveSize, *pnbits, hash, bnFixedMultiplier);
  546. #ifdef USE_WEAVE_CHEMISIST
  547. Weave_Chemisist(thr, psieve);
  548. #else
  549. while (psieve_Weave(psieve))
  550. #endif
  551. if (unlikely(thr->work_restart))
  552. {
  553. applog(LOG_DEBUG, "%"PRIpreprv": MineProbablePrimeChain() : weaved interrupted by work restart", proc_repr);
  554. return false;
  555. }
  556. applog(LOG_DEBUG, "%"PRIpreprv": MineProbablePrimeChain() : new sieve (%u/%u@%u%%) ready in %uus", proc_repr, psieve_GetCandidateCount(psieve), nMaxSieveSize, psieve_GetProgressPercentage(psieve), (unsigned int) (GetTimeMicros() - nStart));
  557. }
  558. mpz_t bnChainOrigin;
  559. mpz_init(bnChainOrigin);
  560. nStart = GetTimeMicros();
  561. nCurrent = nStart;
  562. #ifdef USE_WEAVE_CHEMISIST
  563. while (nCurrent - nStart < pl->sieveBuildTime * 3 && nCurrent >= nStart)
  564. #else
  565. while (nCurrent - nStart < 10000 && nCurrent >= nStart)
  566. #endif
  567. {
  568. if (unlikely(thr->work_restart))
  569. {
  570. applog(LOG_DEBUG, "%"PRIpreprv": MineProbablePrimeChain() : interrupted by work restart", proc_repr);
  571. return false;
  572. }
  573. ++*pnTests;
  574. if (!psieve_GetNextCandidateMultiplier(psieve, pnTriedMultiplier))
  575. {
  576. // power tests completed for the sieve
  577. psieve_reset(psieve);
  578. *pfNewBlock = true; // notify caller to change nonce
  579. #ifdef USE_WEAVE_CHEMISIST
  580. ++pl->completed;
  581. nSearch = GetTimeMicros() - nStart;
  582. if (nSearch < pl->sieveBuildTime)
  583. pl->sieveBuildTime *= 0.99;
  584. else
  585. pl->sieveBuildTime *= 1.01;
  586. applog(LOG_DEBUG, "%"PRIpreprv": %u ms (Timers: num power tests completed: %u", proc_repr, (unsigned int) (GetTimeMicros() - nStart)/1000, pl->completed);
  587. #endif
  588. mpz_clear(bnChainOrigin);
  589. return false;
  590. }
  591. #ifdef SUPERDEBUG
  592. printf("nTriedMultiplier=%d\n", *pnTriedMultiplier=640150);
  593. #endif
  594. mpz_mul(bnChainOrigin, *hash, *bnFixedMultiplier);
  595. mpz_mul_ui(bnChainOrigin, bnChainOrigin, *pnTriedMultiplier);
  596. unsigned int nChainLengthCunningham1 = 0;
  597. unsigned int nChainLengthCunningham2 = 0;
  598. unsigned int nChainLengthBiTwin = 0;
  599. #ifdef SUPERDEBUG
  600. printf("ProbablePrimeChainTest(bnChainOrigin=");
  601. mpz_out_str(stdout, 0x10, bnChainOrigin);
  602. printf(", nbits=%08lx, false, %d, %d, %d)\n", (unsigned long)*pnbits, nChainLengthCunningham1, nChainLengthCunningham2, nChainLengthBiTwin);
  603. #endif
  604. if (ProbablePrimeChainTest(&bnChainOrigin, *pnbits, false, &nChainLengthCunningham1, &nChainLengthCunningham2, &nChainLengthBiTwin))
  605. {
  606. // bnChainOrigin is not used again, so recycled here for the result
  607. // block.bnPrimeChainMultiplier = *bnFixedMultiplier * *pnTriedMultiplier;
  608. mpz_mul_ui(bnChainOrigin, *bnFixedMultiplier, *pnTriedMultiplier);
  609. size_t exportsz, resultoff;
  610. uint8_t *export = mpz_export(NULL, &exportsz, -1, 1, -1, 0, bnChainOrigin);
  611. assert(exportsz < 250); // FIXME: bitcoin varint
  612. resultoff = 1;
  613. if (export[0] & 0x80)
  614. ++resultoff;
  615. uint8_t *result = malloc(exportsz + resultoff);
  616. result[0] = exportsz + resultoff - 1;
  617. result[1] = '\0';
  618. memcpy(&result[resultoff], export, exportsz);
  619. if (mpz_sgn(bnChainOrigin) < 0)
  620. result[1] |= 0x80;
  621. free(export);
  622. work->sig = result;
  623. work->sigsz = exportsz + resultoff;
  624. char hex[1 + (work->sigsz * 2)];
  625. bin2hex(hex, work->sig, work->sigsz);
  626. applog(LOG_DEBUG, "%"PRIpreprv": SIGNATURE: %s", proc_repr, hex);
  627. // printf("Probable prime chain found for block=%s!!\n Target: %s\n Length: (%s %s %s)\n", block.GetHash().GetHex().c_str(),
  628. // TargetToString(nbits).c_str(), TargetToString(nChainLengthCunningham1).c_str(), TargetToString(nChainLengthCunningham2).c_str(), TargetToString(nChainLengthBiTwin).c_str());
  629. applog(LOG_DEBUG, "%"PRIpreprv": Probable prime chain found for block", proc_repr);
  630. *pnProbableChainLength = nChainLengthCunningham1;
  631. if (*pnProbableChainLength < nChainLengthCunningham2)
  632. *pnProbableChainLength = nChainLengthCunningham2;
  633. if (*pnProbableChainLength < nChainLengthBiTwin)
  634. *pnProbableChainLength = nChainLengthBiTwin;
  635. mpz_clear(bnChainOrigin);
  636. return true;
  637. }
  638. *pnProbableChainLength = nChainLengthCunningham1;
  639. if (*pnProbableChainLength < nChainLengthCunningham2)
  640. *pnProbableChainLength = nChainLengthCunningham2;
  641. if (*pnProbableChainLength < nChainLengthBiTwin)
  642. *pnProbableChainLength = nChainLengthBiTwin;
  643. if(TargetGetLength(*pnProbableChainLength) >= 1)
  644. ++*pnPrimesHit;
  645. nCurrent = GetTimeMicros();
  646. }
  647. mpz_clear(bnChainOrigin);
  648. #ifdef USE_WEAVE_CHEMISIST
  649. ++pl->timeouts;
  650. pl->sieveBuildTime *= 1.025;
  651. applog(LOG_DEBUG, "%"PRIpreprv": %u ms (Timers: num total time outs: %u", proc_repr, (unsigned int) (GetTimeMicros() - nStart)/1000, pl->completed);
  652. #endif
  653. return false; // stop as timed out
  654. }
  655. // Checks that the high bit is set, and low bit is clear (ie, divisible by 2)
  656. static
  657. bool check_ends(const uint8_t *hash)
  658. {
  659. return (hash[31] & 0x80) && !(hash[0] & 1);
  660. }
  661. static inline
  662. void set_mpz_to_hash(mpz_t *hash, const uint8_t *hashb)
  663. {
  664. mpz_import(*hash, 8, -1, 4, -1, 0, hashb);
  665. }
  666. static
  667. struct prime_longterms *get_prime_longterms()
  668. {
  669. struct bfgtls_data *bfgtls = get_bfgtls();
  670. struct prime_longterms *pl = bfgtls->prime_longterms;
  671. if (unlikely(!pl))
  672. {
  673. pl = bfgtls->prime_longterms = malloc(sizeof(*pl));
  674. *pl = (struct prime_longterms){
  675. .nPrimorialHashFactor = 7,
  676. .fIncrementPrimorial = true,
  677. .current_prime = 3, // index 3 is prime number 7
  678. .nHPSTimerStart = GetTimeMillis(),
  679. #ifdef USE_WEAVE_CHEMISIST
  680. .sieveBuildTime = 400000,
  681. #endif
  682. };
  683. }
  684. return pl;
  685. }
  686. bool prime(struct thr_info *thr, uint8_t *header, struct work *work)
  687. {
  688. const char *proc_repr = thr->cgpu->proc_repr;
  689. struct prime_longterms *pl = get_prime_longterms();
  690. bool rv = false;
  691. uint32_t *nonce = (void*)(&header[76]);
  692. unsigned char hashb[32];
  693. mpz_t hash, bnPrimeMin;
  694. mpz_init(hash);
  695. mpz_init_set_ui(bnPrimeMin, 1);
  696. mpz_mul_2exp(bnPrimeMin, bnPrimeMin, 255);
  697. bool fNewBlock = true;
  698. unsigned int nTriedMultiplier = 0;
  699. struct SieveOfEratosthenes sieve = {
  700. .valid = false,
  701. };
  702. const unsigned nHashFactor = 210;
  703. // a valid header must hash to have the MSB set, and a multiple of nHashFactor
  704. while (true)
  705. {
  706. gen_hash(header, hashb, 80);
  707. if (check_ends(hashb))
  708. {
  709. set_mpz_to_hash(&hash, hashb);
  710. if (!mpz_fdiv_ui(hash, 105))
  711. break;
  712. }
  713. if (unlikely(*nonce == 0xffffffff))
  714. {
  715. mpz_clear(hash);
  716. mpz_clear(bnPrimeMin);
  717. return false;
  718. }
  719. ++*nonce;
  720. }
  721. {
  722. char hex[9];
  723. bin2hex(hex, nonce, 4);
  724. applog(LOG_DEBUG, "%"PRIpreprv": Pass 1 found: %s", proc_repr, hex);
  725. }
  726. // primorial fixed multiplier
  727. mpz_t bnPrimorial;
  728. mpz_init(bnPrimorial);
  729. unsigned int nRoundTests = 0;
  730. unsigned int nRoundPrimesHit = 0;
  731. int64_t nPrimeTimerStart = GetTimeMicros();
  732. if (pl->nTimeExpected > pl->nTimeExpectedPrev)
  733. pl->fIncrementPrimorial = !pl->fIncrementPrimorial;
  734. pl->nTimeExpectedPrev = pl->nTimeExpected;
  735. // dynamic adjustment of primorial multiplier
  736. if (pl->fIncrementPrimorial)
  737. {
  738. ++pl->current_prime;
  739. if (pl->current_prime >= PRIMORIAL_COUNT)
  740. quit(1, "primorial increment overflow");
  741. }
  742. else if (vPrimes[pl->current_prime] > pl->nPrimorialHashFactor)
  743. {
  744. if (!pl->current_prime)
  745. quit(1, "primorial decrement overflow");
  746. --pl->current_prime;
  747. }
  748. mpz_set(bnPrimorial, vPrimorials[pl->current_prime]);
  749. while (true)
  750. {
  751. unsigned int nTests = 0;
  752. unsigned int nPrimesHit = 0;
  753. mpz_t bnMultiplierMin;
  754. // bnMultiplierMin = bnPrimeMin * nHashFactor / hash + 1
  755. mpz_init(bnMultiplierMin);
  756. mpz_mul_ui(bnMultiplierMin, bnPrimeMin, nHashFactor);
  757. mpz_fdiv_q(bnMultiplierMin, bnMultiplierMin, hash);
  758. mpz_add_ui(bnMultiplierMin, bnMultiplierMin, 1);
  759. while (mpz_cmp(bnPrimorial, bnMultiplierMin) < 0)
  760. {
  761. ++pl->current_prime;
  762. if (pl->current_prime >= PRIMORIAL_COUNT)
  763. quit(1, "primorial minimum overflow");
  764. mpz_set(bnPrimorial, vPrimorials[pl->current_prime]);
  765. }
  766. mpz_clear(bnMultiplierMin);
  767. mpz_t bnFixedMultiplier;
  768. mpz_init(bnFixedMultiplier);
  769. // bnFixedMultiplier = (bnPrimorial > nHashFactor) ? (bnPrimorial / nHashFactor) : 1
  770. if (mpz_cmp_ui(bnPrimorial, nHashFactor) > 0)
  771. {
  772. mpz_t bnHashFactor;
  773. mpz_init_set_ui(bnHashFactor, nHashFactor);
  774. mpz_fdiv_q(bnFixedMultiplier, bnPrimorial, bnHashFactor);
  775. mpz_clear(bnHashFactor);
  776. }
  777. else
  778. mpz_set_ui(bnFixedMultiplier, 1);
  779. #ifdef SUPERDEBUG
  780. fprintf(stderr,"bnFixedMultiplier=");
  781. mpz_out_str(stderr, 0x10, bnFixedMultiplier);
  782. fprintf(stderr, " nPrimorialMultiplier=%u nTriedMultiplier=%u\n", vPrimes[pl->current_prime], nTriedMultiplier);
  783. #endif
  784. // mine for prime chain
  785. unsigned int nProbableChainLength;
  786. if (MineProbablePrimeChain(thr, &sieve, header, &hash, &bnFixedMultiplier, &fNewBlock, &nTriedMultiplier, &nProbableChainLength, &nTests, &nPrimesHit, work))
  787. {
  788. // TODO CheckWork(pblock, *pwalletMain, reservekey);
  789. mpz_clear(bnFixedMultiplier);
  790. rv = true;
  791. break;
  792. }
  793. if (unlikely(thr->work_restart))
  794. {
  795. applog(LOG_DEBUG, "%"PRIpreprv": prime interrupted by work restart", proc_repr);
  796. break;
  797. }
  798. mpz_clear(bnFixedMultiplier);
  799. nRoundTests += nTests;
  800. nRoundPrimesHit += nPrimesHit;
  801. // Meter primes/sec
  802. if (pl->nHPSTimerStart == 0)
  803. {
  804. pl->nHPSTimerStart = GetTimeMillis();
  805. pl->nPrimeCounter = 0;
  806. pl->nTestCounter = 0;
  807. }
  808. else
  809. {
  810. pl->nPrimeCounter += nPrimesHit;
  811. pl->nTestCounter += nTests;
  812. }
  813. if (GetTimeMillis() - pl->nHPSTimerStart > 60000)
  814. {
  815. double dPrimesPerMinute = 60000.0 * pl->nPrimeCounter / (GetTimeMillis() - pl->nHPSTimerStart);
  816. double dPrimesPerSec = dPrimesPerMinute / 60.0;
  817. double dTestsPerMinute = 60000.0 * pl->nTestCounter / (GetTimeMillis() - pl->nHPSTimerStart);
  818. pl->nHPSTimerStart = GetTimeMillis();
  819. pl->nPrimeCounter = 0;
  820. pl->nTestCounter = 0;
  821. if (GetTime() - pl->nLogTime > 60)
  822. {
  823. pl->nLogTime = GetTime();
  824. applog(LOG_NOTICE, "%"PRIpreprv": primemeter %6d test/s %5dpps", proc_repr, (int)(dTestsPerMinute / 60.0), (int)dPrimesPerSec);
  825. }
  826. }
  827. // Check for stop or if block needs to be rebuilt
  828. // TODO
  829. // boost::this_thread::interruption_point();
  830. // if (vNodes.empty())
  831. // break;
  832. if (fNewBlock || thr->work_restart /*|| pblock->nNonce >= 0xffff0000*/)
  833. break;
  834. // if (nTransactionsUpdated != nTransactionsUpdatedLast && GetTime() - nStart > 60)
  835. // break;
  836. // if (pindexPrev != pindexBest)
  837. // break;
  838. }
  839. mpz_clear(bnPrimorial);
  840. // Primecoin: estimate time to block
  841. pl->nTimeExpected = (GetTimeMicros() - nPrimeTimerStart) / max(1u, nRoundTests);
  842. pl->nTimeExpected = pl->nTimeExpected * max(1u, nRoundTests) / max(1u, nRoundPrimesHit);
  843. //TODO
  844. // for (unsigned int n = 1; n < TargetGetLength(pblock->nBits); n++)
  845. // nTimeExpected = nTimeExpected * max(1u, nRoundTests) * 3 / max(1u, nRoundPrimesHit);
  846. applog(LOG_DEBUG, "%"PRIpreprv": PrimecoinMiner() : Round primorial=%u tests=%u primes=%u expected=%us", proc_repr, vPrimes[pl->current_prime], nRoundTests, nRoundPrimesHit, (unsigned int)(pl->nTimeExpected/1000000));
  847. mpz_clear(hash);
  848. mpz_clear(bnPrimeMin);
  849. return rv;
  850. }
  851. #if 0
  852. void pmain()
  853. {
  854. setbuf(stderr, NULL);
  855. setbuf(stdout, NULL);
  856. GeneratePrimeTable();
  857. unsigned char array[80] = {
  858. 0x02,0x00,0x00,0x00,
  859. 0x59,0xf7,0x56,0x1c,0x21,0x25,0xc1,0xad,0x0d,0xee,0xbd,0x05,0xb8,0x41,0x38,0xab,
  860. 0x2e,0xfb,0x65,0x40,0xc8,0xc7,0xa3,0xef,0x90,0x3d,0x75,0x8c,0x03,0x1c,0x7a,0xcc,
  861. 0x8d,0x27,0x4d,0xeb,0x7b,0x6a,0xf8,0xe0,0x44,0x2d,0x7c,0xf6,0xb9,0x71,0x12,0xd8,
  862. 0x61,0x60,0x5b,0x1f,0xa5,0xa3,0xf7,0x4f,0x61,0xe3,0x59,0x67,0x03,0xc2,0xfb,0x56,
  863. 0xed,0x78,0xdb,0x51,
  864. 0xd5,0xbe,0x38,0x07,
  865. 0xe8,0x02,0x00,0x00,
  866. };
  867. prime(array);
  868. }
  869. #endif
  870. bool scanhash_prime(struct thr_info *thr, const unsigned char *pmidstate, unsigned char *pdata, unsigned char *phash1, unsigned char *phash, const unsigned char *ptarget, uint32_t max_nonce, uint32_t *last_nonce, uint32_t nonce)
  871. {
  872. struct work *work = (struct work *)(&pmidstate[-offsetof(struct work, midstate)]);
  873. if (work->blk.nonce == 0x10000)
  874. {
  875. // HACK: After we found a nonce
  876. next_header:
  877. *last_nonce = 0xfffffffe; // HACK: force next header
  878. return false;
  879. }
  880. unsigned char header[80];
  881. swap32yes(header, pdata, 80 / 4);
  882. #if 0
  883. memcpy(header,(unsigned char[80]){
  884. 0x02,0x00,0x00,0x00,
  885. 0x59,0xf7,0x56,0x1c,0x21,0x25,0xc1,0xad,0x0d,0xee,0xbd,0x05,0xb8,0x41,0x38,0xab,
  886. 0x2e,0xfb,0x65,0x40,0xc8,0xc7,0xa3,0xef,0x90,0x3d,0x75,0x8c,0x03,0x1c,0x7a,0xcc,
  887. 0x8d,0x27,0x4d,0xeb,0x7b,0x6a,0xf8,0xe0,0x44,0x2d,0x7c,0xf6,0xb9,0x71,0x12,0xd8,
  888. 0x61,0x60,0x5b,0x1f,0xa5,0xa3,0xf7,0x4f,0x61,0xe3,0x59,0x67,0x03,0xc2,0xfb,0x56,
  889. 0xed,0x78,0xdb,0x51,
  890. 0xd5,0xbe,0x38,0x07,
  891. 0xe8,0x02,0x00,0x00,
  892. },80);
  893. #endif
  894. #ifdef SUPERDEBUG
  895. char hex[161];
  896. bin2hex(hex, header, 80);
  897. applog(LOG_DEBUG, "HEADER: %s", hex);
  898. #endif
  899. if (!prime(thr, header, work))
  900. goto next_header;
  901. swap32yes(&pdata[76], &header[76], 1);
  902. *last_nonce = 0xffff; // Trigger HACK above
  903. return true;
  904. }