Modular Arithmetic
Congruence Relation
If
begin{aligned}
p \mid a - b \
\text{, where } a, b, p \in Z \text{ with } 0 \leq a, b < p}
end{aligned}
then we denote:
a≡b(modp)
Assumptions for Following Operations
If
a≡b(modp)
and
c≡d(modp)
Addition and Subtraction
a±c≡b±d(modp)
Proof:
We know p∣a−b and p∣c−d, so
a−b=mp
begin{aligned}
c - d = np \
\text{, where $m,n \in Z$}
end{aligned}
The above equations can be represented as:
a=mp+b
c=np+d
Thus,
a±c===(mp+b)±(np+d)(m±n)p+(b±d)kp+(b±d), where k∈Z
and it's obviously that
a±c≡b±d(modp)
Multiplication
a⋅c≡b⋅d(modp)
Proof:
\begin{aligned}
a - b = mp \Rightarrow a = mp + b \
\text{, where } m \in Z
\end{aligned}
\begin{aligned}
c - d = np \Rightarrow c = np + d \
\text{, where } n \in Z
\end{aligned}
a⋅c===(mp+b)⋅(np+d)(mnp+md+nb)⋅p+(b⋅d)kp+(b⋅d), where k∈Z
Thus,
a⋅c≡b⋅d(modp)
The other obvious truth is that:
\begin{aligned}
r \cdot a \equiv r \cdot b \pmod p \
\text{, where } r \in Z
\end{aligned}
This is pretty easy to prove by:
a=mp+b
r⋅a===r⋅(mp+b)(rm)⋅p+r⋅bq⋅p+r⋅b
Exponentiation
\begin{aligned}
a^n \equiv b^n \pmod p \
\text{, where } n \in Z
\end{aligned}
Proof:
I. Bottom-up Approach
By a⋅c≡b⋅d(modp)
, if we replace c with a and d with b,
then
a⋅a≡b⋅b(modp)
⇒a2≡b2(modp)
Next, by replacing c with a2 and d with b2,
we can get
a⋅a2≡b⋅b2(modp)
⇒a3≡b3(modp)
Finally you can run to an≡bn(modp).
II. Mathematical Induction
Formally, we can use mathematical induction to prove it.
1. Basis: the statement is true for n=1
because a≡b(modp) is true.
2. Inductive step:
Show that if n=k holds, then also n=k+1 holds.
Assume ak≡bk(modp) is true.
By a⋅c≡b⋅d(modp):
a⋅ak≡b⋅bk(modp)
⇒ak+1≡bk+1(modp)
thereby showing that indeed n=k+1 holds.
Modular Operation
Suppose that a,b,p∈Z
Addition and Subtraction
(a±b)modp=(amodp±bmodp)modp
Proof:
Assume that
\begin{aligned}
a = mp + i \Rightarrow a \bmod p = i \
\text{, where } m, i\in Z \text{ and } 0 \leq i < p
\end{aligned}
and
\begin{aligned}
b = np + j \Rightarrow b \bmod p = j \
\text{, where } n, j \in Z \text{ and } 0 \leq j < p
\end{aligned}
Therefore,
(a±b)modp=((m±n)p+(i±j))modp=(i±j)modp=(amodp±bmodp)modp
Multiplication
(a⋅b)modp=(amodp⋅bmodp)modp
Proof:
The step is basically same as above.
First, we assume that
\begin{aligned}
a = mp + i \Rightarrow a \bmod p = i \
\text{, where } m, i\in Z \text{ and } 0 \leq i < p
\end{aligned}
and
\begin{aligned}
b = np + j \Rightarrow b \bmod p = j \
\text{, where } n, j \in Z \text{ and } 0 \leq j < p
\end{aligned}
Therefore,
(a⋅b)modp=((mp+i)⋅(np+j))modp=((mnp+mj+ni)p+i⋅j)modp=(i⋅j)modp=(amodp⋅bmodp)modp
On the other hand, another multiplication operation is:
\begin{aligned}
(x \cdot y) \bmod p = x \cdot (y \bmod p) \bmod p \
\text{, where } x, y \in Z
\end{aligned}
This is easy to prove:
(x⋅y)modp=(x⋅(mp+r))modp=(x⋅mp+x⋅r)modp=x⋅rmodp=x⋅(ymodp)modp, where y=mp+r,m,r∈Z and 0≤r<p
Exponentiation
\begin{aligned}
a^n \bmod p = (a \bmod p)^n \bmod p \
\text{, where } n \in Z
\end{aligned}
Proof:
I. Mathematical Induction
1. Basis: the statement is true for n=1
because (amodp)modp still equal to amodp.
2. Inductive step:
Show that if n=k holds, then also n=k+1 holds.
Assume akmodp=(amodp)kmodp is true, then
(a⋅ak)modp=(amodp⋅akmodp)modp=(amodp⋅(amodp)kmodp)modp=(amodp⋅(amodp)k)modp=(amodp)k+1modp
- The first equation is derived by replacing
b with ak in (a⋅b)modp=(amodp⋅bmodp)modp
- The second equation is derived from the assumption:
akmodp=(amodp)kmodp
- The third equation is derived by replacing x with amodp
and y with (amodp)k in x⋅(ymodp)modp=(x⋅y)modp
Finally, we can get:
⇒ak+1modp=(amodp)k+1modp
thereby showing that indeed n=k+1 holds.
II. Bottom-up Approach
By (a⋅b)modp=(amodp⋅bmodp)modp,
if we replace b with a, then
(a⋅a)modp=(amodp⋅amodp)modp
⇒a2modp=(amodp)2modp
Next, by replacing b with a2, we can get:
(a⋅a2)modp=(amodp⋅a2modp)modp=(amodp⋅(amodp)2modp)modp
By (x⋅y)modp=x⋅(ymodp)modp,
if we put amodp to x and (amodp)2 to y,
then the above equation will be
a3modp=(a⋅a2)modp=(amodp⋅(amodp)2modp)modp=(amodp⋅(amodp)2)modp=(amodp)3modp
Again and again, you finally can run to n by same technique.
Modular Multiplicative Inverse
the modular multiplicative inverse of an integer a modulo p
is an integer a−1 such that
a⋅a−1≡1(modp)
or
a⋅a−1modp=1
The a−1 exists if and only if a and p are
coprime (i.e., if gcd(a,p)=1).
Theorem. Let a,p∈Z with p>0.
Then a has a multiplicative inverse modulo p
if and only if a and p are relatively prime.
Proof:
First ,we prove that:
if a has an inverse mod p, then a and p must be coprime.
a⋅a−1≡1(modp) implies that
p∣a⋅a−1−1. Thus,
\begin{aligned}
a \cdot a^{-1} - 1 = mp \
\text{, where } m \in Z
\end{aligned}
and it can be rewritten into
\begin{aligned}
a \cdot a^{-1} + np = 1 \
\text{, where } n = -m
\end{aligned}
Then, assume s is the product of all the prime factors that
a and p share, s>0, which is actually the
greatest common divisors of a and p:
s=gcd(a,p)
and
\begin{aligned}
a = s \cdot x \
\text{, where } x \in Z
\end{aligned}
\begin{aligned}
p = s \cdot y \
\text{, where } y \in Z
\end{aligned}
Therefore,
a⋅a−1+n⋅p=(s⋅x)⋅a−1+n⋅(s⋅y)=s⋅(x⋅a−1+n⋅y)=1
and we get:
x⋅a−1+n⋅y=s1
The left side x⋅a−1+n⋅y is the sum of products of integers,
so it must be an integer.
Thus, the right side s1 must also be an integer.
We have only two choices: s=±1.
However, s>0, so s=1.
Finally, we can know that:
If a has an inverse mod p, then a and p must be coprime.
Second, we prove that:
if a and p is coprime, then a has an inverse mod p.
From Bézout's identity:
ax+py=gcd(a,p)=1
Thus,
ax≡1(modp)
The x here is actually the a−1.
References
KHANacademy: What is modular arithmetic