Function of #cas

modularInverse

The inverse of an integer modulo m≥2 in ZMod m, or a proof that there is none.

#cas (modularInverse a m)
gives the inverse of the integer a modulo m ≥ 2 and proves it in ZMod m, or proves that there is none.

modularInverse finds the inverse of a modulo m, the r with ar≡1(modm). We get it from the extended Euclidean algorithm, and the theorem is about ZMod m, where the kernel checks that the product of a and the answer is 1. When a and m have a common factor there is no inverse, and the theorem you get says so instead.

Details and Options

  • a is an integer and m a natural number with m≥2, literals or closed integer expressions.
  • When gcd(a,m)=1 the theorem is (a : ZMod m) * r = 1 ∧ (a : ZMod m)⁻¹ = r with 0≤r<m, by kernel evaluation in ZMod m and ZMod.inv_eq_of_mul_eq_one.
  • Otherwise the theorem is Nat.gcd |a| m = g ∧ ¬ IsUnit (a : ZMod m) with g>1, by ZMod.isUnit_iff_coprime, and the report prints the gcd.
  • The inverse comes from the extended Euclidean algorithm. extendedGCD prints its Bézout coefficients.
  • In a proof, cas proves (a : ZMod m)⁻¹ = b for a unit a with the certificate of the call.
  • The call is evaluated when it is the whole input of #cas.
  • There are no options.

Examples (7)#

Basic Examples (1)

3·4=12≡1(mod11), and the theorem states both the product and the inverse:

In[1]:=
#cas (modularInverse 3 11)
Out[1]=

modular inverse

4
Domain
ZMod 11
Input (integer and modulus)
3 mod 11

Checked by Lean’s kernel: modular inverse

3 * 4 = 1 ∧ 3⁻¹ = 4
The certificate: 1 checked step
  1. modular inverse

    3 * 4 = 1 ∧ 3⁻¹ = 4

    Checked by Lean’s kernel

Scope (3)

The integer may be negative, and (−3)·2=−6≡1(mod7):

In[2]:=
#cas (modularInverse (-3) 7)
Out[2]=

modular inverse

2
Domain
ZMod 7
Input (integer and modulus)
-3 mod 7

Checked by Lean’s kernel: modular inverse

-3 * 2 = 1 ∧ (-3)⁻¹ = 2
The certificate: 1 checked step
  1. modular inverse

    -3 * 2 = 1 ∧ (-3)⁻¹ = 2

    Checked by Lean’s kernel


The private exponent of a small RSA key, the inverse of e=17 modulo φ(3233)=3120:

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

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


When gcd(a,m)>1 there is no inverse, and the theorem says so:

In[4]:=
#cas (modularInverse 6 9)
Out[4]=

no inverse

none: gcd = 3
Domain
ZMod 9
Input (integer and modulus)
6 mod 9

Checked by Lean’s kernel: no inverse

Nat.gcd 6 9 = 3 ∧ ¬IsUnit 6
The certificate: 1 checked step
  1. no inverse

    Nat.gcd 6 9 = 3 ∧ ¬IsUnit 6

    Checked by Lean’s kernel

Applications (1)

In a proof, cas proves the inverse with the certificate of the call:

In[5]:=
theorem inverse_three_mod_eleven : (3 : ZMod 11)⁻¹ = 4 := by
  cas

#print axioms inverse_three_mod_eleven
Out[5]=

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

Properties and Relations (1)

extendedGCD prints the Bézout coefficients, 17·(−367)+3120·2=1. Modulo 3120 the coefficient −367 is 3120−367=2753, the inverse above:

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

Possible Issues (1)

ZMod 1 is the zero ring, where every element is its own inverse, and the call asks for m≥2:

In[7]:=
#cas (modularInverse 3 1)
Out[7]=

Error

modularInverse: the modulus must be at least 2

See Also

extendedGCD · powerMod · chineseRemainder · jacobiSymbol