The contract of the chunked first stage #
The contracts of the arm of the first stage for a chunk from 2 to 16
(Ratpoints.Sieve.andRowsChunked): the starts of the rows at the first word number of a piece
(Ratpoints.Sieve.chunkStarts), a start stepped back by the modulus (Ratpoints.Sieve.wrapStart),
one chunk in registers (Ratpoints.Sieve.chunkSieve), the whole chunks of a piece
(Ratpoints.Sieve.andChunks), one leg over the tail (Ratpoints.Sieve.tailLeg) and the tail in
legs of 8, 4, 2 and 1 bit arrays (Ratpoints.Sieve.tailLegs), as @[spec] theorems proved with
vcgen, the program logic of Lean core, with loop invariants that are named definitions. The
chunked arm leaves the words of the piece that the arm one row at a time leaves
(Ratpoints.Sieve.andRowsChunked_spec, with the postcondition of Ratpoints.Sieve.andRows_spec).
The chunked arm reads each row from its start, which it moves on by the chunk after each chunk and
back into the first p bit arrays of the row: from the starts at the first word number of the piece
to those at the word number of the next chunk (Ratpoints.Sieve.SieveSpec.startAt_advance). The
registers read up to chunk - 1 bit arrays past the first p of a row, which repeat its first
ones, and so hold the words of the first stage at the word numbers of the chunk
(Ratpoints.Sieve.partialReg_map_startAt).
Main definitions #
Ratpoints.Sieve.SieveSpec.startAt,Ratpoints.Sieve.SieveSpec.advance: a sieve spec with its start at a word number, and with its start moved on.Ratpoints.Sieve.partialReg,Ratpoints.Sieve.RegsInvariant,Ratpoints.Sieve.RegsStored: the registers of a chunk or a leg after some of the moduli, the invariant of the loops that and the rows into them, and the bit arrays that the legs store.
Main statements #
Ratpoints.Sieve.wrapStart_spec: the start stepped back is the start modulop.Ratpoints.Sieve.chunkStarts_spec: the starts of the rows at the first word number.Ratpoints.Sieve.chunkSieve_spec,Ratpoints.Sieve.andChunks_spec: one chunk, and the whole chunks of a piece.Ratpoints.Sieve.tailLeg_spec,Ratpoints.Sieve.tailLegs_spec: one leg, and the tail.Ratpoints.Sieve.andRowsChunked_spec: the chunked arm leaves each word of the piece the class word and-ed with its row words (Ratpoints.Sieve.andWords).
Implementation notes #
The file uses Lean core and Std only, as the contracts of the sieve do. The proofs use only that
Ratpoints.rbaPack and Ratpoints.chunk are positive, never their values; the cover of the tail by
the legs is a fact decided over the tails below 16, which the arm's chunk of at most 16 bounds.
The starts of the rows #
The sieve spec s with its start at the word number w0: the bit array (w0 + offset) mod p
of the row, which the bit array with the word number w0 reads.
Equations
Instances For
The sieve spec s with its start moved on by c bit arrays and back into the first p bit
arrays of the row.
Equations
Instances For
The registers of a chunk #
The registers of the chunked first stage at word κ of bit array x after the moduli l: the
class word cw and-ed in turn with word κ of bit array start + x of each row, read past the
first p bit arrays into the copies of the first ones.
Equations
- Ratpoints.Sieve.partialReg l cw x κ = List.foldl (fun (acc : UInt64) (s : Ratpoints.Sieve.SieveSpec) => acc &&& s.row[Ratpoints.rbaPack * (s.start + x) + κ]!) cw l
Instances For
The registers of a chunk, read from the starts of the rows at the word number w up to
chunk - 1 bit arrays past them, hold the words of the first stage at the word numbers from w on
(Ratpoints.Sieve.andWords), since the rows repeat their first bit arrays after the first p
(Ratpoints.Sieve.SieveSpec.Wraps).
The w registers regs of the chunked first stage, register j reading bit array
start + f j of the rows, after the row of the modulus K of the local array sv is and-ed into
the registers below c: register j holds the class word cw and-ed with the rows of the first
K + 1 moduli for j < c and of the first K otherwise (Ratpoints.Sieve.partialReg). The
invariant of the loops of Ratpoints.Sieve.chunkSieve (f j = j) and of
Ratpoints.Sieve.tailLeg (f j = off + j) that and the row of a modulus into the registers.
Equations
- One or more equations did not get rendered due to their size.
Instances For
A step of the loops of Ratpoints.Sieve.chunkSieve and Ratpoints.Sieve.tailLeg that load the
row of the first modulus into w ≤ M registers, register j reading bit array start + f j of the
row: register cur holds the class word cw and-ed with its bit array, the registers before it
as they were.
The start of the loop of Ratpoints.Sieve.chunkSieve or Ratpoints.Sieve.tailLeg that ands
the row of the modulus cur into the registers, which hold the moduli before it.
A step of the loops of Ratpoints.Sieve.chunkSieve and Ratpoints.Sieve.tailLeg that and the
row of the modulus K into w ≤ M registers, register j reading bit array start + f j of the
row: register cur and-ed with its bit array of the row of s, the sieve spec of K with the
start that the loop moves on only after the registers, holds the moduli up to K.
At the end of the loop that ands the row of the modulus cur into the registers, they hold the
moduli up to cur.
The words of the bit arrays from lo to below hi of surv' are the class word cw and-ed
with the rows of all the moduli of the local array sv, read at their starts plus the bit array
minus base (Ratpoints.Sieve.partialReg), and the other words those of surv: what the legs of
the tail from the bit array base on store (Ratpoints.Sieve.tailLeg,
Ratpoints.Sieve.tailLegs).
Equations
- One or more equations did not get rendered due to their size.
Instances For
The starts at the first word number #
The invariant of the loop of Ratpoints.Sieve.chunkStarts over the moduli specs and, in step
with them, their reciprocals mgs, after the moduli pref, whose state is the local array of the
sieve specs and the reciprocals still to come: before the loop has gone through all the moduli,
these are the reciprocals after the first pref.length; and the array holds the sieve specs of
pref with their starts at the word number w0.
Equations
- One or more equations did not get rendered due to their size.
Instances For
A step of the loop of Ratpoints.Sieve.chunkStarts: the start of the modulus s at w0 by the
reduction with the reciprocal of the cursor, which is that of s
(Ratpoints.Sieve.SieveSpec.Fits.ite_modMul_modLong_add_eq_emod).
The local array of the sieve specs holds them with their starts at the word number w0, the
reduction by the reciprocal of the cursor under the test small giving the Euclidean remainder of
w0 + offset; the reciprocals run in step with the moduli and do not run out before them.
The step back of a start #
The invariant of the loop of Ratpoints.Sieve.wrapStart: the start keeps its residue modulo
p, and is below p at the exit.
Equations
Instances For
One chunk #
The invariant of the loop of Ratpoints.Sieve.chunkSieve over the moduli after the first K
of the local array sv: the registers hold the first K moduli, and the starts of those moduli
are moved on by the chunk.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The start of the loop over the further moduli: the registers hold the first modulus, its start moved on.
In the loop of Ratpoints.Sieve.chunkSieve over the further moduli, the sieve spec of the
modulus cur is still that of the local array: its start moves on only after the registers.
The ands of the modulus cur done: its start moves on.
The registers of a chunk hold the class word and-ed with the rows of all the moduli of the
local array from their starts on (Ratpoints.Sieve.partialReg), and the starts move on by the
chunk and back into the first p bit arrays of the rows (Ratpoints.Sieve.wrapStart_spec).
The whole chunks of a piece #
The invariant of the loop of Ratpoints.Sieve.andChunks over the chunks, on the local array of
the sieve specs, the words of the piece and the first bit array c of the next chunk, for the
moduli specs of the first stage over a piece from the word number w0 on: c a multiple of the
chunk up to full, the starts at the word number w0 + c, and the words of the bit arrays below
c those of the first stage (Ratpoints.Sieve.andWords), the others as in surv.
Equations
- One or more equations did not get rendered due to their size.
Instances For
A store of a register of the chunk from the bit array c on into the words of the piece.
A chunk done: the starts move on by the chunk, and the words of its bit arrays are stored.
The whole chunks below full, a multiple of the chunk, of a piece from the word number w0 on
store into the words of their bit arrays those of the first stage with the moduli specs
(Ratpoints.Sieve.andWords), read from the local array of their sieve specs with the starts at
w0, which they move on to the word number w0 + full; the other words stay as they are. The
moduli and the word number are arguments, since the program does not mention them.
One leg over the tail #
A store of register cur of a leg of Ratpoints.Sieve.tailLeg from off on into the bit array
base + off + cur of the piece.
A leg of w ≤ 8 registers from off on stores into the bit arrays base + off + i, i < w,
the class word and-ed with the rows of all the moduli of the local array at their starts plus
off + i; the other words stay as they are.
The tail #
The four guarded legs over the tail as nested conditional values: the first stage over the
tail of t = n - full bit arrays, the legs of 8, 4, 2 and 1 bit arrays from the offsets 0,
t &&& 8, t &&& 12 and t &&& 14 on.
A guarded leg of w bit arrays from the offset u on, which runs exactly when bit w of t
is set, does the bit arrays from full + u on, t &&& w of them.
The legs over the tail of t = n - full < chunk ≤ 16 bit arrays do its bit arrays
full, …, n - 1: the class word and-ed with the rows of all the moduli of the local array at their
starts plus the place in the tail (Ratpoints.Sieve.partialReg); the other words stay as they
are.
The chunked first stage #
With no modulus, the bit arrays set to the class word are the words of the first stage.
The chunked first stage, for the arm's chunk of at most 16, leaves each word of the piece the
class word cw and-ed with its row words (Ratpoints.Sieve.andWords), as the first stage one row
at a time does (Ratpoints.Sieve.andRows_spec): the whole chunks and the tail read the rows from
their starts at the first word number, into the copies of their first bit arrays
(Ratpoints.Sieve.SieveSpec.Wraps), and with no modulus every bit array is the class word.