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..cb55543 100644 --- a/EvmYul.lean +++ b/EvmYul.lean @@ -28,6 +28,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..564ff34 100644 --- a/EvmYul/EVM/Gas.lean +++ b/EvmYul/EVM/Gas.lean @@ -52,7 +52,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) := [ @@ -161,22 +161,42 @@ def Ctload : ℕ := -/ def L (n : ℕ) : ℕ := n - (n / 64) -def Cnew (t : AccountAddress) (val : UInt256) (σ : AccountMap .EVM) : ℕ := +def Cnew {τ : OperationType} (t : AccountAddress) (val : UInt256) + (σ : AccountMap τ) : ℕ := if EvmYul.State.dead σ t && val != ⟨0⟩ then Gnewaccount else 0 def Cxfer (val : UInt256) : ℕ := if val != ⟨0⟩ then Gcallvalue else 0 -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) := +def CdelegatedCodeAccess + (t : AccountAddress) (σ : AccountMap .EVM) (A : Substate) : ℕ := + match σ.delegatedCodeTarget? t with + | none => 0 + | some target => Caccess target (A.addAccessedAccount t) + +def Cextra {τ : OperationType} (t r : AccountAddress) (val : UInt256) + (σ : AccountMap τ) (A : Substate) : ℕ := + let delegatedAccess := + match τ with + | .EVM => CdelegatedCodeAccess t σ A + | .Yul => 0 + Caccess t A + delegatedAccess + Cxfer val + Cnew r val σ + +def Cgascap {τ : OperationType} (t r : AccountAddress) (val g : UInt256) + (σ : AccountMap τ) (μ : MachineState) (A : Substate) := if μ.gasAvailable.toNat >= Cextra t r val σ A then min (L <| (μ.gasAvailable.toNat - Cextra t r val σ A)) g.toNat else g.toNat -def Ccallgas (t r : AccountAddress) (val g : UInt256) (σ : AccountMap .EVM) (μ : MachineState) (A : Substate) : ℕ := +def Ccallgas {τ : OperationType} (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 @@ -184,7 +204,8 @@ def Ccallgas (t r : AccountAddress) (val g : UInt256) (σ : AccountMap .EVM) (μ /-- NB Assumes stack coherence. -/ -def Ccall (t r : AccountAddress) (val g : UInt256) (σ : AccountMap .EVM) (μ : MachineState) (A : Substate) : ℕ := +def Ccall {τ : OperationType} (t r : AccountAddress) (val g : UInt256) + (σ : AccountMap τ) (μ : MachineState) (A : Substate) : ℕ := Cgascap t r val g σ μ A + Cextra t r val σ A /-- 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..48550b4 --- /dev/null +++ b/EvmYul/EVM/PostFusakaTests.lean @@ -0,0 +1,98 @@ +import EvmYul.EVM.Gas +import EvmYul.EVM.Instr +import EvmYul.EVM.Semantics +import EvmYul.Maps.AccountMap +import EvmYul.State.Account + +namespace EvmYul + +open Operation + +private def bytes (xs : List UInt8) : ByteArray := List.toByteArray xs + +private def wordBEByte (b : UInt8) : ByteArray := + bytes (List.replicate 31 0 ++ [b]) + +private def truncatedPush2Code : ByteArray := bytes [0x61, 0xab] + +private def modexpWideBaseCalldata : ByteArray := + wordBEByte 33 ++ wordBEByte 1 ++ wordBEByte 2 ++ + bytes ([1] ++ List.replicate 32 0 ++ [1, 1, 1]) + +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 + match EVM.decode truncatedPush2Code (UInt256.ofNat 0) with + | some (.PUSH2, some (value, 2)) => value == UInt256.ofNat 0xab00 + | _ => false + +#guard + let base := nat_of_slice modexpWideBaseCalldata 96 33 + let exp := nat_of_slice modexpWideBaseCalldata (96 + 33) 1 + let modulus := nat_of_slice modexpWideBaseCalldata (96 + 33 + 1) 2 + expMod modulus base exp == 1 + +#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..c1ffdfc 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 @@ -137,17 +137,19 @@ def expModAux (m : ℕ) (a : ℕ) (c : ℕ) : ℕ → ℕ else expModAux m (a % m) (c * c % m) (n / 2) -def expMod (m : ℕ) (b : UInt256) (n : ℕ) : ℕ := expModAux m 1 b.toNat n +def expMod (m : ℕ) (b : ℕ) (n : ℕ) : ℕ := expModAux m 1 b 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 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..2466186 100644 --- a/EvmYul/EVM/Semantics.lean +++ b/EvmYul/EVM/Semantics.lean @@ -76,6 +76,10 @@ def argOnNBytesOfInstr : Operation .EVM → ℕ def N (pc : UInt256) (instr : Operation .EVM) := pc + ⟨1⟩ + .ofNat (argOnNBytesOfInstr instr) +def codeBytesWithRightPadding (arr : ByteArray) (start len : ℕ) : ByteArray := + let read := arr.extract' start (start + len) + ⟨read.data ++ Array.replicate (len - read.size) 0⟩ + /-- Returns the instruction from `arr` at `pc` assuming it is valid. @@ -89,20 +93,44 @@ def decode (arr : ByteArray) (pc : UInt256) : instr, if argWidth == 0 then .none - else .some (EvmYul.uInt256OfByteArray (arr.extract' pc.toNat.succ (pc.toNat.succ + argWidth)), argWidth) + else .some (EvmYul.uInt256OfByteArray (codeBytesWithRightPadding arr pc.toNat.succ argWidth), argWidth) ) def fetchInstr (I : EvmYul.ExecutionEnv .EVM) (pc : UInt256) : Except EVM.ExecutionException (Operation .EVM × Option (UInt256 × Nat)) := decode I.code pc |>.option (.error .StackUnderflow) Except.ok -partial def D_J_aux (c : ByteArray) (i : UInt256) (result : Array UInt256) : Array UInt256 := - match c.get? i.toNat >>= EvmYul.EVM.parseInstr with - | none => result - | some cᵢ => D_J_aux c (N i cᵢ) (if cᵢ = .JUMPDEST then result.push i else result) +def D_J_aux (c : ByteArray) (n : Nat) (result : Array UInt256) : Array UInt256 := + if h : n < c.size then + match EvmYul.EVM.parseInstr c[n] with + | none => result + | some cᵢ => + D_J_aux c (n + 1 + argOnNBytesOfInstr cᵢ) + (if cᵢ = .JUMPDEST then result.push (.ofNat n) else result) + else result + termination_by c.size - n + decreasing_by omega def D_J (c : ByteArray) (i : UInt256) : Array UInt256 := - D_J_aux c i #[] + D_J_aux c i.toNat #[] + +theorem D_J_aux_out_of_bounds (c : ByteArray) (n : Nat) (result : Array UInt256) + (h : ¬ n < c.size) : D_J_aux c n result = result := by + unfold D_J_aux; simp [h] + +theorem D_J_aux_step (c : ByteArray) (n : Nat) (result : Array UInt256) + (h : n < c.size) : + D_J_aux c n result = + match EvmYul.EVM.parseInstr c[n] with + | none => result + | some cᵢ => + D_J_aux c (n + 1 + argOnNBytesOfInstr cᵢ) + (if cᵢ = .JUMPDEST then result.push (.ofNat n) else result) := by + conv_lhs => rw [D_J_aux] + rw [dif_pos h] + +theorem D_J_def (c : ByteArray) (i : UInt256) : D_J c i = D_J_aux c i.toNat #[] := + rfl private def BitVec.ofFn {k} (x : Fin k → Bool) : BitVec k := BitVec.ofNat k (natOfBools (Vector.ofFn x)) @@ -136,6 +164,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) @@ -161,6 +193,7 @@ def call (fuel : Nat) -- 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 +204,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ₒ, ..) @@ -189,11 +225,9 @@ def call (fuel : Nat) -- 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 +238,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 +290,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ₒ @@ -296,6 +328,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 +360,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ₒ @@ -354,6 +389,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) @@ -529,6 +565,7 @@ def Ξ -- Type `Ξ` using `\GX` or `\Xi` (blocks : ProcessedBlocks) (σ : AccountMap .EVM) (σ₀ : AccountMap .EVM) + (chainContext : ChildFrameChainContext) (g : UInt256) (A : Substate) (I : ExecutionEnv .EVM) @@ -545,6 +582,8 @@ def Ξ -- Type `Ξ` using `\GX` or `\Xi` { defState with accountMap := σ σ₀ := σ₀ + totalGasUsedInBlock := chainContext.totalGasUsedInBlock + transactionReceipts := chainContext.transactionReceipts executionEnv := I substate := A createdAccounts := createdAccounts @@ -567,6 +606,7 @@ def Lambda (blocks : ProcessedBlocks) (σ : AccountMap .EVM) (σ₀ : AccountMap .EVM) + (chainContext : ChildFrameChainContext) (A : Substate) (s : AccountAddress) -- sender (o : AccountAddress) -- original transactor @@ -651,7 +691,8 @@ def Lambda , perm := w , blobVersionedHashes := blobVersionedHashes } - 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) @@ -695,6 +736,75 @@ 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) : 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 + } + /-- Message cal `σ` - evm state @@ -721,6 +831,7 @@ def Θ (fuel : Nat) (blocks : ProcessedBlocks) (σ : AccountMap .EVM) (σ₀ : AccountMap .EVM) + (chainContext : ChildFrameChainContext) (A : Substate) (s : AccountAddress) (o : AccountAddress) @@ -742,61 +853,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 -- 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 +875,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) @@ -875,6 +944,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 +960,8 @@ def Υ (fuel : ℕ) blocks σ₀ σ₀ + { totalGasUsedInBlock := 0 + transactionReceipts := #[] } AStar S_T S_T @@ -911,6 +986,8 @@ def Υ (fuel : ℕ) blocks σ₀ σ₀ + { totalGasUsedInBlock := 0 + transactionReceipts := #[] } AStar S_T S_T 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..6b58b01 100644 --- a/EvmYul/FFI/ffi.lean +++ b/EvmYul/FFI/ffi.lean @@ -1,3 +1,6 @@ +import Init.Data.Array.Lemmas +import EvmYul.SpongeHash.Keccak256 + namespace ffi @[extern "sha256"] @@ -15,10 +18,72 @@ 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 [] + +/-- Keccak-256 of the first `len` bytes of `input`. + +No longer `opaque`: the logical meaning is the pure sponge implementation in +`EvmYul.SpongeHash.Keccak256`, so `KEC` is provable-about; the `@[extern]` +attribute keeps compiled execution on the native `keccak256` symbol +(byte-parity with the pure body is asserted by downstream witnesses). -/ @[extern "keccak256"] -opaque keccak256 (input : @& ByteArray) (len : USize) : ByteArray +def keccak256 (input : @& ByteArray) (len : USize) : ByteArray := + EvmYul.SpongeHash.Keccak256.keccak256 + (ByteArray.mk (input.data.extract 0 len.toNat)) def KECCAK256 (d : ByteArray) : Except String ByteArray := pure <| keccak256 d d.size.toUSize 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/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/SpongeHash/Keccak256.lean b/EvmYul/SpongeHash/Keccak256.lean index 2591e96..d05a1e6 100644 --- a/EvmYul/SpongeHash/Keccak256.lean +++ b/EvmYul/SpongeHash/Keccak256.lean @@ -1,5 +1,214 @@ /- - Switch to our Cryptographic repository once open sourced for equational version. +Pure, total Keccak-256 (restored). - Use FFI in the meanwhile. +This is the equational implementation this file's stub always promised: a +plain Lean sponge over `Nat` lanes, no `partial`, no FFI. The compiled/native +path may still use the `keccak256` C symbol via `EvmYul.ffi` — whose `def` +body is THIS function, so the logical meaning is pure and provable while +execution stays fast. + +Provenance: verbatim port of the implementation maintained (corpus-validated +against solc/Forge fixtures and byte-parity-checked against the native +`keccak256` symbol) in the solidity-lean project, itself a relocation of the +former upstream implementation of this module. -/ + +namespace EvmYul +namespace SpongeHash +namespace Keccak256 + +abbrev Byte := Nat + +def word64 : Nat := 18446744073709551616 +def mask64 : Nat := 18446744073709551615 + +def u64 (n : Nat) : Nat := + n % word64 + +def bxor (a b : Nat) : Nat := + u64 (Nat.xor (u64 a) (u64 b)) + +def band (a b : Nat) : Nat := + u64 (Nat.land (u64 a) (u64 b)) + +def bnot (a : Nat) : Nat := + mask64 - u64 a + +def rotl64 (x r : Nat) : Nat := + let x := u64 x + if r % 64 == 0 then + x + else + u64 (((x * (2 ^ (r % 64))) % word64) + (x / (2 ^ (64 - r % 64)))) + +def getD (xs : List Nat) (i : Nat) : Nat := + xs.getD i 0 + +def setD : List Nat -> Nat -> Nat -> List Nat + | [], _, _ => [] + | _ :: xs, 0, value => value :: xs + | x :: xs, i + 1, value => x :: setD xs i value + +def laneIndex (x y : Nat) : Nat := + (x % 5) + 5 * (y % 5) + +def lane (st : List Nat) (x y : Nat) : Nat := + getD st (laneIndex x y) + +def xorList : List Nat -> Nat + | [] => 0 + | x :: xs => bxor x (xorList xs) + +def columnXor (st : List Nat) (x : Nat) : Nat := + xorList ((List.range 5).map fun y => lane st x y) + +def rhoOffset (x y : Nat) : Nat := + match x % 5, y % 5 with + | 0, 0 => 0 | 0, 1 => 36 | 0, 2 => 3 | 0, 3 => 41 | 0, _ => 18 + | 1, 0 => 1 | 1, 1 => 44 | 1, 2 => 10 | 1, 3 => 45 | 1, _ => 2 + | 2, 0 => 62 | 2, 1 => 6 | 2, 2 => 43 | 2, 3 => 15 | 2, _ => 61 + | 3, 0 => 28 | 3, 1 => 55 | 3, 2 => 25 | 3, 3 => 21 | 3, _ => 56 + | _, 0 => 27 | _, 1 => 20 | _, 2 => 39 | _, 3 => 8 | _, _ => 14 + +def roundConstants : List Nat := + [0x0000000000000001, 0x0000000000008082, + 0x800000000000808A, 0x8000000080008000, + 0x000000000000808B, 0x0000000080000001, + 0x8000000080008081, 0x8000000000008009, + 0x000000000000008A, 0x0000000000000088, + 0x0000000080008009, 0x000000008000000A, + 0x000000008000808B, 0x800000000000008B, + 0x8000000000008089, 0x8000000000008003, + 0x8000000000008002, 0x8000000000000080, + 0x000000000000800A, 0x800000008000000A, + 0x8000000080008081, 0x8000000000008080, + 0x0000000080000001, 0x8000000080008008] + +def theta (st : List Nat) : List Nat := + let c := (List.range 5).map fun x => columnXor st x + let d := (List.range 5).map fun x => + bxor (getD c ((x + 4) % 5)) (rotl64 (getD c ((x + 1) % 5)) 1) + (List.range 25).map fun i => + bxor (getD st i) (getD d (i % 5)) + +def rhoPiSet (a : List Nat) (x y : Nat) (b : List Nat) : List Nat := + let dest := y + 5 * ((2 * x + 3 * y) % 5) + setD b dest (rotl64 (lane a x y) (rhoOffset x y)) + +def rhoPi (st : List Nat) : List Nat := + Id.run do + let mut b := List.replicate 25 0 + for y in List.range 5 do + for x in List.range 5 do + b := rhoPiSet st x y b + pure b + +def chi (b : List Nat) : List Nat := + (List.range 25).map fun i => + let x := i % 5 + let y := i / 5 + bxor (lane b x y) (band (bnot (lane b (x + 1) y)) (lane b (x + 2) y)) + +def iota (st : List Nat) (rc : Nat) : List Nat := + setD st 0 (bxor (getD st 0) rc) + +def round (st : List Nat) (rc : Nat) : List Nat := + iota (chi (rhoPi (theta st))) rc + +def permute : List Nat -> List Nat -> List Nat + | st, [] => st + | st, rc :: rest => permute (round st rc) rest + +def keccakF1600 (st : List Nat) : List Nat := + permute st roundConstants + +def byteShift (i : Nat) : Nat := + 2 ^ (8 * (i % 8)) + +def absorbByte (st : List Nat) (i byte : Nat) : List Nat := + let li := i / 8 + let value := bxor (getD st li) ((byte % 256) * byteShift i) + setD st li value + +def absorbBlockFrom : List Nat -> Nat -> List Byte -> List Nat + | st, _, [] => st + | st, i, byte :: bytes => + absorbBlockFrom (absorbByte st i byte) (i + 1) bytes + +def absorbBlock (st : List Nat) (block : List Byte) : List Nat := + keccakF1600 (absorbBlockFrom st 0 block) + +def rateBytes : Nat := 136 + +def padKeccak (msg : List Byte) : List Byte := + let rem := msg.length % rateBytes + let padLen := rateBytes - rem + if padLen == 1 then + msg ++ [0x81] + else + msg ++ [0x01] ++ List.replicate (padLen - 2) 0 ++ [0x80] + +def chunksOfWithFuel : Nat -> Nat -> List Byte -> List (List Byte) + | 0, _, _ => [] + | _, _, [] => [] + | fuel + 1, n, bytes => + bytes.take n :: chunksOfWithFuel fuel n (bytes.drop n) + +def chunksOf (n : Nat) (bytes : List Byte) : List (List Byte) := + if n == 0 then + [] + else + chunksOfWithFuel (bytes.length + 1) n bytes + +def absorbBlocks : List Nat -> List (List Byte) -> List Nat + | st, [] => st + | st, block :: blocks => absorbBlocks (absorbBlock st block) blocks + +def laneByte (lane : Nat) (i : Nat) : Byte := + (lane / (2 ^ (8 * i))) % 256 + +def laneBytesLE (lane : Nat) : List Byte := + (List.range 8).map fun i => laneByte lane i + +def squeezeBytes (st : List Nat) : List Byte := + ((List.range 25).flatMap fun i => laneBytesLE (getD st i)).take 32 + +def bytesOfString (s : String) : List Byte := + s.toUTF8.toList.map UInt8.toNat + +def keccak256Bytes (bytes : List Byte) : List Byte := + squeezeBytes + (absorbBlocks (List.replicate 25 0) + (chunksOf rateBytes (padKeccak bytes))) + +def keccak256String (s : String) : List Byte := + keccak256Bytes (bytesOfString s) + +def wordFromBytesBE : List Byte -> Nat + | [] => 0 + | byte :: bytes => + (byte % 256) * (2 ^ (8 * bytes.length)) + wordFromBytesBE bytes + +def selector4 (signature : String) : Nat := + wordFromBytesBE ((keccak256String signature).take 4) + +def digestWord (signature : String) : Nat := + wordFromBytesBE (keccak256String signature) + +def transferSelectorExample : Bool := + selector4 "transfer(address,uint256)" == 0xa9059cbb + +def balanceOfSelectorExample : Bool := + selector4 "balanceOf(address)" == 0x70a08231 + +def setSelectorExample : Bool := + selector4 "set(uint256)" == 0x60fe47b1 + + +/-- ByteArray convenience wrapper: hash the whole array. -/ +def keccak256 (input : ByteArray) : ByteArray := + ByteArray.mk (Array.mk ((keccak256Bytes (input.toList.map UInt8.toNat)).map UInt8.ofNat)) + +end Keccak256 +end SpongeHash +end EvmYul 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..cf5b08a 100644 --- a/EvmYul/State/ExecutionEnv.lean +++ b/EvmYul/State/ExecutionEnv.lean @@ -30,6 +30,7 @@ structure ExecutionEnv (τ : OperationType) where depth : ℕ perm : Bool blobVersionedHashes : List ByteArray + codeBytes : ByteArray := default deriving BEq, Inhabited, Repr def prevRandao {τ} (e : ExecutionEnv τ) : UInt256 := diff --git a/EvmYul/StateOps.lean b/EvmYul/StateOps.lean index 1e81e71..b094b61 100644 --- a/EvmYul/StateOps.lean +++ b/EvmYul/StateOps.lean @@ -48,6 +48,11 @@ def balance {τ} (self : State τ) (k : UInt256) : State τ × UInt256 := let addr := AccountAddress.ofUInt256 k (self.addAccessedAccount addr, self.accountMap.find? addr |>.elim ⟨0⟩ (·.balance)) +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 +70,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 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..2a1a475 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 @@ -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..3e58a95 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,98 @@ 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⟩]) + +/-- +Commit the state of a *successful* external sub-call `s₂` back into the +caller's state `s₀`. + +The `accountMap.isEmpty` guards below mirror the Yellow Paper's `σ'' = ∅` +abort sentinel (eqs. (127)/(129)): an empty post-call account map denotes +"no resulting state", in which case the caller's `accountMap`/`substate` +are kept. On a genuinely successful sub-call this branch is expected to be +unreachable — the child starts from the caller's (non-empty, post-transfer) +account map and its result always contains the executing account — so the +guard is a defensive structural mirror of the EVM-side merge, not a +behavioral rule real executions depend on. `createdAccounts` is taken from +the child unconditionally; on the (unreachable) sentinel branch this is +asymmetric with `accountMap`/`substate`, which is harmless precisely +because that branch cannot fire on a successful call. +-/ +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 +143,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 +262,130 @@ 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.Ccallgas 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.Ccallgas 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 +393,78 @@ 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.Ccallgas 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 +472,89 @@ 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.Ccallgas 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 +601,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 +618,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 +651,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 +676,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 +701,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 +741,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 +778,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 +796,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/FORK_NOTES.md b/FORK_NOTES.md new file mode 100644 index 0000000..35ed052 --- /dev/null +++ b/FORK_NOTES.md @@ -0,0 +1,128 @@ +# Fork notes: how this fork differs from NethermindEth/EVMYulLean + +This fork of [NethermindEth/EVMYulLean](https://github.com/NethermindEth/EVMYulLean) +is the pinned semantic trust anchor for the +[Solidus](https://github.com/paradigmxyz/solidus) verified Yul → EVM compiler: +Solidus's correctness theorem interprets Yul source with this repository's Yul +interpreter and EVM bytecode with this repository's EVM semantics. Because any +edit to this repository moves the goalposts of that theorem, this file +discloses every way the fork diverges from upstream and which differences sit +inside the trusted base. The fork range is upstream `047f630..` this branch +(28 commits as of the `b08573c` pin). + +The one-line summary: the fork adds **zero axioms and zero sorries**, removes +no upstream checks or tests, and its semantic changes either fix upstream +interpreter bugs (bringing behavior *closer* to the official Yul specification +and the Yellow Paper/EIPs) or implement previously-stubbed features. The items +below are the ones a careful auditor should know about anyway. + +## 1. Shared Yul/EVM specification surface + +Several fork commits make the Yul interpreter and the EVM semantics consume +the *same* definitions instead of two parallel copies: + +- `runPrecompiledContract` and the `Ξ_*` precompile implementations + (`EvmYul/EVM/PrecompiledContracts.lean`); +- `PrecompiledContract` / `ofAddress?` precompile-address classification + (`EvmYul/State/Account.lean`); +- `toExecute` address → (precompile | code) dispatch + (`EvmYul/Maps/AccountMap.lean`); the `.EVM` branch additionally resolves + EIP-7702 delegation designators, the `.Yul` branch does not; +- `MachineState.finishExternalCall` — post-call output copy, returndata, + memory-expansion accounting (`EvmYul/MachineStateOps.lean`); +- `EVM.Ccallgas` and its helpers — the 63/64 forwarding rule and call stipend + (`EvmYul/EVM/Gas.lean`); +- `emptyAccount` / `dead` (EIP-161) and the `extCodeSize` / `extCodeHash` / + `extCodeCopy'` external-code-inspection skeleton. + +This is deliberate one-spec/two-consumers factoring, and each shared +definition was checked against the Yellow Paper rule it implements. But be +aware of the consequence: **for exactly these computations, a Yul↔EVM +equivalence theorem holds by shared definition, not by independent proof.** +They are part of the trusted specification, like every other line of this +repository. + +Conversely, the value-transfer preludes are intentionally *duplicated*, not +shared (`thetaCallTransfer` on the EVM side vs. `callTransferAccountMap?` on +the Yul side), so the theorem does independently relate them; keep them in +sync when editing. + +## 2. The `keccak256` native/pure boundary + +Upstream declared `keccak256` as an `opaque` constant with `@[extern]` — no +logical content at all. This fork gives it a total pure-Lean Keccak-f[1600] +sponge as its logical body (`EvmYul/SpongeHash/Keccak256.lean`) while keeping +`@[extern "keccak256"]` for compiled execution (`EvmYul/FFI/ffi.lean`). + +Consequence: Lean proofs constrain the pure sponge; the compiled binary runs +the native C implementation. Byte-parity between the two is asserted by +runtime self-tests and downstream test witnesses, **not proven in-tree**. A +divergence could make concrete executions disagree with the theorem's model, +but cannot make the theorem false. The same pattern applies (trivially) to +`ByteArray.zeroes` / `memset_zero`. `sha256` and `blake2compressb64` remain +opaque externs as upstream had them. + +## 3. Hardfork pinning: Fusaka MODEXP, no version gates + +The MODEXP precompile implements the Fusaka rules unconditionally — EIP-7883 +repricing (min 500 gas, revised complexity formula) and EIP-7823 input-length +limits (1024-byte base/exponent/modulus caps). Pre-Fusaka MODEXP behavior is +**not** modeled. Likewise CLZ (EIP-7939) and EIP-7702 delegation lookup are +present without fork gating on this repository's side (downstream consumers +apply their own version gating). + +## 4. Yul-side gas is threaded, not charged + +The Yul interpreter forwards call gas to child frames (`EVM.Ccallgas` → +`freshExternalCall`) so that sub-call semantics are frame-accurate, but Yul +execution itself does not charge gas per operation. Gas-sensitive claims are +the EVM side's business. + +## 5. The `isEmpty` merge sentinel + +`restoreSuccessfulContractCallState` (and the precompile analogue) keeps the +caller's `accountMap`/`substate` when a successful child returns an *empty* +account map. This mirrors the Yellow Paper's `σ'' = ∅` abort sentinel and is +expected to be unreachable on genuinely successful calls; see the docstring +in `EvmYul/Yul/Interpreter.lean` for the invariant and the (harmless) +`createdAccounts` asymmetry. + +## 6. Minor mechanical notes + +- Two `native_decide` uses in `EvmYul/UInt256.lean` prove the fixed numeric + fact `2 ^ 8 = UInt8.size` inside private byte-conversion lemmas; they add + `Lean.ofReduceBool` to those two lemmas' axiom footprints only. +- The Lean toolchain was bumped v4.22.0 → v4.28.0 with matching official + Mathlib/batteries/etc. bumps; no dependency was replaced by a fork, and the + lake manifest pins exact commit hashes. +- The new `EvmYul/Yul/YulSemanticsTests/Main.lean` suite encodes + official-Yul-spec outcomes (switch selects exactly the matching case, + callee `revert` returns 0 to the caller, `selfdestruct` halts, block + scoping/no-shadowing, zero-initialized return variables); several of these + tests fail against the upstream interpreter, which is why the corresponding + interpreter fixes exist. + +## Summary of semantic changes vs. upstream + +Yul interpreter fixes (each toward the official Yul spec): switch executes +exactly the matching case with no side effects from non-taken branches; +omitted switch default is a no-op (upstream synthesized a spurious `break`); +callee `revert` rolls back the child's state, delivers revert data, and +returns 0 to the *continuing* caller (upstream aborted the caller); external +CALL/STATICCALL executes the callee's code (upstream ran the caller's); +`selfdestruct` halts the current context; `leave` inside a for-loop body/post +exits the function; block-scoped variables, no shadowing, +assignment-requires-declaration, zero-initialized return variables; +RETURNDATACOPY is bounds-checked (EIP-211); EXTCODESIZE/COPY/HASH implemented +(upstream stubbed them as errors); precompile dispatch and depth-limit/value +checks aligned with the EVM side; EIP-2929 target warming applied before +dispatch and preserved across child revert. + +EVM-side fixes/additions (each checked against the cited rule): PUSH +immediates past end-of-code zero-pad (Yellow Paper); MODEXP no longer +truncates >32-byte bases; Fusaka MODEXP (EIP-7883/EIP-7823); CLZ (EIP-7939); +EIP-7702 delegation lookup with EXTCODE* observing the raw designator; +EIP-6780 selfdestruct refactor (behavior preserved); RETURNDATACOPY bounds +(EIP-211); `D_J` jump-destination analysis totalized by structural recursion +on remaining code size (bit-identical behavior, no fuel that can run out); +keccak256 given a genuine pure Keccak-f[1600] logical definition. diff --git a/README.md b/README.md index fbc60f0..18067bc 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,14 @@ Where applicable, the underlying EVM primops are used directly by the Yul model. Everything here is work in progress and is subject to change therefore. +> This is a fork of +> [NethermindEth/EVMYulLean](https://github.com/NethermindEth/EVMYulLean) used +> as the pinned semantic trust anchor for the +> [Solidus](https://github.com/paradigmxyz/solidus) verified compiler. Every +> divergence from upstream — including which definitions are shared between +> the Yul and EVM models and therefore sit inside the trusted base — is +> disclosed in [`FORK_NOTES.md`](FORK_NOTES.md). + # Requirements - Python packages: coincurve, typing-extensions, pycryptodome, eth-typing, py-ecc @@ -99,4 +107,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