Prime Numbers
Prime Numbers Prime Numbers
9.5 Large-integer multiplication 495 (2) Perform length-D weighted convolution, with weight generator A a primitive (2D)-th root of unity, do carry adjust as necessary. (3) Create length-(D/2) “fold-over” signals, as x ′ = L(x) +iH(x) and similarly for a y ′ , employ a weighted convolution with generator A a primitive (4D)-th root of unity, do carry adjust. Method (1) could, of course, involve Algorithm 9.5.12, with perhaps a fast Fermat-mod of Section 9.2.3; but one could instead use a pureinteger Nussbaumer convolution discussed later. Method (2) is the negacyclic approach, in which the weighted convolution can be seen to be multiplication (mod Fn); that is, the mod operation is “free” (see Exercises). Method (3) is the right-angle convolution approach, which also gives the mod operation for free (see Exercises). Note that neither method (2) nor method (3) involves zero-padding, and that method (3) actually halves the signal lengths (at the expense of complex arithmetic). We focus on method (3), to state the following algorithm, which, as with Algorithm 9.5.12, is often implemented in a floatingpoint paradigm: Algorithm 9.5.17 (DWT multiplication modulo Fermat numbers). For a given Fermat number Fn =22n +1, and positive integers x, y ≡ −1(modFn), this algorithm returns (xy) modFn. We choose B,D such that Fn = BD +1, with the inputs x, y interpreted as length-D signals of base-B digits. We assume that there exists a primitive 4D-th root of unity, A, in the field. 1. [Initialize] E = D/2; // Halve the signal length and “fold-over” the signals. x = L(x)+iH(x); // Length-E signals. y = L(y)+iH(y); a = 1,A,A 2 ,...,A E−1 ; // Weight signal. 2. [Apply transforms] X = DWT(x, a); // Via an efficient length-E FFT algorithm. Y = DWT(y, a); 3. [Dyadic product] Z = X ∗ Y ; 4. [Inverse transform] z = DWT −1 (Z, a); 5. [Unfold signal] z =Re(z) ∪ Im(z); // Now z will have length D. 6. [Round digits] z = round(z); // Elementwise rounding to nearest integer. 7. [Adjust carry in base B] carry =0; for(0 ≤ n
496 Chapter 9 FAST ALGORITHMS FOR LARGE-INTEGER ARITHMETIC } carry = ⌊v/B⌋; 8. [Final modular adjustment] Include possible carry > 0 as a high digit of z; z = z mod Fn; // Via another ’carry’ loop or via special-form mod methods. return z; Note that in the steps [Adjust carry in base B] and [Final modular adjustment] the logic depends on the digits of the reconstructed integer z being positive. We say this because there are efficient variants using balanceddigit representation, in which variants care must be taken to interpret negative digits (and negative carry) correctly. This algorithm was used in the discoveries of new factors of F13,F15,F16, and F18 [Brent et al. 2000] (see the Fermat factor tabulation in Section 1.3.2), and also to establish the composite character of F22, F24, and of various cofactors for other Fn [Crandall et al. 1995], [Crandall et al. 1999]. In more recent times, [Woltman 2000] has implemented the algorithm to forge highly efficient factoring software for Fermat numbers (see remarks following Algorithm 7.4.4). Another DWT variant has been used in the discovery of eight Mersenne primes 2 1398269 −1, 2 2976221 −1, 2 3021377 −1, 2 6972593 −1, 2 13466917 −1, 2 20996011 − 1, 2 24036583 −1, 2 25964951 −1 (see Table 1.2), the last of which being the largest known explicit prime as of the present writing. For these discoveries, a network of volunteer users ran extensive Lucas–Lehmer tests that involve vast numbers of squarings modulo p =2 q − 1. The algorithm variant in question has been called the irrational-base discrete weighted transform (IBDWT) [Crandall and Fagin 1994], [Crandall 1996a] for the reason that a special digit representation reminiscent of irrational-base expansion is used, which representation amounts to a discrete rendition of an attempt to expand in an irrational base. Let p =2 q − 1 and observe first that if an integer x be represented in base B =2 as q−1 x = xj2 j , j=0 equivalently, x is the length-q signal (xj); and similarly for an integer y, then the cyclic convolution x×y has, without carry, the digits of (xy) modp. Thus, in principle, the standard FFT multiply could be effected in this way, modulo Mersenne primes, without zero-padding. However, there are two problems with this approach. First, the arithmetic is merely bitwise, not exploiting typical machine advantages of word arithmetic. Second, one would have to invoke a length-q FFT. This can certainly be done (see Exercises), but power-of-two lengths are usually more efficient, definitely more prevalent. It turns out that both of the obstacles to a not-zero-padded Mersenne multiply-mod can be overcome, if only we could somehow represent integers x in the irrational base B =2 q/D ,with1
- Page 454 and 455: 9.1 Tour of “grammar-school” me
- Page 456 and 457: 9.2 Enhancements to modular arithme
- Page 458 and 459: 9.2 Enhancements to modular arithme
- Page 460 and 461: 9.2 Enhancements to modular arithme
- Page 462 and 463: 9.2 Enhancements to modular arithme
- Page 464 and 465: 9.2 Enhancements to modular arithme
- Page 466 and 467: 9.3 Exponentiation 457 Algorithm 9.
- Page 468 and 469: 9.3 Exponentiation 459 But there is
- Page 470 and 471: 9.3 Exponentiation 461 the benefit
- Page 472 and 473: 9.4 Enhancements for gcd and invers
- Page 474 and 475: 9.4 Enhancements for gcd and invers
- Page 476 and 477: 9.4 Enhancements for gcd and invers
- Page 478 and 479: 9.4 Enhancements for gcd and invers
- Page 480 and 481: 9.4 Enhancements for gcd and invers
- Page 482 and 483: 9.5 Large-integer multiplication 47
- Page 484 and 485: 9.5 Large-integer multiplication 47
- Page 486 and 487: 9.5 Large-integer multiplication 47
- Page 488 and 489: 9.5 Large-integer multiplication 47
- Page 490 and 491: 9.5 Large-integer multiplication 48
- Page 492 and 493: 9.5 Large-integer multiplication 48
- Page 494 and 495: 9.5 Large-integer multiplication 48
- Page 496 and 497: 9.5 Large-integer multiplication 48
- Page 498 and 499: 9.5 Large-integer multiplication 48
- Page 500 and 501: 9.5 Large-integer multiplication 49
- Page 502 and 503: 9.5 Large-integer multiplication 49
- Page 506 and 507: 9.5 Large-integer multiplication 49
- Page 508 and 509: 9.5 Large-integer multiplication 49
- Page 510 and 511: 9.5 Large-integer multiplication 50
- Page 512 and 513: 9.5 Large-integer multiplication 50
- Page 514 and 515: 9.5 Large-integer multiplication 50
- Page 516 and 517: 9.5 Large-integer multiplication 50
- Page 518 and 519: 9.6 Polynomial arithmetic 509 can i
- Page 520 and 521: 9.6 Polynomial arithmetic 511 Incid
- Page 522 and 523: 9.6 Polynomial arithmetic 513 where
- Page 524 and 525: 9.6 Polynomial arithmetic 515 such
- Page 526 and 527: 9.6 Polynomial arithmetic 517 Note
- Page 528 and 529: 9.7 Exercises 519 (3) Write out com
- Page 530 and 531: 9.7 Exercises 521 where “do” si
- Page 532 and 533: 9.7 Exercises 523 9.23. How general
- Page 534 and 535: 9.7 Exercises 525 two (and thus, me
- Page 536 and 537: 9.7 Exercises 527 0 2 +3 2 +0 2 is
- Page 538 and 539: 9.7 Exercises 529 9.49. In the FFT
- Page 540 and 541: 9.7 Exercises 531 adjustment step.
- Page 542 and 543: 9.7 Exercises 533 9.69. Implement A
- Page 544 and 545: 9.8 Research problems 535 less than
- Page 546 and 547: 9.8 Research problems 537 1.66), na
- Page 548 and 549: 9.8 Research problems 539 9.82. A c
- Page 550 and 551: 542 Appendix BOOK PSEUDOCODE Becaus
- Page 552 and 553: 544 Appendix BOOK PSEUDOCODE } ...;
9.5 Large-integer multiplication 495<br />
(2) Perform length-D weighted convolution, with weight generator A a<br />
primitive (2D)-th root of unity, do carry adjust as necessary.<br />
(3) Create length-(D/2) “fold-over” signals, as x ′ = L(x) +iH(x) and<br />
similarly for a y ′ , employ a weighted convolution with generator A a<br />
primitive (4D)-th root of unity, do carry adjust.<br />
Method (1) could, of course, involve Algorithm 9.5.12, with perhaps a<br />
fast Fermat-mod of Section 9.2.3; but one could instead use a pureinteger<br />
Nussbaumer convolution discussed later. Method (2) is the negacyclic<br />
approach, in which the weighted convolution can be seen to be multiplication<br />
(mod Fn); that is, the mod operation is “free” (see Exercises). Method (3)<br />
is the right-angle convolution approach, which also gives the mod operation<br />
for free (see Exercises). Note that neither method (2) nor method (3) involves<br />
zero-padding, and that method (3) actually halves the signal lengths (at the<br />
expense of complex arithmetic). We focus on method (3), to state the following<br />
algorithm, which, as with Algorithm 9.5.12, is often implemented in a floatingpoint<br />
paradigm:<br />
Algorithm 9.5.17 (DWT multiplication modulo Fermat numbers). For a<br />
given Fermat number Fn =22n +1, and positive integers x, y ≡ −1(modFn),<br />
this algorithm returns (xy) modFn. We choose B,D such that Fn = BD +1,<br />
with the inputs x, y interpreted as length-D signals of base-B digits. We assume<br />
that there exists a primitive 4D-th root of unity, A, in the field.<br />
1. [Initialize]<br />
E = D/2; // Halve the signal length and “fold-over” the signals.<br />
x = L(x)+iH(x); // Length-E signals.<br />
y = L(y)+iH(y);<br />
a = 1,A,A 2 ,...,A E−1 ; // Weight signal.<br />
2. [Apply transforms]<br />
X = DWT(x, a); // Via an efficient length-E FFT algorithm.<br />
Y = DWT(y, a);<br />
3. [Dyadic product]<br />
Z = X ∗ Y ;<br />
4. [Inverse transform]<br />
z = DWT −1 (Z, a);<br />
5. [Unfold signal]<br />
z =Re(z) ∪ Im(z); // Now z will have length D.<br />
6. [Round digits]<br />
z = round(z); // Elementwise rounding to nearest integer.<br />
7. [Adjust carry in base B]<br />
carry =0;<br />
for(0 ≤ n