Debian Codegraph @

src/Bedrock/End2End/X25519/clamp.v

raw
1 From Coq Require Import ZArith Lia. Local Open Scope Z_scope.
2
3 Module Z.
4 Lemma testbit_pow2 n i (H : 0 <= n) : Z.testbit (2^n) i = Z.eqb i n.
5 Proof.
6 rewrite <-(Z.mul_1_l (2^n)), Z.mul_pow2_bits by trivial.
7 case (Z.eqb_spec i n) as [->|]; rewrite ?Z.sub_diag; trivial.
8 case (Z.ltb_spec (i-n) 0) as []; rewrite ?Z.testbit_neg_r by lia; trivial.
9 eapply Z.bits_above_log2; cbn; lia.
10 Qed.
11 End Z.
12
13 Local Ltac t :=
14 try (eapply Z.bits_inj'; intros i Hi);
15 repeat match goal with
16 | _ => progress subst
17 | _ => solve [trivial | lia]
18 | _ => progress rewrite
19 ?Z.sub_simpl_r,
20 ?Z.testbit_0_l, ?Z.testbit_neg_r, ?Z.testbit_pow2, ?Z.testbit_ones,
21 ?Z.lor_spec, ?Z.land_spec, ?Z.lxor_spec, ?Z.ldiff_spec,
22 ?Z.testbit_mod_pow2, ?Z.shiftl_spec, ?Z.shiftl_spec_low, ?Z.shiftr_spec
23 by lia
24 | _ => progress rewrite
25 ?Bool.andb_false_r, ?Bool.andb_true_r,
26 ?Bool.andb_false_l, ?Bool.andb_true_l,
27 ?Bool.orb_false_r, ?Bool.orb_true_r,
28 ?Bool.orb_false_l, ?Bool.orb_true_l,
29 ?Bool.xorb_false_r, ?Bool.xorb_true_r,
30 ?Bool.xorb_false_l, ?Bool.xorb_true_l,
31 ?Bool.negb_involutive
32 | |- _ => progress cbn [negb]
33 | |- context [Z.leb ?x ?y] => destruct (Z.leb_spec x y) as []
34 | |- context [Z.ltb ?x ?y] => destruct (Z.ltb_spec x y) as []
35 | |- context [Z.eqb ?x ?y] => destruct (Z.eqb_spec x y) as []
36 end.
37
38 Require Import Crypto.Spec.Curve25519.
39
40 Lemma mod8_clamp' k : clamp k mod 8 = 0.
41 Proof. cbv [clamp]. Z.div_mod_to_equations. lia. Qed.
42
43 Lemma clamp_range k : 2^254 <= clamp k < 2^255.
44 Proof. cbv [clamp]. Z.div_mod_to_equations. lia. Qed.
45
46 Definition clamp' k := Z.lor (Z.ldiff k (Z.ones 3) mod 2^255) (2^254).
47
48 Lemma clamp'_correct k : clamp' k = Curve25519.clamp k.
49 Proof.
50 cbv [Curve25519.clamp clamp']; change 8 with (2^3).
51 rewrite Z.mul_comm, <-Z.shiftl_mul_pow2, <-Z.shiftr_div_pow2, ?Z.add_nocarry_lxor; try lia; t.
52 Qed.
53
54
55 From Coq Require Import ZArith Lia.
56 From Coq Require Import String.
57 From Coq Require Import List.
58 From Coq.Init Require Import Byte.
59 Require Import bedrock2.Map.Separation.
60 Require Import bedrock2.Syntax.
61 Require Import bedrock2.NotationsCustomEntry.
62
63 Local Open Scope Z_scope.
64 Local Open Scope string_scope.
65 Import Syntax Syntax.Coercions NotationsCustomEntry.
66 Import ListNotations.
67
68 Definition clamp := func! (sk) {
69 store1(sk, load1(sk) & $248);
70 store1(sk+$31, (load1(sk+$31) & $127) | $64)
71 }.
72
73 From coqutil Require Import Word.Interface Word.Naive.
74 From coqutil Require Import SortedListWord.
75 From coqutil Require Import symmetry.
76 Require Import bedrock2.FE310CSemantics.
77 Local Existing Instances SortedListString.map SortedListWord.map coqutil.Word.Naive.word.
78
79 From coqutil Require Import LittleEndianList.
80 Require Import bedrock2.Syntax bedrock2.WeakestPrecondition bedrock2.Map.SeparationLogic.
81 Import Coq.Lists.List. (* TODO: SeparationLogic should not override skipn and etc *)
82 From bedrock2 Require Import ProgramLogic. Import ProgramLogic.Coercions.
83 Local Notation "m =* P" := ((P%sep) m) (at level 70, only parsing) (* experiment*).
84 Local Notation "xs $@ a" := (Array.array ptsto (word.of_Z 1) a xs) (at level 10, format "xs $@ a").
85 Import Byte Word.Properties.
86 Local Coercion byte.unsigned : byte >-> Z.
87 Local Arguments le_combine !_.
88
89 Global Instance spec_of_clamp : spec_of "clamp" :=
90 fnspec! "clamp" sk / (s : list Byte.byte) (R : _ -> Prop),
91 { requires t m := m =* s$@sk ⋆ R /\ length s = 32%nat;
92 ensures t' m := t=t' /\ m=* (le_split 32 (Curve25519.clamp (le_combine s)))$@sk ⋆ R }.
93
94 Lemma clamp_correct : program_logic_goal_for_function! clamp.
95 Proof.
96 straightline; repeat straightline_cleanup.
97
98 do 1 try destruct s as [|?b s]; try (cbn [length] in *; congruence).
99 cbn [Array.array] in *; cbn [length] in *.
100 repeat straightline.
101
102 rewrite <-(firstn_skipn 30 s) in H1 |- *.
103 seprewrite_in @Array.bytearray_append H1; rewrite ?app_length, ?firstn_length_le in * by lia.
104 rewrite <-!Properties.word.add_assoc, <-!Properties.word.ring_morph_add in *; simpl Z.add in *.
105
106 assert (length (skipn 30 s) = 1)%nat by (rewrite skipn_length; lia).
107 assert (length (firstn 30 s) = 30)%nat by (rewrite firstn_length; lia).
108 destruct (skipn 30 s) as [|b31 [] ] eqn:? in *; try (cbn [length] in *; congruence).
109 cbn [Array.array] in *.
110
111 repeat straightline.
112
113 eassert (Hrw : Lift1Prop.iff1 (ptsto a (Byte.byte.of_Z v0)) ([Byte.byte.of_Z v0]$@a)) by (cbn [Array.array]; cancel).
114 repeat seprewrite_in Hrw H4.
115 repeat seprewrite_in (symmetry! @Array.array_cons) H4.
116 seprewrite_in @Array.bytearray_index_merge H4.
117 { cbn [length]; rewrite ?app_length, ?length_firstn, ?Properties.word.unsigned_of_Z_nowrap; try lia. }
118
119 eassert (le_split _ _ = _) as ->; [|ecancel_assumption].
120 clear -H0 Heql0.
121
122 eapply le_combine_inj; rewrite ?le_combine_split.
123 { repeat (cbn [length]; rewrite ?length_le_split, ?app_length, ?firstn_length). lia. }
124
125 rewrite <-clamp'_correct.
126
127 repeat (cbn [le_combine]; rewrite ?le_combine_app, ?le_combine_firstn).
128
129 subst v v0.
130
131 cbn [length]; rewrite ?firstn_length_le by lia.
132 simpl Z.mul at 1 2.
133 rewrite ?byte.unsigned_of_Z; cbv [byte.wrap].
134 repeat rewrite ?word.unsigned_and_nowrap, ?word.unsigned_or_nowrap.
135
136 pose proof byte.unsigned_range b.
137 pose proof byte.unsigned_range b31.
138 rewrite ?word.unsigned_of_Z_nowrap; try lia.
139 cbv [clamp'].
140
141 rewrite <-(byte.wrap_unsigned b);
142 rewrite <-(byte.wrap_unsigned b31);
143 cbv [byte.wrap].
144 change 127 with (Z.ones 7).
145 change 64 with (2^6).
146 change 248 with (Z.ldiff (Z.ones 8) (Z.ones 3)).
147 change (31%nat * 8) with 248 in *.
148
149 t.
150
151 f_equal; lia.
152 Qed.