CSEC3616Cybersecurity Engineering

    Modular arithmetic and residue classes

    The mod operator for positive and negative integers, congruence modulo n, the addition, subtraction and multiplication properties, residue classes in Zn, and a full worked modular exponentiation.

    • Compute a mod n for negative and positive a, including the case -11 mod 7 = 3.
    • State when two integers are congruent modulo n and check a given pair.
    • Verify the addition, subtraction and multiplication properties of modular arithmetic against a worked numeric example.
    • List the residue classes of Zn and compute a modular exponentiation step by step.

    15 min readJump to the quiz ↓

    Mod wraps every integer into a fixed range, and the remainder is never negative

    Every cryptographic scheme in this unit computes inside a fixed-size set of numbers. RSA ciphertexts stay below the modulus nn, and Diffie-Hellman keys stay below a prime pp. Without a boundary, encrypting even a short message would produce numbers thousands of digits long. The boundary is enforced by one operation, mod: values wrap around like a clock instead of growing forever.

    For an integer aa and a positive integer nn, a mod na \bmod n is the remainder when aa is divided by nn. It is the division algorithm from the previous page, a=qn+ra = qn + r with 0≤r<n0 \le r < n and q=⌊a/n⌋q = \lfloor a/n \rfloor, now extended to integers aa that can be negative. The remainder is always non-negative, whatever the sign of aa.

    For a=11a = 11, n=7n = 7: q=⌊11/7⌋=1q = \lfloor 11/7 \rfloor = 1, so 11 mod 7=11−7=411 \bmod 7 = 11 - 7 = 4.

    For a=−11a = -11, n=7n = 7: q=⌊−11/7⌋=⌊−1.571…⌋=−2q = \lfloor -11/7 \rfloor = \lfloor -1.571\ldots \rfloor = -2, so −11 mod 7=−11−(−2×7)=−11+14=3-11 \bmod 7 = -11 - (-2 \times 7) = -11 + 14 = 3.

    Pitfall

    q=⌊a/n⌋q = \lfloor a/n \rfloor rounds toward negative infinity, not toward zero. For a=−11a = -11, truncating toward zero gives q=−1q = -1 and a remainder of −4-4, which is negative and outside the required range 0≤r<n0 \le r < n. The two rounding rules agree for non-negative aa and disagree the moment aa goes negative. Every mod result on this site, and every mod result the exam expects, is non-negative.

    Recall

    Why does -11 mod 7 give 3, and not -4?

    Because the definition requires 0≤r<n0 \le r < n. qq must be ⌊−11/7⌋=−2\lfloor -11/7 \rfloor = -2, the floor, not −1-1, the truncation toward zero. −11−(−2×7)=3-11 - (-2 \times 7) = 3, which satisfies the range; −11−(−1×7)=−4-11 - (-1 \times 7) = -4 does not.

    Congruent numbers share a remainder, so you can reduce before you combine

    Two integers aa and bb are congruent modulo nn if they leave the same remainder: (a mod n)=(b mod n)(a \bmod n) = (b \bmod n). This is written a≡b(modn)a \equiv b \pmod{n}. Congruence does not require aa and bb to be close, equal, or both positive, only that dividing each by nn leaves the same remainder. One consequence: a≡0(modn)a \equiv 0 \pmod{n} exactly when n∣an \mid a.

    73≡4(mod23)73 \equiv 4 \pmod{23}, because 73=3×23+473 = 3 \times 23 + 4 and 4<234 < 23. 21≡31(mod10)21 \equiv 31 \pmod{10}, because 21 mod 10=121 \bmod 10 = 1 and 31 mod 10=131 \bmod 10 = 1, the same remainder even though 2121 and 3131 are ten apart. Checking congruence never requires comparing aa and bb directly, only their two remainders.

    Formula

    Congruence modulo n

    a≡b(modn)  ⟺  (a mod n)=(b mod n)a \equiv b \pmod{n} \iff (a \bmod n) = (b \bmod n)
    a,ba, b
    any integers
    nn
    the modulus

    a ≡ 0 (mod n) if and only if n divides a.

    Ordinary arithmetic properties carry over. For any integers aa, bb and modulus nn:

    [(a mod n)+(b mod n)] mod n=(a+b) mod n[(a \bmod n) + (b \bmod n)] \bmod n = (a + b) \bmod n [(a mod n)−(b mod n)] mod n=(a−b) mod n[(a \bmod n) - (b \bmod n)] \bmod n = (a - b) \bmod n [(a mod n)×(b mod n)] mod n=(a×b) mod n[(a \bmod n) \times (b \bmod n)] \bmod n = (a \times b) \bmod n

    Reducing first and then combining gives the same answer as combining first and then reducing. This is what lets implementations keep every intermediate value small: reduce mod nn at every step instead of computing the full-size result and reducing once at the end.

    Try it — worked example

    With a = 11, b = 9, n = 8, check that modular addition, subtraction and multiplication give the same result whether you reduce first or last.

    Show answer and steps

    AnswerAll three properties hold for a = 11, b = 9, n = 8

    The lecture verifies each property with the same pair, a=11a = 11, b=9b = 9, n=8n = 8. First, 11 mod 8=311 \bmod 8 = 3 and 9 mod 8=19 \bmod 8 = 1.

    1. Addition. LHS: [(11 mod 8)+(9 mod 8)] mod 8=[3+1] mod 8=4 mod 8=4[(11 \bmod 8) + (9 \bmod 8)] \bmod 8 = [3 + 1] \bmod 8 = 4 \bmod 8 = 4. RHS: (11+9) mod 8=20 mod 8=4(11 + 9) \bmod 8 = 20 \bmod 8 = 4. LHS = RHS.
    2. Subtraction. LHS: [(11 mod 8)−(9 mod 8)] mod 8=[3−1] mod 8=2[(11 \bmod 8) - (9 \bmod 8)] \bmod 8 = [3 - 1] \bmod 8 = 2. RHS: (11−9) mod 8=2 mod 8=2(11 - 9) \bmod 8 = 2 \bmod 8 = 2. LHS = RHS.
    3. Multiplication. LHS: [(11 mod 8)×(9 mod 8)] mod 8=[3×1] mod 8=3[(11 \bmod 8) \times (9 \bmod 8)] \bmod 8 = [3 \times 1] \bmod 8 = 3. RHS: (11×9) mod 8=99 mod 8=3(11 \times 9) \bmod 8 = 99 \bmod 8 = 3, since 8×12=968 \times 12 = 96 and 99−96=399 - 96 = 3. LHS = RHS.

    Z_n holds one representative per residue class, and powers are built by reducing as you go

    Define Zn\mathbb{Z}_n as the set of non-negative integers less than nn: Zn={0,1,…,n−1}\mathbb{Z}_n = \{0, 1, \ldots, n-1\}. Every integer, not only the ones in this range, falls into one of these nn residue classes. The class [r][r] is the set of every integer congruent to rr modulo nn: [r]={a:a is an integer, a≡r(modn)}.[r] = \{a : a \text{ is an integer}, \ a \equiv r \pmod{n}\}. Zn\mathbb{Z}_n lists the smallest non-negative member of each class, which is why a mod na \bmod n always lands inside Zn\mathbb{Z}_n. Arithmetic on Zn\mathbb{Z}_n is the modular arithmetic above: add, subtract or multiply the representatives and reduce mod nn to land back inside the set.

    Exponentiation is repeated multiplication, so the same reduce-as-you-go idea applies. Compute ak mod na^k \bmod n by building the power through smaller modular multiplications instead of computing the full power first. This one calculation is the basis of Diffie-Hellman key exchange in Module 6.

    Try it — worked example

    Compute 11^7 mod 13 without calculating 11^7 in full.

    Show answer and steps

    Answer11^7 mod 13 = 2

    1. 112=12111^2 = 121. 121 mod 13=4121 \bmod 13 = 4, since 13×9=11713 \times 9 = 117 and 121−117=4121 - 117 = 4.
    2. 114=(112)2≡42=16(mod13)11^4 = (11^2)^2 \equiv 4^2 = 16 \pmod{13}. 16 mod 13=316 \bmod 13 = 3.
    3. Write 11711^7 as 111×112×11411^1 \times 11^2 \times 11^4, since 1+2+4=71 + 2 + 4 = 7.
    4. 117≡11×4×3(mod13)11^7 \equiv 11 \times 4 \times 3 \pmod{13}. 11×4=4411 \times 4 = 44, and 44×3=13244 \times 3 = 132.
    5. 132 mod 13132 \bmod 13: 13×10=13013 \times 10 = 130, and 132−130=2132 - 130 = 2. So 117≡2(mod13)11^7 \equiv 2 \pmod{13}.

    Pitfall

    The Week 4 abstract-algebra supplement uses Z8\mathbb{Z}_8 to show that 22 has no multiplicative inverse modulo 88, and its printed working states 2⋅2 mod 8=02 \cdot 2 \bmod 8 = 0. That line is wrong: 2×2=42 \times 2 = 4, and 4 mod 8=44 \bmod 8 = 4, not 00. The corrected table of 2⋅k mod 82 \cdot k \bmod 8 for k=0,…,7k = 0, \ldots, 7 is 0,2,4,6,0,2,4,60, 2, 4, 6, 0, 2, 4, 6. Every result is even, so none is 11, and the conclusion (no inverse exists) still holds. 04-06 returns to this table as the counter-example for why Z8\mathbb{Z}_8 is a commutative ring but not a field.

    Check yourself