utils.utils module¶
Useful functions for creating encryption schemes.
- utils.utils.extended_euclidean(num_a, num_b)[source]¶
Perform the extended euclidean algorithm on the input numbers. The method returns gcd, x, y, such that a*x + b*y = gcd.
- Parameters:
num_a (
int
) – First number a.num_b (
int
) – Second number b.
- Return type:
tuple
[int
,int
,int
]- Returns:
Tuple containing gcd, x, and y, such that a*x + b*y = gcd.
- utils.utils.is_prime(number)[source]¶
Check if the input number is a prime number. Uses GMPY2 if available
- Parameters:
number (
int
) – The number to check- Return type:
bool
- Returns:
Whether the input is prime or not
- utils.utils.lcm(num_a, num_b)[source]¶
Compute the least common multiple of two input numbers. Uses GMPY2 if available.
- Parameters:
num_a (
int
) – First number a.num_b (
int
) – Second number b.
- Return type:
int
- Returns:
Least common multiple of a and b.
- utils.utils.mod_inv(value, modulus)[source]¶
Compute the inverse of a number, given the modulus of the group. Note that the inverse might not exist. Uses GMPY2 if available.
- Parameters:
value (
int
) – The number to be inverted.modulus (
int
) – The group modulus.
- Raises:
ZeroDivisionError – Raised when the inverse of the value does not exist.
- Return type:
int
- Returns:
The inverse of a under the modulus.
- utils.utils.next_prime(low)[source]¶
Generate the first prime number greater than the given value. Returns GMPY2 MPZ integer if available.
- Parameters:
low (
int
) – Lower bound for the prime.- Return type:
int
- Returns:
First prime number strictly greater than low.
- utils.utils.pow_mod(base, exponent, modulus)[source]¶
Compute base**exponent % modulus. Uses GMPY2 if available.
- Parameters:
base (
int
) – baseexponent (
int
) – exponentmodulus (
int
) – modulus
- Return type:
int
- Returns:
base**exponent % modulus
- utils.utils.randprime(low, high)[source]¶
Generate a random prime number in the range [low, high). Returns GMPY2 MPZ integer if available.
- Parameters:
low (
int
) – Lower bound (inclusive) of the range.high (
int
) – Upper bound (exclusive) of the range.
- Return type:
int
- Returns:
Random prime number.
- Raises:
ValueError – the lower bound should be strictly lower than the upper bound