\section{Formal verification}\label{sec:formal} All statements of Sections~\ref{sec:engine}--\ref{sec:machines} that carry a Lean name are proved in Lean~4, and the three main theorems are checked against a specification that a reader can verify independently of the proofs. This section describes the specification, where each part of the development lives, what has to be trusted, what was checked, and how to reproduce the checks. The source is public at \url{https://github.com/6teamFurina/prime-algorithm}; the verified snapshot is the release \texttt{v0.1-verified} (commit \lean{febd5a5}). Later commits on the default branch change only the announcement and the README; the Lean sources are identical. \subsection{The specification}\label{sec:spec} The specification is a short file that imports only six modules of Mathlib (real powers, logarithms and square roots, Turing machines, \lean{Nat.bits} and the prime-counting function) and none of the definitions of the proof. It is \lean{MainResults.lean} in the directory \lean{formal/independent/PrimeIndependent/}. Without its documentation comments it reads: \begin{footnotesize} \begin{verbatim} namespace PrimeIndependent def sumPrimes (N : ℕ) : ℕ := ∑ p ∈ Finset.range (N + 1), if Nat.Prime p then p else 0 noncomputable def timeBound (C N : ℕ) : ℕ := ⌊(C : ℝ) * (2 : ℝ) ^ ((C : ℝ) * (Nat.bits N).length / Real.sqrt (Real.logb 2 ((Nat.bits N).length + 2)))⌋₊ def Outputs (M : Turing.TM2ComputableAux Bool Bool) (x y : List Bool) (t : ℕ) : Prop := Nonempty (Turing.TM2OutputsInTime M.tm (x.map M.inputAlphabet.invFun) (some (y.map M.outputAlphabet.invFun)) t) def ExactPrimeSum : Prop := ∃ (M : Turing.TM2ComputableAux Bool Bool) (C : ℕ), (∀ i, Finite (M.tm.Γ i)) ∧ ∀ N : ℕ, Outputs M (Nat.bits N) (Nat.bits (sumPrimes N)) (timeBound C N) def ExactPrimeCounting : Prop := ∃ (M : Turing.TM2ComputableAux Bool Bool) (C : ℕ), (∀ i, Finite (M.tm.Γ i)) ∧ ∀ N : ℕ, Outputs M (Nat.bits N) (Nat.bits (Nat.primeCounting N)) (timeBound C N) def ExactPrimeFinding : Prop := ∃ (M : Turing.TM2ComputableAux Bool Bool) (C : ℕ), (∀ i, Finite (M.tm.Γ i)) ∧ ∀ N : ℕ, ∃ p : ℕ, p.Prime ∧ N < p ∧ p ≤ 2 * max N 1 ∧ Outputs M (Nat.bits N) (Nat.bits p) (timeBound C N) end PrimeIndependent \end{verbatim} \end{footnotesize} Its definitions correspond to the statements of Section~\ref{sec:intro} as follows. \begin{itemize} \item \lean{sumPrimes N} is $S(N)$, defined directly as a finite sum, and \lean{Nat.primeCounting N} is Mathlib's $\pi(N)$. \item Numbers are encoded by \lean{Nat.bits}: binary, least significant bit first, with $0$ the empty list; so $(\lean{Nat.bits N}).\lean{length}=b(N)$. \item \lean{timeBound C N} is $T_C(N)$ of \eqref{eq:TC}. \item \lean{Outputs M x y t} is Mathlib's timed halting relation \lean{TM2OutputsInTime} for the multi-stack machine \lean{M.tm}, started on the input $x$ and halting within $t$ steps with exactly $y$ on the output stack, all other stacks empty; the input and output alphabets are translated by the bijections that are part of \lean{M}. \item Each of \lean{ExactPrimeSum}, \lean{ExactPrimeCounting} and \lean{ExactPrimeFinding} chooses one machine \lean{M} with finite stack alphabets and one constant \lean{C} before quantifying over all $N\in\N$, including $0$ and $1$. In \lean{ExactPrimeFinding} the output is constrained only by its properties: a prime $p$ with $N{\raggedright\arraybackslash}p{0.55\linewidth}>{\raggedright\arraybackslash}p{0.38\linewidth}} \toprule Location & Content \\ \midrule \lean{formal/independent/PrimeIndependent/} & \lean{MainResults.lean} (specification), \lean{Solution.lean} (the three endpoints), \lean{Challenge.lean} (statements for Comparator); \lean{PrintAxioms.lean} one level up \\ \lean{formal/unified/PrimeAlgorithm/} & \\ \quad \lean{RamanujanSum}, \lean{MoebiusPrimeIndicator}, \lean{DoubleRamanujan} & Section~\ref{sec:formula} \\ \quad \lean{Cauchy*} & Section~\ref{sec:quadrature} \\ \quad \lean{SignedQuantization}, \lean{PhaseApproximation}, \lean{DyadicCauchy}, \lean{FixedCauchyPrimeSum}, \lean{CertifiedCauchy}, \lean{CertifiedPrimeSum}, \lean{CertifiedNumerator}, \lean{IntegerPrimeSum} & Section~\ref{sec:rounding} \\ \quad \lean{CertifiedGenerator*}, \lean{CertifiedRaw*}, \lean{CertifiedSeries}, \lean{CertifiedNearest}, \lean{CertifiedGaussianRat}, \lean{CertifiedAnalytic*}, \lean{CertifiedIntegerPower} & Section~\ref{sec:coefficients} \\ \quad \lean{IntegerKernelExpansion}, \lean{KernelSumExpansion}, \lean{PrimeWitnessReindex}, \lean{PrimeQueryExpansion}, \lean{IntegerPrimeQueryFinal}, \lean{CertifiedCoordinateBounds}, \lean{PrimeQueryBounds}, \lean{PrimeQueryEnumeration}, \lean{PrimeQueryPoly} & Section~\ref{sec:network} \\ \quad \lean{PrimeQueryFinish}, \lean{PrimeMachineInput}, \lean{PrimeSumOracle}, \lean{PrimeSumMachine}; \lean{PrimeCount*}, \lean{CountQuery*}, \lean{CountWitnessReindex}, \lean{IntegerCountQueryFinal}; \lean{PrimeFinding*}, \lean{AdaptiveWordQueries} & Section~\ref{sec:machines} \\ \lean{formal/bridge/EthFollowup/ModP/} & \lean{BatchNatQueries}, \lean{BatchCollect}: Section~\ref{sec:batches} \\ \lean{formal/bridge/EthFollowup/Primes/} & \lean{FourKernel*}, \lean{CountKernel*}, \lean{PhaseKernel*}, \lean{Coprime*}, \lean{LinearPadding}, \lean{WeightedNetworkBounds}: Section~\ref{sec:network} and the padding of Section~\ref{sec:batches} \\ \lean{anthropics/formal-math} at \lean{16a5ce4} & the formalization of \cite{A} (folder \lean{eth}): Theorem~\ref{thm:A} \\ \lean{xennygrimmato/eth-hamiltonicity} at \lean{4193596} & the library of \cite{T1}: Lemma~\ref{lem:compile}, Definition~\ref{def:package}, Proposition~\ref{prop:onequery}, Theorem~\ref{thm:desc} \\ \lean{dependencies.lock.json} & all pinned revisions, including Mathlib at \lean{0df444a}; toolchain Lean~4.33.1 \\ \lean{verification/} & the records of the checks described below \\ \lean{scripts/} & build, verification and reproduction scripts \\ \bottomrule \end{tabular} \caption{Layout of the release \texttt{v0.1-verified}.} \label{tab:layout} \end{table} \subsection{Correspondence} Every result of Sections~\ref{sec:engine}--\ref{sec:machines} is followed in the text by the names of the Lean declarations that prove it; all of them were checked against the release. Table~\ref{tab:corr} lists the principal ones. \begin{table}[htbp] \centering\footnotesize \begin{tabular}{ll} \toprule Statement & Lean declaration \\ \midrule Theorem A & \lean{PrimeIndependent.primeSum\_time} \\ Theorem B & \lean{PrimeIndependent.primeCounting\_time} \\ Theorem C & \lean{PrimeIndependent.primeFinding\_time} \\ Proposition~\ref{prop:four} & \lean{DoubleRamanujan.primeSum\_phase} \\ Proposition~\ref{prop:stability} & \lean{DoubleRamanujan.phaseSum\_round} \\ Lemma~\ref{lem:bezout} & \lean{CoprimeNode.residue\_coprime\_iff} \\ Theorem~\ref{thm:quad} & \lean{Cauchy.cauchyApprox\_error} \\ Theorem~\ref{thm:factor} & \lean{CertifiedCauchy.primeEvaluate\_accuracy} \\ Corollary~\ref{cor:rounding} & \lean{CertifiedPrimeSum.primeSum\_all\_inputs} \\ Equation~\eqref{eq:integerround} & \lean{IntegerPrimeSum.integerResult\_eq\_primeSum} \\ Theorem~\ref{thm:generator} & \lean{CertifiedGenerator.generate\_certified} \\ Theorem~\ref{thm:batch} & \lean{NatBatchQuery.machine\_TM} \\ Corollary~\ref{cor:padding} & \lean{NatBatchQuery.machine\_TM\_linear\_input} \\ Theorem~\ref{thm:adaptivecalls} & \lean{AdaptiveWordQuery.machine\_TM} \\ Proposition~\ref{prop:queries} & \lean{IntegerPrimeQueryFinal.integerPrimeSum\_eq\_queries} \\ Proposition~\ref{prop:bijection} & \lean{FourKernel.descriptor\_count} \\ Lemma~\ref{lem:intervalsum} & \lean{PrimeFinding.primeSum\_lt\_iff} \\ Machine for Theorem A & \lean{PrimeSumMachine.primeSum\_TM\_all\_inputs} \\ Machine for Theorem B & \lean{PrimeCountMachine.primeCounting\_TM\_all\_inputs} \\ Machine for Theorem C & \lean{PrimeFindingMachine.primeFinding\_TM\_all\_inputs} \\ \bottomrule \end{tabular} \caption{Principal statements and their Lean declarations.} \label{tab:corr} \end{table} \subsection{What has to be trusted} A reader who accepts the checks below needs to trust only: the Lean kernel (the proofs were checked by two independent kernels); the three axioms \lean{propext}, \lean{Classical.choice} and \lean{Quot.sound}, on which the three endpoints depend and no others; and the specification of Section~\ref{sec:spec}, together with the Mathlib definitions it uses (\lean{Nat.Prime}, \lean{Nat.primeCounting}, \lean{Nat.bits}, \lean{Turing.TM2ComputableAux}, \lean{Turing.TM2OutputsInTime} and the real functions in \lean{timeBound}). Everything else, including the formalization of \cite{A}, the library of \cite{T1} and all the definitions used in the proofs, is checked and need not be trusted. In particular the descriptions of these intermediate objects in this paper are expository: an inaccuracy in them could not invalidate Theorems~A--C. \subsection{The checks that were run} The three endpoints were checked in a fresh, isolated directory as follows (record \lean{verification/original/}, run \lean{72ea63e4-8e5d-42d1-8468-2df02d7b3647}). \begin{enumerate} \item \emph{Source build.} All dependencies were exported at the pinned revisions with no compiled proof artifacts (\lean{.olean}, \lean{.ilean}), and the whole closure was built from source with Lean~4.33.1 and all proof caches disabled: the $8{,}312$ modules of Mathlib, the default targets of \cite{A} and the project's endpoints. The pinned Lean binary release with its bundled \lean{Init}, \lean{Lean}, \lean{Std} and \lean{Lake} served as the bootstrap toolchain; the compiler itself was not rebuilt. \item \emph{Fresh replay.} \lean{leanchecker --fresh} replayed the complete import closure of \lean{PrimeIndependent.Solution} into a new environment. \item \emph{Comparator.} Comparator compared the statements of \lean{Challenge.lean} with the proved declarations, with no definitions open to replacement, allowed only the three axioms above, and had the exported proofs accepted both by the Lean kernel and by the independent kernel nanoda. \end{enumerate} The first source build was made when only Theorems~A and~B had independent statements; the final run reused its source-built dependencies, incrementally built the third statement and its proof, and repeated steps 2 and 3 for all three endpoints. For the release, the $96$ Lean files were compared with the verified sources and found unchanged, and an ordinary build with a recursive axiom audit was repeated (\lean{verification/release/}). All of these checks ran on one machine (macOS). Independent reproduction on another machine is welcome. \subsection{Reproducing the checks} The release contains scripts for both levels of checking. An ordinary build and axiom audit, using the official pinned Mathlib cache, needs Git, Python~3.9 or newer, a C compiler and \lean{elan}: \begin{footnotesize} \begin{verbatim} git clone https://github.com/6teamFurina/prime-algorithm.git cd prime-algorithm git checkout v0.1-verified python3 scripts/bootstrap.py --cache python3 scripts/verify.py \end{verbatim} \end{footnotesize} Omitting \lean{--cache} builds all dependencies from source. The full independent reproduction (source-only export, build from source without caches, fresh replay, and Comparator with both kernels) is \begin{footnotesize} \begin{verbatim} python3 scripts/reproduce.py \end{verbatim} \end{footnotesize} which can take an hour or more and writes its records under \lean{verification/local/}. Its stages can also be run one by one (\lean{prepare\_verification\_tools.py}, \lean{prepare\_independent.py}, \lean{build\_independent.py}, \lean{replay\_independent.py}, \lean{check\_independent.py}, \lean{report\_independent.py}). The dual-kernel stage runs the checkers in a macOS Seatbelt sandbox and is tested on macOS only; the ordinary build also runs on Linux. \subsection{Verification of the general theorem} The development checkout extends the tagged release with a proof of Theorem~\ref{thm:general} in \lean{AdmissibleTheorem.lean}. The hypotheses give polynomial-time generators for the analytic tables and for the arithmetic description of the summation domain. The proof supplies the query generator, proves its exact counting identity, and obtains the time bound on the original binary input. The four machine statements in \lean{AdmissibleCorollary.lean} cover the three parts of Corollary~\ref{cor:general}, including both outputs in part~(2). The development checkout provides the following commands for the Lean build and its recursive axiom report. \begin{footnotesize} \begin{verbatim} ./scripts/unified_lake build PrimeAlgorithm.AdmissibleCorollary ./scripts/unified_lake env lean ../../formal/Section10Audit.lean \end{verbatim} \end{footnotesize} The independent specification and dual-kernel checks described above cover the three main theorems of the tagged release. \subsection{Other statements} Corollary~\ref{cor:interval} follows from Theorem~A by two runs of its machine but is not a separate endpoint. The remark on faster engines in Section~\ref{sec:remarks} remains a paper argument. The mathematical arguments of Sections~\ref{sec:engine}--\ref{sec:machines} are written to follow the formal proofs; where a step is only sketched, the cited Lean declarations contain the full argument.