🌑

Explore / Study / Mathematics / Number Theory 643 words | 4 minutes

Modular Arithmetic

  1. Congruency
  2. Addition / Subtraction / Multiplication
  3. Division
  4. Euler’s Theorem
  5. Extended Euclidean Algorithm eGCD
  6. An Efficient Modular Exponentiation Algorithm

Congruency

  • Theorem. Let aa be an integer and let NN be a positive integer. Then there exist unique integers q,rq, r for which a=qN+ra = qN+ r and 0r<N0 ≤ r < N.
    • We denote by [amodN][a \bmod N] the remainder obtained when aa is divided by NN. 0[amodN]N10 \le [a \bmod N] \le N - 1.
    • a=bmodN[amodN]=[bmodN]a = b \bmod N \Leftrightarrow [a \bmod N] = [b \bmod N]. We say that aa and bb are congruent modulo N.
    • Congruence mod NN defines an equivalence class on the set of integers.

Addition / Subtraction / Multiplication

  • Congruence modulo NN respects the rules of addition, subtraction and multiplication:

    a=amodNandb=bmodN(a+b)=(a+b)modNandab=abmodN\begin{array}{c} a=a^{\prime} \bmod N and b=b^{\prime} \bmod N \\ \Downarrow \\ (a+b)=\left(a^{\prime}+b^{\prime}\right) \bmod N and a b=a^{\prime} b^{\prime} \bmod N \end{array}

Division

  • Definition. If for a given integer bb, there exists an integer cc such that bc=1modNbc = 1 \bmod N, we say that bb is invertible modulo NN, and call cc a multiplicative inverse of bb modulo NN.
  • In particular, if ab=cbmodNab = cb \bmod N and bb is invertible, multiplying by b1b^{-1} gives a=cmodNa = c \bmod N.

Euler’s Theorem

  • Theorem. Let b,Nb, N be integers, with b1b \ge 1, and N>1N > 1. Then bb is invertible modulo NN if and only if gcd(b,N)=1\gcd(b, N) = 1.
  • Lemma. Let a,ba, b be positive integers. Then there exist integers X,YX, Y such that Xa+Yb=gcd(a,b)Xa + Yb = \gcd(a, b). Furthermore, gcd(a,b)\gcd(a, b) is the smallest positive integer that can be expressed this way.

Extended Euclidean Algorithm eGCD

  • Extended Euclidean Algorithm eGCD:

    • Input: Integers a,ba, b with ab>0a ≥ b > 0.

    • Output: (d,X,Y)(d, X, Y) with d=gcd(a,b)d = \gcd(a, b) and Xa+Yb=dXa + Yb = d.

    • If bb divides aa return (b,0,1)(b,0,1);

    • Else compute integers q,rq, r with a=qb+ra = qb + r and 0<r<b0 < r < b,

      Set (d,X,Y):=eGCD(b,r)(d, X, Y) := \operatorname{eGCD}(b, r) (note that Xb+Yr=dXb+Yr=d),

      Return (d,Y,XYq)(d, Y, X - Yq).

  • The Extended Euclidean algorithm gives [Xmodb][X \bmod b] as the multiplicative inverse of amodba \bmod b, and [Ymoda][Y \bmod a] as the multiplicative inverse of bmodab \bmod a when gcd(a,b)=1\gcd(a, b) = 1.

An Efficient Modular Exponentiation Algorithm

  • To compute [abmodN][a^b \bmod N]:

    exp(a, b, N) {
    	t = 1
    	x = a
    	while (b > 0) {
    		if (b is odd) {
    			t = [t*x mod N]
    			b = b-1
    		}
    		x = [x^2 mod N]
    		b = b/2
    	}
    	return t
    }

    Running Time of this algorithm is polynomial in a,b,N\|a\|,\|b\|,\|N\|.

— Mar 14, 2023

Creative Commons License
Modular Arithmetic by Lu Meng is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at About.