1. Home
  2. Explore
  3. Mathematics
  4. Number Theory and Cryptography

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Number Theory and Cryptography

Number Theory and Cryptography

Number theory studies the properties of integers, including divisibility, primes, and remainders. Cryptography uses mathematical methods to protect messages and verify who sent them. Many cryptographic methods depend on modular arithmetic: calculations in which numbers wrap around after reaching a fixed modulus.

The main ideas build on one another: divisibility leads to remainders and congruences; prime numbers and greatest common divisors help determine when modular equations can be solved; and these tools support algorithms, error-detection methods, and cryptographic systems.


Divisibility and Modular Arithmetic

Divisibility

For integers aaa and bbb, with a≠0a \ne 0a=0, aaa divides bbb if there is an integer ccc such that

b=ac.b = ac.b=ac.

The notation a∣ba \mid ba∣b means that aaa divides bbb. If no such integer ccc exists, the notation is a∤ba \nmid ba∤b.

When a∣ba \mid ba∣b:

  • aaa is a divisor or factor of bbb.

  • bbb is a multiple of aaa.

  • The quotient b/ab/ab/a is an integer.

For example, 3∣123 \mid 123∣12 because 12=3⋅412 = 3 \cdot 412=3⋅4, but 3∤73 \nmid 73∤7 because 7/37/37/3 is not an integer.

Basic divisibility properties

For integers a,b,ca,b,ca,b,c, with a≠0a \ne 0a=0:

  1. If a∣ba \mid ba∣b and a∣ca \mid ca∣c, then a∣(b+c)a \mid (b+c)a∣(b+c).

  2. If a∣ba \mid ba∣b, then a∣bca \mid bca∣bc for every integer ccc.

  3. If a∣ba \mid ba∣b and b∣cb \mid cb∣c, then a∣ca \mid ca∣c.

For the first property, write b=asb=asb=as and c=atc=atc=at for integers sss and ttt. Then

b+c=as+at=a(s+t),b+c=as+at=a(s+t),b+c=as+at=a(s+t),

so aaa divides b+cb+cb+c.

A linear combination of bbb and ccc has the form mb+ncmb+ncmb+nc, where mmm and nnn are integers. If aaa divides both bbb and ccc, then it divides every linear combination of them:

a∣b,a∣c⟹a∣(mb+nc).a \mid b,\quad a \mid c \quad\Longrightarrow\quad a \mid (mb+nc).a∣b,a∣c⟹a∣(mb+nc).

This fact is used throughout number theory, particularly in arguments involving greatest common divisors.

The division algorithm

For any integer aaa and positive integer ddd, there is exactly one pair of integers qqq and rrr satisfying

a=dq+r,0≤r<d.a=dq+r,\qquad 0\le r<d.a=dq+r,0≤r<d.

The four quantities are:

  • Dividend: aaa, the number being divided.

  • Divisor: ddd, the positive number by which it is divided.

  • Quotient: qqq, written adiv⁡da \operatorname{ div } dadivd.

  • Remainder: rrr, written amod⁡da \operatorname{ mod } damodd.

The remainder must be nonnegative and strictly smaller than the divisor. The requirement matters for negative dividends. For example,

−11=3(−4)+1,-11=3(-4)+1,−11=3(−4)+1,

so −11div⁡3=−4-11 \operatorname{ div } 3=-4−11div3=−4 and −11mod⁡3=1-11 \operatorname{ mod } 3=1−11mod3=1. The expression −11=3(−3)−2-11=3(-3)-2−11=3(−3)−2 is also an equation, but it does not give the permitted remainder because −2<0-2<0−2<0.

For a positive example,

101=11(9)+2,101=11(9)+2,101=11(9)+2,

so 101div⁡11=9101 \operatorname{ div } 11=9101div11=9 and 101mod⁡11=2101 \operatorname{ mod } 11=2101mod11=2.

Congruences

Let mmm be a positive integer. Integers aaa and bbb are congruent modulo mmm if mmm divides their difference:

a≡b(modm)⟺m∣(a−b).a \equiv b \pmod m \quad\Longleftrightarrow\quad m \mid (a-b).a≡b(modm)⟺m∣(a−b).

The integer mmm is the modulus. Congruence means that the two integers have the same remainder when divided by mmm.

For example, 17≡5(mod6)17 \equiv 5 \pmod 617≡5(mod6) because 17−5=1217-5=1217−5=12, which is divisible by 666. In contrast, 24≢14(mod6)24 \not\equiv 14 \pmod 624≡14(mod6) because their difference, 101010, is not divisible by 666.

The equivalence

a≡b(modm)⟺a=b+kma \equiv b \pmod m \quad\Longleftrightarrow\quad a=b+kma≡b(modm)⟺a=b+km

holds for some integer kkk. Thus, congruent numbers differ by a whole-number multiple of the modulus. The value of kkk can be negative, corresponding to moving backward by steps of size mmm.

Congruence is different from the remainder function:

  • In a≡b(modm)a \equiv b \pmod ma≡b(modm), “modulo mmm” describes a relation that is either true or false.

  • In amod⁡ma \operatorname{ mod } mamodm, the expression gives the remainder after division.

The two ideas are connected by

a≡b(modm)⟺amod⁡m=bmod⁡m.a \equiv b \pmod m \quad\Longleftrightarrow\quad a \operatorname{ mod } m=b \operatorname{ mod } m.a≡b(modm)⟺amodm=bmodm.

A clock provides a useful model. On a twelve-hour clock, 333 o’clock and 151515 o’clock indicate the same position because their difference is 121212:

15≡3(mod12).15 \equiv 3 \pmod {12}.15≡3(mod12).

Addition and multiplication of congruences

Congruences can be added and multiplied. If

a≡b(modm)andc≡d(modm),a\equiv b\pmod m \quad\text{and}\quad c\equiv d\pmod m,a≡b(modm)andc≡d(modm),

then

a+c≡b+d(modm)a+c\equiv b+d\pmod ma+c≡b+d(modm)

and

ac≡bd(modm).ac\equiv bd\pmod m.ac≡bd(modm).

In calculations, a number can therefore be replaced by any congruent number before adding or multiplying. For example, modulo 555, 7≡27\equiv27≡2 and 11≡111\equiv111≡1. Therefore,

7+11≡2+1≡3(mod5)7+11\equiv2+1\equiv3\pmod 57+11≡2+1≡3(mod5)

