| 1 |
From Coq Require Import BinPosDef. |
| 2 |
Require Import Crypto.Algebra.Field. |
| 3 |
Require Import Crypto.Util.GlobalSettings. |
| 4 |
Require Import Crypto.Util.Sum Crypto.Util.Prod. |
| 5 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 6 |
Require Import Crypto.Util.Tactics.DestructHead. |
| 7 |
Require Import Crypto.Spec.MontgomeryCurve Crypto.Spec.WeierstrassCurve. |
| 8 |
|
| 9 |
Module M. |
| 10 |
Section MontgomeryCurve. |
| 11 |
Import BinNat. |
| 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 |
{Feq_dec:Decidable.DecidableRel Feq} |
| 15 |
{char_ge_3:@Ring.char_ge F Feq Fzero Fone Fopp Fadd Fsub Fmul (BinNat.N.succ_pos (BinNat.N.two))}. |
| 16 |
Local Infix "=" := Feq : type_scope. Local Notation "a <> b" := (not (a = b)) : type_scope. |
| 17 |
Local Infix "+" := Fadd. Local Infix "*" := Fmul. |
| 18 |
Local Infix "-" := Fsub. Local Infix "/" := Fdiv. |
| 19 |
Local Notation "- x" := (Fopp x). |
| 20 |
Local Notation "x ^ 2" := (x*x) (at level 30). |
| 21 |
Local Notation "x ^ 3" := (x*x^2) (at level 30). |
| 22 |
Local Notation "0" := Fzero. Local Notation "1" := Fone. |
| 23 |
Local Notation "'∞'" := unit : type_scope. |
| 24 |
Local Notation "'∞'" := (inr tt) : core_scope. |
| 25 |
Local Notation "( x , y )" := (inl (pair x y)). |
| 26 |
Local Open Scope core_scope. |
| 27 |
|
| 28 |
Context {a b: F} {b_nonzero:b <> 0}. |
| 29 |
|
| 30 |
Local Notation opp := (M.opp(a:=a)(b_nonzero:=b_nonzero)). |
| 31 |
Local Notation add := (M.add(b_nonzero:=b_nonzero)). |
| 32 |
Local Notation point := (@M.point F Feq Fadd Fmul a b). |
| 33 |
|
| 34 |
Global Instance Decidable_eq : Decidable.DecidableRel (@M.eq _ Feq Fadd Fmul a b) := _. |
| 35 |
|
| 36 |
Section MontgomeryWeierstrass. |
| 37 |
Local Notation "2" := (1+1). |
| 38 |
Local Notation "3" := (1+2). |
| 39 |
Local Notation "4" := (1+1+1+1). |
| 40 |
Local Notation "9" := (3*3). |
| 41 |
Local Notation "27" := (4*4 + 4+4 +1+1+1). |
| 42 |
|
| 43 |
Context {char_ge_28:@Ring.char_ge F Feq Fzero Fone Fopp Fadd Fsub Fmul 28}. |
| 44 |
|
| 45 |
Context {aw bw} {Haw:aw=(3-a^2)/(3*b^2)} {Hbw:bw=(2*a^3-9*a)/(27*b^3)}. |
| 46 |
Local Notation Wpoint := (@W.point F Feq Fadd Fmul aw bw). |
| 47 |
Local Notation Wadd := (@W.add F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv field Feq_dec char_ge_3 aw bw). |
| 48 |
Program Definition to_Weierstrass (P:@point) : Wpoint := |
| 49 |
match M.coordinates P return F*F+∞ with |
| 50 |
| (x, y) => ((x + a/3)/b, y/b) |
| 51 |
| _ => ∞ |
| 52 |
end. |
| 53 |
Next Obligation. Proof. destruct_head' @point; cbv; break_match; trivial; fsatz. Qed. |
| 54 |
|
| 55 |
Program Definition of_Weierstrass (P:Wpoint) : point := |
| 56 |
match W.coordinates P return F*F+∞ with |
| 57 |
| (x,y) => (b*x-a/3, b*y) |
| 58 |
| _ => ∞ |
| 59 |
end. |
| 60 |
Next Obligation. Proof. destruct_head' @Wpoint; cbv; break_match; trivial; fsatz. Qed. |
| 61 |
End MontgomeryWeierstrass. |
| 62 |
End MontgomeryCurve. |
| 63 |
End M. |