1. Home
  2. Explore
  3. Computer Science
  4. Number Theory for Cryptography

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Number Theory for Cryptography

Number Theory for Computer Science: Modular Arithmetic and Modular Exponentiation

Number theory studies the properties and relationships of integers. Although it is a broad mathematical subject, several of its ideas are especially important in computer science, particularly in cryptography. The central topics in this lesson are modular arithmetic, congruence, arithmetic rules for remainders, the limitations of modular division, and an efficient method for modular exponentiation.

A recurring theme is efficiency. In cryptography, the numbers can contain hundreds or thousands of bits, so an algorithm must be efficient in terms of the number of bits used to represent the input—not merely efficient in terms of the numerical value of the input.

Modular Arithmetic

Suppose aaa is an integer and NNN is a positive integer called the modulus. The expression a mod Na \bmod NamodN is the unique remainder rrr satisfying

a=qN+ra = qN + ra=qN+r

for some integer quotient qqq, where the remainder must satisfy

0≤r≤N−1.0 \leq r \leq N-1.0≤r≤N−1.

Thus, modular arithmetic records the remainder left after dividing by the modulus.

For example,

5 mod 4=15 \bmod 4 = 15mod4=1

because

5=1⋅4+1.5 = 1\cdot 4 + 1.5=1⋅4+1.

Similarly,

−11 mod 3=1-11 \bmod 3 = 1−11mod3=1

because

−11=(−4)⋅3+1.-11 = (-4)\cdot 3 + 1.−11=(−4)⋅3+1.

The remainder is 111, not −2-2−2, because the standard definition requires the remainder to lie in the range from 000 through N−1N-1N−1. For N=3N=3N=3, the allowed remainders are 000, 111, and 222.

The Clock Interpretation

A useful way to visualize modular arithmetic is as a clock with NNN positions:

0,1,2,…,N−1.0,1,2,\ldots,N-1.0,1,2,…,N−1.

When finding a mod Na \bmod NamodN for a positive aaa, the process begins at 000 and moves forward around the clock. The position where the counting stops is the remainder.

For example, with modulus 444, the possible positions are 000, 111, 222, and 333. Counting forward five steps from 000 ends at 111, so

5 mod 4=1.5 \bmod 4 = 1.5mod4=1.

For a negative integer, the equivalent visualization moves in the opposite, or anticlockwise, direction. This explains why

−11 mod 3=1.-11 \bmod 3 = 1.−11mod3=1.

Starting at 000 and moving backward eleven positions on a three-position cycle ends at 111.

Key definition: a mod Na \bmod NamodN is the unique remainder in the range 000 through N−1N-1N−1 such that a=qN+ra=qN+ra=qN+r for some integer qqq.

Think About It

Why is the result of −11 mod 3-11 \bmod 3−11mod3 written as 111 rather than −2-2−2, even though both values can appear in equivalent equations involving multiples of 333?

Congruence Modulo NNN

Two integers can have different values but produce the same remainder when divided by the same modulus. This relationship is called congruence.

The statement

a≡b(modN)a \equiv b \pmod Na≡b(modN)

means that aaa and bbb have the same remainder when divided by NNN:

a mod N=b mod N.a \bmod N = b \bmod N.amodN=bmodN.

Congruence therefore treats numbers as equivalent when they occupy the same position on the modular clock.

For example, since

5 mod 4=15 \bmod 4 = 15mod4=1

and

9 mod 4=1,9 \bmod 4 = 1,9mod4=1,

it follows that

5≡9(mod4).5 \equiv 9 \pmod 4.5≡9(mod4).

There is an equivalent and often more useful way to recognize congruence:

a≡b(modN)⟺N divides a−b.a \equiv b \pmod N \quad\Longleftrightarrow\quad N \text{ divides } a-b.a≡b(modN)⟺N divides a−b.

In other words, aaa and bbb are congruent modulo NNN exactly when their difference is completely divisible by NNN.

For the previous example,

9−5=4,9-5=4,9−5=4,

and 444 is divisible by 444, so

5≡9(mod4).5 \equiv 9 \pmod 4.5≡9(mod4).

