Debian Codegraph @

src/Bedrock/Field/Synthesis/New/UnsaturatedSolinas.v

raw
1 From Coq Require Import String.
2 From Coq Require Import List.
3 From Coq Require Import ZArith.
4 Require Import bedrock2.Syntax.
5 Require Import coqutil.Map.Interface.
6 Require Import Crypto.Arithmetic.Core.
7 Require Import Crypto.Spec.ModularArithmetic.
8 Require Import Crypto.Arithmetic.ModularArithmeticTheorems.
9 Require Import Crypto.Bedrock.Specs.Field.
10 Require Import Crypto.Bedrock.Field.Common.Types.
11 Require Import Crypto.Bedrock.Field.Common.Util.
12 Require Import Crypto.Bedrock.Field.Common.Arrays.ByteBounds.
13 Require Import Crypto.Bedrock.Field.Common.Names.VarnameGenerator.
14 Require Import Crypto.Bedrock.Field.Synthesis.New.ComputedOp.
15 Require Import Crypto.Bedrock.Field.Synthesis.New.Signature.
16 Require Import Crypto.Bedrock.Field.Translation.Parameters.Defaults.
17 Require Import Crypto.Bedrock.Field.Translation.Proofs.Func.
18 Require Import Crypto.BoundsPipeline.
19 Require Import Crypto.COperationSpecifications.
20 Require Import Crypto.PushButtonSynthesis.UnsaturatedSolinas.
21 Require Import Crypto.Language.API.
22 Require Import Crypto.UnsaturatedSolinasHeuristics.
23 Require Import Crypto.Util.ZUtil.Tactics.LtbToLt.
24 Require Import Crypto.Util.ZUtil.Tactics.PullPush.Modulo.
25 Require Import Crypto.Util.Tactics.BreakMatch.
26 Require Import Crypto.Util.Tactics.DestructHead.
27 Require Import Crypto.Util.Tactics.SpecializeBy.
28 Import ListNotations API.Compilers Types.Notations.
29
30 Class unsaturated_solinas_ops
31 {width BW word mem locals ext_spec varname_gen error}
32 {parameters_sentinel : @parameters width BW word mem locals ext_spec varname_gen error}
33 {field_parameters : FieldParameters}
34 {n s c} : Type :=
35 { mul_op :
36 computed_op
37 (UnsaturatedSolinas.carry_mul n s c width) Field.mul
38 list_binop_insizes list_binop_outsizes (list_binop_inlengths n);
39 add_op :
40 computed_op
41 (UnsaturatedSolinas.add n s c width) Field.add
42 list_binop_insizes list_binop_outsizes (list_binop_inlengths n);
43 carry_add_op :
44 computed_op
45 (UnsaturatedSolinas.carry_add n s c width) Field.add
46 list_binop_insizes list_binop_outsizes (list_binop_inlengths n);
47 sub_op :
48 computed_op
49 (UnsaturatedSolinas.sub n s c width) Field.sub
50 list_binop_insizes list_binop_outsizes (list_binop_inlengths n);
51 carry_sub_op :
52 computed_op
53 (UnsaturatedSolinas.carry_sub n s c width) Field.sub
54 list_binop_insizes list_binop_outsizes (list_binop_inlengths n);
55 opp_op :
56 computed_op
57 (UnsaturatedSolinas.opp n s c width) Field.opp
58 list_unop_insizes list_unop_outsizes (list_unop_inlengths n);
59 square_op :
60 computed_op
61 (UnsaturatedSolinas.carry_square n s c width)
62 Field.square
63 list_unop_insizes list_unop_outsizes (list_unop_inlengths n);
64 scmula24_op :
65 computed_op
66 (UnsaturatedSolinas.carry_scmul_const n s c width
67 (F.to_Z a24)) Field.scmula24
68 list_unop_insizes list_unop_outsizes (list_unop_inlengths n);
69 felem_copy_op :
70 computed_op
71 (UnsaturatedSolinas.copy n width) Field.felem_copy
72 list_unop_insizes list_unop_outsizes (list_unop_inlengths n);
73 from_word_op :
74 computed_op
75 (UnsaturatedSolinas.encode_word n s c width)
76 Field.from_word
77 from_word_insizes from_word_outsizes from_word_inlengths;
78 from_bytes_op :
79 computed_op
80 (UnsaturatedSolinas.from_bytes n s c width)
81 Field.from_bytes
82 from_bytes_insizes from_bytes_outsizes (from_bytes_inlengths
83 (n_bytes s));
84 to_bytes_op :
85 computed_op
86 (UnsaturatedSolinas.to_bytes n s c width)
87 Field.to_bytes
88 to_bytes_insizes to_bytes_outsizes (to_bytes_inlengths n);
89 }.
90 Arguments unsaturated_solinas_ops {_ _ _ _ _ _ _ _ _ _} n.
91
92 (** We need to tell [check_args] that we are requesting these functions in order to get the relevant properties out *)
93 Notation necessary_requests := ["to_bytes"; "from_bytes"]%string (only parsing).
94
95 Section UnsaturatedSolinas.
96 Context
97 {width BW word mem locals ext_spec error}
98 {parameters_sentinel : @parameters width BW word mem locals ext_spec default_varname_gen error}
99 {field_parameters : FieldParameters}
100 {ok : Types.ok}.
101
102 Context (n : nat) (s : Z) (c : list (Z * Z))
103 (M_eq : M = m s c)
104 (check_args_ok :
105 check_args n s c width necessary_requests (ErrorT.Success tt)
106 = ErrorT.Success tt)
107 (* TODO: prove a transitivity lemma for list_Z_tighter_than and use
108 loose_bounds_tighter_than to prove tight_bounds_tighter_than *)
109 (tight_bounds_tighter_than:
110 list_Z_tighter_than (tight_bounds n s c)
111 (@MaxBounds.max_bounds width n))
112 (loose_bounds_tighter_than:
113 list_Z_tighter_than (loose_bounds n s c)
114 (@MaxBounds.max_bounds width n)).
115 Local Open Scope Z_scope.
116
117 Context (ops : unsaturated_solinas_ops n s c)
118 mul_func add_func carry_add_func sub_func carry_sub_func opp_func square_func
119 scmula24_func felem_copy_func from_word_func from_bytes_func to_bytes_func
120 (mul_func_eq : mul_func = b2_func mul_op)
121 (add_func_eq : add_func = b2_func add_op)
122 (carry_add_func_eq : carry_add_func = b2_func carry_add_op)
123 (sub_func_eq : sub_func = b2_func sub_op)
124 (carry_sub_func_eq : carry_sub_func = b2_func carry_sub_op)
125 (opp_func_eq : opp_func = b2_func opp_op)
126 (square_func_eq : square_func = b2_func square_op)
127 (scmula24_func_eq : scmula24_func = b2_func scmula24_op)
128 (felem_copy_func_eq : felem_copy_func = b2_func felem_copy_op)
129 (from_word_func_eq : from_word_func = b2_func from_word_op)
130 (from_bytes_func_eq : from_bytes_func = b2_func from_bytes_op)
131 (to_bytes_func_eq : to_bytes_func = b2_func to_bytes_op).
132 Local Notation weight :=
133 (ModOps.weight (QArith_base.Qnum (limbwidth n s c))
134 (Z.pos (QArith_base.Qden (limbwidth n s c))))
135 (only parsing).
136 (* Note: annoyingly, prime_bytes_bounds is an option type, unlike loose_bounds
137 or tight_bounds, so we have to reconstruct it to match list_Z_bounded_by *)
138 Local Notation prime_bytes_bounds_value :=
139 (map (fun v : Z => Some {| ZRange.lower := 0; ZRange.upper := v |})
140 (prime_bytes_upperbound_list s)).
141 Local Instance field_representation : FieldRepresentation
142 := field_representation
143 n (n_bytes s) weight (list (option ZRange.zrange))
144 (UnsaturatedSolinasHeuristics.loose_bounds n s c)
145 (UnsaturatedSolinasHeuristics.tight_bounds n s c)
146 (prime_bytes_bounds_value)
147 list_Z_bounded_by (fun x => x).
148 Context (felem_size_ok : felem_size_in_bytes <= 2 ^ width).
149
150 Local Ltac specialize_correctness_hyp Hcorrect :=
151 cbv [feval feval_bytes bounded_by bytes_in_bounds Field.loose_bounds
152 field_representation Signature.field_representation
153 Representation.frep Representation.eval_bytes
154 Representation.eval_words
155 bin_model bin_xbounds bin_ybounds
156 un_model un_xbounds
157 ] in *;
158 (* if prime_bytes_bounds, simplify first *)
159 try match type of Hcorrect with
160 | context [list_Z_bounded_by (map ?f ?x)] =>
161 change (map f x) with prime_bytes_bounds_value in Hcorrect
162 end;
163 lazymatch type of Hcorrect with
164 | forall x y, ?Px x -> ?Py y -> _ =>
165 match goal with
166 | Hx : Px ?x, Hy : Py ?y |- _ =>
167 specialize (Hcorrect x y Hx Hy)
168 end
169 | forall x, ?Px x -> _ =>
170 match goal with
171 | Hx : Px ?x |- _ =>
172 specialize (Hcorrect x Hx)
173 end
174 end.
175
176 Lemma loose_bounds_eq : Field.loose_bounds = loose_bounds n s c.
177 Proof using Type. reflexivity. Qed.
178 Lemma tight_bounds_eq : Field.tight_bounds = tight_bounds n s c.
179 Proof using Type. reflexivity. Qed.
180
181 (* TODO: move to coqutil.Datatypes.List *)
182 Lemma Forall_repeat : forall {A} (R : A -> Prop) n x,
183 R x -> Forall R (repeat x n).
184 Proof using. clear.
185 intros; induction n; intros; cbn [repeat];
186 constructor; auto.
187 Qed.
188
189 Lemma byte_bounds_tighter_than :
190 list_Z_tighter_than prime_bytes_bounds_value
191 (byte_bounds (n_bytes s)).
192 Proof using Type.
193 clear. cbv [prime_bytes_upperbound_list].
194 apply tighter_than_if_upper_bounded_by;
195 eauto using Forall_repeat, partition_bounded_by.
196 Qed.
197
198 Lemma length_byte_bounds :
199 length prime_bytes_bounds_value = encoded_felem_size_in_bytes.
200 Proof using Type.
201 autorewrite with distr_length.
202 apply length_prime_bytes_upperbound_list.
203 Qed.
204
205 Lemma modulus_fits_in_bytes :
206 (0 < m s c <= ModOps.weight 8 1 (n_bytes s))%Z.
207 Proof using check_args_ok.
208 (* Extract information from check_args *)
209 clear - check_args_ok. apply use_curve_good in check_args_ok; rename check_args_ok into H.
210 vm_compute Primitives.request_present in H.
211 destruct_head'_and.
212 specialize_by reflexivity.
213 match goal with
214 | H : s = ModOps.weight _ _ n |- _ =>
215 pose proof H;
216 apply Freeze.bytes_n_big in H
217 end;
218 cbv [m n_bytes]; auto using limbwidth_good with zarith.
219 Qed.
220
221 Local Hint Resolve
222 relax_valid func_eq
223 inname_gen_varname_gen_disjoint
224 outname_gen_varname_gen_disjoint
225 length_tight_bounds length_loose_bounds
226 length_byte_bounds
227 byte_bounds_tighter_than
228 : helpers.
229
230 Local Hint Unfold
231 Solinas.selectznz_correct
232 Solinas.from_bytes_correct
233 Solinas.encode_word_correct
234 Solinas.to_bytes_correct
235 Solinas.carry_mul_correct
236 Solinas.carry_square_correct
237 Solinas.carry_scmul_const_correct
238 Solinas.add_correct
239 Solinas.carry_add_correct
240 Solinas.sub_correct
241 Solinas.carry_sub_correct
242 Solinas.opp_correct
243 Solinas.carry_correct
244 Solinas.zero_correct
245 Solinas.one_correct : solinas_specs.
246
247 (* Extra lemma for to_bytes because the COperationSpecifications spec does not
248 include bounds *)
249 Lemma partition_bounded_by_prime_bytes_bounds x :
250 (0 <= x < m s c)%Z ->
251 list_Z_bounded_by
252 prime_bytes_bounds_value
253 (Partition.Partition.partition
254 (ModOps.weight 8 1) (n_bytes s) x).
255 Proof using check_args_ok.
256 clear - check_args_ok. intros.
257 apply use_curve_good in check_args_ok.
258 vm_compute Primitives.request_present in check_args_ok.
259 destruct_head' and.
260 specialize_by reflexivity.
261 assert (0 <= x < s)%Z as xbounds
262 by (cbv [m] in *; auto with zarith).
263 cbv [prime_bytes_upperbound_list].
264 assert (exists lg2s, s = 2 ^ lg2s)%Z as [lg2s ?]
265 by (eexists;
266 lazymatch goal with
267 H : s = ModOps.weight _ _ _ |- _ =>
268 cbv [ModOps.weight] in H; apply H
269 end).
270 subst s. apply partition_bounded_by_partition_ones.
271 auto with zarith.
272 Qed.
273
274 (* We wrap the tactic in [once] so it doesn't take forever to fail *)
275 Ltac handle_side_conditions :=
276 once
277 (lazymatch goal with
278 | |- API.Wf (res ?op)
279 => generalize (res op), (res_eq op);
280 (* Kludge around auto being bad (COQBUG(https://github.com/coq/coq/issues/14190)) and eauto not respecting Hint Opaque *)
281 typeclasses eauto with wf_op_cache;
282 let e := lazymatch goal with | |- forall res, ?e = _ -> API.Wf _ => e end in
283 idtac "Warning: Falling back to manually proving pipeline well-formedness for" e;
284 PipelineTactics.prove_pipeline_wf ()
285 | |- context [Field.tight_bounds] => rewrite tight_bounds_eq
286 | |- context [Field.loose_bounds] => rewrite loose_bounds_eq
287 | _ => idtac
288 end;
289 lazymatch goal with
290 | |- context [expr.interp] =>
291 cbv [expr.Interp] in *; autounfold with solinas_specs in *
292 | _ => eauto with helpers
293 end).
294
295 Hint Resolve relax_list_Z_bounded_by partition_bounded_by : bounds.
296
297 Ltac simpl_map_unsigned :=
298 lazymatch goal with
299 | |- context [map Interface.word.unsigned
300 (map Interface.word.of_Z _)] =>
301 rewrite map_unsigned_of_Z;
302 erewrite MaxBounds.map_word_wrap_bounded
303 by eauto with bounds
304 | |- context [map Byte.byte.unsigned
305 (map Byte.byte.of_Z _)] =>
306 rewrite byte_map_unsigned_of_Z;
307 erewrite map_byte_wrap_bounded
308 by eauto with bounds
309 end.
310 Ltac FtoZ :=
311 apply F.eq_of_Z_iff; rewrite ?F.to_Z_of_Z;
312 cbv [M] in M_eq; rewrite ?M_eq; pull_Zmod.
313
314 Ltac loosen_bounds := lazymatch goal with
315 | |- forall _, list_Z_bounded_by ?thesebounds _ -> _ => simpl; intros; eapply relax_list_Z_bounded_by; [| eauto];
316 simpl; try apply tight_bounds_tighter_than; try apply loose_bounds_tighter_than
317 | _ => idtac
318 end.
319
320 Ltac bounds_length := simpl; intros; erewrite length_list_Z_bounded_by; eauto; try apply length_tight_bounds; try apply length_loose_bounds.
321
322 Lemma mul_func_correct :
323 valid_func (res mul_op _) ->
324 forall functions, Interface.map.get functions mul = Some mul_func ->
325 spec_of_BinOp bin_mul functions.
326 Proof using M_eq check_args_ok mul_func_eq ok
327 tight_bounds_tighter_than felem_size_ok.
328 cbv [spec_of_BinOp bin_mul]. rewrite mul_func_eq. intros.
329 pose proof carry_mul_correct
330 _ _ _ _ _ ltac:(eassumption) _ (res_eq mul_op)
331 as Hcorrect.
332 eapply list_binop_correct with (res:=res mul_op); [ .. | eassumption ];
333 handle_side_conditions; [ | | loosen_bounds | bounds_length ].
334 { (* output *value* is correct *)
335 intros.
336 specialize_correctness_hyp Hcorrect.
337 destruct Hcorrect. simpl_map_unsigned.
338 FtoZ; congruence. }
339 { (* output *bounds* are correct *)
340 intros. apply Hcorrect; auto. }
341 Qed.
342
343 Lemma square_func_correct :
344 valid_func (res square_op _) ->
345 forall functions, Interface.map.get functions square = Some square_func ->
346 spec_of_UnOp un_square functions.
347 Proof using M_eq check_args_ok ok square_func_eq
348 tight_bounds_tighter_than felem_size_ok.
349 cbv [spec_of_UnOp un_square]. rewrite square_func_eq. intros.
350 pose proof carry_square_correct
351 _ _ _ _ _ ltac:(eassumption) _ (res_eq square_op)
352 as Hcorrect.
353 eapply list_unop_correct with (res:=res square_op); [ .. | eassumption ];
354 handle_side_conditions; [ | | loosen_bounds| bounds_length ].
355 { (* output *value* is correct *)
356 intros. specialize_correctness_hyp Hcorrect.
357 destruct Hcorrect. simpl_map_unsigned.
358 rewrite F.pow_2_r. FtoZ; congruence. }
359 { (* output *bounds* are correct *)
360 intros. apply Hcorrect; auto. }
361 Qed.
362
363 Lemma add_func_correct :
364 valid_func (res add_op _) ->
365 forall functions, Interface.map.get functions Field.add = Some add_func ->
366 spec_of_BinOp bin_add functions.
367 Proof using M_eq check_args_ok add_func_eq ok
368 tight_bounds_tighter_than loose_bounds_tighter_than felem_size_ok.
369 cbv [spec_of_BinOp bin_add]. rewrite add_func_eq. intros.
370 pose proof add_correct
371 _ _ _ _ _ ltac:(eassumption) _ (res_eq add_op)
372 as Hcorrect.
373 eapply list_binop_correct with (res:=res add_op); [ .. | eassumption ];
374 handle_side_conditions; [ | | loosen_bounds | bounds_length ].
375 { (* output *value* is correct *)
376 intros.
377 specialize_correctness_hyp Hcorrect.
378 destruct Hcorrect. simpl_map_unsigned.
379 FtoZ; congruence. }
380 { (* output *bounds* are correct *)
381 intros. apply Hcorrect; auto. }
382 Qed.
383
384 Lemma carry_add_func_correct :
385 valid_func (res carry_add_op _) ->
386 forall functions, Interface.map.get functions Field.carry_add = Some carry_add_func ->
387 spec_of_BinOp bin_carry_add functions.
388 Proof using M_eq check_args_ok carry_add_func_eq ok
389 tight_bounds_tighter_than felem_size_ok.
390 cbv [spec_of_BinOp bin_carry_add]. rewrite carry_add_func_eq. intros.
391 pose proof carry_add_correct
392 _ _ _ _ _ ltac:(eassumption) _ (res_eq carry_add_op)
393 as Hcorrect.
394 eapply list_binop_correct with (res:=res carry_add_op); [ .. | eassumption ];
395 handle_side_conditions; [ | | loosen_bounds | bounds_length ].
396 { (* output *value* is correct *)
397 intros.
398 specialize_correctness_hyp Hcorrect.
399 destruct Hcorrect. simpl_map_unsigned.
400 FtoZ; congruence. }
401 { (* output *bounds* are correct *)
402 intros. apply Hcorrect; auto. }
403 Qed.
404
405 Lemma sub_func_correct :
406 valid_func (res sub_op _) ->
407 forall functions, Interface.map.get functions Field.sub = Some sub_func ->
408 spec_of_BinOp bin_sub functions.
409 Proof using M_eq check_args_ok sub_func_eq ok
410 tight_bounds_tighter_than loose_bounds_tighter_than felem_size_ok.
411 cbv [spec_of_BinOp bin_sub]. rewrite sub_func_eq. intros.
412 pose proof sub_correct
413 _ _ _ _ _ ltac:(eassumption) _ (res_eq sub_op)
414 as Hcorrect.
415 eapply list_binop_correct with (res:=res sub_op); [ .. | eassumption ];
416 handle_side_conditions; [ | | loosen_bounds | bounds_length ].
417 { (* output *value* is correct *)
418 intros.
419 specialize_correctness_hyp Hcorrect.
420 destruct Hcorrect. simpl_map_unsigned.
421 rewrite <-F.of_Z_sub. FtoZ. congruence. }
422 { (* output *bounds* are correct *)
423 intros. apply Hcorrect; auto. }
424 Qed.
425
426 Lemma carry_sub_func_correct :
427 valid_func (res carry_sub_op _) ->
428 forall functions, Interface.map.get functions Field.carry_sub = Some carry_sub_func ->
429 spec_of_BinOp bin_carry_sub functions.
430 Proof using M_eq check_args_ok carry_sub_func_eq ok
431 tight_bounds_tighter_than felem_size_ok.
432 cbv [spec_of_BinOp bin_carry_sub]. rewrite carry_sub_func_eq. intros.
433 pose proof carry_sub_correct
434 _ _ _ _ _ ltac:(eassumption) _ (res_eq carry_sub_op)
435 as Hcorrect.
436 eapply list_binop_correct with (res:=res carry_sub_op); [ .. | eassumption ];
437 handle_side_conditions; [ | | loosen_bounds | bounds_length ].
438 { (* output *value* is correct *)
439 intros.
440 specialize_correctness_hyp Hcorrect.
441 destruct Hcorrect. simpl_map_unsigned.
442 rewrite <-F.of_Z_sub. FtoZ. congruence. }
443 { (* output *bounds* are correct *)
444 intros. apply Hcorrect; auto. }
445 Qed.
446
447 Lemma opp_func_correct :
448 valid_func (res opp_op _) ->
449 forall functions, Interface.map.get functions Field.opp = Some opp_func ->
450 spec_of_UnOp un_opp functions.
451 Proof using M_eq check_args_ok loose_bounds_tighter_than opp_func_eq ok felem_size_ok.
452 cbv [spec_of_UnOp un_opp]. rewrite opp_func_eq. intros.
453 pose proof opp_correct
454 _ _ _ _ _ ltac:(eassumption) _ (res_eq opp_op)
455 as Hcorrect.
456
457 eapply list_unop_correct with (res:=res opp_op); [ .. | eassumption ];
458 handle_side_conditions; [ | | loosen_bounds | bounds_length ].
459 { (* output *value* is correct *)
460 intros. specialize_correctness_hyp Hcorrect.
461 destruct Hcorrect. simpl_map_unsigned.
462 FtoZ. rewrite Z.sub_0_l; congruence. }
463 { (* output *bounds* are correct *)
464 intros. apply Hcorrect; auto. }
465 Qed.
466
467 Lemma scmula24_func_correct :
468 valid_func (res scmula24_op _) ->
469 forall functions, Interface.map.get functions scmula24 = Some scmula24_func ->
470 spec_of_UnOp un_scmula24 functions.
471 Proof using M_eq check_args_ok ok scmula24_func_eq
472 tight_bounds_tighter_than felem_size_ok.
473 cbv [spec_of_UnOp un_scmula24]. rewrite scmula24_func_eq. intros.
474 pose proof carry_scmul_const_correct
475 _ _ _ _ _ (ltac:(eassumption)) (F.to_Z a24) _
476 (res_eq scmula24_op)
477 as Hcorrect.
478
479 eapply list_unop_correct with (res:=res scmula24_op); [ .. | eassumption ];
480 handle_side_conditions; [ | | loosen_bounds | bounds_length ].
481 { (* output *value* is correct *)
482 intros. specialize_correctness_hyp Hcorrect.
483 destruct Hcorrect. simpl_map_unsigned.
484 FtoZ. congruence. }
485 { (* output *bounds* are correct *)
486 intros. apply Hcorrect; auto. }
487 Qed.
488
489 Lemma list_Z_bounded_by_unsigned (xs : list (@Interface.word.rep _ word)) :
490 list_Z_bounded_by
491 (Primitives.saturated_bounds (List.length xs) width)
492 (map Interface.word.unsigned xs).
493 Proof using parameters_sentinel ok.
494 induction xs; cbn; [reflexivity|].
495 eapply list_Z_bounded_by_cons; split; [|assumption].
496 eapply Bool.andb_true_iff; split; eapply Z.leb_le;
497 cbv [Primitives.word_bound]; cbn.
498 { eapply Properties.word.unsigned_range. }
499 { eapply Le.Z.le_sub_1_iff, Properties.word.unsigned_range. }
500 Qed.
501
502 Lemma felem_copy_func_correct :
503 valid_func (res felem_copy_op _) ->
504 forall functions, Interface.map.get functions felem_copy = Some felem_copy_func ->
505 spec_of_felem_copy functions.
506 Proof using M_eq check_args_ok ok felem_copy_func_eq
507 tight_bounds_tighter_than loose_bounds_tighter_than
508 parameters_sentinel felem_size_ok.
509 cbv [spec_of_felem_copy]. rewrite felem_copy_func_eq. intros.
510 pose proof copy_correct _ _ _ _ _ ltac:(eassumption) _ (res_eq felem_copy_op)
511 as Hcorrect.
512
513 eapply felem_copy_correct; [ .. | eassumption | eassumption ];
514 repeat handle_side_conditions; [ | ]; intros.
515 { (* output *value* is correct *)
516 unshelve erewrite (proj1 (Hcorrect _ _)); cycle 1.
517 { rewrite map_map, List.map_ext_id; trivial; intros.
518 rewrite ?Word.Interface.word.of_Z_unsigned; trivial. }
519 { rewrite <- H2. exact (list_Z_bounded_by_unsigned x0). } }
520 { (* output *bounds* are correct *)
521 intros. apply Hcorrect; auto. }
522 Qed.
523
524 Lemma from_word_func_correct :
525 valid_func (res from_word_op _) ->
526 forall functions, Interface.map.get functions from_word = Some from_word_func ->
527 spec_of_from_word functions.
528 Proof using M_eq check_args_ok from_word_func_eq ok
529 tight_bounds_tighter_than felem_size_ok.
530 cbv [spec_of_from_word]. rewrite from_word_func_eq. intros.
531 epose proof encode_word_correct _ _ _ _ _ ltac:(eassumption) _ (res_eq from_word_op)
532 as Hcorrect.
533 cbv [expr.Interp] in *; autounfold with solinas_specs in *; cbn [ZRange.lower ZRange.upper] in *.
534
535 eapply from_word_correct; [ .. | eassumption | eassumption ];
536 repeat handle_side_conditions.
537 { (* value *)
538 intros.
539 destruct (Hcorrect (Interface.word.unsigned w)); clear Hcorrect.
540 { pose proof Properties.word.unsigned_range w.
541 eapply Bool.andb_true_iff; split; eapply Zle_is_le_bool; Lia.lia. }
542 rewrite <- M_eq in *; cbv [M] in *; eapply F.eq_of_Z_iff in H2.
543 rewrite <-H2.
544 unfold feval.
545 unfold Signature.field_representation.
546 unfold Representation.eval_words, Representation.frep.
547 cbv [Representation.eval_words].
548 Morphisms.f_equiv. Morphisms.f_equiv.
549 rewrite map_unsigned_of_Z, List.map_ext_id; trivial.
550 intros y Hy.
551 eapply relax_list_Z_bounded_by, MaxBounds.max_bounds_range_iff in H3;
552 try eapply tight_bounds_tighter_than; destruct H3.
553 eapply List.Forall_In in H4; eauto.
554 rewrite MakeAccessSizes.bits_per_word_eq_width in H4.
555 unfold Interface.word.wrap; rewrite Z.mod_small; trivial. }
556 { intros.
557 destruct (Hcorrect (Interface.word.unsigned w)); clear Hcorrect.
558 { pose proof Properties.word.unsigned_range w.
559 eapply Bool.andb_true_iff; split; eapply Zle_is_le_bool; Lia.lia. }
560 rewrite <- M_eq in *; cbv [M] in *; eapply F.eq_of_Z_iff in H2.
561 trivial. }
562 { eauto using relax_list_Z_bounded_by, tight_bounds_tighter_than. }
563 Qed.
564
565 Lemma from_bytes_func_correct :
566 valid_func (res from_bytes_op _) ->
567 forall functions,
568 Interface.map.get functions Field.from_bytes = Some from_bytes_func ->
569 spec_of_from_bytes functions.
570 Proof using M_eq check_args_ok from_bytes_func_eq ok
571 tight_bounds_tighter_than felem_size_ok.
572 cbv [spec_of_from_bytes]. rewrite from_bytes_func_eq. intros.
573 pose proof UnsaturatedSolinas.from_bytes_correct
574 _ _ _ _ _ ltac:(eassumption) _ (res_eq from_bytes_op) (eq_refl true)
575 as Hcorrect.
576
577 eapply Signature.from_bytes_correct with (res:=res from_bytes_op);
578 [ .. | eassumption | eassumption ];
579 handle_side_conditions; [ loosen_bounds | bounds_length | | | ].
580 { intros. erewrite length_list_Z_bounded_by, length_byte_bounds; trivial. }
581 { (* output *value* is correct *)
582 intros. specialize_correctness_hyp Hcorrect.
583 destruct Hcorrect.
584 FtoZ. simpl_map_unsigned. congruence. }
585 { (* output *bounds* are correct *)
586 intros. apply Hcorrect; auto. }
587 Qed.
588
589 Lemma to_bytes_func_correct :
590 valid_func (res to_bytes_op _) ->
591 forall functions, Interface.map.get functions Field.to_bytes = Some to_bytes_func ->
592 spec_of_to_bytes functions.
593 Proof using M_eq check_args_ok ok to_bytes_func_eq.
594 cbv [spec_of_to_bytes]. rewrite to_bytes_func_eq. intros.
595 pose proof UnsaturatedSolinas.to_bytes_correct
596 _ _ _ _ _ ltac:(eassumption) _ (res_eq to_bytes_op) (eq_refl true)
597 as Hcorrect.
598
599 eapply Signature.to_bytes_correct with (res:=res to_bytes_op);
600 [ .. | eassumption | eassumption ];
601 handle_side_conditions; [ | | | ].
602 {
603 intros. eapply relax_list_Z_bounded_by; [| eauto]. apply byte_bounds_tighter_than.
604 }
605 {
606 intros. erewrite length_list_Z_bounded_by; [| eauto]. apply length_byte_bounds.
607 }
608 { (* output *value* is correct *)
609 intros. specialize_correctness_hyp Hcorrect.
610 rewrite Hcorrect.
611 rewrite F.to_Z_of_Z, <-M_eq.
612 reflexivity. }
613 { (* output *bounds* are correct *)
614 intros. rewrite Hcorrect by auto.
615 cbn [bytes_in_bounds Representation.frep
616 Signature.field_representation].
617 erewrite ByteBounds.byte_map_unsigned_of_Z,
618 ByteBounds.map_byte_wrap_bounded
619 by apply ByteBounds.partition_bounded_by.
620 apply partition_bounded_by_prime_bytes_bounds.
621 apply Z.mod_pos_bound.
622 apply modulus_fits_in_bytes. }
623 Qed.
624
625 End UnsaturatedSolinas.
626
627 (* Prototyping full pipeline: *)
628
629 From Coq Require Import String.
630 Require Import Crypto.Bedrock.Field.Translation.Proofs.ValidComputable.Func.
631
632 (* TODO: move somewhere common *)
633 Definition field_parameters_prefixed
634 M_pos a24 (prefix: string) : FieldParameters :=
635 Build_FieldParameters
636 M_pos a24
637 (prefix ++ "mul")
638 (prefix ++ "add")
639 (prefix ++ "carry_add")
640 (prefix ++ "sub")
641 (prefix ++ "carry_sub")
642 (prefix ++ "opp")
643 (prefix ++ "square")
644 (prefix ++ "scmula24")
645 (prefix ++ "inv")
646 (prefix ++ "from_bytes")
647 (prefix ++ "to_bytes")
648 (prefix ++ "select_znz")
649 (prefix ++ "copy")
650 (prefix ++ "from_word")
651 .
652
653 Import Coq.Program.Tactics.
654 Local Ltac begin_derive_bedrock2_func :=
655 lazymatch goal with
656 | |- context [spec_of_BinOp bin_mul] => rapply mul_func_correct
657 | |- context [spec_of_UnOp un_square] => rapply square_func_correct
658 | |- context [spec_of_BinOp bin_add] => rapply add_func_correct
659 | |- context [spec_of_BinOp bin_carry_add] => rapply carry_add_func_correct
660 | |- context [spec_of_BinOp bin_sub] => rapply sub_func_correct
661 | |- context [spec_of_BinOp bin_carry_sub] => rapply carry_sub_func_correct
662 | |- context [spec_of_UnOp un_opp] => rapply opp_func_correct
663 | |- context [spec_of_UnOp un_scmula24] => rapply scmula24_func_correct
664 | |- context [spec_of_from_bytes] => rapply from_bytes_func_correct
665 | |- context [spec_of_to_bytes] => rapply to_bytes_func_correct
666 | |- context [spec_of_felem_copy] => rapply felem_copy_func_correct
667 | |- context [spec_of_from_word] => rapply from_word_func_correct
668 end.
669
670 Ltac epair :=
671 lazymatch goal with
672 | f := _ : string * Syntax.func |- _ =>
673 let p := open_constr:((_, _)) in
674 unify f p;
675 subst f
676 | f := _ : Syntax.func |- _ => idtac
677 end.
678
679 Ltac derive_bedrock2_func op :=
680 epair;
681 begin_derive_bedrock2_func;
682 (* this goal fills in the evar, so do it first for [abstract] to be happy *)
683 try lazymatch goal with
684 | |- _ = b2_func _ => vm_compute; reflexivity
685 end;
686 (* solve all the remaining goals *)
687 lazymatch goal with
688 | |- _ = @ErrorT.Success ?ErrT unit tt =>
689 abstract (vm_cast_no_check (@eq_refl _ (@ErrorT.Success ErrT unit tt)))
690 | |- list_Z_tighter_than _ _ =>
691 abstract (vm_cast_no_check (@eq_refl bool true))
692 | |- valid_func _ =>
693 eapply valid_func_bool_iff;
694 abstract vm_cast_no_check (eq_refl true)
695 | |- _ = m _ _ => vm_compute; reflexivity
696 | |- _ = default_varname_gen => vm_compute; reflexivity
697 | |- (felem_size_in_bytes <= 2 ^ _)%Z => apply felem_size_ok
698 end.
699
700 (*
701 Require Import Crypto.Bedrock.Field.Translation.Parameters.Defaults64.
702
703 Section Tests.
704 Definition n := 5%nat.
705 Definition s := (2^255)%Z.
706 Definition c := [(1, 19)]%Z.
707
708 Existing Instances Defaults64.default_parameters default_parameters_ok.
709 Definition prefix : string := "fe25519_"%string.
710
711 Instance field_parameters : FieldParameters.
712 Proof using Type.
713 let M := (eval vm_compute in (Z.to_pos (m s c))) in
714 (* Curve25519 "A" parameter (see section 4.1 of RFC 7748) *)
715 let a := constr:(F.of_Z M 486662) in
716 let prefix := constr:("fe25519_"%string) in
717 eapply
718 (field_parameters_prefixed
719 M ((a - F.of_Z _ 2) / F.of_Z _ 4)%F prefix).
720 Defined.
721
722 Instance fe25519_ops : unsaturated_solinas_ops n s c.
723 Proof using Type. Time constructor; make_computed_op. Defined.
724
725 Local Notation functions_contain functions f :=
726 (Interface.map.get functions (fst f) = Some (snd f)).
727
728 Derive fe25519_mul
729 SuchThat (forall functions,
730 functions_contain functions fe25519_mul ->
731 spec_of_BinOp bin_mul
732 (field_representation:=field_representation n s c)
733 functions)
734 As fe25519_mul_correct.
735 Proof. Time derive_bedrock2_func mul_op. Qed.
736
737 Derive fe25519_square
738 SuchThat (forall functions,
739 functions_contain functions fe25519_square ->
740 spec_of_UnOp un_square
741 (field_representation:=field_representation n s c)
742 functions)
743 As fe25519_square_correct.
744 Proof. Time derive_bedrock2_func square_op. Qed.
745
746 Derive fe25519_add
747 SuchThat (forall functions,
748 functions_contain functions fe25519_add ->
749 spec_of_BinOp bin_add
750 (field_representation:=field_representation n s c)
751 functions)
752 As fe25519_add_correct.
753 Proof. Time derive_bedrock2_func add_op. Qed.
754
755 Derive fe25519_carry_add
756 SuchThat (forall functions,
757 functions_contain functions fe25519_carry_add ->
758 spec_of_BinOp bin_carry_add
759 (field_representation:=field_representation n s c)
760 functions)
761 As fe25519_carry_add_correct.
762 Proof. Time derive_bedrock2_func carry_add_op. Qed.
763
764 Derive fe25519_sub
765 SuchThat (forall functions,
766 functions_contain functions fe25519_sub ->
767 spec_of_BinOp bin_sub
768 (field_representation:=field_representation n s c)
769 functions)
770 As fe25519_sub_correct.
771 Proof. Time derive_bedrock2_func sub_op. Qed.
772
773 Derive fe25519_carry_sub
774 SuchThat (forall functions,
775 functions_contain functions fe25519_carry_sub ->
776 spec_of_BinOp bin_carry_sub
777 (field_representation:=field_representation n s c)
778 functions)
779 As fe25519_carry_sub_correct.
780 Proof. Time derive_bedrock2_func carry_sub_op. Qed.
781
782 Derive fe25519_opp
783 SuchThat (forall functions,
784 functions_contain functions fe25519_opp ->
785 spec_of_UnOp un_opp
786 (field_representation:=field_representation n s c)
787 functions)
788 As fe25519_opp_correct.
789 Proof. Time derive_bedrock2_func opp_op. Qed.
790
791 Derive fe25519_scmula24
792 SuchThat (forall functions,
793 functions_contain functions fe25519_scmula24 ->
794 spec_of_UnOp un_scmula24
795 (field_representation:=field_representation n s c)
796 functions)
797 As fe25519_scmula24_correct.
798 Proof. Time derive_bedrock2_func scmula24_op. Qed.
799
800 Derive fe25519_from_bytes
801 SuchThat (forall functions,
802 functions_contain functions fe25519_from_bytes ->
803 spec_of_from_bytes
804 (field_representation:=field_representation n s c)
805 functions)
806 As fe25519_from_bytes_correct.
807 Proof. Time derive_bedrock2_func from_bytes_op. Qed.
808
809 Derive fe25519_to_bytes
810 SuchThat (forall functions,
811 functions_contain functions fe25519_to_bytes ->
812 spec_of_to_bytes
813 (field_representation:=field_representation n s c)
814 functions)
815 As fe25519_to_bytes_correct.
816 Proof. Time derive_bedrock2_func to_bytes_op. Qed.
817 End Tests.
818
819 (* Uncomment print statements below to see generated bedrock2 *)
820 (*
821 Print fe25519_mul.
822 Print fe25519_square.
823 Print fe25519_add.
824 Print fe25519_carry_add.
825 Print fe25519_sub.
826 Print fe25519_carry_sub.
827 Print fe25519_opp.
828 Print fe25519_scmula24.
829 Print fe25519_from_bytes.
830 Print fe25519_to_bytes.
831 *)
832 *)