Mathematics for Computer Science 201-N11 • CEGEP, Montreal

Corrected exercises: modular arithmetic (201-N11)

This is the corrected exercise set on modular arithmetic for Mathematics for Computer Science, 201-N11, the mathematics course of CEGEP computer science programs: Euclidean division, congruences, the GCD, the modular inverse, Fermat's theorem and applications to cryptography. It is the chapter that links elementary arithmetic to the tools actually used in computing.

Exercises 0.1 and 0.2 open the set with two lists of direct calculations, as in textbook drill lists: nine remainders to compute modulo nn, from a negative integer such as −38 mod 7-38\bmod 7 to a sum and a product reduced term by term, up to the last digit of 7837^{83} by the cycle of powers and 2405 mod 132^{405}\bmod 13 by Fermat's little theorem, plus a GCD by the Euclidean algorithm; then eight modular inverses and congruences to solve, from the inverse of 55 modulo 1111 to Bézout coefficients by the extended Euclidean algorithm and the congruence 8x≡12(mod28)8x\equiv 12\pmod{28} with its four solutions. Every line has its value to find, checked as you type it.

The thread of the set fits in one sentence: we no longer care about the quotient but about the REMAINDER, and computing with remainders is legitimate because congruence is compatible with addition and multiplication. Everything else, from check digits to RSA, follows from that compatibility and from its single exception, division.

Part A builds the tools, from Euclidean division to linear congruences. Part B puts them together: Bézout, Fermat, fast exponentiation, then two complete applications, the check digit of an ISBN code and an RSA encryption carried out from start to finish. Every solution is on the page, with the Python code of the algorithms.

12 corrected exercises • 120 points • 150 minutes

This chapter is part of 201-N11, every chapter with its set and sheet Before you start Revision sheet: the traps and the method for this chapter

Self-checking set Type your answers and check them question by question: the page tells you right or wrong before you open the solution. With an account, every answer right on the first try earns points, and your right answers stay filled in when you come back.

Course recap

  • • EUCLIDEAN DIVISION: for n>0n>0, there is a unique pair (q,r)(q,r) with a=nq+ra=nq+r and 0≤r<n0\leq r<n. The remainder is ALWAYS nonnegative, even when a<0a<0: −17 mod 5=3-17\bmod 5=3. Watch out, the operator of the C and Java languages does not follow this convention.
  • • CONGRUENCES: a≡b(modn)a\equiv b\pmod n means that nn divides a−ba-b, or that aa and bb leave the same remainder. The relation is compatible with ADDITION and MULTIPLICATION, so you reduce BEFORE computing. It is NOT compatible with division.
  • • CANCELLATION: ac≡bc(modn)ac\equiv bc\pmod n implies a≡b(modn)a\equiv b\pmod n only if gcd⁡(c,n)=1\gcd(c,n)=1. Otherwise, the modulus must be divided by gcd⁡(c,n)\gcd(c,n).
  • • EUCLID: gcd⁡(a,n)=gcd⁡(n,a mod n)\gcd(a,n)=\gcd(n,a\bmod n), which gives the algorithm. The number of steps is at most about 5 times the number of digits, a worst case reached on two consecutive Fibonacci numbers.
  • • BÉZOUT: there exist uu and vv such that au+nv=gcd⁡(a,n)au+nv=\gcd(a,n), computed by the EXTENDED Euclidean algorithm. If the GCD equals 1, then uu is the inverse of aa modulo nn.
  • • INVERSE: aa is invertible modulo nn if and only if gcd⁡(a,n)=1\gcd(a,n)=1. All the nonzero elements are invertible if and only if nn is PRIME. Otherwise ZERO DIVISORS appear.
  • • CONGRUENCE ax≡b(modn)ax\equiv b\pmod n: it has solutions if and only if gcd⁡(a,n)\gcd(a,n) divides bb, and then it has exactly gcd⁡(a,n)\gcd(a,n) of them modulo nn.
  • • FERMAT: if pp is prime and does not divide aa, then ap−1≡1(modp)a^{p-1}\equiv 1\pmod p. So you reduce the BASE modulo pp and the EXPONENT modulo p−1p-1. FAST EXPONENTIATION computes ae mod na^{e}\bmod n in about 2log⁡2e2\log_{2}e multiplications.

Calculations: remainders to compute

Exercise 0.1: Reduce modulo n: ten direct calculations

For each line, give the remainder rr asked for, with 0≤r<n0\leq r<n, or the GCD on the last line. A calculator is no help here: every number reduces by hand.

A single method: reduce BEFORE computing. Replace each number by its remainder, term by term in a sum, factor by factor in a product; for a power, look for the first exponent that brings you back to 11, through the cycle of powers or through Fermat's little theorem when the modulus is prime, then reduce the exponent. A negative remainder does not exist: add nn until you land in [0,n)[0, n).

  • a) 347 mod 9347\bmod 9.
  • b) −38 mod 7-38\bmod 7.
  • c) (482+937+615) mod 8(482+937+615)\bmod 8, reducing each term.
  • d) (123×456) mod 11(123\times 456)\bmod 11, reducing each factor.
  • e) The remainder of n3+2nn^{3}+2n modulo 44, given that n≡3(mod4)n\equiv 3\pmod 4.
  • f) 2,9992 mod 112{,}999^{2}\bmod 11.
  • g) 441 mod 94^{41}\bmod 9, using the cycle of powers of 44.
  • h) The last digit of 7837^{83}, that is, 783 mod 107^{83}\bmod 10.
  • i) 2405 mod 132^{405}\bmod 13, using Fermat's little theorem.
  • j) The GCD of 1,5181{,}518 and 391391, by the Euclidean algorithm.

Type your answers, the page tells you right or wrong 0/10

a)
b)
c)
d)
e)
f)
g)
h)
i)
j)
Show the solution

Answers

  • a) 347=9×38+5347=9\times 38+5; 55
  • b) −38=7×(−6)+4-38=7\times(-6)+4; 44
  • c) 2+1+7≡22+1+7\equiv 2; 22
  • d) 2×5=102\times 5=10; 1010
  • e) 27+6=33≡127+6=33\equiv 1; 11
  • f) 72=49≡57^{2}=49\equiv 5; 55
  • g) 43≡14^{3}\equiv 1, 441≡424^{41}\equiv 4^{2}; 77
  • h) cycle of length 44, 783≡737^{83}\equiv 7^{3}; 33
  • i) 212≡12^{12}\equiv 1, 2405≡292^{405}\equiv 2^{9}; 55
  • j) last nonzero remainder; 2323

The move that decides everything: reduce before multiplying or raising to a power. Congruence is compatible with sums, products and therefore powers: you may replace every number by its remainder at any step, and you NEVER have to write out 2,99922{,}999^{2} or 24052^{405}.

a)

347=9×38+5347=9\times 38+5, so 347 mod 9=5347\bmod 9=5. Check: 9×38=3429\times 38=342 and 342+5=347342+5=347.

b)

You need a quotient that leaves a remainder in [0,7)[0, 7): −38=7×(−6)+4-38=7\times(-6)+4, so −38 mod 7=4-38\bmod 7=4. Pitfall: −38=7×(−5)−3-38=7\times(-5)-3 gives −3-3, which is not a remainder; add 77 and you get 44 back.

c)

482=8×60+2482=8\times 60+2, 937=8×117+1937=8\times 117+1 and 615=8×76+7615=8\times 76+7. The sum is therefore congruent to 2+1+7=10≡2(mod8)2+1+7=10\equiv 2\pmod 8. Check: 482+937+615=2,034=8×254+2482+937+615=2{,}034=8\times 254+2.

