This repository is a collection of exercises and resources to learn the programming language Yul. Understanding Yul is key to become an advanced Solidity developer or an Ethereum Security Researcher.
The Ethereum Virtual Machine (EVM) is the core component of the Ethereum network. This component allows the execution of smart contracts written in Solidity. After writing the contract, it is compiled into bytecode and deployed to the EVM. In this article you can find an introduction to the EVM.
Solidity Assembly refers to a low-level programming language that allows developers to write code at a level closer to the EVM itself. It provides a more granular control over the execution of smart contracts, allowing for optimizations and customization that may not be achievable through higher-level Solidity code alone. The language used for inline assembly in Solidity is known as Yul.
Yul is an intermediate language that compiles to EVM bytecode. It is designed to be a low-level language that gives developers a high degree of control over the execution of their smart contracts. It can be used in stand-alone mode and for inline assembly inside Solidity.
Yul is designed to be a low-level stack-based language, providing developers with the ability to write more optimized and efficient code. It serves as the bridge between the higher-level Solidity code and the low-level EVM bytecode.
In this repository you will find a set of examples that I have written in order to master Yul programming and smart contract optimization. Also, you will find exercises from courses and other articles.
To use assembly within a solidity smart contract, just insert an assembly block inside a solidity function:
function sendFunds(uint256 amount) external{
assembly {
let x := amount
}
}
Yul does not have the concept of types. It only works with 32 bytes
words. However, we can read and write from Solidity variables, for example:
assembly {
let a := 1234
let b := true
let c := "hello world"
let d := 0xabcdef
}
In this example, we asigned Solidity literals to assembly variables. Not that we use the keyword let
to declare a variable. Internally Yul represents these values as follows:
a = 0x0000000000000000000000000000000000000000000000000000000000001234
b = 0x0000000000000000000000000000000000000000000000000000000000000001
c = 0x68656c6c6f20776f726c64000000000000000000000000000000000000000000
d = 0x0000000000000000000000000000000000000000000000000000000000abcdef
Each variable is represented as a 64 characters (or 32 bytes). Every byte is represented by two characters. Because there is no boolean type in Yul, all operations return full 32-byte words, even the logical operations like and, or and not. A false
value is represented as full word with all 0s and a full word with 1 at the end for a true
value.
The not
operator performs negation on the bit level (it flips all 0s to 1s and vice versa), which means that not(1) will still return true, because all bits except the first one will be ones.
To retrieve and set storage variables we use the functions:
- slot - returns the location of a variable.
- sload - reads a value from a given storage slot.
- sstore - writes a value to given storage slot.
contract StorageBasics {
uint256 x = 10;
uint256 y = 20;
uint256 z = 30;
function getValueX() external view returns (bytes32 ret) {
assembly {
ret := sload(x.slot)
}
}
function getSlot(uint256 slot) external view returns (bytes32 ret) {
assembly {
ret := sload(slot)
}
}
// DANGEROUS OPERATION! Never allow external users to set arbitrary slots.
function setValue(uint256 slot, uint256 value) external {
assembly {
sstore(slot, value)
}
}
}
- Udemy-Course-Yul: These are my exercises from the course Advanced Solidity: Yul and Assembly, developed by Jeffrey Scholz in Udemy.
-
Ethereum Under the Hood: Algorithms And Data Structures by Marek Kirejczyk
-
EVM From Solidity to Bytecode, Memory & Storage by Peter Robinson
-
Demystifying EVM Opcodes by Gilbert G at ETH Global (2022).
-
Demystifying Ethereum Assembly by Joshua Riley at Devcon Bogota (2022)
-
EVM Opcodes for Gas Optimizations by Patrick Collins at Chainlink Hackaton (2022)
-
Alex Roan: Hitchhiker’s Guide to the EVM by Alex Roan at SmartCon Developer (2021)
-
EVM Codes - An EVM Opcodes Interactive Reference: Interactive Website & EVM playground. A very useful tool to practice and observe how opcodes work.
-
Andreas Antonopoulos - The Ethereum Virtual Machine: In this ebook, chapter 13 covers the fundamentals of how the EVM works and shows the EVM interaction with the protocol layer.
-
Femboy Capital - A Playdate with the EVM: This blogpost deep dive into the EVM, explaining how stack machines work before showing how to write assembly code.
-
Solidity Tutorial All About Assembly: A blogpost that explains in detail about EVM assembly, opcodes & using it in solidity.
-
Ethereum Virtual Machine (EVM) Deep Dive P.1: This is a series of articles explaining the Ethereum Virtual Machine.
-
Openzeppelin - Deconstructing a Solidity Contract: A series of blog posts to describe in detail how the solidity code is interpreted and executed by the EVM.
-
Diving Into the Ethereum Virtual Machine: In this series of articles the author deep dive into the Ethereum Virtual Machine and Opcodes.
-
Noxx - EVM Deep Dives: A series of blog posts that review in detail specific parts of the EVM taking you from solidity code to the EVM opcodes
-
Fvictorio - EVM Puzzle: These repositories are a collection of interactive EVM puzzles on GitHub.
-
Official Solidity Docs: Official Solidity Docs for how storage, memory & calldata are handled in a solidity contract