Lecture 4.3: Design Methods III

The witness method — randomly hunt an element proving a property: primality (Fermat → Miller–Rabin → Solovay–Strassen), plus witnesses for Boolean matrix products rebuilding all-pairs shortest paths.

Lecture 4 (part 3) — Witnesses (svedkovia)

Course 2-INF-135/15 Pravdepodobnostné algoritmy, LS 2025/26. Source slides: 04_metody456.pdf, slides 20–45 (the witness method). Part 1 was eliminating the adversary; part 2 was fingerprints. This is the fourth method — witnesses, and its two big customers: primality testing and all-pairs shortest paths via fast matrix multiplication.

Where we are: the fourth method

The catalogue from part 1 listed six design methods. We have done eliminating the adversary (online problems) and fingerprints (Freivalds, hashing, matching). Now:

Witnesses (svedkovia). To decide whether an object has a property, randomly hunt for a witness — a small certificate that, if found, proves the property, and whose validity is easy to check.

The whole method lives or dies on one number: how dense are the witnesses? If a constant fraction of candidates are witnesses, random sampling finds one fast. The mathematical work in this lecture is almost entirely proving that witnesses are dense — and the recurring tool for that is Lagrange’s theorem (a proper subgroup has at most half the elements).


1. The witness method, abstractly

We have a universe of candidates and want to test object OO for a property PP. A witness is a candidate aa such that the pair (O,a)(O, a) carries an efficiently checkable proof that OO has property PP.

Three requirements turn this into an algorithm (slides 21, 24):

  1. Easy to verify a candidate: given aa, deciding “is aa a witness for OO?” is efficient.
  2. Witnesses are dense: the ratio (candidates : witnesses) is a constant — so a random candidate is a witness with constant probability.
  3. Efficient access to candidates: we can sample one uniformly at random.

The algorithm is then trivial:

Pick a candidate aa at random. Check if it is a witness.

  • Witness found → report “OO has property PP” — this is certain.
  • No witness → report “OO probably does not have PP.”

Punchline — this is exactly the co-RP / one-sided-error shape from Lecture 3. A found witness is gospel (no false positives). The only error is failing to find a witness that exists — a false negative. Repeat with rr fresh candidates: if witnesses have density ≥12\ge \tfrac12, the chance of missing one rr times is ≤2−r\le 2^{-r}. Amplification is free.

For primality the property will be “nn is composite”, and a witness for nn being composite is what the next sections build, step by step, fixing each failed attempt.


2. The number-theory toolbox (slide 22)

Everything below runs on four classical facts. Keep them on a card.

Little Fermat (malá Fermátová veta). If pp is prime and $a \in \mathbb{Z}_p^* = {d : \gcd(d,p)=1}$, then

ap−1≡1(modp).a^{p-1} \equiv 1 \pmod p.

Chinese Remainder Theorem, version I. If m=m1⋯mkm = m_1 \cdots m_k with the mim_i pairwise coprime, then any tuple of residues (r1,…,rk)(r_1,\dots,r_k) corresponds to a unique r∈Zmr \in \mathbb{Z}_m with r≡ri(modmi)r \equiv r_i \pmod{m_i}.

Chinese Remainder Theorem, version II (the one we actually use). If n=p⋅qn = p\cdot q with gcd⁡(p,q)=1\gcd(p,q)=1, then the ring Zn\mathbb{Z}_n is isomorphic to Zp×Zq\mathbb{Z}_p \times \mathbb{Z}_q:

a  ⟷  (a mod p, a mod q),a \;\longleftrightarrow\; (a \bmod p,\ a \bmod q),
and addition/multiplication act coordinatewise. Reading a number by its two “coordinates” mod pp and mod qq is how we will construct witnesses by hand.

Lagrange’s theorem. For a subgroup HH of a finite group AA,

∣A∣=Index⁡H(A)⋅∣H∣,|A| = \operatorname{Index}_H(A)\cdot |H|,
so ∣H∣|H| divides ∣A∣|A|. In particular a proper subgroup has index ≥2\ge 2, hence ∣H∣≤∣A∣/2|H| \le |A|/2.

