Cryptography module
Primes
- class mathcrypto.cryptography.primes.Primes[source]
Bases:
object
- classmethod factorize(num: int) list [source]
Classic number factorization
Tests divisibility by 2 and then every odd number up to sqrt(num) while appending the factors. If the number contains multiple instances of a factor, this function returns a list with duplicates. Very fast unless the number is a compound of more than one large prime (more than 7 digits, 8 is still acceptable).
- Parameters
num (int) – Number to factorize
- Returns
List of factors including duplicates
- Return type
list
- classmethod get_prime(bit_length: int) int [source]
Get a n-bit prime
- Parameters
bit_length (int) – Bit size of the desired prime number
- Returns
desired prime number
- Return type
int
- classmethod is_prime(num: int) bool [source]
Classic number modulus check
Tests divisibility by 2 and then every odd number up to sqrt(num). Takes longer to compute than Fermat’s primality test but has 100% certainty. Cannot handle numbers larger than the system maxint.
- Parameters
num (int) – Number to test
- Raises
OverflowError – If the number is too large
- Returns
True if
num
is prime- Return type
bool
- classmethod is_probable_prime_fermat(num: int, rounds: int = 5) bool [source]
Automatic Fermat’s primality test
This test can not provide 100% certainty that the number is indeed prime, so more than 1 round should be required. If determined that the number is not prime, that is on the other hand 100% certain. Tests for the number of rounds specified, 2-3 rounds are generally enough. If any round returns a result that is not 1, the number is not prime.
- Parameters
num (int) – Number to be tested
rounds (int) – How many rounds of testing to perform
verbose (bool, optional) – Whether to return optional list of [<number it was tested against>: int,<result>: int]. Defaults to False.
- Returns
True if probably prime
- Return type
bool
- If verbose, returns:
(tuple): tuple containing:
bool: True if probably prime
list: List of [<number it was tested against>: int,<result>: int]
Diffie Hellmann
- class mathcrypto.cryptography.diffie_hellman.DHCracker[source]
Bases:
object
- classmethod baby_step(crack_me) int [source]
Discrete logarithm problem solution using the Baby-step Giant-step algorithm. RAM intensive.
- Parameters
crack_me (DHCryptosystem object) – Object containing the publicly know values of the cryptosystem.
- Returns
int if a key was found, else None
- Return type
int or None
- classmethod brute_force(crack_me, num_cpus: int) int [source]
Calculates the DHCryptosystem key by utilizing multiprocessing enhanced brute force. CPU and time intensive.
- Parameters
crack_me (DHCryptosystem object) – Needs to be containing the publicly known values of the cryptosystem.
num_cpus (int) – Number of CPU cores to utilize. Do not exceed the number of logical cores your CPU has, this will result in slower execution.
- Returns
int if a key was found, else None
- Return type
int or None
- class mathcrypto.cryptography.diffie_hellman.DHCryptosystem(prime: Optional[int] = None, generator: Optional[int] = None, alice_secret: Optional[int] = None, bob_secret: Optional[int] = None, alice_sends: Optional[int] = None, bob_sends: Optional[int] = None, key: Optional[int] = None)[source]
Bases:
object
Object containing all values of the cryptosystem
- generate_from(bit_length: Optional[int] = None, prime: Optional[int] = None)[source]
Generates the DHCryptosystem values (If not passed == if they are None) or assigns them
- Parameters
bit_size (int, optional) – Bit size of the prime. Not necessary if prime also passed.
prime (int, optional) – Prime number base of the cryptosystem.
- Raises
ValueError – If neither
bit_size
orprime
is passed. At least one of these is required.
Elliptic Curves
This module wouldn’t be possible to be created this fast if I didn’t leverage the code that was written by xnomas and Baka-Git in their repository at:
https://github.com/Baka-Git/Crypto_Math
I want to thank them for allowing me to use their code.
- class mathcrypto.cryptography.elliptic_curves.EllipticCurve(a0: int, a1: int, a2: int, a3: int, a4: int, a5: int, a6: int, field: Optional[int] = None, point_px: Optional[int] = None, point_py: Optional[int] = None)[source]
Bases:
object
Elliptic curve objects
- Parameters
a0-a6 (int) – Curve attributes (using the equation a0*y^2 + a1*y + a2*y*x = a3*x^3 + a4*x^2 + a5*x+a6)
field (int, optional) – The curves field
point_px (int, optional) – X coordinate of point P
point_py (int, optional) – Y coordinate of point P
- add_point(point_qx: int, point_qy: int)[source]
Adds point P and point Q (of given coordinates) on the curve.
- Parameters
point_qx (int) – X coordinate of point Q
point_qy (int) – Y coordinate of point Q
- Raises
ValueError – If curve field is not set.
ValueError – If the curve is not elliptic.
ValueError – If point P is not set.
ValueError – If one or both poits are not on the curve.
- Returns
Resulting point coordinates
- Return type
list
- get_all_point_order()[source]
Gets orders of all points on the curve
- Raises
ValueError – If field is not set
- Returns
list of lists[order, point]
- Return type
list of lists
- get_curve_order(get_points: bool = False)[source]
Gets the order of the curve.
- Parameters
get_points (bool, optional) – Whether or not to return the curve points as well. Defaults to False.
- Raises
ValueError – If curve field is not set or the curve is not elliptic.
- Returns
False if this EC is not supported. int: Elliptic curve order if get_points is not set to True (tuple):If get_points is set to true, returns a tuple containing:
int: Elliptic curve order
list: List of points on curve
- Return type
bool
- get_point_order(point_x: Optional[int] = None, point_y: Optional[int] = None)[source]
Gets the order of point of given coordinates or point P if set. Given coordinates take precedence.
- Parameters
point_x (int, optional) – X coordinate of the point
point_y (int, optional) – Y coordinate of the point
- Raises
ValueError – Neither valid parameters were passed and point P was not set
- Returns
Order of the point
- Return type
int
- classmethod get_possible_orders(order: int, new_order: Optional[int] = None)[source]
Gets the possible orders of points on a curve of certain order or if a curves order was changed to a given value.
- Parameters
order (int) – Order of the curve
new_order (int, optional) – New order of the curve. Defaults to None.
- Returns
List of possible orders
- Return type
list
- is_elliptic_curve()[source]
Checks if the curve is elliptic
- Returns
True if curve with these attributes is elliptic
- Return type
bool
- is_point_on_elliptic_curve(x: int, y: int)[source]
Checks if point of given coordinates is on the curve.
- Parameters
x (int) – X coordinate of the point
y (int) – Y coordinate of the point
- Raises
ValueError – If curve field is not set or the curve is not elliptic.
- Returns
True if point is on the curve
- Return type
bool
Math module
Functions
- class mathcrypto.math.funcs.MathFunctions[source]
Bases:
object
A collection of useful mathematical functions
- classmethod crt(lis) int [source]
Chinese remainder theorem
- Solves x for problems like:
x ≡ 8 mod 9
x ≡ 3 mod 5
- Parameters
lis – list of [int, int]:
- Example input:
[[8, 9],[3, 5]] for the example problem
- Returns
Solution for x
- Return type
int
- classmethod eea(modulus: int, number: int, verbose: bool = False) int [source]
Extended Euclidean Algorithm
Get multiplicative inverse of a number in modulus
- Parameters
modulus (int) – modulus of a multiplicative group
number (int) – number to get inverse to
verbose (bool, optional) – Whether to return a graphical solution. Defaults to False.
- Raises
ValueError – If number is not an element of the group defined by
modulus
- Returns
resulting inverse
- Return type
int
- If verbose, returns:
str: Graphical solution of the problem.
Groups
- class mathcrypto.math.groups.MultiplicativeGroup(mod)[source]
Bases:
object
Multiplicative group objects
- mod
Modulus of the group
- Type
int
- elements
List of elements in the group
- Type
list
- order
Order of the group
- Type
int
- generators
List of generators of the group
- Type
list
- get_element_order(element) int [source]
Gets the order of an element in the group
- Parameters
element (int) – Element of the group
- Raises
ValueError – When the
element
does not belong to the group- Returns
Returns the order of
element
in the group- Return type
int