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

Modular Arithmetic and Modular Exponentiation

1. Number Theory in Computer Science

Number theory studies the properties of integers and their relationships. Although number theory is broad enough to support an entire course, several topics are especially important in computer science:

  • Modular arithmetic, which is fundamental to cryptographic algorithms.

  • Properties of prime numbers, used in many cryptographic constructions.

  • Greatest common divisor algorithms, which help determine whether numbers are relatively prime and support modular inverses.

  • Modular exponentiation, a central operation in public-key cryptography.

The efficiency of algorithms in this setting is measured primarily in terms of the number of bits needed to represent the input integers.


2. Definition of Modulo

Let aaa be an integer and let NNN be 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

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

Thus, a mod Na \bmod NamodN is the remainder obtained when aaa is divided by NNN, with the remainder chosen from the range 000 through N−1N-1N−1.

Examples

  • 5 mod 4=15 \bmod 4 = 15mod4=1, because

    5=1⋅4+1.5 = 1 \cdot 4 + 1.5=1⋅4+1.
  • −11 mod 3=1-11 \bmod 3 = 1−11mod3=1, because

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

Although −11-11−11 can also be written as

−11=(−3)⋅3−2,-11 = (-3)\cdot 3 - 2,−11=(−3)⋅3−2,

the value −2-2−2 is not accepted as the modulo result because the remainder must lie in the range

0≤r≤2.0 \leq r \leq 2.0≤r≤2.

Therefore, −11 mod 3-11 \bmod 3−11mod3 is 111, not −2-2−2.

Clock interpretation

Modular arithmetic can be visualized using a clock with NNN positions labeled

0,1,2,…,N−1.0,1,2,\ldots,N-1.0,1,2,…,N−1.
  • To compute a mod Na \bmod NamodN for a positive aaa, start at 000 and move clockwise aaa positions.

  • To compute the modulo of a negative number, move anticlockwise.

For example, with modulus 444, the possible remainders are 000, 111, 222, and 333. Moving five positions from 000 lands at 111, so

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

3. Congruence Modulo NNN

Two integers aaa and bbb are congruent modulo NNN if they produce the same remainder when divided by NNN.

The notation is

a≡b(modN).a \equiv b \pmod N.a≡b(modN).

This means

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

Congruence indicates that aaa and bbb are equivalent with respect to their remainders modulo NNN, even if their actual values are different.

Equivalent definition

The congruence

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

holds if and only if NNN divides the difference a−ba-ba−b:

N∣(a−b).N \mid (a-b).N∣(a−b).

Equivalently, there exists an integer kkk such that

a−b=kN.a-b=kN.a−b=kN.

Example

Since

17 mod 5=217 \bmod 5 = 217mod5=2

and

7 mod 5=2,7 \bmod 5 = 2,7mod5=2,

it follows that

17≡7(mod5).17 \equiv 7 \pmod 5.17≡7(mod5).

The difference is

17−7=10,17-7=10,17−7=10,

and 101010 is divisible by 555.

Key idea: Congruence modulo NNN means that two integers differ by an integer multiple of NNN.


4. Arithmetic Rules of Modular Arithmetic

Suppose

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

and

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

Then 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.

In congruence notation, these rules can be expressed as

a≡a′(modN)a \equiv a' \pmod Na≡a′(modN)

and

b≡b′(modN),b \equiv b' \pmod N,b≡b′(modN),

which imply

a+b≡a′+b′(modN),a+b \equiv a'+b' \pmod N,a+b≡a′+b′(modN),
a−b≡a′−b′(modN),a-b \equiv a'-b' \pmod N,a−b≡a′−b′(modN),

and

ab≡a′b′(modN).ab \equiv a'b' \pmod N.ab≡a′b′(modN).

Why the addition rule works

Suppose

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

and

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

Also suppose that

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

Then

\begin{align*} a+b &=(q_1N+a')+(q_2N+b') &=(q_1+q_2)N+(a'+b') &=(q_1+q_2)N+q_3N+r &=(q_1+q_2+q_3)N+r. \end{align*}

When a+ba+ba+b is divided by NNN, all terms containing NNN contribute remainder 000. The remaining remainder is rrr, which is exactly the remainder obtained from

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

The same reasoning applies to subtraction and multiplication.

Practical interpretation

Modular arithmetic permits the following efficient procedure:

  1. Reduce each operand modulo NNN.

  2. Perform the desired operation on the smaller values.

  3. Reduce the result modulo NNN if necessary.

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

Example: modular multiplication

Suppose the goal is to compute

ab mod 100,ab \bmod 100,abmod100,

where

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

and

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

Instead of multiplying the original, potentially large values, compute

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

Since 28<10028<10028<100, no further reduction is needed.

Key idea: Reducing operands before an operation prevents intermediate values from becoming unnecessarily large.


5. Modular Division and Cancellation

Addition, subtraction, and multiplication behave predictably under modular reduction. Division is different.

Division is not automatically defined

An expression such as

ab mod N\frac{a}{b}\bmod Nba​modN

may not be meaningful because ab\frac{a}{b}ba​ may not be an integer.

For example, let

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

The expression

31 mod 4\frac{3}{1}\bmod 413​mod4

is well-defined and equals

3 mod 4=3.3\bmod 4=3.3mod4=3.

However,

35 mod 4\frac{3}{5}\bmod 453​mod4

involves the noninteger value

35=0.6.\frac{3}{5}=0.6.53​=0.6.

Standard modular arithmetic is defined over integers, so the expression is not automatically well-defined.

Cancellation is not always valid

In ordinary arithmetic, if

ac=bc,ac=bc,ac=bc,

one can usually cancel ccc and conclude that

a=b.a=b.a=b.

In modular arithmetic, cancellation does not always work. From

ac≡bc(modN),ac\equiv bc\pmod N,ac≡bc(modN),

one cannot automatically conclude that

a≡b(modN).a\equiv b\pmod N.a≡b(modN).

The congruence implies only that

N∣c(a−b).N\mid c(a-b).N∣c(a−b).

The factor ccc may contain factors shared with NNN, allowing the product c(a−b)c(a-b)c(a−b) to be divisible by NNN even when a−ba-ba−b is not.

When cancellation is valid

Cancellation by ccc is valid when ccc has a multiplicative inverse modulo NNN. This occurs precisely when

gcd⁡(c,N)=1.\gcd(c,N)=1.gcd(c,N)=1.

In that case, ccc is relatively prime to NNN, and

ac≡bc(modN)ac\equiv bc\pmod Nac≡bc(modN)

does imply

a≡b(modN).a\equiv b\pmod N.a≡b(modN).

If gcd⁡(c,N)≠1\gcd(c,N)\ne 1gcd(c,N)=1, cancellation may fail.


6. Complexity of Modular Arithmetic Algorithms

Assume that the integers aaa, bbb, and NNN are represented using at most nnn bits. The efficiency of an algorithm is measured by the number of bit operations it performs as a function of nnn.

The desired algorithms should run in polynomial time, meaning their running time should be bounded by a polynomial in nnn, such as

O(n2),O(n3),O(n^2),\qquad O(n^3),O(n2),O(n3),

or another polynomial expression.

An algorithm whose running time is exponential in nnn, such as

O(2n),O(2^n),O(2n),

is generally impractical for cryptographic-size inputs.

Addition modulo NNN

To compute

(a+b) mod N,(a+b)\bmod N,(a+b)modN,

the algorithm can:

  1. Add the binary representations of aaa and bbb bit by bit.

  2. Handle carries during the addition.

  3. Reduce the resulting integer modulo NNN.

Binary addition and modular reduction can both be performed using a polynomial number of bit operations.

Subtraction modulo NNN

To compute

(a−b) mod N,(a-b)\bmod N,(a−b)modN,

the algorithm subtracts the binary representations, handles borrowing, and then reduces the result modulo NNN. This also requires polynomially many bit operations.

Multiplication modulo NNN

To compute

(ab) mod N,(ab)\bmod N,(ab)modN,

the algorithm multiplies the binary representations of aaa and bbb, then reduces the product modulo NNN. Standard binary multiplication and division-based reduction are polynomial-time operations.

Therefore:

  • Modular addition is polynomial-time.

  • Modular subtraction is polynomial-time.

  • Modular multiplication is polynomial-time.

The more difficult operation is modular exponentiation.


7. Modular Exponentiation

Modular exponentiation computes an expression of the form

ab mod N.a^b\bmod N.abmodN.

This operation is especially important in cryptography. It appears in public-key systems and protocols where large powers must be computed modulo a large integer.

The naive method

A direct approach computes

aba^bab

by multiplying aaa by itself b−1b-1b−1 times. To prevent the intermediate result from becoming too large, the algorithm can reduce modulo NNN after every multiplication:

ab mod N=a⋅a⋅…⋅a⏟b factors mod N.a^b\bmod N = \underbrace{a\cdot a\cdot \ldots \cdot a}_{b\text{ factors}}\bmod N.abmodN=b factorsa⋅a⋅…⋅a​​modN.

This requires approximately bbb modular multiplications.

Each modular multiplication is polynomial in the bit length nnn, but the exponent bbb is itself an nnn-bit integer. Its numerical value may be as large as

2n−1.2^n-1.2n−1.

Consequently, performing bbb modular multiplications may require exponentially many operations as a function of nnn.

Why the naive algorithm is exponential

If bbb has nnn bits, then bbb can have magnitude close to 2n2^n2n. Therefore, the naive algorithm requires roughly

2n2^n2n

modular multiplications in the worst case.

For example, when the exponent has 102410241024 bits, it can be as large as approximately

21024.2^{1024}.21024.

Even though each individual multiplication is efficient, repeating it that many times is infeasible.

Important distinction: An algorithm is not polynomial-time merely because each individual operation is polynomial-time. The number of repetitions must also be polynomial in the input length.


8. Square-and-Multiply Algorithm

The square-and-multiply algorithm, also called binary exponentiation, computes modular powers efficiently by using the binary representation of the exponent.

Instead of multiplying by aaa once for every unit in the exponent, the algorithm repeatedly squares powers of aaa:

a, a2, a4, a8, a16, a32,…a,\ a^2,\ a^4,\ a^8,\ a^{16},\ a^{32},\ldotsa, a2, a4, a8, a16, a32,…

Each new power is obtained from the previous one by squaring:

a2i+1=(a2i)2.a^{2^{i+1}}=\left(a^{2^i}\right)^2.a2i+1=(a2i)2.

All squaring and multiplication operations are performed modulo NNN.

Binary representation of the exponent

Suppose the exponent is

53.53.53.

Its binary representation is

53=(110101)2.53=(110101)_2.53=(110101)2​.

Reading from least significant bit to most significant bit gives

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

The binary expansion shows that

53=32+16+4+1.53=32+16+4+1.53=32+16+4+1.

Therefore,

a53=a32a16a4a.a^{53}=a^{32}a^{16}a^4a.a53=a32a16a4a.

The algorithm needs to accumulate only the powers corresponding to 111 bits in the binary representation:

  • a1a^1a1, corresponding to the least significant bit 111.

  • a4a^4a4, corresponding to the bit for 444.

  • a16a^{16}a16, corresponding to the bit for 161616.

  • a32a^{32}a32, corresponding to the bit for 323232.

The powers a2a^2a2 and a8a^8a8 are not accumulated because their corresponding binary bits are 000.

Example for exponent 535353

The powers are generated by repeated squaring:

a1, a2, a4, a8, a16, a32.a^1,\ a^2,\ a^4,\ a^8,\ a^{16},\ a^{32}.a1, a2, a4, a8, a16, a32.

The accumulator is updated only when the corresponding bit is 111:

  1. Start with accumulator y=1y=1y=1.

  2. Process a1a^1a1: bit is 111, so update $$ y=1\cdot a=a. $$

  3. Process a2a^2a2: bit is 000, so leave y=ay=ay=a.

  4. Process a4a^4a4: bit is 111, so update $$ y=a\cdot a^4=a^5. $$

  5. Process a8a^8a8: bit is 000, so leave y=a5y=a^5y=a5.

  6. Process a16a^{16}a16: bit is 111, so update $$ y=a^5\cdot a^{16}=a^{21}. $$

  7. Process a32a^{32}a32: bit is 111, so update $$ y=a^{21}\cdot a^{32}=a^{53}. $$

With modular reduction after every operation, the final result is

a53 mod N.a^{53}\bmod N.a53modN.

The example requires:

  • Five squarings to generate a2a^2a2, a4a^4a4, a8a^8a8, a16a^{16}a16, and a32a^{32}a32.

  • Four accumulator multiplications for the noninitial 111 bits, depending on the implementation.

  • Nine modular multiplications in total.

This is far fewer than the 525252 multiplications required by the naive method.


9. General Square-and-Multiply Procedure

Let the input be:

  • Base aaa.

  • Exponent xxx.

  • Modulus NNN.

The algorithm maintains:

  • yyy, the accumulator containing the portion of the result already computed.

  • zzz, the current power of the base.

Initially,

y=1y=1y=1

and

z=a mod N.z=a\bmod N.z=amodN.

The exponent is processed from its least significant bit to its most significant bit.

Pseudocode

Input: a, x, N
Output: a^x mod N

y ← 1
z ← a mod N

while x ≠ 0:
    if x mod 2 = 1:
        y ← (y · z) mod N

    x ← floor(x / 2)

    if x ≠ 0:
        z ← (z · z) mod N

return y

Explanation of the steps

1. Test whether the current exponent bit is 111

The least significant bit of an integer can be determined by examining

x mod 2.x\bmod 2.xmod2.
  • If

    x mod 2=1,x\bmod 2=1,xmod2=1,
  • If

    x mod 2=0,x\bmod 2=0,xmod2=0,

When the bit is 111, the current power zzz must be included in the accumulator:

y←yz mod N.y\leftarrow yz\bmod N.y←yzmodN.

2. Divide the exponent by 222

The next binary bit is obtained by replacing xxx with

⌊x2⌋.\left\lfloor \frac{x}{2}\right\rfloor.⌊2x​⌋.

Repeatedly dividing by 222 and recording the remainders produces the binary representation of the original exponent from least significant bit to most significant bit.

3. Square the current power

The next power of the base is obtained by

z←z2 mod N.z\leftarrow z^2\bmod N.z←z2modN.

For example:

a1⟶a2⟶a4⟶a8⟶a16.a^1\longrightarrow a^2\longrightarrow a^4\longrightarrow a^8\longrightarrow a^{16}.a1⟶a2⟶a4⟶a8⟶a16.

This compulsory squaring is the source of the “square” part of the algorithm. The conditional accumulator update is the “multiply” part.


10. Complexity of Square-and-Multiply

Suppose the exponent has nnn bits.

  • The algorithm performs at most nnn squarings.

  • In the worst case, every bit of the exponent is 111, so it performs at most nnn accumulator multiplications.

  • Therefore, the total number of modular multiplications is at most approximately

    2n.2n.2n.

Since each modular multiplication takes a polynomial number of bit operations, the complete square-and-multiply algorithm also runs in polynomial time.

The improvement over the naive method is substantial:

Naive exponentiation

  • Requires approximately bbb modular multiplications.

  • Since bbb may be as large as 2n2^n2n, the running time can be exponential in nnn.

Square-and-multiply exponentiation

  • Requires at most approximately 2n2n2n modular multiplications.

  • Runs in polynomial time in the bit length of the exponent.

Central result: Representing the exponent in binary changes modular exponentiation from an exponential-time process into a polynomial-time algorithm.


11. Main Takeaways

  • The value a mod Na\bmod NamodN is the unique remainder in the interval

    0≤r≤N−10\leq r\leq N-10≤r≤N−1
    a=qN+r.a=qN+r.a=qN+r.
  • The congruence

    a≡b(modN)a\equiv b\pmod Na≡b(modN)
  • Congruence is equivalent to divisibility:

    N∣(a−b).N\mid(a-b).N∣(a−b).
  • Addition, subtraction, and multiplication can be performed after reducing operands modulo NNN.

  • Modular division is not automatically defined.

  • Cancellation in a congruence is valid only under appropriate conditions, especially when the cancelled factor is relatively prime to the modulus.

  • Modular addition, subtraction, and multiplication can be implemented in polynomial time relative to the bit length of the inputs.

  • The naive algorithm for computing ab mod Na^b\bmod NabmodN may take exponentially many operations because the numerical value of an nnn-bit exponent can be as large as 2n−12^n-12n−1.

  • The square-and-multiply algorithm uses the binary representation of the exponent.

  • Repeated squaring generates powers with exponents 1,2,4,8,…1,2,4,8,\ldots1,2,4,8,….

  • Only powers corresponding to 111 bits in the exponent are multiplied into the accumulator.

  • Square-and-multiply computes modular exponentiation using at most approximately 2n2n2n modular multiplications for an nnn-bit exponent, making it practical for cryptographic applications.

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent