Mode of #cas
extendedGCD
The gcd of two natural numbers with Bézout coefficients and , and a theorem that .
#cas [extendedGCD] (a, b)Whenever I need to invert a number modulo another, solve a linear equation 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: is Mathlib’s Nat.gcd a b, and in .
Examples (12)#
Basic Examples (1)
The gcd of 240 and 46 is 2, and :
#cas [extendedGCD] (240, 46)
gcd and Bézout coefficients
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
gcd and Bézout coefficients
Nat.gcd 240 46 = 2 ∧ 240 * -9 + 46 * 47 = 2
Scope (3)
Swapping 240 and 46 swaps the coefficients:
#cas [extendedGCD] (46, 240)
gcd and Bézout coefficients
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
gcd and Bézout coefficients
Nat.gcd 46 240 = 2 ∧ 46 * 47 + 240 * -9 = 2
A zero: :
#cas [extendedGCD] (0, 5)
gcd and Bézout coefficients
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
gcd and Bézout coefficients
Nat.gcd 0 5 = 5 ∧ 0 * 0 + 5 * 1 = 5
Closed expressions, here and :
#cas [extendedGCD] (2^10, 3^6)
gcd and Bézout coefficients
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
gcd and Bézout coefficients
Nat.gcd 1024 729 = 1 ∧ 1024 * -257 + 729 * 361 = 1
Applications (3)
The first example as a theorem, proved by cas. Only the three standard axioms are used:
theorem bezout_240_46 : Nat.gcd 240 46 = 2 ∧ (240 : ℤ) * (-9) + 46 * 47 = 2 := by cas #print axioms bezout_240_46
bezout_240_46 depends on the axioms propext, Classical.choice, Quot.sound.
RSA with the primes 61 and 53 has and . For the public exponent , the coefficient gives the private exponent , which modularInverse returns directly:
#cas [extendedGCD] (17, 3120)
gcd and Bézout coefficients
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
gcd and Bézout coefficients
Nat.gcd 17 3120 = 1 ∧ 17 * -367 + 3120 * 2 = 1
#cas (modularInverse 17 3120)
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
modular inverse
17 * 2753 = 1 ∧ 17⁻¹ = 2753
Encrypting the message 65 with and decrypting it with gives it back:
#cas (powerMod 65 17 3233)
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
modular power
65 ^ 17 % 3233 = 2790
#cas (powerMod 2790 2753 3233)
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
modular power
2790 ^ 2753 % 3233 = 65
Properties and Relations (1)
The quotients of Euclid’s algorithm on 240 and 46 are the terms of the continued fraction of :
#cas [continuedFraction] (240/46)
continued fraction
[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
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
Possible Issues (2)
The arguments are natural numbers. Since , the coefficients for are those for with the sign of reversed:
#cas [extendedGCD] (-240, 46)
Error
extendedGCD: expected a natural number
The mode takes exactly two numbers:
#cas [extendedGCD] (12, 18, 30)
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:
#cas [extendedGCD] (89, 55)
gcd and Bézout coefficients
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
gcd and Bézout coefficients
Nat.gcd 89 55 = 1 ∧ 89 * -21 + 55 * 34 = 1
For the Fermat prime and , the identity gives the coefficients:
#cas [extendedGCD] (65537, 2^32)
gcd and Bézout coefficients
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
gcd and Bézout coefficients
Nat.gcd 65537 4294967296 = 1 ∧ 65537 * -65535 + 4294967296 * 1 = 1
See Also
chineseRemainder · modularInverse · powerMod · continuedFraction · polynomialGCD
