Documentation

CslibFrontier.Algorithms.WordRAM.Refuter4n.PackedCode

Word-RAM code for the packed state of the 4n - 9d refuter #

Standard-basis Word-RAM routines on the memory image of the packed state PackedState, each with an execution theorem in a caller-given memory window, in the library's Word-RAM cost model (Cslib.WordRAM.ExecWithin: every instruction is charged). This file is the first part of the step code of issue #4322 (Milestone 4); it claims no running time for a step or for the refuter.

Register file. All routines run in Code .standard 32. Registers 0 to 13 form the callee bank (calleeBank, the identity embedding of Fin 14) in which the dense elimination routines XORSATDense.elimPass and XORSATDense.elimCode run, so every register from 14 on survives a call (calleeBank_injective, calleeBank_val, calleeBank_ne). The arguments of the routines live in the registers of Reg.

Memory image. The packed state lives at the addresses of a Layout and is described by the predicate Stored of InputLayout, the one that exec_initCode establishes. Stored does not relate the sizes of the layout to the table lengths of the state, so the execution theorems take the lengths they need as separate hypotheses. Stored.gateRow, Stored.eqnRow, Stored.phiRow and Stored.pair read the words of a stored state, row by row; Stored.of_words of InputLayout is the other direction. Stored.of_eq_below keeps a stored state with at most L.N gate rows, L.E equation words, L.n parameter rows and L.N pair records over a memory that agrees with it below L.top.

Row substitution. Substituting the name v by the row u in a row r (substWord v u r) is the elimination step elimWord v (2 ^ v ^^^ u) r with pivot word 2 ^ v ^^^ u (substWord_eq_elimWord). The substitution pass substPass over a window of rows is therefore one call of XORSATDense.elimPass, after six instructions that set its arguments.

Main definitions #

Main results #

The callee bank: registers 0 to 13 of the step code, where the dense elimination routines run.

The callee bank keeps the register number.

theorem Cslib.Algorithms.WordRAM.Refuter4n.calleeBank_ne {i : Fin 32} (hi : 14 ≤ ↑i) (j : Fin 14) :

A register from 14 on is outside the callee bank.

@[reducible, inline]

The base address of the window of a pass.

@[reducible, inline]

The number of rows of the window of a pass.

@[reducible, inline]

The substituted name of a pass.

@[reducible, inline]

The substituted row of a pass.

@[reducible, inline]

First argument of a word primitive.

@[reducible, inline]

Second argument of a word primitive.

@[reducible, inline]

Third argument of a word primitive.

@[reducible, inline]

Fourth argument of a word primitive.

@[reducible, inline]

The name bound N of a word primitive.

@[reducible, inline]

The result of a word primitive (a register of the callee bank).

@[reducible, inline]

The base address of the image of the packed state.

@[reducible, inline]

The number E of equation words.

@[reducible, inline]

First scratch register of the word primitives and of the routines on the image (a register of the callee bank).

@[reducible, inline]

Second scratch register of the word primitives and of the routines on the image (a register of the callee bank).

@[reducible, inline]

Third scratch register of the word primitives and of the routines on the image (a register of the callee bank).

Row substitution as an elimination step #

Substituting the name x by the row u is the elimination step at column x with pivot word 2 ^ x ^^^ u.

The substitution pass #

Substitute the name in register Reg.passVar by the row in register Reg.passWord in every row of the window of Reg.passLen words at Reg.passBase (exec_substPass).

theorem Cslib.Algorithms.WordRAM.Refuter4n.exec_substPass {w n a v u : ℕ} (W : List ℕ) (x : WordRAM.Data 32) (ha : x.regs Reg.passBase = a) (hm : x.regs Reg.passLen = W.length) (hv : x.regs Reg.passVar = v) (hu : x.regs Reg.passWord = u) (hvn : v ≤ n) (hun : u < 2 ^ (n + 1)) (hn : WordRAM.Fits w (2 ^ (n + 1) - 1)) (hmfit : WordRAM.Fits w (a + W.length)) (hmem : ∀ j < W.length, x.mem (a + j) = W.getD j 0) (hWlt : ∀ r ∈ W, r < 2 ^ (n + 1)) :
∃ (y : WordRAM.Data 32), WordRAM.ExecWithin w substPass x y (14 * W.length + 9) ∧ (∀ (i : Fin 32), 14 ≤ ↑i → y.regs i = x.regs i) ∧ (∀ j < W.length, y.mem (a + j) = substWord v u (W.getD j 0)) ∧ ∀ (c : ℕ), c < a ∨ a + W.length ≤ c → y.mem c = x.mem c

