Matrix Arithmetic Modulo p
Everything we have done with matrices (addition, multiplication, row reduction, inverses) works with any field of scalars, not just the real numbers. For a prime , the integers modulo form a field , and matrix arithmetic over powers cryptography, error-correcting codes and checksums.
Arithmetic in
, where you add and multiply as usual and then take the remainder after division by :
The key property: when is prime, every non-zero element has a multiplicative inverse. In , , so . You can find inverses with the extended Euclidean algorithm, or in Python with pow(a, -1, p). (For composite , is not a field: in , and 2 has no inverse.)
| in | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| 1 | 4 | 5 | 2 | 3 | 6 |
Because is a field, every algorithm built from , , and by non-zero numbers works unchanged. That includes Gaussian elimination.
Matrix operations mod p
Add and multiply matrices as usual, then reduce every entry mod . To row-reduce, "divide a row by its pivot" means "multiply by the pivot's inverse mod ".
from sympy import Matrix
p = 7
A = Matrix([[3, 5], [6, 2]])
B = Matrix([[4, 1], [2, 6]])
print("A + B mod 7:", ((A + B) % p).tolist())
print("A * B mod 7:", ((A * B) % p).tolist())
Ainv = A.inv_mod(p)
print("A^-1 mod 7:", Ainv.tolist())
print("A * A^-1 mod 7:", ((A * Ainv) % p).tolist())
print("inverse of 3 mod 7:", pow(3, -1, 7))A + B mod 7: [[0, 6], [1, 1]]
A * B mod 7: [[1, 5], [0, 4]]
A^-1 mod 7: [[4, 4], [2, 6]]
A * A^-1 mod 7: [[1, 0], [0, 1]]
inverse of 3 mod 7: 5
The 2×2 inverse formula still works, with "divide by " replaced by "multiply by " (Exercise 1 does it by hand for this ).
Solving a system mod p
Here is Gaussian elimination written for in plain Python. It is the familiar algorithm with pow(pivot, -1, p) in place of division:
def solve_mod(A, b, p):
n = len(A)
M = [row[:] + [bi] for row, bi in zip(A, b)]
for col in range(n):
piv = next(r for r in range(col, n) if M[r][col] % p != 0) # find a pivot
M[col], M[piv] = M[piv], M[col]
inv = pow(M[col][col], -1, p)
M[col] = [(v * inv) % p for v in M[col]] # scale pivot to 1
for r in range(n):
if r != col and M[r][col] % p:
f = M[r][col]
M[r] = [(v - f * w) % p for v, w in zip(M[r], M[col])]
return [row[-1] for row in M]
A = [[2, 3, 1], [1, 4, 5], [3, 1, 2]]
b = [1, 0, 4]
x = solve_mod(A, b, 11)
print("x =", x)
print("check A x mod 11:", [sum(a * xi for a, xi in zip(row, x)) % 11 for row in A])x = [3, 10, 9]
check A x mod 11: [1, 0, 4]
When is a matrix invertible mod ?
A square matrix with integer entries is invertible mod exactly when its determinant is not divisible by . So the same integer matrix can be invertible over but singular mod some primes:
from sympy import Matrix
A = Matrix([[2, 1], [1, 4]]) # det = 7
print("det over the integers:", A.det())
for p in [2, 3, 5, 7, 11]:
ok = A.det() % p != 0
print(f"p = {p:2d}: det mod p = {A.det() % p} invertible: {ok}")
print("rank mod 7 is 1, row 2 ≡ 4·row 1:", ((4 * A.row(0)) % 7).tolist(), A.row(1).tolist())det over the integers: 7
p = 2: det mod p = 1 invertible: True
p = 3: det mod p = 1 invertible: True
p = 5: det mod p = 2 invertible: True
p = 7: det mod p = 0 invertible: False
p = 11: det mod p = 7 invertible: True
rank mod 7 is 1, row 2 ≡ 4·row 1: [[1, 4]] [[1, 4]]
: linear algebra with bits
In , addition is XOR and multiplication is AND. A vector over is a bit string and a matrix over is a grid of bits, so matrix–vector products are XORs of selected rows. That makes linear algebra extremely fast in hardware.
In software
- Error-correcting codes: Hamming codes, used in ECC memory, encode a 4-bit message as a 7-bit codeword with a 4×7 generator matrix over ; a 3×7 parity-check matrix finds and fixes any single flipped bit (Application: Error Correcting Codes).
- Checksums: CRCs (Ethernet, ZIP, PNG) are linear maps over computed with shifts and XORs.
- Cryptography: AES mixes bytes with a fixed matrix over the field with 256 elements (MixColumns); the classical Hill cipher uses matrices mod 26 (Application: Cryptography).
- Puzzles and algorithms: the "Lights Out" puzzle is a linear system over ; Gaussian elimination over solves it, and also appears in factoring algorithms (the quadratic sieve).
- Integer overflow: arithmetic on 32-bit unsigned integers is arithmetic mod , which is a ring, not a field, because is not prime: even numbers have no inverse.
Exercises
Exercise 1: Inverse mod 7 by formula
Compute the inverse of with the 2×2 formula and compare with the SymPy result above.
Answer
.
Solution
Check: .
from sympy import Matrix
A = Matrix([[3, 5], [6, 2]])
X = Matrix([[4, 4], [2, 6]])
print(((A * X) % 7).tolist(), X == A.inv_mod(7))[[1, 0], [0, 1]] True
Exercise 2: Inverses in
Find and in .
Answer
and .
Solution
, and . In Python: pow(5, -1, 13) → 8, pow(7, -1, 13) → 2.
Exercise 3: Which primes break it?
For , list the primes for which is not invertible mod .
Answer
and .
Solution
. is singular mod exactly when divides 18, i.e. or .
Exercise 4: Solve over
Solve over : , , , (four equations, three unknowns). Reason with XOR, then confirm by brute force over all 8 bit vectors.
Answer
Exactly one solution: .
Solution
Brute force confirms it is the only one:
from itertools import product
eqs = [((1, 1, 0), 1), ((0, 1, 1), 0), ((1, 0, 1), 1), ((1, 1, 1), 0)]
sols = [x for x in product([0, 1], repeat=3)
if all(sum(a * xi for a, xi in zip(c, x)) % 2 == r for c, r in eqs)]
print(sols)[(0, 1, 1)]
Test yourself
What is in ?
- It does not exist
- 4
- 7
- 3
An integer matrix has determinant 15. Modulo which prime is it NOT invertible?
- 2
- 7
- 5
- 11
In , what operation is addition of bits?
- AND
- XOR
- OR
- NAND