File size: 9,982 Bytes
5d5048e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import OAI.Combinatorics.LowSpaceSubsetSum.Compression.CompressionStreams

namespace OAI

/-!
# The compressed trial at its designated solution residue

Both joins below use the concrete shared-array modular streams. Fuel comes
from products of stored-list lengths, so computing it does not enumerate a
Cartesian product. The ideal experiment still uses the proved uniform
permutation and prime law; bounded-sampler and watchdog losses are separate.
-/

namespace LowSpaceSubsetSum.CompressionSetup

variable {n : ℕ} (R : CompressionSetup n)

def streamLength (e : Equiv.Perm (Fin n)) (i : Fin 4) : ℕ :=
  (R.storedFirst e i).length * (R.storedSecond e i).length

theorem initial_output_length (e : Equiv.Perm (Fin n)) (q : PrimeSpace n)
    (i : Fin 4) (mode : FilteredStream.Mode) :
    (R.groupOutput e q i mode (R.groupStart e q i mode)).length = R.streamLength e i := by
  rw [R.groupOutput_length, groupList, pairSumList_length]
  rfl

def actualLeftDictionary (B : ℕ) (e : Equiv.Perm (Fin n)) (q : PrimeSpace n)
    (z : Fin q.val) : BinaryTrie.BuildResult :=
  (StreamJoin.run (R.groupRead e q 0 .direct) (fun x => x % (q.val : ℤ))
    (R.groupRead e q 1 (.complementary z.val)) (fun y => ((z.val : ℤ) - y) % q.val)
    (BinaryTrie.callback (compressionDictionaryCapacity n)
      (fun x y => WeightEncoding.weightKey R.input (x + y)))
    B (R.streamLength e 0) (R.streamLength e 1) (R.streamLength e 0 + R.streamLength e 1 + 1)
    (R.groupStart e q 0 .direct) (R.groupStart e q 1 (.complementary z.val))
    ⟨.empty, false, 0⟩).user

theorem actualLeftDictionary_fits (B : ℕ) (e : Equiv.Perm (Fin n)) (q : PrimeSpace n)
    (hq : R.BucketFits e q) :
    (R.actualLeftDictionary B e q (R.solutionResidue e q)).overflow = false ∧
      BinaryTrie.contains (R.actualLeftDictionary B e q (R.solutionResidue e q)).tree
        (WeightEncoding.weightKey R.input (R.leftWeight e)) = true := by
  apply R.generated_left_dictionary e q hq
    (R.groupRead e q 0 .direct) (R.groupRead e q 1 (.complementary (R.solutionResidue e q).val))
    (R.groupOutput e q 0 .direct) (R.groupRead_law e q 0 .direct)
    (R.groupOutput e q 1 (.complementary (R.solutionResidue e q).val))
    (R.groupRead_law e q 1 (.complementary (R.solutionResidue e q).val))
    (R.groupStart e q 0 .direct) (R.groupStart e q 1 (.complementary (R.solutionResidue e q).val))
    (R.groupOutput_mem e q 0 .direct) (R.groupOutput_mem e q 1 (.complementary _))
    B (R.streamLength e 0) (R.streamLength e 1) (R.streamLength e 0 + R.streamLength e 1 + 1)
  · exact R.groupOutput_sorted e q 0 .direct
  · exact R.groupOutput_sorted e q 1 (.complementary _)
  · exact (R.initial_output_length e q 0 .direct).le
  · exact (R.initial_output_length e q 1 (.complementary _)).le
  · rw [R.initial_output_length, R.initial_output_length]
    omega

def actualRightHit (B : ℕ) (e : Equiv.Perm (Fin n)) (q : PrimeSpace n)
    (z : Fin q.val) (tree : BinaryTrie.Tree) : Bool :=
  let target := (R.input.target : ℤ) - z.val
  (StreamJoin.run (R.groupRead e q 2 .direct) (fun x => x % (q.val : ℤ))
    (R.groupRead e q 3 (.complementary target)) (fun y => (target - y) % q.val)
    (StreamWitness.remember (fun x y => BinaryTrie.contains tree
      (WeightEncoding.weightKey R.input ((R.input.target : ℤ) - (x + y)))))
    B (R.streamLength e 2) (R.streamLength e 3) (R.streamLength e 2 + R.streamLength e 3 + 1)
    (R.groupStart e q 2 .direct) (R.groupStart e q 3 (.complementary target)) false).user

