# 模幂运算

**模幂运算**（Modular exponentiation）是指计算一个底数的某次幂除以模数后的余数，即对非负整数 \( a \)、\( b \) 和正整数 \( n \) 求 \( a^{b} \bmod n \) 的值。它是数论计算和公钥密码学中的核心运算，通常用平方-乘算法（也称二进制幂法或反复平方法）高效计算，该算法利用指数的二进制表示。<sup>[1](https://archive.nptel.ac.in/content/storage2/courses/106103015/module4/lec2/1.html)</sup><sup> • </sup><sup>[2](https://arxiv.org/abs/2606.00958)</sup>

| 关键事实 | 详情 |
|---|---|
| 问题 | 对非负 \( a \)、\( b \) 和正 \( n \) 计算 \( a^{b} \bmod n \)；所有值均为 \( \beta \) 位数，其中 \( \beta \approx \lceil \log b \rceil \)<sup>[1](https://archive.nptel.ac.in/content/storage2/courses/106103015/module4/lec2/1.html)</sup> |
| 标准方法 | 平方-乘法（二进制幂法、反复平方法）：将指数写成二进制，每一步平方，当指数位为 1 时再乘以底数<sup>[2](https://arxiv.org/abs/2606.00958)</sup><sup> • </sup><sup>[3](https://citeseerx.ist.psu.edu/document?doi=67bcdbb0f32538c9de30ea35ff08fe117a44802d&repid=rep1&type=pdf)</sup> |
| 代价 | \( O(\log b) \) 次模乘法；每次乘法和约减的代价为 \( \Theta((\log n)^{2}) \)，故总代价为 \( \Theta(\log b \cdot (\log n)^{2}) \)<sup>[4](https://crypto.stanford.edu/pbc/notes/numbertheory/exp.html)</sup><sup> • </sup><sup>[5](https://www.usna.edu/Users/cs/wcbrown/courses/S17SI335/notes/03/notes.html)</sup> |
| 为何必须约减 | 每次乘法后对 \( n \) 取模可使中间值保持较小；若先算出 \( a^{b} \) 再在最后约减，所得数字将大到无法存储，且不是输入规模的多项式时间算法<sup>[1](https://archive.nptel.ac.in/content/storage2/courses/106103015/module4/lec2/1.html)</sup> |
| 密码学作用 | RSA、Diffie-Hellman、ElGamal 和数字签名算法（DSA）的核心<sup>[6](https://csrc.nist.gov/projects/cryptographic-algorithm-validation-program/details?validation=35302)</sup> |
| 安全性警示 | 简单的平方-乘算法会通过时序泄漏指数位，因此私钥运算需要常数时间实现（如蒙哥马利阶梯）并辅以盲化<sup>[7](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-rsa-guidance-07)</sup> |
| 量子相关性 | 模幂运算是 Shor 因数分解算法中耗时和占用空间最多的部分<sup>[8](https://arxiv.org/abs/2306.09122)</sup> |

## 平方-乘算法

计算 \( a^{b} \bmod n \) 的朴素做法是将 \( a \) 自乘 \( b \) 次，每步之后约减。这需要 \( b \) 次乘法，是指数时间的——因为输入长度是 \( \log b \)，而不是 \( b \)。<sup>[1](https://archive.nptel.ac.in/content/storage2/courses/106103015/module4/lec2/1.html)</sup><sup> • </sup><sup>[2](https://arxiv.org/abs/2606.00958)</sup>

平方-乘算法利用指数的二进制表示。将 \( E = \sum_{i=0}^{n-1} e_{i} 2^{i} \) 写成各位 \( e_{i} \in \{0, 1\} \) 的形式，从左到右的形式从最高位处理到最低位：以 \( Z = X \) 开始，然后对每个剩余位将累乘器模 \( M \) 平方，当该位为 1 时再额外模 \( M \) 乘以 \( X \)。<sup>[3](https://citeseerx.ist.psu.edu/document?doi=67bcdbb0f32538c9de30ea35ff08fe117a44802d&repid=rep1&type=pdf)</sup> 每一步以一次平方的代价将剩余指数减半，遇到奇数步时再多做一次乘法。<sup>[9](https://datafield.dev/discrete-mathematics/part-04/chapter-23/)</sup>

一个计算实例展示了具体机制。计算 \( 37^{82} \bmod 52 \) 时，将 \( 82 = 64 + 16 + 2 \) 写出。反复平方并约减得 \( 37^{2} \equiv 17 \)、\( 37^{4} \equiv 29 \)、\( 37^{8} \equiv 9 \)、\( 37^{16} \equiv 29 \)、\( 37^{32} \equiv 9 \) 和 \( 37^{64} \equiv 29 \pmod{52} \)。将对应置位的项相乘，\( 17 \cdot 29 \cdot 29 \equiv 49 \pmod{52} \)。<sup>[10](https://www.math.wichita.edu/discrete-book/section-numtheory-modularexp.html)</sup>

同样的技巧也适用于普通整数幂运算，但那里乘法不是模乘法，且每次乘法耗时至少是前一次的两倍，因为数字不断增大；正是模约减使每个中间值保持在模数以下。<sup>[4](https://crypto.stanford.edu/pbc/notes/numbertheory/exp.html)</sup>

## 代价与复杂度

反复平方用 \( O(\log k) \) 次模乘法计算 \( a^{k} \bmod n \)。<sup>[4](https://crypto.stanford.edu/pbc/notes/numbertheory/exp.html)</sup> 更精确地说，平方-乘法需要 \( \Theta(\log e) \) 次模 \( n \) 乘法，而每次对 \( \log n \) 位数的乘法按竖式乘法计算的代价为 \( \Theta((\log n)^{2}) \)。<sup>[5](https://www.usna.edu/Users/cs/wcbrown/courses/S17SI335/notes/03/notes.html)</sup> 因此该算法在其输入规模上是多项式的，实际上是线性的，而朴素方法在输入规模上是指数的。<sup>[2](https://arxiv.org/abs/2606.00958)</sup>

乘法次数可以进一步减少。寻找计算 \( x^{n} \) 的最经济方式归结为寻找短加法链的问题——加法链是每一项为此前某两项之和的序列，因为指数是可加的。平方-乘法对应由指数二进制表示确定的加法链，而最短加法链所需的乘法次数从不超过它，通常更少。<sup>[2](https://arxiv.org/abs/2606.00958)</sup> m 元法和滑动窗口法生成相对较短的加法链，还有人提出了迭代滑动窗口法等变体以进一步缩短乘法序列。<sup>[11](https://www.sciencedirect.com/science/article/abs/pii/S1568494610002711)</sup><sup> • </sup><sup>[12](https://www.tandfonline.com/doi/full/10.1080/23311916.2017.1304499)</sup> 其他方法包括用于模幂的双基数数系，以及在模数分解已知时的专用快速方法。<sup>[13](https://www.sciencedirect.com/science/article/pii/S0020019098000441)</sup><sup> • </sup><sup>[14](https://arxiv.org/abs/2401.10497)</sup>

## 在密码学中的作用

模幂运算是主要公钥密码系统的核心运算。硬件和软件实现专门针对计算 \( Z = Y^{e} \bmod M \) 进行验证，该运算支撑着 RSA、Diffie-Hellman、ElGamal 和 DSA，进而支撑 TLS/SSL 和 IPsec 中使用的密钥交换和基于证书的认证。<sup>[6](https://csrc.nist.gov/projects/cryptographic-algorithm-validation-program/details?validation=35302)</sup> RSA 的密钥生成、加密、解密和签名都归结为带大指数的模幂运算。<sup>[15](https://math.mit.edu/~goemans/18310S15/RSA-notes.pdf)</sup>

由于这些幂运算涉及数百或数千位的数字，模乘法本身主导了代价，而无需除法的乘法与模约减合并步骤——蒙哥马利乘法（Montgomery multiplication）——被广泛用于此目的。对五种蒙哥马利乘法算法的分析发现，粗粒度集成操作数扫描（CIOS）方法在所考虑的一般处理器类别中最高效。<sup>[16](https://www.microsoft.com/en-us/research/publication/analyzing-and-comparing-montgomery-multiplication-algorithms/)</sup> 对于 RSA 私钥运算，中国剩余定理优化将计算拆分为分别对模数的两个素因子取模的幂运算。<sup>[7](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-rsa-guidance-07)</sup>

**侧信道。** 简单的平方-乘算法在私钥运算中存在时序侧信道风险：跳过乘法的步骤会因缺少乘法操作而泄漏指数位。因此 RSA 的安全指南要求幂运算相对于指数是常数时间的，推荐采用每步同时执行平方和乘法的蒙哥马利阶梯，并推荐底数盲化和指数盲化作为进一步防护。<sup>[7](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-rsa-guidance-07)</sup> 对 RSA 的时序攻击最早由 Paul Kocher 于 1996 年演示，RSA 盲化被认为是有效的防御手段。<sup>[17](https://csrc.nist.gov/CSRC/media/projects/cryptographic-module-validation-program/documents/security-policies/140sp4801.pdf)</sup>

**困难性假设。** 这些方案的安全性建立在幂运算的容易与其逆运算的困难之间的差距上。计算 \( a^{b} \bmod n \) 是多项式时间的，但离散对数问题——即寻找满足 \( g^{x} \equiv h \pmod{p} \) 的 \( x \)——没有已知的快速算法；已知最好的离散对数算法比逐个尝试所有元素快，但不是多项式时间的。<sup>[4](https://crypto.stanford.edu/pbc/notes/numbertheory/exp.html)</sup>

模幂运算也出现在素性检验中。费马检验对随机底数 \( a \) 用反复平方计算 \( a^{n-1} \bmod n \)；结果不为 1 即可证明 \( n \) 是合数，而 Miller-Rabin 检验通过沿途检查 1 的非平凡平方根来加强这一点。<sup>[15](https://math.mit.edu/~goemans/18310S15/RSA-notes.pdf)</sup>

## 历史

平方-乘法的历史起源无法确知，但可以通过原始文献追溯出一条有据可查的演进脉络。<sup>[2](https://arxiv.org/abs/2606.00958)</sup>

- **平伽拉（Pingala，约公元前 200 年）。** 关于这种计算方法最早已知的工作出现在古印度平伽拉的韵律研究中，背景是计算诗律模式。它还不是完全发展的一般技术，但似乎包含了该算法的概念基础——利用正整数的二进制表示。<sup>[2](https://arxiv.org/abs/2606.00958)</sup>
- **乌格利迪西与比鲁尼。** 大马士革的算术家乌格利迪西（al-Uqlidisi，约公元 952 年）清晰讨论了如何对任意 \( n \) 高效计算 \( 2^{n} \)，并将其作为一般程序呈现；比鲁尼（al-Biruni）在棋盘问题的背景下应用了逐次平方，从第 5 格到第 65 格追踪了一条固定的四次平方链。两人都未将该方法形式化为一般规则。<sup>[2](https://arxiv.org/abs/2606.00958)</sup>
- **卡西（al-Kashi，1427 年）。** 贾姆希德·卡西十五世纪的《算术之钥》（*Miftah al-Hisab*）明确将该算法表述为一般方法，归类为“规则”并作为独立程序呈现，卡西声称这是他自己的创新，尽管其基本步骤早先的学者已经使用过。<sup>[2](https://arxiv.org/abs/2606.00958)</sup>
- **勒让德（Legendre，1798 年）与高德纳。** 勒让德 1798 年著作中的一个例子表明，平方-乘法已被一位欧洲顶尖数学家当作熟悉的计算工具使用。标准的现代论述及历史注释见于高德纳（Donald E. Knuth）的《计算机程序设计艺术》第 2 卷，算法由此进入其在现代密码学和计算数论中的核心地位。<sup>[2](https://arxiv.org/abs/2606.00958)</sup>

## 量子计算

在 Shor 整数因数分解算法中，模幂运算是部署大部分量子资源的基本组件，也是计算中耗时和占用空间最多的部分。<sup>[8](https://arxiv.org/abs/2306.09122)</sup><sup> • </sup><sup>[18](https://fy.chalmers.se/~delsing/QI/Shor-JOC-97.pdf)</sup> 量子电路将幂运算归约为一系列以常数 \( a^{2^{i}} \) 为乘数的受控模乘法，作用于初始状态为 \( |1\rangle \) 的寄存器，而控制寄存器处于指数位的叠加态上。<sup>[19](https://www.cs.sfu.ca/~meamy/Teaching/cmpt981/Shor.pdf)</sup> 由于电路必须可逆，所需的辅助内存随待分解数字的大小线性增长。<sup>[20](https://arxiv.org/abs/quant-ph/9511018)</sup> 在实际实现中，\( k = 1, 2, 4, \dots \) 时的 \( a^{k} \bmod N \) 可以在构建相应乘法电路之前用迭代平方经典地计算出来；对于诸如分解 \( N = 15 \) 这样的小型演示，模幂算子通常作为针对特定情况专门构建的电路来实现。<sup>[21](https://quantum.cloud.ibm.com/docs/en/tutorials/shors-algorithm)</sup>

## References

1. [Lecture 2: Modular exponentiation, NPTEL course on modular arithmetic](https://archive.nptel.ac.in/content/storage2/courses/106103015/module4/lec2/1.html)
2. [Nuh Aydin, Mohammad K. Azarian, Omid Khormali, Ghaya Mtimet. On the History of the Square and Multiply Algorithm. arXiv.](https://arxiv.org/abs/2606.00958)
3. [Modular Exponentiation on Reconfigurable Hardware, Chapter 4: Preliminaries (CiteSeerX)](https://citeseerx.ist.psu.edu/document?doi=67bcdbb0f32538c9de30ea35ff08fe117a44802d&repid=rep1&type=pdf)
4. [Number Theory: Modular Exponentiation, Stanford PBC notes](https://crypto.stanford.edu/pbc/notes/numbertheory/exp.html)
5. [SI335 Unit 3: Number-Theoretic Computations, US Naval Academy](https://www.usna.edu/Users/cs/wcbrown/courses/S17SI335/notes/03/notes.html)
6. [Cryptographic Algorithm Validation Program: Helion Modular Exponentiation IP Core, NIST CSRC](https://csrc.nist.gov/projects/cryptographic-algorithm-validation-program/details?validation=35302)
7. [draft-irtf-cfrg-rsa-guidance-07, IETF Datatracker](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-rsa-guidance-07)
8. [Shor's Factoring Algorithm and Modular Exponentiation Operators. arXiv.](https://arxiv.org/abs/2306.09122)
9. [Chapter 23: Modular Arithmetic, Discrete Mathematics](https://datafield.dev/discrete-mathematics/part-04/chapter-23/)
10. [Modular exponentiation, Discrete Mathematics textbook, Wichita State University](https://www.math.wichita.edu/discrete-book/section-numtheory-modularexp.html)
11. [High-performance SoC-based implementation of modular exponentiation using evolutionary addition chains, ScienceDirect](https://www.sciencedirect.com/science/article/abs/pii/S1568494610002711)
12. [Iterative sliding window method for shorter number of operations in modular exponentiation and scalar multiplication, Cogent Engineering](https://www.tandfonline.com/doi/full/10.1080/23311916.2017.1304499)
13. [An algorithm for modular exponentiation, Information Processing Letters, ScienceDirect](https://www.sciencedirect.com/science/article/pii/S0020019098000441)
14. [An Elementary Method For Fast Modular Exponentiation With Factored Modulus. arXiv.](https://arxiv.org/abs/2401.10497)
15. [18.310 lecture notes on RSA and repeated squaring, MIT](https://math.mit.edu/~goemans/18310S15/RSA-notes.pdf)
16. [Analyzing and Comparing Montgomery Multiplication Algorithms, Microsoft Research](https://www.microsoft.com/en-us/research/publication/analyzing-and-comparing-montgomery-multiplication-algorithms/)
17. [FIPS 140-3 Non-Proprietary Security Policy (Oracle Linux 9 NSS Cryptographic Module), NIST CSRC](https://csrc.nist.gov/CSRC/media/projects/cryptographic-module-validation-program/documents/security-policies/140sp4801.pdf)
18. [Shor, Polynomial-Time Algorithms for Prime Factorization and Discrete Logarithms on a Quantum Computer, SIAM J. Comput.](https://fy.chalmers.se/~delsing/QI/Shor-JOC-97.pdf)
19. [Quantum circuit for modular exponentiation, CMPT 981 course notes, Simon Fraser University](https://www.cs.sfu.ca/~meamy/Teaching/cmpt981/Shor.pdf)
20. [Quantum Networks for Elementary Arithmetic Operations. arXiv.](https://arxiv.org/abs/quant-ph/9511018)
21. [Shor's algorithm tutorial, IBM Quantum Documentation](https://quantum.cloud.ibm.com/docs/en/tutorials/shors-algorithm)

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

Topic: 物质世界与数学 › 数学与统计学 › 数与代数 › Computational and symbolic algebra › Symbolic and algebraic algorithms › Exact and arbitrary-precision arithmetic

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/modular-exponentiation

English original: https://www.edgepedia.org/modular-exponentiation
