| 1 |
From Coq Require Import ZArith. |
| 2 |
From Coq Require Import List. |
| 3 |
From Coq Require Import Lia. |
| 4 |
Require Import coqutil.Word.Interface. |
| 5 |
Require Import coqutil.Word.Properties. |
| 6 |
Require Import coqutil.Datatypes.List. |
| 7 |
Require Import bedrock2.Syntax. |
| 8 |
Require Import Crypto.Arithmetic.Partition. |
| 9 |
Require Import Crypto.Bedrock.Field.Common.Tactics. |
| 10 |
Require Import Crypto.Bedrock.Field.Common.Types. |
| 11 |
Require Import Crypto.Bedrock.Field.Common.Util. |
| 12 |
Require Import Crypto.Bedrock.Field.Common.Arrays.MakeAccessSizes. |
| 13 |
Require Import Crypto.COperationSpecifications. |
| 14 |
Require Import Crypto.Util.ZRange.BasicLemmas. |
| 15 |
Require Import Crypto.Util.ZUtil.Modulo. |
| 16 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 17 |
Require Import Crypto.Util.ZUtil.Tactics.RewriteModSmall. |
| 18 |
Local Open Scope Z_scope. |
| 19 |
Import ListNotations. |
| 20 |
|
| 21 |
Section MaxBounds. |
| 22 |
Context |
| 23 |
{width BW word mem locals ext_spec varname_gen error} |
| 24 |
`{parameters_sentinel : @parameters width BW word mem locals ext_spec varname_gen error}. |
| 25 |
Context {ok : ok}. |
| 26 |
Context (n : nat). |
| 27 |
|
| 28 |
(* TODO: make Expr.v import this *) |
| 29 |
Definition max_range : ZRange.zrange := |
| 30 |
{|ZRange.lower:=0; ZRange.upper:=2^width-1|}. |
| 31 |
Definition max_bounds : list (option ZRange.zrange) := |
| 32 |
repeat (Some max_range) n. |
| 33 |
|
| 34 |
Lemma max_bounds_range_iff x : |
| 35 |
let bytes := (Memory.bytes_per |
| 36 |
(width:=width) access_size.word) in |
| 37 |
list_Z_bounded_by max_bounds x <-> |
| 38 |
(length x = n /\ |
| 39 |
Forall |
| 40 |
(fun z : Z => |
| 41 |
0 <= z < 2 ^ (Z.of_nat bytes * 8)) x). |
| 42 |
Proof. |
| 43 |
cbv [max_bounds max_range list_Z_bounded_by]. |
| 44 |
replace (Z.of_nat (Memory.bytes_per access_size.word) * 8) with width |
| 45 |
by (destruct Bitwidth.width_cases; subst width; exact eq_refl). |
| 46 |
generalize n as m. |
| 47 |
induction x; destruct m; split; |
| 48 |
cbn [FoldBool.fold_andb_map repeat]; try congruence; intros; |
| 49 |
repeat match goal with |
| 50 |
| _ => progress cleanup |
| 51 |
| _ => progress cbn [length ZRange.lower ZRange.upper] in * |
| 52 |
| |- Forall _ [] => solve [constructor] |
| 53 |
| |- Forall _ (_ :: _) => constructor |
| 54 |
| H: Forall _ (_ :: _) |- _ => |
| 55 |
inversion H; clear H; subst |
| 56 |
| |- (_ && _)%bool = true => |
| 57 |
apply Bool.andb_true_iff; split |
| 58 |
| H: (_ && _)%bool = true |- _ => |
| 59 |
apply Bool.andb_true_iff in H; destruct H |
| 60 |
| H : context [iff] |- _ => eapply H; solve [eauto] |
| 61 |
| H : context [iff] |- _ => |
| 62 |
rewrite H; auto; congruence |
| 63 |
| |- _ /\ _ => split |
| 64 |
| |- S _ = S _ => f_equal |
| 65 |
| _ => progress Z.ltb_to_lt |
| 66 |
| _ => congruence |
| 67 |
| _ => lia |
| 68 |
end. |
| 69 |
Qed. |
| 70 |
|
| 71 |
|
| 72 |
Lemma map_word_wrap_bounded' r x m : |
| 73 |
ZRange.is_tighter_than_bool r max_range = true -> |
| 74 |
list_Z_bounded_by (repeat (Some r) m) x -> |
| 75 |
map word.wrap x = x. |
| 76 |
Proof. |
| 77 |
intros. |
| 78 |
pose proof length_list_Z_bounded_by _ x ltac:(eassumption). |
| 79 |
cbv [max_bounds max_range list_Z_bounded_by |
| 80 |
ZRange.is_tighter_than_bool] in *. |
| 81 |
rewrite repeat_length in *. |
| 82 |
generalize dependent m. |
| 83 |
generalize dependent x; induction x; destruct m; |
| 84 |
repeat match goal with |
| 85 |
| _ => progress intros |
| 86 |
| _ => progress cleanup |
| 87 |
| _ => progress |
| 88 |
cbn [length FoldBool.fold_andb_map |
| 89 |
ZRange.upper ZRange.lower |
| 90 |
repeat map] in * |
| 91 |
| H : (_ && _)%bool = true |- _ => |
| 92 |
apply Bool.andb_true_iff in H |
| 93 |
| IH : context [map word.wrap ?x = ?x] |- _ => |
| 94 |
rewrite IH with (m:=m) by (try eassumption; lia) |
| 95 |
| _ => progress Z.ltb_to_lt |
| 96 |
| |- word.wrap ?x :: ?y = ?x :: ?y => |
| 97 |
cbv [word.wrap]; Z.rewrite_mod_small; |
| 98 |
reflexivity |
| 99 |
| _ => congruence |
| 100 |
end. |
| 101 |
Qed. |
| 102 |
|
| 103 |
Lemma map_word_wrap_bounded x : |
| 104 |
list_Z_bounded_by max_bounds x -> |
| 105 |
map word.wrap x = x. |
| 106 |
Proof. |
| 107 |
intros. eapply map_word_wrap_bounded'; [ | eassumption ]. |
| 108 |
apply ZRange.is_tighter_than_bool_Reflexive. |
| 109 |
Qed. |
| 110 |
|
| 111 |
Lemma byte_unsigned_within_max_bounds x : |
| 112 |
length x = n -> |
| 113 |
list_Z_bounded_by max_bounds (map Byte.byte.unsigned x). |
| 114 |
Proof. |
| 115 |
intros; apply max_bounds_range_iff; split; |
| 116 |
[ rewrite ?map_length; solve [auto] | ]. |
| 117 |
eapply Forall_impl; |
| 118 |
[ | apply Forall_map_byte_unsigned ]. |
| 119 |
cbv beta; intros. |
| 120 |
pose proof Z.pow_le_mono_r 2 8 width ltac:(lia) width_ge_8. |
| 121 |
replace (Z.of_nat (Memory.bytes_per access_size.word) * 8) with width |
| 122 |
by (destruct Bitwidth.width_cases; subst width; exact eq_refl). |
| 123 |
lia. |
| 124 |
Qed. |
| 125 |
|
| 126 |
Lemma partition_max_range : |
| 127 |
forall x, |
| 128 |
let bytes := (Memory.bytes_per |
| 129 |
(width:=width) access_size.word) in |
| 130 |
Forall (fun z => 0 <= z < 2^(Z.of_nat bytes * 8)) |
| 131 |
(Partition.partition |
| 132 |
(UniformWeight.uweight width) n x). |
| 133 |
Proof. |
| 134 |
pose proof word.width_pos. cbv zeta. |
| 135 |
induction n; intros; [ solve [constructor] | ]. |
| 136 |
rewrite partition_step. apply Forall_snoc; auto; [ ]. |
| 137 |
rewrite UniformWeight.uweight_S by lia. |
| 138 |
rewrite <-Z.mod_pull_div by auto with zarith. |
| 139 |
replace (Z.of_nat (Memory.bytes_per access_size.word) * 8) with width |
| 140 |
by (destruct Bitwidth.width_cases; subst width; exact eq_refl). |
| 141 |
apply Z.mod_pos_bound; auto with zarith. |
| 142 |
Qed. |
| 143 |
|
| 144 |
Lemma partition_bounded_by x : |
| 145 |
list_Z_bounded_by |
| 146 |
max_bounds |
| 147 |
(Partition.partition (UniformWeight.uweight width) n x). |
| 148 |
Proof. |
| 149 |
apply max_bounds_range_iff; split; |
| 150 |
auto using partition_max_range, length_partition. |
| 151 |
Qed. |
| 152 |
End MaxBounds. |