Number theory is one of the most intriguing branches of mathematics. For a long time, number theory was considered one of the purest areas of mathematics, in the sense of being most remote from having applications. However, since the 1970's number theory has turned out to have intriguing and unexpected applications, primarily in cryptography.
In this course we discuss only some basic number-theoretic topics that have applications in Computer Science. In addition to the rich applications, a second reason for discussing the basics of number theory in a course in Computer Science is as a preparation for the chapter on algebra (Chapter 5).
4.1.1. Number Theory as a Mathematical Discipline
Number theory (in a strict sense[1]) is the mathematical theory of the natural numbers $ℕ = \{0, 1, 2, …\}$ or, more generally, of the integers, $ℤ$. The laws of the integers are so natural, simple, and well-known to us that it is amazing how apparently simple questions about the integers turn out to be extremely difficult and have resisted all attacks by the brightest mathematicians.
Example 4.1.
A simple conjecture unproven to date is that there are infinitely many prime pairs, i.e., primes $p$ such that $p + 2$ is also prime. The first prime pairs are $(3, 5)$, $(5, 7)$, $(11, 13)$, and $(17, 19)$.
Example 4.2.
Can one find a triangle with a $90°$ angle whose three sides $a$, $b$, and $c$ have integer lengths? An equivalent question is whether there exist positive integers $a$, $b$, and $c$ such that $a^2 + b^2 = c^2$. The answer is yes. Examples are $3^2 + 4^2 = 5^2$ and $12^2 + 5^2 = 13^2$ . A straight-forward generalization of this question is whether there exist positive integers $a$, $b$, $c$, and $n ≥ 3$ such that $a^n + b^n = c^n$. The answer (no such integers exist) is known as Fermat's last theorem, which remained one of the most famous open conjectures until Andrew Wiles settled the question some years ago, using highly sophisticated mathematics.
Example 4.3.
The recent proof of the Catalan conjecture by Preda Mihailescu, who worked at ETH Zürich, is another break-through in number theory. This theorem states that the equation $a^m − b^n = 1$ has no other integer solutions but $3^2 − 2^3 = 1$ (for $m, n ≥ 2$).
In this course we are trying to present a rigorous mathematical treatment of the material. Consequently, in order to present number theory, it appears that we would first have to define the integers, so we know what we are talking about, in contrast to the intuitive understanding of numbers acquired since the early years at school. However, such a formal, axiomatic treatment of the integers is beyond the scope of the course.
In this chapter we take the usual approach where we assume that we know what numbers and operations on numbers are and that we also know the basic facts about numbers (e.g. the commutative, associative and distributive laws, etc.) which we can use to prove statements. But we should point out that in such an informal approach it is difficult (if not impossible) to draw the dividing line between facts that are well-known and facts that require a proof. For example, why is there no integer between $0$ and $1$, why is $−0 = 0$, and why is $a^2 ≥ 0$ for all $a ∈ ℤ$? What is the complete list of facts we consider known, and which facts require a proof? The answer is not clear unless one states a list of axioms. For example, we will show an interesting proof of the fact that every number can be factored uniquely into primes. This is definitely a theorem that requires a proof, even though, after many years of mathematical education, the reader may consider it a well-known basic fact.
The integers are a special case of a mathematical structure called a ring, which will be discussed in Chapter 5. In this chapter we mention in a few places that concepts like divisors, greatest common divisors, ideals, etc. can be defined for any ring, not just for the integers.
For integers $a$ and $b$ we say that $a$divides$b$, denoted $a | b$, if there exists an integer $c$ such that $b = ac$. In this case, $a$ is called a divisor[2] of $b$, and $b$ is called a multiple[3] of $a$. If $a ≠ 0$ and a divisor $c$ exists it is called the[4]quotient when $b$ is divided by $a$, and we write $c = \frac{b}{a}$ or $c = b/a$. We write $a \operatorname{\not\!|}\ b$ if $a$ does not divide $b$.
Note that every non-zero integer is a divisor of $0$. Moreover, $1$ and $−1$ are divisors of every integer.
In the previous section we defined division of $a$ by $d$ for any divisor $d$ of $a$. In this section we generalize division to the case where $d$ is not a divisor of $a$ and hence division yields a remainder[5]. The following theorem was proved by Euclid around 300 B.C.
Theorem 4.1 (Euclid)
For all integers$a$ and $d ≠ 0$there exist unique integers$q$ and $r$satisfying
$a=d q+r$ and $0\leq r<|d|$.
Here $a$ is called the dividend, $d$ is called the divisor, $q$ is called the quotient, and $r$ is called the remainder. The remainder $r$ is often denoted as $R_d(a)$ or sometimes as $a \operatorname{mod} d$.
Proof: We carry out this proof in a detailed manner, also to serve as an example of a systematic proof.
We define $S$ to be the set of possible nonnegative remainders:
We prove the following three claims by first proving $1)$, then proving that $1)$ implies $2)$, and then proving that $2)$ implies $3)$.
$S$ is not empty.
$S$ contains an $r < |d|$.
The $r$ of claim $2)$ is unique.
Proof of 1): We use case distinction and prove the statement for three cases (one of which is always satisfied):
Case 1: $a ≥ 0$. Then $a = d0 + a$ and hence $a ∈ S$.
Case 2: $a < 0$ and $d > 0$. Then $a = da + (1 − d)a$ and thus $(1 − d)a ∈ S$ since $(1 − d)a ≥ 0$ because both $(1 − d)$ and $a$ are $≤ 0$.
Case 3: $a < 0$ and $d < 0$. Then $a = d(−a) + (1 + d)a$ and thus $(1 + d)a ∈ S$ since $(1 + d)a ≥ 0$ because both $(1 + d)$ and $a$ are $≤ 0$.
Proof that 1) implies 2): Because $S$ is not empty, it has a smallest element (due to the well-ordering principle), which we denote by $r$. We now prove that $r < |d|$, by contradiction, i.e., assuming $r ≥ |d|$. By definition of $S$ we have $a = dq + r$ for some $q$. We make a case distinction: $d > 0$ and $d < 0$. If $d > 0$, then
$a=d(q+1)+(r-|d|)$,
hence $r−|d| ≥ 0$ and therefore $r−|d| ∈ S$, which means that $r$ is not the smallest element of $S$, a contradiction. If $d < 0$, then $a = d(q −1)+ (r−|d|)$, and the same argument as above shows that $r − |d| ∈ S$, a contradiction.
Proof that 2) implies 3): It remains to prove that $r$ is unique. We give a proof only for $d > 0$; the case $d < 0$ is analogous and is left as an exercise. The proof is by contradiction. Suppose that there also exist $r^′ ≠ r$ with $0 ≤ r^′ < |d|$ and such that $a = dq^′ + r^′$ for some $q^′$. We distinguish the three cases $q^′ = q$, $q^′ < q$ , and $q^′ > q$. If $q^′ = q$, then $r^′ = a − dq^′ = a − dq = r$, a contradiction since we assumed $r^′ ≠ r$. If $q^′ < q$, then $q − q^′ ≥ 1$, so
Since $r^′ ≥ r + d ≥ d$, the condition $0 ≤ r^′ < |d|$ is violated, which is a contradiction. A symmetric argument shows that $q^′ > q$ also results in a contradiction. Hence $r$ is unique. $\Box$
For integers $a$ and $b$ (not both $0$), an integer $d$ is called a greatest common divisor[6] of $a$ and $b$ if $d$ divides both $a$ and $b$ and if every common divisor of $a$ and $b$ divides $d$, i.e., if
The concept of a greatest common divisor applies not only to $ℤ$, but to more general structures (e.g. polynomial rings). If $d$ and $d^′$ are both greatest common divisors of $a$ and $b$, then $d | d^′$ and $d^′ | d$. For the integers $ℤ$, this means that $d^′ = ±d$, i.e., there are two greatest common divisors. (But for more general structures there can be more than two greatest common divisors.)
Definition 4.3.
For $a, b ∈ ℤ$ (not both $0$) one denotes the unique positive greatest common divisor by $\operatorname{gcd}(a, b)$ and usually calls it the greatest common divisor. If $\operatorname{gcd}(a, b) = 1$, then $a$ and $b$ are called relatively prime[7].
Proof: It is easy to prove (as an exercise) that every common divisor of $m$ and $n − qm$ (and therefore also the greatest) is also a common divisor of $m$ and $n$, and vice versa. $\Box$
which is the basis for Euclid's well-known $\operatorname{gcd}$-algorithm: Start with $m < n$ and repeatedly replace the pair $(m, n)$ by the pair $(R_m(n), m)$ until the remainder is $0$, at which point the last non-zero number is equal to $\operatorname{gcd}(m, n)$.
Definition 4.4. Ideal
For $a, b ∈ ℤ$, the ideal generated by$a$ and $b$[8], denoted $(a, b)$, is the set
The following lemma implies that every ideal in $ℤ$ can be generated by a single integer.
Lemma 4.3.
For $a, b ∈ ℤ$there exists$d ∈ ℤ$such that$(a, b) = (d)$.
Proof: If $a = b = 0$, then $d = 0$. Assume now that at least one of the numbers is non-zero. Then $(a, b)$ contains some positive numbers, so (by the well-ordering principle) let $d$ be the smallest positive element in $(a, b)$. Clearly $(d) ⊆ (a, b)$ since every multiple of $d$ is also in $(a, b)$. It remains to prove $(a, b) ⊆ (d)$. For any $c ∈ (a, b)$ there exist $q$ and $r$ with $0 ≤ r < d$ such that $c = qd + r$. Since both $c$ and $d$ are in $(a, b)$, so is $r = c − qd$. Since $0 ≤ r < d$ and $d$ is (by assumption) the smallest positive element in $(a, b)$, we must have $r = 0$. Thus $c = qd ∈ (d)$.
Lemma 4.4.
Let$a, b ∈ ℤ$ (not both$0$). If$(a, b) = (d)$, then$d$is a greatest common divisor of$a$and$b$.
Proof:$d$ is a common divisor of $a$ and $b$ since $a ∈ (d)$ and $b ∈ (d)$. To show that $d$ is a greatest common divisor, i.e., that every common divisor $c$ of $a$ and $b$ divides $d$, note that $c$ divides every integer of the form $ua + vb$, in particular $d$.
The following corollary follows from Lemmas 4.3 and 4.4.
Corollary 4.5.
For$a, b ∈ ℤ$ (not both$0$), there exist$u, v ∈ ℤ$such that
$\operatorname{gcd}(a,b)=ua+vb$.
Example 4.4.
For $a = 26$ and $b = 18$ we have
$\operatorname*{gcd}(26,18)=2=(-2)⋅26+3⋅18$.
Also, for $a = 17$ and $b = 13$ we have
$\operatorname*{gcd}(17,13)=1=(-3)⋅17+4⋅13$.
An extension of Euclid's well-known gcd-algorithm allows to efficiently compute not only $\operatorname{gcd}(a, b)$, but also $u$ and $v$ such that $\operatorname{gcd}(a, b) = ua + vb$.
The least common multiple is a dual concept of the greatest common divisor.
Definition 4.5.
The least common multiple$l$ of two positive integers $a$ and $b$, denoted $l = \operatorname{lcm}(a, b)$, is the common multiple of $a$ and $b$ which divides every common multiple of $a$ and $b$, i.e.,
4.3.1. Primes and the Fundamental Theorem of Arithmetic
In this section we prove the well-known fact that prime factorization of integers is unique. This statement is true more generally for certain types of rings (see Chapter 5), for example for the ring of polynomials over a field. Even though rings were not introduced so far, we give hints as to how a formulation can be generalized from the integers to more general rings.
Definition 4.6.
A positive integer $p > 1$ is called prime if the only positive divisors of $p$ are $1$ and $p$. An integer greater than $1$ that is not a prime is called composite[9].[10]
This notion of having only trivial divisors extends to other rings, for example the ring of polynomials over $ℝ$. In such a general context, the property is called irreducible rather than prime. The term prime is in general used for the property that if $p$ divides a product of elements, then it divides at least one of them (see Lemma 4.7 below). For the integers, these two concepts are equivalent. The next lemma states one direction of this equivalence.
The following theorem is called the fundamental theorem of arithmetic.
Theorem 4.6.
Every positive integer can be written uniquely (up to the order in which factors are listed) as the product of primes.[11]
4.3.2. Proof of the Fundamental Theorem of Arithmetic*
Lemma 4.7.
If$p$is a prime which divides the product$x_1 x_2 \cdots x_n$of some integers$x_1, …, x_n$, then$p$divides one of them, i.e.,$p \operatorname | x_i$for some$i ∈ \{1, …, n\}$.
Proof: The proof is by induction on $n$. The claim is trivially true for $n = 1$ (induction basis). Suppose it is true for some general $n$ (induction hypothesis). To prove the claim for $n + 1$ (induction step), suppose that $p \operatorname | x_1 x_2 \cdots x_{n+1}$. We let $y = x_1 x_2 \cdots x_n$ (and hence $p \operatorname | y x_{n+1}$) and look at the two cases $p \operatorname| y$ and $p \nmid y$ separately. If $p \operatorname| y$, then $p \operatorname| x_i$ for some $1 ≤ i ≤ n$, due to the induction hypothesis, and we are done. If $p ∤ y$, then, since $p$ has no positive divisor except $1$ and $p$, we have $\operatorname{gcd}(p, y) = 1$. By Corollary 4.5 there are integers $u$ and $v$ such that $up + vy = 1$. Hence we have
$x_{n+1}=(up+vy)x_{n+1}=(ux_{n+1})p+v(yx_{n+1})$.
Because $p$ divides both terms $(ux_{n+1})p$ and $v(yx_{n+1})$ in the sum on the right side, it follows that it also divides the sum, i.e., $p \operatorname | x_{n+1}$, which concludes the proof. $\Box$
We now prove Theorem 4.6:
Proof for Theorem 4.6: We first need to prove that a factorization into primes exists and then that it is unique.
The existence is proved by contradiction. Every prime can obviously be factored into primes. Let $n$ be the smallest positive integer which has no prime factorization. Since it can not be a prime, we have $n = km$ with $1 < k, m < n$. Since both $k$ and $m$ can be factored into primes, so can $km = n$, a contradiction. Hence there is no smallest $n$ that cannot be factored into primes, and therefore every $n ≥ 1$ can be factored into primes.
To prove the uniqueness of the prime factorization, suppose towards a contradiction that an integer $n$ can be factored in two (possibly different) ways as a product of primes,
where the primes $p_1, …, p_r$ and also the primes $q_1, …, q_s$ are put in an increasing order and where we have written products of identical primes as powers (here $a_i > 0$ and $b_i > 0$). Then for every $i$, $p_i \operatorname | n$ and thus $p_i \operatorname | q^{b_1}_1 q^{b_2}_2 \cdots q^{b_S}_s$. Hence, by Lemma 4.7, $p_i \operatorname | q_j$ for some $j$ and, because $q_j$ is a prime and $p_i > 1$, we have $p_i = q_j$. Similarly for every $j$, $q_j = p_i$ for some $i$. Thus the set of primes are identical, i.e., $r = s$ and $p_i = q_i$ for $1 ≤ i ≤ r$. To show that the corresponding exponents $a_i$ and $b_i$ are also identical, suppose that $a_i < b_i$ for some $i$. We can divide both expressions by $p_{i}^{a_{i} }$, which results in two numbers that are equal, yet one is divisible by $p_i$ while the other is not. This is impossible since if two numbers are equal, then they have the same divisors. $\Box$
4.3.3. Expressing $\operatorname{gcd}$ and $\operatorname{lcm}$
The fundamental theorem of arithmetic assures that integers $a$ and $b$ can be written as
$a=\displaystyle\prod_{i}p_{i}^{e_{i} }$ and $b=\displaystyle\prod_{i}p_{i}^{f_{i} }$.
This product can be understood in two different ways. Either it is over all primes, where all but finitely many of the $e_i$ are $0$, or it is over a fixed agreed set of primes. Either view is correct. Now we have
It is worth-while pointing out that this theorem is not self-evident, as it may appear to the reader completely familiar with it. There are in fact examples of rings in which the unique factorization into irreducible elements does not hold. We give two examples, one with unique factorization into irreducible elements, and one without.
Example 4.5.
Let $i = \sqrt{-1}$ denote the complex imaginary unit. The Gaussian integers
are the complex numbers whose real and imaginary parts are both integers. Since the norm (as complex numbers) is multiplied when two elements of $ℤ[i]$ are multiplied, the units (actually four) are the elements with norm $1$, namely $1$, $i$, $−1$, and $−i$. Units are elements that divide every other element. An irreducible element $p$ in $ℤ[i]$ is an element whose only divisors are units and associates of $p$ (i.e., elements $up$ where $u$ is a unit). By a generalization of the arguments given for $ℤ$ one can show that factorization into irreducible elements is unique in $ℤ[i]$. (This is not obvious.)
Example 4.6.
Consider now a slightly twisted version of the Gaussian integers:
The only units in $ℤ\left[\sqrt{−5}\right]$ are $1$ and $−1$. One can check easily, by ruling out all possible divisors with smaller norm, that the elements $2$, $3$, $1 + \sqrt{5}i$, and $1 − \sqrt{5}i$ are irreducible. The element $6$ can be factored in two different ways into irreducible elements:
Proof: Suppose $\sqrt{n} = a/b$ for two integers $a$ and $b$. Then $a^2 = nb^2$. If $n$ is not a square, it contains at least one prime factor $p$ with an odd power. Since the number of prime factors $p$ in any square is even, we have a contradiction: $a^2$ contains an even number of factors $p$ while $nb^2$ contains an odd number of factors $p$. This is impossible according to Theorem 4.6. $\Box$
Note that this proof is simpler and more general than the proof given in Example 2.28 because there we have not made use of the unique prime factorization of the integers.
An octave in music corresponds to the doubling of the frequency of a tone. Similarly, a fifth[12] corresponds to a ratio $3 : 2$, a musical fourth[13] corresponds to a ratio $4 : 3$, and a major and minor third[14] correspond to the ratios $5 : 4$ and $6 : 5$, respectively.
No multiple of fifths (or fourths) yields a multiple of octaves, since otherwise we would have $(\frac{3}{2})^n = 2^m$ for some $n$ and $m$, which is equivalent to $3^n = 2^{m+n}$. This implies that one cannot tune a piano so that all intervals are correct since on the piano (considered to be extended to many octaves), one hits a higher octave after a certain number (namely $12$) of fifths. It can be viewed as a number-theoretic miracle that tuning a piano is nevertheless possible with only very small inaccuracies. If one divides the octave into $12$ equal (half-tone) intervals, a half-tone corresponds to a frequency ratio of $\sqrt[12]{2} ≈ 1.05946$. Three, four, five, and seven half-tones yield frequency ratios of
approximating the minor third, major third, fourth, and fifth astonishingly well.
One can view these relations also as integer approximations. For example, we have $531'441 = 3^{12} ≈ 2^{19} = 524'288$, which implies that $(\frac{3}{2})^{12} ≈ 2^7$ , i.e., $12$ fifths are approximately seven octaves.
A piano for which every half-tone has the same frequency ratio, namely $\sqrt[12]{2}$, is called a well-tempered[15] piano. The reason why music is a pleasure, despite its theoretically unavoidable inaccuracy, is that our ear is trained to "round tones up or down" as needed.
Proof: To arrive at a contradiction, suppose that the set of primes is finite, say $P = \{p_1, …, p_m\}$. Then the number $n = \prod^m_{i=1} p_i + 1$ is not divisible by any of the primes $p_1, …, p_m$ and hence $n$ is either itself a prime, or divisible by a prime not in $\{p_1, …, p_m\}$. In either case, this contradicts the assumption that $p_1, …, p_m$ are the only primes. $\Box$
This is a non-constructive existence proof. We now give a constructive existence proof for another number-theoretic fact.
Theorem 4.10.
Gaps between primes can be arbitrarily large, i.e., for every$k ∈ ℕ$there exists$n ∈ ℕ$such that the set$\{n, n + 1, \cdots, n + k − 1\}$contains no prime.
Proof: Let $n = (k + 1)! + 2$. Then for any $l$ with $2 ≤ l ≤ k + 1$, $l$ divides $(k + 1)! = n − 2$ and hence $l$ also divides $(k + 1)! + l = n − 2 + l$, ruling out $n, n + 1, …, n + k − 1$ as possible primes. $\Box$
Example 4.7.
The largest gap between two primes below $100$ is $8$. Which are these primes?
There exists a huge body of literature on the density and distribution of primes. We only state the most important one of them.
Definition 4.7.
The prime counting function$\pi : ℝ → ℕ$ is defined as follows: For any real $x$, $\pi(x)$ is the number of primes $≤ x$.
The following theorem proved by Hadamard and de la Vall´ee Poussin in the 19th century states that the density of primes $≤ x$ is approximately $1/ \operatorname{ln}(x)$. This shows that if one tries to find a large (say $1024$ bit) prime, for instance for cryptographic purposes, then a randomly selected odd integer has reasonable chances of being prime. Much more precise estimates for $π(x)$ are known today.
How can we test whether a given integer $n$ is a prime? We can test whether any smaller prime is a divisor of $n$. The following lemma provides a well-known short-cut. In a practical implementation, one might not have a list of primes up to $\sqrt n$ and can instead simply try all odd numbers as possible divisors.
Lemma 4.12.
Every composite integer$n$has a prime divisor$≤ \sqrt n$.
Proof: If $n$ is composite, it has a divisor $a$ with $1 < a < n$. Hence $n = ab$ for $b > 1$. Either $a ≤ \sqrt n$ or $b ≤ \sqrt n$ since otherwise $ab > \sqrt n \cdot \sqrt n = n$. Hence $n$ has a divisor $c$ with $1 < c ≤ \sqrt n$. Either $c$ is prime or, by Theorem 4.6, has a prime divisor $d < c ≤ \sqrt n$. $\Box$
For large integers, trial-division up to the square root is hopelessly inefficient. Let us briefly discuss the algorithmic problem of testing primality.
Primes are of great importance in cryptography. In many applications, one needs to generate very large primes, with $1024$ or even $2048$ bits. In many cases, the primes must remain secret and it must be infeasible to guess them. They should therefore be selected uniformly at random from the set of primes in a certain interval, possibly satisfying some further restrictions for security or operational reasons.
Such primes can be generated in three different ways. The first approach is to select a random (odd) integer from the given interval (e.g. $[10^{1023}, 10^{1024} − 1]$) and to apply a general primality test. Primality testing is a very active research area. The record in general primality testing is around $8\,000$ decimal digits and required sophisticated techniques and very massive computing power. As a celebrated theoretical breakthrough (with probably little practical relevance), it was proved in 2002 that primality testing is in $\bf P$, i.e., there is a worst-case polynomial-time algorithm for deciding primality of an integer.[16]
The second approach is like the first, but instead of a primality test one performs a probabilistic compositeness test. Such a test has two outcomes, "composite" and "possibly prime". In the first case, one is certain that the number is composite, while in the other case one has good chances that the number is a prime, without being certain. More precisely, one can fix a (very small) probability $\epsilon$ (e.g. $\epsilon = 10^{−100}$) and then perform a test such that for any composite integer, the probability that the test does not output "composite" is bounded by $\epsilon$.
A third approach is to construct a prime together with a proof of primality. As we might see later, the primality of an integer $n$ can be proved if one knows part of the factorization of $n − 1$.
Fermat's famous "last theorem", proved recently by Wiles, states that the equation $x^n + y^n = z^n$ has no solution in positive integers $x, y, z$ for $n ≥ 3$. Here we consider a similar question. Does $x^3 + x^2 = y^4 + y + 1$ have a solution in integers $x$ and $y$?
The answer is "no", and the proof is surprisingly simple: Obviously, $x$ must be either even or odd. In both cases, $x^3 + x^2$ is even. On the other hand, $y^4 + y + 1$ is odd no matter whether $y$ is even or odd. But an even number cannot be equal to an odd number.
Here is another example whose solution requires a generalization of the above trick.
Example 4.9.
Prove that $x^3 − x = y^2 + 1$ has no integer solutions.
Definition 4.8.
For $a, b, m ∈ ℤ$ with $m ≥ 1$, we say that $a$is congruent to$b$modulo$m$ if $m$ divides $a − b$. We write $a ≡ b\ (\operatorname{mod} m)$ or simply $a ≡_m b$, i.e.,
We have $23 ≡_7 44$ and $54321 ≡_{10} 1$. Note that $a ≡_2 b$ means that $a$ and $b$ are either both even or both odd.
Example 4.11.
If $a ≡_2 b$ and $a ≡_3 b$, then $a ≡_6 b$. The general principle underlying this example will be discussed later.
The above examples 4.8 and 4.9 make use of the fact that if an equality holds over the integers, then it must also hold modulo $2$ or, more generally, modulo any modulus $m$. In other words, for any $a$ and $b$,
$a=b\ \Longrightarrow\ a\equiv_{m}b$(4.1)
for all $m$, i.e., the relation $≡_m$ is reflexive ($a ≡_m a$ for all $a$). It is easy to verify that this relation is also symmetric and transitive, which was already stated in Chapter 3:
Lemma 4.13.
For any$m ≥ 1$, $≡_m$is an equivalence relation on$ℤ$.
The implication (4.1) can be turned around and can be used to prove the inequality of two numbers $a$ and $b$:
$a\neq_{m}b\ \implies\ a\neq b$.
The following lemma shows that modular congruences are compatible with the arithmetic operations on $ℤ$.
Lemma 4.14.
If$a ≡_m b$and$c ≡_m d$, then
$a + c ≡_m b + d$and$ac ≡_m bd$.
Proof: We only prove the first statement and leave the other proof as an exercise. We have $m \operatorname | (a − b)$ and $m \operatorname | (c − d)$. Hence $m$ also divides $(a − b) + (c − d) = (a + c) − (b + d)$, which is the definition of $a + c ≡_m b + d$. $\Box$
Corollary 4.15.
Let$f(x_1, …, x_k)$be a multi-variate polynomial in$k$variables with integer coefficients, and let$m ≥ 1$. If$a_i ≡_m b_i$for$1 ≤ i ≤ k$, then
Proof: Evaluating a polynomial can be achieved by a sequence of additions and multiplications. In each such step the congruence modulo $m$ is maintained, according to Lemma 4.14. $\Box$
There are $m$ equivalence classes of the equivalence relation $≡_m$, namely $[0], [1], \ldots, [m − 1]$. Each equivalence class $[a]$ has a natural representative $R_m(a) ∈ [a]$ in the set
In the following we are often interested only in the remainder of an integer (e.g. the result of a computation) modulo some modulus $m$. Addition and multiplication modulo $m$ can be considered as operations on the set $ℤ_m$. We will be interested in this structure in Chapter 5 where we will see that it is an important example of a so-called ring.
Example 4.12.
Is $n = 84877 · 79683 − 28674 · 43879$ even or odd? The answer is trivial and does not require the computation of $n$. The product of two odd numbers is odd, the product of an even and an odd numbers is even, and the difference of an odd and an even number is odd. Thus $n$ is odd.
The following lemma establishes the simple connection between congruence modulo $m$ and remainders modulo $m$. The proof is easy and left as an exercise.
Lemma 4.16.
For any$a, b, m ∈ ℤ$with$m ≥ 1$,
$a ≡_m R_m(a)$.
$a ≡_m b \iff R_m(a) = R_m(b)$.
The above lemma together with Lemma 4.14 implies that if in a computation involving addition and multiplication one is interested only in the remainder of the result modulo $m$, then one can compute remainders modulo $m$ at any intermediate step (thus keeping the numbers small), without changing the result. This is referred to as modular arithmetic.
Corollary 4.17.
Let$f(x_1, …, x_k)$be a multi-variate polynomial in$k$variables with integer coefficients, and let$m ≥ 1$. Then
Proof: By Lemma 4.16 we have $a_i ≡_m R_m(a_i)$ for all $i$. Therefore, using Corollary 4.15 we have $f(a_1, . . . , a_k) ≡_m f(R_m(a_1), \ldots, R_m(a_k))$. Thus, using Lemma 4.16 (ii) we obtain the statement to be proved. $\Box$
Example 4.13.
Compute $7^{100}$ modulo $24$. We make use of the fact that $7^2 = 49 ≡_{24} 1$. Thus $R_{24}(7^{100}) = R_{24}((7^2)^{50}) = R_{24}(R_{24}(7^2)^{50}) = R_{24}(1^{50}) = R_{24}(1) = 1$.
Example 4.14.
Remainders can be used to check the correctness of calculations (which were, for instance, carried out by hand). If an error occurred during the computation, it is likely that this error also occurs when the computation is considered modulo some $m$. To check the result $n$ of a computation one can compare $R_m(n)$ with the remainder modulo $m$ obtained by continuously reducing intermediate results of the computation. The modulus $m = 9$ is especially suited because $R_9(n)$ can be easily computed by adding the decimal digits of $n$ (prove this!), and computing the remainder modulo $9$ of this sum. For instance, to check whether $247 · 3158 = 780026$ is correct one can compute $R_9(247) = R_9(2 + 4 + 7) = 4$ and $R_9(3158) = R_9(3 + 1 + 5 + 8) = 8$ to obtain $R_9(247 · 3158) = R_9(4 · 8) = 5$. On the other hand we have $R_9(780026) = R_9(7 + 8 + 2 + 6) = 5$. Hence the result can be correct.
Example 4.15.
A similar test can be performed for $m = 11$. $R_{11}(n)$ can be computed by adding the decimal digits of $n$ with alternating sign modulo $11$. This test, unlike that for $m = 9$, detects the swapping of digits.
Example 4.16.
The larger $m$, the more likely it is that a calculation error is detected. How could one implement a similar test for $m = 99$, how for $m = 101$?
Consider the problem of finding the solutions $x$ for the congruence equation
$ax ≡_m b$.
Obviously, if $x$ is a solution, then so is $x + km$ for any $k ∈ ℤ$. Hence we can restrict the consideration to solutions in $ℤ_m$. Of special interest is the case where $\operatorname{gcd}(a, m) = 1$ and $b = 1$.
Lemma 4.18.
The congruence equation
$a x\equiv_{m}1$
has a solution$x ∈ ℤ_m$if and only if$\operatorname{gcd}(a, m) = 1$. The solution is unique.
Proof: ($\implies$) If $x$ satisfies $ax ≡_m 1$, then $ax = km + 1$ for some $k$. Note that $\operatorname{gcd}(a, m)$ divides both $a$ and $m$, hence also $ax − km$, which is $1$. Thus $\operatorname{gcd}(a, m) = 1$.
($\impliedby$) Assume now that $\operatorname{gcd}(a, m) = 1$. According to Corollary 4.5 there exist integers $u$ and $v$ such that $ua + vm = \operatorname{gcd}(a, m) = 1$. Since $vm ≡_m 0$ we have $ua ≡_m 1$. Hence $x = u$ is a solution in $ℤ$, and thus $x = R_m(u)$ is a solution in $ℤ_m$.
To prove uniqueness of $x$ in $ℤ_m$, suppose there is another solution $x' ∈ ℤ_m$. Then $ax − ax' ≡_m 0$, thus $a(x − x') ≡_m 0$ and hence $m$ divides $a(x − x')$. Since $\operatorname{gcd}(a, m) = 1$, $m$ must divide $(x − x')$. [18] Therefore $R_m(x) = R_m(x')$ and hence $R_m(x)$ is the unique solution in $ℤ_m$. $\Box$
Definition 4.9.
If $\operatorname{gcd}(a, m) = 1$, the unique solution $x ∈ ℤ_m$ to the congruence equation $ax ≡_m 1$ is called the multiplicative inverse of$a$modulo$m$. One also uses the notation $x ≡_m a^{−1}$ or $x ≡_m 1/a$.
Example 4.17.
The multiplicative inverse of $5$ modulo $13$ is $8$ since $5 · 8 = 40 ≡_{13} 1$.
The multiplicative inverse of $a$ modulo $m$ can efficiently be computed using the so-called extended Euclidean algorithm. Note that if $\operatorname{gcd}(a, m) ≠ 1$, then $a$ has no multiplicative inverse modulo $m$.
We now consider a system of congruences for an integer $x$.
Example 4.18.
Find an integer $x$ for which $x ≡_3 1$, $x ≡_4 2$, and $x ≡_5 4$. A solution is $x = 34$ as one can easily verify. This is the only solution in $ℤ_{60}$, but by adding multiples of $60$ to $x$ one obtains further solutions.
The following theorem, known as the Chinese Remainder Theorem (CRT), states this for general systems of congruences. The proof of the theorem is constructive: it shows how a solution $x$ can be constructed efficiently.
Theorem 4.19. Chinese Remainder Theorem
Let$m_1, m_2, \ldots, m_r$be pairwise relatively prime integers and let$M = \prod^r_{i=1} m_i$. For every list$a_1, \ldots, a_r$with$0 ≤ a_i < m_i$for$1 ≤ i ≤ r$, the system of congruence equations
$$\begin{gather*} x \equiv_{m_1} a_1\\[4pt] x \equiv_{m_2} a_2\\ \vdots\\ x \equiv_{m_r} a_r \end{gather*} $$
for$x$has a unique solution$x$satisfying$0 ≤ x < M$.
Proof: Let $M_i = M/m_i$. Hence $\operatorname{gcd}(M_i, m_i) = 1$ because every factor $m_k$ (where $k ≠ i$) of $M_i$ is relatively prime to $m_i$, and thus so is $M_i$. Thus there exists an $N_i$ satisfying
$M_i N_i\equiv_{m_i}1$.
Note that for all $k ≠ i$ we have $M_i ≡_{m_k} 0$ and thus
satisfies all the congruences. In order to prove uniqueness, observe that for two solutions $x'$ and $x''$, $x' − x'' ≡_{m_i} 0$ for all $i$, i.e., $x' − x''$ is a multiple of all the $m_i$ and hence of $\operatorname{lcm}(m_1, \ldots, m_r) = M$. Thus $x' ≡_M x''$. $\Box$
The Chinese Remainder Theorem has several applications. When one is interested in a computation modulo $M$, then the moduli $m_i$ can be viewed as a coordinate system. One can project the numbers of interest modulo the $m_i$, and perform the computation in the $r$ projections (which may be more efficient than computing directly modulo $M$). If needed at the end, one can reconstruct the result from the projections.
Lecture Example of $ℤ_{5}$ and $ℤ_{3}$$$\begin{array}{c|c|c|c|} \hline 4 & 9 & 4 & 14 \\ \hline 3 & 3 & 13 & 8 \\ \hline 2 & 12 & 7 & 2 \\ \hline 1 & 6 & 1 & 11 \\ \hline 0 & 0 & 10 & 5 \\ \hline \!\!{\cancel{\!\!\;^{^{\displaystyle{ℤ_5}}} \;_{_{\displaystyle{ℤ_3}}}}\!\!\!} & 0 & 1 & 2 \\ \end{array} $$
Example 4.19.
Compute $R_{35}(2^{1000})$. We can do this computation modulo $5$ and modulo $7$ separately. Since $2^4 ≡_5 1$ we have $2^{1000} ≡_5 1$. Since $2^3 ≡_7 1$ we have $2^{1000} ≡_7 2$. This yields $2^{1000} ≡_{35} 16$ since $16$ is the (unique) integer $x ∈ [0, 34]$ with $x ≡_5 1$ and $x ≡_7 2$.
Until the 1970's, number theory was considered one of the purest of all mathematical disciplines in the sense of being furthest away from any useful applications. However, this has changed dramatically in the 1970's when crucial applications of number theory in cryptography were discovered.
In a seminal 1976 paper[19], Diffie and Hellman proposed the revolutionary concept of public-key cryptography. Most security protocols, and essentially all those used on the Internet, are based on public-key cryptography. Without this amazing and paradoxical invention, security on the Internet would be unthinkable.
Consider the key distribution problem. In order to encrypt the communication between two parties, say Alice and Bob, they need a secret key known only to them. How can they obtain such a key in a setting, like the Internet, where they initially share no secret information and where they are connected only by an insecure communication channel to which a potential adversary has access? We describe the famous Diffie-Hellman protocol which allows to solve this seemingly paradoxical problem.
The Diffie-Hellman protocol (see Figure 4.2), as originally proposed[20], makes use of exponentiation modulo a large prime $p$, for instance with $2048$ bits. While $y = R_p(g^x)$ can be computed efficiently (how?), even if $p$, $g$ and $x$ are numbers of several hundred or thousands of digits, computing $x$ when given $p$, $g$ and $y$ is generally (believed to be) computationally infeasible. This problem is known as (a version of) the discrete logarithm problem. The security of the Diffie-Hellman protocol is based on this asymmetry in computational difficulty. Such a function, like $x \mapsto R_p(g^x)$, is called a one-way function: it is easy to compute in one direction but computationally very hard to invert.[21]
The prime $p$ and the basis $g$ (e.g. $g = 2$) are public parameters, possibly generated once and for all users of the system. The protocol is symmetric, i.e., Alice and Bob perform the same operations. The exchange of the so-called public keys$y_A$ and $y_B$ must be authenticated, but not secret.[22] It is easy to see that Alice and Bob end up with the same value $k_{AB} = k_{BA}$ which they can use as a secret key for encrypting subsequent communication.[23] In order to compute $k_{AB}$ from $y_A$ and $y_B$, an adversary would have to compute either $x_A$ or $x_B$, which is believed to be infeasible.
A mechanical analogue of a one-way function is a padlock without a key.[24] The mechanical analog of the Diffie-Hellman protocol is shown in Figure 4.2. Alice and Bob can exchange their locks (closed) and keep a copy in the open state. Then they can both generate the same configuration, namely the two locks interlocked. For the adversary, this is impossible without breaking open one of the locks.
Another famous (and more widely used) public-key crypto system, the so called RSA-system invented in 1977 and named after Rivest, Shamir and Adleman[25], will be discussed later. Its security is based on the (conjectured) computational difficulty of factoring large integers.
In a more comprehensive understanding, number theory refers to a richer mathematical theory which also includes topics like, for instance, algebraic extensions of the rational numbers. ↩︎
M. Agrawal, N. Kayal, and N. Saxena, PRIMES is in P, Annals of Mathematics vol. 160, pp. 781– 793. ↩︎
Recall that $R_m(a)$ denotes the remainder when $a$ is divided by $m$. ↩︎
If $k$ divides $mn$ and $gcd(k, n) = 1$, then $k$ divides $m$. (Prove this!) ↩︎
W. Diffie and M.E. Hellman, New directions in cryptography, IEEE Transactions on Information Theory, vol. 22, no. 6, pp. 644-654, 1976. ↩︎
Since then, other versions, for instance based on elliptic curves, have been proposed. ↩︎
It is not known whether one-way functions actually exist, but it is conjectured that exponentiation modulo a prime $p$ is a one-way function for most $p$. ↩︎
This can be achieved by recognizing the other party's voice in a phone call or, indirectly, by the use of public-key certificates. ↩︎
More precisely, they can derive a common secret key, for example by applying a hash function to $K_{AB}$. ↩︎
A padlock with a key corresponds to a so-called trapdoor one-way function which is not considered here. ↩︎