Post · 6 min read· pp. 100–103

Linear Diophantine equations

Equations of the form ∑iaixi=0\sum_i a_i x_i = 0 where the xix_i’s are integer unknowns are called linear Diophantine equations. Their integer solutions can be computed using greatest common denominator (GCD) tricks. In this post, we go over a few basic types of such equations and their integer solutions.

\def\lcm{\ \text{lcm}}

ax+by=tax+by = t

Theorem: This equation has an integer solution ⇔gcd⁡(a,b)∣t\Leftrightarrow \gcd(a,b) \mathrel\vert t.

Proof (“⇒\Rightarrow”): Assume an integer solution (x0,y0)(x_0, y_0) exists when gcd⁡(a,b)∤t\gcd(a,b) \nmid t. Since gcd⁡(a,b)\gcd(a,b) divides both aa and bb, it divides any linear combination of them, including ax0+by0=tax_0 + by_0 = t, which implies it divides tt. Contradiction. Proof (“⇒\Rightarrow”): If gcd⁡(a,b)=1\gcd(a,b) = 1, then an integer solution x0,y0x_0,y_0 can be obtained using the Extended Eucliden algorithm, which finds (u,v)(u,v) such that au+bv=gcd⁡(a,b)au + bv = \gcd(a,b). The solution is:

x0=u⋅t/gcd⁡(a,b) y0=v⋅t/gcd⁡(a,b)\begin{align} x_0 &= u\cdot t / \gcd(a,b)\\\ y_0 &= v\cdot t / \gcd(a,b) \end{align}
This is because ax0+ay0=(au+bv)tgcd⁡(a,b)=gcd⁡(a,b)tgcd⁡(a,b)=tax_0 + ay_0 = (au + bv) \frac{t}{\gcd(a,b)} = \gcd(a,b) \frac{t}{\gcd(a,b)} = t.

Theorem: When gcd⁡(a,b)∣t\gcd(a,b) \mathrel\vert t, if (x0,y0)(x_0, y_0) is an integer solution to such an equation, then all integer solutions can be characterized as: \begin{align} x &= x_0 - k b/\gcd(a,b)\
y &= y_0 + k a/\gcd(a,b) \end{align} Here, kk is an arbitrary integer and either a≠0a\ne 0 or b≠0b\ne 0 (since otherwise gcd⁡(a,b)=0\gcd(a,b) = 0 and gcd⁡(a,b)∣t\gcd(a,b)\mathrel\vert t implies t=0t=0, which means all integers (x,y)(x,y) are solutions).

Proof: First, one can easily verify that the proposed (x,y)(x,y) are indeed solutions that satisfy ax+by=tax+by=t:

ax+by=ax0+kab/gcd⁡(a,b)+by0−kab/gcd(a,b)=ax0+by0=t\begin{align} ax + by &= ax_0 + kab / \gcd(a,b) + by_0 - kab/gcd(a,b)\\ &= ax_0 + by_0 = t \end{align}
The more difficult part is to argue that every solution has this form! Assume a≠0a\ne 0 since the other b≠0b\ne 0 case is symmetric. Assume (x,y)(x,y) to be an integer solution and note that, since (x0,y0)(x_0, y_0) is a solution, this means:
a(x−x0)+b(y−y0)=0⇔a(x−x0)=b(y0−y)⇔agcd⁡(a,b)(x−x0)=bgcd⁡(a,b)(y0−y)\begin{align} a(x-x_0) + b(y - y_0) &= 0\Leftrightarrow\\ a(x - x_0) &= b(y_0 - y)\Leftrightarrow\\ \frac{a}{\gcd(a,b)}(x-x_0) &= \frac{b}{\gcd(a,b)} (y_0 - y) \end{align}
This implies agcd⁡(a,b)∣bgcd⁡(a,b)(y−y0)\frac{a}{\gcd(a,b)} \mathrel\vert \frac{b}{\gcd(a,b)} (y - y_0). Since gcd⁡(agcd⁡(a,b),bgcd⁡(a,b))=1\gcd(\frac{a}{\gcd(a,b)}, \frac{b}{\gcd(a,b)}) = 1[^Heff03], this means agcd(a,b)∣(y−y0)\frac{a}{gcd(a,b)} \mathrel\vert (y-y_0), which means ∃k\exists k such that:
a/gcd⁡(a,b)⋅k=(y−y0)⇔y=y0+ka/gcd⁡(a,b)\begin{align} a/\gcd(a,b) \cdot k &= (y-y_0)\Leftrightarrow\\ y &= y_0 + k a / \gcd(a,b) \end{align}
Next, substitute yy in a(x−x0)=b(y0−y)a(x-x_0) = b(y_0 - y), to get:
a(x−x0)=b(y0−y0−ka/gcd⁡(a,b))⇔x−x0=(−bka/gcd⁡(a,b))/a=x0−kb/gcd⁡(a,b)\begin{align} a(x-x_0) &= b(y_0 - y_0 - k a / \gcd(a,b))\Leftrightarrow\\ x - x_0 &= (- b k a / \gcd(a,b)) / a\\ &= x_0 - k b / \gcd(a,b) \end{align}
Note that since a≠0a\ne 0, we are allowed to divide by aa above.

