Algorithm Search⌘K

Proofs: Special number field sieve

These are the human proofs of Special number field sieve, at full length.

Correctness of the special number field sieve full

This proof shows: correctness, termination.

Let N≥3N \ge 3 be an odd integer, let f=xd+cd−1xd−1+⋯+c0∈Z[x]f = x^d + c_{d-1}x^{d-1} + \dots + c_0 \in \mathbb{Z}[x] be monic of odd prime degree dd, and let mm be an integer with f(m)≡0(modN)f(m) \equiv 0 \pmod N. Write Z[α]=Z[x]/(f)\mathbb{Z}[\alpha] = \mathbb{Z}[x]/(f), and represent each element by its coefficients on 1,α,…,αd−11, \alpha, \dots, \alpha^{d-1}. After line L1 the implementation replaces mm by m mod Nm \bmod N, which keeps f(m)≡0(modN)f(m) \equiv 0 \pmod N; we use the reduced mm from then on. We prove that the output is always 11 or a proper factor of NN, that every subset that reaches line L9 gives a congruence of squares, and that the algorithm stops. We do not prove that a proper factor is found; that part is a heuristic. We also prove the two facts about special polynomials that the page uses, and at the end that the driver factorize returns the prime factorization.

Lemma 1 (the special polynomial). Let r≥2r \ge 2, e≥1e \ge 1, s≠0s \ne 0 and d≥2d \ge 2 be integers, l=⌈e/d⌉l = \lceil e/d \rceil, t=dl−et = dl - e, f=xd+srtf = x^d + sr^t and m=rlm = r^l. Then 0≤t<d0 \le t < d and f(m)=rt(re+s)f(m) = r^t(r^e + s). So f(m)≡0(modC)f(m) \equiv 0 \pmod C for every divisor CC of re+sr^e + s.

Proof. By the definition of the ceiling, e/d≤l<e/d+1e/d \le l < e/d + 1, so e≤dl<e+de \le dl < e + d and 0≤t<d0 \le t < d. Then f(m)=rdl+srt=re+t+srt=rt(re+s)f(m) = r^{dl} + sr^t = r^{e + t} + sr^t = r^t(r^e + s). If C∣re+sC \mid r^e + s, then C∣f(m)C \mid f(m). □\square

Lemma 2 (the homomorphism). There is a ring homomorphism φ ⁣:Z[α]→Z/NZ\varphi\colon \mathbb{Z}[\alpha] \to \mathbb{Z}/N\mathbb{Z} with φ(α)=m mod N\varphi(\alpha) = m \bmod N, and φ(u)=u(m) mod N\varphi(u) = u(m) \bmod N for every uu of degree below dd.

Proof. Evaluation at mm followed by reduction modulo NN is a ring homomorphism ψ ⁣:Z[x]→Z/NZ\psi\colon \mathbb{Z}[x] \to \mathbb{Z}/N\mathbb{Z}. It maps ff to f(m) mod N=0f(m) \bmod N = 0, so it maps the ideal (f)(f) to 00, and it factors through Z[x]/(f)\mathbb{Z}[x]/(f). The value on the class of uu is ψ(u)=u(m) mod N\psi(u) = u(m) \bmod N. □\square

Only f(m)≡0(modN)f(m) \equiv 0 \pmod N is used; f(m)=Nf(m) = N is not needed, and neither is the irreducibility of ff. This is the one place where the special sieve differs from the general one, whose polynomial satisfies f(m)=Nf(m) = N.

Lemma 3 (integer roots and algebraic factors). integer_roots(f) returns exactly the integer roots of ff when c0≠0c_0 \ne 0. For each integer root zz, m−zm - z divides f(m)f(m), so a value 1<gcd⁡(m−z,N)<N1 < \gcd(m - z, N) < N returned by line L1 is a proper factor of NN. If f=xd+cf = x^d + c with dd an odd prime and c≠0c \ne 0, then ff is reducible over Q\mathbb{Q} if and only if it has an integer root.

