Debian Codegraph @

src/Bedrock/Field/Interface/Compilation2.v

raw
1 Require Import Rupicola.Lib.Api.
2 Require Import Rupicola.Lib.Alloc.
3 Require Import Crypto.Bedrock.Specs.Field.
4 Require Import Crypto.Arithmetic.PrimeFieldTheorems.
5 Local Open Scope Z_scope.
6 Import bedrock2.Memory.
7
8 Section Compile.
9 Context {width: Z} {BW: Bitwidth width} {word: word.word width} {mem: map.map word Byte.byte}.
10 Context {locals: map.map String.string word}.
11 Context {ext_spec: bedrock2.Semantics.ExtSpec}.
12 Context {word_ok : word.ok word} {mem_ok : map.ok mem}.
13 Context {locals_ok : map.ok locals}.
14 Context {ext_spec_ok : Semantics.ext_spec.ok ext_spec}.
15 Context {field_parameters : FieldParameters}
16 {field_representation : FieldRepresentation}
17 {field_representation_ok : FieldRepresentation_ok (field_representation:=field_representation)}.
18
19 Definition maybe_bounded mbounds v :=
20 match mbounds with
21 | Some bounds => bounded_by bounds v
22 | None => True
23 end.
24
25 (* TODO: Replace uses of the old FElem with this? *)
26 Definition FElem mbounds ptr v :=
27 (Lift1Prop.ex1 (fun v' => (emp (feval v' = v /\ maybe_bounded mbounds v') * FElem ptr v')%sep)).
28
29 Lemma drop_bounds_FElem x_ptr x bounds
30 : Lift1Prop.impl1 (FElem bounds x_ptr x)
31 (FElem None x_ptr x).
32 Proof using mem_ok word_ok.
33 unfold FElem.
34 intros m H.
35 sepsimpl.
36 exists x0.
37 sepsimpl; simpl in *; eauto using relax_bounds.
38 Qed.
39
40 Lemma relax_bounds_FElem x_ptr x
41 : Lift1Prop.impl1 (FElem (Some tight_bounds) x_ptr x)
42 (FElem (Some loose_bounds) x_ptr x).
43 Proof using field_representation_ok mem_ok word_ok.
44 unfold FElem.
45 intros m H.
46 sepsimpl.
47 exists x0.
48 sepsimpl; simpl in *; eauto using relax_bounds.
49 Qed.
50
51 Lemma FElem'_iff_bytes
52 : forall px : word.rep,
53 Lift1Prop.iff1 (Lift1Prop.ex1 (Placeholder px)) (Lift1Prop.ex1 (FElem None px)).
54 Proof using field_representation_ok mem_ok word_ok.
55 unfold FElem.
56 intros.
57 split; intros.
58 {
59 destruct H as [bs H].
60 apply Placeholder_impl_FElem_bytes in H.
61 do 2 eexists.
62 sepsimpl; simpl; eauto.
63 }
64 {
65 destruct H as [ws [? H]].
66 sepsimpl.
67 eexists.
68 eapply FElem_impl_Placeholder; eauto.
69 }
70 Qed.
71
72 Lemma FElem'_to_bytes
73 : forall bounds px x,
74 Lift1Prop.impl1 (FElem bounds px x) (Lift1Prop.ex1 (Placeholder px)).
75 Proof.
76 intros. cbv [FElem]. intros m [f F].
77 extract_ex1_and_emp_in F.
78 sepsimpl.
79 eexists. eapply FElem_impl_Placeholder.
80 eassumption.
81 Qed.
82
83 #[refine]
84 Instance felem_alloc : Allocable (FElem None) :=
85 {|
86 size_in_bytes := felem_size_in_bytes;
87 size_in_bytes_mod := felem_size_in_bytes_mod;
88 |}.
89 Proof.
90 {
91 intros; intros m H.
92 apply FElem'_to_bytes in H.
93 cbv[Placeholder] in *.
94 sepsimpl.
95 eexists.
96 pose felem_size_ok.
97 ssplit; try eassumption.
98 lia.
99 }
100 {
101 intros; intros m H.
102 apply FElem'_iff_bytes.
103 cbv [Memory.anybytes Placeholder] in *. sepsimpl.
104 eexists; try eassumption.
105 sepsimpl; try eassumption.
106 }
107 Defined.
108
109 Local Ltac prove_field_compilation :=
110 repeat straightline';
111 handle_call;
112 lazymatch goal with
113 | |- sep _ _ _ => ecancel_assumption_impl
114 | _ => idtac
115 end; eauto;
116 sepsimpl; repeat straightline'; subst; eauto.
117
118 Local Hint Extern 1 (spec_of _) => (simple refine (@spec_of_BinOp _ _ _ _ _ _ _ _ _ _)) : typeclass_instances.
119 Local Hint Extern 1 (spec_of _) => (simple refine (@spec_of_UnOp _ _ _ _ _ _ _ _ _ _)) : typeclass_instances.
120
121 Lemma compile_binop {name} {op: BinOp name}
122 {tr m l functions} x y:
123 let v := bin_model x y in
124 forall P (pred: P v -> predicate) (k: nlet_eq_k P v) k_impl
125 Rx Ry Rout out x_ptr x_var y_ptr y_var out_ptr out_var
126 bound_out,
127
128 (_: spec_of name) functions ->
129
130 map.get l out_var = Some out_ptr ->
131 (FElem bound_out out_ptr out * Rout)%sep m ->
132
133 (FElem (Some bin_xbounds) x_ptr x * Rx)%sep m ->
134 (FElem (Some bin_ybounds) y_ptr y * Ry)%sep m ->
135 map.get l x_var = Some x_ptr ->
136 map.get l y_var = Some y_ptr ->
137
138 (let v := v in
139 forall m',
140 sep (FElem (Some bin_outbounds) out_ptr v) Rout m' ->
141 (<{ Trace := tr;
142 Memory := m';
143 Locals := l;
144 Functions := functions }>
145 k_impl
146 <{ pred (k v eq_refl) }>)) ->
147 <{ Trace := tr;
148 Memory := m;
149 Locals := l;
150 Functions := functions }>
151 cmd.seq
152 (cmd.call [] name [expr.var out_var; expr.var x_var; expr.var y_var])
153 k_impl
154 <{ pred (nlet_eq [out_var] v k) }>.
155 Proof using ext_spec_ok locals_ok mem_ok word_ok field_representation_ok.
156 repeat straightline'.
157 unfold FElem in *.
158 sepsimpl.
159 prove_field_compilation.
160 apply H6.
161
162 eapply Proper_sep_impl1; eauto.
163 2:exact(fun a b => b).
164 intros m' H'.
165 eexists.
166 sepsimpl;
167 eauto.
168 Qed.
169
170 Lemma compile_unop {name} (op: UnOp name) {tr m l functions} x:
171 let v := un_model x in
172 forall P (pred: P v -> predicate) (k: nlet_eq_k P v) k_impl
173 Rin Rout out x_ptr x_var out_ptr out_var out_bounds,
174
175 (_: spec_of name) functions ->
176
177 map.get l out_var = Some out_ptr ->
178 (FElem out_bounds out_ptr out * Rout)%sep m ->
179
180 (FElem (Some un_xbounds) x_ptr x * Rin)%sep m ->
181 map.get l x_var = Some x_ptr ->
182
183 (let v := v in
184 forall m',
185 sep (FElem (Some un_outbounds) out_ptr v) Rout m' ->
186 (<{ Trace := tr;
187 Memory := m';
188 Locals := l;
189 Functions := functions }>
190 k_impl
191 <{ pred (k v eq_refl) }>)) ->
192 <{ Trace := tr;
193 Memory := m;
194 Locals := l;
195 Functions := functions }>
196 cmd.seq
197 (cmd.call [] name [expr.var out_var; expr.var x_var])
198 k_impl
199 <{ pred (nlet_eq [out_var] v k) }>.
200 Proof using ext_spec_ok locals_ok mem_ok word_ok field_representation_ok.
201 repeat straightline'.
202 unfold FElem in *.
203 sepsimpl.
204 prove_field_compilation.
205 apply H4.
206
207 eapply Proper_sep_impl1; eauto.
208 2:exact(fun a b => b).
209 intros m' H'.
210 eexists.
211 sepsimpl;
212 eauto.
213 Qed.
214
215
216 Ltac cleanup_op_lemma lem := (* This makes [simple apply] work *)
217 let lm := fresh in
218 let op := match lem with _ _ ?op => op end in
219 let op_hd := term_head op in
220 let simp proj :=
221 (let hd := term_head proj in
222 let reduced := (eval cbv [op_hd hd] in proj) in
223 change proj with reduced in (type of lm)) in
224 pose lem as lm;
225 first [ simp (bin_model (BinOp := op));
226 simp (bin_xbounds (BinOp := op));
227 simp (bin_ybounds (BinOp := op));
228 simp (bin_outbounds (BinOp := op))
229 | simp (un_model (UnOp := op));
230 simp (un_xbounds (UnOp := op));
231 simp (un_outbounds (UnOp := op)) ];
232 let t := type of lm in
233 let t := (eval cbv beta in t) in
234 exact (lm: t).
235
236 Notation make_bin_lemma op :=
237 ltac:(cleanup_op_lemma (@compile_binop _ op)) (only parsing).
238
239 Definition compile_mul := make_bin_lemma bin_mul.
240 Definition compile_add := make_bin_lemma bin_add.
241 Definition compile_sub := make_bin_lemma bin_sub.
242
243 Notation make_un_lemma op :=
244 ltac:(cleanup_op_lemma (@compile_unop _ op)) (only parsing).
245
246 Definition compile_square := make_un_lemma un_square.
247 Definition compile_scmula24 := make_un_lemma un_scmula24.
248
249 Local Hint Extern 1 (spec_of _) => (simple refine (@spec_of_felem_copy _ _ _ _ _ _ _ _)) : typeclass_instances.
250
251 Lemma compile_felem_copy {tr m l functions} x :
252 let v := x in
253 forall P (pred: P v -> predicate) (k: nlet_eq_k P v) k_impl
254 R x_ptr x_var out out_ptr out_var x_bound out_bound,
255
256 spec_of_felem_copy functions ->
257
258 map.get l out_var = Some out_ptr ->
259
260 (FElem x_bound x_ptr x * FElem out_bound out_ptr out * R)%sep m ->
261 map.get l x_var = Some x_ptr ->
262
263 (let v := v in
264 forall m',
265 (FElem x_bound x_ptr x * FElem x_bound out_ptr x * R)%sep m' ->
266 (<{ Trace := tr;
267 Memory := m';
268 Locals := l;
269 Functions := functions }>
270 k_impl
271 <{ pred (k v eq_refl) }>)) ->
272 <{ Trace := tr;
273 Memory := m;
274 Locals := l;
275 Functions := functions }>
276 cmd.seq
277 (cmd.call [] felem_copy [expr.var out_var; expr.var x_var])
278 k_impl
279 <{ pred (nlet_eq [out_var] v k) }>.
280 Proof using ext_spec_ok locals_ok mem_ok word_ok field_representation_ok.
281 repeat straightline'.
282 unfold FElem in *.
283 sepsimpl.
284 prove_field_compilation.
285 apply H3.
286
287 extract_ex1_and_emp_in_goal; ssplit; eauto.
288 Qed.
289
290 Local Hint Extern 1 (spec_of _) => (simple refine (@spec_of_from_word _ _ _ _ _ _ _ _)) : typeclass_instances.
291
292 Lemma compile_from_word {tr m l functions} x:
293 let v := F.of_Z _ x in
294 forall P (pred: P v -> predicate) (k: nlet_eq_k P v) k_impl
295 R (wx : word) out out_ptr out_var out_bounds,
296
297 spec_of_from_word functions ->
298
299 map.get l out_var = Some out_ptr ->
300 (FElem out_bounds out_ptr out * R)%sep m ->
301
302 word.unsigned wx = x ->
303
304 (let v := v in
305 forall m',
306 (FElem (Some tight_bounds) out_ptr v * R)%sep m' ->
307 (<{ Trace := tr;
308 Memory := m';
309 Locals := l;
310 Functions := functions }>
311 k_impl
312 <{ pred (k v eq_refl) }>)) ->
313 <{ Trace := tr;
314 Memory := m;
315 Locals := l;
316 Functions := functions }>
317 cmd.seq
318 (cmd.call [] from_word
319 [expr.var out_var; expr.literal x])
320 k_impl
321 <{ pred (nlet_eq [out_var] v k) }>.
322 Proof using ext_spec_ok locals_ok mem_ok word_ok field_representation_ok.
323 repeat straightline'.
324 unfold FElem in *.
325 extract_ex1_and_emp_in H1.
326 prove_field_compilation.
327 match goal with H : _ |- _ => rewrite word.of_Z_unsigned in H end.
328 apply H3.
329 extract_ex1_and_emp_in_goal; ssplit; eauto.
330 Qed.
331
332 Local Hint Extern 1 (spec_of _) => (simple refine (@spec_of_from_bytes _ _ _ _ _ _ _ _)) : typeclass_instances.
333
334 (*
335 Lemma compile_from_bytes {tr m l functions} x :
336 let v : F _ := feval_bytes x in
337 forall P (pred: P v -> predicate) (k: nlet_eq_k P v) k_impl
338 Rx R x_ptr x_var out out_ptr out_var out_bound,
339
340 spec_of_from_bytes functions ->
341
342 map.get l out_var = Some out_ptr ->
343
344 (FElemBytes x_ptr x * Rx)%sep m ->
345 (FElem out_bound out_ptr out * R)%sep m ->
346 map.get l x_var = Some x_ptr ->
347
348 (let v := v in
349 forall m',
350 (FElem (Some tight_bounds) out_ptr v * R)%sep m' ->
351 (<{ Trace := tr;
352 Memory := m';
353 Locals := l;
354 Functions := functions }>
355 k_impl
356 <{ pred (k v eq_refl) }>)) ->
357 <{ Trace := tr;
358 Memory := m;
359 Locals := l;
360 Functions := functions }>
361 cmd.seq
362 (cmd.call [] from_bytes [expr.var out_var; expr.var x_var])
363 k_impl
364 <{ pred (nlet_eq [out_var] v k) }>.
365 Proof using ext_spec_ok locals_ok mem_ok word_ok.
366 repeat straightline'.
367 unfold FElem in *.
368 sepsimpl.
369 prove_field_compilation.
370 apply H4.
371
372 eapply Proper_sep_impl1; eauto; try reflexivity.
373 eapply Lift1Prop.impl1_ex1_r.
374 intros m' H'; ssplit; eapply sep_emp_l; ssplit; eauto.
375 Qed.
376
377 Lemma compile_to_bytes {tr m l functions} x :
378 let v : list _ := Z_to_bytes (F.to_Z x) encoded_felem_size_in_bytes in
379 forall P (pred: P v -> predicate) (k: nlet_eq_k P v) k_impl
380 Rx R x_ptr x_var out out_ptr out_var,
381
382 spec_of_to_bytes functions ->
383
384 map.get l out_var = Some out_ptr ->
385
386 (FElem (Some tight_bounds) x_ptr x * Rx)%sep m ->
387 (FElemBytes out_ptr out * R)%sep m ->
388 map.get l x_var = Some x_ptr ->
389
390 (let v := v in
391 forall m',
392 (FElemBytes out_ptr v * R)%sep m' ->
393 (<{ Trace := tr;
394 Memory := m';
395 Locals := l;
396 Functions := functions }>
397 k_impl
398 <{ pred (k v eq_refl) }>)) ->
399 <{ Trace := tr;
400 Memory := m;
401 Locals := l;
402 Functions := functions }>
403 cmd.seq
404 (cmd.call [] to_bytes [expr.var out_var; expr.var x_var])
405 k_impl
406 <{ pred (nlet_eq [out_var] v k) }>.
407 Proof using ext_spec_ok locals_ok mem_ok word_ok.
408 repeat straightline'.
409 subst v.
410 unfold FElem in *.
411 sepsimpl;
412 eapply Proper_call; [ |eapply H]; cycle 1;
413 [ ssplit;
414 lazymatch goal with
415 | |- (_ ⋆ _) _ => ecancel_assumption
416 | |- exists R, (_ ⋆ R) _ => eexists; ecancel_assumption
417 | _ => idtac
418 end
419 | cbv[postcondition_func postcondition_func_norets] in *;
420 repeat straightline; destruct_lists_of_known_length;
421 repeat straightline ].
422 { unfold maybe_bounded in *; eauto. }
423 intros ? ? ? ?; repeat straightline'.
424 subst; eauto.
425 Qed.
426 *)
427 End Compile.
428
429
430 (*must be higher priority than compile_mul*)
431 #[export] Hint Extern 6 (WeakestPrecondition.cmd _ _ _ _ _ (_ (nlet_eq _ (a24 * _)%F _))) =>
432 simple eapply compile_scmula24; shelve : compiler.
433
434 #[export] Hint Extern 8 (WeakestPrecondition.cmd _ _ _ _ _ (_ (nlet_eq _ (_ * _)%F _))) =>
435 simple eapply compile_mul; shelve : compiler.
436 #[export] Hint Extern 8 (WeakestPrecondition.cmd _ _ _ _ _ (_ (nlet_eq _ (_ + _)%F _))) =>
437 simple eapply compile_add; shelve : compiler.
438 #[export] Hint Extern 8 (WeakestPrecondition.cmd _ _ _ _ _ (_ (nlet_eq _ (_ - _)%F _))) =>
439 simple eapply compile_sub; shelve : compiler.
440 #[export] Hint Extern 8 (WeakestPrecondition.cmd _ _ _ _ _ (_ (nlet_eq _ (_ ^ 2)%F _))) =>
441 simple eapply compile_square; shelve : compiler.
442 #[export] Hint Extern 8 (WeakestPrecondition.cmd _ _ _ _ _ (_ (nlet_eq _ (F.of_Z M_pos _) _))) =>
443 simple eapply compile_from_word; shelve : compiler.
444 #[export] Hint Extern 10 (WeakestPrecondition.cmd _ _ _ _ _ (_ (nlet_eq _ ?v _))) =>
445 is_var v; simple eapply compile_felem_copy; shelve : compiler.
446
447
448 #[export] Hint Immediate relax_bounds_FElem : ecancel_impl.
449 #[export] Hint Immediate drop_bounds_FElem : ecancel_impl.
450
451
452 #[export] Hint Extern 1 (spec_of _) => (simple refine (@spec_of_BinOp _ _ _ _ _ _ _ _ _ _)) : typeclass_instances.
453 #[export] Hint Extern 1 (spec_of _) => (simple refine (@spec_of_UnOp _ _ _ _ _ _ _ _ _ _)) : typeclass_instances.
454 #[export] Hint Extern 1 (spec_of felem_copy) => (simple refine (@spec_of_felem_copy _ _ _ _ _ _ _ _)) : typeclass_instances.
455 #[export] Hint Extern 1 (spec_of from_word) => (simple refine (@spec_of_from_word _ _ _ _ _ _ _ _)) : typeclass_instances.