What is a Commit-Reveal Scheme in Blockchain?

BeginnerNov 22, 2023
Learn about the Commit-Reveal method, a blockchain innovation that improves privacy and security in decentralized digital transactions.
What is a Commit-Reveal Scheme in Blockchain?

Blockchain technology brings innovative approaches to achieving consensus, security, and privacy. One such innovation is the commit-reveal scheme, a cryptographic protocol that enhances privacy and security for blockchain users. As you navigate this course, you will unfold the layers of trust, transparency, and innovation that the Commit-Reveal scheme brings to the decentralized digital domain. Each module in this article is meticulously crafted to provide a well-rounded understanding of the Commit-Reveal scheme, its implementation, and its impact on the blockchain landscape.

What is Blockchain?

At its core, a blockchain is like a digital ledger with a twist. Unlike traditional ledgers, it’s decentralized, meaning no single entity has control over the entire blockchain, and it’s accessible to anyone who’s part of its network. Each ‘block’ in a blockchain carries a list of transactions. Once a block gets filled with transactions, a new block is formed, creating a connected ‘chain’ of blocks - hence the name, ‘blockchain.’

One of the shining features of blockchain is its emphasis on trust and transparency. Anyone in the network has access to and can view every transaction on a blockchain. This openness discourages dishonesty and fosters trust among the community. You might be wondering, how does it achieve such a level of transparency? The answer lies in its unique consensus mechanisms and cryptographic principles, which we will delve into as we progress in this course.

Introduction to Commit-Reveal Scheme

Now that you have a taste of what blockchain is, let’s introduce a special flavor to it - the Commit-Reveal scheme. It’s like a magic show where a magician (the user) first commits to a certain act without revealing it to the audience (the network) and then, at a later stage, unveils the act. In blockchain terms, this is a two-step process: the ‘Commit Phase’ and the ‘Reveal Phase’.

In the Commit Phase, a user submits a hashed version of their information to the blockchain. This hashed version is like a scrambled version of the original information that hides the actual content. The magic of the hash function is that it’s a one-way journey; you can’t unscramble the hash back to its original form, thus keeping the information secret for the time being. Then comes the Reveal Phase, where the user reveals the original information, which is then verified by the network by hashing it again and comparing it to the initial committed hash if it matches, voila! The network confirms the reveal, and the magic trick is complete.

Through the Commit-Reveal scheme, blockchain networks can achieve a new level of engagement in applications like auctions, voting systems, and more, which we will explore in detail in the upcoming modules.

The Commit Phase

As we sail into the depths of the Commit-Reveal scheme, our first stop is the ‘Commit Phase.’ In this phase, users “commit” to a certain value, but they do it in a way that keeps the actual value a secret. Imagine you have a secret number in mind, but instead of telling everyone what it is, you put it in a locked box and show it to everyone. They know you have a number, but they don’t know what it is. That’s what committing is all about in blockchain!

Now, how do we lock away our secret value? In the blockchain world, we use something called a hash function to do this. A hash function is like a magical blender. You put in your secret value, and it spits out a scrambled version of it, known as a hash. This hash is unique; even a tiny change in the original value creates a wildly different hash. The beauty of it is that it’s a one-way process - once the value is hashed, there’s no easy way to figure out the original value from the hash. So, when users commit their value, what they’re really doing is sharing the hash of their value with everyone on the blockchain.

The Reveal Phase

After the suspense built in the Commit Phase, it’s time for the grand reveal! The Reveal Phase is where the committed values are finally unveiled to all the participants on the blockchain. Going back to our locked box analogy, this is the moment when the box is opened, and everyone gets to see what number you had hidden away. In the blockchain world, revealing is a straightforward but crucial process that provides transparency and fairness in various applications.

When a user reveals their committed value, the network can easily verify its authenticity by hashing the revealed value and comparing it to the original hash shared during the Commit Phase. If the hashes match, it confirms that the user hasn’t changed their mind midway. This simple yet powerful verification process helps in maintaining a trustful environment where everyone plays by the rules. It’s like having a referee in a game, ensuring that all players adhere to the agreed terms.

Implementing Commit-Reveal in Smart Contracts

Now that we have unraveled the magic of the Commit-Reveal scheme let’s see how it comes to life in the blockchain world through smart contracts. A smart contract is like a traditional contract, but it’s digital and self-executing. Imagine an impartial robot that automatically ensures all parties stick to their agreements, and you’ve got the idea of a smart contract. It’s the tool that takes the theoretical idea of Commit-Reveal and makes it practical in the digital realm. Implementing a Commit-Reveal scheme via a smart contract is like choreographing a dance where each step follows a set rhythm. Let’s walk through this choreography step-by-step.

Creating The Contract

Start by creating a smart contract on a blockchain platform like Ethereum. This contract will have the rules of engagement for the Commit-Reveal scheme encoded in it.

The Commit Function

Design a commit function within the smart contract. This function will allow users to submit their hashed values (the commit phase) to the contract.

Storing Committed Values

As users commit their values, the smart contract will store these hashes securely on the blockchain, awaiting the reveal phase.

The Reveal Function

Next, within the smart contract, create a reveal function. When the time comes, this function will allow users to reveal their original values.

Verification and Validation

Upon revealing, the smart contract will verify the revealed values by hashing them and comparing them with the initially committed hashes. If everything matches, the reveal is validated.

Finalization

Once all reveals are collected and verified, the smart contract can automatically execute the next steps, be it tallying votes, determining auction winners, or any other application-specific action.

Auditing and Testing

Before deploying, thoroughly test the smart contract to ensure it operates as intended and is secure from potential exploits. Through these steps, the smart contract orchestrates the Commit-Reveal dance, ensuring a fair and transparent process.

