Primality Testing
ian-a-frankel
Posted on November 29, 2023
Number Theory Basics
Most algorithms in cryptography depend on large prime numbers, and the fact that certain algorithms are hard. Encryption is basically the act of applying a mathematical function for which it is very hard to determine the inverse function without a key, but easy with the key.
There are many number theoretic algorithms which are fast and easy. The mother of them all number theoretic algorithms is Euclid's algorithm for finding the greatest common divisor:
Given that we can do division with remainder, if and we want to compute GCD(a,b) we write
and notice Now we can iterate this. This is very fast; in fact its time complexity is !
If and are really large and hard to factor, we can compute their GCD extremely quickly without knowing how to factor them! In fact there is no known way to efficiently determine the prime factorization of a number - if I multiply two random 300-digit primes and it's impractical to find and if you only know the value of although it is pretty easy to detect that is not prime. You could write code to do it, but your computer would not last long enough to execute it.
Powers modulo
If is an integer, we write if and have the same remainder when we divide both by
If is a prime number, we can define multiplication on
How do we actually compute efficiently when are all large?
The answer is repeated squaring. We can write
as a sum of powers of
, and calculate those powers of , i.e. if
So if we want to compute we actually only have to calculate the powers of whose exponents belong to
Logarithms modulo
Another well known fact in number theory: for any prime number you can find a remainder such that all of the different powers are different mod ; in this case we say that is a primitive root mod
We can get at most different remainders, since no power of will give a remainder . Most values of will not be primitive roots, but in practice it usually not that hard to find one such value fairly quickly.) Values of with this property are called primitive roots. For example, the primitive roots mod are and .
powers of 1: 1,1,1,1 (not all distinct mod 5)
powers of 2: 2,4,8,16 (all distinct mod 5)
powers of 3: 3,9,27,81 (all distinct mod 5)
powers of 4: 4,16,64,256 (not all distinct mod 5)
For a = 1,2,3,4 we have
In fact, if
is a large odd number and
and
then
is almost certainly prime, although there are a small number of "pseudoprimes" that pass this test which are not prime.
However, we can detect these pseudoprimes quickly with high probability! And we will want to do this, because even a failure rate of 1 in a million is enough to cause a lot of problems for a company like Google or American Express.
The Miller-Rabin Primality Test
However, if is a large pseudoprime that managed to sneak past the Fermat test, there are two cases that we might have to worry about here. The first is that the pseudoprime p could be a power of a prime, say p = where is prime.
Then is already extremely unlikely to escape the Fermat test - a random guess will only pass with probability 1 in A relatively small number of Fermat tests with randomly chosen values of will detect that is not prime with an acceptably low failure rate.
For any true prime and primitive root we know if is odd, and 1 if is even, and 1 and are the only two square roots it has.
The other case is when where are odd and greater than 1 but and have no prime factors. We can take advantage of their existence without being able to find and
Anything that is 1 (mod ) and (mod ) will be another square root in of 1 in mod arithmetic, which will be different from 1 and
Now, the key idea here is that if I pick a random integer from 0 to the remainders mod v and mod w are independent and uniformly distributed. What we do here is decorate the Fermat test a little bit.
First, find the unique odd number d for which we can write
Now, we will do Fermat tests, with a random number as before, but keep track of sightly more information. In what follows, all calculations are to be reduced modulo
1.) Let
2.) Calculate
If the last result isn't 1, you have failed the Fermat test and you know the result is composite. However, even if you did not, you can look at the last power of m in the list for which the value was different from 1. If it's not then you have failed the Rabin-Miller primality test.
There's an entirely elementary proof that the Rabin-Miller test fails with probability at least 1/2 when is not prime.
Sharing Secrets using Primes
We can think about logarithms as the inverse of exponentiation, similarly to how we do for positive real numbers. log( ) ( with base ) is the power for which =
The key fact here is that when is a large prime number, the "logarithm" operation is computationally intractable.
Given
and
it's easy to find
However, if we know the values of
and
in general it is quite hard to find
such that
So here's a quick way for two parties to exploit this fact and pass information while maintaining their own privacy:
Alice and Bob have a shared prime number and primitive root (In fact doesn't need to be a primitive root, but needs to just have many distinct powers.)
Each has a private key, say for Alice and for Bob (they named them after their sex chromosomes).
Alice tells Bob and Bob tells Alice
The values of and are secure (difficult for either to steal from the other or for an eavesdropper to intercept), but they can both access the value of to decrypt any information that needs to be available to both of them.
In fact the standard cryptographic system is a small variation on this: Instead of a large prime, we choose a product of two large primes in place of and a number like in place of , and those two pieces of infomration combined are Alice's public key. This has the additional layer of security that the exponent we must raise a number to to get 1 (mod ) is hard to compute.
Posted on November 29, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 29, 2024