Mode of #cas

chineseRemainder

All solutions of a system of congruences x≡ri(modmi), as one residue modulo the lcm, or a proof that there are none.

#cas [chineseRemainder] ([r₁, …, rₖ], [m₁, …, mₖ])
solves the congruences x ≡ rᵢ (mod mᵢ) and proves that the printed residue modulo the lcm of the moduli gives all the solutions, or that there are none.

chineseRemainder solves simultaneous congruences. You give it the residues first and then the moduli, and the moduli need not be coprime. The theorem characterizes every solution: the printed x is a solution, and every solution is congruent to x modulo the lcm M of the moduli. When two of the congruences contradict each other, the theorem says that there is no solution, and the report names the two.

Details and Options

  • The argument is a pair of lists of natural numbers, ([r₁, …, rₖ], [m₁, …, mₖ]), with as many residues as moduli and at least one of each. The entries are literals or closed integer expressions.
  • The moduli are positive, and they need not be coprime. A residue may exceed its modulus.
  • The theorem is x < M ∧ ∀ y : ℕ, (∀ p ∈ [(m₁, r₁), …], y ≡ p.2 [MOD p.1]) ↔ y ≡ x [MOD M] with Mathlib’s Nat.ModEq, where M=lcm(m1,…,mk). The list in the theorem pairs each modulus with its residue, modulus first. The kernel decides x<M, the lcm and the congruences of x, and CAS.NumberTheory.modEq_iff_of_lcm gives the equivalence from them.
  • Two congruences x≡ri(modmi) and x≡rj(modmj) have a common solution exactly when ri≡rj(modgcd(mi,mj)). When a pair fails this test, the theorem is ¬∃ y : ℕ, ∀ p ∈ [(m₁, r₁), …], y ≡ p.2 [MOD p.1] (CAS.NumberTheory.not_exists_of_incompatible), and the report prints the pair.
  • The solution comes from merging the congruences one at a time with Bézout coefficients, as extendedGCD computes them. There are no options.

Examples (12)#

Basic Examples (1)

The problem of the Sunzi Suanjing: a number leaves the remainders 2, 3 and 2 on division by 3, 5 and 7:

In[1]:=
#cas [chineseRemainder] ([2, 3, 2], [3, 5, 7])
Out[1]=

all solutions

23 mod 105
Domain
ℕ
Input (natural numbers)
x ≡ 2 mod 3, x ≡ 3 mod 5, x ≡ 2 mod 7

Checked by Lean’s kernel: all solutions

23 < 105 ∧ ∀ (y : ℕ), (∀ p ∈ [(3, 2), (5, 3), (7, 2)], y ≡ p.2 [MOD p.1]) ↔ y ≡ 23 [MOD 105]
The certificate: 1 checked step
  1. all solutions

    23 < 105 ∧ ∀ (y : ℕ), (∀ p ∈ [(3, 2), (5, 3), (7, 2)], y ≡ p.2 [MOD p.1]) ↔ y ≡ 23 [MOD 105]

    Checked by Lean’s kernel

Scope (4)

The moduli need not be coprime. These two have the common factor 2, and the solutions repeat modulo lcm(4,6)=12:

In[2]:=
#cas [chineseRemainder] ([2, 4], [4, 6])
Out[2]=

all solutions

10 mod 12
Domain
ℕ
Input (natural numbers)
x ≡ 2 mod 4, x ≡ 4 mod 6

Checked by Lean’s kernel: all solutions

10 < 12 ∧ ∀ (y : ℕ), (∀ p ∈ [(4, 2), (6, 4)], y ≡ p.2 [MOD p.1]) ↔ y ≡ 10 [MOD 12]
The certificate: 1 checked step
  1. all solutions

    10 < 12 ∧ ∀ (y : ℕ), (∀ p ∈ [(4, 2), (6, 4)], y ≡ p.2 [MOD p.1]) ↔ y ≡ 10 [MOD 12]

    Checked by Lean’s kernel


