| 1 |
From Coq Require Import ZArith Lia. |
| 2 |
Require Import QArith_base Qround Crypto.Util.QUtil. |
| 3 |
Require Import Crypto.Arithmetic.BaseConversion. |
| 4 |
Require Import Crypto.Arithmetic.Core. |
| 5 |
Require Import Crypto.Arithmetic.ModOps. |
| 6 |
Require Import Crypto.Arithmetic.Partition. |
| 7 |
Require Import Crypto.Arithmetic.Saturated. |
| 8 |
Require Import Crypto.Util.Decidable. |
| 9 |
Require Import Crypto.Util.ListUtil. |
| 10 |
Require Import Crypto.Util.Prod. |
| 11 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 12 |
Require Import Crypto.Util.Tactics.DestructHead. |
| 13 |
Require Import Crypto.Util.ZUtil.EquivModulo. |
| 14 |
Require Import Crypto.Util.ZUtil.Opp. |
| 15 |
Require Import Crypto.Util.ZUtil.Tactics.DivModToQuotRem. |
| 16 |
Require Import Crypto.Util.ZUtil.Tactics.PeelLe. |
| 17 |
Require Import Crypto.Util.ZUtil.Tactics.RewriteModSmall. |
| 18 |
Require Import Crypto.Util.ZUtil.Tactics.PullPush.Modulo. |
| 19 |
|
| 20 |
Require Import Crypto.Util.Notations. |
| 21 |
Local Open Scope Z_scope. |
| 22 |
|
| 23 |
(* TODO: rename this module? (Should it be, e.g., [Rows.freeze]?) *) |
| 24 |
Module Freeze. |
| 25 |
Section Freeze. |
| 26 |
Context weight {wprops : @weight_properties weight}. |
| 27 |
|
| 28 |
Definition freeze n mask (m p:list Z) : list Z := |
| 29 |
let '(p, carry) := Rows.sub weight n p m in |
| 30 |
let '(r, carry) := Rows.conditional_add weight n mask (-carry) p m in |
| 31 |
r. |
| 32 |
|
| 33 |
Lemma freezeZ m s c y : |
| 34 |
m = s - c -> |
| 35 |
0 < c < s -> |
| 36 |
s <> 0 -> |
| 37 |
0 <= y < 2*m -> |
| 38 |
((y - m) + (if (dec (-((y - m) / s) = 0)) then 0 else m)) mod s |
| 39 |
= y mod m. |
| 40 |
Proof using Type. |
| 41 |
clear; intros. |
| 42 |
transitivity ((y - m) mod m); |
| 43 |
repeat first [ progress intros |
| 44 |
| progress subst |
| 45 |
| rewrite Z.opp_eq_0_iff in * |
| 46 |
| break_innermost_match_step |
| 47 |
| progress autorewrite with zsimplify_fast |
| 48 |
| rewrite Z.div_small_iff in * by auto |
| 49 |
| progress (Z.rewrite_mod_small; push_Zmod; Z.rewrite_mod_small) |
| 50 |
| progress destruct_head'_or |
| 51 |
| lia ]. |
| 52 |
Qed. |
| 53 |
|
| 54 |
Lemma length_freeze n mask m p : |
| 55 |
length m = n -> length p = n -> length (freeze n mask m p) = n. |
| 56 |
Proof using wprops. |
| 57 |
cbv [freeze Rows.conditional_add Rows.add]; eta_expand; intros. |
| 58 |
distr_length; try assumption; cbn; intros; destruct_head'_or; destruct_head' False; subst; |
| 59 |
distr_length. |
| 60 |
erewrite Rows.length_sum_rows by (reflexivity || eassumption || distr_length); distr_length. |
| 61 |
Qed. |
| 62 |
Lemma eval_freeze_eq n mask m p |
| 63 |
(n_nonzero:n<>0%nat) |
| 64 |
(Hmask : List.map (Z.land mask) m = m) |
| 65 |
(Hplen : length p = n) |
| 66 |
(Hmlen : length m = n) |
| 67 |
: Positional.eval weight n (@freeze n mask m p) |
| 68 |
= (Positional.eval weight n p - Positional.eval weight n m + |
| 69 |
(if dec (-((Positional.eval weight n p - Positional.eval weight n m) / weight n) = 0) then 0 else Positional.eval weight n m)) |
| 70 |
mod weight n. |
| 71 |
(*if dec ((Positional.eval weight n p - Positional.eval weight n m) / weight n = 0) |
| 72 |
then Positional.eval weight n p - Positional.eval weight n m |
| 73 |
else Positional.eval weight n p mod weight n.*) |
| 74 |
Proof using wprops. |
| 75 |
pose proof (@weight_positive weight wprops n). |
| 76 |
cbv [freeze Z.equiv_modulo]; eta_expand. |
| 77 |
repeat first [ solve [auto] |
| 78 |
| rewrite Rows.conditional_add_partitions |
| 79 |
| rewrite Rows.sub_partitions |
| 80 |
| rewrite Rows.sub_div |
| 81 |
| rewrite eval_partition |
| 82 |
| progress distr_length |
| 83 |
| progress pull_Zmod (* |
| 84 |
| progress break_innermost_match_step |
| 85 |
| progress destruct_head'_or |
| 86 |
| lia |
| 87 |
| f_equal; lia |
| 88 |
| rewrite Z.div_small_iff in * by (auto using (@weight_positive weight ltac:(assumption))) |
| 89 |
| progress Z.rewrite_mod_small *) ]. |
| 90 |
Qed. |
| 91 |
|
| 92 |
Lemma eval_freeze n c mask m p |
| 93 |
(n_nonzero:n<>0%nat) |
| 94 |
(Hc : 0 < Associational.eval c < weight n) |
| 95 |
(Hmask : List.map (Z.land mask) m = m) |
| 96 |
(modulus:=weight n - Associational.eval c) |
| 97 |
(Hm : Positional.eval weight n m = modulus) |
| 98 |
(Hp : 0 <= Positional.eval weight n p < 2*modulus) |
| 99 |
(Hplen : length p = n) |
| 100 |
(Hmlen : length m = n) |
| 101 |
: Positional.eval weight n (@freeze n mask m p) |
| 102 |
= Positional.eval weight n p mod modulus. |
| 103 |
Proof using wprops. |
| 104 |
pose proof (@weight_positive weight wprops n). |
| 105 |
rewrite eval_freeze_eq by assumption. |
| 106 |
erewrite freezeZ; try eassumption; try lia. |
| 107 |
f_equal; lia. |
| 108 |
Qed. |
| 109 |
|
| 110 |
Lemma freeze_partitions n c mask m p |
| 111 |
(n_nonzero:n<>0%nat) |
| 112 |
(Hc : 0 < Associational.eval c < weight n) |
| 113 |
(Hmask : List.map (Z.land mask) m = m) |
| 114 |
(modulus:=weight n - Associational.eval c) |
| 115 |
(Hm : Positional.eval weight n m = modulus) |
| 116 |
(Hp : 0 <= Positional.eval weight n p < 2*modulus) |
| 117 |
(Hplen : length p = n) |
| 118 |
(Hmlen : length m = n) |
| 119 |
: @freeze n mask m p = Partition.partition weight n (Positional.eval weight n p mod modulus). |
| 120 |
Proof using wprops. |
| 121 |
pose proof (@weight_positive weight wprops n). |
| 122 |
pose proof (fun v => Z.mod_pos_bound v (weight n) ltac:(lia)). |
| 123 |
pose proof (Z.mod_pos_bound (Positional.eval weight n p) modulus ltac:(lia)). |
| 124 |
subst modulus. |
| 125 |
erewrite <- eval_freeze by eassumption. |
| 126 |
cbv [freeze]; eta_expand. |
| 127 |
rewrite Rows.conditional_add_partitions by (auto; rewrite Rows.sub_partitions; auto; distr_length). |
| 128 |
rewrite !eval_partition by assumption. |
| 129 |
apply Partition.partition_Proper; [ assumption .. | ]. |
| 130 |
cbv [Z.equiv_modulo]. |
| 131 |
pull_Zmod; reflexivity. |
| 132 |
Qed. |
| 133 |
End Freeze. |
| 134 |
End Freeze. |
| 135 |
#[global] |
| 136 |
Hint Rewrite Freeze.length_freeze : distr_length. |
| 137 |
|
| 138 |
Section freeze_mod_ops. |
| 139 |
Import Positional. |
| 140 |
Import Freeze. |
| 141 |
Local Coercion Z.of_nat : nat >-> Z. |
| 142 |
Local Coercion QArith_base.inject_Z : Z >-> Q. |
| 143 |
(* Design constraints: |
| 144 |
- inputs must be [Z] (b/c reification does not support Q) |
| 145 |
- internal structure must not match on the arguments (b/c reification does not support [positive]) *) |
| 146 |
Context (limbwidth_num limbwidth_den : Z) |
| 147 |
(limbwidth_good : 0 < limbwidth_den <= limbwidth_num) |
| 148 |
(s : Z) |
| 149 |
(c : list (Z*Z)) |
| 150 |
(n : nat) |
| 151 |
(bitwidth : Z) |
| 152 |
(m_enc : list Z) |
| 153 |
(m_nz:s - Associational.eval c <> 0) (s_nz:s <> 0) |
| 154 |
(Hn_nz : n <> 0%nat) |
| 155 |
(Hs_big : 1 < s). |
| 156 |
Local Notation bytes_weight := (@weight 8 1). |
| 157 |
Local Notation weight := (@weight limbwidth_num limbwidth_den). |
| 158 |
Let m := (s - Associational.eval c). |
| 159 |
|
| 160 |
Context (Hs : s = weight n). |
| 161 |
Context (c_small : 0 < Associational.eval c < weight n) |
| 162 |
(m_enc_bounded : List.map (BinInt.Z.land (Z.ones bitwidth)) m_enc = m_enc) |
| 163 |
(m_enc_correct : Positional.eval weight n m_enc = m) |
| 164 |
(Hm_enc_len : length m_enc = n). |
| 165 |
|
| 166 |
Definition wprops_bytes := (@wprops 8 1 ltac:(clear; lia)). |
| 167 |
Local Notation wprops := (@wprops limbwidth_num limbwidth_den limbwidth_good). |
| 168 |
Local Definition wprops' := wprops. |
| 169 |
|
| 170 |
Local Definition wunique := (@weight_unique limbwidth_num limbwidth_den limbwidth_good). |
| 171 |
Local Definition wunique_bytes := (@weight_unique 8 1 ltac:(clear; lia)). |
| 172 |
|
| 173 |
Local Definition weight_0_wprops := weight_0 wprops. |
| 174 |
Local Definition weight_positive_wprops := weight_positive wprops. |
| 175 |
Local Definition weight_multiples_wprops := weight_multiples wprops. |
| 176 |
Local Definition weight_divides_wprops := weight_divides wprops. |
| 177 |
Local Definition weight_0_wprops_bytes := weight_0 wprops_bytes. |
| 178 |
Local Definition weight_positive_wprops_bytes := weight_positive wprops_bytes. |
| 179 |
Local Definition weight_multiples_wprops_bytes := weight_multiples wprops_bytes. |
| 180 |
Local Definition weight_divides_wprops_bytes := weight_divides wprops_bytes. |
| 181 |
|
| 182 |
Local Hint Immediate wprops' : core. |
| 183 |
Local Hint Immediate wprops_bytes : core. |
| 184 |
Local Hint Immediate weight_0_wprops : core. |
| 185 |
Local Hint Immediate weight_positive_wprops : core. |
| 186 |
Local Hint Immediate weight_multiples_wprops : core. |
| 187 |
Local Hint Immediate weight_divides_wprops : core. |
| 188 |
Local Hint Immediate weight_0_wprops_bytes : core. |
| 189 |
Local Hint Immediate weight_positive_wprops_bytes : core. |
| 190 |
Local Hint Immediate weight_multiples_wprops_bytes : core. |
| 191 |
Local Hint Immediate weight_divides_wprops_bytes : core. |
| 192 |
Local Hint Immediate wunique wunique_bytes : core. |
| 193 |
Local Hint Resolve wunique wunique_bytes : core. |
| 194 |
|
| 195 |
Definition bytes_n |
| 196 |
:= Eval cbv [Qceiling Qdiv inject_Z Qfloor Qmult Qopp Qnum Qden Qinv Pos.mul] |
| 197 |
in Z.to_nat (Qceiling (Z.log2_up s / 8)). |
| 198 |
|
| 199 |
Lemma weight_bytes_weight_matches |
| 200 |
: weight n <= bytes_weight bytes_n. |
| 201 |
Proof using limbwidth_good Hs. |
| 202 |
clear -limbwidth_good Hs. |
| 203 |
cbv [weight bytes_n] in *; subst s. |
| 204 |
autorewrite with zsimplify_const. |
| 205 |
rewrite Z.log2_up_pow2, !Z2Nat.id, !Z.opp_involutive by (Z.div_mod_to_quot_rem; nia). |
| 206 |
Z.peel_le. |
| 207 |
Z.div_mod_to_quot_rem; nia. |
| 208 |
Qed. |
| 209 |
|
| 210 |
Definition to_bytes (v : list Z) |
| 211 |
:= BaseConversion.convert_bases weight bytes_weight n bytes_n v. |
| 212 |
|
| 213 |
Definition from_bytes (v : list Z) |
| 214 |
:= BaseConversion.convert_bases bytes_weight weight bytes_n n v. |
| 215 |
|
| 216 |
Definition freeze_to_bytesmod (f : list Z) : list Z |
| 217 |
:= to_bytes (freeze weight n (Z.ones bitwidth) m_enc f). |
| 218 |
|
| 219 |
Definition to_bytesmod (f : list Z) : list Z |
| 220 |
:= to_bytes f. |
| 221 |
|
| 222 |
Definition from_bytesmod (f : list Z) : list Z |
| 223 |
:= from_bytes f. |
| 224 |
|
| 225 |
Lemma bytes_pos : (0 < bytes_n)%nat. |
| 226 |
Proof using Hs_big. |
| 227 |
clear -Hs_big. |
| 228 |
cbv [bytes_n]. |
| 229 |
pose proof (Z.log2_up_pos s Hs_big). |
| 230 |
zify; rewrite ?Z2Nat.id in *. (* This line only needed in < 8.11 *) |
| 231 |
all: Z.div_mod_to_quot_rem; try nia. |
| 232 |
Qed. |
| 233 |
|
| 234 |
Lemma bytes_nz : bytes_n <> 0%nat. |
| 235 |
Proof using Hs_big. generalize bytes_pos; clear; lia. Qed. |
| 236 |
|
| 237 |
Lemma bytes_n_big : weight n <= bytes_weight bytes_n. |
| 238 |
Proof using limbwidth_good Hn_nz Hs. |
| 239 |
clear -limbwidth_good Hn_nz Hs. |
| 240 |
cbv [bytes_n bytes_weight] in *; subst s. |
| 241 |
Z.peel_le. |
| 242 |
rewrite Z.log2_up_pow2 by (Z.div_mod_to_quot_rem; nia). |
| 243 |
autorewrite with zsimplify_fast. |
| 244 |
rewrite Z2Nat.id by (Z.div_mod_to_quot_rem; nia). |
| 245 |
Z.div_mod_to_quot_rem; nia. |
| 246 |
Qed. |
| 247 |
|
| 248 |
Lemma eval_to_bytes |
| 249 |
: forall (f : list Z) |
| 250 |
(Hf : length f = n), |
| 251 |
eval bytes_weight bytes_n (to_bytes f) = eval weight n f. |
| 252 |
Proof using limbwidth_good Hs_big. |
| 253 |
generalize wprops wprops_bytes; clear -limbwidth_good Hs_big. |
| 254 |
intros. |
| 255 |
cbv [to_bytes]. |
| 256 |
rewrite BaseConversion.eval_convert_bases |
| 257 |
by (auto using bytes_nz; distr_length; auto using wprops). |
| 258 |
reflexivity. |
| 259 |
Qed. |
| 260 |
|
| 261 |
Lemma to_bytes_partitions |
| 262 |
: forall (f : list Z) |
| 263 |
(Hf : length f = n) |
| 264 |
(Hf_small : 0 <= eval weight n f < s), |
| 265 |
to_bytes f = Partition.partition bytes_weight bytes_n (Positional.eval weight n f). |
| 266 |
Proof using Hs_big limbwidth_good. |
| 267 |
clear -Hs_big limbwidth_good. |
| 268 |
intros; cbv [to_bytes]. |
| 269 |
pose proof bytes_nz. |
| 270 |
apply BaseConversion.convert_bases_partitions; eauto; try lia. |
| 271 |
split; [ | eapply Z.lt_le_trans ]; [ apply Hf_small.. | ]. |
| 272 |
cbv [bytes_weight bytes_n]. |
| 273 |
pose proof (Z.log2_up_pos s Hs_big). |
| 274 |
rewrite Z2Nat.id by (Z.div_mod_to_quot_rem; nia). |
| 275 |
autorewrite with zsimplify_fast. |
| 276 |
etransitivity; [ now apply Z.log2_up_spec | ]. |
| 277 |
Z.peel_le. |
| 278 |
Z.div_mod_to_quot_rem; nia. |
| 279 |
Qed. |
| 280 |
|
| 281 |
Lemma eval_to_bytesmod |
| 282 |
: forall (f : list Z) |
| 283 |
(Hf : length f = n) |
| 284 |
(Hf_small : 0 <= eval weight n f < s), |
| 285 |
eval bytes_weight bytes_n (to_bytesmod f) = eval weight n f |
| 286 |
/\ to_bytesmod f = Partition.partition bytes_weight bytes_n (Positional.eval weight n f). |
| 287 |
Proof using Hs_big limbwidth_good. |
| 288 |
split; apply eval_to_bytes || apply to_bytes_partitions; assumption. |
| 289 |
Qed. |
| 290 |
|
| 291 |
Lemma eval_freeze_to_bytesmod_and_partitions |
| 292 |
: forall (f : list Z) |
| 293 |
(Hf : length f = n) |
| 294 |
(Hf_bounded : 0 <= eval weight n f < 2 * m), |
| 295 |
(eval bytes_weight bytes_n (freeze_to_bytesmod f)) = (eval weight n f) mod m |
| 296 |
/\ freeze_to_bytesmod f = Partition.partition bytes_weight bytes_n (Positional.eval weight n f mod m). |
| 297 |
Proof using m_enc_correct Hs_big limbwidth_good Hn_nz Hs c_small Hm_enc_len m_enc_bounded. |
| 298 |
clear -m_enc_correct Hs_big limbwidth_good Hn_nz Hs c_small Hm_enc_len m_enc_bounded. |
| 299 |
pose proof eval_to_bytes as eval_to_bytes. |
| 300 |
pose proof to_bytes_partitions as to_bytes_partitions. |
| 301 |
intros; subst m; rewrite Hs in *. |
| 302 |
cbv [freeze_to_bytesmod]. |
| 303 |
rewrite eval_to_bytes, to_bytes_partitions; |
| 304 |
erewrite ?eval_freeze by eauto using wprops; |
| 305 |
autorewrite with distr_length; eauto. |
| 306 |
Z.div_mod_to_quot_rem; nia. |
| 307 |
Qed. |
| 308 |
|
| 309 |
Lemma eval_freeze_to_bytesmod |
| 310 |
: forall (f : list Z) |
| 311 |
(Hf : length f = n) |
| 312 |
(Hf_bounded : 0 <= eval weight n f < 2 * m), |
| 313 |
(eval bytes_weight bytes_n (freeze_to_bytesmod f)) = (eval weight n f) mod m. |
| 314 |
Proof using m_enc_correct Hs_big limbwidth_good Hn_nz Hs c_small Hm_enc_len m_enc_bounded. |
| 315 |
intros; now apply eval_freeze_to_bytesmod_and_partitions. |
| 316 |
Qed. |
| 317 |
|
| 318 |
Lemma freeze_to_bytesmod_partitions |
| 319 |
: forall (f : list Z) |
| 320 |
(Hf : length f = n) |
| 321 |
(Hf_bounded : 0 <= eval weight n f < 2 * m), |
| 322 |
freeze_to_bytesmod f = Partition.partition bytes_weight bytes_n (Positional.eval weight n f mod m). |
| 323 |
Proof using m_enc_correct Hs_big limbwidth_good Hn_nz Hs c_small Hm_enc_len m_enc_bounded. |
| 324 |
intros; now apply eval_freeze_to_bytesmod_and_partitions. |
| 325 |
Qed. |
| 326 |
|
| 327 |
Lemma eval_from_bytes |
| 328 |
: forall (f : list Z) |
| 329 |
(Hf : length f = bytes_n), |
| 330 |
eval weight n (from_bytes f) = eval bytes_weight bytes_n f. |
| 331 |
Proof using limbwidth_good Hs_big Hn_nz. |
| 332 |
generalize wprops wprops_bytes; clear -Hs_big limbwidth_good Hn_nz. |
| 333 |
intros. |
| 334 |
cbv [from_bytes]. |
| 335 |
rewrite BaseConversion.eval_convert_bases |
| 336 |
by (auto using bytes_nz; distr_length; auto using wprops). |
| 337 |
reflexivity. |
| 338 |
Qed. |
| 339 |
|
| 340 |
Lemma from_bytes_partitions |
| 341 |
: forall (f : list Z) |
| 342 |
(Hf_small : 0 <= eval bytes_weight bytes_n f < weight n), |
| 343 |
from_bytes f = Partition.partition weight n (Positional.eval bytes_weight bytes_n f). |
| 344 |
Proof using limbwidth_good Hn_nz. |
| 345 |
clear -limbwidth_good Hn_nz. |
| 346 |
intros; cbv [from_bytes]. |
| 347 |
apply BaseConversion.convert_bases_partitions; eauto; lia. |
| 348 |
Qed. |
| 349 |
|
| 350 |
Lemma eval_from_bytesmod |
| 351 |
: forall (f : list Z) |
| 352 |
(Hf : length f = bytes_n), |
| 353 |
eval weight n (from_bytesmod f) = eval bytes_weight bytes_n f. |
| 354 |
Proof using Hn_nz Hs_big limbwidth_good. apply eval_from_bytes. Qed. |
| 355 |
|
| 356 |
Lemma from_bytesmod_partitions |
| 357 |
: forall (f : list Z) |
| 358 |
(Hf_small : 0 <= eval bytes_weight bytes_n f < weight n), |
| 359 |
from_bytesmod f = Partition.partition weight n (Positional.eval bytes_weight bytes_n f). |
| 360 |
Proof using limbwidth_good Hn_nz. apply from_bytes_partitions. Qed. |
| 361 |
|
| 362 |
Lemma eval_from_bytesmod_and_partitions |
| 363 |
: forall (f : list Z) |
| 364 |
(Hf : length f = bytes_n) |
| 365 |
(Hf_small : 0 <= eval bytes_weight bytes_n f < weight n), |
| 366 |
eval weight n (from_bytesmod f) = eval bytes_weight bytes_n f |
| 367 |
/\ from_bytesmod f = Partition.partition weight n (Positional.eval bytes_weight bytes_n f). |
| 368 |
Proof using limbwidth_good Hn_nz Hs_big. |
| 369 |
now (split; [ apply eval_from_bytesmod | apply from_bytes_partitions ]). |
| 370 |
Qed. |
| 371 |
End freeze_mod_ops. |
| 372 |
#[global] |
| 373 |
Hint Rewrite eval_freeze_to_bytesmod eval_to_bytes eval_to_bytesmod eval_from_bytes eval_from_bytesmod : push_eval. |