File size: 3,653 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
import OAI.Computability.DepthThree.LanguageBitLoopInvariants

namespace OAI

universe uDepth1

namespace DepthThreeLowerBound

def hornerStep (r : ℕ) (p h b a : List Bool) : List Bool :=
  List.zipWith Bool.xor b (multiplyBitLists r p h a)

theorem hornerStep_length (r : ℕ) (p h b a : List Bool) (hb : b.length = r) :
    (hornerStep r p h b a).length = r := by
  simp only [hornerStep, List.length_zipWith, multiplyBitLists_length, hb, min_self]

theorem hornerStep_eq_word (r : ℕ) (p h b a : List Bool) (hb : b.length = r) :
    hornerStep r p h b a =
      wordList (wordXor (listWord r b)
        (wordMulMod (listWord r p) (listWord r h) (listWord r a))) := by
  have hx (u v : BitWord r) :
      List.zipWith Bool.xor (wordList u) (wordList v) = wordList (wordXor u v) := by
    apply List.ext_getElem
    · simp only [wordList_length, List.length_zipWith, min_self]
    · intro i hi hj
      simp only [wordList, wordXor, List.getElem_zipWith, List.getElem_ofFn]
  have heq := hx (listWord r b)
    (wordMulMod (listWord r p) (listWord r h) (listWord r a))
  simpa only [wordList_listWord b hb, hornerStep, multiplyBitLists] using heq

def hornerFrom (r : ℕ) (p h : List Bool) (bs : List (List Bool))
    (a : List Bool) : List Bool :=
  bs.foldr (hornerStep r p h) a

@[simp] theorem hornerFrom_nil (r : ℕ) (p h a : List Bool) :
    hornerFrom r p h [] a = a := rfl

theorem hornerFrom_cons (r : ℕ) (p h b : List Bool) (bs : List (List Bool))
    (a : List Bool) :
    hornerFrom r p h (b :: bs) a = hornerStep r p h b (hornerFrom r p h bs a) := rfl

theorem hornerFrom_append (r : ℕ) (p h : List Bool)
    (bs cs : List (List Bool)) (a : List Bool) :
    hornerFrom r p h (bs ++ cs) a =
      hornerFrom r p h bs (hornerFrom r p h cs a) := by
  simp only [hornerFrom, List.foldr_append]

theorem hornerFrom_append_singleton (r : ℕ) (p h : List Bool)
    (bs : List (List Bool)) (b a : List Bool) :
    hornerFrom r p h (bs ++ [b]) a =
      hornerFrom r p h bs (hornerStep r p h b a) := by
  rw [hornerFrom_append, hornerFrom_cons, hornerFrom_nil]

theorem hornerFrom_length (r : ℕ) (p h : List Bool)
    (bs : List (List Bool)) (a : List Bool) (ha : a.length = r)
    (hbs : ∀ b ∈ bs, b.length = r) :
    (hornerFrom r p h bs a).length = r := by
  cases bs with
  | nil => exact ha
  | cons b bs =>
      rw [hornerFrom_cons]
      exact hornerStep_length r p h b _ (hbs b (by simp))

theorem hornerFrom_zero_eq_hornerBitLists (r : ℕ) (p h : List Bool)
    (bs : List (List Bool)) (hbs : ∀ b ∈ bs, b.length = r) :
    hornerFrom r p h bs (List.replicate r false) = hornerBitLists r p h bs := by
  revert hbs
  induction bs with
  | nil =>
      intro hbs
      simp only [hornerFrom_nil, hornerBitLists_nil, wordList, List.ofFn_const]
  | cons b bs ih =>
      intro hbs
      have hb : b.length = r := hbs b (by simp)
      have htail : ∀ c ∈ bs, c.length = r :=
        fun c hc => hbs c (List.mem_cons_of_mem b hc)
      rw [hornerFrom_cons, ih htail, hornerStep_eq_word r p h b _ hb,
        hornerBitLists_cons]

theorem block_flatten_length {α : Type uDepth1} (r : ℕ) (bs : List (List α))
    (hbs : ∀ b ∈ bs, b.length = r) : bs.flatten.length = bs.length * r := by
  revert hbs
  induction bs with
  | nil => intro hbs; simp
  | cons b bs ih =>
      intro hbs
      have hb : b.length = r := hbs b (by simp)
      have htail : ∀ c ∈ bs, c.length = r :=
        fun c hc => hbs c (List.mem_cons_of_mem b hc)
      simp only [List.flatten_cons, List.length_append, List.length_cons, hb, ih htail]
      simp only [Nat.add_mul, Nat.one_mul, Nat.add_comm]

end DepthThreeLowerBound

end OAI