Documentation

CslibFrontier.Algorithms.Lean.Freivalds

Freivalds' Matrix-Product Verification #

Freivalds' algorithm ([Freivalds1977], [Freivalds1979]; [MitzenmacherUpfal2017], Section 1.3; [MotwaniRaghavan95], Section 7.1) checks a claimed product A * B = C without multiplying matrices. It draws random vectors x and accepts when A *ᵥ (B *ᵥ x) = C *ᵥ x for every drawn vector, evaluating three matrix-vector products and one vector comparison per trial.

This module defines the single-vector check check and the randomized verifier verify as a ProbComp program that samples k vectors uniformly and tests check on each. Entries of x are drawn from a finite set S and encoded in the ring by e : S → R. The soundness analysis needs only that every nonzero d makes s ↦ d * e s injective (see Cslib.Probability.card_filter_mulVec_eq_mul_card_le). A machine-level implementation on the randomized word RAM with explicit instruction and rand-call bounds lives in CslibFrontier.Algorithms.WordRAM.Freivalds.

Source Conventions #

Main definitions #

Main results #

References #

def Cslib.Algorithms.Lean.Freivalds.check {R : Type} [Ring R] [DecidableEq R] {l m n : ℕ} (A : Matrix (Fin l) (Fin m) R) (B : Matrix (Fin m) (Fin n) R) (C : Matrix (Fin l) (Fin n) R) (x : Fin n → R) :

One deterministic Freivalds check: compute B *ᵥ x, A *ᵥ (B *ᵥ x) and C *ᵥ x, and compare A *ᵥ (B *ᵥ x) with C *ᵥ x.

@[simp]
theorem Cslib.Algorithms.Lean.Freivalds.check_eq_decide {R : Type} [Ring R] [DecidableEq R] {l m n : ℕ} (A : Matrix (Fin l) (Fin m) R) (B : Matrix (Fin m) (Fin n) R) (C : Matrix (Fin l) (Fin n) R) (x : Fin n → R) :
check A B C x = decide ((A * B).mulVec x = C.mulVec x)

By associativity of matrix-vector multiplication, check tests (A * B) *ᵥ x = C *ᵥ x.

noncomputable def Cslib.Algorithms.Lean.Freivalds.verify {R : Type} [Ring R] [DecidableEq R] {l m n : ℕ} {S : Type} [Fintype S] [Nonempty S] (e : S → R) (k : ℕ) (A : Matrix (Fin l) (Fin m) R) (B : Matrix (Fin m) (Fin n) R) (C : Matrix (Fin l) (Fin n) R) :

Freivalds' randomized verifier: draw k independent uniform vectors in Fin n → S, encode their entries with e, and accept when every vector passes check A B C.

theorem Cslib.Algorithms.Lean.Freivalds.eval_verify {R : Type} [Ring R] [DecidableEq R] {l m n : ℕ} {S : Type} [Fintype S] [Nonempty S] (e : S → R) (k : ℕ) (A : Matrix (Fin l) (Fin m) R) (B : Matrix (Fin m) (Fin n) R) (C : Matrix (Fin l) (Fin n) R) :
(verify e k A B C).eval = PMF.map (fun (xs : Fin k → Fin n → S) => decide (∀ (t : Fin k), (A * B).mulVec (e ∘ xs t) = C.mulVec (e ∘ xs t))) (Probability.PMF.uniformOfFintype (Fin k → Fin n → S))

The law of the verifier: the indicator that all k checks pass, pushed forward from the uniform law on Fin k → Fin n → S.

theorem Cslib.Algorithms.Lean.Freivalds.eval_verify_of_mul_eq {R : Type} [Ring R] [DecidableEq R] {l m n : ℕ} {S : Type} [Fintype S] [Nonempty S] (e : S → R) (k : ℕ) {A : Matrix (Fin l) (Fin m) R} {B : Matrix (Fin m) (Fin n) R} {C : Matrix (Fin l) (Fin n) R} (h : A * B = C) :
(verify e k A B C).eval = PMF.pure true

Perfect completeness: a correct product is always accepted.

theorem Cslib.Algorithms.Lean.Freivalds.eval_verify_true_le {R : Type} [Ring R] [DecidableEq R] {l m n : ℕ} {S : Type} [Fintype S] [Nonempty S] {e : S → R} (he : ∀ (d : R), d ≠ 0 → Function.Injective fun (s : S) => d * e s) (k : ℕ) {A : Matrix (Fin l) (Fin m) R} {B : Matrix (Fin m) (Fin n) R} {C : Matrix (Fin l) (Fin n) R} (h : A * B ≠ C) :
(verify e k A B C).eval true ≤ (↑(Fintype.card S))⁻¹ ^ k

Soundness: if A * B ≠ C, the verifier accepts with probability at most (1/|S|)^k.

theorem Cslib.Algorithms.Lean.Freivalds.eval_verify_bool_true_le {R : Type} [Ring R] [DecidableEq R] {l m n : ℕ} (k : ℕ) {A : Matrix (Fin l) (Fin m) R} {B : Matrix (Fin m) (Fin n) R} {C : Matrix (Fin l) (Fin n) R} (h : A * B ≠ C) :

Freivalds' verifier with 0/1 vectors accepts an incorrect product with probability at most (1/2)^k. The case k = 1 is [MitzenmacherUpfal2017], Theorem 1.4.

theorem Cslib.Algorithms.Lean.Freivalds.eval_verify_field_true_le {l m n : ℕ} {F : Type} [Field F] [Fintype F] [DecidableEq F] (k : ℕ) {A : Matrix (Fin l) (Fin m) F} {B : Matrix (Fin m) (Fin n) F} {C : Matrix (Fin l) (Fin n) F} (h : A * B ≠ C) :
(verify id k A B C).eval true ≤ (↑(Fintype.card F))⁻¹ ^ k

Over a finite field with uniform vectors, Freivalds' verifier accepts an incorrect product with probability at most (1/|F|)^k.