ax+by=0ax+by = 0

In this case, since x0=0x_0 = 0 and y0=0y_0 = 0 is one integer solution, all integer solutions are of the form:

x=−kb/gcd⁡(a,b)y=ka/gcd⁡(a,b)\begin{align} x &= -kb/\gcd(a,b)\\ y &= ka/\gcd(a,b) \end{align}

ax+by+cz=tax+by+cz = t

Theorem: This equation has an integer solution ⇔gcd⁡(a,b,c)∣t\Leftrightarrow \gcd(a,b,c) \mathrel\vert t.

Proof (“⇒\Rightarrow”): Proceeds analogously to the ax+by=tax+by = t case before. Proof (“⇐\Leftarrow”): We will generalize the proof from the ax+by=tax+by = t case before. gcd⁡(a,b,c)∣t⇒gcd⁡(a,gcd⁡(b,c))∣t⇒ax+gcd⁡(b,c)w=t\gcd(a,b,c) \mathrel\vert t \Rightarrow \gcd(a, \gcd(b, c)) \mathrel\vert t \Rightarrow ax + \gcd(b,c)w = t has an integer solution (x0,w0)(x_0, w_0). Let gcd⁡(b,c)=d\gcd(b,c) = d. We know ∃(y0,z0)\exists (y_0, z_0) such that:

by0+cz0=d\begin{align} b y_0 + c z_0 = d \end{align}
Replacing in the previous equation, we have:
ax0+gcd⁡(b,c)w0=t⇔ax0+dw0=t⇔ax0+(by0+cz0)w0=t⇔ax0+bw0y0+cw0z0=t\begin{align} ax_0 + \gcd(b,c)w_0 &= t\Leftrightarrow\\ ax_0 + dw_0 &= t\Leftrightarrow\\ ax_0 + (b y_0 + c z_0) w_0 &= t\Leftrightarrow\\ ax_0 + b w_0 y_0 + c w_0 z_0 &= t \end{align}
Thus, an integer solution is (x0,y0w0,z0w0)(x_0, y_0 w_0, z_0 w_0).

Theorem (from Sec 6.2 in [^Cohe07]): In this case, if (x0,y0,z0)(x_0, y_0, z_0) is one integer solution, then all integer solutions are of the form:

x=x0+mb/gcd⁡(a,b)−ℓc/gcd⁡(a,c)y=y0+kc/gcd⁡(b,c)−ma/gcd⁡(a,b)z=z0+ℓa/gcd⁡(a,c)−kb/gcd⁡(b,c)\begin{align} x &= x_0 + m b / \gcd(a,b) - \ell c / \gcd(a,c)\\ y &= y_0 + k c / \gcd(b,c) - m a / \gcd(a, b)\\ z &= z_0 + \ell a / \gcd(a, c) - k b / \gcd(b, c) \end{align}
Here, m,ℓ,km,\ell,k are integers and at least one of a,ba,b or cc are ≠0\ne 0.

ax+by+cz=0ax+by+cz = 0

In this case, all integer solutions are of the form:

x=mb/gcd⁡(a,b)−ℓc/gcd⁡(a,c)y=kc/gcd⁡(b,c)−ma/gcd⁡(a,b)z=ℓa/gcd⁡(a,c)−kb/gcd⁡(b,c)\begin{align} x &= m b / \gcd(a,b) - \ell c / \gcd(a,c)\\ y &= k c / \gcd(b,c) - m a / \gcd(a, b)\\ z &= \ell a / \gcd(a, c) - k b / \gcd(b, c) \end{align}
Here, m,ℓ,km,\ell,k are arbitrary integers.

This could also be simplified in terms of the lowest common multiple (LCM), since ab=gcd⁡(a,b)\lcm(a,b)⇒b/gcd⁡(a,b)=\lcm(a,b)/aa b = \gcd(a,b) \lcm(a,b)\Rightarrow b / \gcd(a,b) = \lcm(a,b) / a:

