Skip to content

Gimer0x/yul

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Yul Programming Language

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.

Table of Contents

  1. The Ethereum Virtual Machine
  2. Yul Tutorial
  3. Folders
  4. Suggested Videos
  5. Additional Resources

The Ethereum Virtual Machine (EVM)

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.

Yul Tutorial

Basic Syntax

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
  }

}

Types in Yul

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 falsevalue is represented as full word with all 0s and a full word with 1 at the end for a truevalue.

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.

Storage variables

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)
        }
    }
}

Folders

Suggested Videos

Additional Resources

About

Yul exercises and trainning

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages