Proofs: Quadratic sieve
These are the human proofs of Quadratic sieve, at full length.
Correctness of the quadratic sieve full
This proof shows: correctness, termination.
Let and . Since is not a perfect square, for every integer , so at line L5 is defined.
Lemma 1 (the root classes). Let be an odd prime with and , and let . Then , and for every integer , if and only if or . Also if and only if is odd.
Proof. If , then , so and , which is excluded. Next, , and since is prime, divides the product if and only if it divides one factor. For : is odd, so is even if and only if is odd, that is, is odd.
Lemma 2 (the sieve). After line L4 runs on a block , for each , over the distinct primes , , that divide .
Proof. Line L4 sets and then, for each prime of and each of its root classes , adds to exactly the entries with . By Lemma 1, the root classes of an odd are two distinct classes, and exactly when lies in one of them; so is added once if and never otherwise. For the one class gives the same. Every odd has , which implies , so Lemma 1 applies to it.
Lemma 3 (relations). Every relation in satisfies , with included. No two relations have the same .
Proof. Line L6 appends only when trial division shows , with exactly when . Then . The blocks of line L3 are disjoint intervals of : the right blocks cover and the left blocks cover , each cut to . So each , hence each , is looked at once.
Lemma 4 (the elimination). Let be the exponent vectors mod 2, as bit masks over the coordinates of . Gaussian elimination that records, for each reduced row, the set of original rows added into it, produces for each row that reduces to a nonempty set with . If , at least such sets are found.
Proof. Row starts with and . Each reduction adds a stored pivot pair with to by in both parts, so stays true. The stored pairs come from earlier rows, so , and stays in . Each reduction clears the leading bit of and changes only lower bits, so the row either reduces to or becomes a pivot with a new leading bit. There are coordinates, so at most pivots, and at least rows reduce to .
Theorem. QS stops, and it returns or a proper factor of . Each subset tried at line L8 satisfies . If the loop ends with , at least subsets are tried.
Proof. Termination. Each pass of the loop of line L2 increases at line L7, and the loop stops when . Each pass sieves two blocks of at most values with finitely many primes, and trial division of a nonzero integer by primes stops. Line L8 runs over finitely many subsets.
Congruence of squares. Let be a subset from Lemma 4 and over the integers. Every is even, including . By Lemma 3, , since . The count of subsets is Lemma 4 with .
Correctness. The only return other than line L9 is in line L8, which returns only if . A gcd with divides , so is a proper factor. A prime has none, so for a prime the output is . The threshold of line L5 decides only which values are tried by trial division, so it does not affect any of these statements.