File size: 8,096 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
import OAI.Computability.DepthThree.HashBool
import OAI.Computability.DepthThree.FiniteProbabilityBounds
import OAI.Computability.DepthThree.RestrictionProbability
import Mathlib.Tactic.NormNum
import Mathlib.Tactic.Positivity

namespace OAI

universe uDepth1 uDepth2

noncomputable section

open scoped BigOperators Classical

namespace DepthThreeLowerBound

theorem finiteProb_eq_card_div {Ξ© : Type uDepth1} [Fintype Ξ©] (P : Ξ© β†’ Prop) :
    finiteProb P = (Fintype.card {Ο‰ // P Ο‰} : ℝ) / Fintype.card Ξ© := by
  simp [finiteProb, finiteAvg, Fintype.card_subtype, div_eq_mul_inv, mul_comm]

private theorem finiteProb_eq_card_div_decidable {Ξ© : Type uDepth2} [Fintype Ξ©]
    (P : Ξ© β†’ Prop) [DecidablePred P] :
    finiteProb P = (Fintype.card {Ο‰ // P Ο‰} : ℝ) / Fintype.card Ξ© := by
  rw [finiteProb_eq_card_div]
  exact congrArg (fun k : β„• => (k : ℝ) / Fintype.card Ξ©)
    (congrArg (@Fintype.card {Ο‰ // P Ο‰}) (Subsingleton.elim _ _))

namespace BinaryHash

variable {d r : β„•}

private theorem normalize_hash_count (hd : 0 < d) (c : ℝ) :
    c * (2 : ℝ) ^ (d - 1) / (2 : ℝ) ^ (d + r - 1) = c / (2 : ℝ) ^ r := by
  have he : d + r - 1 = (d - 1) + r := by omega
  rw [he, pow_add, mul_comm c]
  exact mul_div_mul_left c _ (pow_ne_zero _ (by norm_num))

theorem hashBool_fiber_probability (x : Fin d β†’ Bool)
    (hx : x β‰  fun _ => false) (a : Fin r β†’ Bool) :
    finiteProb (fun u : Fin (d + r - 1) β†’ Bool => hashBool u x = a) =
      ((2 : ℝ) ^ r)⁻¹ := by
  have hd : 0 < d := by
    by_contra h
    have hd0 : d = 0 := by omega
    subst d
    exact hx (Subsingleton.elim _ _)
  rw [finiteProb_eq_card_div_decidable
    (fun u : Fin (d + r - 1) β†’ Bool => hashBool u x = a), hashBool_fiber_card x hx]
  have hc : (Fintype.card (Fin (d + r - 1) β†’ Bool) : ℝ) =
      (2 : ℝ) ^ (d + r - 1) := by simp []
  rw [hc]
  simp only [Nat.cast_pow, Nat.cast_ofNat]
  simpa only [one_mul, one_div] using normalize_hash_count (r := r) hd 1

theorem hashBool_collision_probability (x y : Fin d β†’ Bool) (hxy : x β‰  y) :
    finiteProb (fun u : Fin (d + r - 1) β†’ Bool => hashBool u x = hashBool u y) =
      ((2 : ℝ) ^ r)⁻¹ := by
  have hd : 0 < d := by
    by_contra h
    have hd0 : d = 0 := by omega
    subst d
    exact hxy (Subsingleton.elim _ _)
  rw [finiteProb_eq_card_div_decidable
    (fun u : Fin (d + r - 1) β†’ Bool => hashBool u x = hashBool u y),
    hashBool_collision_card x y hxy]
  have hc : (Fintype.card (Fin (d + r - 1) β†’ Bool) : ℝ) =
      (2 : ℝ) ^ (d + r - 1) := by simp []
  rw [hc]
  simp only [Nat.cast_pow, Nat.cast_ofNat]
  simpa only [one_mul, one_div] using normalize_hash_count (r := r) hd 1

theorem hashBool_badSeed_probability_le (T : Finset (Fin d))
    (rho : {i : Fin d // i βˆ‰ T} β†’ Bool) :
    finiteProb (fun u : Fin (d + r - 1) β†’ Bool =>
      Β¬ Set.InjOn (hashBool u) (boolCoordinateCube T rho)) ≀
        ((2 : ℝ) ^ T.card - 1) / (2 : ℝ) ^ r := by
  by_cases hd0 : d = 0
  Β· subst d
    have hT : T = βˆ… := by
      ext i
      exact Fin.elim0 i
    have hinj (u : Fin (0 + r - 1) β†’ Bool) :
        Set.InjOn (hashBool u) (boolCoordinateCube T rho) := by
      intro x hx y hy hxy
      exact Subsingleton.elim _ _
    simp [finiteProb, hinj, hT]
  have hd : 0 < d := Nat.pos_of_ne_zero hd0
  have hcount := hashBool_badSeed_card_le (r := r) T rho
  have hcast :
      (Fintype.card {u : Fin (d + r - 1) β†’ Bool //
        Β¬ Set.InjOn (hashBool u) (boolCoordinateCube T rho)} : ℝ) ≀
      ((2 : β„•) ^ T.card - 1 : β„•) * (2 : ℝ) ^ (d - 1) := by
    exact_mod_cast hcount
  have hpow : 1 ≀ (2 : β„•) ^ T.card := by
    have hp : 0 < (2 : β„•) ^ T.card := pow_pos (by decide) _
    omega
  have hsub : (((2 : β„•) ^ T.card - 1 : β„•) : ℝ) =
      (2 : ℝ) ^ T.card - 1 := by
    rw [Nat.cast_sub hpow]
    simp
  have hc : (Fintype.card (Fin (d + r - 1) β†’ Bool) : ℝ) =
      (2 : ℝ) ^ (d + r - 1) := by simp []
  rw [hsub] at hcast
  rw [finiteProb_eq_card_div_decidable
    (fun u : Fin (d + r - 1) β†’ Bool =>
      Β¬ Set.InjOn (hashBool u) (boolCoordinateCube T rho)), hc]
  calc
    _ ≀ (((2 : ℝ) ^ T.card - 1) * (2 : ℝ) ^ (d - 1)) /
        (2 : ℝ) ^ (d + r - 1) :=
      div_le_div_of_nonneg_right hcast (by positivity)
    _ = _ := normalize_hash_count hd _

theorem hashBool_badSeed_probability_le_pow (T : Finset (Fin d))
    (rho : {i : Fin d // i βˆ‰ T} β†’ Bool) :
    finiteProb (fun u : Fin (d + r - 1) β†’ Bool =>
      Β¬ Set.InjOn (hashBool u) (boolCoordinateCube T rho)) ≀
        (2 : ℝ) ^ T.card / (2 : ℝ) ^ r := by
  exact (hashBool_badSeed_probability_le T rho).trans
    (div_le_div_of_nonneg_right (by linarith) (by positivity))

def restrictionFixed (Οƒ : Restriction (Fin d)) :
    {i : Fin d // i βˆ‰ liveSet Οƒ} β†’ Bool :=
  fun i => (Οƒ i.val).getD false

theorem fill_mem_boolCoordinateCube (Οƒ : Restriction (Fin d)) (z : Cube (Live Οƒ)) :
    fill Οƒ z ∈ boolCoordinateCube (liveSet Οƒ) (restrictionFixed Οƒ) := by
  intro i hi
  have hn : Οƒ i β‰  none := fun h => hi ((mem_liveSet Οƒ i).mpr h)
  simp [fill, hn, restrictionFixed]

theorem range_fill_eq_boolCoordinateCube (Οƒ : Restriction (Fin d)) :
    Set.range (fill Οƒ) = boolCoordinateCube (liveSet Οƒ) (restrictionFixed Οƒ) := by
  ext x
  constructor
  · rintro ⟨z, rfl⟩
    exact fill_mem_boolCoordinateCube Οƒ z
  Β· intro hx
    refine ⟨fun v => x v.val, ?_⟩
    funext i
    by_cases hi : Οƒ i = none
    Β· simp [fill, hi]
    Β· have hnot : i βˆ‰ liveSet Οƒ := fun h => hi ((mem_liveSet Οƒ i).mp h)
      simpa [fill, hi, restrictionFixed] using (hx i hnot).symm

theorem fill_injective (Οƒ : Restriction (Fin d)) : Function.Injective (fill Οƒ) := by
  intro z w h
  funext v
  simpa only [fill_live] using congrFun h v.val

theorem hashBool_fill_injective_iff (Οƒ : Restriction (Fin d))
    (u : Fin (d + r - 1) β†’ Bool) :
    Function.Injective (fun z : Cube (Live Οƒ) => hashBool u (fill Οƒ z)) ↔
      Set.InjOn (hashBool u) (boolCoordinateCube (liveSet Οƒ) (restrictionFixed Οƒ)) := by
  constructor
  Β· intro h x hx y hy hxy
    rw [← range_fill_eq_boolCoordinateCube] at hx hy
    obtain ⟨z, rfl⟩ := hx
    obtain ⟨w, rfl⟩ := hy
    exact congrArg (fill Οƒ) (h hxy)
  Β· intro h z w hzw
    apply fill_injective Οƒ
    exact h (fill_mem_boolCoordinateCube Οƒ z) (fill_mem_boolCoordinateCube Οƒ w) hzw

theorem card_liveSet (Οƒ : Restriction (Fin d)) :
    (liveSet Οƒ).card = Fintype.card (Live Οƒ) := by
  let e : Live Οƒ ≃ β†₯(liveSet Οƒ) :=
    Equiv.subtypeEquivRight (fun i => (mem_liveSet Οƒ i).symm)
  exact ((Fintype.card_congr e).trans (Fintype.card_coe (liveSet Οƒ))).symm

theorem hashBool_restriction_failure_le (Οƒ : Restriction (Fin d)) :
    finiteProb (fun u : Fin (d + r - 1) β†’ Bool =>
      Β¬ Function.Injective (fun z : Cube (Live Οƒ) => hashBool u (fill Οƒ z))) ≀
        ((2 : ℝ) ^ Fintype.card (Live Οƒ) - 1) / (2 : ℝ) ^ r := by
  have he : finiteProb (fun u : Fin (d + r - 1) β†’ Bool =>
      Β¬ Function.Injective (fun z : Cube (Live Οƒ) => hashBool u (fill Οƒ z))) =
      finiteProb (fun u : Fin (d + r - 1) β†’ Bool =>
        Β¬ Set.InjOn (hashBool u) (boolCoordinateCube (liveSet Οƒ) (restrictionFixed Οƒ))) := by
    apply finiteAvg_congr
    intro u
    by_cases h : Set.InjOn (hashBool u) (boolCoordinateCube (liveSet Οƒ) (restrictionFixed Οƒ))
    Β· have h' := (hashBool_fill_injective_iff Οƒ u).mpr h
      simp [h, h']
    Β· have h' := mt (hashBool_fill_injective_iff Οƒ u).mp h
      simp [h, h']
  rw [he]
  simpa only [card_liveSet] using
    hashBool_badSeed_probability_le (r := r) (liveSet Οƒ) (restrictionFixed Οƒ)

theorem hashBool_restriction_failure_le_pow (Οƒ : Restriction (Fin d)) :
    finiteProb (fun u : Fin (d + r - 1) β†’ Bool =>
      Β¬ Function.Injective (fun z : Cube (Live Οƒ) => hashBool u (fill Οƒ z))) ≀
        (2 : ℝ) ^ Fintype.card (Live Οƒ) / (2 : ℝ) ^ r := by
  exact (hashBool_restriction_failure_le Οƒ).trans
    (div_le_div_of_nonneg_right (by linarith) (by positivity))

end BinaryHash
end DepthThreeLowerBound

end

end OAI