and

7⋅11≡2⋅1≡2(mod5).7\cdot11\equiv2\cdot1\equiv2\pmod 5.7⋅11≡2⋅1≡2(mod5).

Adding the same integer to both sides of a congruence, or multiplying both sides by the same integer, preserves the congruence.

Division is not generally safe. For example, 14≡8(mod6)14\equiv8\pmod614≡8(mod6), but dividing both numbers by 222 gives 777 and 444, which are not congruent modulo 666. A precise condition for valid cancellation is given in the section on inverses and solving congruences.

Arithmetic modulo mmm

The set

Zm={0,1,…,m−1}\mathbb Z_m=\{0,1,\ldots,m-1\}Zm​={0,1,…,m−1}

contains one representative for each possible remainder modulo mmm. Arithmetic modulo $m means performing ordinary addition or multiplication and then taking the remainder modulo $m.

For example, in Z11\mathbb Z_{11}Z11​,

7+9≡16≡5(mod11)7+9\equiv16\equiv5\pmod{11}7+9≡16≡5(mod11)

and

7⋅9≡63≡8(mod11).7\cdot9\equiv63\equiv8\pmod{11}.7⋅9≡63≡8(mod11).

The results always belong to Z11\mathbb Z_{11}Z11​.

Modular arithmetic has many familiar properties:

  • Closure: Adding or multiplying two elements of Zm\mathbb Z_mZm​ produces another element of Zm\mathbb Z_mZm​.

  • Associativity: The grouping of additions or multiplications does not affect the result.

  • Commutativity: The order does not affect the result: a+b=b+aa+b=b+aa+b=b+a and ab=baab=baab=ba.

  • Identities: 000 is the additive identity and 111 is the multiplicative identity.

  • Additive inverses: Every element has an additive inverse. For nonzero aaa in Zm\mathbb Z_mZm​, its inverse is m−am-am−a; zero is its own additive inverse.

  • Distributivity: Multiplication distributes over addition: a(b+c)=ab+aca(b+c)=ab+aca(b+c)=ab+ac.

Multiplicative inverses do not always exist. In Z6\mathbb Z_6Z6​, no multiple of 222 is congruent to 111, so 222 has no multiplicative inverse modulo 666. The existence of an inverse depends on the greatest common divisor of the number and the modulus.


Integer Representations and Algorithms

Positional notation and bases

In a positional numeral system, each digit represents a multiple of a power of the base. In base ten,

965=9⋅102+6⋅10+5.965=9\cdot10^2+6\cdot10+5.965=9⋅102+6⋅10+5.

Any base b>1b>1b>1 can be used. Every positive integer nnn has a unique base-bbb expansion

n=akbk+ak−1bk−1+⋯+a1b+a0,n=a_kb^k+a_{k-1}b^{k-1}+\cdots+a_1b+a_0,n=ak​bk+ak−1​bk−1+⋯+a1​b+a0​,

where each digit satisfies 0≤ai<b0\le a_i<b0≤ai​<b, and the leading digit aka_kak​ is nonzero. A base is usually shown as a subscript; for instance, (1011)2(1011)_2(1011)2​ is a binary numeral.

The same written digits can represent different values in different bases. For example, (111)2=7(111)_2=7(111)2​=7 while (111)8=73(111)_8=73(111)8​=73.

Binary, octal, and hexadecimal

Binary is base two and uses the digits 000 and 111. Its place values, from right to left, are

1,2,4,8,16,…1,2,4,8,16,\ldots1,2,4,8,16,…

Each place is a power of two. A binary numeral is converted to decimal by adding the place values that have a digit of 111. For example,

(11011)2=16+8+2+1=27.(11011)_2=16+8+2+1=27.(11011)2​=16+8+2+1=27.

Computers commonly represent data in binary because electronic components can represent two distinct states.

Octal is base eight and uses the digits 000 through 777. Its place values are powers of eight: 1,8,64,512,…1,8,64,512,\ldots1,8,64,512,…. For example,

(7016)8=7⋅83+0⋅82+1⋅8+6=3598.(7016)_8=7\cdot8^3+0\cdot8^2+1\cdot8+6=3598.(7016)8​=7⋅83+0⋅82+1⋅8+6=3598.

Hexadecimal is base sixteen. It uses the digits 000 through 999 and the letters AAA through FFF, with

A=10,B=11,C=12,D=13,E=14,F=15.A=10,\quad B=11,\quad C=12,\quad D=13,\quad E=14,\quad F=15.A=10,B=11,C=12,D=13,E=14,F=15.

Its place values are powers of sixteen. For example,

(E5)16=14⋅16+5=229.(E5)_{16}=14\cdot16+5=229.(E5)16​=14⋅16+5=229.

Base choices also appear in history and everyday measurement: ancient Maya used base twenty, and ancient Babylonians used base sixty. The latter is reflected in the sixty minutes per hour and sixty seconds per minute.

Converting from decimal to another base

Repeated division converts a positive integer nnn to base bbb:

  1. Divide nnn by bbb. Record the remainder; it is the rightmost digit.

  2. Divide the quotient by bbb and record that remainder as the next digit.

  3. Continue dividing each new quotient by bbb.

  4. Stop when the quotient is zero.

  5. Read the remainders in reverse order, from last to first.

For example, converting 123451234512345 to base eight gives these successive remainders:

12345=8(1543)+1,12345=8(1543)+1,12345=8(1543)+1,
1543=8(192)+7,1543=8(192)+7,1543=8(192)+7,
192=8(24)+0,192=8(24)+0,192=8(24)+0,
24=8(3)+0,24=8(3)+0,24=8(3)+0,
3=8(0)+3.3=8(0)+3.3=8(0)+3.

Reading the remainders backward gives (30071)8(30071)_8(30071)8​. Checking confirms that

3⋅84+0⋅83+0⋅82+7⋅8+1=12345.3\cdot8^4+0\cdot8^3+0\cdot8^2+7\cdot8+1=12345.3⋅84+0⋅83+0⋅82+7⋅8+1=12345.

Direct conversion between binary, octal, and hexadecimal

Because 8=238=2^38=23, one octal digit corresponds exactly to a group of three binary bits. Because 16=2416=2^416=24, one hexadecimal digit corresponds to four binary bits. Conversion between these bases can therefore be done by regrouping bits rather than by performing full arithmetic.

  • For binary-to-octal conversion, group bits in threes starting from the right. Add leading zeros if necessary.

  • For binary-to-hexadecimal conversion, group bits in fours starting from the right. Again, add leading zeros if necessary.

  • Convert each group into its corresponding octal or hexadecimal digit.

For example, grouping a binary string into blocks of three yields one octal digit per block; grouping the same bits into blocks of four yields hexadecimal digits. Leading zeros used to complete the first group do not change the number’s value.

Binary addition

A computer can add binary numbers one bit at a time, moving from right to left. At each position, it adds the two input bits and the carry from the previous position. If the total is uuu, the output bit is the remainder when uuu is divided by 222, and the new carry is the quotient:

sum bit=umod⁡2,carry=⌊u2⌋.\text{sum bit}=u\operatorname{ mod }2, \qquad \text{carry}=\left\lfloor\frac{u}{2}\right\rfloor.sum bit=umod2,carry=⌊2u​⌋.

A final carry becomes a new leading bit. Adding two nnn-bit integers takes O(n)O(n)O(n) bit operations because each of the nnn positions requires a fixed amount of work.

Binary multiplication

Binary multiplication uses shifted partial products:

  1. For each bit of the multiplier, produce a partial product.

  2. If the bit is 111, shift the multiplicand left by that bit’s position. A shift left by jjj positions multiplies the number by 2j2^j2j.

  3. If the bit is 000, the partial product is zero.

  4. Add the partial products.

There are nnn partial products for an nnn-bit multiplier, and adding each one takes roughly O(n)O(n)O(n) bit operations. The resulting multiplication algorithm takes O(n2)O(n^2)O(n2) bit operations.

Fast modular exponentiation

Cryptographic computations often require finding bn(modm)b^n\pmod mbn(modm) when nnn and mmm are large. Repeatedly multiplying by bbb would require roughly nnn multiplications. Repeated squaring instead uses the binary expansion of the exponent.

If

n=∑iϵi2i,ϵi∈{0,1},n=\sum_i\epsilon_i2^i,\qquad \epsilon_i\in\{0,1\},n=∑i​ϵi​2i,ϵi​∈{0,1},

then

bn=∏ϵi=1b2i.b^n=\prod_{\epsilon_i=1}b^{2^i}.bn=∏ϵi​=1​b2i.

The powers b,b2,b4,b8,…b,b^2,b^4,b^8,\ldotsb,b2,b4,b8,… are obtained by repeatedly squaring. Only the terms corresponding to 111 bits in the exponent are multiplied together.

For example, 11=(1011)2=8+2+111=(1011)_2=8+2+111=(1011)2​=8+2+1, so

311=38⋅32⋅3.3^{11}=3^8\cdot3^2\cdot3.311=38⋅32⋅3.

Repeated squaring obtains 32=93^2=932=9, 34=813^4=8134=81, and 38=65613^8=656138=6561, after which the selected powers are multiplied. This requires only a number of multiplications proportional to log⁡n\log nlogn, rather than nnn.

For modular exponentiation, the remainder is taken after each multiplication. Intermediate values therefore stay below mmm. The process is essential in public-key cryptography because it makes enormous modular powers practical to compute.


Primes and Greatest Common Divisors

Prime and composite numbers

A prime is an integer greater than 111 whose only positive divisors are 111 and itself. An integer greater than 111 that is not prime is composite. The number 111 is neither prime nor composite.

Examples:

  • 777 is prime because no integer strictly between 111 and 777 divides it.

  • 999 is composite because 9=3⋅39=3\cdot39=3⋅3.

Unique prime factorization

Every integer greater than 111 can be written as a product of primes, and this factorization is unique up to the order of the factors. This is the Fundamental Theorem of Arithmetic. Primes can be thought of as the basic building blocks of the positive integers.

Examples include

100=22⋅52,999=33⋅37,1024=210.100=2^2\cdot5^2,\qquad 999=3^3\cdot37,\qquad 1024=2^{10}.100=22⋅52,999=33⋅37,1024=210.

The number 641641641 is prime; testing prime divisors up to 641\sqrt{641}641​ is sufficient to establish this.

The Sieve of Eratosthenes and trial division

The Sieve of Eratosthenes finds all primes up to a chosen bound:

  1. List the integers from 222 to the bound.

  2. Keep 222 and cross out its larger multiples.

  3. Continue with the next number not crossed out, crossing out its larger multiples.

  4. Stop when the numbers used for crossing out exceed the square root of the bound.

  5. The numbers left uncrossed are prime.

For numbers up to 100100100, only the primes 2,3,5,72,3,5,72,3,5,7 are needed, because 100=10\sqrt{100}=10100​=10. The primes left are the primes from 222 through 979797.

The reason the sieve can stop at the square root is that every composite number nnn has a prime divisor no larger than n\sqrt nn​. If n=abn=abn=ab and both factors were greater than n\sqrt nn​, their product would exceed nnn.

The same fact gives trial division: to test whether n>1n>1n>1 is prime, test possible divisors from 222 through n\sqrt nn​. If none divides nnn, then nnn is prime. This method is correct but impractical for very large numbers because the number of trial divisors grows dramatically.

Infinitely many primes

Euclid’s proof that there are infinitely many primes uses contradiction. Suppose there were only finitely many, listed as p1,p2,…,pnp_1,p_2,\ldots,p_np1​,p2​,…,pn​. Form

q=p1p2⋯pn+1.q=p_1p_2\cdots p_n+1.q=p1​p2​⋯pn​+1.

The integer qqq is either prime or has a prime divisor. None of the listed primes can divide qqq, because each would then divide both p1p2⋯pnp_1p_2\cdots p_np1​p2​⋯pn​ and qqq, and so would divide their difference, 111. Thus qqq has a prime divisor missing from the alleged complete list, a contradiction. Therefore, there are infinitely many primes.

Special forms and open questions about primes

A Mersenne prime has the form 2p−12^p-12p−1, where ppp is prime, and the resulting number is itself prime. For instance, 22−1=32^2-1=322−1=3, 23−1=72^3-1=723−1=7, and 25−1=312^5-1=3125−1=31 are prime, while

211−1=2047=23⋅892^{11}-1=2047=23\cdot89211−1=2047=23⋅89

