Advanced Algorithms Unit 4 PP
Advanced Algorithms Unit 4 PP
1
1. Elementary Number-theoretic Notions :
2,3,5,7,11
39 is a composite no. since it divides by 3.
1 is called unit & is neither prime nor composite.
Similarly 0 and all –ve nos. are neither prime
nor composite.
3
TH-4.1 : Division Theorem
“ For any integer ‘a’ and any positive integer ‘n’,
there exists unique integers ‘q’ and ‘r’ such that
0 ≤ r < n and a = qn + r”.
The value q = a/n is the quotient of the division.
The value r = a mod n is the remainder of the division.
Here n | a ( n divides a), if and only if a mod n = 0.
4
c) Common Divisors & Greatest CD :
If ‘d’ is a divisor of ‘a’
and ‘d’ is also divisor of ‘b’
then ‘d’ is a common divisor of ‘a’ and ‘b’.
Note : a) ‘1’ is a common divisor of any two integers.
b) If a | b and b | a then a= b
Important Property :
If d | a and d | b then d | (a + b) & d | (a – b)
If d | a and d | b then d | (ax + by)
if p | ab then p | a or p | b or both.
Unique Factorization :
There is exactly one way to write any composite
integer ‘a’ as a product of the form
a = p1^ e1. p2^ e2. p3^ e3…… pr^ er
where all pi are prime, p1<p2<..<pr and ei are +ve
7
d) Common Divisors & Greatest CD :
Let there are two positive integers ‘a’ and ‘b’
a = p1^ e1. p2^ e2. p3^ e3…… pr^ er
Proof : case-1 :
Let d = gcd (a,b) d | a & d | b
Here, a mod b = a – q b where q = a / b
Since, a mod b is a linear combination of ‘a’ and ‘b’,
we can say that d | (a mod b).
So, d | b and d | (a mod b)
d | gcd (b, a mod b)
gcd (a,b) | gcd (b, a mod b) ….(1)
9
Case-2 : Let d = gcd (b, a mod b).
d | b & d | (a mod b)
Since, a = q b + (a mod b) where q = a / b
we have that a is a linear combination of ‘b’ and
‘a mod b’ d | a
11
2 b) Extended Euclid’s Algorithm :
In this algorithm we find additional information
like the values of ‘x’ and ‘y’, where
d = gcd (a,b) = ax + by
EXTENDED-EUCLID(a,b)
1 If b = = 0
2 return (a,1,0)
3 else (d’, x’, y’) = EXTENDED-EUCLID(b, a mod b)
4 (d,x,y) = (d’, y’, x’ - a / b y’)
5 return (d, x, y)
12
In the above algorithm,
d = ax + b y
d’ = bx’ + (a mod b) y’
13
Step-1 : a = 99 b =78
a / b = 1 d = gcd(99,78) = 3
Here, a = 99 = 1. 78 + 21
78 = 3. 21 + 15
21 = 1. 15 + 6
15 = 2.6 + 3
6 = 2. 3 + 0
And 3 = 15 - 2 . 6 = 15 – 2 (21 – 1. 15)
= 3.15 - 2.21 = 3(78 – 3.21) – 2.21
= 3. 78 – 11. 21 = 3. 78 – 11(99 – 1.78)
= 3.78 - 11.99 + 11.78 = -11.99 + 14.78
3 = gcd(99,78) = -11.99 + 14. 78 …(1)
14
Step-2 : a = 78 b = 21
a / b = 3 d = gcd(78,21) = 3
Here, a = 78 = 3. 21 + 15
21 = 1. 15 + 6
15 = 2.6 + 3
6 = 2. 3 + 0
15
Step-3 : a = 21 b = 15
a / b = 1 d = gcd(21,15) = 3
Here, a = 21 = 1. 15 + 6
15 = 2.6 + 3
6 = 2. 3 + 0
16
Step-4 : a = 15 b=6
a / b = 2 d = gcd(15,6) = 3
Here, a = 15 = 2.6 + 3
6 = 2. 3 + 0
And 3 = 15 - 2 . 6
So, x=1 y = -2
17
Step-5 : a=6 b=3
a / b = 2 d = gcd(6,3) = 3
Here, a = 6 = 2. 3 + 0
And 3 = 0.6 + 1.3
So, x=0 y=1
3 = gcd(6,3) = 0.6 + 1. 3 …(5)
a b a / b d x y
99 78 1 3 -11 14
78 21 3 3 3 -11
21 15 1 3 -2 3
15 6 2 3 1 -2
6 3 2 3 0 1
3 0 -- 3 1 0
19
3. Modular Arithmetic :
a) Group : A group (S,) is a set S together with
binary operation defined on S for which
the following properties hold :
21
Multiplicative Inverse : The factor b-1 is the
‘multiplicative inverse’ of b in GF(p).
b b-1 mod p = 1
Ex-9 : Find the multiplicative inverses of the
following, where p = 7.
1 2 3 4 5 6
Answer : 1 4 5 2 3 6
Answer : 1 6 4 3 9 2 8 7 5 10
22
Ex-11 :
Let the moduli be p1 = 3, p2 = 5, p3 = 7
Let us consider the integers : 10, 15
24
Basis for Chinese Remainder Theorem :
Ex-13 : Find the lowest integer x such that it
leaves remainders 2, 3 and 2 when divided by
3, 5 and 7.
The Answer : 23
The answer is : 49
27
5. Powers of an Element :
Consider the sequence of powers of ‘a’, modulo n
where a ∊ Zn*. For example,
i 0 1 2 3 4 5 6 7 8 9
3i mod 7 1 3 2 6 4 5 1 3 2 6
i 0 1 2 3 4 5 6 7 8 9
2i mod 7 1 2 4 1 2 4 1 2 4 1
28
6 (a) Euler’s Theorem :
For any integer n > 1
a ^ (n) 1 (mod n) for all a ∊ Zn*
29
7. RSA Cryptosystem :
In RSA Cryptosystem, the public and private
keys are generated as follows :
a) Select at random two large prime numbers
p and q such that p ≠ q.
b) Compute n = pq
c) Select a small odd integer ‘e’ that is relatively
prime to p-1 and q-1. (public exponent)
d) Compute the integer ‘d’ (private exponent)
from e, p and q such that de ≡ 1 mod L, where
L = LCM [ (p-1), (q-1) ]
30
e) Publish P = (e,n) RSA Public Key
Secret S = (d,n) RSA Secret Key
Here n = pq = 55 (n) = 40
and d : ed ≡ 1 mod L L = 20
So, d = 7
31
Let A = Message(m) B = m2 mod n
C = m3 mod n (encrypted message)
D = c2 mod n E = c3 mod n F = c6 mod n
G = c7 mod n (decrypted message)
A B C D E H G
0 0 0 0 0 0 0
1 1 1 1 1 1 1
2 4 8 9 17 14 2
3 9 27 14 48 49 3
4 16 9 26 14 31 4
32
A B C D E H G
5 25 15 5 20 15 5
6 36 51 16 46 26 6
7 49 13 4 52 9 7
8 9 17 14 18 49 8
9 26 14 31 49 36 9
34
The Procedure MILLER-RABIN is a probabilistic
search for a proof that n is composite.
b) MILLER-RABIN (n,s)
for j = 1 to s
a = RANDOM(1, n-1)
if WITNESS (a,n)
return COMPOSITE
return PRIME
35
c) WITNESS(a,n)
1. Let t and u be such that t ≥ 1.
u is odd, and n-1 = 2t u
2. x0 = MODULAR-EXPONENTIATION(a,u,n)
3. for i = 1 to t
4. xi = x2i-1 mod n
5. if ( xi = = 1) and ( xi-1 ≠ 1) and ( xi-1 ≠ n-1)
6. return TRUE
7. if xt ≠ 1
8. return TRUE
9. return FALSE
36
d) MODULAR-EXPONENTIATION (a, b, n)
1. c = 0
2. d = 1
3. Let (bk , bk-1 , …..,b1 , b0 )
4. for i = k downto 0
5. c = 2c
6. d = (d.d) mod n
7. if bi = = 1
8. c=c+1
9. d = (d.a) mod n
10. return d
37
Ex-18 : Let ‘n’ be a carmichael number. n = 561
So, here n – 1 = 560
If n – 1 is written in the form of n-1 = 2t u, then
t=4 and u = 35
Let the value of ‘a’ is chosen from the
algorithm as : 7
From the WITNESS algorithm,
find the value of x0.
Here, d = ac mod n (c = b = u)
From above, the value of ‘d’ returned is : 241
Here, x0 ≡ a35 ≡ 241 (mod 561).
Note : 561 = 3 . 11 . 17
40
9. Integer Factorization :
This is the process of integer factorization into a
product of primes.
POLLARD-RHO(n)
1. i=1
2. x1 = RANDOM(0, n-1)
3. y = x1
4. k=2
41
5. While TRUE
6. i = i+1
7. xi = (x2i-1 - 1 ) mod n
8. d = gcd(y – xi , n)
9. if ( d 1) and (d n)
10. print d
11. if ( i = = k)
12. y = xi
13. k = 2k
42
Ex-19 : Pollard’s Rho Heuristic
Let n = 1387
So, Initialization :
i=1 x1 = 2 y=2 k=2
WHILE : STEP-1 :
i= 2
xi = (x2i-1 - 1 ) mod n x2 = 3
d = gcd(y – xi , n) d=1
if [ (d 1) and ( d n) ] FALSE
if ( i = =k) TRUE
y=3 k=4
43
STEP-2 : i= 3
xi = (x2i-1 - 1 ) mod n x3 = 8
d = gcd(y – xi , n) d=1
if [ (d 1) and ( d n) ] FALSE
if ( i = =k) FALSE
STEP-3 : i= 4
xi = (x2i-1 - 1 ) mod n x4 = 63
d = gcd(y – xi , n) d=1
if [ (d 1) and ( d n) ] FALSE
if ( i = =k) TRUE y = 63 k = 8
44
STEP-4 : i= 5
xi = (x2i-1 - 1 ) mod n x5 = 1194
d = gcd(y – xi , n) d=1
if [ (d 1) and ( d n) ] FALSE
if ( i = = k) FALSE
STEP-5 : i= 6
xi = (x2i-1 - 1 ) mod n x6 = 1186
d = gcd(y – xi , n) d=1
if [ (d 1) and ( d n) ] FALSE
if ( i = = k) FALSE
45
STEP-6 : i= 7
xi = (x2i-1 - 1 ) mod n x7 = 177
d = gcd(y – xi , n) d = 19
if [ (d 1) and ( d n) ] TRUE
Print d = 19
if ( i = = k) FALSE
46
310
996
396
814
84
x7 177
x6 1186 120
x4 63 595 1053
x3 8
x2 3
47
x 2