File size: 13,038 Bytes
4a56cd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import OAI.Algorithms.SubexponentialSAT.LeanCompat
import OAI.Algorithms.SubexponentialSAT.Subexp.BlockSearchProgram
import OAI.Algorithms.SubexponentialSAT.Subexp.BlockSearchEncoding
import OAI.Algorithms.SubexponentialSAT.Subexp.BlockZeroProgram
import OAI.Algorithms.SubexponentialSAT.Subexp.MatrixWordEncoding
import OAI.Algorithms.SubexponentialSAT.Subexp.MatrixProgramSupport
import OAI.Algorithms.SubexponentialSAT.Subexp.DimensionProgram

namespace OAI.Algorithms

/-!
# Executable predicates for the framed basis-table search

Finite decoding extends the algebraic test to every bit payload. On actual
generated payloads it is exactly the four-zero-test classifier used by the
matrix callback. The proofs retain the generator's actual table, coefficient,
and split representations.
-/

namespace SubexponentialSAT.Subexp.BlockSearchPredicate

open BlockSearch BlockSearchEncoding
open SubexpTupleRefinement

def finiteTest {A : Type*} (values : List A) (encode : A β†’ List Bool)
    (predicate : A β†’ Bool) (bits : List Bool) : Bool :=
  values.any (fun x => decide (encode x = bits) && predicate x)

theorem finiteTest_encoded {A : Type*} (values : List A) (encode : A β†’ List Bool)
    (hinj : Function.Injective encode) (predicate : A β†’ Bool)
    (x : A) (hx : x ∈ values) :
    finiteTest values encode predicate (encode x) = predicate x := by
  apply Bool.eq_iff_iff.mpr
  simp only [finiteTest, List.any_eq_true, Bool.and_eq_true, decide_eq_true_eq]
  constructor
  · rintro ⟨y, hy, heq, hp⟩
    have hxy : y = x := hinj heq
    simpa only [hxy] using hp
  Β· intro hp
    exact ⟨x, hx, rfl, hp⟩

section Algebra

variable {K : Type*} [Field K] [DecidableEq K]
variable {m N : β„•}

def classify (M : Fin m β†’ Fin N β†’ K) (d : Fin (N + 1))
    (C : Fin N β†’ Fin N β†’ K) (x : Fin N β†’ K) : Bool :=
  BlockZeroProgram.classifyResult
    (decide (matrixMap C x = 0)) (decide (x = 0))
    (decide (tailCoefficients d x = 0)) (decide (matrixMap M (matrixMap C x) = 0))

theorem classify_eq_coefficientTest (M : Fin m β†’ Fin N β†’ K) (d : Fin (N + 1))
    (C : Fin N β†’ Fin N β†’ K) (x : Fin N β†’ K) :
    classify M d C x = BlockSearchProgram.coefficientTest M (splitPair d C)
      (headCoefficients d x) (tailCoefficients d x) := by
  have coefficientTest_eq (c : Candidate K N Γ— Candidate K N)
      (a : Fin c.1.1.val β†’ K) (b : Fin c.2.1.val β†’ K) :
      BlockSearchProgram.coefficientTest M c a b =
        BlockZeroProgram.classifyResult
          (decide (combine c.1 a + combine c.2 b = 0))
          (decide (a = 0 ∧ b = 0)) (decide (b = 0))
          (decide (matrixMap M (combine c.1 a + combine c.2 b) = 0)) := by
    unfold BlockSearchProgram.coefficientTest BlockZeroProgram.classifyResult
    by_cases hy : combine c.1 a + combine c.2 b = 0 <;>
      by_cases ha : a = 0 <;> by_cases hb : b = 0 <;>
      by_cases hm : matrixMap M (combine c.1 a + combine c.2 b) = 0 <;>
      simp [hy, ha, hb, hm]
  rw [coefficientTest_eq (splitPair d C) (headCoefficients d x)
    (tailCoefficients d x), ← matrixMap_eq_combine d C x]
  unfold classify
  have hcoeff : decide (headCoefficients d x = 0 ∧ tailCoefficients d x = 0) =
      decide (x = 0) := by
    apply Bool.eq_iff_iff.mpr
    simpa only [decide_eq_true_eq] using coefficients_zero_iff d x
  exact congrArg (fun zeroCoefficients =>
    BlockZeroProgram.classifyResult (decide (matrixMap C x = 0)) zeroCoefficients
      (decide (tailCoefficients d x = 0))
      (decide (matrixMap M (matrixMap C x) = 0))) hcoeff.symm

