Documentation

CslibFrontier.Algorithms.WordRAM.ThreeSum.ChanHe.TraversalStack

Native stack calls for Chan–He traversal records #

Traversal keeps a seven-word record in registers 64–70 and its word stack in registers 96–104. Registers below 64 remain available to the complete modulus and node programs. Each record contains fuel followed by three base/length pairs. The stack stores words, so a record is pushed in reverse order and popped in forward order.

The word stack is a fixed nine-register program, WordStack.program, that dispatches on an operation tag: push = 0, pop = 1, size = 2. It occupies a fixed consecutive memory block, with its top at the highest live address; a pop leaves the old cell in place. Traversal's time bound adds up exact instruction counts, so the two calls it makes, a push with spare capacity and a pop from a nonempty stack, are proved instruction by instruction. The library's bounded stack is Stack.Memory, which Stack.Native runs on the word RAM; this dispatcher is part of the hand-stepped traversal and goes with it. Argument delivery and result copies here are actual native instructions and are charged.

Fixed-length records are transferred through the same dispatcher. Pushing visits a register bank in reverse order; popping visits it in forward order, restoring the same record order. A final self-copy on idle supplies the empty-bank case. The reserved stack memory may be dirty outside its live prefix, and popped cells are not cleared.

Main definitions #

Main results #

The native controller and its composition proofs are original.

References #

@[reducible, inline]

Encoded operation tag: push = 0, pop = 1, size = 2.

@[reducible, inline]

Caller-supplied push value.

@[reducible, inline]

Persistent number of live entries.

@[reducible, inline]

Persistent base address of the reserved region.

@[reducible, inline]

Temporary cell address.

@[reducible, inline]

Presence bit distinguishing none from a returned zero word.

Fixed operation dispatch. No code is generated from an operation or its payload.

Deliver an operation tag and a push value.

Persistent register, bounded-word, and live array invariants. Cells above the live prefix may contain old popped values; they are part of the reserved footprint.

Reserved memory footprint; it stays fixed through pushes and pops.

theorem Cslib.Algorithms.WordRAM.ThreeSum.ChanHe.Traversal.WordStack.exec_push {w base capacity : ℕ} (h2 : WordRAM.Fits w 2) (hcap : WordRAM.Fits w (base + capacity)) {vs : List ℕ} {x : WordRAM.Data 9} {v : ℕ} (hr : Rep w base capacity vs x) (hv : vs.length < capacity) (hw : WordRAM.Fits w v) :
∃ (y : WordRAM.Data 9), WordRAM.Exec w program (prepare 0 v x) y 7 ∧ Rep w base capacity (v :: vs) y ∧ WordRAM.Modifies (footprint base capacity) x.mem y.mem

A push with spare capacity writes the value above the top in seven instructions.

theorem Cslib.Algorithms.WordRAM.ThreeSum.ChanHe.Traversal.WordStack.exec_pop {w base capacity : ℕ} (h2 : WordRAM.Fits w 2) (hcap : WordRAM.Fits w (base + capacity)) {vs : List ℕ} {x : WordRAM.Data 9} {v : ℕ} (hr : Rep w base capacity (v :: vs) x) :
∃ (y : WordRAM.Data 9), WordRAM.Exec w program (prepare 1 0 x) y 11 ∧ y.regs out = v ∧ Rep w base capacity vs y ∧ WordRAM.Modifies (footprint base capacity) x.mem y.mem

A pop from a nonempty stack returns its top in eleven instructions and leaves memory outside the reserved block unchanged.

Push a register bank in reverse order, leaving its words in forward order at the stack top. The terminal self-copy preserves the entire state, including for a zero-length record.

Pop into successive record registers, charging dispatch and delivery for every word. The terminal self-copy supplies the zero-length case.

Pushing a fixed record does not inspect the physical word width.

theorem Cslib.Algorithms.WordRAM.ThreeSum.ChanHe.Traversal.WordStack.popRecord_not_readsWidth {r n : ℕ} (reg : Fin 9 → Fin r) (dst : Fin n → Fin r) (idle : Fin r) :
¬(popRecord reg dst idle).ReadsWidth

Popping a fixed record does not inspect the physical word width.

theorem Cslib.Algorithms.WordRAM.ThreeSum.ChanHe.Traversal.WordStack.pushRecord_exec {r n : ℕ} (reg : Fin 9 → Fin r) (hi : Function.Injective reg) (src : Fin n → Fin r) (idle : Fin r) (hd : ∀ (i : Fin n) (j : Fin 9), src i ≠ reg j) {w base capacity : ℕ} (x : WordRAM.Data r) (vs : List ℕ) (h2 : WordRAM.Fits w 2) (hcap : WordRAM.Fits w (base + capacity)) (hr : Rep w base capacity vs (WordRAM.Data.comap reg x)) (hw : ∀ (i : Fin n), WordRAM.Fits w (x.regs (src i))) (hv : n + vs.length ≤ capacity) :
∃ (y : WordRAM.Data r), WordRAM.Exec w (pushRecord reg src idle) x y (9 * n + 1) ∧ Rep w base capacity ((List.ofFn fun (i : Fin n) => x.regs (src i)) ++ vs) (WordRAM.Data.comap reg y) ∧ WordRAM.Modifies (footprint base capacity) x.mem y.mem ∧ ∀ (j : Fin r), (∀ (i : Fin 9), j ≠ reg i) → y.regs j = x.regs j

Push all record words with exact instruction cost, preserving caller registers and memory outside the reserved stack. Repeated source registers are permitted.

theorem Cslib.Algorithms.WordRAM.ThreeSum.ChanHe.Traversal.WordStack.popRecord_exec {r n : ℕ} (reg : Fin 9 → Fin r) (hi : Function.Injective reg) (dst : Fin n → Fin r) (idle : Fin r) (hinj : Function.Injective dst) (hd : ∀ (i : Fin n) (j : Fin 9), dst i ≠ reg j) {w base capacity : ℕ} (x : WordRAM.Data r) (vs : List ℕ) (a : Fin n → ℕ) (h2 : WordRAM.Fits w 2) (hcap : WordRAM.Fits w (base + capacity)) (hr : Rep w base capacity (List.ofFn a ++ vs) (WordRAM.Data.comap reg x)) :
∃ (y : WordRAM.Data r), WordRAM.Exec w (popRecord reg dst idle) x y (14 * n + 1) ∧ Rep w base capacity vs (WordRAM.Data.comap reg y) ∧ (∀ (i : Fin n), y.regs (dst i) = a i) ∧ WordRAM.Modifies (footprint base capacity) x.mem y.mem ∧ ∀ (j : Fin r), (∀ (i : Fin 9), j ≠ reg i) → (∀ (i : Fin n), j ≠ dst i) → y.regs j = x.regs j

Pop a complete record into distinct registers with exact cost. Memory outside the reserved stack and registers outside the dispatcher and destination bank are preserved.

Fuel and the three base/length pairs, retained across both 64-register native kernels.

Pointer word of the selected pending set.

Length word of the selected pending set.

The existing stack's workspace lies above every 64-register node-kernel register.

@[reducible, inline]

Caller-owned registers outside the stack dispatcher.

Push all seven record words in reverse order through the record transfers.

theorem Cslib.Algorithms.WordRAM.ThreeSum.ChanHe.Traversal.pushFrame_exec {w base capacity : ℕ} (x : WordRAM.Data 128) (vs : List ℕ) (h2 : WordRAM.Fits w 2) (hcap : WordRAM.Fits w (base + capacity)) (hr : WordStack.Rep w base capacity vs (WordRAM.Data.comap registers x)) (hw : ∀ (i : Fin 7), WordRAM.Fits w (x.regs (slot i))) (hv : 7 + vs.length ≤ capacity) :
∃ (y : WordRAM.Data 128), WordRAM.Exec w pushFrame x y 64 ∧ WordStack.Rep w base capacity ([x.regs (slot 0), x.regs (slot 1), x.regs (slot 2), x.regs (slot 3), x.regs (slot 4), x.regs (slot 5), x.regs (slot 6)] ++ vs) (WordRAM.Data.comap registers y) ∧ WordRAM.Modifies (WordStack.footprint base capacity) x.mem y.mem ∧ ∀ (j : Fin 128), Outside j → y.regs j = x.regs j

The complete native frame push charges all seven dispatches and argument delivery. There is no host-level record write, and the exact seven-word sequence is at the stack top.

Pop all seven record words in forward order through the record transfers.

theorem Cslib.Algorithms.WordRAM.ThreeSum.ChanHe.Traversal.popFrame_exec {w base capacity : ℕ} (x : WordRAM.Data 128) (vs : List ℕ) (a : Fin 7 → ℕ) (h2 : WordRAM.Fits w 2) (hcap : WordRAM.Fits w (base + capacity)) (hr : WordStack.Rep w base capacity ([a 0, a 1, a 2, a 3, a 4, a 5, a 6] ++ vs) (WordRAM.Data.comap registers x)) :
∃ (y : WordRAM.Data 128), WordRAM.Exec w popFrame x y 99 ∧ WordStack.Rep w base capacity vs (WordRAM.Data.comap registers y) ∧ (∀ (i : Fin 7), y.regs (slot i) = a i) ∧ WordRAM.Modifies (WordStack.footprint base capacity) x.mem y.mem ∧ ∀ (j : Fin 128), Outside j → ↑j < 64 ∨ 71 ≤ ↑j → y.regs j = x.regs j

The complete record pop charges every argument, dispatch and result-copy instruction. The seven restored words have their original order; other caller registers are preserved.