The contracts of the builders of the rows of a prime #
The contracts of the functions that the build of ratpoints generates for the primes of the table
from CODE_INIT_SIEVE1 (Ratpoints.Sieve.sieveInit1, a prime below 64) and CODE_INIT_SIEVE2
(Ratpoints.Sieve.sieveInit2, a prime above 64), and of their pieces and macros, as @[spec]
theorems proved with vcgen, the program logic of Lean core, with loop invariants that are named
definitions: the row of the residue 0 < b < p of a prime p is the closed form of the row of b
of its table (Ratpoints.Sieve.closedRow of Ratpoints.Sieve.Entry.admits), and the builder stores
it into the table of rows of its entry (Ratpoints.Sieve.RowStore.publish).
The pattern of a row is computed four bits at a time by four lanes, whose residues step through the
residues x d mod p for the inverse d of b (Ratpoints.Sieve.LanesAt,
Ratpoints.Sieve.lanePattern); CODE_INIT_SIEVE1 doubles the pattern of p bits up to a word
and rotates it through the first p words, CODE_INIT_SIEVE2 keeps it in a ring of ⌊p / 64⌋ + 1
words (Ratpoints.Sieve.RingState); both end with the copies of RP_INIT_REPLICATE.
Main definitions #
Ratpoints.Sieve.lanePattern,Ratpoints.Sieve.LanesAt: the pattern of the lanes and the lanes at a bit of it.Ratpoints.Sieve.RingState: the ring of the words ofCODE_INIT_SIEVE2.
Main statements #
Ratpoints.Sieve.initFourDecl_lanes,Ratpoints.Sieve.initFourLoop_spec: the four lanes and the bits that they compute.Ratpoints.Sieve.initReplicate_spec: the copies ofRP_INIT_REPLICATE.Ratpoints.Sieve.sieveInit1_spec,Ratpoints.Sieve.sieveInit2_spec: the rows of a prime below and above 64 are the closed forms of the rows of their tables, stored into the tables of rows of their entries.
Notation #
Local to the file: diffShift p, the diff_shift of CODE_INIT_SIEVE1; wordLen p k, the number
of the bits of word k of the pattern in CODE_INIT_SIEVE2; lanePatternOf t b, the pattern of
the lanes of the row of the residue b of the table t.
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; every shift that the
builders execute is shown to be by an amount below 64, where Lean's shifts and the C code's agree.
The builders need of the prime only the inverse of b below p, and for CODE_INIT_SIEVE2 that
p mod 64 ≠ 0: no primality and nothing of the table of squares.
The four lanes #
The pattern of the lanes from the residue ab₀ on, stepping by d modulo p: bit N is the
entry of isfs at ab₀ + N d mod p. It has the period p (Ratpoints.Sieve.lanePattern_mod).
Instances For
The four lanes at the bit x of the pattern from ab₀ on: the residues of ab₀ + (x + k) d
modulo p, k < 4.
Equations
Instances For
RP_INIT_FOUR_DECL puts the lanes at the bits 0, 1, 2, 3 of the pattern from 0 on, for
d < p, and their step at 4 d mod p.
The bits i, …, i + 3 of the four lanes, or-ed into the word of the bits below i.
The lanes and the word of the loop of four lanes at the bit i, for the step d of the lanes
from the residue ab₀ on.
Equations
- Ratpoints.Sieve.FourLanes isfs p d ab0 w ab ab1 ab2 ab3 i = (Ratpoints.Sieve.LanesAt p d ab0 i ab ab1 ab2 ab3 ∧ Ratpoints.Sieve.PatternBits (Ratpoints.Sieve.lanePattern isfs p ab0 d) p 0 i w)
Instances For
The invariant of the loop of Ratpoints.Sieve.initFourLoop from the bit 0 and the word 0
on, with the lanes ab₀, ab1₀, ab2₀, ab3₀ at the start: the bit i a multiple of 4 up to hi, and
for every step d of the lanes with d4 = 4 d mod p, the lanes at i and the bits below i of
the pattern of the lanes in the word.
Equations
- One or more equations did not get rendered due to their size.
Instances For
A step of the loop of four lanes.
The loop of four lanes from the bit 0 and the word 0 on, as its callers start it, ends at
the bit 4 ⌊hi / 4⌋ with the bits below it of the pattern of the lanes in the word and the lanes
at that bit, for every step d < p of the lanes with d4 = 4 d mod p.
The bits left over #
The state of a loop that sets the bits from lo on one at a time from the first lane, after c
of them: the word holds the bits of the pattern of the lanes from ab₀ on below lo + c, and the
lane is at the bit lo + c.
Equations
- Ratpoints.Sieve.RestInvariant isfs p d ab0 lo c st = (Ratpoints.Sieve.PatternBits (Ratpoints.Sieve.lanePattern isfs p ab0 d) p 0 (lo + c) st.fst ∧ st.snd = (ab0 + (lo + c) * d) % p)
Instances For
A bit set from the first lane, RP_INIT_BIT, and the lane stepped by d.
The loop of the bits left over from lo ≤ hi on ends with the bits below hi.
The rows of a prime below 64 #
The pattern of the row of the residue b of the prime p = t.p ≤ 64 with the inverse
d < p of b: bit a < p is the entry of the table at a d mod p, the higher bits clear.
The copies of RP_INIT_REPLICATE #
The invariant of the loops of the copies of Ratpoints.Sieve.initReplicate through the first
p bit arrays, with the residues below a done and of residue a the copies up to a + c p: the
words below p and those done are the words of si below p that they repeat.
Equations
Instances For
The copies of residue a all done: the residues below a + 1 are done.
The step of the loop of the copies of a residue, in the shape of the verification condition.
A copy of word k to word p rbaPack + k, in the shape of the verification condition.
After the copies, word j of the row is word j mod p of the row before, for every j below
the size (p + chunk - 1) rbaPack of the row.
CODE_INIT_SIEVE1 #
The shifts of CODE_INIT_SIEVE1 for 0 < p < 64: diff_shift = (⌊64 / p⌋ + 1) p mod 64 and
64 mod p add up to p, 64 + diff_shift is the multiple (⌊64 / p⌋ + 1) p of p, and
0 < diff_shift < 64, so that every shift of the rotation is by an amount below 64.
The rotation of CODE_INIT_SIEVE1: from the window at s in x0 and the next ds bits in
x1, for 64 + ds ≡ 0 (mod p), the window at s + 64 and the ds bits after it.
The invariant of the loop of the rotation of Ratpoints.Sieve.sieve1Rows after the words below
c + 1: help0 the window at 64 c, help1 the next ds bits, and the words below c + 1 the
windows of the pattern.
Equations
- One or more equations did not get rendered due to their size.
Instances For
A step of the rotation: word c + 1 of the row the window at 64 (c + 1).
The first p words of the row of a prime p < 64: for every pattern g of period p whose
window at 0 is help0 and whose diff_shift bits after it are help1, word a < p is the
window of g at 64 a.
One doubling x | x << i of the pattern of period p ∣ i in the bits below i < 64.
The invariant of the loop of Ratpoints.Sieve.sieveInit1 that doubles the pattern of period
p through the word, on the word and the shift.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The row of words whose first p words are the windows of the pattern of the lanes, copied
through all the words, is the closed form of the row of 0 < b < p, and so is what the builder
stores.
The pattern shifted right by 64 mod p holds the diff_shift bits after the window at 0.
The row of the residue 0 < b < p of a prime p < 64 by CODE_INIT_SIEVE1 is the closed form
of the row of b of its table, for the inverse of b below p, and the builder stores it into the
table of rows of the entry ref.
CODE_INIT_SIEVE2 #
The lanes at the bit x of the pattern from 0 on are at the bit 0 of the pattern from their
first residue on.
The pattern of the lanes from the residue x d mod p on is the pattern from 0 on at x.
The word n of the pattern of the row and the lanes after it.
Word n of the pattern of the row of a prime p in CODE_INIT_SIEVE2, from the lanes at the
bit 64 n of the pattern from 0 on: the bits 64 n, … of the pattern, 64 of them for
n < ⌊p / 64⌋ and p mod 64 for the last word, and after a whole word the lanes at the next.
The invariant of the loop of Ratpoints.Sieve.sieve2Pattern over the words of the pattern
after the words below c: word k < c holds the bits 64 k, … of the pattern, 64 of them for
k < wp and ds for k = wp, and the four lanes are at the bit 64 c.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The precondition of the word cur of the pattern.
A step of the loop of the pattern: word cur of help set to the word of the pattern.
The pattern of the row of the residue b of a prime p in CODE_INIT_SIEVE2, for the inverse
d < p of b: word k ≤ ⌊p / 64⌋ of help holds the bits 64 k, … of the pattern of the lanes
from 0 on, 64 of them for k < ⌊p / 64⌋ and p mod 64 for the last.
The ring of CODE_INIT_SIEVE2 at the bit s of the pattern with the cursor a1 ≤ wp: word
a1 holds the ds bits from s on, and word (a1 + 1 + k) mod (wp + 1) the 64 bits from
s + ds + 64 k on, for k < wp.
Equations
- One or more equations did not get rendered due to their size.
Instances For
After a step of the ring, the word at the new cursor t, shifted right by 64 - ds, holds the
ds bits from s + 64 on.
After a step of the ring, the words after the new cursor hold the 64 bits from
s + 64 + ds + 64 k on: the words of the ring before the step moved up by one, the last the word
written.
A step of the ring: help[a1] |= help[t] << ds, the cursor moved on to t, and
help[t] >>= 64 - ds, for p = 64 wp + ds, 0 < wp, 0 < ds < 64: the ring at s + 64.
A copy of word cur < ⌊p / 64⌋ of the pattern into the row.
The start of the ring: the cursor at wp, the ring at the bit 64 wp, the first wp words of
the row copied.
A step of the loop of the ring, in the shape of the verification condition.
The first p words of the row of a prime p > 64 with p mod 64 ≠ 0 in CODE_INIT_SIEVE2:
for every pattern g of period p held in the words help (Ratpoints.Sieve.PatternWords),
word a < p of the row is the window of g at 64 a.
The row of the residue 0 < b < p of a prime p ≥ 64 with p mod 64 ≠ 0 by CODE_INIT_SIEVE2
is the closed form of the row of b of its table, for the inverse of b below p, and the builder
stores it into the table of rows of the entry ref. The words help that the builder clears from
the last are overwritten by the pattern before they are read.