Montgomery modular multiplication


In modular arithmetic computation, Montgomery modular multiplication, more commonly referred to as Montgomery multiplication, is a method for performing fast modular multiplication. It was introduced in 1985 by the American mathematician Peter L. Montgomery.
Given two integers and and modulus, the classical modular multiplication algorithm computes the double-width product, and then performs a division, subtracting multiples of to cancel out the unwanted high bits until the remainder is once again less than. Montgomery reduction instead adds multiples of to cancel out the low bits until the result is a multiple of a convenient constant. Then the low bits are discarded, producing a result less than. One conditional final subtraction reduces this to less than. This procedure has a better computational complexity than standard division algorithms, since it avoids the quotient digit estimation and correction that they need.
The result is the desired product divided by, which is less inconvenient than it might appear. To multiply and, they are first converted to Montgomery form or Montgomery representation and. When multiplied, these produce, and the following Montgomery reduction produces, the Montgomery form of the desired product.
Converting to and from Montgomery form makes this slower than the conventional or Barrett reduction algorithms for a single multiply. However, when performing many multiplications in a row, as in modular exponentiation, intermediate results can be left in Montgomery form, and the initial and final conversions become a negligible fraction of the overall computation. Many important cryptosystems such as RSA and Diffie–Hellman key exchange are based on arithmetic operations modulo a large number, and for these cryptosystems, the computation by Montgomery multiplication is faster than the available alternatives.

Modular arithmetic and Montgomery form

Let denote a positive integer modulus. The quotient ring consists of residue classes modulo, that is, its elements are sets of the form
where ranges across the integers. Each residue class is a set of integers such that the difference of any two integers in the set is divisible by . The residue class corresponding to is denoted. Equality of residue classes is called congruence and is denoted
Storing an entire residue class on a computer is impossible because the residue class has infinitely many elements. Instead, residue classes are stored as representatives. Conventionally, these representatives are the integers for which. If is an integer, then the representative of is written. When writing congruences, it is common to identify an integer with the residue class it represents. With this convention, the above equality is written.
Arithmetic on residue classes is done by first performing integer arithmetic on their representatives. The output of the integer operation determines a residue class, and the output of the modular operation is determined by computing the residue class's representative. For example, if, then the sum of the residue classes and is computed by finding the integer sum, then determining, the integer between 0 and 16 whose difference with 22 is a multiple of 17. In this case, that integer is 5, so.
If and are integers in the range, then their sum is in the range and their difference is in the range, so determining the representative in requires at most one subtraction or addition of. However, the product is in the range. Storing the intermediate integer product requires twice as many bits as either or, and efficiently determining the representative in requires division. Mathematically, the integer between 0 and that is congruent to can be expressed by applying the Euclidean division theorem:
where is the quotient and, the remainder, is in the interval. The remainder is. Determining can be done by computing, then subtracting from. For example, the product is determined by computing, dividing, and subtracting.
Because the computation of requires division, it is undesirably expensive on most computer hardware. Montgomery form is a different way of expressing the elements of the ring in which modular products can be computed without expensive divisions. While divisions are still necessary, they can be done with respect to a different divisor. This divisor can be chosen to be a power of two, for which division can be replaced by shifting, or a whole number of machine words, for which division can be replaced by omitting words. These divisions are fast, so most of the cost of computing modular products using Montgomery form is the cost of computing ordinary products.
The auxiliary modulus must be a positive integer such that. For computational purposes it is also necessary that division and reduction modulo R be inexpensive, and the modulus is not useful for modular multiplication unless. The Montgomery form of the residue class with respect to is, that is, it is the representative of the residue class. For example, suppose that and that. The Montgomery forms of 3, 5, 7, and 15 are,,, and.
Addition and subtraction in Montgomery form are the same as ordinary modular addition and subtraction because of the distributive law:
This is a consequence of the fact that, because, multiplication by is an isomorphism on the additive group. For example,, which in Montgomery form becomes.
Multiplication in Montgomery form, however, is seemingly more complicated. The usual product of and does not represent the product of and because it has an extra factor of :
Computing products in Montgomery form requires removing the extra factor of. While division by is cheap, the intermediate product is not divisible by because the modulo operation has destroyed that property. So for instance, the product of the Montgomery forms of 7 and 15 modulo 17 is the product of 3 and 4, which is 12. Since 12 is not divisible by 100, additional effort is required to remove the extra factor of.
Removing the extra factor of can be done by multiplying by an integer such that, that is, by an R′ whose residue class is the modular inverse of mod. Then, working modulo,
The integer exists because of the assumption that and are coprime. It can be constructed using the extended Euclidean algorithm. The extended Euclidean algorithm efficiently determines integers and that satisfy Bézout's identity:
,, and:
This shows that it is possible to do multiplication in Montgomery form. A straightforward algorithm to multiply numbers in Montgomery form is therefore to multiply,, and as integers and reduce modulo.
For example, to multiply 7 and 15 modulo 17 in Montgomery form, again with, compute the product of 3 and 4 to get 12 as above. The extended Euclidean algorithm implies that, so. Multiply 12 by 8 to get 96 and reduce modulo 17 to get 11. This is the Montgomery form of 3, as expected.

