Portable, compliant, and high-performance pure Swift implementation of the Ethereum Virtual Machine (EVM)
SwiftEVM is a modular EVM implementation for the Ethereum protocol, written in pure Swift. It is designed for low-overhead integration into blockchain nodes, embedded systems, and Layer-2 solutions requiring deterministic EVM bytecode execution.
Unlike general-purpose wrappers, SwiftEVM offers a ground-up implementation of the Yellow Paper specifications, optimizing critical paths (dispatch loop, stack operations, and memory expansion) for Apple Silicon and other architectures.
The project is indexed and verified on the Swift Package Index. You can view detailed build compatibility reports across platforms and generated documentation here:
- Yellow Paper Compliant: Strict adherence to Ethereum protocol specifications.
- Zero-Copy Architecture: Optimized memory handling to minimize ARC overhead in hot execution paths.
- Platform Agnostic: Runs natively on macOS, iOS, Linux, and WebAssembly (wasm32) and many others.
- Uncompromising Reliability: The codebase maintains 100% unit test coverage. Every opcode, edge case, and gas calculation path is rigorously tested, ensuring production-grade stability and correctness rarely seen in early-stage implementations.
- Deterministic Execution: 100% reproducible state transitions.
- Modular Design: Decoupled components (Gasometer, Stack, Memory, Interpreter) allowing for custom extensions and instrumentation.
SwiftEVM is composed of specialized modules to ensure performance and correctness:
A specialized math library tailored for the EVM's 256-bit word size.
- Why not BigInt? Generic BigInt libraries introduce overhead for dynamic allocation and do not natively handle EVM-specific behaviors (e.g., specific overflow wrapping, two's complement representation for
SDIV/SMOD). - UInt128 Support: Leverages Swift 6 native
UInt128for optimized high-precision calculations.
The heart of the virtual machine.
- Interpreter: Optimized opcode dispatch loop.
- Stack: Fixed-size, high-performance LIFO structure with boundary safety checks.
- Memory: Dynamic linear memory with gas-metered expansion logic.
- Gasometer: Exact gas accounting for opcodes, intrinsic costs, and memory expansion.
Granular execution tracing for debugging and state analysis. Supports standard JSON-RPC trace formats and custom hooks for indexers.
The project targets the Ethereum specification upgrades listed below.
| Component | Status | Notes |
|---|---|---|
| Machine State | ✅ Complete | Stack, Memory, Context, Gas |
| Opcode Logic | ✅ Complete | Arithmetic, Bitwise, Control Flow, System |
| Precompiles | 🔄 In Progress | ECRecover, SHA256, RIPEMD160, Identity etc. |
| Runtime | 🛠 Active Dev | Transaction context, Block environment |
| zkEVM | 🔜 Planned | Guest program, Block validation & execution |
- ✅ Unit Testing: 100% Code Coverage enforced by CI and CodeCov.
- 🔜 Ethereum State Tests: Planned full integration with the official Ethereum Test Suite (execution-spec-tests) to guarantee pixel-perfect consensus compatibility.
Targeting compliance with the following upgrades:
- ✅ Berlin
- ✅ London
- ✅ Shanghai
- ✅ Cancun
- 🔜 Prague / Osaka (Planned)
- Swift 6.0+ (Required for
UInt128and concurrency features). - OS: macOS 14+, iOS 17+, Ubuntu 22.04+, or any environment supporting Swift 6.
Add SwiftEVM to your Package.swift dependencies:
dependencies: [
.package(url: "https://github.com/mrLSD/swift-evm.git", from: "0.5.24")
]We welcome contributions from EVM experts and systems engineers. To maintain the integrity of the consensus engine, we enforce strict quality gates:
- 100% Test Coverage: No code merges without full unit test coverage. Edge cases must be proven.
- Linting: Code must pass
swiftlintand be formatted viaswiftformat. - Performance: PRs affecting the hot loop must demonstrate no regression in benchmarks.
# Run test suite
swift test
# Run tests with prettified output
swift test | xcbeautify
# Check coverage (requires llvm-cov)
./Scripts/coverage.shSwiftEVM is released under the MIT License.