Cryptographic commitments
A sealed envelope made of a hash
A commitment works like a sealed envelope. First you send C — the envelope with the value m inside — and only later the opening: m and whatever you sealed the envelope with. The scheme needs two properties. Hiding: C alone tells nothing about m. Binding: C opens to one value only, so once it is sent you cannot change your mind. The simplest construction is a hash, C = SHA-256(r ‖ m), where r is 32 random bytes from a cryptographic generator and ‖ joins the bytes. The random r gives hiding; SHA-256’s collision resistance gives binding. r has a fixed length, so the receiver knows where r ends and m begins.
Manuel Blum posed the problem in 1981 in “Coin Flipping by Telephone”: how to flip a coin over the phone when neither side can see the other’s coin. His protocol was built on factoring; here the commitment is a hash. In the toy you play rock, paper, scissors over the network. Whoever answers second could cheat — unless the first one sends a commitment. Your browser simulates the opponent, and WebCrypto computes SHA-256.
Rock, paper, scissors over the network
Pick a move and commit. The opponent gets only C — a hash that does not give the move away.
You
Guessing the contents and changing your mind
Hiding rests on r. Without it C = SHA-256(“rock”), and with only three moves the opponent computes three hashes and reads your move before the opening. A short r hardly helps: with 24 bits there are 3 · 2²⁴, about 50 million, (r, move) pairs, and a browser goes through all of them. Try it in the toy’s “Break it” mode above — pick a length of r, and the toy measures the rate and works out how long a longer r would take. Any commitment to a value from a small set falls the same way: a “yes” or “no” vote, a rating from 1 to 5, a round bid amount.
Binding rests on the hash function. To open C as a different move after the opponent’s move, you need two pairs (r, m) and (r′, m′) with different moves and the same hash — a collision. The cheater does not have to find it after the fact: two openings with the same hash, found before sending C, are enough, so the birthday paradox applies and an n-bit hash takes about 2n/2 attempts. For the full SHA-256 that is about 2¹²⁸ — NIST rates SHA-256 at 128 bits of collision resistance (SP 800-57 Part 1 Rev. 5, Table 3). Below, the opponent compares only the first 16, 24 or 32 bits of C, so you find a collision in the browser. Why 2n/2 and not 2n is shown at the Hash functions station.
Two openings of one commitment
Toy implementation — not for productionThe opponent compares only the first 16 bits of C, not the whole SHA-256.
You compute SHA-256(r ‖ m) for successive 32-byte r, with the three moves in turn, and look for two openings with different moves and the same first 16 bits — a collision of the truncated hash.
Expected: about 393 attempts, although there are 65,536 possible values — the birthday paradox.
Press “Play” or → and hash the next openings.
Attempts: 0
Where commitments keep the order
- ENS: sealed-bid auctions (2017)From 4 May 2017, .eth names in the Ethereum Name Service were allocated by Vickrey auctions: the highest bid won and paid the second-highest. For 72 hours bids were submitted as a hash of the amount with a random salt, and for the next 48 hours they were revealed — nobody saw other bids while the auction ran.Source (ENS: sealed-bid auctions (2017))
- ENS: registration without peekingThe .eth registrar controller used commit–reveal against front-running: first a hash of the registration data with a random 32-byte secret, then, at least 60 seconds and at most 24 hours later, the registration with the data revealed. The hash is keccak256, not SHA-256. With the launch of ENSv2, new registrations through this controller were disabled.Source (ENS: registration without peeking)
- UMA: voting with hidden votesUMA’s Data Verification Mechanism (DVM) resolves price requests by commit–reveal voting: voters first send a keccak256 hash of their vote with a salt, their address and the round data, then reveal the vote. The salt becomes public with the vote, so the contract code warns never to reuse it.Source (UMA: voting with hidden votes)
- Schnorr and Fiat–Shamir (RFC 8235)In a Schnorr proof the prover first sends V = G × [v] — a commitment to a random v — and only then gets the challenge. In the non-interactive version the challenge is the hash c = H(G ‖ V ‖ A ‖ UserID ‖ OtherInfo), so it covers the commitment (the Fiat–Shamir transform).Source (Schnorr and Fiat–Shamir (RFC 8235))
- Monero: hidden amountsMonero hides transaction amounts in Pedersen commitments: only the recipient knows the input and output amounts, yet anyone can check that the sums match and the transaction does not create monero out of thin air.Source (Monero: hidden amounts)
- Lightning Network: pay for the revealAn HTLC payment is locked under payment_hash = SHA-256(payment_preimage), where the preimage is 32 bytes the recipient knows. Revealing the preimage claims the payment; a node rejects a preimage that does not hash to it.Source (Lightning Network: pay for the reveal)
In a hash commitment both properties rest on SHA-256. A Pedersen commitment C = gmhr (Pedersen, CRYPTO ’91) hides unconditionally — with a random r it is uniformly distributed whatever is inside — but binds only computationally: opening it to another value takes the discrete logarithm loggh. You cannot have both unconditionally: at least one side has to be computationally bounded (Halevi and Micali, CRYPTO ’96). Commitments also open zero-knowledge proofs: the order commitment, challenge, response is shown at the Ali Baba’s cave station; the Graph 3-coloring station does the same for a graph.
Random r, a domain tag, fixed lengths
Draw r from a cryptographic generator, at least 128 bits — 32 bytes cost the same. 128 bits is the security strength NIST considers acceptable beyond 2030 too (SP 800-57 Part 1 Rev. 5, Table 4). Every commitment gets a new r. Put a fixed domain tag first (application, purpose, version), so a commitment from one protocol does not fit another, and join only fixed-length fields or fields prefixed with their length. When opening, the receiver checks the length of r and compares the whole hash.
const enc = new TextEncoder();
const TAG = enc.encode('example.com/rps/commit/v1');
const r = crypto.getRandomValues(new Uint8Array(32));
// tag ‖ r (32 B) ‖ m — fixed-length r, m last
const data = new Uint8Array([...TAG, ...r, ...enc.encode(move)]);
const c = new Uint8Array(await crypto.subtle.digest('SHA-256', data));
// send c; keep r and move until the openingbyte[] r = RandomNumberGenerator.GetBytes(32);
byte[] c = SHA256.HashData([.. Tag, .. r, .. Encoding.UTF8.GetBytes(move)]);Don’t commit with a bare hash of the value: anyone who hashes the possible values opens SHA-256(“yes”) or
the hash of an amount. Don’t take a short r or an r from Math.random() (see the
Randomness station), and don’t use the same r twice — the same r and m give the same
C, so a repeat shows. Don’t truncate the hash and don’t join variable-length fields: with a variable-length r,
“7f3a” ‖ “100” and “7f3a1” ‖ “00” are the same bytes, so one C opens to two amounts.
// ✕ no r: three hashes are enough to read the move
const c = await crypto.subtle.digest(
'SHA-256',
new TextEncoder().encode(move),
);