第 10 章
同余、欧几里得算法与中国剩余定理
从带余除法建立同余的语言,证明「同余等价于余数相等」;用欧几里得算法快速求最大公因数,再用扩展算法得到 Bézout 等式,从而判定并计算模逆元;最后证明中国剩余定理并给出构造解法。
Congruences, Euclid's algorithm and the Chinese remainder theorem
The division algorithm supplies the language of congruences, and congruence is equivalent to equality of remainders. Euclid's algorithm computes gcds quickly; the extended version yields Bézout identities, which decide and compute modular inverses. The lecture closes with a proof and a construction for the Chinese remainder theorem.
篇内目录
- What this lecture is for
- Timeline
- Congruences and division with remainder
- Definition
- The division algorithm
- Congruence equals equality of remainders
- The clock picture
- Arithmetic modulo
- Addition and multiplication
- Three algebraic laws
- Not every rule survives
- Multiplicative inverses
- Definition and examples
- The key question
- Greatest common divisors
- Euclid's algorithm
- Lemma 1: subtracting one copy preserves the gcd
- Lemma 2: subtract all at once
- A numerical example
- The extended algorithm and Bézout's identity
- The identity is a certificate
- Inverses: existence and computation
- Why a prime modulus guarantees inverses
- Non-prime moduli
- Examples
- The Chinese remainder theorem
- Starting from a puzzle
- Statement
- Proof
- A constructive solution
- Example
- Application: fountain codes
- Zero-knowledge proofs: the card demonstration
- What this lecture establishes
- Comprehension check
- Relation to neighbouring lectures
本讲要解决的问题
「偶数加奇数得奇数」是小学知识,但它其实是模 的算术:只看除以 的余数,就能预测和的奇偶性。本讲把这一观察推广到任意模数,回答三个问题:
- 哪些运算规则可以照搬到模运算里?
- 什么时候可以「除以」一个数,也就是求乘法逆元?
- 多个模数下的余数条件能否同时满足?能的话,解是否唯一、如何求?
第三个问题就是中国剩余定理,它会在编码与密码学中反复出现。
时间轴
| 时间 | 内容 |
|---|---|
| 0:00 | 从奇偶性到模运算 |
| 0:57 | 同余的定义 |
| 2:16 | 带余除法 |
| 4:21 | 同余与余数相等等价的证明 |
| 9:04 | 时钟图像 |
| 10:25 | 模运算的加法与乘法 |
| 16:25 | 结合律、交换律、分配律 |
| 18:13 | 零知识证明的卡片演示 |
| 23:39 | 乘法逆元;关键问题 |
| 27:50 | 最大公因数 |
| 29:54 | 欧几里得算法的引理 |
| 42:06 | 数值例子 |
| 44:06 | 扩展欧几里得与 Bézout 等式 |
| 49:37 | 逆元的存在性与计算 |
| 54:26 | 中国剩余定理 |
| 1:11:08 | 构造解法 |
| 1:14:22 | 应用:喷泉码 |
同余与带余除法
定义
设 为正整数。称 与 模 同余,记作
当且仅当 是 的整数倍,即 。
带余除法
对任意整数 与正整数 ,存在唯一的整数 与 ,使得
是商, 是余数。唯一性来自区间 的长度恰为 :若有两组解,相减会得到一个绝对值小于 的 的倍数,只能是 。
同余等价于余数相等
命题。
证明()。 设 、,两者余数同为 。则
是 的倍数,故同余。
证明()。 设 、,且 ,即 是 的倍数。另一方面
因此 也是 的倍数。但 ,所以
在这个开区间内唯一的 的倍数是 ,故 。
这个命题把「同余」这个抽象关系翻译成了「做除法看余数」这个具体操作,后面所有算法都建立在这一翻译上。
时钟图像
把 均匀排在钟面上。整数 对应从 出发走 步后落到的位置。
例如模 : 落在位置 ; 绕一整圈后再走 步,也落在位置 ,所以 。模运算不区分绕了几圈,只关心停在哪里。
模运算
加法与乘法
若 、,则
直观做法:先按普通整数算,最后减去若干个 ,把结果拉回 。
例(模 ):、,于是
核对: ✓。
为什么可以「先算再取余」? 因为每个整数都可以写成 ,而 这一部分在模 下等于 。剩下起作用的只有余数 。
三条代数律
对任意 与模 :
- 结合律:,。
- 交换律:,。
- 分配律:。
这三条与普通整数运算完全一致:因为两边相减都是 的倍数。模运算因此构成一个环——这正是下一讲群论的起点。
不是所有规则都能照搬
可以相加、相乘,但不能随意约去。例如模 时
所以 ,却推不出 。约去因子需要该因子可逆,这正是下一节的主题。
乘法逆元
定义与例子
称 在模 下有乘法逆元,若存在 使
模 时,,所以 的逆元是 , 的逆元是 。同理 ,二者互为逆元。
有了逆元,「除以 」就可以实现为「乘以 」:若已知 ,两边乘 得 。这正是密码学里「加密后还能解密」的原型——乘以某个数把信息打乱,再乘以它的逆元还原。
关键问题
模 时,每个非零元素都有逆元吗? 逐个查表可以确认: 都有。但当模数很大时,不可能写出整张乘法表。于是需要理论回答两个问题:
- 什么时候存在逆元?
- 不用查表,怎样算出逆元?
答案要用到最大公因数与欧几里得算法。
最大公因数
是同时整除 与 的最大正整数。若没有更大的公因数,它就是 。
暴力算法是从 试到 ,对大整数完全不可行。需要一个快速算法。
欧几里得算法
引理 1:减去一份不改变最大公因数
设 ,则
证明。 比较两边的公因数集合。
若 且 ,则 ,所以 也是 与 的公因数。
反之,若 且 ,则 也被 整除,所以 也是 与 的公因数。
两边的公因数集合完全相同,最大者自然相同。
逻辑上要注意:不是「 是某个下界」,而是两边的公因数逐一对应。集合相等比大小相等更强,也更干净。
引理 2:一次减到底
设 (带余除法)。反复使用引理 1:
所以
一步除法代替了 次减法,这是算法快的原因:要么 、 接近,一次减法就让较大的数减半;要么差距很大,一次取余就让较大的数降到 以下。两种情况都让数值迅速变小。
数值例子
计算 :
最后一个非零余数是 ,故 。,因为 被任何正整数整除。
扩展欧几里得与 Bézout 等式
普通算法只保留余数;扩展算法还记录每个余数是如何由原始两数拼出来的。
从上面的例子倒推:
于是找到整数 、,使
一般结论(Bézout)。 对任意整数 ,存在整数 使
这个等式是一张证书
假设有人只告诉你答案是 。你怎么确认它是最大公因数,而不只是某个公因数?
- 公因数性: 且 ,可以直接验算。
- 最大性:设 同时整除 与 ,则由 ,两边除以 ,左边是整数,右边是两个整数之和,也是整数,所以 ,即 。
一个等式同时证明了「是公因数」和「没有更大的公因数」。这就是 Bézout 等式的价值:它把「最大」这个看似需要比较的量,变成了一个可验证的整除关系。
逆元:存在性与计算
为什么模素数时一定有逆元
设 为素数,。因为 只有 与 两个正因数,而 不是 的倍数,所以
由 Bézout,存在 使
两边模 , 消失:
所以 就是 的逆元。这不仅证明了存在性,还给出了求法:对 与 跑扩展欧几里得算法,读出 。
非素数模数
条件其实是 ,与 是否素数无关。例如模 时,,故 有逆元,且 ,逆元就是 自己。而 , 没有逆元。
例子
模 求 的逆元:,扩展算法给出 ,故逆元是 。
模 求 的逆元:,,故逆元是 。
中国剩余定理
从谜题开始
一箱金币:按 个一组分,剩 个;按 个一组分,剩 个。最少有多少枚?
翻译成同余方程组:
猜一个解: ✓。但猜出来的答案需要理论保证:解是否唯一?模数不互素时会怎样?
陈述
设 。则对任意余数 ,同余方程组
在 中有唯一解。
换句话说,映射
是从 到「 种余数 种余数」的双射。
证明
两侧都是有限集,各含 个元素。只要证明映射单射,由计数即得双射,也就同时证明了存在性与唯一性。
反设 都落在 内,且两边的余数对完全相同:
于是 且 。由 ,两个互素的数同时整除一个数,它们的乘积也整除它:
但 且 ,所以 ,不可能被 整除——矛盾。
「互素」这一步不能省。 若 , 且 只能推出 ,推不出 : 就是反例,因为两个模数共享因子 。
构造解法
唯一性只是保证;实际求 用下面的步骤。
设 是模 的余数,把 写成
代入第一个同余式:
因为 , 在模 下可逆。两边乘 :
再由 , 就等于这个余数本身,代回即得 。
例子
、。取 、、、。
先求 在模 下的逆元:,,故 。
与猜出的答案一致。
应用:喷泉码
把一个大文件看成一个巨大的整数 。服务器不发送文件本身,而是持续广播一组消息
其中 是一批素数。
接收方无论收到哪一组消息,只要
就可以由中国剩余定理唯一地还原 (这是两个模数情形的推广)。
好处在于:所有接收者可以收听同一路广播,谁先攒够「乘积超过 」的那一部分,谁就先还原文件,不要求大家收到相同的片段。这正是喷泉码(fountain codes)的核心思想——像喷泉一样持续喷出数据,每个杯子接满即可。真实系统还要处理传输错误,但基本框架已经在这里。
零知识证明:卡片演示
课上用五张牌(三红两黑)做了一个小实验。
- 中间放一张红牌。
- 两位参与者各自决定「是否想看某部电影」:想看,就把自己的红牌放在靠近中间红牌的位置、黑牌放外侧;不想看,则黑牌靠内、红牌靠外。
- 把牌叠起来,各自随机切一次(等价于循环移位)。
结论:两人都想看 循环排列中出现三张连续的红牌。若只有一人想看,则不会出现三连红。
因此旁观者只能知道「是否两人都同意」,而无法知道具体是谁想看、谁不想看:切牌把所有可区分的信息抹掉了。这就是零知识证明的雏形——证明「我知道某个事实」,却不泄露事实本身。
本节结论
- 两者除以 的余数相等。
- 模运算保持加法、乘法、结合律、交换律、分配律;但不能随意约去因子。
- ,由此得到欧几里得算法。
- 扩展欧几里得算法给出 Bézout 等式 ,它同时是最大性的证书。
- 在模 下可逆 ;逆元由扩展算法读出。
- 中国剩余定理: 时, 与 唯一确定 。
- 构造解法:,。
- 应用:喷泉码用 CRT 从任意足够多的片段还原文件。
理解检验
1. 证明: 当且仅当它们除以 的余数相等。
参考答案:设 、,其中 。若 ,则 是 的倍数,故同余。反之若 ,则 说明 ;而 ,该区间内唯一的 的倍数是 ,故 。
2. 证明 (设 ),并说明为什么这样证比「比较大小」更干净。
参考答案:设 是 的公因数,即 、,则 ,故 也是 的公因数。反之若 、,则 ,故 也是 的公因数。两边的公因数集合完全相同,因此最大公因数相同。直接比较大小只能得到一边是另一边的下界,还必须再证反方向;而证明集合相等一次就得到双向结论。
3. 用欧几里得算法计算 ,再用扩展算法求出 ,并解释 Bézout 等式为什么是「最大性证书」。
参考答案:;;;;。最后一个非零余数是 ,故 。倒推:;;;。所以 。若 同时整除 和 ,则由 得 ,故 :这证明 已是最大公因数,无需与其它数逐一比较。
4. 为什么 时 在模 下可逆?如何算出逆元?举 与 在模 下的例子。
参考答案:由 Bézout, 给出整数 使 。两边模 得 ,故 是逆元。算法上对 跑扩展欧几里得即可读出 。例:,故 ;,故 。
5. 证明中国剩余定理( 时解在 内唯一)。
参考答案:考虑映射 。若 有相同的余数对,则 且 。因 ,推出 。但 时 ,不可能被 整除,矛盾。故映射单射。两侧元素个数同为 ,单射即双射,唯一性与存在性同时成立。
6. 用构造法解 、。
参考答案:取 ,令 。代入第一式:,即 。 的逆元是 (),故 。由 得 ,于是 。
7. 五张牌的演示为什么能实现「零知识」?切牌起了什么作用?
参考答案:每人的摆法只编码一个比特(想看 / 不想看),但两种摆法在叠牌并循环移位后无法区分。最终公开的只有「是否存在三张连续的红牌」,而这只在两人都想看时出现。切牌的作用是施加随机循环移位,把所有与「谁喜欢、谁不喜欢」相关的具体位置信息全部抹掉,只留下一个对双方对称的判定结果,因此旁观者无法反推出任何一方的私人选择。
与前后课的关系
上一讲是 Chernoff 界;本讲转入数论,建立同余、欧几里得算法与中国剩余定理。下一讲把这些结构抽象成群,讨论群的定义、子群与元素的阶。
这些工具在后续课程中直接用于密码学:RSA 建立在模幂与逆元之上,Diffie–Hellman 建立在有限域的乘法群上,而编码理论中的 Reed–Solomon 码则建立在中国剩余定理的推广(多项式插值)之上。
What this lecture is for
"Even plus odd is odd" is elementary, yet it is already arithmetic modulo : knowing only the remainders after division by predicts the parity of the sum. This lecture pushes the observation to arbitrary moduli and answers three questions:
- Which algebraic rules carry over to modular arithmetic?
- When can one "divide" by a number, i.e. when does a multiplicative inverse exist?
- Can several congruence conditions with different moduli hold at once, and if so is the solution unique and how is it found?
The third question is the Chinese remainder theorem, which reappears throughout coding and cryptography.
Timeline
| Time | Topic |
|---|---|
| 0:00 | From parity to modular arithmetic |
| 0:57 | Definition of congruence |
| 2:16 | The division algorithm |
| 4:21 | Proof that congruence equals equality of remainders |
| 9:04 | The clock picture |
| 10:25 | Addition and multiplication modulo |
| 16:25 | Associativity, commutativity, distributivity |
| 18:13 | A zero-knowledge proof with playing cards |
| 23:39 | Multiplicative inverses; the key question |
| 27:50 | Greatest common divisors |
| 29:54 | The lemma behind Euclid's algorithm |
| 42:06 | A numerical example |
| 44:06 | The extended algorithm and Bézout's identity |
| 49:37 | Existence and computation of inverses |
| 54:26 | The Chinese remainder theorem |
| 1:11:08 | A constructive solution |
| 1:14:22 | Application: fountain codes |
Congruences and division with remainder
Definition
Let be a positive integer. We say and are congruent modulo , written
exactly when is an integer multiple of , that is .
The division algorithm
For any integers and there are unique integers and with
Here is the quotient and the remainder. Uniqueness follows because the interval has length : two solutions would differ by a multiple of of absolute value less than , hence by .
Congruence equals equality of remainders
Proposition.
Proof (). Write and with the same remainder . Then
a multiple of , so the two are congruent.
Proof (). Write , and assume is a multiple of . On the other hand
Therefore is also a multiple of . But , so
The only multiple of in this open interval is , hence .
This proposition translates an abstract relation into a concrete operation — divide and look at the remainder — and every algorithm below relies on the translation.
The clock picture
Place evenly around a clock face. An integer corresponds to the position reached by walking steps from .
Modulo , for example, lands on position ; goes once around and then five more steps, landing on as well, so . Modular arithmetic does not distinguish how many laps were made, only where you stop.
Arithmetic modulo
Addition and multiplication
If and , then
The practical procedure: compute with ordinary integers, then subtract enough multiples of to land in .
Example modulo : and , so
Check: .
Why may one compute first and reduce afterwards? Every integer has the form , and is modulo ; only the remainder does any work.
Three algebraic laws
For all and modulus :
- Associativity: and .
- Commutativity: and .
- Distributivity: .
These match ordinary integer arithmetic because the two sides differ by a multiple of . Modular arithmetic therefore forms a ring — the starting point of the next lecture on groups.
Not every rule survives
Addition and multiplication are safe, but cancellation is not. Modulo ,
so yet . Cancelling a factor requires that factor to be invertible, which is the subject of the next section.
Multiplicative inverses
Definition and examples
An element has a multiplicative inverse modulo if some satisfies
Modulo , , so is the inverse of and vice versa. Likewise .
With an inverse, "dividing by " becomes "multiplying by ": from we get . This is the prototype of encryption and decryption in cryptography — multiply to scramble a message, multiply by the inverse to unscramble it.
The key question
Modulo , does every nonzero element have an inverse? Checking the table confirms that all do. But for large moduli no one can write down the whole table, so theory must answer two questions:
- When does an inverse exist?
- How is it computed without a table?
The answers involve gcds and Euclid's algorithm.
Greatest common divisors
is the largest positive integer dividing both and ; it is when no larger common divisor exists.
The brute-force method tries every integer from to , which is hopeless for large numbers. A fast algorithm is needed.
Euclid's algorithm
Lemma 1: subtracting one copy preserves the gcd
For ,
Proof. Compare the two sets of common divisors.
If and , then , so is a common divisor of and .
Conversely, if and , then is also divisible by , so is a common divisor of and .
The two sets of common divisors coincide, so their largest elements coincide.
Logically, this is not a lower-bound argument but a one-to-one match of common divisors; equality of sets is stronger and cleaner than comparing sizes.
Lemma 2: subtract all at once
Write by the division algorithm. Applying Lemma 1 repeatedly,
Hence
One division replaces subtractions, which is why the algorithm is fast: if and are close, one subtraction halves the larger; if they are far apart, one remainder operation brings the larger below . Either way the numbers shrink rapidly.
A numerical example
Compute :
The last nonzero remainder is , so . Indeed , since every positive integer divides .
The extended algorithm and Bézout's identity
The plain algorithm keeps only remainders; the extended one also records how each remainder is built from the original pair.
Working backwards:
Thus and satisfy
Theorem (Bézout). For any integers there exist integers with
The identity is a certificate
Suppose someone reports the answer . How can you be sure it is the greatest common divisor rather than merely some common divisor?
- It divides both: and are checked directly.
- Nothing larger divides both: if divides and , then from dividing by gives an integer on the left and a sum of integers on the right, so , hence .
One identity establishes both "is a common divisor" and "no larger one exists". That is the value of Bézout: it turns a comparison of sizes into a divisibility check.
Inverses: existence and computation
Why a prime modulus guarantees inverses
Let be prime and . The only positive divisors of are and , and is not a multiple of , so
By Bézout there are with
Reducing modulo kills :
So is the inverse of . This proves existence and also gives an algorithm: run the extended Euclidean algorithm on and and read off .
Non-prime moduli
The real condition is , independent of whether is prime. Modulo , , so has an inverse, and indeed : it is its own inverse. But , so has no inverse.
Examples
Modulo , the inverse of : and , so the inverse is .
Modulo , the inverse of : , so the inverse is .
The Chinese remainder theorem
Starting from a puzzle
A chest of coins: grouped in fives, four are left over; grouped in sevens, one is left over. What is the smallest possible number?
In congruences:
Guess . A guess, however, needs theory: is the solution unique, and what happens when the moduli share a factor?
Statement
Let . Then for any residues the system
has a unique solution with .
Equivalently, the map
is a bijection from onto the possible residue pairs.
Proof
Both sides are finite sets with elements. It suffices to prove the map is injective; counting then upgrades it to a bijection, settling existence and uniqueness at once.
Suppose both lie in and have identical residue pairs:
Then and . Since , two coprime numbers dividing the same integer force their product to divide it:
But when , so cannot divide — a contradiction.
Coprimality cannot be dropped. With and , the conditions and imply only , not ; is a counterexample, because the two moduli share the factor .
A constructive solution
Uniqueness is only a guarantee; to compute , proceed as follows.
Let be the residue modulo and write
Substitute into the first congruence:
Since , is invertible modulo . Multiplying by :
Because , equals that residue, and substituting back gives .
Example
Solve , . Take , , , .
The inverse of modulo : and , so .
Matching the guess.
Application: fountain codes
Regard a large file as a huge integer . The server does not transmit the file itself; it continuously broadcasts pairs
where the are distinct primes.
A receiver can reconstruct from any set of messages whose moduli satisfy
by the Chinese remainder theorem (the extension from two moduli to many).
The advantage: every receiver listens to the same broadcast, and each one finishes as soon as it has collected enough — no requirement that all receivers obtain the same pieces. This is the core idea of fountain codes: data sprays out continuously and each cup fills when it has enough. Real systems must also handle transmission errors, but the framework is here.
Zero-knowledge proofs: the card demonstration
Five cards (three red, two black) support a small experiment.
- A red card is placed face up in the middle.
- Each participant decides privately whether they want to watch a certain embarrassing movie. If yes, they place their red card adjacent to the centre card and their black card outside; if no, the order is reversed.
- The cards are stacked and each participant performs a random cut, i.e. a cyclic shift.
The outcome: both want to watch if and only if three red cards appear consecutively in the cyclic order. If only one wants to, there is no run of three reds.
An observer therefore learns only whether both agree, not who wanted what: the cuts erase every distinguishable detail. This is the germ of a zero-knowledge proof — proving that a fact is known without revealing the fact itself.
What this lecture establishes
- the remainders on division by agree.
- Modular arithmetic preserves addition, multiplication, associativity, commutativity and distributivity; cancellation is not always valid.
- , which yields Euclid's algorithm.
- The extended algorithm yields Bézout's identity , which certifies maximality.
- is invertible modulo iff ; the inverse is read off from the extended algorithm.
- Chinese remainder theorem: when , the residues modulo and determine uniquely.
- Construction: with .
- Application: fountain codes reconstruct a file from any sufficiently many fragments via CRT.
Comprehension check
1. Prove that iff the remainders on division by are equal.
Reference answer: Write and with . If , then is a multiple of , so the numbers are congruent. Conversely, if , then shows ; since , the only multiple of in that range is , so .
2. Prove for , and explain why this is cleaner than comparing sizes.
Reference answer: If divides both and , then divides , so is a common divisor of and . Conversely, if divides and , then divides , so is a common divisor of and . The two common-divisor sets coincide, hence so do their maxima. Comparing sizes directly would give only a one-sided bound and require a separate argument for the reverse inequality; proving set equality settles both directions at once.
3. Compute with Euclid's algorithm, find with , and explain why the identity certifies maximality.
Reference answer: ; ; ; ; . The last nonzero remainder is , so the gcd is . Back-substituting: ; ; ; . Thus , . If divides both and , then from it follows that , hence : no larger common divisor exists, without comparing against other candidates.
4. Why does make invertible modulo ? How is the inverse computed? Give examples for and modulo .
Reference answer: Bézout gives integers with . Reducing modulo kills , leaving , so is the inverse. Computationally, run the extended Euclidean algorithm on and and read off . Examples: , so ; , so .
5. Prove the Chinese remainder theorem (uniqueness in when ).
Reference answer: Consider . If have identical residue pairs, then and . Coprimality forces . But when , so cannot divide the difference — a contradiction. Hence the map is injective. Both sides have elements, so injectivity implies bijectivity, giving existence and uniqueness together.
6. Solve , by the constructive method.
Reference answer: Take and write . Substituting into the first congruence, , i.e. . The inverse of is since , so . With , , hence .
7. Why does the five-card demonstration achieve zero knowledge? What role does the cut play?
Reference answer: Each participant's arrangement encodes one bit (wants to watch or not), but after stacking and a random cyclic shift the two arrangements are indistinguishable. The only public information is whether three consecutive red cards appear, which happens exactly when both want to watch. The cut applies a random cyclic shift that erases all positional information about who chose what, leaving only a symmetric yes/no answer, so an observer cannot recover either private choice.
Relation to neighbouring lectures
The previous lecture proved Chernoff bounds; this one turns to number theory, establishing congruences, Euclid's algorithm and the Chinese remainder theorem. The next lecture abstracts these structures into groups, covering the definition, subgroups and the order of an element.
The tools are used directly later in cryptography: RSA rests on modular exponentiation and inverses, Diffie–Hellman on the multiplicative group of a finite field, and Reed–Solomon codes on the polynomial form of the Chinese remainder theorem (interpolation).