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:

ab(modp) a \equiv b \pmod p

Assumptions for Following Operations

If ab(modp) a \equiv b \pmod p

and

cd(modp) c \equiv d \pmod p

Addition and Subtraction

a±cb±d(modp) a \pm c \equiv b \pm d \pmod p

Proof: We know pabp \mid a-b and pcdp \mid c-d, so

ab=mp 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 a = mp + b

c=np+d c = np + d

Thus,

a±c=(mp+b)±(np+d)=(m±n)p+(b±d)=kp+(b±d), where kZ \begin{aligned} a \pm c =& (mp + b) \pm (np + d) \\ =& (m \pm n)p + (b \pm d) \\ =& kp + (b \pm d) \\ \end{aligned} \text{, where } k \in Z and it's obviously that

a±cb±d(modp) a \pm c \equiv b \pm d \pmod p

Multiplication

acbd(modp) a \cdot c \equiv b \cdot d \pmod p

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}

ac=(mp+b)(np+d)=(mnp+md+nb)p+(bd)=kp+(bd), where kZ \begin{aligned} a \cdot c =& (mp + b) \cdot (np + d) \\ =& (mnp + md + nb) \cdot p + (b \cdot d) \\ =& kp + (b \cdot d) \\ \end{aligned} \text{, where } k \in Z Thus,

acbd(modp) a \cdot c \equiv b \cdot d \pmod p

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 a = mp + b

ra=r(mp+b)=(rm)p+rb=qp+rb \begin{aligned} r \cdot a =& r \cdot (mp + b) \\ =& (rm) \cdot p + r \cdot b \\ =& q \cdot p + r \cdot b \end{aligned}

Exponentiation

\begin{aligned} a^n \equiv b^n \pmod p \ \text{, where } n \in Z \end{aligned}

Proof:

I. Bottom-up Approach

By acbd(modp)a \cdot c \equiv b \cdot d \pmod p , if we replace cc with aa and dd with bb, then

aabb(modp) a \cdot a \equiv b \cdot b \pmod p

a2b2(modp) \Rightarrow a^2 \equiv b^2 \pmod p

Next, by replacing cc with a2a^2 and dd with b2b^2, we can get

aa2bb2(modp) a \cdot a^2 \equiv b \cdot b^2 \pmod p

a3b3(modp) \Rightarrow a^3 \equiv b^3 \pmod p

Finally you can run to anbn(modp)a^n \equiv b^n \pmod p.

II. Mathematical Induction

Formally, we can use mathematical induction to prove it.

1. Basis: the statement is true for n=1n = 1 because ab(modp)a \equiv b \pmod p is true.

2. Inductive step: Show that if n=kn = k holds, then also n=k+1n = k + 1 holds. Assume akbk(modp)a^k \equiv b^k \pmod p is true. By acbd(modp)a \cdot c \equiv b \cdot d \pmod p:

aakbbk(modp) a \cdot a^k \equiv b \cdot b^k \pmod p

ak+1bk+1(modp) \Rightarrow a^{k+1} \equiv b^{k+1} \pmod p

thereby showing that indeed n=k+1n = k + 1 holds.

Modular Operation

Suppose that a,b,pZa, b, p \in Z

Addition and Subtraction

(a±b)modp=(amodp±bmodp)modp (a \pm b) \bmod p = (a \bmod p \pm b \bmod p ) \bmod p

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 \begin{aligned} (a \pm b) \bmod p &= ((m \pm n)p + (i \pm j)) \bmod p \\ &= (i \pm j) \bmod p \\ &= (a \bmod p \pm b \bmod p) \bmod p \end{aligned}

Multiplication

(ab)modp=(amodpbmodp)modp (a \cdot b) \bmod p = (a \bmod p \cdot b \bmod p ) \bmod p

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, (ab)modp=((mp+i)(np+j))modp=((mnp+mj+ni)p+ij)modp=(ij)modp=(amodpbmodp)modp \begin{aligned} (a \cdot b) \bmod p &= ((mp + i) \cdot (np + j)) \bmod p \\ &= ((mnp + mj + ni)p + i \cdot j) \bmod p \\ &= (i \cdot j) \bmod p \\ &= (a \bmod p \cdot b \bmod p) \bmod p \end{aligned}

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: (xy)modp=(x(mp+r))modp=(xmp+xr)modp=xrmodp=x(ymodp)modp, where y=mp+r,m,rZ and 0r<p \begin{aligned} (x \cdot y) \bmod p &= (x \cdot (mp + r)) \bmod p \\ &= (x \cdot mp + x \cdot r) \bmod p \\ &= x \cdot r \bmod p \\ &= x \cdot (y \bmod p) \bmod p \end{aligned} \text{, where } y = mp + r, m, r \in Z \text{ and } 0 \leq 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=1n = 1 because (amodp)modp(a \bmod p) \bmod p still equal to amodpa \bmod p.

2. Inductive step: Show that if n=kn = k holds, then also n=k+1n = k + 1 holds. Assume akmodp=(amodp)kmodpa^k \bmod p = (a \bmod p)^k \bmod p is true, then