This condition works because if two numbers differ by an exact multiple of NNN, moving around a clock with NNN positions brings both numbers to the same final position.

Arithmetic Rules for Modular Values

Suppose

a′=a mod Na' = a \bmod Na′=amodN

and

b′=b mod N.b' = b \bmod N.b′=bmodN.

The values a′a'a′ and b′b'b′ are the reduced representatives of aaa and bbb. Modular arithmetic allows the operands to be reduced before performing addition, subtraction, or multiplication:

(a+b) mod N=(a′+b′) mod N,(a+b)\bmod N = (a'+b')\bmod N,(a+b)modN=(a′+b′)modN, (a−b) mod N=(a′−b′) mod N,(a-b)\bmod N = (a'-b')\bmod N,(a−b)modN=(a′−b′)modN,

and

(ab) mod N=(a′b′) mod N.(ab)\bmod N = (a'b')\bmod N.(ab)modN=(a′b′)modN.

The important practical meaning is that a calculation can be performed in stages:

  1. Reduce each operand modulo NNN.
  2. Perform the desired arithmetic operation.
  3. Reduce the result modulo NNN if necessary.

This produces the same final remainder as performing the operation on the original values first.

Why the Addition Rule Works

Because a′=a mod Na'=a\bmod Na′=amodN, there is some integer q1q_1q1​ such that

a=q1N+a′.a=q_1N+a'.a=q1​N+a′.

Likewise, because b′=b mod Nb'=b\bmod Nb′=bmodN, there is some integer q2q_2q2​ such that

b=q2N+b′.b=q_2N+b'.b=q2​N+b′.

Adding these equations gives

a+b=(q1N+a′)+(q2N+b′).a+b=(q_1N+a')+(q_2N+b').a+b=(q1​N+a′)+(q2​N+b′).

Rearranging,

a+b=(q1+q2)N+(a′+b′).a+b=(q_1+q_2)N+(a'+b').a+b=(q1​+q2​)N+(a′+b′).

Now suppose that reducing a′+b′a'+b'a′+b′ modulo NNN gives remainder rrr. Then there is some integer q3q_3q3​ such that

a′+b′=q3N+r.a'+b'=q_3N+r.a′+b′=q3​N+r.

Substituting this into the previous equation gives

a+b=(q1+q2+q3)N+r.a+b=(q_1+q_2+q_3)N+r.a+b=(q1​+q2​+q3​)N+r.

Everything except rrr is a multiple of NNN, so the remainder of a+ba+ba+b modulo NNN is rrr. That is exactly the same remainder obtained by reducing a′+b′a'+b'a′+b′ modulo NNN.

The proofs for subtraction and multiplication use the same basic idea: terms involving NNN disappear when the final remainder is taken.

Example: Reducing Before Multiplication

Suppose the goal is to compute

ab mod 100ab \bmod 100abmod100

for two large integers aaa and bbb, where

a mod 100=28a\bmod 100=28amod100=28

and

b mod 100=1.b\bmod 100=1.bmod100=1.

Instead of first multiplying the two large numbers, the modular multiplication rule gives

ab mod 100=(28⋅1) mod 100.ab\bmod 100 = (28\cdot 1)\bmod 100.abmod100=(28⋅1)mod100.

Therefore,

ab mod 100=28.ab\bmod 100=28.abmod100=28.

The reduced operands are much smaller, so the calculation is easier. This reduction-before-operation strategy becomes especially valuable when the original values contain many bits.

Practical principle: In modular addition, subtraction, and multiplication, reducing the operands first gives the same final result as performing the operation first and reducing afterward.

Think About It

If two operands are very large but their remainders modulo NNN are small, why is it advantageous to reduce them before multiplying?

Why Modular Division Is Different

Addition, subtraction, and multiplication behave predictably under modular reduction. Division does not work in the same straightforward way.

Suppose

a′=a mod Na' = a\bmod Na′=amodN

and

b′=b mod N.b' = b\bmod N.b′=bmodN.

It is not generally valid to claim that

