| 1 |
Require Import Crypto.Spec.WeierstrassCurve. |
| 2 |
Require Import Crypto.Util.Decidable Crypto.Algebra.Field. |
| 3 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 4 |
Require Import Crypto.Util.Tactics.DestructHead. |
| 5 |
Require Import Crypto.Util.Tactics.SpecializeBy. |
| 6 |
Require Import Crypto.Util.Tactics.SetoidSubst. |
| 7 |
Require Import Crypto.Util.Notations Crypto.Util.FixCoqMistakes. |
| 8 |
Require Import Crypto.Util.Sum Crypto.Util.Prod Crypto.Util.Sigma. |
| 9 |
|
| 10 |
Module Projective. |
| 11 |
Section Projective. |
| 12 |
Context {F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv} {a b:F} |
| 13 |
{field:@Algebra.Hierarchy.field F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv} |
| 14 |
{char_ge_3:@Ring.char_ge F Feq Fzero Fone Fopp Fadd Fsub Fmul (BinNat.N.succ_pos (BinNat.N.two))} |
| 15 |
{Feq_dec:DecidableRel Feq}. |
| 16 |
Local Infix "=" := Feq : type_scope. Local Notation "a <> b" := (not (a = b)) : type_scope. |
| 17 |
Local Notation "0" := Fzero. Local Notation "1" := Fone. |
| 18 |
Local Infix "+" := Fadd. Local Infix "-" := Fsub. |
| 19 |
Local Infix "*" := Fmul. Local Infix "/" := Fdiv. |
| 20 |
Local Notation "x ^ 2" := (x*x). Local Notation "x ^ 3" := (x*x^2). |
| 21 |
Local Notation Wpoint := (@W.point F Feq Fadd Fmul a b). |
| 22 |
|
| 23 |
(* Originally from |
| 24 |
<http://www.mat.uniroma3.it/users/pappa/CORSI/CR510_13_14/BosmaLenstra.pdf> |
| 25 |
"Commplete Systems of Addition Laws" by Bosma and Lenstra; |
| 26 |
optimized in <https://eprint.iacr.org/2015/1060.pdf> "Complete |
| 27 |
addition formulas for prime order elliptic curves" Algorithm 1 |
| 28 |
"Complete, projective point addition for arbitrary prime order |
| 29 |
short Weierstrass curves" by Joost Renes, Craig Costello, and |
| 30 |
Lejla Batina. *) |
| 31 |
|
| 32 |
Ltac t := |
| 33 |
repeat match goal with |
| 34 |
| _ => solve [ discriminate | contradiction | trivial ] |
| 35 |
| _ => progress cbv zeta |
| 36 |
| _ => progress intros |
| 37 |
| _ => progress destruct_head' @W.point |
| 38 |
| _ => progress destruct_head' sum |
| 39 |
| _ => progress destruct_head' prod |
| 40 |
| _ => progress destruct_head' unit |
| 41 |
| _ => progress destruct_head' and |
| 42 |
| _ => progress specialize_by assumption |
| 43 |
| _ => progress cbv [W.eq W.add W.coordinates proj1_sig] in * |
| 44 |
| _ => progress break_match_hyps |
| 45 |
| _ => progress break_match |
| 46 |
| |- _ /\ _ => split | |- _ <-> _ => split |
| 47 |
| [H:~ _ <> _ |- _ ] => rewrite (not_not (_ = _)) in H |
| 48 |
| [H: not ?P, G: ?P -> _ |- _ ] => clear G |
| 49 |
| [H: ?P, G: not ?P -> _ |- _ ] => clear G |
| 50 |
| [H: ?x = ?x |- _ ] => clear H |
| 51 |
| [H: True |- _ ] => clear H |
| 52 |
end. |
| 53 |
|
| 54 |
Definition point : Type := { P : F*F*F | let '(X,Y,Z) := P in Y^2*Z = X^3 + a*X*Z^2 + b*Z^3 /\ (Z = 0 -> Y <> 0) }. |
| 55 |
|
| 56 |
Program Definition to_affine (P:point) : Wpoint := |
| 57 |
match proj1_sig P return F*F+_ with |
| 58 |
| (X, Y, Z) => |
| 59 |
if dec (Z = 0) then inr tt |
| 60 |
else inl (X/Z, Y/Z) |
| 61 |
end. |
| 62 |
Next Obligation. Proof. t. fsatz. Qed. |
| 63 |
|
| 64 |
Program Definition of_affine (P:Wpoint) : point := |
| 65 |
match W.coordinates P return F*F*F with |
| 66 |
| inl (x, y) => (x, y, 1) |
| 67 |
| inr _ => (0, 1, 0) |
| 68 |
end. |
| 69 |
Next Obligation. Proof. t; fsatz. Qed. |
| 70 |
|
| 71 |
Definition eq (P Q:point) : Prop := |
| 72 |
match proj1_sig P, proj1_sig Q with |
| 73 |
| (X1, Y1, Z1), (X2, Y2, Z2) => |
| 74 |
if dec (Z1 = 0) then Z2 = 0 |
| 75 |
else if dec (Z2 = 0) then False |
| 76 |
else X1*Z2 = X2*Z1 /\ Y1*Z2 = Y2*Z1 |
| 77 |
end. |
| 78 |
|
| 79 |
Lemma eq_iff_Weq P Q : eq P Q <-> W.eq (to_affine P) (to_affine Q). |
| 80 |
Proof. |
| 81 |
cbv [W.eq eq to_affine] in *; t; specialize_by_assumption; fsatz. |
| 82 |
Qed. |
| 83 |
|
| 84 |
Program Definition opp (P:point) : point := |
| 85 |
match proj1_sig P return F*F*F with |
| 86 |
| (X, Y, Z) => (X, Fopp Y, Z) |
| 87 |
end. |
| 88 |
Next Obligation. Proof. t; fsatz. Qed. |
| 89 |
|
| 90 |
Context (three_b:F) (three_b_correct: three_b = b+b+b). |
| 91 |
Local Notation "4" := (1+1+1+1). Local Notation "27" := (4*4 + 4+4 +1+1+1). |
| 92 |
Context {discriminant_nonzero: id(4*a*a*a + 27*b*b <> 0)}. |
| 93 |
|
| 94 |
Let not_exceptional_y (P Q:point) := |
| 95 |
match W.coordinates (W.add (to_affine P) (to_affine (opp Q))) return Prop with |
| 96 |
| inr _ => True |
| 97 |
| inl (_, y) => y <> 0 |
| 98 |
end. |
| 99 |
|
| 100 |
Global Instance DecidableRel_not_exceptional_y : DecidableRel not_exceptional_y. |
| 101 |
Proof. |
| 102 |
cbv [not_exceptional_y]; intros; break_match; exact _. |
| 103 |
Defined. |
| 104 |
|
| 105 |
Lemma exceptional_P_neq_Q P Q (H:~not_exceptional_y P Q) : |
| 106 |
~ W.eq (to_affine P) (to_affine Q). |
| 107 |
Proof using three_b_correct three_b. |
| 108 |
destruct P as [p ?]; destruct p as [p Z1]; destruct p as [X1 Y1]. |
| 109 |
destruct Q as [q ?]; destruct q as [q Z2]; destruct q as [X2 Y2]. |
| 110 |
cbv [not_exceptional_y opp to_affine] in *; clear not_exceptional_y. |
| 111 |
t; try exact id. |
| 112 |
all: repeat match goal with |
| 113 |
[H:~ _ <> _ |- _ ] => rewrite (not_not (_ = _)) in H |
| 114 |
end. |
| 115 |
all: intro HX; destruct HX; fsatz. |
| 116 |
Qed. |
| 117 |
|
| 118 |
Import BinPos. |
| 119 |
Context {char_ge_21 : @Ring.char_ge F Feq Fzero Fone Fopp Fadd Fsub Fmul 21}. |
| 120 |
Lemma exceptional_2P_eq_2Q P Q (H:~not_exceptional_y P Q) : |
| 121 |
W.eq |
| 122 |
(W.add (to_affine P) (to_affine P)) |
| 123 |
(W.add (to_affine Q) (to_affine Q)). |
| 124 |
Proof using three_b_correct three_b discriminant_nonzero char_ge_21 . |
| 125 |
destruct P as [p ?]; destruct p as [p Z1]; destruct p as [X1 Y1]. |
| 126 |
destruct Q as [q ?]; destruct q as [q Z2]; destruct q as [X2 Y2]. |
| 127 |
cbv [not_exceptional_y opp to_affine] in *; clear not_exceptional_y. |
| 128 |
t. |
| 129 |
par: abstract solve [ fsatz | cbv [id] in * ; fsatz ]. |
| 130 |
Qed. |
| 131 |
|
| 132 |
Definition not_exceptional P Q := |
| 133 |
let p := to_affine P in |
| 134 |
let q := to_affine Q in |
| 135 |
(W.eq (W.add p p) (W.add q q) -> W.eq p q). |
| 136 |
Lemma not_exceptional_y_of_not_exceptional P Q |
| 137 |
: not_exceptional P Q -> not_exceptional_y P Q. |
| 138 |
Proof. |
| 139 |
cbv [not_exceptional]. |
| 140 |
pose proof exceptional_2P_eq_2Q P Q. |
| 141 |
pose proof exceptional_P_neq_Q P Q. |
| 142 |
intro Himpl. |
| 143 |
rewrite <-not_not by typeclasses eauto. |
| 144 |
intuition trivial. |
| 145 |
Qed. |
| 146 |
|
| 147 |
Program Definition add (P Q:point) (except: not_exceptional P Q) : point := |
| 148 |
match proj1_sig P, proj1_sig Q return F*F*F with (X1, Y1, Z1), (X2, Y2, Z2) => |
| 149 |
let t0 := X1*X2 in |
| 150 |
let t1 := Y1*Y2 in |
| 151 |
let t2 := Z1*Z2 in |
| 152 |
let t3 := X1+Y1 in |
| 153 |
let t4 := X2+Y2 in |
| 154 |
let t3 := t3*t4 in |
| 155 |
let t4 := t0+t1 in |
| 156 |
let t3 := t3-t4 in |
| 157 |
let t4 := X1+Z1 in |
| 158 |
let t5 := X2+Z2 in |
| 159 |
let t4 := t4*t5 in |
| 160 |
let t5 := t0+t2 in |
| 161 |
let t4 := t4-t5 in |
| 162 |
let t5 := Y1+Z1 in |
| 163 |
let X3 := Y2+Z2 in |
| 164 |
let t5 := t5*X3 in |
| 165 |
let X3 := t1+t2 in |
| 166 |
let t5 := t5-X3 in |
| 167 |
let Z3 := a*t4 in |
| 168 |
let X3 := three_b*t2 in |
| 169 |
let Z3 := X3+Z3 in |
| 170 |
let X3 := t1-Z3 in |
| 171 |
let Z3 := t1+Z3 in |
| 172 |
let Y3 := X3*Z3 in |
| 173 |
let t1 := t0+t0 in |
| 174 |
let t1 := t1+t0 in |
| 175 |
let t2 := a*t2 in |
| 176 |
let t4 := three_b*t4 in |
| 177 |
let t1 := t1+t2 in |
| 178 |
let t2 := t0-t2 in |
| 179 |
let t2 := a*t2 in |
| 180 |
let t4 := t4+t2 in |
| 181 |
let t0 := t1*t4 in |
| 182 |
let Y3 := Y3+t0 in |
| 183 |
let t0 := t5*t4 in |
| 184 |
let X3 := t3*X3 in |
| 185 |
let X3 := X3-t0 in |
| 186 |
let t0 := t3*t1 in |
| 187 |
let Z3 := t5*Z3 in |
| 188 |
let Z3 := Z3+t0 in |
| 189 |
(X3, Y3, Z3) |
| 190 |
end. |
| 191 |
Next Obligation. |
| 192 |
Proof using three_b three_b_correct discriminant_nonzero char_ge_21. |
| 193 |
apply not_exceptional_y_of_not_exceptional in except. |
| 194 |
cbv [not_exceptional_y to_affine opp] in except. clear not_exceptional_y. |
| 195 |
destruct P as [p ?]; destruct p as [p Z1]; destruct p as [X1 Y1]. |
| 196 |
destruct Q as [q ?]; destruct q as [q Z2]; destruct q as [X2 Y2]. |
| 197 |
t. |
| 198 |
par: abstract solve [ fsatz | cbv [id] in * ; fsatz ]. |
| 199 |
Qed. |
| 200 |
|
| 201 |
Lemma to_affine_add P Q except : |
| 202 |
W.eq |
| 203 |
(to_affine (add P Q except)) |
| 204 |
(WeierstrassCurve.W.add (to_affine P) (to_affine Q)). |
| 205 |
Proof using Type. |
| 206 |
destruct P as [p ?]; destruct p as [p Z1]; destruct p as [X1 Y1]. |
| 207 |
destruct Q as [q ?]; destruct q as [q Z2]; destruct q as [X2 Y2]. |
| 208 |
pose proof (not_exceptional_y_of_not_exceptional _ _ except). |
| 209 |
cbv [not_exceptional_y opp to_affine add] in *; t; |
| 210 |
clear except not_exceptional_y; |
| 211 |
specialize_by_assumption. |
| 212 |
all: try abstract fsatz. |
| 213 |
|
| 214 |
(* zero + P = P -- cases for x and y *) |
| 215 |
{ assert (X1 = 0) by (setoid_subst_rel Feq; Nsatz.nsatz_power 3%nat); t; fsatz. } |
| 216 |
{ assert (X1 = 0) by (setoid_subst_rel Feq; Nsatz.nsatz_power 3%nat); t; fsatz. } |
| 217 |
|
| 218 |
(* P + zero = P -- cases for x and y *) |
| 219 |
{ assert (X2 = 0) by (setoid_subst_rel Feq; Nsatz.nsatz_power 3%nat); t; fsatz. } |
| 220 |
{ assert (X2 = 0) by (setoid_subst_rel Feq; Nsatz.nsatz_power 3%nat); t; fsatz. } |
| 221 |
Qed. |
| 222 |
End Projective. |
| 223 |
End Projective. |