monotone

monotone Mtn Source Tree

Root/cryptopp/nbtheory.h

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

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status