Problem: linearsys_gf2

Your task is to implement a Gaussian Elimination solver for linear systems over GF(2) and verify its correctness in Lean. Given a binary matrix A (m x n) and a binary vector b (length m), find a binary vector x (length n) such that Ax = b (modulo 2), or return None if no solution exists. Postconditions: If result is Some(x), then Ax = b (mod 2). If result is None, then no such x exists. Preconditions: Matrix and vector dimensions match. Elements are 0 or 1. Dimensions are small (<= 100) to ensure tractable verification. Implementation should use standard Gaussian elimination with row operations modulo 2.

import Mathlib

-- Precondition definitions
@[reducible, simp]
def solve_linear_system_gf2_precond (matrix : List (List Nat)) (b : List Nat) : Prop :=
  -- !benchmark @start precond
  matrix.length > 0 ∧
  (matrix.getD 0 []).length > 0 ∧
  matrix.length = b.length ∧
  matrix.length ≤ 100 ∧
  (matrix.getD 0 []).length ≤ 100 ∧
  (∀ row, row ∈ matrix → (∀ x, x ∈ row → x = 0 ∨ x = 1)) ∧
  (∀ x, x ∈ b → x = 0 ∨ x = 1)
  -- !benchmark @end precond

def is_solution (matrix : List (List Nat)) (b : List Nat) (x : List Nat) : Prop :=
  x.length = (matrix.getD 0 []).length ∧
  (∀ i, i < matrix.length →
    ( (List.range x.length).foldl (fun acc j => acc + (matrix.getD i []).getD j 0 * x.getD j 0) 0 ) % 2 = b.getD i 0)

-- !benchmark @start auxcode
-- !benchmark @end auxcode

-- Main function definitions
def solve_linear_system_gf2 (matrix : List (List Nat)) (b : List Nat) (h_precond : solve_linear_system_gf2_precond matrix b) : Option (List Nat) :=
  -- !benchmark @start code
  sorry
  -- !benchmark @end code

-- Postcondition definitions
@[reducible, simp]
def solve_linear_system_gf2_postcond (matrix : List (List Nat)) (b : List Nat) (result : Option (List Nat)) (h_precond : solve_linear_system_gf2_precond matrix b) : Prop :=
  -- !benchmark @start postcond
  match result with
  | some x => is_solution matrix b x
  | none => ∀ x, x.length = (matrix.getD 0 []).length → (∀ v, v ∈ x → v = 0 ∨ v = 1) → ¬ is_solution matrix b x
  -- !benchmark @end postcond

-- !benchmark @start lemma
-- !benchmark @end lemma

-- Proof content
theorem solve_linear_system_gf2_postcond_satisfied (matrix : List (List Nat)) (b : List Nat) (h_precond : solve_linear_system_gf2_precond matrix b) :
    solve_linear_system_gf2_postcond matrix b (solve_linear_system_gf2 matrix b h_precond) h_precond := by
  -- !benchmark @start proof
  sorry
  -- !benchmark @end proof