# Number Theory

For generating keys, we need strong prime number, they are the basis. CryptoTools provides functions for generating these numbers.

## prime numbers

## `are_coprime(p1, p2)`

This function check if p1 and p2 are coprime

Parameters:

- `p1` (`list`) –

  list of prime numbers of the first number

- `p2` (`list`) –

  list of prime number of the second number

Returns:

- –

  Return a boolean result

Source code in `Cryptotools/Numbers/primeNumber.py`

    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169

    def are_coprime(p1, p2):
        """
            This function check if p1 and p2 are coprime

            Args:
                p1 (list): list of prime numbers of the first number
                p2 (list): list of prime number of the second number

            Returns:
                Return a  boolean result
        """
        r = True
        for entry in p1:
            if entry in p2:
                r = False
                break
        return r

## `getPrimeNumber(n, safe=True)`

This function generate a large prime number based on "A method for Obtaining Digital Signatures and Public-Key Cryptosystems" Section B. How to Find Large Prime Numbers https://dl.acm.org/doi/pdf/10.1145/359340.359342

Parameters:

- `n` (`Integer`) –

  The size of the prime number. Must be a multiple of 64

- `safe` (`Boolean`, default: `True` ) –

  When generating the prime number, the function must find a safe prime number, based on the Germain primes (2p + 1 is also a prime number)

Returns:

- –

  Return the prime number

