source: git/src-cryptopp/nbtheory.h

Last change on this file was e230cb0, checked in by David Stainton <dstainton415@…>, at 2016-10-12T13:27:29Z

Add cryptopp from tag CRYPTOPP_5_6_5

  • Property mode set to 100644
File size: 8.6 KB
Line 
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
13NAMESPACE_BEGIN(CryptoPP)
14
15// obtain pointer to small prime table and get its size
16CRYPTOPP_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
24CRYPTOPP_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.
31CRYPTOPP_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.
39CRYPTOPP_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.
45CRYPTOPP_DLL bool CRYPTOPP_API TrialDivision(const Integer &p, unsigned bound);
46
47// returns true if p is NOT divisible by small primes
48CRYPTOPP_DLL bool CRYPTOPP_API SmallDivisorsTest(const Integer &p);
49
50// These is no reason to use these two, use the ones below instead
51CRYPTOPP_DLL bool CRYPTOPP_API IsFermatProbablePrime(const Integer &n, const Integer &b);
52CRYPTOPP_DLL bool CRYPTOPP_API IsLucasProbablePrime(const Integer &n);
53
54CRYPTOPP_DLL bool CRYPTOPP_API IsStrongProbablePrime(const Integer &n, const Integer &b);
55CRYPTOPP_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
59CRYPTOPP_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().
66CRYPTOPP_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.
76CRYPTOPP_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
80class CRYPTOPP_DLL PrimeSelector
81{
82public:
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>
97CRYPTOPP_DLL bool CRYPTOPP_API FirstPrime(Integer &p, const Integer &max, const Integer &equiv, const Integer &mod, const PrimeSelector *pSelector);
98
99CRYPTOPP_DLL unsigned int CRYPTOPP_API PrimeSearchInterval(const Integer &max);
100
101CRYPTOPP_DLL AlgorithmParameters CRYPTOPP_API MakeParametersForTwoPrimesOfEqualSize(unsigned int productBitLength);
102
103// ********** other number theoretic functions ************
104
105inline Integer GCD(const Integer &a, const Integer &b)
106        {return Integer::Gcd(a,b);}
107inline bool RelativelyPrime(const Integer &a, const Integer &b)
108        {return Integer::Gcd(a,b) == Integer::One();}
109inline Integer LCM(const Integer &a, const Integer &b)
110        {return a/Integer::Gcd(a,b)*b;}
111inline 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
115CRYPTOPP_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
119CRYPTOPP_DLL int CRYPTOPP_API Jacobi(const Integer &a, const Integer &b);
120
121// calculates the Lucas function V_e(p, 1) mod n
122CRYPTOPP_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
124CRYPTOPP_DLL Integer CRYPTOPP_API InverseLucas(const Integer &e, const Integer &m, const Integer &p, const Integer &q, const Integer &u);
125
126inline 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
129CRYPTOPP_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
134CRYPTOPP_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
138CRYPTOPP_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
141CRYPTOPP_DLL unsigned int CRYPTOPP_API DiscreteLogWorkFactor(unsigned int bitlength);
142CRYPTOPP_DLL unsigned int CRYPTOPP_API FactoringWorkFactor(unsigned int bitlength);
143
144// ********************************************************
145
146//! generator of prime numbers of special forms
147class CRYPTOPP_DLL PrimeAndGenerator
148{
149public:
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
167private:
168        Integer p, q, g;
169};
170
171NAMESPACE_END
172
173#endif
Note: See TracBrowser for help on using the repository browser.