Library SetcodeConcrete.codebytes
Bytecode without code adoption
From Stdlib Require Import String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Vm QedkConcrete.systemcode.
Require Import MonadExecutionSpecs.Refinement.VmPcConversion.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Definition notAdoption (op : Opcode.t) : bool :=
match op with Opcode.Setcodefrom ⇒ false | _ ⇒ true end.
Fixpoint adoptionFreeBytes (code : string) : bool :=
match code with
| EmptyString ⇒ true
| String byte rest ⇒ notAdoption (Opcode.of_byte byte) && adoptionFreeBytes rest
end.
Lemma adoption_free_get code index byte :
adoptionFreeBytes code = true → Strings.String.get index code = Some byte →
notAdoption (Opcode.of_byte byte) = true.
Proof.
revert index. induction code as [|head tail IH]; intros [|index] Hcode Hget;
cbn [Strings.String.get] in Hget; try discriminate.
- injection Hget as <-. apply Bool.andb_true_iff in Hcode. tauto.
- apply IH with (index := index); [apply Bool.andb_true_iff in Hcode; tauto|exact Hget].
Qed.
Lemma adoption_free_selected_opcode {UI : Basics.Unreachable int} {UC : Basics.Unreachable ascii}
code pc :
adoptionFreeBytes code = true → 0 ≤ pc →
notAdoption (@selected_opcode_model UI UC code pc) = true.
Proof.
intros Hcode Hpc. unfold selected_opcode_model, pc_to_int_opt.
destruct (OCamlZ.fits_int pc) eqn:Hfits; [|reflexivity].
assert (Hconvert : @OCamlZ.to_int UI pc = pc).
{ unfold OCamlZ.to_int, OCamlZ.checked_signed. unfold OCamlZ.fits_int in Hfits.
rewrite Hfits. reflexivity. }
rewrite Hconvert.
destruct (Z.ltb pc (MonadVM.String.length code)) eqn:Hinside; [|reflexivity].
apply Z.ltb_lt in Hinside. unfold MonadVM.String.get, OCamlString.get.
assert (Hnegative : Z.ltb pc 0 = false) by (apply Z.ltb_ge; exact Hpc).
rewrite Hnegative.
destruct (Strings.String.get (Z.to_nat pc) code) as [byte|] eqn:Hget.
- eapply adoption_free_get; eassumption.
- pose proof (bytecodec.get_none_bound code (Z.to_nat pc) Hget) as Hbound.
change (pc < Z.of_nat (Strings.String.length code)) in Hinside. lia.
Qed.
Example beacon_roots_no_adoption : adoptionFreeBytes beaconRootsCode = true.
Proof. vm_compute. reflexivity. Qed.
Example history_storage_no_adoption : adoptionFreeBytes historyStorageCode = true.
Proof. vm_compute. reflexivity. Qed.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Vm QedkConcrete.systemcode.
Require Import MonadExecutionSpecs.Refinement.VmPcConversion.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Definition notAdoption (op : Opcode.t) : bool :=
match op with Opcode.Setcodefrom ⇒ false | _ ⇒ true end.
Fixpoint adoptionFreeBytes (code : string) : bool :=
match code with
| EmptyString ⇒ true
| String byte rest ⇒ notAdoption (Opcode.of_byte byte) && adoptionFreeBytes rest
end.
Lemma adoption_free_get code index byte :
adoptionFreeBytes code = true → Strings.String.get index code = Some byte →
notAdoption (Opcode.of_byte byte) = true.
Proof.
revert index. induction code as [|head tail IH]; intros [|index] Hcode Hget;
cbn [Strings.String.get] in Hget; try discriminate.
- injection Hget as <-. apply Bool.andb_true_iff in Hcode. tauto.
- apply IH with (index := index); [apply Bool.andb_true_iff in Hcode; tauto|exact Hget].
Qed.
Lemma adoption_free_selected_opcode {UI : Basics.Unreachable int} {UC : Basics.Unreachable ascii}
code pc :
adoptionFreeBytes code = true → 0 ≤ pc →
notAdoption (@selected_opcode_model UI UC code pc) = true.
Proof.
intros Hcode Hpc. unfold selected_opcode_model, pc_to_int_opt.
destruct (OCamlZ.fits_int pc) eqn:Hfits; [|reflexivity].
assert (Hconvert : @OCamlZ.to_int UI pc = pc).
{ unfold OCamlZ.to_int, OCamlZ.checked_signed. unfold OCamlZ.fits_int in Hfits.
rewrite Hfits. reflexivity. }
rewrite Hconvert.
destruct (Z.ltb pc (MonadVM.String.length code)) eqn:Hinside; [|reflexivity].
apply Z.ltb_lt in Hinside. unfold MonadVM.String.get, OCamlString.get.
assert (Hnegative : Z.ltb pc 0 = false) by (apply Z.ltb_ge; exact Hpc).
rewrite Hnegative.
destruct (Strings.String.get (Z.to_nat pc) code) as [byte|] eqn:Hget.
- eapply adoption_free_get; eassumption.
- pose proof (bytecodec.get_none_bound code (Z.to_nat pc) Hget) as Hbound.
change (pc < Z.of_nat (Strings.String.length code)) in Hinside. lia.
Qed.
Example beacon_roots_no_adoption : adoptionFreeBytes beaconRootsCode = true.
Proof. vm_compute. reflexivity. Qed.
Example history_storage_no_adoption : adoptionFreeBytes historyStorageCode = true.
Proof. vm_compute. reflexivity. Qed.