Skip to content

ayazabbas/dark-fleet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

59 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Dark Fleet

Zero-Knowledge Naval Warfare on Stellar

A zero-knowledge battleship game built on Stellar for the Stellar Hacks: ZK Gaming hackathon. Two players connect via Freighter wallets, commit hidden board states using Pedersen hash commitments, and prove shot results with ZK proofs โ€” all on-chain, all without revealing ship positions.

๐ŸŽฎ Play Now ยท ๐Ÿ“– Docs

Dark Fleet gameplay โ€” battle view with sonar ping, proof verification, and transaction log

How It Works

  1. Connect Wallet โ€” Both players connect Freighter wallets on Stellar testnet
  2. Create / Join โ€” Player 1 creates a game and shares the game code; Player 2 joins with the code
  3. Place Ships โ€” Each player places 5 ships on a private 10ร—10 grid
  4. Commit Board โ€” A ZK proof validates placement and commits a Pedersen hash on-chain
  5. Battle โ€” Players take turns firing shots; the defender generates a ZK proof for every hit/miss report, stored on-chain and verified by the opponent in real time
  6. Sonar Ping โ€” After 3 turns, unlock a one-time sonar ability โ€” the opponent proves ship count in a 3ร—3 area via ZK without revealing positions
  7. Win โ€” First to sink all 17 ship cells claims victory on-chain

The game uses Noir zero-knowledge circuits for privacy and Soroban smart contracts on Stellar for trustless game state management.

Architecture

  Browser A (Player 1)              Browser B (Player 2)
  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”              โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚  React Frontend  โ”‚              โ”‚  React Frontend  โ”‚
  โ”‚  + Noir WASM     โ”‚โ—„โ”€โ”€ verify โ”€โ”€โ–ถโ”‚  + Noir WASM     โ”‚
  โ”‚  + Freighter     โ”‚              โ”‚  + Freighter     โ”‚
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜              โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
           โ”‚                                 โ”‚
           โ”‚    Stellar Testnet (Soroban)     โ”‚
           โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”‚
           โ””โ”€โ”€โ”ค   Battleship Contract    โ”œโ”€โ”€โ”€โ”˜
              โ”‚  Game state + ZK proofs  โ”‚
              โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                         โ”‚
              โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
              โ”‚     Game Hub Contract    โ”‚
              โ”‚  (Hackathon registry)    โ”‚
              โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

ZK Circuits (Noir)

Built with Noir 0.34.0, based on BattleZips-Noir patterns.

Board Circuit (circuits/board/)

  • Validates ship placement: all 5 ships within 10ร—10 grid, no overlaps
  • Ships: Carrier (5), Battleship (4), Cruiser (3), Submarine (3), Destroyer (2) = 17 cells
  • Computes and outputs Pedersen hash commitment of ship positions
  • Private input: ships[15] (x, y, orientation for each ship)
  • Public output: board hash (Field)

Shot Circuit (circuits/shot/)

  • Proves whether a shot at (x, y) is a hit or miss
  • Verifies the prover's ships match their committed board hash
  • Private input: ships[15]
  • Public inputs: board_hash, hit (0 or 1), shot_x, shot_y

Sonar Circuit (circuits/sonar/)

  • Proves the count of ship cells in a 3ร—3 area around a center point
  • Verifies the prover's ships match their committed board hash
  • Demonstrates ZK's power beyond simple boolean proofs โ€” partial information disclosure
  • Private input: ships[15]
  • Public inputs: board_hash, center_x, center_y, count

Smart Contract (Soroban/Rust)

The Soroban contract (contracts/battleship/) manages the full game lifecycle:

Function Description
new_game(player1) Create a game session, returns game ID
join_game(game_id, player2) Join an existing game with a game code
commit_board(game_id, player, board_hash) Submit Pedersen hash commitment
take_shot(game_id, player, x, y) Fire a shot (must be your turn)
report_result(game_id, player, hit, proof) Report hit/miss with ZK proof stored on-chain
use_sonar(game_id, player, center_x, center_y) Sonar ping instead of firing (once per game, after 3 turns)
report_sonar(game_id, player, count, proof) Report sonar count with ZK proof stored on-chain
claim_victory(game_id, player) Claim win after 17 hits
get_game(game_id) Read current game state (view-only)

Integrates with the Stellar Game Hub contract (CB4VZAT2U3UC6XFK3N23SKRF2NDCMP3QHJYMCHHFMZO7MRQO6DQ2EMYG) via start_game() and end_game() calls.

Frontend (React + TypeScript)

  • Freighter wallet integration for 2-player on-chain gameplay
  • Create/join game flow with shareable base-32 game codes
  • Ship placement with click-to-place and R to rotate
  • In-browser ZK proof generation via @noir-lang/noir_js + @noir-lang/backend_barretenberg
  • On-chain battle: every shot, report, and sonar is a Stellar transaction
  • Auto-reporting: opponent actions detected via polling, ZK proofs generated and reported automatically
  • Cross-verification: opponent's browser fetches and verifies ZK proofs from on-chain state in real time
  • Proof & transaction log with StellarExpert explorer links
  • Dark theme with Tailwind CSS

Trust Model & Anti-Cheat

Dark Fleet uses on-chain proof storage + client-side cross-verification to ensure fair play:

  1. Proof submission โ€” When a player reports a shot result (hit/miss) or sonar count, their browser generates a ZK proof and submits it alongside the report to the Soroban contract. The proof is stored as Bytes in the contract's game state.
  2. Cross-verification โ€” The opponent's browser fetches the proof from the contract state and verifies it client-side using the Barretenberg WASM verifier. If the proof is invalid, a cheating alert is displayed.
  3. Public auditability โ€” All proofs are stored on-chain and can be fetched and verified by anyone via get_game(game_id).
P1 fires โ†’ P2 generates ZK proof โ†’ report_result(hit, proof) on-chain
                                        โ†“
                            P1 fetches proof from contract
                            P1 verifies proof in-browser
                                        โ†“
                               โœ… Valid / โŒ Invalid

Future work: On-chain proof verification (contract auto-rejects invalid proofs) via a Soroban UltraHonk verifier.

Deployed on Stellar Testnet

Quick Start

Prerequisites

1. Test ZK Circuits

cd circuits/board && nargo test
cd ../shot && nargo test
cd ../sonar && nargo test

2. Build Smart Contract

cd contracts/battleship
cargo build --target wasm32v1-none --release
cargo test

3. Run Frontend

cd frontend
npm install
npm run dev

Open http://localhost:5173 to play.

Project Structure

dark-fleet/
โ”œโ”€โ”€ circuits/
โ”‚   โ”œโ”€โ”€ board/           # Board validation ZK circuit
โ”‚   โ”‚   โ””โ”€โ”€ src/main.nr  # Ship placement + Pedersen hash
โ”‚   โ”œโ”€โ”€ shot/            # Shot verification ZK circuit
โ”‚   โ”‚   โ””โ”€โ”€ src/main.nr  # Hit/miss proof
โ”‚   โ””โ”€โ”€ sonar/           # Sonar ping ZK circuit
โ”‚       โ””โ”€โ”€ src/main.nr  # 3ร—3 area ship count proof
โ”œโ”€โ”€ contracts/
โ”‚   โ””โ”€โ”€ battleship/      # Soroban smart contract
โ”‚       โ””โ”€โ”€ src/lib.rs   # Game state management
โ”œโ”€โ”€ frontend/            # React web application
โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”œโ”€โ”€ App.tsx              # Main game flow (create/join/battle)
โ”‚   โ”‚   โ”œโ”€โ”€ components/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ Board.tsx        # 10ร—10 grid component
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ ShipPlacement.tsx # Ship placement UI
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ OnlineBattle.tsx # On-chain 2-player battle
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ GamePlay.tsx     # Hotseat battle (legacy)
โ”‚   โ”‚   โ”œโ”€โ”€ lib/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ game.ts          # Game logic & types
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ gameCode.ts      # Base-32 game code encoding
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ noir.ts          # ZK proof generation & verification
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ stellar.ts       # Stellar wallet & contract calls
โ”‚   โ”‚   โ””โ”€โ”€ pages/
โ”‚   โ”‚       โ””โ”€โ”€ Docs.tsx         # Documentation page
โ”‚   โ””โ”€โ”€ public/circuits/         # Compiled circuit artifacts
โ”œโ”€โ”€ .github/workflows/
โ”‚   โ””โ”€โ”€ release.yml              # SEP-0055 attestation workflow
โ””โ”€โ”€ README.md

Test Results

Board Circuit: 6 tests passed
  - Valid horizontal/vertical/mixed placements
  - Out-of-bounds detection
  - Ship collision detection

Shot Circuit: 7 tests passed
  - Hit detection (origin, middle of ship, vertical ships)
  - Miss detection
  - False hit/miss claim rejection
  - Wrong hash rejection

Sonar Circuit: 7 tests passed
  - Empty area scan (count 0)
  - Dense area scan (count 9)
  - Corner/edge clamping
  - Partial count with mixed layout
  - Wrong count rejection
  - Wrong hash rejection

Smart Contract: 10 tests passed
  - Game creation and joining
  - Board commitment
  - Shot and result reporting (with proof storage)
  - Full game to victory
  - Turn enforcement
  - Premature victory rejection
  - Sonar availability after 3 turns
  - Sonar full flow (use + report with proof)
  - Sonar too early rejection
  - Sonar double use rejection

Technology Stack

Component Technology
ZK Circuits Noir 0.34.0 (Pedersen hash, BN254)
Smart Contract Soroban SDK 25.1.1 (Rust)
Frontend React 19, TypeScript, Vite, Tailwind CSS
Proof Engine noir_js + backend_barretenberg (in-browser WASM)
Blockchain Stellar Soroban (Testnet)
Wallet Freighter (@stellar/freighter-api)

Design Decisions

  • Pedersen hash for board commitments โ€” native to Noir circuits, efficient and ZK-friendly
  • Board hash as circuit output โ€” the board circuit computes and returns the hash, eliminating external hash computation
  • On-chain proof storage + client-side cross-verification โ€” ZK proofs stored on-chain with every report, verified by the opponent's browser in real time; on-chain UltraHonk verification is the natural next step
  • 2-player on-chain multiplayer โ€” each player connects via Freighter in separate browser windows; game state synced via Soroban contract polling
  • Sonar ping (unique ZK mechanic) โ€” most ZK battleship implementations only prove binary hit/miss; our sonar circuit proves a count of ship cells in a region, demonstrating ZK's ability to disclose partial information without revealing exact positions
  • Base-32 game codes โ€” short, shareable codes (e.g. 19VG) for game joining instead of raw numeric IDs

Future Improvements

  • On-chain proof verification via UltraHonk Soroban Verifier
  • WebSocket real-time sync (replace polling)
  • Ship sinking detection and animation
  • Game replay from on-chain events
  • Mainnet deployment

Hackathon

Built for Stellar Hacks: ZK Gaming โ€” February 2026

About

Battleship game on Stellar Blockchain - hidden fleets, sonar pings, all verified with ZK proofs

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors