Debian Codegraph @

src/Spec/WeierstrassCurve.v

raw
1 Require Crypto.Algebra.Field.
2
3 Module W.
4 Section WeierstrassCurves.
5 (* Short Weierstrass curves with addition laws. References:
6 * <https://hyperelliptic.org/EFD/g1p/auto-shortw.html>
7 * <https://cr.yp.to/talks/2007.06.07/slides.pdf>
8 * See also:
9 * <http://cs.ucsb.edu/~koc/ccs130h/2013/EllipticHyperelliptic-CohenFrey.pdf> (page 79)
10 *)
11
12 Context {F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv} {field:@Algebra.Hierarchy.field F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv} {Feq_dec:Decidable.DecidableRel Feq} {char_ge_3:@Ring.char_ge F Feq Fzero Fone Fopp Fadd Fsub Fmul (BinNat.N.succ_pos (BinNat.N.two))}.
13 Local Infix "=" := Feq : type_scope. Local Notation "a <> b" := (not (a = b)) : type_scope.
14 Local Notation "x =? y" := (Decidable.dec (Feq x y)) (at level 70, no associativity) : type_scope.
15 Local Notation "x =? y" := (Sumbool.bool_of_sumbool (Decidable.dec (Feq x y))) : bool_scope.
16 Local Infix "+" := Fadd. Local Infix "*" := Fmul.
17 Local Infix "-" := Fsub. Local Infix "/" := Fdiv.
18 Local Notation "- x" := (Fopp x).
19 Local Notation "x ^ 2" := (x*x) (at level 30). Local Notation "x ^ 3" := (x*x^2) (at level 30).
20 Local Notation "'∞'" := unit : type_scope.
21 Local Notation "'∞'" := (inr tt) : core_scope.
22
23 Local Notation "( x , y )" := (inl (pair x y)).
24 Local Open Scope core_scope.
25
26 Context {a b: F}.
27
28 Definition point := { P | match P with
29 | (x, y) => y^2 = x^3 + a*x + b
30 | ∞ => True
31 end }.
32 Definition coordinates (P:point) : F*F + ∞ := let (xyi,_) := P in xyi.
33
34 Definition eq (P1 P2:point) :=
35 match coordinates P1, coordinates P2 with
36 | (x1, y1), (x2, y2) => x1 = x2 /\ y1 = y2
37 | ∞, ∞ => True
38 | _, _ => False
39 end.
40
41 Program Definition zero : point := ∞.
42
43 Local Notation "0" := Fzero. Local Notation "1" := Fone.
44 Local Notation "2" := (1+1). Local Notation "3" := (1+2).
45
46 Program Definition add (P1 P2:point) : point :=
47 match coordinates P1, coordinates P2 return F*F+∞ with
48 | (x1, y1), (x2, y2) =>
49 if x1 =? x2
50 then
51 if y2 =? -y1
52 then ∞
53 else let k := (3*x1^2+a)/(2*y1) in
54 let x3 := k^2-x1-x2 in
55 let y3 := k*(x1-x3)-y1 in
56 (x3, y3)
57 else let k := (y2-y1)/(x2-x1) in
58 let x3 := k^2-x1-x2 in
59 let y3 := k*(x1-x3)-y1 in
60 (x3, y3)
61 | ∞, ∞ => ∞
62 | ∞, _ => coordinates P2
63 | _, ∞ => coordinates P1
64 end.
65 Next Obligation.
66 cbv [coordinates]; BreakMatch.break_match; trivial; Field.fsatz.
67 Qed.
68
69 Fixpoint mul (n:nat) (P : point) : point :=
70 match n with
71 | O => zero
72 | S n' => add P (mul n' P)
73 end.
74 End WeierstrassCurves.
75 End W.
76
77 Declare Scope W_scope.
78 Delimit Scope W_scope with W.
79 Infix "+" := W.add : W_scope.
80 Infix "*" := W.mul : W_scope.