Aa3 (Rsa)
Aa3 (Rsa)
Grau-AA
Complexity of dealing with large integers.
[a]N = {a + kN|k ∈ Z}
Notice that
2285 ≡31 (25 )57 ≡31 3257 ≡31 (32 mod 31)57 ≡31 157 ≡31 1
Modular Operations
Modular multiplication
INPUT: x, y , N ∈ N
OUTPUT: (x · y ) mod N.
To implement x · y mod N we must do a non-mod multiplication
x × y and divide by N, which needs O(n2 ) steps where
n = max{|x|, |y |, |N|}.
Modular exponentiation
Modular exponentiation
INPUT: Two n bit integers x and N, an integer exponent y
OUTPUT: x y mod N.
Obvious way: Multiply repeatedly by (x mod N),
x mod N → x 2 mod N → . . . → x y mod N
Modular exponentiation
Modular exponentiation
INPUT: Two n bit integers x and N, an integer exponent y
OUTPUT: x y mod N.
Obvious way: Multiply repeatedly by (x mod N),
x mod N → x 2 mod N → . . . → x y mod N
Total cost: O(y n2 ), but y = O(2|y | ). Then the cost is exponential!
Modular exponentiation
Modular exponentiation
INPUT: Two n bit integers x and N, an integer exponent y
OUTPUT: x y mod N.
Obvious way: Multiply repeatedly by (x mod N),
x mod N → x 2 mod N → . . . → x y mod N
Total cost: O(y n2 ), but y = O(2|y | ). Then the cost is exponential!
Clever way: Repeating squaring,
blog y c
x mod N → x 2 mod N → x 4 mod N → . . . → x 2 mod N
Modular Exponentiation
Function modexp(x, y , N)
if y = 0 then
return 1
end if
z := modexp(x, by /2c, N)
if y is even then
return z 2 mod N
else
return x · z 2 mod N
end if
Modular Exponentiation
Function modexp(x, y , N)
if y = 0 then
return 1
end if
z := modexp(x, by /2c, N)
if y is even then
return z 2 mod N
else
return x · z 2 mod N
end if
Complexity: n recursive calls, during each call it multiplies n bit
numbers (doing computation modulo N saves us here!)
Total running time O(n3 ).
Greatest Common Divisor
GCD
INPUT: a, b ∈ Z
QUESTION: Compute gcd (a, b)
Recall that given a, b ∈ Z, the gcd (a, b) is the largest integer
which divides a and b.
How to compute the gcd?
Greatest Common Divisor
GCD
INPUT: a, b ∈ Z
QUESTION: Compute gcd (a, b)
Recall that given a, b ∈ Z, the gcd (a, b) is the largest integer
which divides a and b.
How to compute the gcd?
Obvious approach: factor and multiply common factors.
Factoring
INPUT: N ∈ N
OUTPUT: Prime factors of N
Related problem:
Prime N
INPUT: N ∈ N
QUESTION: Decide if N is prime.
Factoring is a very difficult problem!
Greatest Common Divisor
Proof.
If c ∈ Z s.t. c|a and c|b then c|a − b ⇒ gcd(a, b) ≤ gcd(a − b, b).
If c ∈ Z s.t. c|a − b and c|b then c|a ⇒ gcd(a, b) ≥ gcd(a − b, b).
Therefore gcd(a, b) = gcd(a − b, b).
Euclid’s algorithm.
EUCLID(a, b)
if b = 0 then
return a
else
EUCLID(b, a mod b)
end if
Euclid’s algorithm.
EUCLID(a, b)
if b = 0 then
return a
else
EUCLID(b, a mod b)
end if
Example
EUCLID(30,21) =EUCLID(21,9)= EUCLID(9,3)=
EUCLID(3,0)= 3
Correctness of Euclid’s algorithm
Theorem
The algorithm EUCLID is correct. Moreover for any integers
a > b ≥ 0, the total time of EUCLID (a, b) is O(n3 ), where
n = max{|a|, |b|}.
Proof.
The correctness follows from the previous theorem + the fact that
each time we decrease b till it is 0, and then gcd (a, 0) = a.
On the other hand, notice that after two consecutive recursive calls
the length of both a and b decrease by at least one bit. Then the
base case will be reached within 2n recursive calls.
And since each call involves a O(n2 ) division, so the total time is
O(n3 ).
Extended Euclid
Theorem
If a and b are any integers, not both zero, then gcd(a, b) is the
smallest positive element of the set {ax + by |x, y ∈ Z} of linear
combinations of a and b.
An alternative and useful characterization of gcd (a, b):
Lemma
For any integers a and b, if d|a and d|b and d = ax + by for some
integers x and y , then necessarily d = gcd (a, b).
EXT-EUCLID(a, b)
if b = 0 then
return (a, 1, 0)
else
(d, x 0 , y 0 ) := EXT-EUCLID (b, a mod b)
return (d, y 0 , x 0 − ba/bcy 0 )
end if
Lemma
For any positive integers a and b, EXT-EUCLID (a, b) returns
(d, x, y ) s.t. gcd (a, b) = d = ax + by . The total time of
EXT-EUCLID (a, b) is O(n3 ), where n = max{|a|, |b|}.
Example
EXT-EUCLID(99,78)
(d, x1 , y1 ) := EXT-EUCLID (99, 78)
(d, x2 , y2 ) := EXT-EUCLID (78, 21)
(d, x3 , y3 ) := EXT-EUCLID (21, 15)
(d, x4 , y4 ) := EXT-EUCLID (15, 6)
(d, x5 , y5 ) := EXT-EUCLID (6, 3)
(d, x6 , y6 ) := EXT-EUCLID (3, 0)
Example
EXT-EUCLID(99,78)
(d, x1 , y1 ) := EXT-EUCLID (99, 78)
(d, x2 , y2 ) := EXT-EUCLID (78, 21)
(d, x3 , y3 ) := EXT-EUCLID (21, 15)
(d, x4 , y4 ) := EXT-EUCLID (15, 6)
(d, x5 , y5 ) := EXT-EUCLID (6, 3)
(d, x6 , y6 ) := EXT-EUCLID (3, 0) = (3, 1, 0)
Example
EXT-EUCLID(99,78)
(d, x1 , y1 ) := EXT-EUCLID (99, 78)
(d, x2 , y2 ) := EXT-EUCLID (78, 21)
(d, x3 , y3 ) := EXT-EUCLID (21, 15)
(d, x4 , y4 ) := EXT-EUCLID (15, 6)
(d, x5 , y5 ) := EXT-EUCLID (6, 3) = (3, 0, 1)
(d, x6 , y6 ) := EXT-EUCLID (3, 0) = (3, 1, 0)
Example
EXT-EUCLID(99,78)
(d, x1 , y1 ) := EXT-EUCLID (99, 78) = (3, −11, 14)
(d, x2 , y2 ) := EXT-EUCLID (78, 21) = (3, 3, −11)
(d, x3 , y3 ) := EXT-EUCLID (21, 15) = (3, −2, 3)
(d, x4 , y4 ) := EXT-EUCLID (15, 6) = (3, 1, −2)
(d, x5 , y5 ) := EXT-EUCLID (6, 3) = (3, 0, 1)
(d, x6 , y6 ) := EXT-EUCLID (3, 0) = (3, 1, 0)
I In real arithmetic:
I Every number a 6= 0 has an inverse 1/a.
I Dividing by a is the same as multiplying by 1/a
I In modular arithmetic,
x is the multiplicative inverse of a modulo N if a · x ≡N 1
(if it exists!)
Modular division
I In real arithmetic:
I Every number a 6= 0 has an inverse 1/a.
I Dividing by a is the same as multiplying by 1/a
I In modular arithmetic,
x is the multiplicative inverse of a modulo N if a · x ≡N 1
(if it exists!)
Lemma
For any N > 1, if gcd(a, N) = 1 then the equation a · x ≡N 1 has a
unique solution, modulo N. Otherwise it has no solution.
Modular division
INPUT: x, y , N ∈ N
OUTPUT: (x · y −1 ) mod N (if it exists!)
Modular division
Modular division
INPUT: x, y , N ∈ N
OUTPUT: (x · y −1 ) mod N (if it exists!)
Define, Z∗N = {a|a ∈ ZN ∧ gcd (a, N) = 1}.
Example: Z∗15 = {1, 2, 4, 7, 8, 11, 13, 14}
Notice that (Z∗N , ·N ) is an abelian group. Therefore,
∀a ∈ Z∗N , ∃a−1 ∈ Z∗N the multiplicative inverse such that
a · a−1 ≡ 1( mod N)
To compute the multiplicative inverse of a ∈ Z∗N : use
EXT-EUCLID(a, N) to get ax + Ny = 1 or ax ≡ 1( mod N)
φ(N) = |Z∗N |
1 1 (p − 1)(q − 1)
φ(N) = N(1 − )(1 − ) = pq = (p − 1)(q − 1).
p q pq
Examples.
Z∗45 ={1, 2, 4, 7, 8, 11, 13, 14, 16, 17, 19, 22, 23, 26, 28, 29,
31, 32, 34, 37, 38, 41, 43, 44}
As 45 = 3 × 3 × 5,
φ(45) = 45(1 − 13 )(1 − 51 ) = 24.
φ(35): As 35 = 5 × 7 ⇒ φ(35) = 4 × 6 = 24
Primality
Too slow!
Theorem (Euler)
For any integer N > 1, then
aφ(N) ≡ 1( mod N)
Theorem (Fermat)
If p is prime, then
ap−1 ≡ 1( mod p)
for all a ∈ Z∗p .
The Fermat Test
PRIMALITY(N)
Pick a positive integer a < N at random
if aN−1 ≡ 1( mod N) then
return yes
else
return no
end if
The Fermat Test
PRIMALITY(N)
Pick a positive integer a < N at random
if aN−1 ≡ 1( mod N) then
return yes {almost sure}
else
return no {sure}
end if
The Fermat Test
PRIMALITY(N)
Pick a positive integer a < N at random
if aN−1 ≡ 1( mod N) then
return yes {almost sure}
else
return no {sure}
end if
Time: O(n3 ).
Fermat’s little theorem
Reapeated-Fermat N, k
for i = 1 to k do
a := random (1, N − 1)
if aN−1 6≡ 1 mod N then
return non-prime {sure}
end if
end for
return prime {almost sure}
Taking into account the Carmichael numbers
Theorem
If N is an odd prime and e ≥ 1, then the equation
x 2 ≡ 1( mod N e ) has only two solutions x ≡ 1( mod N e ) and
x ≡ −1( mod N e ).
Corollary
If there exists a nontrivial square root of 1 modulo N, then N is
composite.
Example: 52 6≡ 1 mod 21
62 6≡ 1 mod 21
72 6≡ 1 mod 21
82 = 64 ≡ 1 mod 21
Therefore, 8 is a non-trivial root of 1 mod 21, so 21 is composite.
Witness to the compositeness of N
Theorem
If N is an odd composite number, the number of witnesses to the
compositeness of N is ≥ N−1
2 .
Theorem
For any odd integer N > 2 and s ∈ Z+ the probability that
Miller-Rabin(N, s) errs is ≤ 2−s .
Proof.
If N composite, Miller-Rabin errs if misses to discover a witness in
the s iterations.
If N composite, each execution of the algorithm has probability
≥ 1/2 of discovering a witnes a.
The probability it misses in all iteractions is < 1/2s .
Generating random numbers
We need a fast algorithm for choosing random primes that are few
hundred bits long.
π(N)
lim =1
N→∞ (N/ ln N)
Generating random numbers
We need a fast algorithm for choosing random primes that are few
hundred bits long.
π(N)
lim =1
N→∞ (N/ ln N)
Exercise: We claim that since about a 1/n fraction of n-bit numbers are
prime, on average it is sufficient to draw O(n) random n-bit nubers
before hitting a prime. Show this claim.
To generate a n-bit prime:
BOB
ALICE
$E(M)$
$M$ Encoder Decoder $M=D(E(M))$
EVE
RSA : Rivest-Shamir-Adleman
Change text into numbers modulo N (ASCII)
(messages larger than N can be broken into smaller pieces).
Given N, c and y ,
it is computationally intractable to determine x s.t.
y = x c mod N.
Note that:
I Eve can not experiment all the possible values of x (An
exponential number of possibilities!).
I She could not try to factor N to retrieve p and q and then
figure out d by inverting c modulo (p − 1)(q − 1) (Factoring
is hard!)
Example
M=2
1. Select large p and q
primes
1. p = 3, q = 17
2. Compute N = p · q
2. N = 3 × 17 = 51
3. Compute
3. φ(51) = 2 × 16 = 32
φ(n) = (p − 1) · (q − 1)
4. c = 3
4. Choose c ∈ Z∗φ(N)
5. d = 11
5. Compute d such that
cd ≡ 1 mod φ(N) 6. P = (3, 51)
6. PA = (c, N). 7. S = (11, 51)
7. SA = (d, N).
To encrypt: E (2, (3, 51)) = 23 mod 51 = 8
82 mod 51 = 64 mod 51 = 13
84 mod 51 = 169 mod 51 = 16
85 mod 51 = 16 × 8 mod 51 = 128 mod 51 = 26
810 mod 51 = 262 mod 51 = 13
811 mod 51 = 13 × 8 mod 51 = 2
The hidden history
The british GCHQ (Government Communication Headquarter)
discovered the public key scheme a few years before the
Stanford-MIT teams, but is was considered a national secret until
1997.
So, contrary to Diffie and Hellman (Public Key, discrete
logarithm,1976), Rivest, Shamir and Adleman (Public Key,
factorization,1977), the mathematicians of the british GCHQ, James
Ellis (1970) and Clifford Cocks (1973), remain basically unknown
to almost everybody.