Logo
Back to Hub

Rock Paper Scissors

Two-player game with encrypted moves

Gaming
Documentation

Rock Paper Scissors

A two-player encrypted game demonstrating the commit-reveal pattern using Fully Homomorphic Encryption. This template shows how to implement fair multiplayer gaming where players cannot see each other's moves until both have committed, preventing cheating and ensuring trustless gameplay.

What You'll Learn

This template demonstrates the commit-reveal pattern for two-player games, encrypted move submission to prevent front-running, game state management for multiple concurrent matches, encrypted comparison and conditional logic for determining winners, and fair gaming mechanics without a trusted third party.

Core Patterns

Commit-Reveal Pattern

The commit-reveal pattern is essential for fair two-player games. Players first commit their encrypted moves without revealing them. Only after both players have committed their moves does the contract evaluate the winner. This prevents the second player from seeing the first player's move and choosing their move accordingly.

Loading...

Encrypted Winner Determination

The contract uses encrypted boolean logic to determine the winner without decrypting the moves. It checks all possible win conditions using FHE comparison and logical operations. Rock beats Scissors, Paper beats Rock, and Scissors beats Paper. The result is calculated using encrypted conditionals that select between draw, player 1 wins, or player 2 wins.

Loading...

Game Flow

The game follows a structured multi-step process. First, Player 1 creates a game by specifying a bet amount, which locks their funds. Player 2 then joins the game by matching the bet, which also locks their funds. Both players commit their encrypted moves where 1 represents Rock, 2 represents Paper, and 3 represents Scissors. Once both moves are committed, the contract automatically determines the winner using encrypted logic. Finally, players can decrypt the result client-side to see the outcome and the winner receives the combined pot.

Fairness Guarantees

Moves are encrypted using FHE, ensuring neither player can see the other's choice before committing. Both players must commit their moves before the winner is determined, preventing strategic advantage. The encrypted logic makes front-running impossible since moves cannot be observed in the mempool. Winner calculation is deterministic and based on the classic game rules, ensuring no manipulation is possible. The contract acts as a trustless referee without seeing the actual moves.

Client-Side Usage

Loading...

Move Encoding

The contract uses a simple numeric encoding for moves. The value 1 represents Rock, 2 represents Paper, and 3 represents Scissors. Any other value is invalid and should be validated client-side before submission. This encoding allows for efficient encrypted comparison operations.

Key Concepts

The commit-reveal pattern prevents information leakage by ensuring both players commit before any reveal. Encrypted boolean logic using FHE.and, FHE.or, and FHE.eq enables complex game rule evaluation without decryption. Game state tracking maintains multiple concurrent games with independent state for each match. Permission management ensures only game participants can access their own moves. The contract provides trustless execution where no centralized authority is needed to determine the winner.

Production Considerations

For production multiplayer gaming systems, you should integrate the Gateway pattern for result decryption to enable automatic payout distribution. Implement timeout mechanisms to handle cases where a player abandons the game after committing. Add re-entry protection and proper state validation to prevent exploit attempts. Replace the mock balance system with integration to confidential tokens for real value transfers. Consider adding a ranking or ELO system for competitive play. Implement game history tracking for player statistics and dispute resolution.

Comparison with Traditional Approaches

Traditional smart contract implementations of Rock Paper Scissors face a fundamental problem: the second player can see the first player's move in the transaction mempool and choose their move accordingly. Previous solutions require complex commit-reveal schemes with hash commitments and reveal phases, adding multiple transaction rounds and gas costs. FHE eliminates these problems entirely by allowing both players to commit encrypted moves simultaneously without the need for separate reveal transactions or hash-based commitments.

Quick Start

Loading...

Next Steps

See the dice-game template for single-player encrypted gaming patterns. The confidential-bank template demonstrates balance management that can be extended for gaming economies. Refer to advanced gaming patterns for tournament systems, matchmaking, and multiplayer scenarios beyond two players.

Loading...