d)

123=11×11+2123=11\times 11+2 and 456=11×41+5456=11\times 41+5, so 123×456≡2×5=10(mod11)123\times 456\equiv 2\times 5=10\pmod{11}. The remainder is 1010, without ever computing 56,08856{,}088.

e)

Replace nn by its remainder 33: 33+2×3=27+6=33=4×8+13^{3}+2\times 3=27+6=33=4\times 8+1, so the remainder is 11, whatever the integer n≡3(mod4)n\equiv 3\pmod 4. Check with n=7n=7: 343+14=357=4×89+1343+14=357=4\times 89+1.

f)

2,999=11×272+72{,}999=11\times 272+7, so 2,9992≡72=49=44+5≡5(mod11)2{,}999^{2}\equiv 7^{2}=49=44+5\equiv 5\pmod{11}. Pitfall: squaring first, which forces you to divide 8,994,0018{,}994{,}001 by 1111.

g)

The cycle: 41≡44^{1}\equiv 4, 42=16≡74^{2}=16\equiv 7, 43≡4×7=28≡1(mod9)4^{3}\equiv 4\times 7=28\equiv 1\pmod 9. Its length is 33, and 41=3×13+241=3\times 13+2, so 441=(43)13×42≡1×7=7(mod9)4^{41}=\left(4^{3}\right)^{13}\times 4^{2}\equiv 1\times 7=7\pmod 9. Pitfall: reducing the exponent modulo 99, the modulus, instead of the length of the cycle.

h)

The cycle modulo 1010: 77, 49≡949\equiv 9, 9×7=63≡39\times 7=63\equiv 3, 3×7=21≡13\times 7=21\equiv 1, of length 44. Since 83=4×20+383=4\times 20+3, 783≡73≡3(mod10)7^{83}\equiv 7^{3}\equiv 3\pmod{10}: the last digit is 33.

i)

1313 is prime and does not divide 22, so 212≡1(mod13)2^{12}\equiv 1\pmod{13}. Now 405=12×33+9405=12\times 33+9, so 2405≡29=512=13×39+5≡5(mod13)2^{405}\equiv 2^{9}=512=13\times 39+5\equiv 5\pmod{13}. Pitfall: reducing the exponent modulo 1313 instead of 13−1=1213-1=12, which gives 405≡2405\equiv 2 and a wrong 44.

j)

1,518=3×391+3451{,}518=3\times 391+345; 391=1×345+46391=1\times 345+46; 345=7×46+23345=7\times 46+23; 46=2×23+046=2\times 23+0. The last nonzero remainder is 2323, so the GCD is 2323. Check: 1,518=23×661{,}518=23\times 66 and 391=23×17391=23\times 17.

Check: verify a remainder by recomputing nq+rnq+r, and make sure it falls in [0,n)[0, n); verify a cycle of powers by making sure it really comes back to 11 before using it to reduce the exponent.

Tick the exercises you have done or want to review: a free account, no password, keeps your ticks and your right answers from one visit to the next, tells you which chapter to tackle next and lets you ask for the exercise you are missing. Create your space, an email is enough.

Calculations: inverses and congruences to solve

Exercise 0.2: Invert and solve modulo n: eight direct calculations

For each line, give the inverse, the solution or the number asked for. An inverse and a solution are written like a remainder, between 00 and n−1n-1.

A single method: first compute gcd⁡(a,n)\gcd(a,n), otherwise you cannot know whether the inverse exists; then find the inverse by the extended Euclidean algorithm, or by trial when nn is small; finally, to solve ax≡b(modn)ax\equiv b\pmod n, multiply both sides by the inverse of aa. If d=gcd⁡(a,n)>1d=\gcd(a,n)>1, the congruence has a solution only if dd divides bb: you then divide everything by dd, modulus included, and you get dd solutions modulo nn.

  • a) The inverse of 55 modulo 1111.
  • b) The inverse of 44 modulo 1515.
  • c) Does the inverse of 99 modulo 2424 exist?
  • d) The inverse of 2323 modulo 6060, by the extended Euclidean algorithm.
  • e) The Bézout coefficients uu and vv such that 75u+32v=175u+32v=1, with 0<u<320<u<32.
  • f) The solution of 5x≡3(mod17)5x\equiv 3\pmod{17}.
  • g) The solution of 7x≡5(mod18)7x\equiv 5\pmod{18}.
  • h) The congruence 8x≡12(mod28)8x\equiv 12\pmod{28}: the number of solutions modulo 2828, then the smallest positive solution.

Type your answers, the page tells you right or wrong 0/10

a)
b)
c)
d)
e)
f)
g)
h)
Show the solution

Answers

  • a) 5×9≡1(mod11)5\times 9\equiv 1\pmod{11}; 99
  • b) 4×4≡1(mod15)4\times 4\equiv 1\pmod{15}; 44
  • c) gcd⁡(9,24)=3\gcd(9,24)=3; no
  • d) 1=5×60−13×231=5\times 60-13\times 23; 4747
  • e) 1=3×75−7×321=3\times 75-7\times 32; u=3u=3, v=−7v=-7
  • f) x≡7×3x\equiv 7\times 3; x=4x=4
  • g) x≡13×5x\equiv 13\times 5; x=11x=11
  • h) 2x≡3(mod7)2x\equiv 3\pmod 7; 44 solutions, the smallest is 55

The move that decides everything: the GCD BEFORE any calculation. gcd⁡(a,n)=1\gcd(a,n)=1 tells you that the inverse exists and that the congruence has a single solution modulo nn; otherwise, the inverse does not exist, and the congruence has several solutions or none.

a)

gcd⁡(5,11)=1\gcd(5,11)=1. By trial: 5×9=45=44+1≡1(mod11)5\times 9=45=44+1\equiv 1\pmod{11}, so the inverse is 99. Pitfall: answering 15\frac{1}{5} or 0.20.2; a modular inverse is an INTEGER between 00 and 1010.

b)

gcd⁡(4,15)=1\gcd(4,15)=1, and 4×4=16=15+1≡1(mod15)4\times 4=16=15+1\equiv 1\pmod{15}: 44 is its own inverse modulo 1515.

c)

No: gcd⁡(9,24)=3≠1\gcd(9,24)=3\neq 1. All the multiples of 99 are multiples of 33, and so are their remainders modulo 2424, so none of them equals 11. Pitfall: believing that every nonzero number smaller than the modulus is invertible; that is true only when the modulus is prime.

d)

Euclid: 60=2×23+1460=2\times 23+14; 23=1×14+923=1\times 14+9; 14=1×9+514=1\times 9+5; 9=1×5+49=1\times 5+4; 5=1×4+15=1\times 4+1. Back substitution: 1=5−4=5−(9−5)=2×5−9=2(14−9)−9=2×14−3×9=2×14−3(23−14)=5×14−3×23=5(60−2×23)−3×23=5×60−13×231=5-4=5-(9-5)=2\times 5-9=2(14-9)-9=2\times 14-3\times 9=2\times 14-3(23-14)=5\times 14-3\times 23=5(60-2\times 23)-3\times 23=5\times 60-13\times 23. So −13×23≡1(mod60)-13\times 23\equiv 1\pmod{60}, and the inverse is −13+60=47-13+60=47. Check: 23×47=1,081=18×60+123\times 47=1{,}081=18\times 60+1. Pitfall: stopping at −13-13, which is not written as a remainder.

e)

