Debian Codegraph @

src/Curves/Weierstrass/Affine.v

raw
1 Require Import Crypto.Spec.WeierstrassCurve.
2 Require Import Crypto.Algebra.Field.
3 Require Import Crypto.Util.Decidable Crypto.Util.Tactics.DestructHead Crypto.Util.Tactics.BreakMatch.
4 Require Import Crypto.Util.Tactics.SetoidSubst.
5 Import RelationClasses Morphisms.
6
7 Module W.
8 Section W.
9 Context {F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv} {a b:F}
10 {field:@Algebra.Hierarchy.field F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv}
11 {Feq_dec:DecidableRel Feq}.
12 Local Infix "+" := Fadd. Local Infix "-" := Fsub.
13 Local Infix "*" := Fmul. Local Infix "/" := Fdiv.
14 Local Notation "x ^ 2" := (x*x) (at level 30).
15 Local Notation point := (@W.point F Feq Fadd Fmul a b).
16
17 Definition opp (P : point) : point. refine (exist _ (
18 match W.coordinates P with
19 | inl (x1, y1) => inl (x1, Fopp y1)
20 | inr tt => inr tt
21 end) _).
22 Proof. abstract (cbv [W.coordinates]; break_match; trivial; fsatz). Defined.
23
24 Global Instance Equivalence_eq : Equivalence (@W.eq _ Feq Fadd Fmul a b).
25 Proof.
26 cbv [W.eq W.coordinates]; split; repeat intros [ [ []|[] ] ?]; intuition try solve
27 [contradiction | apply reflexivity | apply symmetry; trivial | eapply transitivity; eauto 1].
28 Qed.
29
30 Global Instance Proper_opp : Proper (W.eq ==> W.eq) opp.
31 Proof.
32 repeat (intros [ [[]|[] ]?] || intro); cbv [W.coordinates opp W.eq] in *;
33 repeat (try destruct_head' @and; try case dec as []; try contradiction; try split); trivial.
34 setoid_subst_rel Feq; reflexivity.
35 Qed.
36
37 (* Weierstraß Elliptic Curves and Side-Channel Attacks
38 by Eric Brier and Marc Joye, 2002 *)
39 Definition add' (P1 P2 : point) : point. refine (exist _
40 match W.coordinates P1, W.coordinates P2 with
41 | inl (x1, y1), inl (x2, y2) =>
42 if dec (Feq y1 (Fopp y2)) then
43 if dec (Feq x1 x2) then inr tt
44 else let k := (y2-y1)/(x2-x1) in
45 let x3 := k^2-x1-x2 in
46 let y3 := k*(x1-x3)-y1 in
47 inl (x3, y3)
48 else let k := ((x1^2 + x1*x2 + x2^2 + a)/(y1+y2)) in
49 let x3 := k^2-x1-x2 in
50 let y3 := k*(x1-x3)-y1 in
51 inl (x3, y3)
52 | inr tt, inr tt => inr tt
53 | inr tt, _ => W.coordinates P2
54 | _, inr tt => W.coordinates P1
55 end _).
56 Proof. abstract (cbv [W.coordinates]; break_match; trivial; fsatz). Defined.
57
58 Lemma add'_correct char_ge_3 : forall P Q : point, W.eq (W.add' P Q) (W.add(char_ge_3:=char_ge_3) P Q).
59 Proof. intros [ [[]|]?] [ [[]|]?]; cbv [W.coordinates W.add W.add' W.eq]; break_match; try split; try fsatz. Qed.
60
61 Global Instance Proper_add' : Proper (W.eq ==> W.eq ==> W.eq) add'.
62 Proof.
63 repeat (intros [ [[]|[] ]?] || intro); cbv [W.coordinates W.add' W.eq] in *;
64 repeat (try destruct_head' @and; try case dec as []; try contradiction; try split); trivial.
65 Time par : fsatz. (* setoid_subst_rel is slower *)
66 Qed.
67
68 Global Instance Proper_add {char_ge_3} :
69 Proper (W.eq ==> W.eq ==> W.eq) (@W.add _ Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv _ _ char_ge_3 a b).
70 Proof. repeat intro. rewrite <-2add'_correct. apply Proper_add'; trivial. Qed.
71 End W.
72 End W.