| 1 |
From Coq Require Import ZArith. |
| 2 |
Require Import Crypto.Util.ListUtil Coq.Lists.List Crypto.Util.ListUtil.FoldBool. |
| 3 |
Require Import Crypto.Util.ZRange. |
| 4 |
Require Import Crypto.Util.ZRange.Operations. |
| 5 |
Require Import Crypto.Util.ZUtil.Definitions. |
| 6 |
Require Import Crypto.Util.ZUtil.Notations. |
| 7 |
Require Import Crypto.Util.ZRange. |
| 8 |
Require Import Crypto.Util.ZRange.Operations. |
| 9 |
Require Import Crypto.Language.PreExtra. |
| 10 |
Require Import Crypto.Util.LetIn. |
| 11 |
Require Import Crypto.Util.Notations. |
| 12 |
Import ListNotations. Local Open Scope bool_scope. Local Open Scope Z_scope. |
| 13 |
|
| 14 |
Local Definition mymap {A B} := Eval cbv in @List.map A B. |
| 15 |
Local Definition myapp {A} := Eval cbv in @List.app A. |
| 16 |
Local Definition myflatten {A} := Eval cbv in List.fold_right myapp (@nil A). |
| 17 |
Local Notation dont_do_again := (pair false) (only parsing). |
| 18 |
Local Notation do_again := (pair true) (only parsing). |
| 19 |
|
| 20 |
Local Notation "' x" := (ident.literal x). |
| 21 |
Local Notation cstZ r := (ident.cast ('r%zrange)). |
| 22 |
Local Notation cstZZ r1 r2 := (ident.cast2 ('(r1%zrange), '(r2%zrange))). |
| 23 |
Local Notation "'plet' x := y 'in' z" |
| 24 |
:= (match y return _ with x => z end). |
| 25 |
|
| 26 |
Local Notation dlet2_opp2 rv rc e |
| 27 |
:= (plet rc' := (-rc)%zrange in |
| 28 |
plet cst' := cstZZ rv rc' in |
| 29 |
plet cst1 := cstZ rv in |
| 30 |
plet cst2 := cstZ rc in |
| 31 |
plet cst2' := cstZ rc' in |
| 32 |
(dlet vc := cst' e in |
| 33 |
(cst1 (fst (cst' vc)), cst2 (-(cst2' (snd (cst' vc))))))). |
| 34 |
|
| 35 |
Local Notation dlet2 rv rc e |
| 36 |
:= (dlet vc := cstZZ rv rc e in |
| 37 |
(cstZ rv (fst (cstZZ rv rc vc)), |
| 38 |
cstZ rc (snd (cstZZ rv rc vc)))). |
| 39 |
|
| 40 |
|
| 41 |
Local Notation "x '\in' y" := (is_bounded_by_bool x (ZRange.normalize y) = true) : zrange_scope. |
| 42 |
Local Notation "x ∈ y" := (is_bounded_by_bool x (ZRange.normalize y) = true) : zrange_scope. |
| 43 |
Local Notation "x <= y" := (is_tighter_than_bool (ZRange.normalize x) y = true) : zrange_scope. |
| 44 |
Local Notation "x <= y <= z" := (andb (is_tighter_than_bool (ZRange.normalize x) y) (is_tighter_than_bool (ZRange.normalize y) z) = true) : zrange_scope. |
| 45 |
Local Notation litZZ x := (ident.literal (fst x), ident.literal (snd x)) (only parsing). |
| 46 |
Local Notation n r := (ZRange.normalize r) (only parsing). |
| 47 |
|
| 48 |
(* N.B. [ident.eagerly] does not play well with [do_again] *) |
| 49 |
Definition nbe_rewrite_rulesT : list (bool * Prop) |
| 50 |
:= Eval cbv [myapp mymap myflatten] in |
| 51 |
myflatten |
| 52 |
[mymap |
| 53 |
dont_do_again |
| 54 |
[(forall A B x y, @fst A B (x, y) = x) |
| 55 |
; (forall A B x y, @snd A B (x, y) = y) |
| 56 |
; (forall P t f, @Thunked.bool_rect P t f true = t tt) |
| 57 |
; (forall P t f, @Thunked.bool_rect P t f false = f tt) |
| 58 |
; (forall A B C f x y, @prod_rect A B (fun _ => C) f (x, y) = f x y) |
| 59 |
; (forall A B C f p, @prod_rect A B (fun _ : A * B => C) f p = dlet p := p in f (fst p) (snd p)) |
| 60 |
|
| 61 |
; (forall A x n, |
| 62 |
@List.repeat A x ('n) |
| 63 |
= ident.eagerly (@nat_rect) _ nil (fun k repeat_k => x :: repeat_k) ('n)) |
| 64 |
; (forall A xs ys, |
| 65 |
xs ++ ys |
| 66 |
= ident.eagerly (@list_rect) A _ ys (fun x xs app_xs_ys => x :: app_xs_ys) xs) |
| 67 |
; (forall A B f a ls, |
| 68 |
@fold_right A B f a ls |
| 69 |
= (ident.eagerly (@list_rect) _ _) |
| 70 |
a |
| 71 |
(fun x xs fold_right_xs => f x fold_right_xs) |
| 72 |
ls) |
| 73 |
; (forall A P N C ls, |
| 74 |
@Thunked.list_rect A P N C ls |
| 75 |
= ident.eagerly (@Thunked.list_rect) A P N C ls) |
| 76 |
; (forall A P Q N C ls v, |
| 77 |
@list_rect A (fun _ => P -> Q) N C ls v |
| 78 |
= ident.eagerly (@list_rect) A (fun _ => P -> Q) N C ls v) |
| 79 |
; (forall A P N C, @Thunked.list_case A P N C nil = N tt) |
| 80 |
; (forall A P N C x xs, @Thunked.list_case A P N C (x :: xs) = C x xs) |
| 81 |
; (forall A B f ls, |
| 82 |
@List.map A B f ls |
| 83 |
= (ident.eagerly (@list_rect) _ _) |
| 84 |
nil |
| 85 |
(fun x xs map_f_xs => f x :: map_f_xs) |
| 86 |
ls) |
| 87 |
; (forall P O_case S_case n, |
| 88 |
@Thunked.nat_rect P O_case S_case ('n) |
| 89 |
= (ident.eagerly (@Thunked.nat_rect) _) |
| 90 |
O_case |
| 91 |
S_case |
| 92 |
('n)) |
| 93 |
; (forall P Q O_case S_case n v, |
| 94 |
@nat_rect (fun _ => P -> Q) O_case S_case ('n) v |
| 95 |
= (ident.eagerly (@nat_rect) _) |
| 96 |
O_case |
| 97 |
S_case |
| 98 |
('n) |
| 99 |
v) |
| 100 |
; (forall A default ls n, |
| 101 |
@List.nth_default A default ls ('n) |
| 102 |
= ident.eagerly (@List.nth_default) _ default ls ('n)) |
| 103 |
] |
| 104 |
; mymap |
| 105 |
do_again |
| 106 |
[(forall A B xs ys, |
| 107 |
@List.combine A B xs ys |
| 108 |
= (list_rect _) |
| 109 |
(fun _ => nil) |
| 110 |
(fun x xs combine_xs ys |
| 111 |
=> match ys with |
| 112 |
| nil => nil |
| 113 |
| y :: ys => (x, y) :: combine_xs ys |
| 114 |
end) |
| 115 |
xs |
| 116 |
ys) |
| 117 |
; (forall A n ls, |
| 118 |
@List.firstn A ('n) ls |
| 119 |
= (nat_rect _) |
| 120 |
(fun _ => nil) |
| 121 |
(fun n' firstn_n' ls |
| 122 |
=> match ls with |
| 123 |
| nil => nil |
| 124 |
| cons x xs => x :: firstn_n' xs |
| 125 |
end) |
| 126 |
('n) |
| 127 |
ls) |
| 128 |
; (forall A n ls, |
| 129 |
@List.skipn A ('n) ls |
| 130 |
= (nat_rect _) |
| 131 |
(fun ls => ls) |
| 132 |
(fun n' skipn_n' ls |
| 133 |
=> match ls with |
| 134 |
| nil => nil |
| 135 |
| cons x xs => skipn_n' xs |
| 136 |
end) |
| 137 |
('n) |
| 138 |
ls) |
| 139 |
; (forall A xs, |
| 140 |
@List.length A xs |
| 141 |
= (list_rect _) |
| 142 |
0%nat |
| 143 |
(fun _ xs length_xs => S length_xs) |
| 144 |
xs) |
| 145 |
; (forall A xs, |
| 146 |
@List.rev A xs |
| 147 |
= (list_rect _) |
| 148 |
nil |
| 149 |
(fun x xs rev_xs => rev_xs ++ [x]) |
| 150 |
xs) |
| 151 |
; (forall A B f xs, |
| 152 |
@List.flat_map A B f xs |
| 153 |
= (list_rect _) |
| 154 |
nil |
| 155 |
(fun x _ flat_map_tl => f x ++ flat_map_tl) |
| 156 |
xs) |
| 157 |
; (forall A f xs, |
| 158 |
@List.partition A f xs |
| 159 |
= (list_rect _) |
| 160 |
([], []) |
| 161 |
(fun x tl partition_tl |
| 162 |
=> let '(g, d) := partition_tl in |
| 163 |
if f x then (x :: g, d) else (g, x :: d)) |
| 164 |
xs) |
| 165 |
; (forall A f xs, |
| 166 |
@List.filter A f xs |
| 167 |
= (list_rect _) |
| 168 |
nil |
| 169 |
(fun x xs' filter_xs' |
| 170 |
=> if f x then x :: filter_xs' else filter_xs') |
| 171 |
xs) |
| 172 |
; (forall A n f xs, |
| 173 |
@update_nth A ('n) f xs |
| 174 |
= (nat_rect _) |
| 175 |
(fun xs => match xs with |
| 176 |
| nil => nil |
| 177 |
| x' :: xs' => f x' :: xs' |
| 178 |
end) |
| 179 |
(fun n' update_nth_n' xs |
| 180 |
=> match xs with |
| 181 |
| nil => nil |
| 182 |
| x' :: xs' => x' :: update_nth_n' xs' |
| 183 |
end) |
| 184 |
('n) |
| 185 |
xs) |
| 186 |
] |
| 187 |
]. |
| 188 |
|
| 189 |
Definition unfold_value_barrier_rewrite_rulesT : list (bool * Prop) |
| 190 |
:= Eval cbv [myapp mymap myflatten] in |
| 191 |
myflatten |
| 192 |
[mymap |
| 193 |
dont_do_again |
| 194 |
[(forall x, Z.value_barrier x = x) |
| 195 |
] |
| 196 |
]. |
| 197 |
|
| 198 |
Definition arith_rewrite_rulesT (max_const_val : Z) : list (bool * Prop) |
| 199 |
:= Eval cbv [myapp mymap myflatten] in |
| 200 |
myflatten |
| 201 |
[mymap |
| 202 |
dont_do_again |
| 203 |
[(forall A B x y, @fst A B (x, y) = x) |
| 204 |
; (forall A B x y, @snd A B (x, y) = y) |
| 205 |
; (forall v, 0 + v = v) |
| 206 |
; (forall v, v + 0 = v) |
| 207 |
; (forall x y, (-x) + (-y) = -(x + y)) |
| 208 |
; (forall x y, (-x) + y = y - x) |
| 209 |
; (forall x y, x + (-y) = x - y) |
| 210 |
|
| 211 |
; (forall v, 0 - (-v) = v) |
| 212 |
; (forall v, 0 - v = -v) |
| 213 |
; (forall v, v - 0 = v) |
| 214 |
; (forall x y, (-x) - (-y) = y - x) |
| 215 |
; (forall x y, (-x) - y = -(x + y)) |
| 216 |
; (forall x y, x - (-y) = x + y) |
| 217 |
|
| 218 |
; (forall v, 0 * v = 0) |
| 219 |
; (forall v, v * 0 = 0) |
| 220 |
; (forall v, 1 * v = v) |
| 221 |
; (forall v, v * 1 = v) |
| 222 |
; (forall v, (-1) * (-v) = v) |
| 223 |
; (forall v, (-v) * (-1) = v) |
| 224 |
; (forall v, (-1) * v = -v) |
| 225 |
; (forall v, v * (-1) = -v) |
| 226 |
; (forall x y, (-x) * (-y) = x * y) |
| 227 |
; (forall x y, (-x) * y = -(x * y)) |
| 228 |
; (forall x y, x * (-y) = -(x * y)) |
| 229 |
|
| 230 |
; (forall x, x &' 0 = 0) |
| 231 |
|
| 232 |
; (forall x, x / 1 = x) |
| 233 |
; (forall x, x mod 1 = 0) |
| 234 |
|
| 235 |
; (forall v, -(-v) = v) |
| 236 |
|
| 237 |
; (forall v, Z.land v ('0) = '0) |
| 238 |
; (forall v, Z.land ('0) v = '0) |
| 239 |
; (forall v, Z.land v ('-1) = v) |
| 240 |
; (forall v, Z.land ('-1) v = v) |
| 241 |
|
| 242 |
; (forall v, Z.lor v ('0) = v) |
| 243 |
; (forall v, Z.lor ('0) v = v) |
| 244 |
; (forall v, Z.lor v ('-1) = '-1) |
| 245 |
; (forall v, Z.lor ('-1) v = '-1) |
| 246 |
|
| 247 |
; (forall z v, z > 0 -> 'z + (-v) = 'z - v) |
| 248 |
; (forall z v, z > 0 -> (-v) + 'z = 'z - v) |
| 249 |
; (forall z v, z < 0 -> 'z + (-v) = -('(-z) + v)) |
| 250 |
; (forall z v, z < 0 -> (-v) + 'z = -(v + '(-z))) |
| 251 |
|
| 252 |
; (forall z v, z > 0 -> 'z - (-v) = 'z + v) |
| 253 |
; (forall z v, z < 0 -> 'z - (-v) = v - '(-z)) |
| 254 |
; (forall z v, z < 0 -> 'z - v = -('(-z) + v)) |
| 255 |
; (forall z v, z > 0 -> (-v) - 'z = -(v + 'z)) |
| 256 |
; (forall z v, z < 0 -> (-v) - 'z = '(-z) - v) |
| 257 |
; (forall z v, z < 0 -> v - 'z = v + '(-z)) |
| 258 |
|
| 259 |
; (forall x y, 'x * 'y = '(x*y)) |
| 260 |
; (forall z v, z < 0 -> 'z * v = -('(-z) * v)) |
| 261 |
; (forall z v, z < 0 -> v * 'z = -(v * '(-z))) |
| 262 |
|
| 263 |
; (forall x y, y = 2^Z.log2 y -> y <> 2 -> x * 'y = x << '(Z.log2 y)) |
| 264 |
; (forall x y, y = 2^Z.log2 y -> y <> 2 -> 'y * x = x << '(Z.log2 y)) |
| 265 |
|
| 266 |
; (forall x y, y = 2^Z.log2 y -> x / 'y = x >> '(Z.log2 y)) |
| 267 |
; (forall x y, y = 2^Z.log2 y -> x mod 'y = x &' '(y-1)) |
| 268 |
|
| 269 |
(* We reassociate some multiplication of small constants *) |
| 270 |
; (forall c1 c2 x y, |
| 271 |
Z.abs c1 <= Z.abs max_const_val |
| 272 |
-> Z.abs c2 <= Z.abs max_const_val |
| 273 |
-> 'c1 * ('c2 * (x * y)) = (x * (y * ('c1 * 'c2)))) |
| 274 |
; (forall c1 c2 x y, |
| 275 |
Z.abs c1 <= Z.abs max_const_val |
| 276 |
-> Z.abs c2 <= Z.abs max_const_val |
| 277 |
-> 'c1 * (x * (y * 'c2)) = (x * (y * ('c1 * 'c2)))) |
| 278 |
; (forall c x y, |
| 279 |
Z.abs c <= Z.abs max_const_val |
| 280 |
-> 'c * (x * y) = x * (y * 'c)) |
| 281 |
; (forall c x, |
| 282 |
Z.abs c <= Z.abs max_const_val |
| 283 |
-> 'c * x = x * 'c) |
| 284 |
|
| 285 |
(* transform +- to + *) |
| 286 |
; (forall s y x, |
| 287 |
Z.add_get_carry_full s x (- y) |
| 288 |
= dlet vb := Z.sub_get_borrow_full s x y in (fst vb, - snd vb)) |
| 289 |
; (forall s y x, |
| 290 |
Z.add_get_carry_full s (- y) x |
| 291 |
= dlet vb := Z.sub_get_borrow_full s x y in (fst vb, - snd vb)) |
| 292 |
; (forall s y x, |
| 293 |
Z.add_with_get_carry_full s 0 x (- y) |
| 294 |
= dlet vb := Z.sub_get_borrow_full s x y in (fst vb, - snd vb)) |
| 295 |
; (forall s y x, |
| 296 |
Z.add_with_get_carry_full s 0 (- y) x |
| 297 |
= dlet vb := Z.sub_get_borrow_full s x y in (fst vb, - snd vb)) |
| 298 |
; (forall s c y x, |
| 299 |
Z.add_with_get_carry_full s (- c) (- y) x |
| 300 |
= dlet vb := Z.sub_with_get_borrow_full s c x y in (fst vb, - snd vb)) |
| 301 |
; (forall s c y x, |
| 302 |
Z.add_with_get_carry_full s (- c) x (- y) |
| 303 |
= dlet vb := Z.sub_with_get_borrow_full s c x y in (fst vb, - snd vb)) |
| 304 |
; (forall b x, (* inline negation when the rewriter wouldn't already inline it *) |
| 305 |
ident.gets_inlined b x = false |
| 306 |
-> -x = dlet v := x in -v) |
| 307 |
] |
| 308 |
]. |
| 309 |
|
| 310 |
Definition arith_with_casts_rewrite_rulesT (adc_no_carry_to_add : bool) : list (bool * Prop) |
| 311 |
:= Eval cbv [myapp mymap myflatten] in |
| 312 |
myflatten |
| 313 |
[mymap |
| 314 |
dont_do_again |
| 315 |
[(forall A B x y, @fst A B (x, y) = x) |
| 316 |
; (forall A B x y, @snd A B (x, y) = y) |
| 317 |
; (forall P t f, @Thunked.bool_rect P t f true = t tt) |
| 318 |
; (forall P t f, @Thunked.bool_rect P t f false = f tt) |
| 319 |
; (forall r v, lower r = upper r -> cstZ r v = cstZ r ('(lower r))) |
| 320 |
; (forall r0 v, 0 ∈ r0 -> cstZ r0 0 + v = v) |
| 321 |
; (forall r0 v, 0 ∈ r0 -> v + cstZ r0 0 = v) |
| 322 |
; (forall r0 v, 0 ∈ r0 -> v - cstZ r0 0 = v) |
| 323 |
; (forall r0 v, 0 ∈ r0 -> cstZ r0 0 - v = -v) |
| 324 |
; (forall r0 v, 0 ∈ r0 -> cstZ r0 0 << v = 0) |
| 325 |
; (forall r0 rnv rv v, |
| 326 |
(rv <= -n rnv)%zrange -> 0 ∈ r0 |
| 327 |
-> cstZ r0 0 - cstZ rnv (-(cstZ rv v)) = cstZ rv v) |
| 328 |
; (forall rnv rv v, |
| 329 |
(rv <= -n rnv)%zrange |
| 330 |
-> -(cstZ rnv (-(cstZ rv v))) = cstZ rv v) |
| 331 |
|
| 332 |
; (forall rland r0 rv v, |
| 333 |
0 ∈ rland -> 0 ∈ r0 |
| 334 |
-> cstZ rland (Z.land (cstZ rv v) (cstZ r0 ('0))) = cstZ r0 ('0)) |
| 335 |
; (forall rland r0 rv v, |
| 336 |
0 ∈ rland -> 0 ∈ r0 |
| 337 |
-> cstZ rland (Z.land (cstZ r0 ('0)) (cstZ rv v)) = cstZ r0 ('0)) |
| 338 |
; (forall rland rm1 rv v, |
| 339 |
(rv <= rland)%zrange -> -1 ∈ rm1 |
| 340 |
-> cstZ rland (Z.land (cstZ rv v) (cstZ rm1 ('-1))) = cstZ rv v) |
| 341 |
; (forall rland rm1 rv v, |
| 342 |
(rv <= rland)%zrange -> -1 ∈ rm1 |
| 343 |
-> cstZ rland (Z.land (cstZ rm1 ('-1)) (cstZ rv v)) = cstZ rv v) |
| 344 |
|
| 345 |
; (forall rlor r0 rv v, |
| 346 |
(rv <= rlor)%zrange -> 0 ∈ r0 |
| 347 |
-> cstZ rlor (Z.lor (cstZ rv v) (cstZ r0 ('0))) = cstZ rv v) |
| 348 |
; (forall rlor r0 rv v, |
| 349 |
(rv <= rlor)%zrange -> 0 ∈ r0 |
| 350 |
-> cstZ rlor (Z.lor (cstZ r0 ('0)) (cstZ rv v)) = cstZ rv v) |
| 351 |
; (forall rlor rm1 rv v, |
| 352 |
-1 ∈ rlor -> -1 ∈ rm1 |
| 353 |
-> cstZ rlor (Z.lor (cstZ rv v) (cstZ rm1 ('-1))) = cstZ rm1 ('-1)) |
| 354 |
; (forall rlor rm1 rv v, |
| 355 |
-1 ∈ rlor -> -1 ∈ rm1 |
| 356 |
-> cstZ rlor (Z.lor (cstZ rm1 ('-1)) (cstZ rv v)) = cstZ rm1 ('-1)) |
| 357 |
|
| 358 |
; (forall rx x ry y, upper (n rx) <= lower (n ry) -> (cstZ rx x <=? cstZ ry y)%Z = true) |
| 359 |
; (forall rx x ry y, upper (n ry) < lower (n rx) -> (cstZ rx x <=? cstZ ry y)%Z = false) |
| 360 |
; (forall rx x ry y, upper (n rx) < lower (n ry) -> (cstZ rx x <? cstZ ry y)%Z = true) |
| 361 |
; (forall rx x ry y, upper (n ry) <= lower (n rx) -> (cstZ rx x <? cstZ ry y)%Z = false) |
| 362 |
; (forall rx x ry y, upper (n ry) <= lower (n rx) -> (cstZ rx x >=? cstZ ry y)%Z = true) |
| 363 |
; (forall rx x ry y, upper (n rx) < lower (n ry) -> (cstZ rx x >=? cstZ ry y)%Z = false) |
| 364 |
; (forall rx x ry y, upper (n ry) < lower (n rx) -> (cstZ rx x >? cstZ ry y)%Z = true) |
| 365 |
; (forall rx x ry y, upper (n rx) <= lower (n ry) -> (cstZ rx x >? cstZ ry y)%Z = false) |
| 366 |
|
| 367 |
; (forall rx x ry y, upper (n rx) < lower (n ry) -> Z.ltz (cstZ rx x) (cstZ ry y) = 1) |
| 368 |
; (forall rx x ry y, upper (n ry) <= lower (n rx) -> Z.ltz (cstZ rx x) (cstZ ry y) = 0) |
| 369 |
; (forall rx x rc c, c ∈ rc -> upper (n rx) < c -> Z.ltz (cstZ rx x) (cstZ rc ('c)) = 1) |
| 370 |
; (forall rx x rc c, c ∈ rc -> c <= lower (n rx) -> Z.ltz (cstZ rx x) (cstZ rc ('c)) = 0) |
| 371 |
; (forall rc c ry y, c ∈ rc -> c < lower (n ry) -> Z.ltz (cstZ rc ('c)) (cstZ ry y) = 1) |
| 372 |
; (forall rc c ry y, c ∈ rc -> upper (n ry) <= c -> Z.ltz (cstZ rc ('c)) (cstZ ry y) = 0) |
| 373 |
|
| 374 |
; (forall s r0 y, 0 ∈ r0 -> Z.mul_split s (cstZ r0 0) y = (cstZ r[0~>0] 0, cstZ r[0~>0] 0)) |
| 375 |
; (forall s r0 y, 0 ∈ r0 -> Z.mul_split s y (cstZ r0 0) = (cstZ r[0~>0] 0, cstZ r[0~>0] 0)) |
| 376 |
; (forall rs s r1 ry y, |
| 377 |
1 ∈ r1 -> s ∈ rs -> (ry <= r[0~>s-1])%zrange |
| 378 |
-> Z.mul_split (cstZ rs ('s)) (cstZ r1 1) (cstZ ry y) |
| 379 |
= (cstZ ry y, cstZ r[0~>0] 0)) |
| 380 |
; (forall rs s r1 ry y, |
| 381 |
1 ∈ r1 -> s ∈ rs -> (ry <= r[0~>s-1])%zrange |
| 382 |
-> Z.mul_split (cstZ rs ('s)) (cstZ ry y) (cstZ r1 1) |
| 383 |
= (cstZ ry y, cstZ r[0~>0] 0)) |
| 384 |
] |
| 385 |
; mymap |
| 386 |
dont_do_again |
| 387 |
[ (** remove unnecessary masks *) |
| 388 |
(forall rv rx x ry y, |
| 389 |
(rx <= rv)%zrange -> |
| 390 |
(rx <= r[0~>y])%zrange -> |
| 391 |
y ∈ ry -> y = Z.ones (Z.succ (Z.log2 y)) |
| 392 |
-> cstZ rv (cstZ rx x &' cstZ ry ('y)) = cstZ rx x); |
| 393 |
(forall rv rx x ry y, |
| 394 |
(rx <= rv)%zrange -> |
| 395 |
(rx <= r[0~>y])%zrange -> |
| 396 |
y ∈ ry -> y = Z.ones (Z.succ (Z.log2 y)) |
| 397 |
-> cstZ rv (cstZ ry ('y) &' cstZ rx x) = cstZ rx x) |
| 398 |
]%Z%zrange |
| 399 |
; mymap |
| 400 |
do_again |
| 401 |
[ (* [do_again], so that we can trigger add/sub rules on the output *) |
| 402 |
(** flatten add/sub which incurs no carry/borrow *) |
| 403 |
(forall rv rs s rx x ry y, |
| 404 |
adc_no_carry_to_add = true -> s ∈ rs -> (n rx + n ry <= r[0~>s-1])%zrange |
| 405 |
-> cstZZ rv r[0~>0] (Z.add_get_carry_full (cstZ rs ('s)) (cstZ rx x) (cstZ ry y)) |
| 406 |
= (cstZ rv (cstZ rx x + cstZ ry y), cstZ r[0~>0] ('0))) |
| 407 |
; (forall rv rs s rc c rx x ry y, |
| 408 |
adc_no_carry_to_add = true -> s ∈ rs -> (n rc + n rx + n ry <= r[0~>s-1])%zrange |
| 409 |
-> cstZZ rv r[0~>0] (Z.add_with_get_carry_full (cstZ rs ('s)) (cstZ rc c) (cstZ rx x) (cstZ ry y)) |
| 410 |
= (cstZ rv (cstZ rc c + cstZ rx x + cstZ ry y), cstZ r[0~>0] ('0))) |
| 411 |
; (forall rv rs s rx x ry y, |
| 412 |
adc_no_carry_to_add = true -> s ∈ rs -> (n rx - n ry <= r[0~>s-1])%zrange |
| 413 |
-> cstZZ rv r[0~>0] (Z.sub_get_borrow_full (cstZ rs ('s)) (cstZ rx x) (cstZ ry y)) |
| 414 |
= (cstZ rv (cstZ rx x - cstZ ry y), (cstZ r[0~>0] ('0)))) |
| 415 |
; (forall rv rs s rc c rx x ry y, |
| 416 |
adc_no_carry_to_add = true -> s ∈ rs -> (n rx - n ry - n rc <= r[0~>s-1])%zrange |
| 417 |
-> cstZZ rv r[0~>0] (Z.sub_with_get_borrow_full (cstZ rs ('s)) (cstZ rc c) (cstZ rx x) (cstZ ry y)) |
| 418 |
= (cstZ rv (cstZ rx x - cstZ ry y - cstZ rc c), (cstZ r[0~>0] ('0)))) |
| 419 |
]%Z%zrange |
| 420 |
; mymap |
| 421 |
dont_do_again |
| 422 |
[(forall rv rc s rny ry y x, |
| 423 |
(ry <= -n rny)%zrange |
| 424 |
-> cstZZ rv rc (Z.add_get_carry_full s (cstZ rny (-cstZ ry y)) x) |
| 425 |
= dlet2_opp2 rv rc (Z.sub_get_borrow_full s x (cstZ ry y))) |
| 426 |
; (forall rv rc s rny ry y x, |
| 427 |
(ry <= -n rny)%zrange |
| 428 |
-> cstZZ rv rc (Z.add_get_carry_full s x (cstZ rny (-cstZ ry y))) |
| 429 |
= dlet2_opp2 rv rc (Z.sub_get_borrow_full s x (cstZ ry y))) |
| 430 |
; (forall rv rc s ryy yy x, |
| 431 |
yy ∈ ryy -> yy < 0 |
| 432 |
-> cstZZ rv rc (Z.add_get_carry_full s (cstZ ryy ('yy)) x) |
| 433 |
= dlet2_opp2 rv rc (Z.sub_get_borrow_full s x (cstZ (-ryy) ('(-yy))))) |
| 434 |
; (forall rv rc s ryy yy x, |
| 435 |
yy ∈ ryy -> yy < 0 |
| 436 |
-> cstZZ rv rc (Z.add_get_carry_full s x (cstZ ryy ('yy))) |
| 437 |
= dlet2_opp2 rv rc (Z.sub_get_borrow_full s x (cstZ (-ryy) ('(-yy))))) |
| 438 |
; (forall rv rc s rnc rc' c' rny ry y x, |
| 439 |
(ry <= -n rny)%zrange -> (rc' <= -n rnc)%zrange |
| 440 |
-> cstZZ rv rc (Z.add_with_get_carry_full s (cstZ rnc (-cstZ rc' c')) (cstZ rny (-cstZ ry y)) x) |
| 441 |
= dlet2_opp2 rv rc (Z.sub_with_get_borrow_full s (cstZ rc' c') x (cstZ ry y))) |
| 442 |
; (forall rv rc s rnc rc' c' rny ry y x, |
| 443 |
(ry <= -n rny)%zrange -> (rc' <= -n rnc)%zrange |
| 444 |
-> cstZZ rv rc (Z.add_with_get_carry_full s (cstZ rnc (-cstZ rc' c')) x (cstZ rny (-cstZ ry y))) |
| 445 |
= dlet2_opp2 rv rc (Z.sub_with_get_borrow_full s (cstZ rc' c') x (cstZ ry y))) |
| 446 |
; (forall rv rc s r0 rny ry y x, |
| 447 |
0 ∈ r0 -> (ry <= -n rny)%zrange |
| 448 |
-> cstZZ rv rc (Z.add_with_get_carry_full s (cstZ r0 0) (cstZ rny (-cstZ ry y)) x) |
| 449 |
= dlet2_opp2 rv rc (Z.sub_get_borrow_full s x (cstZ ry y))) |
| 450 |
; (forall rv rc s rcc cc rny ry y x, |
| 451 |
cc < 0 -> cc ∈ rcc -> (ry <= -n rny)%zrange |
| 452 |
-> cstZZ rv rc (Z.add_with_get_carry_full s (cstZ rcc ('cc)) (cstZ rny (-cstZ ry y)) x) |
| 453 |
= dlet2_opp2 rv rc (Z.sub_with_get_borrow_full s (cstZ (-rcc) ('(-cc))) x (cstZ ry y))) |
| 454 |
; (forall rv rc s r0 rny ry y x, |
| 455 |
0 ∈ r0 -> (ry <= -n rny)%zrange |
| 456 |
-> cstZZ rv rc (Z.add_with_get_carry_full s (cstZ r0 0) x (cstZ rny (-cstZ ry y))) |
| 457 |
= dlet2_opp2 rv rc (Z.sub_get_borrow_full s x (cstZ ry y))) |
| 458 |
; (forall rv rc s rcc cc rny ry y x, |
| 459 |
cc < 0 -> cc ∈ rcc -> (ry <= -n rny)%zrange |
| 460 |
-> cstZZ rv rc (Z.add_with_get_carry_full s (cstZ rcc ('cc)) x (cstZ rny (-cstZ ry y))) |
| 461 |
= dlet2_opp2 rv rc (Z.sub_with_get_borrow_full s (cstZ (-rcc) ('(-cc))) x (cstZ ry y))) |
| 462 |
; (forall rv rc s rnc rc' c' ryy yy x, |
| 463 |
yy <= 0 -> yy ∈ ryy -> (rc' <= -n rnc)%zrange |
| 464 |
-> cstZZ rv rc (Z.add_with_get_carry_full s (cstZ rnc (-cstZ rc' c')) (cstZ ryy ('yy)) x) |
| 465 |
= dlet2_opp2 rv rc (Z.sub_with_get_borrow_full s (cstZ rc' c') x (cstZ (-ryy) ('(-yy))))) |
| 466 |
; (forall rv rc s rnc rc' c' ryy yy x, |
| 467 |
yy <= 0 -> yy ∈ ryy -> (rc' <= -n rnc)%zrange |
| 468 |
-> cstZZ rv rc (Z.add_with_get_carry_full s (cstZ rnc (-cstZ rc' c')) x (cstZ ryy ('yy))) |
| 469 |
= dlet2_opp2 rv rc (Z.sub_with_get_borrow_full s (cstZ rc' c') x (cstZ (-ryy) ('(-yy))))) |
| 470 |
; (forall rv rc s rcc cc ryy yy x, |
| 471 |
yy <= 0 -> cc <= 0 -> yy + cc < 0 (* at least one must be strictly negative *) -> yy ∈ ryy -> cc ∈ rcc |
| 472 |
-> cstZZ rv rc (Z.add_with_get_carry_full s (cstZ rcc ('cc)) (cstZ ryy ('yy)) x) |
| 473 |
= dlet2_opp2 rv rc (Z.sub_with_get_borrow_full s (cstZ (-rcc) ('(-cc))) x (cstZ (-ryy) ('(-yy))))) |
| 474 |
; (forall rv rc s rcc cc ryy yy x, |
| 475 |
yy <= 0 -> cc <= 0 -> yy + cc < 0 (* at least one must be strictly negative *) -> yy ∈ ryy -> cc ∈ rcc |
| 476 |
-> cstZZ rv rc (Z.add_with_get_carry_full s (cstZ rcc ('cc)) x (cstZ ryy ('yy))) |
| 477 |
= dlet2_opp2 rv rc (Z.sub_with_get_borrow_full s (cstZ (-rcc) ('(-cc))) x (cstZ (-ryy) ('(-yy))))) |
| 478 |
|
| 479 |
|
| 480 |
; (forall rs s rxx xx ryy yy, |
| 481 |
s ∈ rs -> xx ∈ rxx -> yy ∈ ryy |
| 482 |
-> Z.add_get_carry_full (cstZ rs ('s)) (cstZ rxx ('xx)) (cstZ ryy ('yy)) |
| 483 |
= litZZ (Z.add_get_carry_full s xx yy)) |
| 484 |
; (forall rs s r0 ry y, |
| 485 |
s ∈ rs -> 0 ∈ r0 -> (ry <= r[0~>s-1])%zrange |
| 486 |
-> Z.add_get_carry_full (cstZ rs ('s)) (cstZ r0 0) (cstZ ry y) |
| 487 |
= (cstZ ry y, cstZ r[0~>0] 0)) |
| 488 |
; (forall rs s r0 ry y, |
| 489 |
s ∈ rs -> 0 ∈ r0 -> (ry <= r[0~>s-1])%zrange |
| 490 |
-> Z.add_get_carry_full (cstZ rs ('s)) (cstZ ry y) (cstZ r0 0) |
| 491 |
= (cstZ ry y, cstZ r[0~>0] 0)) |
| 492 |
|
| 493 |
; (forall r0 x y, 0 ∈ r0 -> Z.add_with_carry (cstZ r0 0) x y = x + y) |
| 494 |
|
| 495 |
; (forall rs s rcc cc rxx xx ryy yy, |
| 496 |
s ∈ rs -> cc ∈ rcc -> xx ∈ rxx -> yy ∈ ryy |
| 497 |
-> Z.add_with_get_carry_full (cstZ rs ('s)) (cstZ rcc ('cc)) (cstZ rxx ('xx)) (cstZ ryy ('yy)) |
| 498 |
= litZZ (Z.add_with_get_carry_full s cc xx yy)) |
| 499 |
; (forall rs s r0c r0x ry y, |
| 500 |
s ∈ rs -> 0 ∈ r0c -> 0 ∈ r0x -> (ry <= r[0~>s-1])%zrange |
| 501 |
-> Z.add_with_get_carry_full (cstZ rs ('s)) (cstZ r0c 0) (cstZ r0x 0) (cstZ ry y) |
| 502 |
= (cstZ ry y, cstZ r[0~>0] 0)) |
| 503 |
; (forall rs s r0c r0x ry y, |
| 504 |
s ∈ rs -> 0 ∈ r0c -> 0 ∈ r0x -> (ry <= r[0~>s-1])%zrange |
| 505 |
-> Z.add_with_get_carry_full (cstZ rs ('s)) (cstZ r0c 0) (cstZ ry y) (cstZ r0x 0) |
| 506 |
= (cstZ ry y, cstZ r[0~>0] 0)) |
| 507 |
|
| 508 |
; (forall rv rc s r0 x y, (* carry = 0: ADC x y -> ADD x y *) |
| 509 |
0 ∈ r0 |
| 510 |
-> cstZZ rv rc (Z.add_with_get_carry_full s (cstZ r0 0) x y) |
| 511 |
= dlet2 rv rc (Z.add_get_carry_full s x y)) |
| 512 |
; (forall rv rc rs s rc' c' r0x r0y, (* ADC 0 0 -> (ADX 0 0, 0) *) (* except we don't do ADX, because C stringification doesn't handle it *) |
| 513 |
0 ∈ r0x -> 0 ∈ r0y -> (rc' <= r[0~>s-1])%zrange -> 0 ∈ rc -> s ∈ rs |
| 514 |
-> cstZZ rv rc (Z.add_with_get_carry_full (cstZ rs ('s)) (cstZ rc' c') (cstZ r0x 0) (cstZ r0y 0)) |
| 515 |
= (dlet vc := (cstZZ rv rc (Z.add_with_get_carry_full (cstZ rs ('s)) (cstZ rc' c') (cstZ r0x 0) (cstZ r0y 0))) in |
| 516 |
(cstZ rv (fst (cstZZ rv rc vc)), |
| 517 |
cstZ r[0~>0] 0))) |
| 518 |
|
| 519 |
(* let-bind any adc/sbb/mulx *) |
| 520 |
; (forall rv rc s c x y, |
| 521 |
cstZZ rv rc (Z.add_with_get_carry_full s c x y) |
| 522 |
= dlet2 rv rc (Z.add_with_get_carry_full s c x y)) |
| 523 |
; (forall rv c x y, |
| 524 |
cstZ rv (Z.add_with_carry c x y) |
| 525 |
= (dlet vc := cstZ rv (Z.add_with_carry c x y) in |
| 526 |
cstZ rv vc)) |
| 527 |
; (forall rv rc s x y, |
| 528 |
cstZZ rv rc (Z.add_get_carry_full s x y) |
| 529 |
= dlet2 rv rc (Z.add_get_carry_full s x y)) |
| 530 |
; (forall rv rc s c x y, |
| 531 |
cstZZ rv rc (Z.sub_with_get_borrow_full s c x y) |
| 532 |
= dlet2 rv rc (Z.sub_with_get_borrow_full s c x y)) |
| 533 |
; (forall rv rc s x y, |
| 534 |
cstZZ rv rc (Z.sub_get_borrow_full s x y) |
| 535 |
= dlet2 rv rc (Z.sub_get_borrow_full s x y)) |
| 536 |
; (forall rv rc s x y, |
| 537 |
cstZZ rv rc (Z.mul_split s x y) |
| 538 |
= dlet2 rv rc (Z.mul_split s x y)) |
| 539 |
] |
| 540 |
; mymap |
| 541 |
do_again |
| 542 |
[ (* [do_again], so that if one of the arguments is concrete, we automatically get the rewrite rule for [Z_cast] applying to it *) |
| 543 |
(forall rx ry x y, cstZZ rx ry (x, y) = (cstZ rx x, cstZ ry y)) |
| 544 |
] |
| 545 |
; mymap |
| 546 |
dont_do_again |
| 547 |
[(forall r1 r2 x, (r2 <= n r1)%zrange -> cstZ r1 (cstZ r2 x) = cstZ r2 x) |
| 548 |
] |
| 549 |
]%Z%zrange. |
| 550 |
|
| 551 |
Definition arith_with_relaxed_casts_rewrite_rulesT : list (bool * Prop) |
| 552 |
:= Eval cbv [myapp mymap myflatten] in |
| 553 |
myflatten |
| 554 |
[mymap |
| 555 |
dont_do_again |
| 556 |
[(forall rland rm1 rv v, |
| 557 |
rland.(upper) ∈ rm1 |
| 558 |
-> rland.(upper) = Z.ones (Z.succ (Z.log2 rland.(upper))) |
| 559 |
-> 0 = rland.(lower) |
| 560 |
-> 0 = rv.(lower) |
| 561 |
-> 0 <= rv.(upper) |
| 562 |
-> (rv.(upper) + 1) mod (rland.(upper) + 1) = 0 |
| 563 |
-> cstZ rland (Z.land (cstZ rv v) (cstZ rm1 ('rland.(upper)))) = cstZ rland v) |
| 564 |
; (forall rland rm1 rv v, |
| 565 |
rland.(upper) ∈ rm1 |
| 566 |
-> rland.(upper) = Z.ones (Z.succ (Z.log2 rland.(upper))) |
| 567 |
-> 0 = rland.(lower) |
| 568 |
-> 0 = rv.(lower) |
| 569 |
-> 0 <= rv.(upper) |
| 570 |
-> (rv.(upper) + 1) mod (rland.(upper) + 1) = 0 |
| 571 |
-> cstZ rland (Z.land (cstZ rm1 ('rland.(upper))) (cstZ rv v)) = cstZ rland v) |
| 572 |
] |
| 573 |
]%Z%zrange. |
| 574 |
|
| 575 |
Definition strip_literal_casts_rewrite_rulesT : list (bool * Prop) |
| 576 |
:= [dont_do_again (forall rx x, x ∈ rx -> cstZ rx ('x) = 'x)]%Z%zrange. |
| 577 |
|
| 578 |
Definition add_assoc_left_rewrite_rulesT : list (bool * Prop) |
| 579 |
:= Eval cbv [myapp mymap myflatten] in |
| 580 |
myflatten |
| 581 |
[mymap |
| 582 |
dont_do_again |
| 583 |
[(forall x y z, x + (y + z) = (x + y) + z) |
| 584 |
; (forall x y z, x + (y + z) = (x + y) + z)%nat |
| 585 |
] |
| 586 |
]. |
| 587 |
|
| 588 |
Definition flatten_thunked_rects_rewrite_rulesT : list (bool * Prop) |
| 589 |
:= [dont_do_again (forall P t f b, @Thunked.bool_rect P t f b = @bool_rect_nodep P (t tt) (f tt) b)]. |
| 590 |
|
| 591 |
Definition relax_bitwidth_adc_sbb_rewrite_rulesT (which_bitwidths : list Z) : list (bool * Prop) |
| 592 |
:= Eval cbv [myapp mymap myflatten] in |
| 593 |
mymap |
| 594 |
dont_do_again |
| 595 |
[(forall rv rc' rs s rc c rx x ry y, |
| 596 |
(((ZRange.normalize rc + ZRange.normalize rx + ZRange.normalize ry) / ZRange.normalize rs) <= rc' <= rv)%zrange -> |
| 597 |
List.existsb (Z.eqb (Z.log2 s)) which_bitwidths = true -> |
| 598 |
cstZZ rv rc' (Z.add_with_get_carry_full (cstZ rs ('s)) (cstZ rc c) (cstZ rx x) (cstZ ry y)) |
| 599 |
= cstZZ rv rv (Z.add_with_get_carry_full (cstZ rs ('s)) (cstZ rc c) (cstZ rx x) (cstZ ry y))) |
| 600 |
; (forall rv rc' rs s rx x ry y, |
| 601 |
(((ZRange.normalize rx + ZRange.normalize ry) / ZRange.normalize rs) <= rc' <= rv)%zrange -> |
| 602 |
List.existsb (Z.eqb (Z.log2 s)) which_bitwidths = true -> |
| 603 |
cstZZ rv rc' (Z.add_get_carry_full (cstZ rs ('s)) (cstZ rx x) (cstZ ry y)) |
| 604 |
= cstZZ rv rv (Z.add_get_carry_full (cstZ rs ('s)) (cstZ rx x) (cstZ ry y))) |
| 605 |
; (forall rv rc' rs s rc c rx x ry y, |
| 606 |
(- ((ZRange.normalize rx - ZRange.normalize ry - ZRange.normalize rc) / ZRange.normalize rs) <= rc' <= rv)%zrange -> |
| 607 |
List.existsb (Z.eqb (Z.log2 s)) which_bitwidths = true -> |
| 608 |
cstZZ rv rc' (Z.sub_with_get_borrow_full (cstZ rs ('s)) (cstZ rc c) (cstZ rx x) (cstZ ry y)) |
| 609 |
= cstZZ rv rv (Z.sub_with_get_borrow_full (cstZ rs ('s)) (cstZ rc c) (cstZ rx x) (cstZ ry y))) |
| 610 |
; (forall rv rc' rs s rx x ry y, |
| 611 |
(- ((ZRange.normalize rx - ZRange.normalize ry) / ZRange.normalize rs) <= rc' <= rv)%zrange -> |
| 612 |
List.existsb (Z.eqb (Z.log2 s)) which_bitwidths = true -> |
| 613 |
cstZZ rv rc' (Z.sub_get_borrow_full (cstZ rs ('s)) (cstZ rx x) (cstZ ry y)) |
| 614 |
= cstZZ rv rv (Z.sub_get_borrow_full (cstZ rs ('s)) (cstZ rx x) (cstZ ry y))) |
| 615 |
]. |
| 616 |
|
| 617 |
Section fancy. |
| 618 |
Context (invert_low invert_high : Z (*log2wordmax*) -> Z -> option Z) |
| 619 |
(value_range flag_range : zrange). |
| 620 |
|
| 621 |
Definition fancy_rewrite_rulesT : list (bool * Prop) |
| 622 |
:= []. |
| 623 |
|
| 624 |
Local Coercion ZRange.constant : Z >-> zrange. (* for ease of use with sanity-checking bounds *) |
| 625 |
Local Notation bounds1_good f |
| 626 |
:= (fun (output x_bs : zrange) |
| 627 |
=> is_tighter_than_bool (f (ZRange.normalize x_bs)) (ZRange.normalize output) = true). |
| 628 |
Local Notation bounds2_good f |
| 629 |
:= (fun (output x_bs y_bs : zrange) |
| 630 |
=> is_tighter_than_bool (f (ZRange.normalize x_bs) (ZRange.normalize y_bs)) (ZRange.normalize output) = true). |
| 631 |
Local Notation range_in_bitwidth r s |
| 632 |
:= (is_tighter_than_bool (ZRange.normalize r) r[0~>s-1]%zrange = true). |
| 633 |
Local Notation shiftl_good := (bounds2_good ZRange.shiftl). |
| 634 |
Local Notation shiftr_good := (bounds2_good ZRange.shiftr). |
| 635 |
Local Notation land_good := (bounds2_good ZRange.land). |
| 636 |
Local Notation mul_good := (bounds2_good ZRange.mul). |
| 637 |
Local Notation cc_m_good output s := (bounds1_good (ZRange.cc_m s) output). |
| 638 |
Local Notation lit_good x rx := (is_bounded_by_bool x (ZRange.normalize rx)). |
| 639 |
|
| 640 |
Definition fancy_with_casts_rewrite_rulesT : list (bool * Prop) |
| 641 |
:= Eval cbv [myapp mymap myflatten] in |
| 642 |
myflatten |
| 643 |
[mymap |
| 644 |
dont_do_again |
| 645 |
[(* |
| 646 |
(Z.add_get_carry_concrete 2^256) @@@ (?x, ?y << 128) --> (add 128) @@@ (x, y) |
| 647 |
(Z.add_get_carry_concrete 2^256) @@@ (?x << 128, ?y) --> (add 128) @@@ (y, x) |
| 648 |
(Z.add_get_carry_concrete 2^256) @@@ (?x, ?y >> 128) --> (add (- 128)) @@@ (x, y) |
| 649 |
(Z.add_get_carry_concrete 2^256) @@@ (?x >> 128, ?y) --> (add (- 128)) @@@ (y, x) |
| 650 |
(Z.add_get_carry_concrete 2^256) @@@ (?x, ?y) --> (add 0) @@@ (y, x) |
| 651 |
*) |
| 652 |
(forall r1 r2 rs s rx x rshiftl rland ry y rmask mask roffset offset, |
| 653 |
s = 2^Z.log2 s -> s ∈ rs -> offset ∈ roffset -> mask ∈ rmask -> shiftl_good rshiftl rland offset -> land_good rland ry mask -> range_in_bitwidth rshiftl s -> (mask = Z.ones (Z.log2 s - offset)) -> (0 <= offset <= Z.log2 s) |
| 654 |
-> cstZZ r1 r2 (Z.add_get_carry_full (cstZ rs ('s)) (cstZ rx x) (cstZ rshiftl ((cstZ rland (cstZ ry y &' cstZ rmask ('mask))) << cstZ roffset ('offset)))) |
| 655 |
= cstZZ r1 r2 (ident.fancy.add (('(Z.log2 s), 'offset), (cstZ rx x, cstZ ry y)))) |
| 656 |
; (forall r1 r2 rs s rx x rshiftl rland ry y rmask mask roffset offset, |
| 657 |
(s = 2^Z.log2 s) -> (mask = Z.ones (Z.log2 s - offset)) -> (0 <= offset <= Z.log2 s) -> s ∈ rs -> mask ∈ rmask -> offset ∈ roffset -> shiftl_good rshiftl rland offset -> land_good rland ry mask -> range_in_bitwidth rshiftl s |
| 658 |
-> cstZZ r1 r2 (Z.add_get_carry_full (cstZ rs ('s)) (cstZ rx x) (cstZ rshiftl (cstZ rland (cstZ ry y &' cstZ rmask ('mask)) << cstZ roffset ('offset)))) |
| 659 |
= cstZZ r1 r2 (ident.fancy.add (('(Z.log2 s), 'offset), (cstZ rx x, cstZ ry y)))) |
| 660 |
|
| 661 |
; (forall r1 r2 rs s rshiftl rland ry y rmask mask roffset offset rx x, |
| 662 |
s ∈ rs -> mask ∈ rmask -> offset ∈ roffset -> (s = 2^Z.log2 s) -> shiftl_good rshiftl rland offset -> land_good rland ry mask -> range_in_bitwidth rshiftl s -> (mask = Z.ones (Z.log2 s - offset)) -> (0 <= offset <= Z.log2 s) |
| 663 |
-> cstZZ r1 r2 (Z.add_get_carry_full (cstZ rs ('s)) (cstZ rshiftl (Z.shiftl (cstZ rland (Z.land (cstZ ry y) (cstZ rmask ('mask)))) (cstZ roffset ('offset)))) (cstZ rx x)) |
| 664 |
= cstZZ r1 r2 (ident.fancy.add (('(Z.log2 s), 'offset), (cstZ rx x, cstZ ry y)))) |
| 665 |
|
| 666 |
; (forall r1 r2 rs s rx x rshiftr ry y roffset offset, |
| 667 |
s ∈ rs -> offset ∈ roffset -> (s = 2^Z.log2 s) -> shiftr_good rshiftr ry offset -> range_in_bitwidth rshiftr s |
| 668 |
-> cstZZ r1 r2 (Z.add_get_carry_full (cstZ rs ('s)) (cstZ rx x) (cstZ rshiftr (Z.shiftr (cstZ ry y) (cstZ roffset ('offset))))) |
| 669 |
= cstZZ r1 r2 (ident.fancy.add (('(Z.log2 s), '(-offset)), (cstZ rx x, cstZ ry y)))%core) |
| 670 |
|
| 671 |
; (forall r1 r2 rs s rshiftr ry y roffset offset rx x, |
| 672 |
s ∈ rs -> offset ∈ roffset -> (s = 2^Z.log2 s) -> shiftr_good rshiftr ry offset -> range_in_bitwidth rshiftr s |
| 673 |
-> cstZZ r1 r2 (Z.add_get_carry_full (cstZ rs ('s)) (cstZ rshiftr (Z.shiftr (cstZ ry y) (cstZ roffset ('offset)))) (cstZ rx x)) |
| 674 |
= cstZZ r1 r2 (ident.fancy.add (('(Z.log2 s), '(-offset)), (cstZ rx x, cstZ ry y))%core)) |
| 675 |
|
| 676 |
; (forall r1 r2 rs s rx x ry y, |
| 677 |
s ∈ rs -> (s = 2^Z.log2 s) -> range_in_bitwidth ry s |
| 678 |
-> cstZZ r1 r2 (Z.add_get_carry_full (cstZ rs ('s)) (cstZ rx x) (cstZ ry y)) |
| 679 |
= cstZZ r1 r2 (ident.fancy.add (('(Z.log2 s), '0), (cstZ rx x, cstZ ry y)))) |
| 680 |
|
| 681 |
(* |
| 682 |
(Z.add_with_get_carry_concrete 2^256) @@@ (?c, ?x, ?y << 128) --> (addc 128) @@@ (c, x, y) |
| 683 |
(Z.add_with_get_carry_concrete 2^256) @@@ (?c, ?x << 128, ?y) --> (addc 128) @@@ (c, y, x) |
| 684 |
(Z.add_with_get_carry_concrete 2^256) @@@ (?c, ?x, ?y >> 128) --> (addc (- 128)) @@@ (c, x, y) |
| 685 |
(Z.add_with_get_carry_concrete 2^256) @@@ (?c, ?x >> 128, ?y) --> (addc (- 128)) @@@ (c, y, x) |
| 686 |
(Z.add_with_get_carry_concrete 2^256) @@@ (?c, ?x, ?y) --> (addc 0) @@@ (c, y, x) |
| 687 |
*) |
| 688 |
; (forall r1 r2 rs s rc c rx x rshiftl rland ry y rmask mask roffset offset, |
| 689 |
s ∈ rs -> mask ∈ rmask -> offset ∈ roffset -> (s = 2^Z.log2 s) -> shiftl_good rshiftl rland offset -> land_good rland ry mask -> range_in_bitwidth rshiftl s -> (mask = Z.ones (Z.log2 s - offset)) -> (0 <= offset <= Z.log2 s) |
| 690 |
-> cstZZ r1 r2 (Z.add_with_get_carry_full (cstZ rs ('s)) (cstZ rc c) (cstZ rx x) (cstZ rshiftl (Z.shiftl (cstZ rland (Z.land (cstZ ry y) (cstZ rmask ('mask)))) (cstZ roffset ('offset))))) |
| 691 |
= cstZZ r1 r2 (ident.fancy.addc (('(Z.log2 s), 'offset), (cstZ rc c, cstZ rx x, cstZ ry y)))) |
| 692 |
|
| 693 |
; (forall r1 r2 rs s rc c rshiftl rland ry y rmask mask roffset offset rx x, |
| 694 |
s ∈ rs -> mask ∈ rmask -> offset ∈ roffset -> (s = 2^Z.log2 s) -> shiftl_good rshiftl rland offset -> range_in_bitwidth rshiftl s -> land_good rland ry mask -> (mask = Z.ones (Z.log2 s - offset)) -> (0 <= offset <= Z.log2 s) |
| 695 |
-> cstZZ r1 r2 (Z.add_with_get_carry_full (cstZ rs ('s)) (cstZ rc c) (cstZ rshiftl (Z.shiftl (cstZ rland (Z.land (cstZ ry y) (cstZ rmask ('mask)))) (cstZ roffset ('offset)))) (cstZ rx x)) |
| 696 |
= cstZZ r1 r2 (ident.fancy.addc (('(Z.log2 s), 'offset), (cstZ rc c, cstZ rx x, cstZ ry y)))) |
| 697 |
|
| 698 |
; (forall r1 r2 rs s rc c rx x rshiftr ry y roffset offset, |
| 699 |
s ∈ rs -> offset ∈ roffset -> (s = 2^Z.log2 s) -> shiftr_good rshiftr ry offset -> range_in_bitwidth rshiftr s |
| 700 |
-> cstZZ r1 r2 (Z.add_with_get_carry_full (cstZ rs ('s)) (cstZ rc c) (cstZ rx x) (cstZ rshiftr (Z.shiftr (cstZ ry y) (cstZ roffset ('offset))))) |
| 701 |
= cstZZ r1 r2 (ident.fancy.addc (('(Z.log2 s), '(-offset)), (cstZ rc c, cstZ rx x, cstZ ry y))%core)) |
| 702 |
|
| 703 |
; (forall r1 r2 rs s rc c rshiftr ry y roffset offset rx x, |
| 704 |
s ∈ rs -> offset ∈ roffset -> (s = 2^Z.log2 s) -> shiftr_good rshiftr ry offset -> range_in_bitwidth rshiftr s |
| 705 |
-> cstZZ r1 r2 (Z.add_with_get_carry_full (cstZ rs ('s)) (cstZ rc c) (cstZ rshiftr (Z.shiftr (cstZ ry y) (cstZ roffset ('offset)))) (cstZ rx x)) |
| 706 |
= cstZZ r1 r2 (ident.fancy.addc (('(Z.log2 s), '(-offset)), (cstZ rc c, cstZ rx x, cstZ ry y))%core)) |
| 707 |
|
| 708 |
; (forall r1 r2 rs s rc c rx x ry y, |
| 709 |
s ∈ rs -> (s = 2^Z.log2 s) -> range_in_bitwidth ry s |
| 710 |
-> cstZZ r1 r2 (Z.add_with_get_carry_full (cstZ rs ('s)) (cstZ rc c) (cstZ rx x) (cstZ ry y)) |
| 711 |
= cstZZ r1 r2 (ident.fancy.addc (('(Z.log2 s), '0), (cstZ rc c, cstZ rx x, cstZ ry y)))) |
| 712 |
|
| 713 |
(* |
| 714 |
(Z.sub_get_borrow_concrete 2^256) @@@ (?x, ?y << 128) --> (sub 128) @@@ (x, y) |
| 715 |
(Z.sub_get_borrow_concrete 2^256) @@@ (?x, ?y >> 128) --> (sub (- 128)) @@@ (x, y) |
| 716 |
(Z.sub_get_borrow_concrete 2^256) @@@ (?x, ?y) --> (sub 0) @@@ (y, x) |
| 717 |
*) |
| 718 |
|
| 719 |
; (forall r1 r2 rs s rx x rshiftl rland ry y rmask mask roffset offset, |
| 720 |
s ∈ rs -> mask ∈ rmask -> offset ∈ roffset -> (s = 2^Z.log2 s) -> shiftl_good rshiftl rland offset -> range_in_bitwidth rshiftl s -> land_good rland ry mask -> (mask = Z.ones (Z.log2 s - offset)) -> (0 <= offset <= Z.log2 s) |
| 721 |
-> cstZZ r1 r2 (Z.sub_get_borrow_full (cstZ rs ('s)) (cstZ rx x) (cstZ rshiftl (Z.shiftl (cstZ rland (Z.land (cstZ ry y) (cstZ rmask ('mask)))) (cstZ roffset ('offset))))) |
| 722 |
= cstZZ r1 r2 (ident.fancy.sub (('(Z.log2 s), 'offset), (cstZ rx x, cstZ ry y)))) |
| 723 |
|
| 724 |
; (forall r1 r2 rs s rx x rshiftr ry y roffset offset, |
| 725 |
s ∈ rs -> offset ∈ roffset -> (s = 2^Z.log2 s) -> shiftr_good rshiftr ry offset -> range_in_bitwidth rshiftr s |
| 726 |
-> cstZZ r1 r2 (Z.sub_get_borrow_full (cstZ rs ('s)) (cstZ rx x) (cstZ rshiftr (Z.shiftr (cstZ ry y) (cstZ roffset ('offset))))) |
| 727 |
= cstZZ r1 r2 (ident.fancy.sub (('(Z.log2 s), '(-offset)), (cstZ rx x, cstZ ry y))%core)) |
| 728 |
|
| 729 |
; (forall r1 r2 rs s rx x ry y, |
| 730 |
s ∈ rs -> (s = 2^Z.log2 s) -> range_in_bitwidth ry s |
| 731 |
-> cstZZ r1 r2 (Z.sub_get_borrow_full (cstZ rs ('s)) (cstZ rx x) (cstZ ry y)) |
| 732 |
= cstZZ r1 r2 (ident.fancy.sub (('(Z.log2 s), '0), (cstZ rx x, cstZ ry y)))) |
| 733 |
|
| 734 |
(* |
| 735 |
(Z.sub_with_get_borrow_concrete 2^256) @@@ (?c, ?x, ?y << 128) --> (subb 128) @@@ (c, x, y) |
| 736 |
(Z.sub_with_get_borrow_concrete 2^256) @@@ (?c, ?x, ?y >> 128) --> (subb (- 128)) @@@ (c, x, y) |
| 737 |
(Z.sub_with_get_borrow_concrete 2^256) @@@ (?c, ?x, ?y) --> (subb 0) @@@ (c, y, x) |
| 738 |
*) |
| 739 |
|
| 740 |
; (forall r1 r2 rs s rb b rx x rshiftl rland ry y rmask mask roffset offset, |
| 741 |
s ∈ rs -> mask ∈ rmask -> offset ∈ roffset -> (s = 2^Z.log2 s) -> shiftl_good rshiftl rland offset -> range_in_bitwidth rshiftl s -> land_good rland ry mask -> (mask = Z.ones (Z.log2 s - offset)) -> (0 <= offset <= Z.log2 s) |
| 742 |
-> cstZZ r1 r2 (Z.sub_with_get_borrow_full (cstZ rs ('s)) (cstZ rb b) (cstZ rx x) (cstZ rshiftl (Z.shiftl (cstZ rland (Z.land (cstZ ry y) (cstZ rmask ('mask)))) (cstZ roffset ('offset))))) |
| 743 |
= cstZZ r1 r2 (ident.fancy.subb (('(Z.log2 s), 'offset), (cstZ rb b, cstZ rx x, cstZ ry y)))) |
| 744 |
|
| 745 |
; (forall r1 r2 rs s rb b rx x rshiftr ry y roffset offset, |
| 746 |
s ∈ rs -> offset ∈ roffset -> (s = 2^Z.log2 s) -> shiftr_good rshiftr ry offset -> range_in_bitwidth rshiftr s |
| 747 |
-> cstZZ r1 r2 (Z.sub_with_get_borrow_full (cstZ rs ('s)) (cstZ rb b) (cstZ rx x) (cstZ rshiftr (Z.shiftr (cstZ ry y) (cstZ roffset ('offset))))) |
| 748 |
= cstZZ r1 r2 (ident.fancy.subb (('(Z.log2 s), '(-offset)), (cstZ rb b, cstZ rx x, cstZ ry y))%core)) |
| 749 |
|
| 750 |
; (forall r1 r2 rs s rb b rx x ry y, |
| 751 |
s ∈ rs -> (s = 2^Z.log2 s) -> range_in_bitwidth ry s |
| 752 |
-> cstZZ r1 r2 (Z.sub_with_get_borrow_full (cstZ rs ('s)) (cstZ rb b) (cstZ rx x) (cstZ ry y)) |
| 753 |
= cstZZ r1 r2 (ident.fancy.subb (('(Z.log2 s), '0), (cstZ rb b, cstZ rx x, cstZ ry y)))) |
| 754 |
|
| 755 |
(*(Z.rshi_concrete 2^256 ?n) @@@ (?c, ?x, ?y) --> (rshi n) @@@ (x, y)*) |
| 756 |
|
| 757 |
; (forall r rs s rx x ry y rn n, |
| 758 |
s ∈ rs -> n ∈ rn -> (s = 2^Z.log2 s) |
| 759 |
-> cstZ r (Z.rshi (cstZ rs ('s)) (cstZ rx x) (cstZ ry y) (cstZ rn ('n))) |
| 760 |
= cstZ r (ident.fancy.rshi (('(Z.log2 s), 'n), (cstZ rx x, cstZ ry y)))) |
| 761 |
|
| 762 |
(* |
| 763 |
Z.zselect @@@ (Z.cc_m_concrete 2^256 ?c, ?x, ?y) --> selm @@@ (c, x, y) |
| 764 |
Z.zselect @@@ (?c &' 1, ?x, ?y) --> sell @@@ (c, x, y) |
| 765 |
Z.zselect @@@ (?c, ?x, ?y) --> selc @@@ (c, x, y) |
| 766 |
*) |
| 767 |
; (forall r rccm rs s rc c rx x ry y, |
| 768 |
s ∈ rs -> (s = 2^Z.log2 s) -> cc_m_good rccm s rc |
| 769 |
-> cstZ r (Z.zselect (cstZ rccm (Z.cc_m (cstZ rs ('s)) (cstZ rc c))) (cstZ rx x) (cstZ ry y)) |
| 770 |
= cstZ r (ident.fancy.selm ('(Z.log2 s), (cstZ rc c, cstZ rx x, cstZ ry y)))) |
| 771 |
|
| 772 |
; (forall r rland r1 rc c rx x ry y, |
| 773 |
1 ∈ r1 -> land_good rland 1 rc |
| 774 |
-> cstZ r (Z.zselect (cstZ rland (cstZ r1 1 &' cstZ rc c)) (cstZ rx x) (cstZ ry y)) |
| 775 |
= cstZ r (ident.fancy.sell (cstZ rc c, cstZ rx x, cstZ ry y))) |
| 776 |
|
| 777 |
; (forall r rland rc c r1 rx x ry y, |
| 778 |
1 ∈ r1 -> land_good rland rc 1 |
| 779 |
-> cstZ r (Z.zselect (cstZ rland (cstZ rc c &' cstZ r1 1)) (cstZ rx x) (cstZ ry y)) |
| 780 |
= cstZ r (ident.fancy.sell (cstZ rc c, cstZ rx x, cstZ ry y))) |
| 781 |
|
| 782 |
; (forall r c x y, |
| 783 |
cstZ r (Z.zselect c x y) |
| 784 |
= cstZ r (ident.fancy.selc (c, x, y))) |
| 785 |
|
| 786 |
(*Z.add_modulo @@@ (?x, ?y, ?m) --> addm @@@ (x, y, m)*) |
| 787 |
; (forall x y m, |
| 788 |
Z.add_modulo x y m |
| 789 |
= ident.fancy.addm (x, y, m)) |
| 790 |
|
| 791 |
(* |
| 792 |
Z.mul @@@ (?x &' (2^128-1), ?y &' (2^128-1)) --> mulll @@@ (x, y) |
| 793 |
Z.mul @@@ (?x &' (2^128-1), ?y >> 128) --> mullh @@@ (x, y) |
| 794 |
Z.mul @@@ (?x >> 128, ?y &' (2^128-1)) --> mulhl @@@ (x, y) |
| 795 |
Z.mul @@@ (?x >> 128, ?y >> 128) --> mulhh @@@ (x, y) |
| 796 |
*) |
| 797 |
(* literal on left *) |
| 798 |
; (forall r rx x rland ry y rmask mask, |
| 799 |
plet s := (2*Z.log2_up mask)%Z in |
| 800 |
plet xo := invert_low s x in |
| 801 |
plet xv := match xo with Some x => x | None => 0 end in |
| 802 |
xo <> None -> x ∈ rx -> mask ∈ rmask -> (mask = 2^(s/2)-1) -> land_good rland ry mask |
| 803 |
-> cstZ r (cstZ rx ('x) * cstZ rland (Z.land (cstZ ry y) (cstZ rmask ('mask)))) |
| 804 |
= cstZ r (ident.fancy.mulll (('s), ('xv, cstZ ry y)))) |
| 805 |
|
| 806 |
; (forall r rx x rland rmask mask ry y, |
| 807 |
plet s := (2*Z.log2_up mask)%Z in |
| 808 |
plet xo := invert_low s x in |
| 809 |
plet xv := match xo with Some x => x | None => 0 end in |
| 810 |
xo <> None -> x ∈ rx -> mask ∈ rmask -> (mask = 2^(s/2)-1) -> land_good rland mask ry |
| 811 |
-> cstZ r (cstZ rx ('x) * cstZ rland (Z.land (cstZ rmask ('mask)) (cstZ ry y))) |
| 812 |
= cstZ r (ident.fancy.mulll (('s), ('xv, cstZ ry y)))) |
| 813 |
|
| 814 |
; (forall r rx x rshiftr ry y roffset offset, |
| 815 |
plet s := (2*offset)%Z in |
| 816 |
plet xo := invert_low s x in |
| 817 |
plet xv := match xo with Some x => x | None => 0 end in |
| 818 |
xo <> None -> x ∈ rx -> offset ∈ roffset -> shiftr_good rshiftr ry offset |
| 819 |
-> cstZ r (cstZ rx ('x) * cstZ rshiftr (Z.shiftr (cstZ ry y) (cstZ roffset ('offset)))) |
| 820 |
= cstZ r (ident.fancy.mullh (('s), ('xv, cstZ ry y)))) |
| 821 |
|
| 822 |
; (forall r rx x rland rmask mask ry y, |
| 823 |
plet s := (2*Z.log2_up mask)%Z in |
| 824 |
plet xo := invert_high s x in |
| 825 |
plet xv := match xo with Some x => x | None => 0 end in |
| 826 |
xo <> None -> x ∈ rx -> mask ∈ rmask -> (mask = 2^(s/2)-1) -> land_good rland mask ry |
| 827 |
-> cstZ r (cstZ rx ('x) * cstZ rland (Z.land (cstZ rmask ('mask)) (cstZ ry y))) |
| 828 |
= cstZ r (ident.fancy.mulhl (('s), ('xv, cstZ ry y)))) |
| 829 |
|
| 830 |
; (forall r rx x rland ry y rmask mask, |
| 831 |
plet s := (2*Z.log2_up mask)%Z in |
| 832 |
plet xo := invert_high s x in |
| 833 |
plet xv := match xo with Some x => x | None => 0 end in |
| 834 |
xo <> None -> x ∈ rx -> mask ∈ rmask -> (mask = 2^(s/2)-1) -> land_good rland ry mask |
| 835 |
-> cstZ r (cstZ rx ('x) * cstZ rland (Z.land (cstZ ry y) (cstZ rmask ('mask)))) |
| 836 |
= cstZ r (ident.fancy.mulhl (('s), ('xv, cstZ ry y)))) |
| 837 |
|
| 838 |
; (forall r rx x rshiftr ry y roffset offset, |
| 839 |
plet s := (2*offset)%Z in |
| 840 |
plet xo := invert_high s x in |
| 841 |
plet xv := match xo with Some x => x | None => 0 end in |
| 842 |
xo <> None -> x ∈ rx -> offset ∈ roffset -> shiftr_good rshiftr ry offset |
| 843 |
-> cstZ r (cstZ rx ('x) * cstZ rshiftr (Z.shiftr (cstZ ry y) (cstZ roffset ('offset)))) |
| 844 |
= cstZ r (ident.fancy.mulhh (('s), ('xv, cstZ ry y)))) |
| 845 |
|
| 846 |
(* literal on right *) |
| 847 |
; (forall r rland rmask mask rx x ry y, |
| 848 |
plet s := (2*Z.log2_up mask)%Z in |
| 849 |
plet yo := invert_low s y in |
| 850 |
plet yv := match yo with Some y => y | None => 0 end in |
| 851 |
yo <> None -> y ∈ ry -> mask ∈ rmask -> (mask = 2^(s/2)-1) -> land_good rland mask rx |
| 852 |
-> cstZ r (cstZ rland (Z.land (cstZ rmask ('mask)) (cstZ rx x)) * cstZ ry ('y)) |
| 853 |
= cstZ r (ident.fancy.mulll (('s), (cstZ rx x, 'yv)))) |
| 854 |
|
| 855 |
; (forall r rland rx x rmask mask ry y, |
| 856 |
plet s := (2*Z.log2_up mask)%Z in |
| 857 |
plet yo := invert_low s y in |
| 858 |
plet yv := match yo with Some y => y | None => 0 end in |
| 859 |
yo <> None -> y ∈ ry -> mask ∈ rmask -> (mask = 2^(s/2)-1) -> land_good rland rx mask |
| 860 |
-> cstZ r (cstZ rland (Z.land (cstZ rx x) (cstZ rmask ('mask))) * cstZ ry ('y)) |
| 861 |
= cstZ r (ident.fancy.mulll (('s), (cstZ rx x, 'yv)))) |
| 862 |
|
| 863 |
; (forall r rland rmask mask rx x ry y, |
| 864 |
plet s := (2*Z.log2_up mask)%Z in |
| 865 |
plet yo := invert_high s y in |
| 866 |
plet yv := match yo with Some y => y | None => 0 end in |
| 867 |
yo <> None -> y ∈ ry -> mask ∈ rmask -> (mask = 2^(s/2)-1) -> land_good rland mask rx |
| 868 |
-> cstZ r (cstZ rland (Z.land (cstZ rmask ('mask)) (cstZ rx x)) * cstZ ry ('y)) |
| 869 |
= cstZ r (ident.fancy.mullh (('s), (cstZ rx x, 'yv)))) |
| 870 |
|
| 871 |
; (forall r rland rx x rmask mask ry y, |
| 872 |
plet s := (2*Z.log2_up mask)%Z in |
| 873 |
plet yo := invert_high s y in |
| 874 |
plet yv := match yo with Some y => y | None => 0 end in |
| 875 |
yo <> None -> y ∈ ry -> mask ∈ rmask -> (mask = 2^(s/2)-1) -> land_good rland rx mask |
| 876 |
-> cstZ r (cstZ rland (Z.land (cstZ rx x) (cstZ rmask ('mask))) * cstZ ry ('y)) |
| 877 |
= cstZ r (ident.fancy.mullh (('s), (cstZ rx x, 'yv)))) |
| 878 |
|
| 879 |
; (forall r rshiftr rx x roffset offset ry y, |
| 880 |
plet s := (2*offset)%Z in |
| 881 |
plet yo := invert_low s y in |
| 882 |
plet yv := match yo with Some y => y | None => 0 end in |
| 883 |
yo <> None -> y ∈ ry -> offset ∈ roffset -> shiftr_good rshiftr rx offset |
| 884 |
-> cstZ r (cstZ rshiftr (Z.shiftr (cstZ rx x) (cstZ roffset ('offset))) * cstZ ry ('y)) |
| 885 |
= cstZ r (ident.fancy.mulhl (('s), (cstZ rx x, 'yv)))) |
| 886 |
|
| 887 |
; (forall r rshiftr rx x roffset offset ry y, |
| 888 |
plet s := (2*offset)%Z in |
| 889 |
plet yo := invert_high s y in |
| 890 |
plet yv := match yo with Some y => y | None => 0 end in |
| 891 |
yo <> None -> y ∈ ry -> offset ∈ roffset -> shiftr_good rshiftr rx offset |
| 892 |
-> cstZ r (cstZ rshiftr (Z.shiftr (cstZ rx x) (cstZ roffset ('offset))) * cstZ ry ('y)) |
| 893 |
= cstZ r (ident.fancy.mulhh (('s), (cstZ rx x, 'yv)))) |
| 894 |
|
| 895 |
(* no literal *) |
| 896 |
; (forall r rland1 rmask1 mask1 rx x rland2 rmask2 mask2 ry y, |
| 897 |
plet s := (2*Z.log2_up mask1)%Z in |
| 898 |
mask1 ∈ rmask1 -> mask2 ∈ rmask2 -> (mask1 = 2^(s/2)-1) -> (mask2 = 2^(s/2)-1) -> land_good rland1 mask1 rx -> land_good rland2 mask2 ry |
| 899 |
-> cstZ r (cstZ rland1 (Z.land (cstZ rmask1 ('mask1)) (cstZ rx x)) * cstZ rland2 (Z.land (cstZ rmask2 ('mask2)) (cstZ ry y))) |
| 900 |
= cstZ r (ident.fancy.mulll (('s), (cstZ rx x, cstZ ry y)))) |
| 901 |
|
| 902 |
; (forall r rland1 rx x rmask1 mask1 rland2 rmask2 mask2 ry y, |
| 903 |
plet s := (2*Z.log2_up mask1)%Z in |
| 904 |
mask1 ∈ rmask1 -> mask2 ∈ rmask2 -> (mask1 = 2^(s/2)-1) -> (mask2 = 2^(s/2)-1) -> land_good rland1 rx mask1 -> land_good rland2 mask2 ry |
| 905 |
-> cstZ r (cstZ rland1 (Z.land (cstZ rx x) (cstZ rmask1 ('mask1))) * cstZ rland2 (Z.land (cstZ rmask2 ('mask2)) (cstZ ry y))) |
| 906 |
= cstZ r (ident.fancy.mulll (('s), (cstZ rx x, cstZ ry y)))) |
| 907 |
|
| 908 |
; (forall r rland1 rmask1 mask1 rx x rland2 ry y rmask2 mask2, |
| 909 |
plet s := (2*Z.log2_up mask1)%Z in |
| 910 |
mask1 ∈ rmask1 -> mask2 ∈ rmask2 -> (mask1 = 2^(s/2)-1) -> (mask2 = 2^(s/2)-1) -> land_good rland1 mask1 rx -> land_good rland2 ry mask2 |
| 911 |
-> cstZ r (cstZ rland1 (Z.land (cstZ rmask1 ('mask1)) (cstZ rx x)) * cstZ rland2 (Z.land (cstZ ry y) (cstZ rmask2 ('mask2)))) |
| 912 |
= cstZ r (ident.fancy.mulll (('s), (cstZ rx x, cstZ ry y)))) |
| 913 |
|
| 914 |
; (forall r rland1 rx x rmask1 mask1 rland2 ry y rmask2 mask2, |
| 915 |
plet s := (2*Z.log2_up mask1)%Z in |
| 916 |
mask1 ∈ rmask1 -> mask2 ∈ rmask2 -> (mask1 = 2^(s/2)-1) -> (mask2 = 2^(s/2)-1) -> land_good rland1 rx mask1 -> land_good rland2 ry mask2 |
| 917 |
-> cstZ r (cstZ rland1 (Z.land (cstZ rx x) (cstZ rmask1 ('mask1))) * cstZ rland2 (Z.land (cstZ ry y) (cstZ rmask2 ('mask2)))) |
| 918 |
= cstZ r (ident.fancy.mulll (('s), (cstZ rx x, cstZ ry y)))) |
| 919 |
|
| 920 |
; (forall r rland1 rmask mask rx x rshiftr2 ry y roffset offset, |
| 921 |
plet s := (2*offset)%Z in |
| 922 |
mask ∈ rmask -> offset ∈ roffset -> (mask = 2^(s/2)-1) -> land_good rland1 mask rx -> shiftr_good rshiftr2 ry offset |
| 923 |
-> cstZ r (cstZ rland1 (Z.land (cstZ rmask ('mask)) (cstZ rx x)) * cstZ rshiftr2 (Z.shiftr (cstZ ry y) (cstZ roffset ('offset)))) |
| 924 |
= cstZ r (ident.fancy.mullh (('s), (cstZ rx x, cstZ ry y)))) |
| 925 |
|
| 926 |
; (forall r rland1 rx x rmask mask rshiftr2 ry y roffset offset, |
| 927 |
plet s := (2*offset)%Z in |
| 928 |
mask ∈ rmask -> offset ∈ roffset -> (mask = 2^(s/2)-1) -> land_good rland1 rx mask -> shiftr_good rshiftr2 ry offset |
| 929 |
-> cstZ r (cstZ rland1 (Z.land (cstZ rx x) (cstZ rmask ('mask))) * cstZ rshiftr2 (Z.shiftr (cstZ ry y) (cstZ roffset ('offset)))) |
| 930 |
= cstZ r (ident.fancy.mullh (('s), (cstZ rx x, cstZ ry y)))) |
| 931 |
|
| 932 |
; (forall r rshiftr1 rx x roffset offset rland2 rmask mask ry y, |
| 933 |
plet s := (2*offset)%Z in |
| 934 |
mask ∈ rmask -> offset ∈ roffset -> (mask = 2^(s/2)-1) -> shiftr_good rshiftr1 rx offset -> land_good rland2 mask ry |
| 935 |
-> cstZ r (cstZ rshiftr1 (Z.shiftr (cstZ rx x) (cstZ roffset ('offset))) * cstZ rland2 (Z.land (cstZ rmask ('mask)) (cstZ ry y))) |
| 936 |
= cstZ r (ident.fancy.mulhl (('s), (cstZ rx x, cstZ ry y)))) |
| 937 |
|
| 938 |
; (forall r rshiftr1 rx x roffset offset rland2 ry y rmask mask, |
| 939 |
plet s := (2*offset)%Z in |
| 940 |
mask ∈ rmask -> offset ∈ roffset -> (mask = 2^(s/2)-1) -> shiftr_good rshiftr1 rx offset -> land_good rland2 ry mask |
| 941 |
-> cstZ r (cstZ rshiftr1 (Z.shiftr (cstZ rx x) (cstZ roffset ('offset))) * cstZ rland2 (Z.land (cstZ ry y) (cstZ rmask ('mask)))) |
| 942 |
= cstZ r (ident.fancy.mulhl (('s), (cstZ rx x, cstZ ry y)))) |
| 943 |
|
| 944 |
; (forall r rshiftr1 rx x roffset1 offset1 rshiftr2 ry y roffset2 offset2, |
| 945 |
plet s := (2*offset1)%Z in |
| 946 |
offset1 ∈ roffset1 -> offset2 ∈ roffset2 -> (offset1 = offset2) -> shiftr_good rshiftr1 rx offset1 -> shiftr_good rshiftr2 ry offset2 |
| 947 |
-> cstZ r (cstZ rshiftr1 (Z.shiftr (cstZ rx x) (cstZ roffset1 ('offset1))) * cstZ rshiftr2 (Z.shiftr (cstZ ry y) (cstZ roffset2 ('offset2)))) |
| 948 |
= cstZ r (ident.fancy.mulhh (('s), (cstZ rx x, cstZ ry y)))) |
| 949 |
|
| 950 |
(** Dummy rule to make sure we use the two value ranges; this can be removed *) |
| 951 |
; (forall rx x, |
| 952 |
((is_tighter_than_bool rx value_range = true) |
| 953 |
\/ (is_tighter_than_bool rx flag_range = true)) |
| 954 |
-> cstZ rx x = cstZ rx x) |
| 955 |
]%Z%zrange |
| 956 |
]. |
| 957 |
End fancy. |
| 958 |
|
| 959 |
Section with_bitwidth. |
| 960 |
Context (bitwidth : Z) |
| 961 |
(lgcarrymax : Z). |
| 962 |
|
| 963 |
Local Notation singlewidth_upperbound := (2^bitwidth - 1). |
| 964 |
Local Notation singlewidth_range := r[0~>singlewidth_upperbound]%zrange. |
| 965 |
Local Notation doublewidth := (cstZ r[0~>2^(2*bitwidth) - 1]). |
| 966 |
Local Notation singlewidth := (cstZ singlewidth_range). |
| 967 |
Local Notation carrymax := (2^lgcarrymax-1). |
| 968 |
Local Notation carrywidth := (cstZ r[0~>carrymax]). |
| 969 |
Local Notation singlewidth_carry := (cstZZ singlewidth_range r[0~>carrymax]). |
| 970 |
Local Notation alt_singlewidth_carry := (cstZZ singlewidth_range r[0~>2^bitwidth-1]). |
| 971 |
Local Notation pairsinglewidth := (cstZZ singlewidth_range singlewidth_range). |
| 972 |
Local Notation cstZsingle_to_double l h |
| 973 |
:= (doublewidth (Z.combine_at_bitwidth ('bitwidth) (singlewidth l) (singlewidth h))). |
| 974 |
Local Notation cstZsingle_to_double_pair lh |
| 975 |
:= (cstZsingle_to_double (fst lh) (snd lh)). |
| 976 |
Local Notation cstZ_pow2_bitwidth |
| 977 |
:= (cstZ r[2^bitwidth~>2^bitwidth] ('(2^bitwidth))). |
| 978 |
|
| 979 |
Definition mul_split_rewrite_rulesT : Datatypes.list (Datatypes.bool * Prop) |
| 980 |
:= Eval cbv [myapp mymap myflatten] in |
| 981 |
myflatten |
| 982 |
[mymap |
| 983 |
dont_do_again |
| 984 |
[(forall r x, cstZ r (cstZ r x) = cstZ r x) (* when inlining Z.combine_at_bitwidth, casts will sometimes get doubled up, so we need to strip the extra casts *) |
| 985 |
; (forall x y, |
| 986 |
0 <= bitwidth |
| 987 |
-> doublewidth (singlewidth x * singlewidth y) |
| 988 |
= (dlet lh := pairsinglewidth (Z.mul_split cstZ_pow2_bitwidth (singlewidth x) (singlewidth y)) in |
| 989 |
cstZsingle_to_double_pair lh)) |
| 990 |
; (forall xl xh y, |
| 991 |
0 <= bitwidth |
| 992 |
-> doublewidth (cstZsingle_to_double xl xh * singlewidth y) |
| 993 |
= (dlet lh1 := pairsinglewidth (Z.mul_split cstZ_pow2_bitwidth (singlewidth xl) (singlewidth y)) in |
| 994 |
dlet h2 := singlewidth (singlewidth xh * singlewidth y) in |
| 995 |
dlet h := singlewidth (singlewidth (snd lh1) + singlewidth h2) in |
| 996 |
cstZsingle_to_double (fst lh1) h)) |
| 997 |
; (forall x yl yh, |
| 998 |
0 <= bitwidth |
| 999 |
-> doublewidth (singlewidth x * cstZsingle_to_double yl yh) |
| 1000 |
= (dlet lh1 := pairsinglewidth (Z.mul_split cstZ_pow2_bitwidth (singlewidth x) (singlewidth yl)) in |
| 1001 |
dlet h2 := singlewidth (singlewidth x * singlewidth yh) in |
| 1002 |
dlet h := singlewidth (singlewidth (snd lh1) + singlewidth h2) in |
| 1003 |
cstZsingle_to_double (fst lh1) h)) |
| 1004 |
; (forall xl xh yl yh, |
| 1005 |
0 <= bitwidth |
| 1006 |
-> 1 <= lgcarrymax |
| 1007 |
-> doublewidth (cstZsingle_to_double xl xh + cstZsingle_to_double yl yh) |
| 1008 |
= (dlet lc := singlewidth_carry (Z.add_get_carry_full cstZ_pow2_bitwidth (singlewidth xl) (singlewidth yl)) in |
| 1009 |
dlet hc := singlewidth_carry (Z.add_with_get_carry_full cstZ_pow2_bitwidth (carrywidth (snd lc)) (singlewidth xh) (singlewidth yh)) in |
| 1010 |
cstZsingle_to_double (fst lc) (fst hc))) |
| 1011 |
; (forall x yl yh, |
| 1012 |
0 <= bitwidth |
| 1013 |
-> 1 <= lgcarrymax |
| 1014 |
-> doublewidth (singlewidth x + cstZsingle_to_double yl yh) |
| 1015 |
= (dlet lc := singlewidth_carry (Z.add_get_carry_full cstZ_pow2_bitwidth (singlewidth x) (singlewidth yl)) in |
| 1016 |
dlet h := singlewidth (Z.add(*_with_carry*) (carrywidth (snd lc)) (*(singlewidth ('0))*) (singlewidth yh)) in |
| 1017 |
cstZsingle_to_double (fst lc) h)) |
| 1018 |
; (forall xl xh y, |
| 1019 |
0 <= bitwidth |
| 1020 |
-> 1 <= lgcarrymax |
| 1021 |
-> doublewidth (cstZsingle_to_double xl xh + singlewidth y) |
| 1022 |
= (dlet lc := singlewidth_carry (Z.add_get_carry_full cstZ_pow2_bitwidth (singlewidth xl) (singlewidth y)) in |
| 1023 |
dlet h := singlewidth (Z.add(*_with_carry*) (carrywidth (snd lc)) (singlewidth xh) (*(singlewidth ('0))*)) in |
| 1024 |
cstZsingle_to_double (fst lc) h)) |
| 1025 |
; (forall x offset, |
| 1026 |
0 < offset < bitwidth |
| 1027 |
-> doublewidth (singlewidth x << singlewidth ('offset)) |
| 1028 |
= (dlet xl := singlewidth (Z.truncating_shiftl (singlewidth ('bitwidth)) (singlewidth x) (singlewidth ('offset))) in |
| 1029 |
dlet xh := singlewidth (singlewidth x >> singlewidth ('(bitwidth - offset))) in |
| 1030 |
cstZsingle_to_double xl xh)) |
| 1031 |
; (forall xl xh mask, |
| 1032 |
0 < mask < 2^bitwidth |
| 1033 |
-> singlewidth (cstZsingle_to_double xl xh &' singlewidth ('mask)) |
| 1034 |
= singlewidth (singlewidth xl &' singlewidth ('mask))) |
| 1035 |
; (forall xl xh mask, |
| 1036 |
0 < mask < 2^bitwidth |
| 1037 |
-> singlewidth (singlewidth ('mask) &' cstZsingle_to_double xl xh) |
| 1038 |
= singlewidth (singlewidth ('mask) &' singlewidth xl)) |
| 1039 |
; (forall xl xh offset, |
| 1040 |
0 < offset < bitwidth |
| 1041 |
-> singlewidth (cstZsingle_to_double xl xh >> singlewidth ('offset)) |
| 1042 |
= singlewidth (Z.lor (singlewidth (singlewidth xl >> singlewidth ('offset))) |
| 1043 |
(singlewidth |
| 1044 |
(Z.truncating_shiftl |
| 1045 |
(singlewidth ('bitwidth)) |
| 1046 |
(singlewidth xh) |
| 1047 |
(singlewidth ('(bitwidth - offset))))))) |
| 1048 |
; (forall xl xh offset, |
| 1049 |
0 < offset < bitwidth |
| 1050 |
-> doublewidth (cstZsingle_to_double xl xh >> singlewidth ('offset)) |
| 1051 |
= (dlet l := singlewidth |
| 1052 |
(Z.lor (singlewidth (singlewidth xl >> singlewidth ('offset))) |
| 1053 |
(singlewidth |
| 1054 |
(Z.truncating_shiftl |
| 1055 |
(singlewidth ('bitwidth)) |
| 1056 |
(singlewidth xh) |
| 1057 |
(singlewidth ('(bitwidth - offset)))))) in |
| 1058 |
dlet h := singlewidth |
| 1059 |
(singlewidth xh >> singlewidth ('offset)) in |
| 1060 |
cstZsingle_to_double l h)) |
| 1061 |
; (forall xl xh rbitwidth, |
| 1062 |
0 < bitwidth -> bitwidth ∈ rbitwidth |
| 1063 |
-> singlewidth (cstZsingle_to_double xl xh >> cstZ rbitwidth ('bitwidth)) |
| 1064 |
= singlewidth xh) |
| 1065 |
] |
| 1066 |
]%Z%zrange. |
| 1067 |
|
| 1068 |
Definition multiret_split_rewrite_rulesT : Datatypes.list (Datatypes.bool * Prop) |
| 1069 |
:= Eval cbv [myapp mymap myflatten] in |
| 1070 |
myflatten |
| 1071 |
[mymap dont_do_again [] |
| 1072 |
; mymap |
| 1073 |
do_again |
| 1074 |
[(forall A B x y, @fst A B (x, y) = x) |
| 1075 |
; (forall A B x y, @snd A B (x, y) = y) |
| 1076 |
(** In order to avoid tautological compares, we need to deal with carry/borrows being 0 *) |
| 1077 |
; (forall r0 s x y r1 r2, |
| 1078 |
0 ∈ r0 -> |
| 1079 |
cstZZ r1 r2 (Z.add_with_get_carry_full s (cstZ r0 ('0)) x y) |
| 1080 |
= cstZZ r1 r2 (Z.add_get_carry_full s x y)) |
| 1081 |
; (forall r0 s x y r1 r2, |
| 1082 |
0 ∈ r0 -> |
| 1083 |
cstZZ r1 r2 (Z.sub_with_get_borrow_full s (cstZ r0 ('0)) x y) |
| 1084 |
= cstZZ r1 r2 (Z.sub_get_borrow_full s x y)) |
| 1085 |
; (forall r0 s x y r1 r2, |
| 1086 |
0 ∈ r0 -> |
| 1087 |
cstZZ r1 r2 (Z.sub_with_get_borrow_full s y x (cstZ r0 ('0))) |
| 1088 |
= cstZZ r1 r2 (Z.sub_get_borrow_full s x y)) |
| 1089 |
] |
| 1090 |
; mymap |
| 1091 |
dont_do_again |
| 1092 |
[(forall A B x y, @fst A B (x, y) = x) |
| 1093 |
; (forall A B x y, @snd A B (x, y) = y) |
| 1094 |
; ((** At this point, we need zeros to be casted up to bitwidth range, rather than to 0-range, so we add a rule to widen the casts *) |
| 1095 |
forall c rc, |
| 1096 |
c ∈ rc -> c ∈ singlewidth_range -> rc <> singlewidth_range |
| 1097 |
-> cstZ rc ('c) = singlewidth ('c)) |
| 1098 |
; (forall x y rpow2_bitwidth, |
| 1099 |
0 <= lgcarrymax <= bitwidth -> 2^bitwidth ∈ rpow2_bitwidth |
| 1100 |
-> singlewidth_carry (Z.add_get_carry_full (cstZ rpow2_bitwidth ('(2^bitwidth))) (singlewidth x) (singlewidth y)) |
| 1101 |
= (dlet sum_xy := singlewidth (singlewidth x + singlewidth y) in |
| 1102 |
dlet carry_xy := carrywidth (Z.ltz (singlewidth sum_xy) (singlewidth x)) in |
| 1103 |
(singlewidth sum_xy, carrywidth carry_xy))) |
| 1104 |
; (forall c x y rpow2_bitwidth, |
| 1105 |
0 <= lgcarrymax <= bitwidth -> 2^bitwidth ∈ rpow2_bitwidth |
| 1106 |
-> singlewidth_carry (Z.add_with_get_carry_full (cstZ rpow2_bitwidth ('(2^bitwidth))) (carrywidth c) (singlewidth x) (singlewidth y)) |
| 1107 |
= (dlet sum_cx := singlewidth (carrywidth c + singlewidth x) in |
| 1108 |
dlet carry_cx := carrywidth (Z.ltz (singlewidth sum_cx) (singlewidth x)) in |
| 1109 |
dlet sum_cxy := singlewidth (singlewidth sum_cx + singlewidth y) in |
| 1110 |
dlet carry_cx_y := carrywidth (Z.ltz (singlewidth sum_cxy) (singlewidth y)) in |
| 1111 |
dlet carry_cxy := carrywidth (carrywidth carry_cx + carrywidth carry_cx_y) in |
| 1112 |
(singlewidth sum_cxy, carrywidth carry_cxy))) |
| 1113 |
|
| 1114 |
; (forall x y rpow2_bitwidth, |
| 1115 |
0 <= lgcarrymax <= bitwidth -> 2^bitwidth ∈ rpow2_bitwidth |
| 1116 |
-> singlewidth_carry (Z.sub_get_borrow_full (cstZ rpow2_bitwidth ('(2^bitwidth))) (singlewidth x) (singlewidth y)) |
| 1117 |
= (dlet diff_xy := singlewidth (singlewidth x - singlewidth y) in |
| 1118 |
dlet borrow_xy := carrywidth (Z.ltz (singlewidth x) (singlewidth diff_xy)) in |
| 1119 |
(singlewidth diff_xy, carrywidth borrow_xy))) |
| 1120 |
; (forall c x y rpow2_bitwidth, |
| 1121 |
0 <= lgcarrymax <= bitwidth -> 2^bitwidth ∈ rpow2_bitwidth |
| 1122 |
-> singlewidth_carry (Z.sub_with_get_borrow_full (cstZ rpow2_bitwidth ('(2^bitwidth))) (carrywidth c) (singlewidth x) (singlewidth y)) |
| 1123 |
= (dlet diff_xy := singlewidth (singlewidth x - singlewidth y) in |
| 1124 |
dlet borrow_xy := carrywidth (Z.ltz (singlewidth x) (singlewidth diff_xy)) in |
| 1125 |
dlet diff_xyc := singlewidth (singlewidth diff_xy - carrywidth c) in |
| 1126 |
dlet borrow_xy_c := carrywidth (Z.ltz (singlewidth diff_xy) (singlewidth diff_xyc)) in |
| 1127 |
dlet borrow_xyc := carrywidth (carrywidth borrow_xy + carrywidth borrow_xy_c) in |
| 1128 |
(singlewidth diff_xyc, carrywidth borrow_xyc))) |
| 1129 |
|
| 1130 |
; (forall x y rpow2_bitwidth, |
| 1131 |
0 <= lgcarrymax <= bitwidth -> 2^bitwidth ∈ rpow2_bitwidth |
| 1132 |
-> pairsinglewidth (Z.mul_split (cstZ rpow2_bitwidth ('(2^bitwidth))) (singlewidth x) (singlewidth y)) |
| 1133 |
= (dlet low := singlewidth (singlewidth x * singlewidth y) in |
| 1134 |
dlet high := singlewidth (Z.mul_high (cstZ rpow2_bitwidth ('(2^bitwidth))) (singlewidth x) (singlewidth y)) in |
| 1135 |
(singlewidth low, singlewidth high))) |
| 1136 |
; (forall x y two_pow_n r, |
| 1137 |
0 <= lgcarrymax <= bitwidth |
| 1138 |
-> two_pow_n = 2 ^ Z.log2 two_pow_n |
| 1139 |
-> 0 < Z.log2 two_pow_n < bitwidth |
| 1140 |
-> bitwidth < Z.log2 two_pow_n + lgcarrymax |
| 1141 |
-> two_pow_n ∈ r |
| 1142 |
-> singlewidth_carry |
| 1143 |
(Z.add_get_carry_full (cstZ r ('two_pow_n)) (singlewidth x) (singlewidth y)) |
| 1144 |
= (dlet sum_xy := singlewidth (singlewidth x + singlewidth y) in |
| 1145 |
dlet carry_xy := carrywidth (Z.ltz (singlewidth sum_xy) (singlewidth x)) in |
| 1146 |
dlet low := singlewidth |
| 1147 |
(Z.land (singlewidth sum_xy) (singlewidth ('(two_pow_n - 1)))) in |
| 1148 |
dlet high := |
| 1149 |
singlewidth |
| 1150 |
(Z.add (singlewidth (Z.shiftr (singlewidth sum_xy) |
| 1151 |
(singlewidth ('(Z.log2 two_pow_n))))) |
| 1152 |
(singlewidth (Z.shiftl (carrywidth carry_xy) |
| 1153 |
(singlewidth ('(bitwidth - Z.log2 two_pow_n)))))) |
| 1154 |
in |
| 1155 |
(singlewidth low, singlewidth high))) |
| 1156 |
; (forall c x y two_pow_n r, |
| 1157 |
0 <= lgcarrymax <= bitwidth |
| 1158 |
-> two_pow_n = 2 ^ Z.log2 two_pow_n |
| 1159 |
-> 0 < Z.log2 two_pow_n < bitwidth |
| 1160 |
-> bitwidth + 1 < Z.log2 two_pow_n + lgcarrymax |
| 1161 |
-> two_pow_n ∈ r |
| 1162 |
-> singlewidth_carry |
| 1163 |
(Z.add_with_get_carry_full (cstZ r ('two_pow_n)) (carrywidth c) (singlewidth x) (singlewidth y)) |
| 1164 |
= (dlet sum_cx := singlewidth (carrywidth c + singlewidth x) in |
| 1165 |
dlet carry_cx := carrywidth (Z.ltz (singlewidth sum_cx) (singlewidth x)) in |
| 1166 |
dlet sum_cxy := singlewidth (singlewidth sum_cx + singlewidth y) in |
| 1167 |
dlet carry_cxy := carrywidth (Z.ltz (singlewidth sum_cxy) (singlewidth y)) in |
| 1168 |
dlet carry := singlewidth (Z.add (carrywidth carry_cx) (carrywidth carry_cxy)) in |
| 1169 |
dlet low := singlewidth |
| 1170 |
(Z.land (singlewidth sum_cxy) (singlewidth ('(two_pow_n - 1)))) in |
| 1171 |
dlet high := singlewidth |
| 1172 |
(Z.add (singlewidth (Z.shiftr (singlewidth sum_cxy) |
| 1173 |
(singlewidth ('(Z.log2 two_pow_n))))) |
| 1174 |
(singlewidth (Z.shiftl (carrywidth carry) |
| 1175 |
(singlewidth ('(bitwidth - Z.log2 two_pow_n)))))) in |
| 1176 |
(singlewidth low, singlewidth high))) |
| 1177 |
; (forall x y two_pow_n r, |
| 1178 |
0 <= lgcarrymax <= bitwidth |
| 1179 |
-> two_pow_n = 2 ^ Z.log2 two_pow_n |
| 1180 |
-> 0 < Z.log2 two_pow_n < bitwidth |
| 1181 |
-> bitwidth < Z.log2 two_pow_n + lgcarrymax |
| 1182 |
-> two_pow_n ∈ r |
| 1183 |
-> singlewidth_carry |
| 1184 |
(Z.sub_get_borrow_full (cstZ r ('two_pow_n)) (singlewidth x) (singlewidth y)) |
| 1185 |
= (dlet diff_xy := singlewidth (singlewidth x - singlewidth y) in |
| 1186 |
dlet borrow_xy := carrywidth (Z.ltz (singlewidth x) (singlewidth diff_xy)) in |
| 1187 |
dlet low := singlewidth |
| 1188 |
(Z.land (singlewidth diff_xy) (singlewidth ('(two_pow_n - 1)))) in |
| 1189 |
dlet high := |
| 1190 |
singlewidth |
| 1191 |
(Z.sub (singlewidth (Z.shiftl (carrywidth borrow_xy) |
| 1192 |
(singlewidth ('(bitwidth - Z.log2 two_pow_n))))) |
| 1193 |
(singlewidth (Z.shiftr (singlewidth diff_xy) |
| 1194 |
(singlewidth ('(Z.log2 two_pow_n)))))) |
| 1195 |
in |
| 1196 |
(singlewidth low, carrywidth high))) |
| 1197 |
; (forall c x y two_pow_n r, |
| 1198 |
0 <= lgcarrymax <= bitwidth |
| 1199 |
-> two_pow_n = 2 ^ Z.log2 two_pow_n |
| 1200 |
-> 0 < Z.log2 two_pow_n < bitwidth |
| 1201 |
-> bitwidth < Z.log2 two_pow_n + lgcarrymax |
| 1202 |
-> two_pow_n ∈ r |
| 1203 |
-> singlewidth_carry |
| 1204 |
(Z.sub_with_get_borrow_full (cstZ r ('two_pow_n)) (carrywidth c) (singlewidth x) (singlewidth y)) |
| 1205 |
= (dlet diff_xy := singlewidth (singlewidth x - singlewidth y) in |
| 1206 |
dlet borrow_xy := carrywidth (Z.ltz (singlewidth x) (singlewidth diff_xy)) in |
| 1207 |
dlet diff_xyc := singlewidth (singlewidth diff_xy - carrywidth c) in |
| 1208 |
dlet borrow_xyc := carrywidth (Z.ltz (singlewidth diff_xy) (singlewidth diff_xyc)) in |
| 1209 |
dlet borrow := singlewidth (carrywidth borrow_xy + carrywidth borrow_xyc) in |
| 1210 |
dlet low := singlewidth |
| 1211 |
(Z.land (singlewidth diff_xyc) (singlewidth ('(two_pow_n - 1)))) in |
| 1212 |
dlet high := |
| 1213 |
singlewidth |
| 1214 |
(Z.sub (singlewidth (Z.shiftl (carrywidth borrow) |
| 1215 |
(singlewidth ('(bitwidth - Z.log2 two_pow_n))))) |
| 1216 |
(singlewidth (Z.shiftr (singlewidth diff_xyc) |
| 1217 |
(singlewidth ('(Z.log2 two_pow_n)))))) |
| 1218 |
in |
| 1219 |
(singlewidth low, carrywidth high))) |
| 1220 |
] |
| 1221 |
; mymap |
| 1222 |
do_again |
| 1223 |
[ (* [do_again], so that if one of the arguments is concrete, we automatically get the rewrite rule for [Z_cast] applying to it *) |
| 1224 |
(forall rx ry x y, cstZZ rx ry (x, y) = (cstZ rx x, cstZ ry y)) |
| 1225 |
; (* handle things other than pairsinglewidth on Z.mul_split, most importantly r[0~>0] *) |
| 1226 |
(* N.B. we need to abstract over ranges, not just their upper bounds, due to https://github.com/mit-plv/rewriter/issues/84 *) |
| 1227 |
(forall r1 r2 s x y, |
| 1228 |
lower r1 = 0 -> lower r2 = 0 -> 0 <= singlewidth_upperbound -> 0 <= upper r1 -> 0 <= upper r2 |
| 1229 |
-> ((upper r1+1) | (singlewidth_upperbound+1)) |
| 1230 |
-> ((upper r2+1) | (singlewidth_upperbound+1)) |
| 1231 |
-> (upper r1 <> singlewidth_upperbound \/ upper r2 <> singlewidth_upperbound) |
| 1232 |
-> cstZZ r1 r2 (Z.mul_split s x y) |
| 1233 |
= cstZZ r1 r2 (pairsinglewidth (Z.mul_split s x y))) |
| 1234 |
] |
| 1235 |
]%Z%zrange. |
| 1236 |
|
| 1237 |
Definition noselect_rewrite_rulesT : list (bool * Prop) |
| 1238 |
:= Eval cbv [myapp mymap myflatten] in |
| 1239 |
mymap |
| 1240 |
dont_do_again |
| 1241 |
[(* no-op rule to prevent firing on selects between 0 and mask (since |
| 1242 |
these can be succinctly expressed as 0-c *) |
| 1243 |
(forall rc c, |
| 1244 |
singlewidth (Z.zselect (cstZ rc c) |
| 1245 |
(singlewidth ('0)) |
| 1246 |
(singlewidth ('(2^bitwidth - 1)))) |
| 1247 |
= singlewidth (Z.zselect (cstZ rc c) |
| 1248 |
(singlewidth ('0)) |
| 1249 |
(singlewidth ('(2^bitwidth - 1))))) |
| 1250 |
; (forall rc c x y, |
| 1251 |
0 <= bitwidth -> |
| 1252 |
singlewidth (Z.zselect (cstZ rc c) (singlewidth x) (singlewidth y)) |
| 1253 |
= (dlet a := |
| 1254 |
singlewidth |
| 1255 |
(Z.zselect (cstZ rc c) |
| 1256 |
(singlewidth ('0)) |
| 1257 |
(singlewidth ('(2^bitwidth - 1)))) in |
| 1258 |
dlet b := |
| 1259 |
singlewidth (Z.lxor (singlewidth a) |
| 1260 |
(singlewidth ('(2^bitwidth-1)))) in |
| 1261 |
singlewidth |
| 1262 |
(Z.lor (singlewidth (Z.land (singlewidth y) (singlewidth a))) |
| 1263 |
(singlewidth (Z.land (singlewidth x) (singlewidth b)))))) |
| 1264 |
]%Z. |
| 1265 |
End with_bitwidth. |