Skip to main content

Battleship with cryptographic protections and a zero-knowledge framework.

Project description

ZKBattleship

This project was built to demonstrate a protocol to play Battleship without a trusted party. In this project, multiple modules have been created to fulfill this task, and all code is my own. I will go over each module in the following sections.

Adaptable modules (useful for other cases)

random_prime.py

The random prime module was the first one I had designed. It's function is to return a random prime that fits certain sets of constraint depending on which function is called, such as the length of the prime in bits or if a prime of the form 4 * x + 1 is also a prime. It does so by finding a random odd number with the bit length specified and adding two until it is prime under a set of primality tests. These include the Fermat and Miller-Rabin primality tests.

pedersen.py

Next, I designed the pedersen module because it had use with Bulletproofs, my initial design idea. This class was meant to generate, hold, create generators for, add, and verify Pedersen commitments (https://link.springer.com/chapter/10.1007/3-540-46766-1_9). Rather than holding the Pedersen generator as an instance of the Pedersen class, Pedersen generators are instead held as dataclasses inside the Pedersen class for ease of transporting values and referencing internal variables. The commitment outputs are held the same way. Rather than using an instance of the Pedersen class, most operations are done statically and through arguments.

bitproof.py

This module implements a modified Schnorr protocol (https://link.springer.com/article/10.1007/BF00196725) to prove knowledge of the message in a commitment was a bit. This protocol was modified such that one could cheat to prove that the value of the commitment was both a zero and a one if the value was either, giving the verifier zero knowledge of the message except that it is a bit. A Fiat-Shamir heuristic (https://link.springer.com/chapter/10.1007%2F3-540-47721-7_12) was also implemented to make the proof non interactive and easier to implement. This proof is also held in a data class for ease of transporting values and referencing the many variables in the proof.

Case specific modules (useful for Battleship only)

board.py

In the board module, each board that would be used in the actual Battleship was designed. First a board class that all others would be inherited from was programmed with the methods to return a spot on the board given a Battleship coordinate. Then boards for specific purposes like holding public commitments or responses from the other player were made. Finally, a ship board was made that inherited from the commitment board such that it could hold the commitments and spaces parallel to each other. The commitment board contains the functionality to create commitments and proofs for those commitments while the other two boards contain the functionality to return the board as a string and toggle a space.

main.py

Finally, the main program was written. I designed a player class such that, theoretically, the two players can play online and there is a degree of encapsulation. It was asynchronous so that one player could wait until another player to input their board or do their turn without making IO operations broken. First both players input their board, and the game starts. Each player has the other player instance has a variable so that they can call a function to get the value of, say, a specific square. Players take turns asking squares until all ships are found.

stats.py

This module can be safely ignored. It is only for my personal use of analyzing the distributions of bitproofs or commitments and making graphs for my posterboard. This is the only module that uses libraries outside of the standard library. MatPlotLib and SciPy are not needed if this module is not used.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ZKBattleship-1.0.0.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ZKBattleship-1.0.0-py3-none-any.whl (23.3 kB view details)

Uploaded Python 3

File details

Details for the file ZKBattleship-1.0.0.tar.gz.

File metadata

  • Download URL: ZKBattleship-1.0.0.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.9.1

File hashes

Hashes for ZKBattleship-1.0.0.tar.gz
Algorithm Hash digest
SHA256 da2278726ba8a3c318a8866dd0878a8d357e1a9feaa32af898bbda8634e65bb5
MD5 bbef96debfa7e1c6bb8de59cec58d5d3
BLAKE2b-256 e49078b94b0be8e5b75a2262eb4c277d3a5a4f5ef94487831faee9a729cb6c33

See more details on using hashes here.

File details

Details for the file ZKBattleship-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: ZKBattleship-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 23.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.9.1

File hashes

Hashes for ZKBattleship-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1d96013f95685f872c1fec1ef3025d587b45600cd3ee362d8deb68c979d40b8d
MD5 4e2ed6351ca95b4d295424f0897afc69
BLAKE2b-256 5d50bb16dedde728e8d991577b81c74453dea5fd80e3e417a7930343b55bf06a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page