Why Lagrange is the hero of this lecture. Almost every “witnesses are dense” theorem has the same skeleton: the non-witnesses form a proper subgroup of Zn∗\mathbb{Z}_n^*. By Lagrange that subgroup has ≤∣Zn∗∣/2\le |\mathbb{Z}_n^*|/2 elements, so at least half the group are witnesses. Density ≥12\ge \tfrac12 falls out of group theory, not of any delicate counting.


3. Attempt 0 — the divisor witness (and why it fails)

The naive definition: nn is prime iff its only divisors are 11 and nn. So a divisor a∣na \mid n with 1<a<n1 < a < n is a perfectly valid witness that nn is composite — and trivially checkable.

The problem is requirement 2 (density). Take the hardest composites, n=p⋅qn = p\cdot q with p,q≈np,q \approx \sqrt n (RSA-shaped numbers). Among the n−1n-1 candidates there are only a handful of divisors. The witness density is ≈1/n\approx 1/\sqrt n, astronomically far from constant. Random sampling would never hit one.

Lesson. Divisors prove compositeness but are far too rare. We need a different, denser notion of witness for the same property. The rest of §4–§9 is the search for one.


4. Attempt 1 — the Fermat witness (and the Carmichael catastrophe)

Little Fermat says: if nn is prime, then an−1≡1(modn)a^{n-1} \equiv 1 \pmod n for every a∈Zn∗a \in \mathbb{Z}_n^*. Contrapositive:

aa is a Fermat witness for “nn composite” if   an−1 mod n≠1\;a^{n-1} \bmod n \ne 1.

If we ever see an−1≢1a^{n-1} \not\equiv 1, then nn cannot be prime — certain proof.

It is efficient to test (iterated squaring / opakované umocňovanie). We never form an−1a^{n-1} as an integer; we reduce mod nn after every squaring:

a2 mod n,a2k mod n=[(a2k−1 mod n)2] mod n,a^{2} \bmod n,\quad a^{2^k} \bmod n = \big[(a^{2^{k-1}}\bmod n)^2\big]\bmod n,
and for n−1=∑ibi2i−1n-1 = \sum_i b_i 2^{i-1} we multiply together the a2i−1a^{2^{i-1}} with bi=1b_i=1. That is O(log⁡n)O(\log n) multiplications in Zn\mathbb{Z}_n, i.e. O((log⁡n)3)O((\log n)^3) bit operations. Cheap.

The catastrophe: Carmichael numbers (Carmichaelove čísla). There exist composite nn for which

an−1≡1(modn)for all a coprime to n.a^{n-1} \equiv 1 \pmod n \quad \text{for } \textbf{all } a \text{ coprime to } n.
The smallest is 561=3⋅11⋅17561 = 3\cdot 11\cdot 17. For such nn the Fermat test has no witnesses at all among coprime aa — density 00, requirement 2 dead. (There are infinitely many Carmichael numbers, so this is not a finite annoyance we can table.)

Punchline. Fermat’s test is fast and one-sided but not robust: Carmichael numbers are composites that the test cannot distinguish from primes. We need a witness that survives even them. Two routes do it: looking at square roots of 11 (Miller–Rabin, §6) and the Jacobi symbol (Solovay–Strassen, §7).


5. The fix’s foundation — an algebraic characterization of primes (slides 25–26)

The escape is to test not an−1a^{n-1} but its square root a(n−1)/2a^{(n-1)/2}.

Theorem (alternative definition of a prime). For odd p>2p > 2,

p is prime  ⟺  a(p−1)/2 mod p∈{1, p−1}  ∀a∈Zp∖{0}.p \text{ is prime} \iff a^{(p-1)/2} \bmod p \in \{1,\, p-1\}\ \ \forall a \in \mathbb{Z}_p \setminus\{0\}.

Proof sketch.

