Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Primes, Primality Test,

Factorization & CRT


Chittaranjan Pradhan

Cryptography 7 Primes
Generation of Primes

Primes, Primality Test, Factorization Primality Testing


Deterministic algorithms

& CRT Probabilistic algorithms


Recommended Primality
Test

Factorization

Chinese Remainder
Theorem (CRT)

Chittaranjan Pradhan
School of Computer Engineering,
KIIT University
7.1
Primes, Primality Test,
Primes Factorization & CRT
Chittaranjan Pradhan
Primes

• A prime is divisible only by itself and 1 Primes


Generation of Primes
• Number 1 is relatively prime with any integer Primality Testing
Deterministic algorithms
Probabilistic algorithms

• Number of Primes: Recommended Primality


Test

[n/(ln n)] < π(n) < [n/(ln n - 1.08366)] Factorization

• Check whether the number n is divisible by the primes less Chinese Remainder
√ Theorem (CRT)

than n
• Ex: 97 is prime? 301 is prime?

1. Sieve of Eratosthenes

• Write all the numbers between 2 and n


• Check any number in the above range is divisible by the

primes less than n
• Cross out the numbers divisible by the above primes
• Remaining numbers are the primes
• Ex: Primes under 50 7.2
Primes, Primality Test,
Primes... Factorization & CRT
Chittaranjan Pradhan

Euler’s Phi-Function Primes


Generation of Primes

• Φ(1) = 0 Primality Testing


Deterministic algorithms

• Φ(p) = p-1, if p is prime Probabilistic algorithms


Recommended Primality
Test
• Φ(m x n) = Φ(m) x Φ(n), if m & n are relatively prime Factorization

• Φ(pe ) = pe - pe−1 , if p is prime Chinese Remainder


Theorem (CRT)

• We can combine the above four rules to find the value of


Φ(n)
• Φ(n) finds the number of integers that are both smaller
than n and relatively prime to n

• Φ(10), Φ(13)
• What is the number of elements in Z14 ∗

7.3
Primes, Primality Test,
Primes... Factorization & CRT
Chittaranjan Pradhan
Fermat’s Little Theorem

• If p is a prime number and a is an integer such that p Primes


Generation of Primes
doesn’t divide a, then Primality Testing
ap−1 ≡ 1 mod p Deterministic algorithms
Probabilistic algorithms

• If p is a prime number and a is an integer, then Recommended Primality


Test

ap ≡ a mod p Factorization

Chinese Remainder
Theorem (CRT)
• If the exponent and the modulus are not the same, with
substitution this can be solved
• Ex: 610 mod 11, 312 mod 11
Application of Fermat’s Little Theorem:
• Used to find multiplicative inverses quickly if the modulus
is a prime
• If p is a prime and a is an integer such that p doesn’t
divide a, then
a−1 mod p = ap−2 mod p
• Ex: 8−1 mod 17, 5−1 mod 23
7.4
Primes, Primality Test,
Primes... Factorization & CRT
Chittaranjan Pradhan

Euler’s Theorem Primes


Generation of Primes

• Generalization of Fermat’s Little theorem Primality Testing


Deterministic algorithms

• Here, the modulus is an integer Probabilistic algorithms


Recommended Primality
Test

Factorization
• If a & n are coprimes, aΦ(n) ≡ 1 (mod n) Chinese Remainder
Theorem (CRT)
• If a & n are not coprimes and if n = p x q,
ak ∗Φ(n)+1 ≡ a (mod n)

• Ex: 624 mod 35, 2062 mod 77


Application of Euler’s Theorem:
• Used to find multiplicative inverses modulo a composite
a−1 mod n = aΦ(n)−1 mod n
• Ex: 8−1 mod 77, 7−1 mod 15, 71−1 mod 100

7.5
Primes, Primality Test,
Generation of Primes Factorization & CRT
Chittaranjan Pradhan

Primes
Generation of Primes
Generation of Primes Primality Testing
Deterministic algorithms

• Mersenne Primes Probabilistic algorithms


Recommended Primality
Test

Mp = 2p - 1 Factorization

Chinese Remainder
Theorem (CRT)

Ex: p= 2, 3, 5, 7, 11

• Fermat Primes
n
Fn = 22 +1

Ex: n=1, 2, 3, 4, 5

7.6
Primes, Primality Test,
Primality Testing Factorization & CRT
Chittaranjan Pradhan

Primes
Generation of Primes

Primality Testing
Deterministic algorithms
Probabilistic algorithms
Recommended Primality
Primality Testing Test

