{"id":267,"date":"2026-11-13T12:36:24","date_gmt":"2026-11-13T04:36:24","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-the-aks-test-proves-primality-in-polynomial-time\/"},"modified":"2026-11-13T21:54:03","modified_gmt":"2026-11-13T13:54:03","slug":"how-the-aks-test-proves-primality-in-polynomial-time","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-the-aks-test-proves-primality-in-polynomial-time\/","title":{"rendered":"How the AKS test proves primality in polynomial time"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> The <strong>AKS primality test<\/strong> decides whether an integer n is prime in time polynomial in the number of bits of n. It generalizes the observation that n is prime exactly when (x\u22121)^n \u2261 x^n \u2212 1 (mod n) as a polynomial, and it checks a related congruence modulo a carefully chosen polynomial x^r \u2212 1 and modulo n, for a polynomial number of shifts. The test is deterministic and unconditional. Its exponent is large enough that <strong>Miller-Rabin<\/strong> remains the test people run.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Trial division is exponential in the bit length. Miller-Rabin is fast and probabilistic, or deterministic only inside a verified range. AKS was the first published deterministic test whose worst-case time is polynomial in log n, with no unproved hypothesis. The congruence it checks is strong enough to force primality and weak enough that only polynomially many coefficients need to be examined.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The original exponent was high. Later improvements brought the complexity down to roughly (log n)^6 times polyloglog factors in one widely cited version. Even that is far above a few Miller-Rabin rounds for any n anyone tests.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The algorithm returns prime or composite correctly for every integer n &gt; 1. The running time is polynomial in log n. The degree r of the polynomial modulus is polylogarithmic in n, chosen so that r has a large prime factor and n has a suitable order modulo r. Extra memory holds a polynomial of that degree. The test does not produce a factor when it says composite.<\/p>\n\n\n<p class=\"wp-block-paragraph\">Perfect power detection is a required first step. A prime power p^k for k \u2265 2 must be rejected before the congruence phase, and it can be found by integer roots in logarithmic time. Skipping that step misclassifies powers.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>If n is a perfect power a^b with b \u2265 2, return composite.<\/li>\n<li>Find the smallest r such that the order of n modulo r is large enough for the theorem, greater than (log n)\u00b2 in the usual statement. This search is part of the polynomial bound.<\/li>\n<li>If n shares a proper factor with any integer up to r, return composite. If n is at most r, return prime.<\/li>\n<li>For a from 1 to the stated bound, which is on the order of sqrt(\u03c6(r)) \u00b7 log n, check that (x+a)^n \u2261 x^n + a modulo (x^r \u2212 1, n).<\/li>\n<li>If every congruence holds, return prime. A failed congruence returns composite.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">Polynomial time here is a theoretical classification. It is not a claim that AKS should replace Miller-Rabin in a library. The deterministic 64-bit Miller-Rabin base sets are proofs inside their range and are vastly faster. AKS does not factor n. Its role is the existence of a deterministic polynomial-time primality proof.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the perfect-power check is omitted<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Run only the polynomial congruences<\/td>\n<td>A prime power can satisfy the later setup poorly or be misread by a shortcut implementation. The perfect-power test is a required case, not an optimization.<\/td>\n<\/tr>\n<tr>\n<td>Replace AKS with a fixed list of Miller-Rabin bases for every size of n<\/td>\n<td>A fixed list is deterministic only up to the bound for which it was proved. AKS is the test whose proof has no size cap.<\/td>\n<\/tr>\n<tr>\n<td>Expect a factor as a byproduct<\/td>\n<td>The congruences fail or hold. A factorisation is a different algorithm.<\/td>\n<\/tr>\n<tr>\n<td>Cite AKS as the practical test inside a crypto library<\/td>\n<td>The degree and the number of coefficient checks are far beyond a handful of modular exponentiations. Libraries use Miller-Rabin or a deterministic variant.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Practical primality is Miller-Rabin. Enumeration of small primes is a sieve. AKS is the deterministic polynomial-time primality theorem turned into an algorithm.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment rejects perfect powers, finds a modulus whose multiplicative order is larger than (log n)\u00b2, and checks the polynomial congruence (x+a)^n \u2261 x^n + a. It prints <code>composite prime<\/code> for 9 and 31.<\/p>\n\n<pre><code class=\"language-python\">import math\n\ndef perfect_power(n):\n    for exp in range(2, int(math.log2(n)) + 1):\n        base = round(n ** (1 \/ exp))\n        if base &gt;= 2 and base ** exp == n:\n            return True\n    return False\n\ndef multiplicative_order(n, modulus):\n    if math.gcd(n, modulus) != 1:\n        return None\n    value = n % modulus\n    k = 1\n    while value != 1:\n        value = value * n % modulus\n        k += 1\n        if k &gt; modulus:\n            return None\n    return k\n\ndef poly_pow(base, exp, degree, mod):\n    result = [0] * degree\n    result[0] = 1\n    while exp:\n        if exp &amp; 1:\n            nxt = [0] * degree\n            for i, ai in enumerate(result):\n                if not ai:\n                    continue\n                for j, bj in enumerate(base):\n                    nxt[(i + j) % degree] = (nxt[(i + j) % degree] + ai * bj) % mod\n            result = nxt\n        nxt = [0] * degree\n        for i, ai in enumerate(base):\n            if not ai:\n                continue\n            for j, bj in enumerate(base):\n                nxt[(i + j) % degree] = (nxt[(i + j) % degree] + ai * bj) % mod\n        base = nxt\n        exp &gt;&gt;= 1\n    return result\n\ndef aks(n):\n    if n &lt; 2 or perfect_power(n):\n        return False\n    need = math.log2(n) ** 2\n    r = 2\n    while True:\n        order = multiplicative_order(n, r)\n        if order is not None and order &gt; need:\n            break\n        r += 1\n    for a in range(2, r + 1):\n        g = math.gcd(n, a)\n        if 1 &lt; g &lt; n:\n            return False\n    if n &lt;= r:\n        return True\n    phi = sum(1 for i in range(r) if math.gcd(i, r) == 1)\n    bound = int((phi ** 0.5) * math.log2(n)) + 1\n    for a in range(1, bound + 1):\n        base = [0] * r\n        base[0] = a % n\n        base[1 % r] = (base[1 % r] + 1) % n\n        got = poly_pow(base, n, r, n)\n        expect = [0] * r\n        expect[0] = a % n\n        expect[n % r] = (expect[n % r] + 1) % n\n        if got != expect:\n            return False\n    return True\n\ndef sieve(limit):\n    prime = [False, False] + [True] * (limit - 1)\n    for i in range(2, int(limit ** 0.5) + 1):\n        if prime[i]:\n            for j in range(i * i, limit + 1, i):\n                prime[j] = False\n    return prime\n\nprime = sieve(40)\nassert all(aks(n) == prime[n] for n in range(2, 41))\nprint(&quot;composite&quot; if not aks(9) else &quot;prime&quot;, &quot;prime&quot; if aks(31) else &quot;composite&quot;)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The AKS test is the proof that primality is in polynomial time. Key generation does not run it. <strong>Miller-Rabin<\/strong> is the probable-prime test inside OpenSSL and the other libraries that draw RSA primes.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop AKS for any integer a normal program needs to test. Use Miller-Rabin, with a deterministic base set when the integer fits a proved range. Keep AKS as the algorithm that shows deterministic primality is in polynomial time.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for the AKS test returns the polynomial congruence, the perfect-power check, and the reason it is not the practical primality test. <strong>The AKS test decides primality in time polynomial in the number of bits, by perfect-power rejection and a polynomial number of congruences modulo x^r \u2212 1.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: The AKS primality test decides whether an integer n is prime in time polynomial in the number of bits of n. It generalizes the observation that n is prime exactly when (x\u22121)^n \u2261 x^n \u2212 1 (mod n) as a polynomial, and it checks a related congruence&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,42],"tags":[],"class_list":["post-267","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-number-theory"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/267","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=267"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/267\/revisions"}],"predecessor-version":[{"id":666,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/267\/revisions\/666"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}