Proof. If z∈Zz \in \mathbb{Z} and f(z)=0f(z) = 0, then c0=−z(zd−1+cd−1zd−2+⋯+c1)c_0 = -z(z^{d-1} + c_{d-1}z^{d-2} + \dots + c_1), so z∣c0z \mid c_0, and z≠0z \ne 0 since c0≠0c_0 \ne 0. Every divisor u>0u > 0 of ∣c0∣|c_0| is tt or ∣c0∣/t|c_0|/t for some t≤∣c0∣t \le \sqrt{|c_0|} with t∣∣c0∣t \mid |c_0|, and the function tests uu and −u-u for each of them by exact evaluation. So it returns every integer root and nothing else. For an integer root zz, f=(x−z)hf = (x - z)h with h∈Z[x]h \in \mathbb{Z}[x] monic, by division by the monic x−zx - z; so f(m)=(m−z)h(m)f(m) = (m - z)h(m) and m−z∣f(m)m - z \mid f(m). Any gcd with NN divides NN, and line L1 returns it only if it is strictly between 11 and NN.

For the last claim, an integer root gives the factor x−zx - z, and d≥3d \ge 3, so ff is reducible. Conversely, let f=xd−af = x^d - a with a=−ca = -c be reducible over Q\mathbb{Q}, and let θ\theta be a complex root, so θd=a\theta^d = a. Let gg be the minimal polynomial of θ\theta over Q\mathbb{Q}, of degree kk with 1≤k<d1 \le k < d. The roots of ff are θζj\theta\zeta^j with ζ=e2πi/d\zeta = e^{2\pi i/d}, and gg divides ff, so the roots of gg are kk of them. Their product is b=θkζjb = \theta^k\zeta^j for some jj, and it is (−1)k(-1)^k times the constant term of gg, so bb is rational, and b≠0b \ne 0 since a≠0a \ne 0. As ζd=1\zeta^d = 1, bd=θkd=akb^d = \theta^{kd} = a^k. Since dd is prime and 1≤k<d1 \le k < d, there are integers u,vu, v with uk+vd=1uk + vd = 1. Then w=buavw = b^u a^v is rational and wd=(bd)uavd=auk+vd=aw^d = (b^d)^u a^{vd} = a^{uk + vd} = a. So aa is the dd-th power of a rational number, and since aa is an integer, of an integer zz (a rational root of a monic integer polynomial is an integer, by the argument of the first paragraph). Then f(z)=zd−a=0f(z) = z^d - a = 0. □\square

So for the special polynomials of the driver, the check of line L1 removes every reducible case; the example 233−12^{33} - 1 with f=x3−1f = x^3 - 1, z=1z = 1 and m=211m = 2^{11} gives gcd⁡(2047,N)=2047\gcd(2047, N) = 2047.

Lemma 4 (exact products). The functions mulmod with no modulus and product_in_order compute products in Z[α]\mathbb{Z}[\alpha] exactly.

Proof. mulmod forms the product ww of the two polynomials in Z[x]\mathbb{Z}[x], of degree at most 2d−22d - 2. For k=2d−2k = 2d - 2 down to dd it subtracts wkxk−dfw_k x^{k-d} f, which does not change the class modulo ff. Since ff is monic, this sets the coefficient of xkx^k to 00 and changes only lower coefficients. After the loop the degree is below dd, so the result is the representative of uvuv. product_in_order multiplies pairs level by level; multiplication in a commutative ring is associative and commutative, so the result is the product of all the elements. □\square

Lemma 5 (the rational square). Let TT be a set of relations whose vectors of line L5 add to 00 modulo 22, and let epe_p be the sum over TT of the exponents of pp in a−bma - bm. Then ∏T(a−bm)=v2\prod_T (a - bm) = v^2 with v=∏p∈Ppep/2v = \prod_{p \in P} p^{e_p/2}, and the value xx of line L7 satisfies x≡f′(m)v(modN)x \equiv f'(m) v \pmod N.

Proof. Each relation has ∣a−bm∣=∏p∈Ppep(a,b)|a - bm| = \prod_{p \in P} p^{e_p(a, b)}, which line L4 checks by trial division. The rational prefilter of the implementation only skips points; it never adds a point that fails this check. The product over TT of the signs is (−1)s(-1)^s, where ss is the number of negative a−bma - bm; bit 00 of the vectors is that sign, so ss is even and the product is positive. The bits for the rational primes are the exponents modulo 22, so every epe_p is even. Hence ∏T(a−bm)=∏ppep=v2\prod_T (a - bm) = \prod_p p^{e_p} = v^2. Line L7 computes f′(m)f'(m) times ∏pep/2\prod p^{e_p/2} modulo NN. □\square

