Botan

Number Theory Functions

License
Botan is released under the Simplified BSD License (see LICENSE.md)

BigInt  mulAdd()(auto ref const BigInt a, auto ref const BigInt b, auto ref const BigInt c);

Fused multiply-add

Parameters
BigInt a an integer
BigInt b an integer
BigInt c an integer
Returns
(a*b)+c

BigInt  subMul()(auto ref const BigInt a, auto ref const BigInt b, auto ref const BigInt c);

Fused subtract-multiply

Parameters
BigInt a an integer
BigInt b an integer
BigInt c an integer
Returns
(a-b)*c

BigInt  abs()(auto ref const BigInt n);

Return the absolute value

Parameters
BigInt n an integer
Returns
absolute value of n

BigInt  gcd()(auto ref const BigInt a, auto ref const BigInt b);

Compute the greatest common divisor

Parameters
BigInt a positive integer x
BigInt b positive integer y
Returns
 gcd(x,y)

BigInt  lcm()(auto ref const BigInt a, auto ref const BigInt b);

Least common multiple

Parameters
BigInt a a positive integer x
BigInt b a positive integer y
Returns
z, smallest integer such that z % x == 0 and z % y == 0

BigInt  square()(auto ref const BigInt x);

Parameters
BigInt x an integer
Returns
(x*x)

BigInt  inverseMod()(auto ref const BigInt n, auto ref const BigInt mod);

Modular inversion

Parameters
BigInt n a positive integer x
BigInt mod a positive integer modulus
Returns
y st (x*y) % modulus == 1

int  jacobi()(auto ref const BigInt a, auto ref const BigInt n);

Compute the Jacobi symbol. If n is prime, this is equivalent to the Legendre symbol. @see http://mathworld.wolfram.com/JacobiSymbol.html

Parameters
BigInt a is a non-negative integer
BigInt n is an odd integer > 1
Returns
(n / m)

BigInt  powerMod()(auto ref const BigInt base, auto ref const BigInt exp, auto ref const BigInt mod);

Modular exponentation

Parameters
BigInt base an integer base b
BigInt exp a positive exponent x
BigInt mod a positive modulus m
Returns
(b^x) % m

BigInt  ressol()(auto ref const BigInt a, auto ref const BigInt p);

Compute the square root of x modulo a prime using the Shanks-Tonnelli algorithm

Parameters
BigInt a the input x
BigInt p the prime p
Returns
y such that (y*y)%p == x, or -1 if no such integer

size_t  lowZeroBits()(auto ref const BigInt n);

Parameters
BigInt n a positive integer x
Returns
count of the zero bits in x, or, equivalently, the largest value of n such that 2^n divides x evenly. Returns zero if n is less than or equal to zero.

bool  isPrime()(auto ref const BigInt n, RandomNumberGenerator rng, size_t prob = 56, bool is_random = false);

Check for primality using Miller-Rabin

Parameters
BigInt n a positive integer to test for primality
RandomNumberGenerator rng a random number generator
size_t prob chance of false positive is bounded by 1/2**prob
bool is_random true if n was randomly chosen by us
Returns
true if all primality tests passed, otherwise false

BigInt  randomPrime()(RandomNumberGenerator rng, size_t bits, ref const BigInt coprime, size_t equiv = 1, size_t modulo = 2);
BigInt  randomPrime()(RandomNumberGenerator rng, size_t bits, const BigInt coprime = 1, size_t equiv = 1, size_t modulo = 2);

Randomly generate a prime

Parameters
RandomNumberGenerator rng a random number generator
size_t bits how large the resulting prime should be in bits
BigInt coprime a positive integer the result should be coprime to
size_t equiv a non-negative number that the result should be equivalent to modulo equiv_mod
size_t modulo the modulus equiv should be checked against
Returns
random prime with the specified criteria

BigInt  randomSafePrime(RandomNumberGenerator rng, size_t bits);

Return a random 'safe' prime, of the form p=2*q+1 with q prime

Parameters
RandomNumberGenerator rng a random number generator
size_t bits is how long the resulting prime should be
Returns
prime randomly chosen from safe primes of length bits

Vector!ubyte  generateDsaPrimes(RandomNumberGenerator rng, AlgorithmFactory af, ref BigInt p_out, ref BigInt q_out, size_t pbits, size_t qbits);

Generate DSA parameters using the FIPS 186 kosherizer

Parameters
RandomNumberGenerator rng a random number generator
AlgorithmFactory af an algorithm factory
BigInt p_out where the prime p will be stored
BigInt q_out where the prime q will be stored
size_t pbits how long p will be in bits
size_t qbits how long q will be in bits
Returns
random seed used to generate this parameter set

bool  generateDsaPrimes()(RandomNumberGenerator rng, AlgorithmFactory af, ref BigInt p_out, ref BigInt q_out, size_t pbits, size_t qbits, auto ref const Vector!ubyte seed_c);

Generate DSA parameters using the FIPS 186 kosherizer

Parameters
RandomNumberGenerator rng a random number generator
AlgorithmFactory af an algorithm factory
BigInt p_out where the prime p will be stored
BigInt q_out where the prime q will be stored
size_t pbits how long p will be in bits
size_t qbits how long q will be in bits
Vector!ubyte seed_c the seed used to generate the parameters
Returns
true if seed generated a valid DSA parameter set, otherwise false. p_out and q_out are only valid if true was returned.