The number a(n−1)/2a^{(n-1)/2} is a square root of an−1a^{n-1}. For a prime it must be one of the trivial roots of unity ±1\pm 1. Composites can have other roots — and those other roots are the new, denser witnesses.


6. The clean case n≡3(mod4)n\equiv 3 \pmod 4 — and the density engine (slides 25, 27, 28)

For the special shape n≡3(mod4)n\equiv 3 \pmod 4 the witness is simply: a(n−1)/2a^{(n-1)/2} landing outside {1,n−1}\{1, n-1\}.

Theorem. For n≡3(mod4)n \equiv 3 \pmod 4:

So define the witness set and its complement (the “liars”):

Wit={a:a(n−1)/2 mod n∉{1,n−1}},Euler={a:a(n−1)/2 mod n∈{1,n−1}}.\mathrm{Wit} = \{a : a^{(n-1)/2}\bmod n \notin\{1,n-1\}\},\qquad \mathrm{Euler} = \{a : a^{(n-1)/2}\bmod n \in\{1,n-1\}\}.

Why ∣Wit∣≥∣Euler∣|\mathrm{Wit}| \ge |\mathrm{Euler}| — the injection argument. This is the heart, and it is beautiful. Suppose we have one witness b∈Witb \in \mathrm{Wit} (we construct one below). Define

hb: Euler → Wit,hb(a)=a⋅b mod n.h_b:\ \mathrm{Euler}\ \to\ \mathrm{Wit},\qquad h_b(a) = a\cdot b \bmod n.

An injection Euler↪Wit\mathrm{Euler}\hookrightarrow\mathrm{Wit} gives $|\mathrm{Euler}|\le |\mathrm{Wit}|, so witnesses are **at least half** of $\mathrm{Euler}\cup\mathrm{Wit}.

This is the Lagrange idea in disguise. Multiplying by a fixed bb is a “coset shift”: it maps the liars injectively onto (part of) the witnesses, so the liars can be at most half. One real witness ⇒\Rightarrow a flood of them.

Constructing one witness bb (slide 28). Let n=p⋅qn = p\cdot q with gcd⁡(p,q)=1\gcd(p,q)=1. By CRT II read every aa as a pair (a mod p, a mod q)(a\bmod p,\ a\bmod q). Members of Euler\mathrm{Euler} map to (1,1)(1,1) or (p−1,q−1)=(−1,−1)(p-1,q-1)=(-1,-1). Now define bb by its coordinates

b  ⟷  (1, q−1)=(1,−1).b \;\longleftrightarrow\; (1,\ q-1) = (1,-1).
Then b(n−1)/2↔(1(n−1)/2,(−1)(n−1)/2)=(1,−1)b^{(n-1)/2}\leftrightarrow(1^{(n-1)/2},(-1)^{(n-1)/2})=(1,-1), which is neither (1,1)(1,1) nor (−1,−1)(-1,-1) — so b∉Eulerb\notin\mathrm{Euler}, i.e. b∈Witb\in\mathrm{Wit}. (Bonus: bb is its own inverse, since (1,−1)⊙(1,−1)=(1,1)(1,-1)\odot(1,-1)=(1,1).) The witness exists; the injection fires; density ≥12\ge \tfrac12. □\square

The catch: this only covers n≡3(mod4)n\equiv 3\pmod 4. The two real algorithms remove that restriction.


7. Miller–Rabin — square roots of unity for all nn (slide 23)

Miller–Rabin upgrades the Fermat test with one extra idea: a nontrivial square root of 11 betrays a composite. In a field, the only solutions of x2≡1x^2\equiv 1 are x≡±1x\equiv\pm1. So if we ever find xx with x2≡1(modb)x^2\equiv 1 \pmod b but x≢±1x\not\equiv\pm1, then bb is not a field, hence not prime.

Write b−1=2t⋅(odd)b-1 = 2^t\cdot(\text{odd}), i.e. 2t∣(b−1)2^t\mid(b-1) and 2t+1∤(b−1)2^{t+1}\nmid(b-1). The chain of repeated square roots is

