Mode of #cas
chineseRemainder
All solutions of a system of congruences , as one residue modulo the lcm, or a proof that there are none.
#cas [chineseRemainder] ([r₁, …, rₖ], [m₁, …, mₖ])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 is a solution, and every solution is congruent to modulo the lcm 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.
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:
#cas [chineseRemainder] ([2, 3, 2], [3, 5, 7])
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
all solutions
23 < 105 ∧ ∀ (y : ℕ), (∀ p ∈ [(3, 2), (5, 3), (7, 2)], y ≡ p.2 [MOD p.1]) ↔ y ≡ 23 [MOD 105]
Scope (4)
The moduli need not be coprime. These two have the common factor 2, and the solutions repeat modulo :
#cas [chineseRemainder] ([2, 4], [4, 6])
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
all solutions
10 < 12 ∧ ∀ (y : ℕ), (∀ p ∈ [(4, 2), (6, 4)], y ≡ p.2 [MOD p.1]) ↔ y ≡ 10 [MOD 12]
When two congruences disagree modulo the gcd of their moduli, there is no solution. Here makes odd and makes it even:
#cas [chineseRemainder] ([1, 2], [4, 6])
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
no solution
¬∃ y, ∀ p ∈ [(4, 1), (6, 2)], y ≡ p.2 [MOD p.1]
A residue may exceed its modulus:
#cas [chineseRemainder] ([5], [3])
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
all solutions
2 < 3 ∧ ∀ (y : ℕ), (∀ p ∈ [(3, 5)], y ≡ p.2 [MOD p.1]) ↔ y ≡ 2 [MOD 3]
Large moduli, the two primes and :
#cas [chineseRemainder] ([3, 7], [1000000007, 1000000009])
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
all solutions
1000000014000000052 < 1000000016000000063 ∧ ∀ (y : ℕ), (∀ p ∈ [(1000000007, 3), (1000000009, 7)], y ≡ p.2 [MOD p.1]) ↔ y ≡ 1000000014000000052 [MOD 1000000016000000063]
Applications (2)
A proof can take the printed theorem as a hypothesis and use the equivalence, here for the next solution . This proof uses no axioms at all:
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
sunzi_next depends on no axioms.
To decrypt the RSA cipher 2790 with the key for , I can raise it to the power modulo 61 and to modulo 53, and put the two residues together into the message 65:
#cas (powerMod 2790 53 61)
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
modular power
2790 ^ 53 % 61 = 4
#cas (powerMod 2790 49 53)
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
modular power
2790 ^ 49 % 53 = 12
#cas [chineseRemainder] ([4, 12], [61, 53])
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
all solutions
65 < 3233 ∧ ∀ (y : ℕ), (∀ p ∈ [(61, 4), (53, 12)], y ≡ p.2 [MOD p.1]) ↔ y ≡ 65 [MOD 3233]
Properties and Relations (1)
The multiples of 3 that are 1 modulo 11 are the with , so the solution is modulo 33. modularInverse gives the inverse:
#cas [chineseRemainder] ([0, 1], [3, 11])
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
all solutions
12 < 33 ∧ ∀ (y : ℕ), (∀ p ∈ [(3, 0), (11, 1)], y ≡ p.2 [MOD p.1]) ↔ y ≡ 12 [MOD 33]
#cas (modularInverse 3 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
modular inverse
3 * 4 = 1 ∧ 3⁻¹ = 4
Possible Issues (3)
Each modulus needs its residue, and the residues come first:
#cas [chineseRemainder] ([2, 3], [3, 5, 7])
Error
chineseRemainder: expected as many residues as moduli
The moduli must be positive:
#cas [chineseRemainder] ([1], [0])
Error
chineseRemainder: the moduli must be positive
The residues are natural numbers, so modulo 3 is written 2:
#cas [chineseRemainder] ([-1], [3])
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:
#cas [chineseRemainder] ([1, 1, 1, 1, 1, 0], [2, 3, 4, 5, 6, 7])
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
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]
