1-- The two-modulus Chinese remainder theorem over quotient integers2-- Part of Cumulus, the Grace17 mathematics library.3-- Copyright (c) 2026 Grant Passmore.4-- Released under the Apache License 2.0. See LICENSE in the repository root.5-- Authors: Grant Passmore67module Grace.Cumulus.NumberTheory.ChineseRemainder8import Coprime910theorem IntegerDivides.coprime_product (m n z : Integer)11requires coprime : IntegerCoprime(m, n)12requiresleft : IntegerDivides(m, z)13requiresright : IntegerDivides(n, z)14shows IntegerDivides(m * n, z)15proof16obtain⟨q, qInteger, equation⟩:=left17have symmetric := IntegerCoprime.symmetric with*18rw← equation atright19have quotient := IntegerCoprime.cancel with*20obtain⟨k, kInteger, factor⟩:= quotient21witness k22rw [← equation, ← factor]23ring2425-- If u*m+v*n=1, then a*v*n+b*u*m has the prescribed residues.26def chineseCombination (m n a b u v : Integer) : Integer := a * v * n + b * u * m2728theorem chineseCombination.correct (m n a b u v : Integer)29requires identity : u * m + v * n = integerOne30shows IntegerCongruent(m, chineseCombination(m, n, a, b, u, v), a) ∧31 IntegerCongruent(n, chineseCombination(m, n, a, b, u, v), b)32proof33constructor34witness u * (b - a)35unfold chineseCombination36linear_combination -a * identity37witness v * (a - b)38unfold chineseCombination39linear_combination -b * identity4041theorem ChineseRemainder.exists (m n a b : Integer)42requires coprime : IntegerCoprime(m, n)43shows∃ x, Integer(x) ∧ IntegerCongruent(m, x, a) ∧ IntegerCongruent(n, x, b)44proof45obtain⟨u, uInteger, v, vInteger, identity⟩:= IntegerCoprime.bezout at m, n with*46have integer := chineseCombination.type at m, n, a, b, u, v with*47have residues := chineseCombination.correct at m, n, a, b, u, v with*48witness chineseCombination(m, n, a, b, u, v)49exact residues5051theorem ChineseRemainder.unique (m n a b x y : Integer)52requires coprime : IntegerCoprime(m, n)53requires xm : IntegerCongruent(m, x, a)54requires xn : IntegerCongruent(n, x, b)55requires ym : IntegerCongruent(m, y, a)56requires yn : IntegerCongruent(n, y, b)57shows IntegerCongruent(m * n, x, y)58proof59have reverseM := IntegerCongruent.symmetric at m, y, a with*60have reverseN := IntegerCongruent.symmetric at n, y, b with*61have sameM := IntegerCongruent.transitive at m, x, a, y with*62have sameN := IntegerCongruent.transitive at n, x, b, y with*63unfold IntegerCongruent at sameM64unfold IntegerCongruent at sameN65unfold IntegerCongruent66exact IntegerDivides.coprime_product with*67