Skip to content

0xhaz/CrossSwap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cross-Chain Liquidity and Swap Circuits with Recursive GKR Compression

Overview

This project introduces a system of zero-knowledge circuits—LiquidityCircuit, SwapCircuitGKR, and VerifierCircuitGKR—designed to enable efficient onchain verification of cross-chain liquidity provision and token swaps. Leveraging Recursive GKR (Gennaro, Gentry, Krawczyk, and Rabin) compression, we minimize gas costs and proof sizes, making complex DeFi operations scalable and verifiable on Ethereum.

Built for the hackathon theme "Efficient Onchain Verifiers Using Recursive GKR Compression", this solution proves liquidity and swap computations offchain, compressing them into a single, constant-sized proof for cheap onchain verification.


How the Circuits Work

1. LiquidityCircuit

  • Purpose: Proves a user has provided or withdrawn liquidity in a pool (e.g., Uniswap-style AMM).
  • Inputs:
    • Owner address (U256)
    • Tick range (tick_lower, tick_upper)
    • Liquidity delta (i128)
    • Current sqrt price (sqrt_price_current_x96)
    • Optional hook data
  • Logic:
    • Validates tick alignment and range (tick_lower < tick_upper).
    • Computes token deltas (amount0, amount1) using sqrt price math, scaled by 10^18.
    • Outputs deltas as public signals.
  • Design: Uses BN254 arithmetic and Poseidon hashing for efficiency.

2. SwapCircuitGKR

  • Purpose: Proves a swap operation adheres to pool rules.
  • Inputs:
    • Swap direction (zero_for_one)
    • Amount specified (I256)
    • Price limit (sqrt_price_limit_x96)
    • Current price (sqrt_price_current_x96)
    • Liquidity (u128)
    • Fee pips (u32)
  • Logic:
    • Calculates next sqrt price, amount in/out, and fee.
    • Adjusts deltas based on hooks (e.g., 1% fee tweak).
    • Outputs signed amount0 and amount1 as public signals.
  • Design: GKR-friendly arithmetic for swap step computation.

3. VerifierCircuitGKR

  • Purpose: Aggregates and verifies multiple proofs onchain.
  • Inputs:
    • Final proof (Proof)
    • Aggregated prior proofs
    • Public outputs (amount0, amount1)
  • Logic:
    • Recursively hashes proofs with Poseidon: Poseidon(prev_proof, amount0)Poseidon(intermediate, amount1).
    • Asserts the final proof matches the aggregated hash.
  • Design: Lightweight, optimized for recursive compression.

Recursive GKR Compression

How It Works

  • Each circuit generates a GKR proof for its computation.
  • generate_gkr_proof hashes proofs sequentially:
    1. Poseidon(prev_proof, amount0) → Intermediate hash.
    2. Poseidon(intermediate, amount1) → Circuit proof.
  • VerifierCircuitGKR aggregates all proofs into a single 32-byte hash, verified onchain with one Poseidon operation.

Efficiency

  • Without Compression: 150 circuits = 150 × 32 bytes (~4.8 KB), ~1M+ gas.
  • With Recursive GKR: 1 × 32 bytes (constant size), ~50K gas.
  • Result: Scales verification linearly with circuits, not proof count.

What We Aim to Accomplish

Goal

Enable trustless cross-chain DeFi by proving liquidity and swap operations offchain, with a minimal onchain footprint.

Hackathon Fit

  • Efficiency: Reduces gas from ~1M to ~50K for 150 proofs, rivaling SNARKs without complex setup.
  • Onchain Verifiers: Integrates with Solidity’s PoseidonT3 for seamless Ethereum deployment.
  • Scalability: Supports batched operations (e.g., 150 swaps) in one proof, perfect for rollups or bridges.

Outcome

A demo where users execute swaps or liquidity changes, generate compressed proofs, and verify them onchain cheaply—unlocking scalable, secure DeFi.


Why It Matters

Cross-chain DeFi is bottlenecked by trust and cost. Our solution proves complex operations (e.g., Uniswap V3 math) efficiently, enabling secure bridges and pools verifiable on Ethereum with minimal overhead.


Demo Flow

  1. User Action: Execute a swap or liquidity provision.
  2. Proof Generation: Circuits compute amount0, amount1, and proofs.
  3. Compression: Recursive GKR aggregates proofs into 32 bytes.
  4. Onchain Verification: VerifierCircuitGKR checks the proof in ~50K gas.

Workflow Diagram

    +-----------------------------------------------+
    | Cross-Chain Liquidity & Swap Workflow         |
    +-----------------------------------------------+
                    [User Action]
                   (Swap/Liquidity)
                          |
                          v
+-------------------------+-----------------------------+
| LiquidityCircuit        | SwapCircuitGKR              |
| - Inputs:               | - Inputs:                   |
| - owner                 | - zero_for_one              |
| - tick_lower/upper      | - amount_specified          |
| - liquidity_delta       | - sqrt_price_limit          |
| - sqrt_price_x96        | - sqrt_price_x96            |
| - Computes:             | - Computes:                 |
| - amount0, amount1      | - amount0, amount1          |
+-------------------------+-----------------------------+
            |                           |
            v                           v
    [Public Outputs]            [Public Outputs]
   (amount0, amount1)          (amount0, amount1)
            |                           |
            +-------------+-------------+
                          |
                          v
  +---------------------------------------------------+
  |              Proof Generation (GKR)               |
  | - Poseidon(prev_proof, amount0) --> Intermediate  |
  | - Poseidon(intermediate, amount1) --> Proof       |
  | - For each circuit: Proof 0, Proof 1, ...         |
  +---------------------------------------------------+
                          |
                          v
          +-------------------------------+
          |   Recursive GKR Compression   |
          |     - Aggregate Proofs:       |
          |  Proof 0 --> Proof 1 --> ...  |
          |     --> Single 32-byte Proof  |
          +-------------------------------+
                          |
                          v
          +-------------------------------+
          |       VerifierCircuitGKR      |
          |       - Input: Final Proof    |
          |         - Verifies:           |
          |  Poseidon(aggregated) = Proof |
          +-------------------------------+
                          |
                          v
                [Ethereum Blockchain]
              (Efficient Verification)

Test Summary

   +------------------------------------------------------------------------+
   | Test Name        | Circuits | Gen Time (ms) | Verify Time (ms) | Valid |
   +------------------------------------------------------------------------+
   | Large Batch Test | 150      | 12.66         | 2.41             | Yes   |
   +------------------------------------------------------------------------+

Getting Started

Prerequisites

  • Rust (for circuit compilation)
  • Foundry (for Solidity integration)
  • Ethereum node (for deployment)

Build & Run

# Compile circuits
cargo build --release

# Run proof generation
cargo run --release

About

UniswapV4 with Polyhedra Network

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors