
LeoMafia
The Private Onchain Mafia Game on Aleo
About
The problem it solves # 🎭 LeoMafia: Zero-Knowledge Blockchain Mafia Game ## 🚀 Overview LeoMafia is a revolutionary implementation of the classic Mafia party game built on the **Aleo blockchain** using the **Leo programming language**. This project combines the strategic gameplay of traditional Mafia with cutting-edge **zero-knowledge proofs** and **decentralized smart contracts** to create a trustless, verifiable, and privacy-preserving gaming experience. ## 🌟 Innovation Highlights ### 🔐 Zero-Knowledge Privacy - **Hidden Role System**: Player roles are cryptographically committed using BHP256 hash functions - **Private Detective Investigations**: Investigation results are revealed only to the detective using ZK proofs - **Confidential Night Actions**: Mafia votes and Angel protections remain private until resolution ### ⛓️ Blockchain Integration - **Smart Contract Game Logic**: All game mechanics implemented in Leo smart contracts - **Immutable Game State**: Game progression and results stored on-chain - **Cryptographic Randomness**: Secure role assignment using blockchain-based entropy - **Stake-Based Participation**: Players commit tokens to participate, while voting for mafia, creating economic incentives ## 🎮 User Flow: From Joining to Winning ### 🛠️ Game Initialization - A player or game host deploys a new game instance via the **Leo smart contract**. - A unique `game_id` is assigned and broadcasted on-chain. ### 👥 Joining the Game - Players connect their **Aleo wallets** via the frontend (built with **Next.js + TypeScript**). - They **stake tokens** to enter the game, which also acts as their commitment. - Upon joining, each player is assigned a **role** (Villager, Mafia, Detective, Angel), which is: - Cryptographically committed using **BHP256 hashing** - Stored privately using a zero-knowledge commitment ### 🌙 Night Phase Mechanics Players with special roles perform secret actions via ZK proofs: - 🗡️ **Mafia** selects a player to eliminate. - 🔍 **Detective** investigates one player. The result is shown only to them via **ZK proof**. - 🛡️ **Angel** selects a player to protect from elimination. > All night actions are submitted as **zero-knowledge transactions**, where both inputs and results are hidden but cryptographically verifiable. ### ☀️ Day Phase & Voting - Surviving players **vote** to eliminate a suspected Mafia member. - Votes are: - Secured via **zero-knowledge proofs** - Committed on-chain using hashed commitments - Optional **vote staking** increases the weight of a vote, encouraging stronger commitment. ### 🔄 Game Progression & Elimination - The backend tracks **time-locked phases** using mappings. - Game transitions automatically between night and day. - Eliminated players are updated on-chain, but their roles remain secret. - The game continues until one side (Mafia or Village) fulfills their win condition. ### 🏆 Game Conclusion & Rewards - Once victory conditions are met (e.g., all Mafia are eliminated), the game ends. - **Roles are revealed**, and the **reward pool is automatically distributed** to the surviving winning team. - Distribution is handled fully on-chain using **Leo logic**. ## ⚙️ Technical Flow ### 1. Smart Contract Mechanics (Leo) - **Player Record**: Each player has a smart contract record containing owner, role, game_id, stake, and is_alive. - **Role Privacy**: Roles are hashed using **BHP256** and stored in a `role_commitments` mapping. - **Phase Timing**: `round_deadline` mapping handles deadlines for each phase, ensuring fair progression. - **Game Logic**: All player actions (investigation, vote, protect, kill) are implemented as verifiable private functions using **ZKPs**. --- ### 2. Frontend Infrastructure - Built using **Next.js** and **TypeScript**, interfaced with **Leo CLI** and the **Aleo blockchain**. - Connects directly to the **Leo Wallet** for transaction signing and role verification. - Implements **real-time state polling** to reflect game events and updates via on-chain reads. - Displays **cryptographic operations** (like proofs, commitments, hashes) in an intuitive format for players. --- ### 3. ZK Proof Usage - **Detective Results**: Players submit a proof to validate another’s role without revealing their own. - **Vote Validation**: Votes are cast as commitments; tallying is done through **ZK aggregation**. - **Protection and Kill Logic**: Intersecting ZK operations determine if a Mafia kill was blocked by the Angel. --- ### 4. Economic Design - **Stake to Join**: Each player deposits a stake, which forms the reward pool. - **Vote Staking**: Additional tokens can be staked to back a vote (e.g., higher conviction = higher weight). - **Reward Distribution**: On game completion, the prize pool is auto-distributed to the surviving winning team.
How it's made
Challenges we ran into - Gaining a solid understanding of Leo was challenging, especially during deployment and testing where we encountered several issues. - Wallets frequently threw errors related to locating contracts and differentiating between the testnet and testnetBeta environments. - Assigning the Detective role to a user was tricky, as it risked exposing sensitive information about players during detective phase. For now, we’ve implemented a centralized oracle to handle this logic, but we plan to transition to a decentralized oracle soon.