Euclid: 75=2×32+1175=2\times 32+11; 32=2×11+1032=2\times 11+10; 11=1×10+111=1\times 10+1. Back substitution: 1=11−10=11−(32−2×11)=3×11−32=3(75−2×32)−32=3×75−7×321=11-10=11-(32-2\times 11)=3\times 11-32=3(75-2\times 32)-32=3\times 75-7\times 32. So u=3u=3 and v=−7v=-7, and uu does lie between 00 and 3232. Check: 225−224=1225-224=1. Pitfall: expanding every product at each step, which makes you lose track of 7575 and 3232; substitute one division at a time, from the bottom up.

f)

1717 is prime, so the inverse of 55 exists: 5×7=35=34+15\times 7=35=34+1, it is 77. Multiply both sides by 77: x≡7×3=21≡4(mod17)x\equiv 7\times 3=21\equiv 4\pmod{17}. Check: 5×4=20=17+35\times 4=20=17+3.

g)

gcd⁡(7,18)=1\gcd(7,18)=1, and 7×13=91=5×18+17\times 13=91=5\times 18+1, so the inverse of 77 modulo 1818 is 1313. Then x≡13×5=65=3×18+11≡11(mod18)x\equiv 13\times 5=65=3\times 18+11\equiv 11\pmod{18}. Check: 7×11=77=4×18+57\times 11=77=4\times 18+5. Pitfall: trying to divide 55 by 77; modulo 1818, you never divide, you multiply by the inverse.

h)

d=gcd⁡(8,28)=4d=\gcd(8,28)=4, and 44 divides 1212: there are 44 solutions modulo 2828. Divide everything by 44, modulus included: 2x≡3(mod7)2x\equiv 3\pmod 7. The inverse of 22 modulo 77 is 44, so x≡12≡5(mod7)x\equiv 12\equiv 5\pmod 7. Modulo 2828, the solutions are 55, 1212, 1919 and 2626, and the smallest is 55. Check: 8×5=40=28+128\times 5=40=28+12. Pitfall: dividing 88 and 1212 by 44 while keeping the modulus 2828, which gives 2x≡3(mod28)2x\equiv 3\pmod{28}, with no solution since 2x−32x-3 is odd.

Check: an inverse uu of aa is checked by computing au mod nau\bmod n, which must equal 11; a solution is checked by substituting it into the congruence; Bézout coefficients, by recomputing au+bvau+bv. Each check costs one multiplication and one division.

Part A: the basics (/50)

Exercise 1: Euclidean division and the remainder

All of modular arithmetic rests on an operation you think you have known since elementary school. The thread of the set: we no longer care about the QUOTIENT but about the REMAINDER, and a remainder is always nonnegative, even when the dividend is not.

01234567891011
  • a) State the Euclidean division theorem for aa divided by nn, specifying the bounds on the remainder.
  • b) Compute the quotient and the remainder of 2026 divided by 12, then of 2026 divided by 7.
  • c) Compute −17 mod 5-17 \bmod 5 and −1 mod 12-1 \bmod 12. Watch the sign of the remainder.
  • d) In Python, the operator %\% always returns a remainder with the sign of the DIVISOR; in C and Java, it returns a remainder with the sign of the DIVIDEND. What is −17 % 5-17\ \%\ 5 in each language?
  • e) If today is a Tuesday, what day will it be in 100 days? And what day was it 100 days ago?

Type your answers, the page tells you right or wrong 0/13

a)
b)
c)
d)
e)
Show the solution

Answers

  • a) a=nq+ra = nq+r with 0≤r<n0 \le r < n, the pair (q,r)(q,r) being unique
  • b) 2026=12(168)+102026 = 12(168)+10 and 2026=7(289)+32026 = 7(289)+3
  • c) −17 mod 5=3-17 \bmod 5 = 3 and −1 mod 12=11-1 \bmod 12 = 11
  • d) 33 in Python, −2-2 in C and Java
  • e) Thursday in 100 days, Sunday 100 days ago
a)

For every integer aa and every integer n>0n>0, there is a UNIQUE pair of integers (q,r)(q,r) such that a=nq+ra=nq+r with 0≤r<n0\leq r<n. Two things matter in this statement: existence and uniqueness on the one hand, and above all the BOUNDS on the remainder, between 0 included and nn excluded. Those bounds are what rule out a negative remainder, and they are what students forget when a<0a<0.

b)

2026=12×168+102026=12\times 168+10, so q=168q=168 and r=10r=10: this is what the clock in the figure shows, where you go around 168 times before stopping on 10. For 7: 2026=7×289+32026=7\times 289+3, so q=289q=289 and r=3r=3. Always check by recomputing nq+rnq+r, which takes one second and catches every multiplication-table error.

c)

For −17-17 divided by 5: the quotient must be chosen so that the remainder falls in [0,5)[0, 5). With q=−4q=-4: −17=5×(−4)+3-17=5\times(-4)+3, so r=3r=3. The knee-jerk division −17=5×(−3)−2-17=5\times(-3)-2 would give r=−2r=-2, which is not a valid remainder. For −1-1 divided by 12: −1=12×(−1)+11-1=12\times(-1)+11, so r=11r=11: it is the last notch on the clock before going back through zero, which is exactly the picture to remember.

d)

In PYTHON, −17 % 5-17\ \%\ 5 equals 3, the mathematical convention, since the divisor 5 is positive. In C and in JAVA, the operator returns −2-2, with the sign of the dividend. Both languages compute correctly; they simply do not answer the same question. The practical consequence for a programmer: circular indexing code written in C must correct the negative case, for example with ((a % n)+n) % n((a\ \%\ n)+n)\ \%\ n, whereas the same code in Python works as is.

e)

Days of the week are counted modulo 7. 100=7×14+2100=7\times 14+2, so in 100 days you move forward 2 notches: Tuesday plus 2 gives THURSDAY. 100 days ago, you move back 2 notches: Tuesday minus 2 gives SUNDAY. You can also write −100 mod 7=5-100\bmod 7=5 and move forward 5, which also gives Sunday, since moving forward 5 or back 2 comes to the same thing on a cycle of 7.

012345678910112026 = 12 × 168 + 10, so 2026 mod 12 = 10

Exercise 2: Congruences and their rules of computation

Writing a≡b(modn)a\equiv b\pmod n is not decorative notation: it is an EQUIVALENCE relation compatible with addition and multiplication, which lets you compute with remainders as you do with numbers.

  • a) Give the definition of a≡b(modn)a\equiv b\pmod n in terms of divisibility, then in terms of remainders.
  • b) Prove that congruence modulo nn is reflexive, symmetric and transitive.
  • c) Prove that if a≡ba\equiv b and c≡dc\equiv d modulo nn, then a+c≡b+da+c\equiv b+d and ac≡bdac\equiv bd.
  • d) Compute the remainder of 17417^{4} modulo 5 without ever raising 17 to the power 4.
  • e) Does compatibility hold for DIVISION? Test it on 6≡12(mod6)6\equiv 12\pmod 6 divided by 2.

Type your answers, the page tells you right or wrong 0/13

a)
b)
c)
d)
e)
Show the solution

Answers

  • a) nn divides a−ba-b, which amounts to saying that aa and bb leave the same remainder modulo nn
  • b) Reflexive, symmetric and transitive: it is an equivalence relation, with nn classes
  • c) (a+c)−(b+d)=(k+ℓ)n(a+c)-(b+d) = (k+\ell)n and ac−bd=(ck+bℓ)nac-bd = (ck+b\ell)n, by adding and subtracting bcbc
  • d) 17≡217 \equiv 2, so 174≡24=16≡1(mod5)17^{4} \equiv 2^{4} = 16 \equiv 1 \pmod 5
  • e) NO: 6≡12(mod6)6 \equiv 12 \pmod 6, but 3≢6(mod6)3 \not\equiv 6 \pmod 6. You cancel kk only if kk is relatively prime to nn
