# 模乘法逆元

整数 b 模 m 的模乘法逆元是一个整数 b⁻¹，满足 b·b⁻¹ ≡ 1 (mod m)；它是模算术中除法的类比物。模算术是由卡尔·弗里德里希·高斯（Carl Friedrich Gauss）在其1801年的《算术研究》（*Disquisitiones Arithmeticae*）中形式化的余数系统。<sup>[1](https://mathworld.wolfram.com/ModularInverse.html)</sup><sup> • </sup><sup>[2](https://mathshistory.st-andrews.ac.uk/Extras/Gauss_Disquisitiones/)</sup> 逆元存在当且仅当 b 与 m 没有大于1的公因子，而高效算法能在与输入的对数成正比的步数内计算出它，这正是它在密码学和计算机代数中实用的原因。<sup>[3](https://www.usna.edu/Users/cs/wcbrown/courses/S17SI335/notes/03/notes.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 定义 | 一个整数 b⁻¹，满足 b·b⁻¹ ≡ 1 (mod m)；例如，1、2、3、4 模 5 的逆元分别是 1、3、2、4。<sup>[1](https://mathworld.wolfram.com/ModularInverse.html)</sup> |
| 存在性 | 非零整数 b 模 m 有逆元当且仅当 gcd(b, m) = 1；12 模 15 没有逆元，因为 gcd(15, 12) = 3，而 8 有，因为 gcd(15, 8) = 1。<sup>[1](https://mathworld.wolfram.com/ModularInverse.html)</sup><sup> • </sup><sup>[3](https://www.usna.edu/Users/cs/wcbrown/courses/S17SI335/notes/03/notes.html)</sup> |
| 标准算法 | 扩展欧几里得算法返回整数 s、t，使得 s·m + t·a = gcd(m, a)；当 gcd 为 1 时，t 就是 a 模 m 的逆元，需 Θ(log a) 步。<sup>[3](https://www.usna.edu/Users/cs/wcbrown/courses/S17SI335/notes/03/notes.html)</sup> |
| 幂运算替代法 | 欧拉定理给出 b⁻¹ = b^(φ(m)−1) (mod m)；对于素数模 p，这简化为 b^(p−2)。<sup>[1](https://mathworld.wolfram.com/ModularInverse.html)</sup> |
| 密码学用途 | 在 RSA 中，私钥指数 d 是公钥指数 e 模 φ(n) 的乘法逆元。<sup>[4](https://engineering.purdue.edu/kak/compsec/NewLectures/Lecture12.pdf)</sup> |
| 库支持 | Python 内置的 `pow` 自 3.8 版起接受带模数的负指数：`pow(38, -1, mod=97)` 返回 23。GMP 的 `mpz_invert` 计算逆元，若不存在则返回 0。<sup>[5](https://docs.python.org/3/library/functions.html)</sup><sup> • </sup><sup>[6](https://gmplib.org/gmp-man-6.3.0.pdf)</sup> |

## 存在性与唯一性

逆元是否存在只取决于最大公约数。在“模 m”的世界里，数 a 有乘法逆元当且仅当 gcd(m, a) = 1，即两数互素。<sup>[3](https://www.usna.edu/Users/cs/wcbrown/courses/S17SI335/notes/03/notes.html)</sup> 当 m 为素数时，每个不是 m 的倍数的非零整数都有逆元；当 m 为合数时，某些非零元素没有逆元。例如模 4 下，1 和 3 是自身的逆元，但 2 没有。<sup>[1](https://mathworld.wolfram.com/ModularInverse.html)</sup>

逆元若存在，则在加上 m 的倍数意义下唯一，因此习惯上报告 0 到 m − 1 之间的代表元。GMP 的 `mpz_invert` 规定返回满足 0 ≤ rop < |op2| 的结果，仅当 |op2| = 1 的退化情形下可能为 0。<sup>[6](https://gmplib.org/gmp-man-6.3.0.pdf)</sup>

## 用扩展欧几里得算法计算逆元

扩展欧几里得算法不仅计算其参数的最大公约数，还计算两个辅因子：它返回整数 g、s、t，使得 g = gcd(a, b) 且 a·s + b·t = g。这就是贝祖等式（Bézout's identity），它把存在性陈述转化为一种构造。<sup>[3](https://www.usna.edu/Users/cs/wcbrown/courses/S17SI335/notes/03/notes.html)</sup>

假设模数为 m 且 gcd(m, a) = 1。对 (m, a) 运行该算法返回 (1, s, t)，满足 1 = s·m + t·a。两边模 m 约化得 t·a ≡ 1 (mod m)，因此算法返回的系数 t 恰好是 a 模 m 的逆元。代价为 Θ(log a) 步，与普通欧几里得算法相同。<sup>[3](https://www.usna.edu/Users/cs/wcbrown/courses/S17SI335/notes/03/notes.html)</sup> 递归形式在递归回溯时跟踪系数：当第二个参数为 0 时，基本情况返回 (m, 1, 0)；递归调用 (n, m mod n) 后，该步返回 (d, y, x − ⌊m/n⌋·y)。<sup>[7](https://people.eecs.berkeley.edu/~demmel/cs70_Spr11/Lectures/CS70_Lecture10_Feb09.html)</sup>

朴素的替代方法是逐一测试候选 x 直到 a·x mod m 等于 1，这可行，但规模随 m 而非 log m 增长，对于密码学中使用的大数不切实际。<sup>[3](https://www.usna.edu/Users/cs/wcbrown/courses/S17SI335/notes/03/notes.html)</sup>

## 用幂运算计算逆元

欧拉定理指出，当 b 与 m 互素时，b^(φ(m)) ≡ 1 (mod m)，其中 φ 是欧拉函数。对两边适当乘方得 b⁻¹ = b^(φ(m)−1) (mod m)。<sup>[1](https://mathworld.wolfram.com/ModularInverse.html)</sup> 当模数 p 为素数时，φ(p) = p − 1，因此任何非零元素的逆元为 b^(p−2)；这是费马小定理的推论。<sup>[1](https://mathworld.wolfram.com/ModularInverse.html)</sup>

这种幂运算方法简单且天然是常数时间的，因为指数只依赖于模数，但它需要一次完整的模幂运算。2019年，伊利诺伊大学芝加哥分校的数学家与密码学家丹尼尔·J.伯恩斯坦（Daniel J. Bernstein）和中央研究院的杨伯因（Bo-Yin Yang）分析了成本：在素数域中用普通竖式乘法将 1/x 计算为 x^(p−2) 需要 n^(3+o(1)) 次位运算，而欧几里得算法的扩展需要 n^(2+o(1))，复杂度的指数整整相差 1，欧几里得方法更优。<sup>[8](https://ches.iacr.org/2019/src/slides/Day1/Session4_EfficientImp_onlypdfs/Paper2_20190826-2.pdf)</sup>

## 在密码学中的作用

模逆运算是公钥系统中的核心步骤。在 RSA 密钥生成中，私钥指数 d 定义为公钥指数 e 模 φ(n) 的乘法逆元，其中 φ(n) 是模数 n 的欧拉函数值；e·d ≡ 1 (mod φ(n)) 这一关系正是解密能够撤销加密的原因。<sup>[4](https://engineering.purdue.edu/kak/compsec/NewLectures/Lecture12.pdf)</sup> 同样的逆运算出现在数字签名中：NIST 的 FIPS 186-5，即2023年2月定稿的数字签名标准，要求在 ECDSA 签名生成中计算每条消息的秘密 k 模 n 的逆元，在验证中计算签名分量 s 的逆元，并在其附录 B.1 中提供了参考的扩展欧几里得风格例程，同时允许其他产生等价结果的常数时间算法。<sup>[9](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-5.pdf)</sup>

**侧信道约束。** 在椭圆曲线密码学中，逆运算的实现比加法或乘法更复杂，而可变时间的欧几里得算法会通过执行时间泄露关于秘密值的信息。常数时间模逆（CTMI）算法解决了这一问题。伯恩斯坦–杨的 divstep 算法已被采纳为 CTMI 的事实标准，而提交至 NIST 后量子标准化第三轮的 NTRU 后量子方案的逆算法就是基于它的常数时间算法，运行固定的 2N + 1 次迭代来求一个 N 次多项式的逆。<sup>[10](https://ken.ieice.org/ken/paper/202603030cTZ/eng/)</sup><sup> • </sup><sup>[11](https://link.springer.com/article/10.1007/s13389-025-00380-w)</sup> 伯恩斯坦和杨自己的算法在常数时间内运行的同时达到 n^(1+o(1)) 次位运算，递归调用之间不需要除法子程序。<sup>[8](https://ches.iacr.org/2019/src/slides/Day1/Session4_EfficientImp_onlypdfs/Paper2_20190826-2.pdf)</sup>

## 在软件中

通用算术库直接提供逆运算。GMP 的 `mpz_invert(rop, op1, op2)` 计算 op1 模 op2 的逆元，成功时返回非零值，不存在逆元时返回 0；内部调用扩展 GCD 例程，但不必把两个贝祖系数都算出来，从而节省时间。<sup>[6](https://gmplib.org/gmp-man-6.3.0.pdf)</sup><sup> • </sup><sup>[12](https://gmplib.org/list-archives/gmp-discuss/2002-November/000057.html)</sup> Python 在 3.8 版中将该运算内置到三参数 `pow` 中，当底数与模数互素时允许负指数：`pow(38, -1, mod=97)` 返回 23，而 23 · 38 mod 97 确实等于 1。<sup>[5](https://docs.python.org/3/library/functions.html)</sup> Wolfram 语言提供 `ModularInverse[b, m]` 和 `PowerMod[b, -1, m]`。<sup>[1](https://mathworld.wolfram.com/ModularInverse.html)</sup>

## References

1. [Weisstein, Eric W. "Modular Inverse." MathWorld – A Wolfram Resource.](https://mathworld.wolfram.com/ModularInverse.html)
2. [Gauss: "Disquisitiones Arithmeticae", MacTutor History of Mathematics, University of St Andrews.](https://mathshistory.st-andrews.ac.uk/Extras/Gauss_Disquisitiones/)
3. [Brown, W. C. SI335 Unit 3: Number-Theoretic Computations, United States Naval Academy.](https://www.usna.edu/Users/cs/wcbrown/courses/S17SI335/notes/03/notes.html)
4. [Kak, A. Lecture 12: Public-Key Cryptography and the RSA, Purdue University.](https://engineering.purdue.edu/kak/compsec/NewLectures/Lecture12.pdf)
5. [Built-in Functions, Python 3 documentation.](https://docs.python.org/3/library/functions.html)
6. [GNU MP 6.3.0 Manual, Chapter 5: Integer Functions.](https://gmplib.org/gmp-man-6.3.0.pdf)
7. [Demmel, J. CS 70 Lecture 10: Modular Arithmetic, UC Berkeley.](https://people.eecs.berkeley.edu/~demmel/cs70_Spr11/Lectures/CS70_Lecture10_Feb09.html)
8. [Bernstein, D. J. and Yang, B.-Y. Fast Constant-Time GCD Computation and Modular Inversion, CHES 2019.](https://ches.iacr.org/2019/src/slides/Day1/Session4_EfficientImp_onlypdfs/Paper2_20190826-2.pdf)
9. [FIPS 186-5: Digital Signature Standard, NIST, February 2023.](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-5.pdf)
10. [Ozaki, T. and Miyaji, A. Revisited Constant-Time Modular Inversion, IEICE Tech. Rep. ICSS2025-110.](https://ken.ieice.org/ken/paper/202603030cTZ/eng/)
11. [Fast polynomial inversion algorithms for the post-quantum cryptography, Journal of Cryptographic Engineering (2025).](https://link.springer.com/article/10.1007/s13389-025-00380-w)
12. [Granlund, T. mpz_invert function, GMP discussion list, November 2002.](https://gmplib.org/list-archives/gmp-discuss/2002-November/000057.html)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 17, 2026; Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 物质世界与数学 › 数学与统计学 › 数与代数 › Number theory › Elementary number theory › Congruences and modular arithmetic

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/modular-multiplicative-inverse

English original: https://www.edgepedia.org/modular-multiplicative-inverse
