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 a be an integer and let N be a positive integer called the modulus. The expression amodN is the unique remainder r satisfying
for some integer quotient q, where
Thus, amodN is the remainder obtained when a is divided by N, with the remainder chosen from the range 0 through N−1.
Examples
5mod4=1, because
5=1⋅4+1.−11mod3=1, because
−11=(−4)⋅3+1.
Although −11 can also be written as
the value −2 is not accepted as the modulo result because the remainder must lie in the range
Therefore, −11mod3 is 1, not −2.
Clock interpretation
Modular arithmetic can be visualized using a clock with N positions labeled
To compute amodN for a positive a, start at 0 and move clockwise a positions.
To compute the modulo of a negative number, move anticlockwise.
For example, with modulus 4, the possible remainders are 0, 1, 2, and 3. Moving five positions from 0 lands at 1, so
3. Congruence Modulo N
Two integers a and b are congruent modulo N if they produce the same remainder when divided by N.
The notation is
This means
Congruence indicates that a and b are equivalent with respect to their remainders modulo N, even if their actual values are different.
Equivalent definition
The congruence
holds if and only if N divides the difference a−b:
Equivalently, there exists an integer k such that
Example
Since
and
it follows that
The difference is
and 10 is divisible by 5.
Key idea: Congruence modulo N means that two integers differ by an integer multiple of N.
4. Arithmetic Rules of Modular Arithmetic
Suppose
and
Then modular arithmetic allows the operands to be reduced before performing addition, subtraction, or multiplication:
and
In congruence notation, these rules can be expressed as
and
which imply
and
Why the addition rule works
Suppose
and
Also suppose that
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+b is divided by N, all terms containing N contribute remainder 0. The remaining remainder is r, which is exactly the remainder obtained from
The same reasoning applies to subtraction and multiplication.
Practical interpretation
Modular arithmetic permits the following efficient procedure:
Reduce each operand modulo N.
Perform the desired operation on the smaller values.
Reduce the result modulo N 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
where
and
Instead of multiplying the original, potentially large values, compute
Since 28<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
may not be meaningful because ba may not be an integer.
For example, let
The expression
is well-defined and equals
However,
involves the noninteger value
Standard modular arithmetic is defined over integers, so the expression is not automatically well-defined.
Cancellation is not always valid
In ordinary arithmetic, if
one can usually cancel c and conclude that
In modular arithmetic, cancellation does not always work. From
one cannot automatically conclude that
The congruence implies only that
The factor c may contain factors shared with N, allowing the product c(a−b) to be divisible by N even when a−b is not.
When cancellation is valid
Cancellation by c is valid when c has a multiplicative inverse modulo N. This occurs precisely when
In that case, c is relatively prime to N, and
does imply
If gcd(c,N)=1, cancellation may fail.
6. Complexity of Modular Arithmetic Algorithms
Assume that the integers a, b, and N are represented using at most n bits. The efficiency of an algorithm is measured by the number of bit operations it performs as a function of n.
The desired algorithms should run in polynomial time, meaning their running time should be bounded by a polynomial in n, such as
or another polynomial expression.
An algorithm whose running time is exponential in n, such as
is generally impractical for cryptographic-size inputs.
Addition modulo N
To compute
the algorithm can:
Add the binary representations of a and b bit by bit.
Handle carries during the addition.
Reduce the resulting integer modulo N.
Binary addition and modular reduction can both be performed using a polynomial number of bit operations.
Subtraction modulo N
To compute
the algorithm subtracts the binary representations, handles borrowing, and then reduces the result modulo N. This also requires polynomially many bit operations.
Multiplication modulo N
To compute
the algorithm multiplies the binary representations of a and b, then reduces the product modulo N. 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
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
by multiplying a by itself b−1 times. To prevent the intermediate result from becoming too large, the algorithm can reduce modulo N after every multiplication:
This requires approximately b modular multiplications.
Each modular multiplication is polynomial in the bit length n, but the exponent b is itself an n-bit integer. Its numerical value may be as large as
Consequently, performing b modular multiplications may require exponentially many operations as a function of n.
Why the naive algorithm is exponential
If b has n bits, then b can have magnitude close to 2n. Therefore, the naive algorithm requires roughly
modular multiplications in the worst case.
For example, when the exponent has 1024 bits, it can be as large as approximately
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 a once for every unit in the exponent, the algorithm repeatedly squares powers of a:
Each new power is obtained from the previous one by squaring:
All squaring and multiplication operations are performed modulo N.
Binary representation of the exponent
Suppose the exponent is
Its binary representation is
Reading from least significant bit to most significant bit gives
The binary expansion shows that
Therefore,
The algorithm needs to accumulate only the powers corresponding to 1 bits in the binary representation:
a1, corresponding to the least significant bit 1.
a4, corresponding to the bit for 4.
a16, corresponding to the bit for 16.
a32, corresponding to the bit for 32.
The powers a2 and a8 are not accumulated because their corresponding binary bits are 0.
Example for exponent 53
The powers are generated by repeated squaring:
The accumulator is updated only when the corresponding bit is 1:
Start with accumulator y=1.
Process a1: bit is 1, so update $$ y=1\cdot a=a. $$
Process a2: bit is 0, so leave y=a.
Process a4: bit is 1, so update $$ y=a\cdot a^4=a^5. $$
Process a8: bit is 0, so leave y=a5.
Process a16: bit is 1, so update $$ y=a^5\cdot a^{16}=a^{21}. $$
Process a32: bit is 1, so update $$ y=a^{21}\cdot a^{32}=a^{53}. $$
With modular reduction after every operation, the final result is
The example requires:
Five squarings to generate a2, a4, a8, a16, and a32.
Four accumulator multiplications for the noninitial 1 bits, depending on the implementation.
Nine modular multiplications in total.
This is far fewer than the 52 multiplications required by the naive method.
9. General Square-and-Multiply Procedure
Let the input be:
Base a.
Exponent x.
Modulus N.
The algorithm maintains:
y, the accumulator containing the portion of the result already computed.
z, the current power of the base.
Initially,
and
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 yExplanation of the steps
1. Test whether the current exponent bit is 1
The least significant bit of an integer can be determined by examining
If
xmod2=1,If
xmod2=0,
When the bit is 1, the current power z must be included in the accumulator:
2. Divide the exponent by 2
The next binary bit is obtained by replacing x with
Repeatedly dividing by 2 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
For example:
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 n bits.
The algorithm performs at most n squarings.
In the worst case, every bit of the exponent is 1, so it performs at most n accumulator multiplications.
Therefore, the total number of modular multiplications is at most approximately
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 b modular multiplications.
Since b may be as large as 2n, the running time can be exponential in n.
Square-and-multiply exponentiation
Requires at most approximately 2n 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 amodN is the unique remainder in the interval
0≤r≤N−1a=qN+r.The congruence
a≡b(modN)Congruence is equivalent to divisibility:
N∣(a−b).Addition, subtraction, and multiplication can be performed after reducing operands modulo N.
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 abmodN may take exponentially many operations because the numerical value of an n-bit exponent can be as large as 2n−1.
The square-and-multiply algorithm uses the binary representation of the exponent.
Repeated squaring generates powers with exponents 1,2,4,8,….
Only powers corresponding to 1 bits in the exponent are multiplied into the accumulator.
Square-and-multiply computes modular exponentiation using at most approximately 2n modular multiplications for an n-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.