a(b−1)/2, a(b−1)/4, …, a(b−1)/2t.a^{(b-1)/2},\ a^{(b-1)/4},\ \dots,\ a^{(b-1)/2^t}.

Miller–Rabin test. Input: odd b>2b>2, rounds rr.

  1. Find tt with 2t ∥ (b−1)2^t\,\|\,(b-1).
  2. Repeat rr times:
  3.    pick a∈{1,…,b−1}a\in\{1,\dots,b-1\} at random;
  4.    if some x∈{a(b−1)/2,…,a(b−1)/2t}x\in\{a^{(b-1)/2},\dots,a^{(b-1)/2^t}\} has x2≡1x^2\equiv1 and x≢±1(modb)x\not\equiv\pm1\pmod b → return “bb is composite” (nontrivial root of 1);
  5.    if ab−1≢1(modb)a^{b-1}\not\equiv1\pmod b → return “bb is composite” (Fermat witness);
  6. return “bb is probably prime.”

It catches both failure modes — Fermat witnesses and nontrivial square roots — and the second kind even nails Carmichael numbers, which the pure Fermat test missed.

Guarantee. If bb is composite, then

Pr⁡a[ a is a witness ] ≥ 12,\Pr_a[\,a \text{ is a witness}\,]\ \ge\ \tfrac12,
so rr independent rounds give error ≤(1/2)r\le (1/2)^r.

Cost. O(r m3)O(r\,m^3) for an mm-bit bb. For m=340m=340 (≈ 100 decimal digits) and r=50r=50: rm3≈2⋅109r m^3 \approx 2\cdot10^9 operations, error 2−50≈10−152^{-50}\approx 10^{-15}. This is the primality test used in practice.


8. Solovay–Strassen — the Jacobi symbol for all nn (slides 29–34)

A second route to a witness for all odd nn, historically first, built on quadratic residues.

Legendre symbol (for prime pp, gcd⁡(a,p)=1\gcd(a,p)=1):

