Primality Testing
Primality Testing
475165978210434043461497548213136512482950118098973052348824
176048498914597780570549672540174022839510190045680432456992
360961741935519563208889600380907557279000822004062650176806
284358846194689505641704548464324538623804572446099692473066
CA622
157515673381726666633698375084863037682864295501473972970546
Advanced Algorithms
880367754363056730790520422148005319506863367773492032373234
979755179939457158552789684329197941772851810182842681740336
100029255731681289331823627949676009115110034499369615725541
459543271828652075025578371053587319419135908910949668716622
481365685909900638145321990221870700973968193449377389864675
208236432308143359655216424196454913078588531433716410752651
980393977586783131755384278266383136594922300104562405089876
532059199931283194274471068805866130485633954777204668750834
288991788915951948868404508603964359998785038433491385254956
292134951936862937763180945054362165593724089126952540435970
835869567728425525144686610083797438341830313574080644760791
569041303292947198922921205137208143838254993897891691570159
Course Overview
4 Parts: 6 Lectures on each part
Part I
Primality Testing
Factorisation
Implications to Cryptography
Part II,III and IV will be taught by another lecturer
Continuous Assessment
25%
Groups of 5
Will be related to part I of the course
Course Overview
Exam:
8 exam questions, answer 4, one from each
part of the course
Therefore there will be two questions on the
exam relating to the next 6 hours of lectures
Course Website
Notes for part I can be found at
www.computing.dcu.ie/~cwhelan/teaching.html
11364600
Testing Primality?
Obvious approach:
Given an integer n, check if any integer m from 2 to
n-1 divides n. If n is divisible by any m, then n is
composite, otherwise it is prime.
An improvement:
Check whether n is divisible by any of the prime
numbers n
Also can skip all even m except 2, since if any even
number divides n then 2 does
Can repeat this for all prime numbers (Sieve of
Eratosthenes)
Eratosthenes
Sieve of Erratosthenes
pic
Primality Testing
Two varieties of prime tests
1. Probablistic
“Probable prime”: a false positive, a composite number is identified
as prime. This will happen with very low probablity.
But much faster than deterministic tests
Pseudoprime: A number which passes a probablistic primality test
but which is actually composite
Types:
Fermat’s Little Theorem
Miller-Rabin
2. Deterministic
Identifes real prime numbers
Types:
Lucas-Lehmer
Elliptic Curve Primality Proving
AKS
Probabilistic Algorithms
Fermat’s Little Theorem
If p is prime and 1 < a < p, then
a p-1 = 1 mod p
To test if n is prime, a number of random a’s are chosen in
the interval, and see if the equality holds for each value of
a.
a n-1 = 1 mod n
However, some composites pass Fermat’s test, and so are
falsely identified as prime….Carmichael Numbers
Carmichael numbers are numbers that for all values of a
for which gcd(a,n) = 1 are Fermat liars.
Modular Exponentiation
How to calculate ak mod n?
Set b = 1
If k = 0, return b
Set A = a
If k0 = 1 then set b = a
For i = 1 to t
Set A = A2 mod n
if ki is 1
Set b = A*b mod n
Output ak mod n
Modular Exponentiation:
Square and Multiply
Example: Calculate 5596 mod 1234= 1013
i 0 1 2 3 4 5 6 7 8 9
ki 0 0 1 0 1 0 1 0 0 1
A 5 25 625 681 1011 369 421 779 947 925
b 1 1 625 625 67 67 1059 1059 1059 1013
Miller-Rabin
Similar to Fermat in that it tests a series of
equalities that old true for prime values. Then
see whether they hold for a number we want to
test.
First a note about x 2 = 1 mod p where x in Fp
x 2 = 1 mod p and so x = sqrt(1) mod p and so x =
+-1
However, (x+1)(x-1) = 0 mod p and so x can neither
be + or – 1.
Therefore, if a prime cannot divide either of these two
integers, it cannot divide their product.
Miller-Rabin
Let n be an odd prime. We can write n -1
as 2sr, where s is an interget and r is odd.
For i to t do
Choose a random integer a, 2 < a < n – 2
Compute y = ar mod n
If y !=1 and y != n – 1 then
j=1
While j s – 1 and y != n – 1
Compute y = y2 mod n
if y = 1 then return COMPOSITE
j = j+1
if y != n-1 then return COMPOSITE
Output PRIME
What is required to compute this?
Algorithm for modular exponentiation
Deterministic Algorithms
Special Primes
Generally deterministic algorithms are
used to test prime numbers with a special
form, such as
Mersenne Primes
A prime of the form 2s - 1
Solinas Primes
A prime number with low hamming weight
Particularly of interest for pairing-based
cryptography
Lucas-Lehmer test for Mersenne
Primes
Input: a Mersenne number n = 2s –1 for s >3
Use trial division to check whether s has any factors between 2 and s
Return COMPOSITE
Set u = 4
For k=1 to s-2
Compute u = (u2 – 2) mod n
If u = 0 Return PRIME
Else Return COMPOSITE
Mersenne Primes
Index Mj Digits Index Mj Digits
1 2 1 13 521 157
2 3 1 14 607 183
3 5 2 15 1279 386
4 7 3 16 2203 664
5 13 4 17 2281 687
6 17 6 18 3217 969
7 19 6 19 4253 1281
8 31 10 20 4423 1332
9 61 19 21 9689 2917
10 89 27 22 9941 2993
11 107 33 23 11213 3376
12 127 39 24 19937 6002
Elliptic Curve Primality Proving
2004
4769 digit number was certified as prime
in approx. 2000 hours of computation ~ 3
months of uninterupted computing time
on a 1GHz processor
AKS
The first polynomial time primality test
In August 2002 a deterministic polynomial
time primality test was written by Agrawal,
Kayal & Saxena, namely the AKS algorithm
Module 1:
Detecting Perfect Powers
Module 2:
Finding a suitable r value
Module 3:
Evaluating a prime identity function
What is a Perfect Power?
n is a perfect power if n
can be written as: n = ab
For example,
65536 = 216
Therefore, need an algorithm to determine whether n
can be written as an integer to the power of another
integer.
A brute force approach is obviously not feasible.
A method by Dan Bernstein gives an optimised
approach
Ref: “Detecting Perfect Powers in Essentially Linear
Time”, Mathematics of Computation,1998.
Bernsteins Detecting Perfect Powers
Only prime exponents are checked.
If n = ab, then n = (am)p = xp
While b!=0
set r = a mod b, a = b, b = r
Output a
Example
Find gcd(4864,3458)
Therefore gcd(4864,3458) = 38
Square Root
Firstly, use the Legendre Symbol to determine
whether a square root exists or not
x = y^2
Is x a quadratic residue or a quadratic non-residue?
x ( n 1) / 2 1 x is a QR
x mod n
n 1 x is a QNR
for a = 1….bound
(x – a)n = xn – a mod (n, xr-1)