Mental Texas Hold'em is a Peer-to-Peer, serverless Texas Hold'em game that runs purely in browsers using WebRTC.
Play a live demo here.
The game leverages a WebRTC framework called PeerJS, where no peer-to-peer data goes through the server once connections are established. Technically, there is no "Game Server" running in the background. Instead, every player is involved in "serving" the game, including shuffling the deck and dealing cards. The host, who creates the game, merely acts as a hub that proxies the data sent by those who join the game later.
You may ask, is it still a fair game? Well, here is the most interesting part of this project.
The game ensures fairness using a cryptographic protocol called Mental Poker, which allows two or more players to play a fair game of poker without a trusted third party.
For a full explanation, you can check out the Mental Poker Wikipedia. Here’s a simplified overview:
- There are two players, Alice and Bob.
- Alice and Bob shuffle and encrypt the deck one after another using a commutative encryption algorithm.
- Then, they decrypt the deck using their private keys and encrypt each card individually using different keys.
- Finally, the deck is shuffled and double-encrypted by Alice and Bob. No player can view the card values unless both players agree to share their private keys for a card.
The commutative encryption algorithm can be described as follows:
where,
-
$m$ is the plaintext (the Card), -
$c$ is the double-encrypted ciphertext, -
$k_1$ and$k_2$ are the keys of the players Alice and Bob, -
$f_{k_n}$ and$f_{k_n}^{-1}$ are the encryption and decryption functions using key$k_n$ ,
This cryptographic protocol guarantees that no one can know the card values until they mutually decrypt the deck.
This protocol is implemented in the separate project: mental-poker-toolkit, also available on NPM.
Currently, there is no fault tolerance implemented. If a peer disconnects or leaves the game, the game will be interrupted, and no recovery mechanism is in place.
The game uses SRA, a variant of RSA, as the commutative encryption algorithm. This encryption and decryption process can be CPU-intensive, particularly with large key sizes (e.g., greater than 128 bits).
Since these cryptographic operations are run in the browser using JavaScript, performance may be slower compared to native applications. Additionally, the ciphertext size increases exponentially with key size, potentially causing network latency during deck shuffling if the key length is too large.
If you find any bugs or have suggestions, please feel free to open an issue.
This project is licensed under the MIT License.