Note: Due to changes in the Riesel prime template, most of those pages (and related) are not shown properly.This will take some time!Wanna help? Please move any Riesel prime page first, then edit/add the base parameter.
 Topics Register • News • History • How to • Sequences statistics • Template prototypes

# Modular arithmetic

Modular arithmetic is the set of operations that can be done when working modulo N, where N is an integer greater than 1.

We can visualize this arithmetic using a clock. Suppose that the number 12 in the clock is replaced by zero. Then when we have to add an hour, we get, for example: 3+1 = 4, 10+1 = 11, 11+1 = 0. If we have to add three hours we get: 5+3 = 8, 11+3 = 2 (as in 11 AM + 3 hours = 2 PM). We can also subtract: 2-3 = 11 and even multiply: 5×4 = 8 (because 5+5+5+5 = 8). This is arithmetic modulo 12 and the set of numbers representing the hours 0, 1, 2, 3,..., 11 is known as Z/12Z.

When making these modular operations, the equal symbol is not used. We use the congruence symbol ($\displaystyle{ \equiv }$) instead. Note that two numbers A and B are said to be congruent modulo n if A-B is a multiple of n.

The set Z/nZ of numbers modulo n contains the numbers 0, 1, 2, 3, ..., n-2 and n-1. The following operations are defined:

Let A and B be numbers in Z/nZ. Then the addition is defined as:

$\displaystyle{ A + B \equiv C }$ (mod $\displaystyle{ n }$)

where:

$\displaystyle{ C = A + B }$ if $\displaystyle{ A + B \lt n }$
$\displaystyle{ C = A + B - n }$ if $\displaystyle{ A + B \geq n }$

### Modular subtraction

The subtraction is defined as:

$\displaystyle{ A - B \equiv C }$ (mod $\displaystyle{ n }$)

where:

$\displaystyle{ C = A - B }$ if $\displaystyle{ A \geq B }$
$\displaystyle{ C = A - B + n }$ if $\displaystyle{ A \lt B }$

### Modular multiplication

The multiplication is defined as:

$\displaystyle{ A * B \equiv C }$ (mod $\displaystyle{ n }$)

where C is the remainder of the division between A × B and n.

### Modular exponentiation

The exponentiation is defined as:

$\displaystyle{ A^B \equiv C }$ (mod $\displaystyle{ n }$)

where $\displaystyle{ C = A*A*A*A*...*A }$ (B times). An efficient method to perform modular exponentiation is the binary method.

Since reducing modulo $\displaystyle{ n }$ takes a lot of time, Montgomery multiplication is used in this context where only one modular reduction is needed.

### Modular inversion

The inverse is defined as:

$\displaystyle{ A^{-1} \equiv B }$ (mod $\displaystyle{ n }$)

where B is the number such that $\displaystyle{ A * B = 1 }$ (mod $\displaystyle{ n }$).

This operation is only defined when the numbers A and n are coprime, i.e., when gcd(A, n) = 1.

The following algorithm to compute the multiplicative inverse n-1 mod q for n with 0 < n < q, where 0 < n-1 < q is based on the Extended Euclidean algorithm. All variables are integers (some programming languages call them Big Integers).

1. Set i = q, h = n, v = 0, and d = 1.
2. Set t = i DIV h, where DIV is defined as integer division.
3. Set x = h.
4. Set h = i - tx.
5. Set i = x.
6. Set x = d.
7. Set d = v - tx.
8. Set v = x.
9. If h > 0, go to step 2.
10. Let n-1 = v mod q.

When the modulus is a power of 2, there is an easier method. Let the modulus be $\displaystyle{ 2^m }$, let the number to be inverted be $\displaystyle{ N }$ and let $\displaystyle{ k = \log_2 m }$ rounded to the next integer. Then the method is:

1. Set x = 1.
2. Perform k times: Set x = x(2-Nx) mod 2^m

### Modular division

The division is defined as:

$\displaystyle{ A / B \equiv C }$ (mod $\displaystyle{ n }$)

where:

$\displaystyle{ C \equiv A * B^{-1} }$