CryptoTools provides several utils function wich can be used for cryptography purposes

bin_expo(n, e)

This function perform an binary exponentiation, also known as Exponentiation squaring. A binary exponentiation is the process for computing an integer power of a number, such as a ** n. For doing that, the first step is to convert the exponent into a binary representation And for each 1 bit, we compute the exponent.

Parameters:
  • n (Integer) –

    it's the base

  • e (Integer) –

    it's the exponent

Returns:
  • int

    Return the result of the exponentation of n ** e

Source code in Cryptotools/Utils/utils.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def bin_expo(n, e) -> int:
    """
        This function perform an binary exponentiation, also known as Exponentiation squaring.
        A binary exponentiation is the process for computing an integer power of a number, such as a ** n.
        For doing that, the first step is to convert the exponent into a binary representation
        And for each 1 bit, we compute the exponent.

        Args:
            n (Integer): it's the base
            e (Integer): it's the exponent

        Returns:
            Return the result of the exponentation of n ** e

    """
    binary = bin(e)[2:] # Remove the prefix 0b

    r = 1
    exp = 1
    # We need to reverse, and to start from the right to left
    # Otherwise, we do on left to the right
    # It's dirty, maybe we can find another way to do that
    for b in binary[::-1]:
        if b == '1':
            r *= n ** exp
            print(n ** exp, exp)
        exp *= 2
    return r

egcd(a, b)

This function compute the Extended Euclidean algorithm https://user.eng.umd.edu/~danadach/Cryptography_20/ExtEuclAlg.pdf https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm

Parameters:
  • a (Integer) –

    the number a

  • b (Integer) –

    the number b

Source code in Cryptotools/Utils/utils.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def egcd(a, b):
    """
        This function compute the Extended Euclidean algorithm
        https://user.eng.umd.edu/~danadach/Cryptography_20/ExtEuclAlg.pdf
        https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm

        Args:
            a (Integer): the number a
            b (Integer): the number b
    """
    if a == 0:
        return b, 0, 1
    gcd, x1, y1 = egcd(b % a, a)
    x = y1 - (b // a) * x1
    y = x1
    return gcd, x, y

exponent_squaring(n, e)

This function perform an exponentiation squaring, which compute an integer power of a number based on the following algorithm: n ** e = { 1 # if n is 0 (n ** (e / 2)) ** 2 # if n is even ((n ** (e - 1 / 2)) ** 2) * n # if n is odd }

Parameters:
  • n (Integer) –

    n is the base of n ** e

  • e (Integer) –

    e is the exponent

Source code in Cryptotools/Utils/utils.py
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
def exponent_squaring(n, e):
    """
        This function perform an exponentiation squaring, which compute an integer power of a number based on the following algorithm:
            n ** e = {
                1                               # if n is 0
                (n ** (e / 2)) ** 2             # if n is even
                ((n ** (e - 1 / 2)) ** 2) * n   # if n is odd
            }

        Args:
            n (Integer): n is the base of n ** e
            e (Integer): e is the exponent
    """
    if e < 0:
        return exponent_squaring(1 / n, -e)
    elif e == 0:
        return 1
    elif e % 2 == 1: # n is odd
        return exponent_squaring(n * n, (e - 1) / 2) * n
    elif e % 2 == 0: # n is even
        return exponent_squaring(n * n, e / 2)

gcd(a, b)

This function calculate the GCD (Greatest Common Divisor of the number a of b Args: a (Integer): the number a b (integer): the number b

Return

the GCD

Source code in Cryptotools/Utils/utils.py
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
def gcd(a, b):
    """
        This function calculate the GCD (Greatest Common Divisor of the number a of b
        Args:
            a (Integer): the number a
            b (integer): the number b

        Return:
            the GCD 
    """

    if b == 0:
        return a
    return gcd(b, a%b)