Documentation

CslibFrontier.DataStructure.Dictionary.WordRAM.BitPackedArray

Overflow-free bit-packed arrays on the w-bit Word RAM #

This file defines a bit-packed array primitive storing N entries of k bits each (0 ≤ k ≤ w) across packedWords N k w = ⌈N * k / w⌉ + 2 contiguous w-bit machine words starting at address base.

To avoid intermediate arithmetic overflow (idx * k ≥ 2^w) when idx < N < 2^w approaches 2^w and k ≤ w is Θ(w) (so that idx * k can reach 2^w), the word and bit offsets of index idx are computed via the Euclidean decomposition idx = (idx / w) * w + (idx % w):

Each k-bit entry at (wOff, bOff) splits into lowWidth = min k (w - bOff) low bits at offset bOff of word base + wOff and highWidth = k - lowWidth high bits at offset 0 of word base + wOff + 1, updated in place via bitwise XOR. Unlike QuotientFilter's single-word extractPacked/writePackedMem (which packs ⌊w / k⌋ entries per word without crossing word boundaries and wastes up to half of each word when k > w / 2), allowing entries to straddle adjacent words achieves the tight ⌈N * k / w⌉ + 2 word bound needed for succinctness. Unlike Cslib.BitString.readPackedField, which is a pure bit-level specification over a tuple of words with no Word-RAM machine code or memory layout, this module provides O(1)-step Word-RAM read and write programs over ℕ → ℕ.

Main definitions #

Main results #

Section 1: Overflow-free index arithmetic and word bounds #

Number of w-bit words (N * k + w - 1) / w + 2 reserved for N packed k-bit entries. The + 2 slack ensures that for every idx < N, both wordOffset k w idx and wordOffset k w idx + 1 lie strictly inside [0, packedWords N k w).

theorem Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.div_add_packedWords_le (B C₀ N_aux k_aux w : ℕ) :
(B + w - 1) / w + C₀ + packedWords N_aux k_aux w ≤ (B + N_aux * k_aux + w - 1) / w + (C₀ + 3)

Adding an auxiliary bit-packed array packedWords N_aux k_aux w to an existing word bound ((B + w - 1) / w) + C₀ increases the bit numerator to B + N_aux * k_aux and the additive word constant by at most 3.

theorem Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.packedWords_add_le (N₁ k₁ N₂ k₂ w : ℕ) :
packedWords N₁ k₁ w + packedWords N₂ k₂ w ≤ (N₁ * k₁ + N₂ * k₂ + w - 1) / w + 5

Ceiling-division subadditivity for packedWords: packedWords N₁ k₁ w + packedWords N₂ k₂ w ≤ (N₁ * k₁ + N₂ * k₂ + w - 1) / w + 5.

theorem Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.div_add_mul_packedWords_le (c B C₀ N_aux k_aux w : ℕ) :
(B + w - 1) / w + C₀ + c * packedWords N_aux k_aux w ≤ (B + c * N_aux * k_aux + w - 1) / w + (C₀ + 3 * c)

Adding c copies of packedWords N_aux k_aux w increases the bit numerator to B + (c * N_aux) * k_aux and the additive word constant by at most 3 * c.

Overflow-free word offset (idx / w) * k + ((idx % w) * k) / w, equal to (idx * k) / w.

Overflow-free bit offset ((idx % w) * k) % w < w, equal to (idx * k) % w.

Number of bits min k (w - bitOffset k w idx) of entry idx stored in word base + wordOffset k w idx.

Number of bits k - lowWidth k w idx of entry idx stored in word base + wordOffset k w idx + 1.

For every w ≥ 1, (w - 1) * w < 2 ^ w.

theorem Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.mod_mul_lt_two_pow {w k : ℕ} (hw : 1 ≤ w) (hkw : k ≤ w) (idx : ℕ) :
idx % w * k < 2 ^ w

Whenever 1 ≤ w and k ≤ w, the product (idx % w) * k is strictly less than 2 ^ w.

wordOffset k w idx equals (idx * k) / w for every w > 0.

bitOffset k w idx equals (idx * k) % w.

bitOffset k w idx < w whenever 0 < w.

Fundamental global bit identity: wordOffset k w idx * w + bitOffset k w idx = idx * k.

For every idx < N, wordOffset k w idx + 1 < packedWords N k w.

For every idx < N, wordOffset k w idx < packedWords N k w.

bitOffset k w idx + lowWidth k w idx ≤ w whenever 0 < w.

Whenever highWidth k w idx > 0 and 0 < w, bitOffset k w idx + lowWidth k w idx = w.

theorem Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.highWidth_lt {k w idx : ℕ} (hw : 0 < w) (hkw : k ≤ w) :
highWidth k w idx < w

Whenever k ≤ w, highWidth k w idx < w for w > 0.

Section 2: Bitwise shift/mask algebra and disjoint-interval non-interference #

Shifting x < 2 ^ len left by a and right by a, then masking to len bits, recovers x.

theorem Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.shiftLeft_shiftRight_and_eq_zero {x a len b len' : ℕ} (hx : x < 2 ^ len) (hdisj : a + len ≤ b ∨ b + len' ≤ a) :
x <<< a >>> b &&& 2 ^ len' - 1 = 0

Shifting x < 2 ^ len left by a, right by b, and masking to len' bits gives 0 whenever the intervals [a, a + len) and [b, b + len') are disjoint (a + len ≤ b ∨ b + len' ≤ a).

theorem Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.extract_xor_shiftLeft_of_disjoint (word : ℕ) {δ a len b len' : ℕ} (hδ : δ < 2 ^ len) (hdisj : a + len ≤ b ∨ b + len' ≤ a) :
(word ^^^ δ <<< a) >>> b &&& 2 ^ len' - 1 = word >>> b &&& 2 ^ len' - 1

Extracting len' bits at offset b after XORing a word with δ <<< a (δ < 2 ^ len) leaves the extracted bits unchanged whenever [a, a + len) and [b, b + len') are disjoint.

theorem Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.extract_xor_shiftLeft_self (word a len : ℕ) {val : ℕ} (hval : val < 2 ^ len) :
have old := word >>> a &&& 2 ^ len - 1; (word ^^^ (old ^^^ val) <<< a) >>> a &&& 2 ^ len - 1 = val

Extracting len bits at offset a after replacing the field old with val < 2 ^ len via word ^^^ ((old ^^^ val) <<< a) returns val.

theorem Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.low_xor_high_shiftLeft {val k₀ k₁ : ℕ} (hval : val < 2 ^ (k₀ + k₁)) :
val &&& 2 ^ k₀ - 1 ^^^ (val >>> k₀ &&& 2 ^ k₁ - 1) <<< k₀ = val

Recombining the low k₀ bits val &&& (2 ^ k₀ - 1) and high k₁ bits (val >>> k₀) &&& (2 ^ k₁ - 1) recovers val whenever val < 2 ^ (k₀ + k₁).

Section 3: Pure bit-slice and bit-packed array read/write and laws #

Read a k-bit field starting at (base + wOff, bOff) across at most 2 words.

def Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.writeBitSlice (base wOff bOff k w : ℕ) (mem : ℕ → ℕ) (val : ℕ) :
ℕ → ℕ

Write val < 2 ^ k into the k-bit field starting at (base + wOff, bOff).

theorem Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.readBitSlice_lt (base wOff bOff k w : ℕ) (mem : ℕ → ℕ) :
readBitSlice base wOff bOff k w mem < 2 ^ k

Every value returned by readBitSlice base wOff bOff k w mem fits in k bits (< 2 ^ k).

theorem Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.readBitSlice_zero {base wOff bOff k w : ℕ} {mem : ℕ → ℕ} (h0 : mem (base + wOff) = 0) (h1 : mem (base + wOff + 1) = 0) :
readBitSlice base wOff bOff k w mem = 0

Reading a bit-slice from zeroed words returns 0.

theorem Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.readBitSlice_writeBitSlice_self (base wOff bOff k w : ℕ) (mem : ℕ → ℕ) {val : ℕ} (hval : val < 2 ^ k) :
readBitSlice base wOff bOff k w (writeBitSlice base wOff bOff k w mem val) = val

Reading a bit slice immediately after writing val < 2 ^ k returns val.