is not. A prime exponent does not guarantee a Mersenne prime. Mersenne numbers are important partly because specialized fast tests exist for this form.

The Prime Number Theorem estimates the number of primes up to a large number xxx as approximately

xln⁡x.\frac{x}{\ln x}.lnxx​.

Equivalently, a randomly chosen positive integer near nnn has probability roughly 1/ln⁡n1/\ln n1/lnn of being prime. Primes become less frequent as numbers grow, but they do not stop appearing.

No nonconstant polynomial with integer coefficients produces a prime for every integer input. A polynomial can produce many prime values in a row and still eventually fail. For example,

f(n)=n2−n+41f(n)=n^2-n+41f(n)=n2−n+41

is prime for n=1n=1n=1 through 404040, but at n=41n=41n=41 it equals 41241^2412, which is composite. This illustrates the difference between evidence and proof.

Important conjectures remain unresolved:

  • Goldbach’s conjecture: Every even integer greater than 222 is the sum of two primes. It has been checked computationally over an enormous range, but no general proof is known.

  • Infinitely many primes of the form n2+1n^2+1n2+1: This is believed to be true but has not been proved. A weaker result establishes that infinitely many such values are prime or products of at most two primes.

  • Twin prime conjecture: There are infinitely many prime pairs that differ by 222, such as 333 and 555 or 111111 and 131313. No proof is known.

Greatest common divisor and relative primality

A common divisor of two integers divides both. Their greatest common divisor, written gcd⁡(a,b)\gcd(a,b)gcd(a,b), is the largest positive common divisor.

For example, the divisors shared by 242424 and 363636 include 1,2,3,4,6,121,2,3,4,6,121,2,3,4,6,12, so

gcd⁡(24,36)=12.\gcd(24,36)=12.gcd(24,36)=12.

Since 171717 is prime and does not divide 222222, the only common positive divisor of 171717 and 222222 is 111:

gcd⁡(17,22)=1.\gcd(17,22)=1.gcd(17,22)=1.

Two integers are relatively prime or coprime if their greatest common divisor is 111. A list is pairwise relatively prime if every pair in it is relatively prime. It is not enough for the list as a whole to have no single factor shared by every entry; every pair must be checked. For example, 10,17,2110,17,2110,17,21 are pairwise relatively prime, but 10,19,2410,19,2410,19,24 are not because gcd⁡(10,24)=2\gcd(10,24)=2gcd(10,24)=2.

Finding the greatest common divisor from prime factorizations

If the prime factorizations of aaa and bbb are known, the greatest common divisor is formed by taking each prime to the smaller of its exponents in the two factorizations. A missing prime has exponent zero.

For example,

120=23⋅3⋅5,500=22⋅53.120=2^3\cdot3\cdot5,\qquad 500=2^2\cdot5^3.120=23⋅3⋅5,500=22⋅53.

Taking the smaller exponents gives

gcd⁡(120,500)=22⋅30⋅51=20.\gcd(120,500)=2^2\cdot3^0\cdot5^1=20.gcd(120,500)=22⋅30⋅51=20.

This method can be inefficient for large numbers because finding prime factorizations is difficult.

Least common multiple

The least common multiple, written lcm⁡(a,b)\operatorname{lcm}(a,b)lcm(a,b), is the smallest positive integer divisible by both aaa and bbb. From prime factorizations, it is found by taking each prime to the larger of its exponents.

For example, if the factorizations are 23⋅35⋅722^3\cdot3^5\cdot7^223⋅35⋅72 and 24⋅332^4\cdot3^324⋅33, then

lcm⁡=24⋅35⋅72.\operatorname{lcm}=2^4\cdot3^5\cdot7^2.lcm=24⋅35⋅72.

For positive integers,

ab=gcd⁡(a,b)lcm⁡(a,b).ab=\gcd(a,b)\operatorname{lcm}(a,b).ab=gcd(a,b)lcm(a,b).

For each prime, the gcd uses the smaller exponent and the lcm uses the larger one; together, these are the two exponents that occur in the product ababab.

The Euclidean algorithm

The Euclidean algorithm computes a greatest common divisor without factoring. If

a=bq+r,a=bq+r,a=bq+r,

then

gcd⁡(a,b)=gcd⁡(b,r).\gcd(a,b)=\gcd(b,r).gcd(a,b)=gcd(b,r).

It repeatedly replaces the pair by the divisor and remainder until the remainder is zero. The last nonzero remainder is the greatest common divisor.

For example,

287=91(3)+14,287=91(3)+14,287=91(3)+14,
91=14(6)+7,91=14(6)+7,91=14(6)+7,
14=7(2)+0.14=7(2)+0.14=7(2)+0.

The last nonzero remainder is 777, so gcd⁡(287,91)=7\gcd(287,91)=7gcd(287,91)=7.

The key reason the algorithm works is that the pairs (a,b)(a,b)(a,b) and (b,r)(b,r)(b,r) have exactly the same common divisors. If a number divides both aaa and bbb, it divides a−bq=ra-bq=ra−bq=r. Conversely, if it divides both bbb and rrr, it divides bq+r=abq+r=abq+r=a.

The algorithm terminates because each remainder is a nonnegative integer smaller than the previous divisor. Such a sequence cannot decrease forever. Its running time is logarithmic in the size of the smaller input, so it is efficient even for very large integers.

Bézout’s identity

For positive integers aaa and bbb, there are integers sss and ttt such that

gcd⁡(a,b)=sa+tb.\gcd(a,b)=sa+tb.gcd(a,b)=sa+tb.

This is Bézout’s identity. The integers sss and ttt are called Bézout coefficients and may be negative. For example,

2=(−2)(6)+1(14),2=(-2)(6)+1(14),2=(−2)(6)+1(14),

so −2-2−2 and 111 are Bézout coefficients for 666 and 141414.

The coefficients can be found by two passes through the Euclidean algorithm:

  1. Forward pass: Write down all divisions in the Euclidean algorithm until the gcd is reached.

  2. Backward pass: Start with the last nonzero remainder and repeatedly substitute earlier remainder expressions until the gcd is written as a linear combination of the original inputs.

For example,

252=1(198)+54,252=1(198)+54,252=1(198)+54,
198=3(54)+36,198=3(54)+36,198=3(54)+36,
54=1(36)+18.54=1(36)+18.54=1(36)+18.

The last nonzero remainder is 181818. Back-substitution gives

