| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147 |
15×
14×
13×
13×
13×
13×
13×
13×
13×
13×
6×
6×
5×
5×
5×
1×
4×
3×
3×
3×
3×
3×
3×
3×
1×
2×
2×
2×
2×
2×
2×
2×
2×
2×
2×
2×
2×
3×
1×
| // SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.20;
import "@openzeppelin/contracts/utils/cryptography/MerkleProof.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Pausable.sol";
import "./Verifier.sol";
contract ArtemisZkVoting is Ownable, Groth16Verifier, Pausable {
Groth16Verifier public verifier;
struct Proposal {
bytes32 merkleRoot;
string proposalDescription;
mapping(bool => uint256) votes; // true: support, false: against
mapping(bytes32 => bool) voted; // leaf: hasVoted
uint256 startTime;
uint256 endTime;
uint256 quorum; // Expressed as a percentage
uint256 passcodeHash;
bool hasEnded;
}
mapping(uint256 => Proposal) public proposals;
uint256 public proposalCount;
// Events
event ProposalAdded(uint256 proposalId, string description);
event VoteReceived(uint256 proposalId, bool support);
event ProposalEnded(
uint256 proposalId,
bool achievedQuorum,
bool supported
);
// Custom errors
error InvalidVote();
error AlreadyVoted();
error InvalidZkSnarkProof();
error InvalidMerkleProof();
error InvalidProposal();
error VotingEnded();
error NotYetStarted();
constructor(address initialOwner) Ownable(initialOwner) {}
function addProposal(
string memory description,
bytes32 _merkleRoot,
uint256 duration,
uint256 _quorum,
uint256 passcodeHash
) external onlyOwner {
require(duration > 0, "Duration should be greater than zero");
require(_quorum <= 100, "Quorum should be between 0 and 100");
proposalCount += 1;
proposals[proposalCount].merkleRoot = _merkleRoot;
proposals[proposalCount].proposalDescription = description;
proposals[proposalCount].startTime = block.timestamp;
proposals[proposalCount].endTime = block.timestamp + duration;
proposals[proposalCount].quorum = _quorum;
proposals[proposalCount].passcodeHash = passcodeHash;
emit ProposalAdded(proposalCount, description);
}
function voteForProposal(
uint256 proposalId,
bool support,
bytes32[] calldata merkleProof,
bytes32 leaf,
uint256[] memory proof
) external EwhenNotPaused {
Proposal storage proposal = proposals[proposalId];
require(proposal.merkleRoot != 0, "Invalid proposal.");
Erequire(!proposal.hasEnded, "Voting has ended for this proposal.");
Erequire(
block.timestamp >= proposal.startTime,
"Voting not yet started."
);
// require(block.timestamp <= proposal.endTime, "Voting time has ended.");
if (block.timestamp > proposal.endTime) {
revert VotingEnded();
}
require(!proposal.voted[leaf], "Already voted.");
Erequire(proof.length == 8, "Invalid proof");
uint256[2] memory a = [proof[0], proof[1]];
uint256[2][2] memory b = [[proof[2], proof[3]], [proof[4], proof[5]]];
uint256[2] memory c = [proof[6], proof[7]];
uint256[2] memory input = [
proposal.passcodeHash,
uint256(uint160(msg.sender))
];
Iif (!this.verifyProof(a, b, c, input)) {
revert InvalidZkSnarkProof();
}
if (!verifyMerkleProof(proposal, merkleProof, leaf)) {
revert InvalidMerkleProof();
}
proposal.votes[support] += 1;
proposal.voted[leaf] = true;
emit VoteReceived(proposalId, support);
}
function endVoting(uint256 proposalId) external {
Proposal storage proposal = proposals[proposalId];
Erequire(
block.timestamp > proposal.endTime,
"Voting period is not yet over."
);
Iif (proposal.hasEnded) {
revert VotingEnded();
}
proposal.hasEnded = true;
uint256 totalVotes = proposal.votes[true] + proposal.votes[false];
bool achievedQuorum = false;
Iif (totalVotes > 0) {
achievedQuorum = (totalVotes * 100) / totalVotes >= proposal.quorum;
}
bool isSupported = proposal.votes[true] > proposal.votes[false];
emit ProposalEnded(proposalId, achievedQuorum, isSupported);
}
function verifyMerkleProof(
Proposal storage proposal,
bytes32[] memory proof,
bytes32 leaf
) internal view returns (bool) {
return MerkleProof.verify(proof, proposal.merkleRoot, leaf);
}
function pauseVoting() external onlyOwner {
_pause();
}
function resumeVoting() external onlyOwner {
_unpause();
}
function transferContractOwnership(address newOwner) external EonlyOwner {
transferOwnership(newOwner);
}
}
|