Skip to content

Commit 9378552

Browse files
merge MontgomeryEquivalence into MontgomeryLadder, use in GaragDoor (#1841)
1 parent b8f7dc8 commit 9378552

File tree

8 files changed

+75
-94
lines changed

8 files changed

+75
-94
lines changed

src/Bedrock/End2End/X25519/GarageDoor.v

+3-3
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ Definition garagedoor_iteration : state -> list (lightbulb_spec.OP _) -> state -
220220
udp_local ++ udp_remote ++
221221
be2 udp_length ++ be2 0 ++
222222
garagedoor_header ++
223-
le_split 32 (F.to_Z (x25519_gallina (le_combine sk) (F.of_Z Field.M_pos 9)))))
223+
le_split 32 (F.to_Z (x25519_gallina (le_combine sk) (F.of_Z _ 9)))))
224224
ioh /\ SEED=seed /\ SK=sk.
225225

226226
Local Instance spec_of_recvEthernet : spec_of "recvEthernet" := spec_of_recvEthernet.
@@ -387,7 +387,7 @@ Proof.
387387
subst pPPP.
388388
seprewrite_in_by (Array.bytearray_append cmp1) H33 SepAutoArray.listZnWords.
389389

390-
remember (le_split 32 (F.to_Z (x25519_gallina (le_combine sk) (Field.feval_bytes _)))) as vv.
390+
set (k := (Field.feval_bytes _)); remember (le_split 32 (F.to_Z (x25519_gallina (le_combine sk) k))) as vv; subst k.
391391
repeat straightline.
392392
pose proof (List.firstn_skipn 16 vv) as Hvv.
393393
pose proof (@firstn_length_le _ vv 16 ltac:(subst vv; rewrite ?length_le_split; ZnWords)).
@@ -852,7 +852,7 @@ Optimize Proof. Optimize Heap.
852852
{ rewrite ?app_length, ?length_le_split. SepAutoArray.listZnWords. }
853853
{ ZnWords. }
854854

855-
pose proof length_le_split 32 (F.to_Z (x25519_gallina (le_combine sk) (F.of_Z Field.M_pos 9))) as Hpkl.
855+
pose proof length_le_split 32 (F.to_Z (x25519_gallina (le_combine sk) (F.of_Z _ 9))) as Hpkl.
856856
seprewrite_in_by (fun xs ys=>@bytearray_address_merge _ _ _ _ _ xs ys buf) H37 SepAutoArray.listZnWords.
857857
seprewrite_in_by (fun xs ys=>@bytearray_address_merge _ _ _ _ _ xs ys buf) H37 SepAutoArray.listZnWords.
858858

src/Bedrock/End2End/X25519/MontgomeryLadder.v