(ap)={+1a is a quadratic residue mod p,−1a is a non-residue.Euler:(ap)=a(p−1)/2 mod p.\left(\tfrac{a}{p}\right) = \begin{cases} +1 & a \text{ is a quadratic residue mod }p,\\ -1 & a \text{ is a non-residue.}\end{cases} \qquad\text{Euler:}\quad \left(\tfrac{a}{p}\right)=a^{(p-1)/2}\bmod p.

Jacobi symbol (for odd n=p1k1⋯pℓkℓn=p_1^{k_1}\cdots p_\ell^{k_\ell}, gcd⁡(a,n)=1\gcd(a,n)=1):

(an)=∏i=1ℓ(api)ki=∏i=1ℓ(a(pi−1)/2 mod pi)ki.\left(\tfrac{a}{n}\right) = \prod_{i=1}^{\ell}\left(\tfrac{a}{p_i}\right)^{k_i} = \prod_{i=1}^{\ell}\big(a^{(p_i-1)/2}\bmod p_i\big)^{k_i}.

The crucial efficiency fact. The Jacobi symbol can be computed without factoring nn — by reciprocity rules (slide 30) that look exactly like a Euclidean GCD: each step at least halves a parameter, so depth O(log⁡n)O(\log n) and total O((log⁡n)3)O((\log n)^3) bit operations. The rules are

(abn)=(an) ⁣(bn),(an)=(bn) if a≡b,(2n)=(−1)(n2−1)/8,(na) ⁣(an)=(−1)a−12n−12.\left(\tfrac{ab}{n}\right)=\left(\tfrac{a}{n}\right)\!\left(\tfrac{b}{n}\right),\quad \left(\tfrac{a}{n}\right)=\left(\tfrac{b}{n}\right)\text{ if }a\equiv b,\quad \left(\tfrac{2}{n}\right)=(-1)^{(n^2-1)/8},\quad \left(\tfrac{n}{a}\right)\!\left(\tfrac{a}{n}\right)=(-1)^{\frac{a-1}{2}\frac{n-1}{2}}.

Solovay–Strassen witness. aa is a witness for “nn composite” if either

gcd⁡(a,n)≠1,orgcd⁡(a,n)=1 and (an)≠a(n−1)/2 mod n.\gcd(a,n)\ne 1,\qquad\text{or}\qquad \gcd(a,n)=1\ \text{and}\ \left(\tfrac{a}{n}\right)\ne a^{(n-1)/2}\bmod n.

Theorem. For odd n≥3n\ge 3:

The density proof is the Lagrange/subgroup argument again. Let the non-witnesses be

Wit‾n={a∈Zn∗:(an)=a(n−1)/2 mod n}.\overline{\mathrm{Wit}}_n = \Big\{a\in\mathbb{Z}_n^* : \big(\tfrac{a}{n}\big) = a^{(n-1)/2}\bmod n\Big\}.
This set is closed under multiplication (slide 32): if a,ba,b each satisfy it then
(abn)=(an)(bn)=a(n−1)/2b(n−1)/2=(ab)(n−1)/2(modn),\Big(\tfrac{ab}{n}\Big)=\Big(\tfrac{a}{n}\Big)\Big(\tfrac{b}{n}\Big) = a^{(n-1)/2}b^{(n-1)/2} = (ab)^{(n-1)/2}\pmod n,
so ab∈Wit‾nab\in\overline{\mathrm{Wit}}_n — it is a subgroup of Zn∗\mathbb{Z}_n^*. To finish we must show it is a proper subgroup, i.e. exhibit some a∈Zn∗a\in\mathbb{Z}_n^* outside it (slides 32–34). With n=p1i1mn=p_1^{i_1}m, two cases:

By Lagrange the proper subgroup has ≤∣Zn∗∣/2\le|\mathbb{Z}_n^*|/2 elements, so witnesses are ≥\ge half. □\square

Algorithm SSA (Solovay–Strassen), input odd n≥3n\ge3: pick random a∈{1,…,n−1}a\in\{1,\dots,n-1\}; if gcd⁡(a,n)≠1\gcd(a,n)\ne1 return composite; else compute J=(an)J=\left(\tfrac{a}{n}\right) and A=a(n−1)/2 mod nA=a^{(n-1)/2}\bmod n; return prime if J=AJ=A, else composite. Everything is O((log⁡n)3)O((\log n)^3).


9. From testing to generating primes (slides 35–37)

Cryptography needs random primes. PrimGen(ℓ, k): repeatedly draw a random odd ℓ\ell-bit number n=2ℓ−1+∑i=1ℓ−2ai2i+1n = 2^{\ell-1}+\sum_{i=1}^{\ell-2}a_i 2^i + 1 and run kk rounds of Solovay–Strassen; output the first nn that passes. There are two error sources, and both are crushed.

Error A — fails to output any prime. By the Prime Number Theorem a random ℓ\ell-bit number is prime with probability ≥1ln⁡n>12ℓ\ge \tfrac{1}{\ln n} > \tfrac{1}{2\ell}. Over 2ℓ22\ell^2 attempts,

Pr⁡[no prime found and certified]<(1−12ℓ)2ℓ2⋅(test-miss factor)<e−ℓ.\Pr[\text{no prime found and certified}] < \Big(1-\tfrac{1}{2\ell}\Big)^{2\ell^2}\cdot \big(\text{test-miss factor}\big) < e^{-\ell}.
For ℓ≥100\ell\ge100: e−100≪10−40e^{-100}\ll 10^{-40}.

Error B — outputs a composite as “prime”. Each run lets a composite slip with probability ≤2−ℓ\le 2^{-\ell} (with k=ℓk=\ell rounds). Summed over the ≤2ℓ2\le 2\ell^2 attempts,

Pr⁡[output composite]≤ℓ22ℓ−1.\Pr[\text{output composite}] \le \frac{\ell^2}{2^{\ell-1}}.
For ℓ≥100\ell\ge100: ≤1.58⋅10−26\le 1.58\cdot10^{-26}.

Punchline. A Monte-Carlo test (one-sided error on a single number) composes into a generator whose two failure modes are both exponentially unlikely. You get a prime with overwhelming confidence and no factorization in sight — the bedrock of RSA key generation.


10. The second arena — witnesses for a Boolean matrix product (slides 38–45)

The witness method is not only number theory. Its second appearance is a gem of algorithm design: all-pairs shortest paths (APSP) in an unweighted, undirected graph, via fast matrix multiplication. Let MM(n)=nω\mathrm{MM}(n)=n^\omega be the cost of multiplying two n×nn\times n matrices.

distances only distances and paths
deterministic BFS O(mn)O(mn), Floyd–Warshall O(n3)O(n^3) —
randomized APD O(MM(n)log⁡n)O(\mathrm{MM}(n)\log n) + BPWM →O(MM(n)log⁡2n)\to O(\mathrm{MM}(n)\log^2 n)

10a. APD — distances by halving (Seidel), slides 39–40

Build G′G' by adding an edge between any two vertices at distance ≤2\le 2 in GG (computed from AA and A2A^2: Z=A2Z=A^2, then B[i,j]=A[i,j]∨Z[i,j]B[i,j]=A[i,j]\vee Z[i,j]). Distances in G′G' are halved:

D[i,j] even⇒D[i,j]=2D′[i,j],D[i,j] odd⇒D[i,j]=2D′[i,j]−1.D[i,j]\text{ even}\Rightarrow D[i,j]=2D'[i,j],\qquad D[i,j]\text{ odd}\Rightarrow D[i,j]=2D'[i,j]-1.
Recurse on G′G' to get D′D', then recover the parity of D[i,j]D[i,j] from a neighbour sum (slide 39): D[i,j]D[i,j] is even   ⟺  ∑k∈Γ(i)D′[k,j] ≥ deg⁡(i) D′[i,j]\iff \sum_{k\in\Gamma(i)}D'[k,j]\ \ge\ \deg(i)\,D'[i,j]. The recursion T(n,δ)=2 MM(n)+T(n,⌈δ/2⌉)+O(n2)T(n,\delta)=2\,\mathrm{MM}(n)+T(n,\lceil\delta/2\rceil)+O(n^2) gives
  APD computes the full distance matrix in O(MM(n)log⁡n).  \boxed{\;\text{APD computes the full distance matrix in } O(\mathrm{MM}(n)\log n).\;}

But distances are not paths. To walk a shortest ii–jj path we need, at ii, some neighbour kk on a shortest path to jj. Finding that kk is a witness problem.

10b. APSP reduces to BPWM (slide 41–42)

Define the Boolean Product Witness Matrix problem:

BPWM. Given Boolean matrices A,BA,B, output WW where W[i,j]=kW[i,j]=k for some kk with A[i,k]=B[k,j]=1A[i,k]=B[k,j]=1 (a witness for the Boolean product entry), or 00 if none exists.

A successor on a shortest path is exactly such a witness: S[i,j]=kS[i,j]=k iff A[i,k]=1A[i,k]=1 (kk is a neighbour of ii) and D[k,j]=D[i,j]−1D[k,j]=D[i,j]-1. We don’t want to solve this for all n−1n-1 possible distance values — but neighbours satisfy D[i,j]−1≤D[k,j]≤D[i,j]+1D[i,j]-1\le D[k,j]\le D[i,j]+1, so it suffices to match D[k,j]≡D[i,j]−1(mod3)D[k,j]\equiv D[i,j]-1 \pmod 3. Run BPWM on AA against the three “distance-class” matrices D(r)D^{(r)} (r∈{0,1,2}r\in\{0,1,2\}). Three products replace n−1n-1.

10c. Finding the witness — isolate it by subsampling (slides 43–45)

How do we extract an actual witness kk from a Boolean product? Two ideas.

Idea 1: a unique witness reveals itself arithmetically. Compute the integer product W=A⋅BW = A\cdot B. Then W[i,j]W[i,j] counts the witnesses kk. Now weight column kk of AA by the number kk and multiply: the (i,j)(i,j) entry becomes ∑kk A[i,k]B[k,j]=\sum_k k\,A[i,k]B[k,j]= sum of all witness indices. If there is exactly one witness k∗k^*, this sum equals k∗k^* — the witness drops out for free.

Idea 2: make “exactly one” happen, by random subsampling. When (i,j)(i,j) has cc witnesses, randomly keep a subset of dd columns k1,…,kdk_1,\dots,k_d. If we pick the scale dd so that n/2≤c d≤nn/2\le c\,d\le n, exactly one witness survives with good probability:

Balls lemma. Among nn balls, ww are white. Draw dd independently with replacement, with n/2≤wd≤nn/2\le wd\le n. Then

Pr⁡[exactly one white]=d wn(1−wn)d−1≥12(1−1d)d−1>12e.\Pr[\text{exactly one white}]=d\,\tfrac{w}{n}\Big(1-\tfrac{w}{n}\Big)^{d-1} \ge \tfrac12\Big(1-\tfrac1d\Big)^{d-1} > \tfrac{1}{2e}.

Proof. d⋅wn≥12d\cdot\frac{w}{n}\ge\frac12 from wd≥n/2wd\ge n/2; and wn≤1d\frac{w}{n}\le\frac1d from wd≤nwd\le n, so (1−wn)d−1≥(1−1d)d−1>e−1(1-\frac{w}{n})^{d-1}\ge(1-\frac1d)^{d-1}>e^{-1}. □\square

Since cc is unknown, try every scale d=2ℓd=2^\ell for $\ell=0,\dots,\lceil\log n\rceil-1$, each repeated 3.42⌈log⁡n⌉3.42\lceil\log n\rceil times. For the right scale a single try isolates a witness with probability ≥12e\ge\frac1{2e}, so the chance of never isolating one is

(1−12e)3.42⌈log⁡n⌉≤1n.\Big(1-\tfrac{1}{2e}\Big)^{3.42\lceil\log n\rceil}\le \tfrac1n.
Hence only a ≤1/n\le 1/n fraction of entries fall through to a direct computation (step 3), and the whole witness matrix costs O(MM(n)log⁡2n)O(\mathrm{MM}(n)\log^2 n).

Punchline. This is the same deep idea as the Isolation Lemma from part 2: when many solutions confuse you, randomly thin them down until exactly one survives — a unique witness is one you can read off arithmetically. Randomness here is not for speed but for isolation.


Recurring themes from the witness method

Theme Where it appeared
One found witness = certain proof (co-RP / one-sided error) every primality test (§1)
Density of witnesses is everything divisor fails (§3), Fermat fails on Carmichael (§4)
Non-witnesses form a (proper) subgroup → Lagrange → ≤ half n≡3 mod 4n\equiv3\bmod4 (§6), Solovay–Strassen (§8)
One witness floods into many (multiply by bb: injective coset shift) injection Euler↪Wit\mathrm{Euler}\hookrightarrow\mathrm{Wit} (§6)
Read a number by CRT coordinates ( mod  p, mod  q)(\bmod\,p,\bmod\,q) building an explicit witness bb (§6, §8)
A nontrivial square root of 11 betrays a composite Miller–Rabin (§7)
Compute a hard invariant without the hard data Jacobi symbol without factoring (§8)
Monte-Carlo test → reliable generator random prime generation (§9)
Isolate a unique witness by random subsampling BPWM for shortest paths (§10)

The one sentence tying it together:

A witness is a needle whose mere existence is a proof — so the only question is how much hay surrounds it. Group theory (Lagrange) guarantees the needles are at least half the haystack for primality; random subsampling manufactures a lone needle for matrix products. Find one and you are certain; the randomness only governs how fast you find it.

witnessesprimalityMiller-RabinSolovay-Strassennumber-theoryJacobi-symbolmatrix-multiplicationshortest-pathsco-RP

Last updated 20 October 2026