A trustless, on-chain Battleship game implementation using Zero-Knowledge Succinct Non-Interactive Arguments of Knowledge (ZK-SNARKs) for complete anti-cheat guarantees.
This project implements a fully trustless Battleship game where players cannot cheat due to cryptographic guarantees. Ship positions remain private until game end, and all game actions are verified through zero-knowledge proofs before being accepted on-chain.
- Prerequisites
- Quick Start
- Installation
- Usage
- Project Structure
- Architecture
- Development
- Troubleshooting
- Resources
- Node.js (v22+) with Pnpm (v10+)
- Docker or Podman for running services
- Foundry for smart contract development
- Just as task runner (recommended,
cargo install just)
⚠️ Version Requirements⚠️ Please verify you're running Node.js v22+ and Pnpm v10+ before proceeding. Older versions may cause compatibility issues. Check your versions with
node --versionandpnpm --version.
- Nix for complete development environment setup
- Circom (2.1.0+) for compiling our circuits (installation guide)
The amp Docker image is currently hosted in our private GitHub container registry. In order to access it, you'll have to login to ghcr.io with Docker. Create a new personal access token (classic) with the read:packages permission at https://github.com/settings/tokens. Now run the following command and insert the newly generated token as your password when prompted.
docker login ghcr.io --username <YOUR GITHUB USERNAME>-
Clone and install dependencies:
# NOTE: Make sure to use `--recursive` to also clone the `forge-std` git submodule git clone --recursive <repository-url> cd battleship just install
-
Start services:
just up
-
Deploy contracts:
just deploy
-
Start development servers:
just dev
-
Open your browser:
- Game interface: http://localhost:5173
- Database explorer: http://localhost:7402
- Monitoring: http://localhost:3030
Clone the repository (with --recursive) and install dependencies:
# NOTE: Make sure to use `--recursive` to also clone the `forge-std` git submodule
git clone --recursive <repository-url>
cd battleship
just installLaunch PostgreSQL, Anvil (local Ethereum), and other services:
just upThis starts:
- PostgreSQL (port 5432) - Database backend
- Anvil (port 8545) - Local Ethereum node
- Amp (ports 1602, 1603, 1610) - Data engineering layer
- Adminer (port 7402) - Database explorer
- Grafana (port 3030) - Monitoring dashboard
This is an optional step. The repository contains pre-built circuits. You'll only need to run this if you are modifying the circuits.
Compile circuits and generate verification keys:
just circuitsThis process:
- Downloads Powers of Tau ceremony file (if needed)
- Compiles Circom circuits to R1CS
- Runs trusted setup ceremony with random entropy
- Exports Solidity verifier contracts
- Copies circuit files to frontend
Deploy the Battleship contract to local network:
just deployStart the application frontend and the Amp development & proxy servers.
just devjust install- Install all dependenciesjust up [services]- Start infrastructure servicesjust dev- Run all development services in paralleljust down- Stop all services and clean up
just circuits- Build all circuits with full ceremonyjust circuits board- Build only board validation circuitjust circuits impact- Build only impact verification circuit
just test- Run all tests (Solidity + TypeScript)just check- Run linting and type checkingjust fmt- Format all code
just logs- View logs from all servicesjust logs postgres- View PostgreSQL logs onlyjust stop- Stop services without cleanup
- Make circuit changes in
circuits/ - Rebuild circuits:
just circuits - Run tests:
just test - Redeploy contracts (if needed):
just deploy - Test in browser: Game runs at http://localhost:5173
Run comprehensive test suite covering anti-cheat mechanisms:
# All tests
just test
# Specific test patterns
just test-sol --match-path "**/Battleship.t.sol"
just test-ts circuitbattleship/
├── circuits/ # ZK-SNARK circuits
│ ├── board.circom # Board validation circuit
│ ├── impact.circom # Impact verification circuit
│ └── build/ # Compiled circuit artifacts
├── contracts/ # Smart contracts
│ ├── src/Battleship.sol # Main game contract
│ ├── src/*Verifier.sol # Generated proof verifiers
│ └── test/ # Comprehensive test suite
├── app/ # Frontend application
│ ├── src/components/ # React components
│ ├── src/lib/ # Blockchain utilities
│ └── public/circuits/ # Circuit files for browser
├── infra/ # Infrastructure configuration
│ ├── postgres/ # Database setup
│ ├── amp/ # Data indexing config
│ └── grafana/ # Monitoring dashboards
├── justfile # Development commands
└── docker-compose.yaml # Service orchestration
- Board Setup: Players generate cryptographic commitments to their ship positions using the board circuit
- Game Turns: Each attack requires a zero-knowledge proof from the defender using the impact circuit
- State Chain: Game states are linked through commitments to prevent tampering
- Verification: Smart contract validates all proofs before accepting state transitions
- Purpose: Validates ship placement follows game rules
- Inputs: Ship positions + random salt (private)
- Outputs: Board commitment + initial state commitment
- Validates: Bounds checking, no overlaps, correct ship lengths
- Purpose: Proves hit/miss results and maintains game state integrity
- Inputs: Previous state, attack coordinates, ship positions (private)
- Outputs: New state commitment, remaining ships count
- Validates: State consistency, hit detection, claim accuracy
- Ship Movement Prevention: Board commitments lock ship positions
- False Claims Prevention: Impact proofs cryptographically verify hit/miss results
- State Manipulation Prevention: Commitment chains prevent tampering
- Double-Shot Prevention: On-chain tracking with bit-packed grids
- Fork the repository
- Create a feature branch:
git checkout -b feature-name - Make your changes and add tests
- Run the full test suite:
just test - Format code:
just fmt - Commit and push changes
- Open a pull request
- Frontend: http://localhost:5173
- Database Explorer: http://localhost:7402 (user: postgres, pass: postgres)
- Ethereum RPC: http://localhost:8545
- Grafana Monitoring: http://localhost:3030
- Amp Admin: http://localhost:1610
- Use
just logsto monitor service health - Check
just --listfor all available commands - Circuit artifacts are automatically copied to
app/public/circuits/
- ZK-SNARK Technical Details - Comprehensive technical documentation
- Circom Documentation - Circuit development
- snarkjs Guide - Proof generation and verification
- Foundry Documentation - Smart contract development
For technical implementation details, see BATTLESHIP.md.