| 1 |
Require Rupicola.Lib.Tactics. |
| 2 |
From Coq Require Import Lia. |
| 3 |
From Coq Require Import String. |
| 4 |
From Coq Require Import List. (* after strings *) |
| 5 |
From Coq Require Import QArith. |
| 6 |
From Coq Require Import ZArith. |
| 7 |
Require Import bedrock2.ArrayCasts. |
| 8 |
Require Import bedrock2.Map.Separation. |
| 9 |
Require Import bedrock2.Map.SeparationLogic. |
| 10 |
Require Import bedrock2.ProgramLogic. |
| 11 |
Require Import bedrock2.Semantics. |
| 12 |
Require Import bedrock2.SepAutoArray. |
| 13 |
Require Import bedrock2.Syntax. |
| 14 |
Require Import bedrock2.WeakestPreconditionProperties. |
| 15 |
Require Import coqutil.Byte. |
| 16 |
Require Import coqutil.Map.Interface. |
| 17 |
Require Import coqutil.Tactics.Tactics. |
| 18 |
Require Import coqutil.Word.Interface. |
| 19 |
Require Import Crypto.Arithmetic.Core. |
| 20 |
Require Import Crypto.Arithmetic.Partition. |
| 21 |
Require Import Crypto.Arithmetic.PrimeFieldTheorems. |
| 22 |
Require Import Crypto.Bedrock.Field.Common.Arrays.MaxBounds. |
| 23 |
Require Import Crypto.Bedrock.Field.Common.Names.MakeNames. |
| 24 |
Require Import Crypto.Bedrock.Field.Common.Names.VarnameGenerator. |
| 25 |
Require Import Crypto.Bedrock.Field.Common.Tactics. |
| 26 |
Require Import Crypto.Bedrock.Field.Common.Types. |
| 27 |
Require Import Crypto.Bedrock.Field.Translation.Func. |
| 28 |
Require Import Crypto.Bedrock.Field.Translation.Proofs.Func. |
| 29 |
Require Import Crypto.Bedrock.Field.Interface.Representation. |
| 30 |
Require Import Crypto.Bedrock.Specs.Field. |
| 31 |
Require Import Crypto.COperationSpecifications. |
| 32 |
Require Import Crypto.Language.API. |
| 33 |
Require Import Crypto.Spec.ModularArithmetic. |
| 34 |
Import Language.API.Compilers. |
| 35 |
Import ListNotations. |
| 36 |
Import Types.Notations. |
| 37 |
Import Syntax.Coercions. |
| 38 |
Local Open Scope Z_scope. |
| 39 |
|
| 40 |
Section Generic. |
| 41 |
Context |
| 42 |
{width BW word mem locals ext_spec varname_gen error} |
| 43 |
`{parameters_sentinel : @parameters width BW word mem locals ext_spec varname_gen error}. |
| 44 |
Definition make_bedrock_func {t} insizes outsizes inlengths (res : API.Expr t) |
| 45 |
: func := |
| 46 |
let innames := make_innames (inname_gen:=default_inname_gen) _ in |
| 47 |
let outnames := make_outnames (outname_gen:=default_outname_gen) _ in |
| 48 |
fst (translate_func res innames inlengths insizes outnames outsizes). |
| 49 |
End Generic. |
| 50 |
|
| 51 |
Local Hint Unfold fst snd : pairs. |
| 52 |
Local Hint Unfold type.final_codomain : types. |
| 53 |
Local Hint Unfold Equivalence.equivalent_args |
| 54 |
Equivalence.equivalent_base rep.equiv |
| 55 |
rep.listZ_mem rep.Z type.map_for_each_lhs_of_arrow |
| 56 |
rtype_of_ltype base_rtype_of_ltype rep.rtype_of_ltype |
| 57 |
WeakestPrecondition.dexpr WeakestPrecondition.expr |
| 58 |
WeakestPrecondition.expr_body |
| 59 |
: equivalence. |
| 60 |
Local Hint Unfold LoadStoreList.list_lengths_from_args |
| 61 |
LoadStoreList.list_lengths_from_value |
| 62 |
: list_lengths. |
| 63 |
Local Hint Unfold LoadStoreList.access_sizes_good_args |
| 64 |
LoadStoreList.access_sizes_good |
| 65 |
LoadStoreList.base_access_sizes_good |
| 66 |
LoadStoreList.within_access_sizes_args |
| 67 |
LoadStoreList.within_base_access_sizes |
| 68 |
: access_sizes. |
| 69 |
Local Hint Unfold LoadStoreList.lists_reserved_with_initial_context |
| 70 |
LoadStoreList.lists_reserved |
| 71 |
LoadStoreList.extract_listnames |
| 72 |
Flatten.flatten_listonly_base_ltype |
| 73 |
Flatten.flatten_argnames |
| 74 |
Flatten.flatten_base_ltype |
| 75 |
List.app map.of_list_zip |
| 76 |
map.putmany_of_list_zip |
| 77 |
: lists_reserved. |
| 78 |
|
| 79 |
Local Hint Resolve MakeAccessSizes.bits_per_word_le_width |
| 80 |
MakeAccessSizes.width_ge_8 width_0mod_8 |
| 81 |
Util.Forall_map_byte_unsigned |
| 82 |
: translate_func_preconditions. |
| 83 |
|
| 84 |
Section WithParameters. |
| 85 |
Context |
| 86 |
{width BW word mem locals ext_spec varname_gen error} |
| 87 |
`{parameters_sentinel : @parameters width BW word mem locals ext_spec varname_gen error}. |
| 88 |
Context {ok : Types.ok} |
| 89 |
{field_parameters : FieldParameters}. |
| 90 |
Context (n n_bytes : nat) (weight : nat -> Z) |
| 91 |
(bounds : Type) |
| 92 |
(loose_bounds tight_bounds byte_bounds : bounds) |
| 93 |
(list_in_bounds : bounds -> list Z -> Prop) |
| 94 |
(relax_bounds : |
| 95 |
forall X, |
| 96 |
list_in_bounds tight_bounds X -> |
| 97 |
list_in_bounds loose_bounds X) |
| 98 |
(eval_transformation : list Z -> list Z). |
| 99 |
Context (inname_gen_varname_gen_disjoint : |
| 100 |
disjoint default_inname_gen varname_gen) |
| 101 |
(outname_gen_varname_gen_disjoint : |
| 102 |
disjoint default_outname_gen varname_gen). |
| 103 |
Local Instance field_representation : FieldRepresentation |
| 104 |
:= @frep _ BW _ _ field_parameters n n_bytes weight bounds list_in_bounds loose_bounds tight_bounds |
| 105 |
byte_bounds eval_transformation. |
| 106 |
|
| 107 |
Context (felem_size_ok : felem_size_in_bytes <= 2 ^ width). |
| 108 |
Local Instance field_representation_ok : FieldRepresentation_ok |
| 109 |
:= frep_ok n n_bytes weight bounds list_in_bounds loose_bounds tight_bounds byte_bounds |
| 110 |
relax_bounds _ felem_size_ok. |
| 111 |
|
| 112 |
Local Notation felem_size_in_words := (felem_size_in_words(FieldRepresentation:=field_representation)). |
| 113 |
|
| 114 |
Lemma FElem_array_truncated_scalar_iff1 px x : |
| 115 |
Lift1Prop.iff1 |
| 116 |
(FElem px x) |
| 117 |
(sep (map:=mem) |
| 118 |
(emp (map:=mem) (length x = n)) |
| 119 |
(Array.array |
| 120 |
(Scalars.truncated_scalar access_size.word) |
| 121 |
(word.of_Z |
| 122 |
(Z.of_nat (BinIntDef.Z.to_nat (bytes_per_word width)))) |
| 123 |
px (map word.unsigned x))). |
| 124 |
Proof using ok. |
| 125 |
cbv [FElem Bignum.Bignum field_representation frep]. |
| 126 |
rewrite Util.array_truncated_scalar_scalar_iff1. |
| 127 |
Morphisms.f_equiv. Morphisms.f_equiv. Morphisms.f_equiv. |
| 128 |
destruct Bitwidth.width_cases as [W|W]; rewrite W; trivial. |
| 129 |
Qed. |
| 130 |
|
| 131 |
Lemma FElemBytes_array_truncated_scalar_iff1 pbs bs : |
| 132 |
Lift1Prop.iff1 |
| 133 |
(FElemBytes pbs bs) |
| 134 |
(sep (map:=mem) |
| 135 |
(emp (map:=mem) |
| 136 |
(length bs = encoded_felem_size_in_bytes |
| 137 |
/\ bytes_in_bounds bs)) |
| 138 |
(Array.array |
| 139 |
(Scalars.truncated_scalar access_size.one) |
| 140 |
(word.of_Z 1) pbs (map byte.unsigned bs))). |
| 141 |
Proof using ok. |
| 142 |
cbv [FElemBytes]. |
| 143 |
rewrite Util.array_truncated_scalar_ptsto_iff1. |
| 144 |
rewrite ByteBounds.byte_map_of_Z_unsigned. |
| 145 |
reflexivity. |
| 146 |
Qed. |
| 147 |
|
| 148 |
Ltac felem_to_array := |
| 149 |
repeat |
| 150 |
lazymatch goal with |
| 151 |
| H:context[sep (FElem _ _) _] |
| 152 |
|- _ => seprewrite_in FElem_array_truncated_scalar_iff1 H |
| 153 |
| H : context [sep (FElemBytes _ _) _] |- _ => |
| 154 |
seprewrite_in FElemBytes_array_truncated_scalar_iff1 H |
| 155 |
end. |
| 156 |
|
| 157 |
Ltac equivalence_side_conditions_hook := fail. |
| 158 |
Ltac solve_equivalence_side_conditions := |
| 159 |
lazymatch goal with |
| 160 |
| |- map word.unsigned _ = map word.unsigned _ => reflexivity |
| 161 |
| |- word.unsigned _ = word.unsigned _ => reflexivity |
| 162 |
| |- WeakestPrecondition.get _ _ _ => |
| 163 |
repeat (apply Util.get_put_diff; [ congruence | ]); |
| 164 |
apply Util.get_put_same; reflexivity |
| 165 |
| |- Forall (fun z => 0 <= z < 2 ^ (?e * 8)) |
| 166 |
(map word.unsigned _) => |
| 167 |
unshelve eapply Util.Forall_word_unsigned_within_access_size; |
| 168 |
destruct Bitwidth.width_cases as [W|W]; rewrite W; trivial |
| 169 |
| |- Forall (fun z => 0 <= z < ?e) |
| 170 |
(map byte.unsigned _) => |
| 171 |
change e with 256; |
| 172 |
apply Util.Forall_map_byte_unsigned |
| 173 |
| |- sep _ _ _ => |
| 174 |
change (Z.of_nat (bytes_per access_size.one)) with 1; |
| 175 |
try erewrite Util.map_unsigned_of_Z,MaxBounds.map_word_wrap_bounded |
| 176 |
by eauto using byte_unsigned_within_max_bounds; |
| 177 |
felem_to_array; sepsimpl; (assumption || ecancel_assumption) |
| 178 |
| |- map word.unsigned ?x = map byte.unsigned _ => |
| 179 |
is_evar x; |
| 180 |
erewrite Util.map_unsigned_of_Z,MaxBounds.map_word_wrap_bounded |
| 181 |
by (eapply byte_unsigned_within_max_bounds; eauto); |
| 182 |
reflexivity |
| 183 |
| _ => equivalence_side_conditions_hook |
| 184 |
end. |
| 185 |
|
| 186 |
Ltac crush_sep := |
| 187 |
repeat lazymatch goal with |
| 188 |
| |- exists _, _ => eexists |
| 189 |
| |- Lift1Prop.ex1 _ _ => Tactics.lift_eexists |
| 190 |
| |- True => tauto |
| 191 |
| _ => progress sepsimpl; cleanup |
| 192 |
end. |
| 193 |
|
| 194 |
Ltac compute_names := |
| 195 |
repeat lazymatch goal with |
| 196 |
| |- context [@make_innames ?w ?B ?W ?M ?L ?X ?G ?R ?p ?gen ?t] => |
| 197 |
let x := constr:(@make_innames w B W M L X G R p gen t) in |
| 198 |
let y := (eval compute in x) in |
| 199 |
change x with y |
| 200 |
| |- context [@make_outnames ?w ?B ?W ?M ?L ?X ?G ?R ?p ?gen ?t] => |
| 201 |
let x := constr:(@make_outnames w B W M L X G R p gen t) in |
| 202 |
let y := (eval compute in x) in |
| 203 |
change x with y |
| 204 |
end. |
| 205 |
|
| 206 |
Ltac use_translate_func_correct b2_args R_ := |
| 207 |
lazymatch goal with |
| 208 |
| H: map.get ?functions ?name = |
| 209 |
Some (fst (translate_func ?e0 ?argns ?argls ?argszs ?retns ?retszs)) |
| 210 |
|- WeakestPrecondition.call ?functions ?name _ _ ?arg_ptrs _ => |
| 211 |
let out_ptr := (eval compute in (hd (word.of_Z 0) arg_ptrs)) in |
| 212 |
let in_ptrs := (eval compute in (tl arg_ptrs)) in |
| 213 |
eapply (translate_func_correct (parameters_sentinel:=parameters_sentinel)) |
| 214 |
with (out_ptrs:=[out_ptr]) (flat_args:=in_ptrs) |
| 215 |
(e:=e0) (argnames:=argns) (arglengths:=argls) (argsizes:=argszs) |
| 216 |
(retnames:=retns) (retsizes:=retszs) |
| 217 |
(args:=b2_args) (R:=R_); |
| 218 |
[ .. | exact H] |
| 219 |
end. |
| 220 |
|
| 221 |
Ltac types_autounfold := |
| 222 |
repeat first [ progress autounfold with types pairs |
| 223 |
| progress autounfold with equivalence ]. |
| 224 |
Ltac lists_autounfold := |
| 225 |
repeat first [ progress types_autounfold |
| 226 |
| progress autounfold with list_lengths |
| 227 |
| progress autounfold with lists_reserved ]. |
| 228 |
|
| 229 |
Ltac translate_func_precondition_hammer := |
| 230 |
lazymatch goal with |
| 231 |
| |- valid_func _ => assumption |
| 232 |
| |- API.Wf _ => assumption |
| 233 |
| |- @eq (list word.rep) _ _ => reflexivity |
| 234 |
| |- length [?p] = _ => reflexivity |
| 235 |
| |- forall _, ~ VarnameSet.varname_set_args _ _ => |
| 236 |
solve [auto using make_innames_varname_gen_disjoint] |
| 237 |
| |- forall _, ~ VarnameSet.varname_set_base (make_outnames _) |
| 238 |
(varname_gen _) => |
| 239 |
apply make_outnames_varname_gen_disjoint; |
| 240 |
solve [apply outname_gen_varname_gen_disjoint] |
| 241 |
| |- NoDup (Flatten.flatten_argnames (make_innames _)) => |
| 242 |
apply flatten_make_innames_NoDup; |
| 243 |
solve [eapply prefix_name_gen_unique] |
| 244 |
| |- NoDup (Flatten.flatten_base_ltype (make_outnames _)) => |
| 245 |
apply flatten_make_outnames_NoDup; |
| 246 |
solve [eapply prefix_name_gen_unique] |
| 247 |
| |- LoadStoreList.base_access_sizes_good _ => |
| 248 |
autounfold with types access_sizes; cbn; |
| 249 |
destruct Bitwidth.width_cases as [W|W]; rewrite ?W; |
| 250 |
clear; cbn; intuition Lia.lia |
| 251 |
| |- PropSet.disjoint |
| 252 |
(VarnameSet.varname_set_args (make_innames _)) |
| 253 |
(VarnameSet.varname_set_base (make_outnames _)) => |
| 254 |
apply make_innames_make_outnames_disjoint; |
| 255 |
eauto using outname_gen_inname_gen_disjoint; |
| 256 |
solve [apply prefix_name_gen_unique] |
| 257 |
| |- Equivalence.equivalent_flat_args _ _ _ _ => |
| 258 |
eapply (equivalent_flat_args_iff1 |
| 259 |
(make_innames (inname_gen:=default_inname_gen) _) |
| 260 |
_ _ _ |
| 261 |
map.empty); |
| 262 |
[ apply flatten_make_innames_NoDup; |
| 263 |
solve [eapply prefix_name_gen_unique] |
| 264 |
| reflexivity | ]; |
| 265 |
compute_names; autounfold with equivalence pairs; |
| 266 |
cbv [Equivalence.equivalent_base]; |
| 267 |
autounfold with equivalence pairs; |
| 268 |
rewrite <-?MakeAccessSizes.bytes_per_word_eq; |
| 269 |
sepsimpl; crush_sep; solve [solve_equivalence_side_conditions] |
| 270 |
| |- LoadStoreList.within_access_sizes_args _ _ => |
| 271 |
autounfold with types access_sizes; cbn; ssplit; trivial; |
| 272 |
solve_equivalence_side_conditions |
| 273 |
| |- LoadStoreList.within_base_access_sizes _ _ => |
| 274 |
autounfold with types access_sizes; |
| 275 |
first [ eapply MaxBounds.max_bounds_range_iff |
| 276 |
| eapply ByteBounds.byte_bounds_range_iff ]; |
| 277 |
cbn [type.app_curried fst snd]; |
| 278 |
solve [eauto using relax_list_Z_bounded_by] |
| 279 |
| |- LoadStoreList.access_sizes_good_args _ => |
| 280 |
autounfold with access_sizes pairs access_sizes; cbn; |
| 281 |
destruct Bitwidth.width_cases as [W|W]; rewrite ?W; |
| 282 |
clear; cbn; intuition Lia.lia |
| 283 |
| |- _ = LoadStoreList.list_lengths_from_args _ => |
| 284 |
autounfold with list_lengths pairs list_lengths; |
| 285 |
felem_to_array; sepsimpl; rewrite !map_length; |
| 286 |
repeat match goal with |
| 287 |
| H : length _ = _ |- _ => rewrite H end; |
| 288 |
reflexivity |
| 289 |
| _ => idtac |
| 290 |
end. |
| 291 |
|
| 292 |
Ltac lists_reserved_simplify pout := |
| 293 |
compute_names; cbn [type.app_curried fst snd]; |
| 294 |
autounfold with types list_lengths pairs; |
| 295 |
lists_autounfold; sepsimpl; |
| 296 |
match goal with |
| 297 |
| H : context [FElem pout ?old_out] |
| 298 |
|- @Lift1Prop.ex1 (list Z) _ _ _ => |
| 299 |
exists (map word.unsigned old_out) |
| 300 |
| H : context [Placeholder pout ?old_out ] |
| 301 |
|- @Lift1Prop.ex1 (list Z) _ _ _ => |
| 302 |
seprewrite_in Placeholder_iff_FElem_bytes H; extract_ex1_and_emp_in_hyps; |
| 303 |
exists (map word.unsigned (bs2ws (word:=word) (Z.to_nat (bytes_per_word width)) old_out)) |
| 304 |
end; |
| 305 |
crush_sep. |
| 306 |
|
| 307 |
Ltac postcondition_simplify := |
| 308 |
lists_autounfold; |
| 309 |
cbn [type.app_curried fst snd]; |
| 310 |
cbv [Equivalence.equivalent_listexcl_flat_base |
| 311 |
Equivalence.equivalent_listonly_flat_base |
| 312 |
Equivalence.equivalent_flat_base |
| 313 |
]; lists_autounfold; cbn [hd]; |
| 314 |
repeat intro; |
| 315 |
cbv [Core.postcondition_func_norets |
| 316 |
Core.postcondition_func]; |
| 317 |
sepsimpl; [ assumption .. | ]; |
| 318 |
repeat match goal with |
| 319 |
| _ => progress subst |
| 320 |
| H : WeakestPrecondition.literal (word.unsigned _) _ |- _ => |
| 321 |
cbv [WeakestPrecondition.literal dlet.dlet] in H; |
| 322 |
rewrite word.of_Z_unsigned in H |
| 323 |
| H : word.unsigned _ = word.unsigned _ |- _ => |
| 324 |
apply Properties.word.unsigned_inj in H |
| 325 |
| |- exists _, _ => eexists |
| 326 |
| |- _ /\ _ => eexists |
| 327 |
end. |
| 328 |
|
| 329 |
Ltac solve_length x Hlength := rewrite map_length; |
| 330 |
match goal with |
| 331 |
| H : (FElem _ x * _)%sep _ |- _ => cbv [FElem Bignum.Bignum] in H; sepsimpl_hyps |
| 332 |
| _ => idtac |
| 333 |
end; |
| 334 |
match goal with |
| 335 |
| H : Datatypes.length x = _ |- _ => rewrite H |
| 336 |
| _ => idtac |
| 337 |
end; rewrite Hlength; auto. |
| 338 |
|
| 339 |
Section ListBinop. |
| 340 |
Context {res : API.Expr (type_listZ -> type_listZ -> type_listZ)} |
| 341 |
(res_valid : |
| 342 |
valid_func (res (fun _ : API.type => unit))) |
| 343 |
(res_Wf : API.Wf res). |
| 344 |
Context name (bop : BinOp name) |
| 345 |
(res_eq : forall x y : list word, |
| 346 |
bounded_by bin_xbounds x -> |
| 347 |
bounded_by bin_ybounds y -> |
| 348 |
feval (map word.of_Z |
| 349 |
(API.interp (res _) |
| 350 |
(map word.unsigned x) |
| 351 |
(map word.unsigned y))) |
| 352 |
= bin_model (feval x) (feval y)) |
| 353 |
(res_bounds : forall x y, |
| 354 |
list_in_bounds bin_xbounds x -> |
| 355 |
list_in_bounds bin_ybounds y -> |
| 356 |
list_in_bounds bin_outbounds (API.interp (res _) x y)) |
| 357 |
|
| 358 |
(outbounds_tighter_than_max : forall x, list_in_bounds bin_outbounds x -> list_Z_bounded_by ((@max_bounds width) n) x) |
| 359 |
(xbounds_length : forall x, list_in_bounds bin_xbounds x -> length x = n) |
| 360 |
(ybounds_length : forall x, list_in_bounds bin_ybounds x -> length x = n) |
| 361 |
(outbounds_length : forall x, list_in_bounds bin_outbounds x -> length x = n). |
| 362 |
|
| 363 |
Local Ltac equivalence_side_conditions_hook ::= |
| 364 |
lazymatch goal with |
| 365 |
| |- context [length (API.interp (res _) ?x ?y)] => |
| 366 |
specialize (res_bounds x y ltac:(auto) ltac:(auto)); |
| 367 |
rewrite (length_list_Z_bounded_by _ _ res_bounds); |
| 368 |
try congruence; |
| 369 |
rewrite !map_length, outbounds_length; |
| 370 |
felem_to_array; sepsimpl; congruence |
| 371 |
| _ => idtac |
| 372 |
end. |
| 373 |
|
| 374 |
Local Notation t := |
| 375 |
(type.arrow type_listZ (type.arrow type_listZ type_listZ)) |
| 376 |
(only parsing). |
| 377 |
|
| 378 |
Definition list_binop_insizes |
| 379 |
: type.for_each_lhs_of_arrow access_sizes t := |
| 380 |
(access_size.word, (access_size.word, tt)). |
| 381 |
Definition list_binop_outsizes |
| 382 |
: base_access_sizes (type.final_codomain t) := |
| 383 |
access_size.word. |
| 384 |
Definition list_binop_inlengths |
| 385 |
: type.for_each_lhs_of_arrow list_lengths t := |
| 386 |
(n, (n, tt)). |
| 387 |
Let insizes := list_binop_insizes. |
| 388 |
Let outsizes := list_binop_outsizes. |
| 389 |
Let inlengths := list_binop_inlengths. |
| 390 |
|
| 391 |
Lemma list_binop_correct f : |
| 392 |
f = make_bedrock_func insizes outsizes inlengths res -> |
| 393 |
forall functions, map.get functions name = Some f -> (binop_spec _ functions). |
| 394 |
Proof. |
| 395 |
subst inlengths insizes outsizes. |
| 396 |
cbv [binop_spec list_binop_insizes list_binop_outsizes list_binop_inlengths]. |
| 397 |
cbv beta. intros. subst f. cbv [make_bedrock_func] in *. |
| 398 |
cleanup. eapply Proper_call. |
| 399 |
2: { |
| 400 |
use_translate_func_correct |
| 401 |
constr:((map word.unsigned x, (map word.unsigned y, tt))) Rr; |
| 402 |
translate_func_precondition_hammer. |
| 403 |
{ |
| 404 |
(* lists_reserved_with_initial_context *) |
| 405 |
lists_reserved_simplify pout; try solve_equivalence_side_conditions; |
| 406 |
try solve_length (bs2ws (word:=word) (Z.to_nat (bytes_per_word width)) out) outbounds_length. |
| 407 |
} } |
| 408 |
{ postcondition_simplify; [ | | ]; cycle -1. |
| 409 |
{ refine (proj1 (Proper_sep_iff1 _ _ _ _ _ _ _) _); |
| 410 |
[symmetry; eapply FElem_array_truncated_scalar_iff1 | reflexivity | sepsimpl ]. |
| 411 |
2:eassumption. |
| 412 |
erewrite <-map_length. |
| 413 |
match goal with H : map word.unsigned _ = API.interp (res _) _ _ |- _ => |
| 414 |
rewrite H end. |
| 415 |
erewrite length_list_Z_bounded_by; eauto using res_bounds; apply repeat_length. } |
| 416 |
{ (* output correctness *) |
| 417 |
erewrite <-res_eq; auto. |
| 418 |
match goal with H : map word.unsigned _ = API.interp (res _) _ _ |- _ => |
| 419 |
rewrite <-H end. |
| 420 |
rewrite map_map. |
| 421 |
f_equal; eapply List.nth_error_ext; intros i; rewrite ListUtil.nth_error_map. |
| 422 |
case (nth_error _ i); cbn; trivial; []; intros; eapply f_equal. |
| 423 |
symmetry; eapply word.of_Z_unsigned. } |
| 424 |
{ (* output bounds *) |
| 425 |
cbn [bounded_by field_representation frep] in *. |
| 426 |
match goal with H : map word.unsigned _ = API.interp (res _) _ _ |- _ => |
| 427 |
rewrite H end. |
| 428 |
eauto using res_bounds. } } |
| 429 |
Qed. |
| 430 |
End ListBinop. |
| 431 |
|
| 432 |
Section ListUnop. |
| 433 |
Context {res : API.Expr (type_listZ -> type_listZ)} |
| 434 |
(res_valid : |
| 435 |
valid_func (res (fun _ : API.type => unit))) |
| 436 |
(res_Wf : API.Wf res). |
| 437 |
Context name (uop : UnOp name) |
| 438 |
(res_eq : forall x : list word, |
| 439 |
bounded_by un_xbounds x -> |
| 440 |
feval (map word.of_Z |
| 441 |
(API.interp (res _) (map word.unsigned x))) |
| 442 |
= un_model (feval x)) |
| 443 |
(res_bounds : forall x, |
| 444 |
list_in_bounds un_xbounds x -> |
| 445 |
list_in_bounds un_outbounds (API.interp (res _) x)) |
| 446 |
(outbounds_tighter_than_max : forall x, list_in_bounds un_outbounds x -> list_Z_bounded_by (@max_bounds width n) x) |
| 447 |
(xbounds_length : forall x, list_in_bounds un_xbounds x -> length x = n) |
| 448 |
(outbounds_length : forall x, list_in_bounds un_outbounds x -> length x = n). |
| 449 |
|
| 450 |
Local Ltac equivalence_side_conditions_hook ::= |
| 451 |
lazymatch goal with |
| 452 |
| |- context [length (API.interp (res _) ?x)] => |
| 453 |
specialize (res_bounds x ltac:(auto)); |
| 454 |
rewrite (length_list_Z_bounded_by _ _ res_bounds); |
| 455 |
try congruence; |
| 456 |
rewrite !map_length, outbounds_length; |
| 457 |
felem_to_array; sepsimpl; congruence |
| 458 |
| _ => idtac |
| 459 |
end. |
| 460 |
|
| 461 |
Local Notation t := |
| 462 |
(type.arrow type_listZ type_listZ) (only parsing). |
| 463 |
|
| 464 |
Definition list_unop_insizes |
| 465 |
: type.for_each_lhs_of_arrow access_sizes t := |
| 466 |
(access_size.word, tt). |
| 467 |
Definition list_unop_outsizes |
| 468 |
: base_access_sizes (type.final_codomain t) := |
| 469 |
access_size.word. |
| 470 |
Definition list_unop_inlengths |
| 471 |
: type.for_each_lhs_of_arrow list_lengths t := |
| 472 |
(n, tt). |
| 473 |
Let insizes := list_unop_insizes. |
| 474 |
Let outsizes := list_unop_outsizes. |
| 475 |
Let inlengths := list_unop_inlengths. |
| 476 |
|
| 477 |
Lemma list_unop_correct f : |
| 478 |
f = make_bedrock_func insizes outsizes inlengths res -> |
| 479 |
forall functions, map.get functions name = Some f -> unop_spec _ functions. |
| 480 |
Proof using inname_gen_varname_gen_disjoint outbounds_length |
| 481 |
outbounds_tighter_than_max outname_gen_varname_gen_disjoint |
| 482 |
ok relax_bounds res_Wf res_bounds res_eq res_valid felem_size_ok. |
| 483 |
subst inlengths insizes outsizes. |
| 484 |
cbv [unop_spec list_unop_insizes list_unop_outsizes list_unop_inlengths]. |
| 485 |
cbv beta; intros; subst f. cbv [make_bedrock_func] in *. |
| 486 |
cleanup. eapply Proper_call. |
| 487 |
2: { |
| 488 |
use_translate_func_correct constr:((map word.unsigned x, tt)) Rr. |
| 489 |
all:translate_func_precondition_hammer. |
| 490 |
{ (* lists_reserved_with_initial_context *) |
| 491 |
lists_reserved_simplify pout. |
| 492 |
all: try solve_equivalence_side_conditions. |
| 493 |
solve_length (bs2ws (word:=word) (Z.to_nat (bytes_per_word width)) out) outbounds_length. } } |
| 494 |
{ postcondition_simplify; [ | | ]. |
| 495 |
{ (* output correctness *) |
| 496 |
eapply res_eq; auto. } |
| 497 |
{ (* output bounds *) |
| 498 |
cbn [bounded_by field_representation frep] in *. |
| 499 |
erewrite Util.map_unsigned_of_Z, MaxBounds.map_word_wrap_bounded |
| 500 |
by eauto using relax_list_Z_bounded_by. |
| 501 |
eauto. } |
| 502 |
{ (* separation-logic postcondition *) |
| 503 |
eapply Proper_sep_iff1; |
| 504 |
[ solve [apply FElem_array_truncated_scalar_iff1] |
| 505 |
| reflexivity | ]. |
| 506 |
sepsimpl; [ | ]. |
| 507 |
{ rewrite !map_length. apply outbounds_length; auto. } |
| 508 |
{ erewrite Util.map_unsigned_of_Z, MaxBounds.map_word_wrap_bounded |
| 509 |
by eauto using relax_list_Z_bounded_by. |
| 510 |
rewrite MakeAccessSizes.bytes_per_word_eq. |
| 511 |
clear outbounds_length; subst. |
| 512 |
match goal with |
| 513 |
H : map word.unsigned _ = API.interp (res _) _ |- _ => |
| 514 |
rewrite <-H end. |
| 515 |
auto. } } } |
| 516 |
Qed. |
| 517 |
End ListUnop. |
| 518 |
|
| 519 |
Section FromWord. |
| 520 |
Context {res : API.Expr (type_Z -> type_listZ)} |
| 521 |
(res_valid : |
| 522 |
valid_func (res (fun _ : API.type => unit))) |
| 523 |
(res_Wf : API.Wf res). |
| 524 |
Context (res_eq : forall w:word, |
| 525 |
feval (map word.of_Z |
| 526 |
(API.interp (res _) (word.unsigned w))) |
| 527 |
= F.of_Z _ (word.unsigned w)) |
| 528 |
(res_bounds : forall w:word, |
| 529 |
list_in_bounds |
| 530 |
tight_bounds |
| 531 |
(API.interp (res _) (word.unsigned w))). |
| 532 |
Context (tight_bounds_tighter_than_max : forall x, |
| 533 |
list_in_bounds tight_bounds x -> list_Z_bounded_by (@MaxBounds.max_bounds width n) x). |
| 534 |
|
| 535 |
Local Notation t := |
| 536 |
(type.arrow type_Z type_listZ) (only parsing). |
| 537 |
|
| 538 |
Definition from_word_insizes |
| 539 |
: type.for_each_lhs_of_arrow access_sizes t := |
| 540 |
(tt, tt). |
| 541 |
Definition from_word_outsizes |
| 542 |
: base_access_sizes (type.final_codomain t) := |
| 543 |
access_size.word. |
| 544 |
Definition from_word_inlengths |
| 545 |
: type.for_each_lhs_of_arrow list_lengths t := |
| 546 |
(tt, tt). |
| 547 |
Let insizes := from_word_insizes. |
| 548 |
Let outsizes := from_word_outsizes. |
| 549 |
Let inlengths := from_word_inlengths. |
| 550 |
|
| 551 |
Lemma from_word_correct f : |
| 552 |
f = make_bedrock_func insizes outsizes inlengths res -> |
| 553 |
forall functions, map.get functions from_word = Some f -> |
| 554 |
spec_of_from_word functions. |
| 555 |
Proof using inname_gen_varname_gen_disjoint |
| 556 |
outname_gen_varname_gen_disjoint ok relax_bounds res_Wf |
| 557 |
res_bounds res_eq res_valid tight_bounds_tighter_than_max felem_size_ok. |
| 558 |
subst inlengths insizes outsizes. cbv [spec_of_from_word]. |
| 559 |
cbv [from_word_insizes from_word_outsizes from_word_inlengths]. |
| 560 |
cbv beta; intros; subst f. cbv [make_bedrock_func] in *. |
| 561 |
cleanup. |
| 562 |
eapply Proper_call. |
| 563 |
2:{ |
| 564 |
use_translate_func_correct constr:((word.unsigned x, tt)) R. |
| 565 |
all:try translate_func_precondition_hammer. |
| 566 |
1:reflexivity. |
| 567 |
{ cbv [Equivalence.equivalent_flat_args]; eexists 1%nat; split; [eexists|reflexivity]. |
| 568 |
cbv [Equivalence.equivalent_flat_base rep.equiv rep.Z]; sepsimpl; [reflexivity|eexists]. |
| 569 |
sepsimpl; trivial. |
| 570 |
{ cbn. cbv [WeakestPrecondition.literal dlet.dlet]. rewrite word.of_Z_unsigned; trivial. } |
| 571 |
{ eassumption. } } |
| 572 |
{ (* lists_reserved_with_initial_context *) |
| 573 |
lists_reserved_simplify pout. |
| 574 |
all:try solve_equivalence_side_conditions. |
| 575 |
symmetry. |
| 576 |
erewrite length_list_Z_bounded_by; [| eapply tight_bounds_tighter_than_max, res_bounds]. |
| 577 |
cbv [max_bounds]. rewrite repeat_length. rewrite map_length. |
| 578 |
cbv [FElem Bignum.Bignum] in *. sepsimpl. auto. |
| 579 |
} } |
| 580 |
{ postcondition_simplify; [ | | ]. |
| 581 |
{ (* output correctness *) |
| 582 |
eapply res_eq; auto. } |
| 583 |
{ (* output bounds *) |
| 584 |
cbn [bounded_by field_representation frep] in *. |
| 585 |
erewrite Util.map_unsigned_of_Z, MaxBounds.map_word_wrap_bounded |
| 586 |
by eauto using relax_list_Z_bounded_by. cbv [Field.tight_bounds]. simpl. |
| 587 |
eauto. } |
| 588 |
{ (* separation-logic postcondition *) |
| 589 |
eapply Proper_sep_iff1; |
| 590 |
[ solve [apply FElem_array_truncated_scalar_iff1] |
| 591 |
| reflexivity | ]. |
| 592 |
sepsimpl; [ | ]. |
| 593 |
{ rewrite !map_length. |
| 594 |
erewrite length_list_Z_bounded_by; [| eapply tight_bounds_tighter_than_max, res_bounds]. |
| 595 |
cbv [max_bounds]; rewrite repeat_length; trivial. } |
| 596 |
{ erewrite Util.map_unsigned_of_Z, MaxBounds.map_word_wrap_bounded |
| 597 |
by eauto using relax_list_Z_bounded_by. |
| 598 |
rewrite MakeAccessSizes.bytes_per_word_eq. |
| 599 |
(* clear tight_bounds_length; subst. *) |
| 600 |
match goal with |
| 601 |
H : map word.unsigned _ = API.interp (res _) _ |- _ => |
| 602 |
rewrite <-H end. |
| 603 |
auto. } } } |
| 604 |
Qed. |
| 605 |
End FromWord. |
| 606 |
|
| 607 |
Section FelemCopy. |
| 608 |
Context {res : API.Expr (type_listZ -> type_listZ)} |
| 609 |
(res_valid : |
| 610 |
valid_func (res (fun _ : API.type => unit))) |
| 611 |
(res_Wf : API.Wf res). |
| 612 |
Context (res_eq : forall x : list word, |
| 613 |
length x = n -> |
| 614 |
(map word.of_Z (API.interp (res _) (map word.unsigned x))) |
| 615 |
= x) |
| 616 |
(res_bounds : forall x, |
| 617 |
list_Z_bounded_by (max_bounds (width:=width) n) x -> |
| 618 |
list_Z_bounded_by (max_bounds (width:=width) n) (API.interp (res _) x)). |
| 619 |
|
| 620 |
Local Ltac equivalence_side_conditions_hook ::= |
| 621 |
lazymatch goal with |
| 622 |
| |- context [length (API.interp (res _) ?x)] => |
| 623 |
idtac (*; specialize (res_bounds x ltac:(auto)); |
| 624 |
rewrite (length_list_Z_bounded_by _ _ res_bounds); |
| 625 |
try congruence; |
| 626 |
rewrite !map_length, outbounds_length; |
| 627 |
felem_to_array; sepsimpl; congruence*) |
| 628 |
| _ => idtac |
| 629 |
end. |
| 630 |
|
| 631 |
Local Notation t := |
| 632 |
(type.arrow type_listZ type_listZ) (only parsing). |
| 633 |
|
| 634 |
Definition felem_copy_insizes |
| 635 |
: type.for_each_lhs_of_arrow access_sizes t := |
| 636 |
(access_size.word, tt). |
| 637 |
Definition felem_copy_outsizes |
| 638 |
: base_access_sizes (type.final_codomain t) := |
| 639 |
access_size.word. |
| 640 |
Definition felem_copy_inlengths |
| 641 |
: type.for_each_lhs_of_arrow list_lengths t := |
| 642 |
(n, tt). |
| 643 |
Let insizes := felem_copy_insizes. |
| 644 |
Let outsizes := felem_copy_outsizes. |
| 645 |
Let inlengths := felem_copy_inlengths. |
| 646 |
|
| 647 |
Lemma felem_copy_correct f : |
| 648 |
f = make_bedrock_func insizes outsizes inlengths res -> |
| 649 |
forall functions, map.get functions felem_copy = Some f -> |
| 650 |
spec_of_felem_copy functions. |
| 651 |
Proof. |
| 652 |
subst inlengths insizes outsizes. |
| 653 |
cbv [spec_of_felem_copy felem_copy_insizes felem_copy_outsizes felem_copy_inlengths]. |
| 654 |
cbv beta; intros; subst f. cbv [make_bedrock_func] in *. |
| 655 |
cleanup. eapply Proper_call. |
| 656 |
2: { |
| 657 |
rename R into Rr. |
| 658 |
use_translate_func_correct constr:((map word.unsigned x, tt)) (FElem px x * Rr)%sep. |
| 659 |
all:try translate_func_precondition_hammer. |
| 660 |
{ autounfold with types access_sizes; |
| 661 |
first [ eapply MaxBounds.max_bounds_range_iff |
| 662 |
| eapply ByteBounds.byte_bounds_range_iff ]; |
| 663 |
cbn [type.app_curried fst snd]. |
| 664 |
apply res_bounds. |
| 665 |
rewrite max_bounds_range_iff. |
| 666 |
seprewrite_in @FElem_array_truncated_scalar_iff1 H1; extract_ex1_and_emp_in H1. |
| 667 |
ssplit; rewrite ?map_length; trivial. |
| 668 |
eapply List.Forall_map, Forall_forall; intros. |
| 669 |
rewrite MakeAccessSizes.bits_per_word_eq_width. |
| 670 |
eapply Properties.word.unsigned_range. } |
| 671 |
{ (* lists_reserved_with_initial_context *) |
| 672 |
lists_reserved_simplify pout. |
| 673 |
all:try solve_equivalence_side_conditions; |
| 674 |
seprewrite_in (FElem_array_truncated_scalar_iff1 pout) H1; extract_ex1_and_emp_in H1; try eassumption. |
| 675 |
seprewrite_in (FElem_array_truncated_scalar_iff1 px) H1; extract_ex1_and_emp_in H1. |
| 676 |
setoid_rewrite max_bounds_range_iff in res_bounds. |
| 677 |
rewrite (fun x pf => proj1 (res_bounds x pf)), ?map_length; trivial. |
| 678 |
ssplit; rewrite ?map_length; trivial. |
| 679 |
eapply List.Forall_map, Forall_forall; intros. |
| 680 |
rewrite MakeAccessSizes.bits_per_word_eq_width. |
| 681 |
eapply Properties.word.unsigned_range. } } |
| 682 |
postcondition_simplify. |
| 683 |
eapply sep_assoc,sep_comm, sep_assoc. eapply Proper_sep_iff1. |
| 684 |
{ eapply FElem_array_truncated_scalar_iff1. } |
| 685 |
{ reflexivity. } |
| 686 |
cbn [List.hd] in *. rewrite MakeAccessSizes.bytes_per_word_eq. |
| 687 |
extract_ex1_and_emp_in_goal; ssplit; |
| 688 |
try (use_sep_assumption; cancel; cbv [seps]); |
| 689 |
seprewrite_in (FElem_array_truncated_scalar_iff1 px) H1; extract_ex1_and_emp_in H1; trivial. |
| 690 |
Morphisms.f_equiv. |
| 691 |
rewrite H5. |
| 692 |
rewrite <-(res_eq x) at 2 by trivial. |
| 693 |
rewrite Util.map_unsigned_of_Z. |
| 694 |
erewrite map_word_wrap_bounded; trivial. |
| 695 |
eapply max_bounds_range_iff; ssplit; eauto. |
| 696 |
Qed. |
| 697 |
End FelemCopy. |
| 698 |
|
| 699 |
Section FromBytes. |
| 700 |
Context {res : API.Expr (type_listZ -> type_listZ)} |
| 701 |
(res_valid : |
| 702 |
valid_func (res (fun _ : API.type => unit))) |
| 703 |
(res_Wf : API.Wf res). |
| 704 |
Context (tight_bounds_tighter_than_max : |
| 705 |
forall x, |
| 706 |
list_in_bounds tight_bounds x -> |
| 707 |
list_Z_bounded_by (@max_bounds width n) x) |
| 708 |
(tight_bounds_length : forall x, list_in_bounds tight_bounds x -> length x = n) |
| 709 |
(byte_bounds_length : |
| 710 |
forall x, |
| 711 |
list_in_bounds byte_bounds x -> |
| 712 |
length x = encoded_felem_size_in_bytes) |
| 713 |
(res_eq : forall bs, |
| 714 |
bytes_in_bounds bs -> |
| 715 |
feval (map word.of_Z |
| 716 |
(API.interp (res _) (map byte.unsigned bs))) |
| 717 |
= feval_bytes bs) |
| 718 |
(res_bounds : forall bs, |
| 719 |
bytes_in_bounds bs -> |
| 720 |
list_in_bounds |
| 721 |
tight_bounds |
| 722 |
(API.interp (res _) (map byte.unsigned bs))). |
| 723 |
|
| 724 |
Lemma FElemBytes_in_bounds p bs R m : |
| 725 |
(FElemBytes p bs ⋆ R)%sep m -> |
| 726 |
bytes_in_bounds bs. |
| 727 |
Proof. cbv [FElemBytes]. intros; sepsimpl. assumption. Qed. |
| 728 |
|
| 729 |
Local Ltac equivalence_side_conditions_hook ::= |
| 730 |
lazymatch goal with |
| 731 |
| |- context [length (API.interp (res _) |
| 732 |
(map byte.unsigned ?x))] => |
| 733 |
specialize (res_bounds x ltac:(auto)); |
| 734 |
rewrite (length_list_Z_bounded_by _ _ res_bounds); |
| 735 |
try congruence; |
| 736 |
rewrite !map_length, tight_bounds_length; |
| 737 |
felem_to_array; sepsimpl; congruence |
| 738 |
| _ => idtac |
| 739 |
end. |
| 740 |
|
| 741 |
Local Notation t := |
| 742 |
(type.arrow type_listZ type_listZ) (only parsing). |
| 743 |
|
| 744 |
Definition from_bytes_insizes |
| 745 |
: type.for_each_lhs_of_arrow access_sizes t := |
| 746 |
(access_size.one, tt). |
| 747 |
Definition from_bytes_outsizes |
| 748 |
: base_access_sizes (type.final_codomain t) := |
| 749 |
access_size.word. |
| 750 |
Definition from_bytes_inlengths |
| 751 |
: type.for_each_lhs_of_arrow list_lengths t := |
| 752 |
(n_bytes, tt). |
| 753 |
Let insizes := from_bytes_insizes. |
| 754 |
Let outsizes := from_bytes_outsizes. |
| 755 |
Let inlengths := from_bytes_inlengths. |
| 756 |
|
| 757 |
Import coqutil.Macros.symmetry. |
| 758 |
Lemma from_bytes_correct f : |
| 759 |
f = make_bedrock_func insizes outsizes inlengths res -> |
| 760 |
forall functions, map.get functions from_bytes = Some f -> |
| 761 |
spec_of_from_bytes functions. |
| 762 |
Proof using inname_gen_varname_gen_disjoint |
| 763 |
outname_gen_varname_gen_disjoint ok relax_bounds res_Wf |
| 764 |
res_bounds res_eq res_valid tight_bounds_length |
| 765 |
tight_bounds_tighter_than_max byte_bounds_length felem_size_ok. |
| 766 |
subst inlengths insizes outsizes. cbv [spec_of_from_bytes]. |
| 767 |
cbv [from_bytes_insizes from_bytes_outsizes from_bytes_inlengths]. |
| 768 |
cbv beta; intros; subst f. cbv [make_bedrock_func] in *. |
| 769 |
cleanup. |
| 770 |
eapply Proper_call. |
| 771 |
2:{ |
| 772 |
use_translate_func_correct constr:((map Byte.byte.unsigned bs, tt)) Rr. |
| 773 |
all: try translate_func_precondition_hammer. |
| 774 |
{ autounfold with list_lengths pairs list_lengths; rewrite byte_bounds_length; trivial. } |
| 775 |
eapply (equivalent_flat_args_iff1 |
| 776 |
(make_innames (inname_gen:=default_inname_gen) _) |
| 777 |
_ _ _ |
| 778 |
map.empty); |
| 779 |
[ apply flatten_make_innames_NoDup; |
| 780 |
solve [eapply prefix_name_gen_unique] |
| 781 |
| reflexivity | ]; |
| 782 |
compute_names; autounfold with equivalence pairs; |
| 783 |
cbv [Equivalence.equivalent_base]; |
| 784 |
autounfold with equivalence pairs; |
| 785 |
rewrite <-?MakeAccessSizes.bytes_per_word_eq; |
| 786 |
sepsimpl; crush_sep; try solve [solve_equivalence_side_conditions]. |
| 787 |
change (Z.of_nat (bytes_per access_size.one)) with 1. |
| 788 |
try |
| 789 |
erewrite Util.map_unsigned_of_Z, map_word_wrap_bounded. |
| 790 |
{ rewrite <-(ByteBounds.byte_map_of_Z_unsigned bs) in H. |
| 791 |
seprewrite_in (symmetry! @Util.array_truncated_scalar_ptsto_iff1) H. |
| 792 |
ecancel_assumption. } |
| 793 |
{ eapply byte_unsigned_within_max_bounds; try rewrite byte_bounds_length; trivial. } |
| 794 |
{ (* lists_reserved_with_initial_context *) |
| 795 |
lists_reserved_simplify pout. |
| 796 |
all: try solve_equivalence_side_conditions. |
| 797 |
solve_length (bs2ws (word:=word) (Z.to_nat (bytes_per_word width)) out) tight_bounds_length. |
| 798 |
} } |
| 799 |
{ postcondition_simplify; [ | | ]. |
| 800 |
{ (* output correctness *) |
| 801 |
eapply res_eq; auto. } |
| 802 |
{ (* output bounds *) |
| 803 |
cbn [bounded_by field_representation frep] in *. |
| 804 |
erewrite Util.map_unsigned_of_Z, MaxBounds.map_word_wrap_bounded |
| 805 |
by eauto using relax_list_Z_bounded_by. |
| 806 |
eauto. } |
| 807 |
{ (* separation-logic postcondition *) |
| 808 |
eapply Proper_sep_iff1; |
| 809 |
[ solve [apply FElem_array_truncated_scalar_iff1] |
| 810 |
| reflexivity | ]. |
| 811 |
sepsimpl; [ | ]. |
| 812 |
{ rewrite !map_length; apply tight_bounds_length; auto. } |
| 813 |
{ erewrite Util.map_unsigned_of_Z, MaxBounds.map_word_wrap_bounded |
| 814 |
by eauto using relax_list_Z_bounded_by. |
| 815 |
rewrite MakeAccessSizes.bytes_per_word_eq. |
| 816 |
clear tight_bounds_length; subst. |
| 817 |
match goal with |
| 818 |
H : map word.unsigned _ = API.interp (res _) _ |- _ => |
| 819 |
rewrite <-H end. |
| 820 |
auto. } } } |
| 821 |
Qed. |
| 822 |
End FromBytes. |
| 823 |
|
| 824 |
Section ToBytes. |
| 825 |
Context {res : API.Expr (type_listZ -> type_listZ)} |
| 826 |
(res_valid : |
| 827 |
valid_func (res (fun _ : API.type => unit))) |
| 828 |
(res_Wf : API.Wf res). |
| 829 |
Context (byte_bounds_tighter_than_max : |
| 830 |
forall x, |
| 831 |
list_in_bounds byte_bounds x -> |
| 832 |
list_Z_bounded_by (ByteBounds.byte_bounds n_bytes) x) |
| 833 |
(byte_bounds_length : |
| 834 |
forall x, |
| 835 |
list_in_bounds byte_bounds x -> |
| 836 |
length x = encoded_felem_size_in_bytes) |
| 837 |
(res_eq : forall x, |
| 838 |
bounded_by tight_bounds x -> |
| 839 |
API.interp (res _) (map word.unsigned x) |
| 840 |
= Partition.partition |
| 841 |
(ModOps.weight 8 1) |
| 842 |
encoded_felem_size_in_bytes |
| 843 |
(F.to_Z (feval x))) |
| 844 |
(res_bounds : forall x, |
| 845 |
bounded_by tight_bounds x -> |
| 846 |
bytes_in_bounds |
| 847 |
(map byte.of_Z |
| 848 |
(API.interp (res _) |
| 849 |
(map word.unsigned x)))). |
| 850 |
|
| 851 |
Local Ltac equivalence_side_conditions_hook ::= |
| 852 |
lazymatch goal with |
| 853 |
| |- context [length (Partition.partition _ _ _)] => |
| 854 |
autorewrite with distr_length; |
| 855 |
cbv [FElemBytes] in *; sepsimpl; solve [auto] |
| 856 |
end. |
| 857 |
|
| 858 |
Local Notation t := |
| 859 |
(type.arrow type_listZ type_listZ) (only parsing). |
| 860 |
|
| 861 |
Definition to_bytes_insizes |
| 862 |
: type.for_each_lhs_of_arrow access_sizes t := |
| 863 |
(access_size.word, tt). |
| 864 |
Definition to_bytes_outsizes |
| 865 |
: base_access_sizes (type.final_codomain t) := |
| 866 |
access_size.one. |
| 867 |
Definition to_bytes_inlengths |
| 868 |
: type.for_each_lhs_of_arrow list_lengths t := |
| 869 |
(n, tt). |
| 870 |
Let insizes := to_bytes_insizes. |
| 871 |
Let outsizes := to_bytes_outsizes. |
| 872 |
Let inlengths := to_bytes_inlengths. |
| 873 |
|
| 874 |
Lemma partition_le_split : forall n z, |
| 875 |
map byte.of_Z (Partition.partition (ModOps.weight 8 1) n z) |
| 876 |
= LittleEndianList.le_split n z. |
| 877 |
Proof. |
| 878 |
cbv [Partition.partition]. intros. rewrite map_map. |
| 879 |
eapply List.nth_error_ext; intros i. |
| 880 |
rewrite nth_error_map, Crypto.Util.ListUtil.nth_error_seq. |
| 881 |
destruct (lt_dec i n0); cbn. |
| 882 |
2: { symmetry; eapply nth_error_None. |
| 883 |
rewrite LittleEndianList.length_le_split. Lia.lia. } |
| 884 |
rewrite LittleEndianList.nth_error_le_split; trivial; f_equal. |
| 885 |
rewrite Z.shiftr_div_pow2 by Lia.lia. |
| 886 |
apply byte.unsigned_inj. rewrite !byte.unsigned_of_Z. |
| 887 |
cbv [byte.wrap ModOps.weight]. |
| 888 |
autorewrite with zsimplify. |
| 889 |
rewrite Nat2Z.inj_succ. |
| 890 |
autorewrite with zsimplify. |
| 891 |
rewrite Z.add_comm, Z.pow_add_r by Lia.lia. |
| 892 |
rewrite !Modulo.Z.mod_pull_div by Lia.lia. |
| 893 |
rewrite Z.mod_mod by auto with zarith. |
| 894 |
reflexivity. |
| 895 |
Qed. |
| 896 |
|
| 897 |
Import coqutil.Macros.symmetry. |
| 898 |
Lemma to_bytes_correct f : |
| 899 |
f = make_bedrock_func insizes outsizes inlengths res -> |
| 900 |
forall functions, map.get functions to_bytes = Some f -> |
| 901 |
spec_of_to_bytes functions. |
| 902 |
Proof using byte_bounds_length byte_bounds_tighter_than_max |
| 903 |
inname_gen_varname_gen_disjoint |
| 904 |
outname_gen_varname_gen_disjoint ok res_Wf |
| 905 |
res_eq res_valid res_bounds. |
| 906 |
subst inlengths insizes outsizes. cbv [spec_of_to_bytes]. |
| 907 |
cbv [to_bytes_insizes to_bytes_outsizes to_bytes_inlengths]. |
| 908 |
cbv beta; intros; subst f. cbv [make_bedrock_func] in *. |
| 909 |
cleanup. |
| 910 |
eapply Proper_call. |
| 911 |
2:{ |
| 912 |
use_translate_func_correct |
| 913 |
constr:((map word.unsigned x, tt)) Rr. |
| 914 |
all:try translate_func_precondition_hammer. |
| 915 |
all:cbn [type.app_curried fst snd]. |
| 916 |
all:try rewrite res_eq by auto. |
| 917 |
{ eapply ByteBounds.byte_bounds_range_iff. |
| 918 |
auto using ByteBounds.partition_bounded_by. } |
| 919 |
{ (* lists_reserved_with_initial_context *) |
| 920 |
compute_names; cbn [type.app_curried fst snd]; |
| 921 |
autounfold with types list_lengths pairs; |
| 922 |
lists_autounfold; sepsimpl. |
| 923 |
exists (map byte.unsigned out). |
| 924 |
rewrite <-(ByteBounds.byte_map_of_Z_unsigned out) in H. |
| 925 |
seprewrite_in (symmetry! @Util.array_truncated_scalar_ptsto_iff1) H. |
| 926 |
crush_sep. |
| 927 |
all:try solve_equivalence_side_conditions. } } |
| 928 |
{ lists_autounfold; cbn[type.app_curried fst snd]; |
| 929 |
cbv[Equivalence.equivalent_listexcl_flat_base |
| 930 |
Equivalence.equivalent_listonly_flat_base |
| 931 |
Equivalence.equivalent_flat_base]; lists_autounfold; |
| 932 |
cbn[hd]; repeat intro; |
| 933 |
cbv[Core.postcondition_func_norets Core.postcondition_func]; |
| 934 |
sepsimpl; try congruence; |
| 935 |
repeat |
| 936 |
match goal with |
| 937 |
| _ => |
| 938 |
progress subst |
| 939 |
| H:WeakestPrecondition.literal (word.unsigned _) _ |
| 940 |
|- _ => |
| 941 |
cbv[WeakestPrecondition.literal dlet.dlet] in H; |
| 942 |
rewrite word.of_Z_unsigned in H |
| 943 |
| H:word.unsigned _ = word.unsigned _ |
| 944 |
|- _ => apply Properties.word.unsigned_inj in H |
| 945 |
| |- exists _, _ => eexists |
| 946 |
| |- _ /\ _ => eexists |
| 947 |
end; |
| 948 |
change Field.tight_bounds with tight_bounds in *. |
| 949 |
{ seprewrite_in (@Util.array_truncated_scalar_ptsto_iff1) H11; cbn in H11. |
| 950 |
rewrite H8, res_eq, partition_le_split in *; trivial. } |
| 951 |
rewrite <-partition_le_split, <-res_eq; eauto. } |
| 952 |
Qed. |
| 953 |
End ToBytes. |
| 954 |
|
| 955 |
|
| 956 |
Section SelectZnZ. |
| 957 |
|
| 958 |
Local Notation bit_range := {|ZRange.lower := 0; ZRange.upper := 1|}. |
| 959 |
Context {res : API.Expr (type_Z -> type_listZ -> type_listZ -> type_listZ)} |
| 960 |
(res_valid : |
| 961 |
valid_func (res (fun _ : API.type => unit))) |
| 962 |
(res_Wf : API.Wf res). |
| 963 |
|
| 964 |
Context |
| 965 |
(res_eq : forall (x y : list word) (c : word), |
| 966 |
list_Z_bounded_by (@max_bounds width n) (map word.unsigned x) -> |
| 967 |
list_Z_bounded_by (@max_bounds width n) (map word.unsigned y) -> |
| 968 |
ZRange.is_bounded_by_bool (word.unsigned c) bit_range = true -> |
| 969 |
(API.interp (res _) |
| 970 |
(word.unsigned c) |
| 971 |
(map word.unsigned x) |
| 972 |
(map word.unsigned y)) |
| 973 |
= map word.unsigned (if (word.unsigned c =? 0) then x else y)). |
| 974 |
|
| 975 |
Local Ltac equivalence_side_conditions_hook ::= |
| 976 |
lazymatch goal with |
| 977 |
| |- context [length (Partition.partition _ _ _)] => |
| 978 |
autorewrite with distr_length; |
| 979 |
cbv [FElemBytes] in *; sepsimpl; solve [auto] |
| 980 |
end. |
| 981 |
|
| 982 |
Local Notation t := |
| 983 |
(type.arrow type_Z (type.arrow type_listZ (type.arrow type_listZ type_listZ))) |
| 984 |
(only parsing). |
| 985 |
|
| 986 |
Definition list_selectznz_insizes |
| 987 |
: type.for_each_lhs_of_arrow access_sizes t := |
| 988 |
(tt, (access_size.word, (access_size.word, tt))). |
| 989 |
Definition list_selectznz_outsizes |
| 990 |
: base_access_sizes (type.final_codomain t) := |
| 991 |
access_size.word. |
| 992 |
Definition list_selectznz_inlengths |
| 993 |
: type.for_each_lhs_of_arrow list_lengths t := |
| 994 |
(tt, (n, (n, tt))). |
| 995 |
|
| 996 |
Let insizes := list_selectznz_insizes. |
| 997 |
Let outsizes := list_selectznz_outsizes. |
| 998 |
Let inlengths := list_selectznz_inlengths. |
| 999 |
|
| 1000 |
Lemma bit_range_eq : forall x, ZRange.is_bounded_by_bool x bit_range = true -> x = 0 \/ x = 1. |
| 1001 |
Proof. |
| 1002 |
intros. apply RulesProofs.unfold_is_bounded_by_bool in H. |
| 1003 |
simpl in H. lia. |
| 1004 |
Qed. |
| 1005 |
|
| 1006 |
Lemma max_bounds_words : forall (x : list word) n, length x = n -> list_Z_bounded_by (@max_bounds width n) (map word.unsigned x). |
| 1007 |
Proof. |
| 1008 |
intros. generalize dependent x. |
| 1009 |
induction n0; intros. |
| 1010 |
- destruct x; try discriminate. simpl. cbv. auto. |
| 1011 |
- destruct x; try discriminate. simpl. |
| 1012 |
eapply Util.list_Z_bounded_by_cons. split. |
| 1013 |
2: { |
| 1014 |
simpl in IHn0. eapply IHn0. auto. |
| 1015 |
} |
| 1016 |
apply Expr.is_bounded_by_bool_width_range. |
| 1017 |
eauto. |
| 1018 |
pose proof Properties.word.unsigned_range. auto. |
| 1019 |
Qed. |
| 1020 |
|
| 1021 |
Lemma FElem_max_bounds : forall px x m R, (FElem px x * R)%sep m -> list_Z_bounded_by (@max_bounds width n) (map word.unsigned x). |
| 1022 |
Proof. |
| 1023 |
intros. eapply max_bounds_words. cbv [FElem Bignum.Bignum] in H. sepsimpl. eauto. |
| 1024 |
Qed. |
| 1025 |
|
| 1026 |
Lemma select_znz_correct f : |
| 1027 |
f = make_bedrock_func insizes outsizes inlengths res -> |
| 1028 |
forall functions, map.get functions select_znz = Some f -> |
| 1029 |
spec_of_selectznz functions. |
| 1030 |
Proof using inname_gen_varname_gen_disjoint |
| 1031 |
outname_gen_varname_gen_disjoint ok res_Wf |
| 1032 |
res_eq res_valid relax_bounds felem_size_ok. |
| 1033 |
subst inlengths insizes outsizes. cbv [spec_of_selectznz]. |
| 1034 |
cbv [list_selectznz_insizes list_selectznz_outsizes list_selectznz_inlengths]. |
| 1035 |
cbv beta; intros; subst f. cbv [make_bedrock_func] in *. |
| 1036 |
cleanup. |
| 1037 |
pose proof (FElem_max_bounds _ _ _ _ H1) as Hxbounds. |
| 1038 |
pose proof (FElem_max_bounds _ _ _ _ H2) as Hybounds. |
| 1039 |
match goal with |
| 1040 |
| H : ZRange.is_bounded_by_bool _ _ = _ |- _ => rename H into Hbound |
| 1041 |
| _ => idtac |
| 1042 |
end. |
| 1043 |
eapply Proper_call. |
| 1044 |
2:{ use_translate_func_correct |
| 1045 |
constr:((word.unsigned pc, (map word.unsigned x, (map word.unsigned y, tt)))) Rout. |
| 1046 |
all:try translate_func_precondition_hammer. |
| 1047 |
all:cbn [type.app_curried fst snd]. |
| 1048 |
all:try rewrite res_eq by auto. |
| 1049 |
{ |
| 1050 |
eapply (equivalent_flat_args_iff1 |
| 1051 |
(make_innames (inname_gen:=default_inname_gen) _) |
| 1052 |
_ _ _ |
| 1053 |
map.empty). |
| 1054 |
- apply flatten_make_innames_NoDup; solve [eapply prefix_name_gen_unique]. |
| 1055 |
- reflexivity. |
| 1056 |
- compute_names. autounfold with equivalence pairs. |
| 1057 |
cbv [Equivalence.equivalent_base]. |
| 1058 |
autounfold with equivalence pairs. |
| 1059 |
rewrite <- ?MakeAccessSizes.bytes_per_word_eq; sepsimpl. |
| 1060 |
+ crush_sep; try solve_equivalence_side_conditions. eauto. (*Which one?*) |
| 1061 |
+ crush_sep; try solve_equivalence_side_conditions. |
| 1062 |
+ crush_sep; try solve_equivalence_side_conditions. |
| 1063 |
+ auto. |
| 1064 |
} |
| 1065 |
1: destruct (bit_range_eq _ Hbound) as [Hbit| Hbit]; rewrite Hbit; simpl; |
| 1066 |
eapply MaxBounds.max_bounds_range_iff; eauto. |
| 1067 |
lists_reserved_simplify pout. |
| 1068 |
all: try solve_equivalence_side_conditions. |
| 1069 |
destruct (bit_range_eq _ Hbound) as [Hbit| Hbit]; rewrite Hbit; simpl; auto; cbv [FElem Bignum.Bignum] in *; sepsimpl; |
| 1070 |
repeat rewrite map_length; |
| 1071 |
match goal with |
| 1072 |
| H : ?n1 = _ |- ?n1 = _ => rewrite H |
| 1073 |
| _ => idtac |
| 1074 |
end; eauto. |
| 1075 |
} |
| 1076 |
postcondition_simplify. |
| 1077 |
destruct (bit_range_eq _ Hbound) as [Hbit| Hbit]. |
| 1078 |
- rewrite Hbit. simpl. eapply Proper_sep_iff1. |
| 1079 |
+ apply FElem_array_truncated_scalar_iff1. |
| 1080 |
+ intros; split; intros; eauto. |
| 1081 |
+ sepsimpl; try ecancel_assumption. |
| 1082 |
* erewrite <- map_length. erewrite length_list_Z_bounded_by; eauto. |
| 1083 |
cbv [max_bounds]. rewrite repeat_length. eauto. |
| 1084 |
* specialize (res_eq x y pc); rewrite Hbit in res_eq; simpl in res_eq. |
| 1085 |
erewrite <- res_eq; eauto; rewrite <- Hbit; simpl in *; eauto. |
| 1086 |
match goal with |
| 1087 |
| H : map word.unsigned _ = _ |- _ => rewrite <- H |
| 1088 |
| _ => idtac |
| 1089 |
end; eauto. |
| 1090 |
- rewrite Hbit; simpl; eapply Proper_sep_iff1. |
| 1091 |
+ apply FElem_array_truncated_scalar_iff1. |
| 1092 |
+ intros; split; intros; eauto. |
| 1093 |
+ sepsimpl; try ecancel_assumption. |
| 1094 |
* erewrite <- map_length; erewrite length_list_Z_bounded_by; eauto; |
| 1095 |
cbv [max_bounds]; rewrite repeat_length; eauto. |
| 1096 |
* specialize (res_eq x y pc); rewrite Hbit in res_eq; simpl in res_eq. |
| 1097 |
erewrite <- res_eq; eauto; rewrite <- Hbit; simpl in *; eauto. |
| 1098 |
match goal with |
| 1099 |
| H : map word.unsigned _ = _ |- _ => rewrite <- H |
| 1100 |
| _ => idtac |
| 1101 |
end; eauto. |
| 1102 |
Qed. |
| 1103 |
End SelectZnZ. |
| 1104 |
End WithParameters. |