Skip to content

Chapter 4:
Number Theory

4.1. Introduction

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 a2+b2=c2. The answer is yes. Examples are 32+42=52 and 122+52=132 . A straight-forward generalization of this question is whether there exist positive integers a, b, c, and n3 such that an+bn=cn. 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 ambn=1 has no other integer solutions but 3223=1 (for m,n2).

4.1.2. What are the Integers?

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 a20 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.

4.2. Divisors and Division

4.2.1 Divisors

Definition 4.1.

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 a0 and a divisor c exists it is called the[4] quotient when b is divided by a, and we write c=ba or c=b/a. We write a| 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.

4.2.2. Division with Remainders

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 d0 there exist unique integers q and r satisfying

a=dq+r  and  0r<|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 Rd(a) or sometimes as amodd.

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:

S=def{s | s0  and  a=dt+s  for some tZ}.

We prove the following three claims by first proving 1), then proving that 1) implies 2), and then proving that 2) implies 3).

  1. S is not empty.

  2. S contains an r<|d|.

  3. 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: a0. Then a=d0+a and hence aS.

Case 2: a<0 and d>0. Then a=da+(1d)a and thus (1d)aS since (1d)a0 because both (1d) and a are 0.

Case 3: a<0 and d<0. Then a=d(a)+(1+d)a and thus (1+d)aS since (1+d)a0 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(q1)+(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 rr with 0r<|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=adq=adq=r, a contradiction since we assumed rr. If q<q, then qq1, so

r=adq=(adq)+d(qq)r+d.

Since rr+dd, the condition 0r<|d| is violated, which is a contradiction. A symmetric argument shows that q>q also results in a contradiction. Hence r is unique.

4.2.3. Greatest Common Divisors

Definition 4.2.

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

da  db  c ((ca  cb)  cd).

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 gcd(a,b) and usually calls it the greatest common divisor. If gcd(a,b)=1, then a and b are called relatively prime[7].

Lemma 4.2.

For any integers m, n and q, we have

gcd(m,nqm)=gcd(m,n).

Proof: It is easy to prove (as an exercise) that every common divisor of m and nqm (and therefore also the greatest) is also a common divisor of m and n, and vice versa.

This lemma implies in particular that

gcd(m,Rm(n))=gcd(m,n),

which is the basis for Euclid's well-known gcd-algorithm: Start with m<n and repeatedly replace the pair (m,n) by the pair (Rm(n),m) until the remainder is 0, at which point the last non-zero number is equal to gcd(m,n).

Definition 4.4. Ideal

For a,b, the ideal generated by a and b[8], denoted (a,b), is the set

(a,b) =def {ua+vbu,vZ}.

Similarly, the ideal generated by a single integer a is

(a)=def{uauZ}.

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 0r<d such that c=qd+r. Since both c and d are in (a,b), so is r=cqd. Since 0r<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

gcd(a,b)=ua+vb.

Example 4.4.

For a=26 and b=18 we have

gcd(26,18)=2=(2)26+318.

Also, for a=17 and b=13 we have

gcd(17,13)=1=(3)17+413.

An extension of Euclid's well-known gcd-algorithm allows to efficiently compute not only gcd(a,b), but also u and v such that gcd(a,b)=ua+vb.

4.2.4. Least Common Multiples

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=lcm(a,b), is the common multiple of a and b which divides every common multiple of a and b, i.e.,

al  bl  m ((am  bm)  lm).

4.3. Factorization into Primes

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 x1x2xn of some integers x1,,xn, then p divides one of them, i.e., p|xi 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|x1x2xn+1. We let y=x1x2xn (and hence p|yxn+1) and look at the two cases p|y and py separately. If p|y, then p|xi for some 1in, due to the induction hypothesis, and we are done. If py, then, since p has no positive divisor except 1 and p, we have gcd(p,y)=1. By Corollary 4.5 there are integers u and v such that up+vy=1. Hence we have

xn+1=(up+vy)xn+1=(uxn+1)p+v(yxn+1).

Because p divides both terms (uxn+1)p and v(yxn+1) in the sum on the right side, it follows that it also divides the sum, i.e., p|xn+1, which concludes the proof.

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 n1 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,

n=p1a1p2a2prar=q1b1q2b2qsbs,

where the primes p1,,pr and also the primes q1,,qs are put in an increasing order and where we have written products of identical primes as powers (here ai>0 and bi>0). Then for every i, pi|n and thus pi|q1b1q2b2qsbS. Hence, by Lemma 4.7, pi|qj for some j and, because qj is a prime and pi>1, we have pi=qj. Similarly for every j, qj=pi for some i. Thus the set of primes are identical, i.e., r=s and pi=qi for 1ir. To show that the corresponding exponents ai and bi are also identical, suppose that ai<bi for some i. We can divide both expressions by piai, which results in two numbers that are equal, yet one is divisible by pi while the other is not. This is impossible since if two numbers are equal, then they have the same divisors.

4.3.3. Expressing gcd and lcm

The fundamental theorem of arithmetic assures that integers a and b can be written as

a=ipiei    and    b=ipifi.

This product can be understood in two different ways. Either it is over all primes, where all but finitely many of the ei are 0, or it is over a fixed agreed set of primes. Either view is correct. Now we have

gcd(a,b)=ipimin(ei,fi)

and

lcm(a,b)=ipimax(ei,fi).

It is easy to see that

gcd(a,b)lcm(a,b)=ab

because for all i we have

min(ei,fi)+max(ei,fi)=ei+fi.

4.3.4. Non-triviality of Unique Factorization*

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=1 denote the complex imaginary unit. The Gaussian integers

Z[i]=Z[1]={a+bia,bZ}

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:

Z[5]={a+b5ia,bZ}.

Like the Gaussian integers, this set is closed with respect to addition and multiplication (of complex numbers). For example,

(a+b5i)(c+d5i)=ac5bd+(bc+ad)5i.

The only units in [5] are 1 and 1. One can check easily, by ruling out all possible divisors with smaller norm, that the elements 2, 3, 1+5i, and 15i are irreducible. The element 6 can be factored in two different ways into irreducible elements:

6=23=(1+5i)(15i).

4.3.5. Irrationality of Roots*

As a consequence of the unique prime factorization we can prove:

Theorem 4.8.

n is irrational unless n is a square (n=c2 for some c).

Proof: Suppose n=a/b for two integers a and b. Then a2=nb2. 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: a2 contains an even number of factors p while nb2 contains an odd number of factors p. This is impossible according to Theorem 4.6.

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.

4.3.6. A Digression to Music Theory*

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 (32)n=2m for some n and m, which is equivalent to 3n=2m+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 2121.05946. Three, four, five, and seven half-tones yield frequency ratios of

21/4=1.18926/5,
21/3=1.25995/4,
25/12=1.334824/3, and
27/12=1.498283/2,

approximating the minor third, major third, fourth, and fifth astonishingly well.

One can view these relations also as integer approximations. For example, we have 531441=312219=524288, which implies that (32)1227 , i.e., 12 fifths are approximately seven octaves.

A piano for which every half-tone has the same frequency ratio, namely 212, 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.

4.4. Some Basic Facts About Primes*

4.4.1. The Density of Primes

The following fact was known already to Euclid.

Theorem 4.9.

There are infinitely many primes.

Proof: To arrive at a contradiction, suppose that the set of primes is finite, say P={p1,,pm}. Then the number n=i=1mpi+1 is not divisible by any of the primes p1,,pm and hence n is either itself a prime, or divisible by a prime not in {p1,,pm}. In either case, this contradicts the assumption that p1,,pm are the only primes.

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,,n+k1} contains no prime.

