diff --git a/Conform/Model.lean b/Conform/Model.lean index a80c4e3..b3b2722 100644 --- a/Conform/Model.lean +++ b/Conform/Model.lean @@ -33,14 +33,14 @@ instance : IsTrans ((_ : UInt256) × UInt256) (· ≤ ·) where unfold LE.le instLESigmaUInt256_conform at h₁ h₂ ⊢; simp at * aesop (config := {warnOnNonterminal := false}) <;> omega -instance : IsAntisymm ((_ : UInt256) × UInt256) (· ≤ ·) where +instance : Std.Antisymm (α := ((_ : UInt256) × UInt256)) (· ≤ ·) where antisymm a b h₁ h₂ := by rcases a with ⟨⟨a, ha⟩, ⟨a', ha'⟩⟩ rcases b with ⟨⟨b, hb⟩, ⟨b', hb'⟩⟩ unfold LE.le instLESigmaUInt256_conform at h₁ h₂; simp at * aesop (config := {warnOnNonterminal := false}) <;> omega -instance : IsTotal ((_ : UInt256) × UInt256) (· ≤ ·) where +instance : Std.Total (α := ((_ : UInt256) × UInt256)) (· ≤ ·) where total a b := by rcases a with ⟨⟨a, ha⟩, ⟨a', ha'⟩⟩ rcases b with ⟨⟨b, hb⟩, ⟨b', hb'⟩⟩ diff --git a/Conform/Wheels.lean b/Conform/Wheels.lean index 7eec645..6dc3e96 100644 --- a/Conform/Wheels.lean +++ b/Conform/Wheels.lean @@ -32,11 +32,11 @@ def getObjValAsD! (j : Json) (α : Type) [FromJson α] [Inhabited α] (k : Strin def getObjVals? (self : Json) (α β : Type) [Ord α] [FromJson α] [FromJson β] : Except String (Batteries.RBMap α β compare) := do - let keys ← Array.map Sigma.fst <$> RBNode.toArray <$> self.getObj? + let keys := (← self.getObj?).toArray.map Prod.fst let mut result : Batteries.RBMap α β compare := ∅ for k in keys do - if let .ok key := FromJson.fromJson? k then - result := result.insert key (← self.getObjValAs? β k) + if let .ok key := FromJson.fromJson? (.str k) then + result := result.insert key (← self.getObjValAs? β k) pure result def fromFile (path : System.FilePath) : IO Json := do @@ -103,24 +103,10 @@ end DebuggingAndProfiling end EvmYul def computeToList! {α} - [LE α] [IsTrans α (· ≤ ·)] [IsAntisymm α (· ≤ ·)] [IsTotal α (· ≤ ·)] + [LE α] [IsTrans α (· ≤ ·)] [Std.Antisymm (α := α) (· ≤ ·)] [Std.Total (α := α) (· ≤ ·)] [DecidableRel (α := α) (· ≤ ·)] (m : Multiset α) : List α := m.sort (· ≤ ·) def Batteries.RBMap.partition {α β : Type} {cmp : α → α → Ordering} (t : Batteries.RBMap α β cmp) (p : α → β → Bool) : Batteries.RBMap α β cmp × Batteries.RBMap α β cmp := (t.filter p, t.filter (λ k v ↦ not (p k v))) - -namespace Std - -namespace HashSet - -def diff {α : Type} [DecidableEq α] [Hashable α] (a b : HashSet α) : HashSet α := Id.run do - let mut res := a - for elem in b do - res := res.erase elem - return res - -end HashSet - -end Std diff --git a/EvmYul.lean b/EvmYul.lean index fbfb9d4..477ef2e 100644 --- a/EvmYul.lean +++ b/EvmYul.lean @@ -2,6 +2,7 @@ import EvmYul.MachineState import EvmYul.MachineStateOps import EvmYul.Operations import EvmYul.Pretty +import EvmYul.Protocol import EvmYul.Semantics import EvmYul.SharedState import EvmYul.SharedStateOps @@ -28,6 +29,7 @@ import EvmYul.EVM.Semantics import EvmYul.EVM.State import EvmYul.EVM.StateOps import EvmYul.EVM.PrecompiledContracts +import EvmYul.EVM.PostFusakaTests import EvmYul.EVM.Gas import EvmYul.EVM.GasConstants diff --git a/EvmYul/EVM/Gas.lean b/EvmYul/EVM/Gas.lean index e5ebec8..90baa2d 100644 --- a/EvmYul/EVM/Gas.lean +++ b/EvmYul/EVM/Gas.lean @@ -4,11 +4,14 @@ import EvmYul.EVM.State import EvmYul.StateOps import EvmYul.MachineStateOps import EvmYul.EVM.GasConstants +import EvmYul.Protocol namespace EvmYul namespace EVM +universe u + /- Appendix G. Fee Schedule -/ @@ -52,7 +55,7 @@ def Wverylow : List (Operation .EVM) := [ ] def Wlow : List (Operation .EVM) := [ - .MUL, .DIV, .SDIV, .MOD, .SMOD, .SIGNEXTEND, .SELFBALANCE + .MUL, .DIV, .SDIV, .MOD, .SMOD, .SIGNEXTEND, .CLZ, .SELFBALANCE ] def Wmid : List (Operation .EVM) := [ @@ -95,7 +98,7 @@ with the definition of `C_<>` functions that are described inline along with the It would be worth restructing everything to obtain cleaner separation of concerns. -/ -def Csstore (s : EVM.State) : ℕ := +def CsstoreWithProtocol (protocol : Protocol) (s : EVM.State) : ℕ := let { stack := μₛ, accountMap := σ, σ₀ := σ₀, executionEnv.codeOwner := Iₐ, .. } := s let { storage := σ_Iₐ, .. } := σ.find! Iₐ let storeAddr := μₛ[0]! @@ -111,10 +114,13 @@ def Csstore (s : EVM.State) : ℕ := else Gcoldsload let storeComponent := if v = v' || v₀ ≠ v then Gwarmaccess else - if v ≠ v' && v₀ = v && v₀ = ⟨0⟩ then Gsset else + if v ≠ v' && v₀ = v && v₀ = ⟨0⟩ then protocol.sstoreSetGasFor s else /- v ≠ v' ∧ v₀ = v ∧ v₀ ≠ 0 -/ Gsreset loadComponent + storeComponent +def Csstore (s : EVM.State) : ℕ := + CsstoreWithProtocol Protocol.osaka s + def Ctstore : ℕ := let loadComponent := 0 let storeComponent := Gwarmaccess @@ -161,38 +167,134 @@ def Ctload : ℕ := -/ def L (n : ℕ) : ℕ := n - (n / 64) -def Cnew (t : AccountAddress) (val : UInt256) (σ : AccountMap .EVM) : ℕ := - if EvmYul.State.dead σ t && val != ⟨0⟩ then Gnewaccount else 0 +def CnewWithProtocolFor {ρ : Type u} {τ : OperationType} (protocol : Protocol) + (policyState : ρ) + (t : AccountAddress) (val : UInt256) (σ : AccountMap τ) : ℕ := + if EvmYul.State.dead σ t && val != ⟨0⟩ then + protocol.callNewAccountGasFor policyState + else 0 + +def CnewWithProtocol {τ : OperationType} (protocol : Protocol) + (t : AccountAddress) (val : UInt256) (σ : AccountMap τ) : ℕ := + CnewWithProtocolFor protocol () t val σ + +def Cnew {τ : OperationType} (t : AccountAddress) (val : UInt256) + (σ : AccountMap τ) : ℕ := + CnewWithProtocol Protocol.osaka t val σ + +def CxferWithProtocolFor {ρ : Type u} (protocol : Protocol) + (policyState : ρ) (val : UInt256) : ℕ := + if val != ⟨0⟩ then protocol.callValueTransferGasFor policyState else 0 + +def CxferWithProtocol (protocol : Protocol) (val : UInt256) : ℕ := + CxferWithProtocolFor protocol () val def Cxfer (val : UInt256) : ℕ := - if val != ⟨0⟩ then Gcallvalue else 0 + CxferWithProtocol Protocol.osaka val -def Cextra (t r : AccountAddress) (val : UInt256) (σ : AccountMap .EVM) (A : Substate) : ℕ := - Caccess t A + Cxfer val + Cnew r val σ +def addDelegatedCodeAccess + (t : AccountAddress) (σ : AccountMap .EVM) (A : Substate) : Substate := + match σ.delegatedCodeTarget? t with + | none => A + | some target => A.addAccessedAccount target -def Cgascap (t r : AccountAddress) (val g : UInt256) (σ : AccountMap .EVM) (μ : MachineState) (A : Substate) := - if μ.gasAvailable.toNat >= Cextra t r val σ A then - min (L <| (μ.gasAvailable.toNat - Cextra t r val σ A)) g.toNat +def CdelegatedCodeAccess + (t : AccountAddress) (σ : AccountMap .EVM) (A : Substate) : ℕ := + match σ.delegatedCodeTarget? t with + | none => 0 + | some target => Caccess target (A.addAccessedAccount t) + +def CextraWithProtocolFor {ρ : Type u} {τ : OperationType} + (protocol : Protocol) (policyState : ρ) + (t r : AccountAddress) (val : UInt256) + (σ : AccountMap τ) (A : Substate) : ℕ := + let delegatedAccess := + match τ with + | .EVM => CdelegatedCodeAccess t σ A + | .Yul => 0 + Caccess t A + delegatedAccess + CxferWithProtocolFor protocol policyState val + + CnewWithProtocolFor protocol policyState r val σ + +def CextraWithProtocol {τ : OperationType} (protocol : Protocol) + (t r : AccountAddress) (val : UInt256) + (σ : AccountMap τ) (A : Substate) : ℕ := + CextraWithProtocolFor protocol () t r val σ A + +def Cextra {τ : OperationType} (t r : AccountAddress) (val : UInt256) + (σ : AccountMap τ) (A : Substate) : ℕ := + CextraWithProtocol Protocol.osaka t r val σ A + +def CgascapWithProtocolFor {ρ : Type u} {τ : OperationType} + (protocol : Protocol) (policyState : ρ) + (t r : AccountAddress) (val g : UInt256) + (σ : AccountMap τ) (μ : MachineState) (A : Substate) := + if μ.gasAvailable.toNat >= + CextraWithProtocolFor protocol policyState t r val σ A then + min (L <| (μ.gasAvailable.toNat - + CextraWithProtocolFor protocol policyState t r val σ A)) g.toNat else g.toNat -def Ccallgas (t r : AccountAddress) (val g : UInt256) (σ : AccountMap .EVM) (μ : MachineState) (A : Substate) : ℕ := +def CgascapWithProtocol {τ : OperationType} (protocol : Protocol) + (t r : AccountAddress) (val g : UInt256) + (σ : AccountMap τ) (μ : MachineState) (A : Substate) : ℕ := + CgascapWithProtocolFor protocol () t r val g σ μ A + +def Cgascap {τ : OperationType} (t r : AccountAddress) (val g : UInt256) + (σ : AccountMap τ) (μ : MachineState) (A : Substate) : ℕ := + CgascapWithProtocol Protocol.osaka t r val g σ μ A + +def CcallgasWithProtocolFor {ρ : Type u} {τ : OperationType} + (protocol : Protocol) (policyState : ρ) + (t r : AccountAddress) (val g : UInt256) + (σ : AccountMap τ) (μ : MachineState) (A : Substate) : ℕ := match val with - | ⟨0⟩ => Cgascap t r val g σ μ A - | _ => Cgascap t r val g σ μ A + GasConstants.Gcallstipend + | ⟨0⟩ => CgascapWithProtocolFor protocol policyState t r val g σ μ A + | _ => CgascapWithProtocolFor protocol policyState t r val g σ μ A + GasConstants.Gcallstipend + +def CcallgasWithProtocol {τ : OperationType} (protocol : Protocol) + (t r : AccountAddress) (val g : UInt256) + (σ : AccountMap τ) (μ : MachineState) (A : Substate) : ℕ := + CcallgasWithProtocolFor protocol () t r val g σ μ A + +def Ccallgas {τ : OperationType} (t r : AccountAddress) (val g : UInt256) + (σ : AccountMap τ) (μ : MachineState) (A : Substate) : ℕ := + CcallgasWithProtocol Protocol.osaka t r val g σ μ A /-- NB Assumes stack coherence. -/ -def Ccall (t r : AccountAddress) (val g : UInt256) (σ : AccountMap .EVM) (μ : MachineState) (A : Substate) : ℕ := - Cgascap t r val g σ μ A + Cextra t r val σ A +def CcallWithProtocolFor {ρ : Type u} {τ : OperationType} + (protocol : Protocol) (policyState : ρ) + (t r : AccountAddress) (val g : UInt256) + (σ : AccountMap τ) (μ : MachineState) (A : Substate) : ℕ := + CgascapWithProtocolFor protocol policyState t r val g σ μ A + + CextraWithProtocolFor protocol policyState t r val σ A + +def CcallWithProtocol {τ : OperationType} (protocol : Protocol) + (t r : AccountAddress) (val g : UInt256) + (σ : AccountMap τ) (μ : MachineState) (A : Substate) : ℕ := + CcallWithProtocolFor protocol () t r val g σ μ A + +def Ccall {τ : OperationType} (t r : AccountAddress) (val g : UInt256) + (σ : AccountMap τ) (μ : MachineState) (A : Substate) : ℕ := + CcallWithProtocol Protocol.osaka t r val g σ μ A /-- (65) -/ -def R (x : ℕ) : ℕ := Ginitcodeword * ((x + 31) / 32) +def RWithProtocolFor {ρ : Type u} (protocol : Protocol) + (policyState : ρ) (x : ℕ) : ℕ := + protocol.initCodeWordGasFor policyState * ((x + 31) / 32) -def intrinsicGas (T : Transaction) : ℕ := +def RWithProtocol (protocol : Protocol) (x : ℕ) : ℕ := + RWithProtocolFor protocol () x + +def R (x : ℕ) : ℕ := + RWithProtocol Protocol.osaka x + +def intrinsicGasWithProtocolFor {ρ : Type u} (protocol : Protocol) + (policyState : ρ) (T : Transaction) : ℕ := let g₀_data := T.base.data.foldl (λ acc b ↦ @@ -204,7 +306,12 @@ def intrinsicGas (T : Transaction) : ℕ := 0 let g₀_create : ℕ := if T.base.recipient == none then - GasConstants.Gtxcreate + R (T.base.data.size) + protocol.txCreateGasFor policyState + + RWithProtocolFor protocol policyState (T.base.data.size) + else 0 + let g₀_firstNonce : ℕ := + if T.base.nonce == ⟨0⟩ then + protocol.firstNonceAccountCreationGasFor policyState else 0 let g₀_accessList : ℕ := @@ -213,7 +320,14 @@ def intrinsicGas (T : Transaction) : ℕ := acc + GasConstants.Gaccesslistaddress + s.size * GasConstants.Gaccessliststorage ) 0 - g₀_data + g₀_create + GasConstants.Gtransaction + g₀_accessList + g₀_data + g₀_create + g₀_firstNonce + GasConstants.Gtransaction + + g₀_accessList + +def intrinsicGasWithProtocol (protocol : Protocol) (T : Transaction) : ℕ := + intrinsicGasWithProtocolFor protocol () T + +def intrinsicGas (T : Transaction) : ℕ := + intrinsicGasWithProtocol Protocol.osaka T /-- H.1. Gas Cost - the third summand. @@ -221,10 +335,10 @@ H.1. Gas Cost - the third summand. NB Stack accesses are assumed guarded here and we access with `!`. This is for keeping in sync with the way the YP is structures, at least for the time being. -/ -def C' (s : State) (instr : Operation .EVM) : ℕ := +def C'WithProtocol (protocol : Protocol) (s : State) (instr : Operation .EVM) : ℕ := let { accountMap := σ, stack := μₛ, substate := A, toMachineState := μ, executionEnv := I, ..} := s match instr with - | .SSTORE => Csstore s + | .SSTORE => CsstoreWithProtocol protocol s | .TSTORE => Ctstore | .EXP => let μ₁ := μₛ[1]!; if μ₁ == ⟨0⟩ then Gexp else Gexp + Gexpbyte * (1 + Nat.log 256 μ₁.toNat) -- TODO(check) I think this floors by itself. cf. H.1. YP. | .EXTCODECOPY => Caccess (AccountAddress.ofUInt256 μₛ[0]!) A + Gcopy * ((μₛ[3]!.toNat + 31) / 32) @@ -234,8 +348,13 @@ def C' (s : State) (instr : Operation .EVM) : ℕ := | .LOG3 => Glog + Glogdata * μₛ[1]!.toNat + 3 * Glogtopic | .LOG4 => Glog + Glogdata * μₛ[1]!.toNat + 4 * Glogtopic | .SELFDESTRUCT => Cselfdestruct s - | .CREATE => Gcreate + R μₛ[2]!.toNat - | .CREATE2 => let μ₂ := μₛ[2]!; Gcreate + Gkeccak256word * ((μ₂.toNat + 31) / 32) + R μ₂.toNat + | .CREATE => + protocol.createBaseGasFor s + + RWithProtocolFor protocol s μₛ[2]!.toNat + | .CREATE2 => + let μ₂ := μₛ[2]! + protocol.createBaseGasFor s + Gkeccak256word * ((μ₂.toNat + 31) / 32) + + RWithProtocolFor protocol s μ₂.toNat | .KECCAK256 => Gkeccak256 + Gkeccak256word * ((μₛ[1]!.toNat + 31) / 32) | .JUMPDEST => Gjumpdest | .SLOAD => Csload μₛ A I @@ -246,10 +365,10 @@ def C' (s : State) (instr : Operation .EVM) : ℕ := not what happens to be on the stack at index 2. Therefore it is 0 for `DELEGATECALL` and `STATICCALL`. -/ - | .CALL => Ccall (AccountAddress.ofUInt256 μₛ[1]!) (AccountAddress.ofUInt256 μₛ[1]!) μₛ[2]! μₛ[0]! σ μ A - | .CALLCODE => Ccall (AccountAddress.ofUInt256 μₛ[1]!) s.executionEnv.codeOwner μₛ[2]! μₛ[0]! σ μ A - | .DELEGATECALL => Ccall (AccountAddress.ofUInt256 μₛ[1]!) s.executionEnv.codeOwner ⟨0⟩ μₛ[0]! σ μ A - | .STATICCALL => Ccall (AccountAddress.ofUInt256 μₛ[1]!) (AccountAddress.ofUInt256 μₛ[1]!) ⟨0⟩ μₛ[0]! σ μ A + | .CALL => CcallWithProtocolFor protocol s (AccountAddress.ofUInt256 μₛ[1]!) (AccountAddress.ofUInt256 μₛ[1]!) μₛ[2]! μₛ[0]! σ μ A + | .CALLCODE => CcallWithProtocolFor protocol s (AccountAddress.ofUInt256 μₛ[1]!) s.executionEnv.codeOwner μₛ[2]! μₛ[0]! σ μ A + | .DELEGATECALL => CcallWithProtocolFor protocol s (AccountAddress.ofUInt256 μₛ[1]!) s.executionEnv.codeOwner ⟨0⟩ μₛ[0]! σ μ A + | .STATICCALL => CcallWithProtocolFor protocol s (AccountAddress.ofUInt256 μₛ[1]!) (AccountAddress.ofUInt256 μₛ[1]!) ⟨0⟩ μₛ[0]! σ μ A | .BLOBHASH => HASH_OPCODE_GAS | w => if w ∈ Wcopy then Gverylow + Gcopy * ((μₛ[2]!.toNat + 31) / 32) else @@ -262,6 +381,15 @@ def C' (s : State) (instr : Operation .EVM) : ℕ := if w ∈ Whigh then Ghigh else 0 +def C' (s : State) (instr : Operation .EVM) : ℕ := + C'WithProtocol s.executionEnv.protocol s instr + +def selectedC' (s : State) (instr : Operation .EVM) : ℕ := + C' s instr + +@[simp] theorem selectedC'_eq_C' (s : State) (instr : Operation .EVM) : + selectedC' s instr = C' s instr := rfl + /-- H.1. Gas Cost diff --git a/EvmYul/EVM/Instr.lean b/EvmYul/EVM/Instr.lean index ec095e3..5762d53 100644 --- a/EvmYul/EVM/Instr.lean +++ b/EvmYul/EVM/Instr.lean @@ -35,6 +35,7 @@ def serializeCompBitInstr : CBLOp .EVM → UInt8 | .SHL => 0x1b | .SHR => 0x1c | .SAR => 0x1d + | .CLZ => 0x1e def serializeKeccakInstr : KOp .EVM → UInt8 | .KECCAK256 => 0x20 @@ -217,6 +218,7 @@ def δ : Operation .EVM → Option ℕ | .SHL => some 2 | .SHR => some 2 | .SAR => some 2 + | .CLZ => some 1 | .KECCAK256 => some 2 | .ADDRESS => some 0 | .BALANCE => some 1 @@ -336,6 +338,7 @@ def α : Operation .EVM → Option ℕ | .SHL => some 1 | .SHR => some 1 | .SAR => some 1 + | .CLZ => some 1 | .KECCAK256 => some 1 | .ADDRESS => some 1 | .BALANCE => some 1 @@ -452,6 +455,7 @@ def parseInstr : UInt8 → Option (Operation .EVM) | 0x1b => some .SHL | 0x1c => some .SHR | 0x1d => some .SAR + | 0x1e => some .CLZ | 0x20 => some .KECCAK256 diff --git a/EvmYul/EVM/PostFusakaTests.lean b/EvmYul/EVM/PostFusakaTests.lean new file mode 100644 index 0000000..3cccc2f --- /dev/null +++ b/EvmYul/EVM/PostFusakaTests.lean @@ -0,0 +1,75 @@ +import EvmYul.EVM.Gas +import EvmYul.EVM.Instr +import EvmYul.Maps.AccountMap +import EvmYul.State.Account + +namespace EvmYul + +open Operation + +private def delegationCode (targetBytes : ByteArray) : ByteArray := + ⟨#[0xef, 0x01, 0x00]⟩ ++ targetBytes + +private def authority : AccountAddress := .ofNat 0x1000 +private def target : AccountAddress := .ofNat 0x2000 +private def targetAddressBytes : ByteArray := + ⟨#[0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x20, 0x00]⟩ +private def expmodAddressBytes : ByteArray := + ⟨#[0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x05]⟩ +private def targetCode : ByteArray := ⟨#[0x60, 0x01, 0x00]⟩ + +private def delegatedAccountMap : AccountMap .EVM := + (default : AccountMap .EVM) + |>.insert authority { (default : Account .EVM) with code := delegationCode targetAddressBytes } + |>.insert target { (default : Account .EVM) with code := targetCode } + +private def delegatedToPrecompileAccountMap : AccountMap .EVM := + (default : AccountMap .EVM) + |>.insert authority { (default : Account .EVM) with code := delegationCode expmodAddressBytes } + +private def precompileWithDelegationBytesAccountMap : AccountMap .EVM := + (default : AccountMap .EVM) + |>.insert 5 { (default : Account .EVM) with code := delegationCode targetAddressBytes } + +#guard UInt256.clz ⟨0⟩ == ⟨256⟩ +#guard UInt256.clz ⟨1⟩ == ⟨255⟩ +#guard UInt256.clz ⟨0x8000000000000000000000000000000000000000000000000000000000000000⟩ == ⟨0⟩ +#guard EVM.parseInstr 0x1e == some (.CLZ : Operation .EVM) +#guard EVM.δ (.CLZ : Operation .EVM) == some 1 +#guard EVM.α (.CLZ : Operation .EVM) == some 1 +#guard EVM.C' (default : EVM.State) (.CLZ : Operation .EVM) == GasConstants.Glow + +#guard eip7702DelegationTargetOfCode? (delegationCode targetAddressBytes) == some target +#guard eip7702DelegationTargetOfCode? targetCode == none + +#guard + match toExecute .EVM delegatedAccountMap authority with + | .Code code => code == targetCode + | .Precompiled _ => false + +#guard + match toExecute .EVM delegatedToPrecompileAccountMap authority with + | .Code code => code == default + | .Precompiled _ => false + +#guard + match toExecute .EVM precompileWithDelegationBytesAccountMap 5 with + | .Precompiled .expmod => true + | _ => false + +#guard + EVM.CdelegatedCodeAccess authority delegatedAccountMap (default : Substate) == + GasConstants.Gcoldaccountaccess + +#guard + EVM.CdelegatedCodeAccess authority delegatedAccountMap + ((default : Substate).addAccessedAccount target) == + GasConstants.Gwarmaccess + +end EvmYul diff --git a/EvmYul/EVM/PrecompiledContracts.lean b/EvmYul/EVM/PrecompiledContracts.lean index 414bdf9..63fc8ec 100644 --- a/EvmYul/EVM/PrecompiledContracts.lean +++ b/EvmYul/EVM/PrecompiledContracts.lean @@ -20,13 +20,13 @@ import EvmYul.FFI.ffi open EvmYul -def Ξ_ECREC - (σ : (AccountMap .EVM)) +def Ξ_ECREC {τ : OperationType} + (σ : (AccountMap τ)) (g : UInt256) (A : Substate) - (I : ExecutionEnv .EVM) + (I : ExecutionEnv τ) : - (Bool × AccountMap .EVM × UInt256 × Substate × ByteArray) + (Bool × AccountMap τ × UInt256 × Substate × ByteArray) := let gᵣ : ℕ := 3000 @@ -53,13 +53,13 @@ def Ξ_ECREC .empty (true, σ, g - .ofNat gᵣ, A, o) -def Ξ_SHA256 - (σ : AccountMap .EVM) +def Ξ_SHA256 {τ : OperationType} + (σ : AccountMap τ) (g : UInt256) (A : Substate) - (I : ExecutionEnv .EVM) + (I : ExecutionEnv τ) : - (Bool × AccountMap .EVM × UInt256 × Substate × ByteArray) + (Bool × AccountMap τ × UInt256 × Substate × ByteArray) := let gᵣ : ℕ := let l := I.calldata.size @@ -77,13 +77,13 @@ def Ξ_SHA256 .empty (true, σ, g - .ofNat gᵣ, A, o) -def Ξ_RIP160 - (σ : AccountMap .EVM) +def Ξ_RIP160 {τ : OperationType} + (σ : AccountMap τ) (g : UInt256) (A : Substate) - (I : ExecutionEnv .EVM) + (I : ExecutionEnv τ) : - (Bool × AccountMap .EVM × UInt256 × Substate × ByteArray) + (Bool × AccountMap τ × UInt256 × Substate × ByteArray) := let gᵣ : ℕ := let l := I.calldata.size @@ -101,13 +101,13 @@ def Ξ_RIP160 .empty (true, σ, g - .ofNat gᵣ, A, o) -def Ξ_ID - (σ : AccountMap .EVM) +def Ξ_ID {τ : OperationType} + (σ : AccountMap τ) (g : UInt256) (A : Substate) - (I : ExecutionEnv .EVM) + (I : ExecutionEnv τ) : - (Bool × AccountMap .EVM × UInt256 × Substate × ByteArray) + (Bool × AccountMap τ × UInt256 × Substate × ByteArray) := let gᵣ : ℕ := let l := I.calldata.size @@ -139,15 +139,17 @@ def expModAux (m : ℕ) (a : ℕ) (c : ℕ) : ℕ → ℕ def expMod (m : ℕ) (b : UInt256) (n : ℕ) : ℕ := expModAux m 1 b.toNat n -def Ξ_EXPMOD - (σ : AccountMap .EVM) - (g : UInt256) - (A : Substate) - (I : ExecutionEnv .EVM) - : - (Bool × AccountMap .EVM × UInt256 × Substate × ByteArray) -:= - let data := I.calldata +def Ξ_EXPMOD_lengthLimit : ℕ := 1024 + +def Ξ_EXPMOD_lengthsWithinLimit (data : ByteArray) : Bool := + let base_length := nat_of_slice data 0 32 + let exp_length := nat_of_slice data 32 32 + let modulus_length := nat_of_slice data 64 32 + base_length ≤ Ξ_EXPMOD_lengthLimit + && exp_length ≤ Ξ_EXPMOD_lengthLimit + && modulus_length ≤ Ξ_EXPMOD_lengthLimit + +def Ξ_EXPMOD_gasCost (data : ByteArray) : ℕ := let base_length := nat_of_slice data 0 32 let exp_length := nat_of_slice data 32 32 let modulus_length := nat_of_slice data 64 32 @@ -155,46 +157,62 @@ def Ξ_EXPMOD -- We don't want to call `nat_of_slice` unless we need it let exp := λ () ↦ nat_of_slice data (96 + base_length) exp_length - let gᵣ := - let multiplication_complexity x y := ((max x y + 7) / 8) ^ 2 - let adjusted_exp_length := - if exp_length ≤ 32 && exp () == 0 then - 0 + let multiplication_complexity x y := + let max_length := max x y + let words := (max_length + 7) / 8 + if max_length > 32 then 2 * words ^ 2 else 16 + let adjusted_exp_length := + if exp_length ≤ 32 && exp () == 0 then + 0 + else + if exp_length ≤ 32 then + Nat.log 2 (exp ()) else - if exp_length ≤ 32 then - Nat.log 2 (exp ()) - else - let length_part := 8 * (exp_length - 32) - let bits_part := - let exp_head := nat_of_slice data (96 + base_length) 32 - if 32 < exp_length ∧ exp_head != 0 then - Nat.log 2 exp_head - else - 0 - length_part + bits_part - let iterations := max adjusted_exp_length 1 - let G_quaddivisor := 3 + let length_part := 16 * (exp_length - 32) + let bits_part := + let exp_head := nat_of_slice data (96 + base_length) 32 + if 32 < exp_length ∧ exp_head != 0 then + Nat.log 2 exp_head + else + 0 + length_part + bits_part + let iterations := max adjusted_exp_length 1 + max 500 (multiplication_complexity base_length modulus_length * iterations) - max 200 (multiplication_complexity base_length modulus_length * iterations / G_quaddivisor) +def Ξ_EXPMOD_output (data : ByteArray) : ByteArray := + let base_length := nat_of_slice data 0 32 + let exp_length := nat_of_slice data 32 32 + let modulus_length := nat_of_slice data 64 32 + let modulus := nat_of_slice data (96 + base_length + exp_length) modulus_length + if modulus_length == 0 || modulus == 0 then + ffi.ByteArray.zeroes ⟨modulus_length⟩ + else + let base := nat_of_slice data 96 base_length + let exp := nat_of_slice data (96 + base_length) exp_length + let expmod_base := BE (expMod modulus (.ofNat base) exp) + let expmod_zeroes := + if modulus_length ≥ expmod_base.size then + ffi.ByteArray.zeroes ⟨modulus_length - expmod_base.size⟩ + else + ByteArray.empty + expmod_zeroes ++ expmod_base - if g.toNat < gᵣ then +def Ξ_EXPMOD {τ : OperationType} + (σ : AccountMap τ) + (g : UInt256) + (A : Substate) + (I : ExecutionEnv τ) + : + (Bool × AccountMap τ × UInt256 × Substate × ByteArray) +:= + let data := I.calldata + let gᵣ := Ξ_EXPMOD_gasCost data + if !Ξ_EXPMOD_lengthsWithinLimit data then + (false, ∅, ⟨0⟩, A, .empty) + else if g.toNat < gᵣ then (false, ∅, ⟨0⟩, A, .empty) else - let modulus := nat_of_slice data (96 + base_length + exp_length) modulus_length - let o : ByteArray := - if modulus_length == 0 || modulus == 0 then - ffi.ByteArray.zeroes ⟨modulus_length⟩ - else - let base := nat_of_slice data 96 base_length - let exp := nat_of_slice data (96 + base_length) exp_length - let expmod_base := BE (expMod modulus (.ofNat base) exp) - let expmod_zeroes := - if modulus_length ≥ expmod_base.size then - ffi.ByteArray.zeroes ⟨modulus_length - expmod_base.size⟩ - else - ByteArray.empty - expmod_zeroes ++ expmod_base - (true, σ, g - .ofNat gᵣ, A, o) + (true, σ, g - .ofNat gᵣ, A, Ξ_EXPMOD_output data) private def expmodOutput := let (_, _, _, _, o) := @@ -214,13 +232,13 @@ private def expmodOutput := E : ByteArray := ⟨#[2]⟩ M : ByteArray := ⟨#[100]⟩ -def Ξ_BN_ADD - (σ : AccountMap .EVM) +def Ξ_BN_ADD {τ : OperationType} + (σ : AccountMap τ) (g : UInt256) (A : Substate) - (I : ExecutionEnv .EVM) + (I : ExecutionEnv τ) : - (Bool × AccountMap .EVM × UInt256 × Substate × ByteArray) + (Bool × AccountMap τ × UInt256 × Substate × ByteArray) := let gᵣ : ℕ := 150 @@ -268,13 +286,13 @@ private def bn_addOutput₁ := x : ByteArray := UInt256.toByteArray ⟨1⟩ y : ByteArray := UInt256.toByteArray ⟨2⟩ -def Ξ_BN_MUL - (σ : AccountMap .EVM) +def Ξ_BN_MUL {τ : OperationType} + (σ : AccountMap τ) (g : UInt256) (A : Substate) - (I : ExecutionEnv .EVM) + (I : ExecutionEnv τ) : - (Bool × AccountMap .EVM × UInt256 × Substate × ByteArray) + (Bool × AccountMap τ × UInt256 × Substate × ByteArray) := let gᵣ : ℕ := 6000 @@ -307,13 +325,13 @@ private def bn_mulOutput := y₁ : ByteArray := UInt256.toByteArray ⟨2⟩ n : ByteArray := UInt256.toByteArray ⟨2⟩ -def Ξ_SNARKV - (σ : AccountMap .EVM) +def Ξ_SNARKV {τ : OperationType} + (σ : AccountMap τ) (g : UInt256) (A : Substate) - (I : ExecutionEnv .EVM) + (I : ExecutionEnv τ) : - (Bool × AccountMap .EVM × UInt256 × Substate × ByteArray) + (Bool × AccountMap τ × UInt256 × Substate × ByteArray) := let d := I.calldata let k := d.size / 192 @@ -343,13 +361,13 @@ private def snarkvOutput := x : ByteArray := UInt256.toByteArray ⟨1⟩ y : ByteArray := UInt256.toByteArray ⟨2⟩ -def Ξ_BLAKE2_F - (σ : AccountMap .EVM) +def Ξ_BLAKE2_F {τ : OperationType} + (σ : AccountMap τ) (g : UInt256) (A : Substate) - (I : ExecutionEnv .EVM) + (I : ExecutionEnv τ) : - (Bool × AccountMap .EVM × UInt256 × Substate × ByteArray) + (Bool × AccountMap τ × UInt256 × Substate × ByteArray) := let d := I.calldata let gᵣ : ℕ := fromByteArrayBigEndian (d.extract 0 4) @@ -365,13 +383,13 @@ def Ξ_BLAKE2_F dbg_trace s!"Ξ_BLAKE2_F failed: {e}" (false, ∅, ⟨0⟩, A, .empty) -def Ξ_PointEval - (σ : AccountMap .EVM) +def Ξ_PointEval {τ : OperationType} + (σ : AccountMap τ) (g : UInt256) (A : Substate) - (I : ExecutionEnv .EVM) + (I : ExecutionEnv τ) : - (Bool × AccountMap .EVM × UInt256 × Substate × ByteArray) + (Bool × AccountMap τ × UInt256 × Substate × ByteArray) := let d := I.calldata let gᵣ : ℕ := 50000 @@ -385,3 +403,19 @@ def Ξ_PointEval | .error e => dbg_trace s!"Ξ_PointEval failed: {e}" (false, ∅, ⟨0⟩, A, .empty) + +def runPrecompiledContract {τ : OperationType} (precompiled : PrecompiledContract) + (σ : AccountMap τ) (g : UInt256) (A : Substate) + (I : ExecutionEnv τ) : + Bool × AccountMap τ × UInt256 × Substate × ByteArray := + match precompiled with + | .ecrec => Ξ_ECREC σ g A I + | .sha256 => Ξ_SHA256 σ g A I + | .rip160 => Ξ_RIP160 σ g A I + | .identity => Ξ_ID σ g A I + | .expmod => Ξ_EXPMOD σ g A I + | .bnAdd => Ξ_BN_ADD σ g A I + | .bnMul => Ξ_BN_MUL σ g A I + | .snarkv => Ξ_SNARKV σ g A I + | .blake2F => Ξ_BLAKE2_F σ g A I + | .pointEval => Ξ_PointEval σ g A I diff --git a/EvmYul/EVM/Semantics.lean b/EvmYul/EVM/Semantics.lean index 14dcf58..c582cb7 100644 --- a/EvmYul/EVM/Semantics.lean +++ b/EvmYul/EVM/Semantics.lean @@ -136,6 +136,10 @@ def swap (n : ℕ) : Transformer := local instance : MonadLift Option (Except EVM.ExecutionException) := ⟨Option.option (.error .StackUnderflow) .ok⟩ +structure ChildFrameChainContext where + totalGasUsedInBlock : ℕ + transactionReceipts : Array TransactionReceipt + mutual def call (fuel : Nat) @@ -156,11 +160,15 @@ def call (fuel : Nat) let Iₐ := evmState.executionEnv.codeOwner let σ := evmState.accountMap let Iₑ := evmState.executionEnv.depth - let callgas := Ccallgas t recipient value gas σ evmState.toMachineState evmState.substate + let protocol := evmState.executionEnv.protocol + let callgas := + CcallgasWithProtocolFor protocol evmState t recipient value gas σ + evmState.toMachineState evmState.substate let evmState := {evmState with gasAvailable := evmState.gasAvailable - UInt256.ofNat gasCost} -- m[μs[3] . . . (μs[3] + μs[4] − 1)] let i := evmState.memory.readWithPadding inOffset.toNat inSize.toNat let A' := evmState.addAccessedAccount t |>.substate + let A' := addDelegatedCodeAccess t σ A' let (cA, σ', g', A', z, o) ← do if value ≤ (σ.find? Iₐ |>.option ⟨0⟩ (·.balance)) ∧ Iₑ < 1024 then let resultOfΘ ← @@ -171,6 +179,9 @@ def call (fuel : Nat) (blocks := evmState.blocks) (σ := σ) -- σ in Θ(σ, ..) (σ₀ := evmState.σ₀) + (chainContext := + { totalGasUsedInBlock := evmState.totalGasUsedInBlock + transactionReceipts := evmState.transactionReceipts }) (A := A') -- A* in Θ(.., A*, ..) (s := source) (o := evmState.executionEnv.sender) -- Iₒ in Θ(.., Iₒ, ..) @@ -184,16 +195,15 @@ def call (fuel : Nat) (e := Iₑ + 1) (H := evmState.executionEnv.header) (w := permission) -- I_w in Θ(.., I_W) + (protocol := protocol) pure resultOfΘ else -- otherwise (σ, CCALLGAS(σ, μ, A), A, 0, ()) .ok (evmState.createdAccounts, evmState.toState.accountMap, .ofNat callgas, A', false, .empty) - -- n ≡ min({μs[6], ‖o‖}) - let n : UInt256 := min outSize (.ofNat o.size) - - let μ'ₘ := writeBytes o 0 evmState.toMachineState outOffset.toNat n.toNat -- μ′_m[μs[5] ... (μs[5] + n − 1)] = o[0 ... (n − 1)] - let μ'ₒ := o -- μ′o = o + let μ'ₘ := + evmState.toMachineState.finishExternalCall o + inOffset inSize outOffset outSize let μ'_g := μ'ₘ.gasAvailable + g' -- Ccall is subtracted in X as part of C let codeExecutionFailed : Bool := !z @@ -204,11 +214,7 @@ def call (fuel : Nat) -- NB. `MachineState` here does not contain the `Stack` nor the `PC`, thus incomplete. let μ'incomplete : MachineState := { μ'ₘ with - returnData := μ'ₒ gasAvailable := μ'_g - activeWords := - let m : ℕ:= MachineState.M evmState.toMachineState.activeWords.toNat inOffset.toNat inSize.toNat - .ofNat <| MachineState.M m outOffset.toNat outSize.toNat } @@ -260,6 +266,8 @@ def step (fuel : ℕ) (gasCost : ℕ) (instr : Option (Operation .EVM × Option evmState.blocks σStar evmState.σ₀ + { totalGasUsedInBlock := evmState.totalGasUsedInBlock + transactionReceipts := evmState.transactionReceipts } evmState.toState.substate Iₐ Iₒ @@ -271,6 +279,7 @@ def step (fuel : ℕ) (gasCost : ℕ) (instr : Option (Operation .EVM × Option ζ I.header I.perm + I.protocol match Λ with | .ok (a, cA, σ', g', A', z, o) => ( a @@ -296,6 +305,7 @@ def step (fuel : ℕ) (gasCost : ℕ) (instr : Option (Operation .EVM × Option { evmState' with activeWords := .ofNat <| MachineState.M evmState.activeWords.toNat μ₁.toNat μ₂.toNat returnData := newReturnData + H_return := ByteArray.empty gasAvailable := .ofNat <| evmState.gasAvailable.toNat - L (evmState.gasAvailable.toNat) + g'.toNat } @@ -327,6 +337,8 @@ def step (fuel : ℕ) (gasCost : ℕ) (instr : Option (Operation .EVM × Option evmState.blocks σStar evmState.σ₀ + { totalGasUsedInBlock := evmState.totalGasUsedInBlock + transactionReceipts := evmState.transactionReceipts } evmState.toState.substate Iₐ Iₒ @@ -338,6 +350,7 @@ def step (fuel : ℕ) (gasCost : ℕ) (instr : Option (Operation .EVM × Option ζ I.header I.perm + I.protocol match Λ with | .ok (a, cA, σ', g', A', z, o) => (a, {evmState with accountMap := σ', substate := A', createdAccounts := cA}, g', z, o) @@ -354,6 +367,7 @@ def step (fuel : ℕ) (gasCost : ℕ) (instr : Option (Operation .EVM × Option { evmState' with activeWords := .ofNat <| MachineState.M evmState.activeWords.toNat μ₁.toNat μ₂.toNat returnData := newReturnData + H_return := ByteArray.empty gasAvailable := .ofNat <| evmState.gasAvailable.toNat - L (evmState.gasAvailable.toNat) + g'.toNat } .ok <| evmState'.replaceStackAndIncrPC (stack.push x) @@ -445,7 +459,7 @@ def X (fuel : ℕ) (validJumps : Array UInt256) (evmState : State) .error .OutOfGass let gasAvailable := evmState.gasAvailable - .ofNat cost₁ let evmState := { evmState with gasAvailable := gasAvailable} - let cost₂ := C' evmState w + let cost₂ := selectedC' evmState w if evmState.gasAvailable.toNat < cost₂ then .error .OutOfGass @@ -529,6 +543,7 @@ def Ξ -- Type `Ξ` using `\GX` or `\Xi` (blocks : ProcessedBlocks) (σ : AccountMap .EVM) (σ₀ : AccountMap .EVM) + (chainContext : ChildFrameChainContext) (g : UInt256) (A : Substate) (I : ExecutionEnv .EVM) @@ -545,6 +560,8 @@ def Ξ -- Type `Ξ` using `\GX` or `\Xi` { defState with accountMap := σ σ₀ := σ₀ + totalGasUsedInBlock := chainContext.totalGasUsedInBlock + transactionReceipts := chainContext.transactionReceipts executionEnv := I substate := A createdAccounts := createdAccounts @@ -567,6 +584,7 @@ def Lambda (blocks : ProcessedBlocks) (σ : AccountMap .EVM) (σ₀ : AccountMap .EVM) + (chainContext : ChildFrameChainContext) (A : Substate) (s : AccountAddress) -- sender (o : AccountAddress) -- original transactor @@ -578,6 +596,7 @@ def Lambda (ζ : Option ByteArray) -- the salt (92) (H : BlockHeader) -- "I_H has no special treatment and is determined from the blockchain" (w : Bool) -- permission to make modifications to the state + (protocol : Protocol := Protocol.osaka) : Except EVM.ExecutionException ( AccountAddress @@ -650,8 +669,10 @@ def Lambda , depth := e.toNat , perm := w , blobVersionedHashes := blobVersionedHashes + , protocol := protocol } - match Ξ f createdAccounts genesisBlockHeader blocks σStar σ₀ g AStar exEnv with + match Ξ f createdAccounts genesisBlockHeader blocks σStar σ₀ + chainContext g AStar exEnv with | .error e => if e == .OutOfFuel then throw .OutOfFuel .ok (a, createdAccounts, σ, ⟨0⟩, AStar, false, .empty) @@ -659,7 +680,7 @@ def Lambda .ok (a, createdAccounts, σ, g', AStar, false, o) | .ok (.success (createdAccounts', σStarStar, gStarStar, AStarStar) returnedData) => -- The code-deposit cost (113) - let c := GasConstants.Gcodedeposit * returnedData.size + let c := protocol.codeDepositGasPerByteFor exEnv * returnedData.size let F : Bool := Id.run do -- (118) let F₀ : Bool := @@ -695,6 +716,77 @@ def Lambda | none => RLP <| .𝕃 [.𝔹 s, .𝔹 n] | some ζ => .some <| BE 255 ++ s ++ ζ ++ ffi.KEC i +/-- +Recipient-credit step of the message-call account-map prelude. + +This mirrors equations (124)-(126) for `Θ`: a missing recipient is +materialized only for nonzero value, while an existing recipient is credited. +-/ +def thetaCallRecipientCredit + (σ : AccountMap .EVM) (r : AccountAddress) (v : UInt256) : + AccountMap .EVM := + match σ.find? r with + | none => + if v != ⟨0⟩ then + σ.insert r { (default : Account .EVM) with balance := v } + else + σ + | some acc => + σ.insert r { acc with balance := acc.balance + v } + +/-- +Sender-debit step of the message-call account-map prelude. + +The balance precondition is checked by the caller-side opcode semantics before +`Θ`; this helper only mirrors the unconditional debit shape used inside `Θ`. +-/ +def thetaCallSourceDebit + (σ : AccountMap .EVM) (s : AccountAddress) (v : UInt256) : + AccountMap .EVM := + match σ.find? s with + | none => σ + | some acc => + σ.insert s { acc with balance := acc.balance - v } + +/-- +Full account-map prelude for an EVM message call. +-/ +def thetaCallTransfer + (σ : AccountMap .EVM) (s r : AccountAddress) (v : UInt256) : + AccountMap .EVM := + thetaCallSourceDebit (thetaCallRecipientCredit σ r v) s v + +/-- +Execution environment installed for a `Θ` child frame. +-/ +def thetaCallExecutionEnv + (blobVersionedHashes : List ByteArray) + (s o r : AccountAddress) + (c : ToExecute .EVM) + (p v' : UInt256) + (d : ByteArray) + (e : Nat) + (H : BlockHeader) + (w : Bool) + (protocol : Protocol := Protocol.osaka) : ExecutionEnv .EVM := + { + codeOwner := r + sender := o + gasPrice := p.toNat + calldata := d + source := s + weiValue := v' + depth := e + perm := w + code := + match c with + | ToExecute.Precompiled _ => default + | ToExecute.Code code => code + header := H + blobVersionedHashes := blobVersionedHashes + protocol := protocol + } + /-- Message cal `σ` - evm state @@ -721,6 +813,7 @@ def Θ (fuel : Nat) (blocks : ProcessedBlocks) (σ : AccountMap .EVM) (σ₀ : AccountMap .EVM) + (chainContext : ChildFrameChainContext) (A : Substate) (s : AccountAddress) (o : AccountAddress) @@ -734,6 +827,7 @@ def Θ (fuel : Nat) (e : Nat) (H : BlockHeader) (w : Bool) + (protocol : Protocol := Protocol.osaka) : Except EVM.ExecutionException (Batteries.RBSet AccountAddress compare × AccountMap .EVM × UInt256 × Substate × Bool × ByteArray) := @@ -742,61 +836,19 @@ def Θ (fuel : Nat) | fuel + 1 => do -- (124) (125) (126) - let σ'₁ := - match σ.find? r with - | none => - if v != ⟨0⟩ then - σ.insert r { (default : Account .EVM) with balance := v} - else - σ - | some acc => - σ.insert r { acc with balance := acc.balance + v} - - -- If `v` ≠ 0 then the sender must have passed the `INSUFFICIENT_ACCOUNT_FUNDS` check - let σ₁ := - match σ'₁.find? s with - | none => σ'₁ - | some acc => - σ'₁.insert s { acc with balance := acc.balance - v} + let σ₁ := thetaCallTransfer σ s r v let I : ExecutionEnv .EVM := - { - codeOwner := r -- Equation (132) - sender := o -- Equation (133) - gasPrice := p.toNat -- Equation (134) - calldata := d -- Equation (135) - source := s -- Equation (136) - weiValue := v' -- Equation (137) - depth := e -- Equation (138) - perm := w -- Equation (139) - -- Note that we don't use an address, but the actual code. Equation (141)-ish. - code := - match c with - | ToExecute.Precompiled _ => default - | ToExecute.Code code => code - header := H - blobVersionedHashes := blobVersionedHashes - } + thetaCallExecutionEnv blobVersionedHashes s o r c p v' d e H w protocol -- Equation (131) - -- Note that the `c` used here is the actual code, not the address. TODO - Handle precompiled contracts. let (createdAccounts, z, σ'', g', A'', out) ← match c with | ToExecute.Precompiled p => - match p with - | 1 => .ok <| (∅, Ξ_ECREC σ₁ g A I) - | 2 => .ok <| (∅, Ξ_SHA256 σ₁ g A I) - | 3 => .ok <| (∅, Ξ_RIP160 σ₁ g A I) - | 4 => .ok <| (∅, Ξ_ID σ₁ g A I) - | 5 => .ok <| (∅, Ξ_EXPMOD σ₁ g A I) - | 6 => .ok <| (∅, Ξ_BN_ADD σ₁ g A I) - | 7 => .ok <| (∅, Ξ_BN_MUL σ₁ g A I) - | 8 => .ok <| (∅, Ξ_SNARKV σ₁ g A I) - | 9 => .ok <| (∅, Ξ_BLAKE2_F σ₁ g A I) - | 10 => .ok <| (∅, Ξ_PointEval σ₁ g A I) - | _ => default + .ok <| (createdAccounts, runPrecompiledContract p σ₁ g A I) | ToExecute.Code _ => - match Ξ fuel createdAccounts genesisBlockHeader blocks σ₁ σ₀ g A I with + match Ξ fuel createdAccounts genesisBlockHeader blocks σ₁ σ₀ + chainContext g A I with | .error e => if e == .OutOfFuel then throw .OutOfFuel pure (createdAccounts, false, σ, ⟨0⟩, A, .empty) @@ -806,10 +858,10 @@ def Θ (fuel : Nat) pure (a, true, b, c, d, o) -- Equation (127) - let σ' := if σ'' == ∅ then σ else σ'' + let σ' := if σ''.isEmpty then σ else σ'' -- Equation (129) - let A' := if σ'' == ∅ then A else A'' + let A' := if σ''.isEmpty then A else A'' -- Equation (119) .ok (createdAccounts, σ', g', A', z, out) @@ -828,9 +880,10 @@ def Υ (fuel : ℕ) (blocks : ProcessedBlocks) (T : Transaction) (S_T : AccountAddress) + (protocol : Protocol := Protocol.osaka) : Except EVM.Exception (AccountMap .EVM × Substate × Bool × UInt256) := do - let g₀ : ℕ := EVM.intrinsicGas T + let g₀ : ℕ := EVM.intrinsicGasWithProtocolFor protocol T T -- "here can be no invalid transactions from this point" let senderAccount := (σ.find? S_T).get! -- The priority fee (67) @@ -875,6 +928,10 @@ def Υ (fuel : ℕ) | none => a let AStar := -- (77) { A0 with accessedAccounts := AStarₐ, accessedStorageKeys := Batteries.RBSet.ofList AStar_K Substate.storageKeysCmp} + let AStar := + match T.base.recipient with + | some t => addDelegatedCodeAccess t σ₀ AStar + | none => AStar let createdAccounts : Batteries.RBSet AccountAddress compare := .empty let (/- provisional state -/ σ_P, g', A, z) ← -- (76) match T.base.recipient with @@ -887,6 +944,8 @@ def Υ (fuel : ℕ) blocks σ₀ σ₀ + { totalGasUsedInBlock := 0 + transactionReceipts := #[] } AStar S_T S_T @@ -898,6 +957,7 @@ def Υ (fuel : ℕ) none H true + protocol with | .ok (_, _, σ_P, g', A, z, _) => pure (σ_P, g', A, z) | .error e => .error <| .ExecutionException e @@ -911,6 +971,8 @@ def Υ (fuel : ℕ) blocks σ₀ σ₀ + { totalGasUsedInBlock := 0 + transactionReceipts := #[] } AStar S_T S_T @@ -924,6 +986,7 @@ def Υ (fuel : ℕ) 0 H true + protocol with | .ok (_, σ_P, g', A, z, _) => pure (σ_P, g', A, z) | .error e => .error <| .ExecutionException e diff --git a/EvmYul/EllipticCurves.lean b/EvmYul/EllipticCurves.lean index a7e86c8..db830b7 100644 --- a/EvmYul/EllipticCurves.lean +++ b/EvmYul/EllipticCurves.lean @@ -16,7 +16,8 @@ def blobECDSARECOVER (e v r s : String) : String := } def blobSign (e pᵣ : String) : List String := - (String.split · Char.isWhitespace) ∘ totallySafePerformIO ∘ IO.Process.run <| + ((String.split · Char.isWhitespace) · |>.toList.map (·.toString)) ∘ + totallySafePerformIO ∘ IO.Process.run <| pythonCommandOfInput e pᵣ where pythonCommandOfInput (e pᵣ : String) : IO.Process.SpawnArgs := { cmd := "python3", diff --git a/EvmYul/FFI/ffi.lean b/EvmYul/FFI/ffi.lean index a7178da..6015465 100644 --- a/EvmYul/FFI/ffi.lean +++ b/EvmYul/FFI/ffi.lean @@ -1,3 +1,5 @@ +import Init.Data.Array.Lemmas + namespace ffi @[extern "sha256"] @@ -15,7 +17,61 @@ def BLAKE2 (d : ByteArray) : Except String ByteArray := do return BLAKE2Compress d @[extern "memset_zero"] -opaque ByteArray.zeroes (n : USize) : ByteArray +def ByteArray.zeroes (n : USize) : ByteArray := + ⟨Array.replicate n.toNat 0⟩ + +@[simp] theorem ByteArray.size_zeroes (n : USize) : + (ByteArray.zeroes n).size = n.toNat := by + change (Array.replicate n.toNat 0).size = n.toNat + simp + +@[simp] theorem ByteArray.getElem_zeroes + (n : USize) (index : Nat) + (hIndex : index < (ByteArray.zeroes n).size) : + (ByteArray.zeroes n)[index] = 0 := by + rw [ByteArray.size_zeroes] at hIndex + change + (Array.replicate n.toNat 0)[index]'(by simpa using hIndex) = 0 + simp + +@[simp] theorem ByteArray.data_getD_zeroes + (n : USize) (index : Nat) : + (ByteArray.zeroes n).data.getD index 0 = 0 := by + simp [ByteArray.zeroes, Array.getD] + +@[simp] theorem ByteArray.data_getElem?_zeroes + (n : USize) (index : Nat) : + (ByteArray.zeroes n).data[index]? = + if index < n.toNat then some 0 else none := by + simp [ByteArray.zeroes, Array.getElem?_replicate] + +private theorem ByteArray.toList_loop + (bytes : ByteArray) (index : Nat) (acc : List UInt8) : + ByteArray.toList.loop bytes index acc = + acc.reverse ++ bytes.data.toList.drop index := by + fun_induction ByteArray.toList.loop bytes index acc with + | case1 index acc hIndex ih => + rw [ih] + simp only [List.reverse_cons, List.singleton_append, + List.append_assoc] + have hDataIndex : index < bytes.data.toList.length := by + simpa [ByteArray.size_data] using hIndex + rw [List.drop_eq_getElem_cons hDataIndex] + congr 2 + unfold ByteArray.get! + cases bytes with + | mk data => + exact getElem!_pos data index (by simpa using hDataIndex) + | case2 index acc hIndex => + have hData : bytes.data.toList.length ≤ index := by + simpa [ByteArray.size_data, Nat.not_lt] using hIndex + simp [List.drop_eq_nil_iff.mpr hData] + +@[simp] theorem ByteArray.toList_eq_data_toList + (bytes : ByteArray) : + bytes.toList = bytes.data.toList := by + unfold ByteArray.toList + simpa using ByteArray.toList_loop bytes 0 [] @[extern "keccak256"] opaque keccak256 (input : @& ByteArray) (len : USize) : ByteArray diff --git a/EvmYul/MachineStateOps.lean b/EvmYul/MachineStateOps.lean index 490a9f5..c7e0422 100644 --- a/EvmYul/MachineStateOps.lean +++ b/EvmYul/MachineStateOps.lean @@ -78,6 +78,9 @@ def gas (self : MachineState) : UInt256 := section ReturnData +def freshExternalCall (gas : UInt256) : MachineState := + { (default : MachineState) with gasAvailable := gas } + def setReturnData (self : MachineState) (r : ByteArray) : MachineState := { self with returnData := r } @@ -97,6 +100,21 @@ def returndatacopy (self : MachineState) (mstart rstart size : UInt256) : Machin .ofNat (MachineState.M self.activeWords.toNat mstart.toNat size.toNat) } +def finishExternalCall + (self : MachineState) (returnData : ByteArray) + (inOffset inSize outOffset outSize : UInt256) : MachineState := + let copyLen := min outSize.toNat returnData.size + let self := writeBytes returnData 0 self outOffset.toNat copyLen + { self with + activeWords := + .ofNat <| + MachineState.M + (MachineState.M self.activeWords.toNat inOffset.toNat inSize.toNat) + outOffset.toNat outSize.toNat + returnData := returnData + H_return := ByteArray.empty + } + def evmReturn (self : MachineState) (mstart s : UInt256) : MachineState := { self with diff --git a/EvmYul/Maps/AccountMap.lean b/EvmYul/Maps/AccountMap.lean index 3b8ecca..0f59f6e 100644 --- a/EvmYul/Maps/AccountMap.lean +++ b/EvmYul/Maps/AccountMap.lean @@ -64,18 +64,40 @@ def AccountMap.transferBalance (τ : OperationType) (σ : AccountMap τ) (from_a | .none => .none | .some σ' => σ'.increaseBalance τ to_addr amount +def AccountMap.delegatedCodeTarget? (σ : AccountMap .EVM) (t : AccountAddress) : + Option AccountAddress := + match PrecompiledContract.ofAddress? t with + | some _ => none + | none => + match σ.find? t with + | none => none + | some account => account.eip7702DelegationTarget? + +def AccountMap.delegatedToExecute (σ : AccountMap .EVM) (t : AccountAddress) : + ToExecute .EVM := + match σ.delegatedCodeTarget? t with + | none => + match σ.find? t with + | none => ToExecute.Code default + | some account => ToExecute.Code account.code + | some target => + if (PrecompiledContract.ofAddress? target).isSome then + ToExecute.Code default + else + match σ.find? target with + | none => ToExecute.Code default + | some account => ToExecute.Code account.code + def toExecute (τ : OperationType) (σ : AccountMap τ) (t : AccountAddress) : ToExecute τ := - if /- t is a precompiled account -/ t ∈ π then - ToExecute.Precompiled t - else Id.run do - match τ with - | .EVM => - -- We use the code directly without an indirection a'la `codeMap[t]`. - let .some tDirect := σ.find? t | ToExecute.Code default - ToExecute.Code tDirect.code - | .Yul => - let .some tDirect := σ.find? t | ToExecute.Code default - ToExecute.Code tDirect.code + match PrecompiledContract.ofAddress? t with + | some precompiled => ToExecute.Precompiled precompiled + | none => Id.run do + match τ with + | .EVM => + AccountMap.delegatedToExecute σ t + | .Yul => + let .some tDirect := σ.find? t | ToExecute.Code default + ToExecute.Code tDirect.code def L_S (σ : PersistentAccountMap .EVM) : Array (ByteArray × ByteArray) := σ.foldl diff --git a/EvmYul/Operations.lean b/EvmYul/Operations.lean index 214f80f..0d02cac 100644 --- a/EvmYul/Operations.lean +++ b/EvmYul/Operations.lean @@ -156,6 +156,11 @@ inductive CBLOp (τ : OperationType) : Type where δ:2 ; α:1 -/ | protected SAR : CBLOp τ + /-- + CLZ: count leading zero bits in a 256-bit word + δ:1 ; α:1 + -/ + | protected CLZ : CBLOp τ deriving DecidableEq, Repr /-- @@ -604,6 +609,7 @@ abbrev BYTE {τ : OperationType} : Operation τ := .CompBit .BYTE abbrev SHL {τ : OperationType} : Operation τ := .CompBit .SHL abbrev SHR {τ : OperationType} : Operation τ := .CompBit .SHR abbrev SAR {τ : OperationType} : Operation τ := .CompBit .SAR +abbrev CLZ {τ : OperationType} : Operation τ := .CompBit .CLZ abbrev KECCAK256 {τ : OperationType} : Operation τ := .Keccak .KECCAK256 diff --git a/EvmYul/Pretty.lean b/EvmYul/Pretty.lean index 99bc48e..abdb473 100644 --- a/EvmYul/Pretty.lean +++ b/EvmYul/Pretty.lean @@ -21,7 +21,7 @@ This breaks the moment that `Repr` changes its behaviour; it is fine for the tim def Operation.pretty {τ} (self : Operation τ) : String := let reprStr := ToString.toString <| repr self let lastComponent := reprStr.splitOn "." |>.getLast! - lastComponent.take lastComponent.length.pred + (lastComponent.take lastComponent.length.pred).toString /-- `Finmap`s are not very computation-friendly and so the API is ever so slightly meh; @@ -32,8 +32,8 @@ TODO(not critical) - Unify all the maps used throught the formalisation one day. def Finmap.pretty {α β : Type} [ToString α] [ToString β] [LE ((_ : α) × β)] [IsTrans ((_ : α) × β) fun x x_1 => x ≤ x_1] - [IsAntisymm ((_ : α) × β) fun x x_1 => x ≤ x_1] - [IsTotal ((_ : α) × β) fun x x_1 => x ≤ x_1] + [Std.Antisymm (α := ((_ : α) × β)) fun x x_1 => x ≤ x_1] + [Std.Total (α := ((_ : α) × β)) fun x x_1 => x ≤ x_1] [DecidableRel fun (x : ((_ : α) × β)) x_1 => x ≤ x_1] (self : Finmap (λ _ : α ↦ β)) : String := Id.run do let mut result : String := "" diff --git a/EvmYul/Protocol.lean b/EvmYul/Protocol.lean new file mode 100644 index 0000000..0e9e22e --- /dev/null +++ b/EvmYul/Protocol.lean @@ -0,0 +1,351 @@ +namespace EvmYul + +universe u + +/-- +Ethereum fork baseline represented by this semantics checkout. + +This fork intentionally does not keep historical protocol variants alive: the +compiler-facing Ethereum baseline is Osaka. +-/ +inductive EthereumFork where + | osaka + deriving BEq, DecidableEq, Inhabited, Repr + +/-- +Tempo protocol levels relevant to EVM execution/accounting. + +`t6` is the active mainnet level at the time this profile was added; `t7` is +the latest documented Tempo level and adds storage credits plus dynamic-base-fee +accounting. Pre-T1C opcode behavior is intentionally excluded. +-/ +inductive TempoFork where + | t6 + | t7 + deriving BEq, DecidableEq, Inhabited, Repr + +inductive Protocol where + | ethereum (fork : EthereumFork) + | tempo (fork : TempoFork) + deriving BEq, DecidableEq, Repr + +/-- +Open gas/accounting policy interface. + +The policy is parameterized by the state type it observes. Osaka and current +Tempo profiles below are constant policies, but callers can instantiate this +record with a richer state carrying private accounting metadata and make any +gas component depend on that state. +-/ +structure GasPolicy (State : Type u) where + sstoreSetGas : State → Nat + callValueTransferGas : State → Nat + callNewAccountGas : State → Nat + firstNonceAccountCreationGas : State → Nat + createBaseGas : State → Nat + codeDepositGasPerByte : State → Nat + txCreateGas : State → Nat + initCodeWordGas : State → Nat + eip7702AuthorizationBaseGas : State → Nat + hasStorageCredits : State → Bool := fun _ => false + hasDynamicBaseFee : State → Bool := fun _ => false + tip20RewardsDeprecated : State → Bool := fun _ => false + +namespace GasPolicy + +def osaka {State : Type u} : GasPolicy State where + sstoreSetGas := fun _ => 20000 + callValueTransferGas := fun _ => 9000 + callNewAccountGas := fun _ => 25000 + firstNonceAccountCreationGas := fun _ => 0 + createBaseGas := fun _ => 32000 + codeDepositGasPerByte := fun _ => 200 + txCreateGas := fun _ => 32000 + initCodeWordGas := fun _ => 2 + eip7702AuthorizationBaseGas := fun _ => 25000 + hasStorageCredits := fun _ => false + hasDynamicBaseFee := fun _ => false + tip20RewardsDeprecated := fun _ => false + +def tempoT6 {State : Type u} : GasPolicy State where + sstoreSetGas := fun _ => 250000 + callValueTransferGas := fun _ => 0 + callNewAccountGas := fun _ => 0 + firstNonceAccountCreationGas := fun _ => 250000 + createBaseGas := fun _ => 500000 + codeDepositGasPerByte := fun _ => 1000 + txCreateGas := fun _ => 500000 + initCodeWordGas := fun _ => 0 + eip7702AuthorizationBaseGas := fun _ => 12500 + hasStorageCredits := fun _ => false + hasDynamicBaseFee := fun _ => false + tip20RewardsDeprecated := fun _ => false + +def tempoLatest {State : Type u} : GasPolicy State where + sstoreSetGas := fun _ => 250000 + callValueTransferGas := fun _ => 0 + callNewAccountGas := fun _ => 0 + firstNonceAccountCreationGas := fun _ => 250000 + createBaseGas := fun _ => 500000 + codeDepositGasPerByte := fun _ => 1000 + txCreateGas := fun _ => 500000 + initCodeWordGas := fun _ => 0 + eip7702AuthorizationBaseGas := fun _ => 12500 + hasStorageCredits := fun _ => true + hasDynamicBaseFee := fun _ => true + tip20RewardsDeprecated := fun _ => true + +end GasPolicy + +namespace Protocol + +def osaka : Protocol := + .ethereum .osaka + +def tempoT6 : Protocol := + .tempo .t6 + +def tempoLatest : Protocol := + .tempo .t7 + +instance : Inhabited Protocol := + ⟨osaka⟩ + +def isTempo : Protocol → Bool + | .tempo _ => true + | .ethereum _ => false + +def nativeBalanceAlwaysZero (protocol : Protocol) : Bool := + protocol.isTempo + +def callValueAlwaysZero (protocol : Protocol) : Bool := + protocol.isTempo + +def nativeValueTransfersEnabled (protocol : Protocol) : Bool := + !protocol.isTempo + +def gasPolicy {State : Type u} : Protocol → GasPolicy State + | .ethereum .osaka => GasPolicy.osaka + | .tempo .t6 => GasPolicy.tempoT6 + | .tempo .t7 => GasPolicy.tempoLatest + +def sstoreSetGasFor {State : Type u} (protocol : Protocol) + (state : State) : Nat := + (protocol.gasPolicy).sstoreSetGas state + +def sstoreSetGas (protocol : Protocol) : Nat := + protocol.sstoreSetGasFor () + +def callValueTransferGasFor {State : Type u} (protocol : Protocol) + (state : State) : Nat := + (protocol.gasPolicy).callValueTransferGas state + +def callValueTransferGas (protocol : Protocol) : Nat := + protocol.callValueTransferGasFor () + +/-- +Gas charged by CALL-style native-value transfer to an empty account. + +Tempo has no native value transfer surface; its explicit account-creation charge +is modeled separately as `firstNonceAccountCreationGas`. +-/ +def callNewAccountGasFor {State : Type u} (protocol : Protocol) + (state : State) : Nat := + (protocol.gasPolicy).callNewAccountGas state + +def callNewAccountGas (protocol : Protocol) : Nat := + protocol.callNewAccountGasFor () + +def firstNonceAccountCreationGasFor {State : Type u} (protocol : Protocol) + (state : State) : Nat := + (protocol.gasPolicy).firstNonceAccountCreationGas state + +def firstNonceAccountCreationGas (protocol : Protocol) : Nat := + protocol.firstNonceAccountCreationGasFor () + +def createBaseGasFor {State : Type u} (protocol : Protocol) + (state : State) : Nat := + (protocol.gasPolicy).createBaseGas state + +def createBaseGas (protocol : Protocol) : Nat := + protocol.createBaseGasFor () + +def codeDepositGasPerByteFor {State : Type u} (protocol : Protocol) + (state : State) : Nat := + (protocol.gasPolicy).codeDepositGasPerByte state + +def codeDepositGasPerByte (protocol : Protocol) : Nat := + protocol.codeDepositGasPerByteFor () + +def txCreateGasFor {State : Type u} (protocol : Protocol) + (state : State) : Nat := + (protocol.gasPolicy).txCreateGas state + +def txCreateGas (protocol : Protocol) : Nat := + protocol.txCreateGasFor () + +def initCodeWordGasFor {State : Type u} (protocol : Protocol) + (state : State) : Nat := + (protocol.gasPolicy).initCodeWordGas state + +def initCodeWordGas (protocol : Protocol) : Nat := + protocol.initCodeWordGasFor () + +def eip7702AuthorizationBaseGasFor {State : Type u} (protocol : Protocol) + (state : State) : Nat := + (protocol.gasPolicy).eip7702AuthorizationBaseGas state + +def eip7702AuthorizationBaseGas (protocol : Protocol) : Nat := + protocol.eip7702AuthorizationBaseGasFor () + +def hasStorageCreditsFor {State : Type u} (protocol : Protocol) + (state : State) : Bool := + (protocol.gasPolicy).hasStorageCredits state + +def hasStorageCredits (protocol : Protocol) : Bool := + protocol.hasStorageCreditsFor () + +def hasDynamicBaseFeeFor {State : Type u} (protocol : Protocol) + (state : State) : Bool := + (protocol.gasPolicy).hasDynamicBaseFee state + +def hasDynamicBaseFee (protocol : Protocol) : Bool := + protocol.hasDynamicBaseFeeFor () + +def tip20RewardsDeprecatedFor {State : Type u} (protocol : Protocol) + (state : State) : Bool := + (protocol.gasPolicy).tip20RewardsDeprecated state + +def tip20RewardsDeprecated (protocol : Protocol) : Bool := + protocol.tip20RewardsDeprecatedFor () + +def allOpcodesSupported (_protocol : Protocol) : Bool := + true + +@[simp] theorem sstoreSetGasFor_eq {State : Type u} + (protocol : Protocol) (state : State) : + protocol.sstoreSetGasFor state = protocol.sstoreSetGas := by + cases protocol with + | ethereum fork => cases fork <;> rfl + | tempo fork => cases fork <;> rfl + +@[simp] theorem callValueTransferGasFor_eq {State : Type u} + (protocol : Protocol) (state : State) : + protocol.callValueTransferGasFor state = + protocol.callValueTransferGas := by + cases protocol with + | ethereum fork => cases fork <;> rfl + | tempo fork => cases fork <;> rfl + +@[simp] theorem callNewAccountGasFor_eq {State : Type u} + (protocol : Protocol) (state : State) : + protocol.callNewAccountGasFor state = + protocol.callNewAccountGas := by + cases protocol with + | ethereum fork => cases fork <;> rfl + | tempo fork => cases fork <;> rfl + +@[simp] theorem firstNonceAccountCreationGasFor_eq {State : Type u} + (protocol : Protocol) (state : State) : + protocol.firstNonceAccountCreationGasFor state = + protocol.firstNonceAccountCreationGas := by + cases protocol with + | ethereum fork => cases fork <;> rfl + | tempo fork => cases fork <;> rfl + +@[simp] theorem createBaseGasFor_eq {State : Type u} + (protocol : Protocol) (state : State) : + protocol.createBaseGasFor state = protocol.createBaseGas := by + cases protocol with + | ethereum fork => cases fork <;> rfl + | tempo fork => cases fork <;> rfl + +@[simp] theorem codeDepositGasPerByteFor_eq {State : Type u} + (protocol : Protocol) (state : State) : + protocol.codeDepositGasPerByteFor state = + protocol.codeDepositGasPerByte := by + cases protocol with + | ethereum fork => cases fork <;> rfl + | tempo fork => cases fork <;> rfl + +@[simp] theorem txCreateGasFor_eq {State : Type u} + (protocol : Protocol) (state : State) : + protocol.txCreateGasFor state = protocol.txCreateGas := by + cases protocol with + | ethereum fork => cases fork <;> rfl + | tempo fork => cases fork <;> rfl + +@[simp] theorem initCodeWordGasFor_eq {State : Type u} + (protocol : Protocol) (state : State) : + protocol.initCodeWordGasFor state = protocol.initCodeWordGas := by + cases protocol with + | ethereum fork => cases fork <;> rfl + | tempo fork => cases fork <;> rfl + +@[simp] theorem eip7702AuthorizationBaseGasFor_eq {State : Type u} + (protocol : Protocol) (state : State) : + protocol.eip7702AuthorizationBaseGasFor state = + protocol.eip7702AuthorizationBaseGas := by + cases protocol with + | ethereum fork => cases fork <;> rfl + | tempo fork => cases fork <;> rfl + +@[simp] theorem hasStorageCreditsFor_eq {State : Type u} + (protocol : Protocol) (state : State) : + protocol.hasStorageCreditsFor state = protocol.hasStorageCredits := by + cases protocol with + | ethereum fork => cases fork <;> rfl + | tempo fork => cases fork <;> rfl + +@[simp] theorem hasDynamicBaseFeeFor_eq {State : Type u} + (protocol : Protocol) (state : State) : + protocol.hasDynamicBaseFeeFor state = protocol.hasDynamicBaseFee := by + cases protocol with + | ethereum fork => cases fork <;> rfl + | tempo fork => cases fork <;> rfl + +@[simp] theorem tip20RewardsDeprecatedFor_eq {State : Type u} + (protocol : Protocol) (state : State) : + protocol.tip20RewardsDeprecatedFor state = + protocol.tip20RewardsDeprecated := by + cases protocol with + | ethereum fork => cases fork <;> rfl + | tempo fork => cases fork <;> rfl + +theorem osaka_not_tempo : osaka.isTempo = false := rfl + +theorem tempoLatest_isTempo : tempoLatest.isTempo = true := rfl + +theorem tempoLatest_sstoreSetGas : + tempoLatest.sstoreSetGas = 250000 := rfl + +theorem tempoLatest_createBaseGas : + tempoLatest.createBaseGas = 500000 := rfl + +theorem tempoLatest_codeDepositGasPerByte : + tempoLatest.codeDepositGasPerByte = 1000 := rfl + +theorem tempoLatest_hasStorageCredits : + tempoLatest.hasStorageCredits = true := rfl + +theorem tempoLatest_hasDynamicBaseFee : + tempoLatest.hasDynamicBaseFee = true := rfl + +theorem osaka_gasPolicy {State : Type u} : + Protocol.gasPolicy (State := State) osaka = GasPolicy.osaka := rfl + +theorem tempoLatest_gasPolicy {State : Type u} : + Protocol.gasPolicy (State := State) tempoLatest = + GasPolicy.tempoLatest := rfl + +theorem osaka_gasPolicy_sstoreSetGas {State : Type u} (state : State) : + (Protocol.gasPolicy (State := State) osaka).sstoreSetGas state = + 20000 := rfl + +theorem tempoLatest_gasPolicy_sstoreSetGas {State : Type u} (state : State) : + (Protocol.gasPolicy (State := State) tempoLatest).sstoreSetGas state = + 250000 := rfl + +end Protocol + +end EvmYul diff --git a/EvmYul/Semantics.lean b/EvmYul/Semantics.lean index 07e65a9..c5e636a 100644 --- a/EvmYul/Semantics.lean +++ b/EvmYul/Semantics.lean @@ -215,7 +215,96 @@ def swap (n : ℕ) : Transformer .EVM := else .error .StackUnderflow --- TODO: Yul halting for `SELFDESTRUCT` +private def evmStop : EVM.Transformer := + fun evmState => + .ok <| + { evmState with + toMachineState := + (evmState.toMachineState.setReturnData ByteArray.empty).setHReturn + ByteArray.empty } + +private def yulStop : Yul.Transformer := + fun yulState _ => + .error <| + Yul.Exception.YulHalt + (Yul.State.setMachineState + ((Yul.State.toMachineState yulState).setHReturn ByteArray.empty) + yulState) + ⟨0⟩ + +def selfdestructAccountMap {τ : OperationType} + (accountMap : AccountMap τ) (source target : AccountAddress) + (created : Bool) : AccountMap τ := + match accountMap.find? source with + | none => + dbg_trace + "No 'self' found to be destructed; this should probably not be happening;" + accountMap + | some sourceAccount => + match accountMap.find? target with + | none => + if sourceAccount.balance == ⟨0⟩ then + accountMap + else + accountMap.insert target + { (default : Account τ) with balance := sourceAccount.balance } + |>.insert source { sourceAccount with balance := ⟨0⟩ } + | some targetAccount => + if target ≠ source then + accountMap.insert target + { targetAccount with + balance := targetAccount.balance + sourceAccount.balance } + |>.insert source { sourceAccount with balance := ⟨0⟩ } + else if created then + accountMap.insert target { targetAccount with balance := ⟨0⟩ } + |>.insert source { sourceAccount with balance := ⟨0⟩ } + else + accountMap + +def EVM.selfdestructState (evmState : EVM.State) (recipient : UInt256) + (tail : Stack UInt256) : EVM.State := + let source := evmState.executionEnv.codeOwner + let target : AccountAddress := AccountAddress.ofUInt256 recipient + let created := evmState.createdAccounts.contains source + let substate' : Substate := + if created then + { evmState.substate with + selfDestructSet := evmState.substate.selfDestructSet.insert source + accessedAccounts := evmState.substate.accessedAccounts.insert target } + else + { evmState.substate with + accessedAccounts := evmState.substate.accessedAccounts.insert target } + let accountMap' := + selfdestructAccountMap evmState.accountMap source target created + let evmState' := { evmState with accountMap := accountMap', substate := substate' } + let evmState'' := evmState'.replaceStackAndIncrPC tail + { evmState'' with + toMachineState := evmState''.toMachineState.setHReturn ByteArray.empty } + +def Yul.selfdestructState (yulState : Yul.State) (recipient : UInt256) : + Yul.State := + let Iₐ := yulState.executionEnv.codeOwner + let r : AccountAddress := AccountAddress.ofUInt256 recipient + let created := yulState.toState.createdAccounts.contains Iₐ + let A' : Substate := + if created then + { yulState.toState.substate with + selfDestructSet := + yulState.toState.substate.selfDestructSet.insert Iₐ + accessedAccounts := + yulState.toState.substate.accessedAccounts.insert r } + else + { yulState.toState.substate with + accessedAccounts := + yulState.toState.substate.accessedAccounts.insert r } + let accountMap' := + selfdestructAccountMap yulState.toState.accountMap Iₐ r created + let yulState' := + yulState.setState + { yulState.toState with accountMap := accountMap', substate := A' } + yulState'.setMachineState + (yulState'.toMachineState.setHReturn ByteArray.empty) + def step {τ : OperationType} (op : Operation τ) (arg : Option (UInt256 × Nat) := .none) : Transformer τ := Id.run do let _ : Id Unit := -- For debug logging match τ with @@ -225,8 +314,8 @@ def step {τ : OperationType} (op : Operation τ) (arg : Option (UInt256 × Nat) -- TODO: Revisit STOP, this is likely not the best way to do it. | τ, .STOP => match τ with - | .EVM => λ evmState ↦ .ok <| {evmState with toMachineState := evmState.toMachineState.setReturnData .empty} - | .Yul => λ yulState _ ↦ .error (Yul.Exception.YulHalt yulState ⟨0⟩) + | .EVM => evmStop + | .Yul => yulStop | τ, .ADD => dispatchBinary τ UInt256.add | τ, .MUL => @@ -277,6 +366,8 @@ def step {τ : OperationType} (op : Operation τ) (arg : Option (UInt256 × Nat) dispatchBinary τ (flip UInt256.shiftRight) | τ, .SAR => dispatchBinary τ UInt256.sar + | τ, .CLZ => + dispatchUnary τ UInt256.clz | τ, .KECCAK256 => dispatchBinaryMachineStateOp' τ MachineState.keccak256 @@ -299,22 +390,26 @@ def step {τ : OperationType} (op : Operation τ) (arg : Option (UInt256 × Nat) dispatchTernaryCopyOp τ .calldatacopy | .EVM, .CODESIZE => dispatchExecutionEnvOp .EVM (.ofNat ∘ ByteArray.size ∘ ExecutionEnv.code) + | .Yul, .CODESIZE => + dispatchExecutionEnvOp .Yul (.ofNat ∘ ByteArray.size ∘ ExecutionEnv.codeBytes) | .EVM, .CODECOPY => dispatchTernaryCopyOp .EVM .codeCopy + | .Yul, .CODECOPY => + dispatchTernaryCopyOp .Yul .codeBytesCopy | τ, .GASPRICE => dispatchExecutionEnvOp τ (.ofNat ∘ ExecutionEnv.gasPrice) - | .EVM, .EXTCODESIZE => - dispatchUnaryStateOp .EVM EvmYul.State.extCodeSize - | .Yul, .EXTCODESIZE => - λ _ _ ↦ .error .YulEXTCODESIZENotImplemented - | .EVM, .EXTCODECOPY => - dispatchQuaternaryCopyOp .EVM EvmYul.SharedState.extCodeCopy' + | τ, .EXTCODESIZE => + dispatchUnaryStateOp τ EvmYul.State.extCodeSize + | τ, .EXTCODECOPY => + dispatchQuaternaryCopyOp τ EvmYul.SharedState.extCodeCopy' | τ, .RETURNDATASIZE => dispatchMachineStateOp τ EvmYul.MachineState.returndatasize | .EVM, .RETURNDATACOPY => λ evmState ↦ match evmState.stack.pop3 with | some ⟨stack', μ₀, μ₁, μ₂⟩ => do + if evmState.returnData.size < μ₁.toNat + μ₂.toNat then + .error .InvalidMemoryAccess let mState' := evmState.toMachineState.returndatacopy μ₀ μ₁ μ₂ let evmState' := {evmState with toMachineState := mState'} .ok <| evmState'.replaceStackAndIncrPC stack' @@ -323,10 +418,12 @@ def step {τ : OperationType} (op : Operation τ) (arg : Option (UInt256 × Nat) λ yulState lits ↦ match lits with | [a, b, c] => do + if yulState.toSharedState.returnData.size < b.toNat + c.toNat then + .error .InvalidMemoryAccess let mState' := yulState.toSharedState.toMachineState.returndatacopy a b c .ok <| (yulState.setMachineState mState', .none) | _ => .error .InvalidArguments - | .EVM, .EXTCODEHASH => dispatchUnaryStateOp .EVM EvmYul.State.extCodeHash + | τ, .EXTCODEHASH => dispatchUnaryStateOp τ EvmYul.State.extCodeHash | τ, .BLOCKHASH => dispatchUnaryStateOp τ (λ s v ↦ (s, EvmYul.State.blockHash s v)) | τ, .COINBASE => dispatchStateOp τ (.ofNat ∘ Fin.val ∘ EvmYul.State.coinBase) @@ -391,126 +488,17 @@ def step {τ : OperationType} (op : Operation τ) (arg : Option (UInt256 × Nat) | .Yul, .REVERT => λ yulState lits ↦ match (dispatchBinaryMachineStateOp .Yul MachineState.evmRevert) yulState lits with | .error e => .error e - | .ok (_, _) => .error (Yul.Exception.Revert) + | .ok (s, _) => .error (Yul.Exception.Revert s) | .EVM, .SELFDESTRUCT => λ evmState ↦ match evmState.stack.pop with - | some ⟨ s , μ₁ ⟩ => - let Iₐ := evmState.executionEnv.codeOwner - let r : AccountAddress := AccountAddress.ofUInt256 μ₁ - if evmState.createdAccounts.contains Iₐ then - -- When `SELFDESTRUCT` is executed in the same transaction as the contract was created - let A' : Substate := - { evmState.substate with - selfDestructSet := - evmState.substate.selfDestructSet.insert Iₐ - accessedAccounts := - evmState.substate.accessedAccounts.insert r - } - let accountMap' := - match evmState.lookupAccount Iₐ with - | none => - dbg_trace "No 'self' found to be destructed; this should probably not be happening;"; evmState.accountMap - | some σ_Iₐ => - match evmState.lookupAccount r with - | none => - if σ_Iₐ.balance == ⟨0⟩ then - evmState.accountMap - else - evmState.accountMap.insert r - {(default : Account .EVM) with balance := σ_Iₐ.balance} - |>.insert Iₐ {σ_Iₐ with balance := ⟨0⟩} - | some σ_r => - if r ≠ Iₐ then - evmState.accountMap.insert r - {σ_r with balance := σ_r.balance + σ_Iₐ.balance} - |>.insert Iₐ {σ_Iₐ with balance := ⟨0⟩} - else - -- if the target is the same as the contract calling `SELFDESTRUCT` that Ether will be burnt. - evmState.accountMap.insert r {σ_r with balance := ⟨0⟩} - |>.insert Iₐ {σ_Iₐ with balance := ⟨0⟩} - let evmState' := - {evmState with - accountMap := accountMap' - substate := A' - } - .ok <| evmState'.replaceStackAndIncrPC s - else - /- When SELFDESTRUCT is executed in a transaction that is not the - same as the contract calling SELFDESTRUCT was created: - -/ - let A' : Substate := - { evmState.substate with - accessedAccounts := - evmState.substate.accessedAccounts.insert r - } - let accountMap' := - match evmState.lookupAccount Iₐ with - | none => dbg_trace "No 'self' found to be destructed; this should probably not be happening;"; evmState.accountMap - | some σ_Iₐ => - match evmState.lookupAccount r with - | none => - if σ_Iₐ.balance == ⟨0⟩ then - evmState.accountMap - else - evmState.accountMap.insert r - {(default : Account .EVM) with balance := σ_Iₐ.balance} - |>.insert Iₐ {σ_Iₐ with balance := ⟨0⟩} - | some σ_r => - if r ≠ Iₐ then - evmState.accountMap.insert r - {σ_r with balance := σ_r.balance + σ_Iₐ.balance} - |>.insert Iₐ {σ_Iₐ with balance := ⟨0⟩} - else - -- Note that if the target is the same as the contract - -- calling SELFDESTRUCT there is no net change in balances. - -- Unlike the prior specification, Ether will not be burnt in this case. - evmState.accountMap - let evmState' := - {evmState with - accountMap := accountMap' - substate := A' - } - .ok <| evmState'.replaceStackAndIncrPC s + | some ⟨s, recipient⟩ => + .ok <| EVM.selfdestructState evmState recipient s | _ => .error .StackUnderflow | .Yul, .SELFDESTRUCT => λ yulState lits ↦ match lits with | [a] => - let Iₐ := yulState.executionEnv.codeOwner - let r : AccountAddress := AccountAddress.ofUInt256 a - let A' : Substate := - { yulState.toState.substate with - selfDestructSet := - yulState.toState.substate.selfDestructSet.insert Iₐ - accessedAccounts := - yulState.toState.substate.accessedAccounts.insert r - } - let accountMap' := - match yulState.toState.lookupAccount Iₐ with - | none => - dbg_trace "No 'self' found to be destructed; this should probably not be happening;"; yulState.toState.accountMap - | some σ_Iₐ => - match yulState.toState.lookupAccount r with - | none => - if σ_Iₐ.balance == ⟨0⟩ then - yulState.toState.accountMap - else - yulState.toState.accountMap.insert r - {(default : Account .Yul) with balance := σ_Iₐ.balance} - |>.insert Iₐ {σ_Iₐ with balance := ⟨0⟩} - | some σ_r => - if r ≠ Iₐ then - yulState.toState.accountMap.insert r - {σ_r with balance := σ_r.balance + σ_Iₐ.balance} - |>.insert Iₐ {σ_Iₐ with balance := ⟨0⟩} - else - -- if the target is the same as the contract calling `SELFDESTRUCT` that Ether will be burnt. - yulState.toState.accountMap.insert r {σ_r with balance := ⟨0⟩} - |>.insert Iₐ {σ_Iₐ with balance := ⟨0⟩} - let yulState' := - yulState.setState - { yulState.toState with accountMap := accountMap', substate := A'} - .ok <| (yulState', none) + .error (Yul.Exception.YulHalt (Yul.selfdestructState yulState a) ⟨0⟩) | _ => .error .InvalidArguments | τ, .INVALID => dispatchInvalid τ | .EVM, .Push .PUSH0 => λ evmState => @@ -571,6 +559,17 @@ def step {τ : OperationType} (op : Operation τ) (arg : Option (UInt256 × Nat) | .Yul, .POP => λ yulState _ ↦ .ok (yulState, .none) -- POP is a no-op for Yul as it discards the value only as a hint to the compiler. | .Yul, _ => λ _ _ ↦ default +theorem EVM.step_selfdestruct_of_stack + (state : EVM.State) (recipient : UInt256) (tail : Stack UInt256) + (hStack : state.stack = recipient :: tail) : + (step (τ := .EVM) .SELFDESTRUCT none) state = + .ok (EVM.selfdestructState state recipient tail) := by + cases state with + | mk shared pc stack execLength => + simp at hStack + subst stack + rfl + end Semantics end EvmYul diff --git a/EvmYul/SharedStateOps.lean b/EvmYul/SharedStateOps.lean index da616c0..630fd1e 100644 --- a/EvmYul/SharedStateOps.lean +++ b/EvmYul/SharedStateOps.lean @@ -33,12 +33,19 @@ def codeCopy (self : SharedState .EVM) (mstart cstart size : UInt256) : SharedS .ofNat (MachineState.M self.activeWords.toNat mstart.toNat size.toNat) } -def extCodeCopy' (self : SharedState .EVM) (acc mstart cstart size : UInt256) : SharedState .EVM := +def codeBytesCopy {τ} (self : SharedState τ) (mstart cstart size : UInt256) : SharedState τ := + { self with + memory := self.executionEnv.codeBytes.write cstart.toNat self.memory mstart.toNat size.toNat + activeWords := + .ofNat (MachineState.M self.activeWords.toNat mstart.toNat size.toNat) + } + +def extCodeCopy' {τ} (self : SharedState τ) (acc mstart cstart size : UInt256) : SharedState τ := let mstart := mstart.toNat let cstart := cstart.toNat let size := size.toNat let addr := AccountAddress.ofUInt256 acc - let b : ByteArray := self.toState.lookupAccount addr |>.option .empty (·.code) + let b : ByteArray := self.toState.lookupAccount addr |>.option .empty State.accountCodeImage { self with memory := b.write cstart self.memory mstart size substate := .addAccessedAccount self.toState.substate addr diff --git a/EvmYul/State/Account.lean b/EvmYul/State/Account.lean index f0cee58..0d5586b 100644 --- a/EvmYul/State/Account.lean +++ b/EvmYul/State/Account.lean @@ -8,22 +8,79 @@ import EvmYul.Yul.Ast namespace EvmYul +inductive PrecompiledContract where + | ecrec + | sha256 + | rip160 + | identity + | expmod + | bnAdd + | bnMul + | snarkv + | blake2F + | pointEval + deriving DecidableEq, Inhabited, Repr + +namespace PrecompiledContract + +def address : PrecompiledContract → AccountAddress + | .ecrec => 1 + | .sha256 => 2 + | .rip160 => 3 + | .identity => 4 + | .expmod => 5 + | .bnAdd => 6 + | .bnMul => 7 + | .snarkv => 8 + | .blake2F => 9 + | .pointEval => 10 + +def all : List PrecompiledContract := + [ .ecrec + , .sha256 + , .rip160 + , .identity + , .expmod + , .bnAdd + , .bnMul + , .snarkv + , .blake2F + , .pointEval + ] + +def ofAddress? (addr : AccountAddress) : Option PrecompiledContract := + match addr.val with + | 1 => some .ecrec + | 2 => some .sha256 + | 3 => some .rip160 + | 4 => some .identity + | 5 => some .expmod + | 6 => some .bnAdd + | 7 => some .bnMul + | 8 => some .snarkv + | 9 => some .blake2F + | 10 => some .pointEval + | _ => none + +end PrecompiledContract + /-- Precompiled contract addresses. (142) `π ≡ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}` -/ def π : Batteries.RBSet AccountAddress compare := - Batteries.RBSet.ofList ((List.range 11).tail.map (Fin.ofNat _)) compare + Batteries.RBSet.ofList (PrecompiledContract.all.map PrecompiledContract.address) compare inductive ToExecute (τ : OperationType) where | Code (code : Yul.Ast.contractCode τ) - | Precompiled (precompiled : AccountAddress) + | Precompiled (precompiled : PrecompiledContract) structure PersistentAccountState (τ : OperationType) where nonce : UInt256 balance : UInt256 storage : Storage code : (Yul.Ast.contractCode τ) + codeBytes : ByteArray := default deriving BEq, Inhabited, Repr /-- @@ -42,6 +99,8 @@ a Merkle Tree. KEVM implemets it as just an key/value map. For now, we assume no global map `GM` with which `GM[code_hash] ≡ code`. - `code` +- `codeBytes` -- source-side byte image used by Yul code-image operations + when this account is entered by a call frame. -/ structure Account (τ : OperationType) extends PersistentAccountState τ where tstorage : Storage @@ -53,4 +112,16 @@ def PersistentAccountState.codeHash (self : PersistentAccountState .EVM) : UInt2 def Account.codeHash (self : (Account .EVM)) : UInt256 := self.toPersistentAccountState.codeHash +def eip7702DelegationTargetOfCode? (code : ByteArray) : Option AccountAddress := + if code.size == 23 + && code.get? 0 == some 0xef + && code.get? 1 == some 0x01 + && code.get? 2 == some 0x00 then + some <| .ofNat <| fromByteArrayBigEndian (code.readWithoutPadding 3 20) + else + none + +def Account.eip7702DelegationTarget? (self : Account .EVM) : Option AccountAddress := + eip7702DelegationTargetOfCode? self.code + end EvmYul diff --git a/EvmYul/State/AccountOps.lean b/EvmYul/State/AccountOps.lean index 9b87964..931a5f8 100644 --- a/EvmYul/State/AccountOps.lean +++ b/EvmYul/State/AccountOps.lean @@ -32,7 +32,7 @@ EMPTY(σ, a). Section 4.1., equation 14. def emptyAccount {τ} (self : Account τ) : Bool := match τ with | .EVM => self.code.isEmpty ∧ self.nonce = ⟨0⟩ ∧ self.balance = ⟨0⟩ - | .Yul => false -- Yul statements always hold code. + | .Yul => self.code == default ∧ self.nonce = ⟨0⟩ ∧ self.balance = ⟨0⟩ def addBalance {τ} (self : Account τ) (balance : UInt256) : Option (Account τ) := let overflow : Bool := self.balance + balance < self.balance diff --git a/EvmYul/State/ExecutionEnv.lean b/EvmYul/State/ExecutionEnv.lean index 0e78ed1..2d1d691 100644 --- a/EvmYul/State/ExecutionEnv.lean +++ b/EvmYul/State/ExecutionEnv.lean @@ -1,5 +1,6 @@ import EvmYul.Wheels import EvmYul.UInt256 +import EvmYul.Protocol import EvmYul.State.BlockHeader import EvmYul.Yul.Ast @@ -30,6 +31,8 @@ structure ExecutionEnv (τ : OperationType) where depth : ℕ perm : Bool blobVersionedHashes : List ByteArray + codeBytes : ByteArray := default + protocol : Protocol := Protocol.osaka deriving BEq, Inhabited, Repr def prevRandao {τ} (e : ExecutionEnv τ) : UInt256 := @@ -45,4 +48,14 @@ def blobhash {τ} (e : ExecutionEnv τ) (i : UInt256) : UInt256 := e.blobVersionedHashes[i.toNat]?.option ⟨0⟩ (.ofNat ∘ fromByteArrayBigEndian) +def ExecutionEnv.weiValueWithProtocol {τ} + (protocol : Protocol) (e : ExecutionEnv τ) : UInt256 := + if protocol.callValueAlwaysZero then + ⟨0⟩ + else + e.weiValue + +def ExecutionEnv.selectedWeiValue {τ} (e : ExecutionEnv τ) : UInt256 := + e.weiValueWithProtocol e.protocol + end EvmYul diff --git a/EvmYul/StateOps.lean b/EvmYul/StateOps.lean index 1e81e71..7654062 100644 --- a/EvmYul/StateOps.lean +++ b/EvmYul/StateOps.lean @@ -48,6 +48,23 @@ def balance {τ} (self : State τ) (k : UInt256) : State τ × UInt256 := let addr := AccountAddress.ofUInt256 k (self.addAccessedAccount addr, self.accountMap.find? addr |>.elim ⟨0⟩ (·.balance)) +def balanceWithProtocol {τ} + (protocol : Protocol) (self : State τ) (k : UInt256) : + State τ × UInt256 := + let result := balance self k + if protocol.nativeBalanceAlwaysZero then + (result.1, ⟨0⟩) + else + result + +def selectedBalance {τ} (self : State τ) (k : UInt256) : State τ × UInt256 := + balanceWithProtocol self.executionEnv.protocol self k + +def accountCodeImage {τ} (account : Account τ) : ByteArray := + match τ with + | .EVM => account.code + | .Yul => account.codeBytes + def initialiseAccount (addr : AccountAddress) (self : State .EVM) : State .EVM := if self.accountExists addr then self else self.updateAccount addr default @@ -65,16 +82,21 @@ def selfStorage! {τ} (self : State τ) : Storage := section CodeCopy -def extCodeSize (self : State .EVM) (a : UInt256) : State .EVM × UInt256 := +def extCodeSize {τ} (self : State τ) (a : UInt256) : State τ × UInt256 := let addr := AccountAddress.ofUInt256 a - let s := self.lookupAccount addr |>.option ⟨0⟩ (.ofNat ∘ ByteArray.size ∘ (·.code)) + let s := + self.lookupAccount addr |>.option ⟨0⟩ + (.ofNat ∘ ByteArray.size ∘ accountCodeImage) (self.addAccessedAccount addr, s) -def extCodeHash (self : State .EVM) (v : UInt256) : State .EVM × UInt256 := +def extCodeHash {τ} (self : State τ) (v : UInt256) : State τ × UInt256 := let addr := AccountAddress.ofUInt256 v let newState := self.addAccessedAccount addr if dead self.accountMap addr then (newState, ⟨0⟩) else - let r := self.lookupAccount (AccountAddress.ofUInt256 v) |>.option ⟨0⟩ Account.codeHash + let r := + self.lookupAccount (AccountAddress.ofUInt256 v) |>.option ⟨0⟩ + (fun account => + .ofNat <| fromByteArrayBigEndian (ffi.KEC (accountCodeImage account))) (newState, r) end CodeCopy @@ -108,6 +130,16 @@ def chainId {τ} (_ : State τ) : UInt256 := .ofNat EvmYul.chainId def selfbalance {τ} (self : State τ) : UInt256 := Batteries.RBMap.find? self.accountMap self.executionEnv.codeOwner |>.elim ⟨0⟩ (·.balance) +def selfbalanceWithProtocol {τ} + (protocol : Protocol) (self : State τ) : UInt256 := + if protocol.nativeBalanceAlwaysZero then + ⟨0⟩ + else + selfbalance self + +def selectedSelfbalance {τ} (self : State τ) : UInt256 := + selfbalanceWithProtocol self.executionEnv.protocol self + def setCode (self : State .EVM) (code : ByteArray) : State .EVM := { self with executionEnv.code := code } diff --git a/EvmYul/UInt256.lean b/EvmYul/UInt256.lean index 6a0ac29..35a3560 100644 --- a/EvmYul/UInt256.lean +++ b/EvmYul/UInt256.lean @@ -132,7 +132,7 @@ def pow (b : UInt256) (n : UInt256) := powAux ⟨1⟩ b n.1 instance : HPow UInt256 UInt256 UInt256 := ⟨pow⟩ instance : AndOp UInt256 := ⟨UInt256.land⟩ instance : OrOp UInt256 := ⟨UInt256.lor⟩ -instance : Xor UInt256 := ⟨UInt256.xor⟩ +instance : HXor UInt256 UInt256 UInt256 := ⟨UInt256.xor⟩ instance : ShiftLeft UInt256 := ⟨UInt256.shiftLeft⟩ instance : ShiftRight UInt256 := ⟨UInt256.shiftRight⟩ @@ -254,6 +254,12 @@ def eq (a b : UInt256) := fromBool (a = b) def isZero (a : UInt256) := fromBool (eq0 a) +def clz (a : UInt256) : UInt256 := + if a == ⟨0⟩ then + ⟨256⟩ + else + .ofNat (255 - (UInt256.log2 a).toNat) + end UInt256 -- | Convert from a list of little-endian bytes to a natural number. @@ -357,6 +363,92 @@ private lemma fromBytes'_toBytes' {x : ℕ} : fromBytes' (toBytes' x) = x := by simp [UInt8.size, add_comm] apply Nat.div_add_mod +theorem fromBytesBigEndian_zeroPad_toBytesBigEndian + (value padding : Nat) : + fromBytesBigEndian + (List.replicate padding 0 ++ toBytesBigEndian value) = + value := by + unfold fromBytesBigEndian toBytesBigEndian + simp only [Function.comp_apply] + rw [List.reverse_append, List.reverse_replicate, + List.reverse_reverse] + rw [extend_bytes_zero] + exact fromBytes'_toBytes' + +private lemma fromBytes'_cons_mod (b : UInt8) (bs : List UInt8) : + fromBytes' (b :: bs) % UInt8.size = b.toFin.val := by + unfold fromBytes' + rw [show 2 ^ 8 = UInt8.size by native_decide] + rw [Nat.add_mul_mod_self_left] + exact Nat.mod_eq_of_lt b.toFin.isLt + +private lemma fromBytes'_cons_div (b : UInt8) (bs : List UInt8) : + fromBytes' (b :: bs) / UInt8.size = fromBytes' bs := by + unfold fromBytes' + rw [show 2 ^ 8 = UInt8.size by native_decide] + rw [Nat.add_mul_div_left _ _ (by decide : 0 < UInt8.size)] + rw [Nat.div_eq_of_lt b.toFin.isLt] + rw [zero_add] + cases bs <;> rfl + +private lemma fromBytes'_eq_of_length_eq + {xs ys : List UInt8} {n : ℕ} + (hxs : xs.length = n) (hys : ys.length = n) + (hEq : fromBytes' xs = fromBytes' ys) : + xs = ys := by + induction n generalizing xs ys with + | zero => + cases xs <;> cases ys <;> simp at hxs hys ⊢ + | succ n ih => + cases xs with + | nil => simp at hxs + | cons x xs => + cases ys with + | nil => simp at hys + | cons y ys => + simp at hxs hys + have hHeadNat : x.toFin.val = y.toFin.val := by + have hMod := congrArg (fun value => value % UInt8.size) hEq + simpa [fromBytes'_cons_mod] using hMod + have hHead : x = y := + UInt8.eq_of_toFin_eq (Fin.ext hHeadNat) + subst y + have hTailEq : fromBytes' xs = fromBytes' ys := by + have hDiv := congrArg (fun value => value / UInt8.size) hEq + simpa [fromBytes'_cons_div] using hDiv + have hTail : xs = ys := ih hxs hys hTailEq + subst ys + rfl + +theorem zeroPadBytes_toBytes'_fromBytes'_eq_of_length_eq_32 + {bs : List UInt8} (hLen : bs.length = 32) : + zeroPadBytes 32 (toBytes' (fromBytes' bs)) = bs := by + apply fromBytes'_eq_of_length_eq + · exact zeroPadBytes_len (toBytes'_UInt256_le (by + unfold UInt256.size + exact fromBytes'_UInt256_le hLen)) + · exact hLen + · rw [fromBytes'_zeroPadBytes_32_eq] + exact fromBytes'_toBytes' + +theorem toBytesBigEndian_length_le_of_UInt256 (value : UInt256) : + (toBytesBigEndian value.toNat).length ≤ 32 := by + unfold toBytesBigEndian + simp [UInt256.toNat] + exact toBytes'_UInt256_le value.val.isLt + +theorem zeroPadBytes_toBytesBigEndian_fromBytesBigEndian_eq_of_length_eq_32 + {bs : List UInt8} (hLen : bs.length = 32) : + List.replicate (32 - (toBytesBigEndian (fromBytesBigEndian bs)).length) + 0 ++ toBytesBigEndian (fromBytesBigEndian bs) = + bs := by + have hLittle := + zeroPadBytes_toBytes'_fromBytes'_eq_of_length_eq_32 + (bs := bs.reverse) (by simpa [List.length_reverse] using hLen) + have hRev := congrArg List.reverse hLittle + simpa [toBytesBigEndian, fromBytesBigEndian, zeroPadBytes, + List.reverse_append, List.length_reverse] using hRev + def fromBytes! (bs : List UInt8) : ℕ := fromBytes' (bs.take 32) private lemma fromBytes_was_good_all_year_long @@ -369,6 +461,50 @@ private lemma fromBytes_was_good_all_year_long @[simp] lemma fromBytes_wasnt_naughty : fromBytes! bs < 2^256 := fromBytes_was_good_all_year_long (by simp) +theorem fromBytesBigEndian_lt_UInt256_size_of_length_le_32 + {bs : List UInt8} (hLen : bs.length ≤ 32) : + fromBytesBigEndian bs < UInt256.size := by + unfold fromBytesBigEndian UInt256.size + exact fromBytes_was_good_all_year_long + (bs := bs.reverse) (by simpa [List.length_reverse] using hLen) + +theorem UInt256.toNat_ofNat_of_lt {n : Nat} (hLt : n < UInt256.size) : + (UInt256.ofNat n).toNat = n := by + unfold UInt256.ofNat UInt256.toNat + change (Fin.ofNat UInt256.size n).val = n + rw [Fin.val_ofNat] + exact Nat.mod_eq_of_lt hLt + +@[simp] theorem UInt256.ofNat_toNat (value : UInt256) : + UInt256.ofNat value.toNat = value := by + cases value with + | mk val => + unfold UInt256.ofNat UInt256.toNat + simp + rfl + +theorem UInt256.toNat_ofNat_fromBytesBigEndian_of_length_le_32 + {bs : List UInt8} (hLen : bs.length ≤ 32) : + (UInt256.ofNat (fromBytesBigEndian bs)).toNat = + fromBytesBigEndian bs := + UInt256.toNat_ofNat_of_lt + (fromBytesBigEndian_lt_UInt256_size_of_length_le_32 hLen) + +theorem zeroPadBytes_toBytesBigEndian_ofNat_fromBytesBigEndian_eq_of_length_eq_32 + {bs : List UInt8} (hLen : bs.length = 32) : + List.replicate + (32 - + (toBytesBigEndian + (UInt256.ofNat (fromBytesBigEndian bs)).toNat).length) + 0 ++ + toBytesBigEndian + (UInt256.ofNat (fromBytesBigEndian bs)).toNat = + bs := by + rw [UInt256.toNat_ofNat_fromBytesBigEndian_of_length_le_32 + (by omega : bs.length ≤ 32)] + exact zeroPadBytes_toBytesBigEndian_fromBytesBigEndian_eq_of_length_eq_32 + hLen + -- Convenience function for spooning into UInt256. -- Given that I 'accept' UInt8, might as well live with UInt256. def fromBytes_if_you_really_must? (bs : List UInt8) : UInt256 := diff --git a/EvmYul/Wheels.lean b/EvmYul/Wheels.lean index e2abbc6..4ab039f 100644 --- a/EvmYul/Wheels.lean +++ b/EvmYul/Wheels.lean @@ -13,6 +13,50 @@ def UInt256.toByteArray (val : UInt256) : ByteArray := let b := BE val.toNat ffi.ByteArray.zeroes ⟨32 - b.size⟩ ++ b +@[simp] theorem UInt256.size_toByteArray (value : UInt256) : + value.toByteArray.size = 32 := by + have hLen := + toBytesBigEndian_length_le_of_UInt256 value + unfold UInt256.toByteArray BE + simp only [Function.comp_apply, ByteArray.size_append, + ffi.ByteArray.size_zeroes, List.size_toByteArray] + change + ((32 : BitVec System.Platform.numBits) - + (toBytesBigEndian value.toNat).length).toNat + + (toBytesBigEndian value.toNat).length = + 32 + have hPow : 32 < 2 ^ System.Platform.numBits := by + rcases System.Platform.numBits_eq with h | h <;> simp [h] + have h32 : + (32 : BitVec System.Platform.numBits).toNat = 32 := by + rcases System.Platform.numBits_eq with h | h <;> simp [h] + have hValue : + ((toBytesBigEndian value.toNat).length : + BitVec System.Platform.numBits).toNat = + (toBytesBigEndian value.toNat).length := by + rcases System.Platform.numBits_eq with h | h + · rw [h, BitVec.natCast_eq_ofNat, BitVec.toNat_ofNat, + Nat.mod_eq_of_lt] + omega + · rw [h, BitVec.natCast_eq_ofNat, BitVec.toNat_ofNat, + Nat.mod_eq_of_lt] + omega + rw [BitVec.toNat_sub_of_le] + · rw [h32, hValue] + omega + · rw [BitVec.le_def, h32, hValue] + exact hLen + +@[simp] theorem UInt256.fromByteArrayBigEndian_toByteArray + (value : UInt256) : + EvmYul.fromByteArrayBigEndian value.toByteArray = value.toNat := by + unfold UInt256.toByteArray EvmYul.fromByteArrayBigEndian BE + rw [ffi.ByteArray.toList_eq_data_toList] + simp [ffi.ByteArray.zeroes] + exact + EvmYul.fromBytesBigEndian_zeroPad_toBytesBigEndian + value.toNat _ + abbrev Literal := UInt256 -- 2^160 https://www.wolframalpha.com/input?i=2%5E160 @@ -145,7 +189,7 @@ def ByteArray.extract' (a : ByteArray) (b e : Nat) : ByteArray := def HexPrefix := "0x" -def TargetSchedule := "Cancun" +def TargetSchedule := "Osaka" def isHexDigitChar (c : Char) : Bool := '0' <= c && c <= '9' || 'a' <= c.toLower && c.toLower <= 'f' @@ -176,7 +220,7 @@ def getBlob? (s : String) : Except String Blob := let rest := s.drop HexPrefix.length if rest.any (not ∘ isHexDigitChar) then .error "Blobs must consist of valid hex digits." - else .ok rest.toLower + else .ok rest.toString.toLower else .error "Input does not begin with 0x." where isHex (s : String) := s.startsWith HexPrefix diff --git a/EvmYul/Yul/Ast.lean b/EvmYul/Yul/Ast.lean index 7f7c513..99be310 100644 --- a/EvmYul/Yul/Ast.lean +++ b/EvmYul/Yul/Ast.lean @@ -47,6 +47,7 @@ mutual inductive Stmt where | Block : List Stmt → Stmt | Let : List Identifier → Option Expr → Stmt + | Assign : List Identifier → Expr → Stmt | ExprStmtCall : Expr → Stmt | Switch : Expr → List (Literal × List Stmt) → List Stmt → Stmt | For : Expr → List Stmt → List Stmt → Stmt diff --git a/EvmYul/Yul/Exception.lean b/EvmYul/Yul/Exception.lean index fdd9b4b..8d1c8f9 100644 --- a/EvmYul/Yul/Exception.lean +++ b/EvmYul/Yul/Exception.lean @@ -10,11 +10,14 @@ inductive Exception where | InvalidInstruction : Exception | OutOfFuel : Exception | StaticModeViolation : Exception + | InvalidMemoryAccess : Exception | MissingContract (s : String) : Exception | MissingContractFunction (s : String) : Exception | InvalidExpression : Exception + | UnknownIdentifier (s : String) : Exception + | DuplicateDeclaration (s : String) : Exception | YulEXTCODESIZENotImplemented : Exception - | Revert : Exception + | Revert (state : Yul.State) : Exception | YulHalt (state : Yul.State) (value : UInt256) : Exception -- | StopInvoked : Exception @@ -26,11 +29,14 @@ instance : Repr Exception where | .InvalidInstruction => "InvalidInstruction" | .OutOfFuel => "OutOfFuel" | .StaticModeViolation => "StaticModeViolation" + | .InvalidMemoryAccess => "InvalidMemoryAccess" | .MissingContract s => "MissingContract: " ++ s | .MissingContractFunction f => "MissingContractFunction: " ++ f | .InvalidExpression => "InvalidExpression" + | .UnknownIdentifier s => "UnknownIdentifier: " ++ s + | .DuplicateDeclaration s => "DuplicateDeclaration: " ++ s | .YulEXTCODESIZENotImplemented => "YulEXTCODESIZENotImplemented" - | .Revert => "Revert" + | .Revert _ => "Revert" | .YulHalt _ _ => "YulHalt: (holds a state and a value)" diff --git a/EvmYul/Yul/Interpreter.lean b/EvmYul/Yul/Interpreter.lean index 3e8adfc..6a1f4d9 100644 --- a/EvmYul/Yul/Interpreter.lean +++ b/EvmYul/Yul/Interpreter.lean @@ -7,6 +7,7 @@ import EvmYul.Yul.StateOps import EvmYul.Yul.SizeLemmas import EvmYul.Yul.Exception +import EvmYul.EVM.Gas import EvmYul.Semantics set_option maxHeartbeats 400000 -- Needs more than 200000 @@ -37,6 +38,82 @@ def multifill' (vars : List Identifier) : Except Yul.Exception (State × List Li | .ok (s, rets) => .ok (s.multifill vars rets) | .error e => .error e +def firstDuplicate? : List Identifier → Option Identifier + | [] => .none + | var :: vars => + if vars.contains var then .some var else firstDuplicate? vars + +def firstDeclared? (s : State) (vars : List Identifier) : Option Identifier := + vars.find? (fun var => (s.lookup? var).isSome) + +def firstUndeclared? (s : State) (vars : List Identifier) : Option Identifier := + vars.find? (fun var => (s.lookup? var).isNone) + +def checkDeclaration (s : State) (vars : List Identifier) : Except Yul.Exception Unit := + match firstDuplicate? vars with + | .some var => .error (.DuplicateDeclaration var) + | .none => + match firstDeclared? s vars with + | .some var => .error (.DuplicateDeclaration var) + | .none => .ok () + +def checkAssignment (s : State) (vars : List Identifier) : Except Yul.Exception Unit := + match firstDuplicate? vars with + | .some _ => .error .InvalidArguments + | .none => + match firstUndeclared? s vars with + | .some var => .error (.UnknownIdentifier var) + | .none => .ok () + +def restoreRevertedContractCallState (s₀ s₂ : State) + (inOffset inSize outOffset outSize : Literal) : + Except Yul.Exception (State × List Literal) := + match s₀ with + | .OutOfFuel => .error .OutOfFuel + | .Checkpoint j => .ok (.Checkpoint j, [⟨0⟩]) + | .Ok sharedState₀ varstore => + let returnData := s₂.toMachineState.H_return + let machineState₃ := + s₀.toMachineState.finishExternalCall returnData + inOffset inSize outOffset outSize + let sharedState₃ := + { sharedState₀ with + toMachineState := machineState₃ + } + .ok (.Ok sharedState₃ varstore, [⟨0⟩]) + +def restoreSuccessfulContractCallState (s₀ s₂ : State) + (varstore : VarStore) (returnData : ByteArray) + (inOffset inSize outOffset outSize : Literal) : + Except Yul.Exception (State × List Literal) := + match s₀ with + | .OutOfFuel => .error .OutOfFuel + | .Checkpoint j => .ok (.Checkpoint j, [⟨0⟩]) + | .Ok sharedState₀ _ => + match s₂ with + | .OutOfFuel => .error .OutOfFuel + | .Checkpoint j => .ok (.Checkpoint j, [⟨0⟩]) + | .Ok sharedState₂ _ => + let machineState₃ := + s₀.toMachineState.finishExternalCall returnData + inOffset inSize outOffset outSize + let sharedState₃ := + { sharedState₀ with + toMachineState := machineState₃ + accountMap := + if sharedState₂.accountMap.isEmpty then + sharedState₀.accountMap + else + sharedState₂.accountMap + substate := + if sharedState₂.accountMap.isEmpty then + sharedState₀.substate + else + sharedState₂.substate + createdAccounts := sharedState₂.createdAccounts + } + .ok (.Ok sharedState₃ varstore, [⟨1⟩]) + def setStatic (s : State) (p : Bool) : State := match s with | .OutOfFuel => .OutOfFuel @@ -50,16 +127,114 @@ def setStatic (s : State) (p : Bool) : State := } .Ok sharedState' varstore -def buildContractCallEmptyReturnState (s₀ : State) (accountMap₁ : Option (AccountMap .Yul)) (v : Literal) : Except Yul.Exception (State × List Literal) := +def addAccessedAccount (s : State) (addr : AccountAddress) : State := + s.setState (EvmYul.State.addAccessedAccount s.toState addr) + +def callRecipientCredit {τ : OperationType} + (accountMap : AccountMap τ) + (recipient : AccountAddress) + (value : UInt256) : AccountMap τ := + match accountMap.find? recipient with + | none => + if value != ⟨0⟩ then + accountMap.insert recipient + { (default : Account τ) with balance := value } + else + accountMap + | some account => + accountMap.insert recipient + { account with balance := account.balance + value } + +def callSourceDebit {τ : OperationType} + (accountMap : AccountMap τ) + (source : AccountAddress) + (value : UInt256) : AccountMap τ := + match accountMap.find? source with + | none => accountMap + | some account => + accountMap.insert source + { account with balance := account.balance - value } + +def callTransferUnchecked {τ : OperationType} + (accountMap : AccountMap τ) + (source recipient : AccountAddress) + (value : UInt256) : AccountMap τ := + callSourceDebit (callRecipientCredit accountMap recipient value) source value + +def callTransferAccountMap? (accountMap : AccountMap .Yul) + (source recipient : AccountAddress) (value : UInt256) : + Option (AccountMap .Yul) := + if value ≤ (accountMap.find? source |>.option ⟨0⟩ (·.balance)) then + some (callTransferUnchecked accountMap source recipient value) + else + none + +def buildContractCallEmptyReturnState (s₀ : State) + (accountMap₁ : Option (AccountMap .Yul)) + (inOffset inSize outOffset outSize v : Literal) : + Except Yul.Exception (State × List Literal) := match s₀ with | .OutOfFuel => .error .OutOfFuel | .Checkpoint j => .ok (.Checkpoint j, [⟨0⟩]) | .Ok sharedState₀ varstore => - let sharedState₁ := {sharedState₀ with H_return := ByteArray.empty, - returnData := ByteArray.empty, - accountMap := accountMap₁.getD s₀.toSharedState.accountMap } + let machineState₁ := + s₀.toMachineState.finishExternalCall ByteArray.empty + inOffset inSize outOffset outSize + let sharedState₁ := + { sharedState₀ with + toMachineState := machineState₁ + accountMap := accountMap₁.getD s₀.toSharedState.accountMap + } .ok (.Ok sharedState₁ varstore, [v]) +def buildContractCallReturnState (s₀ : State) (accountMap₂ : AccountMap .Yul) + (substate₂ : Substate) (returnData : ByteArray) + (inOffset inSize outOffset outSize v : Literal) : + Except Yul.Exception (State × List Literal) := + match s₀ with + | .OutOfFuel => .error .OutOfFuel + | .Checkpoint j => .ok (.Checkpoint j, [⟨0⟩]) + | .Ok sharedState₀ varstore => + let machineState₃ := + s₀.toMachineState.finishExternalCall returnData + inOffset inSize outOffset outSize + let sharedState₃ := + { sharedState₀ with + toMachineState := machineState₃ + accountMap := accountMap₂ + substate := substate₂ + } + .ok (.Ok sharedState₃ varstore, [v]) + +def buildPrecompiledContractCallState (s₀ : State) (accountMap₁ : AccountMap .Yul) + (precompiled : PrecompiledContract) (gas : Literal) + (executionEnv : ExecutionEnv .Yul) + (inOffset inSize outOffset outSize : Literal) : + Except Yul.Exception (State × List Literal) := + let (z, accountMap₂, _, substate₂, returnData) := + runPrecompiledContract precompiled accountMap₁ gas s₀.toState.substate executionEnv + if z then + let accountMap₃ := + if accountMap₂.isEmpty then s₀.toSharedState.accountMap else accountMap₂ + buildContractCallReturnState s₀ accountMap₃ substate₂ returnData + inOffset inSize outOffset outSize ⟨1⟩ + else + buildContractCallEmptyReturnState s₀ .none + inOffset inSize outOffset outSize ⟨0⟩ + +/-- + `selectSwitchCase` returns the first switch case body whose literal matches + the evaluated switch condition, or the default body if no case matches. + + This matches Solidity/Yul switch control flow: non-selected case and default + bodies are not executed. +-/ +def selectSwitchCase (cond : Literal) (defaultBody : List Stmt) : + List (Literal × List Stmt) → List Stmt + | [] => defaultBody + | ((val, stmts) :: cases') => + if val = cond then stmts else selectSwitchCase cond defaultBody cases' + mutual def primCall (fuel : ℕ) (s₀ : State) (prim : Operation .Yul) (args : List Literal) : Except Yul.Exception (State × List Literal) := @@ -71,124 +246,134 @@ def primCall (fuel : ℕ) (s₀ : State) (prim : Operation .Yul) (args : List Li match prim with | .CALL => match args with - | _ :: address_arg :: value :: inOffset :: inSize :: outOffset :: outSize :: _ => + | gas :: address_arg :: value :: inOffset :: inSize :: outOffset :: outSize :: _ => if ¬s₀.executionEnv.perm ∧ value ≠ ⟨0⟩ then throw .StaticModeViolation let address := AccountAddress.ofUInt256 address_arg + let s₀Accessed := addAccessedAccount s₀ address let calldata₁ := s₀.toMachineState.memory.readWithPadding inOffset.toNat inSize.toNat - let accountMap₁Opt := (s₀.sharedState.accountMap.transferBalance .Yul s₀.executionEnv.codeOwner address value) + let callGas : UInt256 := + .ofNat <| + EvmYul.EVM.CcallgasWithProtocolFor + s₀.executionEnv.protocol s₀.toSharedState + address address value gas + s₀.sharedState.accountMap s₀.toMachineState s₀.toState.substate + let accountMap₁Opt := + callTransferAccountMap? + s₀.sharedState.accountMap + s₀.executionEnv.codeOwner + address + value match accountMap₁Opt with | .none => - buildContractCallEmptyReturnState s₀ .none ⟨0⟩ -- Insufficient funds: return 0 to indicate error, with empty return data + buildContractCallEmptyReturnState s₀Accessed .none + inOffset inSize outOffset outSize ⟨0⟩ -- Insufficient funds: return 0 to indicate error, with empty return data | .some accountMap₁ => if s₀.executionEnv.depth ≥ 1024 then - buildContractCallEmptyReturnState s₀ .none ⟨0⟩ -- Reached depth limit: return 0 to indicate error, with empty return data + buildContractCallEmptyReturnState s₀Accessed .none + inOffset inSize outOffset outSize ⟨0⟩ -- Reached depth limit: return 0 to indicate error, with empty return data else - match s₀ with + match s₀Accessed with | .OutOfFuel => .error .OutOfFuel | .Checkpoint j => .ok (.Checkpoint j, [⟨0⟩]) | .Ok sharedState varstore => - match s₀.sharedState.accountMap.find? address with - | .none => - buildContractCallEmptyReturnState s₀ accountMap₁ ⟨1⟩ -- No contract at the provided address, return 1 to indicate success, with empty return data. (Like STOP opcode). - | .some yulContract => + match EvmYul.toExecute .Yul s₀.sharedState.accountMap address with + | .Precompiled precompiled => let executionEnv₁ := { sharedState.executionEnv with calldata := calldata₁, - code := yulContract.code, + code := default, + codeBytes := default, codeOwner := address, source := s₀.executionEnv.codeOwner, weiValue := value depth := s₀.executionEnv.depth + 1 - } + } + buildPrecompiledContractCallState + s₀Accessed accountMap₁ precompiled callGas + executionEnv₁ inOffset inSize outOffset outSize + | .Code _ => + match s₀.sharedState.accountMap.find? address with + | .none => + buildContractCallEmptyReturnState s₀Accessed accountMap₁ + inOffset inSize outOffset outSize ⟨1⟩ -- No contract at the provided address, return 1 to indicate success, with empty return data. (Like STOP opcode). + | .some yulContract => + let executionEnv₁ := { sharedState.executionEnv with + calldata := calldata₁, + code := yulContract.code, + codeBytes := yulContract.codeBytes, + codeOwner := address, + source := s₀.executionEnv.codeOwner, + weiValue := value + depth := s₀.executionEnv.depth + 1 + } let sharedState₁ := { sharedState with executionEnv := executionEnv₁, - memory := ByteArray.mk #[], + toMachineState := + EvmYul.MachineState.freshExternalCall callGas, accountMap := accountMap₁ } let s₁ : State := .Ok sharedState₁ default - match callDispatcher fuel₁ .none s₁ with + match callDispatcher fuel₁ (some yulContract.code) s₁ with | .error (.YulHalt s₂ _) => - let memory₃ := s₂.toMachineState.H_return.copySlice 0 s₀.toMachineState.memory outOffset.toNat (min outSize.toNat s₂.toMachineState.H_return.size) - match s₂ with - | .OutOfFuel => .error .OutOfFuel - | .Checkpoint j => .ok (.Checkpoint j, [⟨0⟩]) - | .Ok sharedState₂ _ => - - -- Restore ExecutionEnv - let executionEnv₃ := { sharedState₂.executionEnv with - calldata := default, - code := s₀.executionEnv.code, - codeOwner := s₀.executionEnv.codeOwner, - source := s₀.executionEnv.source, - weiValue := s₀.executionEnv.weiValue, - } - let sharedState₃ := { sharedState₂ with - memory := memory₃, - returnData := s₂.toMachineState.H_return, - executionEnv := executionEnv₃, - H_return := ByteArray.empty - } - .ok (.Ok sharedState₃ varstore, [⟨1⟩]) + restoreSuccessfulContractCallState s₀Accessed s₂ varstore + s₂.toMachineState.H_return + inOffset inSize outOffset outSize + | .error (.Revert s₂) => + restoreRevertedContractCallState s₀Accessed s₂ + inOffset inSize outOffset outSize | .error e => .error e | .ok (s₂, _) => - - /- We note here that if: - `outOffset.toNat + (min outSize.toNat s₂.toMachineState.H_return.size) ≥ UInt256.size` - then we are writing beyond the theoretical memory size limit. - The yellow paper is unclear on the semantics of this (at the time of writing). - We follow the https://github.com/NethermindEth/nethermind execution client (for example). - And we expand the memory beyond the theoretical 2^256 bit max size if needed. - In practice, this is essentially impossible to occur due to the - prohibitively large gas cost of allocating this much memory. - - Similarly in other places in `primCall` where `memory₃` is constructed in this way. - -/ - let memory₃ := s₂.toMachineState.H_return.copySlice 0 s₀.toMachineState.memory outOffset.toNat (min outSize.toNat s₂.toMachineState.H_return.size) - match s₂ with - | .OutOfFuel => .error .OutOfFuel - | .Checkpoint j => .ok (.Checkpoint j, [⟨0⟩]) - | .Ok sharedState₂ _ => - - -- Restore ExecutionEnv - let executionEnv₃ := { sharedState₂.executionEnv with - calldata := default, - code := s₀.executionEnv.code, - codeOwner := s₀.executionEnv.codeOwner, - source := s₀.executionEnv.source, - weiValue := s₀.executionEnv.weiValue, - } - let sharedState₃ := { sharedState₂ with - memory := memory₃, - returnData := s₂.toMachineState.H_return, - H_return := ByteArray.empty, - executionEnv := executionEnv₃ - } - .ok (.Ok sharedState₃ varstore, [⟨1⟩]) + restoreSuccessfulContractCallState s₀Accessed s₂ varstore + ByteArray.empty inOffset inSize outOffset outSize | _ => .error .InvalidArguments -- Incorrect number of arguments, this case should be impossible if the Yul code is parsed correctly. Guaranteed by the compiler. | .STATICCALL => match args with - | _ :: address_arg :: inOffset :: inSize :: outOffset :: outSize :: _ => - if ¬s₀.executionEnv.perm then throw .StaticModeViolation - let s₀Static : State := setStatic s₀ false + | gas :: address_arg :: inOffset :: inSize :: outOffset :: outSize :: _ => let address := AccountAddress.ofUInt256 address_arg - let calldata₁ := s₀Static.toMachineState.memory.readWithPadding inOffset.toNat inSize.toNat + let s₀Accessed := addAccessedAccount s₀ address + let s₀Static : State := setStatic s₀Accessed false + let calldata₁ := s₀.toMachineState.memory.readWithPadding inOffset.toNat inSize.toNat + let callGas : UInt256 := + .ofNat <| + EvmYul.EVM.CcallgasWithProtocolFor + s₀.executionEnv.protocol s₀.toSharedState + address address ⟨0⟩ gas + s₀.sharedState.accountMap s₀.toMachineState s₀.toState.substate - if s₀Static.toSharedState.executionEnv.depth ≥ 1024 + if s₀.executionEnv.depth ≥ 1024 then - buildContractCallEmptyReturnState s₀Static .none ⟨0⟩ -- Reached depth limit: return 0 to indicate error, with empty return data + buildContractCallEmptyReturnState s₀Accessed .none + inOffset inSize outOffset outSize ⟨0⟩ -- Reached depth limit: return 0 to indicate error, with empty return data else match s₀Static with | .OutOfFuel => .error .OutOfFuel | .Checkpoint j => .ok (.Checkpoint j, [⟨0⟩]) | .Ok sharedState varstore => - match s₀Static.sharedState.accountMap.find? address with + match EvmYul.toExecute .Yul s₀.sharedState.accountMap address with + | .Precompiled precompiled => + let executionEnv₁ := { s₀Static.executionEnv with + calldata := calldata₁, + code := default, + codeBytes := default, + codeOwner := address, + source := s₀Static.executionEnv.codeOwner, + weiValue := ⟨0⟩ + depth := s₀Static.toSharedState.executionEnv.depth + 1 + } + buildPrecompiledContractCallState + s₀Accessed s₀.sharedState.accountMap precompiled callGas + executionEnv₁ inOffset inSize outOffset outSize + | .Code _ => + match s₀.sharedState.accountMap.find? address with | .none => - buildContractCallEmptyReturnState s₀Static .none ⟨1⟩ -- No contract at the provided address, return 1 to indicate success, with empty return data. (Like STOP opcode). + buildContractCallEmptyReturnState s₀Accessed .none + inOffset inSize outOffset outSize ⟨1⟩ -- No contract at the provided address, return 1 to indicate success, with empty return data. (Like STOP opcode). | .some yulContract => let executionEnv₁ := { s₀Static.executionEnv with calldata := calldata₁, code := yulContract.code, + codeBytes := yulContract.codeBytes, codeOwner := address, source := s₀Static.executionEnv.codeOwner, weiValue := ⟨0⟩ @@ -196,82 +381,80 @@ def primCall (fuel : ℕ) (s₀ : State) (prim : Operation .Yul) (args : List Li } let sharedState₁ := { sharedState with executionEnv := executionEnv₁, - memory := ByteArray.mk #[], + toMachineState := + EvmYul.MachineState.freshExternalCall callGas, } let s₁ : State := .Ok sharedState₁ default - match callDispatcher fuel₁ .none s₁ with + match callDispatcher fuel₁ (some yulContract.code) s₁ with | .error (.YulHalt s₂ _) => - let memory₃ := s₂.toMachineState.H_return.copySlice 0 s₀Static.toMachineState.memory outOffset.toNat (min outSize.toNat s₂.toMachineState.H_return.size) - match s₂ with - | .OutOfFuel => .error .OutOfFuel - | .Checkpoint j => .ok (.Checkpoint j, [⟨0⟩]) - | .Ok sharedState₂ _ => - -- Restore ExecutionEnv - let executionEnv₃ := { sharedState₂.executionEnv with - calldata := default, - code := s₀.executionEnv.code, - codeOwner := s₀.executionEnv.codeOwner, - source := s₀.executionEnv.source, - weiValue := s₀.executionEnv.weiValue, - } - let sharedState₃ := { sharedState₂ with - memory := memory₃, - returnData := s₂.toMachineState.H_return, - H_return := ByteArray.empty, - executionEnv := executionEnv₃ - } - .ok (setStatic (.Ok sharedState₃ varstore) s₀.executionEnv.perm, [⟨1⟩]) + restoreSuccessfulContractCallState s₀Accessed s₂ varstore + s₂.toMachineState.H_return + inOffset inSize outOffset outSize + | .error (.Revert s₂) => + restoreRevertedContractCallState s₀Accessed s₂ + inOffset inSize outOffset outSize | .error e => .error e | .ok (s₂, _) => - - let memory₃ := s₂.toMachineState.H_return.copySlice 0 s₀Static.toMachineState.memory outOffset.toNat (min outSize.toNat s₂.toMachineState.H_return.size) - match s₂ with - | .OutOfFuel => .error .OutOfFuel - | .Checkpoint j => .ok (.Checkpoint j, [⟨0⟩]) - | .Ok sharedState₂ _ => - -- Restore ExecutionEnv - let executionEnv₃ := { sharedState₂.executionEnv with - calldata := default, - code := s₀.executionEnv.code, - codeOwner := s₀.executionEnv.codeOwner, - source := s₀.executionEnv.source, - weiValue := s₀.executionEnv.weiValue, - } - let sharedState₃ := { sharedState₂ with - memory := memory₃, - returnData := s₂.toMachineState.H_return, - H_return := ByteArray.empty, - executionEnv := executionEnv₃ - } - .ok (setStatic (.Ok sharedState₃ varstore) s₀.executionEnv.perm, [⟨1⟩]) + restoreSuccessfulContractCallState s₀Accessed s₂ varstore + ByteArray.empty inOffset inSize outOffset outSize | _ => .error .InvalidArguments -- Incorrect number of arguments, this case should be impossible if the Yul code is parsed correctly. Guaranteed by the compiler. | .CALLCODE => match args with - | _ :: address_arg :: value :: inOffset :: inSize :: outOffset :: outSize :: _ => - if ¬s₀.executionEnv.perm ∧ value ≠ ⟨0⟩ then throw .StaticModeViolation + | gas :: address_arg :: value :: inOffset :: inSize :: outOffset :: outSize :: _ => let address := AccountAddress.ofUInt256 address_arg + let s₀Accessed := addAccessedAccount s₀ address let calldata₁ := s₀.toMachineState.memory.readWithPadding inOffset.toNat inSize.toNat - let accountMap₁Opt := (s₀.sharedState.accountMap.transferBalance .Yul s₀.executionEnv.codeOwner s₀.executionEnv.codeOwner value) + let callGas : UInt256 := + .ofNat <| + EvmYul.EVM.CcallgasWithProtocolFor + s₀.executionEnv.protocol s₀.toSharedState + address s₀.executionEnv.codeOwner value gas + s₀.sharedState.accountMap s₀.toMachineState s₀.toState.substate + let accountMap₁Opt := + callTransferAccountMap? + s₀.sharedState.accountMap + s₀.executionEnv.codeOwner + s₀.executionEnv.codeOwner + value match accountMap₁Opt with | .none => - buildContractCallEmptyReturnState s₀ .none ⟨0⟩ -- Insufficient funds: return 0 to indicate error, with empty return data + buildContractCallEmptyReturnState s₀Accessed .none + inOffset inSize outOffset outSize ⟨0⟩ -- Insufficient funds: return 0 to indicate error, with empty return data | .some accountMap₁ => if s₀.executionEnv.depth ≥ 1024 then - buildContractCallEmptyReturnState s₀ accountMap₁ ⟨0⟩ -- Reached depth limit: return 0 to indicate error, with empty return data + buildContractCallEmptyReturnState s₀Accessed .none + inOffset inSize outOffset outSize ⟨0⟩ -- Reached depth limit: return 0 to indicate error, with empty return data else - match s₀ with + match s₀Accessed with | .OutOfFuel => .error .OutOfFuel | .Checkpoint j => .ok (.Checkpoint j, [⟨0⟩]) | .Ok sharedState varstore => - match s₀.sharedState.accountMap.find? address with + match EvmYul.toExecute .Yul s₀.sharedState.accountMap address with + | .Precompiled precompiled => + let executionEnv₁ := { sharedState.executionEnv with + calldata := calldata₁, + code := default, + codeBytes := default, + codeOwner := s₀.executionEnv.codeOwner, + source := s₀.executionEnv.codeOwner, + weiValue := value + depth := s₀.executionEnv.depth + 1 + } + buildPrecompiledContractCallState + s₀Accessed accountMap₁ precompiled callGas + executionEnv₁ inOffset inSize outOffset outSize + | .Code _ => + match s₀.sharedState.accountMap.find? address with | .none => - buildContractCallEmptyReturnState s₀ accountMap₁ ⟨1⟩ -- No contract at the provided address, return 1 to indicate success, with empty return data. (Like STOP opcode). + buildContractCallEmptyReturnState s₀Accessed accountMap₁ + inOffset inSize outOffset outSize ⟨1⟩ -- No contract at the provided address, return 1 to indicate success, with empty return data. (Like STOP opcode). | .some yulContract => let executionEnv₁ := { sharedState.executionEnv with calldata := calldata₁, code := yulContract.code, + codeBytes := yulContract.codeBytes, codeOwner := s₀.executionEnv.codeOwner, source := s₀.executionEnv.codeOwner, weiValue := value @@ -279,130 +462,91 @@ def primCall (fuel : ℕ) (s₀ : State) (prim : Operation .Yul) (args : List Li } let sharedState₁ := { sharedState with executionEnv := executionEnv₁, - memory := ByteArray.mk #[], + toMachineState := + EvmYul.MachineState.freshExternalCall callGas, accountMap := accountMap₁ } let s₁ : State := .Ok sharedState₁ default - match callDispatcher fuel₁ yulContract.code s₁ with + match callDispatcher fuel₁ (some yulContract.code) s₁ with | .error (.YulHalt s₂ _) => - let memory₃ := s₂.toMachineState.H_return.copySlice 0 s₀.toMachineState.memory outOffset.toNat (min outSize.toNat s₂.toMachineState.H_return.size) - match s₂ with - | .OutOfFuel => .error .OutOfFuel - | .Checkpoint j => .ok (.Checkpoint j, [⟨0⟩]) - | .Ok sharedState₂ _ => - -- Restore ExecutionEnv - let executionEnv₃ := { sharedState₂.executionEnv with - calldata := default, - code := s₀.executionEnv.code, - codeOwner := s₀.executionEnv.codeOwner, - source := s₀.executionEnv.source, - weiValue := s₀.executionEnv.weiValue, - } - let sharedState₃ := { sharedState₂ with - memory := memory₃, - returnData := s₂.toMachineState.H_return, - H_return := ByteArray.empty, - executionEnv := executionEnv₃ - } - .ok (.Ok sharedState₃ varstore, [⟨1⟩]) + restoreSuccessfulContractCallState s₀Accessed s₂ varstore + s₂.toMachineState.H_return + inOffset inSize outOffset outSize + | .error (.Revert s₂) => + restoreRevertedContractCallState s₀Accessed s₂ + inOffset inSize outOffset outSize | .error e => .error e | .ok (s₂, _) => - let memory₃ := s₂.toMachineState.H_return.copySlice 0 s₀.toMachineState.memory outOffset.toNat (min outSize.toNat s₂.toMachineState.H_return.size) - match s₂ with - | .OutOfFuel => .error .OutOfFuel - | .Checkpoint j => .ok (.Checkpoint j, [⟨0⟩]) - | .Ok sharedState₂ _ => - -- Restore ExecutionEnv - let executionEnv₃ := { sharedState₂.executionEnv with - calldata := default, - code := s₀.executionEnv.code, - codeOwner := s₀.executionEnv.codeOwner, - source := s₀.executionEnv.source, - weiValue := s₀.executionEnv.weiValue, - } - let sharedState₃ := { sharedState₂ with - memory := memory₃, - returnData := s₂.toMachineState.H_return, - H_return := ByteArray.empty, - executionEnv := executionEnv₃ - } - .ok (.Ok sharedState₃ varstore, [⟨1⟩]) + restoreSuccessfulContractCallState s₀Accessed s₂ varstore + ByteArray.empty inOffset inSize outOffset outSize | _ => .error .InvalidArguments -- Incorrect number of arguments, this case should be impossible if the Yul code is parsed correctly. Guaranteed by the compiler. | .DELEGATECALL => match args with - | _ :: address_arg :: inOffset :: inSize :: outOffset :: outSize :: _ => + | gas :: address_arg :: inOffset :: inSize :: outOffset :: outSize :: _ => let address := AccountAddress.ofUInt256 address_arg + let s₀Accessed := addAccessedAccount s₀ address let calldata₁ := s₀.toMachineState.memory.readWithPadding inOffset.toNat inSize.toNat + let callGas : UInt256 := + .ofNat <| + EvmYul.EVM.CcallgasWithProtocolFor + s₀.executionEnv.protocol s₀.toSharedState + address s₀.executionEnv.codeOwner ⟨0⟩ gas + s₀.sharedState.accountMap s₀.toMachineState s₀.toState.substate if s₀.executionEnv.depth ≥ 1024 then - buildContractCallEmptyReturnState s₀ .none ⟨0⟩ -- Reached depth limit: return 0 to indicate error, with empty return data + buildContractCallEmptyReturnState s₀Accessed .none + inOffset inSize outOffset outSize ⟨0⟩ -- Reached depth limit: return 0 to indicate error, with empty return data else - match s₀ with + match s₀Accessed with | .OutOfFuel => .error .OutOfFuel | .Checkpoint j => .ok (.Checkpoint j, [⟨0⟩]) | .Ok sharedState varstore => - match s₀.sharedState.accountMap.find? address with + match EvmYul.toExecute .Yul s₀.sharedState.accountMap address with + | .Precompiled precompiled => + let executionEnv₁ := { sharedState.executionEnv with + calldata := calldata₁, + code := default, + codeBytes := default, + codeOwner := s₀.executionEnv.codeOwner + depth := s₀.executionEnv.depth + 1 + } + buildPrecompiledContractCallState + s₀Accessed s₀.sharedState.accountMap precompiled callGas + executionEnv₁ inOffset inSize outOffset outSize + | .Code _ => + match s₀.sharedState.accountMap.find? address with | .none => - buildContractCallEmptyReturnState s₀ .none ⟨1⟩ -- No contract at the provided address, return 1 to indicate success, with empty return data. (Like STOP opcode). + buildContractCallEmptyReturnState s₀Accessed .none + inOffset inSize outOffset outSize ⟨1⟩ -- No contract at the provided address, return 1 to indicate success, with empty return data. (Like STOP opcode). | .some yulContract => let executionEnv₁ := { sharedState.executionEnv with calldata := calldata₁, code := yulContract.code, + codeBytes := yulContract.codeBytes, codeOwner := s₀.executionEnv.codeOwner depth := s₀.executionEnv.depth + 1 } let sharedState₁ := { sharedState with executionEnv := executionEnv₁, - memory := ByteArray.mk #[] + toMachineState := + EvmYul.MachineState.freshExternalCall callGas } let s₁ : State := .Ok sharedState₁ default - match callDispatcher fuel₁ yulContract.code s₁ with + match callDispatcher fuel₁ (some yulContract.code) s₁ with | .error (.YulHalt s₂ _) => - let memory₃ := s₂.toMachineState.H_return.copySlice 0 s₀.toMachineState.memory outOffset.toNat (min outSize.toNat s₂.toMachineState.H_return.size) - match s₂ with - | .OutOfFuel => .error .OutOfFuel - | .Checkpoint j => .ok (.Checkpoint j, [⟨0⟩]) - | .Ok sharedState₂ _ => - -- Restore ExecutionEnv - let executionEnv₃ := { sharedState₂.executionEnv with - calldata := default, - code := s₀.executionEnv.code, - codeOwner := s₀.executionEnv.codeOwner, - source := s₀.executionEnv.source, - weiValue := s₀.executionEnv.weiValue, - } - let sharedState₃ := { sharedState₂ with - memory := memory₃, - returnData := s₂.toMachineState.H_return, - H_return := ByteArray.empty, - executionEnv := executionEnv₃ - } - .ok (.Ok sharedState₃ varstore, [⟨1⟩]) + restoreSuccessfulContractCallState s₀Accessed s₂ varstore + s₂.toMachineState.H_return + inOffset inSize outOffset outSize + | .error (.Revert s₂) => + restoreRevertedContractCallState s₀Accessed s₂ + inOffset inSize outOffset outSize | .error e => .error e | .ok (s₂, _) => - let memory₃ := s₂.toMachineState.H_return.copySlice 0 s₀.toMachineState.memory outOffset.toNat (min outSize.toNat s₂.toMachineState.H_return.size) - match s₂ with - | .OutOfFuel => .error .OutOfFuel - | .Checkpoint j => .ok (.Checkpoint j, [⟨0⟩]) - | .Ok sharedState₂ _ => - -- Restore ExecutionEnv - let executionEnv₃ := { sharedState₂.executionEnv with - calldata := default, - code := s₀.executionEnv.code, - codeOwner := s₀.executionEnv.codeOwner, - source := s₀.executionEnv.source, - weiValue := s₀.executionEnv.weiValue, - } - let sharedState₃ := { sharedState₂ with - memory := memory₃, - returnData := s₂.toMachineState.H_return, - H_return := ByteArray.empty, - executionEnv := executionEnv₃ - } - .ok (.Ok sharedState₃ varstore, [⟨1⟩]) + restoreSuccessfulContractCallState s₀Accessed s₂ varstore + ByteArray.empty inOffset inSize outOffset outSize | _ => .error .InvalidArguments -- Incorrect number of arguments, this case should be impossible if the Yul code is parsed correctly. Guaranteed by the compiler. | _ => match step prim .none s₀ args with | .ok (s, lit) => .ok (s, lit.toList) @@ -449,7 +593,7 @@ def primCall (fuel : ℕ) (s₀ : State) (prim : Operation .Yul) (args : List Li match fOpt with | .none => .error (.MissingContractFunction (yulFunctionNameOption.getD ".none")) | .some f => - let s₁ := 👌 s.initcall f.params args + let s₁ := 👌 s.initcall f.params f.rets args match exec fuel' (.Block f.body) codeOverride s₁ with | .error e => .error e | .ok s₂ => @@ -466,7 +610,7 @@ def primCall (fuel : ℕ) (s₀ : State) (prim : Operation .Yul) (args : List Li | 0 => .error .OutOfFuel | .succ fuel' => let f := FunctionDefinition.Def [] [] [s.executionEnv.code.dispatcher] - let s₁ := 👌 s.initcall f.params [] + let s₁ := 👌 s.initcall f.params f.rets [] match exec fuel' (.Block f.body) codeOverride s₁ with | .error e => .error e | .ok s₂ => @@ -499,31 +643,24 @@ def primCall (fuel : ℕ) (s₀ : State) (prim : Operation .Yul) (args : List Li | .succ fuel' => multifill' vars (call fuel' args yulFunctionName codeOverride s) | .error e => .error e - /-- - `execSwitchCases` executes each case of a `switch` statement. - -/ - def execSwitchCases (fuel : Nat) (codeOverride : Option YulContract) (s : State) : List (Literal × List Stmt) → Except Yul.Exception (List (Literal × (Except Yul.Exception State))) - | [] => .ok [] - | ((val, stmts) :: cases') => - match fuel with - | 0 => .error .OutOfFuel - | .succ fuel' => - match exec fuel' (.Block stmts) codeOverride s with - | .error (.YulHalt s₂ v) => - match execSwitchCases fuel' codeOverride s cases' with - | .error e => .error e - | .ok s₃ => - .ok ((val, .error (.YulHalt s₂ v)) :: s₃) - | .error e => - match execSwitchCases fuel' codeOverride s cases' with - | .error e => .error e - | .ok s₃ => - .ok ((val, .error e) :: s₃) - | .ok s₂ => - match execSwitchCases fuel' codeOverride s cases' with - | .error e => .error e - | .ok s₃ => - .ok ((val, .ok s₂) :: s₃) + def evalValues (fuel : Nat) (expr : Expr) (codeOverride : Option YulContract) (s : State) : Except Yul.Exception (State × List Literal) := + match fuel with + | 0 => .error .OutOfFuel + | .succ fuel' => + match expr with + | .Call (Sum.inl prim) args => + match reverse' (evalArgs fuel' args.reverse codeOverride s) with + | .ok (s, args) => primCall fuel' s prim args + | .error e => .error e + | .Call (Sum.inr yulFunctionName) args => + match reverse' (evalArgs fuel' args.reverse codeOverride s) with + | .ok (s, args) => call fuel' args yulFunctionName codeOverride s + | .error e => .error e + | .Var id => + match s.lookup? id with + | .some val => .ok (s, [val]) + | .none => .error (.UnknownIdentifier id) + | .Lit val => .ok (s, [val]) /-- `eval` evaluates an expression. @@ -531,24 +668,22 @@ def primCall (fuel : ℕ) (s₀ : State) (prim : Operation .Yul) (args : List Li - calls evaluated here are assumed to have coarity 1 -/ def eval (fuel : Nat) (expr : Expr) (codeOverride : Option YulContract) (s : State) : Except Yul.Exception (State × Literal) := + head' (evalValues fuel expr codeOverride s) + + def execSeq (fuel : Nat) (stmts : List Stmt) (codeOverride : Option YulContract) (s : State) : Except Yul.Exception State := match fuel with | 0 => .error .OutOfFuel | .succ fuel' => - match expr with - - -- We hit these two cases (`PrimCall` and `Call`) when evaluating: - -- - -- 1. f() (expression statements) - -- 2. g(f()) (calls in function arguments) - -- 3. if f() {...} (if conditions) - -- 4. for {...} f() ... (for conditions) - -- 5. switch f() ... (switch conditions) - - | .Call (Sum.inl prim) args => evalPrimCall fuel' prim (reverse' (evalArgs fuel' args.reverse codeOverride s)) - | .Call (Sum.inr yulFunctionName) args => - evalCall fuel' yulFunctionName codeOverride (reverse' (evalArgs fuel' args.reverse codeOverride s)) - | .Var id => .ok (s, s[id]!) - | .Lit val => .ok (s, val) + match stmts with + | [] => .ok s + | stmt :: stmts => + match exec fuel' stmt codeOverride s with + | .error e => .error e + | .ok s₁ => + match s₁ with + | .Ok _ _ => execSeq fuel' stmts codeOverride s₁ + | .OutOfFuel => .ok s₁ + | .Checkpoint _ => .ok s₁ /-- `exec` executs a single statement. @@ -558,24 +693,23 @@ def primCall (fuel : ℕ) (s₀ : State) (prim : Operation .Yul) (args : List Li | 0 => .error .OutOfFuel | .succ fuel' => match stmt with - | .Block [] => .ok s - | .Block (stmt :: stmts) => - let s₁ := exec fuel' stmt codeOverride s - match s₁ with - | .error e => .error e - | .ok s₁ => exec fuel' (.Block stmts) codeOverride s₁ + | .Block stmts => + match execSeq fuel' stmts codeOverride s with + | .error e => .error e + | .ok s₁ => .ok (s₁.restrictStoreTo s.store) | .Let vars exprOption => + match checkDeclaration s vars with + | .error e => .error e + | .ok () => match exprOption with - | .none => .ok (List.foldr (λ var s ↦ s.insert var ⟨0⟩) s vars) - | .some expr => - match expr with - | .Call (Sum.inl prim) args => - execPrimCall fuel' prim vars (reverse' (evalArgs fuel' args.reverse codeOverride s)) - | .Call (Sum.inr yulFunctionName) args => - execCall fuel' yulFunctionName vars codeOverride (reverse' (evalArgs fuel' args.reverse codeOverride s)) - | .Var identifier => .ok (s.insert vars.head! s[identifier]!) -- It should be safe to call head! here if the Yul code is parsed correctly. - | .Lit literal => .ok (s.insert vars.head! literal) -- It should be safe to call head! here if the Yul code is parsed correctly. + | .none => .ok (s.zeroFill vars) + | .some expr => multifill' vars (evalValues fuel' expr codeOverride s) + + | .Assign vars expr => + match checkAssignment s vars with + | .error e => .error e + | .ok () => multifill' vars (evalValues fuel' expr codeOverride s) | .If cond body => match eval fuel' cond codeOverride s with @@ -599,13 +733,7 @@ def primCall (fuel : ℕ) (s₀ : State) (prim : Operation .Yul) (args : List Li match eval fuel' cond codeOverride s with | .error e => .error e | .ok (s₁, cond) => - match execSwitchCases fuel' codeOverride s₁ cases' with - | .error e => .error e - | .ok branches => - match exec fuel' (.Block default') codeOverride s₁ with - | .error e => .error e - | .ok s₂ => - (List.foldr (λ (valᵢ, sᵢ) s ↦ if valᵢ = cond then sᵢ else s) (.ok s₂) branches) + exec fuel' (.Block (selectSwitchCase cond default' cases')) codeOverride s₁ -- A `Break` or `Continue` in the pre or post is a compiler error, -- so we assume it can't happen and don't modify the state in these @@ -642,11 +770,15 @@ def primCall (fuel : ℕ) (s₀ : State) (prim : Operation .Yul) (args : List Li | .error e => .error e | .ok s₃ => let s₄ := s₃✏️⟦s⟧? - match exec fuel' (.For cond post body) codeOverride s₄ with - | .error e => .error e - | .ok s₅ => - let s₆ := s₅✏️⟦s⟧? - .ok s₆ + match s₃ with + | .OutOfFuel => .ok s₄ + | .Checkpoint (.Leave _ _) => .ok s₄ + | _ => + match exec fuel' (.For cond post body) codeOverride s₄ with + | .error e => .error e + | .ok s₅ => + let s₆ := s₅✏️⟦s⟧? + .ok s₆ end def execTopLevel (fuel : Nat) (stmt : Stmt) (s : State) : State := @@ -656,11 +788,14 @@ def execTopLevel (fuel : Nat) (stmt : Stmt) (s : State) : State := | .error .InvalidInstruction => default | .error .OutOfFuel => default | .error .StaticModeViolation => s -- Revert, note that we do not model charging gas in the Yul semantics + | .error .InvalidMemoryAccess => default | .error (.MissingContract _) => default | .error (.MissingContractFunction _) => default -- We do not model fallback functions | .error .InvalidExpression => default + | .error (.UnknownIdentifier _) => default + | .error (.DuplicateDeclaration _) => default | .error .YulEXTCODESIZENotImplemented => default - | .error .Revert => s + | .error (.Revert _) => s | .error (.YulHalt s _) => s | .ok s => s diff --git a/EvmYul/Yul/StateOps.lean b/EvmYul/Yul/StateOps.lean index 357d782..a0d5924 100644 --- a/EvmYul/Yul/StateOps.lean +++ b/EvmYul/Yul/StateOps.lean @@ -16,6 +16,19 @@ def multifill (vars : List Identifier) (vals : List Literal) : Yul.State → Yul | s@(Ok _ _) => (List.zip vars vals).foldr (λ (var, val) s ↦ s.insert var val) s | s => s +def zeroFill (vars : List Identifier) : Yul.State → Yul.State := + vars.foldr (λ var s ↦ s.insert var ⟨0⟩) + +def restrictVarStore (store scope : VarStore) : VarStore := + store.sdiff (store.sdiff scope) + +def restrictStoreTo (scope : VarStore) : Yul.State → Yul.State + | Ok sharedState store => Ok sharedState (restrictVarStore store scope) + | Checkpoint (.Continue sharedState store) => Checkpoint (.Continue sharedState (restrictVarStore store scope)) + | Checkpoint (.Break sharedState store) => Checkpoint (.Break sharedState (restrictVarStore store scope)) + | Checkpoint (.Leave sharedState store) => Checkpoint (.Leave sharedState (restrictVarStore store scope)) + | s => s + -- | Overwrite the EvmYul.Yul.State state of some state. def setSharedState (sharedState : EvmYul.SharedState .Yul) : Yul.State → Yul.State | Ok _ store => Ok sharedState store @@ -53,10 +66,11 @@ def diverge : Yul.State → Yul.State | s => s -- | Initialize function parameters and return values in varstore. -def initcall (params : List Identifier) (args : List Literal) : Yul.State → Yul.State +def initcall (params rets : List Identifier) (args : List Literal) : Yul.State → Yul.State | s@(Ok _ _) => let s₁ := s.setStore default - s₁.multifill params args + let s₂ := s₁.zeroFill rets + s₂.multifill params args | s => s -- | Since it literally does not matter what happens if the state is non-Ok, we just use the default. @@ -93,13 +107,16 @@ def overwrite? (s s' : Yul.State) : Yul.State := -- STATE QUERIES -- ============================================================================ --- | Lookup the literal associated with an variable in the varstore, returning 0 if not found. +def lookup? (var : Identifier) : Yul.State → Option Literal + | Ok _ store => store.lookup var + | Checkpoint (.Continue _ store) => store.lookup var + | Checkpoint (.Break _ store) => store.lookup var + | Checkpoint (.Leave _ store) => store.lookup var + | _ => .none + +-- | Lookup the literal associated with a variable in the varstore, returning 0 if not found. def lookup! (var : Identifier) : Yul.State → Literal - | Ok _ store => (store.lookup var).get! - | Checkpoint (.Continue _ store) => (store.lookup var).get! - | Checkpoint (.Break _ store) => (store.lookup var).get! - | Checkpoint (.Leave _ store) => (store.lookup var).get! - | _ => ⟨0⟩ + | s => (s.lookup? var).getD ⟨0⟩ -- ============================================================================ -- STATE NOTATION @@ -150,7 +167,7 @@ notation:65 s:64 "🏪⟦" s' "⟧" => Yul.State.setStore s s' notation:65 s:64 "🇪⟦" sharedState "⟧" => Yul.State.setSharedState sharedState s notation:65 "🪫" s:64 => Yul.State.diverge s notation:65 "👌" s:64 => Yul.State.mkOk s -notation:65 s:64 "☎️⟦" params "," args "⟧" => Yul.State.initcall params args s +notation:65 s:64 "☎️⟦" params "," rets "," args "⟧" => Yul.State.initcall params rets args s notation:65 s:64 "✏️⟦" s' "⟧?" => Yul.State.overwrite? s s' notation:64 (priority := high) "🧟" s:max => Yul.State.reviveJump s diff --git a/EvmYul/Yul/YulNotation.lean b/EvmYul/Yul/YulNotation.lean index 5863ae6..4ebaea9 100644 --- a/EvmYul/Yul/YulNotation.lean +++ b/EvmYul/Yul/YulNotation.lean @@ -28,20 +28,19 @@ def idSubsequentChar : Array Char := Id.run <| do return arr.push '.' def idFn : ParserFn := fun c s => Id.run do - let input := c.input let start := s.pos - if h : input.atEnd start then + if h : c.atEnd start then s.mkEOIError else - let fst := input.get' start h + let fst := c.get' start h if not (idFirstChar.contains fst) then return s.mkError "yul identifier" - let s := takeWhileFn idSubsequentChar.contains c (s.next input start) + let s := takeWhileFn idSubsequentChar.contains c (s.next c start) let stop := s.pos - let name := .str .anonymous (input.extract start stop) + let name := .str .anonymous (c.extract start stop) if yulKeywords.contains name.lastComponentAsString then return s.mkError "yul identifier" - mkIdResult start none name c s + (mkIdResult start none name) c s def idNoAntiquot : Parser := { fn := idFn } @@ -63,56 +62,50 @@ end @[run_parser_attribute_hooks] def ident : Parser := withAntiquot (mkAntiquot "ident" identKind) idNoAntiquot -declare_syntax_cat expr -declare_syntax_cat stmt +declare_syntax_cat yulExpr +declare_syntax_cat yulStmt -syntax identifier_list := ident,* -syntax typed_identifier_list := ident,* -syntax function_call := ident "(" expr,* ")" -syntax block := "{" stmt* "}" -syntax if' := "if" expr block -syntax function_definition := - "function" ident "(" typed_identifier_list ")" - ("->" typed_identifier_list)? - block -syntax params_list := "[" typed_identifier_list "]" -syntax variable_declaration := "let" ident (":=" expr)? +syntax yulIdentifierList := ident,* +syntax yulTypedIdentifierList := ident,* +syntax yulFunctionCall := ident "(" yulExpr,* ")" +syntax yulBlock := "{" yulStmt* "}" +syntax yulIf := "if" yulExpr yulBlock +syntax yulFunctionDefinition := + "function" ident "(" yulTypedIdentifierList ")" + ("->" yulTypedIdentifierList)? + yulBlock +syntax yulParamsList := "[" yulTypedIdentifierList "]" +syntax yulVariableDeclaration := "let" ident (":=" yulExpr)? -- syntax let_str_literal := "let" ident ":=" str -- TODO(fix) -syntax variable_declarations := "let" typed_identifier_list (":=" expr)? -syntax for_loop := "for" block expr block block -syntax assignment := identifier_list ":=" expr +syntax yulVariableDeclarations := "let" yulTypedIdentifierList (":=" yulExpr)? +syntax yulForLoop := "for" yulBlock yulExpr yulBlock yulBlock +syntax yulAssignment := yulIdentifierList ":=" yulExpr -syntax stmtlist := stmt* +syntax yulStmtList := yulStmt* -syntax block : stmt -syntax if' : stmt -syntax function_definition : stmt -syntax variable_declarations : stmt -syntax assignment : stmt -syntax expr : stmt --- syntax let_str_literal : stmt -- TODO(fix) -syntax for_loop : stmt -syntax "break" : stmt -syntax "continue" : stmt -syntax "leave" : stmt +syntax yulBlock : yulStmt +syntax yulIf : yulStmt +syntax yulFunctionDefinition : yulStmt +syntax yulVariableDeclarations : yulStmt +syntax yulAssignment : yulStmt +syntax yulExpr : yulStmt +-- syntax let_str_literal : yulStmt -- TODO(fix) +syntax yulForLoop : yulStmt +syntax "break" : yulStmt +syntax "continue" : yulStmt +syntax "leave" : yulStmt -syntax ident : expr -syntax numLit : expr -syntax function_call: expr +syntax ident : yulExpr +syntax numLit : yulExpr +syntax yulFunctionCall: yulExpr -syntax default := "default" "{" stmt* "}" -syntax case := "case" expr "{" stmt* "}" -syntax switch := "switch" expr case+ (default)? -syntax switch_default := "switch" expr default +syntax yulDefault := "default" yulBlock +syntax yulCase := "case" yulExpr yulBlock +syntax yulSwitch := "switch" yulExpr yulCase* (yulDefault)? +syntax yulSwitchDefault := "switch" yulExpr yulDefault -syntax switch : stmt -syntax switch_default : stmt - -scoped syntax:max "<<" expr ">>" : term -scoped syntax:max "" : term -scoped syntax:max "" : term -scoped syntax:max "" : term -scoped syntax:max "" : term +syntax yulSwitch : yulStmt +syntax yulSwitchDefault : yulStmt def translateString (s : String) : TermElabM Term := pure (Syntax.mkStrLit s) @@ -127,11 +120,11 @@ partial def translatePrimOp (primOp : PrimOp) : TermElabM Term := do where familyAndInstr (primOp : PrimOp) : TermElabM (String × String) := do let family :: instr :: [] := toString primOp |>.splitOn | throwError s!"{primOp} shape not " - pure (family, instr.drop 1 |>.dropRight 1) + pure (family, (instr.drop 1 |>.dropEnd 1).toString) YulTag : Name := "EvmYul.OperationType.Yul".toName -partial def translateIdent (idn : TSyntax `ident) : TSyntax `term := - Syntax.mkStrLit idn.getId.lastComponentAsString +partial def translateIdent (idn : Syntax) : TSyntax `term := + Syntax.mkStrLit (TSyntax.getId (TSyntax.mk (ks := `ident) idn)).lastComponentAsString def parseFunction : String → PrimOp ⊕ Identifier | "add" => .inl .ADD @@ -204,12 +197,14 @@ def parseFunction : String → PrimOp ⊕ Identifier | "number" => .inl .NUMBER | userF => .inr userF -partial def translateExpr (expr : TSyntax `expr) : TermElabM Term := - match expr with - | `(expr| $idn:ident) => `(Expr.Var $(translateIdent idn)) - | `(expr| $num:num) => `(Expr.Lit (.ofNat $num)) - | `(expr| $name:ident($args:expr,*)) => do - let args' ← (args : TSyntaxArray `expr).mapM translateExpr +partial def translateExpr + (expr : Syntax) +: TermElabM Term := + match (TSyntax.mk (ks := `EvmYul.Yul.Notation.yulExpr) expr) with + | `(yulExpr| $idn:ident) => `(Expr.Var $(translateIdent idn)) + | `(yulExpr| $num:num) => `(Expr.Lit (.ofNat $num)) + | `(yulExpr| $name:ident($args:yulExpr,*)) => do + let args' ← args.getElems.raw.mapM translateExpr let f' := parseFunction (TSyntax.getId name).lastComponentAsString match f' with | .inl primOp => @@ -220,72 +215,96 @@ partial def translateExpr (expr : TSyntax `expr) : TermElabM Term := `(Expr.Call (Sum.inr $yulFunctionName) [$args',*]) | _ => throwError "unknown expr" -partial def translateExpr' (expr : TSyntax `expr) : TermElabM Term := - match expr with - | `(expr| $num:num) => `(.ofNat $num) +partial def translateExpr' + (expr : Syntax) +: TermElabM Term := + match (TSyntax.mk (ks := `EvmYul.Yul.Notation.yulExpr) expr) with + | `(yulExpr| $num:num) => `(.ofNat $num) | exp => translateExpr exp partial def translateParamsList - (params : TSyntax `EvmYul.Yul.Notation.params_list) + (params : Syntax) : TermElabM Term := - match params with - | `(params_list| [ $args:ident,* ]) => do - let args' := (args : TSyntaxArray _).map translateIdent + match (TSyntax.mk (ks := `EvmYul.Yul.Notation.yulParamsList) params) with + | `(yulParamsList| [ $args:ident,* ]) => do + let args' := args.getElems.raw.map translateIdent `([$args',*]) - | _ => throwError (toString params.raw) + | _ => throwError (toString params) mutual -partial def translateFdef - (fdef : TSyntax `EvmYul.Yul.Notation.function_definition) -: TermElabM Term := - match fdef with - | `(function_definition| function $_:ident($args:ident,*) {$body:stmt*}) => do - let args' := (args : TSyntaxArray _).map translateIdent - let body' ← body.mapM translateStmt - `(EvmYul.Yul.Ast.FunctionDefinition.Def [$args',*] [] [$body',*]) - | `(function_definition| function $_:ident($args:ident,*) -> $rets,* {$body:stmt*}) => do - let args' := (args : TSyntaxArray _).map translateIdent - let rets' := (rets : TSyntaxArray _).map translateIdent - let body' ← body.mapM translateStmt - `(EvmYul.Yul.Ast.FunctionDefinition.Def [$args',*] [$rets',*] [$body',*]) - | _ => throwError (toString fdef.raw) +partial def translateBlock + (block : Syntax) +: TermElabM (Array Term) := + match (TSyntax.mk (ks := `EvmYul.Yul.Notation.yulBlock) block) with + | `(yulBlock| { $stmts:yulStmt* }) => stmts.raw.mapM translateStmt + | _ => throwError (toString block) -partial def translateStmt (stmt : TSyntax `stmt) : TermElabM Term := - match stmt with +partial def translateDefault + (dflt : Syntax) +: TermElabM (Array Term) := + match (TSyntax.mk (ks := `EvmYul.Yul.Notation.yulDefault) dflt) with + | `(yulDefault| default $body:yulBlock) => translateBlock body + | _ => throwError (toString dflt) + +partial def translateCase + (c : Syntax) +: TermElabM Term := + match (TSyntax.mk (ks := `EvmYul.Yul.Notation.yulCase) c) with + | `(yulCase| case $lit:yulExpr $body:yulBlock) => do + let lit ← translateExpr' lit + let body ← translateBlock body + `(($lit, [$body,*])) + | _ => throwError (toString c) + +partial def translateFdef + (fdef : Syntax) +: TermElabM Term := + match (TSyntax.mk (ks := `EvmYul.Yul.Notation.yulFunctionDefinition) fdef) with + | `(yulFunctionDefinition| function $_:ident($args:ident,*) $body:yulBlock) => do + let args' := args.getElems.raw.map translateIdent + let body' ← translateBlock body + `(EvmYul.Yul.Ast.FunctionDefinition.Def [$args',*] [] [$body',*]) + | `(yulFunctionDefinition| function $_:ident($args:ident,*) -> $rets,* $body:yulBlock) => do + let args' := args.getElems.raw.map translateIdent + let rets' := rets.getElems.raw.map translateIdent + let body' ← translateBlock body + `(EvmYul.Yul.Ast.FunctionDefinition.Def [$args',*] [$rets',*] [$body',*]) + | _ => throwError (toString fdef) + +partial def translateStmt + (stmt : Syntax) +: TermElabM Term := + match (TSyntax.mk (ks := `EvmYul.Yul.Notation.yulStmt) stmt) with -- Block - | `(stmt| {$stmts:stmt*}) => do - let stmts' ← stmts.mapM translateStmt + | `(yulStmt| $body:yulBlock) => do + let stmts' ← translateBlock body `(Stmt.Block ([$stmts',*])) -- If - | `(stmt| if $cond:expr {$body:stmt*}) => do + | `(yulStmt| if $cond:yulExpr $body:yulBlock) => do let cond' ← translateExpr cond - let body' ← body.mapM translateStmt + let body' ← translateBlock body `(Stmt.If $cond' [$body',*]) -- Switch - | `(stmt| switch $expr:expr $[case $lits { $cs:stmt* }]* $[default { $dflts:stmt* }]?) => do + | `(yulStmt| switch $expr:yulExpr $cases:yulCase* $[$dflt:yulDefault]?) => do let expr ← translateExpr expr - let lits ← lits.mapM translateExpr' - let cases ← cs.mapM (λ cc ↦ cc.mapM translateStmt) - let f (litCase : TSyntax `term × Array Term) : TermElabM Term := do - let (lit, cs) := litCase; `(($lit, [$cs,*])) - let switchCases ← lits.zip cases |>.mapM f - let dflt ← match dflts with - | .none => `([.Break]) - | .some dflts => `([$(←dflts.mapM translateStmt),*]) + let switchCases ← cases.raw.mapM translateCase + let dflt ← match dflt with + | .none => `([]) + | .some dflt => `([$(←translateDefault dflt),*]) `(Stmt.Switch $expr [$switchCases,*] $dflt) -- Switch - | `(stmt| switch $expr:expr default {$dflts:stmt*}) => do + | `(yulStmt| switch $expr:yulExpr $dflt:yulDefault) => do let expr ← translateExpr expr - let dflt ← dflts.mapM translateStmt + let dflt ← translateDefault dflt `(Stmt.Switch $expr [] ([$dflt,*])) -- Let - | `(stmt| let $ids:ident,* := $expr:expr) => do - let ids' := (ids : TSyntaxArray _).map translateIdent + | `(yulStmt| let $ids:ident,* := $expr:yulExpr) => do + let ids' := ids.getElems.raw.map translateIdent let expr ← translateExpr expr `(Stmt.Let [$ids',*] (.some $expr)) @@ -301,57 +320,62 @@ partial def translateStmt (stmt : TSyntax `stmt) : TermElabM Term := -- `(Stmt.LetEq $idn' _) -- Let - | `(stmt| let $ids:ident,*) => do - let ids' := (ids : TSyntaxArray _).map translateIdent + | `(yulStmt| let $ids:ident,*) => do + let ids' := ids.getElems.raw.map translateIdent `(Stmt.Let [$ids',*] .none) -- AssignCall - | `(stmt| $ids:ident,* := $expr:expr) => do - let ids' := (ids : TSyntaxArray _).map translateIdent + | `(yulStmt| $ids:ident,* := $expr:yulExpr) => do + let ids' := ids.getElems.raw.map translateIdent let expr ← translateExpr expr - `(Stmt.Let [$ids',*] (.some $expr)) + `(Stmt.Assign [$ids',*] $expr) -- ExprStmt - | `(stmt| $expr:expr) => do + | `(yulStmt| $expr:yulExpr) => do let expr ← translateExpr expr `(Stmt.ExprStmtCall $expr) -- For - | `(stmt| for {} $cond:expr {$post:stmt*} {$body:stmt*}) => do + | `(yulStmt| for $pre:yulBlock $cond:yulExpr $post:yulBlock $body:yulBlock) => do + let pre ← translateBlock pre + unless pre.isEmpty do + throwError "nonempty for pre-blocks are not supported by Yul notation" let cond' ← translateExpr cond - let post' ← post.mapM translateStmt - let body' ← body.mapM translateStmt + let post' ← translateBlock post + let body' ← translateBlock body `(Stmt.For $cond' [$post',*] [$body',*]) -- Break - | `(stmt| break) => `(Stmt.Break) + | `(yulStmt| break) => `(Stmt.Break) -- Continue - | `(stmt| continue) => `(Stmt.Continue) + | `(yulStmt| continue) => `(Stmt.Continue) -- Leave - | `(stmt| leave) => `(Stmt.Leave) + | `(yulStmt| leave) => `(Stmt.Leave) -- Anything else - | _ => throwError (toString stmt.raw) + | _ => throwError (toString stmt) end -partial def translateStmtList (stmt : TSyntax `stmt) : TermElabM Term := - match stmt with - | `(stmt| {$stmts:stmt*}) => do - let stmts' ← stmts.mapM translateStmt +partial def translateStmtList + (stmt : Syntax) +: TermElabM Term := + match (TSyntax.mk (ks := `EvmYul.Yul.Notation.yulStmt) stmt) with + | `(yulStmt| $body:yulBlock) => do + let stmts' ← translateBlock body `([$stmts',*]) - | _ => throwError (toString stmt.raw) + | _ => throwError (toString stmt) -private def elabWith {β : SyntaxNodeKinds} - (x : Syntax) (translator : TSyntax β → TermElabM Term) : TermElabM Lean.Expr := do - elabTerm (←translator (TSyntax.mk (ks := β) x)) .none +private def elabWith + (x : Syntax) (translator : Syntax → TermElabM Term) : TermElabM Lean.Expr := do + elabTerm (←translator x) .none -elab "<<" e:expr ">>" : term => elabWith e translateExpr -elab "" : term => elabWith f translateFdef -elab "" : term => elabWith s translateStmt -elab "" : term => elabWith ss translateStmtList -elab "" : term => elabWith p translateParamsList +scoped elab:max "<<" e:yulExpr ">>" : term => elabWith e translateExpr +scoped elab:max "" : term => elabWith f translateFdef +scoped elab:max "" : term => elabWith s translateStmt +scoped elab:max "" : term => elabWith ss translateStmtList +scoped elab:max "" : term => elabWith p translateParamsList def f : FunctionDefinition := x, y { @@ -373,10 +397,10 @@ example : = Stmt.Break := rfl example : = Stmt.Let ["a", "b"] (.some (.Call (Sum.inr "f") [Expr.Lit ⟨42⟩])) := rfl example : = Stmt.Let ["a"] .none := rfl example : = Stmt.Let ["a"] (.some (.Lit ⟨5⟩)) := rfl -example : = Stmt.Let ["a", "b"] (.some (.Call (Sum.inr "f") [Expr.Lit ⟨42⟩])) := rfl -example : = Stmt.Let ["a"] (.some (.Lit ⟨42⟩)) := rfl +example : = Stmt.Assign ["a", "b"] (.Call (Sum.inr "f") [Expr.Lit ⟨42⟩]) := rfl +example : = Stmt.Assign ["a"] (.Lit ⟨42⟩) := rfl -example : = Stmt.Let ["c"] (.some (Expr.Call (Sum.inl (Operation.StopArith Operation.SAOp.ADD)) [Expr.Var "a", Expr.Var "b"])) := rfl +example : = Stmt.Assign ["c"] (Expr.Call (Sum.inl (Operation.StopArith Operation.SAOp.ADD)) [Expr.Var "a", Expr.Var "b"]) := rfl example : = Stmt.Let ["c"] (.some (Expr.Call (Sum.inl (Operation.StopArith Operation.SAOp.SUB)) [Expr.Var "a", Expr.Var "b"])) := rfl example : = Stmt.Let ["a"] (.some (.Lit ⟨5⟩)) := rfl example : @@ -438,6 +462,11 @@ example : = Stmt.Switch (Expr.Var "a") [(⟨42⟩, [.Continue])] [.Break] := rfl +example : = Stmt.Switch (.Lit ⟨1⟩) [(⟨2⟩, [])] [] := rfl + example : = Stmt.Let ["a", "b", "c"] .none := rfl example : = Stmt.ExprStmtCall (.Call (Sum.inl (.System (.REVERT))) [(Expr.Lit ⟨0⟩), (Expr.Lit ⟨0⟩)]) := rfl example : = Stmt.If (.Lit ⟨1⟩) [Stmt.Leave] := rfl diff --git a/EvmYul/Yul/YulSemanticsTests/Main.lean b/EvmYul/Yul/YulSemanticsTests/Main.lean index 6387de3..90a0782 100644 --- a/EvmYul/Yul/YulSemanticsTests/Main.lean +++ b/EvmYul/Yul/YulSemanticsTests/Main.lean @@ -1244,6 +1244,177 @@ def test₅ := | .error e => repr e | .ok s => s!"{s.toSharedState.accountMap.toList.map (fun (a : AccountAddress × Account .Yul) => repr a.1 ++ " " ++ repr a.2.storage.toList)}" +def test₆ := + let stmt : Stmt := + .Switch (.Lit ⟨1⟩) + [(⟨1⟩, [])] + [.ExprStmtCall + (.Call (Sum.inl (.System (.REVERT))) [.Lit ⟨0⟩, .Lit ⟨0⟩])] + match exec 99 stmt .none stateEg₁ with + | .error e => repr e + | .ok _ => "selected" + +def showVar? (name : Identifier) : State → String + | .Ok _ store => + match store.lookup name with + | .some value => toString value.toNat + | .none => "none" + | .Checkpoint _ => "checkpoint" + | .OutOfFuel => "out-of-fuel" + +def showExecVar (name : Identifier) (stmt : Stmt) (s : State) : String := + match exec 99 stmt .none s with + | .error e => toString (repr e) + | .ok s => showVar? name s + +def showExecMode (stmt : Stmt) (s : State) : String := + match exec 99 stmt .none s with + | .error e => toString (repr e) + | .ok (.Ok _ _) => "regular" + | .ok (.Checkpoint (.Break _ _)) => "break" + | .ok (.Checkpoint (.Continue _ _)) => "continue" + | .ok (.Checkpoint (.Leave _ _)) => "leave" + | .ok .OutOfFuel => "out-of-fuel" + +def test₇ := + let stmt : Stmt := + .Block + [ .Block [.Let ["x"] (.some (.Lit ⟨1⟩))] + , .Let ["y"] (.some (.Var "x")) + ] + showExecVar "y" stmt stateEg₁ + +def test₈ := + let stmt : Stmt := + .Block + [ .Let ["x"] (.some (.Lit ⟨1⟩)) + , .Block [.Let ["x"] (.some (.Lit ⟨2⟩))] + , .Let ["y"] (.some (.Var "x")) + ] + showExecVar "y" stmt stateEg₁ + +def stateWithX : State := + .Ok stateEg₁.toSharedState ((∅ : VarStore).insert "x" ⟨1⟩) + +def test₉ := + showExecVar "x" (.Assign ["x"] (.Lit ⟨2⟩)) stateWithX + +def test₁₀ := + showExecVar "x" (.Assign ["x"] (.Lit ⟨2⟩)) stateEg₁ + +def auditAddressUInt256 : UInt256 := ⟨100⟩ +def auditCalleeAddressUInt256 : UInt256 := ⟨101⟩ +def auditAddress := AccountAddress.ofUInt256 auditAddressUInt256 +def auditCalleeAddress := AccountAddress.ofUInt256 auditCalleeAddressUInt256 + +def auditStateWithContract (code : YulContract) : State := + let account : Account .Yul := + { code := code + , balance := ⟨1000⟩ + , nonce := ⟨0⟩ + , storage := ∅ + , tstorage := ∅ + } + let accountMap : AccountMap .Yul := (∅ : AccountMap .Yul).insert auditAddress account + let sharedState : SharedState .Yul := + { (Inhabited.default : SharedState .Yul) with + accountMap := accountMap + executionEnv := + { (Inhabited.default : ExecutionEnv .Yul) with + codeOwner := auditAddress + code := code + perm := true + } + } + .Ok sharedState ∅ + +def returnInitContract : YulContract := + { dispatcher := .Block [] + , functions := + (∅ : Finmap (fun (_ : YulFunctionName) ↦ Yul.Ast.FunctionDefinition)) + |>.insert "f" (.Def [] ["r"] []) + } + +def test₁₁ := + let stmt : Stmt := .Let ["x"] (.some (.Call (Sum.inr "f") [])) + showExecVar "x" stmt (auditStateWithContract returnInitContract) + +def callRevertState : State := + let callerCode : YulContract := + { dispatcher := .Block [] + , functions := ∅ + } + let calleeCode : YulContract := + { dispatcher := + .ExprStmtCall + (.Call (Sum.inl (.System (.REVERT))) [.Lit ⟨0⟩, .Lit ⟨0⟩]) + , functions := ∅ + } + let callerAccount : Account .Yul := + { code := callerCode + , balance := ⟨1000⟩ + , nonce := ⟨0⟩ + , storage := ∅ + , tstorage := ∅ + } + let calleeAccount : Account .Yul := + { code := calleeCode + , balance := ⟨0⟩ + , nonce := ⟨0⟩ + , storage := ∅ + , tstorage := ∅ + } + let accountMap : AccountMap .Yul := + ((∅ : AccountMap .Yul).insert auditAddress callerAccount) + |>.insert auditCalleeAddress calleeAccount + let sharedState : SharedState .Yul := + { (Inhabited.default : SharedState .Yul) with + accountMap := accountMap + executionEnv := + { (Inhabited.default : ExecutionEnv .Yul) with + codeOwner := auditAddress + code := callerCode + perm := true + } + } + .Ok sharedState ∅ + +def test₁₂ := + let stmt : Stmt := + .Let ["ok"] + (.some + (.Call (Sum.inl (.System (.CALL))) + [ .Lit ⟨100000⟩ + , .Lit auditCalleeAddressUInt256 + , .Lit ⟨7⟩ + , .Lit ⟨0⟩ + , .Lit ⟨0⟩ + , .Lit ⟨0⟩ + , .Lit ⟨0⟩ + ])) + showExecVar "ok" stmt callRevertState + +def selfdestructState : State := + match callRevertState with + | .Ok sharedState _ => .Ok sharedState ((∅ : VarStore).insert "after" ⟨0⟩) + | s => s + +def test₁₃ := + let stmt : Stmt := + .Block + [ .ExprStmtCall + (.Call (Sum.inl (.System (.SELFDESTRUCT))) [.Lit auditCalleeAddressUInt256]) + , .Assign ["after"] (.Lit ⟨1⟩) + ] + showVar? "after" (execTopLevel 99 stmt selfdestructState) + +def test₁₄ := + let stmt : Stmt := + showExecMode stmt stateEg₁ + end Yul @@ -1259,3 +1430,12 @@ def main : IO Unit := do IO.println (s!"test₃: {test₃} -- " ++ (if s!"{test₃}" = "StaticModeViolation" then "Success" else "Failure")) IO.println (s!"test₄: {test₄} -- " ++ (if s!"{test₄}" = "[1 [], 2 [(0, 5)], 3 [], 4 []]" then "Success" else "Failure")) IO.println (s!"test₅: {test₅} -- " ++ (if s!"{test₅}" = "[1 [], 2 [(0, 5)], 3 [], 4 []]" then "Success" else "Failure")) + IO.println (s!"test₆: {test₆} -- " ++ (if s!"{test₆}" = "selected" then "Success" else "Failure")) + IO.println (s!"test₇: {test₇} -- " ++ (if s!"{test₇}" = "UnknownIdentifier: x" then "Success" else "Failure")) + IO.println (s!"test₈: {test₈} -- " ++ (if s!"{test₈}" = "DuplicateDeclaration: x" then "Success" else "Failure")) + IO.println (s!"test₉: {test₉} -- " ++ (if s!"{test₉}" = "2" then "Success" else "Failure")) + IO.println (s!"test₁₀: {test₁₀} -- " ++ (if s!"{test₁₀}" = "UnknownIdentifier: x" then "Success" else "Failure")) + IO.println (s!"test₁₁: {test₁₁} -- " ++ (if s!"{test₁₁}" = "0" then "Success" else "Failure")) + IO.println (s!"test₁₂: {test₁₂} -- " ++ (if s!"{test₁₂}" = "0" then "Success" else "Failure")) + IO.println (s!"test₁₃: {test₁₃} -- " ++ (if s!"{test₁₃}" = "0" then "Success" else "Failure")) + IO.println (s!"test₁₄: {test₁₄} -- " ++ (if s!"{test₁₄}" = "regular" then "Success" else "Failure")) diff --git a/README.md b/README.md index fbc60f0..72d3531 100644 --- a/README.md +++ b/README.md @@ -99,4 +99,4 @@ These tests are defined in `EvmYul/Yul/YulSemanticsTests/Main.lean`. ## SELFDESTRUCT -- Halting for `SELFDESTRUCT` is not implemented and the semantics for `SELFDESTRUCT` have limitations, such as not triggering the fallback function in a contract that is the recipient of the ether from the contract the self-destructs. We may remove the semantics for `SELFDESTRUCT` once its status changes from deprecated to not being supported. \ No newline at end of file +- Yul `SELFDESTRUCT` halts execution after applying the modelled account and substate effects. The semantics for `SELFDESTRUCT` still have limitations, such as not triggering the fallback function in a contract that is the recipient of the ether from the contract the self-destructs. We may remove the semantics for `SELFDESTRUCT` once its status changes from deprecated to not being supported. diff --git a/lake-manifest.json b/lake-manifest.json index 6d13093..3634b9c 100644 --- a/lake-manifest.json +++ b/lake-manifest.json @@ -5,27 +5,27 @@ "type": "git", "subDir": null, "scope": "", - "rev": "79e94a093aff4a60fb1b1f92d9681e407124c2ca", + "rev": "8f9d9cff6bd728b17a24e163c9402775d9e6a365", "name": "mathlib", "manifestFile": "lake-manifest.json", - "inputRev": "v4.22.0", + "inputRev": "v4.28.0", "inherited": false, "configFile": "lakefile.lean"}, {"url": "https://github.com/leanprover-community/plausible", "type": "git", "subDir": null, "scope": "leanprover-community", - "rev": "b100ad4c5d74a464f497aaa8e7c74d86bf39a56f", + "rev": "55c8532eb21ec9f6d565d51d96b8ca50bd1fbef3", "name": "plausible", "manifestFile": "lake-manifest.json", - "inputRev": "v4.22.0", + "inputRev": "main", "inherited": true, "configFile": "lakefile.toml"}, {"url": "https://github.com/leanprover-community/LeanSearchClient", "type": "git", "subDir": null, "scope": "leanprover-community", - "rev": "99657ad92e23804e279f77ea6dbdeebaa1317b98", + "rev": "c5d5b8fe6e5158def25cd28eb94e4141ad97c843", "name": "LeanSearchClient", "manifestFile": "lake-manifest.json", "inputRev": "main", @@ -35,60 +35,60 @@ "type": "git", "subDir": null, "scope": "leanprover-community", - "rev": "eb164a46de87078f27640ee71e6c3841defc2484", + "rev": "85b59af46828c029a9168f2f9c35119bd0721e6e", "name": "importGraph", "manifestFile": "lake-manifest.json", - "inputRev": "v4.22.0", + "inputRev": "main", "inherited": true, "configFile": "lakefile.toml"}, {"url": "https://github.com/leanprover-community/ProofWidgets4", "type": "git", "subDir": null, "scope": "leanprover-community", - "rev": "1253a071e6939b0faf5c09d2b30b0bfc79dae407", + "rev": "be3b2e63b1bbf496c478cef98b86972a37c1417d", "name": "proofwidgets", "manifestFile": "lake-manifest.json", - "inputRev": "v0.0.68", + "inputRev": "v0.0.87", "inherited": true, "configFile": "lakefile.lean"}, {"url": "https://github.com/leanprover-community/aesop", "type": "git", "subDir": null, "scope": "leanprover-community", - "rev": "1256a18522728c2eeed6109b02dd2b8f207a2a3c", + "rev": "f642a64c76df8ba9cb53dba3b919425a0c2aeaf1", "name": "aesop", "manifestFile": "lake-manifest.json", - "inputRev": "v4.22.0", + "inputRev": "master", "inherited": true, "configFile": "lakefile.toml"}, {"url": "https://github.com/leanprover-community/quote4", "type": "git", "subDir": null, "scope": "leanprover-community", - "rev": "917bfa5064b812b7fbd7112d018ea0b4def25ab3", + "rev": "b8f98e9087e02c8553945a2c5abf07cec8e798c3", "name": "Qq", "manifestFile": "lake-manifest.json", - "inputRev": "v4.22.0", + "inputRev": "master", "inherited": true, "configFile": "lakefile.toml"}, {"url": "https://github.com/leanprover-community/batteries", "type": "git", "subDir": null, "scope": "leanprover-community", - "rev": "240676e9568c254a69be94801889d4b13f3b249f", + "rev": "495c008c3e3f4fb4256ff5582ddb3abf3198026f", "name": "batteries", "manifestFile": "lake-manifest.json", - "inputRev": "v4.22.0", + "inputRev": "main", "inherited": true, "configFile": "lakefile.toml"}, {"url": "https://github.com/leanprover/lean4-cli", "type": "git", "subDir": null, "scope": "leanprover", - "rev": "c682c91d2d4dd59a7187e2ab977ac25bd1f87329", + "rev": "4f10f47646cb7d5748d6f423f4a07f98f7bbcc9e", "name": "Cli", "manifestFile": "lake-manifest.json", - "inputRev": "main", + "inputRev": "v4.28.0", "inherited": true, "configFile": "lakefile.toml"}], "name": "evmyul", diff --git a/lakefile.lean b/lakefile.lean index bdbe015..e1c2149 100644 --- a/lakefile.lean +++ b/lakefile.lean @@ -2,14 +2,14 @@ import Lake open Lake DSL System require mathlib from git - "https://github.com/leanprover-community/mathlib4.git"@"v4.22.0" + "https://github.com/leanprover-community/mathlib4.git"@"v4.28.0" package «evmyul» { moreLeanArgs := #["-DautoImplicit=false"] moreServerOptions := #[⟨`autoImplicit, false⟩] } -def cloneWithCache (pkg : NPackage _package.name) (dirname url : String) : FetchM (Job GitRepo) := do +def cloneWithCache (pkg : NPackage __name__) (dirname url : String) : FetchM (Job GitRepo) := do let repoDir : GitRepo := ⟨pkg.dir / dirname⟩ if !(← repoDir.dir.pathExists) then dbg_trace s!"Cloning: {url}"; GitRepo.clone url repoDir return pure repoDir @@ -61,7 +61,7 @@ extern_lib libleanffi pkg := do discard <| IO.Process.run {cmd := "git", args := #["submodule", "update", "--init"]} let name := nameToStaticLib "leanffi" - buildStaticLib (pkg.nativeLibDir / name) #[sha256O, keccak256, ffiO] + buildStaticLib (pkg.staticLibDir / name) #[sha256O, keccak256, ffiO] lean_lib «Conform» diff --git a/lean-toolchain b/lean-toolchain index 6ac6d4c..4c685fa 100644 --- a/lean-toolchain +++ b/lean-toolchain @@ -1 +1 @@ -leanprover/lean4:v4.22.0 +leanprover/lean4:v4.28.0