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 0≤r<N0 ≤ r < N.
    • We denote by [a mod N][a \bmod N] the remainder obtained when aa is divided by NN. 0≤[a mod N]≤N−10 \le [a \bmod N] \le N - 1.
    • a=b mod N⇔[a mod N]=[b mod N]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=a′ mod Nandb=b′ mod N⇓(a+b)=(a′+b′) mod Nandab=a′b′ mod N\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=1 mod Nbc = 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=cb mod Nab = cb \bmod N and bb is invertible, multiplying by b−1b^{-1} gives a=c mod Na = c \bmod N.

Euler's Theorem

  • Theorem. Let b,Nb, N be integers, with b≥1b \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 a≥b>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,X−Yq)(d, Y, X - Yq).

  • The Extended Euclidean algorithm gives [X mod b][X \bmod b] as the multiplicative inverse of a mod ba \bmod b, and [Y mod a][Y \bmod a] as the multiplicative inverse of b mod ab \bmod a when gcd⁡(a,b)=1\gcd(a, b) = 1.

An Efficient Modular Exponentiation Algorithm

  • To compute [ab mod N][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.