Botan  2.7.0
Crypto and TLS for C++11
numthry.h
Go to the documentation of this file.
1 /*
2 * Number Theory Functions
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #ifndef BOTAN_NUMBER_THEORY_H_
9 #define BOTAN_NUMBER_THEORY_H_
10 
11 #include <botan/bigint.h>
12 
13 namespace Botan {
14 
15 class RandomNumberGenerator;
16 
17 /**
18 * Fused multiply-add
19 * @param a an integer
20 * @param b an integer
21 * @param c an integer
22 * @return (a*b)+c
23 */
24 BigInt BOTAN_PUBLIC_API(2,0) mul_add(const BigInt& a,
25  const BigInt& b,
26  const BigInt& c);
27 
28 /**
29 * Fused subtract-multiply
30 * @param a an integer
31 * @param b an integer
32 * @param c an integer
33 * @return (a-b)*c
34 */
35 BigInt BOTAN_PUBLIC_API(2,0) sub_mul(const BigInt& a,
36  const BigInt& b,
37  const BigInt& c);
38 
39 /**
40 * Fused multiply-subtract
41 * @param a an integer
42 * @param b an integer
43 * @param c an integer
44 * @return (a*b)-c
45 */
46 BigInt BOTAN_PUBLIC_API(2,0) mul_sub(const BigInt& a,
47  const BigInt& b,
48  const BigInt& c);
49 
50 /**
51 * Return the absolute value
52 * @param n an integer
53 * @return absolute value of n
54 */
55 inline BigInt abs(const BigInt& n) { return n.abs(); }
56 
57 /**
58 * Compute the greatest common divisor
59 * @param x a positive integer
60 * @param y a positive integer
61 * @return gcd(x,y)
62 */
63 BigInt BOTAN_PUBLIC_API(2,0) gcd(const BigInt& x, const BigInt& y);
64 
65 /**
66 * Least common multiple
67 * @param x a positive integer
68 * @param y a positive integer
69 * @return z, smallest integer such that z % x == 0 and z % y == 0
70 */
71 BigInt BOTAN_PUBLIC_API(2,0) lcm(const BigInt& x, const BigInt& y);
72 
73 /**
74 * @param x an integer
75 * @return (x*x)
76 */
77 BigInt BOTAN_PUBLIC_API(2,0) square(const BigInt& x);
78 
79 /**
80 * Modular inversion
81 * @param x a positive integer
82 * @param modulus a positive integer
83 * @return y st (x*y) % modulus == 1 or 0 if no such value
84 * Not const time
85 */
86 BigInt BOTAN_PUBLIC_API(2,0) inverse_mod(const BigInt& x,
87  const BigInt& modulus);
88 
89 /**
90 * Modular inversion using extended binary Euclidian algorithm
91 * @param x a positive integer
92 * @param modulus a positive integer
93 * @return y st (x*y) % modulus == 1 or 0 if no such value
94 * Not const time
95 */
96 BigInt BOTAN_PUBLIC_API(2,5) inverse_euclid(const BigInt& x,
97  const BigInt& modulus);
98 
99 /**
100 * Const time modular inversion
101 * Requires the modulus be odd
102 */
103 BigInt BOTAN_PUBLIC_API(2,0) ct_inverse_mod_odd_modulus(const BigInt& n, const BigInt& mod);
104 
105 /**
106 * Return a^-1 * 2^k mod b
107 * Returns k, between n and 2n
108 * Not const time
109 */
110 size_t BOTAN_PUBLIC_API(2,0) almost_montgomery_inverse(BigInt& result,
111  const BigInt& a,
112  const BigInt& b);
113 
114 /**
115 * Call almost_montgomery_inverse and correct the result to a^-1 mod b
116 */
117 BigInt BOTAN_PUBLIC_API(2,0) normalized_montgomery_inverse(const BigInt& a, const BigInt& b);
118 
119 
120 /**
121 * Compute the Jacobi symbol. If n is prime, this is equivalent
122 * to the Legendre symbol.
123 * @see http://mathworld.wolfram.com/JacobiSymbol.html
124 *
125 * @param a is a non-negative integer
126 * @param n is an odd integer > 1
127 * @return (n / m)
128 */
129 int32_t BOTAN_PUBLIC_API(2,0) jacobi(const BigInt& a,
130  const BigInt& n);
131 
132 /**
133 * Modular exponentation
134 * @param b an integer base
135 * @param x a positive exponent
136 * @param m a positive modulus
137 * @return (b^x) % m
138 */
139 BigInt BOTAN_PUBLIC_API(2,0) power_mod(const BigInt& b,
140  const BigInt& x,
141  const BigInt& m);
142 
143 /**
144 * Compute the square root of x modulo a prime using the
145 * Shanks-Tonnelli algorithm
146 *
147 * @param x the input
148 * @param p the prime
149 * @return y such that (y*y)%p == x, or -1 if no such integer
150 */
151 BigInt BOTAN_PUBLIC_API(2,0) ressol(const BigInt& x, const BigInt& p);
152 
153 /*
154 * Compute -input^-1 mod 2^MP_WORD_BITS. Returns zero if input
155 * is even. If input is odd, input and 2^n are relatively prime
156 * and an inverse exists.
157 */
158 word BOTAN_PUBLIC_API(2,0) monty_inverse(word input);
159 
160 /**
161 * @param x a positive integer
162 * @return count of the zero bits in x, or, equivalently, the largest
163 * value of n such that 2^n divides x evenly. Returns zero if
164 * n is less than or equal to zero.
165 */
166 size_t BOTAN_PUBLIC_API(2,0) low_zero_bits(const BigInt& x);
167 
168 /**
169 * Check for primality
170 * @param n a positive integer to test for primality
171 * @param rng a random number generator
172 * @param prob chance of false positive is bounded by 1/2**prob
173 * @param is_random true if n was randomly chosen by us
174 * @return true if all primality tests passed, otherwise false
175 */
176 bool BOTAN_PUBLIC_API(2,0) is_prime(const BigInt& n,
177  RandomNumberGenerator& rng,
178  size_t prob = 56,
179  bool is_random = false);
180 
181 inline bool quick_check_prime(const BigInt& n, RandomNumberGenerator& rng)
182  { return is_prime(n, rng, 32); }
183 
184 inline bool check_prime(const BigInt& n, RandomNumberGenerator& rng)
185  { return is_prime(n, rng, 56); }
186 
187 inline bool verify_prime(const BigInt& n, RandomNumberGenerator& rng)
188  { return is_prime(n, rng, 80); }
189 
190 
191 /**
192 * Randomly generate a prime suitable for discrete logarithm parameters
193 * @param rng a random number generator
194 * @param bits how large the resulting prime should be in bits
195 * @param coprime a positive integer that (prime - 1) should be coprime to
196 * @param equiv a non-negative number that the result should be
197  equivalent to modulo equiv_mod
198 * @param equiv_mod the modulus equiv should be checked against
199 * @param prob use test so false positive is bounded by 1/2**prob
200 * @return random prime with the specified criteria
201 */
202 BigInt BOTAN_PUBLIC_API(2,0) random_prime(RandomNumberGenerator& rng,
203  size_t bits,
204  const BigInt& coprime = 0,
205  size_t equiv = 1,
206  size_t equiv_mod = 2,
207  size_t prob = 128);
208 
209 /**
210 * Generate a prime suitable for RSA p/q
211 * @param keygen_rng a random number generator
212 * @param prime_test_rng a random number generator
213 * @param bits how large the resulting prime should be in bits (must be >= 512)
214 * @param coprime a positive integer that (prime - 1) should be coprime to
215 * @param prob use test so false positive is bounded by 1/2**prob
216 * @return random prime with the specified criteria
217 */
218 BigInt BOTAN_PUBLIC_API(2,7) generate_rsa_prime(RandomNumberGenerator& keygen_rng,
219  RandomNumberGenerator& prime_test_rng,
220  size_t bits,
221  const BigInt& coprime,
222  size_t prob = 128);
223 
224 /**
225 * Return a 'safe' prime, of the form p=2*q+1 with q prime
226 * @param rng a random number generator
227 * @param bits is how long the resulting prime should be
228 * @return prime randomly chosen from safe primes of length bits
229 */
230 BigInt BOTAN_PUBLIC_API(2,0) random_safe_prime(RandomNumberGenerator& rng,
231  size_t bits);
232 
233 /**
234 * Generate DSA parameters using the FIPS 186 kosherizer
235 * @param rng a random number generator
236 * @param p_out where the prime p will be stored
237 * @param q_out where the prime q will be stored
238 * @param pbits how long p will be in bits
239 * @param qbits how long q will be in bits
240 * @return random seed used to generate this parameter set
241 */
242 std::vector<uint8_t> BOTAN_PUBLIC_API(2,0)
243 generate_dsa_primes(RandomNumberGenerator& rng,
244  BigInt& p_out, BigInt& q_out,
245  size_t pbits, size_t qbits);
246 
247 /**
248 * Generate DSA parameters using the FIPS 186 kosherizer
249 * @param rng a random number generator
250 * @param p_out where the prime p will be stored
251 * @param q_out where the prime q will be stored
252 * @param pbits how long p will be in bits
253 * @param qbits how long q will be in bits
254 * @param seed the seed used to generate the parameters
255 * @param offset optional offset from seed to start searching at
256 * @return true if seed generated a valid DSA parameter set, otherwise
257  false. p_out and q_out are only valid if true was returned.
258 */
259 bool BOTAN_PUBLIC_API(2,0)
260 generate_dsa_primes(RandomNumberGenerator& rng,
261  BigInt& p_out, BigInt& q_out,
262  size_t pbits, size_t qbits,
263  const std::vector<uint8_t>& seed,
264  size_t offset = 0);
265 
266 /**
267 * The size of the PRIMES[] array
268 */
269 const size_t PRIME_TABLE_SIZE = 6541;
270 
271 /**
272 * A const array of all primes less than 65535
273 */
274 extern const uint16_t BOTAN_PUBLIC_API(2,0) PRIMES[];
275 
276 }
277 
278 #endif
const size_t PRIME_TABLE_SIZE
Definition: numthry.h:269
BigInt mul_add(const BigInt &a, const BigInt &b, const BigInt &c)
Definition: mp_numth.cpp:30
const uint16_t PRIMES[]
Definition: primes.cpp:12
size_t low_zero_bits(const BigInt &n)
Definition: numthry.cpp:24
BigInt gcd(const BigInt &a, const BigInt &b)
Definition: numthry.cpp:50
BigInt power_mod(const BigInt &base, const BigInt &exp, const BigInt &mod)
Definition: numthry.cpp:398
BigInt inverse_euclid(const BigInt &n, const BigInt &mod)
Definition: numthry.cpp:304
BigInt mul_sub(const BigInt &a, const BigInt &b, const BigInt &c)
Definition: mp_numth.cpp:73
bool quick_check_prime(const BigInt &n, RandomNumberGenerator &rng)
Definition: numthry.h:181
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
Definition: bigint.h:796
BigInt ct_inverse_mod_odd_modulus(const BigInt &n, const BigInt &mod)
Definition: numthry.cpp:163
BigInt normalized_montgomery_inverse(const BigInt &a, const BigInt &p)
Definition: numthry.cpp:148
bool is_prime(const BigInt &n, RandomNumberGenerator &rng, size_t prob, bool is_random)
Definition: numthry.cpp:507
BigInt ressol(const BigInt &x, const BigInt &p)
Definition: ressol.cpp:17
BigInt sub_mul(const BigInt &a, const BigInt &b, const BigInt &c)
Definition: mp_numth.cpp:59
bool generate_dsa_primes(RandomNumberGenerator &rng, BigInt &p, BigInt &q, size_t pbits, size_t qbits, const std::vector< uint8_t > &seed_c, size_t offset)
Definition: dsa_gen.cpp:39
BigInt abs(const BigInt &n)
Definition: numthry.h:55
BigInt lcm(const BigInt &a, const BigInt &b)
Definition: numthry.cpp:83
bool verify_prime(const BigInt &n, RandomNumberGenerator &rng)
Definition: numthry.h:187
BigInt random_safe_prime(RandomNumberGenerator &rng, size_t bits)
Definition: make_prm.cpp:259
BigInt inverse_mod(const BigInt &n, const BigInt &mod)
Definition: numthry.cpp:288
BigInt square(const BigInt &x)
Definition: mp_numth.cpp:19
Definition: alg_id.cpp:13
BigInt random_prime(RandomNumberGenerator &rng, size_t bits, const BigInt &coprime, size_t equiv, size_t modulo, size_t prob)
Definition: make_prm.cpp:73
BigInt abs() const
Definition: bigint.cpp:295
size_t almost_montgomery_inverse(BigInt &result, const BigInt &a, const BigInt &p)
Definition: numthry.cpp:100
BigInt generate_rsa_prime(RandomNumberGenerator &keygen_rng, RandomNumberGenerator &prime_test_rng, size_t bits, const BigInt &coprime, size_t prob)
Definition: make_prm.cpp:176
int32_t jacobi(const BigInt &a, const BigInt &n)
Definition: jacobi.cpp:15
word monty_inverse(word input)
Definition: numthry.cpp:350
bool check_prime(const BigInt &n, RandomNumberGenerator &rng)
Definition: numthry.h:184