Changes for page Programming knowledge
Last modified by Alexandru Pentilescu on 2025/07/09 14:25
From version 3.1
edited by Alexandru Pentilescu
on 2025/05/12 16:08
on 2025/05/12 16:08
Change comment:
There is no comment for this version
To version 5.1
edited by Alexandru Pentilescu
on 2025/07/09 14:20
on 2025/07/09 14:20
Change comment:
There is no comment for this version
Summary
-
Page properties (1 modified, 0 added, 0 removed)
Details
- Page properties
-
- Content
-
... ... @@ -28,7 +28,7 @@ 28 28 29 29 {{code language="python"}} 30 30 def a_to_the_b_mod_n(a, b, n): 31 - if b = 0: 31 + if b == 0: 32 32 return 1 33 33 if is_even(b): 34 34 return a_to_the_b_mod_n((a * a) % n, b / 2, n) ... ... @@ -35,4 +35,84 @@ 35 35 return (a * a_to_the_b_mod_n(a, b - 1, n)) % n 36 36 {{/code}} 37 37 38 += Generating large random prime numbers = 39 +One of the most powerful features of modern computers is their ability to generate relatively large prime numbers very quickly. To do this, they usually employ the following algorithm: 40 + 41 +{{code language="python"}} 42 +def generate_n_digits_in_length_prime(n): 43 + num = generate_n_digits_in_length_number(n) 44 + while is_prime(num) == False 45 + num = generate_n_digits_in_length_number(n) 46 + 47 +{{/code}} 48 + 49 +The is_prime function should be an implementation of the Miller-Rabin test, which is the industry standard for testing the primality of large numbers. 50 + 51 +== The Miller-Rabin primality test == 52 +The Miller-Rabin primality test is an extension of the Miller primality test. Specifically it uses a relatively small number of randomly chosen odd integers that are smaller than N (where N is the number being tested for primality), usually N. If and only if all these tests pass (i.e. they claim that N is prime), then we're to assume that N is prime. The probability of a large number N passing the primality test despite not being prime, when 10 different numbers are randomly chosen to be used, is at most 0.00000095367431640625 (i.e (1/4)^^10^^). 53 + 54 +== The Miller primality test == 55 +The Miller primality test is an extension of Fermat's Little Theorem. 56 + 57 +Specifically, it says that to test a large number N for primality, we are to follow the following steps: 58 + 59 +1. Choose a number a smaller than N. This number is a 60 +1. Compute a^^N^^ 61 +1. Divide the result from the above step by N and check the remainder of that division (i.e. do modulus division). If the remainder is not 1 then the number is composite (i.e. not prime) and stop. If it is 1 but N is odd, it is composite. If it is 1 but N is even, continue to the next step. Keep note of the remainder of this division (i.e. the 1) for later 62 +1. Divide N by 2. This new number is N' 63 +1. Compute again a^^N'^^ and then do modulus division against N. Keep note of the remainder again for later. This remainder is to be considered BEFORE in the sequence that we're keeping note of, compared to the 1 calculated in step 3. 64 +1. Check if N' is odd. If it is, stop. If not, repeat from step 4 onwards, with N' being the new N. 65 + 66 +Once the algorithm above is followed, you should have a sequence of numbers that you kept note of. This sequence will determine whether N is indeed prime. 67 + 68 +The sequence needs to end in 1. If it does not, N is composite. 69 +Any number in the sequence before 1 has to be another 1 or N-1. If it is, N is likely prime (although there are false positives!). 70 + 71 += Diffie Hellman key exchange = 72 +Diffie Hellman key exchange is a key exchange algorithm that relies on both parties sharing information with each other, in such a way that they end up calculating the exact same number, without said number becoming publicly available, even if the connection is being eavesdropped on the entire time. 73 + 74 +The algorithm relies on mathematical principles that boil down to number theory. Let Alice and Bob be two parties that wish to negociate a secret key with each other over an insecure channel and Eve, an eavesdropper on said channel, is recording all of their communications with each other. 75 + 76 +To use Diffie Hellman, Alice and Bob must first agree on two public numbers: a prime number P and a generator number G which is a primitive root of P (P being prime and G being a primitive root of P are very important properties). 77 + 78 +A number g is a primitive root of N if, for every number a coprime number to N (i.e. any number that doesn't share a common divisor with N) is congruent to a power of g modulo N. That is, g is a primitive root modulo N if for every integer a coprime to N, there is some integer k for which g^^k^^ ≡ a (mod N) 79 + 80 +== Examples of primitive roots modulo N == 81 +1. For N=2 we have generator 1. 82 +1. N=3 we have 2. 83 +1. N=5 we have 2, 3 84 +1. N=7 we have 3, 5 85 +1. N=11 we have 2, 6, 7, 8 86 +1. N=13 we have 2, 6, 7, 11 87 +1. N=17 we have 3, 5, 6, 7, 10, 11, 12, 14 88 +1. N=19 we have 2, 3, 10, 13, 14, 15 89 +1. N=23 we have 5, 7, 10, 11, 14, 15, 17, 19, 20, 21 90 +1. N=29 we have 2, 3, 8, 10, 11, 14, 15, 18, 19, 21, 26, 27 91 +1. N=31 we have 3, 11, 12, 13, 17, 21, 22, 24 92 + 93 +== Actual algorithm == 94 +After picking a prime P and a number G primitive root modulo P (from the table above), Alice picks a random starting number that she keeps private to herself named secret,,Alice,, . Let's assume she picks 23. Bob will do the same and pick a secret number secret,,Bob,, which will be 14. Neither Bob nor Alice will share their secret numbers with each other. 95 + 96 +For the sake of this example, we shall assume that Alice and Bob chose to use N=17 and G=6 (both N and G are public at all times). 97 +Alice will calculate 6^^23^^ modulo 17=789730223053602816 modulo 17=14 98 +Alice will send this number 14 to Bob (public information). 99 + 100 +Bob will do the same and calculate 6^^14^^ modulo 17=78364164096 modulo 17=9. 101 +Bob will send Alice the number 9. 102 + 103 +Alice will then calculate 9^^23^^ modulo 17=8862938119652501095929 modulo 17 = 2 104 + 105 +Bob will then calculate 14(public information)^^14 (Bob's secret number)^^ modulo 17=11112006825558016 modulo 17=2 106 +2 is the secret that both Bob and Alice will end up calculating. Eve has no way, just by knowing the N=17, G=6 and the 14 and 9 numbers sent over the insecure channel, how to calculate this number. 107 + 108 +Since the number 2 was never sent over the encrypted channel directly, Eve will not know it. 109 + 110 +For security's sake, it is recommended that N be at least 2048 bits long. 111 + 112 += RSA Algorithm = 113 +RSA is a public key algorithm that relies on using the principles of number theory to encrypt messages in such a way that only a specific person, owning a specific private key, can decrypt said message. 114 + 115 +Let's assume Alice wishes to send Bob a message that only Bob knows how to decrypt. In order to do so, Bob must have previously made public his personal public RSA key, which consists of two numbers: a large modulus N and another number e such that 116 + 117 +(m^^e^^)^^d^^≡ m (mod n). 38 38 )))