The extended Euclidean algorithm returns the gcd and small Bézout coefficients full
This proof shows: correctness.
Let F be a field and f,g∈F[x]. We number the values of the loop as in the page: r0=f, r1=g, s0=1, s1=0, t0=0, t1=1, and the pass of line L4 for i=1,2,… computes ri−1=qiri+ri+1 with degri+1<degri, si+1=si−1−qisi and ti+1=ti−1−qiti. The loop stops after the pass with rℓ+1=0, and then the variables (r0,s0,t0) of the algorithm hold (rℓ,sℓ,tℓ).
Invariant. sif+tig=ri for every i that the algorithm computes.
Proof. For i=0 and i=1 this is 1⋅f+0⋅g=f and 0⋅f+1⋅g=g. If it holds for i−1 and i, then
si+1f+ti+1g=(si−1f+ti−1g)−qi(sif+tig)=ri−1−qiri=ri+1.□ The gcd. The values ri are those of the Euclidean algorithm, so by its proof the loop stops, and rℓ is a gcd of f and g when (f,g)=(0,0). If f=g=0, the loop does not run, line L5 returns (0,1,0), and 1⋅0+0⋅0=0=gcd(0,0). Otherwise rℓ=0, u=lc(rℓ)−1, and multiplying the invariant for i=ℓ by u gives (usℓ)f+(utℓ)g=urℓ, the monic gcd h.
Degree formulas. Assume m=degf≥n=degg≥0, and write ni=degri, so n0=m≥n1=n>n2>⋯>nℓ≥0 and degqi=ni−1−ni for 1≤i≤ℓ (rule 3 of degree of a polynomial). We show, for 2≤i≤ℓ+1,
degsi=n1−ni−1,degti=n0−ni−1. For i=2: s2=1−q1⋅0=1, of degree 0=n1−n1, and t2=0−q1⋅1=−q1, of degree n0−n1.
For i=3: s3=0−q2⋅1, of degree n1−n2, and t3=1−q2t2. Here deg(q2t2)=(n1−n2)+(n0−n1)=n0−n2≥1>0=deg1, so degt3=n0−n2.
For 4≤i+1≤ℓ+1, assume the formulas for i−1 and i, with i≥3. Then
deg(qisi)=(ni−1−ni)+(n1−ni−1)=n1−ni>n1−ni−2=degsi−1, because ni<ni−2. By rule 1 of degree, degsi+1=deg(si−1−qisi)=n1−ni. In the same way deg(qiti)=n0−ni>n0−ni−2=degti−1, so degti+1=n0−ni. □
Degree bounds of the output. Assume f,g=0 and that h is neither the monic f nor the monic g. If m<n, the first pass gives q1=0, r2=f, s2=1, t2=0, so from then on the algorithm runs as XGCD(g,f) with the roles of s and t exchanged; so it suffices to treat m≥n. Then g∤f, since otherwise h would be the monic g; so r2=0 and ℓ≥2. The formulas with i=ℓ give
degs=degsℓ=n1−nℓ−1<n1−nℓ=degg−degh,degt=degtℓ=n0−nℓ−1<n0−nℓ=degf−degh, since nℓ−1>nℓ=degh. Scaling by u does not change degrees. ■