Jump to content

Montgomery reduction: Difference between revisions

cleanup task description
m (small fixes)
(cleanup task description)
Line 1:
{{draft task}}
This is sample code in C++ for implementing Montgomery Reduction algorithm as explained in "Handbook of Applied Cryptography, Section 14.3.2, page 600. Montgomery reduction calculates T*(R<sup>-1</sup>) mod m, without having to divide by m.
* letLet M be a positive integer, and R and T integers such that R > m, gcd(m, R) = 101, and 0 <= T < mR.
* R is usually chosen as b<sup>n</sup>, where b = base (radix) in which the numbers in the calculation as represented in [b = 10 in our 'normal' paper arithmeticsarithmetic, b = 2 for computer implementations] and n = number of bitsdigits in modulusbase m
* Montgomery reduction calculates T*(R<sup>-1</sup>) mod m, without having to divide by m
* theThe numbers m(n digits long), T (2n digits long), R, b, n are known entities, a number m' (represented m_dash in code) = -m<sup>-1</sup> mod b is precomputed.
* R is usually chosen as b<sup>n</sup>, where b = base (radix) in which the numbers in the calculation as represented in [b = 10 in our normal paper arithmetics, b = 2 for computer implementations] and n = number of bits in modulus m
* the numbers m(n digits long), T (2n digits long), R, b, n are known entities, a number m' (represented m_dash in code) = -m<sup>-1</sup> mod b is precomputed
 
Wikipedia link: for[[Wikipedia:Montgomery reduction|Montgomery Reductionreduction]].
[http://en.wikipedia.org/wiki/Montgomery_reduction]
 
See Handbook of Applied Cryptography for brief introduction to theory and numerical example in radix 10.
 
Algorithm:
Line 19 ⟶ 17:
if A >= m, A ← A - m
Return (A)
 
=={{header|C++}}==
<lang cpp>#include<iostream>
Cookies help us deliver our services. By using our services, you agree to our use of cookies.