| 1 |
From Coq Require Import BinPosDef. |
| 2 |
From Coq Require Import BinIntDef. |
| 3 |
Require Import Crypto.Algebra.Field. |
| 4 |
Require Import Crypto.Util.GlobalSettings Crypto.Util.Notations. |
| 5 |
Require Import Crypto.Util.Sum Crypto.Util.Prod Crypto.Util.LetIn. |
| 6 |
Require Import Crypto.Util.Decidable. |
| 7 |
Require Import Crypto.Util.Loops. |
| 8 |
Require Import Crypto.Spec.MontgomeryCurve Crypto.Curves.Montgomery.Affine. |
| 9 |
|
| 10 |
Module M. |
| 11 |
Section MontgomeryCurve. |
| 12 |
Import BinNat. |
| 13 |
Context {F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv} |
| 14 |
{field:@Algebra.Hierarchy.field F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv} |
| 15 |
{Feq_dec:Decidable.DecidableRel Feq} |
| 16 |
{char_ge_5:@Ring.char_ge F Feq Fzero Fone Fopp Fadd Fsub Fmul 5}. |
| 17 |
Declare Scope F_scope. |
| 18 |
Delimit Scope F_scope with F. |
| 19 |
Local Open Scope F_scope. |
| 20 |
Local Infix "=" := Feq : type_scope. Local Notation "a <> b" := (not (a = b)) : type_scope. |
| 21 |
Local Infix "+" := Fadd : F_scope. Local Infix "*" := Fmul : F_scope. |
| 22 |
Local Infix "-" := Fsub : F_scope. Local Infix "/" := Fdiv : F_scope. |
| 23 |
Local Notation "x ^ 2" := (x*x) : F_scope. |
| 24 |
Local Notation "0" := Fzero : F_scope. Local Notation "1" := Fone : F_scope. |
| 25 |
Local Notation "'∞'" := (inr tt) : core_scope. |
| 26 |
Local Notation "( x , y , .. , z )" := (inl (pair .. (pair x y) .. z)) : F_scope. |
| 27 |
|
| 28 |
Context {a b: F} {b_nonzero:b <> 0}. |
| 29 |
Local Notation add := (M.add(b_nonzero:=b_nonzero)). |
| 30 |
Local Notation opp := (M.opp(b_nonzero:=b_nonzero)). |
| 31 |
Local Notation point := (@M.point F Feq Fadd Fmul a b). |
| 32 |
|
| 33 |
Program Definition to_xz (P:point) : F*F := |
| 34 |
match M.coordinates P with |
| 35 |
| (x, y) => (x, 1)%core |
| 36 |
| ∞ => (1, 0)%core |
| 37 |
end. |
| 38 |
|
| 39 |
Let char_ge_3:@Ring.char_ge F Feq Fzero Fone Fopp Fadd Fsub Fmul (BinNat.N.succ_pos (BinNat.N.two)). |
| 40 |
Proof. clear -char_ge_5. eapply Algebra.Hierarchy.char_ge_weaken; eauto; vm_decide. Qed. |
| 41 |
|
| 42 |
(* From Curve25519 paper by djb, appendix B. Credited to Montgomery *) |
| 43 |
Context {a24:F} {a24_correct:(1+1+1+1)*a24 = a-(1+1)}. |
| 44 |
Definition xzladderstep (x1:F) (Q Q':F*F) : ((F*F)*(F*F)) := |
| 45 |
match Q, Q' with |
| 46 |
pair x z, pair x' z' => |
| 47 |
dlet A := x+z in |
| 48 |
dlet B := x-z in |
| 49 |
dlet AA := A^2 in |
| 50 |
dlet BB := B^2 in |
| 51 |
dlet x2 := AA*BB in |
| 52 |
dlet E := AA-BB in |
| 53 |
dlet z2 := E*(AA + a24*E) in |
| 54 |
dlet C := x'+z' in |
| 55 |
dlet D := x'-z' in |
| 56 |
dlet CB := C*B in |
| 57 |
dlet DA := D*A in |
| 58 |
dlet x3 := (DA+CB)^2 in |
| 59 |
dlet z3 := x1*(DA-CB)^2 in |
| 60 |
((x2, z2), (x3, z3))%core |
| 61 |
end. |
| 62 |
|
| 63 |
(* optimized version from curve25519-donna by Adam Langley *) |
| 64 |
Definition donnaladderstep (x1:F) (Q Q':F*F) : (F*F)*(F*F) := |
| 65 |
match Q, Q' with |
| 66 |
pair x z, pair x' z'=> |
| 67 |
dlet origx := x in |
| 68 |
dlet x := x + z in |
| 69 |
dlet z := origx - z in |
| 70 |
dlet origx' := x' in |
| 71 |
dlet x' := x' + z' in |
| 72 |
dlet z' := origx' - z' in |
| 73 |
dlet xx' := x' * z in |
| 74 |
dlet zz' := x * z' in |
| 75 |
dlet origx' := xx' in |
| 76 |
dlet xx' := xx' + zz' in |
| 77 |
dlet zz' := origx' - zz' in |
| 78 |
dlet x3 := xx'^2 in |
| 79 |
dlet zzz' := zz'^2 in |
| 80 |
dlet z3 := zzz' * x1 in |
| 81 |
dlet xx := x^2 in |
| 82 |
dlet zz := z^2 in |
| 83 |
dlet x2 := xx * zz in |
| 84 |
dlet zz := xx - zz in |
| 85 |
dlet zzz := zz * a24 in |
| 86 |
dlet zzz := zzz + xx in |
| 87 |
dlet z2 := zz * zzz in |
| 88 |
((x2, z2), (x3, z3))%core |
| 89 |
end. |
| 90 |
|
| 91 |
Context {ap2d4:F} {ap2d4_correct:(1+1+1+1)*a24 = a+1+1}. |
| 92 |
Definition boringladderstep (x1:F) (Q Q':F*F) : (F*F)*(F*F) := |
| 93 |
match Q, Q' with |
| 94 |
pair x2 z2, pair x3 z3 => |
| 95 |
dlet tmp0l := x3 - z3 in |
| 96 |
dlet tmp1l := x2 - z2 in |
| 97 |
dlet x2l := x2 + z2 in |
| 98 |
dlet z2l := x3 + z3 in |
| 99 |
dlet z3 := tmp0l * x2l in |
| 100 |
dlet z2 := z2l * tmp1l in |
| 101 |
dlet tmp0 := tmp1l^2 in |
| 102 |
dlet tmp1 := x2l^2 in |
| 103 |
dlet x3l := z3 + z2 in |
| 104 |
dlet z2l := z3 - z2 in |
| 105 |
dlet x2 := tmp1 * tmp0 in |
| 106 |
dlet tmp1l := tmp1 - tmp0 in |
| 107 |
dlet z2 := z2l^2 in |
| 108 |
dlet z3 := ap2d4 * tmp1l in |
| 109 |
dlet x3 := x3l^2 in |
| 110 |
dlet tmp0l := tmp0 + z3 in |
| 111 |
dlet z3 := x1 * z2 in |
| 112 |
dlet z2 := tmp1l * tmp0l in |
| 113 |
((x2, z2), (x3, z3))%core |
| 114 |
end. |
| 115 |
|
| 116 |
Definition cswap (b : bool) (x y : F) := if b then pair y x else pair x y. |
| 117 |
Local Notation xor := Coq.Init.Datatypes.xorb. |
| 118 |
Local Open Scope core_scope. |
| 119 |
|
| 120 |
(* TODO: make a nice notations for loops like here *) |
| 121 |
Definition montladder (scalarbits : Z) (testbit:Z->bool) (x1:F) : F := |
| 122 |
let '(x2, z2, x3, z3, swap, _) := (* names of variables as used after the loop *) |
| 123 |
(while (fun '(_, i) => BinInt.Z.geb i 0) (* the test of the loop *) |
| 124 |
(fun '(x2, z2, x3, z3, swap, i) => (* names of variables as used in the loop; we should reuse the same names as for after the loop *) |
| 125 |
dlet b := testbit i in (* the body... *) |
| 126 |
dlet swap := xor swap b in |
| 127 |
let (x2, x3) := cswap swap x2 x3 in |
| 128 |
let (z2, z3) := cswap swap z2 z3 in |
| 129 |
dlet swap := b in |
| 130 |
let '((x2, z2), (x3, z3)) := xzladderstep x1 (x2, z2) (x3, z3) return _ in |
| 131 |
let i := BinInt.Z.pred i in (* the third "increment" component of a for loop; either between the test and body or just inlined into the body like here *) |
| 132 |
(x2, z2, x3, z3, swap, i)) (* the "return value" of the body is always the exact same variable names as in the beginning of the body because we shadow the original binders, but I think for now this will be unavoidable boilerplate. *) |
| 133 |
(BinInt.Z.to_nat scalarbits) (* bound on number of loop iterations, should come between test and body *) |
| 134 |
(1%F, 0%F, x1, 1%F, false, BinInt.Z.pred scalarbits)) in (* initial values, these should come before the test and body *) |
| 135 |
let (x2, x3) := cswap swap x2 x3 in |
| 136 |
let (z2, z3) := cswap swap z2 z3 in |
| 137 |
x2 * Finv z2. |
| 138 |
End MontgomeryCurve. |
| 139 |
End M. |