a)

DIVISIBILITY: a≡b(modn)a\equiv b\pmod n means that nn divides a−ba-b. REMAINDERS: it means that aa and bb leave the SAME remainder when divided by nn. The two formulations are equivalent, and you must be able to switch from one to the other: the first is used in proofs, the second in calculations.

b)

REFLEXIVE: nn divides a−a=0a-a=0, so a≡aa\equiv a. SYMMETRIC: if nn divides a−ba-b, it also divides b−a=−(a−b)b-a=-(a-b), so b≡ab\equiv a. TRANSITIVE: if nn divides a−ba-b and b−cb-c, it divides their sum (a−b)+(b−c)=a−c(a-b)+(b-c)=a-c, so a≡ca\equiv c. These three properties make congruence an equivalence relation, whose classes are exactly the nn sets of numbers that leave the same remainder.

c)

By hypothesis a−b=kna-b=kn and c−d=ℓnc-d=\ell n for two integers kk and ℓ\ell. SUM: (a+c)−(b+d)=(a−b)+(c−d)=(k+ℓ)n(a+c)-(b+d)=(a-b)+(c-d)=(k+\ell)n, a multiple of nn ✓. PRODUCT: ac−bd=ac−bc+bc−bd=c(a−b)+b(c−d)=(ck+bℓ)nac-bd=ac-bc+bc-bd=c(a-b)+b(c-d)=(ck+b\ell)n, a multiple of nn ✓. The trick for the product is to add and subtract bcbc, which brings out both hypotheses; it is worth remembering, it will come up again elsewhere.

d)

17≡2(mod5)17\equiv 2\pmod 5, since 17=5×3+217=5\times 3+2. Compatibility with products lets you raise both sides to the power 4: 174≡24=16≡1(mod5)17^{4}\equiv 2^{4}=16\equiv 1\pmod 5. The remainder is therefore 1, while 174=83,52117^{4}=83{,}521, a number you never had to write down. That is the whole point of modular computation: reduce BEFORE multiplying, never after.

e)

NO, division is not compatible in general. We do have 6≡12(mod6)6\equiv 12\pmod 6, since 12−6=612-6=6. Dividing both sides by 2: 3≡6(mod6)3\equiv 6\pmod 6 would be false, because 6−3=36-3=3 is not a multiple of 6. The correct rule is that you may cancel a factor kk provided you ALSO divide the modulus by gcd⁡(k,n)\gcd(k,n), or if kk is relatively prime to nn. This restriction is what makes exercise 5 on inverses necessary.

Exercise 3: Computing in the integers modulo n

The set of remainders modulo nn is a closed world where you add and multiply without ever leaving it. Its multiplication table reveals a deep difference depending on whether nn is prime or not.

×112233445566123456246135362514415263531642654321
  • a) Write out the addition table modulo 5. Does each row contain each element exactly once?
  • b) The figure gives the multiplication table modulo 7, without zero. Check three entries at random and say what you observe on each row.
  • c) Write out the multiplication table modulo 6, without zero. What is different?
  • d) Find two nonzero elements whose product is zero modulo 6. What are such elements called, and why are there none modulo 7?
  • e) State the general rule that distinguishes the two cases.

Type your answers, the page tells you right or wrong 0/15

a)
b)
c)
d)
e)
Show the solution

Answers

  • a) Yes: adding a fixed number is a permutation, for every nn
  • b) 3×5≡13\times5 \equiv 1, 4×6≡34\times6 \equiv 3, 2×4≡12\times4 \equiv 1: each row contains 1, so every nonzero element is invertible
  • c) The row of 2 is 2,4,0,2,42,4,0,2,4 and the row of 3 is 3,0,3,0,33,0,3,0,3: zeros, and no 1
  • d) 2×3=6≡0(mod6)2 \times 3 = 6 \equiv 0 \pmod 6: ZERO DIVISORS, impossible modulo 7, which is prime
  • e) aa is invertible modulo nn if and only if gcd⁡(a,n)=1\gcd(a,n)=1; all of them are if and only if nn is prime
a)

The addition table modulo 5 is obtained by adding and then taking the remainder. Its first row, the row of 0, is 0,1,2,3,40,1,2,3,4; the row of 1 is 1,2,3,4,01,2,3,4,0; the row of 2 is 2,3,4,0,12,3,4,0,1, and so on. YES, each row contains each element exactly once: adding a fixed number is a permutation, because you can always go back by adding its negative. This is true for every nn, prime or not.

b)

Checks: 3×5=15=14+1≡13\times 5=15=14+1\equiv 1; 4×6=24=21+3≡34\times 6=24=21+3\equiv 3; 2×4=8≡12\times 4=8\equiv 1. You observe that EACH ROW contains each nonzero element exactly once, and in particular that it contains 1. In other words, every nonzero element has an INVERSE modulo 7, which is far from obvious and will be the subject of exercise 5.

c)

Modulo 6, the row of 2 gives 2,4,0,2,42,4,0,2,4 for the multipliers 1,2,3,4,51,2,3,4,5: it contains a ZERO even though neither factor is zero, and it does NOT contain 1. The row of 3 gives 3,0,3,0,33,0,3,0,3, even worse. Only the rows of 1 and 5 contain 1. The table is therefore very different from the one modulo 7.

d)

2×3=6≡0(mod6)2\times 3=6\equiv 0\pmod 6, with 2≠02\neq 0 and 3≠03\neq 0. Such elements are called ZERO DIVISORS. They appear because 6 factors as 2×32\times 3: the product collects all the factors of 6 and becomes a multiple of 6. Modulo 7 this is impossible, because 7 is PRIME: if 7 divides a product abab, it divides aa or bb, a property that characterizes precisely the prime numbers.

e)

An element aa is invertible modulo nn if and only if gcd⁡(a,n)=1\gcd(a,n)=1. Consequently, ALL the nonzero elements are invertible if and only if nn is PRIME, in which case the integers modulo nn form a field. If nn is composite, the elements that share a factor with nn are not invertible and are zero divisors. This distinction is what makes prime moduli so valuable in cryptography and in error-correcting codes.

×112233445566123456246135362514415263531642654321every nonzero element is invertible

Exercise 4: The GCD and the Euclidean algorithm

The oldest algorithm still in use dates from 300 BCE, and it is still the one processors execute. Its justification fits in one line of congruence.

462462
def pgcd(a, n):
    while n != 0:
        a, n = n, a % n
    return a

def pgcd_rec(a, n):
    if n == 0:
        return a
    return pgcd_rec(n, a % n)
  • a) Prove that gcd⁡(a,n)=gcd⁡(n,a mod n)\gcd(a,n)=\gcd(n,a\bmod n), which justifies the algorithm.
  • b) Run the Euclidean algorithm on 1071 and 462, as in the figure, and give the GCD.
  • c) Run it on 240 and 46.
  • d) Write the algorithm in Python, as an iterative version and then as a recursive version.
  • e) At most how many steps does the algorithm take? Which pair of numbers is the worst case?

Type your answers, the page tells you right or wrong 0/15

a)
b)
c)
d)
e)
Show the solution

