March 21, 2024

Getting Started with EVM Puzzles

6 min readDifficulty: Beginner

Introduction

Welcome to the EVM Puzzles challenge series! In this first post, we're going to look at a puzzle breakdown and learn how to set up our environment. This will allow us to access and solve the EVM puzzles.

Prerequisites

Before we begin, make sure you have the following installed:

  • Node.js (v14 or later)
  • Git
  • A code editor (VS Code recommended)
  • Basic understanding of Ethereum and smart contracts

Setting Up the Environment

Option 1:

Clone the repository: https://github.com/fvictorio/evm-puzzles

Run npm install to install dependencies.

Then your terminal run npx hardhat play.

This will allow you to access and solve the puzzles from your terminal.

Option 2:

Use the EVM Playground to run and test each puzzle.

When you open the playground, change the settings from 'YUL' to 'Bytecode' (as I've illustrated in the image below).

I have listed the bytecode to each puzzle right above it. You will copy and paste this bytecode right into the playground. (You may need to delete what's there first.)

EVM Playground Settings

Once you've added the bytecode, click Run and it will load your puzzle.

Understanding EVM Basics

The Ethereum Virtual Machine (EVM) is a stack-based machine that executes bytecode. Here are some key concepts we'll need to understand:

Key EVM Concepts

  • Stack: A last-in-first-out (LIFO) data structure used for operations
  • Memory: A volatile byte array used for temporary storage
  • Storage: A persistent key-value store
  • Program Counter: Points to the current instruction being executed

Common EVM Opcodes

Here are some essential opcodes we'll encounter in the puzzles:

OpcodeHexDescription
PUSH10x60Push 1 byte onto stack
POP0x50Remove top item from stack
ADD0x01Add top two stack items
MUL0x02Multiply top two stack items
JUMP0x56Jump to position
JUMPI0x57Conditional jump

Running Your First Puzzle

Let's look at a simple example to understand how the puzzles work. Here's Puzzle 1:

# Puzzle 1
# The goal is to make the execution reach the 'JUMPDEST' located at position 06.
# When the execution reaches 'JUMPDEST' it will return from the contract successfully.
# When the execution reaches any other part with the REVERT or INVALID opcode ('fail' label),
# it will revert with an error.

00      34      CALLVALUE
01      56      JUMP
02      FD      REVERT
03      FD      REVERT
04      FD      REVERT
05      FD      REVERT
06      5B      JUMPDEST
07      00      STOP

# Bytecode: 0x345656FDFDFDFD5B00

Understanding the Puzzle

Let's break down what this puzzle is doing:

  1. CALLVALUE pushes the amount of ETH sent with the transaction onto the stack
  2. JUMP uses that value as the destination to jump to
  3. If we jump to position 6 (the JUMPDEST), we succeed
  4. If we jump anywhere else, we hit a REVERT and fail

Solution Hint:

To solve this puzzle, we need to send exactly 6 wei with the transaction. This will make the JUMP instruction jump to position 6, where the JUMPDEST is located.

Tools and Resources

Here are some helpful tools for solving EVM puzzles:

Next Steps

Now that you have your environment set up and understand the basics, you're ready to tackle the EVM puzzles! In the next post, we'll dive into Puzzles 1 and 2, which focus on stack operations and basic program flow.