theorem Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.extract_update_xor_of_disjoint (mem : ℕ → ℕ) (addr_w addr_r : ℕ) {δ a len b len' : ℕ} (hδ : δ < 2 ^ len) (hdisj : addr_w = addr_r → a + len ≤ b ∨ b + len' ≤ a) :
Function.update mem addr_w (mem addr_w ^^^ δ <<< a) addr_r >>> b &&& 2 ^ len' - 1 = mem addr_r >>> b &&& 2 ^ len' - 1

Updating word addr_w by XORing at [a, a + len) preserves extracting [b, b + len') from addr_r whenever every overlap addr_w = addr_r has disjoint bit intervals (a + len ≤ b ∨ b + len' ≤ a).

theorem Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.readBitSlice_writeBitSlice_of_disjoint {base wOff bOff k wOff' bOff' k' w : ℕ} (hb : bOff < w) (hb' : bOff' < w) (hdisj : wOff * w + bOff + k ≤ wOff' * w + bOff' ∨ wOff' * w + bOff' + k' ≤ wOff * w + bOff) (mem : ℕ → ℕ) (val : ℕ) :
readBitSlice base wOff' bOff' k' w (writeBitSlice base wOff bOff k w mem val) = readBitSlice base wOff' bOff' k' w mem

Non-interference of writeBitSlice and readBitSlice for disjoint global bit intervals.

Read the k-bit entry at index idx from bit-packed array at base on a w-bit machine.

Write val (< 2 ^ k) into the k-bit entry at index idx of bit-packed array at base.

theorem Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.readPacked_lt (base k w : ℕ) (mem : ℕ → ℕ) (idx : ℕ) :
readPacked base k w mem idx < 2 ^ k

Every value returned by readPacked base k w mem idx fits in k bits (< 2 ^ k).

theorem Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.readPacked_zero {base k w : ℕ} {mem : ℕ → ℕ} {idx : ℕ} (h0 : mem (base + wordOffset k w idx) = 0) (h1 : mem (base + wordOffset k w idx + 1) = 0) :
readPacked base k w mem idx = 0

Reading from zeroed words returns 0.

theorem Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.readPacked_writePacked_self (base k w : ℕ) (mem : ℕ → ℕ) (idx : ℕ) {val : ℕ} (hval : val < 2 ^ k) :
readPacked base k w (writePacked base k w mem idx val) idx = val

Reading index idx immediately after writing val < 2 ^ k at idx returns val.

theorem Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.readPacked_writePacked_of_ne {base k w : ℕ} (hw : 0 < w) (mem : ℕ → ℕ) {idx idx' : ℕ} (hne : idx' ≠ idx) (val : ℕ) :
readPacked base k w (writePacked base k w mem idx val) idx' = readPacked base k w mem idx'

Reading index idx' ≠ idx after writing at idx returns the original value at idx'.

theorem Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.readPacked_writePacked_bounded {base N k w idx val : ℕ} (hw : 0 < w) (hidx : idx < N) (hval : val < 2 ^ k) (mem : ℕ → ℕ) :
(fun (j : ℕ) => if j < N then readPacked base k w (writePacked base k w mem idx val) j else 0) = Function.update (fun (j : ℕ) => if j < N then readPacked base k w mem j else 0) idx val

A packed write updates the bounded logical array and preserves its zero padding.

theorem Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.writePacked_outside {base N k w : ℕ} (hw : 0 < w) (mem : ℕ → ℕ) {idx : ℕ} (hidx : idx < N) (val : ℕ) {addr : ℕ} (haddr : ¬(base ≤ addr ∧ addr < base + packedWords N k w)) :
writePacked base k w mem idx val addr = mem addr

Writing at idx < N leaves all memory addresses outside [base, base + packedWords N k w) unchanged.

theorem Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.readPacked_congr {base N k w : ℕ} (hw : 0 < w) {mem₁ mem₂ : ℕ → ℕ} (h : ∀ (addr : ℕ), base ≤ addr ∧ addr < base + packedWords N k w → mem₁ addr = mem₂ addr) {idx : ℕ} (hidx : idx < N) :
readPacked base k w mem₁ idx = readPacked base k w mem₂ idx

readPacked depends only on memory addresses in [base, base + packedWords N k w).

