Skip to content

Implementation of a voting system using the commit-reveal scheme in Solidity.

Notifications You must be signed in to change notification settings

count-sum/Commit-Reveal-Voting

Repository files navigation

Commit-Reveal Voting Smart Contract

This is a secure implementation of a voting system using the commit-reveal scheme in Solidity. The system ensures vote privacy during the voting period and prevents vote manipulation.

Architecture

The system is split into several contracts, each with a specific responsibility:

  1. CommitRevealVoting.sol: Main contract that orchestrates the voting process
  2. VotingPhases.sol: Library handling the different phases of voting
  3. VoterRegistry.sol: Manages voter registration
  4. CommitRevealLogic.sol: Handles the commit-reveal voting logic

How It Works

The voting process has three phases:

  1. Commit Phase:

    • Voters submit a hash (commitment) of their vote and a secret
    • The actual vote remains hidden during this phase
  2. Reveal Phase:

    • Voters reveal their vote and secret
    • The contract verifies that the hash matches the original commitment
  3. Results Phase:

    • After the reveal phase ends, anyone can query the results

Usage

For Voters

  1. Register as a voter:
await voting.registerVoter(voterAddress);
  1. Create a commitment:
const secret = ethers.utils.randomBytes(32);
const commitment = ethers.utils.keccak256(
    ethers.utils.defaultAbiCoder.encode(
        ["bool", "bytes32"],
        [voteChoice, secret]
    )
);
  1. Submit your commitment during the commit phase:
await voting.commitVote(commitment);
  1. Reveal your vote during the reveal phase:
await voting.revealVote(voteChoice, secret);

For Admin

  1. Deploy the contract:
const voting = await CommitRevealVoting.deploy(commitDuration, revealDuration);
  1. Start the voting:
await voting.startVoting();
  1. Get results after voting ends:
const [yesVotes, noVotes] = await voting.getResults();

Security Features

  • Vote privacy during commit phase
  • Prevention of vote manipulation
  • Immutable vote commitments
  • Verifiable vote reveals
  • Phase-based access control

Testing

Run the tests using:

npx hardhat test

About

Implementation of a voting system using the commit-reveal scheme in Solidity.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published