x=m\lcm(a,b)/a−ℓ\lcm(a,c)/a=m\lcm(a,b)−ℓ\lcm(a,c)ay=k\lcm(b,c)/b−m\lcm(a,b)/b=k\lcm(b,c)−m\lcm(a,b)bz=ℓ\lcm(a,c)/c−k\lcm(b,c)/c=ℓ\lcm(a,c)−k\lcm(b,c)c\begin{align} x &= m \lcm(a,b) / a - \ell \lcm(a,c) / a = \frac{m\lcm(a,b) - \ell\lcm(a,c)}{a}\\ y &= k \lcm(b,c) / b - m \lcm(a, b) / b = \frac{k\lcm(b,c) - m\lcm(a,b)}{b}\\ z &= \ell \lcm(a, c) / c - k \lcm(b, c) / c = \frac{\ell\lcm(a,c) - k\lcm(b,c)}{c} \end{align}

An example

In a Catalano-Fiore vector commitment (VC)[^CF13e] of size n=3n=3, collision resistance is implied by the fact that the following equation with (ℓ+1)(\ell+1)-bit primes e1,e2,e3e_1,e_2,e_3 does not have any ℓ\ell-bit integer solutions:

e2e3v1+e1e3v2+e1e2v3=0e_2 e_3 v_1 + e_1 e_3 v_2 + e_1 e_2 v_3 = 0

The only integer solutions given by the formula above are at least ℓ+1\ell+1 bit wide:

x=m(e1e3)/gcd⁡(e2e3,e1e3)−ℓ(e1e2)/gcd⁡(e2e3,e1e2)==m(e1e3)/e3−ℓ(e1e2)/e2=m’e1 (where m’=m−ℓ)y=k’e2z=ℓ’e3\begin{align} %x &= \frac{m \lcm(e_2 e_3, e_1 e_3) - \ell\lcm(e_2 e_3, e_1 e_2)}{e_2 e_3}\\ % &= \frac{m e_1 e_2 e_3 - \ell e_1 e_2 e_3}{e_2 e_3} = (m-\ell)e_1\\ x &= m (e_1 e_3) / \gcd(e_2 e_3, e_1 e_3) - \ell (e_1 e_2) / \gcd(e_2 e_3, e_1 e_2) =\\ &= m (e_1 e_3) / e_3 - \ell (e_1 e_2) / e_2 = m’ e_1\ (\text{where}\ m’=m-\ell)\\ y &= k’ e_2\\ z &= \ell’ e_3 \end{align}

∑i∈[n]aixi=t\sum_{i\in[n]} a_i x_i = t

Theorem: This equation has an integer solution ⇔gcd⁡(a1,…,an)∣t\Leftrightarrow \gcd(a_1, \dots, a_n) \mathrel\vert t.

Proof: The “⇒\Rightarrow” direction proceeds as before.

The “⇐\Leftarrow” direction proceeds by induction. The statement P(n)P(n) being proved by induction is:

∀ai∈Z,gcd⁡(a1,…,an)∣t⇒∑i∈[n]aixi=t has an integer solution.\forall a_i\in \mathbb{Z}, \gcd(a_1, \dots, a_n) \mathrel\vert t\Rightarrow \sum_{i\in[n]} a_i x_i = t\ \text{has an integer solution.}

We prove P(n)P(n) is true for all n≥2n \ge 2. First, P(2)P(2) is clearly true as we’ve shown before. Second, we must show P(n)⇒P(n+1)P(n) \Rightarrow P(n + 1). This we do analogously to the proof for n=3n=3 from before. Let d=gcd⁡(a1,…,an)d = \gcd(a_1, \dots, a_n). We know that gcd⁡(a1,…,an+1)=gcd⁡(d,an+1)\gcd(a_1, \dots, a_{n+1}) = \gcd(d, a_{n+1}). Since d∣td \mathrel| t (because P(n)P(n) is true), this implies ∃\exists integers s0,w0s_0, w_0 such that:

ds0+an+1w0=t⇔\begin{align} d s_0 + a_{n+1} w_0 = t \Leftrightarrow \end{align}

Since P(n)P(n) is true, we know there exist integers s1,…,sns_1,\dots, s_n such that:

∑i∈[n]aisi=d\begin{align} \sum_{i\in[n]} a_i s_i = d \end{align}

Replacing dd in the previous equation, we get:

ds0+an+1w0=t⇔(∑i∈[n]aisi)s0+an+1w0=t\begin{align} d s_0 + a_{n+1} w_0 &= t \Leftrightarrow\\ \left(\sum_{i\in[n]} a_i s_i\right) s_0 + a_{n+1} w_0 &= t \end{align}
Thus, a solution can be found by setting xi=s0si,∀i∈[n]x_i = s_0 s_i,\forall i\in[n] and xn+1=w0x_{n+1} = w_0.

[^Heff03]: Elementary Number Theory, by Jim Hefferson and W. Edwin Clark, 2004, [URL] [^Cohe07]: Number Theory: Volume I: Tools and Diophantine Equations, by Cohen, H., 2007, [URL] [^CF13e]: Vector Commitments and their Applications, by Dario Catalano and Dario Fiore, in Cryptology ePrint Archive, Report 2011/495, 2011, [URL]