18=4(252)−5(198).18=4(252)-5(198).18=4(252)−5(198).

The extended Euclidean algorithm carries the coefficients along during the forward pass, avoiding a separate backward pass.

Consequences of Bézout’s identity

If gcd⁡(a,b)=1\gcd(a,b)=1gcd(a,b)=1 and a∣bca\mid bca∣bc, then a∣ca\mid ca∣c. Bézout’s identity gives integers s,ts,ts,t such that sa+tb=1sa+tb=1sa+tb=1. Multiplying by ccc gives

sac+tbc=c.sac+tbc=c.sac+tbc=c.

Both terms on the left are divisible by aaa: the first contains a factor of aaa, and the second is divisible by aaa because a∣bca\mid bca∣bc. Therefore, a∣ca\mid ca∣c.

A related fact applies to primes: if a prime ppp divides a product a1a2⋯ana_1a_2\cdots a_na1​a2​⋯an​, then ppp divides at least one factor. This property is important in proving the uniqueness of prime factorization.

Uniqueness of prime factorization

To see why prime factorization is unique, suppose an integer had two different prime factorizations. Cancel all primes appearing in both lists. A prime remaining on one side divides the product of the primes on the other side. By the prime-divides-a-product property, it must divide one of those primes. Since a prime’s only positive divisors are 111 and itself, the two primes must be equal, contradicting the cancellation of all shared primes. Therefore, once primes are arranged in order, the factorization is unique.

Cancelling in a congruence

Suppose

ac≡bc(modm)ac\equiv bc\pmod mac≡bc(modm)

and gcd⁡(c,m)=1\gcd(c,m)=1gcd(c,m)=1. Then ccc can be cancelled:

a≡b(modm).a\equiv b\pmod m.a≡b(modm).

The coprimality condition is essential. The earlier example 14≡8(mod6)14\equiv8\pmod614≡8(mod6) can be written as 2⋅7≡2⋅4(mod6)2\cdot7\equiv2\cdot4\pmod62⋅7≡2⋅4(mod6), but 222 cannot be cancelled because gcd⁡(2,6)=2\gcd(2,6)=2gcd(2,6)=2. When the common factor of ccc and mmm is 111, Bézout’s identity supplies the divisibility argument that makes cancellation valid.


Solving Congruences

Linear congruences and modular inverses

A linear congruence has the form

ax≡b(modm),ax\equiv b\pmod m,ax≡b(modm),

where xxx is unknown. If xxx is a solution, then x+kmx+kmx+km is also a solution for every integer kkk, so solutions occur in congruence classes.

A multiplicative inverse of aaa modulo mmm is an integer aˉ\bar aaˉ satisfying

aˉa≡1(modm).\bar a a\equiv1\pmod m.aˉa≡1(modm).

An inverse acts like a reciprocal in modular arithmetic. It allows a coefficient to be removed from a congruence by multiplying both sides by the inverse; direct division is not generally safe.

An inverse exists exactly when

gcd⁡(a,m)=1.\gcd(a,m)=1.gcd(a,m)=1.

When it exists, it is unique modulo mmm. Bézout’s identity explains why: if gcd⁡(a,m)=1\gcd(a,m)=1gcd(a,m)=1, then there are integers s,ts,ts,t with

sa+tm=1.sa+tm=1.sa+tm=1.

Reducing modulo mmm removes the multiple tmtmtm and gives sa≡1(modm)sa\equiv1\pmod msa≡1(modm), so sss is an inverse of aaa.

For example, the Euclidean algorithm gives

7=2(3)+1,7=2(3)+1,7=2(3)+1,

so 1=7−2(3)1=7-2(3)1=7−2(3). Modulo 777, this means (−2)3≡1(mod7)(-2)3\equiv1\pmod7(−2)3≡1(mod7). Thus −2-2−2 is an inverse of 333 modulo 777, and its representative in Z7\mathbb Z_7Z7​ is 555. Indeed, 5⋅3=15≡1(mod7)5\cdot3=15\equiv1\pmod75⋅3=15≡1(mod7).

To solve 3x≡4(mod7)3x\equiv4\pmod73x≡4(mod7), multiply both sides by the inverse −2-2−2:

x≡(−2)4≡−8≡6(mod7).x\equiv(-2)4\equiv-8\equiv6\pmod7.x≡(−2)4≡−8≡6(mod7).

Thus every solution is congruent to 666 modulo 777.

An inverse does not always exist, and a linear congruence can have no solution or several solution classes. For example, 2x≡3(mod6)2x\equiv3\pmod62x≡3(mod6) has no solution because the left side is always even modulo 666. In contrast, 2x≡2(mod6)2x\equiv2\pmod62x≡2(mod6) has two solution classes: x≡1(mod6)x\equiv1\pmod6x≡1(mod6) and x≡4(mod6)x\equiv4\pmod6x≡4(mod6).

Finding an inverse by the extended Euclidean algorithm

To find the inverse of aaa modulo mmm:

  1. Check that gcd⁡(a,m)=1\gcd(a,m)=1gcd(a,m)=1.

  2. Run the Euclidean algorithm on aaa and mmm.

  3. Use back-substitution to express 111 as a linear combination of aaa and mmm.

  4. Reduce that equation modulo mmm. The coefficient of aaa is an inverse.

For example, to find the inverse of 101101101 modulo 462046204620, the Euclidean algorithm ends with gcd 111. Back-substitution gives

1=1601(101)−35(4620).1=1601(101)-35(4620).1=1601(101)−35(4620).

Modulo 462046204620, the term −35(4620)-35(4620)−35(4620) is congruent to zero. Therefore,

1601(101)≡1(mod4620),1601(101)\equiv1\pmod{4620},1601(101)≡1(mod4620),

so 160116011601 is an inverse of 101101101 modulo 462046204620.

The Chinese Remainder Theorem

The Chinese Remainder Theorem solves a system of congruences with pairwise relatively prime moduli. If m1,…,mnm_1,\ldots,m_nm1​,…,mn​ are pairwise relatively prime, then for any target remainders a1,…,ana_1,\ldots,a_na1​,…,an​, the system

x≡ak(modmk)x\equiv a_k\pmod{m_k}x≡ak​(modmk​)

has exactly one solution modulo

M=m1m2⋯mn.M=m_1m_2\cdots m_n.M=m1​m2​⋯mn​.

