comp106_4_numberTheory
comp106_4_numberTheory
Number Theory
Let p, q be large primes (~600 digits) and e be relatively prime to (p – 1)(q – 1) and
n = pq.
To understand this, we first need to learn about prime numbers, greatest common
divisor algorithms, modular arithmetic, and so on.
4.1/4.2/4.3 Divisibility & Prime Numbers
Divisibility:
We say “a divides b”
iff c integer s.t. b = ac (where a is a factor of b).
Notation: a|b
a b a does not divide b.
Theorem: Let a, b, c be integers.
1. If a | b a | c then a | (b + c).
2. If a | b then a | bc c .
3. If a | b b | c, then a | c.
2. Prove as an exercise.
Prime Number:
An integer p > 1 is called prime iff the only positive factors of p are 1 and p.
If p is not prime, then it is called composite.
e.g.
“Every positive integer, greater than 1, is either prime or can be written uniquely as the
product of primes.”
100 = 2×2×5×5
999 = 3×3×3×37
7=7
Note that this theorem is about existence of prime factorization as well as its uniqueness.
Proof: The uniqueness part of the theorem can easily be proved by using proof by
contradiction. See your textbook.
We will prove the existence part later when we learn strong induction.
Theorem: There are infinitely many primes.
Let q = p1 p2 … pn + 1.
By the Fundamental Theorem of Arithmetic, q is either prime or can be written as product
of primes.
We have a contradiction in both cases. So, our initial assumption was false. There are
indeed infinitely many prime numbers.
Here is how chatGPT proves it in a poetic way: 🙂
Greatest Common Divisor:
d = gcd (a, b)
a = p1a1 p2a2 … pnan b = p1b1 p2b2 … pnbn where pi’s are prime.
gcd (a, b) = p1min (a1, b1) p2min (a2, b2) … pnmin (an,bn)
e.g.
gcd(120, 500) = ?
120 = 23 ∙ 3 ∙ 5 500 = 22 ∙ 53
gcd (120, 500) = 22 ∙ 30 ∙ 51 = 20
But how to find the greatest common divisor of two integers on a computer, especially
when the integers are very large?
The Euclidean Algorithm
An efficient method for finding the greatest common divisor of two integers
287 = 91∙3 + 14
a | 287 a | 91 a | 14
91 = 14∙6 + 7
Lemma:
A lemma is a
less important
Let a = bq + r, where a, b, q and r are integers. Then gcd(a, b) = gcd(b, r). theorem that
is helpful in
the proof of
The Euclidean algorithm is based on this lemma
(as demonstrated in the previous slide): Euclidean algorithm:
gcd (a,b) = gcd (r0, r1) = gcd (r1, r2) The greatest common divisor is the last non-
zero remainder.
= ∙∙∙ = gcd (rn-1, rn) = gcd (rn, 0) = rn
Pseudocode:
We won’t prove this theorem formally, but the example below demonstrates that by
reversing the steps of the Euclidean algorithm, we can always find such integers s and t :
e.g.
gcd (22, 6) = 2
Note that 2 = 4∙6 – 22, where s = 4 and t = -1, but how to find it?
22 = 3∙6 + 4
6 = 1∙4 +2 applying the Euclidean algorithm
4 = 2∙2
2 = 6 – 1∙4
= 6 – (22 – 3∙6) reversing the Euclidean algorithm
= 4∙6 – 22
Smallest: cZ (a | c b | c) → d | c
Fact:
Let a = p1a1 p2a2 … pnan, b = p1b1 p2b2 … pnbn where pi’s are prime.
gcd (a, b) = p1min (a1, b1) p2min (a2, b2) … pnmin (an, bn)
Theorem:
a b (mod m) ↔ a = b + km, for some integer k
Theorem:
If a b (mod m) and c d (mod m) then
a + c b + d (mod m) and ac bd (mod m).
Definition: Modular inverse
ā : inverse of a in modulo m
s.t. aā 1 (mod m)
Theorem:
Solution:
ax b (mod m)
āax āb (mod m) multiply both sides by the inverse
x āb (mod m)
e.g.
3x 4 (mod 7)
x –2∙4 (mod 7)
–8 6 (mod 7)
e.g. Solve 75x 5 (mod 13)
Solution:
Since gcd (75, 13) = 1, inverse exists.
75 = 13∙5 + 10 13 = 10∙1 + 3 10 = 3∙3 + 1 (by Euclidean algorithm)
Reversing the steps of the Euclidean algorithm: (so that express 1 in terms of 75 and 13)
1 = 10 3∙3 = 10 3∙(13 10∙1) = (75 13∙5) 3∙(13 75 + 13∙5)
= 4∙75 23∙13
Inverse of 75 modulo 13 is 4.
Remark: If an inverse exists, the linear congruence has a unique solution in modulo m.
However, if an inverse does not exist, there may still be a solution! (e.g., 2x 4 mod 6)
Remark: The inverse of an integer in modulo m, if exists, can always be found by
reversing the Euclidean algorithm.
Overall: (Modular) Exponentiation is the hardest. Know these when creating algorithms!!!
The Chinese Remainder Problem
How many soldiers are there in Han Xin's army? – If you let them parade in rows of 3 soldiers,
two soldiers will be left. If you let them parade in rows of 5, 3 will be left, and in rows of 7, 2 will
be left.
The Chinese Remainder Problem
How many soldiers are there in Han Xin's army? – If you let them parade in rows of 3 soldiers,
two soldiers will be left. If you let them parade in rows of 5, 3 will be left, and in rows of 7, 2 will
be left.
x 2 (mod 3)
x 3 (mod 5)
x 2 (mod 7)
What is x then?
The Chinese Remainder Theorem
Let m1, m2, …, mn be pairwise relatively prime positive integers. The system
x a1 (mod m1)
x a2 (mod m2)
.
.
x an (mod mn)
has a unique solution in modulo m = m1 ∙ m2 ∙… mn .
(In other words, there is only one integer between 0 and m1 that satisfies all these n congruences at once.)
e.g.
Since 3, 5 and 7 are pairwise relatively prime in the previous example, by Chinese
Remainder Thm., there is only one solution for x between 0 ≤ x < 105.
Solution:
Let Mk = m / mk for k = 1, 2, …, n.
Hence gcd (mk, Mk) = 1
and so yk inverse of Mk s.t. Mk yk 1 (mod mk)
m = 3∙5∙14 = 210
Note that 3, 5 and 14 are pairwise relatively prime, so we can apply Chinese Rem. Thm:
x = a1M1 y1 + a2M2 y2 + a3M3 y3 = 2∙70∙1 + 3∙42∙3 + 2∙15∙1 = 548 128 (mod 210).
Note that 128 is the only solution in mod 210, which means there is no other
number between 0 and 209, which satisfies the above congruences.
Computer Arithmetic with Large Numbers:
e.g. Suppose our computer can perform arithmetic operations with integers < 100 much faster
than with larger integers. (In reality, they work with numbers up to 264.)
Let us pick a group of pairwise relatively prime numbers: 99, 98, 97, 95.
By Chinese Remainder Thm, every n < m = 99 ∙98 ∙97 ∙95 = 89403930 can be represented
uniquely by remainders when divided by these four moduli.
All we need to do to find the sum is to solve the following system of congruences:
x 65 mod 99
x 2 mod 98
x 51 mod 97
x 10 mod 95
Pseudoprimes
Reasons:
i) They observed this holds whenever n is prime (←)
ii) They couldn’t find a composite number for which the congruence holds (→)
However, they were wrong: e.g., 2340 1 (mod 341) and 341=11∙31
But how to compute 2340 (mod 341) in the first place? 2340 is too large!
How to compute 2340 (mod 341)?
One way of doing it: Compute successively 2 mod 341, 22 mod 341, 23 mod 341, …,
2340 mod 341
Note that all computations above are in modulo 341, hence numbers never exceed 340
during computation:
See also Fast Modular Exponentiation algorithm (Algorithm 5, Chapter 4.2) for a more
efficient version.
Yet an even better way to compute ab mod m is to write the prime factorization of m and
then use Chinese Remainder Theorem and Fermat’s Little Theorem (if possible):
2) One can not simply eliminate equal factors from both sides of the
congruence as in usual arithmetic.
e.g., 5 ∙ 2 3 ∙ 2 (mod 4) but 5 3 (mod 4)
Proof:
Remark: You can not simply eliminate equal factors from both sides of the congruence as in usual
arithmetic!
e.g., 5 ∙ 2 3 ∙ 2 (mod 4) but 5 3 (mod 4)
4.6 Cryptography
A: 0
B: 1
…
Z: 25
h(p) = (p + k) mod 26, where p is an integer code for alphabet letters;
not a very high level of security!
A better alternative: h(p) = (ap + b) mod 26 (as if there are two keys a and b)
Choose a and b s.t. h(p) is one-to-one.
There exist, of course, much better more recent cryptography methods. Next, we’ll learn
one of them, which is RSA, a “public key” cryptosystem.
e.g.
Encryption: C = (M + k) (mod 26) M: original message code
Everybody knows the encryption method, but nobody, supposedly, can get the original
message without knowing the private key k.
Problem: How to share the secret key between two parties?
Everybody knows the encryption method and the public encryption key, but nobody
can get the original message without knowing the private decryption key.
RSA – A Public Key Cryptosystem (Rivest, Shamir, Adleman) 76 MIT
Let p, q be large primes (~600 digits) and e be relatively prime to (p – 1)(q – 1) and
n = pq.
Encryption:
1. Suppose Alice wants to send a message M to Bob.
2. Alice creates the ciphertext C by exponentiating s.t. C = M e mod n, where e and n
are Bob's public keys.
3. She sends C to Bob.
4. To decrypt, Bob also exponentiates, but this time with d s.t. M = C d mod n
5. The relationship between e and d ensures that Bob correctly recovers M.
6. Since only Bob knows d, only Bob can decrypt this message. Anyone who knows
Bob’s public keys can send an encrypted message to Bob.
Finally, why RSA works:
Let p, q be large primes (~600 digits) and e be relatively prime to (p – 1)(q – 1) and n = pq.
Cd M (mod pq)
Hash functions: Given a longer value, hash it to a smaller representative.
Goal: Two different values should ideally hash to two different representatives.
IMPOSSIBLE: Hashing longer values to smaller ones means that collisions must occur.
Such hash functions are used in hash table data structure. (more in COMP 202 Data Structures)
There are cryptographic hash functions. For those, finding collisions is extremely hard if the
domain is very large.
Use case: Passwords.
Your login password is not stored on your computer. Only the hash of your password is stored.
Each time you try to login, the password you enter is hashed, and the resulting value is compared
with the stored value. Since collisions are hard to find, no one else can login on your behalf. Since
only a hash is stored, if your laptop is stolen, your password is still safe. (more in COMP 443
Modern Cryptography and COMP 434 Computer and Network Security)
Pseudo-random functions and pseudo-random number generators:
Goal: Generate random-looking numbers with a systematic way.
e.g., Let m = 9, a = 7, c = 4, x0 = 3
x1 = (7x0 + 4) mod 9 = (7*3 + 4) mod 9 = 25 mod 9 = 7
x2 = (7x1 + 4) mod 9 = (7*7 + 4) mod 9 = 53 mod 9 = 8
…
In cryptography, you need cryptographically-secure pseudo-random number generators that are proven to be
indistinguishable from real random numbers under some assumptions.
e.g. java.security.SecureRandom
Remark: If you are working on a secure program, never employ a regular random number generator.
Remark: If you do not need cryptographically-strong randomness, use a regular random number generator since
the cryptographic ones will be slower.