theorem actualRightHit_true (B : ℕ) (e : Equiv.Perm (Fin n)) (q : PrimeSpace n)
    (tree : BinaryTrie.Tree)
    (hleft : BinaryTrie.contains tree (WeightEncoding.weightKey R.input (R.leftWeight e)) = true) :
    R.actualRightHit B e q (R.solutionResidue e q) tree = true := by
  apply R.generated_right_hit e q tree hleft
    (R.groupRead e q 2 .direct)
    (R.groupRead e q 3 (.complementary ((R.input.target : ℤ) - (R.solutionResidue e q).val)))
    (R.groupOutput e q 2 .direct) (R.groupRead_law e q 2 .direct)
    (R.groupOutput e q 3 (.complementary ((R.input.target : ℤ) - (R.solutionResidue e q).val)))
    (R.groupRead_law e q 3 (.complementary _))
    (R.groupStart e q 2 .direct) (R.groupStart e q 3 (.complementary _))
    (R.groupOutput_mem e q 2 .direct) (R.groupOutput_mem e q 3 (.complementary _))
    B (R.streamLength e 2) (R.streamLength e 3) (R.streamLength e 2 + R.streamLength e 3 + 1)
  · exact R.groupOutput_sorted e q 2 .direct
  · exact R.groupOutput_sorted e q 3 (.complementary _)
  · exact (R.initial_output_length e q 2 .direct).le
  · exact (R.initial_output_length e q 3 (.complementary _)).le
  · rw [R.initial_output_length, R.initial_output_length]
    omega

/-- Integer division computes the rational floor used by the size test. -/
def executableCapExponent (n : ℕ) : ℕ :=
  (((400 : ℤ) * ((mixerSize n / 2 : ℕ) : ℤ) - 3 * n) / 400).toNat

theorem executableCapExponent_eq (n : ℕ) :
    executableCapExponent n = compressionCapExponent n := by
  unfold executableCapExponent compressionCapExponent
  rw [← Int.floor_toNat]
  congr 1
  have heq : ((mixerSize n / 2 : ℕ) : ℝ) - (3 / 400 : ℝ) * n =
      (((400 : ℤ) * ((mixerSize n / 2 : ℕ) : ℤ) - 3 * n : ℤ) : ℝ) / (400 : ℕ) := by
    simp only [Int.cast_sub, Int.cast_mul, Int.cast_natCast, Int.cast_ofNat, Nat.cast_ofNat]
    ring
  rw [heq, Int.floor_div_natCast, Int.floor_intCast]
  rfl

def compactTests (e : Equiv.Perm (Fin n)) : Bool :=
  decide (∀ i : Fin 7, (compactWeightList R.weights (R.block e i)).length ≤
    2 ^ executableCapExponent n)

theorem compactTests_iff (e : Equiv.Perm (Fin n)) :
    R.compactTests e = true ↔ R.Passing e := by
  simp only [compactTests, decide_eq_true_eq, compactWeightList_length,
    Passing, distinctSumCount, blockWeights, executableCapExponent_eq]

/-- One explicitly selected residue pass, using concrete streams and tries. -/
def residuePass (B : ℕ) (e : Equiv.Perm (Fin n)) (q : PrimeSpace n)
    (z : Fin q.val) : Bool :=
  if R.compactTests e then
    let dictionary := R.actualLeftDictionary B e q z
    if dictionary.overflow then false else R.actualRightHit B e q z dictionary.tree
  else false

/-- The fixed solution chooses this residue only in the success analysis. -/
def designatedPass (B : ℕ) (e : Equiv.Perm (Fin n)) (q : PrimeSpace n) : Bool :=
  R.residuePass B e q (R.solutionResidue e q)

theorem designatedPass_true (B : ℕ) (e : Equiv.Perm (Fin n)) (q : PrimeSpace n)
    (he : R.Passing e) (hq : R.BucketFits e q) : R.designatedPass B e q = true := by
  have ht := (R.compactTests_iff e).mpr he
  have hd := R.actualLeftDictionary_fits B e q hq
  simp only [designatedPass, residuePass, ht, ↓reduceIte, hd.1, Bool.false_eq_true]
  exact R.actualRightHit_true B e q _ hd.2

theorem designatedPass_probability (B : ℕ) (hn : 168000000 ≤ n)
    (hmean : (153 / 20000 : ℝ) * n ≤
      meanDeficiency R.weights Finset.univ (compressionHalfSize n))
    (hlog : Real.logb 2 (inputScale R.input) ≤ (n : ℝ) / 1000000000) :
    (1 / 4 : ℝ) ≤ (R.trialLaw hn).probability (fun h => R.designatedPass B h.1 h.2 = true) := by
  apply (R.successful_probability hn hmean hlog).trans
  apply FiniteLaw.probability_mono
  intro h hh
  exact R.designatedPass_true B h.1 h.2 hh.1 hh.2

/-- Tail-recursive, constant-state enumeration of a consecutive residue range. -/
def residueLoop (test : ℕ → Bool) : ℕ → ℕ → Bool
  | 0, _ => false
  | fuel + 1, start => if test start then true else residueLoop test fuel (start + 1)

theorem residueLoop_true_iff (test : ℕ → Bool) (fuel start : ℕ) :
    residueLoop test fuel start = true ↔ ∃ j < fuel, test (start + j) = true := by
  induction fuel generalizing start with
  | zero => simp [residueLoop]
  | succ fuel ih =>
    rw [residueLoop]
    by_cases ht : test start = true
    · simp only [ht, ↓reduceIte, true_iff]
      exact ⟨0, Nat.zero_lt_succ _, by simpa using ht⟩
    · simp only [ht, Bool.false_eq_true, ↓reduceIte, ih]
      constructor
      · rintro ⟨j, hj, hv⟩
        exact ⟨j + 1, by omega, by simpa [Nat.add_assoc, Nat.add_comm, Nat.add_left_comm] using hv⟩
      · rintro ⟨j, hj, hv⟩
        have hj0 : 0 < j := by
          by_contra h
          have : j = 0 := by omega
          simp_all
        refine ⟨j - 1, by omega, ?_⟩
        convert hv using 1
        congr 1
        omega

/-- The actual compression trial scans every residue; it never uses the solution. -/
def compressionTrial (B : ℕ) (e : Equiv.Perm (Fin n)) (q : PrimeSpace n) : Bool :=
  residueLoop (fun z => if h : z < q.val then R.residuePass B e q ⟨z, h⟩ else false)
    q.val 0

theorem compressionTrial_true_iff (B : ℕ) (e : Equiv.Perm (Fin n)) (q : PrimeSpace n) :
    R.compressionTrial B e q = true ↔ ∃ z : Fin q.val, R.residuePass B e q z = true := by
  rw [compressionTrial, residueLoop_true_iff]
  simp only [Nat.zero_add]
  constructor
  · rintro ⟨z, hz, hv⟩
    exact ⟨⟨z, hz⟩, by simpa only [dite_eq_left hz] using hv⟩
  · rintro ⟨z, hz⟩
    exact ⟨z.val, z.isLt, by simpa only [dite_eq_left z.isLt] using hz⟩

theorem compressionTrial_true (B : ℕ) (e : Equiv.Perm (Fin n)) (q : PrimeSpace n)
    (he : R.Passing e) (hq : R.BucketFits e q) : R.compressionTrial B e q = true :=
  (R.compressionTrial_true_iff B e q).mpr
    ⟨R.solutionResidue e q, R.designatedPass_true B e q he hq⟩

theorem compressionTrial_probability (B : ℕ) (hn : 168000000 ≤ n)
    (hmean : (153 / 20000 : ℝ) * n ≤
      meanDeficiency R.weights Finset.univ (compressionHalfSize n))
    (hlog : Real.logb 2 (inputScale R.input) ≤ (n : ℝ) / 1000000000) :
    (1 / 4 : ℝ) ≤ (R.trialLaw hn).probability (fun h => R.compressionTrial B h.1 h.2 = true) := by
  apply (R.successful_probability hn hmean hlog).trans
  apply FiniteLaw.probability_mono
  intro h hh
  exact R.compressionTrial_true B h.1 h.2 hh.1 hh.2

end LowSpaceSubsetSum.CompressionSetup

end OAI