Cr. COMP5521 Chap. 02: Number Theory
童夢綺 Domuki自習スタジオ

Abstract: Lecture 2 in DISTRIBUTED LEDGER TECHNOLOGY, CRYPTOCURRENCY AND E-PAYMENT

Keywords: Distributed Algorithms, Protocols, Blockchains,

Number Theory

Number theory is a branch of pure mathematics that studies the properties and relationships of integers. It is one of the oldest and most abstract areas of mathematics (so no need to be panic when there's something incomprehensible), yet it has significant applications in modern fields like cryptography, computer science, and coding theory.

Alt text for the image

Part 1: Divisibility and GCD

Definition: the ability of being completely divided without any reminder. (整除)

Explain: there are aa apples, which need to be given to bb people. As result, everyone has the same number of apples and no apple left.

Examples: 4 % 2 = 0

  • There is no remainder on division
  • Notation bab | a
  • bb is aa divisor/factor of aa
  • aa is divisible by bb

Division Algorithm

In mathematics, when division is "not exact" or "doesn't divide evenly," it means that in integer division, the division operation does not result in a whole number; instead, there is a non-zero remainder. Well, if the integer cannot be completely divided, and the result seems like this:

d0d=q ... r\frac{d_0}{d} = q \text{ ... } r

Or in another way:

d0=dq+rd_0 = d \cdot q + r
.

Where:

  • d0d_0 is the dividend, the number that is being divided.
  • dd is the divisor, the number by which the dividend is divided.
  • qq is the quotient, which is an integer;
  • rr is the remainder, and it satisfies 0r<b0 \leq r < b;
For example: if a=10a = 10 and b=3b = 3, then: 10=33+110 = 3 \cdot 3 + 1.

If the purpose is to calculate the reminder of aa being divided by bb, then:

d0modd=rd0r(modd)d_0 \mod d = r \newline d_0 \equiv r (\mod d)
.

Greatest Common Divisor

Greatest Common Divisor (GCD): The greatest common divisor of aa and bb, i.e., gcd(a,b)gcd(a, b), is the largest integer that divides both aa and bb.

GCD, in other words, it’s the largest number that both (or all) numbers share as a factor.

There is a case that in real world utilizing GCD:

Imagine you have two pieces of rope, one 12 meters long and the other 18 meters long, and you want to cut them into the largest possible equal lengths. The GCD helps here. For 12 and 18 meters, the GCD is 6 meters, so you can cut both ropes into segments that are 6 meters long.

Euclidean Algorithm

The Euclidean algorithm works by repeatedly applying the relation:

gcd(a,b)=gcd(b,amodb)gcd(a,b) = gcd(b, a \mod b)
.

The process continues until the remainder becomes 0, at which point the GCD is the last non-zero remainder.

In detail:

a=bq+rb=rq1+r1r=r1q2+r2...a = b \cdot q + r \newline b = r \cdot q_1 + r_1 \newline r = r_1 \cdot q_2 + r_2 \newline ...\newline
.

Until meet the 0:

rn2=rn1qn+0r_{n-2} = r_{n-1} \cdot q_n + 0
.

Example: divide 48 by 18 and find the remainder:

48=182+1218=121+612=62+0GCD(48,18)=6\quad 48 = 18 \cdot 2 + 12 \newline \quad 18 = 12 \cdot 1 + 6 \newline \quad 12 = 6 \cdot 2 + 0 \newline \therefore GCD(48, 18) = 6
.

And then, it can be represented in form of ax+by=gcd(a,b)a \cdot x + b \cdot y = gcd(a,b)

Which is: GCD(48,18)=48(1)+183=6GCD(48, 18) = 48 \cdot (−1) + 18 \cdot 3 = 6

In this case, x=1x = -1 and y=3y = 3

Part 2: Modular Arithmetic

The modulus

Definition: If aa is an integer and bb is a positive integer, we define amodba \mod b to be the remainder when aa is divided by bb; the integer bb is called the modulus.

Thus, for any integer a:

a=bq+r,0r<b;q=[ab]a=[ab]b+(amodb)a = b \cdot q + r , 0 \leq r < b ; q = [a|b] \newline a = [a|b] \cdot b + (a \mod b)
.

Congruent modulo

Definition: Two integers aa and bb are said to be congruent modulo nn if (amodn)=(bmodn)(a \mod n) = (b \mod n), which can be written as:

ab(modn)a ≡ b (\mod n)
  • : This symbol represents congruence. It indicates that the two values are equivalent in terms of their remainders when divided by nn.
  • (modn)(\mod n): This notation means "modulo nn." It specifies the modulus, which is the divisor nn.

Note 1: if a0(modn)a ≡ 0 (\mod n), then nan | a.

Note 2: if gcd(a,n)=1gcd(a, n) = 1, axay(modn)a \cdot x ≡ a \cdot y (\mod n), then xy(modn)x ≡ y (\mod n)

Why gcd(a,n)=1gcd(a, n) = 1, axay(modn)a \cdot x ≡ a \cdot y (\mod n), then xy(modn)x ≡ y (\mod n)?

Proof:

axay (modn)axay0 (modn)a (xy)0 (modn)a (xy) is a multiple of na (xy)=qn,where q is a integer.xy=qna,where all numbers are integersxy must be an integer and qna must be an integer as wella and n are coprime,a does not divide nq must be a multiple of a,orqna won’t be an integerlet q new=qa(xy)modn=q newnmodn=0xy0(modn)   xy (modn)\because a \cdot x≡a \cdot y \ ( \mod n) \newline \therefore a \cdot x - a \cdot y≡ 0 \ ( \mod n) \newline \therefore a \ (x-y) ≡ 0 \ ( \mod n) \newline \therefore a \ (x-y) \ \text{is a multiple of n} \newline \therefore a \ (x-y) = q \cdot n , \text{where q is a integer.} \newline \therefore x-y = \frac{q \cdot n}{a} , \text{where all numbers are integers} \newline \therefore x - y \ \text{must be an integer and} \ \frac{q \cdot n}{a} \ \text{must be an integer as well} \newline \because a \text{ and } n \text{ are coprime} , a \text{ does not divide } n \newline \therefore q \text{ must be a multiple of } a , \text{or} \frac{q \cdot n}{a} \ \text{won't be an integer}\newline \quad let \ q_{\ new} = q | a \newline \therefore (x - y) \mod n = q_{\ new} \cdot n \mod n = 0 \newline \therefore x - y ≡ 0 (\mod n)\newline \ \ \ x ≡ y \ ( \mod n)

Modular Inverse

In modular arithmetic, instead of working with division (which can be problematic in modular systems), we use multiplication to "undo" the effect of a number. The modular inverse of aa modulo nn is a number xx such that:

ax1(modn)aa11(modn),a1just for present and a11aat herea \cdot x ≡ 1 (\mod n) \newline a \cdot a^{-1} ≡ 1 (\mod n) , \newline a^{-1} \text{just for present and } a^{-1} \neq \frac{1}{a} \text{at here}

There exists aa modular inverse for aa mod bb if aa is relatively prime to bb

Proof: If gcd(a,b)=1gcd(a,b)=1, then aa has a modular inverse modulo bb

gcd(a,b)=1ax+by=1 (Using Bezout’s Identity)ax+by1 (modb)by is a multiple of bby0 (modb)ax1 (modb)aa11 (modn) \because gcd(a,b)=1 \newline \therefore a \cdot x + b \cdot y = 1 \ \text{(Using Bezout's Identity)} \newline \therefore a \cdot x + b \cdot y ≡ 1 \ (\mod b) \newline \because b \cdot y \ \text{is a multiple of} \ b \newline \therefore b \cdot y ≡ 0 \ (\mod b) \newline \therefore a \cdot x ≡ 1 \ (\mod b) \newline \therefore a \cdot a^{-1} ≡ 1 \ (\mod n)

Arithmetic Modulo 8

Ever unit in this table represents (x+y)mod8(x + y) \mod 8, such as in (1,1)(1,1), (1+1)mod8=2(1+1) \mod 8 = 2.

+01234567
001234567
112345670
223456701
334567012
445670123
556701234
667012345
770123456

Multiplication Modulo 8

Ever unit in this table represents (xy)mod8(x \cdot y) \mod 8, such as in (1,1)(1,1), (11)mod8=1(1 \cdot 1) \mod 8 = 1.

×01234567
000000000
101234567
202460246
303614725
404040404
505274163
606420642
707654321

Additive and Multiplicative Inverse Modulo 8

Col.01234567
ww01234567
w-w07654321
w1w^{-1}-1-3-5-7

Column ww: These are the elements (integers) from 0 to 7, which are taken modulo 8.

Column w-w : These represent the additive inverses modulo 8. For each number ww, its additive inverse is the number that, when added to ww, results in 0 modulo 8. For example: 1+7=80mod81 + 7 = 8 ≡ 0 \mod 8, so the additive inverse of 1 is 7.

Column w1w^{-1} : These represent the multiplicative inverses modulo 8. The multiplicative inverse of a number ww is the number xx such that wx1mod8w \cdot x ≡ 1 \mod 8. For example: The multiplicative inverse of 1 is 1, as 11=1mod8.1 \cdot 1 = 1 \mod 8.

Bezout's Identity

For any two integers aa and bb, there exist integers xx and yy such that:

ax+by=GCD(a,b)a \cdot x + b \cdot y = GCD(a,b)

Example: To calculate the modular inverse of 911 mod 999.

let a=911 and b=999;According to Euclidean algorithm:L1:999=1911+88L2:911=1088+31L3:88=231+26L4:31=126+5L5:26=55+1gcd(a,b)=1Then, Tracing backward:Begin with L5:1=2655Substitute L4 to replace 5:1=265(31126)=531+626Substitute L3 to replace 26:1=531+6(88231)=6881731Substitute L2 to replace 31:1=68817(9111088)=17911+17688Substitute L1 to replace 88:1=17911+176(9991911)=176999193911gcd(911,999)=1=193911+176999Modular reduction: 1(mod999)=193911+176999(mod999)Result: 1806911(mod999)806 is the modular inverse of 911 modulo 999.\text{let} \ a = 911 \ \text{and} \ b = 999 ; \newline \text{According to Euclidean algorithm:} \newline \quad L_1: 999 = 1 \cdot 911 + 88 \newline \quad L_2: 911 = 10 \cdot 88 + 31 \newline \quad L_3: 88 = 2 \cdot 31 + 26 \newline \quad L_4: 31 = 1 \cdot 26 + 5 \newline \quad L_5: 26 = 5 \cdot 5 + 1 \newline \therefore gcd(a, b) = 1 \newline \text{Then, Tracing backward:} \newline \text{Begin with } L_5: 1 = 26 – 5 \cdot 5 \newline \text{Substitute } L_4 \text{ to replace 5:} \newline \quad 1 = 26 – 5 \cdot (31 – 1 \cdot 26) = -5 \cdot 31 + 6 \cdot 26 \newline \text{Substitute } L_3 \text{ to replace 26:} \newline \quad 1= -5 \cdot 31 + 6 \cdot (88 – 2 \cdot 31) = 6 \cdot 88 – 17 \cdot 31 \newline \text{Substitute } L_2 \text{ to replace 31:} \newline \quad 1= 6 \cdot 88 – 17 \cdot (911 – 10 \cdot 88) = -17 \cdot 911 + 176 \cdot 88 \newline \text{Substitute } L_1 \text{ to replace 88:} \newline \quad 1= -17 \cdot 911 + 176 \cdot (999 – 1 \cdot 911) = 176 \cdot 999 – 193 \cdot 911 \newline \therefore gcd(911, 999) = 1 = -193 \cdot 911 + 176 \cdot 999 \newline \text{Modular reduction: }1 (\mod 999) = -193 \cdot 911 + 176 \cdot 999 (\mod 999) \newline \text{Result: } 1 ≡ 806 \cdot 911 (\mod 999) \newline \therefore 806 \text{ is the modular inverse of } 911 \text{ modulo } 999.

Part 3: Prime Number, Fermat’s Theorem and Euler’s Theorem

Prime Number

Prime numbers only have divisors of 1 and itself.

Composite number has at least one divisor other than 1 and itself

The fundamental theorem of arithmetic: Any integer a,a>1a , a > 1 can be factored in a unique way as: a=p1a1p2a2...ptata = p_1^{a_1} \cdot p_2^{a_2} \cdot \text{...} \cdot p_t^{a_t} where p1<p2<...<ptp_1 < p_2 < \text{...} < p_t are prime numbers and where each aia_i is a positive integer. For example: 24=2223=233124 = 2 \cdot 2 \cdot 2 \cdot 3 = 2^{3} \cdot 3^{1}

Fermat's Theorem

If pp is prime and aa is a positive integer not divisible by pp then:

ap11(modp) Fermat’s Little Theorema^{p-1} ≡ 1 (\mod p) \ \text{Fermat's Little Theorem}

Here's the proof:

Known: ap,a>0;(p1)<p(any numbermodp){1,2,3,...,(p1)}a{1,2,3,...,(p1)}{1,2,3,...,(p1)} (modp){a,2a,3a,...,(p1)a}{1,2,3,...,(p1)} (modp)a2a3a...(p1)amodp=123...(p1)ap1(p1)!modp=(p1)!ap1(p1)!(p1)! (modp)ap11(modp)\text{Known: } a \nmid p , a > 0 ; \newline \because (p-1) < p \newline \therefore ( \text{any number} \mod p) \in \{1, 2, 3, ..., (p-1)\}\newline \therefore a \cdot \{1, 2, 3, ..., (p-1)\} ≡ \{1, 2, 3, ..., (p-1)\} \ (\mod p) \newline \quad \{a, 2 \cdot a, 3 \cdot a, ..., (p-1) \cdot a\} ≡ \{1, 2, 3, ..., (p-1)\} \ (\mod p) \newline \therefore a \cdot 2 \cdot a \cdot 3 \cdot a \cdot ... \cdot (p-1) \cdot a \mod p = 1 \cdot 2 \cdot 3 \cdot ... \cdot (p-1) \newline \quad a^{p-1}(p-1)! \mod p = (p-1)!\newline \quad a^{p-1}(p-1)! ≡ (p-1)! \ (\mod p)\newline \therefore a^{p-1} ≡ 1 (\mod p)\newline

But, why a{1,2,3,...,(p1)}{1,2,3,...,(p1)} (modp)a \cdot \{1, 2, 3, ..., (p-1)\} ≡ \{1, 2, 3, ..., (p-1)\} \ (\mod p) can be transform to a2a3a...(p1)a123...(p1)(modp)?a \cdot 2 \cdot a \cdot 3 \cdot a \cdot ... \cdot (p-1) \cdot a ≡ 1 \cdot 2 \cdot 3 \cdot ... \cdot (p-1) (\mod p)?

In Permutation Property in Fermat's Little Theorem:

  1. {a,2a,3a,...,(p1)a}\{a, 2 \cdot a, 3 \cdot a, ..., (p-1) \cdot a\} is simply a rearranged (permuted) version of {1,2,3,...,(p1)}\{1, 2, 3, ..., (p-1)\}
  2. The product of elements remains the same: a2a3a...(p1)a123...(p1)(modp)a \cdot 2 \cdot a \cdot 3 \cdot a \cdot ... \cdot (p-1) \cdot a ≡ 1 \cdot 2 \cdot 3 \cdot ... \cdot (p-1) (\mod p)

And here's an example for better understanding:

Assume that p=3p = 3, because aba \nmid b, reminders shouldn't be 0;

×012
0000
1012
2021
3000
4 (%3 = 1)012
5 (%3 = 2)021
6 (%3 = 0)000
7 (%3 = 1)012
8 (%3 = 2)021
9 (%3 = 0)000
a01 or 21 or 2

Therefore, according to the regulation:

If a=1a = 1, 1×11×212 (mod3)1 \times 1 \cdot 1 \times 2 ≡ 1 \cdot 2 \ (\mod 3)

If a=2a = 2, 2×12×212 (mod3)2 \times 1 \cdot 2 \times 2 ≡ 1 \cdot 2 \ (\mod 3)

If a=4a = 4, 4×14×212 (mod3)4 \times 1 \cdot 4 \times 2 ≡ 1 \cdot 2 \ (\mod 3)

Therefore, a×1a×2a×3...a×(p1)123...(p1)(modp)a \times 1 \cdot a \times 2 \cdot a \times 3 \cdot ... \cdot a \times (p-1) ≡ 1 \cdot 2 \cdot 3 \cdot ... \cdot (p-1) (\mod p)

Euler’s Totient Function

For a positive integer nn, Euler's Totient Function ϕ(n)\phi(n) is defined as the number of integers from 1 to nn that are coprime to nn. Two integers are coprime if their greatest common divisor (gcd) is 1.

Basic Property:

ϕ(n)={k1kn,gcd(k,n)=1}\phi(n) = \left| \{ k \mid 1 \leq k \leq n, gcd(k, n) = 1 \} \right|

For a Prime Numberpp: If pp is a prime number, then every number from 1 to p1p - 1 is coprime with pp. Therefore:

ϕ(p)=p1\phi(p) = p - 1

For p=7p = 7, a prime number:ϕ(7)=71=6\phi(7) = 7 - 1 = 6

The numbers from 1 to 6 (i.e., 1, 2, 3, 4, 5, 6) are all coprime with 7.

It is used in Euler's Theorem, which generalizes Fermat's Little Theorem. For any integer aa and nn where aa is coprime to nn:

ap11(modp) aϕ(p)1(modp) Fermat’s Little Theorema^{p-1} ≡ 1 (\mod p) \ \newline a^{\phi(p)} ≡ 1 (\mod p) \ \text{Fermat's Little Theorem}

General Formula:

The general formula for Euler's Totient Function ϕ(n)\phi(n), where nn is expressed as a product of distinct prime factors, is given by:

ϕ(n)=n(11p1)(11p2)(11pm)\phi(n) = n \left(1 - \frac{1}{p_1}\right) \left(1 - \frac{1}{p_2}\right) \cdots \left(1 - \frac{1}{p_m}\right)

where nn can be expressed as:

n=p1k1p2k2pmkmn = p_1^{k_1} \cdot p_2^{k_2} \cdots p_m^{k_m}

and p1,p2,,pmp_1, p_2, \ldots, p_m are distinct prime numbers.

For n=30n = 30, Factorize 30=23530 = 2 \cdot 3 \cdot 5

Therefore, ϕ(30)=30(112)(113)(115)=8\phi(30) = 30 \left(1 - \frac{1}{2}\right) \left(1 - \frac{1}{3}\right) \left(1 - \frac{1}{5}\right) = 8

Thus, ϕ(30)=8\phi(30) = 8, meaning there are 8 numbers from 1 to 30 that are coprime with 30.

If n=pkn = p^{k} (pp is prime), then ϕ(n)=pkpk1\phi(n) = p^{k} - p^{k-1}

Proof:

ϕ(n)=n(11p1)(11p2)(11pm)\phi(n) = n \left(1 - \frac{1}{p_1}\right) \left(1 - \frac{1}{p_2}\right) \cdots \left(1 - \frac{1}{p_m}\right)\newline

If n=pkn = p^{k} (pp is prime), there is only one prime factor pp with exponent kk. Thus:

ϕ(pk)=pk(11p)ϕ(pk)=pkp1pϕ(pk)=pk(p1)p=pk1(p1)\phi(p^{k}) = p^{k} \left(1 - \frac{1}{p}\right) \newline \phi(p^{k}) = p^{k} \cdot \frac{p-1}{p} \newline \phi(p^{k}) = \frac{p^{k} \cdot (p-1)}{p} = p^{k-1} \cdot (p-1) \newline

Simplify this further:

ϕ(n)=pkpk1\phi(n) = p^{k} - p^{k-1}

Part 4: Efficient Modulo Operation for Large Numbers

Chinese Remainder Theorem

The Chinese Remainder Theorem (CRT) is a fundamental result in number theory that provides a method for solving systems of simultaneous congruences with pairwise coprime moduli. It ensures that there is a unique solution to such systems, and this solution is congruent modulo the product of the moduli.

Theorem

Let n1,n2,,nkn_1, n_2, \ldots, n_k be pairwise coprime positive integers, and let a1,a2,,aka_1, a_2, \ldots, a_k be integers. Then there exists a unique integer xx modulo N=n1n2nkN = n_1 \cdot n_2 \cdots n_k such that:

xai(modni)for i=1,2,,kx \equiv a_i \pmod{n_i} \quad \text{for } i = 1, 2, \ldots, k
  1. Define the Problem:

    We want to find an integer xx that satisfies:

    xai(modni)x \equiv a_i \pmod{n_i}

    for each ii, where nin_i are pairwise coprime.

  2. Construct the Solution:

    Let N=n1n2nkN = n_1 \cdot n_2 \cdots n_k. For each ii:

    • Define Ni=NniN_i = \frac{N}{n_i}.
    • Find MiM_i such that NiMi1(modni)N_i \cdot M_i \equiv 1 \pmod{n_i}. This can be done using the Extended Euclidean Algorithm to find the modular inverse of NiN_i modulo nin_i.
  3. Formulate the Solution:

    The solution can be expressed as:

    x=i=1kaiNiMix = \sum_{i=1}^k a_i \cdot N_i \cdot M_i
  4. Verify the Solution:

    Check that xx satisfies all congruences:

    xai(modni)x \equiv a_i \pmod{n_i}

    for all ii.

  5. Uniqueness:

    The solution xx is unique modulo NN because the moduli are pairwise coprime, ensuring that the solution within the range 00 to N1N-1 is unique.

Consider the following system of congruences:

{x2(mod3)x3(mod4)x2(mod5)\begin{cases} x \equiv 2 \pmod{3} \newline x \equiv 3 \pmod{4} \newline x \equiv 2 \pmod{5} \newline \end{cases}

Calculate NN:N=345=60N = 3 \cdot 4 \cdot 5 = 60

Compute NiN_i and MiM_i:

For n1=3n_1 = 3: N1=603=20N_1 = \frac{60}{3} = 20

Find M1M_1 such that 20M11(mod3)20 \cdot M_1 \equiv 1 \pmod{3}.

Here, M1=2M_1 = 2 (since 202401(mod3)20 \cdot 2 \equiv 40 \equiv 1 \pmod{3}).

For n2=4n_2 = 4: N2=604=15N_2 = \frac{60}{4} = 15

Find M2M_2 such that 15M21(mod4)15 \cdot M_2 \equiv 1 \pmod{4}.

Here, M2=3M_2 = 3 (since 153451(mod4)15 \cdot 3 \equiv 45 \equiv 1 \pmod{4}).

For n3=5n_3 = 5: N3=605=12N_3 = \frac{60}{5} = 12

Find M3M_3 such that 12M31(mod5)12 \cdot M_3 \equiv 1 \pmod{5}.

Here, M3=3M_3 = 3 (since 123361(mod5)12 \cdot 3 \equiv 36 \equiv 1 \pmod{5}).

Calculate xx:

x=2202+3153+2123=80+135+72=287x = 2 \cdot 20 \cdot 2 + 3 \cdot 15 \cdot 3 + 2 \cdot 12 \cdot 3 = 80 + 135 + 72 = 287

Reduce xx modulo 6060:

x287(mod60)47x \equiv 287 \pmod{60} \equiv 47

Thus, x=47x = 47 is a solution to the system of congruences.