For centuries, number theorists have refined their intuition by computing examples. The advent of computers and (especially) sophisticated algorithms has gradually led to the emergence of algorithmic number theory as a distinct field. This young discipline has been shaped by strong connections to computer science, cryptography, and other parts of mathematics. One of its charms is that mathematical ideas often lead to better algorithms. Another striking feature is that the algorithmic worldview has led to fascinating new mathematical ideas and questions. This volume contains twenty survey articles on topics in algorithmic number theory, written by leading experts in the field. The first two are introductory, aiming to entice the reader into pursuing the subject more deeply. The next eight cover core areas of the field: factoring, primality, smooth numbers, lattices, elliptic curves, algebraic number theory, and fast arithmetic algorithms. The remaining ten articles survey specific topics, often with a distinctive perspective, including cryptography, Arakelov class groups, computational class field theory, zeta functions over finite fields, arithmetic geometry, and modular forms.
Mathematical Sciences Research Institute Publications
44
Algorithmic Number Theory
Mathematical Sciences Research Institute Publications 1 2 3 4 5 6 7 8 9 10–11 12–13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
Freed/Uhlenbeck: Instantons and Four-Manifolds, second edition Chern (ed.): Seminar on Nonlinear Partial Differential Equations Lepowsky/Mandelstam/Singer (eds.): Vertex Operators in Mathematics and Physics Kac (ed.): Infinite Dimensional Groups with Applications Blackadar: K-Theory for Operator Algebras, second edition Moore (ed.): Group Representations, Ergodic Theory, Operator Algebras, and Mathematical Physics Chorin/Majda (eds.): Wave Motion: Theory, Modelling, and Computation Gersten (ed.): Essays in Group Theory Moore/Schochet: Global Analysis on Foliated Spaces, second edition Drasin/Earle/Gehring/Kra/Marden (eds.): Holomorphic Functions and Moduli Ni/Peletier/Serrin (eds.): Nonlinear Diffusion Equations and Their Equilibrium States Goodman/de la Harpe/Jones: Coxeter Graphs and Towers of Algebras Hochster/Huneke/Sally (eds.): Commutative Algebra Ihara/Ribet/Serre (eds.): Galois Groups over Q Concus/Finn/Hoffman (eds.): Geometric Analysis and Computer Graphics Bryant/Chern/Gardner/Goldschmidt/Griffiths: Exterior Differential Systems Alperin (ed.): Arboreal Group Theory Dazord/Weinstein (eds.): Symplectic Geometry, Groupoids, and Integrable Systems Moschovakis (ed.): Logic from Computer Science Ratiu (ed.): The Geometry of Hamiltonian Systems Baumslag/Miller (eds.): Algorithms and Classification in Combinatorial Group Theory Montgomery/Small (eds.): Noncommutative Rings Akbulut/King: Topology of Real Algebraic Sets Judah/Just/Woodin (eds.): Set Theory of the Continuum Carlsson/Cohen/Hsiang/Jones (eds.): Algebraic Topology and Its Applications Clemens/Koll´ ar (eds.): Current Topics in Complex Algebraic Geometry Nowakowski (ed.): Games of No Chance Grove/Petersen (eds.): Comparison Geometry Levy (ed.): Flavors of Geometry Cecil/Chern (eds.): Tight and Taut Submanifolds Axler/McCarthy/Sarason (eds.): Holomorphic Spaces Ball/Milman (eds.): Convex Geometric Analysis Levy (ed.): The Eightfold Way Gavosto/Krantz/McCallum (eds.): Contemporary Issues in Mathematics Education Schneider/Siu (eds.): Several Complex Variables Billera/Bj¨ orner/Green/Simion/Stanley (eds.): New Perspectives in Geometric Combinatorics Haskell/Pillay/Steinhorn (eds.): Model Theory, Algebra, and Geometry Bleher/Its (eds.): Random Matrix Models and Their Applications Schneps (ed.): Galois Groups and Fundamental Groups Nowakowski (ed.): More Games of No Chance Montgomery/Schneider (eds.): New Directions in Hopf Algebras Buhler/Stevenhagen (eds.): Algorithmic Number Theory Jensen/Ledet/Yui: Generic Polynomials: Constructive Aspects of the Inverse Galois Problem Rockmore/Healy (eds.): Modern Signal Processing Uhlmann (ed.): Inside Out: Inverse Problems and Applications Gross/Kotiuga: Electromagnetic Theory and Computation: A Topological Approach Darmon/Zhang (eds.): Heegner Points and Rankin L-Series Bao/Bryant/Chern/Shen (eds.): A Sampler of Riemann–Finsler Geometry Avramov/Green/Huneke/Smith/Sturmfels (eds.): Trends in Commutative Algebra Goodman/Pach/Welzl (eds.): Combinatorial and Computational Geometry Schoenfeld (ed.): Assessing Mathematical Proficiency Hasselblatt (ed.): Dynamics, Ergodic Theory, and Geometry Pinsky/Birnir (eds.): Probability, Geometry and Integrable Systems
Volumes 1–4, 6–8 and 10–27 are published by Springer-Verlag
Algorithmic Number Theory: Lattices, Number Fields, Curves and Cryptography
Edited by
J. P. Buhler P. Stevenhagen
J. P. Buhler CCR and Reed College 4320 Westerra Ct., San Diego, CA 92121
[email protected] P. Stevenhagen Mathematisch Instituut, Universiteit Leiden Postbus 9512, 2300 RA Leiden, The Netherlands
[email protected] Silvio Levy (Series Editor ) Mathematical Sciences Research Institute 17 Gauss Way, Berkeley, CA 94720
[email protected] The Mathematical Sciences Research Institute wishes to acknowledge support by the National Science Foundation and the Pacific Journal of Mathematics for the publication of this series.
cambridge university press Cambridge, New York, Melbourne, Madrid, Cape Town, Singapore, S˜ ao Paulo, Delhi Cambridge University Press 32 Avenue of the Americas, New York, NY 10013-2473, USA www.cambridge.org Information on this title: www.cambridge.org/9780521808545 c Mathematical Sciences Research Institute 2008 ° This publication is in copyright. Subject to statutory exception and to the provisions of relevant collective licensing agreements, no reproduction of any part may take place without the written permission of Cambridge University Press.
First published 2008 Printed in the United States of America A catalog record for this publication is available from the British Library. Library of Congress Cataloging in Publication data ISBN
978-0-521-80854-5 hardback
Cambridge University Press has no responsibility for the persistence or accuracy of URLs for external or third-party Internet Web sites referred to in this publication and does not guarantee that any content on such Web sites is, or will remain, accurate or appropriate.
Algorithmic Number Theory MSRI Publications Volume 44, 2008
Contents Preface
page ix
Solving the Pell equation H ENDRIK W. L ENSTRA , J R .
1
Basic algorithms in number theory J OE B UHLER AND S TAN WAGON
25
Smooth numbers and the quadratic sieve C ARL P OMERANCE
69
The number field sieve P ETER S TEVENHAGEN
83
Four primality testing algorithms R EN E´ S CHOOF
101
Lattices H ENDRIK W. L ENSTRA , J R .
127
Elliptic curves B JORN P OONEN
183
The arithmetic of number rings P ETER S TEVENHAGEN
209
Smooth numbers: computational number theory and beyond A NDREW G RANVILLE
267
Fast multiplication and its applications DANIEL J. B ERNSTEIN
325
Elementary thoughts on discrete logarithms C ARL P OMERANCE
385
The impact of the number field sieve on the discrete logarithm problem in finite fields O LIVER S CHIROKAUER
397
Reducing lattice bases to find small-height values of univariate polynomials DANIEL J. B ERNSTEIN
421
Computing Arakelov class groups R EN E´ S CHOOF
447
vii
viii
CONTENTS
Computational class field theory H ENRI C OHEN AND P ETER S TEVENHAGEN
497
Protecting communications against forgery DANIEL J. B ERNSTEIN
535
Algorithmic theory of zeta functions over finite fields DAQING WAN
551
Counting points on varieties over finite fields of small characteristic A LAN G. B. L AUDER AND DAQING WAN
579
Congruent number problems and their variants JAAP T OP AND N ORIKO Y UI
613
An introduction to computing modular forms using modular symbols W ILLIAM A. S TEIN
641
Algorithmic Number Theory MSRI Publications Volume 44, 2008
Preface Our subject arises out of two roots of mathematical thought: fascination with properties of whole numbers and the urge to compute. Number theory and computer science flowered vividly during the last quarter of the twentieth century, and the synergy at their intersection was striking. Algorithmic number theory emerged as an exciting field in its own right, containing deep insights, and having surprising applications. In the fall of 2000 the Mathematical Sciences Research Institute Berkeley hosted a one-semester program on algorithmic number theory. Its opening workshop, cosponsored by the Clay Mathematics Institute, featured many foundational and survey talks. During the meeting, it was noted that there was a dearth of sources for newcomers to the field. After the conference, some of the speakers agreed to write articles based on their talks, and we were drafted to edit the volume. A few authors turned in drafts promptly, some retaining the tutorial focus and tone of the original talks, while others were full-blown tutorials or surveys. Many authors (including the editors) dallied. Additional articles were solicited, to provide more coherence and to incorporate newer results that couldn’t be ignored (most notably, the polynomial-time primality algorithm due to Manindra Agrawal, Neeraj Kayal, and Nitin Saxena). This led to complications that might have been expected for a volume with 20 substantial articles, 15 authors, and 650 pages. These have finally run their course, and we are delighted that the volume is ready to see the light of day. We do apologize to the authors who responded promptly, and can only hope that they will be compensated by the greater breadth and interest of the volume in which their contributions appear. The articles in the volume can be loosely categorized as follows. The first two articles are introductory, and are more elementary than their successors — they attempt to entice the reader into pursuing the ideas more deeply. The next eight articles provide surveys of central topics, including smooth numbers, factoring, primality testing, lattices, elliptic curves, algebraic number theory, and fast arithmetic algorithms. The remaining ten articles study specific topics more deeply, ix
x
PREFACE
including cryptography, computational algebraic number theory, modular forms, and arithmetic geometry. Although the articles in this volume are surveys in the broadest sense, the word should not be taken to mean an encyclopedic treatment that captures current conventional wisdom. We prefer the term overviews, and the articles have a distinctive and in some cases even nonstandard perspective. It remains our pleasant duty to thank a number of institutions and people. Most obviously, the authors have produced many fascinating pages, sure to inspire others to pursue the subject. We thank the Clay Institute and MSRI for their generous funding for the workshop that provided the initial spark for this volume. We thank Cambridge University Press and MSRI for their support and patience during the production of this volume, and we especially thank Silvio Levy for his extensive efforts on this volume. John Voight took notes (by typing nearly real-time TEX into his laptop) at most of the talks at workshop, and these were valuable to some of the authors. Finally, Hendrik Lenstra has long been a source of pervasive and brilliant inspiration to the entire field of algorithmic number theory, and this volume is no exception: in addition to two distinctive articles, he has provided muchappreciated advice over the years to the editors and to virtually all of the other authors.
Joe Buhler Peter Stevenhagen San Diego, May 2008
Algorithmic Number Theory MSRI Publications Volume 44, 2008
Solving the Pell equation HENDRIK W. LENSTRA, JR.
A BSTRACT. We illustrate recent developments in computational number theory by studying their implications for solving the Pell equation. We shall see that, if the solutions to the Pell equation are properly represented, the traditional continued fraction method for solving the equation can be significantly accelerated. The most promising method depends on the use of smooth numbers. As with many algorithms depending on smooth numbers, its run time can presently only conjecturally be established; giving a rigorous analysis is one of the many open problems surrounding the Pell equation.
1. Pell’s equation The Pell equation is the equation x 2 D dy 2 C 1; to be solved in positive integers x, y for a given nonzero integer d . For example, for d D 5 one can take x D 9, y D 4. We shall always assume that d is positive but not a square, since otherwise there are clearly no solutions. The English mathematician John Pell (1611–1685) has nothing to do with the equation. Euler (1707–1783) mistakenly attributed to Pell a solution method that had in fact been found by another English mathematician, William Brouncker (1620–1684), in response to a challenge by Fermat (1601–1665); but attempts to change the terminology introduced by Euler have always proved futile. Pell’s equation has an extraordinarily rich history, to which Weil [1984] is the best guide; see also [Dickson 1920, Chapter XII; Konen 1901; Whitford 1912]. Brouncker’s method is in substance identical to a method that was known to Indian mathematicians at least six centuries earlier. As we shall see, the equation This paper appeared in slightly different form in Notices Amer. Math. Soc. 49 (2002), 182–192, with the permission of MSRI and the editors of the present volume.
1
2
HENDRIK W. LENSTRA, JR.
also occurred in Greek mathematics, but no convincing evidence that the Greeks could solve the equation has ever emerged. A particularly lucid exposition of the “Indian” or “English” method of solving the Pell equation is found in Euler’s Algebra [Euler 1770, Abschnitt 2, Capitel 7]. Modern textbooks usually give a formulation in terms of continued fractions, which is also due to Euler (see for example [Niven et al. 1991, Chapter 7]). Euler, as well as his Indian and English predecessors, appears to take it for granted that the method always produces a solution. That is true, but it is not obvious — all that is obvious is that if there is a solution, the method will find one. Fermat was probably in possession of a proof that there is a solution for every d (see [Weil 1984, Chapter II, ~ XIII]), and the first to publish such a proof was Lagrange (1736–1813) [1773]. One may rewrite Pell’s equation as p p .x C y d/ .x y d/ D 1; so p that finding a solution comes down a nontrivial unit of the ring p to finding Z Œ d of norm 1; here the norm Z Œ d ! Z D f˙1g between unit groups p multiplies each unit by its conjugate, and the units ˙1 of Z Œ d are considered trivial. This reformulation implies that once one knows a solution to Pell’s equation, one can find infinitely many. More precisely, if the solutions are ordered by magnitude, then the n-th solution xn , yn can be expressed in terms of the first one, x1 , y1 , by p p xn C yn d D .x1 C y1 d/n : Accordingly, the first solution x1 , y1 is called the fundamental solution to the Pell equation, and solving the Pell equation meansp finding x1 , y1 for given d . By abuse of language, we shall also refer to x Cpy d instead of the pair x, y as a solution to Pell’s equation and call x1 C y1 d the fundamental solution. One may view the solvability of Pell’s equation as a special case of Dirichlet’s unit theorem from algebraic number theory, which describes the structure of the group of units p of a general ring of algebraic integers [Stevenhagen 2008a]; for the ring Z Œ d, it is the product of f˙1g and an infinite cyclic group. As an example, consider d D 14. One has p 14 D 3 C
1
;
1
1C
1
2C 1C
3C
1 p
14
SOLVING THE PELL EQUATION
3
p so the continued fraction expansion of 3 C 14 is purely periodic with period length 4. Truncating the expansion at the end of the first period, one finds that the fraction 1 15 3C D 4 1 1C 1 2C 1 1 p is a fair approximation to 14. The numerator and denominator of this fraction yield the fundamental solution x1 D 15, 4; indeed one p has 152 D 1442 C1. p y1 D 2 Furthermore, one computes .15 C 4 14/ D 449 C 120 14, so x2 D 449, y2 D 120; and so on. One finds: n
xn
yn
1 2 3 4 5 6
15 449 13455 403201 12082575 362074049
4 120 3596 107760 3229204 96768360
The shape of the table reflects the exponential growthpof xn p and yn with n. For general d, the continued fraction expansion of Œ dC d is again purely periodic, and the period displays a symmetry similar to the one visible for d D 14. If the period length is even, one proceeds as above; if the period length is odd, one truncates at the end of the second period [Buhler and Wagon 2008].
2. The cattle problem An interesting example of the Pell equation, both from a computational and from a historical perspective, is furnished by the cattle problem of Archimedes (287–212 B.C.). A manuscript containing this problem was discovered by Lessing (1729–1781) in the Wolffenb¨uttel library, and published by him in 1773 (see [Lessing 1773; Heiberg 1913, pp. 528–534]). It is now generally credited to Archimedes [Fraser 1972; Weil 1984]. In twenty-two Greek elegiac distichs, the problem asks for the number of white, black, dappled, and brown bulls and cows belonging to the Sun god, subject to several arithmetical restrictions. A version in English heroic couplets, published in [Archimedes 1999], is shown on page 4. In modern mathematical notation the problem is no less elegant. Writing x, y, z, t for the numbers of white, black, dappled, and brown bulls,
4
HENDRIK W. LENSTRA, JR.
PROBLEM that Archimedes conceived in verse and posed to the specialists at Alexandria in a letter to Eratosthenes of Cyrene. The Sun god’s cattle, friend, apply thy care to count their number, hast thou wisdom’s share. They grazed of old on the Thrinacian floor of Sic’ly’s island, herded into four, colour by colour: one herd white as cream, the next in coats glowing with ebon gleam, brown-skinned the third, and stained with spots the last. Each herd saw bulls in power unsurpassed, in ratios these: count half the ebon-hued, add one third more, then all the brown include; thus, friend, canst thou the white bulls’ number tell. The ebon did the brown exceed as well, now by a fourth and fifth part of the stained. To know the spotted — all bulls that remained — reckon again the brown bulls, and unite these with a sixth and seventh of the white. Among the cows, the tale of silver-haired was, when with bulls and cows of black compared, exactly one in three plus one in four. The black cows counted one in four once more, plus now a fifth, of the bespeckled breed when, bulls withal, they wandered out to feed. The speckled cows tallied a fifth and sixth of all the brown-haired, males and females mixed. Lastly, the brown cows numbered half a third and one in seven of the silver herd. Tell’st thou unfailingly how many head the Sun possessed, o friend, both bulls well-fed and cows of ev’ry colour — no-one will deny that thou hast numbers’ art and skill, though not yet dost thou rank among the wise. But come! also the foll’wing recognise. Whene’er the Sun god’s white bulls joined the black, their multitude would gather in a pack of equal length and breadth, and squarely throng Thrinacia’s territory broad and long. But when the brown bulls mingled with the flecked, in rows growing from one would they collect, forming a perfect triangle, with ne’er a diff’rent-coloured bull, and none to spare. Friend, canst thou analyse this in thy mind, and of these masses all the measures find, go forth in glory! be assured all deem thy wisdom in this discipline supreme!
SOLVING THE PELL EQUATION
5
respectively, one reads in lines 8–16 the restrictions x D . 21 C 31 /y C t; y D . 14 C 51 /z C t; z D . 61 C 71 /x C t: Next, for the numbers x 0 , y 0 , z 0 , t 0 of cows of the same respective colors, the poet requires in lines 17–26 x 0 D . 13 C 41 /.y C y 0 /;
z 0 D . 15 C 16 /.t C t 0 /;
y 0 D . 14 C 51 /.z C z 0 /;
t 0 D . 16 C 17 /.x C x 0 /:
Whoever can solve the problem thus far is called merely competent by Archimedes; to win the prize for supreme wisdom, one should also meet the conditions formulated in lines 33–40 that x C y be a square and that z C t be a triangular number. The first part of the problem is just linear algebra, and there is indeed a solution in positive integers. The general solution to the first three equations is given by .x; y; z; t / D m.2226; 1602; 1580; 891/, where m is a positive integer. The next four equations turn out to be solvable if and only if m is divisible by 4657; with m D 4657 k one has .x 0 ; y 0 ; z 0 ; t 0 / D k .7206360; 4893246; 3515820; 5439213/: The true challenge is now to choose k such that x C y D 4657 3828 k is a square and z C t D 4657 2471 k is a triangular number. From the prime factorization 4657 3828 D 22 3 11 29 4657 one sees that the first condition is equivalent to k D al 2 , where a D 3 11 29 4657 and l is an integer. Since z C t is a triangular number if and only if 8.z C t / C 1 is a square, we are led to the equation h2 D 8.z C t / C 1 D 8 4657 2471 al 2 C 1, which is the Pell equation h2 D d l 2 C 1 for d D 2 3 7 11 29 353 .2 4657/2 D 410 286423 278424: Thus, by Lagrange’s theorem, the cattle problem admits infinitely many solutions. In 1867 the otherwise unknown German mathematician C. F. Meyer set out to solve the equation by the continued fraction method [Dickson 1920, p. 344]. p After 240 steps in the continued fraction expansion for d he had still not detected the period, and he gave up. He may have been a little impatient; it was later discovered that the period length equals 203254; see [Grosjean and De Meyer 1991]. The first to solve the cattle problem in a satisfactory way was A. Amthor in 1880 (see [Krumbiegel and Amthor 1880]). Amthor did not directly apply the continued fraction method; what he did do we shall discuss
6
HENDRIK W. LENSTRA, JR.
below. Nor did he spell out the decimal digits of the fundamental solution to the Pell equation or the corresponding solution of the cattle problem. He did show that, in the smallest solution to the cattle problem, the total number of cattle is given by a number of 206545 digits; of the four leading digits 7766 that he gave, the fourth was wrong, due to the use of insufficiently precise logarithms. The full number occupies forty-seven pages of computer printout, reproduced in reduced size on twelve pages of the Journal of Recreational Mathematics [Nelson 1980/81]. In abbreviated form, it reads 77602714 : : : 237983357 : : : 55081800; each of the six dots representing 34420 omitted digits. Several nineteenth century German scholars were worried that so many bulls and cows might not fit on the island of Sicily, contradicting lines 3 and 4 of the poem; but, as Lessing remarked, the Sun god, to whom the cattle belonged, will have coped with it. The story of the cattle problem shows that the continued fraction method is not the last word on the Pell equation.
3. Efficiency We are interested in the efficiency of solution methods for the Pell equation. Thus, how much time does a given algorithm for solving the Pell equation take? Here time is to be measured in a realistic way, which reflects, for example, that large positive integers are more time-consuming to operate with than small ones; technically, one counts bit operations. The input to the algorithm is d , and the running time estimates are accordingly expressed as functions of d. If one supposes that d is specified in binary or in decimal, then the length of the input is approximately proportional to log d. An algorithm is said to run in polynomial time if there is a positive real number c0 such that for all d the running time is at most .1 C log d/c0 , in other words, if the time that it takes the algorithm to solve the Pell equation is not much greater than the time required to write down the equation. How fast is the continued fraction method? Can the Pell equation be solved in polynomial time? The central quantity that one needs to consider in order to answer such questions is the regulator Rd , which is defined by p Rd D log.x1 C y1 d/; p where x1 C y1 d denotes, as before, the fundamental solution to Pell’s equation. The regulator coincides with what in algebraic number theory would p be called the regulator of the kernel of the norm map Z Œ d ! Z . From
SOLVING THE PELL EQUATION
7
p p p p x1 y1 d D 1=.x1 C y1 d/ one deduces that 0 < x y d < 1=.2 d /, 1 1 p and combining this with x1 C y1 d D eRd , one finds that eRd eRd 1 < x1 < C p ; 2 2 4 d
eRd p 2 d
1 eRd < y1 < p : 4d 2 d p This shows that Rd is very close to log.2x1 / and to log.2y1 d/. That is, if x1 and y1 are to be represented in binary or in decimal, then Rd is approximately proportional to the length of the output of any algorithm solving the Pell equation. Since the time required for spelling out the output is a lower bound for the total running time, we may conclude: there exists c1 such that any algorithm for solving the Pell equation takes time at least c1 Rd . Here c1 denotes, just as do c2 , c3 , . . . below, a positive real number that does not depend on d . The continued fraction method almost meets this lower p bound. p Let l be the period length of the continued fraction expansion of Œ d C d if that length is even and twice that length if it is odd. Then one has log 2 log.4d / l < Rd < lI 2 2 see [Lenstra 1982, (11.4)]. Thus Rd and l are approximately proportional. Using this, one estimates easily that the time taken by a straightforward implementation of the continued fraction method is at most R2d .1 C log d/c2 for suitable c2 ; and a more refined implementation, which depends on the fast Fourier transform, reduces this to Rd .1 C log d/c3 for suitable c3 ; see [Sch¨onhage 1971]. We conclude that the latter version of the continued fraction method is optimal, apart from a logarithmic factor. In view of these results it is natural to ask how the regulator grows as a function of d. It turns out that it fluctuates wildly. One has p p log.2 d/ < Rd < d .log.4d / C 2/; p the lower bound because of the inequality y1 < eRd =.2 d/ above and the upper bound by [Hua 1942]. The gap between the two bounds is very large, but it 2 cannot be helped: if d ranges over numbers 1, for which one p of the form k has x1 D k and y1 D 1, then Rd log.2 d/ tends to 0; and one can show that there exist p an infinite set D of d’s and a constant c4 such that all d 2 D have Rd D c4 d. In fact, if d0 , d1 are integers greater than 1 and d0 is not a square, then there exists a positive integer m D m.d0 ; d1 / such that D D fd0 d12n W n 2 Z , n mg has this property for some c4 D c4 .d0 ; d1 /. It is believed that for most d the upper bound is closer to the truth. More precisely, a folklore conjecture asserts that there is a set D of nonsquare positive
8
HENDRIK W. LENSTRA, JR.
integers that has density 1 in the sense that limx!1 #fd 2 D W d xg=x D 1, and that satisfies log Rd lim p D 1: d2D log d This conjecture, however, is wide open. p The same is true for the much weaker conjecture that lim supd .log Rd /= log d, with d ranging over the squarefree integers > 1, is positive. If the folklore conjecture is true, then for most d the factor Rd entering the p running time is about d, which is an exponential function of the length log d of the input. Combining the preceding p results, one concludes that the continued fraction method takes time at most d .1Clog d/c5 ; that conjecturally it is exponentially slow for most values of d; and that any method for solving the Pell equation that spells out x1 and y1 in full is exponentially slow for infinitely many d and will therefore fail to run in polynomial time. If we want to improve upon the continued fraction method, then we need a way of representing x1 and y1 that is more compact than the decimal or binary notation.
4. Amthor’s solution Amthor’s solution to the cattle problem depended on the observation that the number d D 410 286423 278424 can be written as .2 4657/2 d 0 , where d 0 D 4 729494 is squarefree. Hence, if x, y solves the Pell equation for d, then x, 2 4657 y solves the Pell equation for d 0 and will therefore for some n be equal to the n-th solution xn0 , yn0 (say) of that equation: p p x C 2 4657 y d 0 D .x10 C y10 d 0 /n : This reduces the cattle problem to two easier problems: first, solving the Pell equation for d 0 ; and second, finding the least value of n for which yn0 is divisible by 2 4657. Since d 0 is much smaller than d, Amthor could use the continued fraction algorithm for d 0 . In a computation that could be summarized in three pages, as in [Krumbiegel and Amthor 1880], he found the period length to be 92 and p 0 0 0 x1 C y1 d to be given by u D 109 931986 732829 734979 866232 821433 543901 088049 C 50549 485234 315033 074477 819735 540408 986340
p
4 729494:
In order to save space, one can write p p 2 u D 300 426607 914281 713365 609C84 129507 677858 393258 7766 :
SOLVING THE PELL EQUATION
w D 300 426607 914281 713365 kj D .w 4658j j -th solution
w
p
9
609 C 84 129507 677858 393258
4658j 2
/ =368 238304
p 7766
.j D 1; 2; 3; : : : /
bulls
cows
all cattle
white black dappled brown
10 366482 kj 7 460514 kj 7 358060 kj 4 149387 kj
7 206360 kj 4 893246 kj 3 515820 kj 5 439213 kj
17 572842 kj 12 353760 kj 10 873880 kj 9 588600 kj
all colors
29 334443 kj
21 054639 kj
50 389082 kj
All solutions to the cattle problem of Archimedes. p p p 2 This is derived from the identity x C y d D .x 1/=2 C .x C 1/=2 , which holds whenever x 2 D dy 2 C 1. The regulator is found to be Rd 0 ‡ 102:101583. In order to determine the least feasible value for n, Amthor developed a little theory, which one would nowadays cast in the language of finite fields and rings. 0 Using that p D 4657 is a prime number for which the Legendre symbol dp equals 1, he deduced from his theory that the least value for n divides p C1 D 4658; had he been a little more careful, he would have found that it must divide .p C 1/=2 D 2329 D 17 137 (see [Vardi 1998]). In any case, trying a few divisors, one discovers that the least value for n is actually equal to 2329. One has Rd D 2329 Rd 0 ‡ 237794:586710. The conclusion is thatpthe fundamental solution to the Pell equation for d itself is given by x1 C y1 d D u2329 , with u as just defined. Amthor failed to put everything together, but I did this for the convenience of the reader: for the first time in history, all infinitely many solutions to the cattle problem displayed in a handy little table! It does, naturally, not contain the full decimal expansion of any of the numbers asked for, but what it does contain should be considered more enlightening. For example, it enables the reader not only to verify easily that the total number of cattle in the smallest solution has 206545 decimal digits and equals 77602714 : : : 55081800, but also to discover that the number of dappled bulls in the 1494 195300th solution equals 111111 : : : 000000, a number of 308 619694 367813 digits. (Finding the middle digits is probably much harder.) There is no doubt that Archimedes, who wrote a lengthy epistle about the representation of large numbers to King Gelon (see [Dijksterhuis 1956] or [Heiberg 1913, pp. 215–259]), would have been pleased and satisfied by the solution as expressed in the table.
10
HENDRIK W. LENSTRA, JR.
5. Power products Suppose one wishes to solve the Pell equation x 2 D dy 2 C 1 for a given value of d. From Amthor’s approach to the cattle problem we learn that for two reasons it may be wise to find the smallest divisor d 0 of d for which d=d 0 is a square: it saves time when performing the continued fraction algorithm, and it saves both time and space when expressing the final answer. There is no known algorithm for finding d 0 from d that is essentially faster than factoring d. In addition, if we want to determine which power of the fundamental solution for d 0 yields the fundamental solution for d — that is, the number pn from the previous section — we also need to know the prime factorization of d=d 0 , as well as the p d0 d=d 0 . Thus, if one prime factorization of p for each prime p dividing p wants to solve the Pell equation, one may as well start by factoring d. Known factoring algorithms may not be very fast for large d, but for most values of d they are still expected to be orders of magnitudes faster than any known method for solving the Pell equation [Stevenhagen 2008b]. p Let it now be assumed that d is squarefree, and write x1 C y1p d for the fundamental p of Z Œ d. Then p solution of the Pell equation, which is a unit x1p C y1 d may still be a proper power in the field Q . d/ of fractions of Z Œ d . For example, the least d with y1 > 6 is d D 13, for which one has x1 D 649, y1 D 180, and 3 C p13 6 p 649 C 180 13 D : 2 Also in the case d D 109, which Fermat posed as a challenge problem in 1657, the fundamental solution is a sixth power: 261 C 25p109 6 p 158 070671 986249 C 15 140424 455100 109 D : 2 However, this is as far as it goes: it is an elementary exercise in algebraic number p theory to show p that if n is a positive integer for which x1 C y1 d has an n-th root in Q . d/, then n D 1, 2, 3, or 6, the case n D 2 being possible only for d 1, 2, or 5 mod 8, and the cases n D 3 and 6 only for d 5 mod 8. Thus,p for large squarefree d one cannot expect to save much space by writing x1 Cy1 d as a power. This is also true when one allows the root to lie in a composite of quadratic fields, as we did for the cattle problem. Let d again be an arbitrary positive integer p that is not a square. Instead of powers, we consider power products in Q . d/, that is, expressions of the form t Y iD1
p .ai C bi d/ni
SOLVING THE PELL EQUATION
11
where t is a nonnegative integer, ai , bi , ni are integers, ni ¤ 0, and for each i at least one of ai and bi is nonzero. We define the length of such an expression to be t X
p log jni j C log.jai j C jbi j d/ :
iD1
This is roughly proportional to the amount of bits needed to specify the numbers p ai , bi , and ni . Each power product represents a nonzero element of Q . d /, p and that element can be expressed uniquely as .a C b d/=c, with a, b, c 2 Z , gcd.a; b; c/ D 1, c > 0. However, the number of bits of a, b, c will typically grow linearly with the exponents jni j themselves rather than with their logarithms. So one avoids using the latter representation and works directly with the power products instead. Several fundamental issues are raised by the representation of elements as power products. For example, canpwe recognize whether two power products represent the same element of Q . d/, by means of a polynomial time algorithm? Here “polynomial time” means, as before, that the run time is bounded by a polynomial function of the length of the input, which in this case equals the sum of the lengths of the two given power products. Similarly, can we decide in polynomial an element of the p time whether a given power product represents p form a C b d with a, b 2 Z , that is, an element of Z Œ d? If it does, can we decide whether one has a2 db 2 D 1 and a, b > 0, so that we have a solution to Pell’s equation, and can we compute the residue classes of a and b modulo a given positive integer m, all in polynomial time? All questions just raised have affirmative answers, even in the context of general algebraic number fields. Algorithms proving this were exhibited by Guoqiang Ge [1993; 1994]. In particular, one can efficiently decide whether a given power product represents a solution to Pell’s equation, and if it does, one can efficiently compute any desired number of “least significant” decimal digits of that solution; taking the logarithm of the power product, one can do the same for the leading digits, and for the number of decimal digits, except possibly in the probably very rare cases that a or b is excessively close to a power of 10. There is no known polynomial time algorithm for deciding whether a given power product represents the fundamental solution to Pell’s equation.
6. Infrastructure Suppose solution p now that, given d, we are not asking for the fundamental p x1 Cy1 d to Pell’s equation, but for a power product in Q . d / that represents it. The following theorem summarizes essentially all that is rigorously known
12
HENDRIK W. LENSTRA, JR.
about the smallest length of such a power product and about algorithms for finding one. T HEOREM . There are positive real numbers c6 and c7 with the following properties. (a) For each positive integer d that is not a square there exists a power product that represents the fundamental solution to Pell’s equation and that has length at most c6 .log d/2 . (b) The problem of computing a power product representing the fundamental solution to Pell’s equation is “polynomial time equivalent” to the problem of computing an integer RQ d with jRd RQ d j < 1. (c) There is an algorithm that given d computes a power product representing the fundamental solution to Pell’s equation in time at most 1=2
.Rd
C log d/.1 C log d/c7 :
Part (a) of the theorem, which is taken from [Buchmann et al. 1995], implies that the question we are asking does admit a brief answer, so that there is no obvious obstruction to the existence of a polynomial time algorithm for finding such an answer. Part (b), which is not formulated too rigorously, asserts the existence of two polynomial time algorithms. The first takes as input a power product Y p .ai C bi d/ni i
representing the fundamental solution to the Pell equation and gives as output an integer approximation to the regulator. pThere is no surprise here, one just P uses the formula Rd D i ni log jai C bi dj and applies a polynomial time algorithm for approximating logarithms; [Brent 1976]. The second algorithm takes as input the number d as well as an integer approximation RQ d to Rd , and it computes a power product representing the fundamental solution to Pell’s equation. Since the algorithm runs in polynomial time, the length of the output is polynomially bounded, and this is in fact the way part (a) of the theorem is proved. The key notion underlying the second algorithm is that of “infrastructure”, a word coined by Shanks [1972] to describe a certain multiplicative structure p that he detected within the period of the continued fraction expansion of d. It was subsequently shown in [Lenstra 1982] that this period can be “embedded” in a circle group of “circumference” Rd , the embedding preserving the cyclical structure. In the modern terminology of Arakelov theory, one may describe p p that circle group as the kernel of the natural map Pic0 Z Œ d ! Pic Z Œ d from the group of “metrized line bundles of degree 0” on the “arithmetic curve”
SOLVING THE PELL EQUATION
13
p corresponding to Z Œ d to the usual class group of invertible ideals. By means of Gauss’s reduced binary quadratic forms one can do explicit computations p in Pic0 Z Œ d and in its circle subgroup. For a fuller explanation of these notions and their algorithmic use we refer to the literature [Buchmann et al. 1995; Lenstra 1982; Schoof 1982; 2008; Shanks 1972; Williams 2002]. The equivalence stated in part (b) of the theorem has an interesting feature that is not commonly encountered in the context of equivalences. Namely, one may achieve an improvement by going “back and forth”. Thus, starting from a power product representing the fundamental solution, one can first use it to compute RQ d , and next use RQ d to find a second power product, possibly of smaller length than the initial one. And conversely, starting from any rough approximation to Rd one can compute a power product and use it to compute Rd to any desired accuracy. The algorithm referred to in part (c) is the fastest rigorously proven algorithm for computing a power product as desired. Its run time is roughly the square root of the run time of the continued fraction algorithm. It again makes use of the infrastructure just discussed, combining it with a search technique that is known as the “baby step-giant step” method. The power product coming out of the algorithm may not have a very small length, but one can easily do something about this by using the algorithms of part (b). Our estimates for Rd show that the run time is at most d 1=4 .1 C log d/c8 for some c8 ; here the exponent 1=4 can be improved to 1=5 if one is willing to assume certain generalized Riemann hypotheses [Schoof 1982]. According to an unpublished result of Ulrich Vollmer, part (c) is valid with c7 D 1 C " for all " > 0 and all d exceeding a bound depending on "; this represents a slight improvement over the result obtained in [Buchmann and Vollmer 2006]. Mathematically the infrastructure methods have great interest. Algorithmically one conjectures that something faster is available. But as we shall see, the final victory may belong to the infrastructure.
7. Smooth numbers The algorithms for solving Pell’s equation that we saw so far have an exponential run time as a function of log d. One prefers to have an algorithm whose run time is polynomial in log d. The method that we shall now discuss is believed to have a run time that is halfway between exponential and polynomial. Like many subexponential algorithms in number theory, it makes use of smooth numbers, that is, nonzero integers that up to sign are built up from small prime factors. Smooth numbers have been used with great success in the design of algorithms for factoring integers and for computing discrete logarithms in multiplicative
14
HENDRIK W. LENSTRA, JR.
groups of rings [Pomerance 2008a; 2008b] Here we shall see how they can be used for the solution of Pell’s equation as well. Instead of giving a formal description, we illustrate the algorithm on the case d D 4 729494 D 2 3 7 11 29 353 derived from the cattle problem. The p computation is less laborious and more entertaining than the expansion of d in a continued fraction performed by Amthor. We shall explain the method on an intuitive level only; readers desirous to see its formal justification should acquaint themselves with the basic theorems of algebraic number theory [Stevenhagen 2008a; 2008b]. The smooth numbers that the algorithm operates with are not ordinary intep gers, but elements of the ring Z Œ d, with d as just chosen. There is a natural way of p extendingpthe notion of smoothness to such numbers. Namely, for p ˛ D a C b d 2 Q . d /, with a, b 2 Q , write ˛ 0 D a b d. Then ˛ ‘ ˛ 0 yields p an automorphism of the field Q .˛/ and the ring Z Œ˛, and the norm map N W Q . d/ ! Q defined by N .˛/ D ˛˛ 0 D a2 pdb 2 respects multiplication. It is now natural to expect that an element ˛ of Z Œ d is smooth if and only if ˛ 0 is smooth; so one may as well pass to their product N .˛/, which is an ordinary integer, and define ˛ to be smooth if jN .˛/j is built up from prime numbers that lie below a certain bound. The size of this bound depends on the circumstances; in the present computation we choose it empirically. Thepfirst steppin the algorithm is to find a good supply of smooth numbers a C b d in Z Œ d, or, equivalently, pairs of integers a, b for which a2 db 2 is smooth. One does this by tryingp b D 1, 2, 3, . . . in succession, and trying integers a in the neighborhood of b d; then ja2 db 2 j is fairly small, which increases its chance to be smooth. For example, with b D 1 one finds for a near p b d ‡ 2174:74 the following smooth values of a2 d: 21562 2162
2
21752
d D 2 7 11 17 31;
21782 2
d D 2 3 5 11 43;
d D 2 5 13 17;
2184
d D 2 3 7 312 ;
d D 3 13 29;
21872
d D 3 52 23 31:
3
For b D 2, 3, 4, one finds, restricting to values of a that are coprime to b: 43292
22 d D 3 5 172 41;
43992
22 d D 52 13 31 43;
43412
22 d D 3 5 173 ;
65142
32 d D 2 53 13 41;
43512
22 d D 52 232 ;
65242
32 d D 2 5 7 41;
43632
22 d D 132 17 41;
65382
32 d D 2 7 13 23 43;
43892
22 d D 3 5 7 11 13 23;
86992
42 d D 17 41:
SOLVING THE PELL EQUATION
15
The prime numbers occurring in these sixteen factorizations are the small prime d factors 2, 3, 7, 11, 29 of d, as well as the prime numbers p 43 with p D 1. It is only the latter primes that matter, and there are seven of them: 5, 13, 17, 23, 31, 41, and 43. It is important that the number of smooth expressions a2 db 2 exceeds the number of those primes, which is indeed the case: 16 > 7. If one uses only the prime numbers up to 31 and the eight factorizations that do not contain 41 or 43, there is still a good margin: 8 > 5. Thus, one decides to work with the “smoothness bound” 31. The next steppis to write p down the prime ideal factorizations of the eight numbers .a C b d/=.a b d/. Consider, for example, the case apD 2162, b D 1. Since 21622 d contains a factor 13, the element 2162 C d has a prime ideal factor of norm 13, p and from 2162 4 mod 13 one sees that this is thepprime ideal p13 D .13; 4p C d/; it is the kernel of the ring homomorphism Z Œ d ! Z =13 p Z sending d to 4 (mod p 13). The conjugate p prime ideal q13 D .13; 4 d/ then occurs in 2162 p d. Likewise, 2162C d ispdivisible by the cube prime ideal p5 D .5; 2Cp d/ and by p17 D .17;p 3C d/, and p of the d by q35 q17 , where q5 D .5; 2 d/ and q17 D .17; 3 d /. Finally, 2162 p p 2162 C d has the prime ideal factor .2; d/, but since 2 divides d, thispprime ideal equals its own conjugate, so it cancels when one divides 2162 C d by its conjugate. Altogether one finds the prime ideal factorization p p .2162 C d/=.2162 d/ D .p5 =q5 /3 .p13 =q13 / .p17 =q17 /: 2 2 As a second example, consider a D 4351, b D 2. We have 4351 p 2 d D 52 232 , and from 4351=2 2 mod 5 one sees that 4351 C 2 d belongs to q5 rather than mod 23 implies that it belongs to pp5 . Similarly, 4351=2 2 p p23 D .23; 2 C d/. Writing q23 D .23; 2 d/, one obtains p p .4351 C 2 d/=.4351 2 d / D .p5 =q5 / 2 .p23 =q23 /2 :
Doing this for all eight pairs a, b, one arrives at this table: p 2156 C pd 2162 C pd 2175 C pd 2184 C pd 2187 C p d 4341 C 2pd 4351 C 2pd 4389 C 2 d
5 13 17 23 31 0 3 0 0 2 1 2 1
0 1 1 0 0 0 0 1
1 1 0 0 0 3 0 0
0 0 0 0 1 0 2 1
1 0 0 2 1 0 0 0
1 1 0 0 1 0 0 1
0 0 0 9 2 9 0 5 0 10 0 3 1 5 2 0
16
HENDRIK W. LENSTRA, JR.
The first row lists the primep numbers p we are using. The first column lists the eight expressions ˛ D a C b d . In the ˛-th row and the p-th column, one finds 0 the exponent of pp =qp in the prime p ideal factorization of ˛=˛ p ; here pp , qp are as above, with p31 D .31; 14 C d/ and q31 D .31; 14 d/. Thus, each ˛ gives rise to an “exponent vector” that belongs to Z 5 . The third step in the algorithm is finding linear relations with integer coefficients between the eight exponent vectors. The set of such relations forms a free abelian group of rank 3, which is 8 minus the rank of the 85 matrix formed by the eight vectors. A set of three independent generators for the relation group is given in the last three columns of the preceding table; in general, one can find such a set by applying techniques of linear algebra over Z ; see [Lenstra 2008, Section 14]. In the final step of the algorithm one inspects the relations one by one. Consider for example the first relation.p It expresses that p the sum of the exponent vectors corresponding to 2156 C d and 2162 C d equals the sum of the p p exponent vectors for 2187 C d and 4389 C 2 d. In other words, if we put p p .2156 C d / .2162 C d/ ˛D p p ; .2187 C d/ .4389 C 2 d/ then the element " D ˛=˛ 0 has all exponents in its prime ideal p factorization equal p to 0. This is the same as saying that " is a unit x C y d of the ring Z Œ d; also, the norm ""0 D x 2 dy 2 of this unit equals N .˛/=N .˛ 0 / D 1, so we obtain an integral solution to Pell’s equation x 2 dy 2 D 1, except that it is uncertain whether x and y are positive. We can write " D ˛=˛ 0 D ˛ 2 =N .˛/, where the prime factorization of N .˛/ is available from the factorizations of a2 db 2 that we started with; one finds in this manner the following two power product representations of ": p p p p .2156 C d / .2162 C d/ .2187 d/ .4389 2 d/ "D p p p p .2156 d/ .2162 d / .2187 C d/ .4389 C 2 d/ p p 32 232 .2156 C d /2 .2162 C d/2 D : p p 22 172 .2187 C d/2 .4389 C 2 d/2 In the second representation, " is visibly a square, or, equivalently, N .˛/ is a square; this is a bad sign, since it is certain to happen when " D 1, in which case one has ˛ 2 Q , N .˛/ D ˛ 2 , x D 1, and y D 0. That is indeed what occurs here. (Likewise, it would have been a bad sign if " were visibly d times a square; this is certain to happen if " D 1.) In the present case, the numbers are small enough that one can directly verify that " D 1. For larger power products, one can decide whether " equals ˙1 by computing log j"j to a suitable precision
SOLVING THE PELL EQUATION
17
p and proving that the logarithm of a positive unit of Z Œ d cannot be close to 0 without being equal to 0. Thus, the first relation disappointingly gives rise to a trivial solution to the Pell equation. The reader may check that the unit p p 292 .4351 C 2 d/2 .4389 C 2 d/4 p 54 72 112 234 .2175 C d /4 obtained from the second relation is also equal to 1. The third relation yields the unit p p p p 24 514 .2175C d/18 .2184C d /10 .2187C d/20 .4341C2 d /6 D : p p 327 75 299 3120 .2162C d/18 .4351C2 d/10 Since this is not visibly a square, we can be certainpthat it is not 1. Since it is positive, it is not 1 either. So is of the form x Cy d , where x, y 2 Z satisfy x 2 dy 2 D 1 and y ¤ 0; thus, jxj, jyj solve Pell’s equation. From the power product, one computes the logarithm of the unit to be about 102:101583. This implies that > 1, so that ispthe largest of the four numbers , 0 D 1=,p , and 0 ; in other words, x Cy d is the largest of the four numbers ˙x ˙y d , which is equivalent to x and y being positive. In general one can achieve this by first replacing by if is negative, and next by 0 if < 1. We conclude that the power product defining does represent a solution to Pell’s equation. The next question is whether it is the fundamental solution. In the present case we can easily confirm this, since from Amthor’s computation we know that Rd ‡ 102:101583, and the logarithm of any nonfundamental solution would be at least 2Rd . Therefore, is equal to the solution u found by Amthor, and it is indeed fundamental. In particular, the numbers log ‡ 102:101583 and log u ‡ 102:101583 are exactly equal, not just to a precision of six decimals. The power product representation we found for is a little more compact than the standard representation we gave for u. Indeed, its length, as defined earlier, is about 93:099810, as compared to Rd ‡ 102:101583 for u. The power product .2175 C .2175
p
p p d/18 .2184 C d/10 .2187 C d/20 p p p d/18 .2184 d/10 .2187 d/20 p p p .4341 C 2 d/6 .2162 d /18 .4351 2 d/10 ; p p p .4341 2 d/6 .2162 C d /18 .4351 C 2 d/10
which also represents u, has length about 125:337907.
18
HENDRIK W. LENSTRA, JR.
8. Performance The smooth numbers method for solving Pell’s equation exemplified in the previous section can be extended to any value of d . There is unfortunately not much one can currently prove either about the run time or about the correctness of the method. Regarding the run time, however, one can make a reasonable conjecture. For x > e, write p L.x/ D exp .log x/ log log x: The conjecture is that, for some positive real number c9 and all d > 2, the smooth numbers method runs in time at most L.d/c9 . This is, at a doubly logarithmic level, the exact average of x c9 D exp.c9 log x/ and .log x/c9 D exp.c9 log log x/; so conjecturally, the run time of the smooth numbers method is in a sense halfway between exponential time and polynomial time. The main ingredient in the heuristic reasoning leading to the conjecture is the following theorem: for fixed positive real numbers c, c 0 , and x ! 1, the prob0 ability for a random positive integer x c (drawn from a uniform distribution) 0 to have all its prime factors L.x/c equals 1=L.x/c =.2c/Co.1/ . This theorem [Pomerance 2008b; Granville 2008] explains the importance of the function L in the analysis of algorithms depending on smooth numbers. Other ingredients of the heuristic run time analysis are the belief that the expressions a2 db 2 that one hopes to be smooth are so with the same probability as if they were random numbers, and the belief that the units produced by the algorithm have a substantial probability of being different from ˙1. These beliefs appear to be borne out in practice. p for Probably one can take c9 D 3= 8 C " in the conjecture just formulated, p any " > 0 and all d exceeding a bound depending on "; one has 3= 8 ‡ 1:06066. One of the bottlenecks is the time spent on solving a large sparse linear system over Z . If one is very optimistic about developingpa better algorithm for doing this, it may be possible to achieve 1 instead of 3= 8. The smooth numbers method needs to be supplemented with an additional technique if one wishes to be reasonably confident that the unit it produces is the fundamental solution to Pell’s equation. We forgo a discussion of this technique, since there is no satisfactory method for testing whether it achieves its purpose. More precisely, there is currently no known way of verifying in subexponential time that a solution to the Pell equation that is given by means of a power product is the fundamental one. The most promising technique for doing this employs the analytic class number formula, but its effectiveness depends on the truth of the generalized Riemann hypothesis. The latter hypothesis, abbreviated “GRH”, asserts that there does not exist an algebraic number field whose associated zeta
SOLVING THE PELL EQUATION
19
function has a complex zero with real part greater than 21 . The GRH can also be used to corroborate the heuristic run time analysis, albeit in a probabilistic setting. This leads to the following theorem. T HEOREM . There is a probabilistic algorithm that for some positive real number c10 has the following properties. (a) Given any positive integer d that is not a square, the algorithm computes a positive integer R that differs by less than 1 from some positive integer multiple m Rd of Rd . (b) If the GRH is true, then (a) is valid with m D 1. (c) If the GRH is true, then for each d > 2 the expected run time of the algorithm is at most L.d/c10 . The algorithm referred to in the theorem is probabilistic in the sense that it employs a random number generator; every time the random number generator is called, it draws, in unit time, a random bit from the uniform distribution, independently of previously drawn bits. The run time and the output of a probabilistic algorithm depend not only on the input, but also on the random bits that are drawn; so given the input, they may be viewed as random variables. In the current case, the expectation of the run time for fixed d is considered in part (c) of the theorem, and (a) and (b) describe what we know about the output. In particular, the algorithm always terminates, and if GRH is true, then it is guaranteed to compute an integer approximation to the regulator. The theorem just stated represents the efforts of several people; up-to-date lists of references being provided in p [Vollmer 2002; 2003]. According to the latter work, one may take c10 D 3= 8 C " for any " > 0 and all d exceeding a bound depending on ". The last word on algorithms for solving Pell’s equation has not been spoken yet. Very recently, a quantum algorithm was exhibited [Hallgren 2002; Schmidt and Vollmer 2005] that computes, in polynomial time, a power product representing the fundamental solution. This algorithm depends on infrastructure, but not on smooth numbers. For practical purposes, the smooth numbers method will remain preferable until quantum computers become available.
Acknowledgments This article was written while I held the 2000–2001 HP-MSRI Visiting Research Professorship. I thank Sean Hallgren, Mike Jacobson, Jr., and Ulrich Vollmer for answering my questions, and Bart de Smit for providing numerical assistance. A special word of thanks is due to Hugh Williams, whose version [2002] of the same story contains many details omitted in mine.
20
HENDRIK W. LENSTRA, JR.
References [Archimedes 1999] Archimedes, The cattle problem, in English verse by S. J. P. Hillion and H. W. Lenstra jr., Mercator, Santpoort, 1999. [Brent 1976] R. P. Brent, “Fast multiple-precision evaluation of elementary functions”, J. Assoc. Comput. Mach. 23:2 (1976), 242–251. [Buchmann and Vollmer 2006] J. Buchmann and U. Vollmer, “A Terr algorithm for computations in the infrastructure of real-quadratic number fields”, J. Th´eor. Nombres Bordeaux 18:3 (2006), 559–572. [Buchmann et al. 1995] J. Buchmann, C. Thiel, and H. Williams, “Short representation of quadratic integers”, pp. 159–185 in Computational algebra and number theory (Sydney, 1992), edited by W. Bosma and A. van der Poorten, Math. Appl. 325, Kluwer Acad. Publ., Dordrecht, 1995. [Buhler and Wagon 2008] J. P. Buhler and S. Wagon, “Basic algorithms in number theory”, pp. 25–68 in Surveys in algorithmic number theory, edited by J. P. Buhler and P. Stevenhagen, Math. Sci. Res. Inst. Publ. 44, Cambridge University Press, New York, 2008. [Dickson 1920] L. E. Dickson, History of the theory of numbers, vol. II, Diophantine analysis, Carnegie Institution, Washington, DC, 1920. [Dijksterhuis 1956] E. J. Dijksterhuis (editor), The Arenarius of Archimedes with glossary, Textus minores 21, Brill, Leiden, 1956. [Euler 1770] L. Euler, Vollst¨andige Anleitung zur Algebra, Zweyter Theil, Kays. Acad. der Wissenschaften, St. Petersburg, 1770. Reprinted in Opera mathematica, ser. I, vol. 1, Teubner, Leipzig, 1911; translated as Elements of algebra, Springer, New York, 1984. [Fraser 1972] P. M. Fraser, Ptolemaic Alexandria, Oxford University Press, Oxford, 1972. [Ge 1993] G. Ge, Algorithms related to multiplicative representations, Ph.D. thesis, University of California, Berkeley, 1993. [Ge 1994] G. Ge, “Recognizing units in number fields”, Math. Comp. 63:207 (1994), 377–387. [Granville 2008] A. Granville, “Smooth numbers: computational number theory and beyond”, pp. 267–323 in Surveys in algorithmic number theory, edited by J. P. Buhler and P. Stevenhagen, Math. Sci. Res. Inst. Publ. 44, Cambridge University Press, New York, 2008. [Grosjean and De Meyer 1991] C. C. Grosjean and H. E. De Meyer, “A new contribution to the mathematical study of the cattle-problem of Archimedes”, pp. 404–453 in Constantin Carath´eodory: an international tribute, vol. I, edited by T. M. Rassias, World Sci. Publishing, Teaneck, NJ, 1991.
SOLVING THE PELL EQUATION
21
[Hallgren 2002] S. Hallgren, “Polynomial-time quantum algorithms for Pell’s equation and the principal ideal problem”, pp. 653–658 in Proceedings of the Thirty-Fourth Annual ACM Symposium on Theory of Computing, ACM, New York, 2002. [Heiberg 1913] J. L. Heiberg (editor), Archimedis opera omnia cum commentariis Eutocii, vol. II, Teubner, Leipzig, 1913. Reprinted Stuttgart, 1972. [Hua 1942] L.-K. Hua, “On the least solution of Pell’s equation”, Bull. Amer. Math. Soc. 48 (1942), 731–735. Reprinted as pp. 119–123 in Selected papers, Springer, New York, 1983. [Konen 1901] H. Konen, Geschichte der Gleichung t 2 1901.
Du2 D 1, S. Hirzel, Leipzig,
[Krumbiegel and Amthor 1880] B. Krumbiegel and A. Amthor, “Das Problema Bovinum des Archimedes”, Historisch-literarische Abteilung der Zeitschrift f¨ur Mathematik und Physik 25 (1880), 121–136, 153–171. [Lagrange 1773] J.-L. de la Grange, “Solution d’un probl`eme d’arithm´etique”, M´elanges de philosophie et de math. de la Soci´et´e Royale de Turin 4 (1766–1769) (1773), 44–97. This paper was written and submitted for publication in 1768, and it appeared in 1773; see [Weil 1984, pp. 314–315]. Reprinted in Lagrange’s Œuvres, vol. I, Gauthier-Villars, Paris, 1867, 669–731. [Lenstra 1982] H. W. Lenstra, Jr., “On the calculation of regulators and class numbers of quadratic fields”, pp. 123–150 in Journ´ees arithm´etiques (Exeter, 1980), edited by J. V. Armitage, London Math. Soc. Lecture Note Ser. 56, Cambridge Univ. Press, Cambridge, 1982. [Lenstra 2008] H. W. Lenstra, Jr., “Lattices”, pp. 127–181 in Surveys in algorithmic number theory, edited by J. P. Buhler and P. Stevenhagen, Math. Sci. Res. Inst. Publ. 44, Cambridge University Press, New York, 2008. [Lessing 1773] G. E. Lessing, “Zur Griechischen Anthologie”, pp. 419–446 in Zur Geschichte und Litteratur: Aus den Sch¨atzen der Herzoglichen Bibliothek zu Wolfenb¨uttel, Zweyter Beytrag, F¨urstl. Waysenhaus-Buchhandlung, Braunschweig, 1773. Appears on pp. 99–115 of his S¨amtliche Schriften, edited by K. Lachmann, 3rd ed., v. 12, G. J. G¨oschen, Leipzig, 1897, reprinted by de Gruyter, Berlin 1968. [Nelson 1980/81] H. L. Nelson, “A solution to Archimedes’ cattle problem”, J. Recreational Math. 13:3 (1980/81), 162–176. [Niven et al. 1991] I. Niven, H. S. Zuckerman, and H. L. Montgomery, An introduction to the theory of numbers, Wiley, New York, 1991. [Pomerance 2008a] C. Pomerance, “Elementary thoughts on discrete logarithms”, pp. 385–396 in Surveys in algorithmic number theory, edited by J. P. Buhler and P. Stevenhagen, Math. Sci. Res. Inst. Publ. 44, Cambridge University Press, New York, 2008. [Pomerance 2008b] C. Pomerance, “Smooth numbers and the quadratic sieve”, pp. 69–81 in Surveys in algorithmic number theory, edited by J. P. Buhler and P.
22
HENDRIK W. LENSTRA, JR.
Stevenhagen, Math. Sci. Res. Inst. Publ. 44, Cambridge University Press, New York, 2008. [Schmidt and Vollmer 2005] A. Schmidt and U. Vollmer, “Polynomial time quantum algorithm for the computation of the unit group of a number field”, pp. 475– 480 in STOC’05: Proceedings of the 37th Annual ACM Symposium on Theory of Computing, ACM, New York, 2005. Extended abstract; full version, Technische Univ. Darmstadt preprint TI-04-01; available at www.cdc.informatik.tu-darmstadt.de /reports/TR/TI-04-01.qalg unit group.pdf. [Sch¨onhage 1971] A. Sch¨onhage, “Schnelle Berechnung von Kettenbruchentwicklungen”, Acta Inform. 1 (1971), 139–144. [Schoof 1982] R. J. Schoof, “Quadratic fields and factorization”, pp. 235–286 in Computational methods in number theory, vol. II, edited by H. W. Lenstra, Jr. and R. Tijdeman, Math. Centre Tracts 155, Math. Centrum, Amsterdam, 1982. [Schoof 2008] R. J. Schoof, “Computing Arakelov class groups”, pp. 447–495 in Surveys in algorithmic number theory, edited by J. P. Buhler and P. Stevenhagen, Math. Sci. Res. Inst. Publ. 44, Cambridge University Press, New York, 2008. [Shanks 1972] D. Shanks, “The infrastructure of a real quadratic field and its applications”, pp. 217–224 in Proceedings of the Number Theory Conference (Boulder, CO, 1972), Univ. Colorado, Boulder, Colo., 1972. [Stevenhagen 2008a] P. Stevenhagen, “The arithmetic of number rings”, pp. 209–266 in Surveys in algorithmic number theory, edited by J. P. Buhler and P. Stevenhagen, Math. Sci. Res. Inst. Publ. 44, Cambridge University Press, New York, 2008. [Stevenhagen 2008b] P. Stevenhagen, “The number field sieve”, pp. 83–100 in Surveys in algorithmic number theory, edited by J. P. Buhler and P. Stevenhagen, Math. Sci. Res. Inst. Publ. 44, Cambridge University Press, New York, 2008. [Vardi 1998] I. Vardi, “Archimedes’ cattle problem”, Amer. Math. Monthly 105:4 (1998), 305–319. [Vollmer 2002] U. Vollmer, “An accelerated Buchmann algorithm for regulator computation in real quadratic fields”, pp. 148–162 in Algorithmic Number Theory, ANTSV, edited by C. Fieker and D. R. Kohel, Lecture Notes in Computer Science 2369, Springer, New York, 2002. [Vollmer 2003] U. Vollmer, Rigorously analyzed algorithms for the discrete logarithm problem in quadratic number fields, Ph.D. thesis, Technische Univ. Darmstadt, Fachbereich Informatik, 2003. Available at http://elib.tu-darmstadt.de/diss/000494/. [Weil 1984] A. Weil, Number theory: an approach through history, Birkh¨auser, Boston, 1984. [Whitford 1912] E. E. Whitford, The Pell equation, self-published, New York, 1912. [Williams 2002] H. C. Williams, “Solving the Pell equation”, pp. 397–435 in Number theory for the millennium (Urbana, IL, 2000), vol. 3, edited by M. A. Bennett et al., A K Peters, Natick, MA, 2002.
SOLVING THE PELL EQUATION
H ENDRIK W. L ENSTRA , J R . M ATHEMATISCH I NSTITUUT U NIVERSITEIT L EIDEN P OSTBUS 9512 2300 RA L EIDEN T HE N ETHERLANDS
[email protected]
23
Algorithmic Number Theory MSRI Publications Volume 44, 2008
Basic algorithms in number theory JOE BUHLER AND STAN WAGON Algorithmic complexity Multiplication Exponentiation Euclid’s algorithm Primality Quadratic nonresidues Factoring, Pollard Discrete logarithms Modular square roots Diophantine equations Euclid’s algorithm Extended Euclid
26 26 28 30 31 36 36 38 40 42 42 43
Continued fractions Rational approximation Modular polynomial equations Cantor–Zassenhaus Equations modulo p n Chinese remainder theorem Quadratic extensions Cipolla Lucas–Lehmer Units in quadratic fields Smith–Cornacchia Bibliography
45 48 51 52 53 57 57 58 59 61 64 66
1. Introduction Our subject combines the ancient charms of number theory with the modern fascination with algorithmic thinking. Newcomers to the field can appreciate this conjunction by studying the many elementary pearls in the subject. The aim here is to describe a few of these gems with the combined goals of providing background for subsequent articles in this volume, and luring the reader into pursuing full-length treatments of the subject, such as [Bach and Shallit 1996; Bressoud and Wagon 2000; Cohen 1993; Crandall and Pomerance 2005; Knuth 1981; von zur Gathen and Gerhard 2003; Shoup 2005]. Many details will be left to the reader, and we will assume that he or she knows (or can look up) basic facts from number theory, algebra, and elementary programming. We tend to focus more on the mathematics and less on the sometimes fascinating algorithmic details. However, the subject is grounded in, and motivated by, examples; one can learn interesting and surprising things by actually implementing algorithms in number theory. Implementing almost any of the algorithms here in a modern programming language isn’t too hard; we encourage budding 25
26
JOE BUHLER AND STAN WAGON
number theorists to follow the venerable tradition of their predecessors: write programs and think carefully about the output.
2. Algorithmic complexity Algorithms take input and produce output. The complexity of an algorithm A is a function CA .n/, defined to be the maximum, over all input I of size at most n, of the cost of running A on input I . Cost is often measured in terms of the number of “elemental operations” that the algorithm performs and is intended, in suitable contexts, to approximate the running time of actual computer programs implementing these algorithms. A formalization of these ideas requires precise definitions for “algorithm,” “input,” “output,” “cost,” “elemental operation,” and so on. We will give none. Instead, we consider a series of number-theoretic algorithms and discuss their complexity from a fairly naive point of view. Fortunately, this informal and intuitive approach is usually sufficient for purposes of algorithmic number theory. More precise foundations can be found in many texts on theoretical computer science or algorithmic complexity such as [Garey and Johnson 1979; Hopcroft and Ullman 1979; Kozen 2006]. The first problem arises in elementary school. P ROBLEM 1. M ULTIPLICATION : Given integers x and y, find their product xy. From the algorithmic perspective, the problem is woefully underspecified. We interpret it in the following natural (but by no means only possible) way. An algorithm that solves M ULTIPLICATION takes two strings of symbols as input and writes a string of symbols as its output. The input strings are base b representation of integers x and y, where b > 1 is fixed, and in practice one might expect b D 2, 10, 232 , or 264 . The algorithm follows a well-defined procedure in which the next step is determined by the current state of the computation; one might imagine a program written in an idealized form of your favorite computer language that has access to unlimited memory. Its output string represents the base-b representation of the product xy. The natural notion of the size of an integer x is the total number of symbols (base-b digits) in the input, perhaps augmented by a small constant to allow for delimiting the integer and specifying its sign. For definiteness, we define the base-b size of x to be sizeb .x/ WD 1 C dlogb .1 C jxj/e; where logb is the logarithm to the base b, and due is the ceiling of u — the smallest integer greater than or equal to u.
BASIC ALGORITHMS IN NUMBER THEORY
27
The size of an integer x is O.log jxj/, where g.x/ D O.f .x// is a shorthand statement saying that g is in the class of functions such that there is a constant C with jg.x/j C jf .x/j for sufficiently large x. Note that O.loga x/ D O.logb x/ for a; b > 1. In particular, if we are interested in complexity only up to a constant factor the choice of b > 1 is irrelevant. The usual elementary school multiplication algorithm uses O.n2 / digit operations to multiply two input integers of size n. More precisely, if x and y have size n, then approximately n2 digit-sized multiplications and n additions of ndigit intermediate products are required. Since adding n-digit integers takes time O.n/, the overall complexity of multiplying n-digit integers using this algorithm is O.n2 /CnO.n/ D O.n2 /: Notice that the O-notation gracefully summarizes an upper bound on the running time of the algorithm — the complexity O.n2 / is independent of the base b, the precise details of measuring the size of an integer, the definition of size of two inputs (as the maximum of the two integer inputs, or the total of their sizes), and so on. An algorithm A is said to take polynomial time if its complexity CA .n/ is O.nk / for some integer k. Although this is a flexible definition, with unclear relevance to computational practice, it has proved to be remarkably robust. In fact, it is sometimes reasonable to take “polynomial time” as synonymous with “efficient,” and in any case the notion has proved useful in both theory and practice. Once it is known that a problem can be solved in polynomial time, it is interesting to find the smallest possible exponent k. Several improvements to the O.n2 / multiplication algorithm are known, and the current state of the art is a striking algorithm of Sch¨onhage [1971] that takes time O.n log n log log n/ to multiply two n-digit integers. This is sometimes written inexactly as O.n1C" /, where " denotes an arbitrarily small positive number. Note that O.n/ is an obvious lower bound since the input has size O.n/ and in order to multiply integers it is necessary to read them. Algorithms that use the Sch¨onhage algorithm, or related ones, are said to use fast arithmetic, and algorithms that are close to obvious lower bounds are sometimes said to be asymptotically fast. Many such algebraic and arithmetic algorithms are known (see [Bernstein 2008] for examples), and they are becoming increasingly important in computational practice. The elemental operations above act on single digits (i.e., bits if b D 2), and the resulting notion of complexity is sometimes called bit complexity. In other contexts it might be more useful to assume that any arithmetic operation takes constant time on integers of arbitrary size; this might be appropriate, for example, if all integers are known to fit into a single computer word. When complexity of an algorithm is defined by counting arithmetic operations, the
28
JOE BUHLER AND STAN WAGON
results is said to be the arithmetic complexity of the algorithm. In this model the cost of a single multiplication is O.1/, reminding us that complexity estimates depends dramatically on the underlying assumptions. P ROBLEM 2. E XPONENTIATION : Given x and a nonnegative integer n, compute x n . Again, the problem is underspecified as it stands. We will assume that x is an element of a set that has a well-defined operation (associative with an identity element) that is written multiplicatively; moreover, we will measure cost as the number of such operations required to compute x n on input x and n. The size of the input will be taken to be the size of the integer n. Although x 16 can be computed with 15 multiplications in an obvious way, it is P faster to compute it by 4 squarings. More generally, the binary expansion n D ai 2i , with ai 2 f0; 1g, implies that x n D x a0 .x 2 /a1 .x 4 /a2
(2-1)
which suggests a clever way to interleave multiplications and squarings: R IGHT- TO - LEFT E XPONENTIATION Input: x as above, and a nonnegative integer n Output: x n 1. y WD 1 2. While n > 0 if n is odd, y WD xy // ai is 1 2 x WD x , n WD bn=2c 3. Return y Here “WD” denotes assignment of values to variables, “==” indicates a comment, “1” denotes the identity for the operation, and the floor buc is the largest integer less than or equal to u. The correctness of the algorithm is reasonably clear from k equation (2-1) since x 2 is multiplied into y if and only if the kth bit ak of the binary expansion of n is nonzero. This can be proved more formally by showing by induction that at the beginning of Step 2, X N D x n y holds, where X and N denote the initial values of the variables x and n. When n is 0 equation says that X N D y, so that y is the desired power. The usual inductive definition of Exp.x; n/ WD x n gives an obvious recursive algorithm: 8 if n D 0; <1 if n > 0 is even; Exp.x; n/ D Exp.x 2 ; n=2/ (2-2) : 2 x Exp.x ; .n 1/=2/ if n is odd.
BASIC ALGORITHMS IN NUMBER THEORY
29
Experienced programmers often implement recursive versions of algorithms because of their elegance and obvious correctness, and when necessary convert them to equivalent, and perhaps faster, iterative (nonrecursive) algorithms. If this is done to the recursive program the result is to Right-to-Left algorithm above. Curiously, if the inductive definition is replaced by the mathematically equivalent algorithm in which squaring follows the recursive calls, 8 if n D 0; <1 2 if n > 0 is even, Exp.x; n/ D Exp.x; n=2/ (2-3) : x Exp.x; .n 1/=2/2 if n is odd, then the corresponding iterative algorithm is genuinely different. L EFT- TO -R IGHT E XPONENTIATION Input: x, a nonnegative integer n, a power of two m D 2a such that m=2 n < m Output: x n 1. y WD 1 2. While m > 1 m WD bm=2c; y WD y 2 If n m then y WD xy; n WD n m 3. Return y Correctness follows inductively by proving that at the beginning of Step 2, n < m and y m x n D x N . In contrast to the earlier algorithm, this version consumes the bits ai in the binary expansion of n starting with the leftmost (most significant) bit. The complexity of any of the versions of this algorithm (collectively called E XP in the sequel) is O.log n/ since the number of operations is bounded by 2 size2 .n/. As will be seen, this remarkable efficiency has numerous applications in algorithmic number theory. Note that the naive idea of computing x n by repeatedly multiplying by x takes time O.n/, which is exponential in the input size. R EMARK 1. In a specific but important practical case the left-to-right version of E XP is better than the right-to-left version. Suppose that our operation is “multiplying modulo N ” and that x is small relative to N . Then multiplying by the original x is likely to take less time than modular multiplication by an arbitrary integer X in the range 0 X < N . The left-to-right version preserves the original x (though the squarings involve arbitrary integers), whereas the right-to-left version modifies x and hence performs almost all operations on arbitrary elements. In other words, with a different computational model (bit
30
JOE BUHLER AND STAN WAGON
complexity, with the specific underlying operation “multiply modulo N ”, and x small) the left-to-right algorithm, either recursive or iterative, is significantly better than right-to-left exponentiation. R EMARK 2. If the underlying operation is multiplication of integers, the bit complexity of computing x n is exponential, since the output has size that is exponential in the input size log n. Any algorithm will be inefficient, illustrating yet again the dependence on the underlying computational model. This discussion of calculating powers barely scratches the surface of a great deal of theoretical and practical work. The overwhelming importance of exponentiation has led to many significant practical improvements; perhaps the most basic is to replace the base-2 expansion of n with a base-b expansion for b a small power of 2. On the theoretical side, there are interesting results on finding the absolutely smallest possible number of operations required to compute x n [Knuth 1981]. P ROBLEM 3. GCD: Given positive integers a and b, find the largest integer that is a divisor of both a and b. The greatest common divisor (GCD) is denoted gcd.a; b/. Perhaps the most famous number-theoretic algorithm of all is due to Euclid. E UCLID ’ S A LGORITHM Input: Positive integers a and b Output: gcd.a; b/ While b > 0 fa; bg := fb; a mod bg Return a Here r D a mod b is the remainder when a is divided by b, i.e., the unique integer r , 0 r < b, such that there is a q with a D qb C r . The simultaneous assignment statement fa; bg D fb; a mod bg could be implemented in a more prosaic programming language by something along the lines of the three statements temp WD b, b D a mod b, a D temp. The correctness of the algorithm can be verified by showing that the GCD of a and b doesn’t change at each step of the algorithm, and that when a becomes divisible by b, then b is the GCD. Just as with multiplication, the remainder a mod b can be found in time log2 .max.a; b// with straightforward algorithms, and time log1C" max.a; b/ for any positive " if asymptotically fast algorithms are used. (Here logr x is shorthand for .log x/r .) It isn’t too hard to work out that a > b after one step of the algorithm, and then the smallest number is halved in (at most) two steps of the algorithm. This means that the number of times that the loop is executed (the number of remainder operations) is bounded by O.log max.a; b//. Thus the
BASIC ALGORITHMS IN NUMBER THEORY
31
algorithm has complexity O.k 3 / on k-bit input, or O.k 2C" / if fast arithmetic is used. We will have much much more to say about Euclid’s algorithm later. If gcd.a; b/ D 1 then a and b are said to be relatively prime, or coprime. P ROBLEM 4. P RIMALITY: Given a positive integer n > 1, is n is a prime? The is an example of a decision problem, for which the output is either “yes” or “no.” Perhaps the most straightforward algorithm for P RIMALITY is the trial division method: for d D 2; 3; : : : ; test whether n is divisible by d. If n is divisible p p by some d n then n is composite; if n is not divisible by any d n then n p is prime. The time required is O. n/, which is an exponential function of the size of n, and this algorithm is impractical for even moderately large n. Fermat’s Little Theorem says that if n is a prime and a is coprime to n then n a 1 1 mod n, which implies that the order of a in .Z=nZ/ / divides n 1. The condition an 1 1 mod n is easy to check, using E XP in .Z=nZ/ . R EMARK 3. We take the opportunity to remind the reader that two integers are congruent modulo n if their difference is divisible by n, that Z=nZ denotes the set of n classes under this equivalence relation, and that Z=nZ is a ring under the natural addition and multiplication operations induced from operations on the integers. Moreover, .Z=nZ/ denotes the group of units in this ring, i.e., the set of congruence classes containing integers that are coprime to n, under the operation of multiplication. This group has .n/ elements, where .n/ is the Euler-phi function — the number of positive integers less than n that are coprime to n. Finally, we let a mod n denote the class of Z=nZ that contains a. We will tolerate the conflict with earlier usage because the meaning can be disambiguated from context: if a mod n is an integer then the remainder is intended, and if a mod n lies in Z=nZ then the congruence class is intended. In the favorable circumstance in which the prime factorization of n 1 is known, Fermat’s Little Theorem can be turned on its head to give a proof of primality. T HEOREM 5. If a and n are integers such that an 1 1 mod n, and a.n 1 mod n for all prime divisors q of n 1, then n is prime.
1/=q
6
P ROOF. As noted above, the congruence an 1 1 mod n implies that the order of a mod n in .Z=nZ/ , which we will denote by ordn .a/, is a divisor of n 1. Any proper divisor of n 1 is a divisor of .n 1/=q for some prime q. The second condition of the theorem says that ord.a/ does not divide .n 1/=q for any q, and we conclude that ord.a/ D n 1. Thus .Z=nZ/ has n 1 elements and n is a prime, as claimed. A generalization of this theorem due to Pocklington says that only a partial p factorization of n 1 is necessary: if m is a divisor of n 1 with m > n, then
32
JOE BUHLER AND STAN WAGON
n is a prime if an 1 1 mod n, and a.n 1/=q 6 1 mod n for prime divisors q of m. Loosely, this says that primality is easy to test if n 1 is half-factored. Unfortunately, this does not give an efficient primality test: For large n no algorithm is known that efficiently factors or half-factors n 1. As a first try, observe that if n is composite then an 1 should be, intuitively, a random integer modulo n. Thus we could choose several random a and report “probable prime” when Fermat’s congruence an 1 1 mod n holds for all a, and “composite” if it fails for any one of them. Unfortunately, there are positive composite integers called Carmichael numbers (e.g., n D 561 D 3 11 17; see [Crandall and Pomerance 2005]) such that the congruence holds for all a that are relatively prime to n. As a second try, we “take the square root” of the Fermat congruence. To explain this it is convenient to review Legendre and Jacobi symbols. An integer a is a quadratic residue modulo an odd prime p if it is a nonzero square, i.e., if there is an x (not divisible by p) such that x 2 a mod p. Nonsquares are said to be quadratic nonresidues. This information is encoded in the Legendre symbol 8 < 0 if p divides a; a 1 if x is a quadratic residue mod p; D p : 1 if x is a quadratic nonresidue mod p: Euler’s Criterion gives an explicit congruence for the Legendre symbol a a.p 1/=2 mod p; p
(2-4)
from which the symbol can be computed efficiently using E XP in .Z=p Z/ . The Jacobi symbol generalizes the Legendre symbol and is defined, for an integer a and a positive odd positive integer b, by reverting to the Legendre symbol whenQb is an odd prime, and enforcing multiplicativity in the denominator; if b D b ep is the factorization of b into prime powers, then Y ep a a D : b p p
The Jacobi symbol is multiplicative in both the numerator and denominator, depends only on a mod b, and obeys the famous law of quadratic reciprocity a b D . 1/..a 1/.b 1//=4 ; a; b odd: b a Moreover, two “supplementary laws” hold: 2 1 2 D . 1/.b 1/=2 ; D . 1/.b b b
1/=8
:
BASIC ALGORITHMS IN NUMBER THEORY
33
This leads to a natural recursive algorithm, using the identities a mod b a D ; b b 2 a a=2 D . 1/.b 1/=8 ; b b a b D . 1/.a 1/.b 1/=4 ; b a applicable, respectively, when a < 0 or a b, a is even, or a is odd. The crucial point is that this is an efficient algorithm even if the factorization of b is unknown. An actual implementation of this resembles Euclid’s algorithm augmented with some bookkeeping. k
R EMARK 4. Let n D Fk WD 22 C 1 be the kth Fermat number. The reader may enjoy using Theorem 5 and quadratic reciprocity to show that if n D Fk and 3.n 1/=2 1 mod n then n is prime, and to prove that if n is prime then the congruence holds. Now we return to the problem of giving an efficient algorithm for primality. We use Euler’s congruence a a.p 1/=2 mod p p together with one of the seminal ideas of twentieth century computer science: it can be beneficial to allow algorithms to make random moves! A probabilistic (randomized) algorithm extends our earlier implicit notion of an algorithm in that such an algorithm is allowed to flip a coin as needed and make its next move depending on the result. Typically a coin flip is deemed to cost one unit of running time. It is possible to model this more formally by thinking of the sequence of random bits b 2 f0; 1g as a second input string to the algorithm. Saying that an algorithm has a property with probability p means that it has the property for a fraction p of the possible auxiliary input strings (coin flips). For example, if the algorithm returns a correct answer for two-thirds of all possible input bit strings, then we say that the probability of correctness is p D 2=3. It may seem worse than useless to allow algorithms to make random moves unrelated to the problem at hand but, as we will see, this additional capability can be surprisingly powerful. R EMARK 5. Probabilistic algorithms are ubiquitous. In circumstances in which it is necessary to emphasize that an algorithm is not probabilistic it will be referred to as a deterministic algorithm. The following primality test is a famous example of a probabilistic algorithm.
34
JOE BUHLER AND STAN WAGON
S OLOVAY–S TRASSEN PRIMALITY TEST Input: A positive integer k, an odd integer n > 1 Output: “Prime” or “Composite” 1. For i D 1; 2; : : : ; k Choose a randomly from f1; 2; : : : ; n 1g a a If D 0 or ¤ a.n 1/=2 mod n then n n Output “Composite” and halt. 2. Output “Prime” R EMARK 6. The algorithm chooses a uniformly random element of a finite set. The reader may enjoy the puzzle of figuring out how to do this using coin flips, i.e., events of probability 1=2. The goal is to simulate an arbitrary probability, and to do so in an efficient manner, e.g., to use about log n coin flips on average to choose a uniformly random element from an n-element set. There are different flavors of probabilistic algorithms, according to whether the output (or some possible outputs) are true or just highly likely to be true, whether running times are bounds, or merely expected running times, etc. The following theorem clarifies this in the case of Solovay–Strassen, e.g., showing that the answer “Composite” is always true, the answer “Prime” is highly likely to be true, and the running time is polynomial in the input size and absolute value of the logarithm of the error probability. T HEOREM 6. The Solovay–Strassen algorithm returns “Prime” if n is prime, and returns “Composite” with probability at least 1 1=2k if n is composite. Its expected running time is bounded by a polynomial in log n and k. P ROOF. (Sketch.) The GCD and exponentiation steps can be done it at most time O.log3 n/, so each iteration takes polynomial time. The only aspect of the running time that depends on the input bits is the choice of a random element of the .Z=nZ/ . If n is prime, then Euler’s congruence implies that the test returns “Prime.” If n is composite then the algorithm will return “Composite” unless each of k random choices of a lies in the subgroup n o a E.n/ WD a 2 .Z=nZ/ W a.n 1/=2 mod n n of the multiplicative group .Z=nZ/ , sometimes called the group of “Euler liars” for the composite number n. The coin flips done by the algorithm allow it to choose a uniformly random element of .Z=nZ/ . It can be shown [Bach and Shallit 1996] that jE.n/j .n/=2, so that the chance of a composite number passing all k of these tests is at most 1 1=2k .
BASIC ALGORITHMS IN NUMBER THEORY
35
The technique of repeating a test to quickly drive the probability of bad behavior to near zero is common in practical probabilistic algorithms. For example, the probability of failure in one round of Solovay–Strassen is at most 1=2, and that means that the probability of failure after k independent tests is at most 1=2k , so assuring the failure probability of at most " requires log.1="/ tests. The Jacobi symbol compositeness test inside the Solovay–Strassen algorithm can be improved. If n is odd, a is coprime to n, and n 1 D 2k r , where r is odd, k let A WD ar mod n. If n is prime then, by Fermat’s Theorem, A2 1 mod n. Since the only square roots of 1 in a field are ˙1, we know that if n is prime then k either A D 1, or else 1 occurs in the sequence A; A2 ; A4 ; : : : ; A2 D 1 mod n. If this happens we say that n is an strong probable prime to the base a. j If this does not happen, i.e., either A ¤ 1 mod n and A2 ¤ 1 mod n for 0 j < k, then n is definitely composite, and we say that a is a witness of n’s compositeness. The obvious generalization of Solovay–Strassen using this test is to choose a series of a’s, and see whether one is a witness of n’s compositeness or n is a strong probable prime to all of those bases. This is sometimes called the strong Fermat test. Here are several nontrivial results related to the above ideas; for a discussion and details see [Crandall and Pomerance 2005] and [Damg˚ard et al. 1993]. (i) If an odd n is composite then at least 3=4 of the a’s coprime to n are witnesses to n being composite. (ii) If a famous conjecture in number theory is true — the Extended Riemann Hypothesis (ERH) — then any odd composite n has a witness that is less than 2 log2 n. In particular, if we assume the ERH then there is a polynomial-time primality test (this also applies to the Solovay–Strassen test). (iii) Let n be an odd integer chosen uniformly randomly between 2k and 2kC1 , and let a be chosen randomly among integers between 1 and n that are coprime to n. If n is a strong probablepprime to the base a then the probability that a is composite is less than k 2 =4 k 2. Thus for large n a single probable prime test can give high confidence of primality. These ideas can provide overwhelming evidence that a given integer is prime, but no rigorous proof. If we want absolute proof, the story changes. Many proof techniques have been considered over the years, and this continues to be an active area for practical work. The central theoretical question is whether primality can be proved in polynomial time, and this was settled dramatically in 2002 when Manindra Agrawal, Neeraj Kayal, and Nitin Saxena [Agrawal et al. 2004] discovered a deterministic polynomial-time algorithm. For a description of this, and algorithms currently used in practice, see [Schoof 2008b].
36
JOE BUHLER AND STAN WAGON
P ROBLEM 7. Q UADRATIC N ONRESIDUES : Given an odd prime p, find a quadratic nonresidue modulo p. This simple problem illustrates stark differences between theory and practice, and deterministic and probabilistic algorithms. If p is an odd prime there are .p 1/=2 quadratic residues and .p 1/=2 nonresidues mod p, so if a mod p is nonzero, then a has a 50/50 chance of being a nonresidue. Moreover, quadratic residuosity/nonresiduosity is easy to establish by calculating a Legendre symbol. Thus there is an obvious probabilistic algorithm: repeatedly choose a at random until a nonresidue is found. With overwhelming likelihood a nonresidue will be found quickly, and thus quadratic nonresidues can be found quickly with a probabilistic polynomialtime algorithm. However, no deterministic polynomial-time algorithm is known. Nothing seems to be better than testing a D 2; 3; : : : until arriving at a nonresidue. There are heuristic grounds to think that there is a nonresidue a D O.log1C" p/. However, the best that can be proved is that one finds a nonresidue a D O.p 1=4 /. The simple-minded (deterministic) algorithm for finding a nonresidue could take exponential time. It is known that if the Extended Riemann Hypothesis is true, then there is a nonresidue a < 2 log2 p D O.log2 p/ [Bach 1990]. P ROBLEM 8. FACTORING : Given a positive integer n > 1, find a proper divisor m of n, i.e., a divisor m such that 1 < m < n. Factoring appears to be much harder than primality, both in theory and practice. Trial division again gives an obvious algorithm that is impractical unless n has a small divisor. The problem has fascinated mathematicians for centuries, and a vast menagerie of algorithms are known. Details of two of the most important current algorithms are described elsewhere in this volume [Poonen 2008; Stevenhagen 2008b]. Both algorithms require the use of sophisticated mathematical ideas: one requires the use of elliptic curves, and the other relies extensively on algebraic number theory. We now describe a striking factoring algorithm, called the Pollard algorithm, due to John Pollard [1978]. Let n be a composite integer that is not a prime power. (It is easy to check whether or not n is a perfect power by taking sufficiently accurate kth roots for 2 k log2 n.) Let f W Z=nZ ! Z=nZ;
x ‘ f .x/ D x 2 C 1;
and let f k .x/ D f .f . f .x/ // denote the kth iterate of f applied to x. The Pollard algorithm is:
BASIC ALGORITHMS IN NUMBER THEORY
37
P OLLARD FACTORING ALGORITHM Input: A composite n > 1, not a prime power Output: A nontrivial factor of n 1. Choose a random x 2 f0; 1; : : : ; n 1g 2. For i WD 1; 2; : : : g WD gcd.f i .x/; f 2i .x// If g D 1, go to the next i If 1 < g < n then output g and halt If g D n then go back to Step 1 and choose another x What could possibly be going on here? The birthday paradox in elementary probability theory says that a collection of 23 or more people is more likely than not to have two people with the same birthday. More generally, if elements are chosen randomly from a set of size n, with replacement, a repeat is likely p after O. n/ choices [Knuth 1981]. Let p be an (unknown) prime divisor of n. Evidence suggests that f k .x/ mod p is indistinguishable from a random sequence. Assuming that this is the case, p the birthday paradox implies that the sequence repeats a value after O. p/ steps, after which time it cycles. Thus the sequence of values mod p has the form y1 ; : : : ; ym ; ymC1 ; : : : ; ymCk D ym ; ymCkC1 D ymC1 ; : : : : A little calculation shows that if i is the smallest multiple of k that exceeds m then yi D y2i . (This elegant idea is usually referred to as Floyd’s cycle-finding algorithm, and it enables the algorithm to use only a very small amount of memory.) In the context of the Pollard algorithm this means that p divides gcd.f i .x/; f 2i .x//. Thus the GCDs in the algorithm will sooner or later be divisible by p. One catastrophe that can happen is that, by some strange coincidence, all primes dividing n happen to divide the GCD at the same time, but this is unlikely in practice. The complexity of this algorithm is O.n1=4 / in the worst case that n is the product of two roughly equal primes. The Pollard algorithm does have the virtue that it finds smaller factors sooner, so that it is reasonable to apply the algorithm to a composite number when there is no knowledge of the size of the factors. Many further details and optimizations of this charming algorithm and variants can be found in [Cohen 1993; Knuth 1981; Teske 2001]. In the last 25 years, a number of algorithms have been proposed for factoring in subexponential time, i.e., in time less than O.n" / for all " > 0. All are probabilistic, and most rely, for this favorable complexity estimate, on highly plausible, but as yet unproved, assumptions. The conjectured run times are sometimes said to be the heuristic complexity of these algorithms.
38
JOE BUHLER AND STAN WAGON
To describe the complexity of these algorithms, let Ln ŒaI c D exp .c C o.1//.log n/a .log log n/1
a
:
where o.1/ denotes a term that goes to 0 as n goes to infinity. This function interpolates between polynomial time, for a D 0, and exponential time, for a D 1, and its peculiar shape arises from formulas for the density of smooth numbers [Granville 2008] (a number is “y-smooth” if all of its prime factors are less than or equal to y). The first of the two new algorithms described elsewhere in this volume is the Elliptic Curve Method (ECM), due to Hendrik Lenstra [1987]; see also [Poonen 2008]. The algorithm relies on the theory of elliptic curves over finite fields and some eminently believable heuristics concerning them; the (heuristic) complexity of this probabilistic algorithm is LŒ1=2I 1. The ECM shares the advantage, with trial division and the Pollard algorithm, that it finds smaller factors faster. A number of other factoring algorithms are known that have complexity LŒ1=2I 1. An algorithm based on class groups of quadratic fields [Lenstra and Pomerance 1992] is “rigorous” in the sense that its LŒ1=2I 1 complexity does not rely on any heuristics. A few years after the ECM appeared, John Pollard described an algorithm for factoring numbers of the form n D ab k Cc for k large. Shortly afterwards, this was generalized to arbitrary positive integers n. This algorithm is a successor to the so-called Quadratic Sieve algorithm (QS), and is called the Number Field Sieve (NFS); its heuristic complexity is Ln Œ1=3I 4=32=3 D Ln Œ1=3I 1:92 : : : . The unproved assertions on which this complexity is based are natural statements concerning the proportion of polynomials values that are smooth; these assertions seem true in practice but their proof seems beyond current techniques. The basic idea of this so-called index-calculus algorithm is to find smooth integers, and smooth elements of algebraic number fields, by sieving, and then solve a system of linear equations over the field with two elements. The speed of the algorithm in practice derives from the fact that the basic sieving operation can be implemented efficiently on modern computers. Details can be found in [Crandall and Pomerance 2005; Lenstra and Lenstra 1993; Stevenhagen 2008b]. P ROBLEM 9. D ISCRETE L OGARITHMS : Given an element x of a finite cyclic group G and a generator g of that group, find a nonnegative integer k such that x D gk . The problem is not precise until the representation of elements of, and the operation in, the finite group is made explicit. The difficulty of the discrete logarithm problem (DLOG) depends on the size of the group and on its specific representation. The additive group G D Z=nZ D f0; 1; 2; : : : ; n 1g is cyclic, and the discrete logarithm problem in G is easy with the usual representation of
BASIC ALGORITHMS IN NUMBER THEORY
39
elements. The discrete logarithm problem for the multiplicative group .Z=p Z/ is hard, and its difficulty is comparable to the difficulty of factoring an integer of the size of p. There is a Pollard algorithm for solving this DLOG problem [Pomerance 2008; Teske 2001], but the best currently known algorithm for this group is a sub-exponential index calculus algorithm closely related to the NFS factoring algorithm, with heuristic complexity Lp Œ1=3I 432=3 ; see [Pomerance 2008; Schirokauer 2008]. R EMARK 7. Interest in the DLOG problem has been stimulated by cryptographic applications, most notably the famous Diffie–Hellman protocol: If A and B want to have a public conversation which ends with them sharing a secret that no onlooker could reasonably discover, then they start by (publicly) agreeing on a suitable cyclic group G of order n together with a generator g of that group. Then A chooses a random integer a < n, and communicates g a to B, in public. Similarly B chooses a random integer b and transmits g b to A over a public channel. They can then each privately compute a joint secret s D .g a /b D .g b /a : The most obvious way for an eavesdropper to defeat this is to intercept g a , solve the implicit DLOG problem to find a, to intercept g b and compute s D .g b /a . More generally, the eavesdropper can succeed if he or she can find g ab knowing g, g a , and g b . No efficient algorithm to do this is known if G is a large cyclic subgroup of prime order in .Z=p Z/ for some prime p, or for a large cyclic subgroup G of prime order of the group of points E.Fp / on a suitable elliptic curve E over a finite field Fp WD Z=p Z. The representation of a cyclic group in the group of points of an elliptic curve seems particularly opaque, and in this case no sub-exponential discrete logarithm algorithms are known at all. For details on these cases, see [Pomerance 2008; Poonen 2008; Schirokauer 2008]. The difficulty of the abstract discrete logarithm problem in a group is dominated by the difficulty of the problem in the largest cyclic subgroup of prime order [Pomerance 2008]. This explains why in the cases of the groups .Z=p Z/ and E.Fp / above, one usually considers cyclic subgroups of prime order. The largest cyclic subgroup of prime order in a cyclic group G of order 2n has order 2, and there is a particularly efficient DLOG algorithm for G. Let g be a generator of G. There is a chain of subgroups 1 D G0 G1 G2 Gn
1
Gn D G;
where Gm has 2m elements. To find the logarithm of a 2 G with respect to g, n 1 note that a2 is of order 1 or 2. In the first case, a lies in Gn 1 . In the second case, ag lies in the subgroup. In either case we then use recursion.
40
JOE BUHLER AND STAN WAGON
P ROBLEM 10. S QUARE ROOTS M ODULO A P RIME : Given an odd prime p and a quadratic residue a, find an x such that x 2 a mod p. We start by showing how to efficiently reduce this problem to Q UADRATIC N ONRESIDUES. This means that modular square roots can be found efficiently once a quadratic nonresidue is known. Let a be a quadratic nonresidue. Write p 1 D 2t q, where q is odd. The element aq lies in the subgroup G of .Z=p Z/ of order 2t , and b D g q is a generator of that group. By the observation above on 2-power cyclic groups, the discrete logarithm in the cyclic 2-group G is easy and we can efficiently find a k such that aq D b k : Note that k is even since a.p 1/=2 1 mod p. Simple calculation shows that x D a.p q/=2 b k=2 is a square root of a: x 2 D a.p
b D a.p
q/ k
q/ q
a D ap a mod p:
The running time of the this procedure depends on the power of 2 dividing p 1. The conclusion is that square roots modulo p can be found efficiently if quadratic nonresidues can be found efficiently; using this idea, taking square roots modulo a prime is easy in practice, and easy in theory if probabilistic algorithms are allowed. More recently, Ren´e Schoof [1985] discovered a deterministic polynomialtime algorithm for finding square roots of a fixed integer a modulo a prime, using elliptic curves. In practice, this algorithm is not competitive with the probabilistic algorithms above since the exponent on log p is large. However, Schoof’s paper has had an enormous impact on the study of elliptic curves over finite fields [Poonen 2008], since it also pioneered new techniques for finding the order of the group E.Fp / for large p. P ROBLEM 11. M ODULAR S QUARE ROOTS : Given an integer n and an integer a, determine whether a is a square modulo n, and find an x such that x 2 a mod n if x exists. If the prime factorization of n is known, then the algorithm above can be combined with Hensel’s Lemma and the Chinese Remainder Theorem (both discussed later) to find a square root of a modulo n. However, the working algorithmic number theorist is often confronted with integers n whose prime factorization is unknown. In this case, no efficient modular square root algorithm is known. In fact, more is true: M ODULAR S QUARE ROOTS is equivalent, in a sense to be made precise shortly, to FACTORING. This says that in addition to the above fact — that factoring n enables the square root problem to be solved easily —
BASIC ALGORITHMS IN NUMBER THEORY
41
it is also the case that an algorithm for M ODULAR S QUARE ROOTS enables n to be factored efficiently. To factor n, first check that it is odd and not a perfect power. Then choose a random y and apply the hypothetical M ODULAR S QUARE ROOTS algorithm to a D y 2 mod n to get an x such that x 2 y 2 mod n: Any common divisor of x and y is a factor of n, so assume that x and y have no common divisor larger than 1. If p is a factor of n then p divides x 2 y 2 D .x y/.x C y/. In addition, it divides exactly one of the factors x y or x C y (if it divides both, it would divide their sum 2x and their difference 2y). If y is random, then any odd prime that divides x 2 y 2 has a 50/50 chance of dividing x C y or x y, and any two primes should presumably (i.e., heuristically) have a 50/50 chance of dividing different factors. In that case, greatest common divisor gcd.x y; n/ will be a proper factor of n. If this fails, try again by choosing another random y. After k choices, the probability that n remains unfactored is 2 k . Thus FACTORING and M ODULAR S QUARE ROOTS are in practice equivalent in difficulty. R EMARK 8. Replacing square roots by eth roots for e > 2 leads to a problem closely related to the RSA cryptosystem, perhaps the most famous of all publickey cryptographic systems. Let n D pq be the product of two large primes, and G D .Z=nZ/ . The RSA cryptosystem uses exponentiation as a mixing transformation on G. A message of arbitrary length is broken a sequence of elements of G, and each element is encrypted separately. If e > 1 is an integer relatively prime to .p 1/.q 1/ then the encryption map EW G ! G defined by E.x mod n/ D x e mod n is a bijection that can be computed efficiently using E XP. The decryption mapping is D.y/ D y d where the integer d is defined by the congruence ed 1 mod .p
1/.q
1/:
The decryption exponent d can be found efficiently, if the factorization of n is known, using the Extended Euclidean Algorithm described in the next section. If n and e are chosen suitably, then finding D.y/ without knowing p and q requires the solution of a modular eth roots problem. It is plausible that breaking this system is no easier than factoring. P ROBLEM 12. B OUNDED M ODULAR S QUARE ROOTS : Given an integer n and integers a and b, determine whether there is an integer x such that x < b and x 2 a mod n.
42
JOE BUHLER AND STAN WAGON
It may be peculiar to ask for square roots below a given bound, but this additional condition turns out to make the problem much more difficult than M ODULAR S QUARE ROOTS, assuming the widely believed P ¤ NP conjecture. Specifically, B OUNDED M ODULAR S QUARE ROOTS is known to be NP -complete. Briefly, a decision problem is in P if there is a polynomial-time algorithm for it. A decision problem is in NP if there is an algorithm f .x; y/ in P that takes an instance x of a problem and a “certificate” y as input, such that for every “yes” instance x there is a certificate y such that f .x; y/ returns “yes.” B OUNDED M ODULAR S QUARE ROOTS is certainly in NP , as are almost all of the algorithms that we have considered. Indeed, given an instance .a; b; n/, the integer x is a certificate: the verifications that x < b and x 2 a mod n can be done in polynomial time. Membership in NP doesn’t imply that finding the certificate is easy, but merely that it exists; if certificates could be found easily the problem would be in P . Finally, a decision problem is NP -complete if it at least as hard as any other problem in NP , in the sense that any other problem in NP can be reduced to it. The notion of reduction needs to be defined precisely, and involves ideas similar to the equivalence of FACTORING and M ODULAR S QUARE ROOTS sketched above. For details see [Garey and Johnson 1979]. A discussion of the complexity of number-theoretic problems would be incomplete without mentioning a problem for which no algorithm whatsoever exists. P ROBLEM 13. D IOPHANTINE E QUATIONS : Given a polynomial f .x1 ; : : : ; xn / with integral coefficients in n variables, is there n-tuple of integers x such that f .x/ D 0? A famous result of Yuri Matijasevic, building on work of Julia Robinson, Martin Davis, and Hilary Putnam shows that this is an undecidable problem [Matiyasevich 1993; Davis 1973]. Although the problem might be easy for a specific f , there is no algorithm (efficient or otherwise) that takes f as input and always determines whether f .x/ D 0 is solvable in integers.
3. Euclid’s algorithm Euclid’s algorithm, given above, has been an extraordinarily fertile source of algorithmic ideas, and can be viewed as a special case of famous modern algorithms, including Hermite normal form algorithms, lattice basis reduction algorithms, and Gr¨obner basis algorithms. The GCD of integers a and b is the unique nonnegative integer d such that d Z D aZ C b Z. Here d Z denotes the principal ideal in the ring of integers consisting of all multiples of d , and aZ C b Z WD fax C by W x; y 2 Zg. To prove
BASIC ALGORITHMS IN NUMBER THEORY
43
that d exists, it suffices to consider nonzero a and b, in which case d can be taken to be the least positive element of aZ C b Z. Indeed, if z D ax C by is an arbitrary element of aZ C b Z then z D qd C r where r D z mod d so that 0 r < d. Since r D z qd is a nonnegative element of aZ C b Z, it follows that r D 0, and z is a multiple of d as claimed. This shows that for nonzero a and b the GCD d D gcd.a; b/ is the smallest positive integral linear combination of a and b. In addition, d is the largest integer that divides both a and b and it is the only positive divisor that is divisible by all other divisors. Moreover, it is described in terms of the prime factorizations of positive a and b by Y Y Y gcd.a; b/ D p min.ap ;bp / ; if a D p ap ; b D p bp : p
p
p
The Greeks would have been more comfortable with the following geometric definition of the GCD: if a > b > 0 consider a rectangle of width a and height b. Remove a b-by-b square from one end of the rectangle. Continue removing maximal subsquares (squares with one side being a side of the rectangle) until the remaining rectangle is a square. The side length of the remaining square is the “common unit of measure” of a and b, i.e., their GCD. For instance, if a D 73 and b D 31, then the removal of two maximal subsquares leaves an 11-by-31 rectangle, the removal of two further maximal squares leaves an 11-by-9 rectangle, the removal of one maximal subsquare leaves a 2-by-9 rectangle, the removal of four maximal subsquares leaves a 2by-1 rectangle, and the removal of one maximal subsquare leaves a unit square. This procedure makes sense for arbitrary real numbers, leading to the notion of a continued fraction; this process terminates for an a-by-b rectangle if and only if a=b is a rational number. 3.1. Extended Euclidean algorithm. In many applications of the Euclidean algorithm the identity aZ C b Z D d Z needs to be made explicit; i.e., in addition to finding d , both x and y must be found. To do this it suffices to augment Euclid’s algorithm. E XTENDED E UCLIDEAN A LGORITHM (EEA) Input: Positive integers a and b Output: x; y; z where z D gcd.a; b/ and z D ax C by fX; Y; Zg WD f1; 0; ag fx; y; zg WD f0; 1; bg While z > 0 q WD bZ=zc fX; Y; Z; x; y; zg WD fx; y; z; X qx; Y qy; Z Return X; Y; Z
qzg
44
JOE BUHLER AND STAN WAGON
Simple algebra shows that at every step aX C bY D Z;
ax C by D z;
(3-1)
so that triples .X; Y; Z/ encode elements of the ideal aZ C b Z. Moreover, Z qz D Z mod z so that the Z-values recapitulate the ordinary Euclidean algorithm. The execution of the algorithm on input a D 73 and b D 31 is summarized in the following table, where each row describes the state of the computation just after q is computed. X
Y
Z
x
1 0 1 2 3 14
0 73 1 31 2 11 5 9 7 2 33 1
0 1 2 3 14 31
y
z q
1 31 2 2 11 2 5 9 1 7 2 4 33 1 2 73 0
Thus gcd.73; 31/ D 1 and . 14/ 73 C 33 31 D 1. The reader should notice that from the third row onwards X and Y have opposite signs, and their values increase in absolute value in successive rows. It isn’t hard to verify that (for positive a and b) this is always the case. What is the running time of this algorithm? It can be shown that the number of arithmetic operations required is linear in the input size. The idea of the argument is as follows. The maximal number of iterations are required when the quotients q is always 1. An induction argument shows that if this is the case and n iterations of the loop are required then a FnC2 , b FnC1 , where Fn denotes the nth Fibonacci number (F1 D F2 D 1, FnC1 D Fn C Fn 1 ). On the other hand, the Euclidean algorithm starting with a D FnC1 , b D Fn takes n p steps. Using Fn D O. n /, D .1 C 5/=2, it follows that n D O.log a/, as desired. A careful accounting of the bit complexity, using the fact that the size of the integers decreases during the course of the algorithm, shows that the bit complexity is O.log2 a/. On occasion it is useful to formulate the Euclidean algorithm in terms of 2-by-2 matrices. Associate the matrix X Y M WD : x y to a row X; Y; Z; x; y; z; q. Then by (3-1) a Z M D : b z
BASIC ALGORITHMS IN NUMBER THEORY
45
The matrix M 0 of the next row is 0 1 M D M: 1 q 0
Iterating this in the specific calculation above gives 1 0 1 0 1 0 1 0 1 0 1 73 D ; 0 1 2 1 4 1 1 1 2 1 2 31
(3-2)
so that the Euclidean algorithm can be interpreted as a sequence of 2-by-2 matrix multiplications by matrices of a special form. All of the ideas in the EEA apply to any commutative ring in which division with “smaller” remainder exists. For instance, the ring F Œx of polynomials over a field F admits a division algorithm for which the remainder polynomial has smaller degree than the divisor. The ring ZŒi WD fa C bi W a; b 2 Zg of Gaussian integers inside the complex numbers has this property if size is measured as the absolute value, and we take the quotient in the ring to be the result of taking the quotient in the complex numbers and rounding to the nearest Gaussian integer. One important application of the EEA is to find inverses in .Z=nZ/ . If gcd.a; n/ D 1 the EEA can be used to find x; y such that ax C ny D 1, which implies that ax 1 mod n and .a mod n/ 1 D x mod n. 3.2. Continued fractions. The partial quotients of the continued fraction expansion of a real number ˛ are the terms of the (finite or infinite) sequence a0 ; a1 ; : : : defined as follows. C ONTINUED F RACTION OF A R EAL N UMBER Input: A real number ˛ Output: A sequence ai of integers ˛0 := ˛ For i WD 0; 1; : : : Output ai WD b˛i c Halt if ˛i D ai ˛iC1 WD 1=.˛i ai / E XAMPLE 1. If ˛ D 73=31 the partial quotients are 2; 2; 1; 4; 2. If ˛ D partial quotients are ultimately periodic: 3; 3; 6; 3; 6; 3; 6; : : : .
p
11 the
The reader should verify that this procedure terminates if and only if ˛ is a rational number. Although written as if it was an algorithm, this is more of a mathematical construct than an algorithm for several reasons: (a) it may fail to terminate, (b) real numbers cannot be represented in finite terms suitable for a computer, and (c) testing real numbers for equality is (algorithmically) problematic.
46
JOE BUHLER AND STAN WAGON
The relationship between ˛i and ˛iC1 can be written as ˛ i D ai C
1 : ˛iC1
(3-3)
Iterating this for ˛ D 73=31 gives the finite continued fraction 73 D 2C 31
1
D Œ2I 2; 1; 4; 2;
1
2C
1
1C
1 2 where Œa0 I a1 ; : : : ; an denotes the value of a finite continued fraction with partial quotients ai . The values of the successive partial continued fractions of a real number are called convergents of the continued fraction; e.g., the convergents of the continued fraction for 73=31 are 4C
9 Œ2I 2 D ; 2
73 Œ2I 2; 1; 4; 2 D : 31 p Periodic continued fractions represent quadratic irrational numbers a C b d , where a; b; d are rational numbers (and d is a nonsquare). For instance, if ˛ denotes the purely periodic continued fraction Œ3I 6; 3; 6; 3; 6; : : : then
Œ2 D 2;
Œ2I 2; 1 D
11 ; 9
Œ2I 2; 1; 4 D
˛ D 3C
1 6C
2 Clearing p fractions gives 2˛ .3 C 11/=2.
6˛
1 ˛
31 ; 11
:
1 D 0, so that ˛ > 0 implies that ˛ D
P ROPOSITION 14. Let a0 ; a1 ; : : : be a sequence of real numbers with ai > 0 for i > 0. Define a real number Œa0 I a1 ; : : : ; an recursively by Œa0 I D a0 ;
Œa0 I a1 ; : : : ; an ; anC1 D Œa0 I a1 ; : : : ; an C1=anC1 :
Finally, define sequences xi ; yi recursively by x
1
D 0;
x0 D 1;
xnC1 D anC1 xn C xn
1;
n 0;
y
1
D 1;
y0 D a0 ;
ynC1 D anC1 yn C yn
1;
n 0:
(3-4)
Then for nonnegative n, yn x n
1
yn
1 xn
D . 1/n
1
;
yn =xn D Œa0 I a1 ; : : : ; an :
Moreover, if the ai are the partial quotients of the continued fraction of a real number ˛ then the integers xn and yn are coprime, and yn ˛nC1 C yn 1 ˛D : (3-5) xn ˛nC1 C xn 1
BASIC ALGORITHMS IN NUMBER THEORY
47
R EMARK . Several notations are commonly used for convergents; the choice here is dictated by a geometric interpretation to be given shortly. P ROOF. The coprimality of xn and yn follows once xn yn 1 xn 1 yn D . 1/n is proved. This and all of the other assertions follow from straightforward induction arguments. For instance, assuming that yn =xn D Œa0 I a1 ; : : : ; an for a given n and arbitrary ai we have anC1 yn C yn 1 anC1 .an yn 1 C yn 2 / C yn 1 ynC1 D D xnC1 anC1 xn C xn 1 anC1 .an xn 1 C xn 2 / C xn 1 .an C 1=anC1 /yn 1 C yn 2 D .an C 1=anC1 /xn 1 C xn 2 D Œa0 I a1 ; : : : ; an C 1=anC1 D Œa0 I a1 ; : : : ; an ; anC1 :
As with the Euclidean algorithm, it is sometimes convenient to formulate continued fractions in terms of 2-by-2 matrices, e.g., defining an 1 yn yn 1 Mn WD ; Pn WD (3-6) 1 0 xn xn 1 and observing that (3-4) implies that Pn D M0 M1 Mn :
(3-7)
A careful look at the convergents of the continued fraction of 73=31 reveals that they recapitulate the Euclidean algorithm! This is easy to verify using
1
a 1 1 0
D
0 1
1 : a
1 1 Indeed, multiplying the relation 73 31 D P4 0 repeatedly on the left by the Mk gives the earlier EEA formula (3-2); moreover, this process can be reversed to show that the EEA yields a continued fraction. Since det.Mk / D 1 and det.Pn / D yn xn 1 xn yn 1 the product formula (3-7) gives another proof of the first statement of the proposition: yn xn
1
yn
1 xn
D . 1/nC1 :
(3-8)
3.3. Rational approximation. Let ˛; ˛n ; an ; yn ; xn be as in the previous section. We wish to quantify the sense in which yn =xn is a good approximation to ˛. Dividing (3-8) by xn xn 1 gives yn xn
yn xn
1 1
D
. 1/nC1 : xn xn 1
48
JOE BUHLER AND STAN WAGON
Iterating this identity and using y0 =x0 D a0 gives the useful formula yn 1 1 1 C C . 1/n : D a0 C x0 x1 x1 x2 xn xnC1 xn
(3-9)
Since the xn D an xn 1 Cxn 2 are strictly increasing it follows that the sequence of convergents yn =xn converges. T HEOREM 15. The sequence yn =xn converges to ˛. For even n the convergents increase and approach ˛ from below; for odd n the convergents decrease and approach ˛ from above. Both yn =xn ˛ and yn xn ˛ alternate in sign, and decrease in absolute value. Moreover, 1 1 < jyn ˛xn j < : xnC2 xnC1 P ROOF. From (3-5), yn ˛xn D yn
xn .yn ˛nC1 C yn 1 / xn 1 yn xn yn 1 . 1/nC1 D D xn ˛nC1 C xn 1 xn ˛nC1 C xn 1 xn ˛nC1 C xn
1
:
From anC1 D b˛nC1 c < ˛nC1 < anC1 C 1 we get xnC1 D xn anC1 C xn
1
< xn ˛nC1 C xn
1
< xn .anC1 C 1/ C xn
1
D xnC1 C xn xnC2 ;
and the desired inequalities in the theorem follow by taking reciprocals. It follows immediately that lim yn =xn D ˛ and the other statements follow from (3-9) and basic facts about alternating series. C OROLLARY 16. Any convergent yn =xn in the continued fraction for ˛ satisfies jyn =xn ˛j < 1=xn2 . It is convenient to formalize the notion of a good approximation to ˛ by rational numbers. If .x; y/ is a point in the X Y -plane, then the distance from .x; y/ to the line Y D ˛X along the vertical line X D x is jy ˛xj. Say that .x; y/, x > 0, is a best approximator to ˛ if x and y are coprime and .x; y/ has the smallest vertical distance to Y D ˛X among all integer points with denominator at most x, i.e., jy
˛xj < jv
u˛j
for all integers u; v with 0 < u < x:
The following theorem says that being a best approximator is equivalent to being a convergent, and it gives an explicit inequality for a rational number y=x that is equivalent to being a convergent. T HEOREM 17. Let ˛ be an irrational real number and x; y a pair of coprime integers with x > 0. Then the following are equivalent: (a) y=x is a convergent to ˛.
BASIC ALGORITHMS IN NUMBER THEORY
49
(b) If x 0 is a multiplicative inverse of y mod x (in the sense that yx 0 1 mod x and 1 x 0 < x), then y 1 1 < : (3-10) ˛< x.2x x 0 / x.x Cx 0 / x (c) y=x is a best approximator to ˛. ˇy ˇ C OROLLARY 18. If y=x is a rational number such that ˇ x y=x is a convergent to ˛.
ˇ ˇ 1 ˛ ˇ < 2 ; then 2x
P ROOF OF THE C OROLLARY. Without loss of generality, x and y are coprime. If x 0 is as in the theorem, then 1 x 0 < x, giving 2x x 0 < 2x and x C x 0 < 2x. Multiplying by x and taking reciprocals gives y 1 1 1 1 < ; ˛< < < 0 2 x.2x x / x.x Cx 0 / x 2x 2x 2 and the corollary follows immediately from the theorem.
The convergents in the continued fraction of ˛ determine lattice points (i.e., points with integer coordinates) in the plane, and they are unusually close to the line Y D ˛X . The convergents alternate back and forth over the line, and each point .x; y/ is closer to the line than all points with smaller X . Since any best approximator yn =xn comes from a convergent, it follows that the open ˚ parallelogram .x; y/ W 0 < x < xnC1 ; jy ˛xj < jyn ˛xn j contains no lattice points other than the origin. If you are standing at the origin of the plane looking in the direction of Y D ˛X , then the lattice points .x; y/ 2 Z2 that are closest to the line occur alternately on both sides of the line, and become very close to the line. It is difficult to illustrate this graphically because the convergents approximate so well that they quickly appear to lie on the line. In Figure 1 an attempt has been made to convey this by greatly distorting the scale on the y-axis; specifically, a point at distance d from the line Y D ˛X is displayed at a distance that is proportional to d 2=5 . In that figure, ˛ is the golden ratio D Œ1I 1; 1; 1; : : : , and the coordinates of the convergents .Fn ; FnC1 / are consecutive Fibonacci numbers. (The figure suggests a fact, brought to our attention by Bill Casselman, which the reader may enjoy proving: The even convergents .xn ; yn / are the vertices on the convex hull of the integer lattice points that lie below the line Y D ˛X , and the odd convergents are the convex hull of the integer lattice points above the line.) Before proving Theorem 17, it is convenient to present a lemma. L EMMA 19. Let x; x 0 ; y; y 0 be integers with yx 0 y 0 x D ˙1, and let ˛ lie in between y=x and y 0 =x 0 . Then there are no lattice points in the interior of the parallelogram bounded by the lines X D x, X D x 0 , the line through .x; y/ with slope ˛, and the line through .x 0 ; y 0 / with slope ˛.
50
JOE BUHLER AND STAN WAGON
123
5
8
13
21
34
Figure 1. Approximations to the golden ratio .
P ROOF OF THE L EMMA . To fix the ideas, assume that 0 < x 0 < x and y 0 ˛x 0 < 0 < y ˛x (any other case is similar). Let .u; v/ be a lattice point with x 0 < u < x. Then .u; v/ is in the stated parallelogram if and only if y0 The condition yx 0
˛x < v
˛u < y
˛x:
(3-11)
y 0 x D ˙1 implies that there are integers r; s such that u D r x 0 C sx;
v D ry 0 C sy:
The first equation implies that r and s are nonzero and have opposite sign; subtracting ˛ times the first equation from the second gives v
˛u D r .y 0
˛x 0 / C s.y
˛x/:
BASIC ALGORITHMS IN NUMBER THEORY
51
Since y ˛x and y 0 ˛x 0 have opposite sign, the terms on the right hand side have the same sign. This implies that jv ˛uj max.jy ˛xj; jy 0 ˛x 0 j/ which contradicts (3-11), showing that .u; v/ does not lie in the parallelogram and proving the Lemma. P ROOF OF T HEOREM 17. (a) ) (b), i.e., convergents satisfy the inequalities (3-10): Assume that y=x D yn =xn is a convergent, and that yn 1 =xn 1 is the preceding convergent. If n is odd then yxn 1 yn 1 x D 1 and x 0 D xn 1 . From Theorem 15, 0
˛x D
1 xn ˛nC1 Cxn
1
<
1 xn Cxn
1
and the inequalities (3-10) 1 x.2x
x0/
<0<
y x
˛<
1 x.x Cx 0 /
follow. The case for even n is similar except that y=x ˛ is negative, yxn 1 yn 1 x D 1 requires us to take x 0 D x y, and the left inequality in (b) is the nontrivial one. (b) ) (c), the inequalities (3-10) imply that y=x is a best approximator: Assume that x > 0, x and y are coprime, and y=x satisfies the inequalities. Let .u; v/ be a supposedly better approximator than .x; y/ so that 0 < u x. There are now two cases. First, suppose that y ˛x > 0. Choose x 0 and y 0 such that yx 0 xy 0 D 1. Subtracting y=x y 0 =x 0 D 1=xx 0 from y=x ˛ < 1=.x.x Cx 0 // gives y0 1 1 1 ˛< D ; (3-12) 0 0 0 x x.x C x / xx .x C x 0 /x showing that y 0 x 0 ˛ is negative. The (proof of the) Lemma now shows that an inequality of the form (3-11) is impossible, which implies that .x; y/ is a best approximator. The case y ˛x < 0 is similar and will be left to the reader. (c) ) (a): We show that if .u; v/ isn’t a convergent then v=u isn’t a best approximator. If .u; v/ isn’t a convergent there is an n such that x 0 D xn 1 < u x D xn . Setting y 0 D yn 1 ; y D yn , the Lemma now applies, and .u; v/ isn’t a best approximator unless u D xn in which case v=u isn’t a best approximator unless v=u D yn =xn .
4. Modular polynomial equations The goal of this section is to consider the problem of solving polynomial congruences f .x/ 0 mod n, i.e., to finding roots of polynomials in .Z=nZ/ŒX .
52
JOE BUHLER AND STAN WAGON
We consider the successively more general cases in which n is a prime, prime power, and arbitrary positive integer. 4.1. Equations modulo primes. The problem of finding integer solutions to polynomial equations modulo a prime p is (essentially) the problem of finding roots to polynomials f .X / 2 Fp ŒX with coefficients in the field with p elements. Fermat’s Little Theorem say that every element of Fp is a root of the polynomial X p X ; comparing degrees and leading coefficients shows that X
p
XD
Y
.X
a/ D
a2Fp
p Y1
.X
a/ 2 Fp ŒX :
aD0
It follows that if f .X / 2 Fp ŒX is an arbitrary polynomial, the gcd of f .X / and X p X is the product of all linear factors X a, where a is a root of f . Similarly the roots of X .p 1/=2 D 1 are the quadratic residues modulo p, so Y X .p 1/=2 1 D .X a/ 2 Fp ŒX ; a2Rp
where Rp denotes the set of quadratic residues modulo p. This suggests the following elegant algorithm for finding all roots of a polynomial in Fp ŒX . C ANTOR –Z ASSENHAUS (f; p) Input: A prime p and a polynomial f 2 Fp ŒX Output: A list of the roots of f 1. f WD gcd.f; X p X / 2. If f has degree 1 Return the root of f 3. Choose b at random in Fp g WD gcd.f .X /; .X C b/.p 1/=2 1/ If 0 < deg.g/ < deg.f / then Return C ANTOR –Z ASSENHAUS(g,p) [ C ANTOR –Z ASSENHAUS(f =g,p) Else, go to Step 3 The correctness follows from the earlier remarks — at Step 3, f .X / is a product of distinct linear factors X a, and then g is the product of all X a such that a C b is a quadratic residue. If b is random then we would expect about half of the linear factors of f to divide g.
BASIC ALGORITHMS IN NUMBER THEORY
53
Moreover, the algorithm takes (probabilistic) polynomial time since the input size for a (general) polynomial over Fp is n log p and all operations take time bounded by a polynomial in n and log p. 4.2. Equations modulo prime powers. Next we consider prime powers. The key idea is due to Kurt Hensel and gives an explicit construction that applies in sufficiently favorable situations to allow us to “lift” a solution to f .X / 0 mod p n to a solution modulo p 2n . The idea is illustrated cleanly by the example of square roots modulo prime powers. Suppose that we have found a square root of a mod p, i.e., and integer x such that x 2 a mod p. Let y D .x 2 Ca/=.2x/ mod p 2 . (Since 2x is invertible mod p it is invertible mod p 2 .) Some algebraic juggling shows that y 2 a mod p 2 . The formula for y comes from Newton’s Lemma from calculus: yDx
f .x/ Dx f 0 .x/
x2 a x2 C a D : 2x 2x
In any event, a root mod p of f .X / D X 2 mod p 2 . The general story is as follows.
a mod p has been lifted to a root
T HEOREM 20 (H ENSEL’ S L EMMA ). Let p be a prime, f .x/ 2 ZŒX a polynomial with integer coefficients, and a 2 Z an integer such that f .a/ 0 mod p k ; Then b a isfies
f 0 .a/ 6 0 mod p :
f .a/=f 0 .a/ mod p 2k is the unique integer modulo p 2k that satf .b/ 0 mod p 2k ;
b a mod p k :
R EMARK 9. Since u D f 0 .a/ is relatively prime to p it is relatively prime to any power of p and has an inverse modulo that power, computable with the EEA. Thus the division in the formula in the theorem makes sense. Inverses modulo high powers can be found using Newton’s method (for rational functions): find the inverse of u modulo prime powers by finding roots of f .X / D u 1=X . Specifically, if au 1 mod p k then bu 1 mod p 2k , where b D a f .a/=f 0 .a/ D a.2 au/. P ROOF. Replacing f .X / by f .X Ca/, it suffices to prove the result when a D 0. Thus we are trying to find a root of f .X / 0 mod p 2k , given f .X / D c0 C c1 X C c2 X 2 C ;
c0 D f .0/; c1 D f 0 .0/:
By hypothesis, c0 0 mod p k and c1 6 0 mod p. It follows that b D c0 =c1 is the unique integer modulo p 2k such that b 0 mod p k and f .b/ 0 mod p 2k , finishing the proof.
54
JOE BUHLER AND STAN WAGON
The reader will find it instructive to find a square root of, say, 11 modulo 38 . The elementary form of Hensel’s Lemma above is equivalent to a more elegant and general statement in the p-adic numbers. Since p-adic numbers (and, more generally, non-archimedean local fields) arise later in this volume, we digress to explain this idea. See [Cassels 1986], [Koblitz 1984], and [Serre 1973] for a much more thorough treatment. Fix a prime p and a constant such that 0 < < 1. An absolute value can be defined on the rational numbers Q by jxjp D n ; where x D p n y, n D vp .x/ is the unique integer (positive or negative) such that y is a rational number whose numerator and denominator are coprime to p. By convention, j0jp D 0. Under this absolute value, powers of p are “small.” The absolute value satisfies the “non-archimedean” inequality jx C yjp max.jxjp ; jyjp /, which is stronger than the triangle inequality. The absolute value jxjp gives the rational numbers Q the structure of a metric space by setting the distance between x and y to be jx yjp . The completion Qp of this metric space is called the p-adic numbers, and it can be shown that the p-adic numbers are a locally compact topological field under natural extensions of the field operations and metric to the completion. The field is easily seen to be independent of the choice of , though in some circumstances in algebraic number theory it is convenient to make the choice D 1=p. A nonzero element a of Qp can be represented concretely as a “Laurent series in p,”, i.e., a D ak p k C akC1 p kC1 C akC1 p kC2 C ; where k is an integer, the digits an are integers chosen in the range 0 an < p, and ak ¤ 0. Moreover, vp .a/ D k and jaj D k . Arithmetic operations are easy to visualize in this Laurent representation, at least approximately, by thinking of p n as being very small if n is very large (analogous in some ways to the usual realization of real numbers as decimal expansions). The subset for which vp .a/ 0 is the unit disk X n Zp D an p D fx 2 Qp W jxjp 1g; n0
which is a subring of Qp and is called the ring of p-adic integers. The p-adic numbers are a bit peculiar when first encountered, but it turns out that they beautifully capture the idea of calculating “modulo arbitrarily large
BASIC ALGORITHMS IN NUMBER THEORY
55
powers of p.” The reader might enjoy proving that 1 D 1 C 2 C 22 C 23 C
in Q2 ;
and the fact that infinite series in Qp converge if and only if their terms go to zero. Suppose that we are interested in roots of f .X / 2 Zp ŒX . (By multiplying by a power of p the problem of finding roots for f .X / 2 Qp ŒX readily reduces to f .X / 2 Zp ŒX .) Finding roots x D a0 Ca1 p Ca2 p 2 C to f .x/ D 0 amounts to solving “f .x/ 0 modulo p 1 ,” and Hensel’s Lemma can be translated to the statement that for x 2 Z=p Z with jf .x/jp < " and jf 0 .x/jp D 1, there is a unique y 2 Z=p Z such that f .y/ D 0;
jy
xj < ":
A more general form of Hensel’s Lemma, which can be proved along the lines above, says that if jf .x/j < jf 0 .x/j2 then there is a unique y 2 Zp such that f .y/ D 0, jy xj jf .x/=f 0 .x/2 j. The problem of finding roots is a special case of finding factors of a polynomial, and an even more general form of Hensel’s Lemma guarantees that approximate factors can, in suitable circumstances, be lifted to p-adic factors. If f is a polynomial with p-adic integer coefficients then let f mod p denote the polynomial obtained by looking at the coefficients modulo p. T HEOREM 21. Assume that f 2 Zp ŒX factors modulo p as f mod p D gh 2 .Z=p Z/Œx and g and h are coprime in .Z=p Z/Œx. Then there are unique G and H in Zp ŒX such that f D GH and G and H lift g and h in the sense that G mod p D g and H mod p D h. We sketch a proof of the theorem. Since p-adic integers are approximated arbitrarily closely by (ordinary) integers, it suffices to prove a statement about integer polynomials: if f 2 ZŒx satisfies f gh mod p where g and h are coprime modulo p (i.e., the elements that they determine in Fp ŒX are coprime), then for arbitrarily large n there are integer polynomials G; H that are congruent to g; h, respectively, modulo p and satisfy f GH mod p n . To do this it turns out to be essential to simultaneously lift a certificate of the fact that g and h are relatively prime. Assume that we are given polynomials f; g; h; r; s with integer coefficients such that f gh mod p k ;
rg C sh 1 mod p k ;
where deg.r / < deg.h/, deg.s/ < deg.g/; our goal is to find G; H; R; S satisfying a similar congruence modulo p 2k . Note that the hypotheses of the theorem imply that r and s exist for k D 1, using the Euclidean Algorithm for polynomials over Fp . The pair r; s is said to be a coprimality certificate of g and h mod p k .
56
JOE BUHLER AND STAN WAGON
Before proving the theorem it is useful to show how to use a coprimality certificate mod p k to represent any polynomial as a linear combination of g and h modulo p k . L EMMA 22. If rg Csh 1 mod p k , then for all u 2 ZŒX there are A; B 2 ZŒX such that Ag C Bh u mod p k : If deg.u/ < deg.g/ C deg.h/ then we can take deg.A/ < deg.h/, deg.B/ < deg.g/. P ROOF. Multiplying the assumed equation by u gives u rguCshu D .r u/gC .su/h mod p k so the first statement is immediate. To verify the assertion about degrees, we have to work harder. Let A be the remainder when r u is divided by h, i.e., r u D q1 h C A, deg.A/ < deg.h/. Similarly, let B be the remainder when su is divided by g, i.e., su D q2 g C B, deg.B/ < deg.g/. Then Ag C Bh D .r u
q1 h/g C .su
D .rg C sh/u
q2 g/h
.q1 C q2 /gh u C Qgh mod p k ;
where Q D q1 q2 . Since gh is monic of degree deg.g/ C deg.h/ and all other terms in the congruence have degree strictly less than deg.g/ C deg.h/ modulo p k , it follows that Q 0 mod p k , finishing the proof. Now we prove the theorem by showing how to lift the factorization, and lift the certificate, i.e., find the desired G; H; R; S given g; h; r; s. To lift the factorization write f D ghCp k u, and use Lemma 22 to find A, B with u Ag C Bh mod p k ; it is straightforward to check that G D g C p k B, and H D h C p k A satisfy f GH mod p 2k . To lift the certificate rg C sh D 1 mod p k , write rg C sh D 1 C p k u, use Lemma 22 to find u AgCBh mod p k , and check that RGCSH 1 mod p 2k where R D r C p k A and S D s C p k B. 4.3. Chinese Remainder Theorem. Finally, we come to the case of solving polynomial equations modulo arbitrary positive integers n, given that we can solve the equations modulo the prime power divisors of n. The Chinese Remainder Theorem gives an immediate way for doing this, by giving an explicit recipe for using solutions f .x1 / 0 mod n1 , f .x2 / 0 mod n2 to produce an x in Z=.n1 n2 /Z such that f .x/ 0 mod n1 n2 when the ni are coprime. T HEOREM 23 (C HINESE R EMAINDER T HEOREM ). If m and n are coprime, and a and b are arbitrary integers, then there is an integer c such that c a mod m;
c b mod n:
Any two such integers c are congruent modulo mn.
BASIC ALGORITHMS IN NUMBER THEORY
57
P ROOF. Apply the EEA to m and n to find x and y such that mx C ny D 1. The integer c D any C bmx is obviously congruent to a mod m and b mod n. If c 0 also satisfies these congruences then d D c c 0 is divisible by the coprime integers m and n, and hence by mn. Thus c and c 0 are congruent modulo mn as claimed. This can be stated more algebraically as follows. C OROLLARY 24. If gcd.m; n/ D 1 then the rings Z=.mn/Z and Z=mZ Z=nZ are isomorphic via the map x mod mn ‘ .x mod m; x mod n/: Also, .mn/ D .m/.n/ where is the Euler -function. Indeed, the first statement in the Chinese Remainder Theorem above says that the map is surjective, and the second says that it is injective. Units in the direct product of two rings are pairs .u; v/ where u and v are units in the respective rings, so the multiplicativity of ' follows from the first statement. As promised, the theorem shows how to combine modular solutions to polynomial equations: if f .a/ 0 mod m and f .b/ 0 mod n then apply the Chinese Remainder Theorem to find a c such that c a mod m and c b mod n; these congruences imply that f .c/ 0 mod mn. By induction, this technique generalizes to more than 2 moduli: if n1 ; n2 ; : : : ; nk are pairwise coprime, and ai are given then there is an integer x, unique modulo the product of the ni , such that x ai mod ni for 1 i k. In Q fact, there are two natural inductive proofs. The product of k moduli n D ni could be reduced to the k D 2 case by n D n1 m, where m is the product of the ni for i > 1, or n D m1 m2 where each mi is the product of approximately half of the ni . The latter method is significantly more efficient in practice: with reasonable assumptions, the first method takes O.k 2 / arithmetic operations, and the second takes O.k log k/ arithmetic operations.
5. Quadratic extensions A quadratic extension K of a field F is a field containing F such that the dimension of K as an F -vector space is 2. This means that K is isomorphic to F ŒX =.f .X // where f .X / 2 F ŒX is an irreducible quadratic polynomial. More concretely, K D fa C b˛ W a; b 2 F g where ˛ 2 K satisfies an irreducible quadratic equation with coefficients in F . Finally, if F D Q then any quadratic p extension isp(isomorphic to) a subset of the complex numbers of the form Q. d/ D fa C b dg, where d is a nonsquare in Q.
58
JOE BUHLER AND STAN WAGON
In this section we consider four algorithms that are either illuminated by, or directly apply to, quadratic field extensions. 5.1. Cipolla. Let p be an odd prime. A natural and direct algorithm, originally due to Cipolla [1902], finds square roots modulo p by taking a single exponentiation in a quadratic extension of Fp D Z=p Z. Unlike the modular square root algorithm presented earlier, it has a running time that is independent of the power of 2 that divides p 1. A polynomial f .X / D X 2 aX C b 2 Fp ŒX is irreducible if and only if its discriminant D WD a2 4b is a quadratic nonresidue. Let ˛ D ŒX be the coset of X in K WD Fp ŒX =.f .X // so that K can be viewed as the set of linear polynomials fa C b˛ W a; b 2 Fp g added in the usual way, and multiplied using the identity ˛ 2 D a˛ b. In any field that contains Fp the map '.x/ D x p is a field automorphism of K, sometimes called the Frobenius map. Indeed, it is obvious that ' is multiplicative, and '.x C y/ D .x C y/p D x p C y p follows from the fact that the interior binomial coefficients are divisible by p. Moreover '.x/ D x if and only if x 2 Fp , since every element of Fp is a root of the polynomial x p x, and a polynomial of degree p can have at most p roots in a field. C IPOLLA’ S MODULAR SQUARE ROOT ALGORITHM Input: An odd prime p and a quadratic residue b 2 Fp Output: u 2 Fp such that u2 D b 1. Select random a until a2 4b is a nonresidue modulo p 2. Return ˛ .pC1/=2 , where ˛ WD a root of x 2 ax C b Once an a has been found in Step 1, then f .X / D X 2 aX C b is irreducible, and the above notation applies. Apply the Frobenius automorphism ' to the equation ˛ 2 a˛ C b D 0 to find that ˇ WD ˛ p is also a root of f , so that f .X / D .X ˛/.X ˇ/. Comparing coefficients gives b D ˛ ˇ D ˛ ˛ p D ˛ .pC1/ so that ˛ .pC1/=2 is a square root of b, proving correctness of the algorithm. Although the exponentiation is done in K, the final result lies in Fp . E XAMPLE 2. Let p D 37, a D 34. Easy Legendre symbol calculations show that 34 is a quadratic residue modulo 37, and b D 1 produces a nonresidue in Step 1, so that f .x/ D x 2 x C 34 is irreducible. We compute ˛ .pC1/=2 D ˛ 19 using E XP; at each step of the calculation we replace ˛ 2 by ˛ 34 D ˛ C3 and reduce
BASIC ALGORITHMS IN NUMBER THEORY
59
the coefficients modulo 37. The following table summarizes the calculation: k
˛k
2 ˛C3 4 ˛ 2 C 6˛ C 9 D 7˛ C 12 8 49˛ 2 C 168˛ C 144 D 32˛ C 32 D 5.˛ C 1/ 9 5.˛ 2 C ˛/ D 5.2˛ C 3/ 18 25.4˛ 2 C 12˛ C 9/ D 30˛ C 7 19 30˛ 2 C 7˛ D 16 As predicted, the final result lies in the field with 37 elements, and 162 34 mod 97. 5.2. Lucas–Lehmer. Theorem 5 gives a method for verifying primality of n when the factorization of n 1 is known. A similar primality test, attributed to Lucas and Lehmer, enables the primality of n to be established efficiently if the prime factorization of n C 1 is known. Let f .X / D X 2 aX C b 2 ZŒx be an irreducible polynomial with integer coefficients, D WD a2 4b its discriminant, and R WD ZŒX =.f .X //. Let ˛ WD ŒX and ˇ WD a ˛ be the two roots of f in R. For k 0 define Vk D ˛ k C ˇ k 2 R. Both sequences f˛ k g and fˇ k g satisfy the recursion skC1 D ask bsk 1 ; so their sum Vk also does, by linearity. Since V0 D 2 and V1 D a are integers it follows that all of the Vk are integers. More elaborate “doubling” recursions are easy to discover, allowing the Vk to be computed in O.log k/ arithmetic operations; these are equivalent to using E XP to calculate high powers of a matrix, noticing that k VkC2 VkC1 a b V2 V1 D : 1 0 VkC1 Vk V1 V0 T HEOREM 25. Let n be an odd positive integer, and a; b integers such that D D a2 4b satisfies D D 1: n Define Vk as above, and let m D .nC1/=2. If Vm 0 mod n and gcd.Vm=q ; n/ D 1 for all prime divisors q of m, then n is prime. P ROOF. Let p be a prime divisor of n with D D 1: p
60
JOE BUHLER AND STAN WAGON
Working modulo p, i.e., in K WD R=pR D ZŒx=.p; f .x// D Fp Œx=.f .x//; we see that f .x/ 2 Fp Œx is irreducible so that the ideas of the preceding section apply. Since the roots ˛ and ˇ of an irreducible quadratic polynomial are nonzero, we can define u D ˇ=˛ D ˛ p =˛ D ˛ p
1
2 K:
The equation Vm 0 mod p implies that ˛ m D ˇ m , i.e., um D 1 and u2m D 1. Similarly, the equations Vm=q 6 0 mod p imply that u2m=q ¤ 1 for all prime divisors q of m. Therefore u has order 2m D n C 1 in K. Since pC1 2 upC1 D ˛ p 1 D ˛ p 1 D 1; it follows that p C 1 is divisible by n C 1, which implies that n D p, i.e., that n is a prime as claimed. R EMARK 10. As in the earlier primality test relying on the factorization of n 1, it can be shown that it suffices to restrict attention to primes q that divide p a factor F > n of n C 1. 5.3. Units in quadratic fields. If d is a rational numberpthat is not a perfect p square, then the quadratic extension F D Q. d/ D faCb d W a; b 2 Qg over Q is unchanged if we replace d by de 2 . We will from now on assume that d is a squarefree integer, i.e., not divisible by any perfect square n2 > 1. Ifpd < 0 then p Q. d/ is said to be an imaginary quadratic field, and if d > 0 Q. d / is said to be a real quadratic field. A number field is a field having finite degree over Q. These fields are the core subject matter of algebraic number theory, and are of critical importance in several subsequent particles in this volume, e.g., [Stevenhagen 2008a; 2008b]. Quadratic fields Q. d/ already illustrate many of the ideas that come up in studying number p fields in general, and are a vast and fertile area of study. Let F D Q. d/. The ring of integers OF in F is the set of v 2 F that are roots of a monic polynomial with integer coefficients; this ring plays the role, in F , that the integers do in Q. P ROPOSITION 26. With the above notation, OF D ZŒ! where p d p if d 2 or 3 mod 4; !D .1 C d/=2 if d 1 mod 4:
BASIC ALGORITHMS IN NUMBER THEORY
61
p Thus OF D ZŒ d if d 2 or 3 mod 4, and p o n x Cy d W x; y 2 Z; x y mod 2 OF D ZŒ! D 2 if d 1 mod 4. p The key idea of the proof is that if v D x C y d 2 F is an algebraic integer then it satisfies the equation X 2 2xX C .x 2 dy 2 / D 0, which implies that x and therefore y are either integers or half-integers; we leave the details to the reader. The algorithmically inclined reader will observe that the ring of integers is an unfriendly object. For instance, we have been assuming in this discussion that the integer d is squarefree. In fact, it is difficult to detect or verify whether a large integer p is squarefree, and therefore impossible to find the ring of integers in F D Q. d / for general d. See [Stevenhagen 2008a] for a discussion of how algorithmic number theorists deal with this issue for arbitrary number fields. We now turn our attention to finding units in rings of integers of quadratic fields. The only units (elements with multiplicative inverses) in Z are ˙1. In order to find units in quadratic fields we introduce some standard terminology. p p If v D x C y d 2 F then v 0 D x y d is the conjugate of v. p The norm of v D x C y d 2 F is p p N .v/ D vv 0 D .x C y d /.x y d / D x 2 dy 2 2 Q: The map u ‘ u0 is easily checked to be an automorphism, and .uv/0 D u0 v 0 implies that the norm map is multiplicative: N .uv/ D N .u/N .v/. Note that if d 2 or 3 mod 4 then N .x Cpy!/ D .x C y!/.x y!/ D 2 x dy 2 , while if d 1 mod 4 and ! D .1 C d/=2 then N .x C y!/ D .x C y!/.x C y! 0 / D x 2
xy
d 1 2 y : 4
L EMMA 27. An element u in OF is a unit if and only if N .u/ D ˙1. P ROOF. If N .u/ D ˙1 then u.˙u0 / D 1 and u is a unit. If v is a unit then there is a u 2 OF such that uv D 1. Taking the norm and using the multiplicativity of the norm gives N .u/N .v/ D 1, so that N .u/ is a unit in Z, i.e., N .u/ D ˙1. In imaginary quadratic fields there are only finitely many units, and they are easy to determine by considering the explicit form of the norm mapping given above; the details of are again left to the reader, and the result is:
62
JOE BUHLER AND STAN WAGON
T HEOREM 28. Units in an imaginary quadratic field F 8 if d D
p D Q. d /, d < 0, are: 1; 3; 3:
Finding units in (the ring of integers of) real quadratic p fields is considerably more interesting. If d 2 or 3 mod 4 then OF D ZŒ d and finding units is equivalent to solving Pell’s equation [Lenstra 2008] x2
dy 2 D ˙1:
If d 1 mod 4 then units u D x C y! correspond to solutions to x 2 xy 1/=4 D ˙1. After multiplying by 4 and completing the square this is equivalent to integer solutions to X 2 dY 2 D ˙4. In either case, the problem of finding units reduces to solving Pell’s equation or a variant. This can be done by finding the continued fraction of !. p T HEOREM 29. If F D Q. d/, d > 0, and u D x y! is a unit in OF with x and y positive, then x=y is a convergent in the continued fraction expansion of !. y 2 .d
If u D x Cy! is a unit then u, u0 , and u0 are all units. If u is a unit, then one of ˙u; ˙u0 has the form x y! with x; y positive, so the restriction to positive x, y is unimportant: any unit can be obtained from a convergent by changing the sign and/or taking a conjugate. P ROOF. Assume that u D x y! is a unit with x and y positive. If d 2 or 3 mod 4, then x 2 dy 2 D ˙1. Moreover d 2 and r p p x p 1 C d D d ˙ 2 C d 1 C 2 > 2: (5-1) y y p p From this equation and .x=y d /.x=y C d / D ˙1=y 2 we get ˇ ˇ ˇx p ˇ 1 dˇ < 2 ; ˇ y 2y so that, by Corollary 18, x=y is a convergent in the continued fraction of !. The case d 1 mod 4 is similar, but slightly more involved. If u D x y! is a unit then N .u/ D x 2 xy .d 1/y 2 =4 D ˙1. Solving the quadratic equation .x=y/2 .x=y/ .d 1/=4 ˙ 1=y 2 D 0 for x=y and doing some algebra shows that x ! 0 2; (5-2) y
BASIC ALGORITHMS IN NUMBER THEORY
63
except possibly in the case d D 5; y D 1. Indeed, this inequality is equivalent to p
d ˙ 4=y 2 C
p
d 4;
which is easy to verify if d > 5 (so that d 13), or if y 2. If either of these hold, proceed as above: .x=y !/.x=y ! 0 / D ˙1=y 2 and (5-2) imply that jx=y !j < 1=2y 2 and the theorem follows as before. If d D 5 and y D 1 then the theorem can be checked directly: both 2=1 and 1=1 are convergents. There is much more to say about continued fractions of arbitrary quadratic irrationals. We enumerate some of the most important facts. (i) The continued fraction of a real number ˛ is periodic if and only if ˛ is a quadratic irrational. (ii) The continued fraction of a quadratic irrational ˛ is purely periodic if and only if ˛ > 1 and 1 < ˛ 0 < 0. (iii) The continued fraction of a quadratic irrational p ˛ can be computed entirely with integer arithmetic; ˛k has the form .Pk C d/=Qk for integers Pk , Qk , where Qk divides d Pk2 , and these integers are determined by the recursions ak D b˛k c;
PkC1 D ak Qk
Pk ;
QkC1 D
d
2 PkC1
Qk
:
p d log d/. The continued (iv) The periodpof the continued fraction of ! is O. p fractions of d and .1 C d/=2 have the respective shapes ŒaI P; 2a;
Œa; P; 2a
1;
where a is a positive integer, the sequence P D a1 ; a2 ; : : : ; a2 ; a1 is a palindrome, and the ai are less than a. (v) If the period of the continued fraction is r , then the .r 1/st convergent corresponds to a so-called fundamental unit u, and any other unit of the form ˙un for integers n; i.e., OF D f˙un W n 2 Zg ' Z Z=2Z: p (vi) The index of ZŒ d in OF is 1 or 3.
p (vii) The fundamental generating unit of Q. d/ has norm 1 if and only if P has even length. If this is the case, the units of norm one are of the form ˙u2k .
64
JOE BUHLER AND STAN WAGON
5.4. The Smith–Cornacchia algorithm. Throughout this section integers d and n satisfy 0 < d < n, and we consider integer solutions x; y to x 2 C dy 2 D n:
(5-3)
Note that the problem of solving this equation is equivalent to p p d /.x y d / D n; .x C y p p i.e., finding elements of ZŒ d of norm n. Fix d and let R D ZŒ d . A coprime solution x; y is said to be primitive. Any coprime solution determines a square root t D x=y mod n of d modulo n; indeed, t 2 C d D .x 2 C dy 2 /=y 2 0 mod nI the unique such t with 1 t < n is sometimes called the signature of the psolution. A signature determines a map from R to Z=nZ defined by taking d to t . The kernel of this ring homomorphism is the principal ideal p p I t D .x C y d/R D nR C .t C d/R: The Smith–Cornacchia algorithm has two steps: determine all potential signatures t , and use p the Euclidean algorithm to determine which of the ideals I t WD nR C .t C d/R are principal. Given a primitive solution .x; y/ to x 2 C dy 2 D n with signature t define an integer z by ty x zD ; n so that x D ty nz. Dividing the inequality j2xyj x 2 C y 2 x 2 C dy 2 D n by 2ny 2 gives ˇ ˇ ˇ ˇx ˇ ˇt ˇ ˇDˇ ny n
ˇ zˇ 1 ˇ 2: y 2y
Corollary 18 implies that z=y is a convergent of the (finite) continued fraction of t =n. Thus to solve (5-3) it suffices to compute the convergents in the continued fraction of t =n and see whether any of the denominators give valid solutions y in the equation x 2 C dy 2 D n. In fact, it is slightly simpler to merely keep track of the remainders in the Euclidean algorithm applied to t and n; indeed jxj is a remainder, and the p equation x 2 C dy 2 D n can be solved for y. Since x n, the remainders p have to be calculated at least to the point that they drop below n.
BASIC ALGORITHMS IN NUMBER THEORY
65
E XAMPLE 3. If d D 5 and n D 12829 then we first have to find a square root t of 5 modulo 12829. Since n is a prime, the square root is unique up to sign; in fact, Cipolla’s algorithm gives t D ˙3705 without difficulty. The sequence of remainders inp the Euclidean algorithm is 12829; 3705; 1714; 277; 52; 17; 1. The first x below 12829 works: p x D 52; y D .n x 2 /=5 D 45; 522 C 5 452 D 12829: T HEOREM 30. If a primitive solution to x 2 C dy 2 D n has signature t then p jxj is the largest (i.e., first encountered) remainder that is less than n in the Euclidean algorithm applied to n and t . Moreover, .x; y/ is (essentially) the unique solution with signature t : if .u; v/ is another such solution then .x; y/ D ˙.u; v/ if d > 1, and if d D 1 reversal might occur, i.e., .x; y/ D ˙.u; v/ or .x; y/ D ˙.v; u/. P ROOF. (See also [Nitaj 1995; Schoof 1995].) Let x be the first remainder less p than n. Assume that the equation has some solution .u; v/ with signature t : u2 C dv 2 D n, u ty mod n. Assume that u ¤ ˙x. Then juj is a subsequent term in the remainder sequence for t and n. By the Euclidean algorithm, there are z, z 0 such that x D yt zn and u D vt z 0 n. The remainders decrease in absolute value, so x > u, and the coefficients of t increase in absolute value, so jyj < jvj. We know that x 2 C dy 2 .t 2 C d/y 2 0 mod n. Moreover, x 2 C dy 2 < n C dv 2 D n C d
n dv 2 < 2n: d
2 Since we also know that x 2 Cdy 2 is divisible by n it follows that x 2 Cdy p D n. Twopdifferent solutions x; y and u; v determine generators x C y p d and u C v p d of I t and hence there is a unit a in R such that x C y d D a.uCv d /. If d > 1 the only units are ˙1. If d D 1 then a D ˙i is possible, and these account for all cases in the theorem.
The algorithm that implements this is obvious. S MITH –C ORNACCHIA A LGORITHM Input: Relatively prime positive integers d and n Output: All primitive solutions to x 2 C dy 2 D n (possibly none) 1. Find all positive solutions (less than n) to t 2 C d 0 mod n p 2. For each solution t , find the first remainder x less p than n in the Euclidean algorithm applied to n and t ; if y WD .n x 2 /=d is an integer, output .x; y/ The second step of the algorithm is efficient. Unfortunately, the first step isn’t in general, since it requires a modular square root. However, in the special case that n is a prime, this step can be done efficiently by a probabilistic algorithm.
66
JOE BUHLER AND STAN WAGON
E XAMPLE 4. The special case d D 1 is interesting as it is the classic problem of writing an integer as the sum of two squares. By Euler’s criterion, 1 is a quadratic residue modulo an odd prime p if and only if p is congruent to 1 modulo 4, and this algorithm gives a constructive proof of Euler’s result that these are exactly the primes that are the sum of two squares. Moreover, it shows that if z is a Gaussian integer then the factorization of z in ZŒi reduces to the problem of factoring N .z/ in the integers. See [Bressoud and Wagon 2000] for further details.
References [Agrawal et al. 2004] M. Agrawal, N. Kayal, and N. Saxena, “PRIMES is in P”, Ann. of Math. .2/ 160:2 (2004), 781–793. [Bach 1990] E. Bach, “Explicit bounds for primality testing and related problems”, Math. Comp. 55:191 (1990), 355–380. [Bach and Shallit 1996] E. Bach and J. Shallit, Algorithmic number theory, I: Efficient algorithms, MIT Press, Cambridge, MA, 1996. [Bernstein 2008] D. Bernstein, “Fast multiplication and its applications”, pp. 325–384 in Surveys in algorithmic number theory, edited by J. P. Buhler and P. Stevenhagen, Math. Sci. Res. Inst. Publ. 44, Cambridge University Press, New York, 2008. [Bressoud and Wagon 2000] D. Bressoud and S. Wagon, A course in computational number theory, Key College, Emeryville, CA, 2000. [Cassels 1986] J. W. S. Cassels, Local fields, London Mathematical Society Student Texts 3, Cambridge University Press, Cambridge, 1986. [Cipolla 1902] M. Cipolla, “La determinazione assintotica dell’nimo numero primo”, Rend. Accad. Sci. Fis. Mat. Napoli 8 (1902), 132–166. [Cohen 1993] H. Cohen, A course in computational algebraic number theory, Graduate Texts in Mathematics 138, Springer, Berlin, 1993. [Cox 1989] D. A. Cox, Primes of the form x 2 C ny 2 : Fermat, class field theory and complex multiplication, Wiley, New York, 1989. [Crandall and Pomerance 2005] R. Crandall and C. Pomerance, Prime numbers: A computational perspective, 2nd ed., Springer, New York, 2005. [Damg˚ard et al. 1993] I. Damg˚ard, P. Landrock, and C. Pomerance, “Average case error estimates for the strong probable prime test”, Math. Comp. 61:203 (1993), 177–194. [Davis 1973] M. Davis, “Hilbert’s tenth problem is unsolvable”, Amer. Math. Monthly 80 (1973), 233–269. [Flajolet and Vall´ee 1998] P. Flajolet and B. Vall´ee, “Continued fraction algorithms, functional operators, and structure constants”, Theoret. Comput. Sci. 194:1-2 (1998), 1–34. [Garey and Johnson 1979] M. R. Garey and D. S. Johnson, Computers and intractability: A guide to the theory of NP-completeness, W. H. Freeman, San Francisco, 1979.
BASIC ALGORITHMS IN NUMBER THEORY
67
[von zur Gathen and Gerhard 2003] J. von zur Gathen and J. Gerhard, Modern computer algebra, 2nd ed., Cambridge University Press, Cambridge, 2003. [Granville 2008] A. Granville, “Smooth numbers: computational theory and beyond”, pp. 267–323 in Surveys in algorithmic number theory, edited by J. P. Buhler and P. Stevenhagen, Math. Sci. Res. Inst. Publ. 44, Cambridge University Press, New York, 2008. [Hopcroft and Ullman 1979] J. E. Hopcroft and J. D. Ullman, Introduction to automata theory, languages, and computation, Addison-Wesley, Reading, MA, 1979. [Knuth 1981] D. E. Knuth, The art of computer programming, II: Seminumerical algorithms, 2nd ed., Addison-Wesley, Reading, MA, 1981. [Koblitz 1984] N. Koblitz, p-adic numbers, p-adic analysis, and zeta-functions, 2nd ed., Graduate Texts in Mathematics 58, Springer, New York, 1984. [Kozen 2006] D. C. Kozen, Theory of computation, Springer, London, 2006. [Lenstra 1987] H. W. Lenstra, Jr., “Factoring integers with elliptic curves”, Ann. of Math. .2/ 126:3 (1987), 649–673. [Lenstra 2008] H. W. Lenstra, Jr., “Solving the Pell equation”, pp. 1–23 in Surveys in algorithmic number theory, edited by J. P. Buhler and P. Stevenhagen, Math. Sci. Res. Inst. Publ. 44, Cambridge University Press, New York, 2008. [Lenstra and Lenstra 1993] A. K. Lenstra and H. W. Lenstra, Jr. (editors), The development of the number field sieve, Lecture Notes in Mathematics 1554, Springer, Berlin, 1993. [Lenstra and Pomerance 1992] H. W. Lenstra, Jr. and C. Pomerance, “A rigorous time bound for factoring integers”, J. Amer. Math. Soc. 5:3 (1992), 483–516. [Matiyasevich 1993] Y. V. Matiyasevich, Hilbert’s tenth problem, MIT Press, Cambridge, MA, 1993. [Nitaj 1995] A. Nitaj, “L’algorithme de Cornacchia”, Exposition. Math. 13:4 (1995), 358–365. [Pollard 1978] J. M. Pollard, “Monte Carlo methods for index computation .mod p/”, Math. Comp. 32:143 (1978), 918–924. [Pomerance 2008] C. Pomerance, “Elementary thoughts on discrete logarithms”, pp. 385–396 in Surveys in algorithmic number theory, edited by J. P. Buhler and P. Stevenhagen, Math. Sci. Res. Inst. Publ. 44, Cambridge University Press, New York, 2008. [Poonen 2008] B. Poonen, “Elliptic curves”, pp. 183–207 in Surveys in algorithmic number theory, edited by J. P. Buhler and P. Stevenhagen, Math. Sci. Res. Inst. Publ. 44, Cambridge University Press, New York, 2008. [van der Poorten 1986] A. J. van der Poorten, “An introduction to continued fractions”, pp. 99–138 in Diophantine analysis (Kensington, 1985), edited by J. H. Loxton and A. J. van der Poorten, London Math. Soc. Lecture Note Ser. 109, Cambridge Univ. Press, Cambridge, 1986. [Schirokauer 2008] O. Schirokauer, “The impact of the number field sieve on the discrete logarithm problem in finite fields”, pp. 397–420 in Surveys in algorithmic
68
JOE BUHLER AND STAN WAGON
number theory, edited by J. P. Buhler and P. Stevenhagen, Math. Sci. Res. Inst. Publ. 44, Cambridge University Press, New York, 2008. [Sch¨onhage 1971] A. Sch¨onhage, “Schnelle Berechnung von Kettenbruchentwicklungen”, Acta Informatica 1 (1971), 139–144. [Schoof 1985] R. Schoof, “Elliptic curves over finite fields and the computation of square roots mod p”, Math. Comp. 44:170 (1985), 483–494. [Schoof 1995] R. Schoof, “Counting points on elliptic curves over finite fields”, J. Th´eor. Nombres Bordeaux 7:1 (1995), 219–254. [Schoof 2008a] R. Schoof, “Computing Arakelov class groups”, pp. 447–495 in Surveys in algorithmic number theory, edited by J. P. Buhler and P. Stevenhagen, Math. Sci. Res. Inst. Publ. 44, Cambridge University Press, New York, 2008. [Schoof 2008b] R. Schoof, “Four primality testing algorithms”, pp. 101–125 in Surveys in algorithmic number theory, edited by J. P. Buhler and P. Stevenhagen, Math. Sci. Res. Inst. Publ. 44, Cambridge University Press, New York, 2008. [Serre 1973] J.-P. Serre, A course in arithmetic, Graduate Texts in Mathematics 7, Springer, New York, 1973. [Shor 1997] P. W. Shor, “Polynomial-time algorithms for prime factorization and discrete logarithms on a quantum computer”, SIAM J. Comput. 26:5 (1997), 1484– 1509. [Shoup 2005] V. Shoup, A computational introduction to number theory and algebra, Cambridge University Press, Cambridge, 2005. [Stevenhagen 2008a] P. Stevenhagen, “The arithmetic of number rings”, pp. 209–266 in Surveys in algorithmic number theory, edited by J. P. Buhler and P. Stevenhagen, Math. Sci. Res. Inst. Publ. 44, Cambridge University Press, New York, 2008. [Stevenhagen 2008b] P. Stevenhagen, “The number field sieve”, pp. 83–100 in Surveys in algorithmic number theory, edited by J. P. Buhler and P. Stevenhagen, Math. Sci. Res. Inst. Publ. 44, Cambridge University Press, New York, 2008. [Teske 2001] E. Teske, “Square-root algorithms for the discrete logarithm problem”, pp. 283–301 in Public-key cryptography and computational number theory (Warsaw, 2000), edited by K. Alster et al., de Gruyter, Berlin, 2001. [Yui and Top 2008] N. Yui and J. Top, “Congruent number problems in dimension one and two”, pp. 613–639 in Surveys in algorithmic number theory, edited by J. P. Buhler and P. Stevenhagen, Math. Sci. Res. Inst. Publ. 44, Cambridge University Press, New York, 2008. J OE B UHLER C ENTER FOR C OMMUNICATIONS R ESEARCH S AN D IEGO , CA, 92121
[email protected] S TAN WAGON M ACALESTER C OLLEGE S T. PAUL , MN 55105
[email protected]
Algorithmic Number Theory MSRI Publications Volume 44, 2008
Smooth numbers and the quadratic sieve CARL POMERANCE
A BSTRACT. This article gives a gentle introduction to factoring large integers via the quadratic sieve algorithm. The conjectured complexity is worked out in some detail.
When faced with a large number n to factor, what do you do first? You might say, “Look at the last digit,” with the idea of cheaply pulling out possible factors of 2 and 5. Sure, and more generally, you can test for divisibility cheaply by all of the very small primes. So it may as well be assumed that the number n has no small prime factors, say below log n. Since it is also cheap to test for probable primeness, say through the strong probable prime test, and then actually prove primality as in [Schoof 2008] in the case that you become convinced n is prime, it also may as well be assumed that the number n is composite. Trial division is a factoring method (and in the extreme, a primality test) that involves sequentially trying n for divisibility by the consecutive primes. This method was invoked above for the removal of the small prime factors of n. The only thing stopping us from continuing beyond the somewhat arbitrary cut off of log n is the enormous time that would be spent if the smallest prime factor of n is fairly large. For example, if n were a modulus being used in the RSA cryptosystem, then as current protocols dictate, n would be the product of two primes of the same order of magnitude. In this case, factoring n by trial division would take roughly n1=2 steps. This already is an enormous calculation if n has thirty decimal digits, and for numbers only slightly longer, the calculation is not possible at this time by the human race and all of their computers. Difference of squares. We have long known however that trial division is not the only game in town for factoring. Take the number 8051 for example. This number is composite and not divisible by any prime up to its logarithm. One can see instantly (if one looks) that it is 8100 49, that is, 8051 D 902 69
72 :
70
CARL POMERANCE
Thus, we can use algebra to factor 8051 as a difference of squares. It is .90 7/ .90 C 7/, or 83 97. Every odd composite can be factored as a difference of squares (an easy exercise), so why don’t we use this method instead of trial division? Let us try again on the number n D 1649. Again, 1649 is composite, but not divisible by any prime up to its logarithm. What worked with 8051 was to take the first square above 8051, namely 902 , and then notice that 902 8051 D 49, where 49 is recognized as a square. It would seem in general that one could walk through the sequence x 2 n with x D dn1=2 e; dn1=2 e C 1; : : : , looking for squares. With n D 1649 we have 412
n D 32;
42
2
n D 115;
432
n D 200;
.1/
and so on, with no squares in immediate sight. Despite this failure, the three equations (1) may in fact be already used to factor n. Note that while neither 32 nor 200 is a square, their product is a square: 6400 D 802 . Thus, since 412 32 .mod n/; 432 200 .mod n/; we have 412 432 802 .mod n/, that is, .41 43/2 802 .mod n/:
.2/
We have found a solution to .mod n/. Is this interesting? There are surely plenty of uninteresting pairs a; b with a2 b 2 .mod n/. Namely, take any value of a and let b D ˙a. Have we exhausted all solutions with this enumeration? Well no, since factoring n as a difference of squares would give a pair a; b with b ¤ ˙a. Further, any pair a; b with a2
a2 b 2 .mod n/;
b2
b 6 ˙a .mod n/
.3/
must lead to a nontrivial factorization of n, via gcd.a b; n/. Indeed, (3) implies that n divides .a b/.a C b/, but divides neither factor, so both gcd.a b; n/; gcd.a C b; n/ must be nontrivial factors of n. Moreover, gcd’s are simple to compute via Euclid’s algorithm of replacing the larger member of gcd.u; v/ by its residue modulo the smaller member, until one member reaches 0. Finally, if n has at least two different odd prime factors, then it turns out that at least half of the solutions to a2 b 2 .mod n/ with ab coprime to n also have b 6 ˙a .mod n/, that is, (3) is satisfied. The proof: For an odd prime power p u , the congruence y 2 1 .mod p u / has exactly 2 solutions, so since n is divisible by
SMOOTH NUMBERS AND THE QUADRATIC SIEVE
71
at least two different odd primes, the congruence y 2 1 .mod n/ has at least 4 solutions. Label these values of y as y1 ; y2 ; : : : ; ys , where y1 D 1; y2 D 1. Then a complete enumeration of the pairs of residues a; b modulo n that are coprime to n and satisfy a2 b 2 .mod n/ consists of all pairs a; yi a, where a runs over residues coprime to n and i takes the values 1; : : : ; s. Two of these pairs have i D 1; 2 and s 2 (out of s) of these pairs have i D 3; : : : ; s. The latter pairs satisfy (3), and since s 4, we are done. So, I repeat the question, is our solution to (2) interesting? Here, we have a D 114 D 41 43 mod n and b D 80. Yes, we do have a; b satisfying (3), so yes this is an interesting pair. We now compute gcd.114 80; 1649/. The first step of Euclid gives gcd.34; 17/, and the second step gives 17. That is, 17 is a divisor of 1649. Division reveals the cofactor, it is 97, and a couple of primality tests show that our factorization is complete. Actually, since we have previously checked that 1649 has no prime factors up to its logarithm, about 7:4, we already know that 17 and 97 have no prime factors below their square roots, and so are both prime. If we had actually waded through the sequence in (1) looking for a square, we would have had to go all the way to 572 n D 402 , and clearly in this example, trial division would have been superior. In general, trying to factor a number as a difference of squares is inferior to trial division for most numbers. But by altering n by multiplying it by small odd numbers, one can then skew things so that in the worst case the time spent is only about n1=3 steps, essentially a factoring method of R. S. Lehman. For example, if one tries to factor 51649 D 8245 by a difference of squares, the first try works, namely 8245 D 912 62 D 97 85. Taking the gcd of the factors with 1649 reveals the factorization we are looking for. A crucial lemma. These thoughts lead down a different road. We would like to pursue what looked like perhaps fortuitous good luck with the equations in (1). If one were to try and describe what we did as an algorithm that works for a general number n, the part where we pick out the first and third of the equations to get the right sides to multiply to a square looks a bit suspect. In general we have this sequence of integers x 2 n as x runs starting above n1=2 and we wish to pick out a subsequence with product a square. Surely we should not be expected to examine all subsequences, since the number of these grows exponentially. Let us look at a probabilistic model for this problem. We are presented with a random sequence of integers in the interval Œ1; X and we wish to stop the sequence as soon as some non-empty subsequence has product a square. After how many terms do we expect to stop, how do we recognize when to stop, and
72
CARL POMERANCE
how do we find the subsequence? The answers to these questions involve smooth numbers. A number m is smooth if all of its prime factors are small. Specifically, we say m is B-smooth if all of its prime factors are B. The first observation is that if a number in our sequence is not smooth, then it is unlikely it will be used in a subsequence with product a square. Indeed, if the number m is divisible by the large prime p, then if m is to be used in the square subsequence, then there necessarily must be at least one other term m0 in the subsequence which also is divisible by p. (This other term m0 may be m itself, that is, perhaps p 2 jm.) But given that p is large, multiples of p will be few and far between, and finding this mate for m will not be easy. So, say we agree to choose some cut off B, and discard any number from the sequence that is not B-smooth. Let us look at the numbers that are not discarded, these B-smooth numbers. The following lemma is crucial. L EMMA . If m1 ; m2 ; : : : ; mk are positive B-smooth integers, and if k > .B/ .where .B/ denotes the number of primes in the interval Œ1; B/, then some non-empty subsequence of .mi / has product a square. P ROOF. For a B-smooth number m, look at its exponent vector v.m/. This is a simple concept. If m has the prime factorization mD
.B/ Y
pivi ;
iD1
where pi is the i th prime number and each exponent vi is a nonnegative integer, then v.m/ D .v1 ; v2 ; : : : ; v.B/ /. Then a subsequence mi1 ; : : : ; mi t has product a square if and only if v.mi1 / C : : : C v.mi t / has all even entries. That is, if and only if this sum of vectors is the 0-vector mod 2. Now the vector space .B/ F 2 , where F 2 is the finite field with 2 elements, has dimension .B/. And we have k > .B/ vectors. So this sequence of vectors is linearly dependent in this vector space. However, a linear dependency when the field of scalars is F 2 is exactly the same as a subsequence sum being the 0-vector. This proof suggests an answer to our algorithmic question of how to find the subsequence. The proof uses linear algebra, and this subject is rife with algorithms. Actually, there is really only one algorithm that is taught in beginning linear algebra classes, namely Gaussian reduction of a matrix, and then there are many, many applications of this one technique. Well, now we have another application. With a collection of smooth numbers, form their exponent vectors, reduce these modulo 2, and then use Gaussian reduction to find a nonempty subsequence with sum the 0-vector modulo 2.
SMOOTH NUMBERS AND THE QUADRATIC SIEVE
73
In particular, we shall find the concept of an exponent vector very useful in the sequel. Note that knowledge of the complete exponent vector of a number m is essentially equivalent to knowledge of the complete prime factorization of m. Also note that though in the proof of the lemma we wrote out exponent vectors in traditional vector notation, doing so in general may itself be an exponentially huge problem, even if we “know” what the vector is. Luckily, exponent vectors are sparse creatures with most entries being zero, so that one can work with them modulo 2 in a more compact notation that merely indicates where the odd entries are. A proto-algorithm. So now we have a proto-algorithm. We are given a number n which is composite, has no prime factors up to its logarithm, and is not a power. We insist that n not be a power in order to ensure that n is divisible by at least two different odd primes. It is easy to check if a number is a power by taking roots via Newton’s method, and for close calls to integers, exponentiating that integer to see if n is a power of it. Our goal is to find a nontrivial factorization of n. 1. Choose a parameter B, and examine the numbers x 2 n for B-smooth values, where x runs through the integers starting at dn1=2 e. 2. When you have more than .B/ numbers x with x 2 n being B-smooth, form the exponent vectors of these B-smooth numbers, and use linear algebra to find a subsequence x12 n; : : : ; x t2 n which has product a square, say A2 . 3. From the exponent vectors of the numbers xi2 n, we can produce the prime factorization of A, and thus find the least nonnegative residue of A modulo n, call it a. Find too the least nonnegative residue of the product x1 : : : x t modulo n, call it b. 4. We have a2 b 2 .mod n/. If a 6 ˙b .mod n/, then compute gcd.a b; n/. Otherwise, return to step 1, find additional smooth values of x 2 n, find a new linear dependency in step 2, and attempt once again to assemble congruent squares to factor n. There are clearly a few gaps in this proto-algorithm. One is a specification of the number B. Another is a fuller description of how one examines a number for Bsmoothness, namely how one recognizes a B-smooth number when presented with one. Recognizing smooth numbers. Let us look at the second gap first, namely the recognition of B-smooth numbers. Trial division is a candidate for a good method, even though it is very slow as a worst-case factorization algorithm. The point is that B-smooth numbers are very far from the worst-case of trial division, in fact they approach the best case. There are fewer than B primes up to B, so
74
CARL POMERANCE
there are very few trials to make. In fact the number of trial divisions is at most the maximum of log2 n and .B/. But we can do better. Let us review the sieve of Eratosthenes. One starts with a list of the numbers from 2 to some bound X . Then one recognizes 2, the first unmarked number, as prime, and crosses off every second number starting at 4, since these are all divisible by 2 and hence not prime. The next unmarked number is 3, which is the next prime, and then every third number is crossed off after 3, and so on. If one completes this with primes up to X 1=2 , then every remaining unmarked number is prime. But we are not so interested in the unmarked numbers, rather the marked ones. In fact, the more marked a number the better, since it is then divisible by many small primes. This sieve procedure can rather easily be made completely rigorous. Indeed, interpret making a “mark” as taking the number in the relevant location and replacing it with its quotient by the prime being sieved. In addition, sieve by higher powers of the primes as well, again dividing by the underlying prime. If one does this procedure with the primes up to B and their higher powers, the B-smooth numbers up to X are detected by locations that have been transformed into the number 1. The time to do this is proportional to X times the sum of the reciprocals of the primes up to B and their powers up to X . This sum, as we will see in (4), is about log log B. Thus, in time proportional to X log log B (for B at least 16, say, to have log log B > 1) we can locate all of the B-smooth numbers up to X . That is, per number, we are spending only about log log B steps on average. This count compares with about B steps per number for trial division, a very dramatic comparison indeed. Further, there is a trick for reducing the complexity of the individual steps in the sieve involving the subtraction of low-precision logarithms to simulate the division. So it is a win-win for sieving. However, we are not so interested in locating smooth numbers in the interval from 2 to X , but rather in the polynomial sequence x 2 n as x runs. This is not a big hurdle, and essentially the same argument works. What makes the sieve of Eratosthenes work is the regular places in the sequence where we see a multiple of p. This holds as well for any polynomial sequence. One solves the quadratic congruence x 2 n 0 .mod p/. For p > 2 there are either no solutions, 2 solutions, or in the case that pjn, just 1 solution. Of course the generic cases are 0 and 2 solutions, and each should occur roughly half the time. If there are no solutions, then there is no sieving at all. If there are 2 solutions, say a1 ; a2 , where a2i n 0 .mod p/, then we find the multiples of the prime p in the sequence for each x ai .mod p/, where i D 1; 2. For example, say n 4 .mod 7/, so that a1 D 2; a2 D 5. Then the values of x where 7 divides x 2 n can be found in quite regular places, namely those values of x that are congruent to 2 or 5 modulo 7. After an initial value is found that is 2 (mod 7), one can
SMOOTH NUMBERS AND THE QUADRATIC SIEVE
75
find the remaining places in this residue class by adding 7’s sequentially to the location number, and similarly for the 5 (mod 7) residue class. One has a similar result for higher powers of p and also for the special case p D 2. As with the sieve of Eratosthenes above, the number of steps we will spend per polynomial value is proportional to just log log B. That is, it takes about as much time to tell if a value is smooth as it does just to look at the value, as long as we amortize the time over many members of the polynomial sequence. So this is how we recognize B-smooth values of x 2 n: we use a quadratic sieve. An optimization problem. Next, in our proto-algorithm, there should be some guidance on the choice of the parameter B. Of course, we would like to choose B optimally, that is, we should choose a value of B which minimizes the time spent to factor n. This optimization problem must balance two principal forces. On the one hand, if B is chosen very small, then we do not have to find very many B-smooths in the sieve, and the matrix we will be dealing with will be small. But numbers that are B-smooth with a very small value of B are very sparsely distributed among the natural numbers, and so we may have to traverse a very long sequence of x values to get even one B-smooth value of x 2 n, much less the requisite number of them. On the other hand, if B is chosen large, then B-smooth numbers are fairly common, and perhaps we will not have such a hard time finding them in the polynomial sequence x 2 n. But, we will need to find a great many of them for large B, and the matrix we will be dealing with will be large. So, the optimization problem must balance these conflicting forces. To solve this problem, we should have a measure of the likelihood that a value x 2 n is B-smooth. This in fact is a very hard problem in analytic number theory, one that is essentially unsolved in the interesting ranges. However, the number n we are trying to factor may not be up on the latest research results! That is, perhaps we should be more concerned with what is true rather than what is provable, at least for the design of a practical algorithm. This is where heuristics enter the fray. Let us assume that a polynomial value is just as likely to be smooth as a random number of the same magnitude. This assumption has been roughly borne out in practice with the quadratic sieve algorithm, as well as other factorization methods such as the number field sieve; see the survey [Stevenhagen 2008]. What is the order of magnitude of our polynomial values? If x runs in the interval Œn1=2 ; n1=2 C n" ; where 0 < " < 1=2, then the numbers x 2 n are all smaller than approximately 2n1=2C" . Let X be this bound, and let us ask for the chance that a random number up to X is B-smooth.
76
CARL POMERANCE
An analytic tidbit. In analytic number theory we use the notation .X; B/ for the counting function of the B-smooth numbers in the interval Œ1; X . That is, .X; B/ D #fm W 1 m X; m is B-smoothg: Let us try our hand at estimating this function in the special case that B D X 1=2 . We can do this by an inclusion-exclusion, with a single exclusion, since no number up to X is divisible by two primes > X 1=2 . Thus, X .X; X 1=2 / D bX c bX=pc; X 1=2
where p runs over primes in the stated interval. This identity uses the fact that there are exactly bX=pc multiples of p in the interval Œ1; X . And the multiples of p are definitely not x 1=2 -smooth, so must be excluded. By removing the floor functions in the above display, we create an error bounded by .X /, so that the prime number theorem implies that X 1=2 .X; X / D X 1 1=p C O.X= log X /: X 1=2
We now use a theorem of Mertens stating that we have X 1=p D log log t C C C O.1= log t /;
.4/
pt
for a particular constant C . This theorem predates the prime number theorem, but can also be derived from it. Using (4) we obtain X X X 1=p D 1=p 1=p X 1=2
pX
D log log X
pX 1=2
log log.X 1=2 / C O 1= log.X 1=2 /
D log 2 C O.1= log X /: We thus have .X; X 1=2 / D .1
log 2/X C O.X= log X /;
so that .X; X 1=2 / 1 log 2 as X ! 1: X For example, about 30% of all numbers have no prime factors above their square root. It may seem surprising that such a large proportion of numbers can be built out of so few primes.
SMOOTH NUMBERS AND THE QUADRATIC SIEVE
77
The uu philosophy. In fact one can easily see that the exact same argument shows that .X; X 1=u / 1 log u X for each fixed value of u in the interval Œ1; 2. But what of larger values of u? Here we have the celebrated result of K. Dickman that .X; X 1=u / .u/ .5/ X for each fixed u 1, where .u/ is the Dickman–de Bruijn function. This function is defined as the continuous solution to the differential difference equation u0 .u/ D .u 1/ for u > 1, with initial condition .u/ 1 on the interval Œ0; 1. The function .u/ is always positive, and as u grows, it decays to 0 roughly like u u . A result of E. R. Canfield, P. Erd˝os, and myself is that even if u is not fixed, we still have something like (5) holding. Namely, for X ! 1; u ! 1 subject to X 1=u > .log X /1C" , we have .X; X 1=u / Du X
.1Co.1//u
;
.6/
for any fixed " > 0. The choice of the smoothness bound. Let B D X 1=u . Then X= .X; X 1=u / is approximately equal to the reciprocal of the probability that a random integer up to X is B-smooth (it is exactly this reciprocal if X is an integer), and so X= .X; X 1=u / is about equal to the expected number of random trials of choosing numbers in Œ1; X to find one which is B-smooth. However, we would like to have about .B/ numbers that are B-smooth and sieving allows us to spend about log log B steps per candidate, so the expected number of steps to find our requisite stable of B-smooths is about .B/.log log B/X= .X; X 1=u /. Our goal is to minimize this expression. It is a bit ungainly, but if we make the rough approximations .B/ log log B X 1=u , X= .X; X 1=u / uu , we are looking at the simpler expression X 1=u uu : We would like to choose u so as to minimize this expression. Take logarithms: so we are to minimize 1 log X C u log u: u The derivative is 0 when u2 .log uC1/ D log X . Taking the log of this equation, we find that log u 21 log log X , so that u .2 log X= log log X /1=2
78
CARL POMERANCE
and B D exp .2
1=2
C o.1//.log X log log X /1=2 :
.7/
This calculation allows us not only to find the key parameter B, but also to estimate the running time. With B given by (7), we have X 1=u uu D exp .21=2 C o.1//.log X log log X /1=2 ; .8/ and so this expression stands as the number of steps to find the requisite number of B-smooth values. Recall that X D 2n1=2C" in our proto-algorithm, so that the expression in (8) is of the form no.1/ . That is, we do not need to take " as fixed in the expression for X ; it may tend to 0 slowly. Letting X D n1=2Co.1/ , we get B D exp .1=2 C o.1//.log n log log n/1=2 ; X 1=u uu D exp .1 C o.1//.log n log log n/1=2 ; where the second expression is the number of steps to do the sieving to find the requisite number of B-smooth polynomial values. A general principle, a moral, and three bullets. The heuristic analysis above is instructive in that it can serve, in an almost intact manner, for many factoring algorithms. What may change is the number X , which is the bound on the auxiliary numbers that are examined for smoothness. In the quadratic sieve algorithm, we have X just a little above n1=2 . In the number field sieve, this bound on auxiliary numbers is much smaller, it is of the form no.1/ . Smaller numbers are much more likely to be smooth than larger numbers, and this general principle “explains” the asymptotic superiority of the number field sieve over the quadratic sieve; see [Stevenhagen 2008]. Our story has a moral. Smooth numbers are not an artifact, they were forced upon us once we decided to combine auxiliary numbers to make a square. In fact for random numbers this is a theorem of mine: the bound in (8), of X 1=u uu for the depth of the search for random auxiliary numbers below X to form a square, is tight. So the heuristic passage to B-smooth numbers is justified — one is unlikely to be able to assemble a square from random numbers below X in fewer choices than the bound in (8), even if one does not restrict to B-smooth numbers with B the bound in (7). There are several important points about smooth numbers that make them indispensable in many number-theoretic algorithms: Smooth numbers have a simple multiplicative structure. Smooth numbers are easy to recognize. Smooth numbers are surprisingly numerous. See [Granville 2008] in this volume for much more about smooth numbers.
SMOOTH NUMBERS AND THE QUADRATIC SIEVE
79
Gaussian reduction. If Gaussian reduction is used on the final matrix, our complexity bound is ruined. In fact, our matrix will be about BB, and the bound for the number of steps to reduce such a matrix is about B 3 . With B given as in (8), the time for the matrix step is then about exp..3=2 C o.1//.log n log log n/1=2 /, which then would be the dominant step in the algorithm, and would indicate that perhaps a smaller value of B than in (8) would be in order. There are several thoughts about the matrix issue. First, Gaussian reduction, though it may be the only trick in the beginning undergraduate linear algebra book, is not the only trick we have. There are in fact fast asymptotic methods, that are practical as well. I refer to the Wiedemann coordinate recurrence method, the Lanczos method, etc. These reduce the complexity to the shape B 2Co.1/ , and so the total asymptotic, heuristic complexity of the quadratic sieve becomes, as first intimated above, exp..1 C o.1//.log n log log n/1=2 /. Also, Gaussian reduction is not quite as expensive as its complexity estimate indicates. In practice, the matrix starts out as quite sparse, and so for awhile fill-in can be avoided. And, the arithmetic in the matrix is binary, so a programmer may exploit this, using say a 32-bit word size in the computer, and so process 32 matrix entries at once. The matrix poses another problem as well, and that is the space that is needed to store it and process it. This space problem is mitigated by using a sparse encoding of the matrix, namely a list of where the 1’s are in the matrix. This sparse encoding might be used at the start until the matrix can be cut down to size somewhat. In practice, people have found that it pays to slightly deoptimize B on the low side. This in essence is a concession to the matrix problem, both to the space required and the time required. While sieving can easily be distributed to many unextraordinary processors, no one knows how to do this efficiently with the matrix, and so this final step might well hog the memory and time of a large expensive computer. Conclusion. The quadratic sieve is a deterministic factoring algorithm with conjectured complexity exp..1 C o.1//.log n log log n/1=2 /: It is currently the algorithm of choice for “hard” composites with about 20 to 120 digits. (By “hard” I mean that the number does not have a small prime factor that could be discovered by trial division or the elliptic curve method, nor does the number succumb easily to other special methods such as the p 1 factoring method or the special number field sieve.) For larger numbers, the number field sieve moves to the front, but this “viability border” between the quadratic sieve and the number field sieve is not very well defined, and shifts as new computer
80
CARL POMERANCE
architectures come on line and when new variations of the underlying methods are developed. There are many variations of the quadratic sieve which speed it up considerably (but do not change the asymptotic complexity estimate of exp..1 C o.1//.log n log log n/1=2 /; that is, the variations only affect the “o.1/”). The most important of these variations is the idea of using multiple polynomials, due to J. Davis and P. Montgomery. Essentially all of the practical factoring methods beyond trial division are heuristic, though the elliptic curve method is “almost” rigorous. The fastest, rigorous factoring algorithm is a probabilistic method of H. W. Lenstra and me, with expected complexity exp..1 C o.1//.log n log log n/1=2 /, namely the same as for the quadratic sieve (though here a “fatter” o.1/ makes the rigorous method inferior in practice). The fastest, rigorous deterministic factoring algorithm is due to J. Pollard and to V. Strassen, with a complexity of n1=4Co.1/ . We refer to [Crandall and Pomerance 2005] and [Granville 2008] for further reading on this, and for references to original papers and other surveys. Acknowledgements. I am indebted to John Voight for the careful notes he took at my lecture, and to Lancelot Pecquet, who offered numerous improvements for an earlier version of this article. This article was written while I was a member of the Technical Staff at Bell Laboratories.
References [Crandall and Pomerance 2005] R. Crandall and C. Pomerance, Prime numbers, 2nd ed., Springer-Verlag, New York, 2005. [Granville 2008] A. Granville, “Smooth numbers: computational number theory and beyond”, pp. 267–323 in Surveys in algorithmic number theory, edited by J. P. Buhler and P. Stevenhagen, Math. Sci. Res. Inst. Publ. 44, Cambridge University Press, New York, 2008. [Pomerance 1996] C. Pomerance, “A tale of two sieves”, Notices Amer. Math. Soc. 43:12 (1996), 1473–1485. [Schoof 2008] R. J. Schoof, “Four primality testing algorithms”, pp. 101–125 in Surveys in algorithmic number theory, edited by J. P. Buhler and P. Stevenhagen, Math. Sci. Res. Inst. Publ. 44, Cambridge University Press, New York, 2008. [Stevenhagen 2008] P. Stevenhagen, “The number field sieve”, pp. 83–100 in Surveys in algorithmic number theory, edited by J. P. Buhler and P. Stevenhagen, Math. Sci. Res. Inst. Publ. 44, Cambridge University Press, New York, 2008.
SMOOTH NUMBERS AND THE QUADRATIC SIEVE
C ARL P OMERANCE D EPARTMENT OF M ATHEMATICS DARTMOUTH C OLLEGE H ANOVER , NH 03755-3551 (603) 646-2415
[email protected]
81
Algorithmic Number Theory MSRI Publications Volume 44, 2008
The number field sieve PETER STEVENHAGEN
A BSTRACT. We describe the main ideas underlying integer factorization using the number field sieve.
C ONTENTS 1. Introduction 2. Factoring by congruent squares 3. Number rings 4. Sieving for smooth elements 5. Primes dividing a b˛ 6. Sieving and linear algebra 7. Nonmaximality of Z Œ˛ 8. Finiteness results from algebraic number theory 9. Quadratic character columns 10. Square root extraction 11. Running time References
83 84 86 88 90 91 93 95 96 97 98 100
1. Introduction The number field sieve is a factoring algorithm that tries to factor a hard composite number by exploiting factorizations of smooth numbers in a wellchosen algebraic number field. It is similar in nature to the quadratic sieve algorithm, but the underlying number theory is less elementary, and the actual implementation involves a fair amount of optimization of the various parameters. The key idea of the algorithm, the use of smooth numbers in number rings different from Z , was proposed in 1988 by Pollard. Many people have contributed theoretical and practical improvements since then. An excellent reference for many of the details left out in this paper is [Lenstra and Lenstra 1993]. It contains 83
84
PETER STEVENHAGEN
a complete bibliography of the early years of the number field sieve, as well as original contributions by most of the main developers of the algorithm. Among the successes of the algorithm are the 2005 factorization of the 663bit RSA challenge number RSA-200 D p100 q100 into a product of two primes of 100 decimal digits each, and the factorization in 2006 of the 275-digit Cunningham number 6353
1 D 5 p120 p155
into a product of 5 and two primes of 120 and 155 digits, respectively. Unlike RSA-200, the second number has a special form that can be exploited by the number field sieve. No other algorithm is currently capable of factoring integers of this size. For the quadratic sieve algorithm and the elliptic curve method, the conjectural asymptotic expected running time for factoring a large number n is p exp log n log log n; which is on a log-log scale halfway between exponential and polynomial. The number field sieve conjecturally improves this bound to .1:1/ exp c.log n/1=3 .log log n/2=3 ; where the constant c D .64=9/1=3 1:93 can be lowered to .32=9/1=3 1:53 if we are dealing with numbers n of the special form explained in section 3.
2. Factoring by congruent squares The number field sieve is one of the algorithms that tries to factor n by producing congruent squares modulo n, as explained in [Pomerance 2008]. For this we will assume from now on that n is odd, composite and not a power of a prime number. Note that each of these conditions can easily be checked for large n. One tries to find integers x and y satisfying x 6 ˙y mod n and .2:1/
x 2 y 2 mod n:
In this case, gcd.x y; n/ is a non-trivial factor of n. As at least half of all pairs .x; y/ of invertible residue classes modulo n satisfying .2:1/ satisfy x 6 ˙y mod n, we may expect to find a non-trivial factor of n within a few tries if we can produce solutions .x; y/ to .2:1/ in a pseudo-random way. An old factoring algorithm based on this idea is the continued fraction method. It uses the convergents xi =yi 2 Q (i D 1; 2; : : :) occurring in the continued
THE NUMBER FIELD SIEVE
85
p fraction expansion of n as defined in [Buhler and Wagon 2008]. These fractions, which can be computed from simple two-term recursive relations for the p integers xi and yi , provide rational approximations to the real number n. The associated integers Qi D xi2 nyi2 p are of absolute value at most 2 n, and we may hope to be able to find a fair number of these Qi which are smooth. As we saw in [Pomerance 2008], it is a matter of linear algebra over the field of two elements to construct a square from a sufficiently large Q set of integers that factor over a given factor base. From 2 every square y D i2I Qi , we find a solution Y 2 xi y 2 mod n i2I
to the congruence .2:1/. The quadratic sieve replaces the integers Qi in the continued fraction algorithm by the values of the polynomial Q.X / D X 2 n: p For integers x satisfying jx nj < M for some small bound M , the absolute p value of Q.x/ is not much larger than 2M n. As M has to be large enough to allow for a reasonable supply of x-values, the numbers Q.x/ we encounter here are somewhat larger than the Qi above. However, the advantage of using values of the polynomial Q is that the values of x for which Q.x/ is smooth may be detected by sieving. Q From the smooth values of Q, we construct a square y 2 D x2S Q.x/ and a solution Y 2 x y 2 mod n s2S
to the basic congruence .2:1/ exactly as for the continued fraction algorithm. The algebraic description one may give of both methods is as follows. We have constructed squares .x 2 ; y 2 / 2 Z Z whose images under the reduction map Z Z Z =nZ Z =nZ .xi2 ; xi2 .x 2 ; x 2
nyi2 /
’
.xi2 ; xi2 /
n/
’
.x 2 ; x 2 /
lie in the diagonal. If we are lucky, .x; y/ does not land in D D f.x; ˙x/ W x 2 Z =nZ g
86
PETER STEVENHAGEN
and we find a non-trivial factor of n. As .x 2 ; y 2 / is constructed in such a way that .x; y/ has no obvious reason to always end up in D, we expect to be lucky in at least half of all cases. The construction of squares in the continued fraction and quadratic sieve p methods requires many auxiliary numbers Qi or Q.x/ of size O. n/ to be smooth. Asymptotically, the superior performance of the number field sieve stems from the fact that it is a sieving method that requires smaller auxiliary numbers to be smooth: they are of size exp c 0 .log n/2=3 .log log n/1=3 with c 0 D .64=3/1=3 2:77. Informally phrased, the length of these numbers is not half of the length of n, but only the 2=3-rd power of the length of n. This improvement is obtained by replacing Z Z by Z Z Œ˛ for a suitable number ring Z Œ˛ and producing squares .x 2 ; 2 / with diagonal image under the reduction map Z Z Œ˛ Z =nZ Z =nZ : Exactly as before, this yields a solution .2:2/
x 2 . /2 mod n
to our basic congruence .2:1/.
3. Number rings A number field is a finite field extension of the field Q of rational numbers, and a number ring [Stevenhagen 2008] is by definition a subring of a number field. The basic type of number ring used in the number field sieve is the ring Z Œ˛ D Z ŒX =f Z ŒX
generated by a formal zero ˛ D .X mod f Z ŒX / of some irreducible polynomial f P 2 Z ŒX i of degree d 1. The elements of this ring are finite expressions i0 ai ˛ with ai 2 Z . One may obtain an embedding Z Œ˛ C by taking ˛ to be a complex zero of f . Note that even though the field of fractions of a number ring is always of the form Q .˛/ for some root ˛ of an irreducible polynomial in Z ŒX , there are many number rings that are of finite rank over Z but not of the form Z Œ˛. We will take f to be a monic irreducible polynomial in Z ŒX , such that Z Œ˛ D Z 1 ˚ Z ˛ ˚ Z ˛ 2 ˚ : : : ˚ Z ˛ d
1
is integral over Z . It is an order in the field of fractions Q .˛/ of Z Œ˛. The norm N W Q .˛/ ! Q
THE NUMBER FIELD SIEVE
87
takes x 2 Q .˛/ to the determinant of the multiplication-by-x map on the Q vector space Q .˛/. It is multiplicative, and for non-zero x 2 Z Œ˛, the absolute value jN .x/j D #.Z Œ˛=x Z Œ˛/ 2 Z of the norm of x measures the “size” of x. (3.1) Example. The best known example of a number ring with d D deg.f / > 1 is probably thep ring Z Œi of Gaussian integers obtained by putting f D X 2 C 1 1. For this ring, the norm function is given by the simple and ˛ D i D formula N .a C bi/ D a2 C b 2 :
//
More generally, one can find the norm of an element x D a P the irreducible polynomial f D diD0 ci X i of ˛ as .3:2/
N .a
b˛/ D b d f .a=b/ D
Pd
iD0 ci a
b˛ 2 Q .˛/ from
i bd i :
For polynomial expressions g.˛/ in ˛ of higher degree the norm can efficiently be computed from the resultant of f and g, but we won’t need this. For a number ring Z Œ˛ to be useful in factoring n, it needs to come with a reduction homomorphism W Z Œ˛ ! Z =nZ : Giving such a homomorphism amounts to giving a zero mD .˛/ of f modulo n. In order to have a small number ring Z Œ˛, one tries to choose a polynomial f of moderate degree — in practice d is usually between 3 and 10, although its optimal value does slowly tend to infinity with n — and having small coefficients. This is not an easy problem, but for certain special n one can find very small f . (3.3) Example. For the Fermat number 9
n D F9 D 22 C 1 D 2512 C 1 the polynomial f D X 5 C 8 is irreducible in Z ŒX and satisfies f .2103 / D 2515 C 8 D 8n 0 mod n: Similarly, for the record factorization of the Cunningham number n D 6353 1 mentioned in the introduction, the polynomial f D X 6 6 is irreducible in Z ŒX and satisfies f .659 / D 6354
6 D 6n 0 mod n:
//
88
PETER STEVENHAGEN
For numbers n of the special form n D r e s, with r , s and e small, one can find a small polynomial f as in the example. For general n we cannot hope to be so lucky in finding f , and one has to deal with large number rings. The special and the general number field sieve stand for the versions of the algorithm corresponding to these two cases. As is to be expected, the special number field sieve has a somewhat better conjectural running time, and this is reflected by the size of the record factorizations for each of these versions. We will mainly be concerned with the case of general integers n to be factored. For such n, the base m method yields a polynomial f of any desired degree d > 1 such that m D m.d/ is a zero of f modulo n. One simply puts m D integer part of n1=d and writes n in base m as nD
Pd
i iD0 ci m :
P Then f D diD0 ci X i is a polynomial in Z ŒX satisfying f .m/ D n. In realistic situations n is much larger than d, which ensures that f will be monic; one may further assume that f is irreducible, as non-trivial factors of f yield non-trivial factors of n. From jci j < m < n1=d we deduce that the discriminant .f / of f satisfies j.f /j < d 2d n2
.3:4/
3=d
:
As j.f /j often exceeds n, we cannot hope to be able to factor .f /.
4. Sieving for smooth elements Having chosen d, f and m as above, we can combine the ordinary reduction map on Z with our reduction map on Z Œ˛ to obtain a ring homomorphism Z Z Œ˛
x;
Pd
1 iD0 ai ˛
i
’
Z =nZ Z =nZ
x mod n;
Pd
1 i iD0 ai m
mod n :
By construction, the elements .a bm; a b˛/ have -image in the diagonal. In order to combine them into squares, we need to find sets S of coprime integer pairs .a; b/ for which we have Y (4.1) .a bm/ is a square in Z ; .a;b/2S
(4.2)
Y .a;b/2S
.a
b˛/
is a square in Z Œ˛.
THE NUMBER FIELD SIEVE
89
As in the case of the quadratic sieve, this is in principle done by sieving for smooth elements .a bm; a b˛/ and combining them into a square via linear algebra methods over F2 . The details are however more involved. Let us define an element .a bm; a b˛/ 2 Z Z Œ˛ to be y-smooth if a bm is a y-smooth rational integer and a b˛ is a y-smooth algebraic integer in Z Œ˛. The latter condition simply means that the norm N .a b˛/ 2 Z is a y-smooth integer. On the rational side, the procedure to find a set S for which (4.1) holds is more or less standard. We pick a universe U D f.a; b/ W jaj u;
0
and
gcd.a; b/ D 1g
of coprime integer pairs .a; b/ depending on a parameter u. Using the factor base B1 consisting of primes p y and a sign-bit, we can determine the subset of pairs .a; b/ 2 U for which a bm is y-smooth by sieving. here we have a 2-dimensional array of pairs .a; b/ over which the sieving with the primes in B1 needs to be done. One may simply choose to sieve over a for each value of b, but there exist other methods than this straightforward line-byline sieving. Recent record factorizations have used a combination of different sieving methods. On the algebraic side, the pairs .a; b/ 2 U for which N .a b˛/ is y-smooth can also be found by sieving with the primes in B1 , since we see from (3.2) that the norms .4:3/
N .a
b˛/ D b d f .a=b/ D
d X
ci ai b d
i
iD0
are the .a; b/-values of the homogeneous polynomial f .X; Y /. But it is not sufficient to find elements a b˛ whose norm factors over our Q factor base B1 . This information will only enable us to construct a product .a;b/2S .a b˛/ with square norm, which is far too weak to imply (4.2). A square in Z Œ˛ certainly has square norm, but the converse only holds in the trivial case Z Œ˛ D Z , where the norm of an element is the element itself. (4.4) Example. In the ring of Gaussian integers Z Œi we have N .3 C 4i / D 32 C 42 D 52 D N .5/: Now 3 C 4i D .2 C i /2 is indeed a square, but 5 D .2 C i /.2
i / is not.
//
The problem we encounter is that different prime divisors of an element x 2 Z Œ˛ can give rise to the same prime factor p in its norm N .x/. This forces us to keep track of “prime factors” of x in the ring Z Œ˛.
90
PETER STEVENHAGEN
5. Primes dividing a
b˛
The theory of prime divisors in number rings lies at the very heart of algebraic number theory, and understanding the workings of the number field sieve is not possible without entering this area. Rather than assuming the more extensive exposition on the arithmetic of number rings in [Stevenhagen 2008], we use the concrete example of our number ring Z Œ˛ to illustrate and motivate the more general statements of algebraic number theory in that paper. Let R be any number ring. A prime in R is a non-zero prime ideal p R. The residue class ring F D R=p of a prime is a finite field. We say that p divides an element x 2 R if x is contained in p. For the number ring Z the primes p Z correspond to the prime numbers p 2 Z . A prime p R lies over a unique prime p Z D p \ Z of Z . The corresponding prime number p is the characteristic of the field F . It is the unique prime number contained in p. The degree of p is the degree of F over its prime field Fp . A prime of degree 1 in Z Œ˛ is just the kernel p of a ring homomorphism W Z Œ˛ Fp for some prime number p. As may be specified by giving the rational prime p together with the zero rp D .˛/ 2 Fp of .f mod p/ to which ˛ is mapped, we use the ad hoc notation .pI rp / to denote p D ker . Primes of degree 1 are the only primes we need for the number field sieve. Indeed, suppose that we have .a; b/ 2 U as in the previous section, and that p is a prime over p dividing a b˛. Then we have p - b, since pjb would imply a 2 p \ Z D p Z , contradicting the coprimality of a and b. From a D b˛ 2 F D Z Œ˛=p we find that rp D ˛ D ab 1 mod p is a zero of .f mod p/, and that p D p Z Œ˛ C .a
b˛/Z Œ˛
is the kernel of the map
Z Œ˛ Fp
˛ ’ .ab
1
mod p/:
Thus p is the prime .pI rp / of degree 1 that is generated by p and rp ˛, with rp D ab 1 mod p a zero of .f mod p/. Conversely, a prime .pI rp / of degree 1 divides a b˛ if we have rp D ab 1 mod p. From our norm formula .3:2/, we see that ab 1 mod p is a zero of .f mod p/ if and only if N .a b˛/ is divisible by p. We conclude that for a rational prime number p, there is a prime divisor p of a b˛ in Z Œ˛ that lies over p if and only if p divides the norm N .a b˛/. If p divides N .a b˛/, the prime p D .pI rp /
THE NUMBER FIELD SIEVE
with rp D .ab
1
91
mod p/ is the unique such prime, and we call ep .a
b˛/ D ordp .N .a
b˛//
the exponent to which p occurs in a b˛. For the primes p of Z Œ˛ that do not divide a b˛, we put ep .a b˛/ D 0. We then have the following fundamental fact. (5.1) Lemma. For each prime p of degree 1, the exponent ep extends to a homomorphism ep W Q .˛/ ! Z : This Lemma is slightly less innocent than it may appear at first sight, and we will define ep .x/ for arbitrary x 2 Q .˛/ in (7.4). There is actually no need to restrict to primes of degree 1, but we do so as we have not defined the exponent at other primes. For our purposes, it suffices to know that we have ep .x/ D 0 whenever x is a product of elements a b˛ with .a; b/ 2 U and p is a prime of degree at least 2.
6. Sieving and linear algebra On the rational side, we already chose a factor base B1 consisting of the primes p y and a sign bit. For the factorization of our numbers a b˛ in Z Œ˛, we choose a factor base B2 consisting of all primes .pI rp / with p y prime and rp 2 Fp a root of .f mod p/. There may be several primes in B2 lying over a given rational prime p, and the notation .pI rp / enables us to distinguish between such primes, and to identify the prime that accounts for the p-contribution (if any) to N .a b˛/. For each rational p, there are at most d D deg.f / values rp . On average, there is 1 root of .f mod p/ in Fp if we let y tend to infinity. This elegant result of Kronecker, which was generalized by Frobenius, is now often proved as a corollary of the Chebotarev density theorem [Stevenhagen and Lenstra 1996]. We deduce that both B1 and B2 are of size y 1Co.1/ . The combination of rational and algebraic sieving yields a subset U 0 U of pairs .a; b/ 2 U that give rise to a y-smooth factorization of a bm in Z and a y-smooth factorization of a b˛ in Z Œ˛. Such a pair .a; b/ 2 U 0 , together with the exponents of the rational primes p 2 B1 in a bm and the exponents of the algebraic primes p in a b˛, is usually referred to as a relation. All exponents are taken modulo 2, so they can be stored in a single bit. In order to obtain dependencies between the exponent vectors of elements in 0 U , the number #U 0 of relations should exceed #B1 C #B2 . For large factorizations, collecting sufficiently many relations may take several years of computer
92
PETER STEVENHAGEN
time. As different computers can independently test elements .a; b/ 2 U for smoothness, distribution of the computation over a large number of computers is usually necessary to perform this step of the algorithm in practice. The set U 0 , which may consist of millions of relations, is often so large that the linear algebra step over F2 needs to be performed on a computer that is equipped to handle huge amounts of data. It is important that the matrix of exponents is a very sparse matrix, which can be transformed into a much smaller dense matrix before it is given to the reduction algorithm that yields the desired dependencies. A practical reduction algorithm, such as the so-called block Lanczos method, may run for several days on a single large computer. In this case, distribution of the problem over more computers is not an easy matter. Every dependency in the matrix of exponent vectors coming from the pairs .a; b/ 2 U 0 corresponds to a subset S U 0 such that the following two conditions are satisfied: Y (6.1) .a bm/ is positive with even exponents at all primes p 2 Z ; .a;b/2S
(6.2)
Y
.a
b˛/
has even exponents at all primes p Z Œ˛.
.a;b/2S
What we need is the validity of (4.1) and (4.2) in order to obtain the required square in Z Z Œ˛. It is a simple and well known fact that .6:1/ implies .4:1/: requiring positivity is enough to produce true squares from integers having even exponents at all prime numbers. The situation is not so simple in Z Œ˛: several obstructions may prevent the validity of the implication .6:2/ ) .4:2/. Writing Q ˇ D .a;b/2S .a b˛/ for the element in (6.2), they are the following. (6.3) The ring Z Œ˛ is possibly not the ring of integers O of Q .˛/. The ring of integers, which is the maximal order in Q .˛/, is the “textbook ring” for which the theorem of unique prime ideal factorization holds. If we have Z Œ˛ ¤ O, then (6.2) need not imply that ˇ O is the square of an ideal. (6.4) If ˇ O is the square of some ideal c, then c does not have to be a principal O-ideal. This is exactly the reason why unique prime element factorization has to be replaced by unique prime ideal factorization in general number fields. (6.5) If ˇ O is the square of some principal ideal O, we only have ˇ D 2 up to multiplication by units in O. This obstruction already occurs in the case for O D Z . Unlike Z , the ring O usually has infinitely many units. (6.6) If we do obtain an equality ˇ D 2 in O, we may have … Z Œ˛. If this happens, the reduction map is not defined on and we do not obtain our final congruence (2.2).
THE NUMBER FIELD SIEVE
93
Algebraic number theory provides the tools for dealing with all of these obstructions. In the next section, we will deal with the obstructions (6.3) and (6.6), which arise from the fact that Z Œ˛ may be strictly smaller than O. Section 8 is devoted to the obstructions (6.4) and (6.5), which are classical and lie at the roots of algebraic number theory. It will be our aim to bound the index ŒV W .V \ Q .˛/ 2 / of the subgroup of true squares inside the group V Q .˛/ generated by the elements that meet condition (6.2).
7. Nonmaximality of Z Œ˛ The ring of integers O Q .˛/, which consists by definition of all elements of Q .˛/ that occur as the zero of some monic polynomial in Z ŒX , is the maximal order contained in Q .˛/. It is free of rank d D deg.f / over Z , and contains Z Œ˛ as a subring of finite index. There is the classical identity .7:1/
.f / D ŒO W Z Œ˛2
relating the index ŒO W Z Œ˛ to the discriminant .f / of the polynomial f from (3.4) and the discriminant of the number field Q .˛/. As is known to be a non-zero integer, we find that ŒO W Z Œ˛ is bounded by j.f /j1=2 . As we do not want to factor the possibly huge number .f /, we may not be able to determine ŒO W Z Œ˛ or O. However, it is a standard fact that for any x 2 O, we have f 0 .˛/ x 2 Z Œ˛: This is enough to deal with obstruction (6.6): we simply multiply our purported square in Z Z Œ˛ by .f 0 .m/2 ; f 0 .˛/2 /: Then its square root gets multiplied by .f 0 .m/; f 0 .˛//, so it will lie in Z Z Œ˛. In order to keep an element that is invertible modulo n, we need to assume that f 0 .m/ is coprime to n. This is not a serious restriction as this condition is always satisfied in practice; if it isn’t, we have found a factor of n without applying the number field sieve! (7.2) Example. Take f D X 2 C 16. Then the order Z Œ˛ D Z Œ4i has index 4 in the maximal order O D Z Œi in Q .i /. Example (4.4) shows that 3 C ˛ D 3 C 4i is a square in O, but its square root D 2 C 41 ˛ is not in Z Œ˛. However, the element f 0 .˛/ D 2˛ D 4˛ 8 does lie in Z Œ˛. // In order for an ideal c O to be a square of some other ideal, it is necessary and sufficient that the exponents ordq .b/ are even at all primes q of O. This is an immediate corollary of the classical theorem of unique prime ideal factorization in O. Now the primes q of O coprime to the index ŒO W Z Œ˛ are “the same” as the ideals p of Z Œ˛ coprime to the index. By this we mean that there is a natural
94
PETER STEVENHAGEN
bijection between the sets of such primes given by q ‘ p D q \ Z Œ˛. Moreover, if p and q are corresponding prime ideals, the inclusion map Z Œ˛ O induces an isomorphism of the local rings .7:3/
Z Œ˛p Oq :
Both rings are discrete valuation rings, and the exponent ep W Q .˛/ ! Z is in this case equal to the familiar prime ideal exponent eq for the ring of integers O, which is multiplicative on the set of all non-zero O-ideals, not just the principal ones. For primes q of O dividing the index ŒO W Z Œ˛, the situation is more complicated. There may be more primes q lying above the same prime p D q \ Z Œ˛, and even if p has a single extension q in O, the natural map (7.3) need not be an isomorphism. If either of these happens, p is said to be a singular prime of Z Œ˛. The other primes are the regular primes of Z Œ˛. For a prime p of Z Œ˛, we define the exponent at p as the homomorphism ep W Q .˛/ ! Z by X .7:4/ ep .x/ D f .q=p/ eq .x/; qp
where the sum ranges over the primes q O lying over p, and f .q=p/ is the degree of the residue field extension Z Œ˛=p O=q. This definition provides the extension of the homomorphism ep occurring in Lemma 5.1. For regular primes p, formula (7.4) reduces to ep D eq . We now consider, inside the subgroup of Q .˛/ that is generated by the elements a b˛ 2 Z Œ˛ having gcd.a; b/ D 1, the group V of those elements that have even exponents at the primes p of Z Œ˛. We let V1 V be the subgroup of elements x 2 V that have even exponents at all primes q of O, i.e., the elements x 2 V for which x O is the square of a O-ideal. We have an injective homomorphism M V =V1 Z =2Z qjŒOWZ Œ˛
x ’ .eq .x/ mod 2/q ; so V =V1 is an F2 -vector space of dimension bounded by the number of primes q of O dividing the index ŒO W Z Œ˛. In view of (7.1), the number of rational primes dividing the index is no more than 21 log j.f /j. For each of these primes there are at most d D deg.f / primes q in O that divide it, so we find .7:5/
dimF2 .V =V1 / 12 d log .f /:
This is a quantitative version of obstruction (6.3). Note that we have completely disregarded the fact that the elements of V have even exponents at the singular
THE NUMBER FIELD SIEVE
95
primes of Z Œ˛. It is possible to obtain a slightly better upper bound for the index ŒV W .V \ Q .˛/ 2 / than that in 8.4 by taking this into account.
8. Finiteness results from algebraic number theory Inequality (7.5) is the first step in bounding the successive F2 -dimensions of the quotient spaces in the filtration 2
V V1 V2 V3 D V \ Q .˛/ : Here V1 is the subgroup from the previous section consisting of those x 2 V for which x O is an ideal square, and V2 is the subgroup of those x 2 V for which x O is the square of a principal O-ideal. Thus, the F2 -spaces V1 =V2 and V2 =V3 measure the obstructions (6.4) and (6.5), respectively. We can bound their dimensions using two fundamental finiteness results from algebraic number theory. The first result says that the class group of Q .˛/, which is the group of all fractional O-ideals modulo the subgroup of principal O-ideals, is a finite abelian group. One can derive from [Lenstra 1992, Theorem 6.5] that its order h can be bounded in terms of the degree d and the discriminant .f / of f by .8:1/
h < j.f /j1=2
d
1 C log j.f /jd .d 1/!
1
:
We can map V1 to the class group by sending x 2 V1 to the ideal class of the ideal a satisfying a2 D x O. This map has kernel V2 , so we find the dimension of the F2 -vector space V1 =V2 to be bounded by log h, yielding .8:2/
dimF2 .V1 =V2 /
log h : log 2
As the elements in V2 are squares in Q .˛/ up to multiplication by elements of the unit group O , the order of V2 =V3 does not exceed the order of O =O 2 . By the Dirichlet unit theorem [Stevenhagen 2008], the group O is the product of a finite cyclic group of roots of unity in Q .˛/ with a free abelian group of rank at most d 1. It follows that O =O 2 is finite of order at most 2d , and we find .8:3/
dimF2 .V2 =V3 / d:
Putting the estimates (3.4), (7.5), (8.1), (8.2) and (8.3) together, we arrive after a short computation at the following theorem for the values of n and d that we need.
96
PETER STEVENHAGEN 2
(8.4) Theorem. Let V be as above, and suppose we have n > d 2d > 1. Then the subgroup V3 D V \ Q .˛/ 2 of squares in V satisfies dimF2 .V =V3 / .log n/3=2 : A more careful analysis using the information at the singular primes of Z Œ˛ as in [Lenstra and Lenstra 1993, Theorem 6.7, p. 61] shows that the exponent 3=2 can be replaced by 1.
9. Quadratic character columns The algorithm described so far is only able to produce elements in Z Z Œ˛ for which the second component is in V , but not necessarily in the subgroup V3 D V \ Q .˛/ 2 of squares. In order for an element x 2 V to be V3 , it is necessary and sufficient that all characters W V =V3 ! F2 vanish on x. At most k D dim.V =V3 / characters are needed to span the dual space W D Hom.V =V3 ; F2 /, and an element x 2 V is a square if and only if all these spanning characters assume the value 1 on x. As there is no easy way to produce a spanning set of characters, we will use random quadratic characters instead. An elementary calculation shows that if W is any k-dimensional F2 -vector space, a randomly chosen set of k C e elements has probability at least 1 2 e of generating W . As this probability converges exponentially to 1 in the number e of extra random elements, we can be practically sure to generate W for moderate values of e. We are now faced with the problem of exhibiting suffciently many “quadratic characters” on ZŒ˛. On Z , quadratic characters can be obtained from Legendre symbols x ‘ px , which are easily evaluated. If x 2 Z is not a square, we have, in a sense that is easily made precise, x p D 1 for about half of the primes p. More precisely, they are the odd primes p that p remain prime in the number ring Z Œ x. // (9.1) Example. We have p16 D 1 for all primes p 3 mod 4. Loosely speaking, we can say that an integer x ¤ 0 that satisfies px D 1 for t randomly chosen primes p is a square with probability 1 2 t . We can use an analogue of this idea over Z Œ˛. Every prime q D ker .q; rq / of degree 1 of Z Œ˛ gives rise to a Legendre symbol .q / q W Z Œ˛ Fq f˙1g [ f0g such that for non-square x 2 Z Œ˛, we have xq D 1 about half the time. For y-smooth elements x 2 Z Œ˛, we can avoid the character value 0 by restricting
THE NUMBER FIELD SIEVE
97
to Legendre symbols coming from primes q .q; rq / of degree 1 with q > y. It is a consequence of the Chebotarev density theorem that the Legendre symbols coming from such q are equidistributed over Hom.V =V3 ; f˙1g/. In the rational factor base B1 consisting of primes p y, we incorporated a sign bit to ensure that the integers with even prime exponents at all primes p are actually squares. This sign bit for Z is nothing but the non-trivial chracter on the 1-dimensional F2 -vector space that becomes V =V3 if we replace Z by Z Œ˛. In a similar way, we incorporate in our algebraic factor base B2 , which so far consisted of the primes .pI rp / with p y, a sufficiently large number of F2 -valued characters q W V ! F2 coming from the Legendre symbols of primes q .q; rq / of degree 1 with q > y. The character q is simply the Legendre symbol in additive notation, and the values q .a b˛/ for .a; b/ 2 U are treated exactly like the exponent values ep .a b˛/. In this way, we obtain a probabilistic algorithm for producing y-smooth elements x 2 V that do not only satisfy (6.2) but that are true squares. In this set-up the outcome of the linear algebra step, which reduces a matrix of approximate size y y, consists of subsets S U such that not only we have (6.1) and (6.2), but in addition Y Y 0 2 0 2 f .m/ .a bm/; f .˛/ .a b˛/ .a;b/2S
.a;b/2S
is with very high probability a square .x 2 ; 2 / 2 Z Z Œ˛.
10. Square root extraction The element .x 2 ; 2 / just found yields a solution to our basic congruence (2.1). In order to obtain a factorization of n, we now need the values .x mod n/ and . / in Z =nZ . The gcd of n with their difference is hopefully a nontrivial factor of n. Thus, we need to compute a square root .x; / of our square .x 2 ; 2 / 2 Z Z Œ˛. On the rational side, this is immediate since we know how to extract squares in Z . It is even possible to avoid computing the large number Y x 2 D f 0 .m/2 .a bm/ .a;b/2S
as we have a complete prime factorization of each of the elements a bm occurring in the product, and therefore a prime factorization of the product itself. On the number field side, the situation is more complicated. The prime ideal factorization of Y .a b˛/ .a;b/2S
98
PETER STEVENHAGEN
is easily determined, but this is not immediately useful as prime ideals may not have generators at all and, moreover, we most likely will be unable to compute generators for the unit group O in the large number field Q .˛/. Only for the special number field sieve [Lenstra and Lenstra 1993, p. 21 ff.], which often yields rings of integers O with small units and trivial class group, one may be able to compute a square root of the element 2 2 Z Œ˛ using explicit generators of the primes in Z Œ˛. For the general number field sieve, one can compute a root of the polynomial X 2 2 in Q .˛/ by standard methods, such as successive approximation using Hensel’s lemma [Buhler and Wagon 2008] at an appropriate prime. Theoretically, this can be done without affecting the expected asymptotic running time of the algorithm. In practice, it is feasible as well but rather cumbersome because of the size of the number 2 , which necessitates the handling of very large numbers in the final iterations. Montgomery’s method [1994] (see also [Nguyen 1998]), which uses complex approximations, has a better practical performance but has not yet been carefully analyzed.
11. Running time From the analysis given in [Pomerance 2008], it follows that the conjectural asymptotic expected running time the quadratic sieve takes to factor n is p exp..1 C o.1// log n log log n/ for n tending to infinity. The elliptic curve method has the same running time, which is halfway between exponential and polynomial. For the number field sieve, we can do better if we carefully choose d and f , and optimize the smoothness bound y and the parameter u for the size of the universe U of pairs .a; b/ accordingly. We briefly sketch how to find heuristically the asymptotic optimal values, disregarding all lower order terms that occur along the way. The basic cost of the algorithm, which is computed as in [Pomerance 2008], is u2Co.1/ C y 2Co.1/ as n tends to infinity. The first term represents the sieving part of the algorithm, and equals the length of the sieve times a lower order factor. The second term is the matrix reduction part, which assumes that fast asymptotic methods are applied to a matrix of size at most y y. In order to balance these contributions, we will take log u log y. The numbers a b˛ we consider are y-smooth if the integer .a bm/N .a b˛/ is, and using (4.3) and the size n1=d of m and of the coefficients of f , we may bound this integer by .11:1/
un1=d .d C 1/ud n1=d n2=d udC1 ;
THE NUMBER FIELD SIEVE
99
Here we already take into account that d will be chosen in (11.3) to be of much smaller order than the other factors. The “uu -philosophy” in [Pomerance 2008] shows that a number x n2=d ud C1 is y-smooth with probability r r , where r D log x= log y. In order to maximize this probability, we minimize the quantity log x log x 2 log n 1 Cd C1 .11:2/ rD log y log u log u d log n 1=2 . by taking the degree of f to be d D . 2log u / In order to obtain sufficiently many relations from our pairs .a; b/ 2 U to create a dependent matrix, we need u2 r r y. Taking logarithms and replacing log y by log u, we find log u r log r or, equivalently, r log u= log log u. Comparison with (11.2) for d as above now leads to 2 log n 1=2 log u ; log u log log u
and we take 2=3-rd powers to obtain log u.log log u/ 2=3 2.log n/1=3 . In order to rewrite this, we observe that if we have real quantities s; t satisfying s D t .log t /a for some a 2 R , then, as t tends to infinity, we have t D .1 C o.1//s.log s/ a . Applying this for t D log u and s D 2.log n/1=3 with a D 2=3 we arrive at log y log u 2.log n/1=3 . 31 log log n/2=3 D .8=9/1=3 .log n/1=3 .log log n/2=3 : With this choice of the basic parameters u and y, the asymptotic running time u2Co.1/ C y 2Co.1/ becomes 1=3 2=3 exp .64=9/1=3 C o.1/ log n log log n ; as claimed in (1.1). The optimal asymptotic value of the degree d of f comes out as 3 log n 1=3 2 log n 1=2 ; .11:3/ d log u log log n and we find that the size in (11.1) of the integers we require to be smooth is 2=3 1=3 exp .64=3/1=3 C o.1/ log n log log n : This bound, which we mentioned already in section 2, makes the number field sieve the fastest general purpose factoring algorithm that is currently known. As with the quadratic sieve, there are various practical improvements to the basic number field sieve as we have described it here. The most important bells and whistles are mentioned in [Crandall and Pomerance 2001, Section 6.2.7]. Although they do not significantly change the asymptotic running time of the
100
PETER STEVENHAGEN
algorithm, they greatly enhance its practical performance, and they are instrumental in completing the record factorizations that mark the borderlines of what is currently feasible in factoring.
References [Buhler and Wagon 2008] J. P. Buhler and S. Wagon, “Basic algorithms in number theory”, pp. 25–68 in Surveys in algorithmic number theory, edited by J. P. Buhler and P. Stevenhagen, Math. Sci. Res. Inst. Publ. 44, Cambridge University Press, New York, 2008. [Crandall and Pomerance 2001] R. Crandall and C. Pomerance, Prime numbers: a computational perspective, Springer, New York, 2001. [Lenstra 1992] H. W. Lenstra, Jr., “Algorithms in algebraic number theory”, Bull. Amer. Math. Soc. .N.S./ 26:2 (1992), 211–244. [Lenstra and Lenstra 1993] A. K. Lenstra and J. H. W. Lenstra, The development of the number field sieve, Lecture Notes in Mathematics 1554, Springer, Berlin, 1993. [Montgomery 1994] P. L. Montgomery, “Square roots of products of algebraic numbers”, pp. 567–571 in Mathematics of Computation 1943–1993: a half-century of computational mathematics (Vancouver, 1993), edited by W. Gautschi, Proc. Sympos. Appl. Math. 48, Amer. Math. Soc., Providence, RI, 1994. [Nguyen 1998] P. Nguyen, “A Montgomery-like square root for the number field sieve”, pp. 151–168 in Algorithmic number theory, ANTS-III (Portland, OR, 1998), edited by J. P. Buhler, Lecture Notes in Comput. Sci. 1423, Springer, Berlin, 1998. [Pomerance 2008] C. Pomerance, “Smooth numbers and the quadratic sieve”, pp. 69–81 in Surveys in algorithmic number theory, edited by J. P. Buhler and P. Stevenhagen, Math. Sci. Res. Inst. Publ. 44, Cambridge University Press, New York, 2008. [Stevenhagen 2008] P. Stevenhagen, “The arithmetic of number rings”, pp. 209–266 in Surveys in algorithmic number theory, edited by J. P. Buhler and P. Stevenhagen, Math. Sci. Res. Inst. Publ. 44, Cambridge University Press, New York, 2008. [Stevenhagen and Lenstra 1996] P. Stevenhagen and H. W. Lenstra, Jr., “Chebotar¨ev and his density theorem”, Math. Intelligencer 18:2 (1996), 26–37. P ETER S TEVENHAGEN M ATHEMATISCH I NSTITUUT U NIVERSITEIT L EIDEN P OSTBUS 9512 2300 RA L EIDEN T HE N ETHERLANDS
[email protected]
Algorithmic Number Theory MSRI Publications Volume 44, 2008
Four primality testing algorithms RENE´ SCHOOF
A BSTRACT. In this expository paper we describe four primality tests. The first test is very efficient, but is only capable of proving that a given number is either composite or ‘very probably’ prime. The second test is a deterministic polynomial time algorithm to prove that a given numer is either prime or composite. The third and fourth primality tests are at present most widely used in practice. Both tests are capable of proving that a given number is prime or composite, but neither algorithm is deterministic. The third algorithm exploits the arithmetic of cyclotomic fields. Its running time is almost, but not quite polynomial time. The fourth algorithm exploits elliptic curves. Its running time is difficult to estimate, but it behaves well in practice.
C ONTENTS 1. Introduction 2. A probabilistic test 3. A deterministic polynomial time primality test 4. The cyclotomic primality test 5. The elliptic curve primality test References
101 102 106 111 120 125
1. Introduction In this expository paper we describe four primality tests. In Section 2 we discuss the Miller–Rabin test. This is one of the most efficient probabilistic primality tests. Strictly speaking, the Miller–Rabin test is not a primality test but rather a ‘compositeness test’, since it does not prove the primality of a number. Instead, if n is not prime, the algorithm proves this in all likelihood very quickly. On the other hand, if n happens to be prime, the algorithm merely provides strong evidence for its primality. Under the assumption of the Generalized Riemann Hypothesis one can turn the Miller–Rabin algorithm into a deterministic polynomial time primality test. This idea, due to G. Miller, is also explained. 101
102
RENE´ SCHOOF
In Section 3 we describe the deterministic polynomial time primality test that was proposed by M. Agrawal, N. Kayal and N. Saxena in 2002 [Agrawal et al. 2004]. At the moment of this writing, this new test, or rather a more efficient probabilistic version of it, had not yet been widely implemented. In practice, therefore, for proving the primality of a given integer, one still relies on older tests that are either not provably polynomial time or not deterministic. In the remaining two sections we present the two most widely used such tests. In Section 4 we discuss the cyclotomic primality test. This test is deterministic and is actually capable of proving that a given integer n is either prime or composite. It does not run in polynomial time, but very nearly so. We describe a practical non-deterministic version of the algorithm. Finally in Section 5, we describe the elliptic curve primality test. This algorithm also provides a proof of the primality or compositeness of a given integer n. Its running time is hard to analyze, but in practice the algorithm seems to run in polynomial time. It is not deterministic. The two ‘practical’ tests described in Sections 4 and 5 have been implemented and fine tuned. Using either of them it is now possible to routinely prove the primality of numbers that have several thousands of decimal digits [Mih˘ailescu 1998; Morain 1998].
2. A probabilistic test In this section we present a practical and efficient probabilistic primality test. Given a composite integer n > 1, this algorithm proves with high probability very quickly that n is not prime. On the other hand, if n passes the test, it is merely likely to be prime. The algorithm consists of repeating one simple step, a Miller–Rabin test, several times with different random initializations. The probability that a composite number is not recognized as such by the algorithm, can be made arbitrarily small by repeating the main step a number of times. The algorithm was first proposed by M. Artjuhov [1966/1967]. Later M. Rabin proposed the probabilistic version [1980]. Under assumption of the Generalized Riemann Hypothesis (GRH) one can actually prove that n is prime by applying the test sufficiently often. This leads to G. Miller’s conditional algorithm [1976]. Under assumption of GRH it runs in polynomial time. Our presentation follows the presentation of the algorithms in the excellent book [Crandall and Pomerance 2001]. Here is the key ingredient: T HEOREM 2.1. Let n > 9 be an odd positive composite integer. We write n 1 D 2k m for some exponent k 1 and some odd integer m. Let i
B D fx 2 .Z =nZ / W x m D 1 or x m2 D 1 for some 0 i < kg:
FOUR PRIMALITY TESTING ALGORITHMS
103
Then we have 1 #B : '.n/ 4 Here '.n/ D #.Z =nZ / denotes Euler’s '-function. P ROOF. Let 2l denote the largest power of 2 that has the property that it divides p 1 for every prime p divisor of n. Then the set B is contained in B 0 D fx 2 .Z =nZ / W x m2
l
1
D ˙1g:
Indeed, clearly any x 2 .Z =nZ / satisfying x m D 1 is contained in B 0 . On the i i other hand, if x m2 D 1 for some 0 i < k, we have x m2 1 .mod p/ for every prime p dividing n. It follows that for every p, the exact power of 2 dividing the order of x modulo p, is equal to 2iC1 . In particular, 2iC1 divides p 1 for every prime divisor p of n. Therefore we have l i C 1. So we l 1 l i 1 can write that x m2 D . 1/2 , which is 1 or C1 depending on whether l D i C 1 or l > i C 1. It follows that B B 0 . By the Chinese Remainder Theorem, the number of elements x 2 .Z =nZ / l 1 for which we have x m2 D 1, is equal to the product over p of the number l 1 of solutions to the equation X m2 D 1 modulo p ap . Here p runs over the prime divisors of n and p ap is the exact power of p dividing n. Since each of the groups .Z =p ap Z / is cyclic, the number of solutions modulo p ap is given by gcd..p
1/p ap
1
; m2l
1
/ D gcd.p
1; m/2l
1
:
The last equality follows from the fact that p does not divide m. Therefore we have Y l 1 #fx 2 .Z =nZ / W x m2 D 1g D gcd.p 1; m/2l 1 : pjn l
Similarly, the number of solutions of the equation X m2 D 1 modulo p ap is l 1 equal to gcd.p 1; m/2l , which is twice the number of solutions of X m2 D 1 l 1 modulo p ap . It follows that the number of solutions of the equation X m2 D 1 modulo p ap is also equal to gcd.p 1; m/2l 1 . Therefore we have Y #B 0 D 2 gcd.p 1; m/2l 1 ; pjn
and hence #B 0 '.n/
D
2
Y gcd.p pjn
.p
1; m/2l 1/p ap 1
1
:
RENE´ SCHOOF
104
Suppose now that the propertion #B='.n/ exceeds 14 . We want to derive a contradiction. Since we have B B 0 , the inequality above implies that 1 4
<
2
Y gcd.p pjn
.p
1; m/2l 1/p ap 1
1
:
./
We draw a number of conclusions from this inequality. First we note that gcd.p 1; m/2l 1 divides .p 1/=2 so that the right hand side of ./ is at most 21 t where t is the number of different primes dividing n. It follows that t 2. Suppose that t D 2, so that n has precisely two distinct prime divisors. If one of them, say p, has the property that p 2 divides n so that ap 2, then the right hand side of ./ is at most 21 2 =3 D 1=6. Contradiction. It follows that all exponents ap are equal to 1, so that n D pq for two distinct primes p and q. The inequality () now becomes p 1 q 1 < 2: gcd.p 1; m/2l gcd.q 1; m/2l Since the factors on the left hand side of this inequality are positive integers, they are both equal to 1. This implies that p 1 D gcd.p 1; m/2l and q 1 D gcd.q 1; m/2l . It follows that the exact power of 2 dividing p 1 as well as the exact power of 2 dividing q 1 are equal to 2l and that the odd parts of p 1 and q 1 divide m. Considering the relation pq D 1 C 2k m modulo the odd part of p 1, we see that the odd part of p 1 divides the odd part of q 1. By symmetry, the odd parts of p 1 and q 1 are therefore equal. This implies p 1 D q 1 and contradicts the fact that p 6D q. Therefore we have t D 1 and hence n D p a for some odd prime p and exponent a 2. The inequality () now says that p a 1 < 4, so that p D 3 and a D 2, contradicting the hypothesis that n > 9. This proves the theorem. When a random x 2 .Z =nZ / is checked to be contained in the set B of Theorem 2.1, we say that ‘n passes a Miller–Rabin test’. Checking that x 2 B involves raising x 2 Z =nZ to an exponent that is no more than n. Using the binary expansion of the exponent, this takes no more that O.log n/ multiplications in Z =nZ . Therefore a single exponentiation involves O..log n/1C / elementary operations or bit operations. Here is a constant with the property that the multiplication algorithm in Z =nZ takes no more than O..log n/ / elementary operations. We have D 2 when we use the usual multiplication algorithm, while one can take D 1 C " for any " > 0 by employing fast multiplication techniques. By Theorem 2.1 the probability that a composite number n passes a single Miller–Rabin test, is at most 25%. Therefore, the probability that n passes log n
FOUR PRIMALITY TESTING ALGORITHMS
105
such tests is smaller than 1=n. The probability that a large composite n passes .log n/2 tests is astronomically small: less than n log n . Since for most composite n the probability that n passes a Miller–Rabin test is much smaller than 1=4, one is in practice already convinced of the primality of n, when n successfully passes a handful of Miller–Rabin tests. This is enough for most commercial applications. Under assumption of the Generalized Riemann Hypothesis (GRH) for quadratic Dirichlet characters, the Miller–Rabin test can be transformed into a deterministic polynomial time primality test. This result goes back to [Miller 1976]. T HEOREM 2.2 (GRH). Let n be an odd positive composite integer. Let n 1 D 2k m for some exponent k 1 and some odd integer m. If for all integers x between 1 and 2.log n/2 one has x m 1 .mod n/
or
x2
im
1 .mod n/ for some 0 i < k;
then n is a prime number. P ROOF. We first show that n is squarefree. See also [Lenstra 1979]. Suppose that p is a prime for which p 2 divides n. A special case of the result [Konyagin and Pomerance 1997, (1.45)] on the distribution of smooth numbers implies that for every odd integer r 5 one has p #fa 2 Z W 1 a r and a is product of primes .log r /2 g r: We apply this with r D p 2 . It follows that the subgroup H of .Z =p 2 Z / that is generated by the natural numbers x .log n/2 has order at least p. On the other hand, the hypothesis of the theorem implies that every x 2 H , being a product of numbers a that satisfy an 1 1 .mod p 2 /, satisfies x n 1 1 .mod p 2 /. Since the order of the group .Z =p 2 Z / is p.p 1/ and p does not divide n 1, we see that any x 2 H must satisfy x p 1 1 .mod p 2 /. But this is impossible, because the subgroup of .Z =p 2 Z / that consists of elements having this property, has order p 1. Therefore, if n is composite, it is divisible by two odd distinct primes p and q. Let denote the quadratic character of conductor p. By a result of E. Bach [1990], proved under assumption of the GRH, there exists a natural number x 2.log p/2 < 2.log n/2 for which .x/ 6D 1. Since the condition of the theorem implies that we have gcd.x; n/ D 1, we must have .x/ D 1. Writing p 1 D 2l for some exponent l 1 and some odd integer , we l 1 have x 2 .x/ D 1 .mod p/. This implies that 1 is contained in the subgroup of .Z =p Z / generated by x. Since the 2-parts of the subgroups of .Z =p Z / generated by x m and by x are the same, we have x m 6 1 .mod p/ and hence x m 6 1 .mod n/. Therefore the hypothesis of the theorem implies
RENE´ SCHOOF
106 i
that x 2 m 1 .mod n/ for some 0 i < k. Since for this value of i we also i have x 2 m 1 .mod p/, necessarily the equality i D l 1 holds. It follows l 1 that we have x 2 m 1 .mod q/, so that the order of x m .mod q/ is equal 0 to 2l . Writing q 1 D 2l 0 for some exponent l 0 1 and some odd integer 0 , we have therefore l l 0 . Repeating the argument, but switching the roles of p and q, we conclude that l D l 0 . Let 0 denote the quadratic character of conductor q. A second application of Bach’s theorem, this time to the non-trivial character 0 , provides us with a natural number y 2.log n/2 for which 0 .y/ 6D 1 and hence, say, .y/ D 1 while 0 .y/ D 1. The arguments given above, but this time applied to y, show that we cannot have y m 1 .mod n/, so that necessarily i y 2 m 1 .mod n/ for some 0 i < k. Moreover, the exponent i is equal l0 1 to l 1 D l 0 1. It follows that y 2 m 1 .mod q/. This implies that 0 the element y m 2 .Z =q Z / has order 2l . Since the subgroups of .Z =q Z / 0 0 0 generated by y m and y are equal, the order of y 2 .Z =q Z / is also 2l . This 0 l 1 0 contradicts the fact that 1 D 0 .y/ y 2 .mod q/. We conclude that n is prime and the result follows. It is clear how to apply Theorem 2.2 and obtain a test that proves that n is prime under condition of GRH: given an odd integer n > 1, we simply test the condition of Theorem 2.2 for all a 2 Z satisfying 1 < a < 2.log n/2 . If n passes all these tests and GRH holds, then n is prime. Each test involves an exponentiation in the ring Z =nZ . Since the exponent is less than n, this can be done using only O..log n/1C / elementary operations. Therefore this is a polynomial time primality test. Testing n takes O..log n/3C / elementary operations. As before, we have D 2 when we use the usual multiplication algorithm, while we can take D 1 C " for any " > 0 by employing fast multiplication techniques.
3. A deterministic polynomial time primality test In the summer of 2002 the three Indian computer scientists M. Agrawal, N. Kayal and N. Saxena presented a deterministic polynomial time primality test. We describe and analyze this extraordinary result in this section. For any prime number r we let ˚r .X / D X r
1
CCX C1
denote the r -th cyclotomic polynomial. Let r be a zero of ˚r .X / and let Z Œr denote the ring generated by r over Z . For any n 2 Z we write Z Œr =.n/ for the residue ring Z Œr modulo the ideal .n/ generated by n. For n 6D 0, this is a finite ring.
FOUR PRIMALITY TESTING ALGORITHMS
107
T HEOREM 3.1. Let n be an odd positive integer and let r be a prime number. Suppose that (i) n is not divisible by any of the primes r ; (ii) the order of n .mod r / is at least .log n=log 2/2 ; (iii) for every 0 j < r we have .r C j /n D rn C j in Z Œr =.n/. Then n is a prime power. P ROOF. It follows from condition (ii) that we have n 6 1 .mod r /. Therefore there exists a prime divisor p of n that is not congruent to 1 .mod r /. Let A denote the Fp -algebra Z Œr =.p/. It is a quotient of the ring Z Œr =.n/. For k 2 Z coprime to r we let k denote the ring automorphism of A determined by k .r / D rk . The map .Z =r Z / ‘ given by k ‘ k is a well defined isomorphism. We single out two special elements of . One is the Frobenius automorphism p and the other is n . Let denote the subgroup of that is generated by p and n . Next we consider the subgroup G of elements of the multiplicative group A that are annihilated by the endomorphism n n 2 Z Œ. In other words, we put G D fa 2 A W n .a/ D an g: Pick a maximal ideal m of A and put k D A=m. Then k is a finite extension of Fp , generated by a primitive r -th root of unity. Let H k be the image of G under the natural map W A k. The group H is cyclic. Its order is denoted by s. We have the following commutative diagram. G
A
# H
#
k
Since is commutative, it acts on G. Since n and p act on G by raising to the power n and p respectively, every m 2 acts by raising g 2 G to a certain power em that is prime to #G. The powers em are well determined modulo the exponent exp.G/ of G. Therefore the map .Z =exp.G/Z / , given by m ‘ em , is a well defined group homomorphism. Since H is a cyclic quotient of G, its order s divides the exponent of G and the map m ‘ em induces a homomorphism .Z =s Z / : If m p i nj .mod r /, then it maps m 2 to em p i nj .mod s/. It is instructive to see what all this boils down to when n is prime. Then we have n D p and n is equal to the Frobenius automorphism p . The group G is all of A so that H is equal to k . Writing f for the order of p modulo r , the
RENE´ SCHOOF
108
group D hp i has order f while the groups H D k and its automorphism group Aut.H / are much larger. Indeed, H has order s D p f 1 D n# 1 and Aut.H / Š .Z =s Z / is of comparable size Under the conditions of the theorem, but without assuming that n is prime, something similar can be shown to be true. p
CLAIM .
s > n[
].
#
Using this inequality, we complete the proof of the theorem. Consider the homomorphism .Z =s Z / constructed above. We first apply the box principle in the small group and then obtain a relation in Z from a relation in .Z =s Z / using the fact that the latter group is very large. p j Let q D n=p. We consider the products pi q 2 for 0 i; j # . p 2 0 0 Since we have 1 C # > # , there are two pairs .i; j / 6D .i ; j / for j
0
j0
which pi q and pi q are the same element in . It follows that their images in the group .Z =s Z / are the same as well. Since q is mapped to q .mod s/, 0 0 this means that pii q j p i q j .mod s/. The integer p i q j does not exceed h nmax.i;j / 0
pi qj
0
p
#
0
0
< s. The same holds for p i q j . We conclude that p i q j D n in Z ! Since .i; j / 6D .i 0 ; j 0 / it follows that n is a power of p.
P ROOF OF THE CLAIM . We first estimate s D #H in terms of #G. Then we show that G is large. The first bound we show is s #G 1=ŒW :
./
Let C denote a set of coset representatives of morphism Y G k
in and consider the homo-
i2C
given by mapping a 2 G to the vector .i .a/ .mod m//i2C . This map is injective. Indeed, if a 2 G has the property that i .a/ D 1 for some i , then we also have i n .a/ D i .an / D i .a/n D 1 and similarly ip .a/ D 1. In other words, we have .a/ D 1 for all elements in the coset of containing i . Therefore, if a 2 G has the property that i .a/ D 1 for all i 2 C , then automatically also i .a/ D 1 for all i 2 .Z =r Z / . It follows that i .a 1/ D 0 for all i 2 .Z =r Z / . Writing the element a 1 as f .r / for some polynomial f .X / 2 Fp ŒX , this implies that f .ri / D 0 for all i 2 .Z =r Z / . It follows that the cyclotomic polynomial ˚r .X / divides f .X / in Fp ŒX and hence that a 1 D 0, as required.
FOUR PRIMALITY TESTING ALGORITHMS
109
Since for every i 2 C , the image of the map G k given by a ‘ i .a/ .mod m/ is equal to H , the injectivity of the homomorphism implies that #G s ŒW as required. The second estimate is #G 2r
1
:
./ .X / D .X r
Since p 6 1 .mod r /, the irreducible factors of ˚r 1/=.X 1/ in the ring Fp ŒX have degree at least 2 and hence cannot divide any polynomial of degree 1. Therefore the elements r C j for 0 j < r 1 are not contained in any maximal ideal of the ring A. It follows that they are units of A. By condition (iii), for each subset J f0; 1; : : : ; r 2g the element Y .r C j / j 2J
is contained in G. All these elements are distinct. Indeed, since the degree of the cyclotomic polynomial ˚r is r 1, the only two elements that could be equal to one another are the ones corresponding to the extreme cases J D ∅ and J D f0; 1; : : : ; r 2g. Q This can only happen when jr D02 .X C j / 1 is divisible by ˚r .X / in the ring Fp ŒX . Since both polynomials have the same degree, we then necessarily have Qr 2 j D0 .X Cj / 1 D ˚r .X /. Inspection of the constant terms shows that p D 2. But this is impossible, because n is odd. Since there are 2r 1 subsets J f0; 1; : : : ; r 2g, we conclude that #G r 2 1 , as required. Combining the inequalities () and () we find that s #G 1=ŒW
2.r
1/=ŒW
D 2#
> n
p #
p
nŒ
#
:
Here we used the inequality # > .log n=log 2/2 . It follows from the fact that the order of n 2 is larger than .log n=log 2/2 . Indeed, this order is equal to the order of n modulo r , which by condition (ii) is larger than .log n=log 2/2 . This proves the claim. Theorem 3.1 leads to the following primality test. A LGORITHM 3.2. Let n > 1 be an odd integer. (i) First check that n is not a proper power of an integer. (ii) By successively trying r D 2; 3; : : : , determine the smallest prime r not dividing n nor any of the numbers ni 1 for 0 i .log n=log 2/2 . (iii) For 0 j < r 1 check that .r C j /n D rn C j in the ring Z Œr =.n/. If the number n does not pass the tests, it is composite. If it passes them, it is a prime.
RENE´ SCHOOF
110
P ROOF OF CORRECTNESS . If n is prime, it passes the tests by Fermat’s little theorem. Conversely, suppose that n passes the tests. We check the conditions of Theorem 3.1. By the definition of r , the number n has no prime divisors r . Since r does not divide any of the ni 1 for 1 i .log n=log 2/2 , the order of n modulo r exceeds .log n=log 2/2 . This shows that the second condition of Theorem 3.1 is satisfied. Since test (iii) has been passed successfully, the third condition is satisfied. We deduce that n is a prime power. Since n passed the first test, it is therefore prime. RUNNING TIME ANALYSIS . The first test is performed by checking that n1=m is not an integer, for all integers m between 2 and log n=log 2. This can be done in time O..log n/4 / by computing sufficiently accurate approximations to the real number n1=m . The second test does not take more than r times O..log n/2 / multiplications with modulus r . This takes at most O.r .log r log n/2 / bit operations. The third test takes r times O.log n/ multiplications in the ring Z Œr =.n/. The latter ring is isomorphic to Z ŒX =.˚r .X /; n/. If the multiplication algorithm that we use to multiply two elements of bit size t takes no more than O.t / elementary operations, then this adds up to O..r log n/1C / elementary operations. Since 1 and since r exceeds the order of n mod r , we have r > .log n=log 2/2 . Therefore the third test is the dominating part of the algorithm. We estimate how small we can take r . By the definition of r , the product Q n i .ni 1/ is divisible by all primes l < r . Here the product runs over values of i .log n=log 2/2 . So X X log l log n C log n i D O..log n/5 /: l
1i.
log n 2 / log 2
A weak and easily provable form of P the prime number theorem says that there exists a constant c > 0 such that l
n # used above implies that # D r 1 needs to be at least .log n=log 2/2 . This we know to be the case because the order of n 2 , which is equal to the order of n 2 .Z =r Z / , exceeds .log n=log 2/2 . The argument involving the prime number theorem given above implies then that we cannot expect to be able to prove that the order of magnitude of the prime r
FOUR PRIMALITY TESTING ALGORITHMS
111
is smaller than O..log n/5 /. Therefore this algorithm cannot be expected to be proved to run faster than O..log n/6.1C/ /. On the other hand, in practice one easily finds a suitable prime of the smallest possible size O..log n/2 . Therefore the practical running time of the algorithm is O..log n/3.1C/ /. R EMARK 2. One may replace the ring Z Œr =.n/ Š .Z =nZ /ŒX =.˚r .X //
by any Galois extension of Z =nZ of the form .Z =nZ /ŒX =.f .X // that admits an automorphism with the properties that .X / D X n
and
has order at least .log n=log 2/2 .
This was pointed out by Hendrik Lenstra shortly after the algorithm described above came out. The running time of the resulting modified algorithm is then O..d log n/1C / where d is the degree of the polynomial f .X /. Since the order of is at most d, one has d > .log n=log 2/2 and one cannot obtain an algorithm that runs faster than O..log n/3.1C/ /. Since then Lenstra and Pomerance [ 2008] showed that for every " > 0 one can construct suitable rings with d D O..log n/2C" /. This leads to a primality test that runs in time O..log n/.3C"/.1C/ /. This is essentially the same as the practical running time mentioned above.
4. The cyclotomic primality test In this section we describe the cyclotomic primality test. This algorithm was proposed in 1981 by L. Adleman, C. Pomerance and R. Rumely [Adleman et al. 1983]. It is one of the most powerful practical tests available today (see [Mih˘ailescu 1998]). Our exposition follows H. Lenstra’s lecture [1981]; see also [Cohen 1993, 9.1; Washington 1997, 16.1]. The actual computations involve Jacobi sums, but the basic idea of the algorithm is best explained in terms of Gaussian sums. See [Washington 1997; Lang 1978] for a more systematic discussion of the basic properties of Gaussian sums and Jacobi sums. For any positive integer r , we denote the subgroup of r -th roots of unity of Q by r . D EFINITION . Let q be a prime and let r be a positive integer prime to q. Let W .Z =q Z / r be a character and let q be a primitive q-th root of unity.
RENE´ SCHOOF
112
Then we define the Gaussian sum ./ by X ./ D
qx .x/:
x2.Z =q Z /
The Gaussian sum ./ is an algebraic integer, contained in the cyclotomic field Q .r ; q /. We have the diagram of fields
G
Q .r ; q / Q .r /
Q .q /
Q
The Galois group of Q .r ; q / over Q is isomorphic to G. Here we have D fi W i 2 .Z =r Z / g, where i 2 is the automorphism that acts trivially on q-th roots of unity, while its action of r -th roots of unity is given by i .r / D ri . The map .Z =r Z / given by i ‘ i is an isomorphism of groups. Similarly, we have G D fj W j 2 .Z =q Z / g where j 2 is the automorphism given by j j .r / D r and j .q / D q . The map .Z =q Z / G given by j ‘ j is an isomorphism of groups. We write the actions of the group rings Z Œ and Z ŒG on the multiplicative group Q .r ; q / using exponential notation. One easily checks the following relations. ./i D .i / for i 2 .Z =r Z / . and ./j D .j /
1
./
for j 2 .Z =q Z / .
We write ./ for the complex conjugate of ./. For 6D 1 one has ././ D q; showing that ./ is an algebraic integer that is only divisible by primes that lie over q. For our purposes the key property of the Gaussian sums is the following. P ROPOSITION 4.1. Let q be a prime, let r be a positive integer prime to q. Let W .Z =q Z / r be a character and let ./ be the corresponding Gaussian sum. Then, for every prime number p not dividing qr we have ./p
p
D p .p/ in the ring Z Œq ; r =.p/.
FOUR PRIMALITY TESTING ALGORITHMS
113
P px p P ROOF. We have ./p x2.Z =q Z / q .x/ modulo the ideal p Z Œq ; r . Multiplying by p .p/ and replacing the variable x by p 1 x, we get that X p .p/ ./p p .p/ qx p .p 1 x/ D .p / ./p .mod p/ x2.Z =q Z /
as required.
The cyclotomic primality test proceeds by checking the congruence of Proposition 4.1 for suitable characters W .Z =q Z / r . The next theorem is the key ingredient for the cyclotomic primality test. T HEOREM 4.2. Let n be a natural number. Let q be a prime not dividing n, let r be a power of a prime number l not dividing n and let W .Z =q Z / r be a character. If – for every prime p dividing n there exists p in the ring Z l of l-adic integers such that p l 1 D n.l 1/p in Z l ; – the Gaussian sum ./ satisfies ./n
n
2 hr i in the ring Z Œq ; r =.n/;
then we have
.p/ D .n/p
for every prime divisor p of n. Note that p 2 Z l in the first condition is well defined because both nl 1 and p l 1 are congruent to 1 .mod l/. In addition, p is unique. When l is odd, the first condition is equivalent to the fraction .p l 1 1/=.nl 1 1/ being l-integral. In the second condition, we denote by hr i the cyclic subgroup of .Z Œr =.n// of order r generated by r . Note that the group hr i is not necessarily equal to the group of r -th roots of unity in the ring Z Œr =.n/. P ROOF OF THE THEOREM . We may assume that is a non-trivial character. By the second condition we have ./n
1n
D ./
for some 2 hr i Z Œq ; r =.n/:
Note that the operator n 1 n 2 Z Œ has the property that n n D . Therefore, for any integer L 0, applying it .l 1/L times leads to the relation 1
./.n
1 n/.l
1/L
D .l
1/L
./;
in the ring Z Œq ; r =.n/:
On the other hand, Proposition 4.1 implies that for any prime divisor p of n we 1 have ./p p D .p/ 1 ./ and hence ./.p
1 p/l
1
D .p/1 l ./
in the ring Z Œq ; r =.p/.
RENE´ SCHOOF
114
Let l M be the order of the l-part of the finite multiplicative group .Z Œq ;r =.n// and A the group .Z Œq ; r =.n// modulo l M -th powers. Let L be an integer between 0 and l M for which L p .mod l M /. Then we have p l 1 n.l 1/L n.l 1/p .mod l M / and hence .n 1 n/.l 1/L D p 1 p in the ring .Z = l M Z /Œ. It follows that the left hand sides of the two formulas above are equal in the group A. Then the same is true for the right hand sides. Since ./ is invertible modulo p, this means .l
1/L
D .p/1
l
in the group A.
Since l 1 is coprime to the order of r and since the natural map hr i Œ A is injective, this implies .p/
1
D L D p ;
in the group hr i .Z Œq ; r =.n// . When we multiply the formulas of the first condition for the various prime divisors p of n together, we see that for every positive divisor d of n there exists d 2 Z l for which d l 1 D n.l 1/d in Z l . We have, of course, n D 1. From the relation d d 0 D d C d 0 , we deduce that d D .d / 1 for every divisor d of n. In particular, we have D n D .n/ 1 and hence .p/ D .n/p ; for every prime divisor p of n, as required.
A LGORITHM . The following algorithm is based on Theorem 4.2. Suppose we want to prove that a natural number n is prime. First determine an integer R > 0 that has the property that Y s D q q 1jR q prime
p exceeds n. At the end of this section we recall that there is a constant c > 0 such that for every natural number n>16 there exists an integer R < .log n/c log log log n that has this property. Taking R equal to the product of the first few small prime powers is a good choice. For all primes q dividing s and for each prime power r that divides q 1 exactly, we make sure that gcd.n; qr / D 1 and then check the two conditions of Theorem 4.2 for one character of conductor q and order r . When n passes all these tests, we check for k D 1; : : : ; R 1 whether the smallest positive residue of nk modulo s divides n. If that never happens, then n is prime. P ROOF OF CORRECTNESS . We first note that when n is prime, Proposition 4.1 p implies that it passes all tests. Conversely, suppose that p n is a prime divisor of n. For every prime l dividing R, let p be the l-adic number that
FOUR PRIMALITY TESTING ALGORITHMS
occurs in the first condition of Theorem 4.2. Let L 2 f0; 1; : : : ; R unique integer for which we have
115
1g be the
L p .mod r /; for the power r of l that exactly divides R. Theorem 4.2 implies therefore that .p/ D .n/L for the set of characters of conductor q and order r forQ which the conditions of Theorem 4.2 have been checked. Since we have s D q 1jR q, the exponent of the group .Z =s Z / divides R. Therefore our set of characters generates the group of all characters of .Z =s Z / . It follows that p nL .mod s/: p Since we have 0 < p n < s, this means that p must actually be equal to the smallest positive residue of nk modulo s for some k D 1; 2; : : : ; R 1. Since we checked that none of these numbers divide n, we obtain a contradiction. It follows that p cannot exist, so that n is necessarily prime. In practice, checking the first condition of Theorem 4.2 is easy. When l 6D 2, the number p 2 Z l of the first condiction exists if and only if for any prime divisor p of n, the rational number .p l 1 1/=.nl 1 1/ is l-integral. Since we have p l 1 1 .mod l/, this is automatic when we have nl 1 6 1 .mod l 2 /. Given n, this usually holds true for various prime numbers l. Another useful criterion is the following. It can be checked for free when one checks the second condition of Theorem 4.2. P ROPOSITION 4.3. Let n > 1 be an integer and let l be a prime number not dividing n. Then there exists for every prime divisor p of n an exponent p 2 Z l for which p l 1 D n.l 1/p in Z l ; whenever there exists a prime q not dividing n for which the following holds. (i) (l 6D 2) for some power r > 1 of l and some character W .Z =q Z / r of order r the number ./n n is a generator of the cyclic subgroup hr i of .Z Œq ; r =.n// . (ii) (l D 2 and n 1 .mod 4/) we have ./n n D 1 for the quadratic character modulo q. (iii) (l D 2 and n 3 .mod 4/) and for some character W .Z =q Z / r of 2-power order r 4, the number ./n n is a generator of the cyclic subgroup hr i of .Z Œq ; r =.n// . Moreover, the Gaussian sum associated to the quadratic character r=2 satisfies .r=2 /n n D 1 in the ring Z Œq =.n/. P ROOF. Let p be a prime divisor of n and let r be a power of l. As in the proof of Theorem 4.2, let l M denote the order of the l-part of the unit group
RENE´ SCHOOF
116
.Z Œq ; r =.p// and let A be the group .Z Œq ; r =.p// modulo l M -th powers. The latter is a module over the l-adic group ring Z l Œ. The multiplicative subgroup fm1 m 2 Z l Œ W m 2 Z l g is naturally isomorphic to Z l . Therefore, when l 6D 2, its subgroup G of .l 1/-th powers is isomorphic to the additive group Z l . When l D 2, this is not true, but in that case the subgroup G 2 of squares is isomorphic to Z 2 . By Proposition 4.1 for any prime q and character W .Z =q Z / r of order r we have ./p If ./n
n
1p
D .p/
1
./
in the group A.
is a generator of the group hr i .Z Œq ; r =.n// , then we have ./n
1n
D ./
in the group A.
for some primitive r -th root of unity 2 hr i .Z Œq ; r =.n// . Now we prove (i). Since is a primitive root, the operator .n 1 n/l 1 2 Z l Œ cannot be a ‘proper’ l-adic power of .p 1 p/l 1 in the sense that there cannot exist 2 l Z l for which .n 1 n/l 1 D .p 1 p/.l 1/ . Since both operators are contained in the pro-cyclic group G Š Z l , the converse must therefore be true: we have .p 1 p/l 1 D .n 1 n/.l 1/p and hence p l 1 D n.l 1/p for some p 2 Z l . To prove (ii), we observe that the values of are either 1 or 1. Therefore we have ./n D ./. Since we have ./2 D . 1/././ D . 1/q, the condition ./n n D 1 means precisely that .. 1/q/.n
1/=2
1 .mod n/:
This shows that the 2-parts of the order of . 1/q .mod p/ and of n 1 are equal. This means that n 1 divides p 1 in the ring of 2-adic integers Z 2 . Since n 1 .mod 4/, this is equivalent to the statement that p D np for some p 2 Z 2 . To prove (iii), we note that for l D 2, the group G that we considered above is not isomorphic to Z 2 , but the subgroup G 2 is. Therefore the arguments of the proof of part (i) only show that p 2 D n2p and hence p D ˙np for some p 2 Z 2 . We show that we have the plus sign. From the relation p 2 D n2p we deduce that 1 .p/2 D 2p . Raising this relation to the power r=4, we find p D r=2 .p/ D r p =2 D . 1/p : q Here we used the usual Legendre symbol to denote the quadratic character r=2 . Since q 1 .mod 4/, we have . 1/ D 1. Therefore the second condition
FOUR PRIMALITY TESTING ALGORITHMS
.r=2 /n n 1 .mod n/ says precisely that we have q .n Since .n 1/=2 is odd, it follows that ! q q .n 1/=2 1 D D : p p p
117 1/=2
1 .mod n/.
Since hasorder at least 4, we have q 1 .mod 4/ and hence, by quadratic p q 1 reciprocity, q D p . The two formulas above imply that p D . 1/p . This means precisely that p np .mod 4/, so that we must have the plus sign, as required. If the number n that is being tested for primality is actually prime, then in each instance the conditions of Proposition 4.3 are satisfied for a prime q that has the property that n is not an l-th power modulo q. Given n, one encounters in practice for every prime l very quickly such a prime q, so that the first condition of Theorem 4.2 can be verified. In the unlikely event that for some prime l none of the primes q has this property, one simply tests the second condition of Theorem 4.2 for some more primes q 1 .mod l/. Testing the second condition of Theorem 4.2 is a straightforward computation in the finite ring Z Œq ; r =.n/. In practice it is important to reduce this to a computation in the much smaller subring Z Œr =.n/. This is done by using Jacobi sums. D EFINITION . Let q be a prime and let ; 0 W .Z =q Z / r be two characters. Then we define the Jacobi sum j .; 0 / by X j .; 0 / D .x/0 .1 x/: x2Z =q Z
Here we extend and 0 to Z =q Z by putting .0/ D 0 .0/ D 0. The Jacobi sum is an algebraic integer, contained in the cyclotomic field Q .r /. If the characters ; 0 W .Z =q Z / r satisfy 0 6D 1, we have j .; 0 / D
./.0 / : .0 /
In particular, if i > 0 is prime to r and less than the order of , we have i i
./
iY 1 ./i D D j .; k /: i . / kD1
The subgroup of the l-power order roots of unity in Q is a Z Œ-module. Let I Z Œ be its annihilator. This ideal is generated by the elements of the
RENE´ SCHOOF
118
form i i with i 2 Z coprime to l. Since we have ./j j 6 0 .mod q/, we have 1 D ./.j
1/x
D ./x.j
1/
1
2 r for all
for every