+3-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ Require Import coqutil.Map.OfListWord Coq.Init.Byte coqutil.Byte.
5353
Import ProgramLogic.Coercions.
5454
Local Notation "m =* P" := ((P%sep) m) (at level 70, only parsing) (* experiment*).
5555
Local Notation "xs $@ a" := (Array.array ptsto (word.of_Z 1) a xs) (at level 10, format "xs $@ a").
56-
Definition x25519_gallina := montladder_gallina (Field.M_pos(FieldParameters:=field_parameters)) Field.a24 (Z.to_nat (Z.log2 (Z.pos order))).
56+
Definition x25519_gallina K U : F (2^255-19) :=
57+
@XZ.M.montladder _ F.zero F.one F.add F.sub F.mul F.inv (F.div (F.of_Z _ 486662 - F.of_Z _ 2) (F.of_Z _ 4))
58+
(Z.to_nat (Z.log2 (Z.pos order))) (Z.testbit K) U.
5759
Global Instance spec_of_x25519 : spec_of "x25519" :=
5860
fnspec! "x25519" out sk pk / (o s p : list Byte.byte) (R : _ -> Prop),
5961
{ requires t m := m =* s$@sk * p$@pk * o$@out * R /\

src/Bedrock/End2End/X25519/MontgomeryLadderProperties.v

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Definition montladder_post (pOUT pK pU : word.rep (word:=Naive.word32))
4242
(BW:=BW32)
4343
(field_representaton:=field_representation n s c)
4444
(Some Field.tight_bounds) pOUT
45-
(montladder_gallina Field.M_pos Field.a24 (Z.to_nat (Z.log2 Curve25519.order)) K U)
45+
(@XZ.M.montladder _ F.zero F.one F.add F.sub F.mul F.inv a24 (Z.log2 Curve25519.order) (Z.testbit K) U)
4646
⋆ Array.array ptsto (word.of_Z 1) pK Kbytes
4747
⋆ FElem (BW:=BW32)
4848
(field_representaton:=field_representation n s c)

src/Bedrock/Group/ScalarMult/MontgomeryEquivalence.v

-83
This file was deleted.

src/Bedrock/Group/ScalarMult/MontgomeryLadder.v

+64-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
Require Crypto.Bedrock.Group.Loops.
2+
Require Import Crypto.Curves.Montgomery.XZ.
13
Require Import Rupicola.Lib.Api.
24
Require Import Rupicola.Lib.Alloc.
35
Require Import Rupicola.Lib.SepLocals.
@@ -43,7 +45,8 @@ Notation "'let/n' ( v , w , x , y , z ) := val 'in' body" :=
4345

4446
Section Gallina.
4547
Local Open Scope F_scope.
46-
Definition montladder_gallina (m : positive) (a24 : F m) (count : nat) (k : Z) (u : F m)
48+
Context (m : positive) (a24 : F m) (count : nat).
49+
Definition montladder_gallina (k : Z) (u : F m)
4750
: F m :=
4851
let/n X1 := stack 1 in
4952
let/n Z1 := stack 0 in
@@ -69,6 +72,62 @@ Section Gallina.
6972
let/n OUT := (F.inv Z1) in
7073
let/n OUT := (X1 * OUT) in
7174
OUT.
75+
76+
(*TODO: which of ladderstep_gallina and M.xzladderstep should we change? either?*)
77+
Definition reorder_pairs {A B C D} (p : \<<A , B , C , D\>>) : (A*B)*(C*D) :=
78+
(P2.car p, P2.car (P2.cdr p),((P2.car (P2.cdr (P2.cdr p))),(P2.cdr (P2.cdr (P2.cdr p))))).
79+
80+
(* TODO: should M.montladder change to accomodate this? *)
81+
Definition to_pair {A B} p : A*B := (P2.car p, P2.cdr p).
82+
83+
Lemma invert_reorder_pairs {A B C D} (p : \<<A , B , C , D\>>) w x y z
84+
: reorder_pairs p = (w,x, (y,z)) <-> p = \<w,x,y,z\>.
85+
Proof.
86+
destruct p as [? [? [? ?]]].
87+
cbv.
88+
intuition congruence.
89+
Qed.
90+
91+
Lemma ladderstep_gallina_equiv X1 P1 P2 :
92+
reorder_pairs (ladderstep_gallina _ a24 X1 (fst P1) (snd P1) (fst P2) (snd P2)) =
93+
@M.xzladderstep _ F.add F.sub F.mul a24 X1 P1 P2.
94+
Proof.
95+
intros. cbv [ladderstep_gallina M.xzladderstep].
96+
destruct P1 as [x1 z1]. destruct P2 as [x2 z2].
97+
cbv [Rewriter.Util.LetIn.Let_In nlet]. cbn [fst snd].
98+
rewrite !F.pow_2_r; trivial.
99+
Qed.
100+
101+
Lemma montladder_gallina_equiv n point :
102+
montladder_gallina n point =
103+
@M.montladder _ F.zero F.one F.add F.sub F.mul F.inv a24 (Z.of_nat count) (Z.testbit n) point.
104+
Proof.
105+
cbv [montladder_gallina M.montladder Rewriter.Util.LetIn.Let_In stack].
106+
do 5 (unfold nlet at 1); cbn [fst snd P2.car P2.cdr].
107+
rewrite Loops.downto_while.
108+
match goal with
109+
| |- ?lhs = ?rhs =>
110+
match lhs with context [Loops.while ?ltest ?lbody ?fuel ?linit] =>
111+
match rhs with context [Loops.while ?rtest ?rbody ?fuel ?rinit] =>
112+
rewrite (Loops.while.preservation ltest lbody rtest rbody
113+
(fun s1 s2 => s1 = let '(x2, z2, x3, z3, swap, i) := s2 in
114+
(\<x2, z2, x3, z3, swap\>, i))) with (init2:=rinit)
115+
end end end.
116+
{ rewrite !Nat2Z.id. destruct (Loops.while _ _ _ _) eqn:? at 1 2.
117+
destruct_products. case b; reflexivity. }
118+
{ intros. destruct_products. congruence. }
119+
{ intros. destruct_products. Prod.inversion_prod. LtbToLt.Z.ltb_to_lt. subst.
120+
rewrite !Z2Nat.id by lia.
121+
cbv [nlet M.cswap].
122+
repeat match goal with
123+
| H : (_,_) = (_,_) |- _ => inversion H; subst; clear H
124+
| _ => progress BreakMatch.break_match
125+
| _ => progress BreakMatch.break_match_hyps
126+
end;
127+
rewrite <- ladderstep_gallina_equiv, invert_reorder_pairs in Heqp0;
128+
cbn [fst snd to_pair] in Heqp0; inversion_clear Heqp0; trivial. }
129+
{ reflexivity. }
130+
Qed.
72131
End Gallina.
73132

74133
Section __.
@@ -101,7 +160,7 @@ Section __.
101160
* R)%sep mem;
102161
ensures tr' mem' :=
103162
tr' = tr
104-
/\ (let OUT := montladder_gallina M_pos a24 scalarbits K U in
163+
/\ (let OUT := @M.montladder _ F.zero F.one F.add F.sub F.mul F.inv a24 (Z.of_nat scalarbits) (Z.testbit K) U in
105164
(FElem (Some tight_bounds) pOUT OUT * Kbytes$@pK
106165
* FElem (Some tight_bounds) pU U
107166
* R)%sep mem') }.
@@ -311,6 +370,9 @@ Section __.
311370
As montladder_correct.
312371
Proof.
313372
pose proof scalarbits_bound.
373+
cbv [spec_of_montladder]; intros; eapply Semantics.weaken_call; cycle 1; intros.
374+
{ rewrite <-montladder_gallina_equiv. match goal with H : ?e t' m' rets |- _ => exact H end. }
375+
314376
compile_setup.
315377
repeat compile_step.
316378
eapply compile_nlet_as_nlet_eq.

src/Bedrock/Group/ScalarMult/ScalarMult.v

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Require Import coqutil.Byte.
44
Require Import Crypto.Algebra.Hierarchy.
55
Require Import Crypto.Algebra.ScalarMult.
66
Require Import Crypto.Arithmetic.PrimeFieldTheorems.
7-
Require Import Crypto.Bedrock.Group.ScalarMult.MontgomeryEquivalence.
87
Require Import Crypto.Bedrock.Group.ScalarMult.MontgomeryLadder.
98
Require Import Crypto.Bedrock.Specs.Field.
109
Require Import Crypto.Bedrock.Specs.Group.

src/Curves/Montgomery/XZ.v

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ Module M.
113113
((x2, z2), (x3, z3))%core
114114
end.
115115

116-
Context {cswap:bool->F->F->F*F}.
116+
Definition cswap (b : bool) (x y : F) := if b then pair y x else pair x y.
117117
Local Notation xor := Coq.Init.Datatypes.xorb.
118118
Local Open Scope core_scope.
119119

src/Curves/Montgomery/XZProofs.v

+3-2
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ Module M.
257257
Global Instance Proper_ladder_invariant : Proper (Feq ==> MontgomeryCurve.M.eq ==> MontgomeryCurve.M.eq ==> iff) ladder_invariant.
258258
Proof. t. Qed.
259259

260-
Local Notation montladder := (M.montladder(a24:=a24)(Fadd:=Fadd)(Fsub:=Fsub)(Fmul:=Fmul)(Fzero:=Fzero)(Fone:=Fone)(Finv:=Finv)(cswap:=fun b x y => if b then pair y x else pair x y)).
260+
Local Notation montladder := (M.montladder(a24:=a24)(Fadd:=Fadd)(Fsub:=Fsub)(Fmul:=Fmul)(Fzero:=Fzero)(Fone:=Fone)(Finv:=Finv)).
261261
Local Notation scalarmult := (@ScalarMult.scalarmult_ref Mpoint Madd M.zero Mopp).
262262

263263
Import Crypto.Util.Loops.
@@ -310,7 +310,7 @@ Module M.
310310
{ (* measure decreases *)
311311
cbv [Let_In]; break_match; cbn; rewrite Z.succ_pred; apply Znat.Z2Nat.inj_lt; lia. } }
312312
{ (* if loop exited, invariant implies postcondition *)
313-
break_match; break_match_hyps; setoid_subst_rel Feq; fsatz. } }
313+
cbv [M.cswap]; break_match; break_match_hyps; setoid_subst_rel Feq; fsatz. } }
314314
Qed.
315315

316316
Lemma montladder_correct_nz
@@ -384,6 +384,7 @@ Module M.
384384
destruct_head' @and; autorewrite with cancel_pair in *.
385385
replace i with ((-(1))%Z) in * by lia; clear Hi Hbranch.
386386
rewrite Z.succ_m1, Z.shiftr_0_r in *.
387+
cbv [M.cswap];
387388
destruct swap eqn:Hswap; rewrite <-!to_x_inv00 by assumption;
388389
eauto using projective_to_xz, proper_to_x_projective. } }
389390
Qed.

0 commit comments

Comments
 (0)