prime.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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 vPrimorials[PRIMORIAL_COUNT];
  18. static
  19. int64_t GetTimeMicros()
  20. {
  21. struct timeval tv;
  22. cgtime(&tv);
  23. return (tv.tv_sec * 1000000) + tv.tv_usec;
  24. }
  25. static
  26. int64_t GetTimeMillis()
  27. {
  28. return GetTimeMicros() / 1000;
  29. }
  30. static
  31. bool error(const char *fmt, ...)
  32. {
  33. puts(fmt); // FIXME
  34. return false;
  35. }
  36. void GeneratePrimeTable()
  37. {
  38. mpz_t bnOne;
  39. mpz_init_set_ui(bnOne, 1);
  40. mpz_t *bnLastPrimorial = &bnOne;
  41. unsigned i = 0;
  42. // Generate prime table using sieve of Eratosthenes
  43. bool vfComposite[nPrimeTableLimit] = {false};
  44. for (unsigned int nFactor = 2; nFactor * nFactor < nPrimeTableLimit; nFactor++)
  45. {
  46. if (vfComposite[nFactor])
  47. continue;
  48. for (unsigned int nComposite = nFactor * nFactor; nComposite < nPrimeTableLimit; nComposite += nFactor)
  49. vfComposite[nComposite] = true;
  50. }
  51. for (unsigned int n = 2; n < nPrimeTableLimit; n++)
  52. if (!vfComposite[n])
  53. {
  54. vPrimes[i] = n;
  55. if (n < nPrimorialTableLimit)
  56. {
  57. mpz_init(vPrimorials[i]);
  58. mpz_mul_ui(vPrimorials[i], *bnLastPrimorial, n);
  59. bnLastPrimorial = &vPrimorials[i];
  60. }
  61. ++i;
  62. }
  63. mpz_clear(bnOne);
  64. applog(LOG_DEBUG, "GeneratePrimeTable() : prime table [1, %d] generated with %lu primes", nPrimeTableLimit, (unsigned long)i);
  65. }
  66. #define nFractionalBits 24
  67. #define TARGET_FRACTIONAL_MASK ((1u << nFractionalBits) - 1)
  68. #define TARGET_LENGTH_MASK (~TARGET_FRACTIONAL_MASK)
  69. // Check Fermat probable primality test (2-PRP): 2 ** (n-1) = 1 (mod n)
  70. // true: n is probable prime
  71. // false: n is composite; set fractional length in the nLength output
  72. static
  73. bool FermatProbablePrimalityTest(mpz_t *n, unsigned int *pnLength)
  74. {
  75. mpz_t a, e, r;
  76. mpz_init_set_ui(a, 2); // base; Fermat witness
  77. mpz_init(e);
  78. mpz_sub_ui(e, *n, 1);
  79. mpz_init(r);
  80. mpz_powm(r, a, e, *n);
  81. mpz_clear(a);
  82. mpz_clear(e);
  83. if (!mpz_cmp_ui(r, 1))
  84. {
  85. mpz_clear(r);
  86. return true;
  87. }
  88. // Failed Fermat test, calculate fractional length
  89. // nFractionalLength = ( (n-r) << nFractionalBits ) / n
  90. mpz_sub(r, *n, r);
  91. mpz_mul_2exp(r, r, nFractionalBits);
  92. mpz_fdiv_q(r, r, *n);
  93. unsigned int nFractionalLength = mpz_get_ui(r);
  94. mpz_clear(r);
  95. if (nFractionalLength >= (1 << nFractionalBits))
  96. return error("FermatProbablePrimalityTest() : fractional assert");
  97. *pnLength = (*pnLength & TARGET_LENGTH_MASK) | nFractionalLength;
  98. return false;
  99. }
  100. static
  101. unsigned int TargetGetLength(unsigned int nBits)
  102. {
  103. return ((nBits & TARGET_LENGTH_MASK) >> nFractionalBits);
  104. }
  105. static
  106. void TargetIncrementLength(unsigned int *pnBits)
  107. {
  108. *pnBits += (1 << nFractionalBits);
  109. }
  110. // Test probable primality of n = 2p +/- 1 based on Euler, Lagrange and Lifchitz
  111. // fSophieGermain:
  112. // true: n = 2p+1, p prime, aka Cunningham Chain of first kind
  113. // false: n = 2p-1, p prime, aka Cunningham Chain of second kind
  114. // Return values
  115. // true: n is probable prime
  116. // false: n is composite; set fractional length in the nLength output
  117. static
  118. bool EulerLagrangeLifchitzPrimalityTest(mpz_t *n, bool fSophieGermain, unsigned int *pnLength)
  119. {
  120. mpz_t a, e, r;
  121. mpz_init_set_ui(a, 2);
  122. mpz_init(e);
  123. mpz_sub_ui(e, *n, 1);
  124. mpz_fdiv_q_2exp(e, e, 1);
  125. mpz_init(r);
  126. mpz_powm(r, a, e, *n);
  127. mpz_clear(a);
  128. mpz_clear(e);
  129. unsigned nMod8 = mpz_fdiv_ui(*n, 8);
  130. bool fPassedTest = false;
  131. if (fSophieGermain && (nMod8 == 7)) // Euler & Lagrange
  132. fPassedTest = !mpz_cmp_ui(r, 1);
  133. else if (nMod8 == (fSophieGermain ? 3 : 5)) // Lifchitz
  134. {
  135. mpz_t mp;
  136. mpz_init_set_ui(mp, 1);
  137. mpz_add(mp, r, mp);
  138. fPassedTest = !mpz_cmp(mp, *n);
  139. mpz_clear(mp);
  140. }
  141. else if ((!fSophieGermain) && (nMod8 == 1)) // LifChitz
  142. fPassedTest = !mpz_cmp_ui(r, 1);
  143. else
  144. {
  145. mpz_clear(r);
  146. return error("EulerLagrangeLifchitzPrimalityTest() : invalid n %% 8 = %d, %s", nMod8, (fSophieGermain? "first kind" : "second kind"));
  147. }
  148. if (fPassedTest)
  149. {
  150. mpz_clear(r);
  151. return true;
  152. }
  153. // Failed test, calculate fractional length
  154. // derive Fermat test remainder
  155. mpz_mul(r, r, r);
  156. mpz_fdiv_r(r, r, *n);
  157. // nFractionalLength = ( (n-r) << nFractionalBits ) / n
  158. mpz_sub(r, *n, r);
  159. mpz_mul_2exp(r, r, nFractionalBits);
  160. mpz_fdiv_q(r, r, *n);
  161. unsigned int nFractionalLength = mpz_get_ui(r);
  162. mpz_clear(r);
  163. if (nFractionalLength >= (1 << nFractionalBits))
  164. return error("EulerLagrangeLifchitzPrimalityTest() : fractional assert");
  165. *pnLength = (*pnLength & TARGET_LENGTH_MASK) | nFractionalLength;
  166. return false;
  167. }
  168. // Test Probable Cunningham Chain for: n
  169. // fSophieGermain:
  170. // true - Test for Cunningham Chain of first kind (n, 2n+1, 4n+3, ...)
  171. // false - Test for Cunningham Chain of second kind (n, 2n-1, 4n-3, ...)
  172. // Return value:
  173. // true - Probable Cunningham Chain found (length at least 2)
  174. // false - Not Cunningham Chain
  175. static
  176. bool ProbableCunninghamChainTest(mpz_t *n, bool fSophieGermain, bool fFermatTest, unsigned int *pnProbableChainLength)
  177. {
  178. #ifdef SUPERDEBUG
  179. printf("ProbableCunninghamChainTest(");
  180. mpz_out_str(stdout, 0x10, *n);
  181. printf(", %d, %d, %u)\n", (int)fSophieGermain, (int)fFermatTest, *pnProbableChainLength);
  182. #endif
  183. *pnProbableChainLength = 0;
  184. mpz_t N;
  185. mpz_init_set(N, *n);
  186. // Fermat test for n first
  187. if (!FermatProbablePrimalityTest(&N, pnProbableChainLength))
  188. {
  189. mpz_clear(N);
  190. return false;
  191. }
  192. #ifdef SUPERDEBUG
  193. printf("N=");
  194. mpz_out_str(stdout, 0x10, N);
  195. printf("\n");
  196. #endif
  197. // Euler-Lagrange-Lifchitz test for the following numbers in chain
  198. while (true)
  199. {
  200. TargetIncrementLength(pnProbableChainLength);
  201. mpz_add(N, N, N);
  202. if (fSophieGermain)
  203. mpz_add_ui(N, N, 1);
  204. else
  205. mpz_sub_ui(N, N, 1);
  206. if (fFermatTest)
  207. {
  208. if (!FermatProbablePrimalityTest(&N, pnProbableChainLength))
  209. break;
  210. }
  211. else
  212. {
  213. #ifdef SUPERDEBUG
  214. if (!fSophieGermain)
  215. {
  216. printf("EulerLagrangeLifchitzPrimalityTest(");
  217. mpz_out_str(stdout, 0x10, N);
  218. printf(", 1, %d)\n", *pnProbableChainLength);
  219. }
  220. #endif
  221. if (!EulerLagrangeLifchitzPrimalityTest(&N, fSophieGermain, pnProbableChainLength))
  222. break;
  223. }
  224. }
  225. mpz_clear(N);
  226. #ifdef SUPERDEBUG
  227. printf("PCCT => %u (%u)\n", TargetGetLength(*pnProbableChainLength), *pnProbableChainLength);
  228. #endif
  229. return (TargetGetLength(*pnProbableChainLength) >= 2);
  230. }
  231. static
  232. unsigned int TargetFromInt(unsigned int nLength)
  233. {
  234. return (nLength << nFractionalBits);
  235. }
  236. // Test probable prime chain for: nOrigin
  237. // Return value:
  238. // true - Probable prime chain found (one of nChainLength meeting target)
  239. // false - prime chain too short (none of nChainLength meeting target)
  240. static
  241. bool ProbablePrimeChainTest(mpz_t *bnPrimeChainOrigin, unsigned int nBits, bool fFermatTest, unsigned int *pnChainLengthCunningham1, unsigned int *pnChainLengthCunningham2, unsigned int *pnChainLengthBiTwin)
  242. {
  243. *pnChainLengthCunningham1 = 0;
  244. *pnChainLengthCunningham2 = 0;
  245. *pnChainLengthBiTwin = 0;
  246. mpz_t mp;
  247. mpz_init(mp);
  248. // Test for Cunningham Chain of first kind
  249. mpz_sub_ui(mp, *bnPrimeChainOrigin, 1);
  250. ProbableCunninghamChainTest(&mp, true, fFermatTest, pnChainLengthCunningham1);
  251. // Test for Cunningham Chain of second kind
  252. mpz_add_ui(mp, *bnPrimeChainOrigin, 1);
  253. ProbableCunninghamChainTest(&mp, false, fFermatTest, pnChainLengthCunningham2);
  254. mpz_clear(mp);
  255. // Figure out BiTwin Chain length
  256. // BiTwin Chain allows a single prime at the end for odd length chain
  257. *pnChainLengthBiTwin = (TargetGetLength(*pnChainLengthCunningham1) > TargetGetLength(*pnChainLengthCunningham2)) ? (*pnChainLengthCunningham2 + TargetFromInt(TargetGetLength(*pnChainLengthCunningham2)+1)) : (*pnChainLengthCunningham1 + TargetFromInt(TargetGetLength(*pnChainLengthCunningham1)));
  258. return (*pnChainLengthCunningham1 >= nBits || *pnChainLengthCunningham2 >= nBits || *pnChainLengthBiTwin >= nBits);
  259. }
  260. struct SieveOfEratosthenes {
  261. bool valid;
  262. unsigned int nSieveSize; // size of the sieve
  263. unsigned int nBits; // target of the prime chain to search for
  264. mpz_t hashBlockHeader; // block header hash
  265. mpz_t bnFixedFactor; // fixed factor to derive the chain
  266. // bitmaps of the sieve, index represents the variable part of multiplier
  267. bool vfCompositeCunningham1[1000000];
  268. bool vfCompositeCunningham2[1000000];
  269. bool vfCompositeBiTwin[1000000];
  270. unsigned int nPrimeSeq; // prime sequence number currently being processed
  271. unsigned int nCandidateMultiplier; // current candidate for power test
  272. };
  273. static
  274. void psieve_reset(struct SieveOfEratosthenes *psieve)
  275. {
  276. mpz_clear(psieve->hashBlockHeader);
  277. mpz_clear(psieve->bnFixedFactor);
  278. psieve->valid = false;
  279. }
  280. static
  281. void psieve_init(struct SieveOfEratosthenes *psieve, unsigned nSieveSize, unsigned nBits, mpz_t *hashBlockHeader, mpz_t *bnFixedMultiplier)
  282. {
  283. assert(!psieve->valid);
  284. *psieve = (struct SieveOfEratosthenes){
  285. .valid = true,
  286. .nSieveSize = nSieveSize,
  287. .nBits = nBits,
  288. };
  289. mpz_init_set(psieve->hashBlockHeader, *hashBlockHeader);
  290. mpz_init(psieve->bnFixedFactor);
  291. mpz_mul(psieve->bnFixedFactor, *bnFixedMultiplier, *hashBlockHeader);
  292. }
  293. // Weave sieve for the next prime in table
  294. // Return values:
  295. // True - weaved another prime; nComposite - number of composites removed
  296. // False - sieve already completed
  297. static
  298. bool psieve_Weave(struct SieveOfEratosthenes *psieve)
  299. {
  300. unsigned nPrime = vPrimes[psieve->nPrimeSeq];
  301. if (psieve->nPrimeSeq >= PRIME_COUNT || nPrime >= psieve->nSieveSize)
  302. return false; // sieve has been completed
  303. if (mpz_fdiv_ui(psieve->bnFixedFactor, nPrime) == 0)
  304. {
  305. // Nothing in the sieve is divisible by this prime
  306. ++psieve->nPrimeSeq;
  307. return true;
  308. }
  309. // Find the modulo inverse of fixed factor
  310. mpz_t bnFixedInverse, p;
  311. mpz_init(bnFixedInverse);
  312. mpz_init_set_ui(p, nPrime);
  313. if (!mpz_invert(bnFixedInverse, psieve->bnFixedFactor, p))
  314. {
  315. mpz_clear(p);
  316. mpz_clear(bnFixedInverse);
  317. return error("CSieveOfEratosthenes::Weave(): BN_mod_inverse of fixed factor failed for prime #%u=%u", psieve->nPrimeSeq, nPrime);
  318. }
  319. mpz_t bnTwo, bnTwoInverse;
  320. mpz_init_set_ui(bnTwo, 2);
  321. mpz_init(bnTwoInverse);
  322. if (!mpz_invert(bnTwoInverse, bnTwo, p))
  323. {
  324. mpz_clear(bnTwoInverse);
  325. mpz_clear(bnTwo);
  326. mpz_clear(p);
  327. mpz_clear(bnFixedInverse);
  328. return error("CSieveOfEratosthenes::Weave(): BN_mod_inverse of 2 failed for prime #%u=%u", psieve->nPrimeSeq, nPrime);
  329. }
  330. mpz_clear(bnTwo);
  331. mpz_clear(p);
  332. mpz_t mp;
  333. mpz_init(mp);
  334. // Weave the sieve for the prime
  335. unsigned int nChainLength = TargetGetLength(psieve->nBits);
  336. for (unsigned int nBiTwinSeq = 0; nBiTwinSeq < 2 * nChainLength; nBiTwinSeq++)
  337. {
  338. // Find the first number that's divisible by this prime
  339. int nDelta = ((nBiTwinSeq % 2 == 0) ? (-1) : 1);
  340. mpz_mul_ui(mp, bnFixedInverse, nPrime - nDelta);
  341. unsigned int nSolvedMultiplier = mpz_fdiv_ui(mp, nPrime);
  342. if (nBiTwinSeq % 2 == 1)
  343. mpz_mul(bnFixedInverse, bnFixedInverse, bnTwoInverse); // for next number in chain
  344. if (nBiTwinSeq < nChainLength)
  345. for (unsigned int nVariableMultiplier = nSolvedMultiplier; nVariableMultiplier < psieve->nSieveSize; nVariableMultiplier += nPrime)
  346. psieve->vfCompositeBiTwin[nVariableMultiplier] = true;
  347. if (((nBiTwinSeq & 1u) == 0))
  348. for (unsigned int nVariableMultiplier = nSolvedMultiplier; nVariableMultiplier < psieve->nSieveSize; nVariableMultiplier += nPrime)
  349. psieve->vfCompositeCunningham1[nVariableMultiplier] = true;
  350. if (((nBiTwinSeq & 1u) == 1u))
  351. for (unsigned int nVariableMultiplier = nSolvedMultiplier; nVariableMultiplier < psieve->nSieveSize; nVariableMultiplier += nPrime)
  352. psieve->vfCompositeCunningham2[nVariableMultiplier] = true;
  353. }
  354. mpz_clear(mp);
  355. mpz_clear(bnTwoInverse);
  356. mpz_clear(bnFixedInverse);
  357. ++psieve->nPrimeSeq;
  358. return true;
  359. }
  360. static
  361. bool psieve_GetNextCandidateMultiplier(struct SieveOfEratosthenes *psieve, unsigned int *pnVariableMultiplier)
  362. {
  363. while (true)
  364. {
  365. psieve->nCandidateMultiplier++;
  366. if (psieve->nCandidateMultiplier >= psieve->nSieveSize)
  367. {
  368. psieve->nCandidateMultiplier = 0;
  369. return false;
  370. }
  371. if (!psieve->vfCompositeCunningham1[psieve->nCandidateMultiplier] ||
  372. !psieve->vfCompositeCunningham2[psieve->nCandidateMultiplier] ||
  373. !psieve->vfCompositeBiTwin[psieve->nCandidateMultiplier])
  374. {
  375. *pnVariableMultiplier = psieve->nCandidateMultiplier;
  376. return true;
  377. }
  378. }
  379. }
  380. // Get total number of candidates for power test
  381. static
  382. unsigned int psieve_GetCandidateCount(struct SieveOfEratosthenes *psieve)
  383. {
  384. unsigned int nCandidates = 0;
  385. for (unsigned int nMultiplier = 0; nMultiplier < psieve->nSieveSize; nMultiplier++)
  386. {
  387. if (!psieve->vfCompositeCunningham1[nMultiplier] || !psieve->vfCompositeCunningham2[nMultiplier] || !psieve->vfCompositeBiTwin[nMultiplier])
  388. nCandidates++;
  389. }
  390. return nCandidates;
  391. }
  392. // Mine probable prime chain of form: n = h * p# +/- 1
  393. bool MineProbablePrimeChain(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)
  394. {
  395. const uint32_t *pnbits = (void*)&header[72];
  396. *pnProbableChainLength = 0;
  397. *pnTests = 0;
  398. *pnPrimesHit = 0;
  399. if (*pfNewBlock && psieve->valid)
  400. {
  401. // Must rebuild the sieve
  402. psieve_reset(psieve);
  403. }
  404. *pfNewBlock = false;
  405. int64_t nStart, nCurrent; // microsecond timer
  406. if (!psieve->valid)
  407. {
  408. // Build sieve
  409. nStart = GetTimeMicros();
  410. #ifdef SUPERDEBUG
  411. fprintf(stderr, "psieve_init(?, %u, %08x, ", nMaxSieveSize, *pnbits);
  412. mpz_out_str(stderr, 0x10, *hash);
  413. fprintf(stderr, ", ");
  414. mpz_out_str(stderr, 0x10, *bnFixedMultiplier);
  415. fprintf(stderr, ")\n");
  416. #endif
  417. psieve_init(psieve, nMaxSieveSize, *pnbits, hash, bnFixedMultiplier);
  418. while (psieve_Weave(psieve));
  419. applog(LOG_DEBUG, "MineProbablePrimeChain() : new sieve (%u/%u) ready in %uus", psieve_GetCandidateCount(psieve), nMaxSieveSize, (unsigned int) (GetTimeMicros() - nStart));
  420. }
  421. mpz_t bnChainOrigin;
  422. mpz_init(bnChainOrigin);
  423. nStart = GetTimeMicros();
  424. nCurrent = nStart;
  425. while (nCurrent - nStart < 10000 && nCurrent >= nStart)
  426. {
  427. ++*pnTests;
  428. if (!psieve_GetNextCandidateMultiplier(psieve, pnTriedMultiplier))
  429. {
  430. // power tests completed for the sieve
  431. psieve_reset(psieve);
  432. *pfNewBlock = true; // notify caller to change nonce
  433. mpz_clear(bnChainOrigin);
  434. return false;
  435. }
  436. #ifdef SUPERDEBUG
  437. printf("nTriedMultiplier=%d\n", *pnTriedMultiplier=640150);
  438. #endif
  439. mpz_mul(bnChainOrigin, *hash, *bnFixedMultiplier);
  440. mpz_mul_ui(bnChainOrigin, bnChainOrigin, *pnTriedMultiplier);
  441. unsigned int nChainLengthCunningham1 = 0;
  442. unsigned int nChainLengthCunningham2 = 0;
  443. unsigned int nChainLengthBiTwin = 0;
  444. #ifdef SUPERDEBUG
  445. printf("ProbablePrimeChainTest(bnChainOrigin=");
  446. mpz_out_str(stdout, 0x10, bnChainOrigin);
  447. printf(", nbits=%08lx, false, %d, %d, %d)\n", (unsigned long)*pnbits, nChainLengthCunningham1, nChainLengthCunningham2, nChainLengthBiTwin);
  448. #endif
  449. if (ProbablePrimeChainTest(&bnChainOrigin, *pnbits, false, &nChainLengthCunningham1, &nChainLengthCunningham2, &nChainLengthBiTwin))
  450. {
  451. // bnChainOrigin is not used again, so recycled here for the result
  452. // block.bnPrimeChainMultiplier = *bnFixedMultiplier * *pnTriedMultiplier;
  453. mpz_mul_ui(bnChainOrigin, *bnFixedMultiplier, *pnTriedMultiplier);
  454. size_t exportsz, resultoff;
  455. uint8_t *export = mpz_export(NULL, &exportsz, -1, 1, -1, 0, bnChainOrigin);
  456. assert(exportsz < 250); // FIXME: bitcoin varint
  457. resultoff = 1;
  458. if (export[0] & 0x80)
  459. ++resultoff;
  460. uint8_t *result = malloc(exportsz + resultoff);
  461. result[0] = exportsz + resultoff - 1;
  462. result[1] = '\0';
  463. memcpy(&result[resultoff], export, exportsz);
  464. if (mpz_sgn(bnChainOrigin) < 0)
  465. result[1] |= 0x80;
  466. free(export);
  467. work->sig = result;
  468. work->sigsz = exportsz + resultoff;
  469. char hex[1 + (work->sigsz * 2)];
  470. bin2hex(hex, work->sig, work->sigsz);
  471. applog(LOG_DEBUG, "SIGNATURE: %s\n", hex);
  472. // printf("Probable prime chain found for block=%s!!\n Target: %s\n Length: (%s %s %s)\n", block.GetHash().GetHex().c_str(),
  473. // TargetToString(nbits).c_str(), TargetToString(nChainLengthCunningham1).c_str(), TargetToString(nChainLengthCunningham2).c_str(), TargetToString(nChainLengthBiTwin).c_str());
  474. applog(LOG_DEBUG, "Probable prime chain found for block");
  475. *pnProbableChainLength = nChainLengthCunningham1;
  476. if (*pnProbableChainLength < nChainLengthCunningham2)
  477. *pnProbableChainLength = nChainLengthCunningham2;
  478. if (*pnProbableChainLength < nChainLengthBiTwin)
  479. *pnProbableChainLength = nChainLengthBiTwin;
  480. mpz_clear(bnChainOrigin);
  481. return true;
  482. }
  483. *pnProbableChainLength = nChainLengthCunningham1;
  484. if (*pnProbableChainLength < nChainLengthCunningham2)
  485. *pnProbableChainLength = nChainLengthCunningham2;
  486. if (*pnProbableChainLength < nChainLengthBiTwin)
  487. *pnProbableChainLength = nChainLengthBiTwin;
  488. if(TargetGetLength(*pnProbableChainLength) >= 1)
  489. ++*pnPrimesHit;
  490. nCurrent = GetTimeMicros();
  491. }
  492. mpz_clear(bnChainOrigin);
  493. return false; // stop as timed out
  494. }
  495. // Checks that the high bit is set, and low bit is clear (ie, divisible by 2)
  496. static
  497. bool check_ends(const uint8_t *hash)
  498. {
  499. return (hash[31] & 0x80) && !(hash[0] & 1);
  500. }
  501. static inline
  502. void set_mpz_to_hash(mpz_t *hash, const uint8_t *hashb)
  503. {
  504. mpz_import(*hash, 8, -1, 4, -1, 0, hashb);
  505. }
  506. unsigned int nPrimorialHashFactor = 7;
  507. int64_t nTimeExpected = 0; // time expected to prime chain (micro-second)
  508. int64_t nTimeExpectedPrev = 0; // time expected to prime chain last time
  509. bool fIncrementPrimorial = true; // increase or decrease primorial factor
  510. unsigned current_prime = 3; // index 3 is prime number 7
  511. int64_t nHPSTimerStart = 0;
  512. bool prime(uint8_t *header, struct work *work)
  513. {
  514. bool rv = false;
  515. uint32_t *nonce = (void*)(&header[76]);
  516. unsigned char hashb[32];
  517. mpz_t hash, bnPrimeMin;
  518. mpz_init(hash);
  519. mpz_init_set_ui(bnPrimeMin, 1);
  520. mpz_mul_2exp(bnPrimeMin, bnPrimeMin, 255);
  521. bool fNewBlock = true;
  522. unsigned int nTriedMultiplier = 0;
  523. struct SieveOfEratosthenes sieve = {
  524. .valid = false,
  525. };
  526. const unsigned nHashFactor = 210;
  527. // a valid header must hash to have the MSB set, and a multiple of nHashFactor
  528. while (true)
  529. {
  530. gen_hash(header, hashb, 80);
  531. if (check_ends(hashb))
  532. {
  533. set_mpz_to_hash(&hash, hashb);
  534. if (!mpz_fdiv_ui(hash, 105))
  535. break;
  536. }
  537. if (unlikely(*nonce == 0xffffffff))
  538. {
  539. mpz_clear(hash);
  540. mpz_clear(bnPrimeMin);
  541. return false;
  542. }
  543. ++*nonce;
  544. }
  545. {
  546. char hex[9];
  547. bin2hex(hex, nonce, 4);
  548. applog(LOG_DEBUG, "Pass 1 found: %s", hex);
  549. }
  550. // primorial fixed multiplier
  551. mpz_t bnPrimorial;
  552. mpz_init(bnPrimorial);
  553. unsigned int nRoundTests = 0;
  554. unsigned int nRoundPrimesHit = 0;
  555. int64_t nPrimeTimerStart = GetTimeMicros();
  556. if (nTimeExpected > nTimeExpectedPrev)
  557. fIncrementPrimorial = !fIncrementPrimorial;
  558. nTimeExpectedPrev = nTimeExpected;
  559. // dynamic adjustment of primorial multiplier
  560. if (fIncrementPrimorial)
  561. {
  562. ++current_prime;
  563. if (current_prime >= PRIMORIAL_COUNT)
  564. quit(1, "primorial increment overflow");
  565. }
  566. else if (vPrimes[current_prime] > nPrimorialHashFactor)
  567. {
  568. if (!current_prime)
  569. quit(1, "primorial decrement overflow");
  570. --current_prime;
  571. }
  572. mpz_set(bnPrimorial, vPrimorials[current_prime]);
  573. while (true)
  574. {
  575. unsigned int nTests = 0;
  576. unsigned int nPrimesHit = 0;
  577. mpz_t bnMultiplierMin;
  578. // bnMultiplierMin = bnPrimeMin * nHashFactor / hash + 1
  579. mpz_init(bnMultiplierMin);
  580. mpz_mul_ui(bnMultiplierMin, bnPrimeMin, nHashFactor);
  581. mpz_fdiv_q(bnMultiplierMin, bnMultiplierMin, hash);
  582. mpz_add_ui(bnMultiplierMin, bnMultiplierMin, 1);
  583. while (mpz_cmp(bnPrimorial, bnMultiplierMin) < 0)
  584. {
  585. ++current_prime;
  586. if (current_prime >= PRIMORIAL_COUNT)
  587. quit(1, "primorial minimum overflow");
  588. mpz_set(bnPrimorial, vPrimorials[current_prime]);
  589. }
  590. mpz_clear(bnMultiplierMin);
  591. mpz_t bnFixedMultiplier;
  592. mpz_init(bnFixedMultiplier);
  593. // bnFixedMultiplier = (bnPrimorial > nHashFactor) ? (bnPrimorial / nHashFactor) : 1
  594. if (mpz_cmp_ui(bnPrimorial, nHashFactor) > 0)
  595. {
  596. mpz_t bnHashFactor;
  597. mpz_init_set_ui(bnHashFactor, nHashFactor);
  598. mpz_fdiv_q(bnFixedMultiplier, bnPrimorial, bnHashFactor);
  599. mpz_clear(bnHashFactor);
  600. }
  601. else
  602. mpz_set_ui(bnFixedMultiplier, 1);
  603. #ifdef SUPERDEBUG
  604. fprintf(stderr,"bnFixedMultiplier=");
  605. mpz_out_str(stderr, 0x10, bnFixedMultiplier);
  606. fprintf(stderr, " nPrimorialMultiplier=%u nTriedMultiplier=%u\n", vPrimes[current_prime], nTriedMultiplier);
  607. #endif
  608. // mine for prime chain
  609. unsigned int nProbableChainLength;
  610. if (MineProbablePrimeChain(&sieve, header, &hash, &bnFixedMultiplier, &fNewBlock, &nTriedMultiplier, &nProbableChainLength, &nTests, &nPrimesHit, work))
  611. {
  612. // TODO CheckWork(pblock, *pwalletMain, reservekey);
  613. mpz_clear(bnFixedMultiplier);
  614. rv = true;
  615. break;
  616. }
  617. mpz_clear(bnFixedMultiplier);
  618. nRoundTests += nTests;
  619. nRoundPrimesHit += nPrimesHit;
  620. // Meter primes/sec
  621. static int64_t nPrimeCounter;
  622. static int64_t nTestCounter;
  623. if (nHPSTimerStart == 0)
  624. {
  625. nHPSTimerStart = GetTimeMillis();
  626. nPrimeCounter = 0;
  627. nTestCounter = 0;
  628. }
  629. else
  630. {
  631. nPrimeCounter += nPrimesHit;
  632. nTestCounter += nTests;
  633. }
  634. #if 0
  635. if (GetTimeMillis() - nHPSTimerStart > 60000)
  636. {
  637. static CCriticalSection cs;
  638. {
  639. LOCK(cs);
  640. if (GetTimeMillis() - nHPSTimerStart > 60000)
  641. {
  642. double dPrimesPerMinute = 60000.0 * nPrimeCounter / (GetTimeMillis() - nHPSTimerStart);
  643. dPrimesPerSec = dPrimesPerMinute / 60.0;
  644. double dTestsPerMinute = 60000.0 * nTestCounter / (GetTimeMillis() - nHPSTimerStart);
  645. nHPSTimerStart = GetTimeMillis();
  646. nPrimeCounter = 0;
  647. nTestCounter = 0;
  648. static int64 nLogTime = 0;
  649. if (GetTime() - nLogTime > 60)
  650. {
  651. nLogTime = GetTime();
  652. printf("%s primemeter %9.0f prime/h %9.0f test/h\n", DateTimeStrFormat("%Y-%m-%d %H:%M:%S", nLogTime).c_str(), dPrimesPerMinute * 60.0, dTestsPerMinute * 60.0);
  653. }
  654. }
  655. }
  656. }
  657. #endif
  658. // Check for stop or if block needs to be rebuilt
  659. // TODO
  660. // boost::this_thread::interruption_point();
  661. // if (vNodes.empty())
  662. // break;
  663. if (fNewBlock /*|| pblock->nNonce >= 0xffff0000*/)
  664. break;
  665. // if (nTransactionsUpdated != nTransactionsUpdatedLast && GetTime() - nStart > 60)
  666. // break;
  667. // if (pindexPrev != pindexBest)
  668. // break;
  669. }
  670. mpz_clear(bnPrimorial);
  671. // Primecoin: estimate time to block
  672. nTimeExpected = (GetTimeMicros() - nPrimeTimerStart) / max(1u, nRoundTests);
  673. nTimeExpected = nTimeExpected * max(1u, nRoundTests) / max(1u, nRoundPrimesHit);
  674. //TODO
  675. // for (unsigned int n = 1; n < TargetGetLength(pblock->nBits); n++)
  676. // nTimeExpected = nTimeExpected * max(1u, nRoundTests) * 3 / max(1u, nRoundPrimesHit);
  677. applog(LOG_DEBUG, "PrimecoinMiner() : Round primorial=%u tests=%u primes=%u expected=%us", vPrimes[current_prime], nRoundTests, nRoundPrimesHit, (unsigned int)(nTimeExpected/1000000));
  678. mpz_clear(hash);
  679. mpz_clear(bnPrimeMin);
  680. return rv;
  681. }
  682. #if 0
  683. void pmain()
  684. {
  685. setbuf(stderr, NULL);
  686. setbuf(stdout, NULL);
  687. GeneratePrimeTable();
  688. unsigned char array[80] = {
  689. 0x02,0x00,0x00,0x00,
  690. 0x59,0xf7,0x56,0x1c,0x21,0x25,0xc1,0xad,0x0d,0xee,0xbd,0x05,0xb8,0x41,0x38,0xab,
  691. 0x2e,0xfb,0x65,0x40,0xc8,0xc7,0xa3,0xef,0x90,0x3d,0x75,0x8c,0x03,0x1c,0x7a,0xcc,
  692. 0x8d,0x27,0x4d,0xeb,0x7b,0x6a,0xf8,0xe0,0x44,0x2d,0x7c,0xf6,0xb9,0x71,0x12,0xd8,
  693. 0x61,0x60,0x5b,0x1f,0xa5,0xa3,0xf7,0x4f,0x61,0xe3,0x59,0x67,0x03,0xc2,0xfb,0x56,
  694. 0xed,0x78,0xdb,0x51,
  695. 0xd5,0xbe,0x38,0x07,
  696. 0xe8,0x02,0x00,0x00,
  697. };
  698. prime(array);
  699. }
  700. #endif
  701. 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)
  702. {
  703. struct work *work = (struct work *)(&pmidstate[-offsetof(struct work, midstate)]);
  704. unsigned char header[80];
  705. swap32yes(header, pdata, 80 / 4);
  706. #if 0
  707. memcpy(header,(unsigned char[80]){
  708. 0x02,0x00,0x00,0x00,
  709. 0x59,0xf7,0x56,0x1c,0x21,0x25,0xc1,0xad,0x0d,0xee,0xbd,0x05,0xb8,0x41,0x38,0xab,
  710. 0x2e,0xfb,0x65,0x40,0xc8,0xc7,0xa3,0xef,0x90,0x3d,0x75,0x8c,0x03,0x1c,0x7a,0xcc,
  711. 0x8d,0x27,0x4d,0xeb,0x7b,0x6a,0xf8,0xe0,0x44,0x2d,0x7c,0xf6,0xb9,0x71,0x12,0xd8,
  712. 0x61,0x60,0x5b,0x1f,0xa5,0xa3,0xf7,0x4f,0x61,0xe3,0x59,0x67,0x03,0xc2,0xfb,0x56,
  713. 0xed,0x78,0xdb,0x51,
  714. 0xd5,0xbe,0x38,0x07,
  715. 0xe8,0x02,0x00,0x00,
  716. },80);
  717. #endif
  718. bool rv = prime(header, work);
  719. swap32yes(pdata, header, 80 / 4);
  720. return rv;
  721. }