Source code in `Cryptotools/Numbers/primeNumber.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
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79

    def getPrimeNumber(n, safe=True):
        """
            This function generate a large prime number
            based on "A method for Obtaining Digital Signatures
            and Public-Key Cryptosystems"
            Section B. How to Find Large Prime Numbers
            https://dl.acm.org/doi/pdf/10.1145/359340.359342

            Args:
                n (Integer): The size of the prime number. Must be a multiple of 64
                safe (Boolean): When generating the prime number, the function must find a safe prime number, based on the Germain primes (2p + 1 is also a prime number)

            Returns:
                Return the prime number
        """
        if n % 64 != 0:
            print("Must be multiple of 64")
            return
        # from sys import getsizeof
        upper = getrandbits(n)
        lower = upper >> 7
        r = randint(lower, upper)

        while r % 2 == 0:
            r += 1

        # Now, we are going to compute r as prime number
        i = 100
        while 1:
            # Check if it's a prime number
            if _millerRabinTest(r):
                break

            # TODO: it's dirty, need to change that
            # i = int(log2(r))
            # i *= randint(2, 50)

            r += i
            # print(f"{i} {r}")
            i += 1

        # print(_fermatLittleTheorem(r))
        # print(getsizeof(r))
        return r

## `getSmallPrimeNumber(n)`

This function is deprecated

Parameters:

- `n` (`Integer`) –

  Get the small prime number until n

Returns:

- `int` –

  Return the first prime number found

Source code in `Cryptotools/Numbers/primeNumber.py`

     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106

    def getSmallPrimeNumber(n) -> int:
        """
            This function is deprecated

            Args:
                n (Integer): Get the small prime number until n

            Returns:
                Return the first prime number found
        """
        is_prime = True

        while True:
            for i in range(2, n):
                # If n is divisible by i, it's not a prime, we break the loop
                if n % i == 0:
                    is_prime = False
                    break

            if is_prime:
                break
            is_prime = True
            n = n + 1

        p = n
        return p

## `get_n_prime_numbers(n)`

This function return a list of n prime numbers

Parameters:

- `n` (`Integer`) –

  the range, must be an integer

Returns:

- `list` –

  Return a list of n prime numbers

Source code in `Cryptotools/Numbers/primeNumber.py`

    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151

    def get_n_prime_numbers(n) -> list:
        """
            This function return a list of n prime numbers

            Args:
                n (Integer): the range, must be an integer

            Returns:
                Return a list of n prime numbers
        """
        l = list()

        count = 2
        index = 0
        while index < n:
            is_prime = True
            for x in range(2, count):
                if count % x == 0:
                    is_prime = False
                    break

            if is_prime:
                l.append(count)
                index += 1

            count += 1

        return l

## `get_prime_numbers(n)`

This function get all prime number of the n

Parameters:

- `n` (`Integer`) –

  find all prime number until n is achieves

Returns:

- `list` –

  Return a list of prime numbers

Source code in `Cryptotools/Numbers/primeNumber.py`

    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122

    def get_prime_numbers(n) -> list:
        """
            This function get all prime number of the n

            Args:
                n (Integer): find all prime number until n is achieves

            Returns:
             Return a list of prime numbers
        """
        l = list()
        for i in range(2, n):
            if n % i == 0:
                l.append(i)
        return l

## `isPrimeNumber(p)`

Check if the number p is a prime number or not. The function iterate until p is achieve.

This function is not the efficient way to determine if p is prime or a composite.

To identify if p is prime or not, the function check the result of the Euclidean Division has a remainder. If yes, it's means it's not a prime number

Parameters:

- `p` (`Integer`) –

  number if possible prime or not

Returns:

- –

  Return a boolean if the number p is a prime number or not. True if yes

Source code in `Cryptotools/Numbers/primeNumber.py`

     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25

    def isPrimeNumber(p):
        """
            Check if the number p is a prime number or not. The function iterate until p is achieve.

            This function is not the efficient way to determine if p is prime or a composite.

            To identify if p is prime or not, the function check the result of the Euclidean Division has a remainder. If yes, it's means it's not a prime number

            Args:
                p (Integer): number if possible prime or not

            Returns:
                Return a boolean if the number p is a prime number or not. True if yes
        """
        for i in range(2, p):
            if p % i == 0:
                return False
        return True

## `isSafePrime(n)`

This function has not been implemented yet, but check if the number n is a safe prime number. This function will test different properties of the possible prime number n

Parameters:

- `n` (`Integer`) –

  the prime number to check

Returns:

- `bool` –

  Return a Boolean if the prime number n is safe or not. True if yes, otherwise it's False

Source code in `Cryptotools/Numbers/primeNumber.py`

    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287

    def isSafePrime(n) -> bool:
        """
            This function has not been implemented yet, but check if the number n is a safe prime number. This function will test different properties of the possible prime number n

            Args:
                n (Integer): the prime number to check

            Returns:
                Return a Boolean if the prime number n is safe or not. True if yes, otherwise it's False
        """
        if n.bit_length() >= 256:
            return True

        # Do Sophie Germain's test

        return False

## `sieveOfEratosthenes(n)`

This function build a list of prime number based on the Sieve of Erastosthenes

Parameters:

- `n` (`Integer`) –

  Interate until n is achives

Returns:

- `list` –

  Return a list of all prime numbers to n

Source code in `Cryptotools/Numbers/primeNumber.py`

    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198

    def sieveOfEratosthenes(n) -> list:
        """
            This function build a list of prime number based on the Sieve of Erastosthenes

            Args:
                n (Integer): Interate until n is achives

            Returns:
                Return a list of all prime numbers to n
        """
        if n < 1:
            return list()

        eratost = dict()
        for i in range(2, n):
            eratost[i] = True

        for i in range(2, n):
            if eratost[i]:
                for j in range(i*i, n, i):
                    eratost[j] = False

        sieve = list()
        for i in range(2, n):
            if eratost[i]:
                sieve.append(i)

        return sieve

## `sophieGermainPrime(p)`

Check if the number p is a safe prime number: 2p + 1 is also a prime

Parameters:

- `p` (`Integer`) –

  Possible prime number

Returns:

- `bool` –

  Return True if p is a safe prime number, otherwise it's False

Source code in `Cryptotools/Numbers/primeNumber.py`

    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270

    def sophieGermainPrime(p) -> bool:
        """
            Check if the number p is a safe prime number: 2p + 1 is also a prime

            Args:
                p (Integer):  Possible prime number

            Returns:
                Return True if p is a safe prime number, otherwise it's False
        """
        pp = 2 * p + 1
        return _millerRabinTest(pp)

## Fibonacci sequence

## `fibonacci(n)`

This function generate the list of Fibonacci

Parameters:

- `n` (`integer`) –

  it's maximum value of Fibonacci sequence

Returns:

- `list` –

  Return a list of n element in the Fibonacci sequence

Source code in `Cryptotools/Numbers/numbers.py`

     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18

    def fibonacci(n) -> list:
        """
            This function generate the list of Fibonacci

            Args:
                n (integer): it's maximum value of Fibonacci sequence

            Returns:
                Return a list of n element in the Fibonacci sequence
        """
        fibo = [0, 1]
        fibo.append(fibo[0] + fibo[1])
        for i in range(2, n):
            fibo.append(fibo[i - 1] + fibo[i])
        return fibo

« Previous Next »