Debian Codegraph @

src/Curves/Montgomery/XZProofs.v

raw
1 Require Import Crypto.Algebra.Field.
2 Require Import Crypto.Algebra.ScalarMult.
3 Require Import Crypto.Util.Sum Crypto.Util.Prod Crypto.Util.LetIn.
4 Require Import Crypto.Util.Decidable.
5 Require Import Crypto.Util.Tuple.
6 Require Import Crypto.Util.ZUtil.Notations.
7 Require Import Crypto.Util.ZUtil.Tactics.LtbToLt.
8 Require Import Crypto.Util.ZUtil.Shift.
9 Require Import Crypto.Util.ZUtil.Peano.
10 Require Import Crypto.Util.Tactics.SetoidSubst.
11 Require Import Crypto.Util.Tactics.SpecializeBy.
12 Require Import Crypto.Util.Tactics.DestructHead.
13 Require Import Crypto.Util.Tactics.BreakMatch.
14 Require Import Crypto.Util.Tactics.UniquePose.
15 Require Import Crypto.Spec.MontgomeryCurve Crypto.Curves.Montgomery.Affine.
16 Require Import Crypto.Curves.Montgomery.AffineInstances.
17 Require Import Crypto.Curves.Montgomery.XZ BinPos.
18 From Coq Require Import Morphisms.
19 From Coq Require Import ZArith.
20 From Coq Require Import Lia.
21
22 Module M.
23 Section MontgomeryCurve.
24 Context {F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv}
25 {field:@Algebra.Hierarchy.field F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv}
26 {Feq_dec:Decidable.DecidableRel Feq}
27 {char_ge_28:@Ring.char_ge F Feq Fzero Fone Fopp Fadd Fsub Fmul 28}.
28
29 Lemma Private_char_ge_3: @Ring.char_ge F Feq Fzero Fone Fopp Fadd Fsub Fmul 3.
30 Proof. clear -char_ge_28; eapply Algebra.Hierarchy.char_ge_weaken; eauto; vm_decide. Qed.
31 Context {char_ge_3 : @Ring.char_ge F Feq Fzero Fone Fopp Fadd Fsub Fmul 3}. (* appears in statement *)
32 Local Instance char_ge_5: @Ring.char_ge F Feq Fzero Fone Fopp Fadd Fsub Fmul 5.
33 Proof. clear -char_ge_28; eapply Algebra.Hierarchy.char_ge_weaken; eauto; vm_decide. Qed.
34 Local Instance char_ge_12: @Ring.char_ge F Feq Fzero Fone Fopp Fadd Fsub Fmul 12.
35 Proof. clear -char_ge_28; eapply Algebra.Hierarchy.char_ge_weaken; eauto; vm_decide. Qed.
36 Local Infix "=" := Feq : type_scope. Local Notation "a <> b" := (not (a = b)) : type_scope.
37 Local Infix "+" := Fadd. Local Infix "*" := Fmul.
38 Local Infix "-" := Fsub. Local Infix "/" := Fdiv.
39 Local Notation "0" := Fzero. Local Notation "1" := Fone.
40 Local Notation "'∞'" := (inr tt) : core_scope.
41 Local Notation "( x , y )" := (inl (pair x y)).
42
43 Context {a b: F} {b_nonzero:b <> 0}.
44 Context {a24:F} {a24_correct:(1+1+1+1)*a24 = a-(1+1)}.
45 Local Notation Madd := (M.add(a:=a)(b_nonzero:=b_nonzero)(char_ge_3:=char_ge_3)).
46 Local Notation Mopp := (M.opp(a:=a)(b_nonzero:=b_nonzero)).
47 Local Notation Mpoint := (@M.point F Feq Fadd Fmul a b).
48 Local Notation X0 := (M.X0(Fzero:=Fzero)(Feq:=Feq)(Fadd:=Fadd)(Fmul:=Fmul)(a:=a)(b:=b)).
49 Local Notation to_xz := (M.to_xz(Fzero:=Fzero)(Fone:=Fone)(Feq:=Feq)(Fadd:=Fadd)(Fmul:=Fmul)(a:=a)(b:=b)).
50 Local Notation xzladderstep := (M.xzladderstep(a24:=a24)(Fadd:=Fadd)(Fsub:=Fsub)(Fmul:=Fmul)).
51
52 (** This tactic is to solve goals that fsatz also solves, but to
53 do so faster in some cases common in this file. It may also
54 work around deficiencies in the Coq 8.6 (released) version of
55 [nsatz]; it has some heuristics for clearing hypotheses and
56 running [exfalso], and then tries [fsatz] *)
57 Local Ltac maybefast :=
58 try lazymatch goal with
59 | [ fld : Hierarchy.field (T:=?F) (eq:=?Feq), Feq_dec : DecidableRel ?Feq |- _ ]
60 => lazymatch goal with
61 | [ H : ?x * 1 = ?y * ?z, H' : ?x <> 0, H'' : ?z = 0 |- _ ]
62 => clear -H H' H'' fld Feq_dec; exfalso; fsatz
63 | [ H : ?x * 0 = 1 * ?y, H' : ?y <> 0 |- _ ]
64 => clear -H H' fld Feq_dec; exfalso; fsatz
65 | _
66 => match goal with
67 | [ H : ?b * ?lhs = ?rhs, H' : ?b * ?lhs' = ?rhs', Heq : ?x = ?y, Hb : ?b <> 0 |- _ ]
68 => exfalso;
69 repeat match goal with H : Ring.char_ge _ |- _ => revert H end;
70 let rhs := match (eval pattern x in rhs) with ?f _ => f end in
71 let rhs' := match (eval pattern y in rhs') with ?f _ => f end in
72 unify rhs rhs';
73 match goal with
74 | [ H'' : ?x = ?Fopp ?x, H''' : ?x <> ?Fopp (?Fopp ?y) |- _ ]
75 => let lhs := match (eval pattern x in lhs) with ?f _ => f end in
76 let lhs' := match (eval pattern y in lhs') with ?f _ => f end in
77 unify lhs lhs';
78 clear -H H' Heq H'' H''' Hb fld Feq_dec; intros
79 | [ H'' : ?x <> ?Fopp ?y, H''' : ?x <> ?Fopp (?Fopp ?y) |- _ ]
80 => let lhs := match (eval pattern x in lhs) with ?f _ => f end in
81 let lhs' := match (eval pattern y in lhs') with ?f _ => f end in
82 unify lhs lhs';
83 clear -H H' Heq H'' H''' Hb fld Feq_dec; intros
84 end;
85 fsatz
86 | [ H : ?x * (?y * ?z) = 0 |- _ ]
87 => exfalso;
88 repeat match goal with
89 | [ H : ?x * 1 = ?y * ?z |- _ ]
90 => is_var x; is_var y; is_var z;
91 revert H
92 end;
93 generalize fld;
94 let lhs := match type of H with ?lhs = _ => lhs end in
95 repeat match goal with
96 | [ x : F |- _ ] => lazymatch type of H with
97 | context[x] => fail
98 | _ => clear dependent x
99 end
100 end;
101 intros _; intros;
102 fsatz
103 end
104 end
105 end.
106
107 (* TODO(#152): deduplicate between curves files *)
108 Ltac prept_step :=
109 match goal with
110 | _ => progress intros
111 | _ => progress autounfold with points_as_coordinates in *
112 | _ => progress destruct_head'_True
113 | _ => progress destruct_head'_unit
114 | _ => progress destruct_head'_prod
115 | _ => progress destruct_head'_sig
116 | _ => progress destruct_head'_bool
117 | _ => progress destruct_head'_sum
118 | _ => progress destruct_head'_and
119 | _ => progress destruct_head'_or
120 | H: context[dec ?P] |- _ => destruct (dec P)
121 | |- context[dec ?P] => destruct (dec P)
122 | |- ?P => lazymatch type of P with Prop => split end
123 end.
124 Ltac prept := repeat prept_step.
125 Ltac t_fast := prept; trivial; try contradiction.
126 Ltac t := t_fast; maybefast; fsatz.
127
128 Create HintDb points_as_coordinates discriminated.
129 Hint Unfold Proper respectful Reflexive Symmetric Transitive : points_as_coordinates.
130 Hint Unfold Let_In : points_as_coordinates.
131 Hint Unfold fst snd proj1_sig : points_as_coordinates.
132 Hint Unfold fieldwise fieldwise' : points_as_coordinates.
133 Hint Unfold M.add M.opp M.point M.coordinates M.xzladderstep M.donnaladderstep M.boringladderstep M.to_xz : points_as_coordinates.
134
135 Lemma donnaladderstep_same x1 Q Q' :
136 fieldwise (n:=2) (fieldwise (n:=2) Feq)
137 (xzladderstep x1 Q Q')
138 (M.donnaladderstep(a24:=a24)(Fadd:=Fadd)(Fsub:=Fsub)(Fmul:=Fmul) x1 Q Q').
139 Proof. t. Qed.
140
141 Lemma boringladderstep_same (ap2d4:F) (ap2d4_correct:(1+1+1+1)*a24 = a+1+1) x1 Q Q' :
142 fieldwise (n:=2) (fieldwise (n:=2) Feq)
143 (xzladderstep x1 Q Q')
144 (M.boringladderstep(ap2d4:=ap2d4)(Fadd:=Fadd)(Fsub:=Fsub)(Fmul:=Fmul) x1 Q Q').
145 Proof. t. Qed.
146
147 Definition projective (P:F*F) :=
148 if dec (snd P = 0) then fst P <> 0 else True.
149 Definition eq (P Q:F*F) := fst P * snd Q = fst Q * snd P.
150 Definition ladder_invariant x1 Q Q' :=
151 match M.coordinates (Madd Q (Mopp Q')) with
152 | ∞ => False (* Q <> Q' *)
153 | (x,y) => x = x1 /\ x1 <> 0 (* Q-Q' <> (0, 0) *)
154 end.
155 Hint Unfold projective eq ladder_invariant : points_as_coordinates.
156
157 Lemma to_xz_add_coordinates (x1:F) (xz x'z':F*F)
158 (Hxz:projective xz) (Hz'z':projective x'z')
159 (Q Q':Mpoint)
160 (HQ:eq xz (to_xz Q)) (HQ':eq x'z' (to_xz Q'))
161 (HI:ladder_invariant x1 Q Q')
162 : projective (snd (xzladderstep x1 xz x'z'))
163 /\ eq (fst (xzladderstep x1 xz x'z')) (to_xz (Madd Q Q ))
164 /\ eq (snd (xzladderstep x1 xz x'z')) (to_xz (Madd Q Q')).
165 Proof. Time t_fast. Time par: abstract (maybefast; fsatz). Time Qed.
166 (* 12 ;; 24 ;; 3 *)
167
168 Context {a2m4_nonsquare:forall r, r*r <> a*a - (1+1+1+1)}.
169
170 Lemma projective_fst_xzladderstep x1 Q (HQ:projective Q) Q'
171 : projective (fst (xzladderstep x1 Q Q')).
172 Proof.
173 specialize (a2m4_nonsquare (fst Q/snd Q - fst Q/snd Q)).
174 destruct (dec (snd Q = 0)); t.
175 Qed.
176
177 Local Definition a2m4_nz : a*a - (1+1+1+1) <> 0.
178 Proof. specialize (a2m4_nonsquare 0). fsatz. Qed.
179
180 Lemma difference_preserved Q Q' :
181 MontgomeryCurve.M.eq
182 (Madd (Madd Q Q) (Mopp (Madd Q Q')))
183 (Madd Q (Mopp Q')).
184 Proof.
185 pose proof (let (_, h, _, _) := AffineInstances.M.MontgomeryWeierstrassIsomorphism b_nonzero (a:=a) a2m4_nz in h) as commutative_group.
186 rewrite Group.inv_op.
187 rewrite <-Hierarchy.associative.
188 rewrite Group.cancel_left.
189 rewrite Hierarchy.commutative.
190 rewrite <-Hierarchy.associative.
191 rewrite Hierarchy.left_inverse.
192 rewrite Hierarchy.right_identity.
193 reflexivity.
194 Qed.
195
196 Lemma ladder_invariant_preserved x1 Q Q' (H:ladder_invariant x1 Q Q')
197 : ladder_invariant x1 (Madd Q Q) (Madd Q Q').
198 Proof.
199 cbv [ladder_invariant] in *.
200 pose proof difference_preserved Q Q' as Hrw.
201 (* FIXME: what we actually want to do here is to rewrite with in
202 match argument with
203 [sumwise (fieldwise (n:=2) Feq) (fun _ _ => True)] *)
204 cbv [MontgomeryCurve.M.eq] in *; break_match; break_match_hyps;
205 destruct_head' @and; repeat split; subst;
206 try solve [intuition congruence].
207 congruence (* congruence failed, idk WHY *)
208 || (match goal with
209 [H:?f1 = ?x1, G:?f = ?f1 |- ?f = ?x1] => rewrite G; exact H
210 end).
211 Qed.
212
213 Lemma ladder_invariant_swap x1 Q Q' (H:ladder_invariant x1 Q Q')
214 : ladder_invariant x1 Q' Q.
215 Proof. t. Qed.
216
217 Lemma to_xz_add x1 xz x'z' Q Q'
218 (Hxz : projective xz) (Hx'z' : projective x'z')
219 (HQ : eq xz (to_xz Q)) (HQ' : eq x'z' (to_xz Q'))
220 (HI : ladder_invariant x1 Q Q')
221 : projective (fst (xzladderstep x1 xz x'z'))
222 /\ projective (snd (xzladderstep x1 xz x'z'))
223 /\ eq (fst (xzladderstep x1 xz x'z')) (to_xz (Madd Q Q ))
224 /\ eq (snd (xzladderstep x1 xz x'z')) (to_xz (Madd Q Q'))
225 /\ ladder_invariant x1 (Madd Q Q) (Madd Q Q').
226 Proof.
227 destruct (to_xz_add_coordinates x1 xz x'z' Hxz Hx'z' Q Q' HQ HQ' HI) as [? [? ?]].
228 eauto 99 using projective_fst_xzladderstep, ladder_invariant_preserved.
229 Qed.
230
231 Definition to_x (xz:F*F) : F :=
232 if dec (snd xz = 0) then 0 else fst xz / snd xz.
233 Hint Unfold to_x : points_as_coordinates.
234
235 Lemma to_x_to_xz Q : to_x (to_xz Q) = X0 Q.
236 Proof. t. Qed.
237
238 Lemma proper_to_x_projective xz x'z'
239 (Hxz:projective xz) (Hx'z':projective x'z')
240 (H:eq xz x'z') : Feq (to_x xz) (to_x x'z').
241 Proof. destruct (dec (snd xz = 0)), (dec(snd x'z' = 0)); t. Qed.
242
243 Lemma to_x_zero x : to_x (pair x 0) = 0.
244 Proof. t. Qed.
245
246 Hint Unfold MontgomeryCurve.M.eq : points_as_coordinates.
247 Global Instance Proper_to_xz : Proper (MontgomeryCurve.M.eq ==> eq) to_xz.
248 Proof. t. Qed.
249
250 Global Instance Reflexive_eq : Reflexive eq.
251 Proof. t. Qed.
252 Global Instance Symmetric_eq : Symmetric eq.
253 Proof. t. Qed.
254 Lemma transitive_eq {p} q {r} : projective q -> eq p q -> eq q r -> eq p r.
255 Proof. t. Qed.
256
257 Lemma projective_to_xz Q : projective (to_xz Q).
258 Proof. t. Qed.
259
260 Global Instance Proper_ladder_invariant : Proper (Feq ==> MontgomeryCurve.M.eq ==> MontgomeryCurve.M.eq ==> iff) ladder_invariant.
261 Proof. t. Qed.
262
263 Local Notation montladder := (M.montladder(a24:=a24)(Fadd:=Fadd)(Fsub:=Fsub)(Fmul:=Fmul)(Fzero:=Fzero)(Fone:=Fone)(Finv:=Finv)).
264 Local Notation scalarmult := (@ScalarMult.scalarmult_ref Mpoint Madd M.zero Mopp).
265
266 Import Crypto.Util.Loops.
267 Import BinInt.
268
269 Lemma to_x_inv00 (HFinv:Finv 0 = 0) x z : to_x (pair x z) = x * Finv z.
270 Proof. t_fast; setoid_subst_rel Feq; rewrite ?HFinv in *; fsatz. Qed.
271
272 Lemma Z_shiftr_testbit_1 n i: Logic.eq (n>>i)%Z (Z.div2 (n >> i) + Z.div2 (n >> i) + Z.b2z (Z.testbit n i))%Z.
273 Proof. rewrite ?Z.testbit_odd, ?Z.add_diag, <-?Z.div2_odd; reflexivity. Qed.
274
275 Context (HFinv : Finv 0 = 0) (scalarbits : Z) (Hscalarbits : (0 <= scalarbits)%Z).
276
277 (* We prove montladder correct by considering the zero and non-zero case
278 separately. *)
279 Lemma montladder_correct_0 n x (Hx : Feq x 0) : montladder scalarbits (Z.testbit n) x = 0.
280 Proof.
281 cbv beta delta [M.montladder].
282 (* [while.by_invariant] expects a goal like [?P (while _ _ _ _)], make it so: *)
283 lazymatch goal with |- context [while ?t ?b ?l ?ii] => pattern (while t b l ii) end.
284 eapply (while.by_invariant_fuel
285 (fun '(x2, z2, x3, z3, swap, i) =>
286 (i < scalarbits)%Z /\
287 z2 = 0 /\
288 if dec (Logic.eq i (Z.pred scalarbits)) then x3 = 0 else z3 = 0)
289 (fun s => Z.to_nat (Z.succ (snd s)))).
290 { split.
291 (* invariant holds in the beginning *)
292 { cbn; split; [lia|split;[reflexivity|try t]]. }
293 { (* fuel <= measure *) cbn. rewrite Z.succ_pred. reflexivity. } }
294 { intros [ [ [ [ [x2 z2] x3] z3] swap] i] [Hi [Hz2 Hx3z3]].
295 destruct (i >=? 0)%Z eqn:Hbranch; (* did the loop continue? *)
296 rewrite Z.geb_ge_iff in Hbranch.
297 { split. (* if loop continued, invariant is preserved *)
298 { destruct (dec (Logic.eq i (Z.pred scalarbits))).
299 { (* first loop iteration *)
300 cbv -[xzladderstep xorb Z.testbit Z.pred dec Z.lt];
301 destruct (xorb swap (Z.testbit n i));
302 split; [lia|t|lia|t]. }
303 { (* subsequent loop iterations *)
304 cbv -[xzladderstep xorb Z.testbit Z.pred dec Z.lt].
305 destruct (xorb swap (Z.testbit n i));
306 (split; [lia| split; [t| break_match;[lia|t]]]). } }
307 { (* measure decreases *)
308 cbv [Let_In]; break_match; cbn; rewrite Z.succ_pred; apply Znat.Z2Nat.inj_lt; lia. } }
309 { (* if loop exited, invariant implies postcondition *)
310 cbv [M.cswap]; break_match; break_match_hyps; setoid_subst_rel Feq; fsatz. } }
311 Qed.
312
313 Lemma montladder_correct_nz n P (Hnz : X0 P <> 0)
314 : montladder scalarbits (Z.testbit n) (X0 P) = X0 (scalarmult (n mod 2^scalarbits) P).
315 Proof.
316 pose proof (let (_, h, _, _) := AffineInstances.M.MontgomeryWeierstrassIsomorphism b_nonzero (a:=a) a2m4_nz in h) as commutative_group.
317 cbv beta delta [M.montladder].
318 (* [while.by_invariant] expects a goal like [?P (while _ _ _ _)], make it so: *)
319 lazymatch goal with |- context [while ?t ?b ?l ?i] => pattern (while t b l i) end.
320 eapply (while.by_invariant_fuel
321 (fun '(x2, z2, x3, z3, swap, i) =>
322 (-1 <= i < scalarbits)%Z /\
323 projective (pair x2 z2) /\
324 projective (pair x3 z3) /\
325 let q := if (swap:bool) then (pair x3 z3) else (pair x2 z2) in
326 let q' := if (swap:bool) then (pair x2 z2) else (pair x3 z3) in
327 let r := ((n mod 2^scalarbits) >> Z.succ i)%Z in
328 eq q (to_xz (scalarmult r P)) /\
329 eq q' (to_xz (scalarmult (Z.succ r) P)) /\
330 ladder_invariant (X0 P) (scalarmult r P) (scalarmult (Z.succ r) P))
331 (fun s => Z.to_nat (Z.succ (snd s))) (* decreasing measure *) ).
332 { split; cbn.
333 { (* invariant holds in the beginning *)
334 rewrite ?Z.succ_pred, ?Z.lt_pow_2_shiftr, <-?Z.one_succ by (apply Z.mod_pos_bound; lia).
335 repeat split; [lia|lia|t..]. }
336 { (* sufficient fuel *) rewrite Z.succ_pred. reflexivity. } }
337 { intros [ [ [ [ [x2 z2] x3] z3] swap] i] [Hi [Hx2z2 [Hx3z3 [Hq [Hq' Hladder]]]]].
338 destruct (i >=? 0)%Z eqn:Hbranch; (* did the loop continue? *)
339 rewrite Z.geb_ge_iff in Hbranch.
340 split.
341 { (* if loop continued, invariant is preserved *)
342 remember (Z.testbit n i) as bit eqn:Hbit; symmetry in Hbit.
343 erewrite <-(Z.mod_pow2_bits_low _ scalarbits) in Hbit by tauto.
344 set (n mod 2 ^ scalarbits)%Z as n' in *.
345 let group _ := ltac:(repeat rewrite ?scalarmult_add_l, ?scalarmult_0_l, ?scalarmult_1_l, ?Hierarchy.left_identity, ?Hierarchy.right_identity, ?Hierarchy.associative, ?(Hierarchy.commutative _ P); reflexivity) in
346 destruct bit in *;
347 destruct swap eqn:Hswap in *;
348 repeat match goal with
349 | _ => solve [ congruence | assumption | lia ]
350 | _ => progress cbv beta delta [Let_In]
351 | _ => progress cbn [xorb Z.b2z]
352 | _ => progress autorewrite with cancel_pair in *
353 | _ => rewrite <-!@surjective_pairing
354 | _ => progress inversion_prod
355 | _ => progress subst
356 | _ => break_match_hyps_step ltac:(fun _ => idtac)
357 | _ => break_match_step ltac:(fun _ => idtac)
358 | H:projective ?xz, G:projective ?x'z', HQ: eq ?xz (to_xz ?Q), HQ': eq ?x'z' (to_xz ?Q')
359 |- context [xzladderstep ?p ?xz ?x'z']
360 => let pf := constr:(to_xz_add p xz x'z' _ _ H G HQ HQ' ltac:(auto using ladder_invariant_swap)) in
361 unique pose proof (proj1 pf); destruct (proj2 pf) as [? [? [? ?]]] (* because there is no unique destruct *)
362 | _ => progress rewrite ?Z.succ_pred, ?Z.shiftr_succ, <-?Z.div2_spec, <-?Z.add_1_r in *
363 | |- context [scalarmult (n'>>i) ] => rewrite (Z_shiftr_testbit_1 n' i), Hbit; cbn [Z.b2z]
364 | |- context [scalarmult (n'>>i+1) ] => rewrite (Z_shiftr_testbit_1 n' i), Hbit; cbn [Z.b2z]
365 | |- ?P => match type of P with Prop => split end
366 | H: eq (?f _) (to_xz ?LHS) |- eq (?f _) (to_xz ?RHS)
367 => eapply (transitive_eq (to_xz LHS) ltac:(auto using projective_to_xz) H); f_equiv; group ()
368 | H: ladder_invariant _ _ _ |- ladder_invariant _ _ _
369 => refine (proj1 (Proper_ladder_invariant _ _ (reflexivity _) _ _ _ _ _ _) H); group ()
370 | H: ladder_invariant _ _ _ |- ladder_invariant _ _ _
371 => refine (proj1 (Proper_ladder_invariant _ _ (reflexivity _) _ _ _ _ _ _) (ladder_invariant_swap _ _ _ H)); group ()
372 | |- ?P => match type of P with Prop => split end
373 end. }
374 { (* measure decreases *)
375 cbv [Let_In]; break_match; cbn; rewrite Z.succ_pred; apply Znat.Z2Nat.inj_lt; lia. }
376 { (* if loop exited, invariant implies postcondition *)
377 replace i with ((-(1))%Z) in * by lia; clear Hi Hbranch.
378 rewrite Z.succ_m1, Z.shiftr_0_r in *.
379 cbv [M.cswap];
380 destruct swap eqn:Hswap; rewrite <-!to_x_inv00 by assumption;
381 etransitivity; try eapply to_x_to_xz; f_equal;
382 eauto using projective_to_xz, proper_to_x_projective. } }
383 Qed.
384
385 (* Using montladder_correct_0 in the combined correctness theorem requires
386 additionally showing that the right-hand-side is 0. This comes from there
387 being two points such that to_x gives 0: infinity and (0, 0). *)
388
389 Lemma X0_opp_0 (P : M.point) (H : X0 P = 0) : X0 (Mopp P) = 0.
390 Proof. t. Qed.
391
392 Lemma X0_add_0 (P Q : M.point) (HP : X0 P = 0) (HQ : X0 Q = 0) : X0(Madd P Q) = 0.
393 Proof. cbv [X0] in *; t. Qed.
394
395 Lemma X0_scalarmult_0 (n : Z) (P : M.point) (H : X0 P = 0) : X0 (scalarmult n P) = 0.
396 Proof.
397 clear dependent scalarbits.
398 induction n using Z.peano_rect_strong; try t.
399 { unfold scalarmult_ref. rewrite Z.peano_rect_succ by lia. fold (scalarmult n P).
400 auto using X0_add_0. }
401 { unfold scalarmult_ref. rewrite Z.peano_rect_pred by lia. fold (scalarmult n P).
402 auto using X0_add_0, X0_opp_0. }
403 Qed.
404
405 (* Combine the two cases together. *)
406
407 Lemma montladder_correct n P :
408 montladder scalarbits (Z.testbit n) (X0 P) = X0 (scalarmult (n mod 2^scalarbits) P).
409 Proof.
410 destruct (dec (X0 P = 0)) as [Hz|?]; auto using montladder_correct_nz .
411 rewrite montladder_correct_0, X0_scalarmult_0 by trivial; reflexivity.
412 Qed.
413 End MontgomeryCurve.
414 End M.