Programming Languages

How can you flip a coin in Glow?

Flipping a coin is a game as old as, well, coins. Alice throws the coin in the air, Bob calls whatever he thinks is going to be the outcome. The loser pays the winner the agreed amount.

However, on blockchain, there is no such thing as in the air. So, how can we have something random that has a 50 / 50 chance for Alice?

For example, Alice flips her coin. Bob flips his coin And then Alice wins if both coins match. Or Bob wins if the coins are different. Both outcomes have a 50 / 50 chance of happening.

Sequence diagram

img

Glow code

The following Glow code example represents the previous interaction:

1 #lang glow
2 // TODO: insert escrow in a compiler pass
3 @interaction([A, B])
4 let coinFlip = (wagerAmount) => {
5 // @A assert! canReach(A_wins);
6 @A let randA = randomUInt256();
7 @verifiably!(A) let commitment = digest(randA);
8 publish! A -> commitment; deposit! A -> wagerAmount+escrowAmount;
9 //@B assert! canReach(B_wins);
10 @B let randB = randomUInt256();
11 publish! B -> randB; deposit! B -> wagerAmount;
12 publish! A -> randA;
13 verify! commitment;
14 if (((randA ^^^ randB) &&& 1) == 0) {
15 //A_wins:
16 withdraw! A <- 2*wagerAmount + escrowAmount
17 } else {
18 //B_wins:
19 withdraw! B <- 2*wagerAmount
20 withdraw! A <- escrowAmount
21 }
22 };
  • 1 Every Glow program starts with the #lang glow identification
  • 3 We know that two actors, A and B (Alice and Bob), are going to participate in this contract
  • 4 coinFlip needs to know the amount each player is going to bet to get started.
  • 5 Alice needs to be assured that there is a state where she can win. Ie. assert! makes sure that the program can reach the label A_wins
  • 6 Alice draws random numbers between 0 and 2^256.
  • 7 Alice stores in the commitment value the digest of the random number she generated.
  • 8 She publishes her commitment on the blockchain and deposits her wager.

Now it is Bob's turn:

  • 9 Bob makes sure that it is possible to get to the B_wins label to know that he can win.
  • 10 Bob flips a coin. i.e., draws a random number between 0 and 2<sup>256</sup>.
  • 11 Bob publishes the coin that he flipped and deposits his wager.
  • 12 Alice publishes the coin that she threw.
  • 13 We verify! that the commitment matches the coin that she threw.
  • 14 By doing a bitwise XOR of the random numbers thrown by Alice and Bob, we find that they match
  • 16 Alice wins and withdraws two times the wager
  • 18 However, If the xor doesn't match, Bob wins
  • 19 In addition, Bob gets double the bet.

Lessons learned

  • You can generate random numbers with randomUInt256
  • You can assert! you can reach a label such as A_wins:
  • There are bitwise operations like ^^^ and &&&

© IOHK 2015 - 2021


Cardano is an open-source project.

Cardano is a software platform ONLY and does not conduct any independent diligence on, or substantive review of, any blockchain asset, digital currency, cryptocurrency or associated funds. You are fully and solely responsible for evaluating your investments, for determining whether you will exchange blockchain assets based on your own judgement, and for all your decisions as to whether to exchange blockchain assets with Cardano. In many cases, blockchain assets you exchange on the basis of your research may not increase in value, and may decrease in value. Similarly, blockchain assets you exchange on the basis of your research may fall or rise in value after your exchange.

Past performance is not indicative of future results. Any investment in blockchain assets involves the risk of loss of part or all of your investment. The value of the blockchain assets you exchange is subject to market and other investment risks