When two congruences disagree modulo the gcd of their moduli, there is no solution. Here x≡1(mod4) makes x odd and x≡2(mod6) makes it even:

In[3]:=
#cas [chineseRemainder] ([1, 2], [4, 6])
Out[3]=

no solution

none: x ≡ 1 mod 4 and x ≡ 2 mod 6 disagree modulo 2
Domain
ℕ
Input (natural numbers)
x ≡ 1 mod 4, x ≡ 2 mod 6

Checked by Lean’s kernel: no solution

¬∃ y, ∀ p ∈ [(4, 1), (6, 2)], y ≡ p.2 [MOD p.1]
The certificate: 1 checked step
  1. no solution

    ¬∃ y, ∀ p ∈ [(4, 1), (6, 2)], y ≡ p.2 [MOD p.1]

    Checked by Lean’s kernel


A residue may exceed its modulus:

In[4]:=
#cas [chineseRemainder] ([5], [3])
Out[4]=

all solutions

2 mod 3
Domain
ℕ
Input (natural numbers)
x ≡ 5 mod 3

Checked by Lean’s kernel: all solutions

2 < 3 ∧ ∀ (y : ℕ), (∀ p ∈ [(3, 5)], y ≡ p.2 [MOD p.1]) ↔ y ≡ 2 [MOD 3]
The certificate: 1 checked step
  1. all solutions

    2 < 3 ∧ ∀ (y : ℕ), (∀ p ∈ [(3, 5)], y ≡ p.2 [MOD p.1]) ↔ y ≡ 2 [MOD 3]

    Checked by Lean’s kernel


Large moduli, the two primes 109+7 and 109+9:

In[5]:=
#cas [chineseRemainder] ([3, 7], [1000000007, 1000000009])
Out[5]=

all solutions

1000000014000000052 mod 1000000016000000063
Domain
ℕ
Input (natural numbers)
x ≡ 3 mod 1000000007, x ≡ 7 mod 1000000009

Checked by Lean’s kernel: all solutions

1000000014000000052 < 1000000016000000063 ∧
  ∀ (y : ℕ),
    (∀ p ∈ [(1000000007, 3), (1000000009, 7)], y ≡ p.2 [MOD p.1]) ↔
      y ≡ 1000000014000000052 [MOD 1000000016000000063]
The certificate: 1 checked step
  1. all solutions

    1000000014000000052 < 1000000016000000063 ∧
      ∀ (y : ℕ),
        (∀ p ∈ [(1000000007, 3), (1000000009, 7)], y ≡ p.2 [MOD p.1]) ↔
          y ≡ 1000000014000000052 [MOD 1000000016000000063]

    Checked by Lean’s kernel

Applications (2)

A proof can take the printed theorem as a hypothesis and use the equivalence, here for the next solution 128=23+105. This proof uses no axioms at all:

In[6]:=
theorem sunzi_next
    (h : 23 < 105 ∧ ∀ y : ℕ, (∀ p ∈ ([(3, 2), (5, 3), (7, 2)] : List (ℕ × ℕ)),
      y ≡ p.2 [MOD p.1]) ↔ y ≡ 23 [MOD 105]) :
    ∀ p ∈ ([(3, 2), (5, 3), (7, 2)] : List (ℕ × ℕ)), 128 ≡ p.2 [MOD p.1] :=
  (h.2 128).2 (by decide)

#print axioms sunzi_next
Out[6]=

sunzi_next depends on no axioms.


To decrypt the RSA cipher 2790 with the key d=2753 for n=61·53, I can raise it to the power dmod60=53 modulo 61 and to dmod52=49 modulo 53, and put the two residues together into the message 65:

In[7]:=
#cas (powerMod 2790 53 61)
Out[7]=

modular power

4
Domain
ℕ
Input (natural numbers)
2790^53 mod 61

Checked by Lean’s kernel: modular power

2790 ^ 53 % 61 = 4
The certificate: 1 checked step
  1. modular power

    2790 ^ 53 % 61 = 4

    Checked by Lean’s kernel