Proof: Let n=(k+1)!+2. Then for any l with 2lk+1, l divides (k+1)!=n2 and hence l also divides (k+1)!+l=n2+l, ruling out n,n+1,,n+k1 as possible primes.

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 π: is defined as follows: For any real x, π(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/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.

Theorem 4.11.

limxπ(x)ln(x)x=1.

Two of the main open conjectures on prime numbers are the following:

Conjecture 4.1.

There exist infinitely many twin primes, i.e., primes p for which also p+2 is prime.

Conjecture 4.2. (Goldbach)

Every even number greater than 2 is the sum of two primes.

4.4.2. Remarks on Primality Testing

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 n and can instead simply try all odd numbers as possible divisors.

Lemma 4.12.

Every composite integer n has a prime divisor n.

Proof: If n is composite, it has a divisor a with 1<a<n. Hence n=ab for b>1. Either an or bn since otherwise ab>nn=n. Hence n has a divisor c with 1<cn. Either c is prime or, by Theorem 4.6, has a prime divisor d<cn.

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. [101023,1010241]) and to apply a general primality test. Primality testing is a very active research area. The record in general primality testing is around 8000 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 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 ϵ (e.g. ϵ=10100) and then perform a test such that for any composite integer, the probability that the test does not output "composite" is bounded by ϵ.

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 n1.

4.5. Congruences and Modular Arithmetic

4.5.1. Modular Congruences

We consider the following motivating example:

Example 4.8.

Fermat's famous "last theorem", proved recently by Wiles, states that the equation xn+yn=zn has no solution in positive integers x,y,z for n3. Here we consider a similar question. Does x3+x2=y4+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, x3+x2 is even. On the other hand, y4+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 x3x=y2+1 has no integer solutions.

Definition 4.8.

For a,b,m with m1, we say that a is congruent to b modulo m if m divides ab. We write ab (modm) or simply amb, i.e.,

ambdefm(ab).

Example 4.10.

We have 23744 and 54321101.
Note that a2b means that a and b are either both even or both odd.

Example 4.11.

If a2b and a3b, then a6b. 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  amb (4.1)

for all m, i.e., the relation m is reflexive (ama 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 m1, 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:

amb  ab.

The following lemma shows that modular congruences are compatible with the arithmetic operations on .

Lemma 4.14.

Ifambandcmd,  then

a+cmb+d   and   acmbd.

Proof: We only prove the first statement and leave the other proof as an exercise. We have m|(ab) and m|(cd). Hence m also divides (ab)+(cd)=(a+c)(b+d), which is the definition of a+cmb+d.

Corollary 4.15.

Let f(x1,,xk) be a multi-variate polynomial in k variables with integer coefficients, and let m1. If aimbi for 1ik, then

f(a1,,ak) m f(b1,,bk).

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.

4.5.2. Modular Arithmetic

There are m equivalence classes of the equivalence relation m, namely [0],[1],,[m1]. Each equivalence class [a] has a natural representative Rm(a)[a] in the set

m:={0,...,m1}

of remainders modulo m. [17]

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·7968328674·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 m1,

  1. amRm(a).
  2. ambRm(a)=Rm(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(x1,,xk) be a multi-variate polynomial in k variables with integer coefficients, and let m1. Then

Rm(f(a1,,ak))=Rm(f(Rm(a1),,Rm(ak))).

Proof: By Lemma 4.16 we have aimRm(ai) for all i. Therefore, using Corollary 4.15 we have f(a1,...,ak)mf(Rm(a1),,Rm(ak)). Thus, using Lemma 4.16 (ii) we obtain the statement to be proved.

Example 4.13.

Compute 7100 modulo 24. We make use of the fact that 72=49241.
Thus R24(7100)=R24((72)50)=R24(R24(72)50)=R24(150)=R24(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 Rm(n) with the remainder modulo m obtained by continuously reducing intermediate results of the computation. The modulus m=9 is especially suited because R9(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 R9(247)=R9(2+4+7)=4 and R9(3158)=R9(3+1+5+8)=8 to obtain R9(247·3158)=R9(4·8)=5. On the other hand we have R9(780026)=R9(7+8+2+6)=5. Hence the result can be correct.

Example 4.15.

A similar test can be performed for m=11. R11(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?

4.5.3. Multiplicative Inverses

Consider the problem of finding the solutions x for the congruence equation

axmb.

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 gcd(a,m)=1 and b=1.

Lemma 4.18.

The congruence equation

axm1

has a solution xm if and only if gcd(a,m)=1. The solution is unique.

Proof:
() If x satisfies axm1, then ax=km+1 for some k. Note that gcd(a,m) divides both a and m, hence also axkm, which is 1. Thus gcd(a,m)=1.

() Assume now that gcd(a,m)=1. According to Corollary 4.5 there exist integers u and v such that ua+vm=gcd(a,m)=1. Since vmm0 we have uam1. Hence x=u is a solution in , and thus x=Rm(u) is a solution in m.

To prove uniqueness of x in m, suppose there is another solution xm. Then axaxm0, thus a(xx)m0 and hence m divides a(xx). Since gcd(a,m)=1, m must divide (xx). [18] Therefore Rm(x)=Rm(x) and hence Rm(x) is the unique solution in m.

Definition 4.9.

If gcd(a,m)=1, the unique solution xm to the congruence equation axm1 is called the multiplicative inverse of a modulo m. One also uses the notation xma1 or xm1/a.

Example 4.17.

The multiplicative inverse of 5 modulo 13 is 8 since 5·8=40131.

The multiplicative inverse of a modulo m can efficiently be computed using the so-called extended Euclidean algorithm. Note that if gcd(a,m)1, then a has no multiplicative inverse modulo m.

4.5.4. The Chinese Remainder Theorem

We now consider a system of congruences for an integer x.

Example 4.18.

Find an integer x for which x31, x42, and x54. 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 m1,m2,,mr be pairwise relatively prime integers and let M=i=1rmi. For every list a1,,ar with 0ai<mi for 1ir, the system of congruence equations

xm1a1xm2a2xmrar

for x has a unique solution x satisfying 0x<M.

Proof: Let Mi=M/mi. Hence gcd(Mi,mi)=1 because every factor mk (where ki) of Mi is relatively prime to mi, and thus so is Mi. Thus there exists an Ni satisfying

MiNimi1.

Note that for all ki we have Mimk0 and thus

MiNimk0.

Therefore

i=1raiMiNi mk ak

for all k. Hence the integer x defined by

x=RM(i=1raiMiNi)

satisfies all the congruences. In order to prove uniqueness, observe that for two solutions x and x, xxmi0 for all i, i.e., xx is a multiple of all the mi and hence of lcm(m1,,mr)=M. Thus xMx.

The Chinese Remainder Theorem has several applications. When one is interested in a computation modulo M, then the moduli mi can be viewed as a coordinate system. One can project the numbers of interest modulo the mi, 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 3494143313821272161110010553012

Example 4.19.

Compute R35(21000). We can do this computation modulo 5 and modulo 7 separately. Since 2451 we have 2100051. Since 2371 we have 2100072. This yields 210003516 since 16 is the (unique) integer x[0,34] with x51 and x72.

4.6. Application: Diffie-Hellman Key-Agreement

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=Rp(gx) 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 xRp(gx), 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 yA and yB must be authenticated, but not secret.[22] It is easy to see that Alice and Bob end up with the same value kAB=kBA which they can use as a secret key for encrypting subsequent communication.[23] In order to compute kAB from yA and yB, an adversary would have to compute either xA or xB, which is believed to be infeasible.

Aliceinsecure channelBobselect xA at random select xB at random from {0,,p2}from {0,,p2}yA:=Rp(gxA)yAyB:=Rp(gxB)kAB:=Rp(yBxA)yBkBA:=Rp(yAxB)kAB p yBxA p (gxB)xA p gxAxB p kBA
Figure 4.1: The Diffie-Hellman key agreement protocol.

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.

Figure 4.2: Mechanical analog of the Diffie-Hellman protocol.

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.


  1. 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. ↩︎

  2. German: Teiler ↩︎

  3. German: Vielfaches ↩︎

  4. One can prove that it is unique. ↩︎

  5. German: Rest ↩︎

  6. Note that the term "greatest" does not refer to the order relation but to the divisibility relation. ↩︎

  7. German: teilerfremd ↩︎

  8. German: durch a und b erzeugtes Ideal ↩︎

  9. German: zusammengesetzt ↩︎

  10. Note that 1 is neither prime nor composite. ↩︎

  11. Note that 1 has zero prime factors, which is allowed. ↩︎

  12. German: Quinte ↩︎

  13. German: Quarte ↩︎

  14. German: Terz ↩︎

  15. German: wohltemperiert ↩︎

  16. M. Agrawal, N. Kayal, and N. Saxena, PRIMES is in P, Annals of Mathematics vol. 160, pp. 781– 793. ↩︎

  17. Recall that Rm(a) denotes the remainder when a is divided by m. ↩︎

  18. If k divides mn and gcd(k,n)=1, then k divides m. (Prove this!) ↩︎

  19. W. Diffie and M.E. Hellman, New directions in cryptography, IEEE Transactions on Information Theory, vol. 22, no. 6, pp. 644-654, 1976. ↩︎

  20. Since then, other versions, for instance based on elliptic curves, have been proposed. ↩︎

  21. 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. ↩︎

  22. This can be achieved by recognizing the other party's voice in a phone call or, indirectly, by the use of public-key certificates. ↩︎

  23. More precisely, they can derive a common secret key, for example by applying a hash function to KAB. ↩︎

  24. A padlock with a key corresponds to a so-called trapdoor one-way function which is not considered here. ↩︎

  25. They received the Turing award in 2003. ↩︎

No guarantee for correctness or completeness. Use at your own risk.
All rights belong to Ueli Maurer and respective authors.