# 平方求幂

**平方求幂**（exponentiation by squaring），又称二进制幂法或平方-乘算法，是一种计算某个数的大正整数次幂的方法，也可用于计算半群中任意元素（如多项式或方阵）的幂；它所需的乘法次数与指数的对数成正比，而非与指数本身成正比。<sup>[1](https://graphsearch.epfl.ch/en/concept/10237)</sup><sup> • </sup><sup>[2](https://github.com/cp-algorithms/cp-algorithms/blob/main/src/algebra/binary-exp.md)</sup> 朴素做法是把底数自乘 \( n - 1 \) 次来计算 \( a^n \)；二进制幂法只需 \( O(\log n) \) 次乘法。<sup>[2](https://github.com/cp-algorithms/cp-algorithms/blob/main/src/algebra/binary-exp.md)</sup>

| 关键事实 | 细节 |
|---|---|
| 核心恒等式 | \( a^{2k} = (a^k)^2 \) 与 \( a^{2k+1} = (a^k)^2 \cdot a \)，因此每一步都把指数减半<sup>[3](https://en.algorithmica.org/hpc/number-theory/exponentiation/)</sup> |
| 代价 | \( O(\log n) \) 次乘法，而非朴素方法的 \( O(n) \) 次<sup>[2](https://github.com/cp-algorithms/cp-algorithms/blob/main/src/algebra/binary-exp.md)</sup> |
| 普适性 | 对任何满足结合律的运算都成立，因此可用于模乘法、矩阵乘法、多项式以及其他半群元素<sup>[1](https://graphsearch.epfl.ch/en/concept/10237)</sup><sup> • </sup><sup>[2](https://github.com/cp-algorithms/cp-algorithms/blob/main/src/algebra/binary-exp.md)</sup> |
| 最早踪迹 | 约公元前200年古印度平伽罗（Pingala）的韵律学研究中，已包含利用指数的二进制表示这一概念基础<sup>[4](https://arxiv.org/abs/2606.00958)</sup> |
| 密码学作用 | 是实用 RSA、Diffie-Hellman、ElGamal 及椭圆曲线实现的关键要素，也是时序侧信道攻击的目标<sup>[4](https://arxiv.org/abs/2606.00958)</sup><sup> • </sup><sup>[5](https://csrc.nist.gov/csrc/media/presentations/2025/systematic-timing-leakage-analysis-%281%29/systematic_timing_leakage_analysis-al-ameen_1.11%20.pdf)</sup> |

## 工作原理

该方法建立在由指数律 \( a^{b+c} = a^b \cdot a^c \) 导出的两个恒等式之上：平方使指数加倍，乘以底数则使指数加一。<sup>[2](https://github.com/cp-algorithms/cp-algorithms/blob/main/src/algebra/binary-exp.md)</sup> 由此得到递推式

\[ a^n = \begin{cases} 1 & n = 0 \\ \left(a^{\lfloor n/2 \rfloor}\right)^2 & n \text{ 为偶数} \\ \left(a^{\lfloor n/2 \rfloor}\right)^2 \cdot a & n \text{ 为奇数} \end{cases} \]

于是计算 \( a^n \) 就归结为计算 \( a^{\lfloor n/2 \rfloor} \)、将其平方，并在 \( n \) 为奇数时再乘一次 \( a \)。<sup>[3](https://en.algorithmica.org/hpc/number-theory/exponentiation/)</sup> 等价地说，该算法逐位读取指数：从值 1 出发，从高位到低位逐位处理，每处理一位先将当前值平方，遇到该位为 1 时再乘以底数。<sup>[6](https://tsapps.nist.gov/publication/get_pdf.cfm?pub_id=934860)</sup>

一个具体的例子利用指数的二进制展开。要计算 \( 3^{13} \)，把 13 写成 \( 1101_2 \)，于是 \( 3^{13} = 3^8 \cdot 3^4 \cdot 3^1 \)。所需的各次幂得来很省，因为每一个都是前一个的平方：\( 3^2 = 9 \)、\( 3^4 = 81 \)、\( 3^8 = 6561 \)，答案为 \( 6561 \cdot 81 \cdot 3 = 1594323 \)。由于 \( n \) 有 \( \lfloor \log_2 n \rfloor + 1 \) 个二进制位，总的工作量为 \( O(\log n) \) 次乘法。<sup>[2](https://github.com/cp-algorithms/cp-algorithms/blob/main/src/algebra/binary-exp.md)</sup>

由于唯一的要求是结合律，即 \( (X \cdot Y) \cdot Z = X \cdot (Y \cdot Z) \)，同一算法可用来计算矩阵、多项式以及任何半群中元素的幂，而不限于数。<sup>[2](https://github.com/cp-algorithms/cp-algorithms/blob/main/src/algebra/binary-exp.md)</sup>

## 与加法链的关系

\( n \) 的一条加法链是一个序列 \( 1 = a_0 \le a_1 \le \cdots \le a_r = n \)，其中每一项都是此前某两项之和；长度为 \( r \) 的链使人可以用 \( r \) 次乘法从 \( x \) 算出 \( x^n \)。<sup>[7](https://www.sciencedirect.com/science/article/am/pii/S0304397517308666)</sup>

链长 \( \ell(n) \) 满足界 \( \log_2 n \le \ell(n) \le \lfloor \log_2 n \rfloor + \nu_2(n) - 1 \)，其中 \( \nu_2(n) \) 统计 \( n \) 的二进制展开中 1 的个数；上界正是二进制方法所达到的。布劳尔（Brauer）于1939年证明 \( \ell(n) \sim \log_2 n \)。<sup>[7](https://www.sciencedirect.com/science/article/am/pii/S0304397517308666)</sup> 寻找最短的链在计算上是困难的：对一组整数计算其链长是 NP 完全的，因此实用的求幂采用次优的链，如二进制法、窗口法和 \( 2^k \) 进制法。<sup>[8](https://www.researchgate.net/publication/266286413_Efficient_computation_of_addition_chains)</sup><sup> • </sup><sup>[9](https://koclab.cs.ucsb.edu/teaching/cren/project/2018/Schibler.pdf)</sup> 一个长期悬而未决的问题——肖尔茨–布劳尔猜想（Scholz–Brauer conjecture）——断言 \( \ell(2^n - 1) \le n + \ell(n) - 1 \)。<sup>[7](https://www.sciencedirect.com/science/article/am/pii/S0304397517308666)</sup>

## 密码学与侧信道

公钥密码体制要把群元素提升到很大的幂，而这一求幂的代价往往决定一个体制是否实用；最佳方法取决于群、硬件，以及底数、指数抑或两者是否变化。<sup>[10](https://www.sciencedirect.com/science/article/pii/S0196677497909135)</sup> 例如 RSA 解密要计算 \( m = c^d \bmod N \)，平方-乘算法使之变得实用，因为乘法次数随指数的位长而非其数值线性增长。<sup>[6](https://tsapps.nist.gov/publication/get_pdf.cfm?pub_id=934860)</sup>

同样的逐位结构也带来了安全问题。保罗·科赫尔（Paul Kocher）于1996年提出了针对 RSA 的平方-乘时序攻击，后来的工作则强调盲化与常数时间实现。<sup>[5](https://csrc.nist.gov/csrc/media/presentations/2025/systematic-timing-leakage-analysis-%281%29/systematic_timing_leakage_analysis-al-ameen_1.11%20.pdf)</sup> 直白的实现会依据指数的每一位是 0 还是 1 而表现不同，从而使侧信道攻击成为可能；实现应当在每一位上表现一致。<sup>[6](https://tsapps.nist.gov/publication/get_pdf.cfm?pub_id=934860)</sup> 已有文献记录的攻击利用了这一模式：libgcrypt 在 `_gcry_mpi_powm` 中采用依赖秘密数据的平方-乘算法，通过环形互连信道泄露密钥位，其中平方阶段无条件执行，而乘法阶段仅在相应位为 1 时执行。<sup>[11](https://www.usenix.org/system/files/sec21-paccagnella.pdf)</sup> 针对 OpenSSL 模幂运算的缓存攻击通过追踪预计算乘数的使用序列，恢复了超过一半的指数位。<sup>[12](https://www.usenix.org/system/files/sec20-garcia.pdf)</sup> 应对措施包括采用常数时间变体：OpenSSL 的 `BN_mod_exp_mont_consttime` 使用固定窗口和特殊的预计算内存布局，在保护秘密指数时限制数据依赖性，<sup>[13](https://docs.openssl.org/3.4/man3/BN_mod_exp_mont/)</sup> 而有些硬件会在平方循环内无条件执行乘法，使运算以常数时间运行。<sup>[14](https://csrc.nist.gov/csrc/media/projects/cryptographic-module-validation-program/documents/security-policies/140sp392.pdf)</sup>

## 软件实现

语言运行时和库在多个层面内置了这一方法。Python 的三参数函数 `pow(base, exp, mod)` 计算模幂，比先求幂再取模更高效；从 Python 3.8 起，它允许在指定模数时使用负指数来计算模逆元。<sup>[15](https://docs.python.org/3.11/library/functions.html?highlight=hash)</sup> CPython 的整数求幂采用从左到右的二进制算法，除非指数超过 60 位，此时便改用每次处理 5 位的滑动窗口法；所有方法对指数的每一位都需要进行一次平方，而窗口法用一张预先计算的小次幂表，换取在值为 1 的位上更少的乘法运算。<sup>[16](https://github.com/python/cpython/blob/main/Objects/longobject.c)</sup> 对方阵 \( A \) 和正整数 \( B \)，MATLAB 通过反复平方计算 \( A^B \)，对于其他指数则改用特征值分解或舒尔分解。<sup>[17](https://www.mathworks.com/help/matlab/ref/mpower.html)</sup>

同样的平方思想也出现在整数次幂以外的数值线性代数中。计算矩阵指数的缩放平方法先以 2 的幂为因子缩放矩阵，求出帕德近似，再反复平方以抵消缩放；MATLAB 的 `expm` 实现的就是这一方法。曼彻斯特大学数值分析教授尼古拉斯·J. 海厄姆（Nicholas J. Higham）于2005年表明，对于 IEEE 双精度，最佳的帕德近似次数是 13，而非此前使用的 6 或 8；当范数超过 1 时，改进后的实现可节省多达 37% 的矩阵乘法。<sup>[18](https://epubs.siam.org/doi/abs/10.1137/04061101X)</sup>

## 历史

这一算法的历史起源尚无定论，证据表明它曾多次独立出现，而非连续传承。<sup>[4](https://arxiv.org/abs/2606.00958)</sup><sup> • </sup><sup>[19](https://papers.ssrn.com/sol3/papers.cfm?abstract_id=7237838)</sup> 已知最早的先例见于约公元前200年古印度平伽罗的韵律学研究，其中包含利用正整数二进制表示的概念基础，但尚未形成充分发展的通用技术。<sup>[4](https://arxiv.org/abs/2606.00958)</sup> 在乌克利迪西（al-Uqlidisi）和比鲁尼（al-Biruni）的著作中，可以找到较早以连续平方进行特定计算的实例，但他们并未将这一过程形式化。<sup>[4](https://arxiv.org/abs/2606.00958)</sup> 十五世纪，贾姆希德·卡西（Jamshid al-Kashi）的《算术之钥》（Miftah al-Hisab）明确将该算法阐述为一种通用方法，并称其为自己的创新。<sup>[4](https://arxiv.org/abs/2606.00958)</sup> 在欧洲，法国数学家阿德里安-马里·勒让德（Adrien-Marie Legendre）于1798年给出的一个例子，展示了这一方法的一次早期应用；他以数论和天体力学方面的工作最为知名。<sup>[19](https://papers.ssrn.com/sol3/papers.cfm?abstract_id=7237838)</sup>

## References

1. [Exponentiation by squaring, EPFL Graph Search](https://graphsearch.epfl.ch/en/concept/10237)
2. [Binary Exponentiation, cp-algorithms](https://github.com/cp-algorithms/cp-algorithms/blob/main/src/algebra/binary-exp.md)
3. [Binary exponentiation, Algorithmica](https://en.algorithmica.org/hpc/number-theory/exponentiation/)
4. [Nuh Aydin, Mohammad K. Azarian, Omid Khormali, Ghaya Mtimet. On the History of the Square and Multiply Algorithm. arXiv:2606.00958](https://arxiv.org/abs/2606.00958)
5. [Systematic Timing Leakage Analysis, NIST presentation (2025)](https://csrc.nist.gov/csrc/media/presentations/2025/systematic-timing-leakage-analysis-%281%29/systematic_timing_leakage_analysis-al-ameen_1.11%20.pdf)
6. [Kaufman, Perlman, Speciner. Network Security: Private Communication in a Public World, 3rd Edition, NIST](https://tsapps.nist.gov/publication/get_pdf.cfm?pub_id=934860)
7. [Internal structure of addition chains: Well-ordering, Theoretical Computer Science](https://www.sciencedirect.com/science/article/am/pii/S0304397517308666)
8. [Efficient computation of addition chains, Journal de Théorie des Nombres de Bordeaux (1994)](https://www.researchgate.net/publication/266286413_Efficient_computation_of_addition_chains)
9. [Binary Method, UCSB course project (2018)](https://koclab.cs.ucsb.edu/teaching/cren/project/2018/Schibler.pdf)
10. [Daniel M. Gordon. A Survey of Fast Exponentiation Methods. Journal of Algorithms 27(1): 129–146 (1998)](https://www.sciencedirect.com/science/article/pii/S0196677497909135)
11. [Paccagnella et al. 5.1 Side Channel Attack On Cryptographic Code, USENIX Security 2021](https://www.usenix.org/system/files/sec21-paccagnella.pdf)
12. [Garcia et al. Cache attack on OpenSSL modular exponentiation, USENIX Security 2020](https://www.usenix.org/system/files/sec20-garcia.pdf)
13. [BN_mod_exp_mont, OpenSSL Documentation 3.4](https://docs.openssl.org/3.4/man3/BN_mod_exp_mont/)
14. [CGX Version 3.18 Security Policy, NIST CMVP](https://csrc.nist.gov/csrc/media/projects/cryptographic-module-validation-program/documents/security-policies/140sp392.pdf)
15. [Built-in Functions, Python 3.11 documentation](https://docs.python.org/3.11/library/functions.html?highlight=hash)
16. [cpython/Objects/longobject.c, Python source](https://github.com/python/cpython/blob/main/Objects/longobject.c)
17. [mpower, Matrix power, MATLAB documentation](https://www.mathworks.com/help/matlab/ref/mpower.html)
18. [Nicholas J. Higham. The Scaling and Squaring Method for the Matrix Exponential Revisited. SIAM J. Matrix Anal. Appl. 26(4): 1179–1193 (2005)](https://epubs.siam.org/doi/abs/10.1137/04061101X)
19. [On the History of the Square-and-Multiply Algorithm, SSRN](https://papers.ssrn.com/sol3/papers.cfm?abstract_id=7237838)

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

Topic: 物质世界与数学 › 数学与统计学 › 数学分析与数学模型 › Numerical analysis and computation

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/exponentiation-by-squaring

English original: https://www.edgepedia.org/exponentiation-by-squaring
