ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::Montgomery64 Class Reference

the arithmetic modulo an odd \( n \) of 64 bits in the form of Montgomery, \( x R \bmod n \) with \( R = 2^{64} \); the numbers in the form are below \( n \) More...

#include <ADS/numeric/NumberTheory.h>

Public member functions

constexpr Montgomery64 (U64 n)
 the arithmetic modulo \( n \), with one division of 128 bits for \( R^2 \bmod n \)
constexpr U64 modulus (void) const
 the modulus \( n \)
constexpr U64 to (U64 x) const
 the form \( x R \bmod n \) of \( x \), as \( \mathrm{mul}( x \bmod n , R^2 \bmod n ) \)
constexpr U64 from (U64 x) const
 the number \( x R^{-1} \bmod n \) whose form is \( x \), as \( \mathrm{REDC}( x ) \)
constexpr U64 one (void) const
 the form \( R \bmod n \) of 1
constexpr U64 mul (U64 a, U64 b) const
 the form \( a b R^{-1} \bmod n \) of the product of the numbers of the forms \( a \) and \( b \)
constexpr U64 pow (U64 x, U64 v) const
 the form of \( x^v \), \( x \) a form

Detailed description

the arithmetic modulo an odd \( n \) of 64 bits in the form of Montgomery, \( x R \bmod n \) with \( R = 2^{64} \); the numbers in the form are below \( n \)

Constructor and destructor documentation

◆ Montgomery64()

ADS::Montgomery64::Montgomery64 ( U64 n)
explicitconstexpr

the arithmetic modulo \( n \), with one division of 128 bits for \( R^2 \bmod n \)

In pseudocode (Montgomery, beyond the book), the inverse by Newton:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{Montgomery64}( n ) \cr 2 & \quad x \gets 3 n \oplus 2\quad \triangleright\ n^{-1} \bmod 2^5 \cr 3 & \quad \textbf{for}\ i \gets 1\ \textbf{to}\ 4\ \textbf{do}\ x \gets x ( 2 - n x ) \bmod 2^{64} \cr 4 & \quad \mathit{inv} \gets x;\ \mathit{one} \gets R \bmod n; \ \mathit{r2} \gets R^2 \bmod n \end{array}} \]

Precondition
\( n \) is odd
Postcondition
\( n \cdot \mathit{inv} \equiv 1 \pmod{2^{64}} \)

Member function documentation

◆ from()

U64 ADS::Montgomery64::from ( U64 x) const
inlinenodiscardconstexpr

the number \( x R^{-1} \bmod n \) whose form is \( x \), as \( \mathrm{REDC}( x ) \)

Precondition
\( x < n \)

◆ modulus()

U64 ADS::Montgomery64::modulus ( void ) const
inlinenodiscardconstexpr

the modulus \( n \)

◆ mul()

U64 ADS::Montgomery64::mul ( U64 a,
U64 b ) const
nodiscardconstexpr

the form \( a b R^{-1} \bmod n \) of the product of the numbers of the forms \( a \) and \( b \)

In pseudocode (Montgomery, beyond the book), \( \mathrm{REDC} \) of the product of 128 bits:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{mul}( a , b ) \cr 2 & \quad ( h , l ) \gets \text{the high and the low half of } a b \cr 3 & \quad \textbf{return}\ \mathrm{reduce}( h , l ) \end{array}} \]

Precondition
\( a < n \) and \( b < n \)

◆ one()

U64 ADS::Montgomery64::one ( void ) const
inlinenodiscardconstexpr

the form \( R \bmod n \) of 1

◆ pow()

U64 ADS::Montgomery64::pow ( U64 x,
U64 v ) const
nodiscardconstexpr

the form of \( x^v \), \( x \) a form

In pseudocode (exercise 17.1 in the forms), from the lowest bit of \( v \), the bit selecting the product with no branch:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{pow}( x , v )\quad \triangleright\ r \cdot x^v \text{ stays the result} \cr 2 & \quad r \gets \mathrm{one}() \cr 3 & \quad \textbf{while}\ v \ne 0\ \textbf{do} \cr 4 & \qquad \textbf{if}\ v \bmod 2 = 1\ \textbf{then}\ r \gets \mathrm{mul}( r , x ) \cr 5 & \qquad ( x , v ) \gets ( \mathrm{mul}( x , x ) , \lfloor v / 2 \rfloor ) \cr 6 & \quad \textbf{return}\ r \end{array}} \]

Precondition
\( x < n \)

◆ to()

U64 ADS::Montgomery64::to ( U64 x) const
nodiscardconstexpr

the form \( x R \bmod n \) of \( x \), as \( \mathrm{mul}( x \bmod n , R^2 \bmod n ) \)


The documentation for this class was generated from the following file: