5 #ifndef CRYPTOPP_IMPORTS 29 const unsigned int maxPrimeTableSize = 3511;
32 std::vector<word16> &primeTable = *pPrimeTable;
33 primeTable.reserve(maxPrimeTableSize);
35 primeTable.push_back(2);
36 unsigned int testEntriesEnd = 1;
41 for (j=1; j<testEntriesEnd; j++)
42 if (p%primeTable[j] == 0)
44 if (j == testEntriesEnd)
46 primeTable.push_back(
word16(p));
47 testEntriesEnd =
UnsignedMin(54U, primeTable.size());
58 size = (
unsigned int)primeTable.size();
59 return &primeTable[0];
64 unsigned int primeTableSize;
67 if (p.
IsPositive() && p <= primeTable[primeTableSize-1])
68 return std::binary_search(primeTable, primeTable+primeTableSize, (
word16)p.
ConvertToLong());
75 unsigned int primeTableSize;
81 for (i = 0; primeTable[i]<bound; i++)
82 if ((p % primeTable[i]) == 0)
85 if (bound == primeTable[i])
86 return (p % bound == 0);
93 unsigned int primeTableSize;
114 if ((n.
IsEven() && n!=2) ||
GCD(b, n) != 1)
127 if (z==1 || z==nminus1)
129 for (
unsigned j=1; j<
a; j++)
148 for (
unsigned int i=0; i<rounds; i++)
181 return Lucas(n+1, b, n)==2;
262 static inline bool FastProbablePrimeTest(
const Integer &n)
269 if (productBitLength < 16)
274 if (productBitLength%2==0)
276 minP =
Integer(182) << (productBitLength/2-8);
282 maxP =
Integer(181) << ((productBitLength+1)/2-8);
305 : m_first(first), m_last(last), m_step(step), m_delta(delta), m_next(0)
338 size_t sieveSize = sieve.size();
339 size_t j = (
word32(p-(first%p))*stepInv) % p;
341 if (first.
WordCount() <= 1 && first + step*long(j) == p)
343 for (; j < sieveSize; j += p)
350 unsigned int primeTableSize;
353 const unsigned int maxSieveSize = 32768;
357 m_sieve.resize(sieveSize,
false);
361 for (
unsigned int i = 0; i < primeTableSize; ++i)
369 for (
unsigned int i = 0; i < primeTableSize; ++i)
375 word16 halfStepInv = 2*stepInv < p ? 2*stepInv : 2*stepInv-p;
389 if (p <= gcd && gcd <= max &&
IsPrime(gcd) && (!pSelector || pSelector->
IsAcceptable(gcd)))
398 unsigned int primeTableSize;
401 if (p <= primeTable[primeTableSize-1])
407 pItr = std::upper_bound(primeTable, primeTable+primeTableSize, (
word)p.
ConvertToLong());
411 while (pItr < primeTable+primeTableSize && !(*pItr%mod == equiv && (!pSelector || pSelector->
IsAcceptable(*pItr))))
414 if (pItr < primeTable+primeTableSize)
420 p = primeTable[primeTableSize-1]+1;
426 return FirstPrime(p, max,
CRT(equiv, mod, 1, 2, 1), mod<<1, pSelector);
456 if (((r%q).Squared()-4*(r/q)).IsSquare())
459 unsigned int primeTableSize;
463 for (
int i=0; i<50; i++)
485 unsigned int qbits = (pbits+2)/3 + 1 + rng.
GenerateWord32(0, pbits/36);
503 if (FastProbablePrimeTest(p) && ProvePrime(p, q))
514 const unsigned smallPrimeBound = 29, c_opt=10;
517 unsigned int primeTableSize;
520 if (bits < smallPrimeBound)
528 const unsigned margin = bits > 50 ? 20 : (bits-10)/2;
531 relativeSize = pow(2.0,
double(rng.
GenerateWord32())/0xffffffff - 1);
532 while (bits * relativeSize >= bits - margin);
538 unsigned int trialDivisorBound = (
unsigned int)
STDMIN((
unsigned long)primeTable[primeTableSize-1], (
unsigned long)bits*bits/c_opt);
539 bool success =
false;
543 p *= q; p <<= 1; ++p;
558 return p * (u * (xq-xp) % q) + xp;
588 while (
Jacobi(n, p) != -1)
611 for (
unsigned i=0; i<r-m-1; i++)
634 r1 = r2 = (-b*(a+
a).InverseMod(p)) % p;
660 return CRT(p2, p, q2, q, u);
797 while (a.GetBit(i)==0)
801 if (i%2==1 && (b%8==3 || b%8==5))
804 if (a%4==3 && b%4==3)
811 return (b==1) ? result : 0;
1004 #pragma omp parallel 1005 #pragma omp sections 1018 return CRT(p2, p, q2, q, u);
1026 else return (
unsigned int)(2.4 * pow((
double)n, 1.0/3.0) * pow(log(
double(n)), 2.0/3.0) - 5);
1033 else return (
unsigned int)(2.4 * pow((
double)n, 1.0/3.0) * pow(log(
double(n)), 2.0/3.0) - 5);
1044 if (qbits+1 == pbits)
1048 bool success =
false;
1060 if (FastProbablePrimeTest(q) && FastProbablePrimeTest(p) &&
IsPrime(q) &&
IsPrime(p))
1116 g =
Lucas((p+1)/q, h, p);
Integer ModularRoot(const Integer &a, const Integer &dp, const Integer &dq, const Integer &p, const Integer &q, const Integer &u)
int Jacobi(const Integer &aIn, const Integer &bIn)
An invalid argument was detected.
bool NextCandidate(Integer &c)
unsigned int PrimeSearchInterval(const Integer &max)
Classes for working with NameValuePairs.
const Integer & Square(const Integer &a) const
Square an element in the ring.
bool SafeConvert(T1 from, T2 &to)
Tests whether a conversion from -> to is safe to perform.
AlgorithmParameters MakeParametersForTwoPrimesOfEqualSize(unsigned int productBitLength)
a number which is probabilistically prime
Utility functions for the Crypto++ library.
bool IsLucasProbablePrime(const Integer &n)
Restricts the instantiation of a class to one static object without locks.
bool GetBit(size_t i) const
Provides the i-th bit of the Integer.
bool IsFermatProbablePrime(const Integer &n, const Integer &b)
void swap(dev::eth::Watch &_a, dev::eth::Watch &_b)
bool IsOdd() const
Determines if the Integer is odd parity.
const Integer & Subtract(const Integer &a, const Integer &b) const
Subtracts elements in the ring.
#define NAMESPACE_BEGIN(x)
bool IsSmallPrime(const Integer &p)
Tests whether a number is a small prime.
virtual word32 GenerateWord32(word32 min=0, word32 max=0xffffffffUL)
Generate a random 32 bit word in the range min to max, inclusive.
bool IsEven() const
Determines if the Integer is even parity.
unsigned int WordCount() const
Determines the number of words required to represent the Integer.
Classes for automatic resource management.
Interface for random number generators.
void Randomize(RandomNumberGenerator &rng, size_t bitCount)
Set this Integer to random integer.
Integer InverseLucas(const Integer &e, const Integer &m, const Integer &p, const Integer &q, const Integer &u)
Integer CRT(const Integer &xp, const Integer &p, const Integer &xq, const Integer &q, const Integer &u)
std::vector< word16 > * operator()() const
unsigned int DiscreteLogWorkFactor(unsigned int n)
PrimeSieve(const Integer &first, const Integer &last, const Integer &step, signed int delta=0)
static const Integer &CRYPTOPP_API One()
Integer representing 1.
bool VerifyPrime(RandomNumberGenerator &rng, const Integer &p, unsigned int level)
Verifies a prime number.
Integer * operator()() const
Integer ModularExponentiation(const Integer &a, const Integer &e, const Integer &m)
Pointer that overloads operator ->
bool IsSquare() const
Determine whether this integer is a perfect square.
unsigned int BitCount() const
Determines the number of bits required to represent the Integer.
Integer GCD(const Integer &a, const Integer &b)
const word s_lastSmallPrime
bool IsPositive() const
Determines if the Integer is positive.
a number with no special properties
bool SolveModularQuadraticEquation(Integer &r1, Integer &r2, const Integer &a, const Integer &b, const Integer &c, const Integer &p)
Integer Squared() const
Multiply this integer by itself.
bool IsNegative() const
Determines if the Integer is negative.
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
signed long ConvertToLong() const
Convert the Integer to Long.
Application callback to signal suitability of a cabdidate prime.
static Integer CRYPTOPP_API Power2(size_t e)
Exponentiates to a power of 2.
bool IsStrongLucasProbablePrime(const Integer &n)
Multiple precision integer with arithmetic operations.
const T1 UnsignedMin(const T1 &a, const T2 &b)
Safe comparison of values that could be neagtive and incorrectly promoted.
Integer Lucas(const Integer &e, const Integer &pIn, const Integer &n)
bool RabinMillerTest(RandomNumberGenerator &rng, const Integer &n, unsigned int rounds)
static const Integer &CRYPTOPP_API Two()
Integer representing 2.
Integer ConvertOut(const Integer &a) const
Reduces an element in the congruence class.
unsigned int FactoringWorkFactor(unsigned int n)
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
#define CRYPTOPP_ASSERT(exp)
#define I2(i, r0, r1, r2, r3, r4)
Classes and functions for number theoretic operations.
std::vector< bool > m_sieve
Integer ModularSquareRoot(const Integer &a, const Integer &p)
Integer EuclideanMultiplicativeInverse(const Integer &a, const Integer &b)
#define pass(a, b, c, mul, X)
Performs modular arithmetic in Montgomery representation for increased speed.
uint8_t const size_t const size
bool FirstPrime(Integer &p, const Integer &max, const Integer &equiv, const Integer &mod, const PrimeSelector *pSelector)
Finds a random prime of special form.
virtual bool IsAcceptable(const Integer &candidate) const =0
#define CRYPTOPP_UNUSED(x)
Integer MihailescuProvablePrime(RandomNumberGenerator &rng, unsigned int pbits)
Generates a provable prime.
const Integer & Multiply(const Integer &a, const Integer &b) const
Multiplies elements in the ring.
An object that implements NameValuePairs.
void Generate(signed int delta, RandomNumberGenerator &rng, unsigned int pbits, unsigned qbits)
Generate a Prime and Generator.
Integer InverseMod(const Integer &n) const
calculate multiplicative inverse of *this mod n
Multiple precision integer with arithmetic operations.
static const Integer &CRYPTOPP_API Zero()
Integer representing 0.
bool SmallDivisorsTest(const Integer &p)
Class file for performing modular arithmetic.
Integer a_exp_b_mod_c(const Integer &x, const Integer &e, const Integer &m)
bool IsPrime(const Integer &p)
Verifies a prime number.
bool IsStrongProbablePrime(const Integer &n, const Integer &b)
const word16 * GetPrimeTable(unsigned int &size)
bool TrialDivision(const Integer &p, unsigned bound)
Integer MaurerProvablePrime(RandomNumberGenerator &rng, unsigned int bits)
Generates a provable prime.
Integer ConvertIn(const Integer &a) const
Reduces an element in the congruence class.
static void SieveSingle(std::vector< bool > &sieve, word16 p, const Integer &first, const Integer &step, word16 stepInv)