Code Example

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract CommitReveal {

struct Commit {

    bytes32 hash;

    bool revealed;

}



mapping(address => Commit) public commits;

mapping(address => uint256) public revealedValues;



// The commit function allows users to submit their hashed values

function commit(bytes32 _hash) public {

    require(commits[msg.sender].hash == 0, "Already committed");

    commits[msg.sender].hash = _hash;

}



// The reveal function allows users to reveal their original values

function reveal(uint256 _value, string memory _salt) public {

    require(commits[msg.sender].revealed == false, "Already revealed");

    require(keccak256(abi.encodePacked(_value, _salt)) == commits[msg.sender].hash, "Hashes don't match");

    commits[msg.sender].revealed = true;

    revealedValues[msg.sender] = _value;

}



// An example finalization function that could tally votes

function tallyVotes() public view returns (uint256) {

    uint256 totalVotes = 0;

    for (address addr = address(0); addr < address(-1); addr++) {

        if (commits[addr].revealed) {

            totalVotes += revealedValues[addr];

        }

    }

    return totalVotes;

}



// A function to audit the contract state (just an example, not practical for large datasets)

function audit() public view returns (mapping(address => uint256) memory) {

    return revealedValues;

}

 }

Explanation:

  • Creating The Contract: The CommitReveal contract is created.
  • The Commit Function: The commit function accepts a hashed value (_hash) and stores it against the sender’s address.
  • Storing Committed Values: The hashed values are stored in a mapping called commits.
  • The Reveal Function: The reveal function allows users to submit their original value (_value) and a salt (_salt). It checks that the hash of these matches the initially committed hash.
  • Verification and Validation: This is done within the reveal function by comparing hashes.
  • Finalization: The tallyVotes function is a simplified example of how you might tally votes. It iterates through all possible addresses (impractical for large datasets) and sums the revealed values.
  • Auditing and Testing: The audit function is a very simplistic example of how you might check the contract state.

This is a very simplified example and lacks many practical considerations you’d need for a production system (like preventing overflows, optimizing for gas usage, managing large datasets, and adding proper access control). The finalization function, in particular, is impractical due to the iteration over all possible addresses and would need a different design in a real-world scenario. This code is intended as a starting point and educational tool, not a production-ready solution.

Use Cases of Commit-Reveal Scheme in Blockchain

Enhancing Auction Platforms

Online auctions are one of the quintessential applications of the Commit-Reveal scheme. To prevent others from using the bid amounts as leverage, participants can submit their bids covertly during the commit phase. When the bidding period concludes, the reveal phase kicks in, allowing participants to disclose their bids. The highest valid bid is determined, ensuring a fair and competitive auction process.

Creating Fair Voting Systems

Voting systems can significantly benefit from the Commit-Reveal scheme, especially in scenarios demanding anonymity and honesty from voters. In this setup, during elections or any voting event, voters commit to their choices in the commit phase without revealing them to others, ensuring the integrity and secrecy of votes. Once the voting period concludes, the reveal phase kicks in, allowing voters to disclose their votes. These are then tallied to determine the outcome, promoting a fair and transparent voting process.

Sealed-bid Contracts

In sealed-bid contracts, bidders submit their bids concealed using the Commit-Reveal scheme. This process ensures that none of the bidders know the bid amounts of others, fostering a fair competition. After the bid submission period, the reveal phase occurs, and the contract is awarded to the qualified bidder with the best offer.

Game Scenarios

The Commit-Reveal scheme finds a fun application in-game scenarios such as a digital version of Rock, Paper, and Scissors. Players commit their choices without revealing them, ensuring a fair game. Once both players have committed, the reveal phase follows, determining the winner based on the choices made.

Mitigating Front-running Issues

In blockchain environments, front-running is a concern where malicious actors could potentially benefit from the knowledge of pending transactions. The Commit-Reveal scheme helps mitigate such issues by concealing transaction details initially. By the time the reveal phase comes around, it’s too late for malicious actors to act on the information, thus preserving transaction integrity​.

Low-Latency Commit-and-Reveal Architectures

Newer architectures like F3B have evolved to reduce the overhead associated with traditional Commit-Reveal schemes. F3B minimizes data writing onto the blockchain, offering a low-latency Commit-and-Reveal architecture. This development is notable as it signifies the continuous optimization and innovation in Commit-Reveal schemes, making them more efficient for real-world applications.

Random Number Generation

In blockchain environments, generating random numbers can be a challenge due to the deterministic nature of blockchain protocols. The Commit-Reveal scheme serves as a decentralized alternative for generating random numbers on the Ethereum blockchain. For example, Randao, a Commit-Reveal RNG implementation, utilizes public data feeds and incentivizes participation in random number generation​.

Conclusion

As this interesting look at the Commit-Reveal scheme comes to a close, it is important to consider how this simple but powerful mechanism helps build trust and openness in blockchain applications. We got a sneak peek at how blockchain technology could change many fields by making auctions fair, voting systems honest, and game scenarios more creative, among other things, with the Commit-Reveal scheme. The journey does not end here; the constantly evolving nature of blockchain technology presents new horizons to explore. Armed with the knowledge acquired through this course, you are well-positioned to delve deeper into the blockchain, explore its endless possibilities, and contribute to shaping a transparent, fair, and decentralized digital future.

Author: Piero
Translator: Cedar
Reviewer(s): Matheus、Edward、Ashley He
* The information is not intended to be and does not constitute financial advice or any other recommendation of any sort offered or endorsed by Gate.io.
* This article may not be reproduced, transmitted or copied without referencing Gate.io. Contravention is an infringement of Copyright Act and may be subject to legal action.
Start Now
Sign up and get a
$100
Voucher!
Create Account