L3 Math2
L3 Math2
Math (part 2)
GCD (Greatest Common Divisor)
› GCD of two numbers is the largest number that divides
both
› Brute Force: start with smaller out of the two downto 1
› Not efficient
› Efficiency: use Euclidean’s Algo
› If we subtract the smaller number from the larger, GCD
does not change.
› For more efficiency: divide a number by other and take
remainder. Repeat until remainder is zero.
› gcd(m,n) : if n== 0 return m;
– return gcd(n, m%n);
› Mathematically provable that its complexity is
O(log K) where K = m+n;
1/22/2025 2
Extended Euclidian Algo (GCD based)
› Given a,b, find coeff x and y such that ax+by = GCD
(a,b); x & y must be integer
› e.g. a=30, b=20; 30*1+20*-1 = 10
› a=35, b=15; 35*1+15*-2 = 5
› Brute Force: Find GCD of a & b, now try with one
value of x (say 1), and compute value of y, if feasible;
otherwise try with next value of x;
› Not efficient
› Efficiency: use Extended Euclidean’s Algo (EEA)
› EEA recursively finds GCD & then in rev way updates
x & y as
– if (a= = 0 ) x = 0, y = 1; //a*0+b*1=GCD(a,b)
– else find GCD recursively & x= y1- floor(b/a)
*x1; y = x1;
1/22/2025 3
Extended Euclidian Algo (GCD based)
› Try to code the EEA, we will discuss the solution in 5
minutes.
int GCDextended(int a, int b, _________)
{ if (a = = 0)
{_____________________}
gcd = GCDextended(b%a, a, &x, &y);
//Is it OK to use x and y in the call? How x1 &
x= ____; y=____ ; return _________; }
1/22/2025 4
Extended Euclidian Algo (GCD based)
int GCDextended(int a, int b, int *x, int *y)
{ if (a==0)
{ *x=0; *y = 1; return b;}
gcd = GCDextended(b%a, a, &x, &y); //x, y vs x1, y1??
*x= y1 –(b/a)*x1; *y=x1; return gcd; }
// how x1 & y1 will get their value??
› EEA is very useful when a & b are co-prime (having gcd as 1)
› Computation of x & y is an essential step in RSA key
encryption algo
› X is modular multiplicative inverse of a modulo b & y is
modular multiplicative inverse of b modulo a
› Must be thinking why should we know this?
1/22/2025 5
Modular Multiplicative Inverse (MMI)
› Given integers a and m, find MMI x
› MMI means x such that (a*x) mod m = 1
› a=3, m=11 :- x must be 4, a=10, m=17:- x will be 12
› x must be in the range of 0.. m-1.
› Brute Force: ?
› Take x from 1.. m-1 one by one and check => O(m)
› Better : Can we have it in O (log m)?
› Note: Mathematically MMI of a and x exists iff a & m
are co-prime (having GCD 1)
› Think for EEA now. Try to formulate for x in O (log m)
MMI exists only if gcd(a, m) == 1
1/22/2025 6
Modular Multiplicative Inverse (MMI)
› Think for EEA now. Try to formulate for x in O (log m)
› By EEA: ax+by = 1 // gcd(a,b) = 1
› Put b = m:- ax+my = 1
› (ax+my ) mod m = 1 (mod m)
› ax mod m + 0 = 1
› Now find x using EEA:- gives MMI of a and m
› Do we need to check for any special & exceptional
conditions?
› Try its recursive as well as iterative implementation
1/22/2025 7
Modular Exponentiation
› Given x, y, p find (xy) % p
› Brute Force: compute x^y in O(logy) efficiently and
then compute mod p
› Issues: overflow for larger value of x & y
› Sol: use basic property:
(ab) mod p => [(a mod p) *(b mod p)] mod p
› Can you take care of overflow while computing x^y ?
(will be computed obviously now in log y steps)
› Hint: while computing x, x2, x4, x8, compute it as x mod
p, x2 mod p, x4 mod p …
1/22/2025 8
Modular Exponentiation: Sample Code
int power(int x, int y, int p)
{ int res = 1;
x = x % p; // Update x if it is more than or equal to p
if (x == 0) return 0; // In case x is divisible by p;
while (y > 0)
{
if (y & 1) //bitwise AND with 1 to check for odd
res = (res*x) % p;
y = y>>1; // y = y/2 ; // y must be even now
x = (x*x) % p; //x => x2 => x4 => x8 modulo also taken eachtime
}
return res;
}
1/22/2025 9