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):
- the setup loads the header parameters and locates four disjoint scratch slices immediately
after the input for a length-
n0/1vectorxand three modular product vectorsy,z,w; - each of
kindependent trials samplesnuniform bits viarand one(Cslib.WordRAM.sampleBits), evaluatesy = B *ᵥ (bit ∘ x),z = A *ᵥ yandw = C *ᵥ (bit ∘ x)moduloqthrough a shared deterministic matrix-vector subroutine (mulVecCode), compareszwithw, and folds the Boolean result intoallOk; - the compiled program halts with
1when allktrials pass and0otherwise.
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:
- Worst-case instruction bound: every execution path halts cleanly within
verifyTime k l m n = k * (12 * (l * m + m * n + l * n) + 10 * m + 30 * l + 6 * n + 38) + 26instructions (verifySquareTime k n = k * (36 * n ^ 2 + 46 * n + 38) + 26 = O(k n²)for squaren × nmatrices); - Randomness budget: every path executes at most
k * nrandinstructions (rand one, consuming at mostk * nentropy bits underRandBitsBounded); - Exact distributional bridge: the halting acceptance bit
Cslib.WordRAM.RandCfg.acceptshas exact lawProbComp.eval (Cslib.Algorithms.Lean.Freivalds.verify bit k A B C), immediately transferring perfect completeness (verifyProgram_accept_of_mul_eq: probability1whenA * B = C) and one-sided soundness (verifyProgram_accept_le_of_mul_ne: error probability at most2⁻¹ ^ kwhenA * B ≠ C, over every modulusq ≥ 1).
Main definitions #
Cslib.Algorithms.WordRAM.Freivalds.encodeMatrix,Cslib.Algorithms.WordRAM.Freivalds.encode: row-major matrix and verifier input word layouts.Cslib.Algorithms.WordRAM.Freivalds.wordBound: sufficient word-width bound ensuring input and intermediate-value fit.Cslib.Algorithms.WordRAM.Freivalds.MatrixAt,Cslib.Algorithms.WordRAM.Freivalds.VecAt,Cslib.Algorithms.WordRAM.Freivalds.readVec: matrix and vector memory views overZMod q.Cslib.Algorithms.WordRAM.Freivalds.mulVecCode,verifyCode,verifyProgram: modular matrix-vector subroutine and structured/compiled randomized word-RAM verifier.Cslib.Algorithms.WordRAM.Freivalds.verifyTime,verifySquareTime: worst-case instruction bounds for rectangular and square instances.
Main results #
Cslib.Algorithms.WordRAM.Freivalds.matrixAt_of_holdsList,Cslib.Algorithms.WordRAM.Freivalds.readVec_of_vecAt: memory-view bridges forMatrixAtandVecAt.Cslib.Algorithms.WordRAM.Freivalds.mulVecCode_spec: deterministic modular matrix-vector multiplication contract onCode .standard.Cslib.Algorithms.WordRAM.Freivalds.verifyCode_law: structured randomized execution law ofverifyCode.Cslib.Algorithms.WordRAM.Freivalds.verifyProgram_spec: worst-case halting,k * nrand-call and entropy-bit bounds, and exact distributional equality withCslib.Algorithms.Lean.Freivalds.verify bit k A B C.Cslib.Algorithms.WordRAM.Freivalds.verifyProgram_accept_of_mul_eq: perfect completeness whenA * B = C.Cslib.Algorithms.WordRAM.Freivalds.verifyProgram_accept_le_of_mul_ne: acceptance probability at most2⁻¹ ^ kwhenA * B ≠ C.Cslib.Algorithms.WordRAM.Freivalds.verifyProgram_square_spec: specialization to squaren × nmatrices inverifySquareTime k n = O(k n²)instructions.
References #
Word layout and memory views #
Input word list for k trials of Freivalds' verifier modulo q on rectangular matrices
A : l × m, B : m × n and C : l × n.
Sufficient upper bound on every constant, counter, memory address and intermediate product
computed by verifyProgram.
Fitting wordBound q k l m n in w bits guarantees InputFits w (encode q k A B C).
A Block.HoldsList view of encodeMatrix M gives MatrixAt.
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.
Running conjunction of trial outcomes (1 if all completed trials passed, 0 otherwise).
Subroutine matrix base pointer.
Subroutine input-vector base pointer.
Subroutine output-vector base pointer.
Subroutine row bound.
Subroutine column bound.
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) #
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 #
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:
- every execution path halts cleanly within
Tinstructions (RandHaltsWithin); - every path executes at most
k * nrandcalls (RandCallsBounded) and consumes at mostk * nentropy bits (RandBitsBounded); - the acceptance observation
RandCfg.acceptshas exact lawProbComp.eval (Cslib.Algorithms.Lean.Freivalds.verify bit k A B C).
Perfect completeness: when A * B = C, verifyProgram accepts on every path (probability
1).
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.