# Fairness: commit-reveal

An epoch is 100 spins in a row.

1. At the start of an epoch, the server makes a `seed` (32 random bytes). The server shows it as 64 hex characters.
2. The server publishes the `commit`. It is `sha256:` and the lowercase hex digest of the 32 seed bytes. Read it with `GET /api/epoch`.
   Decode the hex `seed` to 32 bytes before you hash it or use it as a key.
3. For a spin, the server computes `h = HMAC-SHA256(key = the 32 seed bytes, message = decimal(spin_id) as UTF-8 text)`.
4. Reel `i` (0, 1 or 2) gets the index `uint32_be(h[4i..4i+4]) mod 100`.
5. The index picks the symbol with the cumulative weights:
   - 0 to 29: 🍒
   - 30 to 54: 🍋
   - 55 to 72: 🔔
   - 73 to 84: ⭐
   - 85 to 94: 💎
   - 95 to 99: 🤖
6. After the 100th spin, the server reveals the `seed` and starts a new epoch.

## How to check

1. Save the `commit` before the epoch ends.
2. After the reveal, get the `seed` with `GET /api/epoch/:id` and the spins with `GET /api/epoch/:id/spins`.
3. Decode the `seed` to 32 bytes. Make sure that `sha256:` and the SHA-256 hex digest of these bytes equal the saved `commit`.
4. Compute the reels of each spin with the steps above. Compare them with the server result.

`decimal(spin_id)` is the spin number as a decimal text, for example `"42"`. The bias of `mod 100` is
below 1e-7. The game accepts it.

## Worked example

- `seed` (64 hex characters): `000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f`
- `commit`: `sha256:630dcd2966c4336691125448bbb25b4ff412a49c732db2c8abc1b8581bd710dd`
- `spin_id`: `42` (the message is the two UTF-8 bytes of "42")
- Reel indexes: 82, 88, 50
- Reels: ⭐ 💎 🍋