“Unique modulo MMM” means there is one representative from 000 through M−1M-1M−1; every other integer solution differs from it by a multiple of MMM.

A constructive proof also gives a method for finding the solution:

  1. Compute M=∏kmkM=\prod_k m_kM=∏k​mk​.

  2. For each kkk, define Mk=M/mkM_k=M/m_kMk​=M/mk​.

  3. Since the moduli are pairwise relatively prime, MkM_kMk​ is relatively prime to mkm_kmk​.

  4. Find yky_kyk​, the inverse of MkM_kMk​ modulo mkm_kmk​.

  5. Form $$ x=\sum_k a_kM_ky_k. $$

Modulo mkm_kmk​, every term other than akMkyka_kM_ky_kak​Mk​yk​ is zero because it contains the factor mkm_kmk​. The remaining term is congruent to aka_kak​, so the constructed xxx satisfies every congruence.

For the system

x≡2(mod3),x≡3(mod5),x≡2(mod7),x\equiv2\pmod3,\qquad x\equiv3\pmod5,\qquad x\equiv2\pmod7,x≡2(mod3),x≡3(mod5),x≡2(mod7),

the product modulus is M=105M=105M=105. The corresponding values are M1=35M_1=35M1​=35, M2=21M_2=21M2​=21, and M3=15M_3=15M3​=15. Their inverses modulo the respective moduli are 2,1,12,1,12,1,1. Thus,

x=2(35)(2)+3(21)(1)+2(15)(1)=233≡23(mod105).x=2(35)(2)+3(21)(1)+2(15)(1)=233\equiv23\pmod{105}.x=2(35)(2)+3(21)(1)+2(15)(1)=233≡23(mod105).

The value 232323 leaves remainders 2,3,22,3,22,3,2 upon division by 3,5,73,5,73,5,7, respectively. It is the smallest positive solution, and all solutions are congruent to 232323 modulo 105105105.

Back-substitution for systems

A second way to solve a system is to turn one congruence at a time into an equation and substitute into the next. The equivalence

x≡a(modm)⟺x=a+mtx\equiv a\pmod m \quad\Longleftrightarrow\quad x=a+mtx≡a(modm)⟺x=a+mt

for an integer ttt makes this possible.

For example, begin with x≡1(mod5)x\equiv1\pmod5x≡1(mod5), so write x=5t+1x=5t+1x=5t+1. Substituting into x≡2(mod6)x\equiv2\pmod6x≡2(mod6) gives 5t≡1(mod6)5t\equiv1\pmod65t≡1(mod6). Since 555 is its own inverse modulo 666, t≡5(mod6)t\equiv5\pmod6t≡5(mod6), so write t=6u+5t=6u+5t=6u+5. Then x=30u+26x=30u+26x=30u+26. Substitution into x≡3(mod7)x\equiv3\pmod7x≡3(mod7) gives 2u+5≡3(mod7)2u+5\equiv3\pmod72u+5≡3(mod7), so u≡6(mod7)u\equiv6\pmod7u≡6(mod7). Writing u=7v+6u=7v+6u=7v+6 gives

x=210v+206,x=210v+206,x=210v+206,

and therefore x≡206(mod210)x\equiv206\pmod{210}x≡206(mod210).

Fermat’s Little Theorem

If ppp is prime and p∤ap\nmid ap∤a, Fermat’s Little Theorem states that

ap−1≡1(modp).a^{p-1}\equiv1\pmod p.ap−1≡1(modp).

A related form, valid for every integer aaa, is

ap≡a(modp).a^p\equiv a\pmod p.ap≡a(modp).

The theorem simplifies large exponents. For example, modulo 111111, 710≡17^{10}\equiv1710≡1. Since 222=22(10)+2222=22(10)+2222=22(10)+2,

7222=(710)2272≡122⋅49≡5(mod11).7^{222}=(7^{10})^{22}7^2\equiv1^{22}\cdot49\equiv5\pmod{11}.7222=(710)2272≡122⋅49≡5(mod11).

Pseudoprimes and primality testing

Fermat’s theorem can produce a useful compositeness test. If n>2n>2n>2 and

an−1≢1(modn),a^{n-1}\not\equiv1\pmod n,an−1≡1(modn),

then nnn is definitely composite. If the congruence holds, however, it does not prove that nnn is prime. A composite number that passes the test for a particular base is a pseudoprime to that base.

For example, 341=11⋅31341=11\cdot31341=11⋅31 is composite, but

210=1024≡1(mod341).2^{10}=1024\equiv1\pmod{341}.210=1024≡1(mod341).

Because 340=34(10)340=34(10)340=34(10), this implies 2340≡1(mod341)2^{340}\equiv1\pmod{341}2340≡1(mod341), so 341341341 is a pseudoprime to base 222.

Testing several bases makes a false positive less likely. Pseudoprimes are much less common than primes over many ranges, but these tests provide evidence rather than a proof of primality.

Primitive roots and discrete logarithms

For a prime ppp, a primitive root modulo $p is a number rrr whose powers produce every nonzero element of Zp\mathbb Z_pZp​ before repeating. It is a generator of the nonzero residues modulo ppp.

For example, the powers of 222 modulo 111111 are

2,4,8,5,10,9,7,3,6,1.2,4,8,5,10,9,7,3,6,1.2,4,8,5,10,9,7,3,6,1.

They include every nonzero residue, so 222 is a primitive root modulo 111111. By contrast, the powers of 333 modulo 111111 return to 111 after five powers and reach only five nonzero values, so 333 is not a primitive root.

Every prime has at least one primitive root. If rrr is a primitive root and aaa is nonzero modulo ppp, there is a unique exponent eee modulo p−1p-1p−1 such that

re≡a(modp).r^e\equiv a\pmod p.re≡a(modp).

This exponent is the discrete logarithm of aaa to the base rrr modulo ppp. For example, modulo 111111 with base 222, the discrete logarithm of 333 is 888, because 28≡3(mod11)2^8\equiv3\pmod{11}28≡3(mod11).

Computing a modular power from a known exponent is efficient using repeated squaring. Recovering the exponent from the result is generally believed to be difficult for large parameters. This asymmetry is useful in cryptography.


Applications of Congruences

Hashing

A hashing function maps a record’s key to a location in a table. A simple hash function is

h(k)=kmod⁡m,h(k)=k\operatorname{ mod }m,h(k)=kmodm,