The REDC algorithm

While the above algorithm is correct, it is slower than multiplication in the standard representation because of the need to multiply by R′ and divide by N. Montgomery reduction, also known as REDC, is an algorithm that simultaneously computes the product by R′ and reduces modulo N more quickly than the naive method. The speed is because all computations are done using only reduction and divisions with respect to R, not N:
function REDC is
input: Integers R and N with,
Integer N′ in such that,
Integer T in the range
output: Integer S in the range such that
m ← mod R
t ← / R
if tN then
return
else
return t
end if
end function
To see that this algorithm is correct, first observe that m is chosen precisely so that is divisible by R. A number is divisible by R if and only if it is congruent to zero mod R, and we have:
Therefore, t is an integer. Second, the output is either t or, both of which are congruent to t mod N, so to prove that the output is congruent to, it suffices to prove that t is. Modulo N, t satisfies:
Therefore, the output has the correct residue class. Third, m is in, and therefore is between 0 and. Hence t is less than, and because it's an integer, this puts t in the range. Therefore, reducing t into the desired range requires at most a single subtraction, so the algorithm's output lies in the correct range.
To use REDC to compute the product of 7 and 15 modulo 17, first convert to Montgomery form and multiply as integers to get 12 as above. Then apply REDC with,,, and. The first step sets m to. The second step sets t to. Notice that is 1100, a multiple of 100 as expected. t is set to 11, which is less than 17, so the final result is 11, which agrees with the computation of the previous section.
As another example, consider the product but with. Using the extended Euclidean algorithm, compute, so N′ will be. The Montgomery forms of 7 and 15 are and, respectively. Their product 28 is the input T to REDC, and since, the assumptions of REDC are satisfied. To run REDC, set m to. Then, so. Because, this is the Montgomery form of.

Arithmetic in Montgomery form

Many operations of interest modulo N can be expressed equally well in Montgomery form. Addition, subtraction, negation, comparison for equality, multiplication by an integer not in Montgomery form, and greatest common divisors with N may all be done with the standard algorithms. The Jacobi symbol can be calculated as as long as is stored.
When, most other arithmetic operations can be expressed in terms of REDC. This assumption implies that the product of two representatives mod N is less than RN, the exact hypothesis necessary for REDC to generate correct output. In particular, the product of and is. The combined operation of multiplication and REDC is often called Montgomery multiplication.
Conversion into Montgomery form is done by computing. Conversion out of Montgomery form is done by computing. The modular inverse of is. Modular exponentiation can be done using exponentiation by squaring by initializing the initial product to the Montgomery representation of 1, that is, to, and by replacing the multiply and square steps by Montgomery multiplies.
Performing these operations requires knowing at least and. When R is a power of a small positive integer b, can be computed by Hensel's lemma: The inverse of N modulo b is computed by a naive algorithm, and Hensel's lemma is used repeatedly to find the inverse modulo higher and higher powers of b, stopping when the inverse modulo R is known; is the negation of this inverse. The constants and can be generated as and as. The fundamental operation is to compute REDC of a product. When standalone REDC is needed, it can be computed as REDC of a product with. The only place where a direct reduction modulo N is necessary is in the precomputation of.

Montgomery arithmetic on multiprecision (variable-radix) integers

Most cryptographic applications require numbers that are hundreds or even thousands of bits long. Such numbers are too large to be stored in a single machine word. Typically, the hardware performs multiplication mod some base B, so performing larger multiplications requires combining several small multiplications. The base B is typically 2 for microelectronic applications, 28 for 8-bit firmware, or 232 or 264 for software applications.
The REDC algorithm requires products modulo R, and typically so that REDC can be used to compute products. However, when R is a power of B, there is a variant of REDC which requires products only of machine word sized integers. Suppose that positive multi-precision integers are stored little endian, that is, x is stored as an array such that for all i and. The algorithm begins with a multiprecision integer T and reduces it one word at a time. First an appropriate multiple of N is added to make T divisible by B. Then a multiple of N is added to make T divisible by B2, and so on. Eventually T is divisible by R, and after division by R the algorithm is in the same place as REDC was after the computation of t.
function MultiPrecisionREDC is
Input: Integer N with, stored as an array of p words,
Integer, --thus, r = logB R
Integer N′ in such that,
Integer T in the range, stored as an array of words.
Output: Integer S in such that, stored as an array of p words.
Set '
for do
--loop1- Make T divisible by
c ← 0
m
for do
--loop2- Add the low word of and the carry from earlier, and find the new carry
x
T
c
end for
for do
--loop3- Continue carrying
x
T
c
end for
end for
for do
ST
end for
if then
return
else
return
end if
end function
The final comparison and subtraction is done by the standard algorithms.
The above algorithm is correct for essentially the same reasons that REDC is correct. Each time through the i loop, m is chosen so that is divisible by B. Then mNBi is added to T. Because this quantity is zero mod N, adding it does not affect the value of. If mi denotes the value of m computed in the ith iteration of the loop, then the algorithm sets S to. Because MultiPrecisionREDC and REDC produce the same output, this sum is the same as the choice of m that the REDC algorithm would make.
The last word of T, , is used only to hold a carry, and as the initial reduction result is bound to a result in the range of. It follows that this extra carry word can be avoided completely if it is known in advance that. On a typical binary implementation, this is equivalent to saying that this carry word can be avoided if the number of bits of N is smaller than the number of bits of R. Otherwise, the carry will be either zero or one. Depending upon the processor, it may be possible to store this word as a carry flag instead of a full-sized word.
It is possible to combine multiprecision multiplication and REDC into a single algorithm. This combined algorithm is usually called Montgomery multiplication. Several different implementations are described by Koç, Acar, and Kaliski. The algorithm may use as little as words of storage.
As an example, let,, and. Suppose that and. The Montgomery representations of a and b are and. Compute. The initial input T to MultiPrecisionREDC will be . The number N will be represented as . The extended Euclidean algorithm says that, so N′ will be 7.
i ← 0
m ←
j T c
- ------- -
0 0485670 2
'
1 0485670 2
2 0485670 2
3 0487670 0 '
4 0487670 0
5 0487670 0
6 0487670 0
i ← 1
m ←
j T c
- ------- -
0 0087670 6
'
1 0067670 8
2 0067670 8
3 0067470 1 '
4 0067480 0
5 0067480 0
i ← 2
m ←
j T c
- ------- -
0 0007480 2
'
1 0007480 2
2 0007480 2
3 0007400 1
4 0007401 0
Therefore, before the final comparison and subtraction,. The final subtraction yields the number 50. Since the Montgomery representation of is, this is the expected result.
When working in base 2, determining the correct m at each stage is particularly easy: If the current working bit is even, then m is zero and if it's odd, then m is one. Furthermore, because each step of MultiPrecisionREDC requires knowing only the lowest bit, Montgomery multiplication can be easily combined with a carry-save adder.

Side-channel attacks

Because Montgomery reduction avoids the correction steps required in conventional division when quotient digit estimates are inaccurate, it is mostly free of the conditional branches which are the primary targets of timing and power side-channel attacks; the sequence of instructions executed is independent of the input operand values. The only exception is the final conditional subtraction of the modulus, but it is easily modified to make it resistant. It is of course necessary to ensure that the exponentiation algorithm built around the multiplication primitive is also resistant.