Security Patterns
A commit-reveal scheme is a two-phase protocol where a participant first submits a hash of a secret value (the commit), then later submits the value itself (the reveal). Because the value is hidden during the commit phase, it cannot be front-run or copied, and no single party controls a combined outcome.
keccak256(value, salt). The value is hidden but binding.value and salt; the contract verifies the hash matches.mapping(address => bytes32) public commitment;
function commit(bytes32 hash) external { commitment[msg.sender] = hash; }
function reveal(uint256 value, bytes32 salt) external {
require(commitment[msg.sender] == keccak256(abi.encodePacked(value, salt)), "bad reveal");
// use value now that it can't be front-run
}
Q: How does commit-reveal stop front-running?
A: During the commit phase the value is only a hash, so observers and block proposers cannot see or copy it. By the time it is revealed, ordering no longer gives an advantage.
Q: Is commit-reveal a full randomness solution?
A: It helps by combining multiple hidden inputs, but a single participant can still refuse to reveal. For strong on-chain randomness, combine it with an external source like Chainlink VRF.
Related terms
Firepan
Run a free surface scan — results in minutes, no credit card required.
Run Free Scan →