Fast Modular Multiplication
A modular multiplication requires a remainder operation, which is a slow operation if the modulus is a general integer. For example, contemporary processors can multiply two 64-bit integers, producing a 128-bit result, with a latency of 3 or 4 clock cycles. But, dividing a 128-bit integer by a 64-bit integer, producing a 64-bit quotient and a 64-bit remainder, is considerably slower (tens of clock cycles).
If the modulus is a power of two, say ,the remainder operation is very fast; the remainder is just the last n bits of the number being remaindered. In 1985, Peter Montgomery came up with a beautiful way to explore this to efficiently perform general remaindering operations without performing an expensive division.
The Greatest Common Divisor
Let be the -th prime number, so that , and so on,
Each positive integer can be factored into prime factors in a unique way (this is the fundamental theorem of arithmetic).
Let , where is the number of times divides . Since is a finite number, almost all of the values will be zero.
Likewise of , let .
Then and
If then and are said to be relatively prime (or coprime).
The greatest common division can be generalized to a polynomial with integer coefficients!
Algorithm
Assume that and that . Then:
, and so . Thus, by exchanging with if necessary, we may assume that .
as any positive integer divides 0 we have for . The mathematicians say that , and so we can say that as long as .
If the . We can keep subtracting from (the updated) until it becomes smaller than , and so .
These observations give rise to the following so-called Euclid's algorithm (coded in C
, but it can easily be translated to another programming language):
The GNU MP library has a function, mpz_gcd
, for this; pari-gp
does this with the gcd function.
Last updated