The substitution pass in a caller-given memory window. Let the caller's state x hold a base address a, the number m of rows, a name v ≤ n and a row u < 2 ^ (n + 1) in the registers Reg.passBase, Reg.passLen, Reg.passVar and Reg.passWord, with the row W[j] < 2 ^ (n + 1) at address a + j for j < m. Memory outside this window and the other registers are arbitrary. At every width w at which a + m and 2 ^ (n + 1) - 1 fit, substPass replaces cell a + j by substWord v u W[j] within 14 m + 9 instructions, preserves every register from 14 on, and preserves every memory cell outside [a, a + m).

Word primitives #

Straight-line or one-branch routines that compute the word operations of PackedOps from argument registers into Reg.result, using only registers below 14.

theorem Cslib.Algorithms.WordRAM.Refuter4n.exec_clearBitCode {w a i : ℕ} (x : WordRAM.Data 32) (ha : x.regs Reg.argA = a) (hi : x.regs Reg.argB = i) (hfa : WordRAM.Fits w a) (hfi : WordRAM.Fits w (2 ^ i)) :
∃ (y : WordRAM.Data 32), WordRAM.ExecWithin w clearBitCode x y 4 ∧ y.mem = x.mem ∧ (∀ (j : Fin 32), 14 ≤ ↑j → y.regs j = x.regs j) ∧ y.regs Reg.result = clearBit a i

At every width at which a and 2 ^ i fit, clearBitCode computes clearBit a i in 4 instructions, changing no memory and no register from 14 on.

theorem Cslib.Algorithms.WordRAM.Refuter4n.exec_setBitCode {w a i : ℕ} (x : WordRAM.Data 32) (ha : x.regs Reg.argA = a) (hi : x.regs Reg.argB = i) (hfa : WordRAM.Fits w a) (hfi : WordRAM.Fits w (2 ^ i)) :
∃ (y : WordRAM.Data 32), WordRAM.ExecWithin w setBitCode x y 3 ∧ y.mem = x.mem ∧ (∀ (j : Fin 32), 14 ≤ ↑j → y.regs j = x.regs j) ∧ y.regs Reg.result = setBit a i

At every width at which a and 2 ^ i fit, setBitCode computes setBit a i in 3 instructions, changing no memory and no register from 14 on.

theorem Cslib.Algorithms.WordRAM.Refuter4n.exec_argWordCode {w N a : ℕ} (x : WordRAM.Data 32) (ha : x.regs Reg.argA = a) (hN : x.regs Reg.argN = N) (haN : a < N + 2) (h4 : 4 ≤ w) (hn : WordRAM.Fits w (2 ^ (N + 1) - 1)) :
∃ (y : WordRAM.Data 32), WordRAM.ExecWithin w argWordCode x y 6 ∧ y.mem = x.mem ∧ (∀ (j : Fin 32), 14 ≤ ↑j → y.regs j = x.regs j) ∧ y.regs Reg.result = argWord N a

argWordCode computes argWord N a for an argument code a < N + 2 in at most 6 instructions at every width w ≥ 4 at which 2 ^ (N + 1) - 1 fits, changing no memory and no register from 14 on.

The row litWordOf of the literal with argument code Reg.argA and negation bit Reg.argC, at the name bound Reg.argN, into Reg.result (exec_litWordCode).

