Math behind key pairs

Number theory, key-pair algorithms, symmetric crypto, and hashes, built up from scratch.

Modular Arithmetic Foundations

Modular Arithmetic

The modulus m defines the wrap-around space. Every integer maps to one output from 0 to m-1.
These outputs are called residues.

MOD

mod 3 has exactly 3 possible residues

Residues set

mod 3 { 0, 1, 2 }

Modulus space

Integers space

Congruence check (mod 3)

A

=/=

B

A and B are not congruent modulo 3, because they map to different residues {0} and {2}.

In the modulus space, they are different numbers.

When divided by modulus 3, they have different remainders {0} and {2}.

Modular Operations

Modular Arithmetic Examples

Calculation examples

Click on an example to expand it and see the steps.

Divisibility rules

Decimal decomposition of an integer

Any number in decimal system can be represented as a sum of powers of 10.

General form

N=anan1a1a0N = \overline{a_n a_{n-1}\ldots a_1 a_0}
N=a0+10anan1a1N = a_0 + 10\cdot \overline{a_n a_{n-1}\ldots a_1}
N=a0+a110+102anan1a2N = a_0 + a_1\cdot 10 + 10^2\cdot \overline{a_n a_{n-1}\ldots a_2}
N=a0+a110+a2102++an10nN = a_0 + a_1\cdot 10 + a_2\cdot 10^2 + \cdots + a_n\cdot 10^n

N

1234=12341234 = \overline{1234}
1234=4+101231234 = 4 + 10\cdot \overline{123}
1234=4+310+102121234 = 4 + 3\cdot 10 + 10^2\cdot \overline{12}
1234=4+310+2102+10311234 = 4 + 3\cdot 10 + 2\cdot 10^2 + 10^3\cdot \overline{1}
1234=4+310+2102+11031234 = 4 + 3\cdot 10 + 2\cdot 10^2 + 1\cdot 10^3
1234=4+30+200+10001234 = 4 + 30 + 200 + 1000

Rule for division by: none

Click a value on the line to see its divisibility rule.

Primes

Numbers decomposition

Prime numbers - the building blocks that combine to build all other numbers.

3
3
·
5
·
11

= 495

Primes lie rather unpredictably on the number line.

Every integer greater than 1 can be broken down as a product of primes. This prime set is unique for each number, like a fingerprint.

6 =

2
·
3

Greatest common divisor

Divisibility

Intuition: "3 divides 12" means 12 = 3 * 4, or easier to think 12 is a multiple of 3.

Divisibility Definition

Let a,bZ. We say ab if there exists cZ such that ac=b.\text{Let } a,b \in \mathbb{Z}.\ \text{We say } a \mid b \text{ if there exists } c \in \mathbb{Z} \text{ such that } ac = b.
Notation: \text{Notation: }
ab - "a divides b" - a statementa \mid b \text{ - "a divides b" - a statement}
ab - "a divided by b" - a number\frac{a}{b} \text{ - "a divided by b" - a number}

Number a divides b if you can build b using all blocks from a.
For example, 12 divides 60:

60 =

12
2
·
2
·
3
·
5

GCD Definition

Let a,bZ . Then gdc of a and b, denoted gcd(a,b) is the largest integer g such that ga and gb.\text{Let } a,b \in \mathbb{Z}\ \text{. Then gdc of a and b, denoted } \gcd(a,b) \text{ is the largest integer } g \text{ such that } g \mid a \text{ and } g \mid b.
a=p1α1p2α2pnαn,a = p_1^{\alpha_1} p_2^{\alpha_2} \cdots p_n^{\alpha_n},
b=p1β1p2β2pnβn,b = p_1^{\beta_1} p_2^{\beta_2} \cdots p_n^{\beta_n},
gcd(a,b)=p1min(α1,β1)p2min(α2,β2)pnmin(αn,βn)\gcd(a,b) = p_1^{\min(\alpha_1,\beta_1)} p_2^{\min(\alpha_2,\beta_2)} \cdots p_n^{\min(\alpha_n,\beta_n)}

A

B

12 =
18 =
2
·
2
·
3
2
·
3
·
3
GCD(12, 18) =
2
·
3
=6

LCM Definition

Let a,bZ . Then the lcm of a and b, denoted lcm(a,b), is the smallest positive integer m such that am and bm.\text{Let } a,b \in \mathbb{Z}\ \text{. Then the lcm of } a \text{ and } b, \text{ denoted } \operatorname{lcm}(a,b), \text{ is the smallest positive integer } m \text{ such that } a \mid m \text{ and } b \mid m.
a=p1α1p2α2pnαn,a = p_1^{\alpha_1} p_2^{\alpha_2} \cdots p_n^{\alpha_n},
b=p1β1p2β2pnβn,b = p_1^{\beta_1} p_2^{\beta_2} \cdots p_n^{\beta_n},
lcm(a,b)=p1max(α1,β1)p2max(α2,β2)pnmax(αn,βn)\operatorname{lcm}(a,b) = p_1^{\max(\alpha_1,\beta_1)} p_2^{\max(\alpha_2,\beta_2)} \cdots p_n^{\max(\alpha_n,\beta_n)}

LCM includes all blocks from a, and all blocks from b, repeating blocks can be shared between a and b.

A

B

12 =
18 =
2
·
2
·
3
2
·
3
·
3
LCM(12, 18) =
2
·
2
·
3
·
3
=36

Euclidean algorithm

Find gcd(A, B) by playing with piles of pebbles

Having two piles of pebbles, what happens if you remove smaller pile from the larger one, and repeat until piles are equal?
Turns out, the final pile size is the greatest common divisor of the original piles.

A

B

Proof for the Euclidean algorithm

Proposition. Let a,bZ>0 with ab. Then gcd(a,b)=gcd(a,ba).\text{Proposition. Let } a,b \in \mathbb{Z}_{>0} \text{ with } a \le b. \text{ Then } \gcd(a,b) = \gcd(a,b-a).
Suppose g=gcd(a,b).\text{Suppose } g = \gcd(a,b).
ga and gb.g \mid a \text{ and } g \mid b.
So there exist α,βZ such that a=αg, b=βg.\text{So there exist } \alpha,\beta \in \mathbb{Z} \text{ such that } a = \alpha g,\ b = \beta g.
ba=βgαg=g(βα),b-a = \beta g - \alpha g = g(\beta-\alpha),
hence g(ba).\text{hence } g \mid (b-a).
Therefore g is a common divisor of a and ba, so ggcd(a,ba).\text{Therefore } g \text{ is a common divisor of } a \text{ and } b-a, \text{ so } g \le \gcd(a,b-a).
Conversely, suppose h=gcd(a,ba).\text{Conversely, suppose } h = \gcd(a,b-a).
ha and h(ba).h \mid a \text{ and } h \mid (b-a).
So there exist α,βZ such that a=αh, ba=βh.\text{So there exist } \alpha,\beta \in \mathbb{Z} \text{ such that } a = \alpha h,\ b-a = \beta h.
Trick: rewrite b as (ba)+a.\text{Trick: rewrite } b \text{ as } (b-a)+a.
b=(ba)+a=βh+αh=h(β+α),b = (b-a)+a = \beta h + \alpha h = h(\beta+\alpha),
hence hb.\text{hence } h \mid b.
Therefore h is a common divisor of a and b, so hgcd(a,b).\text{Therefore } h \text{ is a common divisor of } a \text{ and } b, \text{ so } h \le \gcd(a,b).
gcd(a,b)gcd(a,ba)andgcd(a,ba)gcd(a,b).\gcd(a,b) \le \gcd(a,b-a) \quad \text{and} \quad \gcd(a,b-a) \le \gcd(a,b).
Both and  are true only when the two numbers are equal.\text{Both} \le \text{ and } \ge \text{ are true only when the two numbers are equal.}
Hence gcd(a,b)=gcd(a,ba).\text{Hence } \gcd(a,b) = \gcd(a,b-a).
Base case: gcd(n,0)=n.\text{Base case: } \gcd(n,0) = n.
Reason: nn and n0 since 0=n0.\text{Reason: } n \mid n \text{ and } n \mid 0 \text{ since } 0 = n \cdot 0.

Faster Euclidean algorithm

Subtraction removes one copy of the smaller number at a time. Division removes all full copies at once, reducing the number of steps.

If b=qa+r with 0r<a, then gcd(a,b)=gcd(a,r).\text{If } b = qa + r \text{ with } 0 \le r < a,\ \text{then } \gcd(a,b) = \gcd(a,r).

Calculate gcd(12, 18):

1. 18 = 1 * 12 + 6, so gcd(12, 18) = gcd(12, 6). Quotient 1 means removing 1 full copy of 12 in one jump.

2. 12 = 2 * 6 + 0, so gcd(12, 6) = gcd(6, 0). Quotient 2 means removing 2 full copies of 6 in one jump.

3. The last non-zero remainder is 6, so gcd(12, 18) = 6.

Extended Euclidean algorithm

Enhancing Euclidean algorithm to solve more problems

Add bookkeeping - track how each remainder is build from the original inputs, that is keep a "recipe chain" for a remainder r in terms of ingredients a and b. This tiny extra piece of information upgrades an algorithm for gcd into a tool for linear Diophantine equations, multiplicative inverses, and the Chinese remainder theorem.

Pattern aⁿ-n (mod n)

Fermat's little theorem

Cancellation in modular arithmetic

Let's start with acbc(modm)ac \equiv bc \pmod{m}. It is tempting to cancel the shared factor and jump to ab(modm)a \equiv b \pmod{m}, but is this always valid?

A counterexample is 2326(mod6)2\cdot 3 \equiv 2\cdot 6 \pmod{6}, where 3≢6(mod6)3 \not\equiv 6 \pmod{6}.

In what cases we can be sure that cancellation is valid?

acbc(modm)acbc0(modm)ac \equiv bc \pmod{m} \Rightarrow ac-bc \equiv 0 \pmod{m}
c(ab)0(modm), here either c0 or ab0 or a combination of factors from both ab and c\Rightarrow c(a-b) \equiv 0 \pmod{m}\text{, here either }c \equiv 0 \text{ or } a-b\equiv 0\text{ or a combination of factors from both } a-b \text{ and } c

To conclude ab0(modm)a-b \equiv 0 \pmod{m}, we need to be sure that cc is not having common factors with mm. That is, gcd(c,m)=1\gcd(c,m)=1.

{acbc(modm)gcd(c,m)=1    ab(modm)\left\{\begin{array}{l}ac \equiv bc \pmod{m}\\ \gcd(c,m)=1\end{array}\right.\;\Longrightarrow\; a \equiv b \pmod{m}

Pattern aⁿ-n (mod n)

The section above demonstrates that for some numbers, such as m=3,5,7,37m=3,5,7,37, the quantity am11a^{m-1}-1 is always divisible by mm. For other numbers, such as m=9m=9, the pattern does not hold.

Fermat's little theorem proves that for prime moduli pp, the quantity ap11a^{p-1}-1 (or, in the same pattern form, apaa^p-a) is always divisible by pp, provided pap \nmid a.

Proof for Fermat's little theorem

If p is prime and pa, then ap11(modp).\text{If } p \text{ is prime and } p \nmid a,\ \text{then } a^{p-1} \equiv 1 \pmod{p}.

Residues

Possible non-zero residues modulo pp are {1,2,,p1}\{1,2,\dots,p-1\}.

r(modp)r \pmod{p}ar(modp)a \cdot r \pmod{p}r1,r2,,rp1(modp)\equiv r_1, r_2, \dots, r_{p-1} \pmod{p}
11aar1\equiv r_1
222a2ar2\equiv r_2
333a3ar3\equiv r_3
\vdots\vdots\vdots
p1p-1(p1)a(p-1)arp1\equiv r_{p-1}

Setup

The non-zero residues modulo p are 1,2,,p1.\text{The non-zero residues modulo } p \text{ are } 1,2,\dots,p-1.
Multiply each of them by a. Let rkka(modp) for k=1,2,,p1.\text{Multiply each of them by } a. \text{ Let } r_k \equiv ka \pmod{p} \text{ for } k=1,2,\dots,p-1.

Distinctness

Suppose two of the new residues are equal: rlrk(modp) with lk.\text{Suppose two of the new residues are equal: } r_l \equiv r_k \pmod{p} \text{ with } l \ne k.
Then laka(modp).\text{Then } la \equiv ka \pmod{p}.
Since pa, gcd(a,p)=1, we can cancel a and obtain lk(modp).\text{Since } p \nmid a,\ \gcd(a,p)=1,\ \text{we can cancel } a \text{ and obtain } l \equiv k \pmod{p}.
But l,k{1,2,,p1}, so this implies l=k, a contradiction.\text{But } l,k \in \{1,2,\dots,p-1\},\ \text{so this implies } l=k, \text{ a contradiction.}
Therefore r1,r2,,rp1 are all distinct.\text{Therefore } r_1,r_2,\dots,r_{p-1} \text{ are all distinct.}

Non-zero

None of them is 0(modp).\text{None of them is } 0 \pmod{p}.
If rkka0(modp), then pka.\text{If } r_k \equiv ka \equiv 0 \pmod{p},\ \text{then } p \mid ka.
This is impossible because pa and pk for 1kp1.\text{This is impossible because } p \nmid a \text{ and } p \nmid k \text{ for } 1 \le k \le p-1.

Rearrangement

Thus r1,r2,,rp1 are exactly the numbers 1,2,,p1 in some order modulo p.\text{Thus } r_1,r_2,\dots,r_{p-1} \text{ are exactly the numbers } 1,2,\dots,p-1 \text{ in some order modulo } p.

Permutation Intuition

Each arrow shows r -> a*r (mod p). Multiplying every residue by a rearranges the circle.

prime p

multiplier a

Every arrow lands on a different non-zero residue. Changing a rewires the same circle into a new permutation.

Effective Multiplier

3 (since 3 mod 7 = 3)

Behavior

Permutes the non-zero residues

Cycles

(1 3 2 6 4 5)

123456

Multiplying by a permutes the non-zero residues modulo p.

Product

Multiply the congruences kark(modp) for k=1,2,,p1.\text{Multiply the congruences } ka \equiv r_k \pmod{p} \text{ for } k=1,2,\dots,p-1.
The left side becomes (1a)(2a)((p1)a)=ap1(12(p1)).\text{The left side becomes } (1a)(2a)\cdots((p-1)a) = a^{p-1}(1\cdot2\cdots(p-1)).
So ap1(12(p1))r1r2rp1(modp).\text{So } a^{p-1}(1\cdot2\cdots(p-1)) \equiv r_1 r_2 \cdots r_{p-1} \pmod{p}.
Because r1,r2,,rp1 is the same residue set as 1,2,,p1, when multiplied together it is the same as r1r2rp112(p1)(modp).\text{Because } r_1,r_2,\dots,r_{p-1} \text{ is the same residue set as } 1,2,\dots,p-1,\ \text{when multiplied together it is the same as } r_1 r_2 \cdots r_{p-1} \equiv 1\cdot2\cdots(p-1) \pmod{p}.
Rewriting 12(p1) as factorial(p1)!, we get ap1(p1)!(p1)!(modp).\text{Rewriting } 1\cdot2\cdots(p-1) \text{ as factorial} (p-1)!,\ \text{we get } a^{p-1}(p-1)! \equiv (p-1)! \pmod{p}.
ap1(p1)!(p1)!(modp)a^{p-1}(p-1)! \equiv (p-1)! \pmod{p}
gcd((p1)!,p)=1 because p is prime and none of 1,2,,p1 is divisible by p, so we can cancel (p1)! from both sides\gcd((p-1)!,p)=1 \text{ because } p \text{ is prime and none of } 1,2,\dots,p-1 \text{ is divisible by } p \text{, so we can cancel } (p-1)! \text{ from both sides}
ap11(modp)\therefore a^{p-1} \equiv 1 \pmod{p}
Multiplying both sides by a gives another form: apa0(modp) or apa(modp)\text{Multiplying both sides by } a \text{ gives another form: } a^p-a \equiv 0 \pmod{p} \text{ or } a^p \equiv a \pmod{p}

Euler's theorem

Generalizing Fermat's little theorem

Euler extended the idea from the Fermat's little theorem from primes (mod p) to modulus (mod n), using the totient function φ(n)\varphi(n).

The theorem says that if gcd(a,n)=1\gcd(a,n)=1, then powers of aa cycle in a way controlled by Euler's totient function φ(n)\varphi(n):

aφ(n)1(modn)a^{\varphi(n)} \equiv 1 \pmod{n}

Totient Function

Totient function φ(n)\varphi(n) counts the numbers from 11 up to n1n-1 that are coprime to nn.

n

Prime Factors of n

Totient

Totient Filter

Integers 1 to n-1

φ1

1

1

2

2

3

3

4

4

φ2

5

5

6

6

φ3

7

7

8

8

9

9

10

10

φ4

11

11

Euler Product

Each distinct prime divisor removes a fraction of the residue classes, leaving exactly φ(n)\varphi(n) survivors.

These surviving residues are the ones Euler's theorem multiplies and permutes, just like Fermat permutes the full non-zero set when the modulus is prime.

φ(12)=12\varphi(12) = 12(112)\left(1-\frac{1}{\textcolor{deeppink}{2}}\right)(113)\left(1-\frac{1}{\textcolor{firebrick}{3}}\right)=4= 4

Proof for Euler's theorem

If gcd(a,n)=1, then aφ(n)1(modn).\text{If } \gcd(a,n)=1,\ \text{then } a^{\varphi(n)} \equiv 1 \pmod{n}.

Possible residues

The possible residues set is the reduced residue system modulo nn:

{α1,α2,,αφ(n)}, where gcd(αi,n)=1 for every i\{\alpha_1,\alpha_2,\dots,\alpha_{\varphi(n)}\},\ \text{where } \gcd(\alpha_i,n)=1 \text{ for every } i
αi\alpha_iaαia \cdot \alpha_iri(modn)\equiv r_i \pmod{n}
α1\alpha_1aα1a\alpha_1r1\equiv r_1
α2\alpha_2aα2a\alpha_2r2\equiv r_2
α3\alpha_3aα3a\alpha_3r3\equiv r_3
\vdots\vdots\vdots
αφ(n)\alpha_{\varphi(n)}aαφ(n)a\alpha_{\varphi(n)}rφ(n)\equiv r_{\varphi(n)}

Setup

Let {α1,α2,,αφ(n)} be the reduced residue system modulo n.\text{Let } \{\alpha_1,\alpha_2,\dots,\alpha_{\varphi(n)}\} \text{ be the reduced residue system modulo } n.
Each αi satisfies 1αi<n and gcd(αi,n)=1.\text{Each } \alpha_i \text{ satisfies } 1 \le \alpha_i < n \text{ and } \gcd(\alpha_i,n)=1.
Multiply each of them by a. Let riaαi(modn) for i=1,2,,φ(n).\text{Multiply each of them by } a. \text{ Let } r_i \equiv a\alpha_i \pmod{n} \text{ for } i=1,2,\dots,\varphi(n).

Distinctness

Suppose two new residues are equal: rirj(modn) with ij.\text{Suppose two new residues are equal: } r_i \equiv r_j \pmod{n} \text{ with } i \ne j.
Then aαiaαj(modn).\text{Then } a\alpha_i \equiv a\alpha_j \pmod{n}.
Since gcd(a,n)=1, we may cancel a and obtain αiαj(modn).\text{Since } \gcd(a,n)=1,\ \text{we may cancel } a \text{ and obtain } \alpha_i \equiv \alpha_j \pmod{n}.
But α1,α2,,αφ(n) are distinct residues modulo n, so this is impossible.\text{But } \alpha_1,\alpha_2,\dots,\alpha_{\varphi(n)} \text{ are distinct residues modulo } n, \text{ so this is impossible.}
Therefore r1,r2,,rφ(n) are all different.\text{Therefore } r_1,r_2,\dots,r_{\varphi(n)} \text{ are all different.}

Still Coprime

Each ri is also coprime to n.\text{Each } r_i \text{ is also coprime to } n.
gcd(ri,n)=gcd(aαi,n)=1 because gcd(a,n)=1 and gcd(αi,n)=1.\gcd(r_i,n)=\gcd(a\alpha_i,n)=1 \text{ because } \gcd(a,n)=1 \text{ and } \gcd(\alpha_i,n)=1.
So every ri is another residue from the same reduced residue system modulo n.\text{So every } r_i \text{ is another residue from the same reduced residue system modulo } n.

Rearrangement

There are φ(n) of these residues, they are all different, and each is coprime to n.\text{There are } \varphi(n) \text{ of these residues, they are all different, and each is coprime to } n.
Hence r1,r2,,rφ(n) are exactly α1,α2,,αφ(n) in some order modulo n.\text{Hence } r_1,r_2,\dots,r_{\varphi(n)} \text{ are exactly } \alpha_1,\alpha_2,\dots,\alpha_{\varphi(n)} \text{ in some order modulo } n.

Permutation Intuition

Each arrow shows r -> a*r (mod n), but only on residues coprime to n. Grey nodes are filtered out by Euler's totient.

modulus n

multiplier a

Effective Multiplier

5 (since 5 mod 12 = 5)

Behavior

Permutes the reduced residues

Reduced Set

4 residues, cycles (1 5) (7 11)

1234567891011

Multiplying by a permutes only the residues coprime to n; the grey residues are excluded from Euler's theorem.

Product

Multiply the congruences aαiri(modn) for i=1,2,,φ(n).\text{Multiply the congruences } a\alpha_i \equiv r_i \pmod{n} \text{ for } i=1,2,\dots,\varphi(n).
The left side becomes (aα1)(aα2)(aαφ(n))=aφ(n)α1α2αφ(n).\text{The left side becomes } (a\alpha_1)(a\alpha_2)\cdots(a\alpha_{\varphi(n)}) = a^{\varphi(n)}\alpha_1\alpha_2\cdots\alpha_{\varphi(n)}.
Thus aφ(n)α1α2αφ(n)r1r2rφ(n)(modn).\text{Thus } a^{\varphi(n)}\alpha_1\alpha_2\cdots\alpha_{\varphi(n)} \equiv r_1r_2\cdots r_{\varphi(n)} \pmod{n}.
Because r1,r2,,rφ(n) is the same residue set as α1,α2,,αφ(n),\text{Because } r_1,r_2,\dots,r_{\varphi(n)} \text{ is the same residue set as } \alpha_1,\alpha_2,\dots,\alpha_{\varphi(n)},
r1r2rφ(n)α1α2αφ(n)(modn).r_1r_2\cdots r_{\varphi(n)} \equiv \alpha_1\alpha_2\cdots\alpha_{\varphi(n)} \pmod{n}.
aφ(n)α1α2αφ(n)α1α2αφ(n)(modn)a^{\varphi(n)}\alpha_1\alpha_2\cdots\alpha_{\varphi(n)} \equiv \alpha_1\alpha_2\cdots\alpha_{\varphi(n)} \pmod{n}
gcd(α1α2αφ(n),n)=1 because every αi is coprime to n, so we can cancel the product\gcd(\alpha_1\alpha_2\cdots\alpha_{\varphi(n)},n)=1 \text{ because every } \alpha_i \text{ is coprime to } n \text{, so we can cancel the product}
aφ(n)1(modn)\therefore a^{\varphi(n)} \equiv 1 \pmod{n}
When n=p is prime, the reduced residue system is 1,2,,p1, so this becomes Fermat’s little theorem.\text{When } n=p \text{ is prime, the reduced residue system is } 1,2,\dots,p-1 \text{, so this becomes Fermat's little theorem.}

Asymmetric cryptography

The major problem of symmetric cryptography is key distribution - sending private keys over public channels is unsafe, and meeting in person is obviously not convenient.

Public-key cryptography addresses this using mathematical trapdoor functions: operations that are easy to compute with a public key but infeasible to reverse without a corresponding private key. This allows secure key exchange, encryption, and digital signatures even when the public key is openly shared.

Symmetric cryptography

Asymmetric cryptography is powerful, but it is comparatively expensive. Real systems usually use it first to exchange a secret or authenticate the parties, then move to symmetric encryption for the actual message data.

That second phase is where symmetric cryptography takes over. Once both sides share the same secret key, they can encrypt large amounts of data much faster than with public-key operations.

Cryptographic hash functions

A cryptographic hash function turns any input into a fixed-length fingerprint. The output is deterministic, fast to compute, and changes completely when the input changes, making hashes ideal for integrity checks.

Hashes are one-way. Given only a hash, it should be infeasible to recover the original input or find another input that produces the same digest. Hashes provide tamper evidence, not secrecy.

See how hashes secure blockchains in the main demo: tweak a block and the downstream hashes break. Go to the Blockchain Hashing demo.