Documentation

CslibFrontier.Algorithms.WordRAM.Freivalds

Freivalds' Matrix-Product Verifier on the Randomized Word RAM #

Cslib.Algorithms.WordRAM.Freivalds.verifyProgram implements Freivalds' k-trial randomized matrix-product verifier ([Freivalds1977], [Freivalds1979]; [MitzenmacherUpfal2017], Section 1.3; [MotwaniRaghavan95], Section 7.1) on the randomized word RAM (Cslib.WordRAM.runRand over Basis.standard).

On input encode q k A B C, storing a positive modulus q, trial count k, dimensions l, m, n, and the row-major entries (M i j).val of A : Matrix (Fin l) (Fin m) (ZMod q), B : Matrix (Fin m) (Fin n) (ZMod q) and C : Matrix (Fin l) (Fin n) (ZMod q):

  1. the setup loads the header parameters and locates four disjoint scratch slices immediately after the input for a length-n 0/1 vector x and three modular product vectors y, z, w;
  2. each of k independent trials samples n uniform bits via rand one (Cslib.WordRAM.sampleBits), evaluates y = B *ᵥ (bit ∘ x), z = A *ᵥ y and w = C *ᵥ (bit ∘ x) modulo q through a shared deterministic matrix-vector subroutine (mulVecCode), compares z with w, and folds the Boolean result into allOk;
  3. the compiled program halts with 1 when all k trials pass and 0 otherwise.

Whenever Fits w (wordBound q k l m n) holds (O(log (q + k + l + m + n)) word bits suffice, and in particular O(log (n + k + 2)) bits for n × n matrices over ZMod 2), verifyProgram_spec proves:

Main definitions #

Main results #

References #

Word layout and memory views #

def Cslib.Algorithms.WordRAM.Freivalds.encodeMatrix {q rows cols : ℕ} (M : Matrix (Fin rows) (Fin cols) (ZMod q)) :

Row-major word list of a rows × cols matrix over ZMod q.

@[simp]
theorem Cslib.Algorithms.WordRAM.Freivalds.length_encodeMatrix {q rows cols : ℕ} (M : Matrix (Fin rows) (Fin cols) (ZMod q)) :
(encodeMatrix M).length = rows * cols
theorem Cslib.Algorithms.WordRAM.Freivalds.getElem_encodeMatrix {q rows cols : ℕ} (M : Matrix (Fin rows) (Fin cols) (ZMod q)) (i : Fin rows) (j : Fin cols) :
have hlt := ⋯; (encodeMatrix M)[↑i * cols + ↑j] = (M i j).val
theorem Cslib.Algorithms.WordRAM.Freivalds.mem_encodeMatrix_lt {q rows cols : ℕ} [NeZero q] (M : Matrix (Fin rows) (Fin cols) (ZMod q)) {v : ℕ} (hv : v ∈ encodeMatrix M) :
v < q
def Cslib.Algorithms.WordRAM.Freivalds.encode (q k : ℕ) {l m n : ℕ} (A : Matrix (Fin l) (Fin m) (ZMod q)) (B : Matrix (Fin m) (Fin n) (ZMod q)) (C : Matrix (Fin l) (Fin n) (ZMod q)) :

Input word list for k trials of Freivalds' verifier modulo q on rectangular matrices A : l × m, B : m × n and C : l × n.

@[simp]
theorem Cslib.Algorithms.WordRAM.Freivalds.length_encode (q k : ℕ) {l m n : ℕ} (A : Matrix (Fin l) (Fin m) (ZMod q)) (B : Matrix (Fin m) (Fin n) (ZMod q)) (C : Matrix (Fin l) (Fin n) (ZMod q)) :
(encode q k A B C).length = 5 + (l * m + m * n + l * n)

Sufficient upper bound on every constant, counter, memory address and intermediate product computed by verifyProgram.

theorem Cslib.Algorithms.WordRAM.Freivalds.inputFits_encode {w q k l m n : ℕ} [NeZero q] (A : Matrix (Fin l) (Fin m) (ZMod q)) (B : Matrix (Fin m) (Fin n) (ZMod q)) (C : Matrix (Fin l) (Fin n) (ZMod q)) (hw : WordRAM.Fits w (wordBound q k l m n)) :

