Documentation

CslibFrontier.Probability.KWiseIndependent.ReducedLimbs

Limb-Aligned Arithmetic and Bit-Packed Table Bounds for Reduced k-Wise Hashing #

This module develops the pure arithmetic and polynomial-limb identities underlying the Word-RAM implementation of the two-stage universe-reduced k-wise independent hash family (Cslib.DataStructure.HashFamily.WordRAM.ReducedKWise):

  1. Overflow-free full-word multiply-shift (halfMulMod, oddMulModWord): computes (2 * α + 1) * x % 2 ^ w₀ / 2 ^ (w₀ - R) for any 0 < w₀ ≤ w. When α < 2 ^ (w₀ - 1) and x < 2 ^ w₀, every intermediate product and sum is < 2 ^ w₀, avoiding any 2 * w₀ ≤ w restriction.
  2. Exact limb factorization of 2 * 3 ^ t (reducedChunkBits, reducedHalfLimbs, reducedLimbCount): decomposes any 2 * 3 ^ t-bit element of TrinomialField t into reducedLimbCount t = 2 * 3 ^ min t 3 ≤ 54 limbs of reducedChunkBits t = 3 ^ (t - 3) bits each, with 2 * reducedChunkBits t ≤ w unconditionally whenever w₀ ≤ w, b' ≤ w, and 2 ≤ w, and bit-packed Four-Russians table size reducedTableBits t ≤ max 8 (2 ^ (b / 2)) = o(2 ^ b) bits (reducedTableWords t w ≤ 2 * reducedTableBits t / w + 1 words) whenever b' ≤ 3 * b.
  3. Bit-packed word slices (getLimb) and limb-level polynomial multiplication / 4-block trinomial reduction (packLimbs, truncLimbs): proves that limb-by-limb carryless multiplication followed by limb-aligned 4-block XOR reduction computes exact multiplication and addition in TrinomialField t.

Main definitions #

Main results #

Section 1: Overflow-free full-word multiplyShiftReduced arithmetic #

Computes (α * (x' % 2 ^ m)) % 2 ^ m by splitting m at h = m / 2. Whenever α < 2 ^ m and x' < 2 ^ m, every intermediate product and sum is < 2 ^ (m + 1).

Computes (2 * α + 1) * x % 2 ^ w₀ using halfMulMod (w₀ - 1) so that every intermediate quantity is strictly less than 2 ^ w₀ whenever α < 2 ^ (w₀ - 1) and x < 2 ^ w₀.

theorem Cslib.Probability.halfMulMod_lt (m α x' : ℕ) :
halfMulMod m α x' < 2 ^ m
theorem Cslib.Probability.oddMulModWord_lt {w₀ : ℕ} (hw₀ : 0 < w₀) (α x : ℕ) :
oddMulModWord w₀ α x < 2 ^ w₀
theorem Cslib.Probability.oddMulModWord_parts_lt {w₀ α_val u_val : ℕ} (hw₀ : 0 < w₀) (hα : α_val < 2 ^ (w₀ - 1)) (hu : u_val < 2 ^ w₀) :
have m := w₀ - 1; have h := m / 2; have x' := u_val &&& 2 ^ m - 1; have α₀ := α_val &&& 2 ^ h - 1; have α₁ := α_val >>> h; have x₀ := x' &&& 2 ^ h - 1; have x₁ := x' >>> h; have p₀ := α₀ * x₀ &&& 2 ^ m - 1; have p₁ := (α₀ * x₁ &&& 2 ^ (m - h) - 1) <<< h; have p₂ := (α₁ * x₀ &&& 2 ^ (m - h) - 1) <<< h; have p₃ := ((α₁ &&& 2 ^ (m - 2 * h) - 1) * (x₁ &&& 2 ^ (m - 2 * h) - 1)) <<< (2 * h); have s₁ := p₀ + p₁ &&& 2 ^ m - 1; have s₂ := s₁ + p₂ &&& 2 ^ m - 1; have hm := s₂ + p₃ &&& 2 ^ m - 1; α₀ * x₀ < 2 ^ w₀ ∧ α₀ * x₁ < 2 ^ w₀ ∧ (α₀ * x₁ &&& 2 ^ (m - h) - 1) <<< h < 2 ^ w₀ ∧ α₁ * x₀ < 2 ^ w₀ ∧ (α₁ * x₀ &&& 2 ^ (m - h) - 1) <<< h < 2 ^ w₀ ∧ (α₁ &&& 2 ^ (m - 2 * h) - 1) * (x₁ &&& 2 ^ (m - 2 * h) - 1) < 2 ^ w₀ ∧ ((α₁ &&& 2 ^ (m - 2 * h) - 1) * (x₁ &&& 2 ^ (m - 2 * h) - 1)) <<< (2 * h) < 2 ^ w₀ ∧ p₀ + p₁ < 2 ^ w₀ ∧ s₁ + p₂ < 2 ^ w₀ ∧ s₂ + p₃ < 2 ^ w₀ ∧ u_val >>> 1 + hm < 2 ^ w₀ ∧ (u_val >>> 1 + hm &&& 2 ^ m - 1) <<< 1 < 2 ^ w₀ ∧ (u_val &&& 1) + (u_val >>> 1 + hm &&& 2 ^ m - 1) <<< 1 < 2 ^ w₀

Every intermediate product, shift, and sum in halfMulMod (w₀ - 1) α_val (u_val &&& ...) and oddMulModWord w₀ α_val u_val is strictly < 2 ^ w₀ whenever 0 < w₀, α_val < 2 ^ (w₀ - 1), and u_val < 2 ^ w₀.

theorem Cslib.Probability.halfMulMod_eq (m α x' : ℕ) :
halfMulMod m α x' = α * x' % 2 ^ m

Exact modular correctness of halfMulMod: for all m α x' : ℕ, halfMulMod m α x' equals (α * x') % 2 ^ m.

theorem Cslib.Probability.oddMulModWord_eq {w₀ : ℕ} (hw₀ : 0 < w₀) (α x : ℕ) :
oddMulModWord w₀ α x = (2 * α + 1) * x % 2 ^ w₀

Exact modular correctness of oddMulModWord: for all 0 < w₀, oddMulModWord w₀ α x = (2 * α + 1) * x % 2 ^ w₀.

theorem Cslib.Probability.oddMulModWord_shiftRight_eq_multiplyShiftReduced {U w₀ R : ℕ} (hw₀ : 0 < w₀) (α : Fin (2 ^ (w₀ - 1))) (u : Fin U) :
oddMulModWord w₀ ↑α ↑u >>> (w₀ - R) = ↑(multiplyShiftReduced U w₀ R α u)

Shifting oddMulModWord w₀ α.val u.val right by w₀ - R computes (multiplyShiftReduced U w₀ R α u).val.

Section 2: Limb parameters and bit-packed Four-Russians table bounds #

Limb bit-width c = 3 ^ (t - 3) for trinomial parameter t.

Half-field limb count L₀ = 3 ^ min t 3 ≤ 27, so L₀ * c = 3 ^ t.

Full-field limb count L = 2 * L₀ = 2 * 3 ^ min t 3 ≤ 54, so L * c = 2 * 3 ^ t.

theorem Cslib.Probability.two_mul_reducedChunkBits_le {w₀ b b' w : ℕ} (hw₀ : w₀ ≤ w) (hb' : b' ≤ w) (hw : 2 ≤ w) :

Whenever w₀ ≤ w, b' ≤ w, and 2 ≤ w, 2 * reducedChunkBits (reducedTrinomialStep w₀ b b') fits in a single w-bit word (≤ w) unconditionally for all b : ℕ.

Total number of bits (2 * c) * 2 ^ (2 * c) in the Four-Russians carryless multiplication table for trinomial step t.

theorem Cslib.Probability.reducedTableBits_le {w₀ b b' : ℕ} (hb' : b' ≤ 3 * b) :

Whenever b' ≤ 3 * b, the Four-Russians table bit count satisfies reducedTableBits (reducedTrinomialStep w₀ b b') ≤ max 8 (2 ^ (b / 2)) = o(2 ^ b).

Log₂ of the number of 2 * c-bit entries packed into each w-bit word: p = min (2 * c) ⌊log₂ (w / (2 * c))⌋.

Number of w-bit words 2 ^ (2 * c - p) occupied by the bit-packed Four-Russians table.

The bit-packed Four-Russians table uses at most 2 * reducedTableBits t / w + 1 words.

Section 3: Limb extraction, packing, and 4-block trinomial reduction on limbs #

Extract the m-th c-bit limb (a >>> (m * c)) &&& (2 ^ c - 1) of a natural number a.

theorem Cslib.Probability.getLimb_eq_zero_of_lt {c a m : ℕ} (ha : a < 2 ^ (m * c)) :
getLimb c a m = 0

Above bit m * c, the m-th c-bit limb of a < 2 ^ (m * c) is 0.

theorem Cslib.Probability.getLimb_lt (c a m : ℕ) :
getLimb c a m < 2 ^ c
theorem Cslib.Probability.testBit_getLimb (c a m k : ℕ) :
(getLimb c a m).testBit k = (decide (k < c) && a.testBit (m * c + k))
theorem Cslib.Probability.getLimb_xor (c a b m : ℕ) :
getLimb c (a ^^^ b) m = getLimb c a m ^^^ getLimb c b m
theorem Cslib.Probability.getLimb_and_low {c a m s : ℕ} (hm : m < s) :
getLimb c (a &&& 2 ^ (s * c) - 1) m = getLimb c a m
theorem Cslib.Probability.getLimb_and_high (c a : ℕ) {s m : ℕ} (hm : s ≤ m) :
getLimb c (a &&& 2 ^ (s * c) - 1) m = 0
theorem Cslib.Probability.getLimb_and_self (c a s : ℕ) :
getLimb c (a &&& 2 ^ (s * c) - 1) s = 0
theorem Cslib.Probability.getLimb_shiftRight_mul (c a s m : ℕ) :
getLimb c (a >>> (s * c)) m = getLimb c a (s + m)
theorem Cslib.Probability.getLimb_shiftLeft_mul_low (c a : ℕ) {s m : ℕ} (hm : m < s) :
getLimb c (a <<< (s * c)) m = 0
theorem Cslib.Probability.getLimb_shiftLeft_mul_add (c a s m : ℕ) :
getLimb c (a <<< (s * c)) (s + m) = getLimb c a m
theorem Cslib.Probability.getLimb_xor_shiftLeft_self {c a val s : ℕ} (ha : a < 2 ^ (s * c)) (hval : val < 2 ^ c) :
getLimb c (a ^^^ val <<< (s * c)) s = val
theorem Cslib.Probability.getLimb_xor_shiftLeft_of_ne {c a val s m : ℕ} (hval : val < 2 ^ c) (hne : m ≠ s) :
getLimb c (a ^^^ val <<< (s * c)) m = getLimb c a m
def Cslib.Probability.packLimbs (c : ℕ) :
ℕ → (ℕ → ℕ) → ℕ

Pack n limbs f 0, …, f (n - 1) of c bits each into a single natural number < 2 ^ (n * c).

theorem Cslib.Probability.packLimbs_congr {c n : ℕ} {f g : ℕ → ℕ} (h : ∀ i < n, f i = g i) :
packLimbs c n f = packLimbs c n g
theorem Cslib.Probability.packLimbs_lt (c n : ℕ) (f : ℕ → ℕ) :
packLimbs c n f < 2 ^ (n * c)
theorem Cslib.Probability.getLimb_packLimbs (c : ℕ) {n m : ℕ} (f : ℕ → ℕ) (hm : m < n) :
getLimb c (packLimbs c n f) m = f m &&& 2 ^ c - 1
theorem Cslib.Probability.getLimb_packLimbs_of_lt (c : ℕ) {n m : ℕ} {f : ℕ → ℕ} (hm : m < n) (hf : f m < 2 ^ c) :
getLimb c (packLimbs c n f) m = f m
theorem Cslib.Probability.getLimb_packLimbs_of_ge (c : ℕ) {n m : ℕ} (f : ℕ → ℕ) (hm : n ≤ m) :
getLimb c (packLimbs c n f) m = 0
theorem Cslib.Probability.eq_of_getLimb_eq {c : ℕ} (hc : 0 < c) {a b : ℕ} (h : ∀ (m : ℕ), getLimb c a m = getLimb c b m) :
a = b
theorem Cslib.Probability.packLimbs_getLimb {c : ℕ} (hc : 0 < c) (n : ℕ) {a : ℕ} (ha : a < 2 ^ (n * c)) :
packLimbs c n (getLimb c a) = a
theorem Cslib.Probability.packLimbs_xor {c : ℕ} (n : ℕ) (f g : ℕ → ℕ) :
(packLimbs c n fun (i : ℕ) => f i ^^^ g i) = packLimbs c n f ^^^ packLimbs c n g
theorem Cslib.Probability.getLimb_zero_of_lt {c val : ℕ} (hval : val < 2 ^ c) :
getLimb c val 0 = val
theorem Cslib.Probability.packLimbs_update_xor {c n i v : ℕ} (f : ℕ → ℕ) (hi : i < n) (hv : v < 2 ^ c) :
packLimbs c n (Function.update f i (f i ^^^ v)) = packLimbs c n f ^^^ v <<< (i * c)
theorem Cslib.Probability.packLimbs_update_pair {c n i v : ℕ} (f : ℕ → ℕ) (hi : i + 1 < n) (hv : v < 2 ^ (2 * c)) :
packLimbs c n (Function.update (Function.update f i (f i ^^^ v &&& 2 ^ c - 1)) (i + 1) (f (i + 1) ^^^ v >>> c)) = packLimbs c n f ^^^ v <<< (i * c)

Updating two consecutive limbs i and i + 1 of f by XORing v &&& (2 ^ c - 1) and v >>> c (where v < 2 ^ (2 * c)) XORs packLimbs c n f with v <<< (i * c).

theorem Cslib.Probability.getLimb_reduceTrinomialCode_low (t P : ℕ) {m : ℕ} (hm : m < reducedHalfLimbs t) :
have c := reducedChunkBits t; have L₀ := reducedHalfLimbs t; getLimb c (reduceTrinomialCode t P) m = getLimb c P m ^^^ getLimb c P (3 * L₀ + m) ^^^ getLimb c P (2 * L₀ + m)

Low-half limb formula (m < L₀) for reduceTrinomialCode t P.

theorem Cslib.Probability.getLimb_reduceTrinomialCode_high (t P : ℕ) {m : ℕ} (hm : m < reducedHalfLimbs t) :
have c := reducedChunkBits t; have L₀ := reducedHalfLimbs t; getLimb c (reduceTrinomialCode t P) (L₀ + m) = getLimb c P (L₀ + m) ^^^ getLimb c P (4 * L₀ + m) ^^^ getLimb c P (2 * L₀ + m)

High-half limb formula (m < L₀, limb index L₀ + m) for reduceTrinomialCode t P.

def Cslib.Probability.limbsEquivFin (c L : ℕ) (hc : 0 < c) :
(Fin L → Fin (2 ^ c)) ≃ Fin (2 ^ (L * c))

Equivalence between L-tuples of c-bit limbs Fin L → Fin (2 ^ c) and Fin (2 ^ (L * c)).

Low n limbs a &&& (2 ^ (n * c) - 1) of a natural number a.

theorem Cslib.Probability.truncLimbs_of_lt {c n a : ℕ} (ha : a < 2 ^ (n * c)) :
truncLimbs c n a = a
theorem Cslib.Probability.truncLimbs_succ (c n a : ℕ) :
truncLimbs c (n + 1) a = truncLimbs c n a ^^^ getLimb c a n <<< (n * c)
theorem Cslib.Probability.clmul_truncLimbs_right_succ (c i m xc x : ℕ) :
clmul xc (truncLimbs c (m + 1) x) <<< (i * c) = clmul xc (truncLimbs c m x) <<< (i * c) ^^^ clmul xc (getLimb c x m) <<< ((i + m) * c)
theorem Cslib.Probability.clmul_truncLimbs_left_succ (c i a x : ℕ) :
clmul (truncLimbs c (i + 1) a) x = clmul (truncLimbs c i a) x ^^^ clmul (getLimb c a i) x <<< (i * c)

Mask 2 ^ min c (b' - m * c) - 1 for extracting the < b' bits of limb m.

Shift if m * c < b' then m * c else 0 for placing limb m into the < b' output word.

theorem Cslib.Probability.packLimbs_and_mask_succ (c b' n : ℕ) (f : ℕ → ℕ) :
packLimbs c (n + 1) f &&& 2 ^ b' - 1 = packLimbs c n f &&& 2 ^ b' - 1 ^^^ (f n &&& reducedOutMask c b' n) <<< reducedOutShift c b' n
theorem Cslib.Probability.shiftRight_add_one_le (idx p : ℕ) :
(idx + 1) >>> p ≤ idx >>> p + 1
theorem Cslib.Probability.and_lt_of_lt_of_shiftRight_eq {i idx p : ℕ} (hlt : i < idx) (heq : i >>> p = idx >>> p) :
i &&& 2 ^ p - 1 < idx &&& 2 ^ p - 1
theorem Cslib.Probability.xor_shiftLeft_lt_two_pow {c s a v : ℕ} (ha : a < 2 ^ (s * c)) (hv : v < 2 ^ c) :
a ^^^ v <<< (s * c) < 2 ^ ((s + 1) * c)
noncomputable def Cslib.Probability.hornerPrefix (t k : ℕ) (c_field : Fin k → TrinomialField t) (x_field : TrinomialField t) :

Ascending Horner prefix over TrinomialField t, processing coefficients from k - 1 down to k - n as n goes from 0 to k.

theorem Cslib.Probability.hornerPrefix_succ_of_lt {t k n : ℕ} (hn : n < k) (c_field : Fin k → TrinomialField t) (x_field : TrinomialField t) :
hornerPrefix t k c_field x_field (n + 1) = hornerPrefix t k c_field x_field n * x_field + c_field ⟨k - 1 - n, ⋯⟩
theorem Cslib.Probability.hornerPrefix_eq_sum {t k : ℕ} (c_field : Fin k → TrinomialField t) (x_field : TrinomialField t) (n : ℕ) (hn : n ≤ k) :
hornerPrefix t k c_field x_field n = ∑ i : Fin n, c_field ⟨k - 1 - ↑i, ⋯⟩ * x_field ^ (n - 1 - ↑i)
theorem Cslib.Probability.hornerPrefix_self_eq_polynomialHash {t k : ℕ} (c_field : Fin k → TrinomialField t) (x_field : TrinomialField t) :
hornerPrefix t k c_field x_field k = polynomialHash c_field x_field