"Provably fair" shows up on crypto casino sites, loot-box games, giveaway bots and on-chain raffles. It sounds like a marketing slogan, but underneath it is a small and elegant piece of cryptography that any developer can check in a few minutes: a commit-reveal scheme built from SHA-256 and HMAC.
This post explains the idea, walks through a working Node.js implementation, shows how to remove the bias that many naive versions have, and lists what "provably fair" can and cannot tell you.
The problem it solves
When a server generates a random number for you, you normally have to trust it. It could pick the result after seeing your bet, or quietly retry until it gets an outcome it likes. A provably fair system removes that trust in three steps:
- Commit. Before you play, the server picks a secret server seed and publishes only its SHA-256 hash. The hash locks the server into that seed without revealing it.
- Contribute. You supply your own client seed, and every round uses an increasing nonce (0, 1, 2 and so on). The server cannot know your seed in advance, so it cannot pick a server seed that favours itself.
- Reveal. When you rotate seeds, the server reveals the old server seed. You hash it, check that it matches the commitment, and recompute every result from the seeds and nonces.
If the hash matches and every recomputed result matches what you were shown, the server could not have changed the outcomes after the fact.
A working implementation
Here is the whole thing in plain Node.js, with no dependencies:
const crypto = require('crypto');
// 1. Commit: the server picks a secret seed and publishes only its hash
const serverSeed = crypto.randomBytes(32).toString('hex');
const commitment = crypto.createHash('sha256').update(serverSeed).digest('hex');
// 2. Contribute: the player picks a client seed; every bet uses the next nonce
const clientSeed = 'pick-your-own-string';
function hmacBytes(serverSeed, clientSeed, nonce, cursor) {
return crypto.createHmac('sha256', serverSeed)
.update(`${clientSeed}:${nonce}:${cursor}`)
.digest(); // 32 bytes
}
// A uniform integer in [0, n) with no modulo bias
function uniformInt(serverSeed, clientSeed, nonce, n) {
const limit = Math.floor(2 ** 32 / n) * n; // largest multiple of n below 2^32
for (let cursor = 0; ; cursor++) {
const bytes = hmacBytes(serverSeed, clientSeed, nonce, cursor);
for (let i = 0; i < 32; i += 4) {
const x = bytes.readUInt32BE(i);
if (x < limit) return x % n;
}
}
}
// A dice roll from 0.00 to 99.99
const roll = nonce => uniformInt(serverSeed, clientSeed, nonce, 10000) / 100;
for (let nonce = 0; nonce < 3; nonce++) console.log(`bet ${nonce}: ${roll(nonce).toFixed(2)}`);
// 3. Reveal: after rotation anyone can check the seed against the commitment
const verify = (seed, hash) => crypto.createHash('sha256').update(seed).digest('hex') === hash;
console.log('commitment matches:', verify(serverSeed, commitment));
A few things make this work.
HMAC-SHA256 is the random number generator. Keyed with the server seed, it turns "client seed, nonce, cursor" into 32 bytes that look random to anyone who does not know the seed, yet are fully reproducible for anyone who does.
The cursor supplies more bytes when needed. Each cursor value produces a fresh 32 bytes from the same seeds, so a game that needs many values per round, like a shuffled deck, never runs out.
The commitment is safe to publish. A random 256-bit seed cannot be guessed from its SHA-256 hash, so showing the hash gives nothing away.
The bias many implementations get wrong
The obvious way to get a number from 0 to 9,999 is to read four bytes as an unsigned integer and take the remainder after dividing by 10,000. The problem is that 2^32 is not a multiple of 10,000. The remainder is 7,296, so results 0 to 7,295 each have one extra way of being produced and come up about one time in 430,000 more often than the rest.
That is tiny, but it is a real, systematic edge, and it is exactly the kind of detail a careful auditor checks. The fix is rejection sampling. Work out the largest multiple of n below 2^32, throw away any value at or above it, and take the next four bytes instead. The uniformInt function above does that. The chance of a rejection here is under two in a million, so the loop almost never runs twice.
Some platforms convert bytes into a fraction instead, for example by dividing by 256 to the power of the number of bytes used. That has its own small rounding effects. What matters for verification is that the method is documented precisely enough for you to reproduce it.
Check your own implementation
Before trusting any implementation, including your own, run a quick statistical sanity check:
const counts = new Array(10).fill(0);
let sum = 0;
const N = 1_000_000;
for (let nonce = 0; nonce < N; nonce++) {
const r = roll(nonce);
sum += r;
counts[Math.floor(r / 10)]++;
}
console.log('mean', (sum / N).toFixed(3)); // close to 49.995
console.log(counts.map(c => (c / N * 100).toFixed(2) + '%').join(' '));
Every bucket should come out close to 10 %, and the mean close to 49.995. A test like this catches gross mistakes such as off-by-one ranges or the wrong byte order. It will not reveal subtle bias, which is why the rejection step matters.
What to check on a real platform
When a site claims to be provably fair, look for these details:
- The hashed server seed is visible before your first bet with that seed.
- You can set your own client seed instead of only accepting a generated one.
- The nonce increases by one with every bet and is shown next to each result.
- The exact algorithm is documented: the message format, the byte order and how bytes become a result.
- Rotating seeds reveals the previous server seed.
- You verify with your own code, not only with the site's verify button, which runs the site's code.
Independent reviews help you find out whether a platform documents these details at all. A hands-on BC.Game review, for example, covers the casino's in-house games built on provably fair technology alongside its licensing and real withdrawal tests. That tells you where to start digging, and a script like the one above is how you finish.
What provably fair does not prove
The scheme proves that a result was not changed after the fact. It does not prove that:
- The game is a good bet. Payouts can be perfectly fair and still carry a house edge.
- You will get paid. A flawless random number generator says nothing about withdrawals, limits or solvency.
- Every result followed the published rules, unless you actually recompute them.
- Playing is legal where you live. Online gambling rules vary widely between countries. If you play at all, check the law, set limits and treat it as entertainment, not income.
The same pattern outside gambling
Commit-reveal is useful anywhere one party picks randomness that others need to audit: giveaway winners, tournament brackets, randomised trial assignments and sealed-bid auctions. When you want to remove the single trusted server entirely, look at verifiable random functions (VRFs) and public randomness beacons such as drand.
Wrapping up
- Commit with a hash, contribute a client seed and a nonce, then reveal and recompute.
- HMAC-SHA256 plus rejection sampling gives unbiased, reproducible results.
- Verify with your own code, not just the site's.
- Fair randomness is not the same as a fair deal.
Once you have built the 30-line version yourself, "provably fair" stops being a slogan and becomes something you can test.
Top comments (0)