Debian Codegraph @

src/Bedrock/P256/Coord.v

raw
1 Require Import coqutil.Datatypes.List Coq.Lists.List.
2 Require Import Bedrock.P256.Specs.
3 Require Import Bedrock.P256.Platform.
4
5 Import Specs.NotationsCustomEntry Specs.coord Specs.point.
6
7 Import bedrock2.Syntax bedrock2.NotationsCustomEntry
8 LittleEndianList
9 ZArith.BinInt
10 BinInt BinNat Init.Byte
11 PrimeFieldTheorems ModInv
12 micromega.Lia
13 coqutil.Byte
14 Lists.List micromega.Lia
15 Jacobian
16 Coq.Strings.String Coq.Lists.List
17 ProgramLogic WeakestPrecondition
18 ProgramLogic.Coercions
19 Word.Interface OfListWord Separation SeparationLogic
20 letexists
21 BasicC64Semantics
22 ListIndexNotations
23 SepAutoArray
24 symmetry
25 PeanoNat micromega.Lia
26 Tactics
27 UniquePose
28 micromega.Lia Word.Properties
29 coqutil.Tactics.Tactics.
30
31 Import ListIndexNotations.
32 Local Open Scope list_index_scope.
33 Local Open Scope Z_scope.
34 Local Open Scope bool_scope.
35 Local Open Scope string_scope.
36 Local Open Scope list_scope.
37
38 Local Notation "xs $@ a" := (map.of_list_word_at a xs)
39 (at level 10, format "xs $@ a").
40 Local Notation "$ n" := (match word.of_Z n return word with w => w end) (at level 9, format "$ n").
41 Local Notation "p .+ n" := (word.add p (word.of_Z n)) (at level 50, format "p .+ n", left associativity).
42 Local Coercion F.to_Z : F >-> Z.
43
44
45 Definition p256_coord_nonzero := func! (p_x) ~> nz {
46 unpack! nz = br_broadcast_nonzero(load(p_x) | load(p_x.+$8) | load(p_x.+$8.+$8) | load(p_x.+$8.+$8.+$8))
47 }.
48
49 Definition p256_coord_sub := func!(out, x, y) {
50 unpack! t0, borrow = full_sub(load(x), load(y), $0);
51 unpack! t1, borrow = full_sub(load(x+$8), load(y+$8), borrow);
52 unpack! t2, borrow = full_sub(load(x+$8+$8), load(y+$8+$8), borrow);
53 unpack! t3, borrow = full_sub(load(x+$8+$8+$8), load(y+$8+$8+$8), borrow);
54 unpack! mask = br_value_barrier(-borrow);
55 unpack! r0, carry = full_add(t0, mask, $0);
56 unpack! r1, carry = full_add(t1, mask & $0xffffffff, carry);
57 unpack! r2, carry = full_add(t2, $0, carry);
58 unpack! r3, carry = full_add(t3, mask & $0xffffffff00000001, carry);
59 store(out, r0);
60 store(out+$8, r1);
61 store(out+$8+$8, r2);
62 store(out+$8+$8+$8, r3)
63 }.
64
65 Definition u256_shr := func!(p_out, p_x, n) {
66 x0 = load(p_x); x1 = load(p_x+$8); x2 = load(p_x+$8+$8); x3 = load(p_x+$8+$8+$8);
67 unpack! y0 = shrd(x0, x1, n);
68 unpack! y1 = shrd(x1, x2, n);
69 unpack! y2 = shrd(x2, x3, n);
70 y3 = x3 >> n;
71 store(p_out, y0); store(p_out+$8, y1); store(p_out+$8+$8, y2); store(p_out+$8+$8+$8, y3)
72 }.
73
74 Definition u256_set_p256_minushalf_conditional := func!(p_out, mask) {
75 mh0 = -$1; mh1 = mh0>>$33; mh2 = mh0<<$63; mh3 = mh0<<$32>>$1; (* minus one half modulo p256 *)
76 store(p_out, mask&mh0);
77 store(p_out+$8, mask&mh1);
78 store(p_out+$8+$8, mask&mh2);
79 store(p_out+$8+$8+$8, mask&mh3)
80 }.
81
82 Definition p256_coord_add := func!(p_out, p_x, p_y) {
83 unpack! t0, carry = full_add(load(p_x), load(p_y), $0);
84 unpack! t1, carry = full_add(load(p_x+$8), load(p_y+$8), carry);
85 unpack! t2, carry = full_add(load(p_x+$8+$8), load(p_y+$8+$8), carry);
86 unpack! t3, carry = full_add(load(p_x+$8+$8+$8), load(p_y+$8+$8+$8), carry);
87 unpack! r0, borrow = full_sub(t0, $0xffffffffffffffff, $0);
88 unpack! r1, borrow = full_sub(t1, $0xffffffff, borrow);
89 unpack! r2, borrow = full_sub(t2, $0, borrow);
90 unpack! r3, borrow = full_sub(t3, $0xffffffff00000001, borrow);
91 unpack! r4, borrow = full_sub(carry, $0, borrow);
92 unpack! r0 = br_cmov(borrow, t0, r0);
93 unpack! r1 = br_cmov(borrow, t1, r1);
94 unpack! r2 = br_cmov(borrow, t2, r2);
95 unpack! r3 = br_cmov(borrow, t3, r3);
96 store(p_out, r0);
97 store(p_out+$8, r1);
98 store(p_out+$8+$8, r2);
99 store(p_out+$8+$8+$8, r3)
100 }.
101
102
103 Import String List bedrock2.ToCString Macros.WithBaseName.
104 Local Open Scope string_scope. Local Open Scope list_scope.
105
106 Definition coord64 := &[,
107 shrd.shrd;
108 p256_coord_add;
109 p256_coord_sub;
110 p256_coord_nonzero;
111 u256_shr;
112 u256_set_p256_minushalf_conditional
113 ].
114
115 From Coq Require Import String List.
116 Compute String.concat LF (List.map (fun f => "static inline "++ c_func f) coord64)%string.
117
118 Local Ltac length_tac :=
119 repeat rewrite
120 ?length_coord, ?length_point,
121 ?app_length, ?firstn_length, ?skipn_length, ?length_le_split, ?length_nil,
122 ?Nat.min_l
123 by (trivial; lia); trivial; lia.
124
125 Import BasicC64Semantics.
126
127 Lemma fiat_coord_nonzero_ok : program_logic_goal_for_function! p256_coord_nonzero.
128 Proof.
129 cbv [spec_of_p256_coord_nonzero];
130 repeat straightline.
131
132 rename H0 into Hm.
133 cbv [coord.to_bytes] in *.
134 set (x * coord.R)%F as xR in *.
135 do 4 (
136 rewrite <-(firstn_skipn 8 (le_split _ _)), List.firstn_le_split, skipn_le_split, ?Z.shiftr_shiftr in Hm by lia;
137 simpl Nat.min in Hm; simpl Nat.sub in Hm; set (le_split 8 _) in Hm);
138 rewrite List.le_split_0_l in Hm.
139 simpl Z.mul in *; simpl Z.add in *.
140 subst l0 l1 l2 l3.
141
142 repeat (seprewrite_in_by (@Array.sep_eq_of_list_word_at_app) Hm length_tac).
143 repeat seprewrite_in_by (symmetry! @Array.array1_iff_eq_of_list_word_at) Hm length_tac.
144 repeat seprewrite_in_by @Scalars.scalar_of_bytes Hm length_tac.
145 rewrite ?le_combine_split, ?Z.shiftr_div_pow2 in Hm by length_tac.
146 simpl Z.of_nat in *.
147 simpl Z.mul in *.
148
149 repeat (straightline || straightline_call).
150 clear Hm.
151
152 subst x1. f_equal. f_equal. apply Bool.eq_true_iff_eq.
153 rewrite Z.eqb_eq, F.eqb_eq.
154 rewrite <-word.unsigned_of_Z_0, !word.unsigned_inj_iff by exact _.
155 rewrite !word.lor_0_iff, !word.zero_of_Z_iff, !Zdiv.Zmod_mod by exact _.
156
157 rewrite coord.zero_iff; fold xR.
158 rewrite F.zero_iff_to_Z.
159 pose proof F.to_Z_range xR eq_refl as range.
160 clearbody xR; clear x; set (F.to_Z xR) as x in *; clearbody x.
161 clear -range.
162 Time PreOmega.Z.to_euclidean_division_equations.
163 cbv [p256] in *.
164 lia.
165 Qed.
166
167 Import shrd.
168 Lemma u256_shr_ok : program_logic_goal_for_function! u256_shr.
169 Proof.
170 cbv [spec_of_u256_shr].
171 straightline; repeat straightline_cleanup.
172 cbv [coord.to_bytes] in *.
173 let domem Hm :=
174 do 4 (
175 rewrite <-(firstn_skipn 8 (le_split _ _)), List.firstn_le_split, skipn_le_split, ?Z.shiftr_shiftr in Hm by lia;
176 simpl Nat.min in Hm; simpl Nat.sub in Hm; set (le_split 8 _) in Hm) in
177 domem H2.
178 rewrite <-(firstn_skipn 8 out), <-(firstn_skipn 8 out[_:]), <-(firstn_skipn 8 out[_:][_:]), ?skipn_skipn, ?firstn_skipn in H3.
179 subst l l0 l1 l2.
180
181 let domem Hm :=
182 repeat seprewrite_in_by (@Array.sep_eq_of_list_word_at_app) Hm length_tac;
183 repeat seprewrite_in_by (symmetry! @Array.array1_iff_eq_of_list_word_at) Hm length_tac;
184 repeat seprewrite_in_by @Scalars.scalar_of_bytes Hm length_tac;
185 rewrite ?le_combine_split in Hm by lia
186 in domem H2; domem H3.
187
188 simpl Z.of_nat in *; simpl Z.mul in *; simpl Z.add in *; simpl Nat.add in *.
189 progress repeat (straightline || straightline_call); try ZnWords.ZnWords.
190
191 cbv [Scalars.scalar Scalars.truncated_word Scalars.truncated_scalar] in H14.
192 change (Memory.bytes_per access_size.word) with 8%nat in H14.
193 do 3 seprewrite_in_by (@Array.list_word_at_app_of_adjacent_eq) H14 ltac:(rewrite ?app_length, ?length_le_split, ?length_nil; try ZnWords.ZnWords).
194 revert H14;
195 eassert ((_ ++ _) = _) as ->; [|intros;ecancel_assumption].
196 eapply le_combine_inj; rewrite ?app_length, ?length_le_combine, ?length_le_split; trivial.
197 rewrite !le_combine_app, !le_combine_split, ?length_le_split; change (2^(8%nat*8)) with (2^64).
198 rewrite ?Z.mod_small by (cbv [p256] in *; ZnWords.ZnWords).
199 subst y3; rewrite word.unsigned_sru_nowrap, Z.shiftr_div_pow2 by ZnWords.ZnWords.
200 subst x3 x2 x1 x0.
201 progress rewrite ?word.unsigned_of_Z in *; cbv [word.wrap] in *; rewrite <-?Z.land_ones in * by lia.
202 pose proof word.unsigned_range n.
203 DestructHead.destruct_head' @and.
204 simpl Z.mul.
205 all : rewrite ?H11, ?H12, ?H13.
206 Import bitblast.
207 all : apply Z.bits_inj'; intros i Hi;
208 repeat rewrite <-?Z.shiftr_div_pow2, ?Z.land_spec, ?Z.lor_spec, ?Z.shiftr_spec', ?Z.shiftl_spec', ?Z.testbit_ones by try ZnWords.ZnWords.
209 all: repeat (rewrite
210 ?Bool.andb_true_l, ?Bool.andb_true_r, ?Bool.orb_true_l, ?Bool.orb_true_r,
211 ?Bool.andb_false_l, ?Bool.andb_false_r, ?Bool.orb_false_l, ?Bool.orb_false_r,
212 ?Z.testbit_0_l, ?Z.testbit_neg_r, ?Z.testbit_high
213 by intuition (idtac;
214 match goal with
215 | H : ?x < ?y^?a |- ?x < ?y^?b =>
216 apply (Z.lt_le_trans _ (y^a)), Z.pow_le_mono_r; lia
217 | _ => lia
218 end);
219 cbn [negb]; trivial; try lia
220 || ((case Z.ltb_spec; [|]; intros)||(case Z.leb_spec; [|]; intros))).
221 all:f_equal; try lia.
222 Qed.
223
224
225 Import full_sub full_add.
226 Local Existing Instances spec_of_full_sub spec_of_full_add.
227
228 Import Specs.
229
230 Lemma p256_coord_sub_nonmont_ok :
231 let '_ := spec_of_p256_coord_sub_nonmont in
232 program_logic_goal_for_function! p256_coord_sub.
233 Proof.
234 cbv [spec_of_p256_coord_sub_nonmont].
235 straightline; repeat straightline_cleanup.
236 cbv [coord.to_bytes] in *.
237 let domem Hm :=
238 do 4 (
239 rewrite <-(firstn_skipn 8 (le_split _ _)), List.firstn_le_split, skipn_le_split, ?Z.shiftr_shiftr in Hm by lia;
240 simpl Nat.min in Hm; simpl Nat.sub in Hm; set (le_split 8 _) in Hm) in
241 domem H8; domem H9.
242 rewrite <-(firstn_skipn 8 out), <-(firstn_skipn 8 out[_:]), <-(firstn_skipn 8 out[_:][_:]), ?skipn_skipn, ?firstn_skipn in H10.
243 subst l l0 l1 l2 l3 l4 l5 l6.
244 rewrite ?length_le_split in *.
245
246 let domem Hm :=
247 repeat seprewrite_in_by (@Array.sep_eq_of_list_word_at_app) Hm length_tac;
248 repeat seprewrite_in_by (symmetry! @Array.array1_iff_eq_of_list_word_at) Hm length_tac;
249 repeat seprewrite_in_by @Scalars.scalar_of_bytes Hm length_tac;
250 rewrite ?le_combine_split, ?Z.shiftr_div_pow2 in Hm by lia
251 in domem H8; domem H9; domem H10.
252
253 simpl Z.of_nat in *; simpl Z.mul in *; simpl Z.add in *; simpl Nat.add in *.
254 repeat (straightline || straightline_call); try ZnWords.ZnWords.
255
256 cbv [Scalars.scalar Scalars.truncated_word Scalars.truncated_scalar] in H23.
257 change (Memory.bytes_per access_size.word) with 8%nat in H23.
258 repeat seprewrite_in_by (@Array.list_word_at_app_of_adjacent_eq) H23 ltac:(rewrite ?app_length, ?length_le_split, ?length_nil; try ZnWords.ZnWords).
259 rewrite <-?app_assoc in H23.
260 revert H23;
261 eassert ((_ ++ _) = _) as ->; [|intros;ecancel_assumption].
262 eapply le_combine_inj; rewrite ?app_length, ?length_le_combine, ?length_le_split; trivial.
263 rewrite !le_combine_app, !le_combine_split, ?length_le_split; change (2^(8%nat*8)) with (2^64).
264 pose proof F.to_Z_range ((x - y)) eq_refl.
265 rewrite ?Z.mod_small by (cbv [p256] in *; ZnWords.ZnWords).
266
267 pose proof F.to_Z_range x eq_refl.
268 pose proof F.to_Z_range y eq_refl.
269 rewrite F.to_Z_sub.
270 rewrite ?word.unsigned_of_Z in *; cbv [word.wrap] in *; rewrite ?Zdiv.Zmod_mod in *.
271
272 cbv [Semantics.interp_op1] in *.
273 assert (x9 = word.of_Z 0 /\ F.to_Z y <= F.to_Z x
274 \/x9 = word.of_Z 1 /\ F.to_Z x < F.to_Z y) as [ [-> ?]|[-> ?]] by
275 (rewrite <-!word.unsigned_inj_iff; cbv [p256] in *; ZnWords.ZnWords).
276 { rewrite ?Z.add_0_r in *; cbv [p256] in *. ZnWords.ZnWords. }
277 rewrite <-(Z.mod_add _ 1), Z.mod_small by (cbv [p256] in *; ZnWords.ZnWords).
278 rewrite word.and_m1_l, ?word.unsigned_of_Z_nowrap in * by lia.
279 cbv [p256] in *; ZnWords.ZnWords.
280 Qed.
281
282 Import DestructHead.
283 Lemma p256_coord_sub_ok : program_logic_goal_for_function! p256_coord_sub.
284 Proof.
285 cbv [program_logic_goal_for spec_of_p256_coord_sub ]; intros; destruct_head' @and; destruct_head' @ex.
286 eapply WeakestPreconditionProperties.Proper_call; [|unshelve (eapply p256_coord_sub_nonmont_ok; trivial)]; cycle 1.
287 { exact (F.mul x coord.R). } { exact (F.mul y coord.R). } { shelve. } { exact out. }
288 { repeat intro; intuition eauto using ex_intro with nocore. }
289 repeat intro; destruct_head' @and.
290 cbv [coord.to_bytes] in *; ssplit; trivial.
291 enough ((x-y)*coord.R = x*coord.R - y*coord.R)%F as -> by eauto.
292 ring.
293 Qed.
294
295
296 Definition spec_of_p256_coord_add_nonmont : spec_of "p256_coord_add" :=
297 fnspec! "p256_coord_add" p_out p_x p_y / out (x y : F p256) R,
298 { requires t m := m =*> (le_split 32 x)$@p_x /\ m =*> (le_split 32 y)$@p_y /\ m =* out$@p_out * R /\ length out = 32%nat;
299 ensures t' m := t' = t /\ m =* (le_split 32 (x+y)%F)$@p_out * R }.
300
301 Lemma p256_coord_add_nonmont_ok :
302 let '_ := spec_of_p256_coord_add_nonmont in
303 program_logic_goal_for_function! p256_coord_add.
304 Proof.
305 straightline; repeat straightline_cleanup.
306 clear H H0 H1. clear H3 H4 H5 H6.
307 clear H8; rename H12 into H8.
308 clear H9; rename H13 into H9.
309 clear H10; rename H14 into H10.
310
311 cbv [coord.to_bytes] in *.
312 let domem Hm :=
313 do 4 (
314 rewrite <-(firstn_skipn 8 (le_split _ _)), List.firstn_le_split, skipn_le_split, ?Z.shiftr_shiftr in Hm by lia;
315 simpl Nat.min in Hm; simpl Nat.add in Hm; set (le_split 8 _) in Hm) in
316 domem H8; domem H9.
317 rewrite <-(firstn_skipn 8 out), <-(firstn_skipn 8 out[_:]), <-(firstn_skipn 8 out[_:][_:]), ?skipn_skipn, ?firstn_skipn in H10.
318 subst l l0 l1 l2 l3 l4 l5 l6.
319 rewrite ?length_le_split in *.
320
321 let domem Hm :=
322 repeat seprewrite_in_by (@Array.sep_eq_of_list_word_at_app) Hm length_tac;
323 repeat seprewrite_in_by (symmetry! @Array.array1_iff_eq_of_list_word_at) Hm length_tac;
324 repeat seprewrite_in_by @Scalars.scalar_of_bytes Hm length_tac;
325 rewrite ?le_combine_split, ?Z.shiftr_div_pow2 in Hm by lia
326 in domem H8; domem H9; domem H10.
327
328 simpl Z.of_nat in *; simpl Z.mul in *; simpl Z.add in *; simpl Nat.add in *.
329 repeat (straightline || straightline_call); try ZnWords.ZnWords.
330
331 rename H18 into H23.
332 cbv [Scalars.scalar Scalars.truncated_word Scalars.truncated_scalar] in H23.
333 change (Memory.bytes_per access_size.word) with 8%nat in H23.
334 repeat seprewrite_in_by (@Array.list_word_at_app_of_adjacent_eq) H23 ltac:(rewrite ?app_length, ?length_le_split, ?length_nil; try ZnWords.ZnWords).
335 rewrite <-?app_assoc in H23.
336 revert H23; eassert ((_ ++ _) = _)%list as ->; [|intros;ecancel_assumption].
337 eapply le_combine_inj; rewrite ?app_length, ?length_le_combine, ?length_le_split; trivial.
338 rewrite !le_combine_app, !le_combine_split, ?length_le_split; change (2^(8%nat*8)) with (2^64).
339 pose proof F.to_Z_range ((x + y)) eq_refl.
340 rewrite ?Z.mod_small by (cbv [p256] in *; ZnWords.ZnWords).
341
342 pose proof F.to_Z_range x eq_refl.
343 pose proof F.to_Z_range y eq_refl.
344 rewrite F.to_Z_add.
345 rewrite ?word.unsigned_of_Z in *; cbv [word.wrap] in *; rewrite ?Zdiv.Zmod_mod, ?Z.mod_0_l, ?Z.add_0_r, ?Z.sub_0_r in * by (clear; lia).
346
347 destruct Z.eqb eqn:Hborrow in *; [apply Z.eqb_eq in Hborrow|apply Z.eqb_neq in Hborrow]; repeat straightline.
348 { rewrite <-(Z.mod_add _ (-1)) by inversion 1; rewrite Z.mod_small; cbv [p256] in *; ZnWords.ZnWords. }
349 { rewrite Z.mod_small; cbv [p256] in *; try ZnWords.ZnWords. }
350 Qed.
351
352 Lemma p256_coord_add_ok : program_logic_goal_for_function! p256_coord_add.
353 Proof.
354 cbv [program_logic_goal_for spec_of_p256_coord_add ]; intros; destruct_head' @and; destruct_head' @ex.
355 eapply WeakestPreconditionProperties.Proper_call; [|unshelve (eapply p256_coord_add_nonmont_ok; trivial)]; cycle 1.
356 { exact (F.mul x coord.R). } { exact (F.mul y coord.R). } { shelve. } { exact out. }
357 { repeat intro; intuition eauto using ex_intro with nocore. }
358 repeat intro.
359 cbv [coord.to_bytes] in *.
360 assert ((x+y)*coord.R = x*coord.R + y*coord.R)%F as -> by ring; eauto.
361 Qed.
362
363 Lemma u256_set_p256_minushalf_conditional_ok : program_logic_goal_for_function! u256_set_p256_minushalf_conditional.
364 Proof.
365 cbv [spec_of_p256_coord_set_minushalf_conditional].
366 straightline; repeat straightline_cleanup.
367 rename H into Hm.
368
369 rewrite <-(firstn_skipn 8 out), <-(firstn_skipn 8 out[_:]), <-(firstn_skipn 8 out[_:][_:]), ?skipn_skipn, ?firstn_skipn in Hm.
370 repeat seprewrite_in_by (@Array.sep_eq_of_list_word_at_app) Hm length_tac.
371 repeat seprewrite_in_by (symmetry! @Array.array1_iff_eq_of_list_word_at) Hm length_tac.
372 repeat seprewrite_in_by @Scalars.scalar_of_bytes Hm length_tac.
373 simpl Z.of_nat in *.
374
375 repeat straightline; ssplit; trivial.
376
377 (* postcondition *)
378
379 clear Hm; rename H3 into Hm.
380 cbv [Scalars.scalar Scalars.truncated_word Scalars.truncated_scalar] in Hm.
381 progress change (Memory.bytes_per access_size.word) with 8%nat in Hm.
382 repeat seprewrite_in_by (@Array.list_word_at_app_of_adjacent_eq) Hm ltac:(rewrite ?app_length, ?length_le_split, ?length_nil; try ZnWords.ZnWords).
383
384 revert Hm; eassert ((_ ++ _) = _)%list as ->; [|intros;ecancel_assumption].
385 eapply le_combine_inj. { length_tac. }
386 subst v v0 v1 v2 mask mh0 mh1 mh2 mh3.
387 case b; Decidable.vm_decide.
388 Qed.