| 1 |
From Coq Require Import Morphisms. |
| 2 |
|
| 3 |
Require Import Crypto.Spec.CompleteEdwardsCurve Crypto.Curves.Edwards.AffineProofs. |
| 4 |
Require Import Crypto.Curves.Edwards.XYZT.Basic. |
| 5 |
|
| 6 |
Require Import Crypto.Algebra.Field. |
| 7 |
Require Import Crypto.Algebra.Hierarchy. |
| 8 |
Require Import Crypto.Util.Notations Crypto.Util.GlobalSettings. |
| 9 |
Require Export Crypto.Util.FixCoqMistakes. |
| 10 |
Require Import Crypto.Util.Decidable. |
| 11 |
Require Import Crypto.Util.Prod. |
| 12 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 13 |
Require Import Crypto.Util.Tactics.DestructHead. |
| 14 |
Require Import Crypto.Util.Tactics.UniquePose. |
| 15 |
|
| 16 |
Section ExtendedCoordinates. |
| 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 |
{char_ge_3 : @Ring.char_ge F Feq Fzero Fone Fopp Fadd Fsub Fmul (BinNat.N.succ_pos BinNat.N.two)} |
| 20 |
{Feq_dec:DecidableRel Feq}. |
| 21 |
|
| 22 |
Local Infix "=" := Feq : type_scope. Local Notation "a <> b" := (not (a = b)) : type_scope. |
| 23 |
Local Notation "0" := Fzero. Local Notation "1" := Fone. Local Notation "2" := (Fadd 1 1). |
| 24 |
Local Infix "+" := Fadd. Local Infix "*" := Fmul. |
| 25 |
Local Infix "-" := Fsub. Local Infix "/" := Fdiv. |
| 26 |
Local Notation "x ^ 2" := (x*x). |
| 27 |
|
| 28 |
Context {a d: F} |
| 29 |
{nonzero_a : a <> 0} |
| 30 |
{square_a : exists sqrt_a, sqrt_a^2 = a} |
| 31 |
{nonsquare_d : forall x, x^2 <> d}. |
| 32 |
Local Notation Epoint := (@E.point F Feq Fone Fadd Fmul a d). |
| 33 |
Local Notation Eadd := (E.add(nonzero_a:=nonzero_a)(square_a:=square_a)(nonsquare_d:=nonsquare_d)). |
| 34 |
|
| 35 |
Local Notation onCurve x y := (a*x^2 + y^2 = 1 + d*x^2*y^2) (only parsing). |
| 36 |
|
| 37 |
(* Match this context to the Basic.ExtendedCoordinates context *) |
| 38 |
Local Notation point := (Extended.point(F:=F)(Feq:=Feq)(Fzero:=Fzero)(Fadd:=Fadd)(Fmul:=Fmul)(a:=a)(d:=d)). |
| 39 |
Context {a_eq_minus1:a = Fopp 1} {twice_d} {k_eq_2d:twice_d = d+d} {nonzero_d: d<>0}. |
| 40 |
Local Notation m1add := (Extended.m1add(F:=F)(Feq:=Feq)(Fzero:=Fzero)(Fone:=Fone)(Fopp:=Fopp)(Fadd:=Fadd)(Fsub:=Fsub)(Fmul:=Fmul)(Finv:=Finv)(Fdiv:=Fdiv)(field:=field)(char_ge_3:=char_ge_3)(Feq_dec:=Feq_dec)(a:=a)(d:=d)(nonzero_a:=nonzero_a)(square_a:=square_a)(nonsquare_d:=nonsquare_d)(a_eq_minus1:=a_eq_minus1)(twice_d:=twice_d)(k_eq_2d:=k_eq_2d)). |
| 41 |
|
| 42 |
(* Define a new cached point type *) |
| 43 |
Definition cached := |
| 44 |
{ C | let '(half_YmX,half_YpX,Z,Td) := C in |
| 45 |
let X := half_YpX - half_YmX in |
| 46 |
let Y := half_YpX + half_YmX in |
| 47 |
let T := Td / d in |
| 48 |
a * X^2*Z^2 + Y^2*Z^2 = (Z^2)^2 + d * X^2 * Y^2 |
| 49 |
/\ X * Y = Z * T |
| 50 |
/\ Z <> 0 }. |
| 51 |
Definition cached_coordinates (C:cached) : F*F*F*F := proj1_sig C. |
| 52 |
Definition cached_eq (C1 C2:cached) := |
| 53 |
let '(hYmX1, hYpX1, Z1, _) := cached_coordinates C1 in |
| 54 |
let '(hYmX2, hYpX2, Z2, _) := cached_coordinates C2 in |
| 55 |
Z2*(hYpX1-hYmX1) = Z1*(hYpX2-hYmX2) /\ Z2*(hYpX1+hYmX1) = Z1*(hYpX2+hYmX2). |
| 56 |
|
| 57 |
Local Ltac t_step := |
| 58 |
match goal with |
| 59 |
| |- Proper _ _ => intro |
| 60 |
| _ => progress intros |
| 61 |
| _ => progress destruct_head' prod |
| 62 |
| _ => progress destruct_head' @E.point |
| 63 |
| _ => progress destruct_head' @Extended.point |
| 64 |
| _ => progress destruct_head' @cached |
| 65 |
| _ => progress destruct_head' and |
| 66 |
| _ => progress cbv [Extended.eq CompleteEdwardsCurve.E.eq E.eq E.zero E.add E.opp fst snd cached_coordinates E.coordinates proj1_sig] in * |
| 67 |
| |- _ /\ _ => split | |- _ <-> _ => split |
| 68 |
end. |
| 69 |
Local Ltac t := repeat t_step; try Field.fsatz. |
| 70 |
|
| 71 |
Program Definition cached_to_twisted (C:cached) : Epoint := |
| 72 |
let MPZT := cached_coordinates C in |
| 73 |
let Td := snd MPZT in |
| 74 |
let MPZ := fst MPZT in |
| 75 |
let Z := snd MPZ in |
| 76 |
let MP := fst MPZ in |
| 77 |
let hYpX := snd MP in |
| 78 |
let hYmX := fst MP in |
| 79 |
let iZ := Finv Z in (((hYpX-hYmX)*iZ), ((hYpX+hYmX)*iZ)). |
| 80 |
Next Obligation. t. Qed. |
| 81 |
|
| 82 |
Program Definition m1_prep (P : point) : cached := |
| 83 |
match proj1_sig P return F*F*F*F with |
| 84 |
(X, Y, Z, Ta, Tb) => |
| 85 |
let half_YmX := (Y-X)/2 in |
| 86 |
let half_YpX := (Y+X)/2 in |
| 87 |
let Td := Ta*Tb*d in |
| 88 |
(half_YmX, half_YpX, Z, Td) |
| 89 |
end. |
| 90 |
Next Obligation. t. Qed. |
| 91 |
|
| 92 |
Lemma to_affine_denom_nonzero (P1 : point) (P2: cached) : |
| 93 |
let '(X1, Y1, Z1, _, _) := proj1_sig P1 in |
| 94 |
let '(half_YmX, half_YpX, Z2, _) := proj1_sig P2 in |
| 95 |
(d * (X1 * Finv Z1) * ((half_YpX-half_YmX)*(Finv Z2)) * (Y1 * Finv Z1) * ((half_YpX+half_YmX)*(Finv Z2)))^2 <> 1. |
| 96 |
Proof. |
| 97 |
destruct P1 as (((((?X & ?Y) & ?Z) & ?Ta) & ?Tb) & ?HP). |
| 98 |
destruct P2 as ((((?half_YmX & ?half_YpX) & ?Z) & ?T) & ?HP). |
| 99 |
unshelve epose proof |
| 100 |
(E.denominator_nonzero _ nonzero_a square_a _ nonsquare_d |
| 101 |
(X * Finv Z) (Y * Finv Z) _ ((half_YpX-half_YmX)*(Finv Z0)) ((half_YpX+half_YmX)*(Finv Z0)) _); t. |
| 102 |
Qed. |
| 103 |
|
| 104 |
Program Definition m1_readd |
| 105 |
(P1 : point) (C : cached) : point := |
| 106 |
match proj1_sig P1, C return F*F*F*F*F with |
| 107 |
(X1, Y1, Z1, Ta1, Tb1), (half_YmX, half_YpX, Z2, Td) => |
| 108 |
let A := (Y1-X1)*half_YmX in |
| 109 |
let B := (Y1+X1)*half_YpX in |
| 110 |
let C := (Ta1*Tb1)*Td in |
| 111 |
let D := Z1*Z2 in |
| 112 |
let E := B-A in |
| 113 |
let F := D-C in |
| 114 |
let G := D+C in |
| 115 |
let H := B+A in |
| 116 |
let X3 := E*F in |
| 117 |
let Y3 := G*H in |
| 118 |
let Z3 := F*G in |
| 119 |
(X3, Y3, Z3, E, H) |
| 120 |
end. |
| 121 |
Next Obligation. |
| 122 |
pose proof (to_affine_denom_nonzero P1 C). |
| 123 |
t. Qed. |
| 124 |
|
| 125 |
Create HintDb points_as_coordinates discriminated. |
| 126 |
Hint Unfold Extended.point proj1_sig Extended.m1add |
| 127 |
E.point E.coordinates m1_readd m1_prep : points_as_coordinates. |
| 128 |
Lemma readd_correct P Q : |
| 129 |
Extended.eq (m1_readd P (m1_prep Q)) (m1add P Q). |
| 130 |
Proof. cbv [m1add m1_readd m1_prep]; t. Qed. |
| 131 |
|
| 132 |
End ExtendedCoordinates. |