Answers

  • a) dd divides aa and nn if and only if it divides nn and rr: same common divisors, same GCD
  • b) 147147, then 2121, then 00: gcd⁡(1071,462)=21\gcd(1071,462) = 21
  • c) 10,6,4,2,010, 6, 4, 2, 0: gcd⁡(240,46)=2\gcd(240,46) = 2, in five steps instead of three
  • d) Iterative: (a,n)→(n,a mod n)(a,n) \to (n, a \bmod n). Recursive: same equality, base case n=0n=0
  • e) At most about 5 times the number of digits of the smaller one; worst case, two consecutive Fibonacci numbers
a)

Let r=a mod nr=a\bmod n, so a=nq+ra=nq+r. If dd divides aa and nn, then it divides a−nq=ra-nq=r, so it divides nn and rr. Conversely, if dd divides nn and rr, it divides nq+r=anq+r=a, so it divides aa and nn. The two pairs therefore have EXACTLY the same common divisors, and in particular the same greatest one. The algorithm then consists in replacing the pair by a smaller pair until the remainder becomes zero.

b)

1071=2×462+1471071=2\times 462+147; 462=3×147+21462=3\times 147+21; 147=7×21+0147=7\times 21+0. The last nonzero remainder is 21, so gcd⁡(1071,462)=21\gcd(1071,462)=21. The figure gives the geometric reading: you tile the rectangle with smaller and smaller squares, and the side of the last square is the GCD. Three divisions were enough, where searching for the divisors of 1071 would have taken many more.

c)

240=5×46+10240=5\times 46+10; 46=4×10+646=4\times 10+6; 10=1×6+410=1\times 6+4; 6=1×4+26=1\times 4+2; 4=2×2+04=2\times 2+0. The GCD is 2. Note that these two numbers, smaller than those in question b, took MORE steps: the number of steps does not depend on the size of the numbers but on the sequence of quotients, all equal to 1 in the worst case.

d)

Both versions fit in four lines and can be read straight off question a. The ITERATIVE version replaces the pair (a,n)(a,n) by (n,a mod n)(n,a\bmod n) on every pass and stops when the second term becomes zero; the first one then holds the GCD. The RECURSIVE version translates the same equality word for word, with the base case n=0n=0 returning aa. The recursive version is more readable and its number of calls is the number of divisions, so it runs no risk of stack overflow for integers of reasonable size; the iterative version avoids that risk altogether and remains the form used in libraries.

e)

Lamé's theorem states that the number of steps is at most about 55 times the number of DIGITS of the smaller of the two numbers, which is remarkably few: fewer than 50 steps for ten-digit numbers. The WORST CASE is that of two consecutive FIBONACCI numbers, such as 89 and 55, for which every quotient equals 1, so the algorithm cannot speed anything up. It is a rare example where the worst case of an algorithm is known explicitly.

462462then 147, then 211071 = 2 × 462 + 147, 462 = 3 × 147 + 21, 147 = 7 × 21the last nonzero remainder is the GCD: 21

Exercise 5: The modular inverse and linear congruences

You do not divide modulo nn: you multiply by an inverse, when there is one. Deciding whether it exists, then computing it, is the central skill of the chapter.

  • a) Give the condition for the inverse of aa modulo nn to exist, and find the inverse of 3 modulo 7 by exhaustive search.
  • b) Find the inverse of 4 modulo 9, then show that 4 has no inverse modulo 8.
  • c) Solve 3x≡2(mod7)3x\equiv 2\pmod 7.
  • d) Solve 4x≡6(mod10)4x\equiv 6\pmod{10}. How many solutions are there modulo 10?
  • e) Solve 6x≡4(mod9)6x\equiv 4\pmod{9}, or show that there is no solution.

Type your answers, the page tells you right or wrong 0/15

a)
b)
c)
d)
e)
Show the solution

Answers

  • a) The inverse exists if and only if gcd⁡(a,n)=1\gcd(a,n)=1; the inverse of 3 modulo 7 is 5
  • b) 4−1=74^{-1} = 7 modulo 9; modulo 8, gcd⁡(4,8)=4\gcd(4,8)=4, so no inverse
  • c) x≡3(mod7)x \equiv 3 \pmod 7, a unique solution
  • d) x=4x = 4 and x=9x = 9: two solutions modulo 10, as many as the GCD
  • e) No solution: gcd⁡(6,9)=3\gcd(6,9)=3 does not divide 4
a)

The inverse of aa modulo nn exists if and only if gcd⁡(a,n)=1\gcd(a,n)=1, that is, if aa and nn are RELATIVELY PRIME. For 3 modulo 7: gcd⁡(3,7)=1\gcd(3,7)=1, the inverse exists. Try: 3×1=33\times 1=3, 3×2=63\times 2=6, 3×3=9≡23\times 3=9\equiv 2, 3×4=12≡53\times 4=12\equiv 5, 3×5=15≡13\times 5=15\equiv 1 ✓. The inverse of 3 modulo 7 is therefore 5.

b)

gcd⁡(4,9)=1\gcd(4,9)=1, the inverse exists: 4×7=28=27+1≡1(mod9)4\times 7=28=27+1\equiv 1\pmod 9, so the inverse is 7. Modulo 8, on the other hand, gcd⁡(4,8)=4≠1\gcd(4,8)=4\neq 1: there is no inverse. You can check it on the table: the multiples of 4 modulo 8 take only the values 0 and 4, never 1. The same number 4 is therefore invertible or not depending on the modulus, which shows that invertibility is a property of the PAIR and not of the number.

c)

Multiply both sides by the inverse of 3, that is, by 5: 5×3x≡5×25\times 3x\equiv 5\times 2, that is, 15x≡1015x\equiv 10, that is, x≡3(mod7)x\equiv 3\pmod 7 since 15≡115\equiv 1 and 10≡310\equiv 3. Check: 3×3=9≡2(mod7)3\times 3=9\equiv 2\pmod 7 ✓. There is therefore exactly ONE solution modulo 7, as always when the coefficient is invertible.

d)

gcd⁡(4,10)=2\gcd(4,10)=2, which is not 1: you cannot invert. But 2 DIVIDES the right-hand side 6, so the congruence has solutions. Divide everything by 2, MODULUS INCLUDED: 2x≡3(mod5)2x\equiv 3\pmod 5. This time gcd⁡(2,5)=1\gcd(2,5)=1 and the inverse of 2 modulo 5 is 3, hence x≡3×3=9≡4(mod5)x\equiv 3\times 3=9\equiv 4\pmod 5. Modulo 10, this gives TWO solutions, x=4x=4 and x=9x=9. Check: 4×4=16≡64\times 4=16\equiv 6 ✓ and 4×9=36≡64\times 9=36\equiv 6 ✓. The number of solutions equals the GCD, here 2.

e)

gcd⁡(6,9)=3\gcd(6,9)=3, and 3 DOES NOT DIVIDE 4. The congruence therefore has NO solution. You can see it directly: 6x6x is always a multiple of 3, so its remainder modulo 9 belongs to {0,3,6}\{0,3,6\}, and 4 is not there. General rule: ax≡b(modn)ax\equiv b\pmod n has solutions if and only if gcd⁡(a,n)\gcd(a,n) divides bb, and then it has exactly gcd⁡(a,n)\gcd(a,n) of them modulo nn.

Part B: problems and reasoning (/50)

Exercise 6: Bézout's identity and the extended Euclidean algorithm

The Euclidean algorithm gives the GCD; if you keep track of the calculations, it gives much more: two coefficients that express this GCD as a combination of the two starting numbers. That is what makes computing an inverse fast.

def euclide_etendu(a, b):
    r, u, v = a, 1, 0
    rp, up, vp = b, 0, 1
    while rp != 0:
        q = r // rp
        r, u, v, rp, up, vp = rp, up, vp, r - q*rp, u - q*up, v - q*vp
    return r, u, v

def inverse(a, n):
    d, u, _ = euclide_etendu(a, n)
    if d != 1:
        return None
    return u % n
  • a) State Bézout's theorem and say what it becomes when the two numbers are relatively prime.
  • b) Find two integers uu and vv such that 1071u+462v=211071u+462v=21, by working back up the divisions of exercise 4.
  • c) Compute the inverse of 17 modulo 43 by the extended Euclidean algorithm.
  • d) Compare the cost of this method with that of the exhaustive search of exercise 5.
  • e) Write the extended Euclidean algorithm in Python.

Type your answers, the page tells you right or wrong 0/13

a)
b)
c)
d)
e)
Show the solution

Answers

  • a) au+bv=gcd⁡(a,b)au+bv = \gcd(a,b), which becomes au+bv=1au+bv=1 for two relatively prime numbers
  • b) u=−3u = -3 and v=7v = 7: −3(1071)+7(462)=21-3(1071)+7(462) = 21
  • c) 1=2(43)−5(17)1 = 2(43)-5(17), so the inverse of 17 modulo 43 is −5≡38-5 \equiv 38
  • d) Exhaustive: of the order of nn, exponential. Extended Euclid: linear in the number of digits
  • e) The algorithm returns the triple GCD, uu, vv, updating the coefficients on every pass
a)

For all integers aa and bb not both zero, there exist integers uu and vv such that au+bv=gcd⁡(a,b)au+bv=\gcd(a,b). When aa and bb are RELATIVELY PRIME, this becomes au+bv=1au+bv=1, and conversely: such an expression proves that the GCD is 1, since every common divisor divides the left-hand side and therefore divides 1. It is the most convenient criterion for proving that two numbers are relatively prime.

b)

Work back up the divisions. From 462=3×147+21462=3\times 147+21 you get 21=462−3×14721=462-3\times 147. From 1071=2×462+1471071=2\times 462+147 you get 147=1071−2×462147=1071-2\times 462. Substituting: 21=462−3(1071−2×462)=462−3×1071+6×462=7×462−3×107121=462-3\left(1071-2\times 462\right)=462-3\times 1071+6\times 462=7\times 462-3\times 1071. So u=−3u=-3 and v=7v=7. Check: −3×1071+7×462=−3213+3234=21-3\times 1071+7\times 462=-3213+3234=21 ✓. The discipline is to substitute ONE division at a time, from the bottom up, without ever expanding the products before the end.

c)

Euclid first: 43=2×17+943=2\times 17+9; 17=1×9+817=1\times 9+8; 9=1×8+19=1\times 8+1; 8=8×1+08=8\times 1+0. The GCD is 1, the inverse exists. Back substitution: 1=9−81=9-8; then 8=17−98=17-9 gives 1=9−(17−9)=2×9−171=9-(17-9)=2\times 9-17; then 9=43−2×179=43-2\times 17 gives 1=2(43−2×17)−17=2×43−5×171=2\left(43-2\times 17\right)-17=2\times 43-5\times 17. So −5×17≡1(mod43)-5\times 17\equiv 1\pmod{43}, and the inverse of 17 is −5-5, that is, 3838 modulo 43. Check: 17×38=646=43×15+117\times 38=646=43\times 15+1 ✓.

d)

Exhaustive search tries the values 1,2,…,n−11,2,\dots,n-1 until it hits 1: its cost is of the order of nn multiplications, so EXPONENTIAL in the number of digits of nn. The extended Euclidean algorithm, on the other hand, performs at most about 55 times the number of digits in divisions, so a cost LINEAR in that number. On a 600-digit modulus like those of RSA encryption, the first method is impossible within the age of the Universe and the second takes a fraction of a millisecond. That gap is what makes modern cryptography practical.

e)

The code below returns the triple made of the GCD and the two Bézout coefficients, updating the coefficients on every pass through the loop.

Exercise 7: Fermat's little theorem and fast exponentiation

Computing 31003^{100} and then taking the remainder is impossible by hand and costly for a machine. Two ideas bring the calculation down to a few lines: a theorem that shortens the exponent, and an algorithm that breaks it down in binary.

132645powers of 3 modulo 7: a cycle of 6
def puissance_mod(a, e, n):
    resultat = 1
    a = a % n
    while e > 0:
        if e % 2 == 1:
            resultat = (resultat * a) % n
        a = (a * a) % n
        e = e // 2
    return resultat
  • a) State Fermat's little theorem and check it for a=3a=3 and p=7p=7 by writing out the cycle of powers, as in the figure.
  • b) Compute the remainder of 31003^{100} modulo 7.
  • c) Compute the remainder of 210002^{1000} modulo 11.
  • d) Compute 7117^{11} modulo 13 by fast exponentiation, writing the exponent in binary, then check the result with Fermat's theorem.
  • e) How many multiplications does fast exponentiation take for a 600-digit exponent, compared with the naive method?

Type your answers, the page tells you right or wrong 0/14

a)
b)
c)
d)
e)
Show the solution

Answers

  • a) ap−1≡1(modp)a^{p-1} \equiv 1 \pmod p; the cycle of 3 modulo 7 is 3,2,6,4,5,13,2,6,4,5,1, of length 6
  • b) 100≡4(mod6)100 \equiv 4 \pmod 6, so 3100≡34=81≡4(mod7)3^{100} \equiv 3^{4} = 81 \equiv 4 \pmod 7
  • c) 210≡12^{10} \equiv 1, so 21000≡1(mod11)2^{1000} \equiv 1 \pmod{11}
  • d) 711≡2(mod13)7^{11} \equiv 2 \pmod{13}, confirmed by Fermat: 711≡7−1=27^{11} \equiv 7^{-1} = 2
  • e) About 3986 multiplications instead of 1060010^{600}
a)

If pp is PRIME and pp does not divide aa, then ap−1≡1(modp)a^{p-1}\equiv 1\pmod p. Check with a=3a=3 and p=7p=7: 31=33^{1}=3, 32=9≡23^{2}=9\equiv 2, 33≡63^{3}\equiv 6, 34≡18≡43^{4}\equiv 18\equiv 4, 35≡12≡53^{5}\equiv 12\equiv 5, 36≡15≡13^{6}\equiv 15\equiv 1 ✓. The figure shows this cycle of length 6: the six nonzero values all appear, exactly once, before the cycle closes back on 1.

b)

The theorem lets you reduce the EXPONENT modulo p−1=6p-1=6. Since 100=6×16+4100=6\times 16+4, you get 3100=(36)16×34≡116×34=81≡4(mod7)3^{100}=\left(3^{6}\right)^{16}\times 3^{4}\equiv 1^{16}\times 3^{4}=81\equiv 4\pmod 7. The remainder is 4. Remember the rule and its limit: reduce the BASE modulo pp, and the EXPONENT modulo p−1p-1, never the other way around.

c)

p=11p=11 is prime and does not divide 2, so 210≡1(mod11)2^{10}\equiv 1\pmod{11}. Since 1000=10×1001000=10\times 100, you get 21000=(210)100≡1100=1(mod11)2^{1000}=\left(2^{10}\right)^{100}\equiv 1^{100}=1\pmod{11}. The remainder is 1. A number with more than three hundred digits has been reduced in one line.

d)