Lemma 6 (the algebraic square). If line L8 returns δ\delta, then δ2=γ\delta^2 = \gamma in Z[α]\mathbb{Z}[\alpha], where γ=f′(α)2∏T(a−bα)\gamma = f'(\alpha)^2 \prod_T (a - b\alpha).

Proof. The implementation computes γ\gamma exactly by Lemma 4, and algebraic_sqrt returns a candidate only after the test mulmod(delta, delta, f) == gamma, an exact product by Lemma 4. Otherwise it returns None, and line L8 goes on to the next subset. □\square

Lemma 7 (the congruence of squares). If a subset TT reaches line L9, then x2≡y2(modN)x^2 \equiv y^2 \pmod N.

Proof. By Lemma 5, x2≡f′(m)2∏T(a−bm)(modN)x^2 \equiv f'(m)^2 \prod_T (a - bm) \pmod N. By Lemma 2, φ(a−bα)=a−bm\varphi(a - b\alpha) = a - bm and φ(f′(α))=f′(m)\varphi(f'(\alpha)) = f'(m), since f′f' has degree d−1<dd - 1 < d. Line L8 sets y=δ(m) mod N=φ(δ)y = \delta(m) \bmod N = \varphi(\delta). By Lemma 6 and Lemma 2, y2=φ(δ2)=φ(γ)=f′(m)2∏T(a−bm)y^2 = \varphi(\delta^2) = \varphi(\gamma) = f'(m)^2 \prod_T (a - bm) in Z/NZ\mathbb{Z}/N\mathbb{Z}. So x2≡y2x^2 \equiv y^2. □\square

Lemma 8 (the output). snfs returns 11 or an integer gg with 1<g<N1 < g < N and g∣Ng \mid N, or raises ValueError when f(m)≢0(modN)f(m) \not\equiv 0 \pmod N.

