Modular arithmetic
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 (and the residue class is maximal with respect to that property; integers aren't left out of the residue class unless they would violate the divisibility condition). 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 .Montgomery form
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 (respectively) 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 are 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 anThe REDC algorithm
While the above algorithm is correct, it is slower than multiplication in the standard representation because of the need to multiply by and divide by . ''Montgomery reduction'', also known as REDC, is an algorithm which simultaneously computes the product by and reduces modulo more quickly than the naïve method. Unlike conventional modular reduction, which focuses on making the number smaller than , Montgomery reduction focuses on making the number more divisible by . It does this by adding a small multiple of which is chosen to cancel the residue modulo . Dividing the result by yields a much smaller number. This number is so much smaller that it is nearly the reduction modulo , and computing the reduction modulo requires only a final conditional subtraction. Because all computations are done using only reduction and divisions with respect to , not , the algorithm runs faster than a straightforward modular reduction by division. 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'' ← ((''T'' mod ''R'')''N''′) mod ''R'' ''t'' ← (''T'' + ''mN'') / ''R'' if ''t'' ≥ ''N'' then return else return ''t'' end if end function To see that this algorithm is correct, first observe that is chosen precisely so that is divisible by . A number is divisible by if and only if it is congruent to zero mod , and we have: : Therefore, is an integer. Second, the output is either or , both of which are congruent to , so to prove that the output is congruent to , it suffices to prove that is. Modulo , satisfies: : Therefore, the output has the correct residue class. Third, is in , and therefore is between 0 and . Hence is less than , and because it's an integer, this puts in the range . Therefore, reducing 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 to . The second step sets to . Notice that is 1100, a multiple of 100 as expected. 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 will be . The Montgomery forms of 7 and 15 are and , respectively. Their product 28 is the input to REDC, and since , the assumptions of REDC are satisfied. To run REDC, set to . Then , so . Because , this is the Montgomery form of .Arithmetic in Montgomery form
Many operations of interest modulo 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 may all be done with the standard algorithms. TheMontgomery arithmetic on multiprecision 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 , so performing larger multiplications requires combining several small multiplications. The base is typically 2 for microelectronic applications, 28 for 8-bit firmware, or 232 or 264 for software applications. The REDC algorithm requires products modulo , and typically so that REDC can be used to compute products. However, when is a power of , 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, is stored as an array such that for all and . The algorithm begins with a multiprecision integer and reduces it one word at a time. First an appropriate multiple of is added to make divisible by . Then a multiple of is added to make divisible by , and so on. Eventually is divisible by , and after division by the algorithm is in the same place as REDC was after the computation of . function MultiPrecisionREDC is Input: Integer ''N'' with , stored as an array of ''p'' words, Integer , --thus, ''r'' = ''log''''B'' ''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 ''(extra carry word)'' 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'' 'i'' + ''j''← ''c'' ← end for for do ''--loop3- Continue carrying'' ''x'' ← ''T'' 'i'' + ''j''← ''c'' ← end for end for for do ''S'' 'i''← ''T'' 'i'' + ''r'' 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 loop, is chosen so that is divisible by . Then is added to . Because this quantity is zero mod , adding it does not affect the value of . If denotes the value of computed in the th iteration of the loop, then the algorithm sets to . Because MultiPrecisionREDC and REDC produce the same output, this sum is the same as the choice of that the REDC algorithm would make. The last word of , (and consequently ), is used only to hold a carry, 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 is smaller than the number of bits of . 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 (plus a carry bit). As an example, let , , and . Suppose that and . The Montgomery representations of and are and . Compute . The initial input to MultiPrecisionREDC will beSide-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 always subtract something, either the modulus or zero) to make it resistant.See also
* Barrett reductionReferences
External links
* {{cite document , title=Theory and practice of Montgomery multiplication , author=Henry S. Warren, Jr. , date=July 2012, citeseerx=10.1.1.450.6124 Computer arithmetic Cryptographic algorithms Modular arithmetic