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 read

    Intuition

    Every cryptographic scheme in this unit computes inside a fixed-size set of numbers, never letting a result grow past a chosen boundary. RSA ciphertexts stay below the modulus nn. Diffie-Hellman keys stay below a prime pp. That boundary is enforced by one operation: mod. Without it, encrypting even a short message would produce numbers thousands of digits long, and there would be no fixed-size ciphertext space to define a cryptosystem over in the first place. Modular arithmetic is “clock arithmetic” — the values wrap around instead of growing forever — and everything from here to Diffie-Hellman key exchange in Module 6 is built on it.

    Mechanism

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

    For a=11a = 11, n=7n = 7: q=11/7=1q = \lfloor 11/7 \rfloor = 1, so 11mod7=117=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 11mod7=11(2×7)=11+14=3-11 \bmod 7 = -11 - (-2 \times 7) = -11 + 14 = 3.

    Pitfall

    q=a/nq = \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 — negative, and outside the required range 0r<n0 \le r < n. This is the case the Pitfall on the previous page warned about: 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.

    Mechanism

    Two integers aa and bb are congruent modulo nn if they leave the same remainder: (amodn)=(bmodn)(a \bmod n) = (b \bmod n). Written ab(modn)a \equiv b \pmod{n}, read ”aa is congruent to bb modulo nn.” Congruence does not require aa and bb to be close, or equal, or even both positive — only that dividing each by nn leaves the same remainder. One direct consequence: a0(modn)a \equiv 0 \pmod{n} exactly when nan \mid a.

    734(mod23)73 \equiv 4 \pmod{23}, because 73=3×23+473 = 3 \times 23 + 4 and 4<234 < 23. 2131(mod10)21 \equiv 31 \pmod{10}, because 21mod10=121 \bmod 10 = 1 and 31mod10=131 \bmod 10 = 1 — the same remainder, even though 2121 and 3131 are ten apart.

    Mechanism

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

    [(amodn)+(bmodn)]modn=(a+b)modn[(a \bmod n) + (b \bmod n)] \bmod n = (a + b) \bmod n [(amodn)(bmodn)]modn=(ab)modn[(a \bmod n) - (b \bmod n)] \bmod n = (a - b) \bmod n [(amodn)×(bmodn)]modn=(a×b)modn[(a \bmod n) \times (b \bmod n)] \bmod n = (a \times b) \bmod n

    In plain terms: reducing first and then combining gives the same answer as combining first and then reducing. This is what lets cryptographic 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.

    Worked example

    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, 11mod8=311 \bmod 8 = 3 and 9mod8=19 \bmod 8 = 1.

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

    Mechanism

    Define Zn\mathbb{Z}_n as the set of non-negative integers less than nn: Zn={0,1,,n1}\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, ar(modn)}.[r] = \{a : a \text{ is an integer}, \ a \equiv r \pmod{n}\}. Zn\mathbb{Z}_n lists one representative from each class — specifically the smallest non-negative one, which is why amodna \bmod n always lands inside Zn\mathbb{Z}_n. Arithmetic on Zn\mathbb{Z}_n is exactly the modular arithmetic above: add, subtract or multiply the representatives and reduce mod nn to land back inside the set.

    Mechanism

    Exponentiation is repeated multiplication, so the same reduce-as-you-go idea applies: compute akmodna^k \bmod n by building up the power through smaller modular multiplications instead of computing the (potentially huge) full power first. This one calculation is the basis of Diffie-Hellman key exchange in Module 6.

    Worked example

    Answer11^7 mod 13 = 2

    1. 112=12111^2 = 121. 121mod13=4121 \bmod 13 = 4, since 13×9=11713 \times 9 = 117 and 121117=4121 - 117 = 4.
    2. 114=(112)242=16(mod13)11^4 = (11^2)^2 \equiv 4^2 = 16 \pmod{13}. 16mod13=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. 11711×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. 132mod13132 \bmod 13: 13×10=13013 \times 10 = 130, and 132130=2132 - 130 = 2. So 1172(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 22mod8=02 \cdot 2 \bmod 8 = 0. That line is wrong: 2×2=42 \times 2 = 4, and 4mod8=44 \bmod 8 = 4, not 00. The full, corrected table of 2kmod82 \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 of them is 11, and the conclusion (no inverse exists) still holds. Only the one printed line, 22=02 \cdot 2 = 0, is the error. 04-06 returns to this same table as the counter-example for why Z8\mathbb{Z}_8 is a commutative ring but not a field.

    Exam detail

    Two things are worth having automatic. First, a mod result is never negative, no matter how negative aa starts: the definition of q=a/nq = \lfloor a/n \rfloor exists specifically to guarantee 0r<n0 \le r < n. Second, checking congruence never requires comparing aa and bb directly — only their two remainders. 2121 and 3131 look nothing alike, and are still congruent modulo 1010.

    Formula

    Congruence modulo n

    ab(modn)    (amodn)=(bmodn)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.

    Recall

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

    Because the definition requires 0r<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.