| 1 |
From Coq Require Import Morphisms. |
| 2 |
|
| 3 |
Require Import Crypto.Spec.CompleteEdwardsCurve Crypto.Curves.Edwards.AffineProofs. |
| 4 |
|
| 5 |
Require Import Crypto.Util.Notations Crypto.Util.GlobalSettings. |
| 6 |
Require Export Crypto.Util.FixCoqMistakes. |
| 7 |
Require Import Crypto.Util.Decidable. |
| 8 |
Require Import Crypto.Util.Tactics.DestructHead. |
| 9 |
Require Import Crypto.Util.Tactics.UniquePose. |
| 10 |
|
| 11 |
Module Extended. |
| 12 |
Section ExtendedProjective. |
| 13 |
Context {F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv} |
| 14 |
{field:@Algebra.Hierarchy.field F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv} |
| 15 |
{char_ge_3 : @Ring.char_ge F Feq Fzero Fone Fopp Fadd Fsub Fmul (BinNat.N.succ_pos BinNat.N.two)} |
| 16 |
{Feq_dec:DecidableRel Feq}. |
| 17 |
Local Infix "=" := Feq : type_scope. Local Notation "a <> b" := (not (a = b)) : type_scope. |
| 18 |
Local Notation "0" := Fzero. Local Notation "1" := Fone. |
| 19 |
Local Infix "+" := Fadd. Local Infix "*" := Fmul. |
| 20 |
Local Infix "-" := Fsub. Local Infix "/" := Fdiv. |
| 21 |
Local Notation "x ^ 2" := (x*x). |
| 22 |
|
| 23 |
Context {a d: F} |
| 24 |
{nonzero_a : a <> 0} |
| 25 |
{square_a : exists sqrt_a, sqrt_a^2 = a} |
| 26 |
{nonsquare_d : forall x, x^2 <> d}. |
| 27 |
Local Notation affine_point := (@E.point F Feq Fone Fadd Fmul a d). |
| 28 |
Local Notation affine_add := (E.add(nonzero_a:=nonzero_a)(square_a:=square_a)(nonsquare_d:=nonsquare_d)). |
| 29 |
|
| 30 |
(** [Extended.point] represents a point on an elliptic curve using extended projective Edwards coordinates. |
| 31 |
* See <https://eprint.iacr.org/2008/522.pdf> and <https://www.shiftleft.org/papers/fff/fff.pdf>. |
| 32 |
*) |
| 33 |
Definition point := { P | let '(X,Y,Z,Ta,Tb) := P in |
| 34 |
a * X^2*Z^2 + Y^2*Z^2 = (Z^2)^2 + d * X^2 * Y^2 |
| 35 |
/\ X * Y = Z * Ta * Tb |
| 36 |
/\ Z <> 0 }. |
| 37 |
Definition eq (P1 P2:point) := |
| 38 |
let '(X1, Y1, Z1, _, _) := proj1_sig P1 in |
| 39 |
let '(X2, Y2, Z2, _, _) := proj1_sig P2 in |
| 40 |
Z2*X1 = Z1*X2 /\ Z2*Y1 = Z1*Y2. |
| 41 |
|
| 42 |
Ltac t_step := |
| 43 |
match goal with |
| 44 |
| |- Proper _ _ => intro |
| 45 |
| _ => progress intros |
| 46 |
| _ => progress destruct_head' prod |
| 47 |
| _ => progress destruct_head' @E.point |
| 48 |
| _ => progress destruct_head' point |
| 49 |
| _ => progress destruct_head' and |
| 50 |
| _ => progress (Prod.inversion_prod; subst) |
| 51 |
| _ => progress cbv [eq CompleteEdwardsCurve.E.eq E.eq E.zero E.add E.opp fst snd E.coordinates proj1_sig proj2_sig] in * |
| 52 |
| |- _ /\ _ => split | |- _ <-> _ => split |
| 53 |
end. |
| 54 |
Ltac t := repeat t_step; try Field.fsatz. |
| 55 |
|
| 56 |
Global Instance Equivalence_eq : Equivalence eq. |
| 57 |
Proof using Feq_dec field nonzero_a. split; repeat intro; t. Qed. |
| 58 |
Global Instance DecidableRel_eq : Decidable.DecidableRel eq. |
| 59 |
Proof. intros P Q; destruct P as [ [ [ [ ] ? ] ? ] ?], Q as [ [ [ [ ] ? ] ? ] ? ]; exact _. Defined. |
| 60 |
|
| 61 |
Definition from_affine (P : affine_point) : point. |
| 62 |
refine ( exist _ (let '(x, y) := proj1_sig P in (x, y, 1, x, y)) _). |
| 63 |
abstract t. Defined. |
| 64 |
|
| 65 |
Global Instance Proper_from_affine : Proper (E.eq==>eq) from_affine. |
| 66 |
Proof using Type. cbv [from_affine]; t. Qed. |
| 67 |
|
| 68 |
Definition to_affine (P : point) : affine_point. |
| 69 |
refine ( exist _ (let '(X,Y,Z,Ta,Tb) := proj1_sig P in |
| 70 |
let iZ := Finv Z in (X*iZ, Y*iZ))_ ). abstract t. Defined. |
| 71 |
Global Instance Proper_to_affine : Proper (eq==>E.eq) to_affine. |
| 72 |
Proof using Type. cbv [to_affine]; t. Qed. |
| 73 |
|
| 74 |
Local Ltac destruct_point P := |
| 75 |
destruct P as (((((?X & ?Y) & ?Z) & ?Ta) & ?Tb) & ?HP). |
| 76 |
Lemma to_affine_denom_nonzero (P1 P2 : point) : |
| 77 |
let '(X1, Y1, Z1, _, _) := proj1_sig P1 in |
| 78 |
let '(X2, Y2, Z2, _, _) := proj1_sig P2 in |
| 79 |
(d * (X1 * Finv Z1) * (X2 * Finv Z2) * (Y1 * Finv Z1) * (Y2 * Finv Z2))^2 <> 1. |
| 80 |
Proof. |
| 81 |
destruct_point P1. destruct_point P2. |
| 82 |
unshelve epose proof |
| 83 |
(E.denominator_nonzero _ nonzero_a square_a _ nonsquare_d |
| 84 |
(X * Finv Z) (Y * Finv Z) _ (X0 * Finv Z0) (Y0 * Finv Z0) _); t. |
| 85 |
Qed. |
| 86 |
|
| 87 |
Lemma to_affine_from_affine P : E.eq (to_affine (from_affine P)) P. |
| 88 |
Proof using Type. cbv [to_affine from_affine]; t. Qed. |
| 89 |
Lemma from_affine_to_affine P : eq (from_affine (to_affine P)) P. |
| 90 |
Proof using Type. cbv [to_affine from_affine]; t. Qed. |
| 91 |
|
| 92 |
Definition zero : point. refine (exist _ (0, 1, 1, 0, 1) _). abstract t. Defined. |
| 93 |
|
| 94 |
Definition opp (P : point) : point. refine (exist _ ( |
| 95 |
let '(X, Y, Z, Ta, Tb) := proj1_sig P in (Fopp X, Y, Z, Fopp Ta, Tb)) _). |
| 96 |
abstract t. Defined. |
| 97 |
|
| 98 |
Section AMinusOne. |
| 99 |
Context {a_eq_minus1:a = Fopp 1} {twice_d} {k_eq_2d:twice_d = d+d}. |
| 100 |
Definition m1add (P1 P2:point) : point. |
| 101 |
refine (exist _ ( |
| 102 |
let '(X1, Y1, Z1, Ta1, Tb1) := proj1_sig P1 in |
| 103 |
let '(X2, Y2, Z2, Ta2, Tb2) := proj1_sig P2 in |
| 104 |
let A := (Y1-X1)*(Y2-X2) in |
| 105 |
let B := (Y1+X1)*(Y2+X2) in |
| 106 |
let C := (Ta1*Tb1)*twice_d*(Ta2*Tb2) in |
| 107 |
let D := Z1*(Z2+Z2) in |
| 108 |
let E := B-A in |
| 109 |
let F := D-C in |
| 110 |
let G := D+C in |
| 111 |
let H := B+A in |
| 112 |
let X3 := E*F in |
| 113 |
let Y3 := G*H in |
| 114 |
let Z3 := F*G in |
| 115 |
(X3, Y3, Z3, E, H)) _). |
| 116 |
abstract (pose proof (to_affine_denom_nonzero P1 P2); t). |
| 117 |
Defined. |
| 118 |
|
| 119 |
Lemma to_affine_m1add P Q : E.eq (to_affine (m1add P Q)) (affine_add (to_affine P) (to_affine Q)). |
| 120 |
Proof. |
| 121 |
pose proof (to_affine_denom_nonzero P Q). cbv [proj1_sig to_affine m1add] in *. |
| 122 |
t. |
| 123 |
Qed. |
| 124 |
|
| 125 |
Global Instance isomorphic_commutative_group_m1 : |
| 126 |
@Group.isomorphic_commutative_groups |
| 127 |
affine_point E.eq |
| 128 |
affine_add |
| 129 |
(E.zero(nonzero_a:=nonzero_a)) |
| 130 |
(E.opp(nonzero_a:=nonzero_a)) |
| 131 |
point eq m1add zero opp |
| 132 |
from_affine to_affine. |
| 133 |
Proof. |
| 134 |
eapply Group.commutative_group_by_isomorphism; |
| 135 |
[ (exact _) | (apply to_affine_from_affine) | |(apply to_affine_m1add) | | ]; |
| 136 |
solve [cbv [to_affine zero opp m1add]; t]. |
| 137 |
Qed. |
| 138 |
|
| 139 |
Definition m1double (P : point) : point. |
| 140 |
refine (exist _ ( |
| 141 |
let '(X, Y, Z, _, _) := proj1_sig P in |
| 142 |
let trX := X^2 in |
| 143 |
let trZ := Y^2 in |
| 144 |
let trT := (let t0 := Z^2 in t0+t0) in |
| 145 |
let rY := X+Y in |
| 146 |
let t0 := rY^2 in |
| 147 |
let cY := trZ+trX in |
| 148 |
let cZ := trZ-trX in |
| 149 |
let cX := t0-cY in |
| 150 |
let cT := trT-cZ in |
| 151 |
let X3 := cX*cT in |
| 152 |
let Y3 := cY*cZ in |
| 153 |
let Z3 := cZ*cT in |
| 154 |
(X3, Y3, Z3, cX, cY)) _). |
| 155 |
Proof. abstract (pose proof (to_affine_denom_nonzero P P); t). Defined. |
| 156 |
|
| 157 |
Lemma m1double_correct P : eq (m1double P) (m1add P P). |
| 158 |
Proof. intros; progress destruct_head' @point; cbv [m1add m1double]; t. Qed. |
| 159 |
|
| 160 |
Lemma to_affine_m1double P : E.eq (to_affine (m1double P)) (affine_add (to_affine P) (to_affine P)). |
| 161 |
Proof. setoid_rewrite m1double_correct; trivial. eapply to_affine_m1add. Qed. |
| 162 |
End AMinusOne. |
| 163 |
|
| 164 |
(* https://www.hyperelliptic.org/EFD/g1p/auto-twisted-extended-1.html#doubling-double-2008-hwcd *) |
| 165 |
Definition double (P : point) : point. |
| 166 |
refine (exist _ ( |
| 167 |
let '(X, Y, Z, Ta, Tb) := proj1_sig P in |
| 168 |
let A := X^2 in |
| 169 |
let B := Y^2 in |
| 170 |
let t0 := Z^2 in |
| 171 |
let C := t0+t0 in |
| 172 |
let D := a*A in |
| 173 |
let t1 := X+Y in |
| 174 |
let t2 := t1^2 in |
| 175 |
let t3 := t2-A in |
| 176 |
let E := t3-B in |
| 177 |
let G := D+B in |
| 178 |
let F := G-C in |
| 179 |
let H := D-B in |
| 180 |
let X3 := E*F in |
| 181 |
let Y3 := G*H in |
| 182 |
let Z3 := F*G in |
| 183 |
(X3, Y3, Z3, E, H)) _ ). |
| 184 |
Proof. abstract (pose proof (to_affine_denom_nonzero P P); t). Defined. |
| 185 |
|
| 186 |
Lemma to_affine_double P : E.eq (to_affine (double P)) (affine_add (to_affine P) (to_affine P)). |
| 187 |
Proof. |
| 188 |
cbv beta delta [double]. |
| 189 |
pose proof (to_affine_denom_nonzero P P); cbv [E.add double to_affine]; t. |
| 190 |
Qed. |
| 191 |
End ExtendedProjective. |
| 192 |
End Extended. |