Be it to resolve a dispute, be it to bet money with a friend or simply to build your 256bit encryption key the good old way (good luck with weigthed or worn coins!), coin flipping is always a fairly practical solution.

But what happens when the two parties (Alice and Bob) aren't in the same place, without direct check of the coin throw and result?
Let's start with a simple consideration: you won't be able to guarantee that Alice, the party flipping the coin, will physically flip a coin.

Play safe: use a commitment scheme

Cryptography, instead, can resolve this issue, with a feature known as commitment scheme. Basically Bob, the betting party, does not initially reveal its bet (be it heads or tails), but provides its commitment; imagine it as a locked box containing the value of the bet, sent to Alice.

Alice therefore gains no information from the commitment itself, since she can't unlock it, and has no interest in faking the coin flip. She flips the coin and inform Bob of the result.

Bob then sends Alice the key that can unlock the commitment: with this key Alice can understand what was Bob's original bet.

tl;dr: cheat-proof coin flipping, period.

The project

The project is to make a simple TCP/IP protocol with bash to remotely flip a coin. The network communication is made via nc, netcat powered pipes.
The commitment is secured hashing it with the SHA1 algorithm (thanks to the sha1sum utility).

Download

Version Archive Download Size sha1 Hash
0.1 tar.lzma remote-coin-flip-0.1.tar.lzma 3.09k f6931b395ec7aca099535a71e38cb0952324f638
zip remote-coin-flip-0.1.zip 4.33k 4cefb063923e04b8be598c1554871e656e01684e

This program is released under the terms of the GPLv3 or any later version. See the details.

Documentation

Usage

  • Server: launch ./server.sh with no parameters, the program will listen on port 54300 for incoming clients.
  • Client: launch ./client.sh with the hostname of the server as first and only param. The script will try and connect to the specified host/IP address on port 54300.
  • Other data: both server and client will make use of zenity's graphical dialogs, if launched in an X display. To view messages sent under the cover pass them the global variable DEBUG, like this: DEBUG=1 ./server.sh

Protocol interaction example

SERVER (Alice) <-- --> CLIENT (Bob)

 --> HOWDY traveller.
 <-- HELLO buddy, what's up?.
 --> TIME 1286399555
 --> TOKEN eafb563db1a67634
 <-- TIME 1286399555
 <-- TOKEN b04bceaf40c3b0d9
 <-- COMMIT a130638e3b9ec2b6a8064c8c96cc1f00eafbd24f
 --> FLIPPED tails
 <-- BET  387a4675a2628ad25a753d0c6ca6a29c482aa811::heads::a6a81c4beaaa1b4d
 --> BYEBYE Bye bye.
 <-- BYEBYE Bye bye.