Mode of #cas

extendedGCD

The gcd of two natural numbers with Bézout coefficients s and t, and a theorem that as+bt=gcd(a,b).

#cas [extendedGCD] (a, b)
gives the gcd g of the natural numbers a and b with integers s and t such that a s + b t = g, and proves both facts.

Whenever I need to invert a number modulo another, solve a linear equation ax+by=c in integers, or merge two congruences, what I really need is a pair of Bézout coefficients. extendedGCD runs the extended Euclidean algorithm and prints the answer in the order of Mathematica’s ExtendedGCD, the gcd and then the two coefficients, as g, [s, t]. The theorem carries the two facts I end up using: g is Mathlib’s Nat.gcd a b, and as+bt=g in ℤ.

Details and Options

  • a and b are natural numbers: literals, or closed integer expressions such as 2^32. The coefficients s and t are integers.
  • The theorem is Nat.gcd a b = g ∧ (a : ℤ) * s + b * t = g. The kernel evaluates the gcd (decide +kernel), and Mathlib’s tactic norm_num checks the equation.
  • The coefficients are those of the extended Euclidean algorithm.
  • There are no options.

Examples (12)#

Basic Examples (1)

The gcd of 240 and 46 is 2, and 240·(−9)+46·47=2:

In[1]:=
#cas [extendedGCD] (240, 46)
Out[1]=

gcd and Bézout coefficients

2,[−9, 47]
2, [-9, 47]
Domain
ℕ
Input (natural numbers)
(240, 46)

Checked by Lean’s kernel: gcd and Bézout coefficients

Nat.gcd 240 46 = 2 ∧ 240 * -9 + 46 * 47 = 2
The certificate: 1 checked step
  1. gcd and Bézout coefficients

    Nat.gcd 240 46 = 2 ∧ 240 * -9 + 46 * 47 = 2

    Checked by Lean’s kernel

Scope (3)

Swapping 240 and 46 swaps the coefficients:

In[2]:=
#cas [extendedGCD] (46, 240)
Out[2]=

gcd and Bézout coefficients

2,[47, −9]
2, [47, -9]
Domain
ℕ
Input (natural numbers)
(46, 240)

Checked by Lean’s kernel: gcd and Bézout coefficients

Nat.gcd 46 240 = 2 ∧ 46 * 47 + 240 * -9 = 2
The certificate: 1 checked step
  1. gcd and Bézout coefficients

    Nat.gcd 46 240 = 2 ∧ 46 * 47 + 240 * -9 = 2

    Checked by Lean’s kernel


A zero: gcd(0,5)=5=0·0+5·1:

In[3]:=
#cas [extendedGCD] (0, 5)
Out[3]=

gcd and Bézout coefficients

5,[0, 1]
5, [0, 1]
Domain
ℕ
Input (natural numbers)
(0, 5)

Checked by Lean’s kernel: gcd and Bézout coefficients

Nat.gcd 0 5 = 5 ∧ 0 * 0 + 5 * 1 = 5
The certificate: 1 checked step
  1. gcd and Bézout coefficients

    Nat.gcd 0 5 = 5 ∧ 0 * 0 + 5 * 1 = 5

    Checked by Lean’s kernel


Closed expressions, here 210 and 36:

In[4]:=
#cas [extendedGCD] (2^10, 3^6)
Out[4]=

gcd and Bézout coefficients

1,[−257, 361]
1, [-257, 361]
Domain
ℕ
Input (natural numbers)
(1024, 729)

Checked by Lean’s kernel: gcd and Bézout coefficients

Nat.gcd 1024 729 = 1 ∧ 1024 * -257 + 729 * 361 = 1
The certificate: 1 checked step
  1. gcd and Bézout coefficients

    Nat.gcd 1024 729 = 1 ∧ 1024 * -257 + 729 * 361 = 1

    Checked by Lean’s kernel

Applications (3)

The first example as a theorem, proved by cas. Only the three standard axioms are used:

In[5]:=
theorem bezout_240_46 : Nat.gcd 240 46 = 2 ∧ (240 : ℤ) * (-9) + 46 * 47 = 2 := by
  cas

#print axioms bezout_240_46
Out[5]=

bezout_240_46 depends on the axioms propext, Classical.choice, Quot.sound.


RSA with the primes 61 and 53 has n=3233 and φ(n)=60·52=3120. For the public exponent e=17, the coefficient −367 gives the private exponent d=3120−367=2753, which modularInverse returns directly:

In[6]:=
#cas [extendedGCD] (17, 3120)
Out[6]=

gcd and Bézout coefficients

1,[−367, 2]
1, [-367, 2]
Domain
ℕ
Input (natural numbers)
(17, 3120)

Checked by Lean’s kernel: gcd and Bézout coefficients

Nat.gcd 17 3120 = 1 ∧ 17 * -367 + 3120 * 2 = 1
The certificate: 1 checked step
  1. gcd and Bézout coefficients

    Nat.gcd 17 3120 = 1 ∧ 17 * -367 + 3120 * 2 = 1

    Checked by Lean’s kernel

In[7]:=
#cas (modularInverse 17 3120)
Out[7]=

modular inverse

2753
Domain
ZMod 3120
Input (integer and modulus)
17 mod 3120

Checked by Lean’s kernel: modular inverse

17 * 2753 = 1 ∧ 17⁻¹ = 2753
The certificate: 1 checked step
  1. modular inverse

    17 * 2753 = 1 ∧ 17⁻¹ = 2753

    Checked by Lean’s kernel


Encrypting the message 65 with e and decrypting it with d gives it back:

In[8]:=
#cas (powerMod 65 17 3233)
Out[8]=

modular power

2790
Domain
ℕ
Input (natural numbers)
65^17 mod 3233

Checked by Lean’s kernel: modular power

65 ^ 17 % 3233 = 2790
The certificate: 1 checked step
  1. modular power

    65 ^ 17 % 3233 = 2790

    Checked by Lean’s kernel

In[9]:=
#cas (powerMod 2790 2753 3233)
Out[9]=

modular power

65
Domain
ℕ
Input (natural numbers)
2790^2753 mod 3233

Checked by Lean’s kernel: modular power

2790 ^ 2753 % 3233 = 65
The certificate: 1 checked step
  1. modular power

    2790 ^ 2753 % 3233 = 65

    Checked by Lean’s kernel

Properties and Relations (1)

The quotients of Euclid’s algorithm on 240 and 46 are the terms of the continued fraction of 24046:

In[10]:=
#cas [continuedFraction] (240/46)
Out[10]=

continued fraction

[5; 4, 1, 1, 2]
[5; 4, 1, 1, 2]
Domain
ℚ
Input (rational number)
120/23

Checked by Lean’s kernel: Value of the continued fraction

120 / 23 = 5 + 1 / (4 + 1 / (1 + 1 / (1 + 1 / 2))) ∧ (∀ a ∈ [4, 1, 1, 2], 1 ≤ a) ∧ 2 ≤ 2
The certificate: 1 checked step
  1. Value of the continued fraction

    120 / 23 = 5 + 1 / (4 + 1 / (1 + 1 / (1 + 1 / 2))) ∧ (∀ a ∈ [4, 1, 1, 2], 1 ≤ a) ∧ 2 ≤ 2

    Checked by Lean’s kernel

Possible Issues (2)

The arguments are natural numbers. Since gcd(−a,b)=gcd(a,b), the coefficients for −a are those for a with the sign of s reversed:

In[11]:=
#cas [extendedGCD] (-240, 46)
Out[11]=

Error

extendedGCD: expected a natural number

The mode takes exactly two numbers:

In[12]:=
#cas [extendedGCD] (12, 18, 30)
Out[12]=

Error

extendedGCD: expected `(a, b)`

Neat Examples (2)

Consecutive Fibonacci numbers are the slowest case of Euclid’s algorithm, and their coefficients are Fibonacci numbers again, up to sign:

In[13]:=
#cas [extendedGCD] (89, 55)
Out[13]=

gcd and Bézout coefficients

1,[−21, 34]
1, [-21, 34]
Domain
ℕ
Input (natural numbers)
(89, 55)

Checked by Lean’s kernel: gcd and Bézout coefficients

Nat.gcd 89 55 = 1 ∧ 89 * -21 + 55 * 34 = 1
The certificate: 1 checked step
  1. gcd and Bézout coefficients

    Nat.gcd 89 55 = 1 ∧ 89 * -21 + 55 * 34 = 1

    Checked by Lean’s kernel


For the Fermat prime 65537=216+1 and 232, the identity 65537·65535=232−1 gives the coefficients:

In[14]:=
#cas [extendedGCD] (65537, 2^32)
Out[14]=

gcd and Bézout coefficients

1,[−65535, 1]
1, [-65535, 1]
Domain
ℕ
Input (natural numbers)
(65537, 4294967296)

Checked by Lean’s kernel: gcd and Bézout coefficients

Nat.gcd 65537 4294967296 = 1 ∧ 65537 * -65535 + 4294967296 * 1 = 1
The certificate: 1 checked step
  1. gcd and Bézout coefficients

    Nat.gcd 65537 4294967296 = 1 ∧ 65537 * -65535 + 4294967296 * 1 = 1

    Checked by Lean’s kernel

See Also

chineseRemainder · modularInverse · powerMod · continuedFraction · polynomialGCD