Library QedkConcrete.systemcode
Prescribed system-contract code
From Stdlib Require Import List String Ascii ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Vm QedkConcrete.vmhostframe QedkConcrete.wordrange
QedkConcrete.bytecodec.
Require Import MonadExecutionSpecs.Refinement.VmPcConversion.
Set Default Goal Selector "!".
Import ListNotations.
Local Open Scope Z_scope.
Fixpoint callFreeBytes (code : string) : bool :=
match code with
| EmptyString ⇒ true
| String byte rest ⇒ negb (recursiveCallOpcode (Opcode.of_byte byte)) && callFreeBytes rest
end.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Vm QedkConcrete.vmhostframe QedkConcrete.wordrange
QedkConcrete.bytecodec.
Require Import MonadExecutionSpecs.Refinement.VmPcConversion.
Set Default Goal Selector "!".
Import ListNotations.
Local Open Scope Z_scope.
Fixpoint callFreeBytes (code : string) : bool :=
match code with
| EmptyString ⇒ true
| String byte rest ⇒ negb (recursiveCallOpcode (Opcode.of_byte byte)) && callFreeBytes rest
end.
https://eips.ethereum.org/EIPS/eip-4788bytecode
Definition beaconRootsCode : string :=
Lists.List.fold_right (fun n rest ⇒ String (Ascii.ascii_of_nat n) rest) EmptyString
[51; 115; 255; 255; 255; 255; 255; 255; 255; 255; 255; 255; 255; 255; 255; 255;
255; 255; 255; 255; 255; 254; 20; 96; 77; 87; 96; 32; 54; 20; 96; 36;
87; 95; 95; 253; 91; 95; 53; 128; 21; 96; 73; 87; 98; 0; 31; 255;
129; 6; 144; 129; 84; 20; 96; 60; 87; 95; 95; 253; 91; 98; 0; 31;
255; 1; 84; 95; 82; 96; 32; 95; 243; 91; 95; 95; 253; 91; 98; 0;
31; 255; 66; 6; 66; 129; 85; 95; 53; 144; 98; 0; 31; 255; 1; 85;
0]%nat.
Example beaconRootsCode_call_free : callFreeBytes beaconRootsCode = true.
Proof. vm_compute. reflexivity. Qed.
Lists.List.fold_right (fun n rest ⇒ String (Ascii.ascii_of_nat n) rest) EmptyString
[51; 115; 255; 255; 255; 255; 255; 255; 255; 255; 255; 255; 255; 255; 255; 255;
255; 255; 255; 255; 255; 254; 20; 96; 77; 87; 96; 32; 54; 20; 96; 36;
87; 95; 95; 253; 91; 95; 53; 128; 21; 96; 73; 87; 98; 0; 31; 255;
129; 6; 144; 129; 84; 20; 96; 60; 87; 95; 95; 253; 91; 98; 0; 31;
255; 1; 84; 95; 82; 96; 32; 95; 243; 91; 95; 95; 253; 91; 98; 0;
31; 255; 66; 6; 66; 129; 85; 95; 53; 144; 98; 0; 31; 255; 1; 85;
0]%nat.
Example beaconRootsCode_call_free : callFreeBytes beaconRootsCode = true.
Proof. vm_compute. reflexivity. Qed.
https://eips.ethereum.org/EIPS/eip-2935bytecode
Definition historyStorageCode : string :=
Lists.List.fold_right (fun n rest ⇒ String (Ascii.ascii_of_nat n) rest) EmptyString
[51; 115; 255; 255; 255; 255; 255; 255; 255; 255; 255; 255; 255; 255; 255; 255;
255; 255; 255; 255; 255; 254; 20; 96; 70; 87; 96; 32; 54; 3; 96; 66;
87; 95; 53; 96; 1; 67; 3; 129; 17; 96; 66; 87; 97; 31; 255; 129;
67; 3; 17; 96; 66; 87; 97; 31; 255; 144; 6; 84; 95; 82; 96; 32;
95; 243; 91; 95; 95; 253; 91; 95; 53; 97; 31; 255; 96; 1; 67; 3;
6; 85; 0]%nat.
Example historyStorageCode_call_free : callFreeBytes historyStorageCode = true.
Proof. vm_compute. reflexivity. Qed.
Lemma call_free_get code index byte :
callFreeBytes code = true → Strings.String.get index code = Some byte →
recursiveCallOpcode (Opcode.of_byte byte) = false.
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 as [Hhead _].
apply Bool.negb_true_iff in Hhead. exact Hhead.
- apply IH with (index := index); [apply Bool.andb_true_iff in Hcode; tauto|exact Hget].
Qed.
Lemma call_free_selected_opcode {UI : Basics.Unreachable int} {UC : Basics.Unreachable ascii}
code pc :
callFreeBytes code = true → 0 ≤ pc →
recursiveCallOpcode (@selected_opcode_model UI UC code pc) = false.
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 call_free_get; eassumption.
- pose proof (get_none_bound code (Z.to_nat pc) Hget) as Hbound.
change (pc < Z.of_nat (Strings.String.length code)) in Hinside. lia.
Qed.
Print Assumptions call_free_selected_opcode.
Lists.List.fold_right (fun n rest ⇒ String (Ascii.ascii_of_nat n) rest) EmptyString
[51; 115; 255; 255; 255; 255; 255; 255; 255; 255; 255; 255; 255; 255; 255; 255;
255; 255; 255; 255; 255; 254; 20; 96; 70; 87; 96; 32; 54; 3; 96; 66;
87; 95; 53; 96; 1; 67; 3; 129; 17; 96; 66; 87; 97; 31; 255; 129;
67; 3; 17; 96; 66; 87; 97; 31; 255; 144; 6; 84; 95; 82; 96; 32;
95; 243; 91; 95; 95; 253; 91; 95; 53; 97; 31; 255; 96; 1; 67; 3;
6; 85; 0]%nat.
Example historyStorageCode_call_free : callFreeBytes historyStorageCode = true.
Proof. vm_compute. reflexivity. Qed.
Lemma call_free_get code index byte :
callFreeBytes code = true → Strings.String.get index code = Some byte →
recursiveCallOpcode (Opcode.of_byte byte) = false.
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 as [Hhead _].
apply Bool.negb_true_iff in Hhead. exact Hhead.
- apply IH with (index := index); [apply Bool.andb_true_iff in Hcode; tauto|exact Hget].
Qed.
Lemma call_free_selected_opcode {UI : Basics.Unreachable int} {UC : Basics.Unreachable ascii}
code pc :
callFreeBytes code = true → 0 ≤ pc →
recursiveCallOpcode (@selected_opcode_model UI UC code pc) = false.
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 call_free_get; eassumption.
- pose proof (get_none_bound code (Z.to_nat pc) Hget) as Hbound.
change (pc < Z.of_nat (Strings.String.length code)) in Hinside. lia.
Qed.
Print Assumptions call_free_selected_opcode.