| 1 |
From Coq Require Import BinPosDef. |
| 2 |
Require Import Crypto.Algebra.Field. |
| 3 |
Require Import Crypto.Util.Decidable. |
| 4 |
Require Import Crypto.Util.GlobalSettings. |
| 5 |
Require Import Crypto.Util.Sum Crypto.Util.Prod. |
| 6 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 7 |
Require Import Crypto.Util.Tactics.DestructHead. |
| 8 |
Require Import Crypto.Spec.MontgomeryCurve Crypto.Curves.Montgomery.Affine. |
| 9 |
Require Import Crypto.Spec.CompleteEdwardsCurve Crypto.Curves.Edwards.AffineProofs. |
| 10 |
From Coq Require Import Field_theory. |
| 11 |
Require Import Field_tac. |
| 12 |
Require Import UniquePose. |
| 13 |
|
| 14 |
Module M. |
| 15 |
Section EdwardsMontgomery. |
| 16 |
Import BinNat. |
| 17 |
Context {F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv} |
| 18 |
{field:@Algebra.Hierarchy.field F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv} |
| 19 |
{Feq_dec:Decidable.DecidableRel Feq} |
| 20 |
{char_ge_3:@Ring.char_ge F Feq Fzero Fone Fopp Fadd Fsub Fmul (BinNat.N.succ_pos (BinNat.N.two))}. |
| 21 |
Local Infix "=" := Feq : type_scope. Local Notation "a <> b" := (not (a = b)) : type_scope. |
| 22 |
Local Infix "+" := Fadd. Local Infix "*" := Fmul. |
| 23 |
Local Infix "-" := Fsub. Local Infix "/" := Fdiv. |
| 24 |
Local Notation "- x" := (Fopp x). |
| 25 |
Local Notation "x ^ 2" := (x*x) (at level 30). |
| 26 |
Local Notation "x ^ 3" := (x*x^2) (at level 30). |
| 27 |
Local Notation "0" := Fzero. Local Notation "1" := Fone. |
| 28 |
Local Notation "'∞'" := unit : type_scope. |
| 29 |
Local Notation "'∞'" := (inr tt) : core_scope. |
| 30 |
Local Open Scope core_scope. |
| 31 |
|
| 32 |
Context {a b: F} {b_nonzero:b <> 0}. |
| 33 |
|
| 34 |
Local Notation point := (@M.point F Feq Fadd Fmul a b). |
| 35 |
Local Notation add := (M.add(b_nonzero:=b_nonzero)(a:=a)). |
| 36 |
Local Notation opp := (M.opp(b_nonzero:=b_nonzero)(a:=a)). |
| 37 |
|
| 38 |
Local Notation "2" := (1+1). |
| 39 |
Local Notation "3" := (1+2). |
| 40 |
|
| 41 |
Context {ae de: F} {Hae: ae=(a+2)/b} {Hde: de=(a-2)/b} |
| 42 |
{nonzero_ae : ae <> 0} |
| 43 |
{square_ae : exists sqrt_ae, sqrt_ae^2 = ae} |
| 44 |
{nonsquare_de : forall x, x^2 <> de}. |
| 45 |
|
| 46 |
Add Field _field : (Algebra.Field.field_theory_for_stdlib_tactic (T:=F)). |
| 47 |
|
| 48 |
Local Notation Epoint := (@E.point F Feq Fone Fadd Fmul ae de). |
| 49 |
Local Notation Eadd := (@E.add F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv field Feq_dec char_ge_3 ae de). |
| 50 |
Program Definition to_Edwards (P:@point) : Epoint := |
| 51 |
match M.coordinates P return F*F with |
| 52 |
| inl (u, v) => if dec (u + 1 <> 0 /\ v <> 0) |
| 53 |
then (u/v , (u-1)/(u+1)) |
| 54 |
else (0, 0 - 1) |
| 55 |
| ∞ => (0, 1) |
| 56 |
end. |
| 57 |
Next Obligation. |
| 58 |
(* Note: perhaps we can deal with u = -1 or v = 0 case in a better way*) |
| 59 |
Proof. destruct_head' @point; cbv; break_match; trivial; try fsatz. |
| 60 |
destruct H. |
| 61 |
assert (f + 1 <> 0) by auto. |
| 62 |
assert (f0 <> 0) by auto. |
| 63 |
fsatz. |
| 64 |
Qed. |
| 65 |
|
| 66 |
Program Definition of_Edwards (P:Epoint) : point := |
| 67 |
match E.coordinates P return F*F+∞ with |
| 68 |
| (x, y) => if dec (x = 0 /\ y = 0 - 1) |
| 69 |
then inl (0, 0) |
| 70 |
else if dec (x = 0 /\ y = 1) |
| 71 |
then inr tt |
| 72 |
else inl ((1+y)/(1-y), (1+y)/(x*(1-y))) |
| 73 |
end. |
| 74 |
Next Obligation. |
| 75 |
Proof. destruct_head' @Epoint; cbv; break_match; trivial; try fsatz. |
| 76 |
assert (f <> 0). { |
| 77 |
unfold not. intros Hf. rewrite -> Hf in y. |
| 78 |
destruct (dec (f0 = 1)). |
| 79 |
- auto. |
| 80 |
- assert (f0 = 0 - 1). fsatz. auto. |
| 81 |
} |
| 82 |
assert (f0 <> 1) by fsatz. |
| 83 |
fsatz. |
| 84 |
Qed. |
| 85 |
|
| 86 |
Lemma no_roots: forall x : F, x^2 + a * x + 1 <> 0. |
| 87 |
Proof. |
| 88 |
unfold not. intros x H. |
| 89 |
destruct square_ae. |
| 90 |
destruct nonsquare_de with (x := (2 * x + a) / (b * x0)). |
| 91 |
field_simplify_eq; try split; fsatz. |
| 92 |
Qed. |
| 93 |
|
| 94 |
Lemma My_zero_then_Mx_zero: forall x y : F, y = 0 -> b * y ^ 2 = x ^ 3 + a * x ^ 2 + x -> x = 0. |
| 95 |
Proof. intros. pose proof (no_roots x). fsatz. Qed. |
| 96 |
|
| 97 |
Lemma Mx_not_neg1: forall x y : F, b * y ^ 2 = x ^ 3 + a * x ^ 2 + x -> x + 1 <> 0. |
| 98 |
Proof. |
| 99 |
intros. unfold not. intros. |
| 100 |
destruct nonsquare_de with (x:=y). fsatz. |
| 101 |
Qed. |
| 102 |
|
| 103 |
Lemma not_and_True_l: forall A B: Prop, Decidable A -> Decidable B -> A -> ~ (A /\ ~ B) <-> B. |
| 104 |
Proof. intros. destruct (dec A), (dec B); intuition. Qed. |
| 105 |
|
| 106 |
Lemma not_and_decidable: forall P Q: Prop, Decidable P -> Decidable Q -> ~ (P /\ Q) <-> ~ P \/ ~ Q. |
| 107 |
Proof. intros. destruct (dec Q), (dec P); intuition trivial. Qed. |
| 108 |
|
| 109 |
Ltac t := |
| 110 |
repeat |
| 111 |
match goal with |
| 112 |
| _ => solve [ trivial ] |
| 113 |
| _ => progress intros |
| 114 |
| _ => progress subst |
| 115 |
| _ => progress Tactics.DestructHead.destruct_head' @M.point |
| 116 |
| _ => progress Tactics.DestructHead.destruct_head' @prod |
| 117 |
| _ => progress Tactics.DestructHead.destruct_head' @sum |
| 118 |
| _ => progress Tactics.DestructHead.destruct_head' @and |
| 119 |
| _ => progress Sum.inversion_sum |
| 120 |
| _ => progress Prod.inversion_prod |
| 121 |
| _ => progress Tactics.BreakMatch.break_match_hyps |
| 122 |
| _ => progress Tactics.BreakMatch.break_match |
| 123 |
| _ => progress cbv [M.coordinates M.add M.zero M.eq M.opp proj1_sig |
| 124 |
E.coordinates E.add E.zero E.eq E.opp |
| 125 |
of_Edwards to_Edwards fst snd] in * |
| 126 |
| |- _ <-> _ => split |
| 127 |
end. |
| 128 |
|
| 129 |
Ltac t2 := |
| 130 |
repeat match goal with |
| 131 |
| H: ~ ( _ = _ /\ _ = _) |- _ => apply not_and_decidable in H; [ | exact _ ..] |
| 132 |
| _ => progress auto |
| 133 |
| H: ?x = 0 |- _ => progress (rewrite H in * ) |
| 134 |
| _ => progress fsatz |
| 135 |
| H: _ \/ _ |- _ => destruct H |
| 136 |
end. |
| 137 |
|
| 138 |
Ltac t3 := |
| 139 |
repeat match goal with |
| 140 |
| H: False |- _ => destruct H |
| 141 |
| _ => progress fsatz |
| 142 |
| H: ?x = ?y |- _ => rewrite H in * |
| 143 |
| _ => try intuition |
| 144 |
end. |
| 145 |
|
| 146 |
Ltac t4 := |
| 147 |
repeat match goal with |
| 148 |
| H: ?x = ?y |- _ => rewrite H in * |- |
| 149 |
| H: ~ ( _ /\ _ ) |- _ => destruct H; split |
| 150 |
| _ => fsatz |
| 151 |
end. |
| 152 |
|
| 153 |
Ltac t5 := |
| 154 |
repeat match goal with |
| 155 |
| H: b * ?x ^ 2 = _ |- _ => |
| 156 |
match goal with |
| 157 |
| G: ?x = 0 |- _ => unique pose proof (My_zero_then_Mx_zero _ _ G H) |
| 158 |
| _ => unique pose proof (Mx_not_neg1 _ _ H) |
| 159 |
end |
| 160 |
| H: context [?a /\ _] |- _ => |
| 161 |
match goal with G: a |- _ => |
| 162 |
rewrite not_and_True_l in H; [ | exact _ | exact _ | trivial ] |
| 163 |
end |
| 164 |
| H: ?x = 0 |- _ => rewrite H in * |
| 165 |
| H: 0 <> - 0 |- _ => destruct H; field |
| 166 |
| _ => try auto |
| 167 |
end. |
| 168 |
|
| 169 |
Ltac field_simpl_fsatz := field_simplify_eq; [fsatz | repeat split; try auto]. |
| 170 |
|
| 171 |
Ltac t_destruct H := unfold not; intros; destruct H; field_simpl_fsatz. |
| 172 |
|
| 173 |
Ltac sqrt_de_witness y := unfold not; intros; destruct nonsquare_de with (x:= y); fsatz. |
| 174 |
|
| 175 |
Definition Feq_refl:= RelationClasses.reflexivity (R:= Feq). |
| 176 |
Hint Immediate Feq_refl: core. |
| 177 |
|
| 178 |
Global Instance EdwardsMontgomeryIsomorphism {_1 _2 _3 _4} |
| 179 |
{char_ge_12:@Ring.char_ge F Feq Fzero Fone Fopp Fadd Fsub Fmul 12} |
| 180 |
: |
| 181 |
@Group.isomorphic_commutative_groups |
| 182 |
(@E.point F Feq Fone Fadd Fmul ae de) |
| 183 |
E.eq |
| 184 |
(@E.add F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv field _1 _2 ae de nonzero_ae square_ae nonsquare_de) |
| 185 |
(E.zero(nonzero_a:=nonzero_ae)) |
| 186 |
(@E.opp F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv field _2 ae de nonzero_ae) |
| 187 |
|
| 188 |
(@M.point F Feq Fadd Fmul a b) |
| 189 |
M.eq |
| 190 |
(M.add(char_ge_3:=_1)(b_nonzero:=_3)) |
| 191 |
M.zero |
| 192 |
(M.opp(b_nonzero:=_4)) |
| 193 |
|
| 194 |
(of_Edwards) |
| 195 |
(to_Edwards). |
| 196 |
Proof. |
| 197 |
eapply Group.commutative_group_by_isomorphism. |
| 198 |
{ eapply (E.edwards_curve_commutative_group(a:=ae)(d:=de)(nonzero_a:=nonzero_ae)(square_a:=square_ae)(nonsquare_d:=nonsquare_de)). } |
| 199 |
{ (* Bijection, not automated due to non-local case analysis *) |
| 200 |
t; split. |
| 201 |
all: try solve [t2]. Optimize Proof. Optimize Heap. |
| 202 |
all: assert (f <> 0) by t2; assert (f0 <> 1) by fsatz; destruct H1; split; fsatz. |
| 203 |
} Optimize Proof. Optimize Heap. |
| 204 |
{ (* Equality *) |
| 205 |
t. |
| 206 |
7: t5. |
| 207 |
all: try split; t3. |
| 208 |
} Optimize Proof. Optimize Heap. |
| 209 |
{ (* Addition: numerous calls to clear below are needed to make fsatz run in reasonable time *) |
| 210 |
t. |
| 211 |
|
| 212 |
4, 21-25: split; fsatz. Optimize Proof. Optimize Heap. |
| 213 |
7, 13-15, 19: split; t5; fsatz. Optimize Proof. Optimize Heap. |
| 214 |
10, 13-14: t5; destruct H0; split; fsatz. Optimize Proof. Optimize Heap. |
| 215 |
8-9: clear H1; t4. Optimize Proof. Optimize Heap. |
| 216 |
2-3, 5-6: t4. Optimize Proof. Optimize Heap. |
| 217 |
{ |
| 218 |
split. |
| 219 |
- fsatz. |
| 220 |
- field_simpl_fsatz. |
| 221 |
destruct square_ae; assert (x <> 0) by fsatz. |
| 222 |
sqrt_de_witness (f0 ^ 2 / (f ^ 2 * x)). |
| 223 |
} |
| 224 |
Optimize Proof. Optimize Heap. |
| 225 |
{ |
| 226 |
split. |
| 227 |
- field_simpl_fsatz; fsatz. |
| 228 |
- fsatz. |
| 229 |
} |
| 230 |
Optimize Proof. Optimize Heap. |
| 231 |
{ |
| 232 |
rewrite H in * |-. |
| 233 |
assert (f2 = f0) by fsatz. |
| 234 |
assert (f <> 0) by fsatz. |
| 235 |
destruct (dec (f = 1)). |
| 236 |
- split; fsatz. |
| 237 |
- destruct H1. split. |
| 238 |
+ sqrt_de_witness (f0 * (f + 1) / (f ^ 2 - f)). |
| 239 |
+ destruct square_ae; assert (x <> 0) by fsatz. |
| 240 |
sqrt_de_witness (f0 ^ 2 / (f ^ 2 * x)). |
| 241 |
} |
| 242 |
Optimize Proof. Optimize Heap. |
| 243 |
{ |
| 244 |
assert (Epoint1: ae * (f1 / f2) ^ 2 + ((f1 - 1) / (f1 + 1)) ^ 2 = 1 + de * (f1 / f2) ^ 2 * ((f1 - 1) / (f1 + 1)) ^ 2). { |
| 245 |
clear y H H2 H3 H0 H5 f f0. |
| 246 |
fsatz. |
| 247 |
} |
| 248 |
assert (Epoint: ae * (f / f0) ^ 2 + ((f - 1) / (f + 1)) ^ 2 = 1 + de * (f / f0) ^ 2 * ((f - 1) / (f + 1)) ^ 2). { |
| 249 |
clear y0 H H1 H4 H0 H5 f1 f2 Epoint1. |
| 250 |
fsatz. |
| 251 |
} |
| 252 |
pose proof (Pre.denominator_nonzero_x _ nonzero_ae square_ae _ nonsquare_de _ _ Epoint1 _ _ Epoint) as denom_nonzero_x. |
| 253 |
pose proof (Pre.denominator_nonzero_y _ nonzero_ae square_ae _ nonsquare_de _ _ Epoint1 _ _ Epoint) as denom_nonzero_y. |
| 254 |
assert (f - f1 <> 0) by fsatz. |
| 255 |
Optimize Proof. Optimize Heap. |
| 256 |
split; rewrite Hae, Hde in *; |
| 257 |
clear Epoint Epoint1 square_ae nonzero_ae nonsquare_de Hae Hde ae de; |
| 258 |
field_simpl_fsatz. |
| 259 |
- t_destruct denom_nonzero_x. |
| 260 |
- t_destruct H5. |
| 261 |
- t_destruct denom_nonzero_y. |
| 262 |
- t_destruct H0. |
| 263 |
} |
| 264 |
Optimize Proof. Optimize Heap. |
| 265 |
{ |
| 266 |
remember (b * ((f0 - f2) / (f - f1)) ^ 2 - a - f1 - f) as u. |
| 267 |
remember ((2 * f1 + f + a) * ((f0 - f2) / (f - f1)) - b * ((f0 - f2) / (f - f1)) ^ 3 - f2) as v. |
| 268 |
assert (uv_Mpoint: b * v ^ 2 = u ^ 3 + a * u ^ 2 + u). { |
| 269 |
rewrite Hequ, Heqv. |
| 270 |
clear H0 H1 H2 H3 H4 Hequ Heqv. |
| 271 |
fsatz. |
| 272 |
} |
| 273 |
t5. |
| 274 |
rewrite Hequ in H6. |
| 275 |
rewrite Heqv in H0. |
| 276 |
clear u v Hequ Heqv H5 H7 uv_Mpoint. |
| 277 |
assert (Epoint1: ae * (f1 / f2) ^ 2 + ((f1 - 1) / (f1 + 1)) ^ 2 = 1 + de * (f1 / f2) ^ 2 * ((f1 - 1) / (f1 + 1)) ^ 2). { |
| 278 |
clear y H H2 H3 H0 H6 f f0. |
| 279 |
fsatz. |
| 280 |
} |
| 281 |
assert (Epoint: ae * (f / f0) ^ 2 + ((f - 1) / (f + 1)) ^ 2 = 1 + de * (f / f0) ^ 2 * ((f - 1) / (f + 1)) ^ 2). { |
| 282 |
clear y0 H H1 H4 H0 H6 f1 f2 Epoint1. |
| 283 |
fsatz. |
| 284 |
} |
| 285 |
pose proof (Pre.denominator_nonzero_x _ nonzero_ae square_ae _ nonsquare_de _ _ Epoint1 _ _ Epoint) as denom_nonzero_x. |
| 286 |
pose proof (Pre.denominator_nonzero_y _ nonzero_ae square_ae _ nonsquare_de _ _ Epoint1 _ _ Epoint) as denom_nonzero_y. |
| 287 |
split; rewrite Hae, Hde in *; |
| 288 |
clear Epoint Epoint1 square_ae nonzero_ae nonsquare_de Hae Hde ae de; |
| 289 |
field_simpl_fsatz. |
| 290 |
- t_destruct denom_nonzero_x. |
| 291 |
- t_destruct denom_nonzero_y. |
| 292 |
} |
| 293 |
} |
| 294 |
Optimize Proof. Optimize Heap. |
| 295 |
{ (* Inverses *) |
| 296 |
t. |
| 297 |
2-3: t4. |
| 298 |
all: split; fsatz. |
| 299 |
} |
| 300 |
(* Zero *) |
| 301 |
t; auto. |
| 302 |
Qed. |
| 303 |
|
| 304 |
End EdwardsMontgomery. |
| 305 |
End M. |
| 306 |
|