| 1 |
Require Crypto.Algebra.Hierarchy. |
| 2 |
Require Import Crypto.Util.Notations. |
| 3 |
(*Require Crypto.Curves.Montgomery.XZ.*) |
| 4 |
|
| 5 |
Module MxDH. (* from RFC7748 *) |
| 6 |
Section MontgomeryLadderKeyExchange. |
| 7 |
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}. |
| 8 |
Local Infix "=" := Feq : type_scope. Local Notation "a <> b" := (not (a = b)) : type_scope. |
| 9 |
Local Infix "+" := Fadd. Local Infix "*" := Fmul. |
| 10 |
Local Infix "-" := Fsub. Local Infix "/" := Fdiv. |
| 11 |
Local Notation "x ^ 2" := (x*x). |
| 12 |
Local Notation "0" := Fzero. Local Notation "1" := Fone. |
| 13 |
Local Notation "2" := (1+1). Local Notation "4" := (2+2). |
| 14 |
|
| 15 |
Context {a b: F}. (* parameters of the Montgomery curve *) |
| 16 |
Context {nonsquare_aa_m4:~exists sqrt, sqrt^2 = a^2-4} {five_neq_zero:1+4<>0}. |
| 17 |
|
| 18 |
(* Implemention from RFC7748. Note that this differs from EFD and |
| 19 |
Montgomery '87 in the choice of which precomputed constant to use |
| 20 |
for [a] *) |
| 21 |
(* Ideally we would want to verify that this correspons to |
| 22 |
x-coordinate addition on the relevant elliptic curve, as |
| 23 |
specified by addition formulas in Montgomery coordinates. |
| 24 |
However, even if we did get to doing that, we still would not |
| 25 |
want to change the type of the ladder function to talk about |
| 26 |
points that are on the curve -- there are contexts where this |
| 27 |
can be, carefully, used with values that are not verified to |
| 28 |
be on the curve *) |
| 29 |
Context {a24:F} {a24_correct:4*a24 = a-2}. |
| 30 |
Section generic. |
| 31 |
Context (F4 : Type) |
| 32 |
(pair4 : F -> F -> F -> F -> F4) |
| 33 |
(let_in : F -> (F -> F4) -> F4). |
| 34 |
Local Notation "'llet' x := y 'in' z" := (let_in y (fun x => z)). |
| 35 |
Definition ladderstep_gen (X1:F) (P1 P2:F * F) : F4 := |
| 36 |
let '(X2, Z2) := P1 in |
| 37 |
let '(X3, Z3) := P2 in |
| 38 |
llet A := X2+Z2 in |
| 39 |
llet AA := A^2 in |
| 40 |
llet B := X2-Z2 in |
| 41 |
llet BB := B^2 in |
| 42 |
llet E := AA-BB in |
| 43 |
llet C := X3+Z3 in |
| 44 |
llet D := X3-Z3 in |
| 45 |
llet DA := D*A in |
| 46 |
llet CB := C*B in |
| 47 |
llet X5 := (DA+CB)^2 in |
| 48 |
llet Z5 := X1*(DA-CB)^2 in |
| 49 |
llet X4 := AA*BB in |
| 50 |
llet Z4 := E*(AA + a24*E) in |
| 51 |
pair4 X4 Z4 X5 Z5. |
| 52 |
End generic. |
| 53 |
|
| 54 |
(** TODO: Make this the only ladderstep? *) |
| 55 |
Definition ladderstep_other_assoc (X1:F) (P1 P2:F*F) : F*F*F*F := |
| 56 |
Eval cbv beta delta [ladderstep_gen] in |
| 57 |
@ladderstep_gen |
| 58 |
(F*F*F*F) |
| 59 |
(fun X3 Y3 Z3 T3 => (X3, Y3, Z3, T3)) |
| 60 |
(fun x f => let y := x in f y) |
| 61 |
X1 P1 P2. |
| 62 |
|
| 63 |
Definition ladderstep (X1:F) (P1 P2:F*F) : (F*F)*(F*F) := |
| 64 |
Eval cbv beta delta [ladderstep_gen] in |
| 65 |
@ladderstep_gen |
| 66 |
((F*F)*(F*F)) |
| 67 |
(fun X3 Y3 Z3 T3 => ((X3, Y3), (Z3, T3))) |
| 68 |
(fun x f => let y := x in f y) |
| 69 |
X1 P1 P2. |
| 70 |
|
| 71 |
(*Import Curves.Montgomery.XZ. |
| 72 |
Goal forall X1 P1 P2, Logic.eq (ladderstep X1 P1 P2) (@M.xzladderstep F Fadd Fsub Fmul a24 X1 P1 P2). |
| 73 |
Proof. |
| 74 |
intros ? [? ?] [? ?]. |
| 75 |
reflexivity. |
| 76 |
Qed.*) |
| 77 |
|
| 78 |
Context {cswap:bool->F*F->F*F->(F*F)*(F*F)}. |
| 79 |
|
| 80 |
Fixpoint downto_iter (i:nat) : list nat := |
| 81 |
match i with |
| 82 |
| Datatypes.S i' => cons i' (downto_iter i') |
| 83 |
| O => nil |
| 84 |
end. |
| 85 |
|
| 86 |
Definition downto {state} init count (step:state->nat->state) : state := |
| 87 |
List.fold_left step (downto_iter count) init. |
| 88 |
|
| 89 |
Local Notation xor := Coq.Init.Datatypes.xorb. |
| 90 |
|
| 91 |
(* Ideally, we would verify that this corresponds to x coordinate |
| 92 |
multiplication *) |
| 93 |
Definition montladder bound (testbit:nat->bool) (u:F) := |
| 94 |
let '(P1, P2, swap) := |
| 95 |
downto |
| 96 |
((1, 0), (u, 1), false) |
| 97 |
bound |
| 98 |
(fun state i => |
| 99 |
let '(P1, P2, swap) := state in |
| 100 |
let s_i := testbit i in |
| 101 |
let swap := xor swap s_i in |
| 102 |
let '(P1, P2) := cswap swap P1 P2 in |
| 103 |
let swap := s_i in |
| 104 |
let '(P1, P2) := ladderstep u P1 P2 in |
| 105 |
(P1, P2, swap) |
| 106 |
) in |
| 107 |
let '((x, z), _) := cswap swap P1 P2 in |
| 108 |
x * Finv z. |
| 109 |
End MontgomeryLadderKeyExchange. |
| 110 |
End MxDH. |
| 111 |
|
| 112 |
(* see [Test.Curve25519SpecTestVectors] for sanity-checks *) |