theorem Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.readPacked_writePacked_disjoint {off_r N_r k_r off_w N_w k_w w idx_r idx_w val : ℕ} {mem : ℕ → ℕ} (hw : 0 < w) (hr : idx_r < N_r) (hw_idx : idx_w < N_w) (hdisj : off_r + packedWords N_r k_r w ≤ off_w ∨ off_w + packedWords N_w k_w w ≤ off_r) :
readPacked off_r k_r w (writePacked off_w k_w w mem idx_w val) idx_r = readPacked off_r k_r w mem idx_r

readPacked on [off_r, off_r + packedWords N_r k_r w) is invariant under writePacked on a disjoint segment [off_w, off_w + packedWords N_w k_w w).

theorem Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.readPacked_update_outside {off_r N_r k_r w addr val idx : ℕ} {mem : ℕ → ℕ} (hw : 0 < w) (hidx : idx < N_r) (ha : addr < off_r ∨ off_r + packedWords N_r k_r w ≤ addr) :
readPacked off_r k_r w (Function.update mem addr val) idx = readPacked off_r k_r w mem idx

Updating a single plain word at addr < off_r or off_r + packedWords N_r k_r w ≤ addr leaves readPacked off_r k_r w unchanged on 0 .. N_r - 1.

theorem Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.readPacked_eq_zero_of_zero_range {arrBase N k w idx : ℕ} {mem : ℕ → ℕ} (hw : 0 < w) (hidx : idx < N) (hz : ∀ (a : ℕ), arrBase ≤ a → a < arrBase + packedWords N k w → mem a = 0) :
readPacked arrBase k w mem idx = 0

Reading idx < N from a packed segment whose backing words are all 0 returns 0.

Whenever k ≤ w and 0 < highWidth k w idx, lowWidth k w idx < w.

theorem Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.shiftLeft_lt_two_pow_of_lt {x a b w : ℕ} (hx : x < 2 ^ a) (hab : a + b ≤ w) :
x <<< b < 2 ^ w

If x < 2 ^ a and a + b ≤ w, then x <<< b < 2 ^ w.

theorem Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.writePacked_lt_two_pow {base k w : ℕ} (hw : 0 < w) (hkw : k ≤ w) {mem : ℕ → ℕ} (hmem : ∀ (addr : ℕ), mem addr < 2 ^ w) (idx val addr : ℕ) :
writePacked base k w mem idx val addr < 2 ^ w

writePacked base k w mem idx val preserves word-boundedness (< 2 ^ w) at every memory address whenever 0 < w and k ≤ w.

Section 4: Structured Word-RAM programs (readPackedCode and writePackedCode) #

First half of addrSetupCode: given reg 0 = base + offset, reg 1 = idx, reg 8 = w, and reg 9 = k, computes addr₀ (reg 3), addr₁ (reg 4), and bOff (reg 5) in 10 deterministic instructions while preserving reg 0, 1, 2, 8, 9.

Second half of addrSetupCode: given reg 5 = bOff, reg 8 = w, and reg 9 = k, computes k₀ (reg 6), mask₀ = 2 ^ k₀ - 1 (reg 7), and mask₁ = 2 ^ k₁ - 1 (reg 8) in at most 15 deterministic instructions without overflowing < 2 ^ w even when k₀ = w.

Parameter-free address and bit-mask setup subprogram on Data 10: Given reg 0 = base + offset, reg 1 = idx, reg 8 = w, and reg 9 = k (preserving reg 0, 1, 2), computes:

Read suffix on Data 10 after addrSetupCode: loads the two words at reg 3 and reg 4, extracts and combines the low and high bit slices into reg 2.

Parameter-free structured Word-RAM program on Data 10 that reads readPacked (base + offset) k w mem idx into reg 2, given reg 0 = base + offset, reg 1 = idx, reg 8 = w, and reg 9 = k, preserving reg 0, reg 1, and mem.

Write suffix on Data 10 after addrSetupCode: updates the low bit slice at reg 3 and the high bit slice at reg 4 from reg 2 = val, preserving reg 0, 1, 2.

Parameter-free structured Word-RAM program on Data 10 that writes val (reg 2) at idx (reg 1) of the bit-packed array at base + offset (reg 0 = base + offset, reg 8 = w, reg 9 = k), preserving reg 0, 1, 2.