where mmm is the number of locations. Its output lies between 000 and m−1m-1m−1.

Different keys can map to the same location; this is a collision. Collisions are unavoidable when the set of possible keys is larger than the table.

One simple collision-handling method is linear probing. If the location h(k)h(k)h(k) is occupied, test

h(k)+1, h(k)+2, h(k)+3,…(modm)h(k)+1,\ h(k)+2,\ h(k)+3,\ldots \pmod mh(k)+1, h(k)+2, h(k)+3,…(modm)

until an open location is found. The modular reduction makes the search wrap from the end of the table to the beginning.

Pseudorandom numbers

A computer follows deterministic instructions, so a sequence generated by a fixed rule is not truly random. It may nevertheless appear random; such numbers are called pseudorandom.

A common method is the linear congruential generator:

xn+1=(axn+c)mod⁡m,x_{n+1}=(ax_n+c)\operatorname{ mod }m,xn+1​=(axn​+c)modm,

where mmm is the modulus, aaa the multiplier, ccc the increment, and x0x_0x0​ the seed. Each output lies between 000 and m−1m-1m−1. Dividing outputs by mmm produces values between 000 and 111.

For example, with m=9m=9m=9, a=7a=7a=7, c=4c=4c=4, and x0=3x_0=3x0​=3, the sequence is

3,7,8,6,1,2,0,4,5,3,…3,7,8,6,1,2,0,4,5,3,\ldots3,7,8,6,1,2,0,4,5,3,…

The sequence repeats after nine outputs. The period is the number of outputs before the sequence begins repeating. A useful generator needs a long period. If c=0c=0c=0, the generator is pure multiplicative; a zero seed then produces zero forever. Some parameter choices can give very long periods, but the quality of a generator depends on more than its period.

Check digits

A check digit is an extra digit chosen so that a weighted sum of the digits satisfies a modular condition. When a digit is mistyped, the weighted sum often changes and the error is detected. A check digit can reveal that a string is invalid without necessarily identifying or correcting the error.

Universal Product Code

A twelve-digit Universal Product Code uses the last digit as its check digit. For the first eleven digits:

  • Digits in odd-numbered positions are multiplied by 333.

  • Digits in even-numbered positions are used as they are.

  • The check digit is chosen so the total, including that digit, is congruent to 000 modulo 101010.

For example, if the weighted sum of the first eleven digits is 989898, then the check digit is 222, because 98+2=10098+2=10098+2=100, a multiple of 101010. The alternating weights also help detect transpositions of neighboring digits.

ISBN-10

An ISBN-10 uses weights 111 through 101010 on its ten digits and checks whether

∑i=110i di≡0(mod11),\sum_{i=1}^{10}i\,d_i\equiv0\pmod{11},∑i=110​idi​≡0(mod11),

where did_idi​ is the digit in position iii. The final check symbol may be XXX, which represents 101010.

If the first nine digits contribute 189189189 to the weighted sum, the check digit d10d_{10}d10​ must satisfy

189+10d10≡0(mod11).189+10d_{10}\equiv0\pmod{11}.189+10d10​≡0(mod11).

Since 189≡2(mod11)189\equiv2\pmod{11}189≡2(mod11) and 10≡−1(mod11)10\equiv-1\pmod{11}10≡−1(mod11), this gives d10=2d_{10}=2d10​=2. If the required check symbol is 101010, it is written as XXX.

The prime modulus 111111 makes ISBN-10 particularly effective at detecting single-digit errors and transpositions. The weighted sum changes in a way that cannot be cancelled by a nonzero weight modulo a prime.


Cryptography

The Caesar cipher and shift ciphers

A cipher transforms a plaintext message into ciphertext so that only someone with the appropriate key can recover it. Encryption creates the concealed message; decryption reverses the transformation.

The Caesar cipher shifts each letter three positions forward, wrapping from the end of the alphabet to the beginning. To express it mathematically, encode letters as numbers from 000 to 252525 and encrypt a letter value ppp by

f(p)=(p+3)mod⁡26.f(p)=(p+3)\operatorname{ mod }26.f(p)=(p+3)mod26.

For example, YYY is 242424, so it becomes (24+3)mod⁡26=1(24+3)\operatorname{ mod }26=1(24+3)mod26=1, or BBB. Decryption shifts back:

f−1(p)=(p−3)mod⁡26.f^{-1}(p)=(p-3)\operatorname{ mod }26.f−1(p)=(p−3)mod26.

A shift cipher generalizes the Caesar cipher by using a key kkk:

encryption: p↦(p+k)mod⁡26,\text{encryption: }p\mapsto(p+k)\operatorname{ mod }26,encryption: p↦(p+k)mod26,
decryption: c↦(c−k)mod⁡26.\text{decryption: }c\mapsto(c-k)\operatorname{ mod }26.decryption: c↦(c−k)mod26.

There are only 262626 possible keys, so an attacker can try every key. This is a brute-force attack.

Affine ciphers

An affine cipher encrypts a letter value ppp by

f(p)=(ap+b)mod⁡26.f(p)=(ap+b)\operatorname{ mod }26.f(p)=(ap+b)mod26.

The cipher must be one-to-one so that decryption is possible. This happens exactly when

gcd⁡(a,26)=1.\gcd(a,26)=1.gcd(a,26)=1.

For example, a=7a=7a=7 and b=3b=3b=3 give a valid cipher because gcd⁡(7,26)=1\gcd(7,26)=1gcd(7,26)=1. The letter KKK has value 101010, so it encrypts to

(7⋅10+3)mod⁡26=21,(7\cdot10+3)\operatorname{ mod }26=21,(7⋅10+3)mod26=21,

which is VVV.

To decrypt, solve c≡ap+b(mod26)c\equiv ap+b\pmod{26}c≡ap+b(mod26). Since aaa has an inverse aˉ\bar aaˉ modulo 262626,

p≡aˉ(c−b)(mod26).p\equiv\bar a(c-b)\pmod{26}.p≡aˉ(c−b)(mod26).

The condition that makes the affine map invertible is the same condition that guarantees the needed modular inverse.

Cryptanalysis and frequency analysis

Cryptanalysis is the analysis of ciphertext to recover a message or key without being given the key. In English, some letters occur more frequently than others; EEE is especially common.