Fitting wordBound q k l m n in w bits guarantees InputFits w (encode q k A B C).

def Cslib.Algorithms.WordRAM.Freivalds.MatrixAt {q rows cols : ℕ} (mem : ℕ → ℕ) (base : ℕ) (M : Matrix (Fin rows) (Fin cols) (ZMod q)) :

Matrix M is stored in row-major order starting at base.

theorem Cslib.Algorithms.WordRAM.Freivalds.matrixAt_of_holdsList {q rows cols : ℕ} {mem : ℕ → ℕ} {base : ℕ} {M : Matrix (Fin rows) (Fin cols) (ZMod q)} (h : WordRAM.Block.HoldsList base (encodeMatrix M) mem) :
MatrixAt mem base M

A Block.HoldsList view of encodeMatrix M gives MatrixAt.

def Cslib.Algorithms.WordRAM.Freivalds.VecAt {q len : ℕ} (mem : ℕ → ℕ) (base : ℕ) (v : Fin len → ZMod q) :

Vector v : Fin len → ZMod q is stored at base as canonical residues (v i).val.

def Cslib.Algorithms.WordRAM.Freivalds.readVec (q : ℕ) (mem : ℕ → ℕ) (base len : ℕ) :
Fin len → ZMod q

Read len words starting at base as a vector over ZMod q via natural-number coercion.

theorem Cslib.Algorithms.WordRAM.Freivalds.readVec_of_vecAt {q len : ℕ} [NeZero q] {mem : ℕ → ℕ} {base : ℕ} {v : Fin len → ZMod q} (h : VecAt mem base v) :
readVec q mem base len = v

Reading back a canonical residue slice stored under VecAt recovers the vector.

Registers and structured subroutines #

Register allocation for the 28-register Freivalds verifier. Public register names are those appearing in mulVecCode_spec and verifyCode_law; internal scratch registers are private.

@[reducible, inline]

Constant 0.

@[reducible, inline]

Constant 1.

@[reducible, inline]

Modulus q.

@[reducible, inline]

Running conjunction of trial outcomes (1 if all completed trials passed, 0 otherwise).

@[reducible, inline]

Subroutine matrix base pointer.

@[reducible, inline]

Subroutine input-vector base pointer.

@[reducible, inline]

Subroutine output-vector base pointer.

@[reducible, inline]

Subroutine row bound.

@[reducible, inline]

Subroutine column bound.

Deterministic modular matrix-vector multiplication: stores M *ᵥ readVec q mem vecPtr cols at [outPtr, outPtr + rows).

Complete structured Freivalds k-trial verifier.

Compiled flat randomized word-RAM program returning allOk (1 accept, 0 reject).

Worst-case instruction bound of verifyProgram on dimensions (k, l, m, n).

Worst-case instruction bound of verifyProgram on square n × n matrices (O(k n²)).

Proof of deterministic matrix-vector multiplication (mulVecCode) #

theorem Cslib.Algorithms.WordRAM.Freivalds.mulVecCode_spec {w q rows cols mBase vBase oBase : ℕ} [NeZero q] (M : Matrix (Fin rows) (Fin cols) (ZMod q)) (x : WordRAM.Data 28) (hz : x.regs Reg.zero = 0) (ho : x.regs Reg.one = 1) (hq : x.regs Reg.qReg = q) (hmPtr : x.regs Reg.matPtr = mBase) (hvPtr : x.regs Reg.vecPtr = vBase) (hoPtr : x.regs Reg.outPtr = oBase) (hrows : x.regs Reg.rowsReg = rows) (hcols : x.regs Reg.colsReg = cols) (hM : MatrixAt x.mem mBase M) (hdisj_m : mBase + rows * cols ≤ oBase ∨ oBase + rows ≤ mBase) (hdisj_v : vBase + cols ≤ oBase ∨ oBase + rows ≤ vBase) (hw_arith : WordRAM.Fits w (q * q + 2 * q)) (hw_m : WordRAM.Fits w (mBase + rows * cols)) (hw_v : WordRAM.Fits w (vBase + cols)) (hw_o : WordRAM.Fits w (oBase + rows)) :
∃ (y : WordRAM.Data 28), WordRAM.ExecWithin w mulVecCode x y (rows * (12 * cols + 10) + 4) ∧ VecAt y.mem oBase (M.mulVec (readVec q x.mem vBase cols)) ∧ (∀ (a : ℕ), ¬(oBase ≤ a ∧ a < oBase + rows) → y.mem a = x.mem a) ∧ ∀ (i : Fin 28), ↑i < 21 → y.regs i = x.regs i

Deterministic modular matrix-vector multiplication. When M : Matrix (Fin rows) (Fin cols) (ZMod q) is stored at matPtr and [outPtr, outPtr + rows) is disjoint from both M's slice and [vecPtr, vecPtr + cols), mulVecCode writes M *ᵥ readVec q x.mem vecPtr cols to [outPtr, outPtr + rows) within rows * (12 * cols + 10) + 4 instructions, preserving memory outside [outPtr, outPtr + rows) and all registers below 21.

Vector comparison (vecEqCode) #

Persistent frame and one-trial verification #

Randomized vector sampling and multi-trial law #

Initial setup and compiled end-to-end theorems #

theorem Cslib.Algorithms.WordRAM.Freivalds.verifyCode_law {w q k l m n : ℕ} [NeZero q] (A : Matrix (Fin l) (Fin m) (ZMod q)) (B : Matrix (Fin m) (Fin n) (ZMod q)) (C : Matrix (Fin l) (Fin n) (ZMod q)) (hw : WordRAM.Fits w (wordBound q k l m n)) :
WordRAM.ExecRandLaw w verifyCode (WordRAM.Data.init (encode q k A B C)) (fun (y : WordRAM.Data 28) => y.regs Reg.allOk = (decide (y.regs Reg.allOk = 1)).toNat) (k * (12 * (l * m + m * n + l * n) + 10 * m + 30 * l + 6 * n + 38) + 25) (k * n) (fun (y : WordRAM.Data 28) => decide (y.regs Reg.allOk = 1)) (Lean.Freivalds.verify Probability.bit k A B C).eval

Structured execution law of verifyCode from Data.init (encode q k A B C).

End-to-end randomized word-RAM specification of Freivalds' k-trial verifier. At any word width w with Fits w (wordBound q k l m n) and fuel T ≥ verifyTime k l m n:

  1. every execution path halts cleanly within T instructions (RandHaltsWithin);
  2. every path executes at most k * n rand calls (RandCallsBounded) and consumes at most k * n entropy bits (RandBitsBounded);
  3. the acceptance observation RandCfg.accepts has exact law ProbComp.eval (Cslib.Algorithms.Lean.Freivalds.verify bit k A B C).
theorem Cslib.Algorithms.WordRAM.Freivalds.verifyProgram_accept_of_mul_eq {w q k l m n T : ℕ} [NeZero q] {A : Matrix (Fin l) (Fin m) (ZMod q)} {B : Matrix (Fin m) (Fin n) (ZMod q)} {C : Matrix (Fin l) (Fin n) (ZMod q)} (h : A * B = C) (hw : WordRAM.Fits w (wordBound q k l m n)) (hT : verifyTime k l m n ≤ T) :

Perfect completeness: when A * B = C, verifyProgram accepts on every path (probability 1).

theorem Cslib.Algorithms.WordRAM.Freivalds.verifyProgram_accept_le_of_mul_ne {w q k l m n T : ℕ} [NeZero q] {A : Matrix (Fin l) (Fin m) (ZMod q)} {B : Matrix (Fin m) (Fin n) (ZMod q)} {C : Matrix (Fin l) (Fin n) (ZMod q)} (h : A * B ≠ C) (hw : WordRAM.Fits w (wordBound q k l m n)) (hT : verifyTime k l m n ≤ T) :

One-sided soundness: when A * B ≠ C, verifyProgram accepts with probability at most 2⁻¹ ^ k.

Square-matrix specialization (O(k n²) instructions): on n × n matrices over ZMod q, verifyProgram halts within verifySquareTime k n = k * (36 * n ^ 2 + 46 * n + 38) + 26 instructions, makes at most k * n rand calls (k * n entropy bits), accepts A * B = C with probability 1, and accepts A * B ≠ C with probability at most 2⁻¹ ^ k.