HGCD returns the Euclid state straddling 2^m, and GCD returns gcd(a, b) full
This proof shows: correctness.
Write E(q)=(q110) for the quotient matrix of q. Then E(q)(x,y)T=(qx+y,x)T, detE(q)=−1 and E(q)−1=(011−q). One Euclid step maps (x,y) with x≥y>0 to (y,x−qy) with q=⌊x/y⌋; equivalently (x,y)T=E(q)(y,x−qy)T. For an input a≥b≥0 of HGCD, the Euclid states are the pairs s0=(a,b), s1, s2, ..., obtained by Euclid steps until the second entry is 0, and Qi is the list of the first i quotients. The second entries strictly decrease.
Lemma 1 (certificate). Let q1,…,qj≥1 with j≥1, R=E(q1)⋯E(qj), and (α,β)T=R−1(a,b)T. If α>β>0, then (α,β)=sj and the first j quotients of Euclid on (a,b) are q1,…,qj.
Proof. Let pj=(α,β) and pi−1=E(qi)pi for i=j,…,1, so p0=Rpj=(a,b). We show by downward induction that pi=(xi,yi) satisfies xi>yi>0 for all i. It holds for i=j. If it holds for i, then pi−1=(qixi+yi,xi), and qixi+yi≥xi+yi>xi>0, so it holds for i−1. Now for each i≥1, pi−1=(qixi+yi,xi) with 0<yi<xi, so ⌊(qixi+yi)/xi⌋=qi and the remainder is yi: the Euclid step from pi−1 has quotient qi and leads to pi. Since p0=(a,b)=s0, induction on i gives pi=si with quotients q1,…,qi. □
Lemma 2 (FIXUP). Let R=E(q1)⋯E(qj) with all qi≥1 and P=(q1,…,qj), j≥0, and let a≥b≥0 and L≤a. Then FIXUP(R,P,a,b,L) terminates and returns (R′,P′,α,β) with P′ a prefix (q1,…,qi) of P, R′=E(q1)⋯E(qi), (a,b)T=R′(α,β)T, and either i=0 and (α,β)=(a,b), or i≥1, α>β>0 and α≥L. In both cases (α,β)=si and P′=Qi.
Proof. Line L11 sets (α,β)T=R−1(a,b)T; the implementation computes R−1 as detR times the adjugate, which is exact because detR=(−1)j. The loop keeps the relation (α,β)T=R−1(a,b)T with R the product over P: removing q replaces R by RE(q)−1, whose inverse is E(q)R−1, and E(q)(α,β)T=(qα+β,α)T, which is the update of line L12. Each iteration shortens P, so the loop ends. If it ends with P empty, then R=I and (α,β)=(a,b)=s0. Otherwise it ends because α>β>0 and α≥L, and Lemma 1 gives (α,β)=si with quotients P′. □
Lemma 3 (HGCD). For a≥b≥0 and a threshold T≥2, HGCD(a,b) terminates and returns (M,Q,α,β) with Q=Qj, M=E(q1)⋯E(qj), (α,β)=sj, and α≥2m>β, where m=⌈n/2⌉ and n is the bit length of a; or j=0 and b<2m.
Proof. By strong induction on n. Let λ=2m.
Lines L1 to L3. If b<λ, line L2 returns s0 with j=0, as claimed. If n≤T, line L3 does Euclid steps while the second entry is at least λ, and records each quotient; this is the claim directly, since the second entries decrease, and each step is legal because the second entry is positive.
Otherwise n>T≥2, so m≥1, and b≥λ. We use the invariant: the current pair is si for i=∣Q∣, Q=Qi, M=E(q1)⋯E(qi), and the first entry of the current pair is at least λ.
Line L4. The arguments ⌊a/λ⌋≥⌊b/λ⌋≥0 have n−m<n bits, so by the induction hypothesis the call terminates and returns quotients P, all at least 1, and R their product. (Its α and β are not used.)
Line L5. By Lemma 2 with L=λ≤a, the result is si with Q=Qi and M their product, and its first entry is at least λ: either it is a≥λ (i=0), or it was checked. So the invariant holds.
Line L6. If b≥λ, one Euclid step is legal (b>0), gives si+1 and appends its quotient; the new first entry is the old b≥λ. The invariant holds.
Lines L7 and L8. Let ℓ be the bit length of the current a. The call is made only if b≥λ and 2(ℓ−m)<n. Since ℓ≤n≤2m, k=2m−ℓ≥0, and the arguments ⌊a/2k⌋≥⌊b/2k⌋≥0 have ℓ−k=2(ℓ−m)<n bits, so the induction hypothesis applies again. Lemma 2 for the current pair si, with L=λ≤a, gives the Euclid state si+i′ of the current pair after i′ more quotients, which are the next quotients of the original input. Multiplying M by S and appending P keeps the invariant.
Line L9. Each Euclid step is legal because b≥λ>0, and it keeps the invariant because the new first entry is the old b≥λ. The loop ends because the second entries strictly decrease.
Line L10. Now b<λ and a≥λ by the invariant. The current pair is sj with j=∣Q∣, and the second entries of the Euclid states decrease, so sj is the unique state with first entry at least λ and second entry below λ. □
Theorem. GCD(a,b) returns gcd(a,b) for all integers a and b.
Proof. Line L14 replaces a,b by their absolute values in decreasing order, which does not change the gcd. Each pass of line L15 replaces (a,b) by a Euclid state of it (Lemma 3), and then, if b=0, by one more Euclid step. Euclid steps keep the gcd by the GCD recursion theorem: gcd(x,y)=gcd(y,x−qy). The pass strictly decreases b: HGCD does not increase it, and the final step replaces b by amodb<b. So line L15 ends, and the order a≥b is kept. Line L16 is Euclid's algorithm, which ends with (gcd,0), and gcd(x,0)=x for x≥0. With a=b=0, no loop runs and the result is 0=gcd(0,0). ■