@@ -144,16 +144,6 @@ Module M.
144
144
end .
145
145
Hint Unfold projective eq ladder_invariant : points_as_coordinates.
146
146
147
- (* happens if u=0 in montladder, all denominators remain 0 *)
148
- Lemma add_0_numerator_r A B C D
149
- : snd (fst (xzladderstep 0 (pair C 0) (pair 0 A))) = 0
150
- /\ snd (snd (xzladderstep 0 (pair D 0) (pair 0 B))) = 0.
151
- Proof . t. Qed .
152
- Lemma add_0_denominators A B C D
153
- : snd (fst (xzladderstep 0 (pair A 0) (pair C 0))) = 0
154
- /\ snd (snd (xzladderstep 0 (pair B 0) (pair D 0))) = 0.
155
- Proof . t. Qed .
156
-
157
147
Lemma to_xz_add_coordinates (x1:F) (xz x'z':F*F)
158
148
(Hxz:projective xz) (Hz'z':projective x'z')
159
149
(Q Q':Mpoint)
@@ -275,6 +265,46 @@ Module M.
275
265
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.
276
266
Proof . rewrite ?Z.testbit_odd, ?Z.add_diag, <-?Z.div2_odd; reflexivity. Qed .
277
267
268
+ Lemma montladder_correct_0
269
+ (HFinv : Finv 0 = 0)
270
+ (n : Z)
271
+ (scalarbits : Z)
272
+ (Hn : (0 <= n < 2^scalarbits)%Z)
273
+ (Hscalarbits : (0 <= scalarbits)%Z)
274
+ : montladder scalarbits (Z.testbit n) 0 = 0.
275
+ Proof .
276
+ cbv beta delta [M.montladder].
277
+ (* [while.by_invariant] expects a goal like [?P (while _ _ _ _)], make it so: *)
278
+ lazymatch goal with |- context [while ?t ?b ?l ?i] => pattern (while t b l i) end.
279
+ eapply (while.by_invariant
280
+ (fun '(x2, z2, x3, z3, swap, i) =>
281
+ (i < scalarbits)%Z /\
282
+ z2 = 0 /\
283
+ if dec (Logic.eq i (Z.pred scalarbits)) then x3 = 0 else z3 = 0)
284
+ (fun s => Z.to_nat (Z.succ (snd s))) (* decreasing measure *) ).
285
+ { (* invariant holds in the beginning *) cbn.
286
+ split; [lia|split;[reflexivity|t]]. }
287
+ { intros [ [ [ [ [x2 z2] x3] z3] swap] i] [Hi [Hz2 Hx3z3]].
288
+ destruct (i >=? 0)%Z eqn:Hbranch; (* did the loop continue? *)
289
+ rewrite Z.geb_ge_iff in Hbranch.
290
+ { (* if loop continued, invariant is preserved *)
291
+ destruct (dec (Logic.eq i (Z.pred scalarbits))).
292
+ { (* first loop iteration *)
293
+ cbv -[xzladderstep xorb Z.testbit Z.pred dec Z.lt];
294
+ destruct (xorb swap (Z.testbit n i));
295
+ split; [lia|t|lia|t]. }
296
+ { (* subsequent loop iterations *)
297
+ cbv -[xzladderstep xorb Z.testbit Z.pred dec Z.lt].
298
+ destruct (xorb swap (Z.testbit n i));
299
+ (split; [lia| split; [t| break_match;[lia|t]]]). } }
300
+ { (* if loop exited, invariant implies postcondition *)
301
+ break_match; break_match_hyps; setoid_subst_rel Feq; fsatz. } }
302
+ { (* fuel <= measure *) cbn. rewrite Z.succ_pred. reflexivity. }
303
+ { (* measure decreases *) intros [? i].
304
+ destruct (i >=? 0)%Z eqn:Hbranch;rewrite Z.geb_ge_iff in Hbranch; [|exact I].
305
+ cbv [Let_In]; break_match; cbn; rewrite Z.succ_pred; apply Znat.Z2Nat.inj_lt; lia. }
306
+ Qed .
307
+
278
308
Lemma montladder_correct_nz
279
309
(HFinv : Finv 0 = 0)
280
310
(n : Z) (P : M.point)
@@ -348,5 +378,9 @@ Module M.
348
378
destruct (i >=? 0)%Z eqn:Hbranch;rewrite Z.geb_ge_iff in Hbranch; [|exact I].
349
379
cbv [Let_In]; break_match; cbn; rewrite Z.succ_pred; apply Znat.Z2Nat.inj_lt; lia. }
350
380
Qed .
381
+
382
+ (* TODO: Combine the above lemmas. We haven't yet proven that montladder
383
+ preserves Feq, so this is tricky. *)
384
+
351
385
End MontgomeryCurve.
352
386
End M.
0 commit comments