第 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.

首发于 2025.12.18中 / EN
系统课程number-theorymodular-arithmeticeuclidean-algorithmchinese-remainder-theorem
语言
字号
目录
篇内目录

本讲要解决的问题

「偶数加奇数得奇数」是小学知识,但它其实是模 22 的算术:只看除以 22 的余数,就能预测和的奇偶性。本讲把这一观察推广到任意模数,回答三个问题:

  1. 哪些运算规则可以照搬到模运算里?
  2. 什么时候可以「除以」一个数,也就是求乘法逆元?
  3. 多个模数下的余数条件能否同时满足?能的话,解是否唯一、如何求?

第三个问题就是中国剩余定理,它会在编码与密码学中反复出现。

时间轴

时间内容
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应用:喷泉码

同余与带余除法

定义

设 nn 为正整数。称 aa 与 bb 模 nn 同余,记作

a≡b(modn),a\equiv b\pmod n,

当且仅当 a−ba-b 是 nn 的整数倍,即 n∣(a−b)n\mid(a-b)。

带余除法

对任意整数 aa 与正整数 nn,存在唯一的整数 qq 与 rr,使得

a=qn+r,0≤r<n.a=qn+r,\qquad 0\le r<n .

qq 是商,rr 是余数。唯一性来自区间 [0,n−1)[0,n-1) 的长度恰为 nn:若有两组解,相减会得到一个绝对值小于 nn 的 nn 的倍数,只能是 00。

同余等价于余数相等

命题。

a≡b(modn)  ⟺  a mod n=b mod n.a\equiv b\pmod n \iff a\bmod n=b\bmod n .

证明(⇐\Leftarrow)。 设 a=qn+ra=qn+r、b=q′n+rb=q'n+r,两者余数同为 rr。则

a−b=(q−q′)n,a-b=(q-q')n,

是 nn 的倍数,故同余。

证明(⇒\Rightarrow)。 设 a=qn+ra=qn+r、b=q′n+r′b=q'n+r',且 a≡ba\equiv b,即 a−ba-b 是 nn 的倍数。另一方面

a−b=(q−q′)n+(r−r′).a-b=(q-q')n+(r-r').

因此 r−r′r-r' 也是 nn 的倍数。但 0≤r,r′<n0\le r,r'<n,所以

−n<r−r′<n.-n<r-r'<n .

在这个开区间内唯一的 nn 的倍数是 00,故 r=r′r=r'。□\square

这个命题把「同余」这个抽象关系翻译成了「做除法看余数」这个具体操作,后面所有算法都建立在这一翻译上。

时钟图像

把 0,1,…,n−10,1,\dots,n-1 均匀排在钟面上。整数 kk 对应从 00 出发走 kk 步后落到的位置。

例如模 77:55 落在位置 55;1212 绕一整圈后再走 55 步,也落在位置 55,所以 12≡5(mod7)12\equiv5\pmod7。模运算不区分绕了几圈,只关心停在哪里。

模运算

加法与乘法

若 a≡a′a\equiv a'、b≡b′(modn)b\equiv b'\pmod n,则

a+b≡a′+b′,ab≡a′b′(modn).a+b\equiv a'+b',\qquad ab\equiv a'b'\pmod n .

直观做法:先按普通整数算,最后减去若干个 nn,把结果拉回 [0,n−1][0,n-1]。

例(模 77):10≡310\equiv3、12≡512\equiv5,于是

10⋅12=120≡3⋅5=15≡1(mod7).10\cdot12=120\equiv3\cdot5=15\equiv1\pmod7 .

核对:120=17⋅7+1120=17\cdot7+1 ✓。

为什么可以「先算再取余」? 因为每个整数都可以写成 qn+rqn+r,而 qnqn 这一部分在模 nn 下等于 00。剩下起作用的只有余数 rr。

三条代数律

对任意 a,b,ca,b,c 与模 nn:

  • 结合律:(a+b)+c≡a+(b+c)(a+b)+c\equiv a+(b+c),(ab)c≡a(bc)(ab)c\equiv a(bc)。
  • 交换律:a+b≡b+aa+b\equiv b+a,ab≡baab\equiv ba。
  • 分配律:a(b+c)≡ab+aca(b+c)\equiv ab+ac。

这三条与普通整数运算完全一致:因为两边相减都是 nn 的倍数。模运算因此构成一个环——这正是下一讲群论的起点。

不是所有规则都能照搬

可以相加、相乘,但不能随意约去。例如模 66 时

2⋅3=6≡0,2⋅0=0,2\cdot3=6\equiv0,\qquad 2\cdot0=0,

所以 2⋅3≡2⋅02\cdot3\equiv2\cdot0,却推不出 3≡03\equiv0。约去因子需要该因子可逆,这正是下一节的主题。

乘法逆元

定义与例子

称 aa 在模 nn 下有乘法逆元,若存在 bb 使

ab≡1(modn).ab\equiv1\pmod n .

模 77 时,2⋅4=8≡12\cdot4=8\equiv1,所以 44 的逆元是 22,22 的逆元是 44。同理 3⋅5=15≡13\cdot5=15\equiv1,二者互为逆元。

有了逆元,「除以 44」就可以实现为「乘以 22」:若已知 4x≡c4x\equiv c,两边乘 22 得 x≡2cx\equiv2c。这正是密码学里「加密后还能解密」的原型——乘以某个数把信息打乱,再乘以它的逆元还原。

关键问题

模 77 时,每个非零元素都有逆元吗? 逐个查表可以确认:1,2,3,4,5,61,2,3,4,5,6 都有。但当模数很大时,不可能写出整张乘法表。于是需要理论回答两个问题:

  1. 什么时候存在逆元?
  2. 不用查表,怎样算出逆元?

答案要用到最大公因数与欧几里得算法。

最大公因数

gcd⁡(a,b)\gcd(a,b) 是同时整除 aa 与 bb 的最大正整数。若没有更大的公因数,它就是 11。

暴力算法是从 11 试到 min⁡(a,b)\min(a,b),对大整数完全不可行。需要一个快速算法。

欧几里得算法

引理 1:减去一份不改变最大公因数

设 b≥ab\ge a,则

gcd⁡(a,b)=gcd⁡(a, b−a).\gcd(a,b)=\gcd(a,\,b-a).

证明。 比较两边的公因数集合。

若 k∣ak\mid a 且 k∣bk\mid b,则 k∣(b−a)k\mid(b-a),所以 kk 也是 aa 与 b−ab-a 的公因数。

反之,若 k∣ak\mid a 且 k∣(b−a)k\mid(b-a),则 b=(b−a)+ab=(b-a)+a 也被 kk 整除,所以 kk 也是 aa 与 bb 的公因数。

两边的公因数集合完全相同,最大者自然相同。□\square

逻辑上要注意:不是「kk 是某个下界」,而是两边的公因数逐一对应。集合相等比大小相等更强,也更干净。

引理 2:一次减到底

设 b=qa+rb=qa+r(带余除法)。反复使用引理 1:

gcd⁡(a,b)=gcd⁡(a,b−a)=gcd⁡(a,b−2a)=⋯=gcd⁡(a,b−qa)=gcd⁡(a,r).\gcd(a,b)=\gcd(a,b-a)=\gcd(a,b-2a)=\cdots=\gcd(a,b-qa)=\gcd(a,r).

所以

gcd⁡(a,b)=gcd⁡(a, b mod a).\gcd(a,b)=\gcd(a,\ b\bmod a).

一步除法代替了 qq 次减法,这是算法快的原因:要么 aa、bb 接近,一次减法就让较大的数减半;要么差距很大,一次取余就让较大的数降到 aa 以下。两种情况都让数值迅速变小。

数值例子

计算 gcd⁡(46,360)\gcd(46,360):

360=7⋅46+38,46=1⋅38+8,38=4⋅8+6,8=1⋅6+2,6=3⋅2+0.\begin{aligned} 360&=7\cdot46+38,\\ 46&=1\cdot38+8,\\ 38&=4\cdot8+6,\\ 8&=1\cdot6+2,\\ 6&=3\cdot2+0. \end{aligned}

最后一个非零余数是 22,故 gcd⁡(46,360)=2\gcd(46,360)=2。gcd⁡(2,0)=2\gcd(2,0)=2,因为 00 被任何正整数整除。

扩展欧几里得与 Bézout 等式

普通算法只保留余数;扩展算法还记录每个余数是如何由原始两数拼出来的。

从上面的例子倒推:

38=360−7⋅46,8=46−38=8⋅46−360,6=38−4⋅8=5⋅360−39⋅46,2=8−6=47⋅46−6⋅360.\begin{aligned} 38&=360-7\cdot46,\\ 8&=46-38=8\cdot46-360,\\ 6&=38-4\cdot8=5\cdot360-39\cdot46,\\ 2&=8-6=47\cdot46-6\cdot360. \end{aligned}

于是找到整数 s=47s=47、t=−6t=-6,使

46⋅47+360⋅(−6)=2162−2160=2.46\cdot47+360\cdot(-6)=2162-2160=2 .

一般结论(Bézout)。 对任意整数 a,ba,b,存在整数 s,ts,t 使

sa+tb=gcd⁡(a,b).sa+tb=\gcd(a,b).

这个等式是一张证书

假设有人只告诉你答案是 22。你怎么确认它是最大公因数,而不只是某个公因数?

  • 公因数性:2∣462\mid46 且 2∣3602\mid360,可以直接验算。
  • 最大性:设 kk 同时整除 4646 与 360360,则由 2=47⋅46−6⋅3602=47\cdot46-6\cdot360,两边除以 kk,左边是整数,右边是两个整数之和,也是整数,所以 k∣2k\mid2,即 k≤2k\le2。

一个等式同时证明了「是公因数」和「没有更大的公因数」。这就是 Bézout 等式的价值:它把「最大」这个看似需要比较的量,变成了一个可验证的整除关系。

逆元:存在性与计算

为什么模素数时一定有逆元

设 pp 为素数,a≢0(modp)a\not\equiv0\pmod p。因为 pp 只有 11 与 pp 两个正因数,而 aa 不是 pp 的倍数,所以

gcd⁡(a,p)=1.\gcd(a,p)=1 .

由 Bézout,存在 s,ts,t 使

as+pt=1.as+pt=1 .

两边模 pp,ptpt 消失:

as≡1(modp).as\equiv1\pmod p .

所以 ss 就是 aa 的逆元。这不仅证明了存在性,还给出了求法:对 aa 与 pp 跑扩展欧几里得算法,读出 ss。

非素数模数

条件其实是 gcd⁡(a,n)=1\gcd(a,n)=1,与 nn 是否素数无关。例如模 66 时,gcd⁡(5,6)=1\gcd(5,6)=1,故 55 有逆元,且 5⋅5=25≡15\cdot5=25\equiv1,逆元就是 55 自己。而 gcd⁡(2,6)=2≠1\gcd(2,6)=2\ne1,22 没有逆元。

例子

模 77 求 44 的逆元:gcd⁡(4,7)=1\gcd(4,7)=1,扩展算法给出 4⋅2+7⋅(−1)=14\cdot2+7\cdot(-1)=1,故逆元是 22。

模 77 求 55 的逆元:gcd⁡(5,7)=1\gcd(5,7)=1,5⋅3+7⋅(−2)=15\cdot3+7\cdot(-2)=1,故逆元是 33。

中国剩余定理

从谜题开始

一箱金币:按 55 个一组分,剩 44 个;按 77 个一组分,剩 11 个。最少有多少枚?

翻译成同余方程组:

n≡4(mod5),n≡1(mod7).n\equiv4\pmod5,\qquad n\equiv1\pmod7 .

猜一个解:29=5⋅5+4=4⋅7+129=5\cdot5+4=4\cdot7+1 ✓。但猜出来的答案需要理论保证:解是否唯一?模数不互素时会怎样?

陈述

设 gcd⁡(a,b)=1\gcd(a,b)=1。则对任意余数 r,sr,s,同余方程组

n≡r(moda),n≡s(modb)n\equiv r\pmod a,\qquad n\equiv s\pmod b

在 0≤n<ab0\le n<ab 中有唯一解。

换句话说,映射

n⟼(n mod a, n mod b)n\longmapsto\big(n\bmod a,\ n\bmod b\big)

是从 {0,1,…,ab−1}\{0,1,\dots,ab-1\} 到「aa 种余数 ×\times bb 种余数」的双射。

证明

两侧都是有限集,各含 abab 个元素。只要证明映射单射,由计数即得双射,也就同时证明了存在性与唯一性。

反设 x≠yx\ne y 都落在 [0,ab)[0,ab) 内,且两边的余数对完全相同:

x≡y(moda),x≡y(modb).x\equiv y\pmod a,\qquad x\equiv y\pmod b .

于是 a∣(x−y)a\mid(x-y) 且 b∣(x−y)b\mid(x-y)。由 gcd⁡(a,b)=1\gcd(a,b)=1,两个互素的数同时整除一个数,它们的乘积也整除它:

ab∣(x−y).ab\mid(x-y).

但 x,y∈[0,ab)x,y\in[0,ab) 且 x≠yx\ne y,所以 0<∣x−y∣<ab0<|x-y|<ab,不可能被 abab 整除——矛盾。□\square

「互素」这一步不能省。 若 a=6,b=9a=6,b=9,6∣N6\mid N 且 9∣N9\mid N 只能推出 18∣N18\mid N,推不出 54∣N54\mid N:N=18N=18 就是反例,因为两个模数共享因子 33。

构造解法

唯一性只是保证;实际求 nn 用下面的步骤。

设 t=st=s 是模 bb 的余数,把 nn 写成

n=kb+t,0≤k<a.n=kb+t,\qquad 0\le k<a .

代入第一个同余式:

kb+t≡r(moda)⟹kb≡r−t(moda).kb+t\equiv r\pmod a \Longrightarrow kb\equiv r-t\pmod a .

因为 gcd⁡(b,a)=1\gcd(b,a)=1,bb 在模 aa 下可逆。两边乘 b−1b^{-1}:

k≡b−1(r−t)(moda).k\equiv b^{-1}(r-t)\pmod a .

再由 0≤k<a0\le k<a,kk 就等于这个余数本身,代回即得 nn。

例子

n≡4(mod5)n\equiv4\pmod5、n≡1(mod7)n\equiv1\pmod7。取 a=5a=5、b=7b=7、r=4r=4、s=t=1s=t=1。

先求 77 在模 55 下的逆元:7≡27\equiv2,2⋅3=6≡12\cdot3=6\equiv1,故 7−1≡37^{-1}\equiv3。

k≡3⋅(4−1)=9≡4(mod5).k\equiv3\cdot(4-1)=9\equiv4\pmod5 . n=4⋅7+1=29.n=4\cdot7+1=29 .

与猜出的答案一致。

应用:喷泉码

把一个大文件看成一个巨大的整数 NN。服务器不发送文件本身,而是持续广播一组消息

(ai, N mod ai),(a_i,\ N\bmod a_i),

其中 aia_i 是一批素数。

接收方无论收到哪一组消息,只要

∏ai>N,\prod a_i>N,

就可以由中国剩余定理唯一地还原 NN(这是两个模数情形的推广)。

好处在于:所有接收者可以收听同一路广播,谁先攒够「乘积超过 NN」的那一部分,谁就先还原文件,不要求大家收到相同的片段。这正是喷泉码(fountain codes)的核心思想——像喷泉一样持续喷出数据,每个杯子接满即可。真实系统还要处理传输错误,但基本框架已经在这里。

零知识证明:卡片演示

课上用五张牌(三红两黑)做了一个小实验。

  • 中间放一张红牌。
  • 两位参与者各自决定「是否想看某部电影」:想看,就把自己的红牌放在靠近中间红牌的位置、黑牌放外侧;不想看,则黑牌靠内、红牌靠外。
  • 把牌叠起来,各自随机切一次(等价于循环移位)。

结论:两人都想看   ⟺  \iff 循环排列中出现三张连续的红牌。若只有一人想看,则不会出现三连红。

因此旁观者只能知道「是否两人都同意」,而无法知道具体是谁想看、谁不想看:切牌把所有可区分的信息抹掉了。这就是零知识证明的雏形——证明「我知道某个事实」,却不泄露事实本身。

本节结论

  • a≡b(modn)  ⟺  n∣(a−b)  ⟺  a\equiv b\pmod n\iff n\mid(a-b)\iff 两者除以 nn 的余数相等。
  • 模运算保持加法、乘法、结合律、交换律、分配律;但不能随意约去因子。
  • gcd⁡(a,b)=gcd⁡(a,b−a)=gcd⁡(a,b mod a)\gcd(a,b)=\gcd(a,b-a)=\gcd(a,b\bmod a),由此得到欧几里得算法。
  • 扩展欧几里得算法给出 Bézout 等式 sa+tb=gcd⁡(a,b)sa+tb=\gcd(a,b),它同时是最大性的证书。
  • aa 在模 nn 下可逆   ⟺  gcd⁡(a,n)=1\iff\gcd(a,n)=1;逆元由扩展算法读出。
  • 中国剩余定理:gcd⁡(a,b)=1\gcd(a,b)=1 时,n mod an\bmod a 与 n mod bn\bmod b 唯一确定 n∈[0,ab)n\in[0,ab)。
  • 构造解法:n=kb+tn=kb+t,k≡b−1(r−t)(moda)k\equiv b^{-1}(r-t)\pmod a。
  • 应用:喷泉码用 CRT 从任意足够多的片段还原文件。

理解检验

1. 证明:a≡b(modn)a\equiv b\pmod n 当且仅当它们除以 nn 的余数相等。

参考答案:设 a=qn+ra=qn+r、b=q′n+r′b=q'n+r',其中 0≤r,r′<n0\le r,r'<n。若 r=r′r=r',则 a−b=(q−q′)na-b=(q-q')n 是 nn 的倍数,故同余。反之若 n∣(a−b)n\mid(a-b),则 a−b=(q−q′)n+(r−r′)a-b=(q-q')n+(r-r') 说明 n∣(r−r′)n\mid(r-r');而 −n<r−r′<n-n<r-r'<n,该区间内唯一的 nn 的倍数是 00,故 r=r′r=r'。

2. 证明 gcd⁡(a,b)=gcd⁡(a,b−a)\gcd(a,b)=\gcd(a,b-a)(设 b≥ab\ge a),并说明为什么这样证比「比较大小」更干净。

参考答案:设 kk 是 a,ba,b 的公因数,即 k∣ak\mid a、k∣bk\mid b,则 k∣(b−a)k\mid(b-a),故 kk 也是 a,b−aa,b-a 的公因数。反之若 k∣ak\mid a、k∣(b−a)k\mid(b-a),则 k∣((b−a)+a)=bk\mid\big((b-a)+a\big)=b,故 kk 也是 a,ba,b 的公因数。两边的公因数集合完全相同,因此最大公因数相同。直接比较大小只能得到一边是另一边的下界,还必须再证反方向;而证明集合相等一次就得到双向结论。

3. 用欧几里得算法计算 gcd⁡(46,360)\gcd(46,360),再用扩展算法求出 s,ts,t,并解释 Bézout 等式为什么是「最大性证书」。

参考答案:360=7⋅46+38360=7\cdot46+38;46=1⋅38+846=1\cdot38+8;38=4⋅8+638=4\cdot8+6;8=1⋅6+28=1\cdot6+2;6=3⋅2+06=3\cdot2+0。最后一个非零余数是 22,故 gcd⁡=2\gcd=2。倒推:38=360−7⋅4638=360-7\cdot46;8=46−38=8⋅46−3608=46-38=8\cdot46-360;6=38−4⋅8=5⋅360−39⋅466=38-4\cdot8=5\cdot360-39\cdot46;2=8−6=47⋅46−6⋅3602=8-6=47\cdot46-6\cdot360。所以 s=47,t=−6s=47,t=-6。若 kk 同时整除 4646 和 360360,则由 2=47⋅46−6⋅3602=47\cdot46-6\cdot360 得 k∣2k\mid2,故 k≤2k\le2:这证明 22 已是最大公因数,无需与其它数逐一比较。

4. 为什么 gcd⁡(a,n)=1\gcd(a,n)=1 时 aa 在模 nn 下可逆?如何算出逆元?举 44 与 55 在模 77 下的例子。

参考答案:由 Bézout,gcd⁡(a,n)=1\gcd(a,n)=1 给出整数 s,ts,t 使 as+nt=1as+nt=1。两边模 nn 得 as≡1as\equiv1,故 ss 是逆元。算法上对 a,na,n 跑扩展欧几里得即可读出 ss。例:4⋅2+7⋅(−1)=14\cdot2+7\cdot(-1)=1,故 4−1≡24^{-1}\equiv2;5⋅3+7⋅(−2)=15\cdot3+7\cdot(-2)=1,故 5−1≡35^{-1}\equiv3。

5. 证明中国剩余定理(gcd⁡(a,b)=1\gcd(a,b)=1 时解在 [0,ab)[0,ab) 内唯一)。

参考答案:考虑映射 n↦(n mod a,n mod b)n\mapsto(n\bmod a,n\bmod b)。若 x,y∈[0,ab)x,y\in[0,ab) 有相同的余数对,则 a∣(x−y)a\mid(x-y) 且 b∣(x−y)b\mid(x-y)。因 gcd⁡(a,b)=1\gcd(a,b)=1,推出 ab∣(x−y)ab\mid(x-y)。但 x≠yx\ne y 时 0<∣x−y∣<ab0<|x-y|<ab,不可能被 abab 整除,矛盾。故映射单射。两侧元素个数同为 abab,单射即双射,唯一性与存在性同时成立。

6. 用构造法解 n≡4(mod5)n\equiv4\pmod5、n≡1(mod7)n\equiv1\pmod7。

参考答案:取 t=1t=1,令 n=7k+1n=7k+1。代入第一式:7k+1≡4(mod5)7k+1\equiv4\pmod5,即 2k≡3(mod5)2k\equiv3\pmod5。22 的逆元是 33(2⋅3=6≡12\cdot3=6\equiv1),故 k≡9≡4(mod5)k\equiv9\equiv4\pmod5。由 0≤k<50\le k<5 得 k=4k=4,于是 n=4⋅7+1=29n=4\cdot7+1=29。

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 22: knowing only the remainders after division by 22 predicts the parity of the sum. This lecture pushes the observation to arbitrary moduli and answers three questions:

  1. Which algebraic rules carry over to modular arithmetic?
  2. When can one "divide" by a number, i.e. when does a multiplicative inverse exist?
  3. 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

TimeTopic
0:00From parity to modular arithmetic
0:57Definition of congruence
2:16The division algorithm
4:21Proof that congruence equals equality of remainders
9:04The clock picture
10:25Addition and multiplication modulo nn
16:25Associativity, commutativity, distributivity
18:13A zero-knowledge proof with playing cards
23:39Multiplicative inverses; the key question
27:50Greatest common divisors
29:54The lemma behind Euclid's algorithm
42:06A numerical example
44:06The extended algorithm and Bézout's identity
49:37Existence and computation of inverses
54:26The Chinese remainder theorem
1:11:08A constructive solution
1:14:22Application: fountain codes

Congruences and division with remainder

Definition

Let nn be a positive integer. We say aa and bb are congruent modulo nn, written

a≡b(modn),a\equiv b\pmod n,

exactly when a−ba-b is an integer multiple of nn, that is n∣(a−b)n\mid(a-b).

The division algorithm

For any integers aa and n>0n>0 there are unique integers qq and rr with

a=qn+r,0≤r<n.a=qn+r,\qquad 0\le r<n .

Here qq is the quotient and rr the remainder. Uniqueness follows because the interval [0,n−1][0,n-1] has length nn: two solutions would differ by a multiple of nn of absolute value less than nn, hence by 00.

Congruence equals equality of remainders

Proposition.

a≡b(modn)  ⟺  a mod n=b mod n.a\equiv b\pmod n \iff a\bmod n=b\bmod n .

Proof (⇐\Leftarrow). Write a=qn+ra=qn+r and b=q′n+rb=q'n+r with the same remainder rr. Then

a−b=(q−q′)n,a-b=(q-q')n,

a multiple of nn, so the two are congruent.

Proof (⇒\Rightarrow). Write a=qn+ra=qn+r, b=q′n+r′b=q'n+r' and assume a−ba-b is a multiple of nn. On the other hand

a−b=(q−q′)n+(r−r′).a-b=(q-q')n+(r-r').

Therefore r−r′r-r' is also a multiple of nn. But 0≤r,r′<n0\le r,r'<n, so

−n<r−r′<n.-n<r-r'<n .

The only multiple of nn in this open interval is 00, hence r=r′r=r'. □\square

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 0,1,…,n−10,1,\dots,n-1 evenly around a clock face. An integer kk corresponds to the position reached by walking kk steps from 00.

Modulo 77, for example, 55 lands on position 55; 1212 goes once around and then five more steps, landing on 55 as well, so 12≡5(mod7)12\equiv5\pmod7. Modular arithmetic does not distinguish how many laps were made, only where you stop.

Arithmetic modulo nn

Addition and multiplication

If a≡a′a\equiv a' and b≡b′(modn)b\equiv b'\pmod n, then

a+b≡a′+b′,ab≡a′b′(modn).a+b\equiv a'+b',\qquad ab\equiv a'b'\pmod n .

The practical procedure: compute with ordinary integers, then subtract enough multiples of nn to land in [0,n−1][0,n-1].

Example modulo 77: 10≡310\equiv3 and 12≡512\equiv5, so

10⋅12=120≡3⋅5=15≡1(mod7).10\cdot12=120\equiv3\cdot5=15\equiv1\pmod7 .

Check: 120=17⋅7+1120=17\cdot7+1.

Why may one compute first and reduce afterwards? Every integer has the form qn+rqn+r, and qnqn is 00 modulo nn; only the remainder does any work.

Three algebraic laws

For all a,b,ca,b,c and modulus nn:

  • Associativity: (a+b)+c≡a+(b+c)(a+b)+c\equiv a+(b+c) and (ab)c≡a(bc)(ab)c\equiv a(bc).
  • Commutativity: a+b≡b+aa+b\equiv b+a and ab≡baab\equiv ba.
  • Distributivity: a(b+c)≡ab+aca(b+c)\equiv ab+ac.

These match ordinary integer arithmetic because the two sides differ by a multiple of nn. 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 66,

2⋅3=6≡0,2⋅0=0,2\cdot3=6\equiv0,\qquad 2\cdot0=0,

so 2⋅3≡2⋅02\cdot3\equiv2\cdot0 yet 3≢03\not\equiv0. Cancelling a factor requires that factor to be invertible, which is the subject of the next section.

Multiplicative inverses

Definition and examples

An element aa has a multiplicative inverse modulo nn if some bb satisfies

ab≡1(modn).ab\equiv1\pmod n .

Modulo 77, 2⋅4=8≡12\cdot4=8\equiv1, so 44 is the inverse of 22 and vice versa. Likewise 3⋅5=15≡13\cdot5=15\equiv1.

With an inverse, "dividing by 44" becomes "multiplying by 22": from 4x≡c4x\equiv c we get x≡2cx\equiv2c. 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 77, does every nonzero element have an inverse? Checking the table confirms that 1,2,3,4,5,61,2,3,4,5,6 all do. But for large moduli no one can write down the whole table, so theory must answer two questions:

  1. When does an inverse exist?
  2. How is it computed without a table?

The answers involve gcds and Euclid's algorithm.

Greatest common divisors

gcd⁡(a,b)\gcd(a,b) is the largest positive integer dividing both aa and bb; it is 11 when no larger common divisor exists.

The brute-force method tries every integer from 11 to min⁡(a,b)\min(a,b), which is hopeless for large numbers. A fast algorithm is needed.

Euclid's algorithm

Lemma 1: subtracting one copy preserves the gcd

For b≥ab\ge a,

gcd⁡(a,b)=gcd⁡(a, b−a).\gcd(a,b)=\gcd(a,\,b-a).

Proof. Compare the two sets of common divisors.

If k∣ak\mid a and k∣bk\mid b, then k∣(b−a)k\mid(b-a), so kk is a common divisor of aa and b−ab-a.

Conversely, if k∣ak\mid a and k∣(b−a)k\mid(b-a), then b=(b−a)+ab=(b-a)+a is also divisible by kk, so kk is a common divisor of aa and bb.

The two sets of common divisors coincide, so their largest elements coincide. □\square

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 b=qa+rb=qa+r by the division algorithm. Applying Lemma 1 repeatedly,

gcd⁡(a,b)=gcd⁡(a,b−a)=gcd⁡(a,b−2a)=⋯=gcd⁡(a,b−qa)=gcd⁡(a,r).\gcd(a,b)=\gcd(a,b-a)=\gcd(a,b-2a)=\cdots=\gcd(a,b-qa)=\gcd(a,r).

Hence

gcd⁡(a,b)=gcd⁡(a, b mod a).\gcd(a,b)=\gcd(a,\ b\bmod a).

One division replaces qq subtractions, which is why the algorithm is fast: if aa and bb are close, one subtraction halves the larger; if they are far apart, one remainder operation brings the larger below aa. Either way the numbers shrink rapidly.

A numerical example

Compute gcd⁡(46,360)\gcd(46,360):

360=7⋅46+38,46=1⋅38+8,38=4⋅8+6,8=1⋅6+2,6=3⋅2+0.\begin{aligned} 360&=7\cdot46+38,\\ 46&=1\cdot38+8,\\ 38&=4\cdot8+6,\\ 8&=1\cdot6+2,\\ 6&=3\cdot2+0. \end{aligned}

The last nonzero remainder is 22, so gcd⁡(46,360)=2\gcd(46,360)=2. Indeed gcd⁡(2,0)=2\gcd(2,0)=2, since every positive integer divides 00.

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:

38=360−7⋅46,8=46−38=8⋅46−360,6=38−4⋅8=5⋅360−39⋅46,2=8−6=47⋅46−6⋅360.\begin{aligned} 38&=360-7\cdot46,\\ 8&=46-38=8\cdot46-360,\\ 6&=38-4\cdot8=5\cdot360-39\cdot46,\\ 2&=8-6=47\cdot46-6\cdot360. \end{aligned}

Thus s=47s=47 and t=−6t=-6 satisfy

46⋅47+360⋅(−6)=2162−2160=2.46\cdot47+360\cdot(-6)=2162-2160=2 .

Theorem (Bézout). For any integers a,ba,b there exist integers s,ts,t with

sa+tb=gcd⁡(a,b).sa+tb=\gcd(a,b).

The identity is a certificate

Suppose someone reports the answer 22. How can you be sure it is the greatest common divisor rather than merely some common divisor?

  • It divides both: 2∣462\mid46 and 2∣3602\mid360 are checked directly.
  • Nothing larger divides both: if kk divides 4646 and 360360, then from 2=47⋅46−6⋅3602=47\cdot46-6\cdot360 dividing by kk gives an integer on the left and a sum of integers on the right, so k∣2k\mid2, hence k≤2k\le2.

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 pp be prime and a≢0(modp)a\not\equiv0\pmod p. The only positive divisors of pp are 11 and pp, and aa is not a multiple of pp, so

gcd⁡(a,p)=1.\gcd(a,p)=1 .

By Bézout there are s,ts,t with

as+pt=1.as+pt=1 .

Reducing modulo pp kills ptpt:

as≡1(modp).as\equiv1\pmod p .

So ss is the inverse of aa. This proves existence and also gives an algorithm: run the extended Euclidean algorithm on aa and pp and read off ss.

Non-prime moduli

The real condition is gcd⁡(a,n)=1\gcd(a,n)=1, independent of whether nn is prime. Modulo 66, gcd⁡(5,6)=1\gcd(5,6)=1, so 55 has an inverse, and indeed 5⋅5=25≡15\cdot5=25\equiv1: it is its own inverse. But gcd⁡(2,6)=2≠1\gcd(2,6)=2\ne1, so 22 has no inverse.

Examples

Modulo 77, the inverse of 44: gcd⁡(4,7)=1\gcd(4,7)=1 and 4⋅2+7⋅(−1)=14\cdot2+7\cdot(-1)=1, so the inverse is 22.

Modulo 77, the inverse of 55: 5⋅3+7⋅(−2)=15\cdot3+7\cdot(-2)=1, so the inverse is 33.

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:

n≡4(mod5),n≡1(mod7).n\equiv4\pmod5,\qquad n\equiv1\pmod7 .

Guess 29=5⋅5+4=4⋅7+129=5\cdot5+4=4\cdot7+1. A guess, however, needs theory: is the solution unique, and what happens when the moduli share a factor?

Statement

Let gcd⁡(a,b)=1\gcd(a,b)=1. Then for any residues r,sr,s the system

n≡r(moda),n≡s(modb)n\equiv r\pmod a,\qquad n\equiv s\pmod b

has a unique solution with 0≤n<ab0\le n<ab.

Equivalently, the map

n⟼(n mod a, n mod b)n\longmapsto\big(n\bmod a,\ n\bmod b\big)

is a bijection from {0,1,…,ab−1}\{0,1,\dots,ab-1\} onto the a×ba\times b possible residue pairs.

Proof

Both sides are finite sets with abab elements. It suffices to prove the map is injective; counting then upgrades it to a bijection, settling existence and uniqueness at once.

Suppose x≠yx\ne y both lie in [0,ab)[0,ab) and have identical residue pairs:

x≡y(moda),x≡y(modb).x\equiv y\pmod a,\qquad x\equiv y\pmod b .

Then a∣(x−y)a\mid(x-y) and b∣(x−y)b\mid(x-y). Since gcd⁡(a,b)=1\gcd(a,b)=1, two coprime numbers dividing the same integer force their product to divide it:

ab∣(x−y).ab\mid(x-y).

But 0<∣x−y∣<ab0<|x-y|<ab when x≠yx\ne y, so abab cannot divide x−yx-y — a contradiction. □\square

Coprimality cannot be dropped. With a=6a=6 and b=9b=9, the conditions 6∣N6\mid N and 9∣N9\mid N imply only 18∣N18\mid N, not 54∣N54\mid N; N=18N=18 is a counterexample, because the two moduli share the factor 33.

A constructive solution

Uniqueness is only a guarantee; to compute nn, proceed as follows.

Let t=st=s be the residue modulo bb and write

n=kb+t,0≤k<a.n=kb+t,\qquad 0\le k<a .

Substitute into the first congruence:

kb+t≡r(moda)⟹kb≡r−t(moda).kb+t\equiv r\pmod a \Longrightarrow kb\equiv r-t\pmod a .

Since gcd⁡(b,a)=1\gcd(b,a)=1, bb is invertible modulo aa. Multiplying by b−1b^{-1}:

k≡b−1(r−t)(moda).k\equiv b^{-1}(r-t)\pmod a .

Because 0≤k<a0\le k<a, kk equals that residue, and substituting back gives nn.

Example

Solve n≡4(mod5)n\equiv4\pmod5, n≡1(mod7)n\equiv1\pmod7. Take a=5a=5, b=7b=7, r=4r=4, s=t=1s=t=1.

The inverse of 77 modulo 55: 7≡27\equiv2 and 2⋅3=6≡12\cdot3=6\equiv1, so 7−1≡37^{-1}\equiv3.

k≡3⋅(4−1)=9≡4(mod5).k\equiv3\cdot(4-1)=9\equiv4\pmod5 . n=4⋅7+1=29.n=4\cdot7+1=29 .

Matching the guess.

Application: fountain codes

Regard a large file as a huge integer NN. The server does not transmit the file itself; it continuously broadcasts pairs

(ai, N mod ai),(a_i,\ N\bmod a_i),

where the aia_i are distinct primes.

A receiver can reconstruct NN from any set of messages whose moduli satisfy

∏ai>N,\prod a_i>N,

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

  • a≡b(modn)  ⟺  n∣(a−b)  ⟺  a\equiv b\pmod n\iff n\mid(a-b)\iff the remainders on division by nn agree.
  • Modular arithmetic preserves addition, multiplication, associativity, commutativity and distributivity; cancellation is not always valid.
  • gcd⁡(a,b)=gcd⁡(a,b−a)=gcd⁡(a,b mod a)\gcd(a,b)=\gcd(a,b-a)=\gcd(a,b\bmod a), which yields Euclid's algorithm.
  • The extended algorithm yields Bézout's identity sa+tb=gcd⁡(a,b)sa+tb=\gcd(a,b), which certifies maximality.
  • aa is invertible modulo nn iff gcd⁡(a,n)=1\gcd(a,n)=1; the inverse is read off from the extended algorithm.
  • Chinese remainder theorem: when gcd⁡(a,b)=1\gcd(a,b)=1, the residues modulo aa and bb determine n∈[0,ab)n\in[0,ab) uniquely.
  • Construction: n=kb+tn=kb+t with k≡b−1(r−t)(moda)k\equiv b^{-1}(r-t)\pmod a.
  • Application: fountain codes reconstruct a file from any sufficiently many fragments via CRT.

Comprehension check

1. Prove that a≡b(modn)a\equiv b\pmod n iff the remainders on division by nn are equal.

Reference answer: Write a=qn+ra=qn+r and b=q′n+r′b=q'n+r' with 0≤r,r′<n0\le r,r'<n. If r=r′r=r', then a−b=(q−q′)na-b=(q-q')n is a multiple of nn, so the numbers are congruent. Conversely, if n∣(a−b)n\mid(a-b), then a−b=(q−q′)n+(r−r′)a-b=(q-q')n+(r-r') shows n∣(r−r′)n\mid(r-r'); since −n<r−r′<n-n<r-r'<n, the only multiple of nn in that range is 00, so r=r′r=r'.

2. Prove gcd⁡(a,b)=gcd⁡(a,b−a)\gcd(a,b)=\gcd(a,b-a) for b≥ab\ge a, and explain why this is cleaner than comparing sizes.

Reference answer: If kk divides both aa and bb, then kk divides b−ab-a, so kk is a common divisor of aa and b−ab-a. Conversely, if kk divides aa and b−ab-a, then kk divides (b−a)+a=b(b-a)+a=b, so kk is a common divisor of aa and bb. 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 gcd⁡(46,360)\gcd(46,360) with Euclid's algorithm, find s,ts,t with 46s+360t=246s+360t=2, and explain why the identity certifies maximality.

Reference answer: 360=7⋅46+38360=7\cdot46+38; 46=1⋅38+846=1\cdot38+8; 38=4⋅8+638=4\cdot8+6; 8=1⋅6+28=1\cdot6+2; 6=3⋅2+06=3\cdot2+0. The last nonzero remainder is 22, so the gcd is 22. Back-substituting: 38=360−7⋅4638=360-7\cdot46; 8=46−38=8⋅46−3608=46-38=8\cdot46-360; 6=38−4⋅8=5⋅360−39⋅466=38-4\cdot8=5\cdot360-39\cdot46; 2=8−6=47⋅46−6⋅3602=8-6=47\cdot46-6\cdot360. Thus s=47s=47, t=−6t=-6. If kk divides both 4646 and 360360, then from 2=47⋅46−6⋅3602=47\cdot46-6\cdot360 it follows that k∣2k\mid2, hence k≤2k\le2: no larger common divisor exists, without comparing against other candidates.

4. Why does gcd⁡(a,n)=1\gcd(a,n)=1 make aa invertible modulo nn? How is the inverse computed? Give examples for 44 and 55 modulo 77.

Reference answer: Bézout gives integers s,ts,t with as+nt=1as+nt=1. Reducing modulo nn kills ntnt, leaving as≡1as\equiv1, so ss is the inverse. Computationally, run the extended Euclidean algorithm on aa and nn and read off ss. Examples: 4⋅2+7⋅(−1)=14\cdot2+7\cdot(-1)=1, so 4−1≡24^{-1}\equiv2; 5⋅3+7⋅(−2)=15\cdot3+7\cdot(-2)=1, so 5−1≡35^{-1}\equiv3.

5. Prove the Chinese remainder theorem (uniqueness in [0,ab)[0,ab) when gcd⁡(a,b)=1\gcd(a,b)=1).

Reference answer: Consider n↦(n mod a,n mod b)n\mapsto(n\bmod a,n\bmod b). If x,y∈[0,ab)x,y\in[0,ab) have identical residue pairs, then a∣(x−y)a\mid(x-y) and b∣(x−y)b\mid(x-y). Coprimality forces ab∣(x−y)ab\mid(x-y). But 0<∣x−y∣<ab0<|x-y|<ab when x≠yx\ne y, so abab cannot divide the difference — a contradiction. Hence the map is injective. Both sides have abab elements, so injectivity implies bijectivity, giving existence and uniqueness together.

6. Solve n≡4(mod5)n\equiv4\pmod5, n≡1(mod7)n\equiv1\pmod7 by the constructive method.

Reference answer: Take t=1t=1 and write n=7k+1n=7k+1. Substituting into the first congruence, 7k+1≡4(mod5)7k+1\equiv4\pmod5, i.e. 2k≡3(mod5)2k\equiv3\pmod5. The inverse of 22 is 33 since 2⋅3=6≡12\cdot3=6\equiv1, so k≡9≡4(mod5)k\equiv9\equiv4\pmod5. With 0≤k<50\le k<5, k=4k=4, hence n=4⋅7+1=29n=4\cdot7+1=29.

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).

来源