Native minimum-count prime selection controller #
The controller enumerates candidates starting at two while their square is less than D.
It tests the other endpoint by D ≤ 4p², invokes actual trial division, and calls a supplied
finite counting program only on primes in that window. It retains the first strict minimum.
No prime list, bitmap or primality oracle is supplied.
The counting code occupies registers below 128 and uses the concrete CountPrime ABI:
64 is n, 65 is p, 66/67/68 are the three original signed matrix pointers, 69 is the dirty
workspace pointer, and 70 is the returned unsigned count. Selector metadata is above that bank.
This module defines the component controller parameterized by finite code. The concrete
instantiation, initialized correctness and complete width/resource composition are exported by
CslibFrontier/Algorithms/WordRAM/ExactTriangle/PrimeSelection.lean.
The selection policy adapts chKeep/chRound (lines 95–107) in Anthropic's
ChoosePrime source.
The source first constructs a prime list; this
native controller instead enumerates by integer square tests and charged trial division.
Native instruction costs must be proved directly, independently of Light's cost model.
Low-bank candidate delivered to trial division.
Low-bank primality bit returned by trial division.
Side-length argument delivered to the modular counter.
Prime argument delivered to the modular counter.
AB argument delivered to the modular counter.
BC argument delivered to the modular counter.
AC argument delivered to the modular counter.
Workspace argument delivered to the modular counter.
Raw unsigned result returned by the modular counter.
Selected prime, with zero denoting that no eligible prime has yet been seen.
Raw modular count for the currently selected prime.
Whether a prime has already been counted.
Original matrix side length.
Original prime-window parameter D.
Original canonical-signed AB matrix pointer.
Original canonical-signed BC matrix pointer.
Original canonical-signed AC matrix pointer.
Caller-disjoint reusable dirty workspace.
Current candidate, beginning at two.
Square of the current candidate.
Four times the current candidate's square.
Preserved constant one.
Preserved constant zero.
Preserved constant four.
Embed the complete modular-counter bank, leaving selector metadata outside its image.
Reserve the low six registers for the existing trial-division kernel.
CountPrime's actual argument delivery, followed by the supplied finite counting code.
Retain the first strict minimum, preserving the earlier prime on ties.
Retain the selected result in one charged instruction on an ineligible branch.
Deliver the candidate to the actual low-bank trial-division program.
Enforce the lower endpoint, test primality, and count/update only eligible primes.
One actual candidate test/count and advance, including both square computations.
Compute the lower-endpoint test, process this candidate and advance to its successor.
Initialize constants, the first candidate and the explicit empty minimum.
Enumerate exactly the integer square window and retain the first minimum modular count. The finite counting program is a code argument, not a runtime result or counting oracle.
The selector adds fifty flat slots to the single embedded counter body.
A counter that does not inspect word width gives a selector that does not inspect it.