(************************************************************************)
(*         *   The Coq Proof Assistant / The Coq Development Team       *)
(*  v      *   INRIA, CNRS and contributors - Copyright 1999-2018       *)
(* <O___,, *       (see CREDITS file for the list of authors)           *)
(*   \VV/  **************************************************************)
(*    //   *    This file is distributed under the terms of the         *)
(*         *     GNU Lesser General Public License Version 2.1          *)
(*         *     (see LICENSE file for the text of the license)         *)
(************************************************************************)
Basics ByteVector.
Local Open Scope N_scope.
Local Open Scope program_scope.
(** This file is mostly obsolete, see directly [BinNat] now. *)
(** Compatibility names for some bitwise operations *)
Notation Pxor := Pos.lxor (only parsing).
Notation Nxor := N.lxor (only parsing).
Notation Pbit := Pos.testbit_nat (only parsing).
Notation Nbit := N.testbit_nat (only parsing).
Notation Nxor_eq := N.lxor_eq (only parsing).
Notation Nxor_comm := N.lxor_comm (only parsing).
Notation Nxor_assoc := N.lxor_assoc (only parsing).
Notation Nxor_neutral_left := N.lxor_0_l (only parsing).
Notation Nxor_neutral_right := N.lxor_0_r (only parsing).
Notation Nxor_nilpotent := N.lxor_nilpotent (only parsing).
(** Equivalence of bit-testing functions, either with index in [N] or in [nat]. *)
(** Equivalence of shifts, index in [N] or [nat] *)
(** Correctness proofs for shifts, nat version *)
(** A left shift for positive numbers (used in BigN) *)
(** Semantics of bitwise operations with respect to [N.testbit_nat] *)
(** Equality over functional streams of bits *)
Definition eqf (f g:nat -> bool) := forall n:nat, f n = g n.
Program Instance eqf_equiv : Equivalence eqf.
Local Infix "==" := eqf (at level 70, no associativity).
(** If two numbers produce the same stream of bits, they are equal. *)
Local Notation Step H := (fun n => H (S n)).
Local Close Scope N_scope.
(** Checking whether a number is odd, i.e. if its lower bit is set. *)
Notation Nbit0 := N.odd (only parsing).
Definition Nodd (n:N) := N.odd n = true.
Definition Neven (n:N) := N.odd n = false.
(** a lexicographic order on bits, starting from the lowest bit *)
Fixpoint Nless_aux (a a':N) (p:positive) : bool :=
match p with
| xO p' => Nless_aux (N.div2 a) (N.div2 a') p'
| _ => andb (negb (N.odd a)) (N.odd a')
end.
Definition Nless (a a':N) :=
match N.lxor a a' with
| N0 => false
| Npos p => Nless_aux a a' p
end.
(** Number of digits in a number *)
Notation Nsize := N.size_nat (only parsing).
(** conversions between N and bit vectors. *)
Fixpoint P2Bv (p:positive) : Bvector (Pos.size_nat p) :=
match p return Bvector (Pos.size_nat p) with
| xH => Bvect_true 1%nat
| xO p => Bcons false (Pos.size_nat p) (P2Bv p)
| xI p => Bcons true (Pos.size_nat p) (P2Bv p)
end.
Definition N2Bv (n:N) : Bvector (N.size_nat n) :=
match n as n0 return Bvector (N.size_nat n0) with
| N0 => Bnil
| Npos p => P2Bv p
end.
Fixpoint P2Bv_sized (m : nat) (p : positive) : Bvector m :=
match m with
| O => []
| S m =>
match p with
| xI p => true  :: P2Bv_sized  m p
| xO p => false :: P2Bv_sized  m p
| xH   => true  :: Bvect_false m
end
end.
Definition N2Bv_sized (m : nat) (n : N) : Bvector m :=
match n with
| N0     => Bvect_false m
| Npos p => P2Bv_sized  m p
end.
Definition N2ByteV_sized (m : nat) : N -> ByteVector m :=
of_Bvector ∘ N2Bv_sized (m * 8).
Fixpoint Bv2N (n:nat)(bv:Bvector n) : N :=
match bv with
| Vector.nil _ => N0
| Vector.cons _ false n bv => N.double (Bv2N n bv)
| Vector.cons _ true n bv => N.succ_double (Bv2N n bv)
end.
Arguments Bv2N {n} bv, n bv.
Definition ByteV2N {n : nat} : ByteVector n -> N :=
Bv2N ∘ to_Bvector.
(** The opposite composition is not so simple: if the considered bit vector has some zeros on its right, they will disappear during the return [Bv2N] translation: *)
(** In the previous lemma, we can only replace the inequality by an equality whenever the highest bit is non-null. *)
Proof with simpl; auto.
induction bv...
- constructor.
- destruct h.
+ apply N.succ_double_lt...
+ apply N.double_lt_mono...
Qed.
(** To state nonetheless a second result about composition of conversions, we define a conversion on a given number of bits : *)
#[deprecated(since = "8.9.0", note = "Use N2Bv_sized instead.")]
Fixpoint N2Bv_gen (n:nat)(a:N) : Bvector n :=
match n return Bvector n with
| 0 => Bnil
| S n => match a with
| N0 => Bvect_false (S n)
| Npos xH => Bcons true _ (Bvect_false n)
| Npos (xO p) => Bcons false _ (N2Bv_gen n (Npos p))
| Npos (xI p) => Bcons true _ (N2Bv_gen n (Npos p))
end
end.
(** The first [N2Bv] is then a special case of [N2Bv_gen] *)
(** In fact, if [k] is large enough, [N2Bv_gen k a] contains all digits of [a] plus some zeros. *)
(** Here comes now the second composition result. *)

Lemma N2Bv_Bv2N : forall n (bv:Bvector n),
N2Bv_gen n (Bv2N n bv) = bv.