ab mod N=a′b′ mod N.\frac{a}{b}\bmod N = \frac{a'}{b'}\bmod N.ba​modN=b′a′​modN.

There are two problems.

First, ab\frac{a}{b}ba​ may not be an integer. Modular arithmetic as defined here produces remainders for integers. If the quotient is a fraction, its remainder modulo NNN is not defined under this basic definition.

For example, let

a=3,b=5,N=4.a=3,\qquad b=5,\qquad N=4.a=3,b=5,N=4.

Since

a\bmod 4=3 \] and

b\bmod 4=1,

theexpression the expression theexpression

\frac{a'}{b'}\bmod 4

\frac{3}{1}\bmod 4

3

iswell−defined.However, is well-defined. However, iswell−defined.However,

\frac{a}{b}\bmod 4

\frac{3}{5}\bmod 4

involves the fraction $\frac{3}{5}$, so it is not defined by the integer remainder operation. Second, modular congruence does not automatically permit cancellation. If

ac\equiv bc\pmod N,

itdoesnotnecessarilyfollowthat it does not necessarily follow that itdoesnotnecessarilyfollowthat

a\equiv b\pmod N.

In ordinary arithmetic, a common nonzero factor can usually be canceled. In modular arithmetic, that cancellation can fail because multiplication by $c$ may cause distinct modular values to become equal. Therefore, modular division and cancellation require additional conditions. They cannot be assumed simply because the corresponding operations work over ordinary integers. ## Measuring Algorithmic Efficiency The relevant inputs for modular arithmetic are integers such as $a$, $b$, and $N$. If each is represented using approximately $n$ bits, then the input size is measured by $n$, not by the numerical magnitude of the integer. For example, an $n$-bit integer can have a numerical value as large as approximately

2^n.

This distinction is essential. An algorithm that performs a number of operations proportional to the numerical value of an $n$-bit input may actually require exponentially many operations in terms of $n$. The desired algorithms perform a number of bit operations that is a **polynomial function of $n$**. Addition, subtraction, and modular multiplication can all be performed in polynomially many bit operations. ### Addition and Subtraction To add two $n$-bit integers, the bits are processed one position at a time while keeping track of carries. This produces the binary representation of the sum using a number of operations that is polynomial in $n$. After computing the sum, the result can be reduced modulo $N$. Modular subtraction follows the same general principle: perform the subtraction and then reduce the result. ### Multiplication Two $n$-bit integers can also be multiplied using a polynomial number of bit operations. Afterward, the product can be reduced modulo $N$. Thus, modular addition, modular subtraction, and modular multiplication are considered efficiently computable in this setting. The main challenge arises with exponentiation. ## Modular Exponentiation The modular exponentiation problem is to compute

a^b\bmod N.

This operation is especially important in cryptography because cryptographic algorithms often use powers of large integers reduced modulo a large modulus. ### The Naive Method A direct method computes the power by repeatedly multiplying by $a$:

a^b

\underbrace{a\cdot a\cdot \ldots \cdot a}_{b\text{ factors}}.

If modular reduction is performed after every multiplication, this requires approximately $b$ modular multiplications. At first glance, this may appear efficient because each individual modular multiplication is polynomial in $n$. However, the exponent $b$ itself may be an $n$-bit integer. Its numerical value can be as large as approximately

2^n.

Consequently, repeating a polynomial-time modular multiplication approximately $b$ times can require approximately

2^n\cdot \operatorname{poly}(n)

operations. This is exponential in the number of bits $n$. For example, if the exponent has $1024$ bits, its value may be on the order of

2^{1024},

which is an enormously large number. Repeating a multiplication that many times is not practical. The issue is therefore not whether each multiplication is efficient. The issue is that the naive method performs far too many multiplications. ### Think About It Why does an algorithm that repeats a polynomial-time operation $b$ times become inefficient when $b$ is itself an $n$-bit integer? ## The Square-and-Multiply Method The efficient method for modular exponentiation is called **square and multiply**. Its purpose is to reduce the number of modular multiplications from a quantity proportional to the exponent’s numerical value to a quantity proportional to the number of bits in the exponent. The method relies on the binary representation of the exponent. ### Expressing the Exponent in Binary Consider the exponent
Itsbinaryrepresentationis Its binary representation is Itsbinaryrepresentationis

53=(110101)_2.

Thebinarydigitsindicatewhichpowersoftwoareneededtoreconstructtheexponent: The binary digits indicate which powers of two are needed to reconstruct the exponent: Thebinarydigitsindicatewhichpowersoftwoareneededtoreconstructtheexponent:

53=32+16+4+1.

Therefore, Therefore, Therefore,

a^{53}

a^{32}a^{16}a^4a^1.

The powers $a^2$ and $a^8$ are not included because the corresponding bits of the binary representation are $0$. This is the central idea: - A binary bit equal to $1$ means that the corresponding power of $a$ is included. - A binary bit equal to $0$ means that the corresponding power of $a$ is skipped. All operations can be performed modulo $N`, using the modular multiplication rule to keep intermediate values manageable. ### Generating Powers by Squaring The required powers of $a$ can be generated efficiently:

a^1,\ a^2,\ a^4,\ a^8,\ a^{16},\ a^{32},\ldots

Eachpowerisobtainedbysquaringtheprecedingone: Each power is obtained by squaring the preceding one: Eachpowerisobtainedbysquaringtheprecedingone:

a^2=(a^1)^2,

a^4=(a^2)^2,

a^8=(a^4)^2,

andsoforth.Inmodularform,thecalculationiswrittenas and so forth. In modular form, the calculation is written as andsoforth.Inmodularform,thecalculationiswrittenas

z_{\text{new}}=(z_{\text{current}}^2)\bmod N.

Thus, one modular multiplication produces the next power in the sequence. ### Accumulating the Selected Powers An accumulator stores the product of the powers corresponding to the $1$ bits of the exponent. For $53=(110101)_2$, the accumulator eventually multiplies

a^1,\quad a^4,\quad a^{16},\quad a^{32}.

Thefinalvalueistherefore The final value is therefore Thefinalvalueistherefore

a^1a^4a^{16}a^{32}

a^{1+4+16+32}

a^{53}.

The accumulator is updated only when the current binary bit is $1. This update is called the **conditional multiplication** or **optional accumulation** step. The squaring step is compulsory because the next power of $a$ must be generated. The accumulation step is optional because it depends on the current bit. ## Iterative Square-and-Multiply Algorithm The exponent can be processed from its least significant bit to its most significant bit. The least significant bit is the rightmost binary bit and indicates whether the exponent is odd. The algorithm maintains: - $x$, the portion of the exponent still being processed; - $z$, the current power of $a$; - $y$, the accumulator containing the result so far. The initialization is

y=1,\qquad z=a.

The value $y=1$ is useful because multiplying by $1$ leaves the result unchanged. At each iteration, the algorithm performs the following actions: 1. Check whether $x$ is odd by computing $x\bmod 2$. 2. If $x\bmod 2=1$, update the accumulator:

y=(y\cdot z)\bmod N.

3. Divide $x$ by $2$ and retain the integer quotient:

x\leftarrow \left\lfloor\frac{x}{2}\right\rfloor.

4.Squarethecurrentpower:4. Square the current power:4.Squarethecurrentpower:

z\leftarrow (z\cdot z)\bmod N.

5. Repeat until $x=0$. The test $x\bmod 2$ extracts the current least significant binary bit. Dividing $x$ by $2$ shifts the binary representation one position to the right, allowing the next bit to be examined during the next iteration. ### Pseudocode ```text SquareMultiply(a, x, N): y ← 1 z ← a while x ≠ 0: if x mod 2 = 1: y ← (y · z) mod N x ← floor(x / 2) z ← (z · z) mod N return y ``` The returned value is

a^x\bmod N

for the original exponent $x$. ### Walkthrough with Exponent $53$ The exponent $53$ has binary representation

53=(110101)_2.

Whenprocessedfromleastsignificantbittomostsignificantbit,thebitsare When processed from least significant bit to most significant bit, the bits are Whenprocessedfromleastsignificantbittomostsignificantbit,thebitsare

1,0,1,0,1,1.

Thecorrespondingpowersare The corresponding powers are Thecorrespondingpowersare

a^1,\ a^2,\ a^4,\ a^8,\ a^{16},\ a^{32}.

The algorithm behaves as follows: - The bit for $a^1$ is $1$, so $a^1$ is accumulated. - The bit for $a^2$ is $0$, so $a^2$ is skipped. - The bit for $a^4$ is $1$, so $a^4$ is accumulated. - The bit for $a^8$ is $0$, so $a^8$ is skipped. - The bit for $a^{16}$ is $1$, so $a^{16}$ is accumulated. - The bit for $a^{32}$ is $1$, so $a^{32}$ is accumulated. The accumulator therefore computes

a^1a^4a^{16}a^{32}\bmod N

a^{53}\bmod N.

The method requires one compulsory squaring for each bit processed and at most one additional multiplication for each bit whose value is $1$. ## Complexity of Square and Multiply If the exponent has $n$ bits, then the algorithm performs at most $n$ squaring operations. In the worst case, every exponent bit is $1$, so it performs at most another $n$ accumulator multiplications. Therefore, the total number of modular multiplications is at most approximately

2n.

Since each modular multiplication requires a polynomial number of bit operations, the complete square-and-multiply algorithm also runs in polynomial time in the bit length. This is the crucial improvement: - The naive method may require a number of multiplications proportional to $b$, which can be exponential in the number of bits. - Square and multiply requires a number of multiplications proportional to the number of bits in $b$. For a $1024$-bit exponent, the naive approach could require a quantity on the order of

2^{1024}

multiplications, whereas square and multiply requires only a number on the order of $1024$ modular multiplications. > **Central result:** Square and multiply changes modular exponentiation from an exponential-time method in the exponent’s bit length to a polynomial-time method. ## Comprehensive Review Number theory contributes several tools that are useful in computer science and cryptography. Modular arithmetic begins with division with remainder: for a positive modulus $N$, the value $a\bmod N$ is the unique remainder in the range $0$ through $N-1$ satisfying

a=qN+r.

Thenotation The notation Thenotation

a\equiv b\pmod N

means that $a$ and $b$ have the same remainder modulo $N$. Equivalently, their difference is divisible by $N$:

N\mid(a-b).

Modularaddition,subtraction,andmultiplicationpermittheoperandstobereducedbeforetheoperation: Modular addition, subtraction, and multiplication permit the operands to be reduced before the operation: Modularaddition,subtraction,andmultiplicationpermittheoperandstobereducedbeforetheoperation:

(a+b)\bmod N=(a\bmod N+b\bmod N)\bmod N,

withanalogousrulesforsubtractionandmultiplication.Thisallowslargeintermediatevaluestobereducedearly.Divisionisdifferent.Aquotientmaynotbeaninteger,soitsmodularremaindermaynotbedefinedunderthebasicintegerdefinition.Also,from with analogous rules for subtraction and multiplication. This allows large intermediate values to be reduced early. Division is different. A quotient may not be an integer, so its modular remainder may not be defined under the basic integer definition. Also, from withanalogousrulesforsubtractionandmultiplication.Thisallowslargeintermediatevaluestobereducedearly.Divisionisdifferent.Aquotientmaynotbeaninteger,soitsmodularremaindermaynotbedefinedunderthebasicintegerdefinition.Also,from

ac\equiv bc\pmod N

one cannot automatically cancel $c$ and conclude that

a\equiv b\pmod N.

For algorithm design, input size is measured by the number of bits needed to represent the integers. Addition, subtraction, and modular multiplication use polynomially many bit operations. The naive method for computing $a^b\bmod N$ is inefficient because it performs approximately $b$ multiplications, while an $n$-bit exponent can have a value near $2^n$. Square and multiply avoids this problem by writing the exponent in binary, generating powers of $a$ through repeated squaring, and multiplying into an accumulator only when the corresponding binary bit is $1$. With an $n$-bit exponent, it uses at most approximately $2n$ modular multiplications, making modular exponentiation efficient enough to serve as a central operation in cryptographic algorithms.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent