File size: 5,760 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
140
141
142
143
144
145
import OAI.Computability.DepthThree.Hash
import Mathlib.Data.Finset.Max
import Mathlib.Data.Fintype.EquivFin

namespace OAI

namespace DepthThreeLowerBound.BinaryHash

open scoped BigOperators

theorem exists_last_nonzero {d : ℕ} (w : Data d) (hw : w ≠ 0) :
    ∃ s : Fin d, w s ≠ 0 ∧ ∀ a : Fin d, s.val < a.val → w a = 0 := by
  classical
  have hex : ∃ a : Fin d, w a ≠ 0 := by
    by_contra h
    apply hw
    funext a
    by_contra ha
    exact h ⟨a, ha⟩
  obtain ⟨a, ha⟩ := hex
  obtain ⟨s, hs, hmax⟩ := Finset.exists_max_image
    (Finset.univ.filter fun a : Fin d => w a ≠ 0) Fin.val
      ⟨a, by simp [ha]⟩
  refine ⟨s, (Finset.mem_filter.mp hs).2, ?_⟩
  intro b hsb
  by_contra hb
  have hle := hmax b (by simp [hb])
  omega

noncomputable def lastIndex {d : ℕ} (w : Data d) (hw : w ≠ 0) : Fin d :=
  (exists_last_nonzero w hw).choose

theorem lastIndex_nonzero {d : ℕ} (w : Data d) (hw : w ≠ 0) :
    w (lastIndex w hw) ≠ 0 :=
  (exists_last_nonzero w hw).choose_spec.1

theorem zero_after_lastIndex {d : ℕ} (w : Data d) (hw : w ≠ 0)
    (a : Fin d) (ha : (lastIndex w hw).val < a.val) : w a = 0 :=
  (exists_last_nonzero w hw).choose_spec.2 a ha

theorem data_dim_pos {d : ℕ} (w : Data d) (hw : w ≠ 0) : 0 < d :=
  Nat.zero_le (lastIndex w hw).val |>.trans_lt (lastIndex w hw).isLt

def Nonpivot {d r : ℕ} (w : Data d) (hw : w ≠ 0) :=
  {j : Fin (d + r - 1) // ¬ ∃ i : Fin r, sumIndex i (lastIndex w hw) = j}

noncomputable instance {d r : ℕ} (w : Data d) (hw : w ≠ 0) :
    Fintype (Nonpivot (r := r) w hw) := by
  classical
  unfold Nonpivot
  infer_instance

theorem card_nonpivot {d r : ℕ} (w : Data d) (hw : w ≠ 0) :
    Fintype.card (Nonpivot (r := r) w hw) = d - 1 := by
  classical
  have hp : Fintype.card
      {j : Fin (d + r - 1) // ∃ i : Fin r, sumIndex i (lastIndex w hw) = j} = r := by
    let e : Fin r ≃
        {j : Fin (d + r - 1) // ∃ i : Fin r, sumIndex i (lastIndex w hw) = j} :=
      Equiv.ofInjective (fun i : Fin r => sumIndex i (lastIndex w hw))
        (sumIndex_left_injective (lastIndex w hw))
    simpa only [Fintype.card_fin] using (Fintype.card_congr e).symm
  calc
    Fintype.card (Nonpivot (r := r) w hw) = Fintype.card
        {j : Fin (d + r - 1) // ¬ ∃ i : Fin r, sumIndex i (lastIndex w hw) = j} :=
      Fintype.card_congr (Equiv.refl _)
    _ = (d + r - 1) - r := by
      rw [Fintype.card_subtype_compl, hp, Fintype.card_fin]
    _ = d - 1 := by
      have hd := data_dim_pos w hw
      omega

def triangularMap {d r : ℕ} (w : Data d) (hw : w ≠ 0) (u : Seed d r) :
    Output r × (Nonpivot (r := r) w hw → F2) :=
  (hashF2 u w, fun j => u j.val)

theorem triangularMap_injective {d r : ℕ} (w : Data d) (hw : w ≠ 0) :
    Function.Injective (triangularMap (r := r) w hw) := by
  classical
  intro u v huv
  have hhash : hashF2 u w = hashF2 v w := congrArg Prod.fst huv
  have hfree : (fun j : Nonpivot (r := r) w hw => u j.val) =
      (fun j : Nonpivot (r := r) w hw => v j.val) := congrArg Prod.snd huv
  have heq : ∀ n : ℕ, ∀ j : Fin (d + r - 1), j.val = n → u j = v j := by
    intro n
    induction n using Nat.strong_induction_on with
    | h n ih =>
      intro j hj
      by_cases hp : ∃ i : Fin r, sumIndex i (lastIndex w hw) = j
      · obtain ⟨i, rfl⟩ := hp
        have hrow : (∑ a : Fin d,
            (u (sumIndex i a) - v (sumIndex i a)) * w a) = 0 := by
          calc
            (∑ a : Fin d, (u (sumIndex i a) - v (sumIndex i a)) * w a) =
                hashF2 u w i - hashF2 v w i := by
                  simp only [hashF2, sub_mul, Finset.sum_sub_distrib]
            _ = 0 := sub_eq_zero.mpr (congrFun hhash i)
        have hsingle : (∑ a : Fin d,
            (u (sumIndex i a) - v (sumIndex i a)) * w a) =
            (u (sumIndex i (lastIndex w hw)) -
              v (sumIndex i (lastIndex w hw))) * w (lastIndex w hw) := by
          apply Finset.sum_eq_single_of_mem (lastIndex w hw) (Finset.mem_univ _)
          intro a _ ha
          by_cases hlt : a.val < (lastIndex w hw).val
          · have hidx : (sumIndex i a).val < n := by
              simp only [sumIndex_val] at hj ⊢
              omega
            rw [ih (sumIndex i a).val hidx (sumIndex i a) rfl, sub_self, zero_mul]
          · have hne : a.val ≠ (lastIndex w hw).val := by
              intro he
              exact ha (Fin.ext he)
            have hgt : (lastIndex w hw).val < a.val := by omega
            rw [zero_after_lastIndex w hw a hgt, mul_zero]
        rw [hsingle] at hrow
        exact sub_eq_zero.mp ((mul_eq_zero.mp hrow).resolve_right (lastIndex_nonzero w hw))
      · exact congrFun hfree ⟨j, hp⟩
  exact funext fun j => heq j.val j rfl

theorem triangularMap_bijective {d r : ℕ} (w : Data d) (hw : w ≠ 0) :
    Function.Bijective (triangularMap (r := r) w hw) := by
  classical
  apply (Fintype.bijective_iff_injective_and_card _).2
  refine ⟨triangularMap_injective w hw, ?_⟩
  have hd := data_dim_pos w hw
  simp only [Seed, Output, Fintype.card_prod, Fintype.card_fun,
    F2, BinaryAlgebra.F2, ZMod.card, Fintype.card_fin, card_nonpivot]
  rw [← pow_add]
  congr 1
  omega

noncomputable def triangularEquiv {d r : ℕ} (w : Data d) (hw : w ≠ 0) :
    Seed d r ≃ Output r × (Nonpivot (r := r) w hw → F2) :=
  Equiv.ofBijective (triangularMap w hw) (triangularMap_bijective w hw)

@[simp] theorem triangularEquiv_fst {d r : ℕ} (w : Data d) (hw : w ≠ 0)
    (u : Seed d r) : (triangularEquiv w hw u).1 = hashF2 u w := rfl

@[simp] theorem triangularEquiv_snd {d r : ℕ} (w : Data d) (hw : w ≠ 0)
    (u : Seed d r) (j : Nonpivot (r := r) w hw) :
    (triangularEquiv w hw u).2 j = u j.val := rfl

end DepthThreeLowerBound.BinaryHash

end OAI