def payloadTest (code : StackEncoding.BlockCode K) (alphabet : List K)
    (M : Fin m β†’ Fin N β†’ K) (d : Fin (N + 1)) (C : Fin N β†’ Fin N β†’ K) :
    List Bool β†’ Bool :=
  finiteTest (QuarticSearch.vectors alphabet N) (encodeTuple code) (classify M d C)

theorem payloadTest_encoded (code : StackEncoding.BlockCode K) (alphabet : List K)
    (hfull : βˆ€ a, a ∈ alphabet) (M : Fin m β†’ Fin N β†’ K) (d : Fin (N + 1))
    (C : Fin N β†’ Fin N β†’ K) (x : Fin N β†’ K) :
    payloadTest code alphabet M d C (encodeTuple code x) =
      BlockSearchProgram.coefficientTest M (splitPair d C)
        (headCoefficients d x) (tailCoefficients d x) := by
  rw [payloadTest, finiteTest_encoded _ _ (encodeTuple_injective code N) _ x
    (QuarticSearch.mem_vectors alphabet hfull N x)]
  exact classify_eq_coefficientTest M d C x

omit [Field K] [DecidableEq K] in
theorem encodeTuple_split (code : StackEncoding.BlockCode K) (d : Fin (N + 1))
    (x : Fin N β†’ K) :
    encodeTuple code x = encodeTuple code (headCoefficients d x) ++
      encodeTuple code (tailCoefficients d x) := by
  have hN : d.val + (N - d.val) = N := by have := d.isLt; omega
  unfold encodeTuple
  rw [List.ofFn_congr hN.symm x, List.ofFn_add, StackEncoding.encodeDataStack_append]
  rfl

theorem payloadTest_pair (code : StackEncoding.BlockCode K) (alphabet : List K)
    (hfull : βˆ€ a, a ∈ alphabet) (M : Fin m β†’ Fin N β†’ K) (d : Fin (N + 1))
    (C : Fin N β†’ Fin N β†’ K) (a : Fin d.val β†’ K) (b : Fin (N - d.val) β†’ K) :
    payloadTest code alphabet M d C (encodeTuple code a ++ encodeTuple code b) =
      BlockSearchProgram.coefficientTest M (splitPair d C) a b := by
  have h := payloadTest_encoded code alphabet hfull M d C (joinCoefficients d a b)
  rw [encodeTuple_split code d, headCoefficients_join, tailCoefficients_join] at h
  exact h

theorem all_payloads_iff_goodPair (code : StackEncoding.BlockCode K) (alphabet : List K)
    (hfull : βˆ€ a, a ∈ alphabet) (M : Fin m β†’ Fin N β†’ K) (d : Fin (N + 1))
    (C : Fin N β†’ Fin N β†’ K) :
    (BlockSearchProgram.coefficientRecords code alphabet (splitPair d C)).all
      (payloadTest code alphabet M d C) = true ↔ GoodPair M (splitPair d C) := by
  apply BlockSearchProgram.coefficientRecords_all_iff_goodPair code alphabet hfull M
    (splitPair d C) (splitPair_dimensions_add d C)
  exact payloadTest_pair code alphabet hfull M d C

def splitCode (d : Fin (N + 1)) : List Bool := List.replicate d.val true

theorem splitCode_injective : Function.Injective (splitCode (N := N)) := by
  intro d e h
  apply Fin.ext
  have hl := congrArg List.length h
  simpa only [splitCode, List.length_replicate] using hl

def encodedTest (code : StackEncoding.BlockCode K) (alphabet : List K)
    (M : Fin m β†’ Fin N β†’ K) (tableBits splitBits coefficientBits : List Bool) : Bool :=
  finiteTest (QuarticSearch.vectors alphabet (N * N)) (encodeTuple code)
    (fun flat => finiteTest (List.finRange (N + 1)) splitCode
      (fun d => payloadTest code alphabet M d (unflattenTable flat) coefficientBits) splitBits)
    tableBits

theorem encodedTest_encoded (code : StackEncoding.BlockCode K) (alphabet : List K)
    (hfull : βˆ€ a, a ∈ alphabet) (M : Fin m β†’ Fin N β†’ K) (d : Fin (N + 1))
    (C : Fin N β†’ Fin N β†’ K) (bits : List Bool) :
    encodedTest code alphabet M (encodeTuple code (flattenTable C)) (splitCode d) bits =
      payloadTest code alphabet M d C bits := by
  unfold encodedTest
  rw [finiteTest_encoded _ _ (encodeTuple_injective code (N * N)) _ (flattenTable C)
    (QuarticSearch.mem_vectors alphabet hfull (N * N) (flattenTable C))]
  rw [finiteTest_encoded _ _ splitCode_injective _ d (by simp)]
  simp only [unflattenTable_flattenTable]

theorem encodedTest_coefficient (code : StackEncoding.BlockCode K) (alphabet : List K)
    (hfull : βˆ€ a, a ∈ alphabet) (M : Fin m β†’ Fin N β†’ K) (d : Fin (N + 1))
    (C : Fin N β†’ Fin N β†’ K) (x : Fin N β†’ K) :
    encodedTest code alphabet M (encodeTuple code (flattenTable C)) (splitCode d)
      (encodeTuple code x) = BlockSearchProgram.coefficientTest M (splitPair d C)
        (headCoefficients d x) (tailCoefficients d x) := by
  rw [encodedTest_encoded code alphabet hfull, payloadTest_encoded code alphabet hfull]

theorem all_encoded_iff_goodPair (code : StackEncoding.BlockCode K) (alphabet : List K)
    (hfull : βˆ€ a, a ∈ alphabet) (M : Fin m β†’ Fin N β†’ K) (d : Fin (N + 1))
    (C : Fin N β†’ Fin N β†’ K) :
    (BlockSearchProgram.coefficientRecords code alphabet (splitPair d C)).all
      (encodedTest code alphabet M (encodeTuple code (flattenTable C)) (splitCode d)) = true ↔
        GoodPair M (splitPair d C) := by
  have heq : encodedTest code alphabet M (encodeTuple code (flattenTable C)) (splitCode d) =
      payloadTest code alphabet M d C := by
    funext bits
    exact encodedTest_encoded code alphabet hfull M d C bits
  rw [heq]
  exact all_payloads_iff_goodPair code alphabet hfull M d C

theorem exists_encoded_success (code : StackEncoding.BlockCode K) (alphabet : List K)
    (hfull : βˆ€ a, a ∈ alphabet) (M : Fin m β†’ Fin N β†’ K) :
    βˆƒ (d : Fin (N + 1)) (C : Fin N β†’ Fin N β†’ K),
      encodeTuple code (flattenTable C) ∈
        TupleProgram.powerRows (alphabet.map code.block) (N * N) [[]] ∧
      splitCode d ∈ DimensionProgram.dimensionRows 0 N ∧
      (BlockSearchProgram.coefficientRecords code alphabet (splitPair d C)).all
        (encodedTest code alphabet M (encodeTuple code (flattenTable C)) (splitCode d)) = true := by
  obtain ⟨d, C, hgood⟩ := exists_good_splitPair M
  refine ⟨d, C, encodeTuple_mem_powerRows code alphabet hfull (N * N) (flattenTable C), ?_, ?_⟩
  · exact (DimensionProgram.mem_dimensionRows 0 N d.val).mpr ⟨Nat.zero_le _, by have := d.isLt; omega⟩
  Β· exact (all_encoded_iff_goodPair code alphabet hfull M d C).mpr hgood

omit [Field K] [DecidableEq K] in
theorem encodeFramed_list (code : StackEncoding.BlockCode K) (xs : List K) :
    StackEncoding.encodeDataStack (BlockSearchProgram.framedCode code) xs =
      MatrixProgramSupport.words (xs.map code.block) := by
  induction xs with
  | nil => rfl
  | cons x xs ih =>
      change Encoding.frame (code.block x) ++
        StackEncoding.encodeDataStack (BlockSearchProgram.framedCode code) xs =
          Encoding.frame (code.block x) ++ MatrixProgramSupport.words (xs.map code.block)
      rw [ih]

end Algebra

section Prime

variable (p : β„•) [Fact p.Prime]

def scalarCode : StackEncoding.BlockCode (ZMod p) :=
  BlockSearchProgram.framedCode (MatrixWordEncoding.code p)

theorem encoded_vector_eq_words {N : β„•} (x : Fin N β†’ ZMod p) :
    encodeTuple (scalarCode p) x =
      MatrixProgramSupport.words (List.ofFn fun i => MatrixWordEncoding.word p (x i)) := by
  rw [encodeTuple, scalarCode, encodeFramed_list, List.map_ofFn]
  rfl

theorem encoded_table_eq_flatten_words {N : β„•} (C : Fin N β†’ Fin N β†’ ZMod p) :
    encodeTuple (scalarCode p) (flattenTable C) =
      MatrixProgramSupport.words
        ((List.ofFn fun row => List.ofFn fun col => MatrixWordEncoding.word p (C row col)).flatten) := by
  rw [encoded_vector_eq_words]
  apply congrArg MatrixProgramSupport.words
  change tableEntries (fun row col => MatrixWordEncoding.word p (C row col)) = _
  exact tableEntries_eq_flatten_rows (fun row col => MatrixWordEncoding.word p (C row col))

theorem words_split {N : β„•} (d : Fin (N + 1)) (x : Fin N β†’ ZMod p) :
    MatrixProgramSupport.words (List.ofFn fun i => MatrixWordEncoding.word p (x i)) =
      MatrixProgramSupport.words
        (List.ofFn fun i => MatrixWordEncoding.word p (headCoefficients d x i)) ++
      MatrixProgramSupport.words
        (List.ofFn fun i => MatrixWordEncoding.word p (tailCoefficients d x i)) := by
  rw [← encoded_vector_eq_words p x, encodeTuple_split (scalarCode p) d x,
    encoded_vector_eq_words p (headCoefficients d x),
    encoded_vector_eq_words p (tailCoefficients d x)]

def test (alphabet : List (ZMod p)) {m N : β„•} (M : Fin m β†’ Fin N β†’ ZMod p) :
    List Bool β†’ List Bool β†’ List Bool β†’ Bool := encodedTest (scalarCode p) alphabet M

theorem test_on_words (alphabet : List (ZMod p)) (hfull : βˆ€ a, a ∈ alphabet)
    {m N : β„•} (M : Fin m β†’ Fin N β†’ ZMod p) (d : Fin (N + 1))
    (C : Fin N β†’ Fin N β†’ ZMod p) (x : Fin N β†’ ZMod p) :
    test p alphabet M (encodeTuple (scalarCode p) (flattenTable C)) (splitCode d)
      (MatrixProgramSupport.words (List.ofFn fun i => MatrixWordEncoding.word p (x i))) =
      BlockSearchProgram.coefficientTest M (splitPair d C)
        (headCoefficients d x) (tailCoefficients d x) := by
  rw [← encoded_vector_eq_words p x]
  exact encodedTest_coefficient (scalarCode p) alphabet hfull M d C x

theorem test_on_flattened_rows (alphabet : List (ZMod p)) (hfull : βˆ€ a, a ∈ alphabet)
    {m N : β„•} (M : Fin m β†’ Fin N β†’ ZMod p) (d : Fin (N + 1))
    (C : Fin N β†’ Fin N β†’ ZMod p) (x : Fin N β†’ ZMod p) :
    test p alphabet M
      (MatrixProgramSupport.words
        ((List.ofFn fun row => List.ofFn fun col => MatrixWordEncoding.word p (C row col)).flatten))
      (splitCode d)
      (MatrixProgramSupport.words (List.ofFn fun i => MatrixWordEncoding.word p (x i))) =
      BlockSearchProgram.coefficientTest M (splitPair d C)
        (headCoefficients d x) (tailCoefficients d x) := by
  rw [← encoded_table_eq_flatten_words p C]
  exact test_on_words p alphabet hfull M d C x

end Prime

end SubexponentialSAT.Subexp.BlockSearchPredicate

end OAI.Algorithms