A networked implementation of rock-paper-scissors that uses cryptographic commitments to prevent cheating in distributed games.
In traditional networked games of rock-paper-scissors, there's a fundamental trust problem:
- If Player A sends their choice first, Player B can see it and choose accordingly (guaranteed win for Player B)
- If both players send simultaneously, network delays mean one player's choice arrives first
- Even with a trusted third party, you must trust that party won't leak information
How can two players play a fair game over a network without trusting each other or a third party?
This implementation uses a commit-reveal protocol based on cryptographic hashing to solve the cheating problem:
Instead of sending their actual choice, each player sends a cryptographic hash of their choice combined with a random secret (called a "nonce").
Hash = SHA-256(Play + Nonce)
The hash acts as a cryptographic commitment:
- Binding: Once committed, the player cannot change their choice (the hash would no longer match)
- Hiding: The opponent cannot reverse the hash to discover the choice
After both players have committed (by exchanging hashes), they reveal:
- Their actual play (Rock, Paper, or Scissors)
- Their secret nonce
Each player verifies the opponent's commitment:
- Recompute the hash using the revealed play and nonce
- Compare it to the hash received in Phase 1
- If they don't match → the opponent cheated
- If they match → the game is valid
- No premature information leak: The hash reveals nothing about the actual play
- Tamper-proof: Changing the play after commitment would require breaking SHA-256 (computationally infeasible)
- No trusted third party needed: The mathematics guarantee fairness
- Verifiable: Both players can independently verify no cheating occurred
Player A (Server) Network Player B (Client)
─────────────────────────────────────────────────────────────
1. Choose play + nonce
2. Compute hash ───────> Receive hash
Choose play + nonce
3. Receive hash <─────── Send hash
4. Reveal play + nonce ───────> Receive revelation
Verify hash matches
5. Receive revelation <─────── Reveal play + nonce
Verify hash matches
6. Determine winner Determine winner
- Rust toolchain (rustc, cargo)
git clone <repository-url>
cd crypto-rock-paper-scissors
cargo build --releaseTerminal 1 (Server):
cargo run
# Select 's' for Server mode
# Choose your play (r/p/s)Terminal 2 (Client):
cargo run
# Select 'c' for Client mode
# Choose your play (r/p/s)The game will:
- Exchange cryptographic commitments
- Reveal plays and nonces
- Validate both players played fairly
- Display the winner
- Collision Resistance: SHA-256 makes it computationally infeasible to find two different inputs that produce the same hash
- Pre-image Resistance: Cannot reverse the hash to discover the original play
- Nonce Randomness: 256-bit random nonce ensures the same play hashes differently each game
- Replay attacks: Random nonce prevents reusing old commitments
- Brute force: Only 3 possible plays, but 256-bit nonce makes brute force impractical
- Timing attacks: Both commitments exchanged before any reveals
- Man-in-the-middle: Would be detected during validation phase
Messages are JSON-encoded:
Commitment:
{"HashedPlay": "a3f5d8..."}Revelation:
{"Explanation": {"play": "r", "nonce": "7c8f3d..."}}Current State:
- Single round per execution
- Local network only (localhost)
- Basic error handling
- No reconnection support
Potential Enhancements:
- Best-of-N tournament mode
- Remote play over internet
- Persistent game statistics
- GUI interface
- Multiple simultaneous games
- Configurable network settings
This project demonstrates practical applications of:
- Cryptographic commitments in distributed systems
- Zero-knowledge protocols (hiding information until reveal)
- Byzantine fault tolerance (playing fairly with untrusted parties)
- Mental poker protocols (the same concept used in online card games)
The commit-reveal pattern is used in:
- Blockchain smart contracts (preventing front-running)
- Secure multiparty computation
- Online poker and gambling
- Sealed-bid auctions
- Voting systems