A shift cipher preserves the pattern of letter frequencies while shifting the letters. An attacker can:

  1. Count the occurrences of each ciphertext letter.

  2. Guess that the most common ciphertext letter corresponds to EEE.

  3. Infer a candidate shift.

  4. Decrypt and check whether the result resembles English.

  5. Try another likely correspondence if the first guess fails.

For example, if KKK is the most common ciphertext letter and is assumed to represent EEE, the shift is 666. Shifting the ciphertext backward by 666 can reveal a message such as “THE EARLY BIRD GETS THE WORM.”

Ciphers that substitute one fixed ciphertext symbol for each plaintext symbol are monoalphabetic. Their letter-frequency patterns remain visible, making frequency analysis effective.

Transposition ciphers and block ciphers

A transposition cipher rearranges letter positions without changing the letters. Its key is a permutation of the positions in a fixed-size block. To encrypt, divide the message into blocks, rearrange each block according to the permutation, and pad the final block if needed. Decryption uses the inverse permutation.

A transposition cipher changes the order of letters but preserves every single-letter frequency exactly. Therefore, block processing alone does not guarantee protection from frequency analysis. More generally, block ciphers transform groups of symbols together, so a plaintext letter need not always map to the same ciphertext letter.

Cryptosystems

A cryptosystem can be described by five components:

  • PPP: the set of plaintext messages.

  • CCC: the set of ciphertext messages.

  • KKK: the keyspace, the set of possible keys.

  • EEE: the set of encryption functions.

  • DDD: the set of decryption functions.

For a key kkk, encryption is written EkE_kEk​ and the corresponding decryption is written DkD_kDk​. A basic correctness requirement is

Dk(Ek(p))=pD_k(E_k(p))=pDk​(Ek​(p))=p

for every plaintext ppp: decrypting an encrypted message must recover the original.

In a shift cipher, messages are strings of values in Z26\mathbb Z_{26}Z26​, the key is a shift k∈Z26k\in\mathbb Z_{26}k∈Z26​, encryption adds kkk modulo 262626, and decryption subtracts kkk modulo 262626.

Private-key and public-key cryptography

In private-key cryptography, the sender and receiver share a secret key. Knowing the encryption key generally makes it possible to find the decryption key, so both parties must protect the shared secret and arrange to exchange it securely.

Public-key cryptography separates the keys. The encryption key can be made public, allowing anyone to encrypt a message for its owner, while the owner keeps the decryption key secret. The design aims to make encryption easy but decryption infeasible without the private key.

RSA

RSA is a public-key cryptosystem based on modular exponentiation and the difficulty of factoring large integers.

Key generation

  1. Choose two distinct large primes ppp and qqq.

  2. Compute n=pqn=pqn=pq.

  3. Choose an exponent eee relatively prime to $$ (p-1)(q-1). $$

  4. Find ddd, the inverse of eee modulo (p−1)(q−1)(p-1)(q-1)(p−1)(q−1): $$ ed\equiv1\pmod{(p-1)(q-1)}. $$

The public key is (n,e)(n,e)(n,e); the private key includes ddd. The primes ppp and qqq must also remain secret.

Encryption and decryption

A plaintext block MMM is encrypted by

C≡Me(modn).C\equiv M^e\pmod n.C≡Me(modn).

It is decrypted by

M≡Cd(modn).M\equiv C^d\pmod n.M≡Cd(modn).

Messages are encoded as numbers and split into blocks smaller than nnn. In a simple letter encoding, each letter can be represented by a two-digit number; the blocks must be short enough to remain below the modulus.

A small demonstration uses p=43p=43p=43 and q=59q=59q=59, so n=2537n=2537n=2537, with public exponent e=13e=13e=13. Here

(p−1)(q−1)=42⋅58=2436.(p-1)(q-1)=42\cdot58=2436.(p−1)(q−1)=42⋅58=2436.

Since 131313 is relatively prime to 243624362436, it has an inverse modulo 243624362436; that inverse is d=937d=937d=937. In an actual RSA system, the primes and modulus are much larger. The small example is for illustration only.

Large powers are computed by repeated squaring, reducing modulo nnn at each step. The full, enormous integer power is never constructed.

Why RSA is intended to be secure

Multiplying two large primes is efficient, and finding large probable primes is practical. Given n=pqn=pqn=pq, recovering ppp and qqq by factoring is believed to be difficult for appropriately large nnn. If an attacker can factor nnn, the attacker can compute (p−1)(q−1)(p-1)(q-1)(p−1)(q−1) and then find ddd. The security claim depends on the practical difficulty of factoring; it is not a proof that factoring is impossible.

Diffie–Hellman key exchange

The Diffie–Hellman protocol lets two parties establish a shared key over a public channel:

  1. Alice and Bob publicly agree on a prime ppp and a primitive root aaa modulo ppp.

  2. Alice chooses a private exponent k1k_1k1​ and sends $$ a^{k_1}\pmod p. $$

  3. Bob chooses a private exponent k2k_2k2​ and sends $$ a^{k_2}\pmod p. $$

  4. Alice raises Bob’s value to k1k_1k1​; Bob raises Alice’s value to k2k_2k2​.

Both obtain the same key because

(ak2)k1≡ak1k2≡(ak1)k2(modp).(a^{k_2})^{k_1}\equiv a^{k_1k_2}\equiv(a^{k_1})^{k_2}\pmod p.(ak2​)k1​≡ak1​k2​≡(ak1​)k2​(modp).

An eavesdropper sees the public parameters and the two exchanged powers, but recovering a private exponent from one of those powers requires solving a discrete logarithm problem. For sufficiently large parameters, this is intended to be computationally difficult.

Digital signatures

A digital signature aims to establish authenticity and integrity rather than confidentiality. A receiver should be able to verify that a message was signed by the holder of a particular private key.

In a simplified RSA signature scheme, a sender uses the private exponent ddd to transform a message block, and anyone can apply the public exponent eee to verify that the original block is recovered. The fact that the private key is required to produce a valid transformation supports the claim that the holder signed it.

This raw method is only a teaching model. Signing message blocks directly is insecure: an attacker can choose a number sss and compute se(modn)s^e\pmod nse(modn), producing a pair that passes the basic RSA check. Real systems instead sign a carefully designed hash of the message using secure signature padding. Hashing binds the signature to the message while avoiding the vulnerabilities of raw textbook RSA.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent