Shigoto-dev19 / ZK-Battleships-Solana

First decentralized & private Battleships game on Solana. Honorable Mention recognition in Solana Grizzlython Hackathon
https://solana.com/news/solana-grizzlython-winners?utm_source=Iterable&utm_medium=email&utm_campaign=campaign_6567415#:~:text=Pet%20Legends-,Zk%20Battleships,-Little%20Bonkers
25 stars 4 forks source link

ZK Battleship Game on Solana

Basic Game Description


Game Definition:

Motivation:

Solution:

Technical Description


→ Regarding that every move made in the game i.e. transaction is public, the game progression stats is masked applying poseidon encryption to the game results.

→ The boards and shots of the players are kept private utilizing the DSL circom2 to develop the circuits required to integerate the ZKP logic to the solana program.

→ The ZK impelemenation of the game is based on two main circuits.

→ The first circuit is the board circuit, it takes the board of the player as a private input and its poseidon hash as a public input that serves as integrity attestation that the player's board is not tampered with.

→ The second circuit is the shot circuit.

→ To keep the game account data storage as efficient as possible, the account stores only one encrypted shot and one encrypted hit result at a time, this way, when a player makes a move, the program verifies the shot circuit according to the encrypted shot from the previous turn (opponent's shot) and if successful, it updates the shot as the current player's encrypted shot in order to be verified in the next turn.

→ It is unavoidable that each player knows his/her shot result only when the opponent's finishes playing his/her turn because private inputs of the shot circuit can only be entered by the player himself/herself.

→ It is notable that the game stat progression for each player is synchronously retrieved, decrypted, and saved by the client and hence the whole game data of the player and his opponents is displayed clear and complete without any issue and with a minimum data exchange through the game account.

⇒ The ZK Battleship game cli is still under development, however it shows a cool interface for a user to host or join a game with everything clearly displayed without any worries of the technical or cryptographic operations.

Screenshot of the game CLI

Steps (requires Linux/ OS X)

Since compatibility is a common question, M1 chips will outperform the expected wait times on ptau and setup Node v16.17.1 was last used to compile this project, and failure to use it will break ipfs in deploy script (either use node 16 or comment out IPFS)

0. Ensure Circom 2.x.x is installed locally

curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
git clone https://github.com/iden3/circom.git
cd circom
cargo build --release
cargo install --path circom
cd ..

1. Install the neccessary dependencies

yarn install

2. Run POT15 ceremony

yarn ptau

3. Build zkeys and verification keys for each circuit

yarn setup

4. Build, Deploy & Test

anchor build to build the solana program.

anchor deploy to deploy the game to localnet.

anchor test --skip-deploy --skip-local-validator to test the game.

5. Clean Circuit Artifacts

yarn clean-artifacts to clean all circuit artifacts.

yarn clean-setup to clean circuit setup excluding ptau files.

6. Acknowledgements