Debian Codegraph @

src/Curves/Edwards/XYZT/Precomputed.v

raw
1 Require Import Crypto.Util.Decidable Crypto.Util.Notations Crypto.Algebra.Hierarchy.
2
3 Require Import Crypto.Spec.CompleteEdwardsCurve Crypto.Curves.Edwards.XYZT.Basic.
4 From Coq Require Import Morphisms.
5
6 Require Import Crypto.Util.Tactics.DestructHead.
7 Require Import Crypto.Util.Tactics.BreakMatch.
8 Require Import Crypto.Util.Prod.
9 Require Import Crypto.Algebra.Field.
10
11 Section ExtendedCoordinates.
12 Context {F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv}
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
17 Local Infix "=" := Feq : type_scope. Local Notation "a <> b" := (not (a = b)) : type_scope.
18 Local Notation "0" := Fzero. Local Notation "1" := Fone. Local Notation "2" := (Fadd 1 1).
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 Epoint := (@E.point F Feq Fone Fadd Fmul a d).
28 Local Notation onCurve x y := (a*x^2 + y^2 = 1 + d*x^2*y^2) (only parsing).
29 Local Notation point := (Extended.point (Feq:=Feq)(Fzero:=Fzero)(Fadd:=Fadd)(Fmul:=Fmul)(a:=a)(d:=d)).
30
31 Definition precomputed_point := { P | let '(half_ypx, half_ymx, xyd) := P in
32 let x := half_ypx - half_ymx in
33 let y := half_ypx + half_ymx in
34 onCurve x y /\ xyd = x * y * d}.
35 Definition precomputed_coordinates (P:precomputed_point) : F*F*F := proj1_sig P.
36 Definition eq (P1 P2:precomputed_point) :=
37 let '(half_ypx1, half_ymx1, _) := precomputed_coordinates P1 in
38 let '(half_ypx2, half_ymx2, _) := precomputed_coordinates P2 in
39 half_ypx1 = half_ypx2 /\ half_ymx1 = half_ymx2.
40
41 Local Ltac t_step :=
42 match goal with
43 | |- Proper _ _ => intro
44 | _ => progress intros
45 | _ => progress (Prod.inversion_prod; subst)
46 | _ => progress destruct_head' prod
47 | _ => progress destruct_head' @E.point
48 | _ => progress destruct_head' Extended.point
49 | _ => progress destruct_head' @precomputed_point
50 | _ => progress destruct_head' and
51 | _ => progress cbv beta match zeta delta [eq CompleteEdwardsCurve.E.eq Extended.eq E.eq E.zero E.add fst snd precomputed_coordinates E.coordinates proj1_sig point] in *
52 | |- _ /\ _ => split | |- _ <-> _ => split
53 end.
54 Local Ltac t := repeat t_step; try Field.fsatz.
55
56 Definition of_twisted (P:Epoint) : precomputed_point.
57 refine ( exist _ (
58 let '(x, y) := E.coordinates P in
59 ((y+x)/2, (y-x)/2, x*y*d)) _). abstract t. Defined.
60 Global Instance Proper_from_twisted : Proper (E.eq==>eq) of_twisted.
61 Proof using Type. cbv [of_twisted]; t. Qed.
62
63 Definition to_twisted (P: precomputed_point) : Epoint.
64 refine ( exist _ (
65 let '(half_ypx, half_ymx, _) := precomputed_coordinates P in
66 (half_ypx - half_ymx, half_ypx + half_ymx)) _). abstract t. Defined.
67 Global Instance Proper_to_twisted : Proper (eq==>E.eq) to_twisted.
68 Proof using Type. cbv [to_twisted]; t. Qed.
69
70 Definition to_projective (P: precomputed_point) : point.
71 refine ( exist _ (
72 let '(half_ypx, half_ymx, _) := precomputed_coordinates P in
73 (half_ypx - half_ymx, half_ypx + half_ymx, 1,
74 half_ypx - half_ymx, half_ypx + half_ymx)) _ ). abstract t. Defined.
75 Global Instance Proper_to_projective : Proper (eq==>(Extended.eq (Feq:=Feq))) to_projective.
76 Proof using Type. cbv [to_projective]; t. Qed.
77
78 Definition from_projective (P:point) : precomputed_point.
79 refine ( exist _ (
80 let '(X, Y, Z, _, _) := proj1_sig P in
81 let iZ := Finv Z in
82 let x := X * iZ in
83 let y := Y * iZ in
84 ((y+x)/2, (y-x)/2, x*y*d)) _ ). abstract t. Defined.
85 Global Instance Proper_from_projective: Proper (Extended.eq (Feq:=Feq)==>eq) from_projective.
86 Proof using Type. cbv [from_projective]; t. Qed.
87
88 Global Instance Equivalence_eq : Equivalence eq.
89 Proof using Feq_dec field nonzero_a. split; repeat intro; t. Qed.
90 Global Instance DecidableRel_eq : Decidable.DecidableRel eq.
91 Proof. intros P Q. destruct P as [ [ [ ] ? ] ? ], Q as [ [ [ ] ? ] ? ]; exact _. Defined.
92
93 Section TwistMinusOne.
94 Context {a_eq_minus1:a = Fopp 1}.
95
96 (* https://hyperelliptic.org/EFD/g1p/data/twisted/extended-1/addition/madd-2008-hwcd-3,
97 but with halved precomputed coordinates (making D unnecessary) *)
98 Definition m1add_precomputed_coordinates (P: point) (Q:precomputed_point) : point.
99 refine ( exist _ (
100 let '(X1, Y1, Z1, Ta, Tb) := proj1_sig P in
101 let '(half_ypx, half_ymx, xyd) := proj1_sig Q in
102 let YpX1 := Y1+X1 in
103 let YmX1 := Y1-X1 in
104 let A := YmX1*half_ymx in
105 let B := YpX1*half_ypx in
106 let T1 := Ta*Tb in
107 let C := xyd*T1 in (* = T1*2d*T2, since Z2=1 so T2=X2*Y2 *)
108 let E := B-A in
109 let F := Z1-C in
110 let G := Z1+C in
111 let H := B+A in
112 (* X/Z, Y/Z = x, y *)
113 let X3 := E*F in
114 let Y3 := G*H in
115 let Z3 := F*G in
116 (* Leave T split in two parts *)
117 (X3, Y3, Z3, E, H)) _ ). abstract (t; (* The last goal boils down to completeness of addition on edwards curves. *)
118 pose proof Pre.denominator_nonzero a nonzero_a square_a d nonsquare_d _ _ H as H_nz; intro;
119 eapply (H_nz (f2 * Finv f1)(f3 * Finv f1)); t). Defined.
120
121 Create HintDb points_as_coordinates discriminated.
122 Hint Unfold Extended.point proj1_sig Extended.from_affine Extended.m1add
123 E.point E.coordinates m1add_precomputed_coordinates of_twisted precomputed_point : points_as_coordinates.
124 Local Notation m1add := (Extended.m1add(nonzero_a:=nonzero_a)(square_a:=square_a)(a_eq_minus1:=a_eq_minus1)(nonsquare_d:=nonsquare_d)(k_eq_2d:=reflexivity _)).
125 Local Notation XYZT_of_twisted := (Extended.from_affine(nonzero_a:=nonzero_a)(d:=d)).
126 Lemma m1add_precomputed_coordinates_correct P Q :
127 let '(X1, Y1, Z1, Ta1, Tb1) := proj1_sig (m1add_precomputed_coordinates (P) (of_twisted Q)) in
128 let '(X2, Y2, Z2, _, _) := proj1_sig (m1add P (XYZT_of_twisted Q)) in
129 Z2*X1 = Z1*X2 /\ Z2*Y1 = Z1*Y2 /\ X1*Y1 = Z1*Ta1*Tb1.
130 Proof. cbv [proj1_sig m1add_precomputed_coordinates of_twisted m1add XYZT_of_twisted]; t.
131 Qed.
132
133 End TwistMinusOne.
134 End ExtendedCoordinates.