Factorization

• Deterministic algorithms: Gives correct answer Chinese Remainder


Theorem (CRT)

• Probabilistic algorithms: Gives an answer that is correct


most of the times, but not always

7.7
Primes, Primality Test,
Deterministic algorithms Factorization & CRT
Chittaranjan Pradhan
Divisibility Algorithm
√ Primes
• All divisors smaller than n are used. If any of these Generation of Primes

numbers divides n, then n is composite Primality Testing


Deterministic algorithms
Probabilistic algorithms
Recommended Primality
Test

Factorization

Chinese Remainder
Theorem (CRT)

• The algorithm can be improved by testing only odd


numbers
7.8
Primes, Primality Test,
Deterministic algorithms... Factorization & CRT
Chittaranjan Pradhan

Primes
Divisibility Algorithm... Generation of Primes

Primality Testing
• It can be further improved by using a table of primes Deterministic algorithms
√ Probabilistic algorithms
between 2 & n Recommended Primality
Test

Factorization

• If each arithmetic operation uses only


√ one bit operation, Chinese Remainder
Theorem (CRT)
then the bit-operation complexity is 2nb = 2nb /2 , where nb
is the number of bits in n

• The complexity can be represented as O(2nb )

• This algorithm is infeasible (intractable) if nb is large

• Ex: nb = 200bits

7.9
Primes, Primality Test,
Deterministic algorithms... Factorization & CRT
Chittaranjan Pradhan
AKS (Agrawal - Kayal - Saxena) Algorithm

• Can be used to verify the primality of any general number Primes


Generation of Primes
given with time complexity O((log2 nb )12 ) Primality Testing
p
(x − a) ≡ (x p -a) (mod p) Deterministic algorithms
Probabilistic algorithms
Recommended Primality
Test

Factorization

Chinese Remainder
Theorem (CRT)

7.10
Primes, Primality Test,
Probabilistic algorithms Factorization & CRT
Chittaranjan Pradhan

1. Fermat Primality Test Primes


Generation of Primes

Primality Testing
• If p is a prime, then ap−1 ≡ 1 mod p Deterministic algorithms
Probabilistic algorithms
• Bit-operation complexity O(nb ) Recommended Primality
Test

Factorization

• Ex: 5, 561 Chinese Remainder


Theorem (CRT)

2. Square Root Test



• If n is a prime, 1 mod n = ±1

• If n is a composite, 1 mod n = ±1 and possibly other
values

• Ex: 7, 8, 17, 22

7.11
Primes, Primality Test,
Probabilistic algorithms... Factorization & CRT
Chittaranjan Pradhan

Primes
3. Miller - Rabin Test Generation of Primes

Primality Testing
• Combines the Fermat test and Square root test Deterministic algorithms
Probabilistic algorithms

• n-1 is written as the product of an odd number m & a Recommended Primality


Test

power of 2 Factorization

n-1=m x 2k Chinese Remainder


Theorem (CRT)

• Run time complexity of O((logn)3 )

7.12
Primes, Primality Test,
Probabilistic algorithms... Factorization & CRT
Chittaranjan Pradhan

Miller - Rabin Test... Primes


Generation of Primes

Primality Testing
Deterministic algorithms
Probabilistic algorithms
Recommended Primality
Test

Factorization

Chinese Remainder
Theorem (CRT)

• Ex: 561, 27, 61

7.13
Primes, Primality Test,
Recommended Primality Test Factorization & CRT
Chittaranjan Pradhan

Recommended Primality Test Primes


Generation of Primes
Most popular primality test is a combination of the divisibility
Primality Testing
test and the Miller - Rabin test Deterministic algorithms
Probabilistic algorithms
• Choose an odd integer Recommended Primality
Test

• Do some trivial divisibility tests on some known primes Factorization

such as 3, 5, 7, 11, 13 ... Chinese Remainder


Theorem (CRT)
• If the number passes all of these tests, go to next step
• else, go back to step 1 and choose another odd number
• Choose a set of bases for testing. A large set of bases is
preferable
• Do Miller - Rabin tests on each of the bases
• If any of them fails, go back to step 1 and choose another
odd number
• If the test passes for all bases, number is prime

Ex: 4033

7.14
Primes, Primality Test,
Factorization Factorization & CRT
Chittaranjan Pradhan

Factorization Primes
Generation of Primes

• Any positive integer can be written as Primality Testing

n = p1 e1 x p2 e2 x ... x pk ek
Deterministic algorithms
Probabilistic algorithms
Recommended Primality
Test

