# 素性测试

素性测试是一种判定给定整数是素数还是合数的算法。这个问题由来已久，但其现代形式是计算性的：找到一种能正确且快速给出答案的流程，理想情况下其运行时间随输入位数的多项式增长。若干密码学协议需要大素数，这使得高效测试既是数学上的必需，也是实际应用中的必需<sup>[1](https://paperzz.com/doc/7400128/primes.pdf)</sup>。

| 关键事实 | 详情 |
|---|---|
| 经典方法 | 用不超过√n的每个整数进行试除，自古希腊起就为人所知，是埃拉托斯特尼筛法（约公元前240年）的一个特例<sup>[1](https://paperzz.com/doc/7400128/primes.pdf)</sup> |
| 理论基础 | 大多数方法建立在费马小定理的变体之上：若p为素数且a不被p整除，则a^(p−1) = 1 (mod p)<sup>[2](https://cse.iitk.ac.in/users/manindra/algebra/primality_original.pdf)</sup><sup> • </sup><sup>[3](https://www.nationalacademies.org/read/2026/chapter/5)</sup> |
| 主力测试 | Miller–Rabin，一种快速的概率性测试，由加里·米勒（Gary Miller）的确定性测试经拉宾（Rabin）于1980年修改而成<sup>[2](https://cse.iitk.ac.in/users/manindra/algebra/primality_original.pdf)</sup><sup> • </sup><sup>[4](https://onlinelibrary.wiley.com/doi/10.1002/9781118188606.ch3)</sup> |
| 误差界 | 在GMP的实现中，经过reps轮Miller–Rabin之后，合数被报告为素数的概率小于4^(−reps)<sup>[5](https://gmplib.org/gmp-man-6.3.0.pdf)</sup> |
| 里程碑成果 | 阿格拉沃尔、卡亚尔和萨克塞纳于2002年8月6日发布的“PRIMES is in P”，给出了一个无条件的确定性多项式时间测试（AKS）<sup>[6](https://link.springer.com/book/10.1007/b12334)</sup><sup> • </sup><sup>[7](https://annals.math.princeton.edu/2004/160-2/p12)</sup> |
| 实践中的快速证明 | 椭圆曲线素性证明（ECPP），源于1986年戈德瓦瑟（Goldwasser）与基利安（Kilian）以及阿特金（Atkin）的工作，莫兰（Morain）将其描述为证明大数素性最快的实用算法之一<sup>[8](https://www.researchgate.net/publication/221451620_Primality_Proving_Using_Elliptic_Curves_An_Update)</sup><sup> • </sup><sup>[9](https://arxiv.org/abs/math/0502097)</sup> |
| 标准中的使用 | FIPS 186-5指南建议，对RSA素数，先以随机基进行最少轮数的Miller–Rabin测试，随后进行Lucas测试；对于一个2048位的候选数，至少进行两次随机基Miller–Rabin测试<sup>[10](https://csrc.nist.gov/csrc/media/Projects/cryptographic-module-validation-program/documents/fips%20140-3/FIPS%20140-3%20IG.pdf)</sup> |

## 试除法

素性的定义本身就给出了一种测试：尝试用每个不超过√n的数m去除n；若存在某个m整除n，则n为合数，否则为素数。这一流程自古希腊时代起就为人所知，是埃拉托斯特尼筛法的一个特例，后者生成所有小于n的素数<sup>[1](https://paperzz.com/doc/7400128/primes.pdf)</sup>。试除法作为廉价的第一道筛选仍然有用，标准做法是在施加强力测试之前，先用小素数进行若干次试除<sup>[11](https://crypto.stanford.edu/pbc/notes/numbertheory/millerrabin.html)</sup>。其代价大致随√n增长，因此对于密码学中使用的数百位数字而言，它变得毫无希望。

## 费马小定理与概率素数

几乎能给出高效测试的定理是费马小定理：对任意素数p和任意不被p整除的数a，a^(p−1) = 1 (mod p)<sup>[2](https://cse.iitk.ac.in/users/manindra/algebra/primality_original.pdf)</sup>。大多数素性测试方法都基于这一命题的变体<sup>[3](https://www.nationalacademies.org/read/2026/chapter/5)</sup>。对随机基a检验该同余式，就得到一种可能素性测试：大多数合数会在许多基上失败，因此通过若干基的数很可能是素数<sup>[3](https://www.nationalacademies.org/read/2026/chapter/5)</sup>。

其弱点在于，有些合数对每个与n互素的基都能通过。这些就是卡迈克尔数；1729是其中第三个，它仍能骗过增强版费马测试<sup>[11](https://crypto.stanford.edu/pbc/notes/numbertheory/millerrabin.html)</sup>。

## Miller–Rabin

Miller–Rabin测试是一种快速且可用的随机化素性检验方法，有时被称为强伪素性测试，或者——更准确地说——Miller–Selfridge–Rabin测试。<sup>[4](https://onlinelibrary.wiley.com/doi/10.1002/9781118188606.ch3)</sup> 加里·米勒（Gary Miller）设计了一种素性测试，其正确性依赖于黎曼猜想；他的测试经拉宾（Rabin）于1980年修改，产生了一种不依赖此类假设的概率算法<sup>[2](https://cse.iitk.ac.in/users/manindra/algebra/primality_original.pdf)</sup>。米勒算法的概率类比版本也由索洛维（Solovay）与施特拉森（Strassen）于1977年、以及拉宾（Rabin）于1980年分别发展出来<sup>[3](https://www.nationalacademies.org/read/2026/chapter/5)</sup>。

每一轮使用一个基，借助快速模运算可在多项式时间内运行。用独立的随机基重复进行，会成倍降低合数的存活概率：按照GMP库的表述，经过reps轮之后，合数被识别为素数的概率小于4^(−reps)<sup>[5](https://gmplib.org/gmp-man-6.3.0.pdf)</sup>。

Miller–Rabin已被嵌入实际标准之中。FIPS 186-5规定了用于生成和验证素数的概率素性测试，并可在概率方法之前选择进行试除测试<sup>[12](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-5.pdf)</sup>。FIPS 186-5的实施指南建议，在测试RSA素数p和q的概率素性时，采用标准表B.1所示的最少Miller–Rabin测试轮数——例如对一个2048位候选数进行两次随机选择基的测试——随后进行第B.3.3节的Lucas测试<sup>[10](https://csrc.nist.gov/csrc/media/Projects/cryptographic-module-validation-program/documents/fips%20140-3/FIPS%20140-3%20IG.pdf)</sup>。较早的FIPS 186-4则将Miller–Rabin的轮数与错误概率和素数规模挂钩，例如对512位素数采用2^(−80)的错误概率<sup>[13](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf)</sup>。

## AKS测试：PRIMES is in P

2002年8月6日，一篇题为“PRIMES is in P”的论文出现在印度理工学院坎普尔分校的网站上，作者是该机构的曼宁德拉·阿格拉沃尔（Manindra Agrawal）、尼拉杰·卡亚尔（Neeraj Kayal）和尼廷·萨克塞纳（Nitin Saxena）。该文表明素性问题存在一个运行于多项式时间的确定性算法，解决了数十年来在复杂性理论中备受瞩目的一个问题<sup>[6](https://link.springer.com/book/10.1007/b12334)</sup>。该论文提出了一个无条件的确定性多项式时间算法，用于判定输入数是素数还是合数；它于2002年1月24日收稿，2003年3月21日接受，发表在《数学年刊》第160卷（2004年），第781–793页<sup>[7](https://annals.math.princeton.edu/2004/160-2/p12)</sup>。

这一结果确定了素性测试在无需随机性、也无需未证明猜想的情况下的复杂性地位。原始证明使用了富夫里（Fouvry）于1985年提出的一个困难得多的定理，即存在许多素数r，使得r − 1有一个大于r^(2/3)的素因子q<sup>[14](https://cr.yp.to/papers/aks.pdf)</sup>。后来的工作针对若干大类的数改进了该测试，给出了受AKS启发的确定性素性测试<sup>[15](https://arxiv.org/abs/math/0211334)</sup>。

尽管具有理论重要性，AKS并非实践中证明极大数素性的常用工具；诸如PARI/GP之类的软件反而为一般的大输入提供APR-CL测试和阿特金–莫兰的椭圆曲线素性证明器（ECPP）<sup>[16](https://pari.math.u-bordeaux.fr/pub/pari/manuals/2.11.1/users.pdf)</sup>。

## 椭圆曲线素性证明

在朔夫（Schoof）于1986年提出的有限域上椭圆曲线点计数方法的基础上，戈德瓦瑟（Goldwasser）与基利安（Kilian）、以及阿特金（Atkin）分别引入了确立素性的新方法。如今称为ECPP的算法，截至莫兰（Morain）2003年的更新已使用了近十年，该更新报告了理论与实践的改进、各种规模整数的新基准，以及一项新的素性纪录：对2^7331 − 1的2196位十进制数余因子素性的证明<sup>[8](https://www.researchgate.net/publication/221451620_Primality_Proving_Using_Elliptic_Curves_An_Update)</sup>。莫兰（Morain）将ECPP描述为当前证明大数素性最快的实用算法之一<sup>[9](https://arxiv.org/abs/math/0502097)</sup>。

ECPP产生一个证书，即验证者可用来独立确认素性的可检查对象。PARI/GP的`ecpp`函数返回这样的证书，该库还提供`ecppisvalid`来检查证书是否有效<sup>[17](https://pari.math.u-bordeaux1.fr/pub/pari/manuals/2.17.2/libpari.pdf)</sup>。

## 库如何组合这些测试

生产级数论库将各种方法分层组合。GMP的`mpz_probab_prime_p`首先通过若干试除步骤除去小因子，然后按照克努特（Knuth）第4.5.4节算法P，应用Miller–Rabin概率素性测试<sup>[18](https://gmplib.org/manual/Prime-Testing-Algorithm)</sup>。PARI/GP将`ispseudoprime`（仅在高可能性下返回真）与`isprime`（证明素性，速度慢得多）区分开来；其`isprime`目前实现为Baillie–Pomerance–Selfridge–Wagstaff伪素数检验，随后进行严格的证明步骤<sup>[16](https://pari.math.u-bordeaux.fr/pub/pari/manuals/2.11.1/users.pdf)</sup><sup> • </sup><sup>[17](https://pari.math.u-bordeaux1.fr/pub/pari/manuals/2.17.2/libpari.pdf)</sup>。

这两类答案之间的区别正是本主题的实践核心。诸如Miller–Rabin之类的概率测试快速给出答案，且错误率可控地小，这正是密码学中素数生成所需要的；诸如ECPP或AKS之类的证明算法则以更高代价返回确定性。同样的分层也出现在标准中，其中试除法、Miller–Rabin轮次和Lucas测试被组合起来，以达到RSA素数的目标误差界<sup>[10](https://csrc.nist.gov/csrc/media/Projects/cryptographic-module-validation-program/documents/fips%20140-3/FIPS%20140-3%20IG.pdf)</sup>。

## References

1. [Agrawal, Kayal, Saxena, PRIMES is in P (preprint copy), trial-division passage](https://paperzz.com/doc/7400128/primes.pdf)
2. [Agrawal, Kayal, Saxena, PRIMES is in P (August 6, 2002), IIT Kanpur](https://cse.iitk.ac.in/users/manindra/algebra/primality_original.pdf)
3. [Probability and Algorithms, National Academies Press, chapter 5](https://www.nationalacademies.org/read/2026/chapter/5)
4. [Song Y. Yan, Primality Testing, in Computational Number Theory and Modern Cryptography, Wiley (2012)](https://onlinelibrary.wiley.com/doi/10.1002/9781118188606.ch3)
5. [GNU MP 6.3.0 manual, Number Theoretic Functions](https://gmplib.org/gmp-man-6.3.0.pdf)
6. [Martin Dietzfelbinger, Primality Testing in Polynomial Time: From Randomized Algorithms to "PRIMES Is in P", LNCS 3000, Springer (2004)](https://link.springer.com/book/10.1007/b12334)
7. [PRIMES is in P, Annals of Mathematics 160 (2004), 781–793](https://annals.math.princeton.edu/2004/160-2/p12)
8. [François Morain, Primality Proving Using Elliptic Curves: An Update](https://www.researchgate.net/publication/221451620_Primality_Proving_Using_Elliptic_Curves_An_Update)
9. [François Morain, Implementing the asymptotically fast version of the elliptic curve primality proving algorithm, arXiv:math/0502097](https://arxiv.org/abs/math/0502097)
10. [Implementation Guidance for FIPS 140-3, NIST](https://csrc.nist.gov/csrc/media/Projects/cryptographic-module-validation-program/documents/fips%20140-3/FIPS%20140-3%20IG.pdf)
11. [Primality Tests, Stanford PBC notes](https://crypto.stanford.edu/pbc/notes/numbertheory/millerrabin.html)
12. [FIPS 186-5, Digital Signature Standard, NIST](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-5.pdf)
13. [FIPS 186-4 (withdrawn), NIST](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf)
14. [Daniel J. Bernstein, Proving primality after Agrawal-Kayal-Saxena](https://cr.yp.to/papers/aks.pdf)
15. [Sharpening "Primes is in P" for a large family of numbers, arXiv:math/0211334](https://arxiv.org/abs/math/0211334)
16. [User's Guide to PARI/GP 2.11.1](https://pari.math.u-bordeaux.fr/pub/pari/manuals/2.11.1/users.pdf)
17. [PARI library User's Guide 2.17.2](https://pari.math.u-bordeaux1.fr/pub/pari/manuals/2.17.2/libpari.pdf)
18. [Prime Testing Algorithm, GNU MP 6.3.0 manual](https://gmplib.org/manual/Prime-Testing-Algorithm)

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

Topic: 物质世界与数学 › 数学与统计学 › 数与代数 › Number theory › Computational and probabilistic number theory › Primality testing algorithms

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/primality-test

English original: https://www.edgepedia.org/primality-test
