File size: 7,309 Bytes
472a116 | 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 | import OAI.Computability.SpaceSimulation.Register.Search
namespace OAI
/-!
Any characteristic works with the unambiguous selectors below; use a
prime field with a uniform binary computation of its modulus. Supplies
a bit-space certificate for field arithmetic and folds that retain only
residues.
-/
namespace Fifths
structure Modulus where
n : ℕ
prime : n.Prime
namespace Modulus
variable (P : Modulus)
instance : Fact P.n.Prime := ⟨P.prime⟩
abbrev K := ZMod P.n
theorem bound : 1 < P.n := P.prime.one_lt
theorem pos : 0 < P.n := P.prime.pos
def weight (d : ℕ) (u : P.K) : P.K :=
-(u ^ (P.n - 1 - d))
-- Need a uniform computation yielding a modulus with Δ < p-1.
def divisorsOK (p : ℕ) (m : ℕ) : Bool :=
!(decide (2 ≤ m) && decide (p % m = 0))
def isPrime (p : ℕ) : Bool :=
decide (2 ≤ p) && Scan.all (divisorsOK p) p
theorem isPrime_iff (p : ℕ) : isPrime p = true ↔ p.Prime := by
rw [isPrime, Bool.and_eq_true, Scan.all_iff, Nat.prime_def_lt']
simp only [decide_eq_true_eq,divisorsOK,Bool.not_eq_true', Bool.and_eq_false_imp,
decide_eq_false_iff_not]
refine and_congr_right (fun _ => ?_)
constructor
· intro h i l r d
exact h i r l (Nat.mod_eq_zero_of_dvd d)
· intro h i l r d
exact h i r l (Nat.dvd_of_mod_eq_zero d)
def rangeSize (d : ℕ) := 2*(d+1) + 1
def accepted (d p : ℕ) : Bool :=
decide (d+1 < p) && isPrime p
theorem found (d : ℕ) :
Scan.pick (accepted d) (rangeSize d) < rangeSize d ∧
accepted d (Scan.pick (accepted d) (rangeSize d)) = true := by
obtain ⟨p,a,l,h⟩ := Nat.exists_prime_lt_and_le_two_mul (d+1) (by omega)
exact Scan.pick_spec _ _ ⟨p, by unfold rangeSize; omega, by simp [accepted,l,isPrime_iff,a]⟩
def chosen (d : ℕ) : Modulus :=
let p := Scan.pick (accepted d) (rangeSize d)
⟨p, (isPrime_iff _).mp ((Bool.and_eq_true _ _).mp ((found d).2)).2⟩
theorem chosen_large (d : ℕ) : d+1 < (chosen d).n := by
have h := (found d).2
simp only [accepted,Bool.and_eq_true,decide_eq_true_eq] at h
exact h.1
theorem chosen_small (d : ℕ) : (chosen d).n < rangeSize d := (found d).1
end Modulus
namespace Comp
open Modulus
variable {I X : Type} {x : I → List X} {S a b d : I → ℕ}
theorem primality (h : Comp x S a b) :
Comp x S a (fun i => (isPrime (b i)).toNat) := by
refine ((Comp.constant h.source 2).le h).both (scanAll (fun i => divisorsOK (b i)) h ?_)
intro e p r
let f : Before b → I := fun i => i.1.1
exact flip (((Comp.constant p.source 2).le r).both
(((p.trans (h.pull f)).mod r).eq (Comp.constant p.source 0)))
theorem chosenN (h : Comp x S a d) :
Comp x S a (fun i => (chosen (d i)).n) := by
have r : Comp x S a (fun i => rangeSize (d i)) :=
((Comp.constant h.source 2).mul h.plus1).plus1
refine scanPick (fun i => accepted (d i)) r ?_
intro e i j
let f : Before (fun i => rangeSize (d i)) → I := fun i => i.1.1
exact (((i.trans (h.pull f)).plus1.lt j).both j.primality)
-- Field operations uniformly in the modulus. Even pow doesn't
-- materialize an ordinary integer power before reduction.
variable {P : I → Modulus} {v w : ∀ i : I, (P i).K}
theorem fitElement (h : Fits S (fun i => (P i).n)) (v : ∀ i : I, (P i).K) :
Fits S (fun i => (v i).val) :=
h.mono (fun _ => (ZMod.val_lt _).le)
theorem valCast (p : Comp x S a (fun i => (P i).n)) (q : Comp x S a b) :
Comp x S a (fun i => (b i : (P i).K).val) :=
(q.mod p).congr (fun _i => (ZMod.val_natCast _ _).symm)
theorem valZero (p : Comp x S a (fun i => (P i).n)) :
Comp x S a (fun i => (0 : (P i).K).val) :=
(Comp.constant p.source 0).congr (fun i => by simp)
theorem valOne (p : Comp x S a (fun i => (P i).n)) :
Comp x S a (fun i => (1 : (P i).K).val) :=
(p.valCast (Comp.constant p.source 1)).congr (fun i => by simp)
theorem fieldAdd (p : Comp x S a (fun i => (P i).n))
(h : Comp x S a (fun i => (v i).val)) (q : Comp x S a (fun i => (w i).val)) :
Comp x S a (fun i => (v i + w i).val) :=
((h.add q).mod p).congr (fun _i => (ZMod.val_add ..).symm)
theorem fieldNeg (p : Comp x S a (fun i => (P i).n))
(h : Comp x S a (fun i => (v i).val)) :
Comp x S a (fun i => (-v i).val) := by
refine ((p.sub h).mod p).congr (fun i => ?_)
by_cases e : v i = 0
· simp [e]
have : NeZero (v i) := ⟨e⟩
have o : (v i).val ≠ 0 := by simpa using e
have t := (P i).pos
simp [ZMod.val_neg_of_ne_zero,Nat.mod_eq_of_lt (show
(P i).n - (v i).val < (P i).n by omega)]
theorem fieldSub (p : Comp x S a (fun i => (P i).n))
(h : Comp x S a (fun i => (v i).val)) (q : Comp x S a (fun i => (w i).val)) :
Comp x S a (fun i => (v i - w i).val) :=
(fieldAdd p h (fieldNeg p q)).congr (fun i => by simp [sub_eq_add_neg])
theorem fieldMul (p : Comp x S a (fun i => (P i).n))
(h : Comp x S a (fun i => (v i).val)) (q : Comp x S a (fun i => (w i).val)) :
Comp x S a (fun i => (v i * w i).val) :=
((h.mul q).mod p).congr (fun _i => (ZMod.val_mul ..).symm)
theorem fieldPow (p : Comp x S a (fun i => (P i).n))
(h : Comp x S a (fun i => (v i).val)) (q : Comp x S a b) :
Comp x S a (fun i => (v i ^ b i).val) := by
refine fold (fun i j => (v i ^ j).val) q ((valOne p).congr (fun i => by simp)) ?_ ?_
· let f : Through b → I := fun i => i.1.1
exact fitElement (P := fun i => P (f i)) (p.target.pull f) _
· intro e i j r
let f : Before b → I := fun i => i.1.1
exact (fieldMul (P := fun i => P (f i)) (i.trans (p.pull f)) r
(i.trans (h.pull f))).congr (fun i => by simp [pow_succ,f])
-- Summation and product of field-valued procedures. Scratch reused.
open Finset
variable (u : ∀ i : I, ℕ → (P i).K)
theorem fieldSum (p : Comp x S a (fun i => (P i).n)) (q : Comp x S a b)
(h : ∀ e : Before b → ℕ, Comp (fun i => x i.1.1) (fun i => S i.1.1) e
(fun i => a i.1.1) → Comp (fun i => x i.1.1) (fun i => S i.1.1) e (fun i => i.1.2)
→ Comp (fun i => x i.1.1) (fun i => S i.1.1) e
(fun i => (u i.1.1 i.1.2).val)) :
Comp x S a (fun i => (∑ j ∈ range (b i), u i j).val) := by
apply fold (fun i j => (∑ k ∈ range j, u i k).val) q
((valZero p).congr (fun i => by simp))
· let f : Through b → I := fun i => i.1.1
exact fitElement (P := fun i => P (f i)) (p.target.pull f) _
· intro e i j r
let f : Before b → I := fun i => i.1.1
exact (fieldAdd (P := fun i => P (f i)) (i.trans (p.pull f)) r
(h e i j)).congr (fun i => by simp [sum_range_succ])
theorem fieldProd (p : Comp x S a (fun i => (P i).n)) (q : Comp x S a b)
(h : ∀ e : Before b → ℕ, Comp (fun i => x i.1.1) (fun i => S i.1.1) e
(fun i => a i.1.1) → Comp (fun i => x i.1.1) (fun i => S i.1.1) e (fun i => i.1.2)
→ Comp (fun i => x i.1.1) (fun i => S i.1.1) e
(fun i => (u i.1.1 i.1.2).val)) :
Comp x S a (fun i => (∏ j ∈ range (b i), u i j).val) := by
apply fold (fun i j => (∏ k ∈ range j, u i k).val) q
((valOne p).congr (fun i => by simp))
· let f : Through b → I := fun i => i.1.1
exact fitElement (P := fun i => P (f i)) (p.target.pull f) _
· intro e i j r
let f : Before b → I := fun i => i.1.1
exact (fieldMul (P := fun i => P (f i)) (i.trans (p.pull f)) r
(h e i j)).congr (fun i => by simp [prod_range_succ])
end Comp
end Fifths
end OAI
|