1111 is written 10111011 in binary, so 711=78×72×717^{11}=7^{8}\times 7^{2}\times 7^{1}. Compute the successive squares modulo 13: 71≡77^{1}\equiv 7; 72=49≡107^{2}=49\equiv 10; 74≡102=100≡97^{4}\equiv 10^{2}=100\equiv 9; 78≡92=81≡37^{8}\equiv 9^{2}=81\equiv 3. Product: 3×10×7=210≡210−208=2(mod13)3\times 10\times 7=210\equiv 210-208=2\pmod{13}. CHECK with Fermat: 712≡17^{12}\equiv 1, so 711≡7−17^{11}\equiv 7^{-1}, the inverse of 7 modulo 13. Now 7×2=14≡17\times 2=14\equiv 1, so this inverse is 2 ✓. Two independent paths, same answer.

e)

The naive method takes N−1N-1 multiplications for aNa^{N}, that is, about 1060010^{600} operations for a 600-digit exponent: that is unthinkable, the Universe has existed for only about 101710^{17} seconds. Fast exponentiation takes at most 2log⁡2N2\log_{2}N, that is, about 2×600×3.3219≈39862\times 600\times 3.3219\approx 3986 multiplications, a matter of a few milliseconds. This difference, and it alone, is what lets a browser open an encrypted connection in the blink of an eye.

Exercise 8: Five statements to correct

Each of the following five statements is FALSE. Say why and give the correct statement.

  • 1) “−17 mod 5-17 \bmod 5 equals −2-2, since −17=5×(−3)−2-17=5\times(-3)-2.”
  • 2) “If ac≡bc(modn)ac\equiv bc\pmod n then a≡b(modn)a\equiv b\pmod n, just as with integers.”
  • 3) “Every nonzero element has an inverse modulo nn.”
  • 4) “To compute 31003^{100} modulo 7, you reduce the exponent modulo 7.”
  • 5) “The Euclidean algorithm becomes slow when the numbers are large, so it should be avoided in cryptography.”

Type your answers, the page tells you right or wrong 0/11

1)
2)
3)
4)
5)
Show the solution

Answers

  • 1) FALSE: −2-2 is not a valid remainder. −17=5(−4)+3-17 = 5(-4)+3, so the remainder is 3
  • 2) FALSE: you cancel only an invertible cc. 2×3≡4×3(mod6)2\times3 \equiv 4\times3 \pmod 6 and yet 2≢42 \not\equiv 4
  • 3) FALSE: only 1 and 5 are invertible modulo 6. You need gcd⁡(a,n)=1\gcd(a,n)=1
  • 4) FALSE: the exponent is reduced modulo p−1=6p-1=6, not modulo 7. The answer is 4, not 2
  • 5) FALSE: fewer than 3000 divisions for two 600-digit numbers. Extended Euclid is a pillar of RSA
1)

FALSE. The equality written is correct, but −2-2 is NOT a valid remainder: Euclidean division requires 0≤r<n0\leq r<n, so here 0≤r<50\leq r<5. You must choose q=−4q=-4: −17=5×(−4)+3-17=5\times(-4)+3, so −17 mod 5=3-17\bmod 5=3. That is also what Python returns. The confusion comes from languages such as C or Java, whose operator follows another convention and does return −2-2: the language is not wrong, it simply does not compute the Euclidean remainder.

2)

FALSE. You may cancel cc only if cc is INVERTIBLE modulo nn, that is, relatively prime to nn. Counterexample: 2×3≡4×3(mod6)2\times 3\equiv 4\times 3\pmod 6, because 6≡12≡06\equiv 12\equiv 0, and yet 2≢4(mod6)2\not\equiv 4\pmod 6. The correct rule is that ac≡bc(modn)ac\equiv bc\pmod n implies a≡b(modngcd⁡(c,n))a\equiv b\pmod{\frac{n}{\gcd(c,n)}}, which gives back the original modulus only if the GCD is 1.

3)

FALSE. The inverse of aa modulo nn exists if and only if gcd⁡(a,n)=1\gcd(a,n)=1. Modulo 6, the elements 2, 3 and 4 have no inverse, since they share a factor with 6; only 1 and 5 have one. The statement is true only when nn is PRIME, in which case every nonzero element is relatively prime to nn by definition.

4)

FALSE, and the error is only one notch off, but it changes everything. Fermat's little theorem gives 36≡1(mod7)3^{6}\equiv 1\pmod 7, so it is modulo p−1=6p-1=6 that the exponent is reduced, not modulo 7. We have 100≡4(mod6)100\equiv 4\pmod 6, so 3100≡34=81≡43^{100}\equiv 3^{4}=81\equiv 4. Reducing modulo 7 would give 100≡2100\equiv 2, so 32=9≡23^{2}=9\equiv 2, a wrong result. Rule: the base is reduced modulo nn, the exponent modulo p−1p-1.

5)

FALSE, it is exactly the opposite. Lamé's theorem guarantees that the number of divisions is at most about 5 times the number of DIGITS of the smaller number: for two 600-digit numbers, that makes fewer than 3000 divisions, a fraction of a millisecond. This efficiency is what makes it possible to compute the decryption exponent of an RSA key, and the extended Euclidean algorithm is therefore, on the contrary, a pillar of modern cryptography.

Exercise 9: Problem: check digits, from the ISBN to the card number

Every code you type, a book number, a credit card number or a social insurance number, carries one extra digit whose only job is to catch your typing mistakes. That digit is a remainder.

9×17×38×12×31×12×33×14×35×16×38×10×3?cweighted sum 97, the total must reach a multiple of 10
  • a) A thirteen-digit ISBN code satisfies the condition that the sum of its thirteen digits weighted by 1,3,1,3,…1,3,1,3,\dots is a multiple of 10. The figure gives the first twelve digits and a weighted sum of 97. Compute the check digit cc.
  • b) Check that the complete code does satisfy the condition.
  • c) A user mistypes one digit of the code. Prove that the error is always detected.
  • d) The user swaps two adjacent digits. Show that the error is detected EXCEPT in one specific case, and give an example.
  • e) The ten-digit ISBN code used the weights 10,9,…,110,9,\dots,1 and the modulus 11, with an XX for the value 10. Why does 11 detect errors better than 10?

Type your answers, the page tells you right or wrong 0/11

a)
b)
c)
d)
e)
Show the solution

Answers

  • a) The check digit is 3, since 97+3=10097+3 = 100
  • b) 100≡0(mod10)100 \equiv 0 \pmod{10}: the code is valid
  • c) The sum changes by w(d′−d)w(d'-d), which cannot be a multiple of 10: 3 is relatively prime to 10
  • d) The sum changes by 2(a−b)2(a-b): the error escapes when ∣a−b∣=5\lvert a-b \rvert = 5
  • e) 11 is PRIME: no zero divisors, so every transposition is detected, at the cost of the character X
a)

In the ISBN-13 convention, the check digit is the thirteenth digit and it carries the weight 1, since the weights alternate starting with 1. So 97+c97+c must be a multiple of 10, with 0≤c≤90\leq c\leq 9. The next multiple of 10 above 97 is 100, so c=3c=3. General formula: c=(10−(S mod 10)) mod 10c=\left(10-\left(S\bmod 10\right)\right)\bmod 10, and the last modulo is there only for the case where SS is already a multiple of 10, where the check digit is 0 and not 10.

b)

Total sum: 97+3=10097+3=100, which is indeed a multiple of 10 since 100≡0(mod10)100\equiv 0\pmod{10} ✓. The complete code is therefore valid, and it is this check, not the computation of the check digit, that a bookstore checkout performs at every scan.

c)

Suppose a digit of weight ww changes from dd to d′d'. The sum changes by w(d′−d)w\left(d'-d\right), with ww equal to 1 or 3 and d′−dd'-d between −9-9 and 9, nonzero. For the error to go unnoticed, you would need w(d′−d)≡0(mod10)w\left(d'-d\right)\equiv 0\pmod{10}. With w=1w=1: you would need 1010 to divide d′−dd'-d, impossible for a nonzero difference smaller than 10. With w=3w=3: since gcd⁡(3,10)=1\gcd(3,10)=1, you can cancel the 3 and you are back to the same impossibility. Every error in ONE digit is therefore detected, and that is precisely because 3 is relatively prime to 10.

d)

If two adjacent digits aa and bb, carrying the weights 1 and 3, are swapped, the sum goes from a+3ba+3b to b+3ab+3a, a change of 2(a−b)2\left(a-b\right). The error escapes the check if 2(a−b)≡0(mod10)2\left(a-b\right)\equiv 0\pmod{10}, that is, if a−b≡0(mod5)a-b\equiv 0\pmod 5, so if ∣a−b∣=5\left|a-b\right|=5. EXAMPLE: the code 97827234568079782723456807 is valid, with sum 103 plus a check digit 7, that is, 110. Swapping the 2 and the 7 in positions 4 and 5 gives 9782 ⁣→ ⁣9787…9782\!\to\!9787\dots, whose weighted sum becomes 113 plus 7, that is, 120, again a multiple of 10: the typing mistake is NOT detected. This is the known weakness of ISBN-13, and it is a serious flaw, since swapping two adjacent digits is the most common data-entry error.

e)

Because 11 is PRIME while 10 is not. Modulo a prime number, every nonzero coefficient is invertible and there are no zero divisors: swapping two digits aa and bb of weights wiw_{i} and wjw_{j} changes the sum by (wi−wj)(a−b)(w_{i}-w_{j})(a-b), and since wi−wjw_{i}-w_{j} is nonzero and lies between −9-9 and 9, 11 does not divide it, so this change can vanish modulo 11 only if 11 divides a−ba-b, that is, only if a=ba=b: there was no error. ISBN-10 therefore detected ALL transpositions of two digits, including non-adjacent ones. The price to pay is that a check digit can equal 10, hence the character XX, which was judged awkward for barcodes and explains why the system was abandoned in favour of the weaker but more convenient ISBN-13.

9×17×38×12×31×12×33×14×35×16×38×10×3?cweighted sum 97, the total must reach a multiple of 10check digit c = 3, since 97 + 3 = 100

Exercise 10: Problem: RSA encryption with small numbers

Everything so far comes together here. RSA encryption rests on the modular inverse, Fermat's little theorem and fast exponentiation, and it fits on one page as long as you take tiny prime numbers.

  • a) Choose p=11p=11 and q=13q=13. Compute the modulus n=pqn=pq and Euler's totient function φ(n)=(p−1)(q−1)\varphi(n)=(p-1)(q-1).
  • b) Choose the public exponent e=7e=7. Check that it is suitable, then compute the private exponent dd, the inverse of ee modulo φ(n)\varphi(n).
  • c) Encrypt the message M=9M=9 by C=Me mod nC=M^{e}\bmod n.
  • d) Decrypt CC by M=Cd mod nM=C^{d}\bmod n and check that you get 9 back.
  • e) Why is an attacker who knows nn and ee unable to recover dd in practice, when the method is obvious in principle?

Type your answers, the page tells you right or wrong 0/13

a)
b)
c)
d)
e)
Show the solution

Answers

  • a) n=143n = 143 and φ(n)=120\varphi(n) = 120
  • b) gcd⁡(7,120)=1\gcd(7,120)=1 and d=103d = 103, since 7×103=721=6(120)+17 \times 103 = 721 = 6(120)+1
  • c) C=48C = 48
  • d) M=48103 mod 143=9M = 48^{103} \bmod 143 = 9, by Euler's theorem
  • e) You would have to factor nn; with 600 digits, no known algorithm can, and this difficulty is observed, not proved
a)

n=11×13=143n=11\times 13=143. φ(n)=(11−1)(13−1)=10×12=120\varphi(n)=(11-1)(13-1)=10\times 12=120. The modulus nn is PUBLIC, it is part of the public key; φ(n)\varphi(n) is SECRET, because knowing it amounts to knowing the private key, as question e will show.

b)

You need gcd⁡(e,φ(n))=1\gcd(e,\varphi(n))=1: here gcd⁡(7,120)=1\gcd(7,120)=1, because 7 is prime and does not divide 120. The exponent is suitable. Look for dd such that 7d≡1(mod120)7d\equiv 1\pmod{120}. By extended Euclid or by guided trial: 7×103=721=720+1=6×120+17\times 103=721=720+1=6\times 120+1, so d=103d=103. Check: 721 mod 120=1721\bmod 120=1 ✓.

c)

C=97 mod 143C=9^{7}\bmod 143. Proceed by fast exponentiation, reducing at every step: 92=819^{2}=81; 94=812=65619^{4}=81^{2}=6561, and 6561=143×45+1266561=143\times 45+126 so 94≡1269^{4}\equiv 126; finally 97=94×92×9≡126×81×99^{7}=9^{4}\times 9^{2}\times 9\equiv 126\times 81\times 9. Compute 126×81=10,206≡53126\times 81=10{,}206\equiv 53 since 10,206=143×71+5310{,}206=143\times 71+53, then 53×9=477≡4853\times 9=477\equiv 48 since 477=143×3+48477=143\times 3+48. So C=48C=48. The encrypted message is 48.

d)

M=48103 mod 143M=48^{103}\bmod 143. The number 4810348^{103} has 174 digits and of course you never write it out: fast exponentiation reduces modulo 143 after every squaring, and you get M=9M=9 ✓, the original message. The theorem that guarantees this round trip is Euler's theorem, a generalization of Fermat's little theorem: since ed≡1(modφ(n))ed\equiv 1\pmod{\varphi(n)}, you get Med≡M(modn)M^{ed}\equiv M\pmod n.

e)

In principle the method is immediate: you only have to factor nn to recover pp and qq, deduce φ(n)\varphi(n), then dd by extended Euclid. Here, with n=143n=143, a student finds 11×1311\times 13 in a few seconds. But in real use nn has about 600 digits, and no known algorithm can factor such a number in a reasonable time: the best ones would take billions of years. All the security therefore rests on an ASYMMETRY of cost: multiplying two large primes is instantaneous, recovering the factors is not. Note that none of this is proved: nobody knows how to prove that factoring is hard; we only observe that nobody has found a way to make it easy.

Something missing from this set? A type of exercise your teacher gives, a problem you are stuck on, a mistake you spotted: tell me, I read every request.

Sending goes through your free space: that is how I can answer you and tell you when the exercise is added. An email is enough, no password. Create your space. Rather not create an account? Write to me directly.

What is it about?
I read every request myself, nobody else sees it. No need to write your name or your school.
When your request is dealt with, you hear about it by email and in your space.

By sending, you agree that I read your request to answer it. It is kept with your account, erased with it, and never published: privacy policy.

Previous chapter Number systems in computing, 201-N11 Next chapter Set theory and relations, 201-N11

© Ahmed Squalli Houssaini. Exercise set published at www.letuteurscientifique.ca/en/exercices/cegep-n11-modular-arithmetic. Free for personal and classroom use; republishing it elsewhere requires written permission (legal notice).

See also

Looking for a 201-N11 Mathematics for Computer Science tutor in Montreal?

Get in touch for a first session. We work on modular arithmetic at the level of real CEGEP tests, from congruences all the way to the extended Euclidean algorithm and RSA.

Site by Studio Squalli