BV_Gen.v 55.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
(********************************************************************)
(*                                                                  *)
(*  The Why3 Verification Platform   /   The Why3 Development Team  *)
(*  Copyright 2010-2015   --   INRIA - CNRS - Paris-Sud University  *)
(*                                                                  *)
(*  This software is distributed under the terms of the GNU Lesser  *)
(*  General Public License version 2.1, with the special exception  *)
(*  on linking described in file LICENSE.                           *)
(*                                                                  *)
(********************************************************************)

Clement Fumex's avatar
Clement Fumex committed
12
13
14
15
16
17
18
19
20
(* This file is generated by Why3's Coq-realize driver *)
(* Beware! Only edit allowed sections below    *)
Require Import BuiltIn.
Require BuiltIn.
Require bool.Bool.
Require int.Int.
Require int.Abs.
Require int.EuclideanDivision.
Require bv.Pow2int.
Clément Fumex's avatar
Clément Fumex committed
21
22
23


Definition last_bit : nat.
24
25
26
Admitted.

Definition size_bv: nat := S last_bit.
Clément Fumex's avatar
Clément Fumex committed
27
28

(* Why3 goal *)
29
30
Definition size: Z.
  exact (Z.of_nat size_bv).
Clément Fumex's avatar
Clément Fumex committed
31
32
Defined.

33
34
Lemma size_int_S : size = Z.succ (Z.of_nat last_bit).
  unfold size, size_bv.
Clément Fumex's avatar
Clément Fumex committed
35
36
37
38
  rewrite Nat2Z.inj_succ; trivial.
Qed.

(* Why3 goal *)
39
40
Lemma size_pos : (0%Z < size)%Z.
  rewrite size_int_S; omega.
Clément Fumex's avatar
Clément Fumex committed
41
42
43
44
45
46
Qed.

Require Import Bool.Bvector.

(* Why3 goal *)
Definition t : Type.
47
  exact (Bvector size_bv).
Clément Fumex's avatar
Clément Fumex committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
Defined.

Fixpoint nth_aux {l} (v : Vector.t bool l) (m : Z) : bool :=
  match v with
    | Vector.nil => false
    | Vector.cons b _ tl => if Z_eq_dec m 0 then b else nth_aux tl (Z.pred m)
  end.

(* nth helper lemmas *)

Lemma nth_cons {l} (v : Vector.t bool l) (m : Z) b : (m <> -1)%Z ->
  nth_aux (Vector.cons bool b l v) (Z.succ m) = nth_aux v m.
  intro; simpl.
  case Z.eq_dec; intro.
  assert False by omega; easy.
  rewrite <- Zpred_succ; easy.
Qed.

Lemma nth_cons_pred {l} (v : Vector.t bool l) (m : Z) b : (m <> 0)%Z ->
  nth_aux (Vector.cons bool b l v) m = nth_aux v (Z.pred m).
  intro.
  rewrite Zsucc_pred with (n := m), <- Zpred_succ; apply nth_cons; omega.
Qed.

Lemma nth_high : forall {l} (v : Vector.t bool l) m, (m >= (Z.of_nat l))%Z -> nth_aux v m = false.
  induction v.
  easy.
  rewrite Nat2Z.inj_succ.
  intros; rewrite nth_cons_pred by omega.
  apply IHv; omega.
Qed.

Lemma nth_low : forall {l} (v : Vector.t bool l) m, (m < 0)%Z -> nth_aux v m = false.
  induction v.
  easy.
  intros; rewrite nth_cons_pred by omega.
  apply IHv; omega.
Qed.

Lemma nth_zero_is_hd : forall {l} (b : Vector.t bool (S l)), nth_aux b 0 = Vector.hd b.
  apply Vector.rectS; easy.
Qed.

Lemma nth_predl_is_last : forall {l} (b : Vector.t bool (S l)), nth_aux b (Z.of_nat l) = Vector.last b.
  apply Vector.rectS.
  easy.
  intros.
  rewrite Nat2Z.inj_succ, nth_cons by omega.
  easy.
Qed.

Lemma nth_const {l} (m : Z) b: (0 <= m < Z.of_nat l)%Z -> nth_aux (Vector.const b l) m = b.
  revert b m.
  induction l.
  simpl.
  intros; assert False by omega; easy.
  rewrite Nat2Z.inj_succ; intros; simpl.
  case (Z.eq_dec m 0); intro.
  easy.
  apply IHl; omega.
Qed.

Lemma nth_aux_map : forall {l} (f : bool -> bool) (v : Vector.t bool l) m,
                       (0 <= m < Z.of_nat l)%Z ->
                       f (nth_aux v m) = nth_aux (Vector.map f v) m.
  induction v.
  simpl; intros; assert False by omega; easy.
  rewrite Nat2Z.inj_succ; intros; simpl.
  case (Z.eq_dec m 0); intro.
  easy.
  apply IHv; omega.
Qed.

Lemma nth_aux_map2 : forall {l} (f : bool -> bool -> bool) (v1 v2 : Vector.t bool l) m,
                       (0 <= m < Z.of_nat l)%Z ->
                       f (nth_aux v1 m) (nth_aux v2 m) = nth_aux (Vector.map2 f v1 v2) m.
  intros l f v1 v2; pattern l, v1, v2.
  apply Vector.rect2.
  simpl; intros; assert False by omega; easy.
  intros.
  rewrite Nat2Z.inj_succ in H0; simpl.
  case (Z.eq_dec m 0); intro.
  easy.
  apply H; omega.
Qed.

Lemma nth_aux_tl : forall {l} (v : Vector.t bool (S l)) m, (m <> -1)%Z -> nth_aux (Vector.tl v) m = nth_aux v (Z.succ m).
  intros l v; pattern l, v.
  apply Vector.rectS.
  simpl.
  intros; case Z.eq_dec.
  intro; assert False by omega; easy.
  trivial.
  intros.
  simpl (Vector.tl (a :: v0)).
  symmetry; apply nth_cons; omega.
Qed.

Lemma nth_aux_shiftout_last : forall {l} (v : Vector.t bool (S l)), nth_aux (Vector.shiftout v) (Z.of_nat l) = false.
  intros l v; pattern l, v.
  apply Vector.rectS; intros.
  easy.
  rewrite Nat2Z.inj_succ.
  assert (Vector.shiftout (a :: v0) = a :: (Vector.shiftout v0)) by easy.
  rewrite H0, nth_cons by omega.
  apply H.
Qed.

Lemma nth_aux_shiftout_not_last : forall {l} (v : Vector.t bool (S l)) m, (m <> Z.of_nat l)%Z -> nth_aux (Vector.shiftout v) m = nth_aux v m.
  intros l v; pattern l, v.
  apply Vector.rectS; intros.
  simpl; case Z.eq_dec; easy.
  simpl; case Z.eq_dec; trivial.
  intro; apply H.
  rewrite Nat2Z.inj_succ in H0; omega.
Qed.

Lemma nth_aux_shiftin_false : forall {l} (v : Vector.t bool l) m, nth_aux (Vector.shiftin false v) m = nth_aux v m.
  induction v; intro; simpl; case (Z.eq_dec m 0); trivial.
Qed.

Lemma nth_aux_shiftin_low : forall {l} (v : Vector.t bool l) m b, 0 < l -> (0 <= m < Z.of_nat l)%Z -> nth_aux (Vector.shiftin b v) m = nth_aux v m.
  induction v; intros.
  easy.
  simpl; case Z.eq_dec.
  easy.
  rewrite Nat2Z.inj_succ in H0.
  intro; apply IHv; omega.
Qed.

Lemma nth_aux_shiftin_high : forall {l} (v : Vector.t bool l) b, nth_aux (Vector.shiftin b v) (Z.of_nat l) = b.
  induction v.
  easy.
  unfold Vector.shiftin.
  fold (@Vector.shiftin bool).
  rewrite Nat2Z.inj_succ.
  intro; rewrite nth_cons by omega.
  apply IHv.
Qed.

(* end of nth helpers *)

(* Why3 goal *)
Definition nth: t -> Z -> bool.
  exact nth_aux.
Defined.

Clement Fumex's avatar
Clement Fumex committed
195
196
197
198
199
200
201
Lemma nth_aux_out_of_bound : forall {l} (v : Vector.t bool l) (n : Z), ((n < 0%Z)%Z \/
  (Z.of_nat l <= n)%Z) -> ((nth_aux v n) = false).
induction v.
simpl; auto.
simpl.
intros.
case Z.eq_dec.
202
intros; elimtype False; destruct H.
Clement Fumex's avatar
Clement Fumex committed
203
204
omega.
subst n0.
205
auto with zarith.
Clement Fumex's avatar
Clement Fumex committed
206
207
208
209
210
211
212
213
214
intro; rewrite IHv;auto.
intuition; auto with zarith.
right.
rewrite Zpos_P_of_succ_nat in H0.
omega.
Qed.

(* Why3 goal *)
Lemma nth_out_of_bound : forall (x:t) (n:Z), ((n < 0%Z)%Z \/
215
  (size <= n)%Z) -> ((nth x n) = false).
Clement Fumex's avatar
Clement Fumex committed
216
217
218
219
220
intros.
unfold nth.
rewrite nth_aux_out_of_bound; auto with zarith.
Qed.

Clément Fumex's avatar
Clément Fumex committed
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
Definition zero_aux {l} : Vector.t bool l.
  exact (Vector.const false l).
Defined.

(* Why3 goal *)
Definition zero: t.
  exact zero_aux.
Defined.

Lemma Nth_zero_aux : forall {l} (n:Z), ((@nth_aux l zero_aux n) = false).
  induction l.
  easy.
  simpl.
  intro n; case (Z.eq_dec n 0); easy.
Qed.

(* Why3 goal *)
Clement Fumex's avatar
Clement Fumex committed
238
239
Lemma Nth_zero : forall (n:Z), ((nth zero n) = false).
intros n; apply Nth_zero_aux.
Clément Fumex's avatar
Clément Fumex committed
240
241
242
243
244
245
246
247
Qed.

Definition ones_aux l : Vector.t bool l.
  exact (Vector.const true l).
Defined.

(* Why3 goal *)
Definition ones: t.
248
  exact (ones_aux size_bv).
Clément Fumex's avatar
Clément Fumex committed
249
250
251
Defined.

(* Why3 goal *)
252
253
Lemma Nth_ones : forall (n:Z), ((0%Z <= n)%Z /\ (n < size)%Z) -> ((nth ones
  n) = true).
Clément Fumex's avatar
Clément Fumex committed
254
255
256
257
258
259
260
261
262
263
  intros; apply nth_const; easy.
Qed.

(* Why3 goal *)
Definition bw_and: t -> t -> t.
  exact (Vector.map2 (fun x y => x && y)).
Defined.

(* Why3 goal *)
Lemma Nth_bw_and : forall (v1:t) (v2:t) (n:Z), ((0%Z <= n)%Z /\
264
  (n < size)%Z) -> ((nth (bw_and v1 v2) n) = (Init.Datatypes.andb (nth v1
Clément Fumex's avatar
Clément Fumex committed
265
266
267
268
269
270
271
272
273
274
275
276
  n) (nth v2 n))).
  symmetry.
  apply nth_aux_map2 with (f := fun x y => x && y); easy.
Qed.

(* Why3 goal *)
Definition bw_or: t -> t -> t.
  exact (Vector.map2 (fun x y => x || y)).
Defined.

(* Why3 goal *)
Lemma Nth_bw_or : forall (v1:t) (v2:t) (n:Z), ((0%Z <= n)%Z /\
277
  (n < size)%Z) -> ((nth (bw_or v1 v2) n) = (Init.Datatypes.orb (nth v1
Clément Fumex's avatar
Clément Fumex committed
278
279
280
281
282
283
284
285
286
287
288
289
  n) (nth v2 n))).
  symmetry.
  apply nth_aux_map2; easy.
Qed.

(* Why3 goal *)
Definition bw_xor: t -> t -> t.
  exact (Vector.map2 (fun x y => xorb x y)).
Defined.

(* Why3 goal *)
Lemma Nth_bw_xor : forall (v1:t) (v2:t) (n:Z), ((0%Z <= n)%Z /\
290
  (n < size)%Z) -> ((nth (bw_xor v1 v2) n) = (Init.Datatypes.xorb (nth v1
Clément Fumex's avatar
Clément Fumex committed
291
292
293
294
295
296
297
298
299
300
301
  n) (nth v2 n))).
  symmetry.
  apply nth_aux_map2; easy.
Qed.

(* Why3 goal *)
Definition bw_not: t -> t.
  exact (Vector.map (fun x => negb x)).
Defined.

(* Why3 goal *)
302
Lemma Nth_bw_not : forall (v:t) (n:Z), ((0%Z <= n)%Z /\ (n < size)%Z) ->
Clément Fumex's avatar
Clément Fumex committed
303
304
305
306
307
308
309
310
311
312
  ((nth (bw_not v) n) = (Init.Datatypes.negb (nth v n))).
  symmetry.
  apply nth_aux_map; easy.
Qed.

(* Why3 goal *)
Definition lsr: t -> Z -> t.
  exact (fun v m => BshiftRl_iter last_bit v (Z.to_nat m)).
Defined.

313
Lemma bshiftRl_iter_nth : forall b s m,
314
315
                                (0 <= Z.of_nat s)%Z ->
                                (0 <= m)%Z ->
Clément Fumex's avatar
Clément Fumex committed
316
317
318
319
320
321
322
323
324
325
326
327
328
329
   nth_aux (BshiftRl_iter last_bit b s) m = nth_aux b (m + Z.of_nat s).
  induction s.
  simpl.
  intros; rewrite <- Zplus_0_r_reverse; easy.
  rewrite Nat2Z.inj_succ; intros.
  simpl BshiftRl_iter.
  unfold BshiftRl, Bhigh.
  rewrite nth_aux_tl by omega.
  rewrite nth_aux_shiftin_false.
  rewrite <- Zplus_succ_r_reverse, <- Z.add_succ_l.
  apply IHs; omega.
Qed.

(* Why3 goal *)
330
Lemma Lsr_nth_low : forall (b:t) (n:Z) (s:Z), (0%Z <= s)%Z ->
331
332
333
334
335
  ((0%Z <= n)%Z -> (((n + s)%Z < size)%Z -> ((nth (lsr b s) n) = (nth b
  (n + s)%Z)))).
intros b n s h1 h2 h3.
rewrite <-Z2Nat.id with (n := s) at 2; auto.
apply bshiftRl_iter_nth; omega.
336
337
338
339
Qed.

(* Why3 goal *)
Lemma Lsr_nth_high : forall (b:t) (n:Z) (s:Z), (0%Z <= s)%Z ->
340
341
  ((0%Z <= n)%Z -> ((size <= (n + s)%Z)%Z -> ((nth (lsr b s) n) = false))).
  intros b n s h1 h2 h3.
342
343
344
345
  unfold nth,lsr.
  cut (nth_aux b (n + Z.of_nat (Z.to_nat s)) = false).
  intro.
  rewrite <-H.
346
  apply bshiftRl_iter_nth; omega.
347
348
  rewrite Z2Nat.id by omega.
  apply nth_out_of_bound; omega.
Clément Fumex's avatar
Clément Fumex committed
349
350
351
352
353
354
355
Qed.

(* Why3 goal *)
Definition asr: t -> Z -> t.
  exact (fun v m => BshiftRa_iter last_bit v (Z.to_nat m)).
Defined.

Clement Fumex's avatar
Clement Fumex committed
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
Definition eq_aux {l} (v1 v2 : Vector.t bool l): Prop := forall (n:Z), ((0%Z <= n)%Z /\
  (n < (Z.of_nat l))%Z) -> ((nth_aux v1 n) = (nth_aux v2 n)).

Lemma eq_aux_cons : forall {l} v1 v2 b b',  b = b' /\ @eq_aux l v1 v2 <-> eq_aux (b :: v1) (b' :: v2).
  intros; unfold eq_aux.
  rewrite Nat2Z.inj_succ.
  split; intros.
  simpl; case Z.eq_dec; intro.
  easy.
  apply H; omega.
  split.
  apply (H 0%Z); omega.
  intros.
  rewrite <- (nth_cons v1 n b) by omega.
  rewrite <- (nth_cons v2 n b') by omega.
  apply H; omega.
Qed.

Lemma Extensionality_aux : forall {l} (x y : Vector.t bool l), eq_aux x y -> x = y.
  intros l x y; pattern l, x, y.
  apply Vector.rect2.
  easy.
  intros.
  apply (eq_aux_cons v1 v2 a b) in H0; destruct H0.
  apply H in H1.
  rewrite H0, H1; trivial.
Qed.

Clément Fumex's avatar
Clément Fumex committed
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
(* Vector helper lemmas *)

Lemma singleton_is_singl : forall b : Vector.t bool 1, b = [ Vector.hd b ].
  intro; apply Extensionality_aux; unfold eq_aux; intros.
  change (Z.of_nat 1) with 1%Z in H; assert (n = 0%Z) as e by omega; rewrite e; simpl.
  apply nth_zero_is_hd.
Qed.

Lemma shiftrepeat_is_shiftin : forall {l} (v : Vector.t bool (S l)), Vector.shiftrepeat v = Vector.shiftin (Vector.last v) v.
  apply Vector.rectS.
  easy.
  intros; simpl.
  rewrite H; trivial.
Qed.

Lemma last_tail_shiftreapeat : forall {l} (v : Vector.t bool (S l)), Vector.last (Vector.tl (Vector.shiftrepeat v)) = Vector.last v.
  apply Vector.caseS; intros.
  induction n.
  apply Vector.case0 with (v := t0); easy.
  simpl.
  apply Vector.shiftrepeat_last.
Qed.

(* end of Vector helpers *)

Lemma BshiftRa_iter_nth_low : forall (b:t) (s:nat) (n:Z),
410
  (0%Z <= n < (size - (Z.of_nat s)))%Z ->
411
  (nth_aux (BshiftRa_iter last_bit b s) n) = (nth_aux b (n + Z.of_nat s)%Z).
Clément Fumex's avatar
Clément Fumex committed
412
413
414
415
416
417
418
419
420
421
422
423
  induction s.
  simpl.
  intros; rewrite <- Zplus_0_r_reverse; easy.
  rewrite Nat2Z.inj_succ; intros.
  simpl BshiftRa_iter.
  unfold BshiftRa, Bhigh.
  rewrite nth_aux_tl by omega.
  rewrite shiftrepeat_is_shiftin.
  rewrite nth_aux_shiftin_low.
  rewrite <- Zplus_succ_r_reverse, <- Z.add_succ_l.
  apply IHs; omega.
  omega.
424
  fold size_bv; fold size; omega.
Clément Fumex's avatar
Clément Fumex committed
425
426
427
Qed.

(* Why3 goal *)
428
Lemma Asr_nth_low : forall (b:t) (n:Z) (s:Z), (0%Z <= s)%Z ->
429
430
  (((0%Z <= n)%Z /\ (n < size)%Z) -> (((n + s)%Z < size)%Z -> ((nth (asr b s)
  n) = (nth b (n + s)%Z)))).
Clément Fumex's avatar
Clément Fumex committed
431
432
433
434
  unfold nth, lsr.
  intros.
  assert ((n + s)%Z = (n + Z.of_nat (Z.to_nat s))%Z).
  rewrite Z2Nat.id with (n := s); omega.
435
  rewrite H2.
Clément Fumex's avatar
Clément Fumex committed
436
437
438
439
440
441
442
443
444
445
446
447
  apply BshiftRa_iter_nth_low; omega.
Qed.

Lemma bshiftra_iter_last : forall {l} (v : Bvector (S l)) s, Vector.last (BshiftRa_iter l v s) = Vector.last v.
  induction s.
  easy.
  simpl; unfold BshiftRa, Bhigh.
  rewrite last_tail_shiftreapeat.
  apply IHs.
Qed.

Lemma BhiftRa_iter_nth_high : forall (b:t) (s:nat) (n:Z),
448
449
450
  (0%Z <= n < size)%Z ->
  (size <= n + Z.of_nat s)%Z -> ((nth_aux (BshiftRa_iter last_bit b s) n) = nth_aux b
  (size - 1%Z))%Z.
Clément Fumex's avatar
Clément Fumex committed
451
452
453
454
  induction s.
  simpl BshiftRa_iter.
  simpl (Z.of_nat 0).
  intros.
455
  assert (n = size - 1)%Z by omega; rewrite H1; trivial.
Clément Fumex's avatar
Clément Fumex committed
456
457
458
459
460
  rewrite Nat2Z.inj_succ; intros.
  simpl BshiftRa_iter.
  unfold BshiftRa, Bhigh.
  rewrite nth_aux_tl by omega.
  rewrite shiftrepeat_is_shiftin.
461
462
  case (Z.eq_dec (Z.succ n) size).
  intro; unfold size in e.
Clément Fumex's avatar
Clément Fumex committed
463
464
465
466
467
468
469
  rewrite e, nth_aux_shiftin_high.
  rewrite bshiftra_iter_last.
  rewrite size_int_S, Z.sub_1_r, <- Zpred_succ.
  symmetry; apply nth_predl_is_last.
  intro; rewrite nth_aux_shiftin_low.
  apply IHs; omega.
  omega.
470
  fold size_bv; fold size; omega.
Clément Fumex's avatar
Clément Fumex committed
471
472
473
Qed.

(* Why3 goal *)
474
Lemma Asr_nth_high : forall (b:t) (n:Z) (s:Z), (0%Z <= s)%Z ->
475
476
  (((0%Z <= n)%Z /\ (n < size)%Z) -> ((size <= (n + s)%Z)%Z -> ((nth (asr b
  s) n) = (nth b (size - 1%Z)%Z)))).
Clément Fumex's avatar
Clément Fumex committed
477
478
479
480
481
482
483
484
485
486
487
488
  unfold nth, asr.
  intros.
  apply BhiftRa_iter_nth_high.
  omega.
  rewrite Z2Nat.id; omega.
Qed.

(* Why3 goal *)
Definition lsl: t -> Z -> t.
  exact (fun v m => BshiftL_iter last_bit v (Z.to_nat m)).
Defined.

489
Lemma bshiftL_iter_nth_high : forall {l} v s m, (0 <= Z.of_nat s)%Z -> (Z.of_nat s <= m)%Z -> (m < Z.succ (Z.of_nat l))%Z -> nth_aux (BshiftL_iter l v s) m = nth_aux v (m - Z.of_nat s).
Clément Fumex's avatar
Clément Fumex committed
490
491
  induction s; intros.
  simpl.
492
  rewrite <-Zminus_0_l_reverse; reflexivity.
Clément Fumex's avatar
Clément Fumex committed
493
494
495
496
  simpl.
  case Z.eq_dec; intro.
  symmetry; apply nth_low.
  rewrite e; simpl; apply Pos2Z.neg_is_neg.
497
  rewrite Nat2Z.inj_succ in H, H0.
498
  case (Z.eq_dec (Z.pred m) (Z.of_nat l));intro.
Clément Fumex's avatar
Clément Fumex committed
499
500
  assert False by omega; easy.
  rewrite nth_aux_shiftout_not_last by omega.
501
  rewrite Zpos_P_of_succ_nat, Z.sub_succ_r, <- Z.sub_pred_l.
Clément Fumex's avatar
Clément Fumex committed
502
503
504
505
506
  apply IHs; omega.
Qed.

(* Why3 goal *)
Lemma Lsl_nth_high : forall (b:t) (n:Z) (s:Z), ((0%Z <= s)%Z /\
507
  ((s <= n)%Z /\ (n < size)%Z)) -> ((nth (lsl b s) n) = (nth b (n - s)%Z)).
Clément Fumex's avatar
Clément Fumex committed
508
  intros.
509
  unfold lsl, nth.
510
511
512
513
514
515
516
517
  rewrite <-Z2Nat.id with (n := s) at 2 by omega.
  destruct H.
  destruct H0.
  apply (bshiftL_iter_nth_high b (Z.to_nat s) n).
  auto with zarith.
  rewrite Z2Nat.id; omega.
  rewrite size_int_S in H1.
  omega.
Clément Fumex's avatar
Clément Fumex committed
518
519
Qed.

520
521
Lemma Lsl_nth_low_aux : forall {l} x b (n : int),
                          (0 <= n < Z.of_nat x)%Z -> nth_aux (BshiftL_iter l b x) n = false.
Clément Fumex's avatar
Clément Fumex committed
522
523
524
525
  induction x.
  simpl; intros; assert False by omega; easy.
  rewrite Nat2Z.inj_succ; intros.
  simpl.
526
  case Z.eq_dec;intro.
Clément Fumex's avatar
Clément Fumex committed
527
  trivial.
528
529
530
  case (Z.eq_dec (Z.pred n) (Z.of_nat l));intro.
  apply nth_high; omega.
  rewrite nth_aux_shiftout_not_last by auto.
Clément Fumex's avatar
Clément Fumex committed
531
532
533
534
  apply IHx; omega.
Qed.

(* Why3 goal *)
535
536
Lemma Lsl_nth_low : forall (b:t) (n:Z) (s:Z), ((0%Z <= n)%Z /\ (n < s)%Z) ->
  ((nth (lsl b s) n) = false).
Clément Fumex's avatar
Clément Fumex committed
537
  intros.
538
539
  apply Lsl_nth_low_aux.
  rewrite Z2Nat.id; omega.
Clément Fumex's avatar
Clément Fumex committed
540
541
Qed.

542
543
544
545
546
(* Why3 goal *)
Definition two_power_size: Z.
  exact (Pow2int.pow2 size)%Z.
Defined.

Clément Fumex's avatar
Clément Fumex committed
547
548
(* Why3 goal *)
Definition max_int: Z.
549
  exact (Pow2int.pow2 size - 1)%Z.
Clément Fumex's avatar
Clément Fumex committed
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
Defined.

Lemma max_int_nat : forall l, (0 <= Pow2int.pow2 (Z.of_nat l) - 1)%Z.
  intro.
  rewrite Z.sub_1_r.
  apply Zlt_0_le_0_pred.
  apply Pow2int.pow2pos.
  omega.
Qed.

Fixpoint bvec_to_nat n (v : Bvector n) {struct v} : nat :=
  match v with
    | Vector.nil => O
    | Vector.cons false n v => 2 * bvec_to_nat n v
    | Vector.cons true n v => 1 + 2 * bvec_to_nat n v
  end.

Lemma bvec_to_nat_zero : forall {l}, bvec_to_nat l (Vector.const false l) = 0.
  induction l; [easy|simpl; omega].
Qed.

Definition twos_complement n (v : Bvector n) : Z :=
  match v with
    | Vector.nil => 0%Z
    | Vector.cons false n v => Z.of_nat (bvec_to_nat n v)
    | Vector.cons true n v => (Z.of_nat (bvec_to_nat n v) - Pow2int.pow2 (Z.of_nat (S n)))%Z
  end.

Require Arith.Div2.

Fixpoint nat_to_bvec (length val : nat) {struct length} : Bvector length :=
  match length as x return Bvector x with
    | O => Bnil
    | S length =>
      Bcons (Z.odd (Z.of_nat val)) length (nat_to_bvec length (Div2.div2 val))
  end.

Lemma Nat_to_bvec_zero : forall {n}, Vector.const false n = nat_to_bvec n 0.
  induction n.
  easy.
  simpl.
  unfold Bcons.
  rewrite IHn; trivial.
Qed.

Lemma bvec_to_nat_extensionality : forall {m} (v v' : Bvector m),
 (bvec_to_nat m v) = (bvec_to_nat m v') -> v = v'.
  unfold Bvector.
  intros m v v'.
  pattern m,v,v'.
  apply Vector.rect2.
  trivial.
  case a, b; intros.
  rewrite H; auto; inversion H0; omega.
  inversion H0; assert False; omega.
  inversion H0; assert False; omega.
  rewrite H; auto; inversion H0; omega.
Qed.

(* pow2 helper lemmas *)

Lemma id_lt_pow2: forall n, (Z.of_nat (S n) < Pow2int.pow2 (Z.of_nat (S n)))%Z.
  intro.
  induction n.
  easy.
  rewrite Nat2Z.inj_succ.
  apply Z.le_lt_trans with (m := (2 * Z.of_nat (S n))%Z).
  rewrite Nat2Z.inj_succ; omega.
  apply Z.lt_le_trans with (m := (2 * Pow2int.pow2 (Z.of_nat (S n)))%Z).
  omega.
  rewrite two_p_S by omega.
  unfold Pow2int.pow2; omega.
Qed.

Lemma pow2_lt_mono_nat : forall i j : nat, (i < j) -> (Pow2int.pow2 (Z.of_nat i) < Pow2int.pow2 (Z.of_nat j))%Z.
  intros.
  unfold Pow2int.pow2; rewrite two_p_equiv, two_p_equiv.
  apply Z.pow_lt_mono_r; omega.
Qed.

Lemma pow2_le_mono_nat : forall i j : nat, (i <= j) -> (Pow2int.pow2 (Z.of_nat i) <= Pow2int.pow2 (Z.of_nat j))%Z.
  intros.
  unfold Pow2int.pow2; rewrite two_p_equiv, two_p_equiv.
  apply Z.pow_le_mono_r; omega.
Qed.

Lemma pow2_le_mono_pos : forall i j : positive, (Pos.le i j) -> (Pow2int.pow2 (Zpos i) <= Pow2int.pow2 (Zpos j))%Z.
  intros.
  rewrite <- positive_nat_Z, <- positive_nat_Z.
  apply pow2_le_mono_nat.
  apply Pos2Nat.inj_le; trivial.
Qed.

Lemma pow2_pos : forall i : positive, (2 <= Pow2int.pow2 (Z.pos i))%Z.
  intros.
  rewrite <- Pow2int.Power_1.
  apply pow2_le_mono_pos.
  apply Pos.le_1_l.
Qed.

(* end of pow2 helpers *)

(* arithmetic helper lemmas *)

Lemma lem_time2 : forall a b, a < b -> 1 + 2 * a < 2 * b.
  intros; omega.
Qed.

Lemma le_le_le: forall x y z t, (t <= z)%Z -> (x <= y <= t)%Z -> (x <= y <= z)%Z.
  split; [easy|apply Int.Trans with (y := t0); easy].
Qed.

Lemma le_lt_le: forall x y z t, (t <= z)%Z -> (x <= y < t)%Z -> (x <= y <= z)%Z.
  split; [easy|apply Int.Trans with (y := t0); [apply Z.lt_le_incl, H0|easy]].
Qed.

Lemma factor_sub : forall n m p, (n * m - n * p = n * (m - p))%Z.
  intros.
  rewrite Int.infix_mn_def, Int.infix_mn_def, Zopp_mult_distr_r.
  apply Zred_factor4.
Qed.

Lemma lt_sym: forall x y, (x < y)%Z <-> (y > x)%Z.
  intro; split; omega.
Qed.

(* end of arithmetic helpers *)

Lemma bvec_to_nat_range : forall {n} v, bvec_to_nat n v < Z.to_nat (Pow2int.pow2 (Z.of_nat n)).
  induction v.
  simpl bvec_to_nat; auto.
  apply NPeano.Nat.le_lt_trans with (m := 1 + (bvec_to_nat n v * 2)).
  simpl; case h; omega.
  rewrite Nat2Z.inj_succ, <- Z.add_1_r, Pow2int.Power_s by omega.
  rewrite Z2Nat.inj_mul.
  assert (Z.to_nat 2 = 2) by easy; rewrite H.
  rewrite mult_comm.
  apply lem_time2.
  easy.
  easy.
  apply Zlt_le_weak, Pow2int.pow2pos; omega.
Qed.

Lemma twos_complement_neg : forall {n} v, (twos_complement (S n) (true :: v) < 0)%Z.
  intros.
  unfold twos_complement.
  apply Z.lt_sub_0.
  apply Zlt_trans with (m := Pow2int.pow2 (Z.of_nat n)).
  rewrite Z2Nat.inj_lt.
  rewrite Nat2Z.id.
  apply bvec_to_nat_range.
  omega.
  apply Zlt_le_weak, Pow2int.pow2pos; omega.
  apply pow2_lt_mono_nat; omega.
Qed.

Lemma twos_complement_pos : forall {n} v, (twos_complement (S n) (false :: v) >= 0)%Z.
  intros.
  unfold twos_complement; omega.
Qed.

Lemma twos_complement_extensionality : forall {m} (v v' : Bvector m),
  (twos_complement m v = twos_complement m v' -> v = v').
  destruct v; intros.
  apply Vector.case0 with (v := v'); trivial.
  revert v h H.
  apply Vector.caseS with (v := v'); intros.
  case h, h0; intros.
  inversion H.
  apply bvec_to_nat_extensionality; simpl; omega.
  Lemma tmp : forall {n} v v', twos_complement (S n) (false :: v) <> twos_complement (S n) (true :: v').
    intros.
    intro.
    assert ((twos_complement (S n) (true :: v') >= 0)%Z).
    rewrite <- H; apply twos_complement_pos.
    assert ((twos_complement (S n) (true :: v') < 0)%Z).
    apply twos_complement_neg.
    easy.
  Qed.
  assert (twos_complement (S n0) (false :: v) <>
          twos_complement (S n0) (true :: t0)).
  apply tmp.
  assert False by omega; easy.
  assert (twos_complement (S n0) (false :: t0) <>
          twos_complement (S n0) (true :: v)).
  apply tmp.
  assert False by omega; easy.
  inversion H.
  apply bvec_to_nat_extensionality; simpl; omega.
Qed.

(* even / odd helper lemmas *)

Lemma odd_is_odd : forall n : nat, Even.odd n <-> Z.odd (Z.of_nat n) = true.
  intro.
  rewrite Zodd_bool_iff, Zodd_ex_iff, <- NPeano.Odd_equiv.
  unfold NPeano.Odd.
  split; intro; destruct H.
  exists (Z.of_nat x); omega.
  rewrite <-Z2Nat.id with (n := x) in H by omega.
  exists (Z.to_nat x); omega.
Qed.

Lemma even_not_odd : forall n : nat, Even.even n <-> not (Even.odd n).
  intro; split; intro.
  intro; apply Even.not_even_and_odd with (n := n); easy.
  case (Even.even_or_odd n); easy.
Qed.

Lemma even_is_even : forall n : nat, Even.even n <-> Z.even (Z.of_nat n) = true.
  intro.
  rewrite even_not_odd, odd_is_odd, Zeven.Zeven_odd_bool, negb_true_iff, not_true_iff_false; easy.
Qed.

Lemma case_odd : forall (i : nat), (Z.odd (Z.of_nat i) = true) + (Z.odd (Z.of_nat i) = false).
  induction i.
  right; simpl; easy.
  rewrite Nat2Z.inj_succ.
  destruct IHi.
  right; rewrite Z.odd_succ, <- Z.negb_odd; rewrite e; trivial.
  left; rewrite Z.odd_succ, <- Z.negb_odd; rewrite e; trivial.
Qed.

Lemma odd_even_le : forall n m, Z.even n = true -> Z.odd m = true -> (n <= m)%Z -> (n < m)%Z.
  intros.
  case (Z_le_lt_eq_dec n m); trivial.
  intro; assert False.
  rewrite e, <- Z.negb_odd, negb_true_iff, H0 in H; easy.
  easy.
Qed.

Lemma pow2_is_even : forall n, (n > 0)%Z -> Z.even (Pow2int.pow2 n) = true.
  intros.
  unfold Pow2int.pow2.
  rewrite two_p_equiv, Z.even_pow by omega; easy.
Qed.

Lemma max_int_is_odd : forall n, (n > 0)%Z -> Z.odd (Pow2int.pow2 n - 1) = true.
  intros.
  rewrite Z.sub_1_r, Z.odd_pred.
  apply pow2_is_even; trivial.
Qed.

(* end of even / odd helpers *)

Lemma bvec_to_nat_nat_to_bvec : forall {n} i, (Z.of_nat i <= Pow2int.pow2 (Z.of_nat n) - 1)%Z ->
  bvec_to_nat n (nat_to_bvec n i) = i.
  induction n.
  simpl; intro; omega.
  destruct i; intros.
  simpl.
  rewrite <- Nat_to_bvec_zero, bvec_to_nat_zero; trivial.
  unfold nat_to_bvec, Bcons, bvec_to_nat.
  case (case_odd (S i)); intro.
  assert (Even.even i).
  rewrite even_not_odd, odd_is_odd.
  rewrite Nat2Z.inj_succ, Z.odd_succ, <- Z.negb_odd, negb_true_iff in e.
  rewrite e; easy.
  rewrite e.
  apply NPeano.Nat.succ_inj_wd.
  change (2 * bvec_to_nat n (nat_to_bvec n (Div2.div2 (S i))) = i).
  rewrite IHn.
  rewrite <- Div2.even_div2, <- NPeano.double_twice, <- Div2.even_double by easy; trivial.
  rewrite Z.mul_le_mono_pos_l with (p := 2%Z) by easy.
  rewrite Z.add_le_mono_l with (p := 1%Z) by easy.
  apply Z.le_trans with (m := Z.of_nat (S i)).
  rewrite <- Div2.even_div2, <- Nat2Z.inj_mul with (n:= 2), <- NPeano.double_twice, <- Div2.even_double, Nat2Z.inj_succ by trivial; omega.
  apply Z.le_trans with (m := (Pow2int.pow2 (Z.of_nat (S n)) - 1)%Z); trivial.
  rewrite Nat2Z.inj_succ, <- Z.add_1_r, Pow2int.Power_s; omega.
  rewrite e.
  change (2 * bvec_to_nat n (nat_to_bvec n (Div2.div2 (S i))) = S i).
  rewrite IHn.
  rewrite  <- NPeano.double_twice, <- Div2.even_double; trivial.
  rewrite even_not_odd, odd_is_odd, e; auto.
  rewrite Z.mul_le_mono_pos_l with (p := 2%Z) by easy.
  apply Z.le_trans with (m := Z.of_nat (S i)).
  rewrite <- Nat2Z.inj_mul with (n := 2), <- NPeano.double_twice, <- Div2.even_double.
  omega.
  rewrite even_not_odd, odd_is_odd, not_true_iff_false; trivial.
  apply odd_even_le, Z.lt_le_pred in H.
  apply Z.le_trans with (m := (Pow2int.pow2 (Z.of_nat (S n)) - 2)%Z).
  omega.
  rewrite Nat2Z.inj_succ, <- Z.add_1_r, Pow2int.Power_s; omega.
  rewrite Zeven.Zeven_odd_bool, e; easy.
  unfold Pow2int.pow2.
  rewrite Z.sub_1_r, Z.odd_pred, Nat2Z.inj_succ.
  rewrite pow2_is_even by omega; easy.
Qed.

Lemma Nat_to_bvec_ones : forall {n}, Vector.const true n = nat_to_bvec n (Z.to_nat (Pow2int.pow2 (Z.of_nat n) - 1)).
  induction n.
  easy.
  unfold nat_to_bvec.
  rewrite Z2Nat.id by (apply max_int_nat).
  rewrite max_int_is_odd by easy.
  fold nat_to_bvec.
  assert ((Div2.div2 (Z.to_nat (Pow2int.pow2 (Z.of_nat (S n)) - 1))) = (Z.to_nat (Pow2int.pow2 (Z.of_nat n) - 1))).
  apply eq_add_S.
  rewrite Div2.odd_div2.
  rewrite <- Z2Nat.inj_succ by (apply max_int_nat).
  rewrite Z.sub_1_r, <- Zsucc_pred, Nat2Z.inj_succ, <- Z.add_1_r.
  rewrite Pow2int.Power_s by omega.
  rewrite Z2Nat.inj_mul, Div2.div2_double.
  rewrite <- Z2Nat.inj_succ by (apply max_int_nat).
  rewrite Z.sub_1_r, <- Zsucc_pred; trivial.
  easy.
  apply Z.lt_le_incl, Pow2int.pow2pos; omega.
  rewrite odd_is_odd.
  rewrite Z2Nat.id by (apply max_int_nat).
  apply max_int_is_odd; easy.
  rewrite H, <- IHn; auto.
Qed.

Import EuclideanDivision.

(* mod helper lemmas *)

Lemma mod1_nat : forall x y, 0 < y -> 0 <= mod1 x y.
  intros.
  unfold mod1, div.
  case Z_le_dec; intro.
  apply Zle_minus_le_0, Z_mult_div_ge; omega.
  destruct n; apply Z.mod_pos_bound; trivial.
Qed.

Lemma mod1_nat_high_bound_lt: forall u v, 0 < v -> mod1 u v < v.
  intros.
  rewrite <- Z.abs_eq by omega.
  apply Mod_bound; omega.
Qed.

Lemma mod1_nat_high_bound_le: forall u v, 0 <= v ->
                                       mod1 u (v + 1) <= v.
  intros.
  rewrite Zpred_succ, <- Z.lt_le_pred.
  apply mod1_nat_high_bound_lt; omega.
Qed.

Lemma mod1_out : forall x y, 0 <= x < y -> mod1 x y = x.
  intros; unfold mod1.
  rewrite Div_inf; omega.
Qed.

Lemma mod_mod_mult: forall a b c, b > 0 -> c > 0 -> ((a mod (c * b)) mod b) = a mod b.
  intros.
  rewrite Z.mul_comm.
  rewrite Z.rem_mul_r by omega.
  rewrite <-Zplus_mod_idemp_r.
  rewrite Z.mul_comm, Z_mod_mult, Zplus_0_r.
  apply Zmod_mod.
Qed.

(* end of mod helpers *)

Fixpoint rotate_right_aux {l} (b : Vector.t bool (S l)) (p : nat) : Vector.t bool (S l) :=
match p with
  | O => b
  | S q => let prev := rotate_right_aux b q in
            (Vector.shiftin (Vector.hd prev) (Vector.tl prev))
end.

Fixpoint rotate_left_aux {l} (b : Vector.t bool (S l)) (p : nat) : Vector.t bool (S l) :=
match p with
  | O => b
  | S q => let prev := rotate_left_aux b q in
            ((Vector.last prev) :: (Vector.shiftout prev))
end.

Lemma mod1_is_mod : forall x y, y > 0 -> mod1 x y = Zmod x y.
  intros; unfold mod1, div.
  case Z_le_dec; intro.
  rewrite Z.mod_eq by omega; trivial.
  destruct n; apply Z_mod_lt; omega.
Qed.

Lemma mod1_succ_low : forall x y, y > 0 -> mod1 x y < (Z.pred y) -> mod1 (Z.succ x) y = Z.succ (mod1 x y).
  intros x y H.
  rewrite mod1_is_mod, mod1_is_mod by trivial.
  rewrite Z.mod_eq, Z.mod_eq by omega.
  intro; cut (Zsucc x / y = x / y).
  intro e; rewrite e; omega.
  rewrite Z.div_unique_pos with (a := Z.succ x) (b := y) (r := (x mod y) + 1) (q := x / y).
  trivial.
  rewrite <- Z.mod_eq in H0 by omega.
  split; [apply Z.le_le_succ_r, Z_mod_lt; trivial|omega].
  rewrite <- Zplus_assoc_reverse, <- Z_div_mod_eq by omega; omega.
Qed.

Lemma mod1_succ_high : forall x y, y > 0 -> mod1 x y = (Z.pred y) -> mod1 (Z.succ x) y = 0.
  intros x y H.
  rewrite mod1_is_mod, mod1_is_mod by omega; intro.
  rewrite <- Z.add_1_r, Zplus_mod, H0.
  case (Z.eq_dec y 1); intro e.
  rewrite e; easy.
  rewrite Zmod_1_l by omega.
  rewrite Z.add_1_r, <- Zsucc_pred.
  apply Z_mod_same_full.
Qed.

Lemma mod1_add : forall x y, y > 0 -> mod1 (x + y) y = mod1 x y.
  intros.
  rewrite mod1_is_mod, mod1_is_mod by omega.
  rewrite <- Z.mul_1_l with (n := x + y), <- Zred_factor4, Z.mul_1_l.
  apply Z.mod_add; omega.
Qed.

Lemma Nth_rotate_aux_left : forall {l} (v : Vector.t bool (S l)) (n:nat) (i:nat), (Z.of_nat i < (Z.succ (Z.of_nat l))) -> ((nth_aux v (Z.of_nat i)) = (nth_aux (rotate_left_aux v n) (mod1 ((Z.of_nat i) + (Z.of_nat n)) (Z.succ (Z.of_nat l))))).
  induction n; intros.
  simpl.
  rewrite <- Zplus_0_r_reverse, mod1_out; [trivial|omega].
  rewrite Nat2Z.inj_succ, <- Zplus_succ_r_reverse.
  rewrite IHn by trivial.
  case (Z.eq_dec (mod1 (Z.of_nat i + Z.of_nat n) (Z.succ (Z.of_nat l))) (Z.of_nat l)); intro e.
  rewrite e, mod1_succ_high by omega.
  apply nth_predl_is_last.
  rewrite mod1_succ_low.
  change (nth_aux (rotate_left_aux v n)
                  (mod1 (Z.of_nat i + Z.of_nat n) (Z.succ (Z.of_nat l))) =
          nth_aux ((Vector.last (rotate_left_aux v n)) :: (Vector.shiftout (rotate_left_aux v n)))
                  (Z.succ (mod1 (Z.of_nat i + Z.of_nat n) (Z.succ (Z.of_nat l))))).
  rewrite nth_cons.
  symmetry; apply nth_aux_shiftout_not_last.
  omega.
  intro.
  assert (0 <= mod1 (Z.of_nat i + Z.of_nat n) (Z.succ (Z.of_nat l))).
  apply mod1_nat; omega.
  omega.
  omega.
  assert (mod1 (Z.of_nat i + Z.of_nat n) (Z.succ (Z.of_nat l)) <
          Z.succ (Z.of_nat l)).
  apply mod1_nat_high_bound_lt; omega.
  omega.
Qed.

Lemma Nth_rotate_aux_right : forall {l} (v : Vector.t bool (S l)) (n:nat) (i:nat),
  (Z.of_nat i < (Z.succ (Z.of_nat l))) ->
  (nth_aux (rotate_right_aux v n) (Z.of_nat i)) = (nth_aux v (mod1 ((Z.of_nat i) + (Z.of_nat n)) (Z.succ (Z.of_nat l)))).
  induction n.
  simpl; intros; rewrite <- Zplus_0_r_reverse, mod1_out; [trivial|omega].
  intros; rewrite Nat2Z.inj_succ, <-Z.add_succ_comm, <-Nat2Z.inj_succ.
  case (Z.eq_dec (Z.of_nat i) (Z.of_nat l)); intro e.
  rewrite Nat2Z.inj_succ, e.
  simpl (rotate_right_aux v (S n)).
  rewrite nth_aux_shiftin_high.
  rewrite Z.add_comm, mod1_add by omega.
  rewrite <-nth_zero_is_hd.
  apply (IHn 0%nat); simpl; omega.
  rewrite <-IHn by (rewrite Nat2Z.inj_succ; omega).
  simpl (rotate_right_aux v (S n)).
  rewrite nth_aux_shiftin_low by omega.
  rewrite Nat2Z.inj_succ.
  apply nth_aux_tl; omega.
Qed.

Lemma bvec_to_nat_tl: forall {l} v, Z.of_nat (bvec_to_nat l (Vector.tl v)) = Z.of_nat (bvec_to_nat (S l) v) / 2.
  apply Vector.rectS; intros.
  simpl.
  symmetry; case a.
  apply Zdiv_1_l; omega.
  apply Zdiv_0_l.
  simpl (Vector.tl (a :: v)).
  case a.
  change (Z.of_nat (bvec_to_nat (S n) v) =
   Z.of_nat (S (2 * bvec_to_nat (S n) v)) / 2).
  rewrite Nat2Z.inj_succ, Nat2Z.inj_mul, Z.mul_comm, <-Z.add_1_l.
  rewrite Z.div_add, Zdiv_1_l by omega; omega.
  change (Z.of_nat (bvec_to_nat (S n) v) =
   Z.of_nat (2 * bvec_to_nat (S n) v) / 2).
  rewrite Nat2Z.inj_mul, Z.mul_comm, Z_div_mult_full; omega.
Qed.

Lemma bvec_to_nat_shiftin: forall {l} v, bvec_to_nat (S l) (Vector.shiftin false v) = bvec_to_nat l v.
  induction v.
  easy.
  case h; simpl; rewrite IHv; easy.
Qed.

1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
Lemma bvec_to_nat_shiftout_mod1 : forall {l} v, Z.of_nat (bvec_to_nat l (Vector.shiftout v)) = (Z.of_nat (bvec_to_nat (S l) v)) mod (Pow2int.pow2 (Z.of_nat l)).
  intros.
  revert l v.
  apply Vector.rectS; intros.
  simpl.
  symmetry; apply Zmod_1_r.
  case a.
  change (Z.of_nat (S (2 * bvec_to_nat n (Vector.shiftout v))) = (Z.of_nat (S (2 * bvec_to_nat (S n) v))) mod (Pow2int.pow2 (Z.of_nat (S n)))).
  rewrite Nat2Z.inj_succ with (n := n), <-Z.add_1_r with (n := Z.of_nat n), Pow2int.Power_s by omega.
  rewrite Nat2Z.inj_succ, Nat2Z.inj_succ, <-Z.add_1_l, <-Z.add_1_l, Nat2Z.inj_mul, Nat2Z.inj_mul.
  rewrite Z.rem_mul_r.
  rewrite Zmod_odd.
  assert (Z.odd (1 + Z.of_nat 2 * Z.of_nat (bvec_to_nat (S n) v)) = true) by (rewrite Z.odd_add_mul_2; easy).
  rewrite H0.
  rewrite Int.Comm1 with (y := Z.of_nat (bvec_to_nat (S n) v)), Z_div_plus_full, Zdiv_1_l by easy.
  rewrite H; easy.
  omega.
  apply Pow2int.pow2pos; omega.
  change (Z.of_nat (2 * bvec_to_nat n (Vector.shiftout v)) = (Z.of_nat (2 * bvec_to_nat (S n) v)) mod (Pow2int.pow2 (Z.of_nat (S n)))).
  rewrite Nat2Z.inj_succ with (n := n), <-Z.add_1_r with (n := Z.of_nat n), Pow2int.Power_s by omega.
  rewrite Nat2Z.inj_mul, Nat2Z.inj_mul.
  rewrite Zmult_mod_distr_l.
  rewrite H; trivial.
Qed.

(* Why3 goal *)
Lemma two_power_size_val : (two_power_size = (bv.Pow2int.pow2 size)).
  trivial.
Qed.

(* Why3 goal *)
Lemma max_int_val : (max_int = (two_power_size - 1%Z)%Z).
  trivial.
Qed.

Lemma max_int_S : (two_power_size = max_int + 1)%Z.
  rewrite max_int_val; auto with zarith.
Qed.

(* Why3 goal *)
Definition to_int: t -> Z.
  exact (twos_complement size_bv).
Defined.

(* Why3 goal *)
Definition to_uint: t -> Z.
  exact (fun x => Z.of_nat (bvec_to_nat size_bv x)).
Defined.

(* to_uint helper lemmas *)

Clément Fumex's avatar
Clément Fumex committed
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
Lemma to_uint_lsr_aux : forall (v:t) (n:nat), ((to_uint (lsr v
  (Z.of_nat n))) = (div (to_uint v) (Pow2int.pow2 (Z.of_nat n)))).
  unfold div.
  intros; case Z_le_dec; [|intro e; destruct e; apply Z_mod_lt, lt_sym, Pow2int.pow2pos; omega].
  revert v n.
  induction n; intro.
  simpl.
  assert (lsr v 0 = v) as H by easy; rewrite H; symmetry; apply Zdiv_1_r.
  unfold lsr.
  rewrite Nat2Z.id.
  simpl BshiftRl_iter.
  unfold BshiftRl, Bhigh, to_uint, div.
  rewrite bvec_to_nat_tl.
  rewrite bvec_to_nat_shiftin.
  rewrite Nat2Z.inj_succ, <-Z.add_1_r, Pow2int.Power_s by omega.
  rewrite Z.mul_comm, <-Zdiv_Zdiv.
  rewrite <- Nat2Z.id with (n := n).
  change (to_uint (lsr v (Z.of_nat n)) / 2 =
          to_uint v / Pow2int.pow2 (Z.of_nat (Z.to_nat (Z.of_nat n))) / 2).
  rewrite Nat2Z.id.
  rewrite IHn.
  omega.
  apply Z_mod_lt, lt_sym, Pow2int.pow2pos; omega.
  apply Z.lt_le_incl, Pow2int.pow2pos; omega.
  omega.
Qed.

1105
(* end of to_uint helpers *)
Clément Fumex's avatar
Clément Fumex committed
1106
1107

(* Why3 goal *)
1108
1109
Definition of_int: Z -> t.
  exact (fun x => nat_to_bvec size_bv (Z.to_nat x)).
Clément Fumex's avatar
Clément Fumex committed
1110
1111
1112
Defined.

(* Why3 goal *)
1113
1114
1115
1116
1117
1118
Lemma to_uint_extensionality : forall (v:t) (v':t),
  ((to_uint v) = (to_uint v')) -> (v = v').
  unfold to_uint.
  intros v v'.
  rewrite Nat2Z.inj_iff.
  apply bvec_to_nat_extensionality.
Clément Fumex's avatar
Clément Fumex committed
1119
1120
1121
Qed.

(* Why3 goal *)
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
Lemma to_int_extensionality : forall (v:t) (v':t),
  ((to_int v) = (to_int v')) -> (v = v').
  apply twos_complement_extensionality.
Qed.

(* Why3 assumption *)
Definition uint_in_range (i:Z): Prop := (0%Z <= i)%Z /\ (i <= max_int)%Z.

Lemma size_in_range: uint_in_range size.
  unfold uint_in_range, max_int, size, size.
  split; [omega|apply Z.lt_le_pred, id_lt_pow2].
Qed.

Lemma mod1_in_range : forall x, uint_in_range (mod1 x two_power_size).
  split;[|apply Zlt_succ_le;rewrite <-Z.add_1_r, <-max_int_S];
  apply Mod_bound; easy.
Qed.
Clément Fumex's avatar
Clément Fumex committed
1139
1140

(* Why3 goal *)
1141
1142
1143
1144
1145
1146
1147
Lemma to_uint_bounds : forall (v:t), (uint_in_range (to_uint v)).
  intros v.
  unfold to_uint, uint_in_range.
  split.
  omega.
  assert (max_int = Z.of_nat (Z.to_nat max_int)).
  rewrite Z2Nat.id.
Clément Fumex's avatar
Clément Fumex committed
1148
  easy.
1149
1150
1151
1152
1153
1154
1155
1156
1157
  unfold max_int, size.
  apply max_int_nat.
  rewrite H.
  apply inj_le.
  unfold max_int.
  change (bvec_to_nat size_bv v <= Z.to_nat (Z.pred (Pow2int.pow2 size)))%nat.
  rewrite Z2Nat.inj_pred.
  apply NPeano.Nat.lt_le_pred.
  apply bvec_to_nat_range.
Clément Fumex's avatar
Clément Fumex committed
1158
1159
Qed.

1160
1161
1162
1163
1164
1165
1166
1167
1168
Lemma to_uint_bounds_lt: forall v, (0%Z <= to_uint v < two_power_size)%Z.
  split.
  apply to_uint_bounds.
  rewrite max_int_S.
  apply Zle_lt_succ, to_uint_bounds.
Qed.

Lemma to_uint_nat : forall v, (0 <= to_uint v)%Z.
  apply to_uint_bounds.
Clément Fumex's avatar
Clément Fumex committed
1169
1170
1171
1172
Qed.

Lemma to_uint_lsl_aux : forall (v:t) (n:nat), ((to_uint (lsl v
  (Z.of_nat n))) = (mod1 ((to_uint v) * (Pow2int.pow2 (Z.of_nat n)))%Z
1173
  two_power_size)).
Clément Fumex's avatar
Clément Fumex committed
1174
1175
1176
  intros v n.
  rewrite mod1_is_mod.
  Focus 2.
1177
  easy.
Clément Fumex's avatar
Clément Fumex committed
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
  induction n.
  simpl.
  apply Zmod_unique with (q := 0).
  apply to_uint_bounds_lt.
  assert (lsl v 0 = v) as H by easy; rewrite H; omega.
  unfold lsl.
  rewrite Nat2Z.id.
  unfold BshiftL, Bcons, to_uint.
  simpl BshiftL_iter.
  rewrite <- Nat2Z.id with (n := n).
1188
  change (Z.of_nat (2 * bvec_to_nat last_bit (Vector.shiftout (lsl v (Z.of_nat n)))) = (Z.of_nat (bvec_to_nat size_bv v) * Pow2int.pow2 (Z.of_nat (S (Z.to_nat (Z.of_nat n)))))  mod two_power_size)%Z.
Clément Fumex's avatar
Clément Fumex committed
1189
1190
1191
  rewrite Nat2Z.id.
  rewrite Nat2Z.inj_mul.
  rewrite bvec_to_nat_shiftout_mod1.
1192
1193
1194
  change (2 * ((to_uint (lsl v (Z.of_nat n))) mod (Pow2int.pow2 (Z.of_nat last_bit))) = (to_uint v * Pow2int.pow2 (Z.of_nat (S n))) mod two_power_size).
  unfold two_power_size.
  rewrite size_int_S, Nat2Z.inj_succ, <-Z.add_1_r, Pow2int.Power_s by omega.
Clément Fumex's avatar
Clément Fumex committed
1195
1196
1197
1198
  rewrite Zmult_assoc, Int.Comm1 with (y := 2), <-Z.add_1_r, Pow2int.Power_s by omega.
  rewrite Zmult_assoc_reverse, Zmult_mod_distr_l.
  rewrite Z.mul_cancel_l by omega.
  rewrite IHn.
1199
  unfold two_power_size.
Clément Fumex's avatar
Clément Fumex committed
1200
1201
1202
1203
1204
1205
  rewrite size_int_S, <-Z.add_1_r, Pow2int.Power_s by omega.
  apply mod_mod_mult.
  apply lt_sym, Pow2int.pow2pos; omega.
  omega.
Qed.

1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
(* Why3 goal *)
Lemma to_uint_of_int : forall (i:Z), (uint_in_range i) ->
  ((to_uint (of_int i)) = i).
  intros i h1; destruct h1.
  unfold to_uint, of_int.
  rewrite bvec_to_nat_nat_to_bvec.
  apply Z2Nat.id; easy.
  rewrite Z2Nat.id; easy.
Qed.

(* Why3 goal *)
Lemma Of_int_zero : (zero = (of_int 0%Z)).
  apply Nat_to_bvec_zero.
Qed.

(* Why3 goal *)
Lemma Of_int_ones : (ones = (of_int max_int)).
  apply Nat_to_bvec_ones.
Qed.

(* Why3 assumption *)
Definition ult (x:t) (y:t): Prop := ((to_uint x) < (to_uint y))%Z.

(* Why3 assumption *)
Definition ule (x:t) (y:t): Prop := ((to_uint x) <= (to_uint y))%Z.

(* Why3 assumption *)
Definition ugt (x:t) (y:t): Prop := ((to_uint y) < (to_uint x))%Z.

(* Why3 assumption *)
Definition uge (x:t) (y:t): Prop := ((to_uint y) <= (to_uint x))%Z.

(* Why3 assumption *)
Definition slt (v1:t) (v2:t): Prop := ((to_int v1) < (to_int v2))%Z.

(* Why3 assumption *)
Definition sle (v1:t) (v2:t): Prop := ((to_int v1) <= (to_int v2))%Z.

(* Why3 assumption *)
Definition sgt (v1:t) (v2:t): Prop := ((to_int v2) < (to_int v1))%Z.

(* Why3 assumption *)
Definition sge (v1:t) (v2:t): Prop := ((to_int v2) <= (to_int v1))%Z.

(* Why3 goal *)
Definition add: t -> t -> t.
  exact (fun v v' => of_int (mod1 (to_uint v + to_uint v') two_power_size)).
Defined.

(* Why3 goal *)
Lemma to_uint_add : forall (v1:t) (v2:t), ((to_uint (add v1
  v2)) = (int.EuclideanDivision.mod1 ((to_uint v1) + (to_uint v2))%Z
  two_power_size)).
  intros v1 v2.
  apply to_uint_of_int, mod1_in_range.
Qed.

(* Why3 goal *)
Definition sub: t -> t -> t.
  exact (fun v v' => of_int (mod1 (to_uint v - to_uint v') two_power_size)).
Defined.

(* Why3 goal *)
Lemma to_uint_sub : forall (v1:t) (v2:t), ((to_uint (sub v1
  v2)) = (int.EuclideanDivision.mod1 ((to_uint v1) - (to_uint v2))%Z
  two_power_size)).
  intros v1 v2.
  apply to_uint_of_int, mod1_in_range.
Qed.

(* Why3 goal *)
Definition neg: t -> t.
  exact (fun v => of_int (mod1 (- to_uint v) two_power_size)).
Defined.

(* Why3 goal *)
Lemma to_uint_neg : forall (v:t),
  ((to_uint (neg v)) = (int.EuclideanDivision.mod1 (-(to_uint v))%Z
  two_power_size)).
  intros v.
  apply to_uint_of_int, mod1_in_range.
Qed.

(* Why3 goal *)
Definition mul: t -> t -> t.
  exact (fun v v' => of_int (mod1 (to_uint v * to_uint v') two_power_size)).
Defined.

(* Why3 goal *)
Lemma to_uint_mul : forall (v1:t) (v2:t), ((to_uint (mul v1
  v2)) = (int.EuclideanDivision.mod1 ((to_uint v1) * (to_uint v2))%Z
  two_power_size)).
  intros v1 v2.
  apply to_uint_of_int, mod1_in_range.
Qed.

(* Why3 goal *)
Definition udiv: t -> t -> t.
  exact (fun v v' => of_int (div (to_uint v) (to_uint v'))).
Defined.

(* Why3 goal *)
Lemma to_uint_udiv : forall (v1:t) (v2:t), ((to_uint (udiv v1
  v2)) = (int.EuclideanDivision.div (to_uint v1) (to_uint v2))).
  intros v1 v2.
  apply to_uint_of_int.
  apply le_le_le with (t := to_uint v1).
  apply to_uint_bounds.
  case (Z.eq_dec (to_uint v2) 0); intro.
  rewrite e; unfold uint_in_range, div.
  rewrite Zmod_0_r; simpl.
  rewrite Zdiv_0_r.
  split; [easy|apply to_uint_nat].
  apply Div_bound; split.
  apply to_uint_nat.
  apply Z.le_neq; split; [apply to_uint_nat|omega].
Qed.

(* Why3 goal *)
Definition urem: t -> t -> t.
  exact (fun v v' => of_int (mod1 (to_uint v) (to_uint v'))).
Defined.

(* Why3 goal *)
Lemma to_uint_urem : forall (v1:t) (v2:t), ((to_uint (urem v1
  v2)) = (int.EuclideanDivision.mod1 (to_uint v1) (to_uint v2))).
  intros v1 v2.
  apply to_uint_of_int.
  case (Z.eq_dec (to_uint v2) 0); intro.
  rewrite e.
  unfold uint_in_range, mod1; simpl.
  rewrite <- Zminus_0_l_reverse; apply to_uint_bounds.
  apply le_lt_le with (t := Z.abs (to_uint v2)).
  rewrite Z.abs_eq; apply to_uint_bounds.
  apply Mod_bound; trivial.
Qed.

(* Why3 goal *)
Definition nth_bv: t -> t -> bool.
  exact (fun v w => nth v (to_uint w)).
Defined.

(* Why3 goal *)
Lemma Nth_bv_is_nth : forall (x:t) (i:t), ((nth x (to_uint i)) = (nth_bv x
  i)).
  trivial.
Qed.

(* Why3 goal *)
Lemma Nth_bv_is_nth2 : forall (x:t) (i:Z), (uint_in_range i) -> ((nth_bv x
  (of_int i)) = (nth x i)).
  intros x i h1.
  rewrite <-Nth_bv_is_nth.
  rewrite to_uint_of_int by auto.
  reflexivity.
Qed.

(* Why3 goal *)
Definition lsr_bv: t -> t -> t.
  exact (fun v w => lsr v (to_uint w)).
Defined.

(* Why3 goal *)
Lemma lsr_bv_is_lsr : forall (x:t) (n:t), ((lsr_bv x n) = (lsr x
  (to_uint n))).
  easy.
Qed.

(* Why3 goal *)
Lemma to_uint_lsr : forall (v:t) (n:t), ((to_uint (lsr_bv v
  n)) = (int.EuclideanDivision.div (to_uint v)
  (bv.Pow2int.pow2 (to_uint n)))).
  intros v n.
  apply to_uint_lsr_aux.
Qed.

(* Why3 goal *)
Definition asr_bv: t -> t -> t.
  exact (fun v w => asr v (to_uint w)).
Defined.

(* Why3 goal *)
Lemma asr_bv_is_asr : forall (x:t) (n:t), ((asr_bv x n) = (asr x
  (to_uint n))).
  easy.
Qed.

(* Why3 goal *)
Definition lsl_bv: t -> t -> t.
  exact (fun v w => lsl v (to_uint w)).
Defined.

(* Why3 goal *)
Lemma lsl_bv_is_lsl : forall (x:t) (n:t), ((lsl_bv x n) = (lsl x
  (to_uint n))).
  easy.
Qed.

Clément Fumex's avatar
Clément Fumex committed
1404
1405
1406
(* Why3 goal *)
Lemma to_uint_lsl : forall (v:t) (n:t), ((to_uint (lsl_bv v
  n)) = (int.EuclideanDivision.mod1 ((to_uint v) * (bv.Pow2int.pow2 (to_uint n)))%Z
1407
  two_power_size)).
Clément Fumex's avatar
Clément Fumex committed
1408
1409
1410
1411
1412
  intros v n.
  apply to_uint_lsl_aux.
Qed.

(* Why3 goal *)
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
Definition rotate_right: t -> t -> t.
  exact (fun b p => rotate_right_aux b (bvec_to_nat size_bv p)).
Defined.

(* Why3 goal *)
Definition rotate_left: t -> t -> t.
  exact (fun b p => rotate_left_aux b (bvec_to_nat size_bv p)).
Defined.

(* Why3 goal *)
Lemma Nth_rotate_left : forall (v:t) (n:t) (i:t), (ult i (of_int size)) ->
  ((ult n (sub ones (of_int size))) -> ((nth_bv v i) = (nth_bv (rotate_left v
  n) (urem (add i n) (of_int size))))).
  intros v n i h1 h2.
  unfold ult in h1, h2.
  rewrite to_uint_of_int in h1 by apply size_in_range.
  unfold nth_bv.
  rewrite to_uint_urem, to_uint_add.
  rewrite mod1_out with (x := (to_uint i + to_uint n)).
  rewrite to_uint_of_int by apply size_in_range.
  rewrite size_int_S.
  apply Nth_rotate_aux_left.
  fold (to_uint i); rewrite <- size_int_S; trivial.
  split.
  apply Z.add_nonneg_nonneg; apply to_uint_nat.
  rewrite to_uint_sub in h2.
  rewrite to_uint_of_int in h2 by apply size_in_range.
  rewrite Of_int_ones, to_uint_of_int, mod1_out in h2.
  rewrite max_int_S.
  omega.
  split.
  unfold max_int.
  rewrite <- Z.le_add_le_sub_l.
  simpl (_+_).
  apply Z.lt_le_pred, id_lt_pow2.
  unfold two_power_size, max_int.
  rewrite size_int_S.
  omega.
  split.
  unfold max_int.
  apply Z.lt_le_pred, Pow2int.pow2pos.
  unfold size; omega.
  omega.
Qed.

(* Why3 goal *)
Lemma Nth_rotate_right : forall (v:t) (n:t) (i:t), (ult i (of_int size)) ->
  ((ult n (sub ones (of_int size))) -> ((nth_bv (rotate_right v n)
  i) = (nth_bv v (urem (add i n) (of_int size))))).
  intros v n i h1 h2.
  unfold ult in h1, h2.
  rewrite to_uint_of_int in h1 by apply size_in_range.
  unfold nth_bv.
  rewrite to_uint_urem, to_uint_add.
  rewrite mod1_out with (x := (to_uint i + to_uint n)).
  rewrite to_uint_of_int by apply size_in_range.
  rewrite size_int_S.
  apply Nth_rotate_aux_right.
  fold (to_uint i); rewrite <- size_int_S; trivial.
  split.
  apply Z.add_nonneg_nonneg; apply to_uint_nat.
  rewrite to_uint_sub in h2.
  rewrite to_uint_of_int in h2 by apply size_in_range.
  rewrite Of_int_ones, to_uint_of_int, mod1_out in h2.
  rewrite max_int_S.
  omega.
  split.
Clément Fumex's avatar
Clément Fumex committed
1480
  unfold max_int.
1481
1482
1483
1484
1485
1486
1487
1488
1489
  rewrite <- Z.le_add_le_sub_l.
  simpl (_+_).
  apply Z.lt_le_pred, id_lt_pow2.
  unfold two_power_size, max_int, size.
  omega.
  split.
  unfold max_int.
  apply Z.lt_le_pred, Pow2int.pow2pos.
  unfold size; omega.
Clément Fumex's avatar
Clément Fumex committed
1490
1491
  omega.
Qed.
Clement Fumex's avatar
Clement Fumex committed
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501

(* Why3 assumption *)
Definition eq_sub_bv (a:t) (b:t) (i:t) (n:t): Prop := let mask :=
  (lsl_bv (sub (lsl_bv (of_int 1%Z) n) (of_int 1%Z)) i) in ((bw_and b
  mask) = (bw_and a mask)).

(* Why3 assumption *)
Definition eq_sub (a:t) (b:t) (i:Z) (n:Z): Prop := forall (j:Z),
  ((i <= j)%Z /\ (j < (i + n)%Z)%Z) -> ((nth a j) = (nth b j)).

1502
1503
1504
Lemma in_range_1 : uint_in_range 1.
  split; auto with zarith.
  change (Z.succ 0 <= max_int)%Z; apply Zlt_le_succ.
1505
  unfold max_int, size, size.
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
  apply Zlt_succ_pred.
  apply Z.le_lt_trans with (m := Z.of_nat (S last_bit)).
  rewrite Nat2Z.inj_succ; auto with zarith.
  apply id_lt_pow2.
Qed.

Lemma max_int_neq_1 : max_int + 1 <> 0.
  intro; symmetry in H; revert H.
  apply Z.lt_neq, Zle_lt_succ, max_int_nat.
Qed.

(* mask (S n) = lsl (mask n) 1 + 1 *)
Lemma mask_succ :
  forall n,
    sub (lsl (of_int 1) (Z.of_nat (S n))) (of_int 1%Z)
    = add (lsl (sub (lsl (of_int 1%Z) (Z.of_nat n)) (of_int 1%Z)) (Z.of_nat 1)) (of_int 1%Z).
  intro; apply to_uint_extensionality.
  rewrite to_uint_add.
  rewrite to_uint_lsl_aux.
  assert (Pow2int.pow2 (Z.of_nat 1) = 2) as tmp by auto with zarith; rewrite tmp.
  symmetry; rewrite to_uint_sub; symmetry.
  rewrite to_uint_of_int by (apply in_range_1).
1528
1529
  rewrite mod1_is_mod,mod1_is_mod,mod1_is_mod by easy.
  rewrite Z.mul_mod_idemp_l by easy.
1530
1531
1532
  rewrite Z.mul_sub_distr_r.
  rewrite <- Zminus_mod_idemp_l.
  rewrite to_uint_lsl_aux.
1533
1534
  rewrite mod1_is_mod by easy.
  rewrite Z.mul_mod_idemp_l by easy.
1535
1536
  rewrite Zmult_assoc_reverse, (Int.Comm1 (Pow2int.pow2 (Z.of_nat n)) 2).
  rewrite <-Pow2int.Power_s by omega.
1537
  rewrite <-mod1_is_mod with (x := to_uint (of_int 1) * Pow2int.pow2 (Z.of_nat n + 1)) by easy.
1538
1539
1540
1541
1542
1543
1544
1545
  rewrite Z.add_1_r with (n := Z.of_nat n).
  rewrite <- Nat2Z.inj_succ.
  rewrite <-to_uint_lsl_aux.
  rewrite Zplus_mod_idemp_l.
  rewrite <-Z.sub_sub_distr.
  assert (1 * 2 - 1 = to_uint (of_int 1)).
  simpl; symmetry; apply to_uint_of_int; apply in_range_1.
  rewrite H.
1546
  rewrite <-mod1_is_mod by easy.
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
  apply to_uint_sub.
Qed.

Lemma mask_succ_tmp :
  forall n,
    add (lsl (sub (lsl (of_int 1%Z) (Z.of_nat n)) (of_int 1%Z)) (Z.of_nat 1)) (of_int 1%Z)
    = bw_or (lsl (sub (lsl (of_int 1%Z) (Z.of_nat n)) (of_int 1%Z)) (Z.of_nat 1)) (of_int 1%Z).
  intro.

  Lemma add_and_or :
    forall v, nth v 0 = false ->
              add v (of_int 1) = bw_or v (of_int 1).
    Definition add_aux {l} (v w : Vector.t bool l) : Vector.t bool l.
      exact (nat_to_bvec l (Z.to_nat (mod1 (Z.of_nat (bvec_to_nat l v) + Z.of_nat (bvec_to_nat l w)) (Pow2int.pow2 (Z.of_nat l))))).
    Defined.
    Lemma add_is_add_aux : forall v w, add v w = add_aux v w.
1563
      auto.
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
    Qed.
    Lemma add_and_or_aux :
      forall {l} v, @nth_aux l v 0 = false ->
                    add_aux v (nat_to_bvec l 1)
                    = Vector.map2 (fun x y => x || y) v (nat_to_bvec l 1).
      destruct v; auto.
      intro.
      assert (h = false) as hf by auto; rewrite hf.
      simpl.
      unfold add_aux.
      change (
          nat_to_bvec (S n)
                      (Z.to_nat
                         (mod1
                            (Z.of_nat (2 * (bvec_to_nat n v)) +
                             Z.of_nat (1 + 2 * (bvec_to_nat n (nat_to_bvec n 0))))
                            (Pow2int.pow2 (Z.of_nat (S n))))) =
          true :: Vector.map2 (fun x y : bool => x || y) v (nat_to_bvec n 0)).
      rewrite <-Nat_to_bvec_zero, bvec_to_nat_zero.
      change (Z.of_nat (1 + 2 * 0)) with 1.
      set (mod1 (Z.of_nat (2 * bvec_to_nat n v) + 1)
           (Pow2int.pow2 (Z.of_nat (S n)))).
      simpl nat_to_bvec.
      rewrite Z2Nat.id by (apply mod1_nat, Z.lt_le_pred, max_int_nat).
      assert (Z.odd z = true).
      subst z.
      rewrite Zodd_mod, mod1_is_mod by auto with zarith.
      rewrite Nat2Z.inj_succ, <- Z.add_1_r, Pow2int.Power_s by auto with zarith.
      rewrite Int.Comm1, (mod_mod_mult _ 2 _)by auto with zarith.
      rewrite <-Zodd_mod, Int.Comm, Nat2Z.inj_mul, Z.odd_add_mul_2; trivial.
      rewrite H0.
      cut (nat_to_bvec n (Div2.div2 (Z.to_nat z))
           = Vector.map2 (fun x y : bool => x || y) v (Vector.const false n)).
      intro t; rewrite t; trivial.
      case (Z_lt_le_dec (Z.of_nat (2 * bvec_to_nat n v) + 1) (Pow2int.pow2 (Z.of_nat (S n)))); intro.
      subst z.
      revert H0.
      rewrite mod1_out by auto with zarith.
      rewrite Z.add_1_r.
      intro.
      rewrite <-Nat2Z.inj_succ, Nat2Z.id.
      rewrite Z.odd_succ, <-even_is_even in H0.
      rewrite <-(Div2.even_div2 _ H0), Div2.div2_double.
      Lemma nat_to_bvec_bvec_to_nat:
        forall {l} v, nat_to_bvec l (bvec_to_nat l v) = v.
        intros; apply bvec_to_nat_extensionality.
        apply bvec_to_nat_nat_to_bvec.
        apply Z.lt_le_pred.
        rewrite <-Z2Nat.id by (transitivity (Pow2int.pow2 (Z.of_nat l) - 1); [apply max_int_nat|auto with zarith]).
        apply inj_lt, bvec_to_nat_range.
      Qed.
      rewrite nat_to_bvec_bvec_to_nat.
      Lemma tmp2:
        forall {l} v,
          Vector.map2 (fun x y : bool => x || y) v (Vector.const false l) = v.
        induction v; auto.
        simpl.
        rewrite orb_false_r.
        rewrite IHv; trivial.
      Qed.
      symmetry; apply tmp2.
      absurd (Z.of_nat (bvec_to_nat n v) < Pow2int.pow2 (Z.of_nat n)).
      apply Zle_not_lt.
      rewrite Nat2Z.inj_succ in l.
      rewrite <- Z.add_1_r, Pow2int.Power_s in l by auto with zarith.
      apply Zle_lt_succ in l.
      rewrite <-Z.add_1_r, Zplus_assoc_reverse, Nat2Z.inj_mul in l.
      simpl (1 + 1) in l.
      rewrite Zred_factor3 in l.
      rewrite <-(Z.mul_lt_mono_pos_l 2) in l by omega.
      rewrite Z.add_1_l in l.
      apply Zlt_succ_le in l; trivial.
      rewrite Z2Nat.inj_lt, Nat2Z.id.
      apply (bvec_to_nat_range v).
      auto with zarith.
      transitivity (Pow2int.pow2 (Z.of_nat n) - 1).
      apply max_int_nat.
      auto with zarith.
    Qed.
    intros.
    rewrite add_is_add_aux.
    apply add_and_or_aux.
    trivial.
  Qed.
  simpl.
  apply add_and_or.
  intros; apply Lsl_nth_low; auto with zarith.
Qed.

Lemma mask_succ_2 :
  forall n,
    sub (lsl (of_int 1) (Z.of_nat (S n))) (of_int 1%Z)
  = bw_or (lsl (sub (lsl (of_int 1%Z) (Z.of_nat n)) (of_int 1%Z)) (Z.of_nat 1)) (of_int 1%Z).
  intro; rewrite mask_succ.
  apply mask_succ_tmp.
Qed.

Lemma nth_bit_pred_high :
  forall n i,
1663
    to_uint i < size ->
1664
1665
1666
1667
1668
    to_uint i < Z.of_nat n ->
    nth_bv (sub (lsl (of_int 1%Z) (Z.of_nat n)) (of_int 1%Z)) i = true.
  induction n;intros.
  assert (Z.of_nat 0 <= to_uint i) by apply to_uint_bounds.
  omega.
1669
  rewrite <-Nth_bv_is_nth.
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
  rewrite mask_succ_2; simpl.
  unfold bw_or, nth.
  rewrite <-nth_aux_map2.
  Focus 2.
  split; auto with zarith.
  apply to_uint_bounds.

  apply orb_true_iff.
  case (Z_lt_le_dec (to_uint i) 1); intro.
  right.
  assert (to_uint i = 0).
  pose (to_uint_bounds i).
  unfold uint_in_range in u; auto with zarith.
  rewrite H1; apply nth_zero_is_hd.

  left.
  rewrite Lsl_nth_high; auto with zarith.

  assert (to_uint i - 1 = to_uint (sub i (of_int 1))).
  rewrite to_uint_sub by auto with zarith.
  rewrite to_uint_of_int by (apply in_range_1).
  rewrite mod1_out; trivial.
  split; auto with zarith.
1693
  rewrite max_int_S.
1694
1695
  apply Z.lt_lt_pred, Zle_lt_succ, to_uint_bounds.

1696
  rewrite H1, Nth_bv_is_nth.
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
  apply IHn; rewrite <-H1; rewrite Nat2Z.inj_succ in H0; omega.
Qed.

Lemma nth_bit_pred_low :
  forall n i,
    to_uint i >= Z.of_nat n ->
    nth_bv (sub (lsl (of_int 1%Z) (Z.of_nat n)) (of_int 1%Z)) i = false.
  induction n;intros.
  assert (lsl (of_int 1) (Z.of_nat 0) = of_int 1) as h by auto with zarith; rewrite h.
  assert (sub (of_int 1) (of_int 1) = of_int 0).
  apply to_uint_extensionality.
  rewrite to_uint_sub, to_uint_of_int, to_uint_of_int.
  apply mod1_out.
  rewrite two_power_size_val.
1711
  split; [omega|apply Pow2int.pow2pos, Z.lt_le_incl, size_pos].
1712
1713
1714
1715
1716
1717
  split; [easy|apply max_int_nat].
  apply in_range_1.
  rewrite H0, <-Of_int_zero.
  apply Nth_zero.

  rewrite Nat2Z.inj_succ in H.
1718
  rewrite <-Nth_bv_is_nth, mask_succ_2; simpl.
1719
1720

  unfold bw_or, nth.
1721
  case (Z_lt_ge_dec (to_uint i) size); intro.
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
  rewrite <-nth_aux_map2 by (split; auto with zarith).

  apply orb_false_iff.
  split.
  rewrite Lsl_nth_high; auto with zarith.
  assert (to_uint i - 1 = to_uint (sub i (of_int 1))).
  rewrite to_uint_sub by auto with zarith.
  rewrite to_uint_of_int by (apply in_range_1).
  rewrite mod1_out; trivial.
  split; auto with zarith.
1732
  rewrite max_int_S.
Clément Fumex's avatar