Skip to content

A collection of Yul/Yul+ code, from basic token standards, to advanced Defi primitives

Notifications You must be signed in to change notification settings

pynchmeister/yul-greatest-hits

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 

Repository files navigation

🔥 Yul Greatest Hits

A collection of low-level EVM patterns, micro-primitives, and optimizations written in pure Yul / Yul+.

This repository is a curated set of experiments, patterns, and implementation techniques exploring raw EVM execution using Yul and Yul+.
It covers everything from minimal ERC standards to advanced DeFi building blocks, gas-optimized control flow, storage/memory patterns, and opcode-aware micro-architectures.

The goal is to develop a deep, practical understanding of the EVM as a computation model — the kind required for protocol engineering, performance-critical smart contracts, and security-sensitive primitives.


⭐ Why Yul?

Yul is a low-level intermediate language that compiles directly to EVM bytecode. It offers a perfect balance between:

  • The control of raw opcodes
  • The readability of structured programming
  • The optimization potential of hand-crafted bytecode
  • The mental model needed for protocol-level engineering

Yul is used for:

  • Writing gas-critical components
  • Custom memory & storage layouts
  • DeFi/math primitives
  • Minimal proxies
  • Cryptographic routines
  • System-level contracts
  • EIP implementations
  • Auditing and vulnerability research
  • Understanding EVM execution deeply

⭐ What This Repository Contains

🧩 1. Minimal ERC Implementations

Bare-metal ERC-20 / ERC-721 / ERC-1155 style patterns using minimal storage and execution logic.

Focus:

  • zero-overhead function selectors
  • calldata decoding
  • storage slot math
  • packed structs
  • custom revert patterns

⚙️ 2. DeFi Micro-Primitives

Low-level foundations used inside AMMs, DEXes, and money markets:

  • Safe arithmetic patterns
  • Constant-product & curve math
  • Reserve accounting
  • Fee models
  • Swap kernels
  • Oracle-style read-only paths

These are written with explicit gas-model awareness:
stack pressure, memory reuse, branching cost, and warm/cold SLOAD patterns.


🧮 3. Gas Optimization Patterns

A collection of before/after transformations showing how to reduce:

  • memory expansion
  • redundant SLOAD/SSTORE
  • stack shuffling
  • branching overhead
  • function dispatch cost
  • error handling gas

Includes patterns such as:

  • fallback-driven dispatch
  • function-selector jump tables
  • reusable scratch memory
  • inlined arithmetic vs. subroutines

🧱 4. Control-Flow Experiments

Explorations of how to structure execution in Yul:

  • manual jump tables
  • loop unrolling
  • switch vs. if branching
  • early exit patterns
  • zero-copy calldata slicing
  • using assembly blocks for Solidity interop

📦 5. Storage & Memory Layout Techniques

Patterns for:

  • efficient storage slot packing
  • struct layout
  • pointer-style memory operations
  • transient scratch space
  • calldata pointers vs. memory copies
  • high-density data formats

These techniques are essential for high-performance protocols, rollup precompiles, and system contracts.


🔐 6. Security-Oriented Yul Patterns

Experiments involving:

  • reentrancy-aware control flow
  • custom authorization logic
  • manual return data handling
  • calldata validation
  • invariant boundaries

These help develop intuition for auditing and secure system design.


🔬 7. Yul+ Extensions (If Used)

Yul+ adds syntactic sugar on top of raw Yul:

  • typed variables
  • structured control flow
  • easier memory management
  • safer arithmetic helpers
  • cleaner inlining/abstractions

This repo may contain examples in both Yul and Yul+ for comparison.


⭐ Background: What Makes Yul Special

Yul is an intermediate language designed for optimization, with properties that make it ideal for EVM-level engineering:

  • Structured syntax (for, if, switch)
  • No explicit stack-touching ops (SWAP, DUP, etc.) → clearer data flow
  • Minimal abstractions → predictable performance
  • Statically typed (u256 by default)
  • Expressive enough to model most system-level patterns
  • Compiles directly to efficient bytecode

Yul allows you to reason in terms of EVM execution traces without drowning in raw opcode sequences — the sweet spot between Solidity and raw assembly.


🛠️ Tools Used

  • Foundry (forge / cast) for compilation, gas snapshots, and testing
  • Yul / Yul+ compiler targets
  • EVM bytecode disassembly tools
  • Gas profilers (Foundry debug tools)
  • Opcode inspectors

🎯 Goals of This Repository

  • Improve mastery over EVM internals
  • Build intuition for protocol-level engineering
  • Demonstrate high-performance contract design
  • Explore the limits of gas optimization
  • Document reusable patterns for smart contract security
  • Share useful primitives for other developers and auditors

🚀 Want to explore or contribute?

Open an issue or PR — this repo is intended as a living notebook of low-level techniques for EVM engineers, auditors, and protocol designers.

About

A collection of Yul/Yul+ code, from basic token standards, to advanced Defi primitives

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors