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 a is an integer and N is a positive integer called the modulus. The expression amodN is the unique remainder r satisfying
a=qN+rfor some integer quotient q, where the remainder must satisfy
0≤r≤N−1.Thus, modular arithmetic records the remainder left after dividing by the modulus.
For example,
5mod4=1because
5=1⋅4+1.Similarly,
−11mod3=1because
−11=(−4)⋅3+1.The remainder is 1, not −2, because the standard definition requires the remainder to lie in the range from 0 through N−1. For N=3, the allowed remainders are 0, 1, and 2.
The Clock Interpretation
A useful way to visualize modular arithmetic is as a clock with N positions:
0,1,2,…,N−1.When finding amodN for a positive a, the process begins at 0 and moves forward around the clock. The position where the counting stops is the remainder.
For example, with modulus 4, the possible positions are 0, 1, 2, and 3. Counting forward five steps from 0 ends at 1, so
5mod4=1.For a negative integer, the equivalent visualization moves in the opposite, or anticlockwise, direction. This explains why
−11mod3=1.Starting at 0 and moving backward eleven positions on a three-position cycle ends at 1.
Key definition: amodN is the unique remainder in the range 0 through N−1 such that a=qN+r for some integer q.
Think About It
Why is the result of −11mod3 written as 1 rather than −2, even though both values can appear in equivalent equations involving multiples of 3?
Congruence Modulo N
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)means that a and b have the same remainder when divided by N:
amodN=bmodN.Congruence therefore treats numbers as equivalent when they occupy the same position on the modular clock.
For example, since
5mod4=1and
9mod4=1,it follows that
5≡9(mod4).There is an equivalent and often more useful way to recognize congruence:
a≡b(modN)⟺N divides a−b.In other words, a and b are congruent modulo N exactly when their difference is completely divisible by N.
For the previous example,
9−5=4,and 4 is divisible by 4, so
5≡9(mod4).This condition works because if two numbers differ by an exact multiple of N, moving around a clock with N positions brings both numbers to the same final position.
Arithmetic Rules for Modular Values
Suppose
a′=amodNand
b′=bmodN.The values a′ and b′ are the reduced representatives of a and b. Modular arithmetic allows the operands to be reduced before performing addition, subtraction, or multiplication:
(a+b)modN=(a′+b′)modN, (a−b)modN=(a′−b′)modN,and
(ab)modN=(a′b′)modN.The important practical meaning is that a calculation can be performed in stages:
- Reduce each operand modulo N.
- Perform the desired arithmetic operation.
- Reduce the result modulo N if necessary.
This produces the same final remainder as performing the operation on the original values first.
Why the Addition Rule Works
Because a′=amodN, there is some integer q1 such that
a=q1N+a′.Likewise, because b′=bmodN, there is some integer q2 such that
b=q2N+b′.Adding these equations gives
a+b=(q1N+a′)+(q2N+b′).Rearranging,
a+b=(q1+q2)N+(a′+b′).Now suppose that reducing a′+b′ modulo N gives remainder r. Then there is some integer q3 such that
a′+b′=q3N+r.Substituting this into the previous equation gives
a+b=(q1+q2+q3)N+r.Everything except r is a multiple of N, so the remainder of a+b modulo N is r. That is exactly the same remainder obtained by reducing a′+b′ modulo N.
The proofs for subtraction and multiplication use the same basic idea: terms involving N disappear when the final remainder is taken.
Example: Reducing Before Multiplication
Suppose the goal is to compute
abmod100for two large integers a and b, where
amod100=28and
bmod100=1.Instead of first multiplying the two large numbers, the modular multiplication rule gives
abmod100=(28⋅1)mod100.Therefore,
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 N 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′=amodNand
b′=bmodN.It is not generally valid to claim that
bamodN=b′a′modN.There are two problems.
First, ba may not be an integer. Modular arithmetic as defined here produces remainders for integers. If the quotient is a fraction, its remainder modulo N is not defined under this basic definition.
For example, let
a=3,b=5,N=4.Since
a\bmod 4=3 \] andb\bmod 4=1,
theexpression\frac{a'}{b'}\bmod 4
\frac{3}{1}\bmod 4
3
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. Ifac\equiv bc\pmod N,
itdoesnotnecessarilyfollowthata\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 approximately2^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 computea^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 approximately2^n.
Consequently, repeating a polynomial-time modular multiplication approximately $b$ times can require approximately2^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 of2^{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 exponent53=(110101)_2.
Thebinarydigitsindicatewhichpowersoftwoareneededtoreconstructtheexponent:53=32+16+4+1.
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:a^2=(a^1)^2,
a^4=(a^2)^2,
a^8=(a^4)^2,
andsoforth.Inmodularform,thecalculationiswrittenasz_{\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 multipliesa^1,\quad a^4,\quad a^{16},\quad a^{32}.
Thefinalvalueisthereforea^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 isy=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: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 isa^x\bmod N
for the original exponent $x$. ### Walkthrough with Exponent $53$ The exponent $53$ has binary representation53=(110101)_2.
Whenprocessedfromleastsignificantbittomostsignificantbit,thebitsare1,0,1,0,1,1.
Thecorrespondingpowersarea^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 computesa^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 approximately2n.
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 of2^{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$ satisfyinga=qN+r.
Thenotationa\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:(a+b)\bmod N=(a\bmod N+b\bmod N)\bmod N,
withanalogousrulesforsubtractionandmultiplication.Thisallowslargeintermediatevaluestobereducedearly.Divisionisdifferent.Aquotientmaynotbeaninteger,soitsmodularremaindermaynotbedefinedunderthebasicintegerdefinition.Also,fromac\equiv bc\pmod N
one cannot automatically cancel $c$ and conclude thata\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.