The arithmetic of section 17.2 and of exercises 17.1-17.3, on unsigned integers of 64 bits:
More...
|
| 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
|
The arithmetic of section 17.2 and of exercises 17.1-17.3, on unsigned integers of 64 bits:
- gcd(), of Euclid, \( \gcd( s , m ) = \gcd( m , s \bmod m ) \), \( O( \log m ) \) steps; extendedEuclid() also gives \( p \) and \( q \) with \( s p + m q = \gcd( s , m ) \): from \( ( d , p' , q' ) \) of \( ( m , s \bmod m ) \), \( ( d , q' , p' - \lfloor s / m \rfloor q' ) \) (the solution of exercise 17.2 in the text has the two swapped);
- modInverse(), the \( p \) with \( p s \bmod m = 1 \) when \( \gcd( s , m ) = 1 \) (exercise 17.3);
- mulMod(), \( a b \bmod m \) exact for every modulus of 64 bits, the modular product of all of ADS: the product of 64 bits for \( m \le 2^{32} \); mulMod61() for the Mersenne prime \( p = 2^{61} - 1 \) of the universal hashing [see UniversalHashing.h], four products of 32 bits folded by \( 2^{61} \equiv 1 \pmod{p} \), faster than a division of 128 bits; otherwise the product of two words and the division of two words by one of Wide.h;
- powMod(), \( x^v \bmod m \) by repeated squaring, \( O( \log v ) \) multiplications (exercise 17.1), \( \lfloor \log_2 v \rfloor + 1 \) squarings and as many products, each one of Montgomery when \( m \) is odd (see Montgomery64 below): the products and the squarings are two chains that do not depend on each other (from the lowest bit up), and the bit selects the product with no branch. Below \( 2^{62} \) the forms are kept in \( [ 0 , 2 m ) \) (the lazy reduction): for \( a , b < 2 m \), \( T = a b < 4 m^2 < m R \), so that \( ( T - q m ) / R + m \in ( 0 , 2 m ) \) and the comparison that ends each product is spared; below \( 2^{32} \) the forms are of \( R = 2^{32} \), the products of 64 bits, the form of \( x \) by one division of 64 by 32 bits, the trailing zeros of \( v \) by squarings and the power from the lowest bit 1 of the odd part, so that the form of 1 (a division more) is never needed;
- binaryGcd(), beyond the book, the binary gcd of Stein (Algorithm 4.5.2B of Knuth): with \( k \) the trailing zeros of \( a \lor b \), \( \gcd( a , b ) = 2^k \gcd( a' , b' ) \) for the odd parts, and \( \gcd( a , b ) = \gcd( \min( a , b ) , | a - b | ) \) with the trailing zeros of the even difference shifted out, by one instruction; each step halves at least \( \max( a , b ) \), hence at most \( \log_2 a + \log_2 b + 2 \) steps and no division, the minimum and the difference by conditional moves, with no branch to mispredict; on x86-64 the trailing zeros are those of \( b - a \) (equal to those of \( | a - b | \)), so that the count starts with the difference and the step is a subtraction, a count and a shift, the two conditional moves beside them;
- Montgomery64, beyond the book, the arithmetic modulo an odd \( n \) of 64 bits in the form \( \bar{x} = x R \bmod n \), \( R = 2^{64} \): the product is \( \mathrm{REDC}( \bar{a} \bar{b} ) = \bar{a} \bar{b} R^{-1} \bmod n \), where for \( T = h R + l < n R \) and \( q = l n^{-1} \bmod R \) the low half of \( q n \) equals \( l \), so that \( ( T - q n ) / R = h - \lfloor q n / R \rfloor \in ( - n , n ) \) and one addition of \( n \) when negative ends it: two products of 128 bits and one of 64 instead of a division of 128 bits, exact for every odd \( n \); \( n^{-1} \bmod 2^{64} \) comes from \( x_0 = 3 n \oplus 2 \), right on 5 bits, by 4 steps of Newton \( x_{i+1} = x_i ( 2 - n x_i ) \), each doubling the bits that are right, and \( R \bmod n \) and \( R^2 \bmod n \) by one division of 128 by 64 bits each;
- isPrimeTrial(), the divisions up to \( \sqrt{n} \), \( O( 2^{d/2} ) \) for \( n \) of \( d \) bits;
- witness(), \( b \) is a witness that \( n \) is composite if \( \gcd( n , b ) \neq 1 \) or, with \( n - 1 = 2^v m \) and \( m \) odd, neither \( b^m \bmod n = 1 \) nor \( b^{2^i m} \bmod n = n - 1 \) for some \( 0 \le i < v \); millerRabin() tries \( K \) random bases in \( [ 2 , n - 2 ] \), at least \( 3 / 4 \) of which are witnesses for a composite \( n \), and errs with probability at most \( ( 1 / 4 )^K \), only by saying prime;
- isPrime(), beyond the book, exact on 64 bits: first the divisions by the 46 odd primes up to 211, by one product each with no branch, all at once on vectors (for the odd \( p \), \( p \mid n \) if and only if \( n p^{-1} \bmod 2^{64} \le \lfloor ( 2^{64} - 1 ) / p \rfloor \), since \( x \mapsto x p^{-1} \) is a bijection that sends the multiples of \( p \) onto \( [ 0 , \lfloor ( 2^{64} - 1 ) / p \rfloor ] \)), which leave about \( \prod_{3 \le p \le 211} ( 1 - 1 / p ) \approx 0.21 \) of the odd numbers; then the test of Miller and Rabin on the fewest bases known to have no strong pseudoprime below \( n \): below \( 2^{32} \) one base read in a table of 1024 at the hash \( \lfloor ( n \cdot 2654435761 \bmod 2^{32} ) / 2^{22} \rfloor \) (the hashed bases of Forisek and Jancina), the least base, at most 67, of which no odd composite of the bucket with no prime factor up to 211 is a strong pseudoprime (computed for ADS by the exhaustive search over the \( 2^{31} \) odd numbers), in an arithmetic of Montgomery of 32 bits that needs only \( n^{-1} \bmod 2^{32} \) (the form of \( b \) by one division, the power from the lowest bit of \( d \), the comparisons after one reduction), up to the 7 bases of Sinclair for every \( n \) of 64 bits. The \( k \) bases run in lockstep, \( k \) chains of products that the processor overlaps, so that 2 cost about as much as 1; with 3 or more, the exponent goes by windows of 4 bits with a table of \( b^0 , \dots , b^{15} \), \( \lceil \log_2 d \rceil + \lceil \log_2 d / 4 \rceil + 14 \) products per base instead of up to \( 2 \lceil \log_2 d \rceil \). The arithmetic is that of Montgomery of 32 bits below \( 2^{32} \) (\( R = 2^{32} \), its constants by divisions of 32 bits), the lazy one below \( 2^{62} \), and Montgomery64 above;
factorize(), beyond the book, the prime factors of a word: the divisions by the primes below 256, each a product by \( p^{-1} \bmod 2^{64} \) and a comparison (as in isPrime()), then, if the cofactor is composite (isPrime() decides), by those below 4096, 16 at a time; a composite cofactor \( m \) is then split, until all the factors are primes, by
- its root, if \( m = r^2 \);
- from \( 2^{34} \) on, the elliptic curves of Lenstra: on a curve \( E \) modulo \( m \), the multiple \( k P \) of a point is the neutral element modulo a prime \( p \mid m \) when the order of \( E( \mathbb{F}_p ) \), a random integer in \( [ p + 1 - 2 \sqrt{p} , p + 1 + 2 \sqrt{p} ] \), divides \( k \), and then \( p \) divides its coordinate \( Z \), which \( \gcd( Z , m ) \) finds. The curves are those of Montgomery, \( B y^2 = x^3 + A x^2 + x \), on the coordinates \( ( X : Z ) \) alone: the double by 2 squares and 3 products, the sum \( P + Q \) from \( P - Q \) by 2 squares and 4 products (3 if \( P - Q = ( x : 1 ) \)), in the Montgomery arithmetic of \( m \), the sums and the differences that only enter a product left unreduced in \( [ 0 , 4 m ) \) below \( 2^{60} \); the parameter \( \sigma \) of Suyama, \( u = \sigma^2 - 5 \), \( v = 4 \sigma \), \( x_0 = u^3 / v^3 \) and \( ( A + 2 ) / 4 = ( v - u )^3 ( 3 u + v ) / ( 16 u^3 v ) \) (by one inverse), gives an order multiple of 12. The stage 1 computes \( Q = k P \), \( k = \mathrm{lcm}( 1 , \ldots , B_1 ) \), by the ladder of Montgomery on the words of \( k \), computed at compile time, a sum and a double per bit (10 products), the swaps by masks with no branch; the stage 2 finds one more prime \( q \in ( B_1 , B_2 ] \) of the order, with \( q = i D \pm j \), \( D = 60 \) and \( j < D / 2 \) prime to \( D \): \( q Q \) is neutral iff \( X_{iD} Z_j - X_j Z_{iD} \equiv 0 \), computed as \( ( X_{iD} - X_j ) ( Z_{iD} + Z_j ) - X_{iD} Z_{iD} + X_j Z_j \), 2 products, for the pairs \( ( i , j ) \) of a prime only (a table of bits computed at compile time), one product for both \( i D - j \) and \( i D + j \), the giant steps by sums, and one gcd at the end of the curve. \( B_2 = 25 B_1 \) and \( B_1 \) from 30 to 205 with the bits of \( m \), for a factor of half of them, but for the first curve, whose \( B_1 = 40 \) is for one of about 20 bits; after 64 curves with no factor, the rho;
- below \( 2^{34} \), the rho of Pollard with the cycle detection of Brent, in the Montgomery arithmetic of \( m \): the sequence \( x_{i+1} = x_i^2 + c \bmod m \) falls into a cycle modulo the least prime \( p \) of \( m \) after \( O( \sqrt{p} ) = O( m^{1/4} ) \) steps in expectation (the birthday paradox, the map taken as random), which \( \gcd( | x_i - x_j | , m ) \) finds; the differences are multiplied together, 128 at a time, before each gcd, and the last batch is gone through again one by one if the gcd is \( m \); with another \( c \) if the factor is \( m \) itself.
The curves find a factor \( p \) in \( L_p( 1/2 , \sqrt{2} ) \) products expected (Lenstra), in practice a few curves of a few thousand products for \( p \) of 32 bits, against the \( O( \sqrt{p} ) \) steps of the rho;
- sieve(), the sieve of Eratosthenes on a SET of the integers from 2 to \( n \) [see BoolSet.h], beyond the book: the least item \( p \) left in the set is a prime, and its multiples are removed; since the multiples of the primes below \( p \) are already gone, the removal can start at \( p^2 \), and the sieve stops at \( p > \sqrt{n} \), in \( O( n \log \log n ) \).
The contracts [see Contract.h]: extendedEuclid() needs \( s , m \ge 0 \) and gives \( d = p s + q m \); modInverse() needs \( m \ge 1 \) and \( \gcd( s , m ) = 1 \), and its result \( r \) satisfies \( s r \equiv 1 \pmod{m} \); the products and the powers modulo \( m \) need \( m \ge 1 \), and mulMod61() factors below \( 2^{61} - 1 \); witness() needs an odd \( n \ge 3 \) and a base \( 1 \le b < n \), and millerRabin() \( K \ge 0 \); sieve() gives the primes up to \( n \), increasing, as isPrimeTrial() finds them (an audit); factorize() needs \( n \ge 1 \) and gives primes whose product is \( n \) (an audit). The functions are defined in NumberTheory.tpp.
- See also
- A. Montresor, Algoritmi e Strutture Dati, 2025, section 17.2 and exercises 17.1-17.3, https://algoritmicamente.it/materiali/libro/capitoli/cap17.pdf
-
P. L. Montgomery, Modular multiplication without trial division, Mathematics of Computation 44(170), 519-521, 1985, doi:10.1090/S0025-5718-1985-0777282-X
-
J. Stein, Computational problems associated with Racah algebra, Journal of Computational Physics 1(3), 397-405, 1967, doi:10.1016/0021-9991(67)90047-2
-
D. E. Knuth, The Art of Computer Programming, vol. 2, 3rd ed., Addison-Wesley, 1998, section 4.5.2 (Algorithm B)
-
G. L. Miller, Riemann's hypothesis and tests for primality, Journal of Computer and System Sciences 13(3), 300-317, 1976, doi:10.1016/S0022-0000(76)80043-8
-
M. O. Rabin, Probabilistic algorithm for testing primality, Journal of Number Theory 12(1), 128-138, 1980, doi:10.1016/0022-314X(80)90084-0
-
T. Granlund, P. L. Montgomery, Division by invariant integers using multiplication, Proceedings of the ACM SIGPLAN 1994 Conference on Programming Language Design and Implementation (PLDI), 61-72, 1994, doi:10.1145/178243.178249
-
G. Jaeschke, On strong pseudoprimes to several bases, Mathematics of Computation 61(204), 915-926, 1993, doi:10.1090/S0025-5718-1993-1192971-8
-
M. Forisek, J. Jancina, Fast primality testing for integers that fit into a machine word, SOFSEM 2015 Student Research Forum, CEUR-WS 1326, 20-30, 2015, https://ceur-ws.org/Vol-1326/020-Forisek.pdf
-
J. Sinclair and others, the deterministic sets of bases for the test of Miller and Rabin, https://miller-rabin.appspot.com
-
J. M. Pollard, A Monte Carlo method for factorization, BIT 15(3), 331-334, 1975, doi:10.1007/BF01933667
-
R. P. Brent, An improved Monte Carlo factorization algorithm, BIT Numerical Mathematics 20(2), 176-184, 1980, doi:10.1007/BF01933190
-
H. W. Lenstra, Factoring integers with elliptic curves, Annals of Mathematics 126(3), 649-673, 1987, doi:10.2307/1971363
-
P. L. Montgomery, Speeding the Pollard and elliptic curve methods of factorization, Mathematics of Computation 48(177), 243-264, 1987, doi:10.1090/S0025-5718-1987-0866113-7
-
J. W. Bos, T. Kleinjung, ECM at work, Advances in Cryptology - ASIACRYPT 2012, LNCS 7658, 467-484, 2012, doi:10.1007/978-3-642-34961-4_29
- Author
- Donato Meoli
Dipartimento di Informatica
Università di Pisa
- Copyright
- © by Donato Meoli