Solving Linear Congruences
A linear congruence is the modular version of . It may have no solution, exactly one, or several, and the gcd decides which. Affine ciphers, linear congruential generators and hash-function analysis all come down to solving one.
The rule
Let .
- If , there is no solution.
- If , there are exactly solutions modulo . Divide through by : has the unique solution , and the solutions modulo are .
This is the linear Diophantine equation in another notation.
from math import gcd
def solve_linear(a, b, n):
g = gcd(a, n)
if b % g:
return []
a2, b2, n2 = a // g, b // g, n // g
x0 = b2 * pow(a2, -1, n2) % n2 if n2 > 1 else 0
return [x0 + k * n2 for k in range(g)]
print(solve_linear(7, 3, 10)) # g = 1: one solution
print(solve_linear(6, 4, 10)) # g = 2: two solutions
print(solve_linear(6, 5, 10)) # g = 2 does not divide 5: none
print(solve_linear(12, 18, 30))[9]
[4, 9]
[]
[4, 9, 14, 19, 24, 29]
Check one: .
The affine cipher
A classic toy cipher maps letter to . Decryption solves the linear congruence: . It only works when , so 12 values of are usable.
from math import gcd
def affine(text, a, b, decrypt=False):
out = []
for ch in text:
x = ord(ch) - 65
y = (pow(a, -1, 26) * (x - b)) % 26 if decrypt else (a * x + b) % 26
out.append(chr(y + 65))
return "".join(out)
c = affine("MODULARARITHMETIC", 5, 8)
print(c, affine(c, 5, 8, decrypt=True))
print("valid a:", [a for a in range(26) if gcd(a, 26) == 1])QAXELIPIPWZRQCZWS MODULARARITHMETIC
valid a: [1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25]
With only keys, it falls to brute force or to two known letter pairs (two linear congruences in and ). It is a teaching example of the arithmetic, not a cipher.
Linear congruential generators
The old random-number generator is a linear congruence too. Given three consecutive outputs and , you can solve for and and predict every later value: one reason LCGs must never be used for security (Secure Random Numbers).
m, a, c = 2**31, 1103515245, 12345 # a classic C library LCG
xs = [42]
for _ in range(4):
xs.append((a * xs[-1] + c) % m)
# attacker sees xs[0], xs[1], xs[2]; solve a*(x1 - x0) = x2 - x1 (mod m)
d1, d2 = xs[1] - xs[0], xs[2] - xs[1]
from math import gcd
g = gcd(d1, m)
cands = [(d2 // g) * pow(d1 // g, -1, m // g) % (m // g) + k * (m // g) for k in range(g)] if d2 % g == 0 else []
for a_guess in cands:
c_guess = (xs[1] - a_guess * xs[0]) % m
pred = (a_guess * xs[2] + c_guess) % m
print("a, c =", a_guess, c_guess, " predicted next:", pred, " actual:", xs[3])a, c = 1103515245 12345 predicted next: 1000676753 actual: 1000676753
In software
java.util.Random, olderrand()implementations and many game engines use LCGs: fine for shuffling a level, broken as soon as an adversary sees outputs.- Compilers turn division by a constant into multiplication by a modular inverse when the division is known to be exact (for example pointer differences divided by the element size).
Exercises
Exercise 1: Count the solutions
How many solutions modulo 24 do these have: (a) ; (b) ; (c) ?
Answer
(a) 3, (b) 0, (c) 1.
Solution
divides 6 but not 5. , so (c) has exactly one solution.
Exercise 2: Solve by hand
Solve .
Answer
.
Solution
Divide by 3: . The inverse of 3 modulo 8 is 3 (), so . Modulo 24: . Check: .
Exercise 3: Break an affine cipher (code)
An affine cipher maps E to R and T to W (letters numbered A = 0). Find and .
Answer
, .
Solution
E = 4 maps to R = 17 and T = 19 maps to W = 22: and . Subtracting gives . Since , and ; then .
a = 5 * pow(15, -1, 26) % 26
b = (17 - 4 * a) % 26
print(a, b, (4 * a + b) % 26, (19 * a + b) % 26)9 7 17 22
Test yourself
How many solutions does have when divides ?
- exactly 1
- exactly modulo
- none
An affine cipher can be decrypted only if…
- is even
Why are linear congruential generators unsafe for cryptography?
- they are slow
- a few outputs let you solve for the parameters and predict the rest
- they need a prime modulus
- they never repeat