Debian Codegraph @

src/Curves/Edwards/AffineProofs.v

raw
1 Require Export Crypto.Spec.CompleteEdwardsCurve.
2
3 Require Import Crypto.Algebra.Hierarchy Crypto.Algebra.ScalarMult Crypto.Util.Decidable.
4 From Coq Require Import Eqdep_dec.
5 From Coq Require Import Morphisms.
6 From Coq Require Import Relation_Definitions.
7 From Coq Require Field_tac.
8 Require Import Crypto.Util.Tuple Crypto.Util.Notations.
9 Require Import Crypto.Util.Tactics.UniquePose.
10 Require Import Crypto.Util.Tactics.DestructHead.
11 Require Import Crypto.Util.Tactics.BreakMatch.
12 Require Import Crypto.Util.Tactics.SetoidSubst.
13 Require Export Crypto.Util.FixCoqMistakes.
14
15 Module E.
16 Import Group Ring Field CompleteEdwardsCurve.E.
17
18 Notation onCurve_zero := Pre.onCurve_zero.
19 Notation denominator_nonzero := Pre.denominator_nonzero.
20 Notation denominator_nonzero_x := Pre.denominator_nonzero_x.
21 Notation denominator_nonzero_y := Pre.denominator_nonzero_y.
22 Notation onCurve_add := Pre.onCurve_add.
23
24 Section CompleteEdwardsCurveTheorems.
25 Context {F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv}
26 {field:@field F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv}
27 {char_ge_3 : @Ring.char_ge F Feq Fzero Fone Fopp Fadd Fsub Fmul (BinNat.N.succ_pos BinNat.N.two)}
28 {Feq_dec:DecidableRel Feq}.
29 Local Infix "=" := Feq : type_scope. Local Notation "a <> b" := (not (a = b)) : type_scope.
30 Local Notation "0" := Fzero. Local Notation "1" := Fone.
31 Local Infix "+" := Fadd. Local Infix "*" := Fmul.
32 Local Infix "-" := Fsub. Local Infix "/" := Fdiv.
33 Local Notation "x ^ 2" := (x*x).
34
35 Context {a d: F}
36 {nonzero_a : a <> 0}
37 {square_a : exists sqrt_a, sqrt_a^2 = a}
38 {nonsquare_d : forall x, x^2 <> d}.
39
40 Local Notation onCurve x y := (a*x^2 + y^2 = 1 + d*x^2*y^2) (only parsing).
41 Local Notation point := (@E.point F Feq Fone Fadd Fmul a d).
42 Local Notation eq := (@E.eq F Feq Fone Fadd Fmul a d).
43 Local Notation zero := (E.zero(nonzero_a:=nonzero_a)(d:=d)).
44 Local Notation add := (E.add(nonzero_a:=nonzero_a)(square_a:=square_a)(nonsquare_d:=nonsquare_d)).
45 Local Notation mul := (E.mul(nonzero_a:=nonzero_a)(square_a:=square_a)(nonsquare_d:=nonsquare_d)).
46
47 Program Definition opp (P:point) : point := (Fopp (fst P), (snd P)).
48 Next Obligation. match goal with P : point |- _ => destruct P as [ [??]?] end; cbv; fsatz. Qed.
49
50 Global Instance Decidable_eq : Decidable.DecidableRel (@E.eq _ Feq Fone Fadd Fmul a d) := _.
51
52 Ltac t_step :=
53 match goal with
54 | _ => solve [trivial | exact _ ]
55 | _ => intro
56 | |- Equivalence _ => split
57 | |- commutative_group => split | |- group => split | |- monoid => split
58 | |- is_associative => split | |- is_commutative => split
59 | |- is_left_inverse => split | |- is_right_inverse => split
60 | |- is_left_identity => split | |- is_right_identity => split
61 | _ => progress destruct_head' @E.point
62 | _ => progress destruct_head' prod
63 | _ => progress destruct_head' and
64 | |- context[E.add ?P ?Q] =>
65 unique pose proof (Pre.denominator_nonzero_x _ nonzero_a square_a _ nonsquare_d _ _ (proj2_sig P) _ _ (proj2_sig Q));
66 unique pose proof (Pre.denominator_nonzero_y _ nonzero_a square_a _ nonsquare_d _ _ (proj2_sig P) _ _ (proj2_sig Q))
67 | _ => progress cbv [opp E.zero E.eq E.add E.coordinates proj1_sig fieldwise fieldwise'] in *
68 (* [_gather_nonzeros] must run before [fst_pair] or [simpl] but after splitting E.eq and unfolding [E.add] *)
69 | |- _ /\ _ => split | |- _ <-> _ => split
70 end.
71 Ltac t := repeat t_step; fsatz.
72
73 Global Instance associative_add : is_associative(eq:=E.eq)(op:=add).
74 Proof using Type.
75 (* [nsatz_compute] for a denominator runs out of 6GB of stack space *)
76 (* COQBUG: https://coq.inria.fr/bugs/show_bug.cgi?id=5359 *)
77 Add Field _field : (Algebra.Field.field_theory_for_stdlib_tactic (T:=F)).
78 Import Field_tac.
79 repeat t_step; (field_simplify_eq; [IntegralDomain.nsatz|]); repeat split; trivial.
80 { intro. eapply H3. field_simplify_eq; repeat split; trivial. IntegralDomain.nsatz. }
81 { intro. eapply H. field_simplify_eq; repeat split; trivial. IntegralDomain.nsatz. }
82 { intro. eapply H4. field_simplify_eq; repeat split; trivial. IntegralDomain.nsatz. }
83 { intro. eapply H0. field_simplify_eq; repeat split; trivial. IntegralDomain.nsatz. }
84 Qed.
85
86 Global Instance edwards_curve_commutative_group : commutative_group (eq:=eq)(op:=add)(id:=zero)(inv:=opp).
87 Proof using Type. t. Qed.
88
89 Global Instance Proper_coordinates : Proper (eq==>fieldwise (n:=2) Feq) coordinates. Proof using Type. repeat t_step. Qed.
90
91 Global Instance Proper_mul : Proper (Logic.eq==>eq==>eq) mul.
92 Proof using Type.
93 intros n n'; repeat intro; subst n'.
94 induction n; (reflexivity || eapply (_:Proper (eq==>eq==>eq) add); eauto).
95 Qed.
96
97 Section PointCompression.
98 Local Notation "x ^ 2" := (x*x).
99
100 Lemma solve_correct x y : onCurve x y <-> (x^2 = (y^2-1) / (d*y^2-a)).
101 Proof using Feq_dec field nonsquare_d nonzero_a square_a. destruct square_a as [sqrt_a]; pose proof (nonsquare_d (sqrt_a/y));
102 split; intros; fsatz. Qed.
103
104 (* TODO: move *)
105 Definition exist_option {A} (P : A -> Prop) (x : option A)
106 : match x with Some v => P v | None => True end -> option { a : A | P a }.
107 destruct x; intros; [apply Some | apply None]; eauto. Defined.
108 Lemma exist_option_Some {A} P (x:option A) pf s
109 (H:Logic.eq (exist_option P x pf) (Some s))
110 : Logic.eq x (Some (proj1_sig s)).
111 Proof using Type. destruct x, s; cbv [exist_option proj1_sig] in *; congruence. Qed.
112 Lemma exist_option_None {A} P (x:option A) pf
113 (H:Logic.eq (exist_option P x pf) None)
114 : Logic.eq x None.
115 Proof using Type. destruct x; cbv [exist_option proj1_sig] in *; congruence. Qed.
116
117 Context
118 {sqrt_div:F -> F -> option F}
119 {sqrt_Some: forall u v r, Logic.eq (sqrt_div u v) (Some r) -> r^2 = u/v}
120 {sqrt_None: forall u v, Logic.eq (sqrt_div u v) None -> forall r, r^2 <> u/v}
121 {parity:F -> bool} {Proper_parity: Proper (Feq ==> Logic.eq) parity}
122 {parity_opp: forall x, x <> 0 -> Logic.eq (parity (Fopp x)) (negb (parity x)) }.
123
124 Definition compress (P:point) : (bool*F) :=
125 let (x, y) := coordinates P in pair (parity x) y.
126 Definition set_sign r p : option F :=
127 if dec (Logic.eq (parity r) p)
128 then Some r
129 else
130 let r' := Fopp r in
131 if dec (Logic.eq (parity r') p)
132 then Some r'
133 else None.
134 Lemma set_sign_None r p s (H:Logic.eq (set_sign r p) (Some s))
135 : s^2 = r^2 /\ Logic.eq (parity s) p.
136 Proof using Feq_dec field nonzero_a.
137 repeat match goal with
138 | _ => progress subst
139 | _ => progress cbv [set_sign] in *
140 | _ => progress break_match_hyps
141 | _ => progress Option.inversion_option
142 | _ => split
143 | _ => solve [ trivial | fsatz ]
144 end.
145 Qed.
146 Lemma set_sign_Some r p (H:Logic.eq (set_sign r p) None)
147 : forall s, s^2 = r^2 -> not (Logic.eq (parity s) p).
148 repeat match goal with
149 | _ => progress intros
150 | _ => progress subst
151 | _ => progress cbv [set_sign] in *
152 | _ => progress break_match_hyps
153 | _ => progress Option.inversion_option
154 end.
155 destruct (dec (r = 0)).
156 { assert (s = 0) by Nsatz.nsatz_power 2%nat.
157 setoid_subst_rel Feq; trivial. }
158 { progress rewrite parity_opp in * by assumption.
159 destruct (parity r), p; cbv [negb] in *; congruence. }
160 Qed.
161
162 Local Ltac t'_step :=
163 match goal with
164 | _ => progress subst
165 | _ => progress destruct_head' @E.point
166 | _ => progress destruct_head' and
167 | _ => progress break_match
168 | _ => progress break_match_hyps
169 | _ => progress Option.inversion_option
170 | _ => progress Prod.inversion_prod
171 | H:_ |- _ => unique pose proof (sqrt_Some _ _ _ H); clear H
172 | H:_ |- _ => unique pose proof (sqrt_None _ _ H); clear H
173 | H:_ |- _ => unique pose proof (set_sign_None _ _ _ H); clear H
174 | H:_ |- _ => unique pose proof (set_sign_Some _ _ H); clear H
175 | H:_ |- _ => unique pose proof (exist_option_Some _ _ _ _ H); clear H
176 | H:_ |- _ => unique pose proof (exist_option_None _ _ _ H); clear H
177 | _ => solve [trivial | eapply solve_correct; fsatz]
178 end.
179 Local Ltac t' := repeat t'_step.
180
181 Program Definition decompress (b:bool*F) : option point :=
182 exist_option _
183 match b return option (F*F) with
184 (p, y) =>
185 match sqrt_div (y^2 - 1) (d*y^2 - a) return option (F*F) with
186 | None => None
187 | Some r =>
188 match set_sign r p return option (F*F) with
189 | Some x => Some (x, y)
190 | None => None
191 end
192 end
193 end _.
194 Next Obligation. t'. Qed.
195
196 Lemma decompress_Some b P (H:Logic.eq (decompress b) (Some P))
197 : Logic.eq (compress P) b.
198 Proof using Type. cbv [compress decompress] in *; t'. Qed.
199
200 Lemma decompress_None b (H:Logic.eq (decompress b) None)
201 : forall P, not (Logic.eq (compress P) b).
202 Proof.
203 cbv [compress decompress exist_option coordinates] in *; intros.
204 t'.
205 { intro.
206 match goal with
207 | [ H0 : _ |- False ]
208 => apply (H0 f); [t'|congruence]; clear H0
209 end.
210 rewrite solve_correct in y; Nsatz.nsatz_power 2%nat. }
211 { intro. Prod.inversion_prod; subst.
212 rewrite solve_correct in y.
213 eapply H. eapply y. }
214 Qed.
215 End PointCompression.
216 End CompleteEdwardsCurveTheorems.
217 Section Homomorphism.
218 Context {F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv}
219 {field:@field F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv}
220 {Fchar_ge_3 : @Ring.char_ge F Feq Fzero Fone Fopp Fadd Fsub Fmul (BinNat.N.succ_pos BinNat.N.two)}
221 {Feq_dec:DecidableRel Feq}.
222
223 Context {Fa Fd: F}
224 {nonzero_a : not (Feq Fa Fzero)}
225 {square_a : exists sqrt_a, Feq (Fmul sqrt_a sqrt_a) Fa}
226 {nonsquare_d : forall x, not (Feq (Fmul x x) Fd)}.
227
228 Context {K Keq Kzero Kone Kopp Kadd Ksub Kmul Kinv Kdiv}
229 {fieldK: @Algebra.Hierarchy.field K Keq Kzero Kone Kopp Kadd Ksub Kmul Kinv Kdiv}
230 {Keq_dec:DecidableRel Keq}.
231 Context {FtoK:F->K} {HFtoK:@Ring.is_homomorphism F Feq Fone Fadd Fmul
232 K Keq Kone Kadd Kmul FtoK}.
233 Context {KtoF:K->F} {HKtoF:@Ring.is_homomorphism K Keq Kone Kadd Kmul
234 F Feq Fone Fadd Fmul KtoF}.
235 Context {HisoF:forall x, Feq (KtoF (FtoK x)) x}.
236 Context {Ka} {Ha:Keq (FtoK Fa) Ka} {Kd} {Hd:Keq (FtoK Fd) Kd}.
237
238 Lemma nonzero_Ka : ~ Keq Ka Kzero.
239 Proof using Feq_dec HFtoK HKtoF Ha HisoF Keq_dec field fieldK nonzero_a.
240 rewrite <-Ha.
241 Ring.pull_homomorphism FtoK.
242 intro X.
243 eapply (Monoid.is_homomorphism_phi_proper(phi:=KtoF)) in X.
244 rewrite 2HisoF in X.
245 auto.
246 Qed.
247
248 Lemma square_Ka : exists sqrt_a, Keq (Kmul sqrt_a sqrt_a) Ka.
249 Proof using Feq_dec HFtoK Ha Keq_dec field fieldK square_a.
250 destruct square_a as [sqrt_a]. exists (FtoK sqrt_a).
251 Ring.pull_homomorphism FtoK. rewrite <-Ha.
252 eapply Monoid.is_homomorphism_phi_proper; assumption.
253 Qed.
254
255 Lemma nonsquare_Kd : forall x, not (Keq (Kmul x x) Kd).
256 Proof using Feq_dec HKtoF Hd HisoF Keq_dec field fieldK nonsquare_d.
257 intros x X. apply (nonsquare_d (KtoF x)).
258 Ring.pull_homomorphism KtoF. rewrite X. rewrite <-Hd, HisoF.
259 reflexivity.
260 Qed.
261
262 (* TODO: character respects isomorphism *)
263 Global Instance Kchar_ge_2 :
264 @char_ge K Keq Kzero Kone Kopp Kadd Ksub Kmul (BinNat.N.succ_pos BinNat.N.two).
265 Proof.
266 intros p Hp X; apply (Fchar_ge_3 p Hp).
267 eapply Monoid.is_homomorphism_phi_proper in X.
268 rewrite (homomorphism_zero(zero:=Fzero)(phi:=KtoF)) in X.
269 etransitivity; [|eexact X]; clear X.
270 rewrite (of_Z_absorbs_homomorphism(phi:=KtoF)).
271 reflexivity.
272 Qed.
273
274 Local Notation Fpoint := (@E.point F Feq Fone Fadd Fmul Fa Fd).
275 Local Notation Kpoint := (@E.point K Keq Kone Kadd Kmul Ka Kd).
276 Local Notation FzeroP := (E.zero(nonzero_a:=nonzero_a)(d:=Fd)).
277 Local Notation KzeroP := (E.zero(nonzero_a:=nonzero_Ka)(d:=Kd)).
278 Local Notation FaddP := (E.add(nonzero_a:=nonzero_a)(square_a:=square_a)(nonsquare_d:=nonsquare_d)).
279 Local Notation KaddP := (E.add(nonzero_a:=nonzero_Ka)(square_a:=square_Ka)(nonsquare_d:=nonsquare_Kd)).
280
281 Obligation Tactic := idtac.
282 Program Definition point_phi (P:Fpoint) : Kpoint := exist _ (
283 let (x, y) := coordinates P in (FtoK x, FtoK y)) _.
284 Next Obligation.
285 destruct P as [ [? ?] ?]; cbv.
286 rewrite <-!Ha, <-!Hd; pull_homomorphism FtoK.
287 eapply Monoid.is_homomorphism_phi_proper; assumption.
288 Qed.
289
290 Lemma Proper_point_phi : Proper (eq==>eq) point_phi.
291 Proof using Type.
292 intros P Q H.
293 destruct P as [ [? ?] ?], Q as [ [? ?] ?], H as [Hl Hr]; cbv.
294 rewrite !Hl, !Hr. split; reflexivity.
295 Qed.
296
297 Lemma lift_ismorphism : @Monoid.is_homomorphism Fpoint eq FaddP
298 Kpoint eq KaddP point_phi.
299 Proof using Type.
300 repeat match goal with
301 | |- _ => intro
302 | |- Monoid.is_homomorphism => split
303 | _ => progress destruct_head' @E.point
304 | _ => progress destruct_head' prod
305 | _ => progress destruct_head' and
306 | |- context[E.add ?P ?Q] =>
307 unique pose proof (Pre.denominator_nonzero_x _ nonzero_a square_a _ nonsquare_d _ _ (proj2_sig P) _ _ (proj2_sig Q));
308 unique pose proof (Pre.denominator_nonzero_y _ nonzero_a square_a _ nonsquare_d _ _ (proj2_sig P) _ _ (proj2_sig Q))
309 | _ => progress cbv [eq add point_phi coordinates] in *
310 | |- _ /\ _ => split
311 | _ => rewrite !(homomorphism_div(phi:=FtoK)) by assumption
312 | _ => rewrite !Ha
313 | _ => rewrite !Hd
314 | _ => Ring.push_homomorphism FtoK
315 | |- _ ?x ?x => reflexivity
316 | _ => eapply Monoid.is_homomorphism_phi_proper; assumption
317 end.
318 Qed.
319 End Homomorphism.
320 End E.