Skip to content

Instantly share code, notes, and snippets.

@critesjosh
Created May 9, 2023 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save critesjosh/bfddfdab1c9e7f4eb5cec57105526508 to your computer and use it in GitHub Desktop.
Save critesjosh/bfddfdab1c9e7f4eb5cec57105526508 to your computer and use it in GitHub Desktop.
// https://gist.github.com/Turupawn/6a4391fb54d09aae7a091ad2478c1f62#file-zkvoting-sol
//SPDX-License-Identifier: MIT
pragma solidity >=0.8.19;
contract zkVoting is UltraVerifier {
struct Proposal {
string description;
uint deadline;
uint forVotes;
uint againstVotes;
}
bytes32 merkleRoot;
uint proposalCount;
mapping (uint proposalId => Proposal) public proposals;
mapping(bytes32 hash => bool isNullified) nullifiers;
constructor(bytes32 _merkleRoot) {
merkleRoot = _merkleRoot;
}
function propose(string memory description, uint deadline) public {
proposals[proposalCount] = Proposal(description, deadline, 0, 0);
proposalCount += 1;
}
function castVote(bytes calldata proof, uint proposalId, uint vote, bytes32 nullifierHash) public {
require(!nullifier[proof], "Proof has been already submitted");
require(block.timestamp < proposals[proposalId].deadline, "Voting period is over.");
nullifier[proof] = true;
bytes32[] memory publicInputs = new bytes32[](4);
publicInputs[0] = bytes32(merkleRoot);
publicInputs[1] = bytes32(proposalId);
publicInputs[2] = bytes32(vote);
publicInputs[3] = bytes32(nullifierHash);
require(verify(proof, publicInputs), "Invalid proof");
if(vote == 1)
proposals[proposalId].forVotes += 1;
else if (vote == 2)
proposals[proposalId].againstVotes += 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment