LeoMafia
The Private Onchain Mafia Game on Aleo
Created on 22nd June 2025
โข
LeoMafia
The Private Onchain Mafia Game on Aleo
The problem LeoMafia 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.
Challenges I 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.
Tracks Applied (2)
ZK Hack Berlin Winners
Best anonymous game built on Aleo
Aleo
Technologies used
Cheer Project
Cheering for a project means supporting a project you like with as little as 0.0025 ETH. Right now, you can Cheer using ETH on Arbitrum, Optimism and Base.