theorem Cslib.Algorithms.WordRAM.Refuter4n.exec_litWordCode {w N la ln : ℕ} (x : WordRAM.Data 32) (ha : x.regs Reg.argA = la) (hl : x.regs Reg.argC = ln) (hN : x.regs Reg.argN = N) (haN : la < N + 2) (hln : ln ≤ 1) (h4 : 4 ≤ w) (hn : WordRAM.Fits w (2 ^ (N + 1) - 1)) :
∃ (y : WordRAM.Data 32), WordRAM.ExecWithin w litWordCode x y 8 ∧ y.mem = x.mem ∧ (∀ (j : Fin 32), 14 ≤ ↑j → y.regs j = x.regs j) ∧ y.regs Reg.result = litWordOf N la ln

litWordCode computes litWordOf N la ln for an argument code la < N + 2 and a negation bit ln ≤ 1 in at most 8 instructions at every width w ≥ 4 at which 2 ^ (N + 1) - 1 fits, changing no memory and no register from 14 on.

substOut of the name Reg.argA, the argument code Reg.argB, the negation bit Reg.argC and the output code Reg.argD, into Reg.result (exec_substOutCode).

theorem Cslib.Algorithms.WordRAM.Refuter4n.exec_substOutCode {w v la ln o : ℕ} (x : WordRAM.Data 32) (hv : x.regs Reg.argA = v) (hla : x.regs Reg.argB = la) (hl : x.regs Reg.argC = ln) (ho : x.regs Reg.argD = o) (hln : ln ≤ 1) (hfo : WordRAM.Fits w o) (hfv : WordRAM.Fits w (v + 2)) (hfa : WordRAM.Fits w (2 * la + 1)) :
∃ (y : WordRAM.Data 32), WordRAM.ExecWithin w substOutCode x y 9 ∧ y.mem = x.mem ∧ (∀ (j : Fin 32), 14 ≤ ↑j → y.regs j = x.regs j) ∧ y.regs Reg.result = substOut v la ln o

substOutCode computes substOut v la ln o for a negation bit ln ≤ 1 in at most 9 instructions, changing no memory and no register from 14 on, at every width at which o, v + 2 and 2 la + 1 fit.

The gate-row pass #

One pass over a window of gate rows (three words each) that reroutes the name v to the literal with argument code la and negation bit ln in every row (substRow): the two arguments are compared with v + 2, the matching ones are overwritten by la and contribute ln to the negation mask m, and the truth table absorbs the mask by the two conditional swaps swapLo and swapHi of TruthTable (absorbNeg, the operation of the input decoder).

Reroute the name in register Reg.passVar to the literal with argument code Reg.argB and negation bit Reg.argC in every row of the window of Reg.passLen gate rows (three words each) at Reg.passBase (exec_gatePass).

The memory mem holds the three words of the gate row r as row J of the table at G.

theorem Cslib.Algorithms.WordRAM.Refuter4n.GateRowAt.words {G J : ℕ} {r : GateRow} {mem : ℕ → ℕ} (h : GateRowAt G J r mem) :
mem (G + (J + J + J)) = r.tt ∧ mem (G + (J + J + J) + 1) = r.left ∧ mem (G + (J + J + J) + 1 + 1) = r.right

The three words of a gate row at the addresses G + (J + J + J), + 1 and + 1 + 1 that the routines reading a row compute.

theorem Cslib.Algorithms.WordRAM.Refuter4n.exec_gatePass {w G v la ln : ℕ} (R : List GateRow) (x : WordRAM.Data 32) (hG : x.regs Reg.passBase = G) (hm : x.regs Reg.passLen = R.length) (hv : x.regs Reg.passVar = v) (hla : x.regs Reg.argB = la) (hln : x.regs Reg.argC = ln) (hln1 : ln ≤ 1) (h4 : 4 ≤ w) (hfit : WordRAM.Fits w (G + 3 * R.length)) (hfv : WordRAM.Fits w (v + 2)) (hR : ∀ r ∈ R, WordRAM.Fits w r.tt) (hrows : ∀ J < R.length, GateRowAt G J (R.getD J { tt := 0, left := 0, right := 0 }) x.mem) :
∃ (y : WordRAM.Data 32), WordRAM.ExecWithin w gatePass x y (37 * R.length + 7) ∧ (∀ (i : Fin 32), 14 ≤ ↑i → y.regs i = x.regs i) ∧ (∀ J < R.length, GateRowAt G J (substRow v la ln (R.getD J { tt := 0, left := 0, right := 0 })) y.mem) ∧ ∀ (c : ℕ), c < G ∨ G + 3 * R.length ≤ c → y.mem c = x.mem c

