The Ethereum Virtual Machine

An overview

solidititty.eth
6 min readDec 2, 2022

The Ethereum Virtual Machine (EVM) is the runtime environment for smart contracts in Ethereum. It is a decentralized and deterministic platform that allows developers to write and deploy smart contracts on the Ethereum network. In this article, we will explore the technical details of the EVM, including its architecture, instruction set, and gas model.

The Stack-Based Architecture of the EVM

The EVM is a stack-based virtual machine, which means that it uses a pushdown stack to store data and execute instructions. This is different from the register-based architecture used by most modern CPUs, where data is stored in registers and instructions operate on the data in these registers. The stack-based architecture of the EVM has some advantages for smart contract execution, including simplicity and flexibility.

In a stack-based architecture, all data is stored in a stack, which is a last-in, first-out (LIFO) data structure. This means that the last item that was pushed onto the stack is the first item that can be popped off of the stack. This allows for easy manipulation of data, as all operations on the stack can be performed with simple push and pop instructions.

The stack-based architecture also simplifies the process of implementing complex operations like loops and conditional statements. In a register-based architecture, these operations require special instructions and additional registers to store control flow information. In a stack-based architecture, these operations can be implemented using the same push and pop instructions that are used for manipulating data, which makes the instruction set of the EVM simpler and more regular, bringing us to our next point.

The Instruction Set of the EVM

The EVM has a simple instruction set, which consists of approximately 200 opcodes that are used to manipulate data and control the flow of execution. These opcodes can be grouped into categories based on their function, including arithmetic, logical, and cryptographic operations, as well as operations for accessing and modifying the state of the EVM.

The arithmetic operations of the EVM include basic arithmetic operations like addition, subtraction, multiplication, and division, as well as more advanced operations like modular exponentiation and elliptic curve multiplication. These operations are used to perform calculations on numeric data, and they are essential for implementing smart contracts that involve financial transactions or other types of numeric computation.

The logical operations of the EVM include bitwise operations like AND, OR, and XOR, as well as conditional operations like ISZERO and LT. These operations are used to perform logical comparisons and conditional branching, and they are essential for implementing control flow in smart contracts. For example, a smart contract might use the ISZERO operation to check if a certain condition is met, and then use the LT operation to compare two values and decide which branch of execution to follow.

The cryptographic operations of the EVM include operations for hashing and signature verification, which are essential for implementing secure smart contracts. The EVM includes support for several cryptographic algorithms, including the SHA-256 hash function and the Elliptic Curve Digital Signature Algorithm (ECDSA). These operations are used to securely hash and sign data, and they are essential for implementing smart contracts that involve sensitive information or require strong security guarantees.

The state access and modification operations of the EVM include operations for reading and writing data to the Ethereum blockchain. These operations are used to access and modify the state of the EVM, which includes the storage, balance, and code of smart contracts. These operations are essential for implementing smart contracts that interact with the Ethereum network, as they allow contracts to read and write data to the blockchain and to call other contracts.

The Gas Model of the EVM

One of the key features of the EVM is its gas model, which is used to limit the amount of computation that can be performed by a smart contract. Gas is a measure of the computational effort required to execute an operation in the EVM, and it is paid for in ether by the contract’s creator. This ensures that the EVM remains scalable and secure, as it prevents contracts from consuming unlimited resources and potentially causing network congestion. For example, operations that require complex calculations or access sensitive data are typically more expensive in terms of gas than simple operations like arithmetic or logical operations.

When a smart contract is executed in the EVM, the contract’s creator must specify a gas limit, which is the maximum amount of gas that the contract is allowed to consume. This gas limit is included in the transaction that is used to deploy the contract, and it is used by the EVM to enforce the gas limit and prevent the contract from consuming more gas than is allowed.

If a contract reaches its gas limit during execution, the EVM will stop executing the contract and revert any changes that the contract made to the state of the EVM. This ensures that the contract cannot consume unlimited resources and cause network congestion, but it also means that the contract may not always be able to complete its intended execution.

The gas model of the EVM is designed to balance scalability and security, as it allows the Ethereum network to support a large number of transactions without sacrificing security. It is also designed to be flexible, as the cost of operations can be adjusted over time to reflect changes in the network and in the complexity of smart contracts.

The Support for Precompiles in the EVM

The EVM also includes support for advanced features like precompiles, which are specialized opcodes that are implemented in the Ethereum blockchain itself. Precompiles are used to perform complex operations like elliptic curve arithmetic and hash functions, and they can be called by smart contracts to perform these operations efficiently and securely.

Precompiles are implemented in the Ethereum blockchain itself, which means that they are executed by the Ethereum nodes that maintain the blockchain rather than by the EVM. This allows precompiles to perform complex operations more efficiently than if they were implemented in the EVM, as the nodes that maintain the blockchain have more computational resources than the EVM.

Precompiles are also designed to be secure, as they are implemented in the Ethereum blockchain and are subject to the same security guarantees as the rest of the Ethereum network. This means that precompiles can be used to perform sensitive operations like elliptic curve arithmetic and hash functions without sacrificing security.

Smart contracts can call precompiles using special opcodes that are included in the EVM instruction set. These opcodes allow contracts to pass data to precompiles and receive the results of the precompile’s execution. This allows contracts to use precompiles to perform complex operations without having to implement these operations themselves.

The availability of precompiles in the EVM is important for a couple of key reasons. It allows contracts to perform complex operations more efficiently, as precompiles can execute these operations faster and with less gas than if they were implemented in the EVM. Second, it allows contracts to access advanced functionality that would not be possible with the basic instruction set of the EVM.

Conclusion

Overall, the EVM is a critical component of the Ethereum ecosystem, as it provides the runtime environment for smart contracts and enables developers to build and deploy decentralized applications on the Ethereum network. The EVM’s stack-based architecture, simple instruction set, and gas model are key features that make it well-suited for smart contract execution, and its support for precompiles allows for the implementation of advanced functionality.

It remains a versatile and powerful platform for smart contract development, and it continues to evolve and improve as the Ethereum ecosystem grows and changes. Whether you are a seasoned developer or a newcomer to the world of blockchain, the EVM offers a rich and dynamic environment for building decentralized applications and unleashing the full potential of the Ethereum network.

If you enjoyed this article and want to learn more about the Ethereum developer ecosystem, be sure to follow me on Twitter at @0xSolidititty for regular updates and insights.

--

--

solidititty.eth
0 Followers

Solidity and web3 developer sharing my thoughts and knowledge.