12.5 KB · commit a9602da
← Previous: DM-5.4 Modular Arithmetic and Congruence | 🏠 Index | Next: DM-5 Lower-a: Primes, GCD, Fermat, CRT →
Section: B (Discrete Mathematics) | Priority: 🔴 | Exam Frequency: 2/3 DM papers (2022 Q.7(b), 2024 Q.8(b), recycled verbatim) Subtopics: DM-5.5 (Modular Exponentiation) Sources: Rosen §4.2 (pp. 15–18, Algorithm 5 and Example 12), Fahad Notes Class-19, Raidah Notes §5, Shishir Notes §10–§11
This is a two-paper topic with a single dominant question. 2022 and 2024 asked exactly the same computation, 3644mod645, with the same answer. The paper says "using a well-known algorithm" and expects you to name it, so the algorithm name is worth marks on its own.
The idea behind the algorithm is worth more than the arithmetic. Modular exponentiation is how RSA and every modern cipher actually runs. Understanding why O(logn) squarings replace n multiplications is the difference between memorizing a table and knowing the method.
The problem: "In cryptography it is important to be able to find bnmodm efficiently, where b,n, and m are large integers. It is impractical to first compute bn and then find its remainder when divided by m because bn will be a huge number." — Rosen, §4.2, Modular Exponentiation (p. 17)
The idea: "to compute bn, we need only compute the values of b,b2,(b2)2=b4,(b4)2=b8,…,b2k. Once we have these values, we multiply the terms b2j in this list, where aj=1." — Rosen, §4.2 (p. 17)
The algorithm: "Algorithm 5: Modular Exponentiation.
procedure modular exponentiation(b: integer, n = (ak-1 ak-2 ... a1 a0)_2, m: positive integers) x := 1 power := b mod m for i := 0 to k - 1 if ai = 1 then x := (x · power) mod m power := (power · power) mod m return x {x equals b^n mod m} ```" — Rosen, §4.2 (p. 18)
The complexity: "Algorithm 5 is quite efficient; it uses O((logm)2logn) bit operations to find bnmodm." — Rosen, §4.2 (p. 18). In terms of squarings and multiplications it is O(logn) of each.
Key term: "modular exponentiation: computing bnmodm efficiently using the binary expansion of n." — Rosen, §4.2, Key Terms (p. 14)
The name the exam wants is repeated squaring, also called binary exponentiation or the square-and-multiply method. All three are accepted. Say "repeated squaring" first, since that is Rosen's phrase in §4.2 and it is what the paper's "well-known algorithm" refers to.
To compute 3644mod645 by repeated multiplication you would multiply 644 times. Each result is under 645, so the arithmetic is easy, but 644 steps is a page of work with a large chance of one arithmetic slip near the end.
Write the exponent in binary. Each 1 bit says "multiply in the power of two at this position."
644=512+128+4=(1010000100)2So 3644=3512⋅3128⋅34. Only three factors. And those factors are not independent: 3512=(3256)2, 3256=(3128)2, and so on. Squaring once produces the next power of two. So the whole computation needs:
Rosen's Algorithm 5, translated into a procedure you can write in the exam:
| Line | What it does |
|---|---|
| x:=1 | the running product, start at the multiplicative identity |
| power:=bmodm | the current row of the table, b20 |
| for i:=0 to k−1 | one pass per bit of the exponent |
| if ai=1 then x:=(x⋅power)modm | multiply in this row only if the bit is set |
| power:=(power⋅power)modm | advance to the next row, b2i+1 |
| return x | x=bnmodm |
The square happens every iteration. The multiply happens conditionally. That is the whole algorithm.
Source: 2022 Q.7(b), 2024 Q.8(b). The same question appears in both papers.
Question. Solve 3644mod645 using a well-known algorithm.
Solution. The well-known algorithm is repeated squaring.
Step 1. Binary expansion of the exponent.
644=512+128+4=(1010000100)2Step 2. Square repeatedly, reducing modulo 645 at every step.
31=3 32=9 34=92=81 38=812=6561=645(10)+111≡111 316=1112=12321=645(19)+66≡66 332=662=4356=645(6)+486≡486 364=4862=236196=645(366)+126≡126 3128=1262=15876=645(24)+396≡396 3256=3962=156816=645(243)+81≡81 3512=812=6561≡111Step 3. Multiply the powers named by the set bits, which are 29=512, 27=128 and 22=4:
3644≡3512⋅3128⋅34 =111×396×81 111×396=43956=645(68)+96≡96 96×81=7776=645(12)+36≡36 3644mod645=36Reading the squaring table as a table. The bits of 644=(1010000100)2, read from position 0 on the right, are:
| Bit position i | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|---|---|
| Bit ai | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
| 2i | 512 | 256 | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| 32imod645 | 111 | 81 | 396 | 126 | 486 | 66 | 111 | 81 | 9 | 3 |
Three bits are set, at positions 9, 7 and 2. Those three rows are the ones multiplied. Reading it this way is faster than working left to right, because the set bits jump out at you.
Rosen's trace of the same computation. This question is Rosen §4.2 Example 12 (p. 18) reproduced without credit. Rosen runs Algorithm 5 as written, tracking x and power side by side:
| i | ai | x | power |
|---|---|---|---|
| 0 | 0 | 1 | 32mod645=9 |
| 1 | 0 | 1 | 92mod645=81 |
| 2 | 1 | 81 | 812mod645=111 |
| 3 | 0 | 81 | 1112mod645=66 |
| 4 | 0 | 81 | 662mod645=486 |
| 5 | 0 | 81 | 4862mod645=126 |
| 6 | 0 | 81 | 1262mod645=396 |
| 7 | 1 | 471 | 3962mod645=81 |
| 8 | 0 | 471 | 812mod645=111 |
| 9 | 1 | 36 | done |
The power column reproduces the squaring table exactly. The x column is the running product, and only three rows change it.
Examiners will not always use 644 and 645. The method is the same.
| Step | Question | Answer |
|---|---|---|
| 1 | Write n in binary | n=(ak…a0)2 |
| 2 | Build b2imodm for i=0,…,k | about log2n squarings |
| 3 | Multiply the rows where ai=1 | about half of log2n multiplications |
| 4 | Report the residue | lies in [0,m−1] |
When the base shares a factor with the modulus, stop immediately and read off the answer. 3∣645, so 3644≡0(mod3) without any work. That observation is what makes the CRT cross-check above cheap.
| Year | Question | Data | Algorithm | Answer |
|---|---|---|---|---|
| 2022 Q.7(b) | "Solve 3644mod645 using a well-known algorithm" | 3644mod645 | repeated squaring | 3644mod645=36 |
| 2024 Q.8(b) | recycled verbatim from 2022 | 3644mod645 | repeated squaring | 3644mod645=36 |
The variant to rehearse is the method itself, not the numbers. If a paper asks for 2100mod101 or 71000mod11, the same four steps apply.
← Previous: DM-5.4 Modular Arithmetic and Congruence | 🏠 Index | Next: DM-5 Lower-a: Primes, GCD, Fermat, CRT →