The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 lines
6.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. namespace PrimesHelpers
  16. {
  17. static void createSmallSieve (const int numBits, BigInteger& result)
  18. {
  19. result.setBit (numBits);
  20. result.clearBit (numBits); // to enlarge the array
  21. result.setBit (0);
  22. int n = 2;
  23. do
  24. {
  25. for (int i = n + n; i < numBits; i += n)
  26. result.setBit (i);
  27. n = result.findNextClearBit (n + 1);
  28. }
  29. while (n <= (numBits >> 1));
  30. }
  31. static void bigSieve (const BigInteger& base, const int numBits, BigInteger& result,
  32. const BigInteger& smallSieve, const int smallSieveSize)
  33. {
  34. jassert (! base[0]); // must be even!
  35. result.setBit (numBits);
  36. result.clearBit (numBits); // to enlarge the array
  37. int index = smallSieve.findNextClearBit (0);
  38. do
  39. {
  40. const unsigned int prime = ((unsigned int) index << 1) + 1;
  41. BigInteger r (base), remainder;
  42. r.divideBy (prime, remainder);
  43. unsigned int i = prime - remainder.getBitRangeAsInt (0, 32);
  44. if (r.isZero())
  45. i += prime;
  46. if ((i & 1) == 0)
  47. i += prime;
  48. i = (i - 1) >> 1;
  49. while (i < (unsigned int) numBits)
  50. {
  51. result.setBit ((int) i);
  52. i += prime;
  53. }
  54. index = smallSieve.findNextClearBit (index + 1);
  55. }
  56. while (index < smallSieveSize);
  57. }
  58. static bool findCandidate (const BigInteger& base, const BigInteger& sieve,
  59. const int numBits, BigInteger& result, const int certainty)
  60. {
  61. for (int i = 0; i < numBits; ++i)
  62. {
  63. if (! sieve[i])
  64. {
  65. result = base + (unsigned int) ((i << 1) + 1);
  66. if (Primes::isProbablyPrime (result, certainty))
  67. return true;
  68. }
  69. }
  70. return false;
  71. }
  72. static bool passesMillerRabin (const BigInteger& n, int iterations)
  73. {
  74. const BigInteger one (1), two (2);
  75. const BigInteger nMinusOne (n - one);
  76. BigInteger d (nMinusOne);
  77. const int s = d.findNextSetBit (0);
  78. d >>= s;
  79. BigInteger smallPrimes;
  80. int numBitsInSmallPrimes = 0;
  81. for (;;)
  82. {
  83. numBitsInSmallPrimes += 256;
  84. createSmallSieve (numBitsInSmallPrimes, smallPrimes);
  85. const int numPrimesFound = numBitsInSmallPrimes - smallPrimes.countNumberOfSetBits();
  86. if (numPrimesFound > iterations + 1)
  87. break;
  88. }
  89. int smallPrime = 2;
  90. while (--iterations >= 0)
  91. {
  92. smallPrime = smallPrimes.findNextClearBit (smallPrime + 1);
  93. BigInteger r (smallPrime);
  94. r.exponentModulo (d, n);
  95. if (r != one && r != nMinusOne)
  96. {
  97. for (int j = 0; j < s; ++j)
  98. {
  99. r.exponentModulo (two, n);
  100. if (r == nMinusOne)
  101. break;
  102. }
  103. if (r != nMinusOne)
  104. return false;
  105. }
  106. }
  107. return true;
  108. }
  109. }
  110. //==============================================================================
  111. BigInteger Primes::createProbablePrime (const int bitLength,
  112. const int certainty,
  113. const int* randomSeeds,
  114. int numRandomSeeds)
  115. {
  116. using namespace PrimesHelpers;
  117. int defaultSeeds [16];
  118. if (numRandomSeeds <= 0)
  119. {
  120. randomSeeds = defaultSeeds;
  121. numRandomSeeds = numElementsInArray (defaultSeeds);
  122. Random r1, r2;
  123. for (int j = 10; --j >= 0;)
  124. {
  125. r1.setSeedRandomly();
  126. for (int i = numRandomSeeds; --i >= 0;)
  127. defaultSeeds[i] ^= r1.nextInt() ^ r2.nextInt();
  128. }
  129. }
  130. BigInteger smallSieve;
  131. const int smallSieveSize = 15000;
  132. createSmallSieve (smallSieveSize, smallSieve);
  133. BigInteger p;
  134. for (int i = numRandomSeeds; --i >= 0;)
  135. {
  136. BigInteger p2;
  137. Random r (randomSeeds[i]);
  138. r.fillBitsRandomly (p2, 0, bitLength);
  139. p ^= p2;
  140. }
  141. p.setBit (bitLength - 1);
  142. p.clearBit (0);
  143. const int searchLen = jmax (1024, (bitLength / 20) * 64);
  144. while (p.getHighestBit() < bitLength)
  145. {
  146. p += 2 * searchLen;
  147. BigInteger sieve;
  148. bigSieve (p, searchLen, sieve,
  149. smallSieve, smallSieveSize);
  150. BigInteger candidate;
  151. if (findCandidate (p, sieve, searchLen, candidate, certainty))
  152. return candidate;
  153. }
  154. jassertfalse;
  155. return BigInteger();
  156. }
  157. bool Primes::isProbablyPrime (const BigInteger& number, const int certainty)
  158. {
  159. using namespace PrimesHelpers;
  160. if (! number[0])
  161. return false;
  162. if (number.getHighestBit() <= 10)
  163. {
  164. const unsigned int num = number.getBitRangeAsInt (0, 10);
  165. for (unsigned int i = num / 2; --i > 1;)
  166. if (num % i == 0)
  167. return false;
  168. return true;
  169. }
  170. else
  171. {
  172. if (number.findGreatestCommonDivisor (2 * 3 * 5 * 7 * 11 * 13 * 17 * 19 * 23) != 1)
  173. return false;
  174. return passesMillerRabin (number, certainty);
  175. }
  176. }
  177. } // namespace juce