• GCD (Greatest Common Divisor): Factorization

a = p1a1 xp2a2 x...xpk ak Chinese Remainder


Theorem (CRT)

b = p1b1 xp2b2 x...xpk bk

gcd(a, b) = p1min(a1,b1) xp2min(a2,b2) x...xpk min(ak ,bk )

• LCM (Least Common Multiplier):


a = p1a1 xp2a2 x...xpk ak
b = p1b1 xp2b2 x...xpk bk

lcm(a, b) = p1max(a1,b1) xp2max(a2,b2) x...xpk max(ak ,bk )

7.15
Primes, Primality Test,
Factorization... Factorization & CRT
Chittaranjan Pradhan
1. Trial Division Method

• Trial division can be attempted by all primes up to n Primes
Generation of Primes
• This method is good if n < 210 , but it is inefficient and Primality Testing

infeasible for factoring large integers Deterministic algorithms


Probabilistic algorithms

• Ex: 1233 Recommended Primality


Test

Factorization

Chinese Remainder
Theorem (CRT)

7.16
Primes, Primality Test,
Factorization... Factorization & CRT
Chittaranjan Pradhan
2. Fermat Method

• It divides a number n into two positive numbers a & b so Primes


Generation of Primes
that n = a x b Primality Testing

• n = x 2 - y 2 = a x b with a = (x + y) and b = (x - y) Deterministic algorithms


Probabilistic algorithms
Recommended Primality
• It tries to find two integers a and b close to each other Test

Factorization

Chinese Remainder
Theorem (CRT)

7.17
Primes, Primality Test,
Factorization... Factorization & CRT
Chittaranjan Pradhan
3. Pollard’s p-1 Method

• Pollard’s p-1 factoring algorithm is a special-purpose Primes


Generation of Primes
factoring algorithm that can be used to efficiently find any Primality Testing
prime factors p of a composite integer n for which p - 1 is Deterministic algorithms
Probabilistic algorithms
smooth with respect to some relatively small bound B Recommended Primality
Test

• Let B be a positive integer. An integer n is said to be Factorization

B-smooth, or smooth with respect to a bound B, if all its Chinese Remainder


Theorem (CRT)
prime factors are ≤ B. Ex: 57247159 with B=8

7.18
Primes, Primality Test,
Factorization... Factorization & CRT
Chittaranjan Pradhan
4. Pollard rho Method

• Choose x1, a small random integer called, seed Primes


Generation of Primes
• Use a function to calculate x2 such that n doesn’t divide Primality Testing
x1-x2 Deterministic algorithms
Probabilistic algorithms

• Calculate gcd(x1-x2,n): Recommended Primality


Test

• If it isn’t 1, the result is a factor of n; stop Factorization

• If it is 1, return to step 1 and repeat the process with x2 Chinese Remainder


Theorem (CRT)

7.19
Primes, Primality Test,
Chinese Remainder Theorem (CRT) Factorization & CRT
Chittaranjan Pradhan
Chinese Remainder Theorem (CRT)

• The CRT is used to solve a set of congruent equations Primes


Generation of Primes

with one variable but different moduli, which are relatively Primality Testing

prime Deterministic algorithms


Probabilistic algorithms

x ≡ a1 (mod m1 ) Recommended Primality


Test

x ≡ a2 (mod m2 ) Factorization

... Chinese Remainder


Theorem (CRT)
x ≡ ak (mod mk )

• CRT states that these equations have a unique solution if


the moduli are relatively prime:
• Find M = m1 x m2 x ... x mk
• Find M1 = M/m1 , M2 = M/m2 , ... , Mk = M/mk
• Find the multiplicative inverse of M1 , M2 , ..., Mk using the
corresponding moduli (m1 , m2 , ..., mk ). Let they are M1 −1 ,
M2 −1 , ..., Mk −1
• The solution to the simultaneous equations is:
x = (a1 x M1 x M1 −1 + a2 x M2 x M2 −1 + ... + ak x Mk x
Mk −1 ) mod M
7.20
Primes, Primality Test,
CRT... Factorization & CRT
Chittaranjan Pradhan

Primes
Generation of Primes

Primality Testing
Deterministic algorithms

CRT... Probabilistic algorithms


Recommended Primality
Test

• x ≡ 2 mod 3 Factorization

x ≡ 3 mod 5 Chinese Remainder


Theorem (CRT)
x ≡ 2 mod 7

• Find an integer that has a remainder of 3 when divided by


7 and 13, but is divisible by 12

7.21

You might also like