In[8]:=
#cas (powerMod 2790 49 53)
Out[8]=

modular power

12
Domain
ℕ
Input (natural numbers)
2790^49 mod 53

Checked by Lean’s kernel: modular power

2790 ^ 49 % 53 = 12
The certificate: 1 checked step
  1. modular power

    2790 ^ 49 % 53 = 12

    Checked by Lean’s kernel

In[9]:=
#cas [chineseRemainder] ([4, 12], [61, 53])
Out[9]=

all solutions

65 mod 3233
Domain
ℕ
Input (natural numbers)
x ≡ 4 mod 61, x ≡ 12 mod 53

Checked by Lean’s kernel: all solutions

65 < 3233 ∧ ∀ (y : ℕ), (∀ p ∈ [(61, 4), (53, 12)], y ≡ p.2 [MOD p.1]) ↔ y ≡ 65 [MOD 3233]
The certificate: 1 checked step
  1. all solutions

    65 < 3233 ∧ ∀ (y : ℕ), (∀ p ∈ [(61, 4), (53, 12)], y ≡ p.2 [MOD p.1]) ↔ y ≡ 65 [MOD 3233]

    Checked by Lean’s kernel

Properties and Relations (1)

The multiples of 3 that are 1 modulo 11 are the 3k with k≡3−1≡4(mod11), so the solution is 3·4=12 modulo 33. modularInverse gives the inverse:

In[10]:=
#cas [chineseRemainder] ([0, 1], [3, 11])
Out[10]=

all solutions

12 mod 33
Domain
ℕ
Input (natural numbers)
x ≡ 0 mod 3, x ≡ 1 mod 11

Checked by Lean’s kernel: all solutions

12 < 33 ∧ ∀ (y : ℕ), (∀ p ∈ [(3, 0), (11, 1)], y ≡ p.2 [MOD p.1]) ↔ y ≡ 12 [MOD 33]
The certificate: 1 checked step
  1. all solutions

    12 < 33 ∧ ∀ (y : ℕ), (∀ p ∈ [(3, 0), (11, 1)], y ≡ p.2 [MOD p.1]) ↔ y ≡ 12 [MOD 33]

    Checked by Lean’s kernel

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

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

Possible Issues (3)

Each modulus needs its residue, and the residues come first:

In[12]:=
#cas [chineseRemainder] ([2, 3], [3, 5, 7])
Out[12]=

Error

chineseRemainder: expected as many residues as moduli

The moduli must be positive:

In[13]:=
#cas [chineseRemainder] ([1], [0])
Out[13]=

Error

chineseRemainder: the moduli must be positive

The residues are natural numbers, so −1 modulo 3 is written 2:

In[14]:=
#cas [chineseRemainder] ([-1], [3])
Out[14]=

Error

chineseRemainder: expected a natural number

Neat Examples (1)

Ibn al-Haytham asked for a number that leaves the remainder 1 on division by 2, 3, 4, 5 and 6, and is divisible by 7:

In[15]:=
#cas [chineseRemainder] ([1, 1, 1, 1, 1, 0], [2, 3, 4, 5, 6, 7])
Out[15]=

all solutions

301 mod 420
Domain
ℕ
Input (natural numbers)
x ≡ 1 mod 2, x ≡ 1 mod 3, x ≡ 1 mod 4, x ≡ 1 mod 5, x ≡ 1 mod 6, x ≡ 0 mod 7

Checked by Lean’s kernel: all solutions

301 < 420 ∧
  ∀ (y : ℕ), (∀ p ∈ [(2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 0)], y ≡ p.2 [MOD p.1]) ↔ y ≡ 301 [MOD 420]
The certificate: 1 checked step
  1. all solutions

    301 < 420 ∧
      ∀ (y : ℕ), (∀ p ∈ [(2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 0)], y ≡ p.2 [MOD p.1]) ↔ y ≡ 301 [MOD 420]

    Checked by Lean’s kernel

See Also

extendedGCD · modularInverse · powerMod