EVM Puzzle #5: Stack Manipulations and Conditional Logic

EVM Puzzle #5: Stack Manipulations and Conditional Logic

EVM puzzles continue to push the boundaries of our understanding of the Ethereum Virtual Machine. Puzzle #5 introduces stack manipulations, the powerful PUSH2 opcode, and intricate conditional execution to test our abilities. Let's break down the challenge and discover the solution.

Puzzle #5 Bytecode

00      34          CALLVALUE  
01      80          DUP1
02      02          MUL
03      610100      PUSH2 0100  
06      14          EQ
07      600C        PUSH1 0C
09      57          JUMPI
0A      FD          REVERT
0B      FD          REVERT
0C      5B          JUMPDEST
0D      00          STOP 
0E      FD          REVERT
0F      FD          REVERT
        

Analysis

Let's analyze the instructions step by step:

  • 34 (CALLVALUE): Pushes the amount of Ether (in wei) sent with the transaction.
  • 80 (DUP1): Duplicates the top item on the stack.
  • 02 (MUL): Multiplies the top two values on the stack, replacing them with the result.
  • 610100 (PUSH2 0100): Pushes a 2-byte value (256 in decimal) onto the stack.
  • 14 (EQ): Pops two values, compares them for equality. Pushes 1 if equal, 0 otherwise.
  • 600C (PUSH1 0C): Pushes 12 (decimal) onto the stack.
  • 57 (JUMPI): Conditionally jumps to the specified location if the top stack value is nonzero.
  • FD (REVERT): Halts execution and rolls back changes.
  • 5B (JUMPDEST): A potential jump destination.
  • 00 (STOP): Terminates contract execution.

Solving the Puzzle

The key to this puzzle lies in these steps:

  1. Stack Manipulation: The value of CALLVALUE is duplicated and then multiplied by itself, resulting in its square.
  2. Comparison: The squared CALLVALUE is compared to 256 (the value pushed by PUSH2).
  3. Conditional Jump: If the squared CALLVALUE equals 256, the JUMPI will jump to the JUMPDEST, leading to successful execution.

Therefore, to solve this puzzle, you need to send a transaction with a CALLVALUE whose square is 256. The solution is a CALLVALUE of 16.

Key Takeaways

  • Stack manipulation using opcodes like DUP1 is essential in EVM programming.
  • The PUSH2 opcode extends your ability to push larger values onto the stack.
  • Conditional jumps using JUMPI control execution flow depending on stack state.

Looking Ahead

EVM puzzles will get even more challenging! Be prepared to manipulate storage data, tackle intricate calculations, and perhaps even interact with external contracts. The world of EVM offers endless possibilities for exploration and experimentation!

To view or add a comment, sign in

More articles by Shiran Sukumar

  • How to Manage a Team That Misses Deadlines

    How to Manage a Team That Misses Deadlines

    Missing deadlines isn’t just about delays; it’s about trust—the cornerstone of every successful team. Without trust…

  • Dynamic.xyz - Building dApp authentication

    Dynamic.xyz - Building dApp authentication

    As a Web3 consultant, I've been able to work on exciting projects. While I cannot go into too much detail, I was…

    2 Comments
  • Viem vs ethers vs web3 frameworks

    Viem vs ethers vs web3 frameworks

    As blockchain technology advances, developers are constantly searching for better tools to streamline the process of…

  • Capture the Ether - Token Whale

    Capture the Ether - Token Whale

    Introduction The Token Whale challenge within the Capture the Ether series highlights the dire consequences of careless…

  • EVM Puzzle #4: XOR and Code Manipulation

    EVM Puzzle #4: XOR and Code Manipulation

    The world of EVM puzzles invites us to understand the intricate workings of the Ethereum Virtual Machine. In Puzzle #4,…

  • EVM Puzzle 3 - CALLDATASIZE

    EVM Puzzle 3 - CALLDATASIZE

    Introduction In this installment of our EVM puzzle series, we'll examine a seemingly simple yet subtly challenging…

  • EVM Puzzle 2 - Codesize

    EVM Puzzle 2 - Codesize

    Introduction EVM puzzles push our understanding of Ethereum's low-level execution. This installment presents a…

  • Capture the Ether - Predict the Future

    Capture the Ether - Predict the Future

    Intro The "Predict the Future" challenge in Capture the Ether forces us to think outside the box. We're tasked with…

    1 Comment
  • Capture the Ether - Guess The New Number

    Capture the Ether - Guess The New Number

    Intro The "Guess the New Number" challenge presents an updated twist on the classic number-guessing game within the…

    1 Comment
  • Ethernaut #20

    Ethernaut #20

    Ethernaut #20: Denial – More Reentrancy & Gas Exploits - When Withdrawal Goes Wrong Intro Ethernaut's Denial level…

Insights from the community

Others also viewed

Explore topics