ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
NumberTheory.h

The arithmetic of section 17.2 and of exercises 17.1-17.3, on unsigned integers of 64 bits. More...

Files

file  ADS/numeric/NumberTheory.h
 The arithmetic of section 17.2 and of exercises 17.1-17.3, on unsigned integers of 64 bits:

Classes

class  ADS::Montgomery64
 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...

Functions

U64 ADS::gcd (U64 s, U64 m)
 the greatest common divisor \( \gcd( s , m ) \) by the algorithm of Euclid, \( O( \log m ) \) steps
constexpr U64 ADS::binaryGcd (U64 a, U64 b)
 the greatest common divisor of \( a \) and \( b \) by the binary algorithm, with no division; \( \gcd( a , 0 ) = a \)
std::tuple< long long, long long, long long > ADS::extendedEuclid (long long s, long long m)
 the triple \( ( d , p , q ) \) with \( s p + m q = d = \gcd( s , m ) \) (the euclideEsteso() of the book)
long long ADS::modInverse (long long s, long long m)
 the inverse \( p \in [ 0 , m ) \) of \( s \), with \( p s \bmod m = 1 \); a violation of the precondition if \( \gcd( s , m ) \ne 1 \)
constexpr U64 ADS::mulMod61 (U64 a, U64 b)
 the product \( a b \bmod p \), \( p = 2^{61} - 1 \), by four products of 32 bits and the folding \( 2^{61} \equiv 1 \pmod{p} \)
constexpr U64 ADS::mulMod (U64 a, U64 b, U64 m)
 the product \( a b \bmod m \), exact for any \( a \), \( b \) and \( m \): by the product of 64 bits for \( m \le 2^{32} \), by mulMod61() for \( m = 2^{61} - 1 \), and otherwise by the product of 128 bits and one division of two words by one
U64 ADS::powMod (U64 x, U64 v, U64 m)
 the power \( x^v \bmod m \) by repeated squaring, the products of Montgomery when \( m \) is odd
bool ADS::isPrimeTrial (U64 n)
 the primality of \( n \) by the divisions by \( 2 , \ldots , \lfloor \sqrt{n} \rfloor \) (the primo() of the book)
bool ADS::witness (U64 n, U64 b)
 true if \( b \) is a witness that the odd \( n > 2 \) is composite
template<class R>
bool ADS::millerRabin (U64 n, int K, R &rng)
 the primality of \( n \) by the test of Miller and Rabin on \( K \) random bases, wrong with probability at most \( ( 1 / 4 )^K \), and only on a composite \( n \) (the primo() of the book)
bool ADS::isPrime (U64 n)
 the primality of \( n \), exact for every \( n \) of 64 bits
std::vector< U64 > ADS::sieve (U64 n)
 the primes up to \( n \), increasing, by the sieve of Eratosthenes
std::vector< U64 > ADS::factorize (U64 n)
 the prime factors of \( n \), with their multiplicity, increasing, by the divisions by the primes below 4096, the elliptic curves of Lenstra and the rho of Pollard and Brent

Variables

constexpr U64 ADS::kMersenne61 = ( U64( 1 ) << 61 ) - 1
 the Mersenne prime \( 2^{61} - 1 \)

Detailed description

The arithmetic of section 17.2 and of exercises 17.1-17.3, on unsigned integers of 64 bits.

The definitions, the invariants and the costs of the area are in Theory of numerical algorithms.

Function documentation

◆ binaryGcd()

U64 ADS::binaryGcd ( U64 a,
U64 b )
nodiscardconstexpr

the greatest common divisor of \( a \) and \( b \) by the binary algorithm, with no division; \( \gcd( a , 0 ) = a \)

In pseudocode (the binary gcd of Stein, Algorithm 4.5.2B of Knuth, beyond the book), \( \mathrm{tz}( x ) \) the trailing zeros of \( x \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{binaryGcd}( a , b ) \cr 2 & \quad \textbf{if}\ a = 0\ \textbf{then return}\ b;\ \textbf{if}\ b = 0\ \textbf{then return}\ a \cr 3 & \quad k \gets \mathrm{tz}( a \lor b );\ a \gets a / 2^{\mathrm{tz}( a )} \cr 4 & \quad \textbf{repeat}\quad \triangleright\ a \text{ odd} \cr 5 & \qquad b \gets b / 2^{\mathrm{tz}( b )} \cr 6 & \qquad ( a , b ) \gets ( \min( a , b ) , | a - b | ) \cr 7 & \quad \textbf{until}\ b = 0 \cr 8 & \quad \textbf{return}\ 2^k a \end{array}} \]

◆ extendedEuclid()

std::tuple< long long, long long, long long > ADS::extendedEuclid ( long long s,
long long m )
inline

the triple \( ( d , p , q ) \) with \( s p + m q = d = \gcd( s , m ) \) (the euclideEsteso() of the book)

In pseudocode (exercise 17.2):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{extendedEuclid}( s , m )\quad \triangleright\ ( d , p , q ) \text{ with } d = p s + q m \cr 2 & \quad \textbf{if}\ m = 0\ \textbf{then return}\ ( s , 1 , 0 ) \cr 3 & \quad ( d , p' , q' ) \gets \mathrm{extendedEuclid}( m , s \bmod m )\quad \triangleright\ d = p' m + q' ( s - \lfloor s / m \rfloor m ) \cr 4 & \quad \textbf{return}\ ( d , q' , p' - \lfloor s / m \rfloor q' ) \end{array}} \]

Precondition
\( s \ge 0 \) and \( m \ge 0 \)
Postcondition
\( d = p s + q m \), checked modulo \( 2^{64} \)

◆ factorize()

std::vector< U64 > ADS::factorize ( U64 n)
inline

the prime factors of \( n \), with their multiplicity, increasing, by the divisions by the primes below 4096, the elliptic curves of Lenstra and the rho of Pollard and Brent

In pseudocode (Lenstra, Montgomery, Pollard and Brent, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{factorize}( n ) \cr 2 & \quad F \gets \text{the factors of } n \text{ below } 256 \text{ by } \text{division};\ n \gets \text{the cofactor} \cr 3 & \quad \textbf{if}\ n \text{ is composite then the same below } 4096 ;\ T \gets ( n ) \text{ if } n > 1 \cr 4 & \quad \textbf{while}\ T \ne \emptyset\ \textbf{do} \cr 5 & \qquad m \gets \mathrm{pop}( T ) \cr 6 & \qquad \textbf{if}\ m < 4096^2\ \textbf{or}\ \mathrm{isPrime}( m ) \ \textbf{then}\ \text{append } m \text{ to } F \cr 7 & \qquad \textbf{else}\ d \gets \sqrt{m} \text{ if } m \ge 2^{34} \text{ is a square, else } \mathrm{ecm}( m ) \text{ if } m \ge 2^{34} \text{, else } \textbf{nil} \cr 8 & \qquad \quad \textbf{if}\ d = \textbf{nil}\ \textbf{then}\ d \gets \mathrm{rho}( m );\ \mathrm{push}( T , d );\ \mathrm{push}( T , m / d ) \cr 9 & \quad \textbf{return}\ F \text{ sorted} \cr 10 & \textbf{function}\ \mathrm{ecm}( m ) \cr 11 & \quad \textbf{for}\ \sigma \gets 6\ \textbf{to}\ 69\ \textbf{do} \quad \triangleright\ \text{64 curves} \cr 12 & \qquad B_1 \gets 40 \text{ if } \sigma = 6 \text{, else by the bits} \text{ of } m;\ B_2 \gets 25 B_1;\ D \gets 24 , 36 \text{ or } 60 \text{ as } B_1 < 36 , < 60 \text{ or not} \cr 13 & \qquad E , P \gets \text{the curve and the point of Suyama of } \sigma \text{ modulo } m \cr 14 & \qquad Q \gets \mathrm{lcm}( 1 , \ldots , B_1 ) P \text{ by the ladder};\ a \gets Z_Q \cr 15 & \qquad \textbf{for}\ i D \pm j \text{ with a prime in } ( B_1 , B_2 ]\ \textbf{do}\ a \gets a ( X_{iDQ} Z_{jQ} - X_{jQ} Z_{iDQ} ) \cr 16 & \qquad g \gets \mathrm{gcd}( a , m );\ \textbf{if}\ g = m \ \textbf{then}\ g \gets \mathrm{gcd}( Z_Q , m ) \cr 17 & \qquad \textbf{if}\ 1 < g < m\ \textbf{then return}\ g \cr 18 & \quad \textbf{return nil} \cr 19 & \textbf{function}\ \mathrm{rho}( m )\quad \triangleright\ c \gets 1 , 2 , \ldots \text{ until } 1 < g < m \cr 20 & \quad y \gets 2;\ q \gets 1;\ g \gets 1;\ r \gets 1 \cr 21 & \quad \textbf{while}\ g = 1\ \textbf{do} \cr 22 & \qquad x \gets y;\ \text{apply } y \gets y^2 + c \bmod m \text{ } r \text{ times} \cr 23 & \qquad \textbf{for}\ k \gets 0 , 128 , \ldots < r\ \textbf{while}\ g = 1\ \textbf{do} \cr 24 & \qquad\quad \text{at most 128 times: } y \gets y^2 + c;\ q \gets q | x - y | \bmod m \cr 25 & \qquad\quad g \gets \mathrm{gcd}( q , m ) \cr 26 & \qquad r \gets 2 r \cr 27 & \quad \textbf{if}\ g = m\ \textbf{then}\ g \gets \text{the first } \mathrm{gcd}( | x - y | , m ) \ne 1 \text{ of the last batch} \cr 28 & \quad \textbf{return}\ g \end{array}} \]

Precondition
\( n \ge 1 \)
Postcondition
their product is \( n \) and each one is prime (audit)

◆ gcd()

U64 ADS::gcd ( U64 s,
U64 m )
inline

the greatest common divisor \( \gcd( s , m ) \) by the algorithm of Euclid, \( O( \log m ) \) steps

In pseudocode (the solution of exercise 17.2), the invariant \( \gcd( s , m ) = \gcd( m , s \bmod m ) \) kept at each step:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{gcd}( s , m )\quad \triangleright \ \mathrm{gcd}( s , m ) = \mathrm{gcd}( m , s \bmod m ) \cr 2 & \quad \textbf{while}\ m \ne 0\ \textbf{do}\ ( s , m ) \gets ( m , s \bmod m ) \cr 3 & \quad \textbf{return}\ s \end{array}} \]

◆ isPrime()

bool ADS::isPrime ( U64 n)
inline

the primality of \( n \), exact for every \( n \) of 64 bits

In pseudocode (the deterministic bases of Jaeschke, Forisek and Jancina, and Sinclair, beyond the book), \( n - 1 = 2^s d \) with \( d \) odd:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{isPrime}( n ) \cr 2 & \quad \textbf{if}\ n < 256\ \textbf{then return}\ \text{the bit of } n \text{ in a table} \cr 3 & \quad \textbf{if}\ n \text{ is even or a multiple of an odd prime up } \text{to } 211\ \textbf{then return false} \cr 4 & \quad \textbf{if}\ n < 223^2\ \textbf{then return true} \cr 5 & \quad \textbf{if}\ n < 2^{32}\ \textbf{then} \cr 6 & \qquad b \gets \text{the base of the bucket of } n \text{ in the } \text{hashed table} \cr 7 & \qquad \textbf{return}\ n \text{ is a strong probable prime to } \text{the base } b \cr 8 & \quad B \gets \text{the fewest bases with no strong pseudoprime } \text{below } n \cr 9 & \quad \textbf{return}\ n \text{ is a strong probable prime to each } \text{base of } B \end{array}} \]

◆ isPrimeTrial()

bool ADS::isPrimeTrial ( U64 n)
inline

the primality of \( n \) by the divisions by \( 2 , \ldots , \lfloor \sqrt{n} \rfloor \) (the primo() of the book)

