Debian Codegraph @

src/Bedrock/Field/Common/Arrays/ByteBounds.v

raw
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 coqutil.Byte.
8 Require Import bedrock2.Syntax.
9 Require Import Crypto.Arithmetic.Partition.
10 Require Import Crypto.Bedrock.Field.Common.Tactics.
11 Require Import Crypto.Bedrock.Field.Common.Types.
12 Require Import Crypto.Bedrock.Field.Common.Util.
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 Import Partition.
21
22 Section ByteBounds.
23 Context
24 {width BW word mem locals ext_spec varname_gen error}
25 `{parameters_sentinel : @parameters width BW word mem locals ext_spec varname_gen error}.
26 Context {ok : ok}.
27 Context (n : nat).
28
29 Definition byte_range : ZRange.zrange :=
30 {|ZRange.lower:=0; ZRange.upper:=2^8-1|}.
31 Definition byte_bounds : list (option ZRange.zrange) :=
32 repeat (Some byte_range) n.
33
34 (* TODO: consider refactoring this lemma into lemmas about Forall, Forall2,
35 and fold_andb_map *)
36 Lemma byte_bounds_range_iff x :
37 list_Z_bounded_by byte_bounds x <->
38 (length x = n /\
39 Forall (fun z : Z => 0 <= z < 2 ^ 8) x).
40 Proof.
41 cbv [byte_bounds byte_range list_Z_bounded_by].
42 generalize n as m.
43 induction x; destruct m; split;
44 cbn [FoldBool.fold_andb_map repeat]; try congruence; intros;
45 repeat match goal with
46 | _ => progress cleanup
47 | _ => progress cbn [length ZRange.lower ZRange.upper] in *
48 | |- Forall _ [] => solve [constructor]
49 | |- Forall _ (_ :: _) => constructor
50 | H: Forall _ (_ :: _) |- _ =>
51 inversion H; clear H; subst
52 | |- (_ && _)%bool = true =>
53 apply Bool.andb_true_iff; split
54 | H: (_ && _)%bool = true |- _ =>
55 apply Bool.andb_true_iff in H; destruct H
56 | H : context [iff] |- _ => eapply H; solve [eauto]
57 | H : context [iff] |- _ =>
58 rewrite H; auto; congruence
59 | |- _ /\ _ => split
60 | |- S _ = S _ => f_equal
61 | _ => progress Z.ltb_to_lt
62 | _ => congruence
63 | _ => lia
64 end.
65 Qed.
66
67 Lemma map_byte_wrap_bounded' r x m :
68 ZRange.is_tighter_than_bool r byte_range = true ->
69 list_Z_bounded_by (repeat (Some r) m) x ->
70 map byte.wrap x = x.
71 Proof.
72 intros.
73 pose proof length_list_Z_bounded_by _ x ltac:(eassumption).
74 cbv [byte_bounds byte_range list_Z_bounded_by
75 ZRange.is_tighter_than_bool] in *.
76 rewrite repeat_length in *.
77 generalize dependent m.
78 generalize dependent x; induction x; destruct m;
79 repeat match goal with
80 | _ => progress intros
81 | _ => progress cleanup
82 | _ => progress
83 cbn [length FoldBool.fold_andb_map
84 ZRange.upper ZRange.lower
85 repeat map] in *
86 | H : (_ && _)%bool = true |- _ =>
87 apply Bool.andb_true_iff in H
88 | IH : context [map byte.wrap ?x = ?x] |- _ =>
89 rewrite IH with (m:=m) by (try eassumption; lia)
90 | _ => progress Z.ltb_to_lt
91 | |- byte.wrap ?x :: ?y = ?x :: ?y =>
92 cbv [byte.wrap]; Z.rewrite_mod_small;
93 reflexivity
94 | _ => congruence
95 end.
96 Qed.
97
98 Lemma map_byte_wrap_bounded x :
99 list_Z_bounded_by byte_bounds x ->
100 map byte.wrap x = x.
101 Proof.
102 intros. eapply map_byte_wrap_bounded'; [ | eassumption ].
103 apply ZRange.is_tighter_than_bool_Reflexive.
104 Qed.
105
106 Lemma byte_map_unsigned_of_Z x :
107 map byte.unsigned (map byte.of_Z x) = map byte.wrap x.
108 Proof.
109 rewrite map_map. apply map_ext. exact byte.unsigned_of_Z.
110 Qed.
111
112 Lemma byte_map_of_Z_unsigned x :
113 map byte.of_Z (map byte.unsigned x) = x.
114 Proof.
115 rewrite map_map.
116 erewrite map_ext; [ apply map_id | ].
117 auto using byte.of_Z_unsigned.
118 Qed.
119
120 Lemma partition_bytes_range :
121 forall x,
122 Forall (fun z => 0 <= z < 2^8)
123 (partition (ModOps.weight 8 1) n x).
124 Proof.
125 induction n; intros; [ solve [constructor] | ].
126 rewrite partition_step. apply Forall_snoc; auto; [ ].
127 fold (UniformWeight.uweight 8).
128 rewrite UniformWeight.uweight_S by lia.
129 rewrite <-Z.mod_pull_div by auto with zarith.
130 apply Z.mod_pos_bound; auto with zarith.
131 Qed.
132
133 Lemma partition_bounded_by x :
134 list_Z_bounded_by byte_bounds (partition (ModOps.weight 8 1) n x).
135 Proof.
136 apply byte_bounds_range_iff; split;
137 auto using partition_bytes_range, length_partition.
138 Qed.
139 End ByteBounds.