Definition Blist := list bool.
Open Scope Z_scope.
Inductive InBlocks {A : Type} (n : nat) : list A -> Prop :=
| InBlocks_nil : InBlocks n []
| InBlocks_block : forall (front back full : list A),
length front = n ->
full = front ++ back ->
InBlocks n back ->
InBlocks n full.
(* ----- Inductive *)
Inductive bytes_bits_lists : Blist -> list byte -> Prop :=
| eq_empty : bytes_bits_lists nil nil
| eq_cons : forall (bits : Blist) (bytes : list byte)
(b0 b1 b2 b3 b4 b5 b6 b7 : bool) (b : byte),
bytes_bits_lists bits bytes ->
convertByteBits [b0; b1; b2; b3; b4; b5; b6; b7] b ->
bytes_bits_lists (b0 :: b1 :: b2 :: b3 :: b4 :: b5 :: b6 :: b7 :: bits)
(b :: bytes).
(* ----- Computational *)
(* TODO: assumes Z is positive and in range, does not use Z.positive *)
Definition div_mod (num : Z) (denom : Z) : bool * Z :=
(Z.gtb (num / denom) 0, num mod denom).
Definition byteToBits (byte : byte) : Blist :=
let (b7, rem7) := div_mod (Byte.unsigned byte) 128 in
let (b6, rem6) := div_mod rem7 64 in
let (b5, rem5) := div_mod rem6 32 in
let (b4, rem4) := div_mod rem5 16 in
let (b3, rem3) := div_mod rem4 8 in
let (b2, rem2) := div_mod rem3 4 in
let (b1, rem1) := div_mod rem2 2 in
let (b0, rem0) := div_mod rem1 1 in
[b0; b1; b2; b3; b4; b5; b6; b7].
Fixpoint bytesToBits (bytes : list byte) : Blist :=
match bytes with
| [] => []
| byte :: xs => byteToBits byte ++ bytesToBits xs
end.
Definition bitsToByte (bits : Blist) : byte :=
Byte.repr
match bits with
| b0 :: b1 :: b2 :: b3 :: b4 :: b5 :: b6 :: b7 :: nil =>
(1 * (asZ b0) + 2 * (asZ b1) + 4 * (asZ b2) + 8 * (asZ b3)
+ 16 * (asZ b4) + 32 * (asZ b5) + 64 * (asZ b6) + 128 * (asZ b7))
| _ => -1                  (* should not happen *)
end.
Fixpoint bitsToBytes (bits : Blist) : list byte :=
match bits with
| b0 :: b1 :: b2 :: b3 :: b4 :: b5 :: b6 :: b7 :: xs =>
bitsToByte [b0; b1; b2; b3; b4; b5; b6; b7] :: bitsToBytes xs
| _ => []
end.
(* Definition isbyteZ i := (0 <= i < 256)%Z.  Lemma bitsToByte_isbyteZ b0 b1 b2 b3 b4 b5 b6 b7: isbyteZ (bitsToByte [b0; b1; b2; b3; b4; b5; b6; b7]). *)
(* -------------------- Various theorems and lemmas *)
(* ------------------ Theorems relating inductive and computational definitions *)
(* ----------------------------- *)
(* Relating bits to bytes *)

Lemma bytes_bits_lists_append:
forall (l1 : Blist) (l2 : list byte) (m1 : Blist) (m2 : list byte),
bytes_bits_lists l1 l2
-> bytes_bits_lists m1 m2
-> bytes_bits_lists (l1 ++ m1) (l2 ++ m2).