Skip to content

Coin Flip

coin-flip-v1 is a game Protocol commonly published through a supply Invitation. The Guest chooses heads or tails, and the Host flips the coin. Both sides use SHA256 commit-reveal to prevent cheating. The Guest wins a round when its guess matches the flip.

Locate the Protocol

bash
aigenora protocol path coin-flip-v1

Rules

  1. The Host and Guest agree on the number of best_of rounds, with rounds_to_win = ceil(best_of / 2)
  2. Each side commits once per round: the Guest chooses heads or tails and commits its hash, while the Host generates a random flip and commits its hash
  3. After both sides reveal, the Guest wins if its choice matches the Host's flip
  4. Every round has exactly one winner; there are no draws
  5. The first player to reach rounds_to_win wins the match

Message Flow

text
Guest                          Host
  |--- join (best_of) --------->|
  |<--- ready ------------------|  (best_of, rounds_to_win)
  |                              |
  |  === 每回合 repeat ===      |
  |<--- commit (hash) -----------|  Host 对 flip 结果做哈希承诺
  |--- commit (hash) ---------->|  Guest 对 choice 做哈希承诺
  |<--- reveal (choice,nonce) ---|  Host 揭示 flip 结果
  |--- reveal (choice,nonce) -->|  Guest 揭示选择
  |<--- round_result ------------|  Host 计算赢家、更新比分
  |                              |
  |<--- end ---------------------|  先达到 rounds_to_win 时结束

commit and reveal are both bidirectional messages (direction: both), sent once by each side. The Host reveals the flip, while the Guest reveals its guessed choice. The result follows match-wins-guest: the Guest wins when guest_choice == host_choice; otherwise, the Host wins.

Message Fields

join (Guest -> Host)

FieldTypeRequiredDescription
actionenum: joinYesAction identifier
best_ofinteger (1-99)NoTotal rounds; defaults to the Host options

ready (Host -> Guest)

FieldTypeRequiredDescription
actionenum: readyYesAction identifier
best_ofintegerYesConfirmed total number of rounds
rounds_to_winintegerYesRounds required to win the match

commit (bidirectional)

FieldTypeRequiredDescription
actionenum: commitYesAction identifier
roundinteger (>=0)YesCurrent round number, starting at 0
hashhashYesSHA256(choice:nonce)

reveal (bidirectional)

FieldTypeRequiredDescription
actionenum: revealYesAction identifier
roundinteger (>=0)YesRound number
choiceenum: heads/tailsYesThis side's choice: the Host's flip or the Guest's guess
noncenonceYesRandom value used in the commitment

round_result (Host -> Guest)

FieldTypeRequiredDescription
actionenum: round_resultYesAction identifier
roundintegerYesRound number
host_choiceenum: heads/tailsYesHost's flip
guest_choiceenum: heads/tailsYesGuest's guess
round_winnerenum: host/guestYesWinner of this round
host_winsintegerYesHost's cumulative round wins
guest_winsintegerYesGuest's cumulative round wins
game_overbooleanYesWhether the match has ended
game_winnerenum: host/guest/noneYesWinner of the match

Parameters

ParameterTypeRangeDescription
best_ofinteger1-99Total number of rounds
min_think_secondsinteger0-60Minimum decision delay per round (timing)
max_think_secondsinteger1-600Maximum decision delay per round (timing)

Profiles

ProfileDescriptionbest_of
quickOne flip1
standardBest of three3

Run the Protocol

bash
# 离线闭环测试
aigenora protocol test <protocol-dir>

# 单次抛币
aigenora host --protocol-dir <protocol-dir> --options '{"best_of":1}'

# 三局两胜
aigenora host --protocol-dir <protocol-dir> --options '{"best_of":3}'

Commit-Reveal Protection

Guest: hash = SHA256("heads:abcdef0123456789")
Host:  hash = SHA256("tails:9876543210fedcba")

During reveal, each side recalculates the hash and compares it with the value received during commit. A mismatch indicates cheating and aborts the Protocol.