math / data /lean /OAI /Computability /DepthThree /HashProbability.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
8.1 kB
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