theorem Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.offsetSetupCode_execWithin {base offset N k w idx : ℕ} {x : WordRAM.Data 10} (hw : 0 < w) (hkw : k ≤ w) (hidx : idx < N) (hbase : x.regs 0 = base + offset) (hr1 : x.regs 1 = idx) (hr8 : x.regs 8 = w) (hr9 : x.regs 9 = k) (hfit_idx : WordRAM.Fits w idx) (hfit_end : WordRAM.Fits w (base + offset + packedWords N k w)) :
∃ (x₁ : WordRAM.Data 10), WordRAM.Exec w offsetSetupCode x x₁ 10 ∧ x₁.mem = x.mem ∧ x₁.regs 0 = base + offset ∧ x₁.regs 1 = idx ∧ x₁.regs 2 = x.regs 2 ∧ x₁.regs 3 = base + offset + wordOffset k w idx ∧ x₁.regs 4 = base + offset + wordOffset k w idx + 1 ∧ x₁.regs 5 = bitOffset k w idx ∧ x₁.regs 8 = w ∧ x₁.regs 9 = k

offsetSetupCode executes deterministically in 10 instructions.

theorem Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.addrSetupCode_execWithin {base offset N k w idx : ℕ} {x : WordRAM.Data 10} (hw : 0 < w) (hkw : k ≤ w) (hidx : idx < N) (hbase : x.regs 0 = base + offset) (hr1 : x.regs 1 = idx) (hr8 : x.regs 8 = w) (hr9 : x.regs 9 = k) (hfit_idx : WordRAM.Fits w idx) (hfit_end : WordRAM.Fits w (base + offset + packedWords N k w)) :
∃ (x₁ : WordRAM.Data 10), WordRAM.ExecWithin w addrSetupCode x x₁ 25 ∧ x₁.mem = x.mem ∧ x₁.regs 0 = base + offset ∧ x₁.regs 1 = idx ∧ x₁.regs 2 = x.regs 2 ∧ x₁.regs 3 = base + offset + wordOffset k w idx ∧ x₁.regs 4 = base + offset + wordOffset k w idx + 1 ∧ x₁.regs 5 = bitOffset k w idx ∧ x₁.regs 6 = lowWidth k w idx ∧ x₁.regs 7 = 2 ^ lowWidth k w idx - 1 ∧ x₁.regs 8 = 2 ^ highWidth k w idx - 1

addrSetupCode executes deterministically in at most 25 instructions.

theorem Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.readPackedCode_execWithin {base offset N k w idx : ℕ} {x : WordRAM.Data 10} (hw : 0 < w) (hkw : k ≤ w) (hidx : idx < N) (hx : WordRAM.Data.Bounded w x) (hbase : x.regs 0 = base + offset) (hr1 : x.regs 1 = idx) (hr8 : x.regs 8 = w) (hr9 : x.regs 9 = k) (hfit_end : WordRAM.Fits w (base + offset + packedWords N k w)) :
∃ (x₂ : WordRAM.Data 10), WordRAM.ExecWithin w readPackedCode x x₂ 34 ∧ x₂.mem = x.mem ∧ x₂.regs 0 = base + offset ∧ x₂.regs 1 = idx ∧ x₂.regs 2 = readPacked (base + offset) k w x.mem idx

readPackedCode executes deterministically in at most 34 instructions, placing readPacked (base + offset) k w x.mem idx into reg 2 while preserving reg 0, reg 1, and x.mem.

theorem Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.writePackedCode_execWithin {base offset N k w idx val : ℕ} {x : WordRAM.Data 10} (hw : 0 < w) (hkw : k ≤ w) (hidx : idx < N) (hx : WordRAM.Data.Bounded w x) (hbase : x.regs 0 = base + offset) (hr1 : x.regs 1 = idx) (hr2 : x.regs 2 = val) (hr8 : x.regs 8 = w) (hr9 : x.regs 9 = k) (hfit_end : WordRAM.Fits w (base + offset + packedWords N k w)) :
∃ (x₂ : WordRAM.Data 10), WordRAM.ExecWithin w writePackedCode x x₂ 45 ∧ x₂.mem = writePacked (base + offset) k w x.mem idx val ∧ x₂.regs 0 = base + offset ∧ x₂.regs 1 = idx ∧ x₂.regs 2 = val

writePackedCode executes deterministically in at most 45 instructions, updating x.mem to writePacked (base + offset) k w x.mem idx val while preserving reg 0 = base + offset, reg 1 = idx, and reg 2 = val.