Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/coqutil/Byte.v
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Require Coq.Init.Byte Coq.Strings.Byte.
Require Import Coq.ZArith.ZArith.
From coqutil Require Import Z.PushPullMod.

Local Open Scope Z_scope.

Expand All @@ -12,6 +13,10 @@ Module byte.

Definition wrap(z: Z): Z := z mod 2 ^ 8.

Definition swrap(z : Z): Z := (z + 2^7) mod 2^8 - 2^7.

Definition signed(b: byte): Z := swrap (byte.unsigned b).

Lemma wrap_range z:
0 <= byte.wrap z < 2 ^ 8.
Proof. apply Z.mod_pos_bound. reflexivity. Qed.
Expand Down Expand Up @@ -122,4 +127,10 @@ Module byte.
(* FIXME isn't this defined somewhere already? *)
Definition and (b1 b2: byte) := byte.of_Z (Z.land (byte.unsigned b1) (byte.unsigned b2)).
Definition xor a b := byte.of_Z (Z.lxor (byte.unsigned a) (byte.unsigned b)).

Lemma swrap_wrap b : byte.swrap (byte.wrap b) = byte.swrap b.
Proof.
cbv [byte.wrap byte.swrap].
Z.mod_equality.
Qed.
End byte.
26 changes: 26 additions & 0 deletions src/coqutil/Word/Properties.v
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
From Coq Require Import ZArith.
Require Import Coq.ZArith.Znumtheory.
Require Import Coq.ZArith.Zpow_facts.
Require Import coqutil.Z.div_mod_to_equations.
Require Import coqutil.Z.Lia Btauto.
Require Import coqutil.Z.PushPullMod.
Require Coq.setoid_ring.Ring_theory.
Require Import coqutil.Z.bitblast.
Require Import coqutil.Word.Interface. Import word.
Require Import coqutil.Byte.

Local Open Scope Z_scope.

Expand Down Expand Up @@ -767,6 +769,30 @@ Module word.

Lemma mul_comm: forall x y : word, word.mul x y = word.mul y x.
Proof. intros. ring. Qed.

Lemma byte_wrap_word_wrap: forall w,
8 <= width ->
byte.wrap (word.wrap w) = byte.wrap w.
Proof.
intros.
cbv [byte.wrap word.wrap].
rewrite <-Zmod_div_mod.
{ reflexivity. }
{ blia. }
{ blia. }
exists (2^(width - 8)).
rewrite <-Z.pow_add_r by blia.
rewrite Z.sub_add.
reflexivity.
Qed.

Lemma byte_swrap_word_wrap: forall w,
8 <= width ->
byte.swrap (word.wrap w) = byte.swrap w.
Proof.
intros.
rewrite <-byte.swrap_wrap, (byte_wrap_word_wrap _ H), byte.swrap_wrap; reflexivity.
Qed.
End WordConvenienceKitchenSink.
End word.

Expand Down
Loading