Botan 3.13.0
Crypto and TLS for C&
numthry.h
Go to the documentation of this file.
1/*
2* Number Theory Functions
3* (C) 1999-2007,2018 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
13namespace Botan {
14
16
17/**
18* Return the absolute value
19* @param n an integer
20* @return absolute value of n
21*/
22inline BigInt abs(const BigInt& n) {
23 return n.abs();
24}
25
26/**
27* Compute the greatest common divisor
28* @param x a positive integer
29* @param y a positive integer
30* @return gcd(x,y)
31*/
32BigInt BOTAN_PUBLIC_API(2, 0) gcd(const BigInt& x, const BigInt& y);
33
34/**
35* Least common multiple
36* @param x a positive integer
37* @param y a positive integer
38* @return z, smallest integer such that z % x == 0 and z % y == 0
39*/
40BOTAN_DEPRECATED("Deprecated no replacement") BigInt BOTAN_PUBLIC_API(2, 0) lcm(const BigInt& x, const BigInt& y);
41
42/**
43* Square an integer
44* @param x an integer
45* @return (x*x)
46*/
47BOTAN_DEPRECATED("Just use x*x") BigInt BOTAN_PUBLIC_API(2, 0) square(const BigInt& x);
48
49/**
50* Modular inversion. This algorithm is const time with respect to x,
51* as long as x is less than modulus. It also avoids leaking
52* information about the modulus, except that it does leak which of 3
53* categories the modulus is in: an odd integer, a power of 2, or some
54* other even number, and if the modulus is even, leaks the power of 2
55* which divides the modulus.
56*
57* @param x a positive integer
58* @param modulus a positive integer
59* @return y st (x*y) % modulus == 1 or 0 if no such value
60*/
61BigInt BOTAN_PUBLIC_API(2, 0) inverse_mod(const BigInt& x, const BigInt& modulus);
62
63/**
64* Compute the Jacobi symbol. If n is prime, this is equivalent
65* to the Legendre symbol.
66* @see http://mathworld.wolfram.com/JacobiSymbol.html
67*
68* @param a is a non-negative integer
69* @param n is an odd integer > 1
70* @return (n / m)
71*/
72BOTAN_DEPRECATED("Deprecated no replacement") int32_t BOTAN_PUBLIC_API(2, 0) jacobi(BigInt a, BigInt n);
73
74/**
75* Modular exponentiation
76* @param b an integer base
77* @param x a positive exponent
78* @param m a positive modulus
79* @return (b^x) % m
80*/
81BigInt BOTAN_PUBLIC_API(2, 0) power_mod(const BigInt& b, const BigInt& x, const BigInt& m);
82
83/**
84* Compute the square root of x modulo a prime using the Tonelli-Shanks
85* algorithm. This algorithm is primarily used for EC point
86* decompression which takes only public inputs, as a consequence it is
87* not written to be constant-time and may leak side-channel information
88* about its arguments.
89*
90* @param x the input
91* @param p the prime modulus
92* @return y such that (y*y)%p == x, or -1 if no such integer
93*/
94BOTAN_DEPRECATED("Deprecated no replacement")
95BigInt BOTAN_PUBLIC_API(3, 0) sqrt_modulo_prime(const BigInt& x, const BigInt& p);
96
97/**
98* Count the low zero bits of an integer
99* @param x an integer
100* @return count of the low zero bits in x, or, equivalently, the
101* largest value of n such that 2^n divides x evenly. Returns
102* zero if x is equal to zero.
103*/
104BOTAN_DEPRECATED("Deprecated no replacement") size_t BOTAN_PUBLIC_API(2, 0) low_zero_bits(const BigInt& x);
105
106/**
107* Check for primality
108*
109* This uses probabilistic algorithms - there is some non-zero (but very low)
110* probability that this function will return true even if *n* is actually
111* composite.
112*
113* @param n a positive integer to test for primality
114* @param rng a random number generator
115* @param prob chance of false positive is bounded by 1/2**prob
116* @param is_random true if n was randomly chosen by us
117* @return true if all primality tests passed, otherwise false
118*/
119bool BOTAN_PUBLIC_API(2, 0)
120 is_prime(const BigInt& n, RandomNumberGenerator& rng, size_t prob = 64, bool is_random = false);
121
122/**
123* Test if the positive integer x is a perfect square ie if there
124* exists some positive integer y st y*y == x
125* See FIPS 186-4 sec C.4
126* @return 0 if the integer is not a perfect square, otherwise
127* returns the positive y st y*y == x
128*/
129BOTAN_DEPRECATED("Deprecated no replacement") BigInt BOTAN_PUBLIC_API(2, 8) is_perfect_square(const BigInt& x);
130
131/**
132* Randomly generate a prime suitable for discrete logarithm parameters
133* @param rng a random number generator
134* @param bits how large the resulting prime should be in bits
135* @param coprime a positive integer that (prime - 1) should be coprime to
136* @param equiv a non-negative number that the result should be
137 equivalent to modulo equiv_mod
138* @param equiv_mod the modulus equiv should be checked against
139* @param prob use test so false positive is bounded by 1/2**prob
140* @return random prime with the specified criteria
141*/
142BigInt BOTAN_PUBLIC_API(2, 0) random_prime(RandomNumberGenerator& rng,
143 size_t bits,
144 const BigInt& coprime = BigInt::from_u64(0),
145 size_t equiv = 1,
146 size_t equiv_mod = 2,
147 size_t prob = 128);
148
149/**
150* Generate a prime suitable for RSA p/q
151* @param keygen_rng a random number generator
152* @param prime_test_rng a random number generator
153* @param bits how large the resulting prime should be in bits (must be >= 512)
154* @param coprime a positive integer that (prime - 1) should be coprime to
155* @param prob use test so false positive is bounded by 1/2**prob
156* @return random prime with the specified criteria
157*/
158BOTAN_DEPRECATED("Deprecated no replacement")
159BigInt BOTAN_PUBLIC_API(2, 7) generate_rsa_prime(RandomNumberGenerator& keygen_rng,
160 RandomNumberGenerator& prime_test_rng,
161 size_t bits,
162 const BigInt& coprime,
163 size_t prob = 128);
164
165/**
166* Return a 'safe' prime, of the form p=2*q+1 with q prime
167* @param rng a random number generator
168* @param bits is how long the resulting prime should be
169* @return prime randomly chosen from safe primes of length bits
170*/
171BOTAN_DEPRECATED("Deprecated no replacement")
172BigInt BOTAN_PUBLIC_API(2, 0) random_safe_prime(RandomNumberGenerator& rng, size_t bits);
173
174/**
175* The size of the PRIMES[] array
176*/
177BOTAN_DEPRECATED("Deprecated no replacement") const size_t PRIME_TABLE_SIZE = 6541;
178
179/**
180* A const array of all odd primes less than 65535
181*/
182extern const uint16_t BOTAN_PUBLIC_API(2, 0) PRIMES[];
183
184} // namespace Botan
185
186#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
#define BOTAN_DEPRECATED(msg)
Definition api.h:73
BigInt abs() const
Definition bigint.cpp:386
BigInt power_mod(const BigInt &base, const BigInt &exp, const BigInt &mod)
Definition numthry.cpp:310
int32_t jacobi(BigInt a, BigInt n)
Definition numthry.cpp:119
BigInt random_prime(RandomNumberGenerator &rng, size_t bits, const BigInt &coprime, size_t equiv, size_t modulo, size_t prob)
Definition make_prm.cpp:194
BigInt lcm(const BigInt &a, const BigInt &b)
Definition numthry.cpp:296
BigInt square(const BigInt &x)
Definition numthry.cpp:184
const uint16_t PRIMES[]
Definition primes.cpp:12
size_t low_zero_bits(const BigInt &n)
Definition numthry.cpp:194
BigInt abs(const BigInt &n)
Definition numthry.h:22
const size_t PRIME_TABLE_SIZE
Definition numthry.h:177
bool is_prime(const BigInt &n, RandomNumberGenerator &rng, size_t prob, bool is_random)
Definition numthry.cpp:381
BigInt generate_rsa_prime(RandomNumberGenerator &keygen_rng, RandomNumberGenerator &prime_test_rng, size_t bits, const BigInt &coprime, size_t prob)
Definition make_prm.cpp:248
BigInt gcd(const BigInt &a, const BigInt &b)
Definition numthry.cpp:220
BigInt sqrt_modulo_prime(const BigInt &a, const BigInt &p)
Definition numthry.cpp:27
BigInt is_perfect_square(const BigInt &C)
Definition numthry.cpp:347
BigInt random_safe_prime(RandomNumberGenerator &rng, size_t bits)
Definition make_prm.cpp:334
BigInt inverse_mod(const BigInt &n, const BigInt &mod)
Definition mod_inv.cpp:380