Debian Codegraph @

src/Curves/Weierstrass/Jacobian/ScalarMult.v

raw
1 From Coq Require Import Morphisms.
2 Require Import Crypto.Spec.WeierstrassCurve Crypto.Algebra.ScalarMult.
3 Require Import Crypto.Curves.Weierstrass.Jacobian.Jacobian.
4 Require Import Crypto.Curves.Weierstrass.Affine Crypto.Curves.Weierstrass.AffineProofs.
5 Require Import Crypto.Curves.Weierstrass.Jacobian.CoZ.
6 Require Import Crypto.Util.Decidable Crypto.Algebra.Field.
7 Require Import Crypto.Util.Tactics.BreakMatch.
8 Require Import Crypto.Util.Tactics.DestructHead.
9 Require Import Crypto.Util.Tactics.SpecializeBy.
10 Require Import Crypto.Util.Tactics.SetoidSubst.
11 Require Import Crypto.Util.Notations Crypto.Util.LetIn.
12 Require Import Crypto.Util.Sum Crypto.Util.Prod Crypto.Util.Sigma.
13 Require Import Crypto.Util.FsatzAutoLemmas.
14 Require Import Crypto.Util.Loops.
15 Require Import Crypto.Util.ZUtil.Notations.
16 Require Import Crypto.Util.ZUtil.Tactics.LtbToLt.
17 Require Import Crypto.Util.ZUtil.Shift.
18 Require Import Crypto.Util.ZUtil.Peano.
19 Require Import Crypto.Util.Tuple.
20 From Coq Require Import ZArith.
21 From Coq Require Import Lia.
22
23 Module Z.
24 (* Note: ideally we would contribute this to Coq *)
25 Lemma mod_pow2_succ_r a n (Hi : Z.le 0 n) : (a mod 2 ^ Z.succ n = a mod 2 ^ n + Z.b2z (Z.testbit a n) * 2 ^ n)%Z.
26 Proof.
27 clear -Hi.
28 rewrite Z_div_mod_eq_full with (a:=(a mod _)%Z) (b:=(2^n)%Z).
29 rewrite <-(Z.mod_small ((a mod 2 ^ Z.succ n / 2 ^ n)) 2); cycle 1.
30 { rewrite Z.pow_succ_r by lia; Z.div_mod_to_equations; nia. }
31 rewrite <-Z.mod_pow2_bits_low with (n:=Z.succ n) by lia.
32 rewrite <-Znumtheory.Zmod_div_mod; cycle 1.
33 { lia. } { lia. } { exists 2%Z. rewrite <-Z.pow_succ_r; f_equal; lia. }
34 rewrite Z.testbit_spec'; lia.
35 Qed.
36 End Z.
37
38 Module ScalarMult.
39 Section JoyeDoubleAddDecomposition.
40 (* Joye's double-add ladder for computing [n]P basically tracks the
41 values of two sequences [SS i]P and [TT i]P.
42 This section proves some properties on the sequences SS and TT.
43 *)
44 Variables n : Z.
45 Local Coercion Z.of_nat : nat >-> Z.
46
47 Definition SS (i : nat) : Z := n mod 2 ^ i.
48 Definition TT (i : nat) : Z := 2^i - SS i.
49
50 Lemma SS_plus_TT k : (SS k + TT k = 2^(Z.of_nat k))%Z.
51 Proof. cbv [SS TT]; lia. Qed.
52
53 Lemma SS_succ i : SS (S i) = if (Z.testbit n (Z.of_nat i)) then (2 * SS i + TT i)%Z else SS i.
54 Proof.
55 cbv [TT SS].
56 pose proof Z.mod_pow2_succ_r n i; case Z.testbit in *; cbv [Z.b2z] in *; lia.
57 Qed.
58
59 Lemma TT_succ i : TT (S i) = if (Z.testbit n (Z.of_nat i)) then TT i else (2 * TT i + SS i)%Z.
60 Proof.
61 cbv [TT SS].
62 rewrite Nat2Z.inj_succ by lia.
63 pose proof Z.mod_pow2_succ_r n i; case Z.testbit in *; cbv [Z.b2z] in *; rewrite ?Z.pow_succ_r in *; lia.
64 Qed.
65
66 Lemma SS_monotone1 (k : nat) : (SS 1 <= SS (S k))%Z.
67 Proof.
68 cbv [SS].
69 rewrite Z.pow_1_r, Nat2Z.inj_succ, Z.pow_succ_r, Znumtheory.Zmod_div_mod with (n:=2) (m:=(2*2^k)%Z)
70 by (try exists (2^k)%Z; lia).
71 apply Z.mod_bound_pos_le; Z.div_mod_to_equations; lia.
72 Qed.
73
74 Lemma TT_monotone1 (k : nat) : (TT 1 <= TT (S k))%Z.
75 Proof.
76 cbv [TT SS]; repeat rewrite Nat2Z.inj_succ. set (z := Z.succ k).
77 enough (n mod 2 ^ z < 2 ^ z + (n mod 2 ^ Z.succ 0 - 1))%Z by lia; rewrite Z.pow_1_r.
78 enough (n mod 2 = 0 -> n mod 2 ^ z <> Z.pred (2^z))%Z by try (Z.div_mod_to_equations; nia); intros ? X.
79 apply (f_equal (fun x => Z.testbit x 0)) in X.
80 rewrite <-Z.ones_equiv, Z.ones_spec_low, Z.mod_pow2_bits_low, Z.bit0_eqb in X; lia.
81 Qed.
82
83 Lemma SSn scalarbitsz (Hn : (0 <= n < 2^scalarbitsz)%Z) (Hscalarbitsz : (0 < scalarbitsz)%Z) :
84 SS (Z.to_nat scalarbitsz) = n :> Z.
85 Proof.
86 cbv [SS]. rewrite Z2Nat.id by lia.
87 apply Z.mod_small; trivial.
88 Qed.
89 End JoyeDoubleAddDecomposition.
90
91 Section JoyeLadder.
92 Context {F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv} {a b:F}
93 {field:@Algebra.Hierarchy.field F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv}
94 {char_ge_3:@Ring.char_ge F Feq Fzero Fone Fopp Fadd Fsub Fmul (BinNat.N.succ_pos (BinNat.N.two))}
95 {Feq_dec:DecidableRel Feq}.
96 Local Infix "=" := Feq : type_scope. Local Notation "a <> b" := (not (a = b)) : type_scope.
97 Local Notation "0" := Fzero. Local Notation "1" := Fone.
98 Local Infix "+" := Fadd. Local Infix "-" := Fsub.
99 Local Infix "*" := Fmul. Local Infix "/" := Fdiv.
100 Local Notation "x ^ 2" := (x*x). Local Notation "x ^ 3" := (x^2*x).
101 Local Notation "2" := (1+1). Local Notation "4" := (2+1+1).
102 Local Notation "8" := (4+4). Local Notation "27" := (4*4 + 4+4 +1+1+1).
103 Local Notation "'∞'" := (@W.zero F Feq Fadd Fmul a b).
104 Local Notation Wpoint := (@W.point F Feq Fadd Fmul a b).
105 Context {char_ge_12:@Ring.char_ge F Feq Fzero Fone Fopp Fadd Fsub Fmul 12%positive}
106 {discriminant_nonzero:id(4*a*a*a + 27*b*b <> 0)}.
107 Local Notation Wopp := (@W.opp F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv a b field Feq_dec).
108 Local Notation Wadd := (@W.add F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv field Feq_dec char_ge_3 a b).
109 Local Notation Wzero := (@W.zero F Feq Fadd Fmul a b).
110 Local Notation Weq := (@W.eq F Feq Fadd Fmul a b).
111 Local Notation Wgroup := (@W.commutative_group F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv a b field Feq_dec char_ge_3 char_ge_12 discriminant_nonzero).
112 Local Notation point := (@Jacobian.point F Feq Fzero Fadd Fmul a b Feq_dec).
113 Local Notation eq := (@Jacobian.eq F Feq Fzero Fadd Fmul a b Feq_dec).
114 Local Notation x_of := (@Jacobian.x_of F Feq Fzero Fadd Fmul a b Feq_dec).
115 Local Notation y_of := (@Jacobian.y_of F Feq Fzero Fadd Fmul a b Feq_dec).
116 Local Notation z_of := (@Jacobian.z_of F Feq Fzero Fadd Fmul a b Feq_dec).
117 Local Notation add := (@Jacobian.add F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv a b field char_ge_3 Feq_dec).
118 Local Notation opp := (@Jacobian.opp F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv a b field Feq_dec).
119 Local Notation co_z := (@Jacobian.co_z F Feq Fzero Fadd Fmul a b Feq_dec).
120 Local Notation make_co_z := (@Jacobian.make_co_z F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv a b field Feq_dec).
121 Local Notation of_affine := (@Jacobian.of_affine F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv a b field Feq_dec).
122 Local Notation to_affine := (@Jacobian.to_affine F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv a b field Feq_dec).
123 Local Notation zero := (of_affine Wzero).
124 Local Notation dblu := (@Jacobian.dblu F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv a b field Feq_dec).
125 Local Notation tplu := (@Jacobian.tplu F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv a b field Feq_dec).
126 Local Notation zaddu := (@Jacobian.zaddu F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv a b field Feq_dec).
127 Local Notation zdau := (@Jacobian.zdau F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv a b field Feq_dec).
128 Local Notation is_point := (@Jacobian.is_point F Feq Fzero Fadd Fmul a b Feq_dec).
129
130 Ltac prept_step_opt :=
131 match goal with
132 | [ H : ?T |- ?T ] => exact H
133 | [ |- ?x = ?x ] => reflexivity
134 | [ H : ?T, H' : ~?T |- _ ] => solve [ exfalso; apply H', H ]
135 end.
136
137 Ltac prept_step :=
138 match goal with
139 | _ => progress prept_step_opt
140 | _ => progress intros
141 (*| _ => progress specialize_by_assumption*)
142 (*| _ => progress specialize_by trivial*)
143 | _ => progress cbv [proj1_sig fst snd] in *
144 | _ => progress autounfold with points_as_coordinates in *
145 | _ => progress destruct_head'_True
146 | _ => progress destruct_head'_unit
147 | _ => progress destruct_head'_prod
148 | _ => progress destruct_head'_sig
149 | _ => progress destruct_head'_and
150 | _ => progress destruct_head'_sum
151 | _ => progress destruct_head'_bool
152 | _ => progress destruct_head'_or
153 | H: context[@dec ?P ?pf] |- _ => destruct (@dec P pf)
154 | |- context[@dec ?P ?pf] => destruct (@dec P pf)
155 | |- ?P => lazymatch type of P with Prop => split end
156 end.
157 Ltac prept := repeat prept_step.
158 Ltac t := prept; trivial; try contradiction; fsatz.
159
160 Create HintDb points_as_coordinates discriminated.
161 Hint Unfold Proper respectful Reflexive Symmetric Transitive : points_as_coordinates.
162 Hint Unfold Jacobian.point Jacobian.eq W.eq W.point W.coordinates proj1_sig fst snd : points_as_coordinates.
163
164 Hint Unfold Jacobian.x_of Jacobian.y_of Jacobian.z_of Jacobian.opp Jacobian.co_z : points_as_coordinates.
165
166 Lemma co_z_comm (A B : point) (H : co_z A B) :
167 co_z B A.
168 Proof. t. Qed.
169
170 Definition co_z_points : Type := { '(A, B) | co_z A B }.
171
172 Program Definition make_co_z_points (P Q : point) (HQaff : z_of Q = 1) : co_z_points :=
173 make_co_z P Q HQaff.
174 Next Obligation. Proof. t. Qed.
175
176 Program Definition cswap_co_z_points (b : bool) (AB : co_z_points) : co_z_points :=
177 exist _
178 (let '(A, B) := proj1_sig AB
179 in if b then (B, A) else (A, B))
180 _.
181 Next Obligation. Proof. generalize (proj2_sig AB); rewrite (surjective_pairing (proj1_sig _)); destruct b0; [apply co_z_comm|auto]. Qed.
182
183 Program Definition zdau_co_z_points (AB : co_z_points) : co_z_points :=
184 exist _ (let '(A, B) := proj1_sig AB in zdau A B _) _.
185 Next Obligation. Proof. generalize (proj2_sig AB). rewrite <- Heq_anonymous. auto. Qed.
186 Next Obligation. Proof. destruct AB as ((A & B) & HAB). simpl. t. Qed.
187
188 Program Definition zaddu_co_z_points (AB : co_z_points) : co_z_points :=
189 exist _ (let '(A, B) := proj1_sig AB in zaddu A B _) _.
190 Next Obligation. Proof. generalize (proj2_sig AB); rewrite <- Heq_anonymous. auto. Qed.
191 Next Obligation. Proof. destruct AB as ((A & B) & HAB). simpl. t. Qed.
192
193 Program Definition tplu_co_z_points (P : point) (HPaff : z_of P = 1) : co_z_points :=
194 tplu P _.
195 Next Obligation. Proof. t. Qed.
196
197 Lemma opp_of_affine (P : Wpoint) (HPnz : P <> ∞ :> Wpoint) :
198 z_of (opp (of_affine P)) = 1.
199 Proof. t. Qed.
200
201 (* Scalar Multiplication on Weierstraß Elliptic Curves from Co-Z Arithmetic *)
202 (* Goundar, Joye, Miyaji, Rivain, Vanelli *)
203 (* Algorithm 14 Joye’s double-add algorithm with Co-Z addition formulæ *)
204 (* This is an adapted version that consumes and returns points in jacobian
205 coordinates, correctness assumes the scalar is odd (i.e., testbit 0 = true). *)
206 Definition joye_ladder_inner (scalarbitsz : Z) (testbit : Z -> bool) (P : point)
207 (HPaff : z_of P = 1) : point :=
208 (* Initialization *)
209 let swap := testbit 1%Z in
210 let R1R0 := tplu_co_z_points P HPaff in
211 (* loop *)
212 let '(R1R0, swap, _) :=
213 (@while (co_z_points*bool*Z) (fun '(_, _, i) => (Z.ltb i scalarbitsz))
214 (fun '(R1R0, swap, i) =>
215 let b := testbit i in
216 let swap := xorb swap b in
217 let R1R0 := cswap_co_z_points swap R1R0 in
218 let R1R0 := zdau_co_z_points R1R0 in
219 let swap := b in
220 let i := Z.succ i in
221 (R1R0, swap, i))
222 (Z.to_nat scalarbitsz - 2) (* bound on loop iterations *)
223 (R1R0, swap, 2%Z)) in
224 let R1R0 := cswap_co_z_points swap R1R0 in
225 snd (proj1_sig R1R0).
226
227 (* Wrapper around joye_ladder_inner for points in affine coordinates,
228 it also readjusts the result if the scalar input is even. *)
229 Program Definition joye_ladder (scalarbitsz : Z) (testbit : Z -> bool) (P : Wpoint)
230 (HPnz : P <> ∞ :> Wpoint) : Wpoint :=
231 to_affine (
232 let P := of_affine P in
233 let R0 := joye_ladder_inner scalarbitsz testbit P _ in
234 let b := testbit 0%Z in
235 let mP := opp P in
236 (* Make sure R0 and -P are co-z *)
237 let R0R1 := make_co_z_points R0 mP (opp_of_affine _ HPnz) in
238 (* if b = 0 then R0 <- R0 - P and R1 <- R0 *)
239 (* if b = 1 then R0 <- R0 and R1 <- R0 - P *)
240 let R0 := fst (proj1_sig (cswap_co_z_points b (zaddu_co_z_points R0R1))) in
241 R0).
242 Next Obligation. Proof. t. Qed.
243
244 Section Proofs.
245
246 Local Notation scalarmult := (@scalarmult_ref Wpoint Wadd Wzero Wopp).
247 Local Notation scalarmult':= (@scalarmult_ref point add zero opp).
248
249 Local Instance Equivalence_Weq : Equivalence Weq.
250 Proof. apply Wgroup.(Hierarchy.commutative_group_group).(Hierarchy.group_monoid).(Hierarchy.monoid_Equivalence). Qed.
251
252 Lemma Pgroup :
253 @Hierarchy.group point eq add zero opp.
254 Proof.
255 destruct (@Group.group_by_isomorphism _ _ _ _ _ point eq add zero opp of_affine to_affine Wgroup.(Hierarchy.commutative_group_group) ltac:(apply Jacobian.to_affine_of_affine)); auto.
256 - intros; split; intros.
257 + rewrite <- (Jacobian.of_affine_to_affine a0), <- (Jacobian.of_affine_to_affine b0).
258 rewrite H. reflexivity.
259 + apply Jacobian.Proper_to_affine; auto.
260 - apply Jacobian.to_affine_add.
261 - intros. destruct a0 as (((X & Y) & Z) & HP).
262 unfold to_affine, Wopp, opp, Weq. simpl.
263 t.
264 - rewrite Jacobian.to_affine_of_affine. reflexivity.
265 Qed.
266
267 Lemma scalarmult_scalarmult' (n : Z) (P : Wpoint) :
268 eq (of_affine (scalarmult n P)) (scalarmult' n (of_affine P)).
269 Proof.
270 eapply (@homomorphism_scalarmult Wpoint Weq Wadd Wzero Wopp Wgroup.(Hierarchy.commutative_group_group) point eq add zero opp Pgroup scalarmult (@scalarmult_ref_is_scalarmult Wpoint Weq Wadd Wzero Wopp Wgroup.(Hierarchy.commutative_group_group)) scalarmult' (@scalarmult_ref_is_scalarmult point eq add zero opp Pgroup) of_affine ltac:(econstructor; [eapply Jacobian.of_affine_add|eapply Jacobian.Proper_of_affine])).
271 Qed.
272
273 Context {scalarbitsz : Z}
274 {Hscalarbitsz : (2 <= scalarbitsz)%Z}
275 {ordP : Z} {HordPpos : (2 < ordP)%Z}
276 {HordPodd : Z.odd ordP = true :> bool}.
277
278 Section Inner.
279 (* Proofs about joye_ladder_inner *)
280
281 (* Bit 0 of the scalar input is irrelevant *)
282 Lemma joye_ladder_inner_bit0_irr (bitsz : Z) (testbit0 testbit1 : Z -> bool)
283 (P : point) (HPaff : z_of P = 1)
284 (bit0_irr : forall i, (i >= 1)%Z -> testbit0 i = testbit1 i :> bool) :
285 eq (joye_ladder_inner bitsz testbit0 P HPaff)
286 (joye_ladder_inner bitsz testbit1 P HPaff).
287 Proof.
288 unfold joye_ladder_inner.
289 rewrite (surjective_pairing (while _ _ _ (_, testbit0 _, _))).
290 rewrite (surjective_pairing (while _ _ _ (_, testbit1 _, _))).
291 rewrite (surjective_pairing (fst (while _ _ _ (_, testbit0 _, _)))).
292 rewrite (surjective_pairing (fst (while _ _ _ (_, testbit1 _, _)))).
293 rewrite bit0_irr by lia.
294 match goal with
295 | |- eq (snd (proj1_sig (cswap_co_z_points (snd (fst (while ?T0 ?B0 ?F0 ?I0))) _)))
296 (snd (proj1_sig (cswap_co_z_points (snd (fst (while ?T1 ?B1 ?F1 ?I1))) _))) =>
297 set (test0 := T0);
298 set (body0 := B0);
299 set (fuel := F0);
300 set (init0 := I0);
301 set (body1 := B1)
302 end.
303 eelim (while.preservation test0 body0 test0 body1 (fun s1 s2 => (s1 = s2 :> (co_z_points * bool * Z)) /\ (2 <= snd s1)%Z)).
304 { intros A B. rewrite A. reflexivity. }
305 - intros s1 s2 (<- & _). reflexivity.
306 - unfold test0. intros ((PQ1 & b1) & i1) ((PQ2 & b2) & i2) _.
307 cbn [fst snd]. intros (Heq & Hi). inversion Heq; clear Heq.
308 subst PQ1; subst b1; subst i1.
309 unfold body0, body1. rewrite bit0_irr by lia.
310 split; [reflexivity|simpl; lia].
311 - repeat (split; try reflexivity).
312 Qed.
313
314 Context {n : Z} {Hnodd : n = Z.setbit n 0 :> Z}
315 {Hn : (2 <= n < 2^scalarbitsz)%Z}
316 {P : point} {HPaff : z_of P = 1}
317 {HordP : forall l, (eq (scalarmult' l P) zero) <-> exists k, (l = k * ordP :> Z)%Z}.
318 Local Notation testbitn := (Z.testbit n).
319 Context {HSS : forall i, (2 <= i <= scalarbitsz)%Z -> not (eq (scalarmult' (SS n (Z.to_nat i)) P) zero)}
320 {HTT : forall i, (2 <= i <= scalarbitsz)%Z -> not (eq (scalarmult' (TT n (Z.to_nat i)) P) zero)}.
321
322 Lemma mult_two_power (k : Z) :
323 (0 <= k)%Z ->
324 not (eq (scalarmult' (2^k)%Z P) zero).
325 Proof.
326 eapply (Zlt_0_ind (fun x => ~ eq (scalarmult' (2 ^ x) P) zero)).
327 intros x Hind Hxpos Heqz.
328 destruct (proj1 (HordP (2^x)%Z) Heqz) as [l Hl].
329 destruct (Z.eq_dec x 0); [subst x|].
330 - simpl in Hl. clear -Hl HordPpos.
331 generalize (Z.divide_1_r_nonneg ordP ltac:(lia) ltac:(exists l; lia)).
332 lia.
333 - assert (x = Z.succ (Z.pred x) :> Z) by lia.
334 rewrite H in Hl. rewrite Z.pow_succ_r in Hl; [|lia].
335 generalize (Znumtheory.prime_mult 2%Z Znumtheory.prime_2 l ordP ltac:(exists (2 ^ Z.pred x)%Z; lia)).
336 intros [A|A]; destruct A as [m Hm]; [|replace ordP with (0 + 2 * m)%Z in HordPodd by lia; rewrite Z.odd_add_mul_2 in HordPodd; simpl in HordPodd; congruence].
337 rewrite Hm in Hl.
338 assert ((2 ^ Z.pred x)%Z = (m * ordP)%Z :> Z) by lia.
339 apply (Hind (Z.pred x) ltac:(lia)).
340 eapply HordP. exists m; assumption.
341 Qed.
342
343 Lemma mult_two (k : Z) :
344 eq (scalarmult' (2 * k)%Z P) zero ->
345 eq (scalarmult' k P) zero.
346 Proof.
347 intros Heqz. destruct (proj1 (HordP (2 * k)%Z) Heqz) as [l Hl].
348 generalize (Znumtheory.prime_mult 2%Z Znumtheory.prime_2 l ordP ltac:(exists k; lia)).
349 intros [A|A]; destruct A as [m Hm]; [|replace ordP with (0 + 2 * m)%Z in HordPodd by lia; rewrite Z.odd_add_mul_2 in HordPodd; simpl in HordPodd; congruence].
350 rewrite Hm in Hl. assert (k = m * ordP :> Z)%Z as -> by lia.
351 apply HordP; eauto.
352 Qed.
353
354 Lemma HSS_plus_TT (m : Z) (k : nat) :
355 not (eq (scalarmult' (SS m k + TT m k)%Z P) zero).
356 Proof. rewrite SS_plus_TT. apply mult_two_power. lia. Qed.
357
358 Lemma SS1 : SS n 1 = 1%Z :> Z.
359 Proof. cbv [SS]. rewrite Z.pow_1_r, <-Z.bit0_mod, Hnodd, Z.setbit_eqb; trivial; lia. Qed.
360
361 Lemma TT1 : TT n 1 = 1%Z :> Z.
362 Proof. cbv [TT]. rewrite SS1; trivial. Qed.
363
364 Lemma SS2 : SS n 2 = (if Z.testbit n 1 then 3%Z else 1%Z) :> Z.
365 Proof.
366 cbv [SS]; rewrite Hnodd, <-Z.land_ones, Z.land_comm by lia.
367 destruct n; cbn; trivial; repeat (destruct p; cbn; trivial).
368 Qed.
369
370 Lemma TT2 : TT n 2 = (if Z.testbit n 1 then 1%Z else 3%Z) :> Z.
371 Proof. cbv [TT]. rewrite SS2. case Z.testbit; lia. Qed.
372
373 Lemma SS_TT2 : (if testbitn 1 then SS n 2 else TT n 2) = 3%Z :> Z.
374 Proof. rewrite SS2, TT2; try lia; case testbitn; trivial. Qed.
375
376 Lemma HordP3 :
377 not (eq (scalarmult' 3%Z P) zero).
378 Proof.
379 rewrite <- SS_TT2, <- (Nat2Z.id 2).
380 case testbitn; [eapply HSS|eapply HTT]; lia.
381 Qed.
382
383 Hint Unfold fst snd proj1_sig : points_as_coordinates.
384 Hint Unfold fieldwise fieldwise' : points_as_coordinates.
385
386 Lemma eq_fieldwise (P1 P2 : point) :
387 fieldwise (n:=3) Feq
388 (proj1_sig P1) (proj1_sig P2) ->
389 eq P1 P2.
390 Proof. clear -field; intros; t. Qed.
391
392 Lemma Pynz :
393 y_of P <> 0.
394 Proof.
395 intro Hy. assert (HA : eq P (opp P)).
396 - apply eq_fieldwise. destruct P as (((X & Y) & Z) & HP).
397 simpl; cbv in HPaff, Hy; repeat split; fsatz.
398 - apply (mult_two_power 1%Z ltac:(lia)).
399 replace (2 ^ 1)%Z with (1 - -1)%Z by lia.
400 rewrite (scalarmult_sub_l (groupG:=Pgroup) (mul_is_scalarmult:=scalarmult_ref_is_scalarmult (groupG:=Pgroup))).
401 rewrite <- (scalarmult_1_l (groupG:=Pgroup) (mul_is_scalarmult:=scalarmult_ref_is_scalarmult (groupG:=Pgroup))) in HA.
402 rewrite HA.
403 rewrite <- (scalarmult_opp_l (groupG:=Pgroup) (mul_is_scalarmult:=scalarmult_ref_is_scalarmult (groupG:=Pgroup))).
404 rewrite <- (scalarmult_sub_l (groupG:=Pgroup) (mul_is_scalarmult:=scalarmult_ref_is_scalarmult (groupG:=Pgroup))).
405 replace (- (1) - -1)%Z with 0%Z by lia. reflexivity.
406 Qed.
407
408 Lemma add_opp_zero (A : point) :
409 eq (add A (opp A)) zero.
410 Proof.
411 generalize (Jacobian.add_opp_same_r(discriminant_nonzero:=discriminant_nonzero) A).
412 destruct (add A (opp A)) as (((X & Y) & Z) & H).
413 cbn. intros HP; destruct (dec (Z = 0)); t.
414 Qed.
415
416 Lemma scalarmult_difference (A : point) (n1 n2 : Z):
417 eq (scalarmult' n1 A) (scalarmult' n2 A) ->
418 eq (scalarmult' (n1 - n2)%Z A) zero.
419 Proof.
420 intros. rewrite (scalarmult_sub_l (groupG:=Pgroup) (mul_is_scalarmult:=scalarmult_ref_is_scalarmult (groupG:=Pgroup))), H, <- (scalarmult_sub_l (groupG:=Pgroup) (mul_is_scalarmult:=scalarmult_ref_is_scalarmult (groupG:=Pgroup))), Z.sub_diag.
421 apply (scalarmult_0_l (is_scalarmult:=scalarmult_ref_is_scalarmult (groupG:=Pgroup))).
422 Qed.
423
424 Lemma dblu_scalarmult' (Q : point) (Hz1 : z_of Q = 1) (Hynz : y_of Q <> 0) :
425 let '(M, N) := dblu Q Hz1 in
426 eq M (scalarmult' 2 Q)
427 /\ eq N (scalarmult' 1 Q).
428 Proof.
429 generalize (@Jacobian.dblu_correct F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv a b field char_ge_3 Feq_dec char_ge_12 Q Hz1 Hynz).
430 rewrite (surjective_pairing (dblu _ _)) at 1.
431 intros (A & B & C). split.
432 - rewrite <- A. replace 2%Z with (1 + 1)%Z by lia.
433 rewrite (scalarmult_add_l (groupG:=Pgroup) (mul_is_scalarmult:=scalarmult_ref_is_scalarmult (groupG:=Pgroup))).
434 rewrite (scalarmult_1_l (groupG:=Pgroup) (mul_is_scalarmult:=scalarmult_ref_is_scalarmult (groupG:=Pgroup))).
435 rewrite <- Jacobian.add_double; reflexivity.
436 - rewrite (scalarmult_1_l (groupG:=Pgroup) (mul_is_scalarmult:=scalarmult_ref_is_scalarmult (groupG:=Pgroup))).
437 symmetry. assumption.
438 Qed.
439
440 Lemma co_xz_implies (P1 P2 : point) (Hxeq : x_of P1 = x_of P2)
441 (Hzeq : z_of P1 = z_of P2) :
442 eq P1 P2 \/ eq P1 (opp P2).
443 Proof.
444 clear -Hxeq Hzeq. prept; [tauto|fsatz|fsatz|].
445 assert (f4 = f1 \/ f4 = Fopp f1) by (destruct (dec (f4 = f1)); [left; assumption|right; fsatz]).
446 destruct H; [left; repeat split; fsatz|right; repeat split; fsatz].
447 Qed.
448
449 Lemma tplu_scalarmult' {p q} (H : tplu P HPaff = (p, q) :> _) :
450 eq p (scalarmult' 3 P) /\ eq q (scalarmult' 1 P) /\ co_z p q.
451 Proof.
452 intros; unfold tplu. generalize (dblu_scalarmult' P HPaff Pynz).
453 inversion_prod; subst p q.
454 rewrite (surjective_pairing (dblu _ _)) at 1.
455 set (P1 := (snd (dblu P HPaff))).
456 set (P2 := (fst (dblu P HPaff))). intros [A1 A2].
457 destruct (dec (x_of P1 = x_of P2)) as [Hxe|Hxne].
458 { destruct (co_xz_implies P1 P2 Hxe (CoZ.Jacobian.tplu_obligation_1 P HPaff)) as [A|A].
459 - rewrite A1, A2 in A. elim (mult_two_power 1%Z ltac:(lia)).
460 rewrite <- A. replace 1%Z with (2 - 1)%Z by lia.
461 apply scalarmult_difference; symmetry; assumption.
462 - rewrite A1, A2, <- (scalarmult_opp_l (groupG:=Pgroup) (mul_is_scalarmult:=scalarmult_ref_is_scalarmult (groupG:=Pgroup))) in A.
463 apply scalarmult_difference in A.
464 elim HordP3. exact A. }
465 generalize (Jacobian.zaddu_correct _ _ (CoZ.Jacobian.tplu_obligation_1 P HPaff) Hxne).
466 rewrite (surjective_pairing (zaddu _ _ _)) at 1.
467 intros (A & B & C); subst P1 P2.
468 repeat try split; trivial.
469 { rewrite <-A, A1, A2, (@scalarmult_add_l point eq add zero opp Pgroup scalarmult' (@scalarmult_ref_is_scalarmult _ _ _ _ _ Pgroup) 1 2); reflexivity. }
470 { rewrite <-B. rewrite A2. reflexivity. }
471 Qed.
472
473 (* Since co-Z formulas are incomplete, we need to show that we won't hit the neutral point for ZDAU in the loop *)
474 Lemma zaddu_SS_TT (i : Z) (B1 B2 Y1 Y2 R0 R1 : point) (HB12 : co_z B1 B2)
475 (Hi : (2 <= i < scalarbitsz)%Z)
476 (HBx : x_of B1 <> x_of B2)
477 (HY : zaddu B1 B2 HB12 = (Y1, Y2) :> point * point)
478 (HR0 : eq R0 (scalarmult' (SS n (Z.to_nat i)) P))
479 (HR1 : eq R1 (scalarmult' (TT n (Z.to_nat i)) P))
480 (HB1 : B1 = (if testbitn i then R0 else R1) :> point)
481 (HB2 : B2 = (if testbitn i then R1 else R0) :> point) :
482 x_of Y1 <> x_of Y2.
483 Proof.
484 intro XX. generalize (Jacobian.zaddu_correct B1 B2 HB12 HBx).
485 rewrite HY. intros (W1 & W2 & W3).
486 destruct (co_xz_implies _ _ XX W3) as [W|W]; rewrite W, <- W2, HB1, HB2 in W1.
487 - destruct (testbitn i);
488 rewrite HR1, HR0, <- (scalarmult_add_l (groupG:=Pgroup) (mul_is_scalarmult:=scalarmult_ref_is_scalarmult (groupG:=Pgroup))) in W1;
489 apply scalarmult_difference in W1;
490 rewrite Z.add_simpl_l in W1;
491 [apply (HTT i ltac:(lia))|apply (HSS i ltac:(lia))]; auto.
492 - destruct (testbitn i) eqn:Hti;
493 rewrite HR1, HR0, <- (scalarmult_add_l (groupG:=Pgroup) (mul_is_scalarmult:=scalarmult_ref_is_scalarmult (groupG:=Pgroup))), <- (scalarmult_opp_l (groupG:=Pgroup) (mul_is_scalarmult:=scalarmult_ref_is_scalarmult (groupG:=Pgroup))) in W1;
494 apply scalarmult_difference in W1.
495 all: match goal with
496 | H : eq (scalarmult' ?X _) zero |- _ =>
497 match X with
498 | (SS _ _ + _ - _)%Z =>
499 replace X with (SS n (S (Z.to_nat i))) in H by (rewrite SS_succ, Z2Nat.id, Hti; lia)
500 | (TT _ _ + _ - _)%Z =>
501 replace X with (TT n (S (Z.to_nat i))) in H by (rewrite TT_succ, Z2Nat.id, Hti; lia)
502 end
503 end.
504 all: rewrite <- Z2Nat.inj_succ in W1; try lia.
505 all: match goal with
506 | H : eq (scalarmult' (SS _ _) _) zero |- _ =>
507 apply (HSS (Z.succ i) ltac:(lia))
508 | _ =>
509 apply (HTT (Z.succ i) ltac:(lia))
510 end; auto.
511 Qed.
512
513 Hint Unfold Jacobian.of_affine_impl : points_as_coordinates.
514
515 Lemma SS_TT_xne (i : Z) (R0 R1 : point) (HCOZ : co_z R0 R1)
516 (Hi : (2 <= i < scalarbitsz)%Z)
517 (HR0 : eq R0 (scalarmult' (SS n (Z.to_nat i)) P))
518 (HR1 : eq R1 (scalarmult' (TT n (Z.to_nat i)) P)) :
519 x_of R0 <> x_of R1.
520 Proof.
521 assert (HH : forall A, eq (opp A) zero -> eq A zero) by (clear -field; unfold zero, Wzero; intros; t).
522 intros Hxe. generalize (co_xz_implies _ _ Hxe HCOZ).
523 destruct (Z.eq_dec 2 i).
524 { subst i. replace (Z.to_nat 2) with 2%nat in HR1 by lia.
525 replace (Z.to_nat 2) with 2%nat in HR0 by lia.
526 rewrite TT2 in HR1. rewrite SS2 in HR0.
527 rewrite HR0, HR1, <- (scalarmult_opp_l (groupG:=Pgroup) (mul_is_scalarmult:=scalarmult_ref_is_scalarmult (groupG:=Pgroup))).
528 destruct (testbitn 1) eqn:Htj; intros [Q|Q]; apply scalarmult_difference in Q.
529 all: match goal with
530 | H : eq (scalarmult' ?X _) zero |- _ =>
531 try replace X with 2%Z in H by lia;
532 try replace X with 4%Z in H by lia;
533 try replace X with (- (2))%Z in H by lia
534 end.
535 all: match goal with
536 | H : eq (scalarmult' ?X _) zero |- _ =>
537 match X with
538 | 2%Z => apply (mult_two_power 1%Z ltac:(lia) H)
539 | 4%Z => apply (mult_two_power 2%Z ltac:(lia) H)
540 | (- (2))%Z =>
541 rewrite (scalarmult_opp_l (groupG:=Pgroup) (mul_is_scalarmult:=scalarmult_ref_is_scalarmult (groupG:=Pgroup))) in Q;
542 apply HH in H;
543 apply (mult_two_power 1%Z ltac:(lia) H)
544 end
545 end. }
546 { set (j := Z.pred i). assert (His : i = Z.succ j :> Z) by lia.
547 assert (Hj : (2 <= j)%Z) by lia.
548 rewrite His, Z2Nat.inj_succ in HR0, HR1 by lia.
549 rewrite HR0, HR1, <- (scalarmult_opp_l (groupG:=Pgroup) (mul_is_scalarmult:=scalarmult_ref_is_scalarmult (groupG:=Pgroup))).
550 rewrite TT_succ, SS_succ, Z2Nat.id by lia.
551 destruct (testbitn j) eqn:Htj; intros [Q|Q]; apply scalarmult_difference in Q.
552 all: try match goal with
553 | H : eq (scalarmult' ?X _) zero |- _ =>
554 match X with
555 | Z.sub _ ?Y =>
556 match Y with
557 | (- _)%Z =>
558 replace X with (2 * (SS n (Z.to_nat j) + TT n (Z.to_nat j)))%Z in H by lia;
559 apply mult_two in H;
560 apply (HSS_plus_TT n (Z.to_nat j) H)
561 | (TT _ _) =>
562 replace X with (2 * SS n (Z.to_nat j))%Z in H by lia;
563 apply mult_two in H;
564 apply (HSS j ltac:(lia) H)
565 | (SS _ _) =>
566 replace X with (2 * TT n (Z.to_nat j))%Z in H by lia;
567 apply mult_two in H;
568 apply (HTT j ltac:(lia) H)
569 | (Z.add _ _) =>
570 replace X with (2 * - TT n (Z.to_nat j))%Z in H by lia;
571 apply mult_two in H;
572 rewrite (scalarmult_opp_l (groupG:=Pgroup) (mul_is_scalarmult:=scalarmult_ref_is_scalarmult (groupG:=Pgroup))) in H;
573 apply HH in H;
574 apply (HTT j ltac:(lia) H)
575 end
576 end
577 end. }
578 Qed.
579
580 (* When n is odd, joye_ladder_inner computes [n]P *)
581 Lemma joye_ladder_inner_correct :
582 eq (joye_ladder_inner scalarbitsz testbitn P HPaff)
583 (scalarmult' n P).
584 Proof.
585 unfold joye_ladder_inner. set (WW := while _ _ _ _).
586 destruct (tplu_co_z_points P HPaff) as ((A1 & A2) & HA12) eqn:Htplu.
587 rewrite (sig_eta (tplu_co_z_points _ _)) in Htplu.
588 apply proj1_sig_eq in Htplu; simpl in Htplu.
589 (* Initialize the ladder state with ([3]P, [1]P) or its symmetric *)
590 destruct (tplu_scalarmult' Htplu) as (HeqA1 & HeqA2 & _).
591 set (inv :=
592 fun '(R1R0, swap, i) =>
593 let '(R1, R0) := proj1_sig (R1R0:co_z_points) in
594 (2 <= i <= scalarbitsz)%Z /\
595 (eq (if (swap: bool) then R0 else R1) (scalarmult' (TT n (Z.to_nat i)) P)
596 /\ eq (if swap then R1 else R0) (scalarmult' (SS n (Z.to_nat i)) P))
597 /\ ((i < scalarbitsz)%Z -> x_of R1 <> x_of R0)
598 /\ (swap = testbitn (i - 1) :> bool)).
599 assert (HH : forall (A B : Prop), A -> (A -> B) -> A /\ B) by tauto.
600 assert (WWinv : inv WW /\ (snd WW = scalarbitsz :> Z) /\ (snd (fst WW) = testbitn (scalarbitsz - 1) :> bool)).
601 { set (measure := fun (state : (co_z_points*bool*Z)) => ((Z.to_nat scalarbitsz) - (Z.to_nat (snd state)))%nat).
602 unfold WW. replace (Z.to_nat scalarbitsz - 2)%nat with (measure (exist _ (A1, A2) HA12, testbitn 1, 2%Z)) by (unfold measure; simpl; lia).
603 eapply (while.by_invariant inv measure (fun s => inv s /\ (snd s = scalarbitsz :> Z) /\ (snd (fst s) = testbitn (scalarbitsz - 1) :> bool))).
604 - (* Invariant holds at beginning *)
605 unfold inv. cbn [proj1_sig].
606 split; [lia|]. apply HH.
607 + change (Z.to_nat 2) with 2%nat.
608 rewrite SS2, TT2.
609 case Z.testbit; auto.
610 + intros [He1 He2]. split.
611 * intros Hxe.
612 generalize (SS_TT_xne 2%Z (if testbitn 1 then A1 else A2) (if testbitn 1 then A2 else A1) ltac:(destruct (testbitn 1); [|apply co_z_comm]; exact HA12) ltac:(lia) He2 He1).
613 case Z.testbit; [|symmetry]; auto.
614 * reflexivity.
615 - (* Invariant is preserved by the loop,
616 measure decreases,
617 and post-condition i = scalarbitsz. *)
618 intros s Hs. destruct s as ((R1R0 & swap) & i).
619 destruct R1R0 as ((R1 & R0) & HCOZ).
620 destruct Hs as (Hi & (HR1 & HR0) & Hx & Hswape).
621 destruct (Z.ltb i scalarbitsz) eqn:Hltb.
622 + apply Z.ltb_lt in Hltb.
623 split.
624 * (* Invariant preservation.
625 The loop body is basically :
626 (R1, R0) <- cswap (testbitn i) (R1, R0);
627 (R1, R0) <- ZDAU (R1, R0);
628 (R1, R0) <- cswap (testbitn i) (R1, R0);
629 *)
630 (* Start by giving names to all intermediate values *)
631 unfold inv. destruct (cswap_co_z_points (xorb swap (testbitn i)) (exist _ _ _)) as ((B1 & B2) & HB12) eqn:Hswap1.
632 rewrite (sig_eta (cswap_co_z_points _ _)) in Hswap1.
633 apply proj1_sig_eq in Hswap1. simpl in Hswap1.
634 assert (HB1: B1 = (if xorb swap (testbitn i) then R0 else R1) :> point) by (destruct (xorb swap (testbitn i)); congruence).
635 assert (HB2: B2 = (if xorb swap (testbitn i) then R1 else R0) :> point) by (destruct (xorb swap (testbitn i)); congruence).
636 clear Hswap1.
637 destruct (zdau_co_z_points _) as ((C1 & C2) & HC12) eqn:HZDAU.
638 rewrite (sig_eta (zdau_co_z_points _)) in HZDAU.
639 apply proj1_sig_eq in HZDAU. simpl in HZDAU.
640 assert (HBx : x_of B1 <> x_of B2) by (rewrite HB1, HB2; destruct (xorb swap (testbitn i)); [symmetry|]; auto).
641 destruct (zaddu B1 B2 (zdau_co_z_points_obligation_1 (exist (fun '(A, B) => co_z A B) (B1, B2) HB12) B1 B2 eq_refl)) as (Y1 & Y2) eqn:HY.
642 assert (HYx : x_of Y1 <> x_of Y2) by (eapply zaddu_SS_TT; eauto; try lia; destruct swap; destruct (testbitn i); auto).
643 generalize (@Jacobian.zdau_correct_alt F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv a b field char_ge_3 Feq_dec char_ge_12 ltac:(unfold id in *; fsatz) B1 B2 (zdau_co_z_points_obligation_1 (exist (fun '(A, B) => co_z A B) (B1, B2) HB12) B1 B2 eq_refl) HBx ltac:(rewrite HY; simpl; apply HYx)).
644 rewrite HZDAU. intros (HC1 & HC2 & _).
645 (* clear HD. *) simpl.
646 (* invariant preservation *)
647 (* counter still within bounds *)
648 split; [lia|]. (* rewrite HD1, HD2. *) apply HH.
649 { (* New values are indeed [SS (i+1)]P and [TT (i+1)]P *)
650 destruct (testbitn i) eqn:Hti;
651 destruct swap eqn:Hswap;
652 rewrite <- HC1, <- HC2, HB1, HB2;
653 replace (Z.to_nat (Z.succ i)) with (S (Z.to_nat i)) by lia;
654 rewrite SS_succ, TT_succ, Z2Nat.id by lia;
655 rewrite Hti; split; try assumption;
656 rewrite <- Jacobian.add_double; try reflexivity;
657 cbv [xorb]; rewrite HR0, HR1;
658 repeat rewrite <- (scalarmult_add_l (groupG:=Pgroup) (mul_is_scalarmult:=scalarmult_ref_is_scalarmult (groupG:=Pgroup)));
659 rewrite <- Z.add_diag; reflexivity. }
660 { (* Make sure we don't hit bad values *)
661 intros [He1 He2].
662 split; [|f_equal; lia].
663 intros Hxe He. eapply (SS_TT_xne (Z.succ i) (if testbitn i then C1 else C2) (if testbitn i then C2 else C1)); eauto.
664 - destruct (testbitn i); [|apply co_z_comm]; auto.
665 - lia.
666 - destruct (testbitn i); [|symmetry]; auto.
667 }
668 * (* measure decreases *)
669 apply Z.ltb_lt in Hltb.
670 unfold measure; simpl; lia.
671 + (* Post-condition *)
672 simpl; split; auto.
673 rewrite Z.ltb_nlt in Hltb. split; [|rewrite Hswape; f_equal]; try lia. }
674 destruct WWinv as (Hinv & Hj & Hswap).
675 destruct WW as ((R1R0 & swap) & i). destruct R1R0 as ((R1 & R0) & HCOZ).
676 simpl in Hj; subst i. simpl in Hswap; subst swap.
677 destruct Hinv as (_ & (_ & HR0) & _).
678 rewrite (SSn n scalarbitsz ltac:(lia) ltac:(lia)) in HR0.
679 simpl. destruct (testbitn (scalarbitsz - 1)); simpl; exact HR0.
680 Qed.
681 End Inner.
682
683 (* We compute [n]P where P ≠ ∞ and n < ord(P) *)
684 Context {n : Z} {Hn : (2 <= n < 2^scalarbitsz)%Z}
685 {P : Wpoint} {HPnz : P <> ∞ :> Wpoint}
686 {HordP : forall l, (Weq (scalarmult l P) ∞) <-> exists k, (l = k * ordP :> Z)%Z}
687 {HordPn : (n + 2 < ordP)%Z}.
688
689 Local Notation testbitn := (Z.testbit n).
690 (* Local Notation n' := (if testbitn 0 then n else (n + 1)%Z). *)
691 Local Notation n' := (Z.setbit n 0) (only parsing).
692 Local Notation testbitn' := (Z.testbit n') (only parsing).
693
694 (* Technically, if q is the order of the curve, and scalarbitsz = [log2 q],
695 and ordP is close to q, then only the last two values of SS and TT are
696 dangerous.
697 See §3.1 of "Faster Montgomery and double-add ladders for short
698 Weierstrass curves" (Mike Hamburg, CHES'20) for instance.
699 *)
700 Context {HSS : forall i, (2 <= i <= scalarbitsz)%Z -> not (Weq (scalarmult (SS n' (Z.to_nat i)) P) ∞) }
701 {HTT : forall i, (2 <= i <= scalarbitsz)%Z -> not (Weq (scalarmult (TT n' (Z.to_nat i)) P) ∞) }.
702
703 Lemma Hn' : (2 <= n' < 2^scalarbitsz)%Z.
704 Proof.
705 rewrite Z.setbit_spec'; simpl; split.
706 - etransitivity; [|apply LandLorShiftBounds.Z.lor_lower]; lia.
707 - apply (LandLorShiftBounds.Z.lor_range n 1 scalarbitsz); lia.
708 Qed.
709
710 Lemma scalarmult_eq_weq_conversion (k1 k2 : Z) :
711 Weq (scalarmult k1 P) (scalarmult k2 P) <-> eq (scalarmult' k1 (of_affine P)) (scalarmult' k2 (of_affine P)).
712 Proof.
713 split; intros.
714 - repeat rewrite <- scalarmult_scalarmult'.
715 eapply Jacobian.Proper_of_affine. apply H.
716 - rewrite <- Jacobian.to_affine_of_affine at 1.
717 symmetry. rewrite <- Jacobian.to_affine_of_affine at 1.
718 apply Jacobian.Proper_to_affine.
719 symmetry; repeat rewrite scalarmult_scalarmult'; auto.
720 Qed.
721
722 Lemma HordP_alt (k : Z) :
723 (0 < k < ordP)%Z ->
724 not (Weq (scalarmult k P) ∞).
725 Proof.
726 intros Hbds Heq. destruct (proj1 (HordP k) Heq) as [l Hl].
727 clear -Hbds Hl. generalize (Zmult_gt_0_lt_0_reg_r ordP l ltac:(lia) ltac:(lia)).
728 intros. generalize (proj1 (Z.mul_le_mono_pos_r 1%Z l ordP ltac:(lia)) ltac:(lia)). lia.
729 Qed.
730
731 Lemma n'_alt :
732 n' = (if testbitn 0 then n else (n + 1)%Z) :> Z.
733 Proof.
734 apply Z.bits_inj'; intros.
735 destruct (Z.eq_dec n0 0) as [->|?]; [rewrite Z.setbit_eq|rewrite Z.setbit_neq]; try lia.
736 - destruct (testbitn 0) eqn:Hbit0; auto.
737 rewrite Z.bit0_odd, <- Z.even_pred.
738 replace (Z.pred (n + 1))%Z with n by lia.
739 rewrite <- Z.negb_odd, <- Z.bit0_odd, Hbit0; reflexivity.
740 - destruct (testbitn 0) eqn:Hbit0; auto.
741 replace n0 with (Z.succ (n0 - 1))%Z by lia.
742 rewrite Z.bit0_odd in Hbit0.
743 rewrite (Zeven_div2 n); [|apply Zeven_bool_iff; rewrite <- Z.negb_odd, Hbit0; reflexivity].
744 rewrite Z.testbit_even_succ, Z.testbit_odd_succ; auto; lia.
745 Qed.
746
747 Lemma joye_ladder_correct :
748 Weq (joye_ladder scalarbitsz testbitn P HPnz) (scalarmult n P).
749 Proof.
750 (* Unfold the ladder *)
751 rewrite <- (Jacobian.to_affine_of_affine (scalarmult n P)).
752 apply Jacobian.Proper_to_affine. rewrite scalarmult_scalarmult'.
753 set (P' := of_affine P). set (HPaff := joye_ladder_obligation_1 P HPnz).
754 assert (Hnodd : n' = Z.setbit n' 0 :> Z) by (repeat rewrite Z.setbit_spec'; rewrite <- Z.lor_assoc, Z.lor_diag; reflexivity).
755 assert (HordP' : forall l, (eq (scalarmult' l P') zero) <-> exists k, (l = k * ordP :> Z)%Z).
756 { intros l; split; replace zero with (scalarmult' 0%Z P') by reflexivity.
757 - intros H; apply scalarmult_eq_weq_conversion in H; apply HordP; auto.
758 - intros H; apply scalarmult_eq_weq_conversion; apply HordP; auto. }
759 assert (HSS' : forall i, (2 <= i <= scalarbitsz)%Z -> not (eq (scalarmult' (SS n' (Z.to_nat i)) P') zero)).
760 { replace zero with (scalarmult' 0%Z P') by reflexivity.
761 intros i Hi He; apply scalarmult_eq_weq_conversion in He; apply (HSS i Hi); auto. }
762 assert (HTT' : forall i, (2 <= i <= scalarbitsz)%Z -> not (eq (scalarmult' (TT n' (Z.to_nat i)) P') zero)).
763 { replace zero with (scalarmult' 0%Z P') by reflexivity.
764 intros i Hi He; apply scalarmult_eq_weq_conversion in He; apply (HTT i Hi); auto. }
765 generalize (joye_ladder_inner_correct (n:=n') (P:=of_affine P) (HPaff:=HPaff) (Hnodd:=Hnodd) (Hn:=Hn') (HordP:=HordP')(HSS:=HSS') (HTT:=HTT')).
766 intros Hinner. rewrite (joye_ladder_inner_bit0_irr scalarbitsz testbitn' testbitn (of_affine P) HPaff ltac:(intros; rewrite Z.setbit_neq; trivial; lia)) in Hinner.
767 set (R0 := joye_ladder_inner scalarbitsz testbitn P' HPaff).
768 cbv zeta. fold R0. fold P' in Hinner. fold R0 in Hinner.
769 (* We now have R0 = [n']P *)
770 destruct (make_co_z_points R0 _ _) as ((B1 & B2) & HB12) eqn:HMCZ.
771 rewrite (sig_eta (make_co_z_points _ _ _)) in HMCZ.
772 apply proj1_sig_eq in HMCZ; simpl in HMCZ.
773 (* Prove [n']P ≠ ∞ *)
774 assert (HR0znz : z_of R0 <> 0).
775 { intro. apply (HSS scalarbitsz ltac:(lia)).
776 replace Wzero with (scalarmult 0%Z P) by reflexivity.
777 apply scalarmult_eq_weq_conversion.
778 rewrite SSn by (generalize Hn'; lia).
779 fold P'. rewrite <- Hinner. destruct R0 as (((? & ?) & ?) & ?).
780 cbn in H; cbn. clear -field H.
781 destruct (dec (f1 = 0)); fsatz. }
782 (* Have co-Z representations of [n']P and [-1]P *)
783 generalize (Jacobian.make_co_z_correct R0 (opp P') (opp_of_affine P HPnz) HR0znz).
784 rewrite HMCZ. intros (HB1 & HB2 & _).
785 destruct (zaddu_co_z_points _) as ((C1 & C2) & HC12) eqn:HZADDU.
786 rewrite (sig_eta (zaddu_co_z_points _)) in HZADDU.
787 apply proj1_sig_eq in HZADDU. simpl in HZADDU.
788 (* Ensure ZADDU doesn't hit the neutral point *)
789 assert (Hxne: x_of B1 <> x_of B2).
790 { intro Hxe. destruct (co_xz_implies _ _ Hxe HB12) as [HEq|HNeq]; [rewrite <- HB1, Hinner, <- HB2 in HEq|rewrite <- HB1, Hinner, <- HB2 in HNeq].
791 - rewrite <- (scalarmult_opp1_l (groupG:=Pgroup) (mul_is_scalarmult:=scalarmult_ref_is_scalarmult (groupG:=Pgroup))) in HEq.
792 apply scalarmult_difference in HEq.
793 apply (HordP_alt (n' - -1)%Z).
794 + rewrite n'_alt; destruct (testbitn 0); lia.
795 + replace Wzero with (scalarmult 0 P) by reflexivity.
796 apply scalarmult_eq_weq_conversion. auto.
797 - rewrite (Group.inv_inv (H:=Pgroup)) in HNeq.
798 rewrite <- (scalarmult_1_l (groupG:=Pgroup) (mul_is_scalarmult:=scalarmult_ref_is_scalarmult (groupG:=Pgroup)) P') in HNeq at 2.
799 apply scalarmult_difference in HNeq.
800 apply (HordP_alt (n' - 1)%Z).
801 + rewrite n'_alt; destruct (testbitn 0); lia.
802 + replace Wzero with (scalarmult 0 P) by reflexivity.
803 apply scalarmult_eq_weq_conversion. auto. }
804 generalize (Jacobian.zaddu_correct B1 B2 (zaddu_co_z_points_obligation_1 (exist (fun '(A, B) => co_z A B) (B1, B2) HB12) B1 B2 eq_refl) Hxne).
805 rewrite HZADDU. intros (HC1 & HC2 & _).
806 rewrite <- HB1, <- HB2, Hinner in HC1. rewrite <- HB1, Hinner in HC2.
807 destruct (cswap_co_z_points (testbitn 0) _) as ((D1 & D2) & HD12) eqn:Hswap.
808 rewrite (sig_eta (cswap_co_z_points _ _)) in Hswap.
809 apply proj1_sig_eq in Hswap. cbn [proj1_sig cswap_co_z_points] in Hswap.
810 simpl. assert (D1 = (if testbitn 0 then C2 else C1) :> point) as -> by (destruct (testbitn 0); congruence).
811 rewrite n'_alt in HC1, HC2.
812 destruct (testbitn 0); [rewrite <- HC2; reflexivity|rewrite <- HC1].
813 rewrite <- (scalarmult_opp1_l (groupG:=Pgroup) (mul_is_scalarmult:=scalarmult_ref_is_scalarmult (groupG:=Pgroup))), <- (scalarmult_add_l (groupG:=Pgroup) (mul_is_scalarmult:=scalarmult_ref_is_scalarmult (groupG:=Pgroup))).
814 replace (n + 1 + -1)%Z with n by lia. reflexivity.
815 Qed.
816 End Proofs.
817 End JoyeLadder.
818 End ScalarMult.