{"id":224,"date":"2026-11-13T12:35:06","date_gmt":"2026-11-13T04:35:06","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-miller-rabin-tests-a-strong-pseudoprime\/"},"modified":"2026-11-13T21:53:09","modified_gmt":"2026-11-13T13:53:09","slug":"how-miller-rabin-tests-a-strong-pseudoprime","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-miller-rabin-tests-a-strong-pseudoprime\/","title":{"rendered":"How Miller-Rabin tests a strong pseudoprime"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> The <strong>Miller-Rabin<\/strong> test writes an odd number n as d\u00b72^s + 1 and looks at a^d mod n. The test passes for a base a when that power is 1, or when some a^{d\u00b72^r} mod n equals n\u22121 for an r before s. A prime passes for every base. A composite that passes for a base is a strong pseudoprime to that base. k random bases miss all of them with probability at most <strong>4^{\u2212k}<\/strong> under the usual analysis. A fixed set of bases makes the test deterministic for every integer below a published bound, including all 64-bit integers.<\/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\">Fermat&#8217;s test checks a^{n\u22121} \u2261 1 (mod n). Carmichael numbers pass it for every base coprime to n and are composite. Miller-Rabin checks a stronger condition that comes from the fact that, modulo a prime, the equation x\u00b2 \u2261 1 has only the solutions 1 and n\u22121. A nontrivial square root of 1 is a proof of compositeness. The sequence of squarings from a^d up to a^{n\u22121} must hit n\u22121 before it hits 1, or start at 1.<\/p>\n\n\n<p class=\"wp-block-paragraph\">Random bases make the test Monte Carlo. Deterministic base sets are finite lists that have been checked for a range. They are not a new general proof that a handful of bases work for every integer.<\/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\">If the test fails for any base, n is composite, and the witness is unconditional. If n is prime, every base passes. If n is an odd composite, a random base in 2..n\u22122 passes with probability at most 1\/4, so k independent bases err with probability at most 4^{\u2212k}. Even n other than 2 is composite and does not need the test. Extra memory is a few integers the size of n. Modular exponentiation is the cost, O(k log\u00b3 n) with schoolbook multiplication.<\/p>\n\n\n<p class=\"wp-block-paragraph\">For integers below 2^64, a known finite list of bases makes the test deterministic. Above that range the same short list is only a probabilistic test. AKS is a deterministic polynomial-time test with a much larger constant. Miller-Rabin is the test that is actually run.<\/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>Handle n &lt; 3 and even n directly. Write n\u22121 = d\u00b72^s with d odd.<\/li>\n<li>Choose a base a. Compute x = a^d mod n by binary exponentiation.<\/li>\n<li>If x is 1 or n\u22121, this base passes. Otherwise square x repeatedly, s\u22121 times. If a square equals n\u22121, the base passes. If a square equals 1 earlier, or the loop ends without n\u22121, the base fails and n is composite.<\/li>\n<li>Repeat for the chosen bases. Any failure proves composite. All passes mean &#8220;probably prime&#8221; unless the base set is a deterministic set for the range that contains n.<\/li>\n<li>Do not conclude that a failing base yields a factor. The witness proves compositeness. Factoring is a different algorithm.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">A passing probabilistic test is not a proof of primality. A failing test is a proof of compositeness. Deterministic base lists are range-limited. The test does not factor n. Pollard&#8217;s rho factors. The 1\/4 bound is the standard Miller-Rabin analysis for the strong test, not the weaker Fermat test.<\/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 a Fermat test replaces the strong condition<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Check only a^{n\u22121} \u2261 1<\/td>\n<td>Carmichael numbers pass for every coprime base and are composite. The squaring sequence is what catches them.<\/td>\n<\/tr>\n<tr>\n<td>Treat k passing random bases as a theorem<\/td>\n<td>The error probability is at most 4^{\u2212k}, which is small and is not zero. Say probably prime, or use a deterministic base set inside its range.<\/td>\n<\/tr>\n<tr>\n<td>Use a 64-bit base list on an arbitrary-precision integer<\/td>\n<td>The list was verified up to a bound. Past that bound it is just another set of random-looking bases.<\/td>\n<\/tr>\n<tr>\n<td>Return a factor from a failing witness without an extra construction<\/td>\n<td>The witness shows compositeness. Extracting a factor needs a different step, such as a gcd involving a nontrivial square root of 1, and not every failure yields that root in the form the caller expects.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Enumerating small primes is a sieve. Factoring a composite the test has caught is Pollard&#8217;s rho or a heavier factorisation algorithm. A fully general deterministic polynomial test is AKS. Miller-Rabin is the practical primality test.<\/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 writes n\u22121 = d\u00b72^s and looks for a strong liar. A witness that fails the strong-pseudoprime conditions proves compositeness. 91 is composite and 97 is prime under the deterministic bases used for small n. It prints <code>composite prime<\/code>.<\/p>\n\n<pre><code class=\"language-python\">def miller_rabin(n, bases):\n    if n &lt; 2:\n        return False\n    d, s = n - 1, 0\n    while d % 2 == 0:\n        d \/\/= 2\n        s += 1\n    for a in bases:\n        if a % n == 0:\n            continue\n        x = pow(a, d, n)\n        if x == 1 or x == n - 1:\n            continue\n        for _ in range(s - 1):\n            x = pow(x, 2, n)\n            if x == n - 1:\n                break\n        else:\n            return False\n    return True\n\nsmall_bases = [2, 3, 5, 7]\nassert miller_rabin(91, small_bases) is False\nassert miller_rabin(97, small_bases) is True\nprint(&quot;composite&quot;, &quot;prime&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\"><strong>Miller-Rabin<\/strong> is the probable-prime test in RSA key generation: OpenSSL, Java <code>BigInteger.isProbablePrime<\/code>, and every library that draws primes. A composite that passes is a strong pseudoprime to the bases that were tried. AKS proves primality in polynomial time and is not the test those libraries run.<\/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 probabilistic language when the number lies under a bound covered by a published deterministic base set. Use that set and call the result a proof. Keep random bases, and state the error bound, when the integer is larger.<\/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 Miller-Rabin returns the strong pseudoprime condition and the difference between a witness of compositeness and a probabilistic pass. <strong>Miller-Rabin proves compositeness when a base fails the strong pseudoprime test, and k passing random bases are wrong with probability at most 4 to the minus k.<\/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 Miller-Rabin test writes an odd number n as d\u00b72^s + 1 and looks at a^d mod n. The test passes for a base a when that power is 1, or when some a^{d\u00b72^r} mod n equals n\u22121 for an r before s. A prime passes for&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-224","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\/224","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=224"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/224\/revisions"}],"predecessor-version":[{"id":623,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/224\/revisions\/623"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}