Skip to content

Commit 6c923f8

Browse files
committed
More wip
1 parent cbe71bc commit 6c923f8

File tree

5 files changed

+188
-53
lines changed

5 files changed

+188
-53
lines changed

CoqOfRust/links/M.v

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ Module Integer.
230230
Global Instance IsPrimitiveEq {kind : IntegerKind.t} : PrimitiveEq.Trait (t kind) := {
231231
PrimitiveEq.eqb x y := x.(value) =? y.(value);
232232
}.
233+
234+
Definition wrap_of_Z {kind : IntegerKind.t} (z : Z) : t kind :=
235+
{| value := Integer.normalize_wrap kind z |}.
233236
End Integer.
234237

235238
(** ** Integer kinds for better readability *)
@@ -1038,14 +1041,21 @@ Module LowM.
10381041
Inductive t (R Output : Set) : Set :=
10391042
| Pure (value : Output.t R Output)
10401043
| CallPrimitive {A : Set} (primitive : Primitive.t A) (k : A -> t R Output)
1041-
| Call {A : Set} `{Link A} {e : M} (run : {{ e 🔽 A, A }}) (k : SuccessOrPanic.t A -> t R Output)
1044+
(* | Call {A : Set} `{Link A}
1045+
{e : M}
1046+
(run : {{ e 🔽 A, A }})
1047+
(k : SuccessOrPanic.t A -> t R Output) *)
1048+
| Call {A : Set}
1049+
(e : t A A)
1050+
(k : SuccessOrPanic.t A -> t R Output)
10421051
| LetAlloc {A : Set} `{Link A}
10431052
(e : t R A)
10441053
(k : Output.t R (Ref.t Pointer.Kind.Raw A) -> t R Output)
10451054
| Loop {A : Set} (body : t R A) (k : Output.t R A -> t R Output).
10461055
Arguments Pure {_ _}.
10471056
Arguments CallPrimitive {_ _ _}.
1048-
Arguments Call {_ _ _ _ _}.
1057+
(* Arguments Call {_ _ _ _ _}. *)
1058+
Arguments Call {_ _ _}.
10491059
Arguments LetAlloc {_ _ _ _}.
10501060
Arguments Loop {_ _ _}.
10511061
End LowM.
@@ -1144,7 +1154,8 @@ Proof.
11441154
}
11451155
{ (* CallClosure *)
11461156
eapply (LowM.Call (A := Output')). {
1147-
exact run.
1157+
(* exact run. *)
1158+
exact (evaluate _ _ _ _ _ run).
11481159
}
11491160
intros output'; eapply evaluate.
11501161
match goal with

CoqOfRust/revm/revm_interpreter/instructions/simulations/arithmetic.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Proof.
2323
destruct run_InterpreterTypes_for_WIRE.
2424
destruct run_StackTrait_for_Stack, run_LoopControl_for_Control.
2525
simulate_one_step.
26+
simulate_one_step.
2627
simulate.
2728
{ admit. }
2829
{

CoqOfRust/revm/revm_interpreter/interpreter/simulations/stack.v

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,26 @@ Require Import core.links.array.
66
Require Import revm.links.dependencies.
77
Require Import revm_interpreter.interpreter.links.stack.
88

9-
(*
109
Module Stack.
1110
(*
12-
/// Returns the length of the stack in words.
13-
#[inline]
14-
pub fn len(&self) -> usize {
15-
self.data.len()
16-
}
11+
pub struct Stack {
12+
data: Vec<U256>,
13+
}
1714
*)
18-
Definition len '(Stack.data stack) : Z :=
19-
Z.of_nat (List.length stack).
15+
Record t : Set := {
16+
data: list U256.t;
17+
}.
18+
End Stack.
2019

21-
(*
22-
/// Removes the topmost element from the stack and returns it, or `StackUnderflow` if it is
23-
/// empty.
24-
#[inline]
25-
pub fn pop(&mut self) -> Result<U256, InstructionResult> {
26-
self.data.pop().ok_or(InstructionResult::StackUnderflow)
27-
}
28-
*)
20+
Module Impl_Stack.
21+
Definition Self : Set :=
22+
Stack.t.
23+
24+
(* pub fn len(&self) -> usize *)
25+
Definition len (self : Self) : Usize.t :=
26+
Integer.wrap_of_Z (Z.of_nat (List.length self.(Stack.data))).
27+
28+
(* pub fn pop(&mut self) -> Result<U256, InstructionResult> *)
2929
Definition pop :
3030
MS? Stack.t string (U256.t + InstructionResult.t) :=
3131
letS? '(Stack.data stack) := readS? in
@@ -120,4 +120,3 @@ Module Stack.
120120
letS? pop2 := pop_unsafe in
121121
returnS? (pop1, pop2, top_unsafe).
122122
End Stack.
123-
*)

CoqOfRust/revm/revm_interpreter/simulations/gas.v

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,18 @@ Module Impl_MemoryGas.
1414
Definition Self : Set :=
1515
MemoryGas.t.
1616

17-
Instance run_new Stack :
17+
(* pub const fn new() -> Self *)
18+
Instance run_new Stack (stack : Stack.to_Set Stack) :
19+
Run.C Impl_MemoryGas.run_new (fun _ => unit) stack.
20+
Proof.
21+
constructor.
22+
apply Run.Pure.
23+
exact tt.
24+
Defined.
25+
26+
(* Compute run_new. *)
27+
28+
(* Instance run_new Stack :
1829
Run.Trait Stack Impl_MemoryGas.run_new.
1930
Proof.
2031
constructor.
@@ -26,7 +37,39 @@ Module Impl_MemoryGas.
2637
(Output.Success Impl_Default_for_MemoryGas.default, tt).
2738
Proof.
2839
reflexivity.
29-
Qed.
40+
Qed. *)
41+
42+
(* pub fn record_new_len(&mut self, new_num: usize) -> Option<u64> *)
43+
Instance run_record_new_len Stack (stack : Stack.to_Set Stack)
44+
(self : Ref.t Pointer.Kind.MutRef Self)
45+
(new_num : Usize.t)
46+
(H_self : Stack.CanAccess.t Stack self.(Ref.core)) :
47+
Run.C (Impl_MemoryGas.run_record_new_len self new_num) (fun _ => unit) stack.
48+
Proof.
49+
constructor.
50+
Ltac simulate_reduce :=
51+
repeat match goal with
52+
| |- {{ StackM.let_ _ _ 🌲 _ }} =>
53+
unfold StackM.let_
54+
| |- {{ ?e 🌲 _ }} =>
55+
let e' := eval hnf in e in
56+
change e with e'
57+
end.
58+
59+
Ltac simulate_get_can_access :=
60+
match goal with
61+
| |- {{ StackM.GetCanAccess _ _ _ 🌲 _ }} =>
62+
unshelve eapply Run.GetCanAccess; [Stack.CanAccess.infer |]
63+
end.
64+
65+
Time simulate_reduce; unshelve eapply Run.GetCanAccess. {
66+
Time Stack.CanAccess.infer.
67+
}
68+
Time try (simulate_reduce; simulate_get_can_access).
69+
simulate_reduce.
70+
cbn.
71+
Show.
72+
Defined.
3073
End Impl_MemoryGas.
3174

3275
Module Impl_Gas.
@@ -35,6 +78,41 @@ Module Impl_Gas.
3578
Definition Self : Set :=
3679
Gas.t.
3780

81+
Definition run_limit Stack (stack : Stack.to_Set Stack)
82+
(self : Ref.t Pointer.Kind.Ref Self)
83+
(H_self : Stack.CanAccess.t Stack self.(Ref.core)) :
84+
RunEval.t (Impl_Gas.run_limit self).(Run.run_f) stack.
85+
Proof.
86+
unfold RunEval.t; cbn.
87+
unshelve econstructor. {
88+
repeat Stack.CanAccess.infer.
89+
}
90+
match goal with
91+
| |- Run.t (match ?e with _ => _ end) =>
92+
destruct e
93+
end; constructor.
94+
Defined.
95+
96+
Parameter limit : U64.t.
97+
98+
Definition self : Ref.t Pointer.Kind.Ref Self := Ref.immediate Pointer.Kind.Ref {|
99+
Gas.limit := limit;
100+
Gas.remaining := limit;
101+
Gas.refunded := {| Integer.value := 0 |};
102+
Gas.memory := Impl_Default_for_MemoryGas.default;
103+
|}.
104+
105+
Definition H_self : Stack.CanAccess.t [] self.(Ref.core).
106+
Proof.
107+
repeat Stack.CanAccess.infer.
108+
Defined.
109+
110+
Compute Run.eval (run_limit [] tt self H_self).
111+
112+
Compute fun self => Run.eval (
113+
run_limit [] tt {| Ref.core := Ref.Core.Immediate (Some self) |} ltac:(now repeat Stack.CanAccess.infer)
114+
).
115+
38116
(*
39117
pub const fn new(limit: u64) -> Self {
40118
Self {

0 commit comments

Comments
 (0)