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 . Diffie-Hellman keys stay below a prime . 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 and a positive integer , is the remainder when is divided by : the same division algorithm from the previous page, with and , now extended to integers that can be negative. The remainder is always non-negative, regardless of the sign of .
For , : , so .
For , : , so .
Pitfall
rounds toward negative infinity, not toward zero. For , truncating toward zero gives and a remainder of — negative, and outside the required range . This is the case the Pitfall on the previous page warned about: the two rounding rules agree for non-negative and disagree the moment goes negative. Every mod result on this site, and every mod result the exam expects, is non-negative.
Mechanism
Two integers and are congruent modulo if they leave the same remainder: . Written , read ” is congruent to modulo .” Congruence does not require and to be close, or equal, or even both positive — only that dividing each by leaves the same remainder. One direct consequence: exactly when .
, because and . , because and — the same remainder, even though and are ten apart.
Mechanism
Ordinary arithmetic properties carry over into modular arithmetic. For any integers , and modulus :
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 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, , , . First, and .
- Addition. LHS: . RHS: . LHS = RHS.
- Subtraction. LHS: . RHS: . LHS = RHS.
- Multiplication. LHS: . RHS: , since and . LHS = RHS.
Mechanism
Define as the set of non-negative integers less than : . Every integer, not only the ones in this range, falls into one of these residue classes. The class is the set of every integer congruent to modulo : lists one representative from each class — specifically the smallest non-negative one, which is why always lands inside . Arithmetic on is exactly the modular arithmetic above: add, subtract or multiply the representatives and reduce mod to land back inside the set.
Mechanism
Exponentiation is repeated multiplication, so the same reduce-as-you-go idea applies: compute 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
- . , since and .
- . .
- Write as , since .
- . , and .
- : , and . So .
Pitfall
The Week 4 abstract-algebra supplement uses to show that has no multiplicative inverse
modulo , and its printed working states . That line is wrong: , and
, not . The full, corrected table of for is
— every result is even, so none of them is , and the conclusion (no inverse
exists) still holds. Only the one printed line, , is the error. 04-06 returns to this same
table as the counter-example for why 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 starts: the definition of exists specifically to guarantee . Second, checking congruence never requires comparing and directly — only their two remainders. and look nothing alike, and are still congruent modulo .
Formula
Congruence modulo n
- any integers
- 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 . must be , the floor, not , the truncation toward zero. , which satisfies the range; does not.
Source
Week 4 Number Theory PDF