Debian Codegraph @

src/Arithmetic/UniformWeight.v

raw
1 From Coq Require Import ZArith Lia.
2 From Coq Require Import List.
3 Require Import Crypto.Arithmetic.Core.
4 Require Import Crypto.Arithmetic.ModOps.
5 Require Import Crypto.Arithmetic.Partition.
6 Require Import Crypto.Util.ListUtil.
7 Require Import Crypto.Util.Tactics.SpecializeBy.
8 Require Import Crypto.Util.Tactics.SplitInContext.
9 Require Import Crypto.Util.ZUtil.Modulo Crypto.Util.ZUtil.Div.
10 Require Import Crypto.Util.ZUtil.Tactics.RewriteModSmall.
11 Require Import Crypto.Util.ZUtil.Tactics.PullPush.Modulo.
12
13 Require Import Crypto.Util.Notations.
14 Local Open Scope Z_scope.
15 Import Weight.
16
17 Definition uweight (lgr : Z) : nat -> Z
18 := weight lgr 1.
19 Definition uwprops lgr (Hr : 0 < lgr) : @weight_properties (uweight lgr).
20 Proof using Type. apply wprops; lia. Qed.
21 Lemma uweight_eq_alt' lgr n : uweight lgr n = 2^(lgr*Z.of_nat n).
22 Proof using Type. now cbv [uweight weight]; autorewrite with zsimplify_fast. Qed.
23 Lemma uweight_eq_alt lgr (Hr : 0 <= lgr) n : uweight lgr n = (2^lgr)^Z.of_nat n.
24 Proof using Type. now rewrite uweight_eq_alt', Z.pow_mul_r by lia. Qed.
25 Lemma uweight_eval_shift lgr (Hr : 0 <= lgr) xs :
26 forall n,
27 length xs = n ->
28 Positional.eval (fun i => uweight lgr (S i)) n xs =
29 (uweight lgr 1) * Positional.eval (uweight lgr) n xs.
30 Proof using Type.
31 induction xs using rev_ind; destruct n; distr_length;
32 intros; [cbn; ring | ].
33 rewrite !Positional.eval_snoc with (n:=n) by distr_length.
34 rewrite IHxs, !uweight_eq_alt by lia.
35 autorewrite with push_Zof_nat push_Zpow.
36 rewrite !Z.pow_succ_r by auto with zarith.
37 ring.
38 Qed.
39 Lemma uweight_S lgr (Hr : 0 <= lgr) n : uweight lgr (S n) = 2 ^ lgr * uweight lgr n.
40 Proof using Type.
41 rewrite !uweight_eq_alt by auto.
42 autorewrite with push_Zof_nat.
43 rewrite Z.pow_succ_r by auto with zarith.
44 reflexivity.
45 Qed.
46 Lemma uweight_double_le lgr (Hr : 0 < lgr) n : uweight lgr n + uweight lgr n <= uweight lgr (S n).
47 Proof using Type.
48 rewrite uweight_S, uweight_eq_alt by lia.
49 rewrite Z.add_diag.
50 apply Z.mul_le_mono_nonneg_r.
51 { auto with zarith. }
52 { transitivity (2 ^ 1); [ reflexivity | ].
53 apply Z.pow_le_mono_r; lia. }
54 Qed.
55 Lemma uweight_sum_indices lgr (Hr : 0 <= lgr) i j : uweight lgr (i + j) = uweight lgr i * uweight lgr j.
56 Proof.
57 rewrite !uweight_eq_alt by lia.
58 rewrite Nat2Z.inj_add; auto using Z.pow_add_r with zarith.
59 Qed.
60 Lemma uweight_0 lgr : uweight lgr 0 = 1.
61 Proof.
62 rewrite uweight_eq_alt', Z.mul_0_r; reflexivity.
63 Qed.
64 Lemma uweight_1 lgr : uweight lgr 1 = 2^lgr.
65 Proof using Type.
66 cbv [uweight weight].
67 f_equal; autorewrite with zsimplify_const; lia.
68 Qed.
69
70 (* Because the weight is uniform, we can start partitioning from
71 any index and end up with the same result. *)
72 Lemma uweight_recursive_partition_change_start lgr (Hr : 0 <= lgr) n :
73 forall i j x,
74 recursive_partition (uweight lgr) n i x
75 = recursive_partition (uweight lgr) n j x.
76 Proof using Type.
77 induction n; intros; [reflexivity | ].
78 cbn [recursive_partition].
79 rewrite !uweight_eq_alt by lia.
80 autorewrite with push_Zof_nat push_Zpow.
81 rewrite <-!Z.pow_sub_r by auto using Z.pow_nonzero with lia.
82 rewrite !Z.sub_succ_l.
83 autorewrite with zsimplify_fast.
84 erewrite IHn. reflexivity.
85 Qed.
86 Lemma uweight_recursive_partition_equiv lgr (Hr : 0 < lgr) n i x:
87 Partition.partition (uweight lgr) n x =
88 recursive_partition (uweight lgr) n i x.
89 Proof using Type.
90 rewrite recursive_partition_equiv by auto using uwprops.
91 auto using uweight_recursive_partition_change_start with lia.
92 Qed.
93
94 Lemma uweight_firstn_partition lgr (Hr : 0 < lgr) n x m (Hm : (m <= n)%nat) :
95 firstn m (Partition.partition (uweight lgr) n x) = Partition.partition (uweight lgr) m x.
96 Proof.
97 cbv [Partition.partition];
98 repeat match goal with
99 | _ => progress intros
100 | _ => progress autorewrite with push_firstn natsimplify zsimplify_fast
101 | _ => rewrite Nat.min_l by lia
102 | _ => rewrite weight_0 by auto using uwprops
103 | _ => reflexivity
104 end.
105 Qed.
106
107 Lemma uweight_skipn_partition lgr (Hr : 0 < lgr) n x m :
108 skipn m (Partition.partition (uweight lgr) n x) = Partition.partition (uweight lgr) (n - m) (x / uweight lgr m).
109 Proof.
110 cbv [Partition.partition];
111 repeat match goal with
112 | _ => progress intros
113 | _ => progress autorewrite with push_skipn natsimplify zsimplify_fast
114 | _ => rewrite skipn_seq by auto
115 | _ => rewrite weight_0 by auto using uwprops
116 | _ => rewrite recursive_partition_equiv' by auto using uwprops
117 | _ => auto using uweight_recursive_partition_change_start with zarith
118 end.
119 Qed.
120
121 Lemma uweight_partition_unique lgr (Hr : 0 < lgr) n ls :
122 length ls = n -> (forall x, List.In x ls -> 0 <= x <= 2^lgr - 1) ->
123 ls = Partition.partition (uweight lgr) n (Positional.eval (uweight lgr) n ls).
124 Proof using Type.
125 intro; subst n.
126 rewrite uweight_recursive_partition_equiv with (i:=0%nat) by assumption.
127 induction ls as [|x xs IHxs]; [ reflexivity | ].
128 repeat first [ progress cbn [List.length recursive_partition List.In] in *
129 | progress intros
130 | assumption
131 | rewrite Positional.eval_cons by reflexivity
132 | rewrite weight_0 by now apply uwprops
133 | rewrite uweight_1
134 | progress specialize_by_assumption
135 | progress split_contravariant_or
136 | rewrite uweight_recursive_partition_change_start with (i:=1%nat) (j:=0%nat) by lia
137 | rewrite uweight_eval_shift by lia
138 | rewrite Z.div_1_r
139 | progress Z.rewrite_mod_small
140 | rewrite Z.div_add' by auto with arith lia
141 | rewrite Z.div_small by lia
142 | match goal with
143 | [ H : forall x, _ = x -> _ |- _ ] => specialize (H _ eq_refl)
144 | [ |- context[(_ + ?x * _) mod ?x] ]
145 => let k := fresh in
146 set (k := x); push_Zmod; pull_Zmod; subst k;
147 progress autorewrite with zsimplify_const
148 | [ |- ?x :: _ = ?x :: _ ] => apply f_equal
149 end ].
150 Qed.
151
152 Lemma uweight_eval_app' lgr (Hr : 0 <= lgr) n x y :
153 n = length x ->
154 Positional.eval (uweight lgr) (n + length y) (x ++ y) = Positional.eval (uweight lgr) n x + (uweight lgr n) * Positional.eval (uweight lgr) (length y) y.
155 Proof using Type.
156 induction y using rev_ind;
157 repeat match goal with
158 | _ => progress intros
159 | _ => progress distr_length
160 | _ => progress autorewrite with push_eval zsimplify natsimplify
161 | _ => rewrite Nat.add_succ_r
162 | H : ?x = 0%nat |- _ => subst x
163 | _ => progress rewrite ?app_nil_r, ?app_assoc
164 | _ => reflexivity
165 end.
166 rewrite IHy by auto. rewrite uweight_sum_indices; lia.
167 Qed.
168
169 Lemma uweight_eval_app lgr (Hr : 0 <= lgr) n m x y :
170 n = length x ->
171 m = (n + length y)%nat ->
172 Positional.eval (uweight lgr) m (x ++ y) = Positional.eval (uweight lgr) n x + (uweight lgr n) * Positional.eval (uweight lgr) (length y) y.
173 Proof using Type. intros. subst m. apply uweight_eval_app'; lia. Qed.
174
175 Lemma uweight_partition_app lgr (Hr : 0 < lgr) n m a b :
176 Partition.partition (uweight lgr) n a ++ Partition.partition (uweight lgr) m b
177 = Partition.partition (uweight lgr) (n+m) (a mod uweight lgr n + b * uweight lgr n).
178 Proof.
179 assert (0 < uweight lgr n) by auto using uwprops.
180 match goal with |- _ = ?rhs => rewrite <-(firstn_skipn n rhs) end.
181 rewrite uweight_firstn_partition, uweight_skipn_partition by lia.
182 rewrite Z.div_add by lia.
183 rewrite (Z.div_small (_ mod _)) by auto with zarith.
184 f_equal.
185 { apply partition_eq_mod; [ auto using uwprops | ].
186 push_Zmod. autorewrite with zsimplify. reflexivity. }
187 { f_equal; lia. }
188 Qed.
189
190 Lemma mod_mod_uweight lgr (Hr : 0 < lgr) a i j :
191 (i <= j)%nat -> (a mod (uweight lgr j)) mod (uweight lgr i) = a mod (uweight lgr i).
192 Proof.
193 intros. rewrite <-Znumtheory.Zmod_div_mod; auto using uwprops; [ ].
194 rewrite !uweight_eq_alt'. apply Divide.Z.divide_pow_le. nia.
195 Qed.
196
197 Lemma uweight_pull_mod lgr (Hr : 0 < lgr) x i j :
198 (j <= i)%nat ->
199 x mod (uweight lgr i) / uweight lgr j = (x / uweight lgr j) mod (uweight lgr (i - j)).
200 Proof.
201 intros. rewrite Z.mod_pull_div by auto using Z.lt_le_incl, uwprops.
202 rewrite <-uweight_sum_indices by lia.
203 repeat (f_equal; try lia).
204 Qed.