Crypto++  5.6.5 Free C++ class library of cryptographic schemes
nbtheory.h
Go to the documentation of this file.
1 // nbtheory.h - written and placed in the public domain by Wei Dai
2
3 //! \file nbtheory.h
4 //! \brief Classes and functions for number theoretic operations
5
6 #ifndef CRYPTOPP_NBTHEORY_H
7 #define CRYPTOPP_NBTHEORY_H
8
9 #include "cryptlib.h"
10 #include "integer.h"
11 #include "algparam.h"
12
13 NAMESPACE_BEGIN(CryptoPP)
14
15 // obtain pointer to small prime table and get its size
16 CRYPTOPP_DLL const word16 * CRYPTOPP_API GetPrimeTable(unsigned int &size);
17
18 // ************ primality testing ****************
19
20 //! \brief Generates a provable prime
21 //! \param rng a RandomNumberGenerator to produce keying material
22 //! \param bits the number of bits in the prime number
23 //! \returns Integer() meeting Maurer's tests for primality
24 CRYPTOPP_DLL Integer CRYPTOPP_API MaurerProvablePrime(RandomNumberGenerator &rng, unsigned int bits);
25
26 //! \brief Generates a provable prime
27 //! \param rng a RandomNumberGenerator to produce keying material
28 //! \param bits the number of bits in the prime number
29 //! \returns Integer() meeting Mihailescu's tests for primality
30 //! \details Mihailescu's methods performs a search using algorithmic progressions.
31 CRYPTOPP_DLL Integer CRYPTOPP_API MihailescuProvablePrime(RandomNumberGenerator &rng, unsigned int bits);
32
33 //! \brief Tests whether a number is a small prime
34 //! \param p a candidate prime to test
35 //! \returns true if p is a small prime, false otherwise
36 //! \details Internally, the library maintains a table fo the first 32719 prime numbers
37 //! in sorted order. IsSmallPrime() searches the table and returns true if p is
38 //! in the table.
39 CRYPTOPP_DLL bool CRYPTOPP_API IsSmallPrime(const Integer &p);
40
41 //!
42 //! \returns true if p is divisible by some prime less than bound.
43 //! \details TrialDivision() true if p is divisible by some prime less than bound. bound not be
44 //! greater than the largest entry in the prime table, which is 32719.
45 CRYPTOPP_DLL bool CRYPTOPP_API TrialDivision(const Integer &p, unsigned bound);
46
47 // returns true if p is NOT divisible by small primes
48 CRYPTOPP_DLL bool CRYPTOPP_API SmallDivisorsTest(const Integer &p);
49
50 // These is no reason to use these two, use the ones below instead
51 CRYPTOPP_DLL bool CRYPTOPP_API IsFermatProbablePrime(const Integer &n, const Integer &b);
52 CRYPTOPP_DLL bool CRYPTOPP_API IsLucasProbablePrime(const Integer &n);
53
54 CRYPTOPP_DLL bool CRYPTOPP_API IsStrongProbablePrime(const Integer &n, const Integer &b);
55 CRYPTOPP_DLL bool CRYPTOPP_API IsStrongLucasProbablePrime(const Integer &n);
56
57 // Rabin-Miller primality test, i.e. repeating the strong probable prime test
58 // for several rounds with random bases
59 CRYPTOPP_DLL bool CRYPTOPP_API RabinMillerTest(RandomNumberGenerator &rng, const Integer &w, unsigned int rounds);
60
61 //! \brief Verifies a prime number
62 //! \param p a candidate prime to test
63 //! \returns true if p is a probable prime, false otherwise
64 //! \details IsPrime() is suitable for testing candidate primes when creating them. Internally,
65 //! IsPrime() utilizes SmallDivisorsTest(), IsStrongProbablePrime() and IsStrongLucasProbablePrime().
66 CRYPTOPP_DLL bool CRYPTOPP_API IsPrime(const Integer &p);
67
68 //! \brief Verifies a prime number
69 //! \param rng a RandomNumberGenerator for randomized testing
70 //! \param p a candidate prime to test
71 //! \param level the level of thoroughness of testing
72 //! \returns true if p is a strong probable prime, false otherwise
73 //! \details VerifyPrime() is suitable for testing candidate primes created by others. Internally,
74 //! VerifyPrime() utilizes IsPrime() and one-round RabinMillerTest(). If the candiate passes and
75 //! level is greater than 1, then 10 round RabinMillerTest() primality testing is performed.
76 CRYPTOPP_DLL bool CRYPTOPP_API VerifyPrime(RandomNumberGenerator &rng, const Integer &p, unsigned int level = 1);
77
78 //! \class PrimeSelector
79 //! \brief Application callback to signal suitability of a cabdidate prime
80 class CRYPTOPP_DLL PrimeSelector
81 {
82 public:
83  const PrimeSelector *GetSelectorPointer() const {return this;}
84  virtual bool IsAcceptable(const Integer &candidate) const =0;
85 };
86
87 //! \brief Finds a random prime of special form
88 //! \param p an Integer reference to receive the prime
89 //! \param max the maximum value
90 //! \param equiv the equivalence class based on the parameter mod
91 //! \param mod the modulus used to reduce the equivalence class
92 //! \param pSelector pointer to a PrimeSelector function for the application to signal suitability
93 //! \returns true if and only if FirstPrime() finds a prime and returns the prime through p. If FirstPrime()
94 //! returns false, then no such prime exists and the value of p is undefined
95 //! \details FirstPrime() uses a fast sieve to find the first probable prime
96 //! in <tt>{x | p<=x<=max and x%mod==equiv}</tt>
97 CRYPTOPP_DLL bool CRYPTOPP_API FirstPrime(Integer &p, const Integer &max, const Integer &equiv, const Integer &mod, const PrimeSelector *pSelector);
98
99 CRYPTOPP_DLL unsigned int CRYPTOPP_API PrimeSearchInterval(const Integer &max);
100
101 CRYPTOPP_DLL AlgorithmParameters CRYPTOPP_API MakeParametersForTwoPrimesOfEqualSize(unsigned int productBitLength);
102
103 // ********** other number theoretic functions ************
104
105 inline Integer GCD(const Integer &a, const Integer &b)
106  {return Integer::Gcd(a,b);}
107 inline bool RelativelyPrime(const Integer &a, const Integer &b)
108  {return Integer::Gcd(a,b) == Integer::One();}
109 inline Integer LCM(const Integer &a, const Integer &b)
110  {return a/Integer::Gcd(a,b)*b;}
111 inline Integer EuclideanMultiplicativeInverse(const Integer &a, const Integer &b)
112  {return a.InverseMod(b);}
113
114 // use Chinese Remainder Theorem to calculate x given x mod p and x mod q, and u = inverse of p mod q
115 CRYPTOPP_DLL Integer CRYPTOPP_API CRT(const Integer &xp, const Integer &p, const Integer &xq, const Integer &q, const Integer &u);
116
117 // if b is prime, then Jacobi(a, b) returns 0 if a%b==0, 1 if a is quadratic residue mod b, -1 otherwise
118 // check a number theory book for what Jacobi symbol means when b is not prime
119 CRYPTOPP_DLL int CRYPTOPP_API Jacobi(const Integer &a, const Integer &b);
120
121 // calculates the Lucas function V_e(p, 1) mod n
122 CRYPTOPP_DLL Integer CRYPTOPP_API Lucas(const Integer &e, const Integer &p, const Integer &n);
123 // calculates x such that m==Lucas(e, x, p*q), p q primes, u=inverse of p mod q
124 CRYPTOPP_DLL Integer CRYPTOPP_API InverseLucas(const Integer &e, const Integer &m, const Integer &p, const Integer &q, const Integer &u);
125
126 inline Integer ModularExponentiation(const Integer &a, const Integer &e, const Integer &m)
127  {return a_exp_b_mod_c(a, e, m);}
128 // returns x such that x*x%p == a, p prime
129 CRYPTOPP_DLL Integer CRYPTOPP_API ModularSquareRoot(const Integer &a, const Integer &p);
130 // returns x such that a==ModularExponentiation(x, e, p*q), p q primes,
131 // and e relatively prime to (p-1)*(q-1)
132 // dp=d%(p-1), dq=d%(q-1), (d is inverse of e mod (p-1)*(q-1))
133 // and u=inverse of p mod q
134 CRYPTOPP_DLL Integer CRYPTOPP_API ModularRoot(const Integer &a, const Integer &dp, const Integer &dq, const Integer &p, const Integer &q, const Integer &u);
135
136 // find r1 and r2 such that ax^2 + bx + c == 0 (mod p) for x in {r1, r2}, p prime
137 // returns true if solutions exist
138 CRYPTOPP_DLL bool CRYPTOPP_API SolveModularQuadraticEquation(Integer &r1, Integer &r2, const Integer &a, const Integer &b, const Integer &c, const Integer &p);
139
140 // returns log base 2 of estimated number of operations to calculate discrete log or factor a number
141 CRYPTOPP_DLL unsigned int CRYPTOPP_API DiscreteLogWorkFactor(unsigned int bitlength);
142 CRYPTOPP_DLL unsigned int CRYPTOPP_API FactoringWorkFactor(unsigned int bitlength);
143
144 // ********************************************************
145
146 //! generator of prime numbers of special forms
147 class CRYPTOPP_DLL PrimeAndGenerator
148 {
149 public:
150  PrimeAndGenerator() {}
151  // generate a random prime p of the form 2*q+delta, where delta is 1 or -1 and q is also prime
152  // Precondition: pbits > 5
153  // warning: this is slow, because primes of this form are harder to find
154  PrimeAndGenerator(signed int delta, RandomNumberGenerator &rng, unsigned int pbits)
155  {Generate(delta, rng, pbits, pbits-1);}
156  // generate a random prime p of the form 2*r*q+delta, where q is also prime
157  // Precondition: qbits > 4 && pbits > qbits
158  PrimeAndGenerator(signed int delta, RandomNumberGenerator &rng, unsigned int pbits, unsigned qbits)
159  {Generate(delta, rng, pbits, qbits);}
160
161  void Generate(signed int delta, RandomNumberGenerator &rng, unsigned int pbits, unsigned qbits);
162
163  const Integer& Prime() const {return p;}
164  const Integer& SubPrime() const {return q;}
165  const Integer& Generator() const {return g;}
166
167 private:
168  Integer p, q, g;
169 };
170
171 NAMESPACE_END
172
173 #endif
Classes for working with NameValuePairs.
bool FirstPrime(Integer &p, const Integer &max, const Integer &equiv, const Integer &mod, const PrimeSelector *pSelector)
Finds a random prime of special form.
Definition: nbtheory.cpp:381
static Integer Gcd(const Integer &a, const Integer &n)
greatest common divisor
Definition: integer.cpp:4234
Abstract base classes that provide a uniform interface to this library.
Integer MaurerProvablePrime(RandomNumberGenerator &rng, unsigned int bits)
Generates a provable prime.
Definition: nbtheory.cpp:512
bool IsSmallPrime(const Integer &p)
Tests whether a number is a small prime.
Definition: nbtheory.cpp:62
Interface for random number generators.
Definition: cryptlib.h:1201
generator of prime numbers of special forms
Definition: nbtheory.h:147
static const Integer & One()
Integer representing 1.
Definition: integer.cpp:3040
Integer MihailescuProvablePrime(RandomNumberGenerator &rng, unsigned int bits)
Generates a provable prime.
Definition: nbtheory.cpp:472
bool VerifyPrime(RandomNumberGenerator &rng, const Integer &p, unsigned int level=1)
Verifies a prime number.
Definition: nbtheory.cpp:249
Application callback to signal suitability of a cabdidate prime.
Definition: nbtheory.h:80
Multiple precision integer with arithmetic operations.
Definition: integer.h:45
bool IsPrime(const Integer &p)
Verifies a prime number.
Definition: nbtheory.cpp:239
bool TrialDivision(const Integer &p, unsigned bound)
Definition: nbtheory.cpp:73
An object that implements NameValuePairs.
Definition: algparam.h:498
Integer InverseMod(const Integer &n) const
calculate multiplicative inverse of *this mod n
Definition: integer.cpp:4239
Multiple precision integer with arithmetic operations.
Crypto++ library namespace.