This implementation of Liar's Dice uses a Mental Poker protocol to guarantee that all clients are playing by the rules. This enables the clients to police each other: you can be sure your opponent is playing fairly, even if you don't trust them or the server. The server simply manages the connection between players.
Theoretically, the game could be played without a server at all, provided the clients had some other way of communicating. However, I was unable to find a suitable open P2P chat client, so I was forced to write a server as well. I was able to delegate matchmaking to the server, and this significantly relaxed the requirements of the commitment schema without sacrificing the verifiable fairness of the game.
The commitment schema (or protocol) goes something like this (with Hash(x) being some hash function; SHA-512 is used in this implementation):
- Each player picks a secret number
Kand computesC = Hash(K)andD = Hash(C) - Each player reveals
Dto all other players. - Each player reveals
Cto all other players, who can verify thatD = Hash(C). This ensures that players didn't change theirCin response to other players revealing their values. - All revealed
Cs are added together in some commutative way to getS. - Each player calculates
Hash(C + S)(all players can do this for all other players) to obtain the turn ordering for the round (lowest to highest). - Each player privately calculates
R = Hash(K + S).Ris the player's private value; in Liar's Dice, it's used to generate their set of dice. - After the round, each player reveals
KandR, and other players can verify that this matches their commitmentC.