Function of #cas

powerMod

The remainder of ab modulo m by repeated squaring, for exponents with hundreds of digits, with a theorem in ℕ.

#cas (powerMod a b m)
gives the remainder of a^b modulo m for natural numbers a, b and a positive modulus m, and proves a ^ b % m = r.

powerMod computes ab modulo m without ever building the integer ab, which for a large exponent would be far too big to write down. The proof is repeated squaring, so its size grows with the number of digits of the exponent and not with the exponent itself. Exponents with hundreds of digits are fine.

Details and Options

  • a, b and m are natural numbers, literals or closed integer expressions, with m≥1.
  • The theorem is a ^ b % m = r in ℕ, with r<m. The proof is Mathlib’s Mathlib.Meta.NormNum.evalNatPowMod, repeated squaring.
  • The call is evaluated when it is the whole input of #cas.
  • There are no options.

Examples (7)#

Basic Examples (1)

The remainder of 31000 modulo 7:

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

modular power

4
Domain
ℕ
Input (natural numbers)
3^1000 mod 7

Checked by Lean’s kernel: modular power

3 ^ 1000 % 7 = 4
The certificate: 1 checked step
  1. modular power

    3 ^ 1000 % 7 = 4

    Checked by Lean’s kernel

Scope (1)

An exponent with 27 digits, modulo the prime 109+7, with the theorem that the kernel checked:

In[2]:=
#cas (powerMod 2 123456789123456789123456789 1000000007)
Out[2]=

modular power

612059118
Domain
ℕ
Input (natural numbers)
2^123456789123456789123456789 mod 1000000007

Checked by Lean’s kernel: modular power

2 ^ 123456789123456789123456789 % 1000000007 = 612059118
The certificate: 1 checked step
  1. modular power

    2 ^ 123456789123456789123456789 % 1000000007 = 612059118

    Checked by Lean’s kernel

Applications (1)

In a proof, cas proves the equation, with propext as its only axiom:

In[3]:=
theorem three_pow_mod_seven : 3 ^ 1000 % 7 = 4 := by
  cas

#print axioms three_pow_mod_seven
Out[3]=

three_pow_mod_seven depends on the axioms propext.

Properties and Relations (2)

Fermat’s little theorem gives ap−1≡1(modp) for a prime p that does not divide a. Here 26≡1(mod7) and 100=6·16+4, so 2100≡24=16≡2:

In[4]:=
#cas (powerMod 2 100 7)
Out[4]=

modular power

2
Domain
ℕ
Input (natural numbers)
2^100 mod 7

Checked by Lean’s kernel: modular power

2 ^ 100 % 7 = 2
The certificate: 1 checked step
  1. modular power

    2 ^ 100 % 7 = 2

    Checked by Lean’s kernel


For an odd prime p, Euler’s criterion a(p−1)/2≡(a/p)(modp) relates the remainder to the symbol of jacobiSymbol. For a=2 and p=7 the power is 23=8≡1, and the symbol is 1:

In[5]:=
#cas (jacobiSymbol 2 7)
Out[5]=

Jacobi symbol

1
Domain
ℤ × ℕ
Input (an integer and an odd natural number)
(2 / 7)

Checked by Lean’s kernel: Jacobi symbol

jacobiSym 2 7 = 1
The certificate: 1 checked step
  1. Jacobi symbol

    jacobiSym 2 7 = 1

    Checked by Lean’s kernel

Possible Issues (2)

Every natural number is 0 modulo 1, and Mathlib’s a % 0 = a. The call asks for m≥1:

In[6]:=
#cas (powerMod 2 10 0)
Out[6]=

Error

powerMod: the modulus must be positive

The call takes three arguments:

In[7]:=
#cas (powerMod 2 10)
Out[7]=

Error

powerMod takes 3 arguments, as in `powerMod a b m`; the call has 2

See Also

modularInverse · jacobiSymbol · extendedGCD · chineseRemainder · integerDigits