In pseudocode (section 17.2):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{isPrimeTrial}( n ) \cr 2 & \quad \textbf{if}\ n < 2\ \textbf{then return false} \cr 3 & \quad \textbf{return}\ \text{whether no } j \text{ with } 2 \le j \le \lfloor \sqrt{n} \rfloor \text{ divides } n\quad \triangleright \ \text{a factor } \le \sqrt{n} \text{ exists if } n \text{ is composite} \end{array}} \]

◆ millerRabin()

template<class R>
bool ADS::millerRabin ( U64 n,
int K,
R & rng )

the primality of \( n \) by the test of Miller and Rabin on \( K \) random bases, wrong with probability at most \( ( 1 / 4 )^K \), and only on a composite \( n \) (the primo() of the book)

In pseudocode (section 17.2):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{millerRabin}( n , K )\quad \triangleright \ \text{a wrong true with probability} \le 4^{-K} \cr 2 & \quad \textbf{if}\ n < 4\ \textbf{then return}\ n \in \{ 2 , 3 \} \cr 3 & \quad \textbf{if}\ n \bmod 2 = 0\ \textbf{then return false} \cr 4 & \quad \textbf{repeat}\ K \text{ times}\ \textbf{if} \ \mathrm{witness}( n , b ) \text{ for a } b \text{ drawn in } [ 2 , n - 2 ]\ \textbf{then return false} \cr 5 & \quad \textbf{return true} \end{array}} \]

Precondition
\( K \ge 0 \)

◆ modInverse()

long long ADS::modInverse ( long long s,
long long m )
inline

the inverse \( p \in [ 0 , m ) \) of \( s \), with \( p s \bmod m = 1 \); a violation of the precondition if \( \gcd( s , m ) \ne 1 \)

In pseudocode (exercise 17.3), from \( s p + m q = 1 \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{modInverse}( s , m ) \cr 2 & \quad ( d , p , q ) \gets \mathrm{extendedEuclid}( s , m )\quad \triangleright\ p s \equiv d = 1 \pmod{m} \cr 3 & \quad \textbf{return}\ \text{the representative of } p \text{ in } [ 0 , m ) \end{array}} \]

Precondition
\( m \ge 1 \), \( s \ge 0 \) and \( \gcd( s , m ) = 1 \)
Postcondition
\( s r \equiv 1 \pmod{m} \)

◆ mulMod()

U64 ADS::mulMod ( U64 a,
U64 b,
U64 m )
nodiscardconstexpr

the product \( a b \bmod m \), exact for any \( a \), \( b \) and \( m \): by the product of 64 bits for \( m \le 2^{32} \), by mulMod61() for \( m = 2^{61} - 1 \), and otherwise by the product of 128 bits and one division of two words by one

In pseudocode (beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{mulMod}( a , b , m ) \cr 2 & \quad a \gets a \bmod m;\ b \gets b \bmod m \cr 3 & \quad \textbf{if}\ m \le 2^{32}\ \textbf{then return}\ a b \bmod m \cr 4 & \quad \textbf{if}\ m = 2^{61} - 1\ \textbf{then return} \ \mathrm{mulMod61}( a , b ) \cr 5 & \quad ( h , l ) \gets \text{the two words of } a b\quad \triangleright\ h < m \cr 6 & \quad \textbf{return}\ ( h 2^{64} + l ) \bmod m\quad \triangleright\ \text{one division of two words by one} \end{array}} \]

Precondition
\( m \ge 1 \)
Postcondition
the result is below \( m \)

◆ mulMod61()

U64 ADS::mulMod61 ( U64 a,
U64 b )
nodiscardconstexpr

the product \( a b \bmod p \), \( p = 2^{61} - 1 \), by four products of 32 bits and the folding \( 2^{61} \equiv 1 \pmod{p} \)

In pseudocode (beyond the book), \( a = a_1 2^{32} + a_0 \) and \( b = b_1 2^{32} + b_0 \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{mulMod61}( a , b ) \cr 2 & \quad h \gets a_1 b_1;\ \mathit{mid} \gets a_1 b_0 + a_0 b_1;\ l \gets a_0 b_0 \cr 3 & \quad s \gets 8 h + \lfloor \mathit{mid} / 2^{29} \rfloor + ( \mathit{mid} \bmod 2^{29} ) 2^{32} + ( l \bmod 2^{61} ) + \lfloor l / 2^{61} \rfloor \cr 4 & \quad s \gets ( s \bmod 2^{61} ) + \lfloor s / 2^{61} \rfloor \cr 5 & \quad \textbf{if}\ s \ge p\ \textbf{then}\ s \gets s - p \cr 6 & \quad \textbf{return}\ s \end{array}} \]

where \( 2^{64} \equiv 8 \) and \( 2^{61} \equiv 1 \) modulo \( p \).

Precondition
\( a < 2^{61} - 1 \) and \( b < 2^{61} - 1 \)
Postcondition
the result is below \( 2^{61} - 1 \)

◆ powMod()

U64 ADS::powMod ( U64 x,
U64 v,
U64 m )
inline

the power \( x^v \bmod m \) by repeated squaring, the products of Montgomery when \( m \) is odd

In pseudocode (exercise 17.1), from the lowest bit of \( v \), each product of Montgomery when \( m \) is odd [see Montgomery64]:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{powMod}( x , v , m )\quad \triangleright \ \text{as } \mathrm{pow} \text{, every product reduced} \cr 2 & \quad r \gets 1 \bmod m;\ x \gets x \bmod m \cr 3 & \quad \textbf{while}\ v \ne 0\ \textbf{do} \cr 4 & \qquad \textbf{if}\ v \bmod 2 = 1\ \textbf{then}\ r \gets \mathrm{mulMod}( r , x , m ) \cr 5 & \qquad ( x , v ) \gets ( \mathrm{mulMod}( x , x , m ) , \lfloor v / 2 \rfloor ) \cr 6 & \quad \textbf{return}\ r \end{array}} \]

Precondition
\( m \ge 1 \)

◆ sieve()

std::vector< U64 > ADS::sieve ( U64 n)
inline

the primes up to \( n \), increasing, by the sieve of Eratosthenes

In pseudocode (beyond the book), on a SET \( S \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{sieve}( n ) \cr 2 & \quad S \gets \{ 2 , \ldots , n \};\ P \gets \text{the empty list} \cr 3 & \quad \textbf{for}\ p \gets 2\ \textbf{to}\ n\ \textbf{do} \cr 4 & \qquad \textbf{if}\ p \in S\ \textbf{then} \cr 5 & \qquad\quad \text{append } p \text{ to } P \cr 6 & \qquad\quad \textbf{for}\ m \gets p^2 , p^2 + p , \ldots \le n \ \textbf{do}\ S \gets S \setminus \{ m \} \cr 7 & \quad \textbf{return}\ P \end{array}} \]

Postcondition
the primes up to \( n \), as isPrimeTrial() finds them (audit)

◆ witness()

bool ADS::witness ( U64 n,
U64 b )
inline

true if \( b \) is a witness that the odd \( n > 2 \) is composite

In pseudocode (section 17.2), \( n - 1 = 2^v m \) with \( m \) odd:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{witness}( n , b )\quad \triangleright\ n - 1 = 2^v m \text{, } m \text{ odd} \cr 2 & \quad \textbf{if}\ \mathrm{gcd}( n , b ) \ne 1 \ \textbf{then return true} \cr 3 & \quad x \gets \mathrm{powMod}( b , m , n ) \cr 4 & \quad \textbf{if}\ x \in \{ 1 , n - 1 \}\ \textbf{then return false} \cr 5 & \quad \textbf{repeat}\ v - 1 \text{ times}\quad \triangleright\ x = b^{2^i m} \text{ after } i \text{ squarings} \cr 6 & \qquad x \gets \mathrm{mulMod}( x , x , n );\ \textbf{if}\ x = n - 1 \ \textbf{then return false} \cr 7 & \quad \textbf{return true}\quad \triangleright\ b \text{ proves } n \text{ composite} \end{array}} \]

Precondition
\( n \ge 3 \) odd and \( 1 \le b < n \)

Variable documentation

◆ kMersenne61

U64 ADS::kMersenne61 = ( U64( 1 ) << 61 ) - 1
inlineconstexpr

the Mersenne prime \( 2^{61} - 1 \)