Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proved admitted Coq lemmas about xor for xor_swap #1466

Merged
merged 2 commits into from
Oct 4, 2021
Merged
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
13 changes: 7 additions & 6 deletions heapster-saw/examples/xor_swap_proofs.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ From Coq Require Import String.
From Coq Require Import Vectors.Vector.
From CryptolToCoq Require Import SAWCoreScaffolding.
From CryptolToCoq Require Import SAWCoreVectorsAsCoqVectors.
From CryptolToCoq Require Import SAWCoreBitvectors.

From CryptolToCoq Require Import SAWCorePrelude.
From CryptolToCoq Require Import CompMExtra.
Expand All @@ -23,19 +24,19 @@ Proof.
time "no_errors_xor_swap" prove_refinement.
Qed.

(* FIXME: move lemma to SAWCorePrelude...? *)

Lemma bvXor_twice_r n x y :
SAWCorePrelude.bvXor n (SAWCorePrelude.bvXor n x y) y = x.
Proof.
admit.
Admitted.
rewrite <- bvXor_assoc. rewrite bvXor_same. rewrite bvXor_zero. reflexivity.
Qed.

(* FIXME: move lemma to SAWCorePrelude...? *)
Lemma bvXor_twice_l n x y :
SAWCorePrelude.bvXor n (SAWCorePrelude.bvXor n y x) y = x.
Proof.
admit.
Admitted.
rewrite bvXor_comm. rewrite bvXor_assoc.
rewrite bvXor_same. rewrite bvXor_comm. rewrite bvXor_zero. reflexivity.
Qed.

Lemma xor_swap_correct : refinesFun xor_swap xor_swap_spec.
Proof.
Expand Down
47 changes: 46 additions & 1 deletion saw-core-coq/coq/handwritten/CryptolToCoq/SAWCoreBitvectors.v
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Import SAWCorePrelude.

(* A duplicate from `Program.Equality`, because importing that
module directly gives us a conflict with the `~=` notation... *)
Tactic Notation "dependent" "destruction" ident(H) :=
Tactic Notation "dependent" "destruction" ident(H) :=
Equality.do_depelim' ltac:(fun hyp => idtac) ltac:(fun hyp => Equality.do_case hyp) H.

Create HintDb SAWCoreBitvectors_eqs.
Expand Down Expand Up @@ -229,6 +229,51 @@ Admitted.
Lemma bvule_msb_r w a b : isBvule (Succ w) a b -> msb w b = false -> msb w a = false.
Admitted.

(** Lemmas about bitvector xor **)

Lemma bvXor_same n x :
SAWCorePrelude.bvXor n x x = SAWCorePrelude.replicate n Bool false.
Proof.
unfold SAWCorePrelude.bvXor, SAWCorePrelude.bvZipWith, SAWCorePrelude.zipWith, SAWCorePrelude.replicate.
induction x; auto; simpl; f_equal; auto.
rewrite SAWCorePrelude.xor_same; auto.
Qed.

Lemma bvXor_zero n x :
SAWCorePrelude.bvXor n x (SAWCorePrelude.replicate n Bool false) = x.
Proof.
unfold SAWCorePrelude.bvXor, SAWCorePrelude.bvZipWith, SAWCorePrelude.zipWith, SAWCorePrelude.replicate.
induction x; auto; simpl. f_equal; auto; cbn.
rewrite SAWCorePrelude.xor_False2; auto.
Qed.

Lemma bvXor_assoc n x y z :
SAWCorePrelude.bvXor n x (SAWCorePrelude.bvXor n y z) =
SAWCorePrelude.bvXor n (SAWCorePrelude.bvXor n x y) z.
Proof.
unfold SAWCorePrelude.bvXor, SAWCorePrelude.bvZipWith, SAWCorePrelude.zipWith.
induction n; auto; simpl. f_equal; auto; cbn.
unfold xor. rewrite Bool.xorb_assoc_reverse. reflexivity.
remember (S n).
destruct x; try solve [inversion Heqn0; clear Heqn0; subst]. injection Heqn0.
destruct y; try solve [inversion Heqn0; clear Heqn0; subst]. injection Heqn0.
destruct z; try solve [inversion Heqn0; clear Heqn0; subst]. injection Heqn0.
intros. subst. clear Heqn0. cbn. apply IHn.
Qed.

Lemma bvXor_comm n x y :
SAWCorePrelude.bvXor n x y = SAWCorePrelude.bvXor n y x.
Proof.
unfold SAWCorePrelude.bvXor, SAWCorePrelude.bvZipWith, SAWCorePrelude.zipWith.
induction n; auto; simpl. f_equal; auto; cbn.
unfold xor. apply Bool.xorb_comm.
remember (S n).
destruct x; try solve [inversion Heqn0; clear Heqn0; subst]. injection Heqn0.
destruct y; try solve [inversion Heqn0; clear Heqn0; subst]. injection Heqn0.
intros. subst. clear Heqn0. cbn. apply IHn.
Qed.



(** Some general lemmas about boolean equality **)

Expand Down