Title: Addition
1Addition
How fast can you add AB
2Addition
How fast can you add AB
- 1 0 1 0 1 1 1 0 0 1
- 1 0 0 1 0 0 1 1 1
-
3Addition
How fast can you add AB
- 1 0 1 0 1 1 1 0 0 1
- 1 0 0 1 0 0 1 1 1
- 0
-
4Addition
How fast can you add AB
- 1 0 1 0 1 1 1 0 0 1
- 1 0 0 1 0 0 1 1 1
- 0 0
-
5Addition
How fast can you add AB
- 1 0 1 0 1 1 1 0 0 1
- 1 0 0 1 0 0 1 1 1
- 1 0 0
6Addition
How fast can you add AB
- 1 0 1 0 1 1 1 0 0 1
- 1 0 0 1 0 0 1 1 1
- 1 1 1 1 0 1 1 1 0 0
n-bit numbers ? time O(n)
7Multiplication
How fast can you multiply AB
- 1 0 1 0 1 1 1 0 0 1
- 1 0 1 1
-
8Multiplication
How fast can you multiply AB
- 1 0 1 0 1 1 1 0 0 1
- 1 0 1 1
- 1 0 1 0 1 1 1 0 0 1
- 1 0 1 0 1 1 1 0 0 11 0 1 0 1 1 1 0 0 1
9Multiplication
How fast can you multiply AB
- 1 0 1 0 1 1 1 0 0 1
- 1 0 1 1
- 1 0 1 0 1 1 1 0 0 1
- 1 0 1 0 1 1 1 0 0 11 0 1 0 1 1 1 0 0 1
n-bit numbers ? time O(n2)
10Karatsuba-Offman
a2n/2 a1 a0 b2n/2 b1 b0
ab(2n/2a1a0)(2n/2b1b0) 2n a1 b1
2n/2 (a1 b0 a0 b1) a0 b0
11Karatsuba-Offman
a2n/2 a1 a0 b2n/2 b1 b0
Multiply(a,b,n) if n1 return ab else
R1 ? Multiply(a1,b1,n/2) R2 ?
Multiply(a0,b1,n/2) R3 ? Multiply(a1,b0,n/2)
R4 ? Multiply(a0,b0,n/2) return 2n
R1 2n/2 (R2R3) R4
12Karatsuba-Offman
Multiply(a,b,n) if n1 return ab else
R1 ? Multiply(a1,b1,n/2) R2 ?
Multiply(a0,b1,n/2) R3 ? Multiply(a1,b0,n/2)
R4 ? Multiply(a0,b0,n/2) return 2n
R1 2n/2 (R2R3) R4
Recurrence?
13Karatsuba-Offman
Multiply(a,b,n) if n1 return ab else
R1 ? Multiply(a1,b1,n/2) R2 ?
Multiply(a0,b1,n/2) R3 ? Multiply(a1,b0,n/2)
R4 ? Multiply(a0,b0,n/2) return 2n
R1 2n/2 (R2R3) R4
Recurrence?
T(n) 4T(n/2) O(n)
14Karatsuba-Offman
T(n) 4T(n/2) O(n)
T(n)O(n2)
15Karatsuba-Offman
ab(2n/2a1a0)(2n/2b1b0) 2n a1 b1
2n/2 (a1 b0 a0 b1) a0 b0
Can compute in less than 4 multiplications?
16Karatsuba-Offman
ab(2n/2a1a0)(2n/2b1b0) 2n a1 b1
2n/2 (a1 b0 a0 b1) a0 b0
Can compute using 3 multiplications
(a0a1)(b0b1) a0b0 (a1 b0 a0 b1) a1
b1
17Karatsuba-Offman
Multiply(a,b,n) if n1 return ab else
R1 ? Multiply(a1,b1,n/2) R2 ?
Multiply(a0,b0,n/2) R3 ? Multiply(a1a0,b1b
0,n/21) R4 ? R3 R2 R1 return 2n
R1 2n/2 R3 R2
Recurrence?
18Karatsuba-Offman
Multiply(a,b,n) if n1 return ab else
R1 ? Multiply(a1,b1,n/2) R2 ?
Multiply(a0,b0,n/2) R3 ? Multiply(a1a0,b1b
0,n/21) R4 ? R3 R2 R1 return 2n
R1 2n/2 R3 R2
Recurrence?
T(n) 3T(n/2) O(n)
19Karatsuba-Offman
T(n) 3T(n/2) O(n)
T(n)O(nC)
Clog2 3 ? 1.58
20Integer Division
ra mod b
a,b ? q,r a qb r 0 ? r lt b
Can be done in O(n2) time.
21d divides a
DEFINITION d divides a (denoted d a)
if there exists b such that bd a
36 30 03 00
22d divides a
DEFINITION d divides a (denoted d a)
if there exists b such that bd a
36 yes, b2 30 yes, b0 03 no 00 yes, b?
23d divides a
36 yes, b2 30 yes, b0 03 no 00 yes, b?
d a ? a c ? d c
Proof a bd, cba ? c(bb)d
24Divisibility poset
0
8
10
9
4
6
3
5
7
2
1
25GCD
GCD (a,b) largest d such that da, db
26GCD
GCD (a,b) largest d such that da, db
da, db (?c ca,cb) cd
GCD(3,6) GCD(0,8) GCD(0,0)
27GCD
GCD (a,b) largest d such that da, db
da, db (?c ca,cb) cd
GCD(3,6) 3 GCD(0,8) 8 GCD(0,0) 0
28GCD
How quickly can we compute GCD (a,b) ?
29GCD
How quickly can we compute GCD (a,b) ?
Euclid GCD(a,b) GCD(b,a mod b)
30GCD
wlog agtb
GCD(a,b) if b0 then return a else
return GCD(b,a mod b)
Running time?
31GCD
wlog agtb
GCD(a,b) if b0 then return a else
return GCD(b,a mod b)
Running time?
(a,b)?(b,a mod b)?(a mod b, ?)
(a mod b) lt a/2
32GCD
(a,b)?(b,a mod b)?(a mod b, ?)
(a mod b) lt a/2
2(log2 a)O(n) iterations each mod O(n2)
time ? O(n3) time total
33Modular exponentiation (a,b,m) ? ab
mod m
34Modular exponentiation (a,b,m) ? ab
mod m
b 10101
a mod m a2 mod m a4 mod m a8 mod m a16 mod m ...
ab mod m
35Modular exponentiation (a,b,m) ? ab
mod m
mod-ex(a,b,m) if b0 then RETURN 1 else
if b mod 2 0 then RETURN
mod-ex(a,b/2,m)2 mod m else
RETURN amod-ex(a,(b-1)/2,m)2
mod m
36Algorithms so far
a,b,m n-bit integers
addition ab O(n)
time multiplication ab O(n1.58)
time division a/b,a mod b O(n2) time gcd(a,b)
O(n3) time ab mod m
O(n3) time
37GROUP
(G,?) is a group if ? G?G ? G
(a?b)?c a?(b?c) exists ?? G (?a?G)
a?? a a ? a-1 a?a-1?
38Modular arithmetic
modulo m
G 0,...,m-1 Zm
a?b ab mod m
(G,?) is a group if ? G?G ? G
(a?b)?c a?(b?c) exists ?? G (?a?G)
a?? a a ? a-1 a?a-1?
39Modular arithmetic
modulo m
G 0,...,m-1 Zm
a?b ab mod m
(G,?) is a group if ? G?G ? G
(a?b)?c a?(b?c) exists ?? G (?a?G)
a?? a a ? a-1 a?a-1?
IS A GROUP
40Modular arithmetic
modulo m
G 0,...,m-1 Zm
a?b ab mod m
(G,?) is a group if ? G?G ? G
(a?b)?c a?(b?c) exists ?? G (?a?G)
a?? a a ? a-1 a?a-1?
41Modular arithmetic
modulo m
G 0,...,m-1 Zm
a?b ab mod m
(G,?) is a group if ? G?G ? G
(a?b)?c a?(b?c) exists ?? G (?a?G)
a?? a a ? a-1 a?a-1?
? b ab1 mod m
?
GCD(a,m)1
42Modular arithmetic
modulo m
G Zm a GCD(a,m)1
a?b ab mod m
(G,?) is a group if ? G?G ? G
(a?b)?c a?(b?c) exists ?? G (?a?G)
a?? a a ? a-1 a?a-1?
IS A GROUP
43Fermats little Theorem
p a prime
ap-1 1 mod p
ak k? Z is a subgroup of Zp
44Fermats little Theorem
a?(m)1 mod m
?(m) Zm
mp1a1 p2a2 ... pkak
?(m) (1-1/p1) ... (1-1/pk) m
45Fermats little Theorem
mp1a1 p2a2 ... pkak
?(m) (1-1/p1) ... (1-1/pk) m
E.g. if mpq p,q primes
?(m)
46Fermats little Theorem
mp1a1 p2a2 ... pkak
?(m) (1-1/p1) ... (1-1/pk) m
E.g. if mpq p,q primes
?(m)(p-1)(q-1)
47Fermats little Theorem
a(p-1)(q-1) 1 mod pq
E.g. if mpq p,q primes
?(m)(p-1)(q-1)
48RSA
- choose primes p,q
- let n ? pq
- choose e
- compute
- de-1 mod (p-1)(q-1)
- 5) announce n,e
49RSA
- choose primes p13,q17
- let n ? pq
- choose e
- compute
- de-1 mod (p-1)(q-1)
- 5) announce n,e
50RSA
- choose primes p13,q17
- let n ? pq221
- choose e
- compute
- de-1 mod (p-1)(q-1)
- 5) announce n,e
51RSA
- choose primes p13,q17
- let n ? pq221
- choose e5
- compute
- de-1 mod (p-1)(q-1)
- 5) announce n,e
52RSA
- choose primes p13,q17
- let n ? pq221
- choose e5
- compute
- 77de-1 mod (p-1)(q-1)
- 5) announce n,e
53RSA
d 77
- choose primes p13,q17
- let n ? pq221
- choose e5
- compute
- 77de-1 mod (p-1)(q-1)
- 5) announce n,e
n221
e5
54RSA
d 77
n221
e5
ENCODE x? xe mod n DECODE x? xd mod n
55RSA
d 77
m42
n221
e5
ENCODE x? xe mod n DECODE x? xd mod n
56RSA
d 77
m42
425 (mod 221) 9
9
n221
e5
ENCODE x? xe mod n DECODE x? xd mod n
57RSA
d 77
m42
425 (mod 221) 9
977 (mod 221) 42
9
m42
n221
e5
ENCODE x? xe mod n DECODE x? xd mod n
58Primality testing