(aak)modp=(amodpakmodp)modp=(amodp(amodp)kmodp)modp=(amodp(amodp)k)modp=(amodp)k+1modp \begin{aligned} (a \cdot a^k) \bmod p &= (a \bmod p \cdot a^k \bmod p) \bmod p \\ &= (a \bmod p \cdot (a \bmod p)^k \bmod p) \bmod p \\ &= (a \bmod p \cdot (a \bmod p)^k) \bmod p \\ &= (a \bmod p)^{k+1} \bmod p \end{aligned}

  • The first equation is derived by replacing bb with aka^k in (ab)modp=(amodpbmodp)modp(a \cdot b) \bmod p = (a \bmod p \cdot b \bmod p ) \bmod p
  • The second equation is derived from the assumption: akmodp=(amodp)kmodpa^k \bmod p = (a \bmod p)^k \bmod p
  • The third equation is derived by replacing xx with amodpa \bmod p and yy with (amodp)k(a \bmod p)^k in x(ymodp)modp=(xy)modpx \cdot (y \bmod p) \bmod p = (x \cdot y) \bmod p

Finally, we can get: ak+1modp=(amodp)k+1modp \Rightarrow a^{k+1} \bmod p = (a \bmod p)^{k+1} \bmod p thereby showing that indeed n=k+1n = k + 1 holds.

II. Bottom-up Approach

By (ab)modp=(amodpbmodp)modp(a \cdot b) \bmod p = (a \bmod p \cdot b \bmod p ) \bmod p, if we replace bb with aa, then

(aa)modp=(amodpamodp)modp (a \cdot a) \bmod p = (a \bmod p \cdot a \bmod p ) \bmod p

a2modp=(amodp)2modp \Rightarrow a^2 \bmod p = (a \bmod p)^2 \bmod p

Next, by replacing bb with a2a^2, we can get:

(aa2)modp=(amodpa2modp)modp=(amodp(amodp)2modp)modp \begin{aligned} (a \cdot a^2) \bmod p &= (a \bmod p \cdot a^2 \bmod p ) \bmod p \\ &= (a \bmod p \cdot (a \bmod p)^2 \bmod p ) \bmod p \end{aligned}

By (xy)modp=x(ymodp)modp(x \cdot y) \bmod p = x \cdot (y \bmod p) \bmod p, if we put amodpa \bmod p to xx and (amodp)2(a \bmod p)^2 to yy, then the above equation will be

a3modp=(aa2)modp=(amodp(amodp)2modp)modp=(amodp(amodp)2)modp=(amodp)3modp \begin{aligned} a^3 \bmod p &= (a \cdot a^2) \bmod p \\ &= (a \bmod p \cdot (a \bmod p)^2 \bmod p ) \bmod p \\ &= (a \bmod p \cdot (a \bmod p)^2) \bmod p \\ &= (a \bmod p)^3 \bmod p \end{aligned}

Again and again, you finally can run to nn by same technique.

Modular Multiplicative Inverse

the modular multiplicative inverse of an integer a modulo pp is an integer a1a^{-1} such that

aa11(modp) a \cdot a^{-1} \equiv 1 \pmod p

or

aa1modp=1 a \cdot a^{-1} \bmod p = 1

The a1a^{-1} exists if and only if aa and pp are coprime (i.e., if gcd(a,p)=1gcd(a, p) = 1).

Theorem. Let a,pZa, p \in Z with p>0p > 0. Then aa has a multiplicative inverse modulo pp if and only if aa and pp are relatively prime.

Proof:

First ,we prove that: if aa has an inverse mod pp, then aa and pp must be coprime.

aa11(modp)a \cdot a^{-1} \equiv 1 \pmod p implies that paa11p \mid a \cdot 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 ss is the product of all the prime factors that aa and pp share, s>0s > 0, which is actually the greatest common divisors of aa and pp:

s=gcd(a,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, aa1+np=(sx)a1+n(sy)=s(xa1+ny)=1 \begin{aligned} a \cdot a^{-1} + n \cdot p &= (s \cdot x) \cdot a^{-1} + n \cdot (s \cdot y) \\ &= s \cdot (x \cdot a^{-1} + n \cdot y) \\ &= 1 \end{aligned}

and we get: xa1+ny=1s x \cdot a^{-1} + n \cdot y = \frac{1}{s}

The left side xa1+nyx \cdot a^{-1} + n \cdot y is the sum of products of integers, so it must be an integer. Thus, the right side 1s\frac{1}{s} must also be an integer. We have only two choices: s=±1s = \pm 1. However, s>0s > 0, so s=1s = 1.

Finally, we can know that: If aa has an inverse mod pp, then aa and pp must be coprime.

Second, we prove that: if aa and pp is coprime, then aa has an inverse mod pp.

From Bézout's identity:

ax+py=gcd(a,p)=1 ax + py = gcd(a, p) = 1

Thus,

ax1(modp) ax \equiv 1 \pmod p

The xx here is actually the a1a^{-1}.

References

KHANacademy: What is modular arithmetic

results matching ""

    No results matching ""