Algorithm Search⌘K

Proofs: Modular linear equation solver

These are the human proofs of Modular linear equation solver, at full length.

Correctness of the modular linear equation solver full

This proof shows: correctness, termination, sorted-output.

Let aa, bb be integers and n≥1n \ge 1. Write S={x∈Z:n∣ax−b}S = \{ x \in \mathbb{Z} : n \mid ax - b \} for the set of integer solutions. We prove that MODULAR-LINEAR-EQUATION-SOLVER(a,b,n)(a, b, n) terminates and returns the elements of S∩[0,n)S \cap [0, n) in increasing order.

Line L1. Let aˉ=a mod n\bar a = a \bmod n and bˉ=b mod n\bar b = b \bmod n. Since aˉ≡a\bar a \equiv a and bˉ≡b(modn)\bar b \equiv b \pmod n, we have ax−b≡aˉx−bˉ(modn)ax - b \equiv \bar a x - \bar b \pmod n for every xx, so the set SS does not change. Also gcd⁡(aˉ,n)=gcd⁡(a,n)\gcd(\bar a, n) = \gcd(a, n). From here on, aa and bb denote the reduced values, with 0≤a,b<n0 \le a, b < n.

Lemma 1 (line L2). The loop form of EXTENDED-EUCLID on (a,n)(a, n) with a,n≥0a, n \ge 0, not both 0, terminates and returns (d,x′,y′)(d, x', y') with d=gcd⁡(a,n)≥1d = \gcd(a, n) \ge 1 and ax′+ny′=da x' + n y' = d.

Proof. The loop keeps the invariant r0=as0+nt0r_0 = a s_0 + n t_0, r1=as1+nt1r_1 = a s_1 + n t_1 and gcd⁡(r0,r1)=gcd⁡(a,n)\gcd(r_0, r_1) = \gcd(a, n). It holds at the start, with (r0,r1)=(a,n)(r_0, r_1) = (a, n), (s0,s1)=(1,0)(s_0, s_1) = (1, 0) and (t0,t1)=(0,1)(t_0, t_1) = (0, 1). One pass replaces (r0,r1)(r_0, r_1) by (r1,r0−qr1)(r_1, r_0 - q r_1) with q=⌊r0/r1⌋q = \lfloor r_0 / r_1 \rfloor, and each coefficient pair by the same linear combination, so both identities hold after the pass; and gcd⁡(r1,r0−qr1)=gcd⁡(r0,r1)\gcd(r_1, r_0 - q r_1) = \gcd(r_0, r_1). All remainders are non-negative. From the second pass on, r1r_1 strictly decreases, since 0≤r0 mod r1<r10 \le r_0 \bmod r_1 < r_1; so the loop stops, with r1=0r_1 = 0. Then gcd⁡(r0,0)=r0\gcd(r_0, 0) = r_0, so r0=gcd⁡(a,n)r_0 = \gcd(a, n), and r0=as0+nt0r_0 = a s_0 + n t_0. Since n≥1n \ge 1, d≥1d \ge 1. □\square

Lemma 2 (structure of S). Let d=gcd⁡(a,n)d = \gcd(a, n) and m=n/dm = n/d.

  1. If d∤bd \nmid b, then S=∅S = \emptyset.
  2. If d∣bd \mid b, then x′(b/d)∈Sx' (b/d) \in S, and for any x∗∈Sx^* \in S, S={x:x≡x∗(modm)}S = \{ x : x \equiv x^* \pmod m \}.

Proof. (1) If x∈Sx \in S, then ax−b=nkax - b = nk for an integer kk, so b=ax−nkb = ax - nk is a multiple of dd, because dd divides aa and nn.

(2) By Lemma 1, ax′=d−ny′a x' = d - n y'. Multiplying by b/db/d gives a⋅x′(b/d)=b−ny′(b/d)a \cdot x'(b/d) = b - n y'(b/d), so n∣a⋅x′(b/d)−bn \mid a \cdot x'(b/d) - b, and x′(b/d)∈Sx'(b/d) \in S.

Let x∗∈Sx^* \in S. For any integer xx, x∈Sx \in S if and only if n∣a(x−x∗)n \mid a(x - x^*), since ax−b=a(x−x∗)+(ax∗−b)ax - b = a(x - x^*) + (a x^* - b) and n∣ax∗−bn \mid a x^* - b. Write a=da1a = d a_1 and n=dmn = d m, with gcd⁡(a1,m)=1\gcd(a_1, m) = 1. Then n∣a(x−x∗)n \mid a(x - x^*) if and only if m∣a1(x−x∗)m \mid a_1 (x - x^*), and, as gcd⁡(a1,m)=1\gcd(a_1, m) = 1, by Euclid's lemma in its general form, if and only if m∣x−x∗m \mid x - x^*. □\square

Line L3. If d∤bd \nmid b, by Lemma 2(1) S∩[0,n)=∅S \cap [0, n) = \emptyset, and the algorithm returns the empty list, which is correct. From here on, d∣bd \mid b.

Line L5. x0=(x′(b/d)) mod mx_0 = (x' (b/d)) \bmod m satisfies 0≤x0<m0 \le x_0 < m and x0≡x′(b/d)(modm)x_0 \equiv x'(b/d) \pmod m. By Lemma 2(2), with x∗=x′(b/d)x^* = x'(b/d), x0∈Sx_0 \in S and S={x0+jm:j∈Z}S = \{ x_0 + j m : j \in \mathbb{Z} \}.

Lines L6 to L9. We claim the following loop invariant: at the start of the pass with index ii, for 0≤i≤d0 \le i \le d, the list XX holds the elements of S∩[0,im)S \cap [0, i m) in increasing order, and these are x0,x0+m,…,x0+(i−1)mx_0, x_0 + m, \ldots, x_0 + (i - 1) m.

Initialization. For i=0i = 0, XX is empty and [0,0)[0, 0) is empty.

Maintenance. The elements of SS in [im,(i+1)m)[i m, (i + 1) m) are the x0+jmx_0 + j m with im≤x0+jm<(i+1)mi m \le x_0 + jm < (i + 1) m. Since 0≤x0<m0 \le x_0 < m, this holds exactly for j=ij = i. So S∩[0,(i+1)m)S \cap [0, (i+1) m) is S∩[0,im)S \cap [0, im) with the one element x0+imx_0 + i m added, and that element is larger than all earlier ones. Line L8 appends exactly this element.

Termination. The loop runs for i=0,…,d−1i = 0, \ldots, d - 1 and stops with i=di = d. Since dm=nd m = n, the invariant gives that XX is S∩[0,n)S \cap [0, n) in increasing order, with dd elements. Line L9 returns it.

Every loop terminates: the gcd loop by Lemma 1, and the final loop after dd passes. So the algorithm returns, in increasing order, exactly the residues x∈{0,…,n−1}x \in \{0, \ldots, n - 1\} with ax≡b(modn)ax \equiv b \pmod n. ■\blacksquare

Back to Modular linear equation solver