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

# Montgomery multiplication

Montgomery multiplication is an algorithm used to perform multiple precision modular multiplication quickly by replacing division (which is a slow operation) by multiplications.

In this article the modular multiplication to be done will be $\displaystyle{ ab\,\equiv \,c\,\pmod{m} }$. We will also assume that $\displaystyle{ m\,\lt \,2^n }$.

Before performing the actual multiplication, both factors must be converted to Montgomery representation. The algorithm will find the Montgomery representation of the product which will be converted back to the actual product. All these steps are slower than a naive modular multiplication, using a multiplication followed by a division, but when several multiplications and additions can be done without converting to and from Montgomery representation (for example: modular exponentiation, ECM, p-1, etc.) this method is really fast.

The Montgomery representation of a number $\displaystyle{ a }$ is the value

$\displaystyle{ a'=2^n\,a\,\bmod{m} }$.

From this formula we can see that $\displaystyle{ p+q\,\equiv \, r }$ implies $\displaystyle{ p'+q'\,\equiv \, r' }$.

The Montgomery multiplication computes $\displaystyle{ c'\,=\,2^{-n}\,a'b'\bmod{m} }$. Notice that $\displaystyle{ pq\,\equiv \, r }$ implies $\displaystyle{ p'q' \equiv \, r' }$. The sections below show how to compute the Montgomery multipication.

In order to convert back from Montgomery representation to normal, just perform a Montgomery multiplication using the number 1 as the second factor.

Depending on the length of the factors, there are two ways the Montgomery multiplication can be applied.

## Large factors

When the factors are very large and the Karatsuba multiplication or Fourier transform multiplication is faster than long multiplication, the division implied in the modular multiplication can be replaced by two multiplications.

First the negative of the modular inverse of $\displaystyle{ m \,\pmod{2^n} }$ is computed. Let's call this number $\displaystyle{ R }$. Notice that this assumes that $\displaystyle{ m }$ is odd (which is almost always true).\n\nNow we perform the actual Montgomery multiplication $\displaystyle{ c' \,\equiv \,2^{-n}a'b' }$.

• $\displaystyle{ x = a'b' }$
• $\displaystyle{ s = (x \,\bmod{2^n})R\,\bmod{2^n} }$
• $\displaystyle{ t = (x + sm)/(2^n) }$
• if $\displaystyle{ t \,\lt \, m }$ then $\displaystyle{ c' \,=\, t }$ else $\displaystyle{ c' \,=\, t - m }$.

### Example

Let m=5 , n=3 , a = b = 3.
We get: a' = b' = 3 × 8 mod 5 = 4
R = -5-1 mod 8 = 3
x = a' b' = 4 × 4 = 16
s = (16 mod 8) × 3 mod 8 = 0
t = ((x + sm)/(2n) = (16 + 0 × 5) / 8 = 2
c' = 2.

This is right since c = 9 implies that c' = 9 × 8 mod 5 = 2.

## Small factors

When the factors are not large, so the long multiplication is faster, the division can be replaced by a little more than one multiplication.

In this method we will work with the multiprecision factors directly as two arrays of integers.

Let $\displaystyle{ a' = a'[0] \,+\, a'[1] * 2^k \,+\, a'[2] * 2^{2k} \,+\, ... \,+\, a'[s-1] * 2^{(s-1)k} }$.

and similarly for b' and c'. The number $\displaystyle{ k }$ is the number of bits of the integer, and $\displaystyle{ n = ks }$.

Let $\displaystyle{ R }$ be the modular inverse of $\displaystyle{ m\,\pmod{2^k} }$.

• $\displaystyle{ T = 0 }$
• for i from 0 to s-1 perform the following:
• $\displaystyle{ T = T + a'[i] * b' * 2^{ki} }$
• $\displaystyle{ h = T[i] * R \bmod{2^k} }$
• $\displaystyle{ T = T + h * m * 2^{ki} }$
• $\displaystyle{ T = T/2^n }$
• if $\displaystyle{ T \,\lt \, m }$ then $\displaystyle{ c' \,=\, T }$ else $\displaystyle{ c' \,=\, T-m }$