Montgomery reduction: Difference between revisions

Content added Content deleted
m (small fixes)
(cleanup task description)
Line 1: Line 1:
{{draft task}}
{{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
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.
* let M be a positive integer, R and T integers such that R>m, gcd(m, R) = 10<= T < mR
* Let M be a positive integer, and R and T integers such that R > m, gcd(m, R) = 1, 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 'normal' paper arithmetic, b = 2 for computer implementations] and n = number of digits in base m
* Montgomery reduction calculates T*(R<sup>-1</sup>) mod m, without having to divide by 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.
* 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 Montgomery Reduction
Wikipedia link: [[Wikipedia:Montgomery reduction|Montgomery reduction]].
[http://en.wikipedia.org/wiki/Montgomery_reduction]


See Handbook of Applied Cryptography for brief introduction to theory and numerical example in radix 10
See Handbook of Applied Cryptography for brief introduction to theory and numerical example in radix 10.


Algorithm:
Algorithm:
Line 19: Line 17:
if A >= m, A ← A - m
if A >= m, A ← A - m
Return (A)
Return (A)

=={{header|C++}}==
=={{header|C++}}==
<lang cpp>#include<iostream>
<lang cpp>#include<iostream>