Debian Codegraph @

src/Spec/MontgomeryCurve.v

raw
1 Require Crypto.Algebra.Field.
2 Require Crypto.Util.GlobalSettings.
3 Require Crypto.Util.Tactics.DestructHead Crypto.Util.Sum Crypto.Util.Prod.
4
5 Module M.
6 Section MontgomeryCurve.
7 Import BinNat.
8 Context {F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv}
9 {field:@Algebra.Hierarchy.field F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv}
10 {Feq_dec:Decidable.DecidableRel Feq}
11 {char_ge_3:@Ring.char_ge F Feq Fzero Fone Fopp Fadd Fsub Fmul (BinNat.N.succ_pos (BinNat.N.two))}.
12 Local Infix "=" := Feq : type_scope. Local Notation "a <> b" := (not (a = b)) : type_scope.
13 Local Infix "+" := Fadd. Local Infix "*" := Fmul.
14 Local Infix "-" := Fsub. Local Infix "/" := Fdiv.
15 Local Notation "- x" := (Fopp x).
16 Local Notation "x ^ 2" := (x*x) (at level 30).
17 Local Notation "x ^ 3" := (x*x^2) (at level 30).
18 Local Notation "0" := Fzero. Local Notation "1" := Fone.
19 Local Notation "2" := (1+1). Local Notation "3" := (1+2).
20 Local Notation "'∞'" := unit : type_scope.
21 Local Notation "'∞'" := (inr tt) : core_scope.
22 Local Notation "( x , y )" := (inl (pair x y)).
23 Local Open Scope core_scope.
24
25 Context {a b: F} {b_nonzero:b <> 0}.
26
27 Definition point := { P : F*F+∞ | match P with
28 | (x, y) => b*y^2 = x^3 + a*x^2 + x
29 | ∞ => True
30 end }.
31 Definition coordinates (P:point) : (F*F + ∞) := let (xyi, _) := P in xyi.
32
33 Program Definition zero : point := ∞.
34
35 Definition eq (P1 P2:point) :=
36 match coordinates P1, coordinates P2 with
37 | (x1, y1), (x2, y2) => x1 = x2 /\ y1 = y2
38 | ∞, ∞ => True
39 | _, _ => False
40 end.
41
42 Program Definition add (P1 P2:point) : point :=
43 match coordinates P1, coordinates P2 return F*F+∞ with
44 (x1, y1), (x2, y2) =>
45 if Decidable.dec (x1 = x2)
46 then if Decidable.dec (y1 = - y2)
47 then ∞
48 else let k := (3*x1^2 + 2*a*x1 + 1)/(2*b*y1) in
49 let x := b*k^2 - a - x1 - x2 in
50 let y := (2*x1 + x2 + a)*k - b*k^3 - y1 in
51 (x, y)
52 else let k := (y2 - y1)/(x2-x1) in
53 let x := b*k^2 - a - x1 - x2 in
54 let y := (2*x1 + x2 + a)*k - b*k^3 - y1 in
55 (x, y)
56 | ∞, ∞ => ∞
57 | ∞, _ => coordinates P2
58 | _, ∞ => coordinates P1
59 end.
60 Next Obligation.
61 Proof.
62 cbv [coordinates]; BreakMatch.break_match; trivial; Field.fsatz.
63 Qed.
64
65 Program Definition opp (P : point) : point :=
66 match P return F*F+∞ with
67 | (x, y) => (x, -y)
68 | ∞ => ∞
69 end.
70 Next Obligation.
71 Proof.
72 DestructHead.destruct_head @point; cbv; BreakMatch.break_match; trivial; Field.fsatz.
73 Qed.
74
75 Definition X0 (P : point) : F :=
76 match coordinates P with
77 | (x, y) => x
78 | _ => Fzero
79 end.
80 End MontgomeryCurve.
81 End M.