Proof. Line L1 raises the error exactly when f(m) mod N≠0f(m) \bmod N \ne 0. The returns with a value other than 11 are these. Line L1 returns gcd⁡(m−z,N)\gcd(m - z, N) only if it lies strictly between 11 and NN. The trial division returns a prime pp with p∣Np \mid N only if p<Np < N, and 11 otherwise. The test of gcd⁡(f′(m),N)\gcd(f'(m), N) and line L9 return a gcd only if it lies strictly between 11 and NN. A gcd with NN divides NN. Every other exit returns 11. □\square

When gg comes from line L9, Lemma 7 explains why it is a factor: N∣(x−y)(x+y)N \mid (x - y)(x + y), and gg is proper exactly when x≢±yx \not\equiv \pm y modulo some but not all prime power factors of NN.

Lemma 9 (inert primes and the square root modulo pp). Let pp be an odd prime with p≡3(mod4)p \equiv 3 \pmod 4 such that is_inert(f, p) is true. Then ff is irreducible modulo pp, K=Fp[x]/(f)K = \mathbb{F}_p[x]/(f) is a field with q=pdq = p^d elements, and q≡3(mod4)q \equiv 3 \pmod 4. For γ∈K∗\gamma \in K^*, r0=γ(3q−5)/4r_0 = \gamma^{(3q-5)/4} satisfies r02γ=1r_0^2\gamma = 1 if and only if γ\gamma is a square in K∗K^*. Moreover, if f=xd+cf = x^d + c and p≢1(modd)p \not\equiv 1 \pmod d, then ff has a root modulo pp, so pp is not inert.

Proof. The test says that ff has no root modulo pp and xpd≡x(modf,p)x^{p^d} \equiv x \pmod{f, p}. The second condition means that ff divides xpd−xx^{p^d} - x in Fp[x]\mathbb{F}_p[x]. That polynomial is the product of all monic irreducible polynomials over Fp\mathbb{F}_p of degree dividing dd, each once. So every irreducible factor of ff has degree 11 or dd, since dd is prime. There is no root, so no factor of degree 11, and ff is irreducible of degree dd. Hence KK is a field with q=pdq = p^d elements. Since p≡3(mod4)p \equiv 3 \pmod 4 and dd is odd, q≡3d≡3(mod4)q \equiv 3^d \equiv 3 \pmod 4, so (3q−5)/4(3q - 5)/4 is an integer.

K∗K^* is cyclic of order q−1q - 1, so γ\gamma is a square if and only if γ(q−1)/2=1\gamma^{(q-1)/2} = 1. Now r02γ=γ(3q−5)/2+1=γ3(q−1)/2=(γ(q−1)/2)3r_0^2\gamma = \gamma^{(3q-5)/2 + 1} = \gamma^{3(q-1)/2} = (\gamma^{(q-1)/2})^3. The value γ(q−1)/2\gamma^{(q-1)/2} is 11 or −1-1, and its cube is the same value. So r02γ=1r_0^2\gamma = 1 exactly when γ\gamma is a square.

For the last claim, if p=dp = d then xd+c≡(x+c)d(modd)x^d + c \equiv (x + c)^d \pmod d has the root −c-c. Otherwise d∤p−1d \nmid p - 1, and since dd is prime, gcd⁡(d,p−1)=1\gcd(d, p - 1) = 1. Then u↦udu \mapsto u^d is injective on the cyclic group Fp∗\mathbb{F}_p^* of order p−1p - 1, hence onto, and on 00 it is 00. So −c-c is a dd-th power udu^d modulo pp, and uu is a root. □\square

Lemma 9 explains why line L8 finds the square root when it exists, and why inert_prime only succeeds at primes p≡1(modd)p \equiv 1 \pmod d for the special polynomials. Correctness does not depend on it, since Lemma 6 checks the result.

Lemma 10 (Newton's step). Let MM be an odd modulus, and let r,γr, \gamma in (Z/M2)[x]/(f)(\mathbb{Z}/M^2)[x]/(f) satisfy γr2≡1(modM)\gamma r^2 \equiv 1 \pmod M. Then r′=r(3−γr2)/2r' = r(3 - \gamma r^2)/2 satisfies γr′2≡1(modM2)\gamma r'^2 \equiv 1 \pmod{M^2}.

Proof. Write γr2=1−e\gamma r^2 = 1 - e with e≡0(modM)e \equiv 0 \pmod M. Then r′=r(2+e)/2r' = r(2 + e)/2, where 22 is invertible since MM is odd, and

γr′2=(1−e)(2+e)24=4−3e2−e34=1−3e2+e34.\gamma r'^2 = \frac{(1 - e)(2 + e)^2}{4} = \frac{4 - 3e^2 - e^3}{4} = 1 - \frac{3e^2 + e^3}{4}.

Both e2e^2 and e3e^3 are 00 modulo M2M^2. □\square

Lemma 11 (the vector of a relation is defined). For a relation (a,b)(a, b) and a prime p∈Pp \in P that divides F(a,b)F(a, b), p∤bp \nmid b, and the pair (p,ab−1 mod p)(p, ab^{-1} \bmod p) is in the algebraic factor base.

Proof. Modulo bb, F(a,b)=∑ciaibd−i≡adF(a, b) = \sum c_i a^i b^{d-i} \equiv a^d, since cd=1c_d = 1. If p∣bp \mid b and p∣F(a,b)p \mid F(a, b), then p∣adp \mid a^d, so p∣ap \mid a, against gcd⁡(a,b)=1\gcd(a, b) = 1. So p∤bp \nmid b, and with r=ab−1 mod pr = ab^{-1} \bmod p, 0≡F(a,b)=bdf(a/b)≡bdf(r)(modp)0 \equiv F(a, b) = b^d f(a/b) \equiv b^d f(r) \pmod p, so f(r)≡0(modp)f(r) \equiv 0 \pmod p and (p,r)(p, r) is one of the pairs of line L2. □\square

Lemma 12 (termination). snfs stops on every input.

Proof. integer_roots loops up to ∣c0∣\sqrt{|c_0|}. The trial division and the search for an inert prime run over finite ranges (at most 20002000 candidates for the inert prime), and if none is found the function returns 11. If an inert prime pp is found, then ff is irreducible modulo pp by Lemma 9, so it is irreducible over Q\mathbb{Q} (a factorization over Q\mathbb{Q} gives one over Z\mathbb{Z} with monic factors, by Gauss's lemma, and it reduces to a factorization modulo pp). Then the loop of line L2 for the characters stops: the discriminant Δ\Delta of ff is not 00, since an irreducible polynomial over Q\mathbb{Q} has distinct roots, and by Schur's theorem infinitely many primes qq divide some value f(s)f(s) with s∈Zs \in \mathbb{Z}; for such q∤Δq \nmid \Delta, s mod qs \bmod q is a simple root, so f′(s)≢0(modq)f'(s) \not\equiv 0 \pmod q. So enough characters exist, and the search reaches them. The sieve runs over at most YY lines of 2A+12A + 1 points. Trial division of a nonzero integer by finitely many primes stops. The elimination is a loop over the rows, and each inner loop clears the top bit of vv or stops, so it runs at most as many times as vv has binary digits. The dependency loop is finite. In algebraic_sqrt, the modulus is squared at each step and the loop stops when it passes a fixed bound. □\square

Theorem. snfs stops and returns either 11 or a proper factor of NN. When it returns a proper factor from line L9, it has found xx and yy with x2≡y2(modN)x^2 \equiv y^2 \pmod N.

Proof. Lemmas 12, 8 and 7. □\square

The driver. factorize(n) returns the prime factors of n≥1n \ge 1 with multiplicity, in order, provided that is_probable_prime is correct on the numbers it sees. That holds for all numbers below 33170440646798873859619813317044064679887385961981, where the strong test to the 1313 prime bases up to 4141 is a proof of primality.

Proof. special_form returns (r,e,s)(r, e, s) with n=re+sn = r^e + s or nothing, and the polynomials of degrees 33 and 55 come from Lemma 1, so for each of them f(root)≡0f(\text{root}) \equiv 0 modulo every divisor of nn.

The trial division divides out the primes below 10001000 in increasing order. If it stops because p2>np^2 > n for the remaining nn, then nn has no prime factor below pp, so nn is 11 or a prime. Otherwise nn has no prime factor below 10001000. The invariant of the stack is this: the product of the output and of the stack equals the input, every output entry is prime, and every stack entry divides the original nn and has no prime factor below 10001000. It holds at the start. A popped mm that is 11 is dropped. A popped m>1m > 1 with m<106m < 10^6 is prime, because a composite with no prime factor below 10001000 is at least 10092>1061009^2 > 10^6. A popped m≥106m \ge 10^6 that passes is_probable_prime is prime by the assumption. Otherwise mm is composite and odd, and _split(m) returns a proper factor gg:

  • A perfect power m=rem = r^e with e≥2e \ge 2 gives 1<r<m1 < r < m and r∣mr \mid m.
  • _split keeps a special polynomial only if f(root)≡0(modm)f(\text{root}) \equiv 0 \pmod m, which it tests, so snfs never raises. A returned gcd⁡(root−z,m)\gcd(\text{root} - z, m) strictly between 11 and mm is a proper factor.
  • The base-mm polynomial of select_polynomial satisfies f(root)=mf(\text{root}) = m, so f(root)≡0(modm)f(\text{root}) \equiv 0 \pmod m as well. The choice and order of the polynomials, by value_size, only affect the speed. Every value returned from snfs is accepted only if it lies strictly between 11 and mm, and it divides mm by the Theorem.
  • The last resort finds the least odd t≥1001t \ge 1001 with t∣mt \mid m, which exists and is below mm because mm is odd and composite with no prime factor below 10001000.

Each of these loops is finite: at most 2+12 + 1 polynomials, 33 bounds each, and snfs stops by Lemma 12. Then gg and m/gm/g both divide mm, so they divide nn and have no prime factor below 10001000, and their product is mm. The invariant holds after each step. Each split replaces mm by two numbers greater than 11 whose product is mm, so the total number of prime factors counted with multiplicity bounds the number of splits, and the loop stops. When the stack is empty, the output is a list of primes whose product is the input, and sorting puts it in order. ■\blacksquare

Back to Special number field sieve