File size: 5,154 Bytes
9dbc64e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import OAI.Computability.DepthThree.LanguageBitArithmetic
import OAI.Computability.DepthThree.Language
import Mathlib.Tactic.Ring

namespace OAI

namespace DepthThreeLowerBound

open scoped BigOperators
open BinaryAlgebra
open Polynomial

theorem mul_remainder_modByMonic (a b p : Polynomial (ZMod 2)) (hp : p.Monic) :
    (a * (b %ₘ p)) %ₘ p = (a * b) %ₘ p := by
  symm
  calc
    (a * b) %ₘ p = (a * (b %ₘ p + p * (b /ₘ p))) %ₘ p := by
      rw [Polynomial.modByMonic_add_div b p]
    _ = (a * (b %ₘ p)) %ₘ p := by
      rw [mul_add, Polynomial.add_modByMonic]
      have hz : (a * (p * (b /ₘ p))) %ₘ p = 0 := by
        rw [← mul_assoc, mul_comm a p, mul_assoc,
          Polynomial.self_mul_modByMonic hp]
      rw [hz, add_zero]

theorem wordPoly_mod_self {r : ℕ} (p a : BitWord r) :
    wordPoly a %ₘ inputPolynomialBits p = wordPoly a := by
  apply (Polynomial.modByMonic_eq_self_iff (inputPolynomialBits_monic p)).2
  rw [inputPolynomialBits_degree]
  exact degree_wordPoly_lt a

def wordHorner {r : ℕ} (p h : BitWord r) :
    (t : ℕ) → (Fin t → BitWord r) → BitWord r
  | 0, _ => fun _ => false
  | t + 1, b => wordFreeze (wordXor (b 0)
      (wordMulMod p h (wordHorner p h t (fun i => b i.succ))))

theorem wordPoly_horner {r : ℕ} (p h : BitWord r) (t : ℕ)
    (b : Fin t → BitWord r) :
    wordPoly (wordHorner p h t b) =
      (∑ j : Fin t, wordPoly (b j) * wordPoly h ^ j.val) %ₘ
        inputPolynomialBits p := by
  induction t with
  | zero => simp [wordHorner]
  | succ t ih =>
    have hsum :
        (∑ j : Fin (t + 1), wordPoly (b j) * wordPoly h ^ j.val) =
          wordPoly (b 0) + wordPoly h *
            ∑ j : Fin t, wordPoly (b j.succ) * wordPoly h ^ j.val := by
      rw [Fin.sum_univ_succ]
      simp only [Fin.val_zero, pow_zero, mul_one, Fin.val_succ, pow_succ]
      rw [Finset.mul_sum]
      apply congrArg (fun z : Polynomial (ZMod 2) => wordPoly (b 0) + z)
      apply Finset.sum_congr rfl
      intro j _
      ring
    rw [wordHorner, wordFreeze_eq, wordPoly_xor, wordPoly_mulMod,
      ih, mul_remainder_modByMonic _ _ _ (inputPolynomialBits_monic p),
      hsum, Polynomial.add_modByMonic, wordPoly_mod_self]

def wordHash {d r : ℕ} (u : BitWord (d + r - 1)) (x : BitWord d) : BitWord r :=
  wordFreeze fun i =>
    xorBits (List.ofFn fun s : Fin d => u (BinaryHash.sumIndex i s) && x s)

theorem wordHash_eq {d r : ℕ} (u : BitWord (d + r - 1)) (x : BitWord d) :
    wordHash u x = BinaryHash.hashBool u x := by
  funext i
  apply bitValue_injective
  simp only [wordHash, wordFreeze_eq, bitValue_xorBits_ofFn, bitValue_and,
    BinaryHash.bitValue_hashBool, BinaryHash.hashF2]

def multiplyBitLists (r : ℕ) (p a b : List Bool) : List Bool :=
  wordList (wordMulMod (listWord r p) (listWord r a) (listWord r b))

@[simp] theorem multiplyBitLists_length (r : ℕ) (p a b : List Bool) :
    (multiplyBitLists r p a b).length = r := by
  simp [multiplyBitLists]

theorem multiplyBitLists_correct (r : ℕ) (p a b : List Bool) :
    wordPoly (listWord r (multiplyBitLists r p a b)) =
      (wordPoly (listWord r a) * wordPoly (listWord r b)) %ₘ
        inputPolynomialBits (listWord r p) := by
  simp only [multiplyBitLists, listWord_wordList, wordPoly_mulMod]

def hornerBitLists (r : ℕ) (p h : List Bool) (b : List (List Bool)) : List Bool :=
  wordList (wordHorner (listWord r p) (listWord r h) b.length
    (fun j => listWord r (b.getD j.val [])))

@[simp] theorem hornerBitLists_length (r : ℕ) (p h : List Bool)
    (b : List (List Bool)) : (hornerBitLists r p h b).length = r := by
  simp [hornerBitLists]

theorem hornerBitLists_correct (r : ℕ) (p h : List Bool) (b : List (List Bool)) :
    wordPoly (listWord r (hornerBitLists r p h b)) =
      (∑ j : Fin b.length, wordPoly (listWord r (b.getD j.val [])) *
        wordPoly (listWord r h) ^ j.val) %ₘ inputPolynomialBits (listWord r p) := by
  simp only [hornerBitLists, listWord_wordList, wordPoly_horner]

namespace ParsedInput

def evaluationBits (q : ParsedInput) : BitWord q.ringDegree :=
  wordHorner q.polynomial (wordHash q.hashSeed q.data)
    q.coefficientCount q.coefficients

theorem wordPoly_evaluationBits (q : ParsedInput) :
    wordPoly q.evaluationBits = q.evaluationRemainder := by
  rw [evaluationBits, wordPoly_horner, wordHash_eq]
  rfl

def evaluateBits (q : ParsedInput) : Bool :=
  !(wordGet q.evaluationBits 0)

theorem evaluateBits_eq (q : ParsedInput) : q.evaluateBits = q.evaluate := by
  have hc : bitValue (wordGet q.evaluationBits 0) =
      q.evaluationRemainder.coeff 0 := by
    rw [← coeff_wordPoly, wordPoly_evaluationBits]
  apply Bool.eq_iff_iff.mpr
  rw [evaluate_eq_true_iff]
  unfold evaluateBits
  rw [← hc, bitValue_eq_zero_iff]
  cases wordGet q.evaluationBits 0 <;> decide

end ParsedInput

def languageBits (w : List Bool) : Bool :=
  match parseInput w with
  | none => false
  | some q => q.evaluateBits

theorem languageBits_eq_language (w : List Bool) : languageBits w = language w := by
  unfold languageBits language
  cases parseInput w with
  | none => rfl
  | some q => exact q.evaluateBits_eq

end DepthThreeLowerBound

end OAI