@@ -41,7 +41,6 @@ Local Open Scope list_scope.
4141
4242Import (notations) coqutil.Map.Memory.
4343
44-
4544Import Coq.micromega.Lia.
4645Import coqutil.Tactics.Tactics.
4746
@@ -305,8 +304,6 @@ rewrite ?app_length, ?length_coord in *.
305304 trivial. }
306305Qed .
307306
308- Import coqutil.Tactics.Tactics.
309-
310307Definition p256_point_add_vartime_if_doubling := func!(p_out, p_P, p_Q) {
311308 unpack! zeroP = p256_point_iszero(p_P);
312309 unpack! zeroQ = p256_point_iszero(p_Q);
@@ -322,66 +319,72 @@ Definition p256_point_add_vartime_if_doubling := func!(p_out, p_P, p_Q) {
322319 br_memcpy(p_out, p_sel, $(3*32))
323320}.
324321
322+ #[local] Ltac ensure_map m := lazymatch type of m with | @Interface.map.rep _ _ _ => true | _ => false end .
323+ #[local] Ltac newest_memory_hyp := match goal with | H: ?G ?m |- _ =>
324+ match (ensure_map m) with true => H | false => fail end end .
325+
325326Import memcpy.
326- Lemma p256_point_add_vartime_if_doubling_ok : program_logic_goal_for_function! p256_point_add_vartime_if_doubling.
327+ Lemma p256_point_add_vartime_if_doubling_ok :
328+ let spec := spec_of_p256_point_add_vartime_if_doubling in
329+ program_logic_goal_for_function! p256_point_add_vartime_if_doubling.
327330Proof .
328331 cbv [spec_of_p256_point_add_vartime_if_doubling].
329332 repeat straightline.
333+ pose proof (length_point P) as HlengthP. pose proof (length_point Q) as HlengthQ.
330334 straightline_call; repeat straightline. (*iszero*)
331- { letexists . ecancel_assumption. }
335+ { eexists . ecancel_assumption. }
332336 straightline_call; repeat straightline. (*iszero*)
333- { letexists . ecancel_assumption. }
337+ { eexists . ecancel_assumption. }
334338 (* stackalloc *)
335- seprewrite_in_by (@Array.array1_iff_eq_of_list_word_at) H9 ltac:(lia).
339+ seprewrite_in_by (@Array.array1_iff_eq_of_list_word_at) ltac:(newest_memory_hyp) ltac:(lia).
336340 straightline_call; ssplit. (*add*)
337341 { ecancel_assumption. }
338- { rewrite length_point; lia. }
342+ { lia. }
339343 repeat straightline.
340344 straightline_call; repeat straightline (* br_declassify *).
341345 (* stackalloc *)
342- seprewrite_in_by (@Array.array1_iff_eq_of_list_word_at) H17 ltac:(lia).
346+ seprewrite_in_by (@Array.array1_iff_eq_of_list_word_at) ltac:(newest_memory_hyp) ltac:(lia).
343347 straightline_call; ssplit. (* memset *)
344348 { ecancel_assumption. }
345- { ZnWords.ZnWords. }
349+ { ZnWords. }
346350 repeat straightline.
347351 straightline_call; repeat straightline; ssplit (* memcxor *).
348352 { ecancel_assumption. }
349353 { rewrite ?repeat_length; trivial. }
350- { rewrite H18, length_point; trivial . }
354+ { ZnWords . }
351355 straightline_call; repeat straightline; ssplit (* memcxor *).
352356 { ecancel_assumption. }
353357 { rewrite ?repeat_length; trivial. }
354- { rewrite length_point; trivial . }
358+ { ZnWords . }
355359 straightline_call; repeat straightline; ssplit (* memcxor *).
356360 { ecancel_assumption. }
357361 { rewrite ?repeat_length; trivial. }
358- { rewrite length_point; trivial. }
359-
362+ { ZnWords. }
360363 subst x x0 x3.
361- letexists ; ssplit; repeat straightline; subst v (* if ok *).
364+ eexists ; ssplit; repeat straightline. (* if ok *)
362365 { straightline_call; repeat straightline; ssplit (* memcpy *).
363366 { ecancel_assumption. }
364- { rewrite H10, length_point; trivial . }
367+ { ZnWords . }
365368 { trivial. }
366- { clear; ZnWords.ZnWords. }
369+ { clear; ZnWords. }
367370 repeat straightline.
368371 (* stackdealloc *)
369- progress repeat seprewrite_in_by (symmetry! @Array.array1_iff_eq_of_list_word_at) H42 ltac:(rewrite ?length_point in *; lia || ZnWords.ZnWords).
370- progress repeat match type of H42 with context [Array.array ptsto _ _ (point.to_bytes ?x)] =>
371- unique pose proof (length_point x) end.
372+ progress repeat seprewrite_in_by (symmetry! @Array.array1_iff_eq_of_list_word_at) ltac:(newest_memory_hyp)
373+ ltac:(rewrite ?length_point in *; lia || ZnWords).
372374 assert (Datatypes.length x6 = 96%nat) by ZnWords.ZnWords.
373375 repeat straightline.
374- progress repeat seprewrite_in_by (@Array.array1_iff_eq_of_list_word_at) H42 ltac:(rewrite ?length_point in *; lia || ZnWords.ZnWords).
375-
376- rewrite <-word.unsigned_of_Z_0, !word.unsigned_inj_iff in H27 by exact _.
377- rewrite !word.lor_0_iff, !word.broadcast_0_iff in H27.
376+ progress repeat seprewrite_in_by (@Array.array1_iff_eq_of_list_word_at)
377+ ltac:(newest_memory_hyp) ltac:(lia || ZnWords.ZnWords).
378+ let Hzero := match goal with H: _ <> 0 |- _ => H end in
379+ rewrite <-word.unsigned_of_Z_0, !word.unsigned_inj_iff in Hzero by exact _;
380+ rewrite !word.lor_0_iff, !word.broadcast_0_iff in Hzero.
378381 destruct (iszero P) eqn:HP, (iszero Q) eqn:HQ in *; try intuition discriminate;
379382 repeat match goal with
380383 | H : _ = _ -> _ |- _ => specialize (H eq_refl)
381384 | H : ?x = ?y -> _ |- _ => assert (x = y -> False) as _ by inversion 1; clear H
382385 end;
383386 subst x4; subst x5; subst x6;
384- rewrite ?Byte.map_xor_0_l in * by (rewrite ?length_point; ZnWords. ZnWords).
387+ rewrite ?Byte.map_xor_0_l in * by (ZnWords).
385388 { (* 0 + 0 *)
386389 eexists (exist _ (0,0,0)%F I); split.
387390 { use_sep_assumption; cancel. reflexivity. }
@@ -393,7 +396,6 @@ Proof.
393396 eexists; split. { ecancel_assumption. }
394397 apply Decidable.dec_bool, Jacobian.iszero_iff in HP.
395398 rewrite Jacobian.eq_iff, Jacobian.to_affine_add, HP.
396- Import Curves.Weierstrass.AffineProofs.
397399 symmetry.
398400 eapply Hierarchy.left_identity. }
399401 { (* P + 0 *)
@@ -406,7 +408,8 @@ Import Curves.Weierstrass.AffineProofs.
406408 rewrite <-Bool.not_true_iff_false in HP, HQ.
407409 (* Decidable.dec_iff? *)
408410 cbv [iszero] in HP, HQ; case Decidable.dec in HP; case Decidable.dec in HQ; try congruence.
409- destruct (H19 ltac:(trivial) ltac:(trivial)) as [HE|]; [|intuition fail].
411+ match goal with H : ~ Jacobian.iszero ?P -> _ |- _ =>
412+ destruct (H ltac:(trivial) ltac:(trivial)) as [HE|]; [|intuition fail] end.
410413 case HE as [_ (?&HE)].
411414 repeat straightline_cleanup.
412415 eexists; split; [ecancel_assumption|].
@@ -428,7 +431,7 @@ Import Curves.Weierstrass.AffineProofs.
428431 straightline_call; repeat straightline.
429432 { split. { ecancel_assumption. }
430433 rewrite ?map_length, ?combine_length, ?repeat_length.
431- rewrite H18, length_point. clear; ZnWords.ZnWords . }
434+ rewrite H18, length_point. clear; reflexivity . }
432435
433436 straightline_call; repeat straightline; ssplit (* memcpy *).
434437 { ecancel_assumption. }
@@ -437,18 +440,34 @@ Import Curves.Weierstrass.AffineProofs.
437440 { clear; ZnWords.ZnWords. }
438441 repeat straightline.
439442 (* stackdealloc *)
440- progress repeat seprewrite_in_by (symmetry! @Array.array1_iff_eq_of_list_word_at) H31 ltac:(rewrite ?length_point in *; lia || ZnWords.ZnWords).
443+ progress repeat seprewrite_in_by (symmetry! @Array.array1_iff_eq_of_list_word_at)
444+ ltac:(newest_memory_hyp) ltac:(rewrite ?length_point in *; lia || ZnWords.ZnWords).
441445 progress repeat match type of H31 with context [Array.array ptsto _ _ (point.to_bytes ?x)] =>
442446 unique pose proof (length_point x) end.
443447 repeat straightline.
444- progress repeat seprewrite_in_by (@Array.array1_iff_eq_of_list_word_at) H31 ltac:(rewrite ?length_point in *; lia || ZnWords.ZnWords).
448+ progress repeat seprewrite_in_by (@Array.array1_iff_eq_of_list_word_at)
449+ ltac:(newest_memory_hyp) ltac:(rewrite ?length_point in *; lia || ZnWords.ZnWords).
445450
446451 eexists; ssplit. { ecancel_assumption. }
447452 rewrite <-HE, <-Jacobian.double_minus_3_eq_double.
448453 rewrite Jacobian.eq_iff, Jacobian.to_affine_double, Jacobian.to_affine_add.
449454 reflexivity. }
450455Qed .
451456
457+ Lemma p256_point_add_constant_time_ok :
458+ let spec := spec_of_p256_point_add_constant_time in
459+ program_logic_goal_for_function! p256_point_add_vartime_if_doubling.
460+ Proof .
461+ unfold spec_of_p256_point_add_constant_time.
462+ pose proof p256_point_add_vartime_if_doubling_ok as Hvartime.
463+ unfold spec_of_p256_point_add_vartime_if_doubling in Hvartime.
464+ cbv [program_logic_goal_for] in *.
465+ do 21 intros ?. intros (?, (?, ?)).
466+ eapply Semantics.weaken_call.
467+ { eapply Hvartime; try trivial. split; eassumption. }
468+ cbv [id]. intros. assumption.
469+ Qed .
470+
452471Definition p256_point_double := func!(out, in1) {
453472 stackalloc 32 as D;
454473 stackalloc 32 as A;
0 commit comments