The Ethereum Virtual Machine (EVM)
The Ethereum Virtual Machine (EVM) is a decentralized virtual environment that executes code consistently and securely across all Ethereum nodes. Nodes run the EVM to execute smart contracts, using “gas” to measure the computational effort required for operations, ensuring efficient resource allocation and network security.
Prerequisites
A basic familiarity with common computer science terminology, such as bytes, memory, and stacks, is necessary to understand the EVM. It is also helpful to be comfortable with cryptography and blockchain concepts like hash functions and Merkle trees.
From Ledger to State Machine
Blockchains like Bitcoin are often described as ‘distributed ledgers,’ maintaining a record of activity that adheres to a set of rules governing modifications. For example, a Bitcoin address cannot spend more Bitcoin than it has received. These rules underpin all transactions on Bitcoin and many other blockchains.
While Ethereum has its own native cryptocurrency (Ether) with similar rules, it also supports a more powerful feature: smart contracts. To understand this, Ethereum is better described as a distributed state machine. Ethereum’s state is a large data structure that holds not only all accounts and balances but also a machine state that can change from block to block according to predefined rules, executing arbitrary machine code. The EVM defines the rules for these state changes.
What is the Difference Between EVM and Ethereum?
Ethereum is a platform that allows people to create and run applications on a decentralized network of computers. It has its own digital currency called Ether (ETH), which is used to pay for things on the network. Think of Ethereum as a giant, shared computer that everyone can use to build and run their apps without a central authority controlling it.
The Ethereum Virtual Machine is like the engine inside this giant computer. It makes sure that all the applications and smart contracts (automated agreements) run the same way on every computer in the network. To keep things fair and efficient, the EVM uses a system called “gas” to measure how much computing power is needed for each task, ensuring that resources aren’t wasted.
In simple terms, Ethereum is the entire platform that lets you build and use decentralized apps and contracts, while the Ethereum Virtual Machine is part of the platform that runs these apps and contracts, making sure everything works smoothly and consistently across all the computers in the network. This is what is the difference between EVM and Ethereum.
The Ethereum State Transition Function
The EVM behaves like a mathematical function: given an input, it produces a deterministic output. This can be formally described as a state transition function:
\[ Y(S, T) = S’ \]
Given an old valid state \( S \) and a new set of valid transactions \( T \), the Ethereum state transition function \( Y(S, T) \) produces a new valid output state \( S’ \).
State
In Ethereum, the state is an enormous data structure called a modified Merkle Patricia Trie, which links all accounts by hashes and reduces them to a single root hash stored on the blockchain.
Transactions
Transactions are cryptographically signed instructions from accounts, classified into two types: those resulting in message calls and those resulting in contract creation. Contract creation generates a new contract account containing compiled smart contract bytecode. When another account makes a message call to this contract, it executes its bytecode.
EVM Instructions
The EVM operates as a stack machine with a depth of 1024 items, each a 256-bit word, chosen for compatibility with 256-bit cryptography (such as Keccak-256 hashes or secp256k1 signatures). During execution, the EVM maintains transient memory (a word-addressed byte array) that does not persist between transactions. Contracts have a Merkle Patricia storage trie (a word-addressable word array) associated with the account and part of the global state.
Compiled smart contract bytecode executes as Ethereum Virtual Machine opcodes, performing standard stack operations like XOR, AND, ADD, SUB, etc., and blockchain-specific operations like ADDRESS, BALANCE, and BLOCKHASH.
EVM Implementations
All EVM implementations must adhere to the Ethereum Yellow Paper specifications. Over Ethereum’s history, the EVM has undergone several revisions, resulting in multiple implementations in various programming languages, including:
- Py-EVM (Python)
- evmone (C++)
- ethereumjs-vm (JavaScript)
- eEVM (C++)
- revm (Rust)
These implementations ensure that the EVM remains consistent and secure across different platforms.