1// nbtheory.h - written and placed in the public domain by Wei Dai
2
3#ifndef CRYPTOPP_NBTHEORY_H
4#define CRYPTOPP_NBTHEORY_H
5
6#include "integer.h"
7#include "algparam.h"
8
9NAMESPACE_BEGIN(CryptoPP)
10
11// obtain pointer to small prime table and get its size
12CRYPTOPP_DLL const word16 * CRYPTOPP_API GetPrimeTable(unsigned int &size);
13
14// ************ primality testing ****************
15
16// generate a provable prime
17CRYPTOPP_DLL Integer CRYPTOPP_API MaurerProvablePrime(RandomNumberGenerator &rng, unsigned int bits);
18CRYPTOPP_DLL Integer CRYPTOPP_API MihailescuProvablePrime(RandomNumberGenerator &rng, unsigned int bits);
19
20CRYPTOPP_DLL bool CRYPTOPP_API IsSmallPrime(const Integer &p);
21
22// returns true if p is divisible by some prime less than bound
23// bound not be greater than the largest entry in the prime table
24CRYPTOPP_DLL bool CRYPTOPP_API TrialDivision(const Integer &p, unsigned bound);
25
26// returns true if p is NOT divisible by small primes
27CRYPTOPP_DLL bool CRYPTOPP_API SmallDivisorsTest(const Integer &p);
28
29// These is no reason to use these two, use the ones below instead
30CRYPTOPP_DLL bool CRYPTOPP_API IsFermatProbablePrime(const Integer &n, const Integer &b);
31CRYPTOPP_DLL bool CRYPTOPP_API IsLucasProbablePrime(const Integer &n);
32
33CRYPTOPP_DLL bool CRYPTOPP_API IsStrongProbablePrime(const Integer &n, const Integer &b);
34CRYPTOPP_DLL bool CRYPTOPP_API IsStrongLucasProbablePrime(const Integer &n);
35
36// Rabin-Miller primality test, i.e. repeating the strong probable prime test
37// for several rounds with random bases
38CRYPTOPP_DLL bool CRYPTOPP_API RabinMillerTest(RandomNumberGenerator &rng, const Integer &w, unsigned int rounds);
39
40// primality test, used to generate primes
41CRYPTOPP_DLL bool CRYPTOPP_API IsPrime(const Integer &p);
42
43// more reliable than IsPrime(), used to verify primes generated by others
44CRYPTOPP_DLL bool CRYPTOPP_API VerifyPrime(RandomNumberGenerator &rng, const Integer &p, unsigned int level = 1);
45
46class CRYPTOPP_DLL PrimeSelector
47{
48public:
49	const PrimeSelector *GetSelectorPointer() const {return this;}
50	virtual bool IsAcceptable(const Integer &candidate) const =0;
51};
52
53// use a fast sieve to find the first probable prime in {x | p<=x<=max and x%mod==equiv}
54// returns true iff successful, value of p is undefined if no such prime exists
55CRYPTOPP_DLL bool CRYPTOPP_API FirstPrime(Integer &p, const Integer &max, const Integer &equiv, const Integer &mod, const PrimeSelector *pSelector);
56
57CRYPTOPP_DLL unsigned int CRYPTOPP_API PrimeSearchInterval(const Integer &max);
58
59CRYPTOPP_DLL AlgorithmParameters CRYPTOPP_API MakeParametersForTwoPrimesOfEqualSize(unsigned int productBitLength);
60
61// ********** other number theoretic functions ************
62
63inline Integer GCD(const Integer &a, const Integer &b)
64	{return Integer::Gcd(a,b);}
65inline bool RelativelyPrime(const Integer &a, const Integer &b)
66	{return Integer::Gcd(a,b) == Integer::One();}
67inline Integer LCM(const Integer &a, const Integer &b)
68	{return a/Integer::Gcd(a,b)*b;}
69inline Integer EuclideanMultiplicativeInverse(const Integer &a, const Integer &b)
70	{return a.InverseMod(b);}
71
72// use Chinese Remainder Theorem to calculate x given x mod p and x mod q, and u = inverse of p mod q
73CRYPTOPP_DLL Integer CRYPTOPP_API CRT(const Integer &xp, const Integer &p, const Integer &xq, const Integer &q, const Integer &u);
74
75// if b is prime, then Jacobi(a, b) returns 0 if a%b==0, 1 if a is quadratic residue mod b, -1 otherwise
76// check a number theory book for what Jacobi symbol means when b is not prime
77CRYPTOPP_DLL int CRYPTOPP_API Jacobi(const Integer &a, const Integer &b);
78
79// calculates the Lucas function V_e(p, 1) mod n
80CRYPTOPP_DLL Integer CRYPTOPP_API Lucas(const Integer &e, const Integer &p, const Integer &n);
81// calculates x such that m==Lucas(e, x, p*q), p q primes, u=inverse of p mod q
82CRYPTOPP_DLL Integer CRYPTOPP_API InverseLucas(const Integer &e, const Integer &m, const Integer &p, const Integer &q, const Integer &u);
83
84inline Integer ModularExponentiation(const Integer &a, const Integer &e, const Integer &m)
85	{return a_exp_b_mod_c(a, e, m);}
86// returns x such that x*x%p == a, p prime
87CRYPTOPP_DLL Integer CRYPTOPP_API ModularSquareRoot(const Integer &a, const Integer &p);
88// returns x such that a==ModularExponentiation(x, e, p*q), p q primes,
89// and e relatively prime to (p-1)*(q-1)
90// dp=d%(p-1), dq=d%(q-1), (d is inverse of e mod (p-1)*(q-1))
91// and u=inverse of p mod q
92CRYPTOPP_DLL Integer CRYPTOPP_API ModularRoot(const Integer &a, const Integer &dp, const Integer &dq, const Integer &p, const Integer &q, const Integer &u);
93
94// find r1 and r2 such that ax^2 + bx + c == 0 (mod p) for x in {r1, r2}, p prime
95// returns true if solutions exist
96CRYPTOPP_DLL bool CRYPTOPP_API SolveModularQuadraticEquation(Integer &r1, Integer &r2, const Integer &a, const Integer &b, const Integer &c, const Integer &p);
97
98// returns log base 2 of estimated number of operations to calculate discrete log or factor a number
99CRYPTOPP_DLL unsigned int CRYPTOPP_API DiscreteLogWorkFactor(unsigned int bitlength);
100CRYPTOPP_DLL unsigned int CRYPTOPP_API FactoringWorkFactor(unsigned int bitlength);
101
102// ********************************************************
103
104//! generator of prime numbers of special forms
105class CRYPTOPP_DLL PrimeAndGenerator
106{
107public:
108	PrimeAndGenerator() {}
109	// generate a random prime p of the form 2*q+delta, where delta is 1 or -1 and q is also prime
110	// Precondition: pbits > 5
111	// warning: this is slow, because primes of this form are harder to find
112	PrimeAndGenerator(signed int delta, RandomNumberGenerator &rng, unsigned int pbits)
113		{Generate(delta, rng, pbits, pbits-1);}
114	// generate a random prime p of the form 2*r*q+delta, where q is also prime
115	// Precondition: qbits > 4 && pbits > qbits
116	PrimeAndGenerator(signed int delta, RandomNumberGenerator &rng, unsigned int pbits, unsigned qbits)
117		{Generate(delta, rng, pbits, qbits);}
118
119	void Generate(signed int delta, RandomNumberGenerator &rng, unsigned int pbits, unsigned qbits);
120
121	const Integer& Prime() const {return p;}
122	const Integer& SubPrime() const {return q;}
123	const Integer& Generator() const {return g;}
124
125private:
126	Integer p, q, g;
127};
128
129NAMESPACE_END
130
131#endif
132