The gate-row pass in a caller-given memory window. Let the caller's state x hold a base address G, the number m of rows, a name v, an argument code la and a negation bit ln ≤ 1 in the registers Reg.passBase, Reg.passLen, Reg.passVar, Reg.argB and Reg.argC, with the words tt, left, right of the gate row R[J] at G + 3 J, G + 3 J + 1, G + 3 J + 2 for J < m. Memory outside this window and the other registers are arbitrary. At every width w ≥ 4 at which G + 3 m, v + 2 and every truth-table word fit, gatePass replaces row J by substRow v la ln R[J] within 37 m + 7 instructions, preserves every register from 14 on, and preserves every memory cell outside [G, G + 3 m).

The memory image of a packed state #

The packed state P lives at the addresses of a layout L (Layout and Stored of InputLayout): the masks params, aux, gates and the output code at L.base to L.base + 3, the number of pair records at L.base + 4, the gate table (three words per row) from L.rowAddr 0 0, the equation table from L.eqnAddr 0, the parameter rows from L.phiAddr 0 and the pair records (two words each) from L.pairAddr 0 0. Stored is over the full lists of P and carries no list length; the execution theorems take the lengths they need as separate hypotheses (Rep fixes them). The lemmas below read a stored state word by word; Stored.of_words is the other direction.

theorem Cslib.Algorithms.WordRAM.Refuter4n.Stored.gateRow {L : Layout} {P : PackedState} {mem : ℕ → ℕ} (h : Stored L P mem) {J : ℕ} (hJ : J < P.gateRows.length) :
GateRowAt (L.rowAddr 0 0) J (P.gateRows.getD J { tt := 0, left := 0, right := 0 }) mem

Gate row J of a stored state, as row J of the window at the first gate row.

theorem Cslib.Algorithms.WordRAM.Refuter4n.Stored.eqnRow {L : Layout} {P : PackedState} {mem : ℕ → ℕ} (h : Stored L P mem) {e : ℕ} (he : e < P.eqnRows.length) :
mem (L.eqnAddr e) = P.eqnRows.getD e 0

Equation word e of a stored state.

theorem Cslib.Algorithms.WordRAM.Refuter4n.Stored.phiRow {L : Layout} {P : PackedState} {mem : ℕ → ℕ} (h : Stored L P mem) {i : ℕ} (hi : i < P.phiRows.length) :
mem (L.phiAddr i) = P.phiRows.getD i 0

Row i of the parameter map of a stored state.

theorem Cslib.Algorithms.WordRAM.Refuter4n.Stored.pair {L : Layout} {P : PackedState} {mem : ℕ → ℕ} (h : Stored L P mem) {k : ℕ} (hk : k < P.pairs.length) :
mem (L.pairAddr k 0) = (P.pairs.getD k (0, 0)).1 ∧ mem (L.pairAddr k 1) = (P.pairs.getD k (0, 0)).2

Pair record k of a stored state.

theorem Cslib.Algorithms.WordRAM.Refuter4n.Stored.of_eq_below {L : Layout} {P : PackedState} {mem mem' : ℕ → ℕ} (h : Stored L P mem) (hgl : P.gateRows.length ≤ L.N) (hel : P.eqnRows.length ≤ L.E) (hpl : P.phiRows.length ≤ L.n) (hk : P.pairs.length ≤ L.N) (hm : ∀ c < L.top, mem' c = mem c) :
Stored L P mem'

The image of a state with at most L.N gate rows, L.E equation words, L.n parameter rows and L.N pair records reads only the cells below L.top.

theorem Cslib.Algorithms.WordRAM.Refuter4n.Stored.subst {L : Layout} {P : PackedState} {mem : ℕ → ℕ} {v la ln : ℕ} {mem' : ℕ → ℕ} (h : Stored L P mem) (hgate : ∀ J < P.gateRows.length, GateRowAt (L.rowAddr 0 0) J (substRow v la ln (P.gateRows.getD J { tt := 0, left := 0, right := 0 })) mem') (heqn : ∀ e < P.eqnRows.length, mem' (L.eqnAddr e) = substWord v (litWordOf L.N la ln) (P.eqnRows.getD e 0)) (hout : mem' (L.base + 3) = substOut v la ln P.out) (hfr : ∀ (c : ℕ), c ≠ L.base + 3 → c < L.rowAddr 0 0 ∨ L.phiAddr 0 ≤ c → mem' c = mem c) :
Stored L (PackedState.subst L.N v la ln P) mem'

Substitution in the gate and equation tables and the output code gives the image of PackedState.subst, when every other cell is kept.

The packed substitution step #

The packed substitution step PackedState.subst on the image at Reg.base: the gate-row pass, the substitution pass on the equation table with the row litWordOf of the literal, and the output code (exec_substStateCode).

theorem Cslib.Algorithms.WordRAM.Refuter4n.exec_substStateCode {L : Layout} {P : PackedState} {w v la ln : ℕ} (x : WordRAM.Data 32) (hb : x.regs Reg.base = L.base) (hN : x.regs Reg.argN = L.N) (hE : x.regs Reg.numE = L.E) (hv : x.regs Reg.passVar = v) (hla : x.regs Reg.argB = la) (hln : x.regs Reg.argC = ln) (hvN : v < L.N) (haN : la < L.N + 2) (hln1 : ln ≤ 1) (h4 : 4 ≤ w) (hw : L.N + 2 ≤ w) (haddr : WordRAM.Fits w (L.phiAddr 0)) (hI : Stored L P x.mem) (hgl : P.gateRows.length = L.N) (hel : P.eqnRows.length = L.E) (htt : ∀ r ∈ P.gateRows, WordRAM.Fits w r.tt) (heq : ∀ e ∈ P.eqnRows, e < 2 ^ (L.N + 2)) (hout : WordRAM.Fits w P.out) :
∃ (y : WordRAM.Data 32), WordRAM.ExecWithin w substStateCode x y (37 * L.N + 14 * L.E + 49) ∧ Stored L (PackedState.subst L.N v la ln P) y.mem ∧ y.regs Reg.passWord = litWordOf L.N la ln ∧ (∀ (i : Fin 32), 14 ≤ ↑i → i ≠ Reg.passBase → i ≠ Reg.passLen → i ≠ Reg.passWord → i ≠ Reg.argA → i ≠ Reg.argD → y.regs i = x.regs i) ∧ ∀ (c : ℕ), c ≠ L.base + 3 → c < L.rowAddr 0 0 ∨ L.phiAddr 0 ≤ c → y.mem c = x.mem c

The packed substitution step on the memory image. Let the caller's state x hold the base address L.base, the name bound L.N, the number L.E of equation words, a name v < L.N, an argument code la < L.N + 2 and a negation bit ln ≤ 1 in the registers Reg.base, Reg.argN, Reg.numE, Reg.passVar, Reg.argB and Reg.argC, and let the memory hold the image of P at the layout L (Stored), with L.N gate rows, L.E equation words below 2 ^ (L.N + 2), word-sized truth tables and output code. At every width w ≥ max 4 (L.N + 2) at which the end L.phiAddr 0 of the equation table fits, substStateCode leaves the image of P.subst L.N v la ln within 37 L.N + 14 L.E + 49 instructions, leaves the row litWordOf L.N la ln of the literal in Reg.passWord, changes no register from 14 on except Reg.passBase, Reg.passLen, Reg.passWord, Reg.argA and Reg.argD, and changes no memory cell outside the output code, the gate table and the equation table.