The contracts of the bit arrays of words #
The lemmas on the operations on the bit arrays of 64-bit words of the model, on its rows as words,
on the rows under a unit, on the check of a numerator and on the array of a walk through the set
bits of a word, and the contract of the first stage over a piece, as @[spec] theorems proved with
vcgen, the program logic of Lean core, with loop invariants that are named definitions.
Main definitions #
Ratpoints.Sieve.Factor.HasInverses,Ratpoints.Sieve.SieveEntry.HasInverses: what the scaling of the rows by a unit needs of a factor of a modulus, its modulus dividing the modulus and its table of inverses inverting its units, and of a sieve entry, all its factors having it.Ratpoints.Sieve.SieveSpec.wordAt,Ratpoints.Sieve.andWords: the row word that the first stage ands into a word of a piece, at the offset of the row, and the words of a piece after it.Ratpoints.Sieve.SieveSpec.Reads: the rows of a modulus read a test of the bits.Ratpoints.Sieve.AndRowsFillInvariant,Ratpoints.Sieve.AndRowsLoopInvariant,Ratpoints.Sieve.AndRowsStartInvariant: the invariants of the loop of the first stage one row at a time that sets the bit arrays to the class word, from the last, of its loop over the moduli with their reciprocals, and of its loops over the bit arrays of a modulus.Ratpoints.Sieve.WalkWordInvariant: the invariant of the array that a walk through the set bits of a word, lowest first, appends to.
Main statements #
Ratpoints.Sieve.getElem!_andBitArray,Ratpoints.Sieve.getElem!_loadBitArray,Ratpoints.Sieve.getElem!_storeBitArray,Ratpoints.Sieve.getElem!_fillBitArray: the operations on bit arrays of the first stage set the words of one bit array and leave the other words as they are;Ratpoints.Sieve.getElem!_andBitArray_mul_add,Ratpoints.Sieve.getElem!_loadBitArray_mul_add: the same at wordκof bit arrayj.Ratpoints.Sieve.getLsbD_clearLowBits,Ratpoints.Sieve.getLsbD_clearHighBits: which bits the masks at the two ends of the numerator range keep.Ratpoints.Sieve.isZeroAt_eq_true_iff: a bit array is zero when all its words are.Ratpoints.Sieve.Entry.admits_mod_of_dvd,Ratpoints.Sieve.Power.admits_mod_of_dvd,Ratpoints.Sieve.Factor.admits_mod_of_dvd: the row of a prime, a prime power or a factor reads the residues modulo a multiple of its modulus as those of the numbers.Ratpoints.Sieve.Factor.admits_congr,Ratpoints.Sieve.Factor.admits_mod_left: the row of a factor reads the residues of the denominator and the numerator modulo its modulus.Ratpoints.Sieve.SieveEntry.admits_eq_all: the row of a sieve entry whose factors have moduli dividing its modulus is the and of the rows of its factors;Ratpoints.Sieve.SieveEntry.admits_eq_of_factors_eq: that of an entry with one factor of its modulus is the row of the factor.Ratpoints.Sieve.SieveEntry.admits_mul: the row of a sieve entry whose factors have their tables of inverses is unchanged when the denominator and the numerator are scaled by a unit.Ratpoints.Sieve.SieveEntry.reads_closedRow: the closed form of the row ofb / 2 ^ k(Ratpoints.Sieve.closedRow) at the offset of the classa₀ mod 2 ^ kreads at bitjwhether the row ofbadmits the numeratora₀ + 2 ^ k j.Ratpoints.Sieve.getLsbD_andWords: the words of rows that read tests of the bits, and-ed into a word, have the bits of the word that pass all the tests.Ratpoints.Sieve.andWords_append: and-ing the rows of two lists of moduli one after the other is and-ing those of their concatenation.Ratpoints.Sieve.andRows_spec: after the first stage over a piece from the class wordcw, with moduli in the range of the packing and their reciprocals (Ratpoints.Sieve.SieveSpec.Fits) and the testsmallof the piece (Ratpoints.Sieve.SmallFits), each word of the piece iscwand-ed with its row words (Ratpoints.Sieve.andWords).Ratpoints.BruteForce.pointsOfDen_eq_flatMap_checkNum: the points that the brute-force reference finds over a denominator are those ofRatpoints.Sieve.checkNumfor its numerators, one after the other, with the flag of the reversal clear.Ratpoints.Sieve.checkNum_eq_map,Ratpoints.Sieve.flatMap_checkNum: with the flag of the reversal set, the check of a numerator gives the points of the check with the flag clear, mapped byRatpoints.Sieve.swapPoint, and so do the checks of a list of numerators, one after the other.
Implementation notes #
The file uses Lean core and Std only, as Ratpoints.Proof.Sieve does; the lemmas on words that
are not about the model are in Ratpoints.Proof.Bits. The proofs use only that Ratpoints.rbaPack
and Ratpoints.chunk are positive, never their values.
The width of the bit arrays #
Operations on bit arrays #
The rows as words #
The rows under a unit #
The row of a sieve entry whose factors have moduli dividing its modulus admits a numerator when each factor does, the factors reading the residues modulo their own moduli.
A sieve entry whose one factor f has the modulus of the entry, as a prime or a prime power is
its own factor (Ratpoints.makeModulus), has the row of the factor.
What the scaling of the rows by a unit needs of a factor of the modulus P of a sieve entry:
the modulus of the factor divides P, and its table of inverses inverts the residues at which its
row reads it, the nonzero residues of a prime and the units of a prime power p ^ e, whose prime
divides it (Ratpoints.Sieve.SieveEntry.admits_mul).
Equations
Instances For
The factors of a sieve entry have their tables of inverses for its modulus
(Ratpoints.Sieve.Factor.HasInverses): what the scaling of its row by a unit needs
(Ratpoints.Sieve.SieveEntry.admits_mul).
Equations
- e.HasInverses = ∀ (f : Ratpoints.Sieve.Factor), f ∈ e.factors → f.HasInverses e.p
Instances For
The row of a prime q > 0 whose table inverts the nonzero residues is unchanged when the
denominator and the numerator are scaled by a unit u modulo q.
The row of a prime power p ^ e with p ∣ p ^ e, whose table inverts its units, is unchanged
when the denominator and the numerator are scaled by a unit u modulo p ^ e.
The row of a factor of a modulus P > 0 with its tables of inverses is unchanged when the
denominator and the numerator are scaled by a unit u modulo P.
The row of a sieve entry whose factors have their tables of inverses is unchanged when the
denominator and the numerator are scaled by a unit u modulo the modulus, with an inverse v.
One piece #
The word of the row of s that the first stage ands into word y of the piece from the word
number w0 on: word y % rbaPack of bit array (w0 + offset + y / rbaPack) mod p of the row, for
the number rbaPack of words of a bit array (Ratpoints.rbaPack).
Equations
Instances For
Bit t of the row word of word y of a piece from the word number w0 on, in the closed form
of the row of the denominator b with the offset off and any start and end, is the bit of the
numerator rbaLength (w0 + off) + 64 y + t in the row of b.
The rows of the sieve spec s read the test f of the bits: bit t of the row word of word
y of a piece from the word number i on is f at the bit rbaLength i + 64 y + t.
Equations
Instances For
The closed form of the row of the residue bp of the denominator b divided by the stride
2 ^ k (2 ^ k bp ≡ b), with any start and end, read at the offset off of the class
a₀ mod 2 ^ k (off 2 ^ k Ratpoints.rbaLength ≡ a₀), reads at bit j whether the row of b
admits the numerator a₀ + 2 ^ k j, when 2 ^ k has an inverse v modulo p and the factors of
the entry have their tables of inverses: the rows are unchanged when the denominator and the
numerator are scaled by the unit 2 ^ k (Ratpoints.Sieve.SieveEntry.admits_mul).
The word cw and-ed in turn with the words of the rows specs at word y of a piece from the
word number w0 on (Ratpoints.Sieve.SieveSpec.wordAt): word y of the piece after the first
stage from the class word cw, or, with w0 the word number of a bit array and y one of its
words, that word after the second stage from its word cw in a copy of the bit array.
Equations
- Ratpoints.Sieve.andWords specs cw w0 y = List.foldl (fun (acc : UInt64) (s : Ratpoints.Sieve.SieveSpec) => acc &&& s.wordAt w0 y) cw specs
Instances For
The words of rows that read the tests sf.2 of the bits (Ratpoints.Sieve.SieveSpec.Reads),
and-ed into the word cw, have bit t set when cw has it and all the tests pass at the bit
rbaLength w0 + 64 y + t.
The reciprocals of the moduli specs that a loop over the moduli and their reciprocals in step
has still to read after the moduli pref, before it has gone through all of them, are those after
the first pref.length: the next is that of the next modulus, and the cursor moves on.
The reciprocals of a loop over the moduli and their reciprocals in step do not run out before the moduli.
The invariant of the loop of Ratpoints.Sieve.andRows over the moduli and, in step with them,
their reciprocals mgs, whose state is the piece and the reciprocals still to come: after the
moduli pref, the reciprocals still to come are those after the first pref.length, and the words
of the n bit arrays of the piece are the class word cw and-ed with their row words
(Ratpoints.Sieve.andWords).
Equations
Instances For
The invariant of the loops of Ratpoints.Sieve.andRows over the bit arrays for the modulus
s after the moduli pref: the words of the bit arrays below c are and-ed with the row of s
as well, the others not yet.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The residue r of -w0 - offset modulo p that Ratpoints.Sieve.andRows computes with the
reciprocal mg of the cursor, by Ratpoints.Sieve.modMul under the test small and by
Ratpoints.Sieve.modLong otherwise: the first stage ands bit array p - r + j of the row of s
into bit array j of a piece from the word number w0 on, for j < r.
Equations
Instances For
Ratpoints.Sieve.AndRowsRowInvariant for the modulus s with the bit arrays below c done,
and r, the index that the loops over the bit arrays use, is the residue of -(w0 + offset)
modulo p > 0: the invariant of each loop over the bit arrays, with the c of that loop.
Equations
Instances For
The invariant of the loops of Ratpoints.Sieve.andRows over the bit arrays before the first
whole period of the row of s, with the reciprocal mg of the cursor: the bit arrays done are
done.
Equations
- Ratpoints.Sieve.AndRowsHeadInvariant small cw w0 n pref s mg done surv = Ratpoints.Sieve.AndRowsStartInvariant cw w0 n pref s (Ratpoints.Sieve.andRowsStart small w0 s mg) done.length surv
Instances For
The invariant of the loop of Ratpoints.Sieve.andRows over the whole periods of the row of
s, with the reciprocal mg of the cursor: the bit arrays before the first whole period and the
periods done are done.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The invariant of the loop of Ratpoints.Sieve.andRows over one whole period of the row of s,
with the reciprocal mg of the cursor, after the periods periods: the bit arrays done of the
current period are done too.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The invariant of the loop of Ratpoints.Sieve.andRows over the bit arrays after the last whole
period of the row of s, with the reciprocal mg of the cursor: the bit arrays before them and
the bit arrays done are done.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The invariant of the loop of Ratpoints.Sieve.andRows that sets the bit arrays of the piece to
the class word cw, from the last: after c steps, the words of the last c of the n bit arrays
are cw.
Equations
- Ratpoints.Sieve.AndRowsFillInvariant cw n c surv = ∀ (y : Nat), y < n * Ratpoints.rbaPack → n - c ≤ y / Ratpoints.rbaPack → surv[y]! = cw
Instances For
A step of the loop of Ratpoints.Sieve.andRows that sets the bit arrays to the class word:
bit array n - 1 - cur set.
After the bit arrays are all set to the class word, the loop over the moduli starts with no moduli.
The reciprocals do not run out before the moduli.
The index of the first row of the first stage, as Ratpoints.Sieve.andRows computes it with the
reciprocal mg of the cursor, is the residue of -(w0 + offset)
(Ratpoints.Sieve.SieveSpec.Fits.ite_modMul_modLong_eq_emod), since mg is the reciprocal of the
modulus.
After the bit arrays of the modulus s are all done, the cursor moves on.
One step of the loops of Ratpoints.Sieve.andRows for the modulus s: bit array c and-ed
with bit array (w0 + offset + c) mod p of the row.
The loops of Ratpoints.Sieve.andRows before the first whole period: bit array cur is and-ed
with bit array p - r + cur of the row, for r = -(w0 + offset) mod p.
The loop of Ratpoints.Sieve.andRows over one whole period: bit array r + k p + i is and-ed
with bit array i of the row, for r = -(w0 + offset) mod p.
The loop of Ratpoints.Sieve.andRows after the last whole period: bit array
r + ⌊(n - r) / p⌋ p + i is and-ed with bit array i of the row, for
r = -(w0 + offset) mod p.
Each modulus s has its row read at the offset of s (Ratpoints.Sieve.SieveSpec.wordAt): the
loops over the bit arrays start at the index r of the residue of -(w0 + offset), which the
reduction by the reciprocal of the cursor gives under the test small
(Ratpoints.Sieve.SieveSpec.Fits.ite_modMul_modLong_eq_emod); the reciprocals run in step with the
moduli and do not run out before them.
The check of a numerator and the set bits of a word #
The property of the array out of a walk through the set bits of the word x0, lowest first,
that appends the list g t of each set bit t to the array out0, after the set bits below k:
out is out0 followed by the lists of the set bits t < k of x0, in increasing order. In
Ratpoints.Sieve.wordsDens, g t is the list of the denominator of bit t alone; the walk of the
sieve, which delivers its points to a callback (Ratpoints.Sieve.walkWord), has an invariant of its
own (Ratpoints.Sieve.WalkDeliveryInvariant).
Equations
- Ratpoints.Sieve.WalkWordInvariant g x0 out0 k out = (out.toList = out0.toList ++ List.flatMap g (List.filter (fun (x : Nat) => x0.toBitVec.getLsbD x) (List.range k)))