Skip to content
Lattice

PrivateVotingLib

Git Source

Title: PrivateVotingLib

Author: David Dada <daveproxy80@gmail.com> (https://github.com/dadadave80)

Library implementing anonymous one-person-one-vote polling over Semaphore groups. Each member votes once per poll via a Semaphore proof (scope = poll id, message = choice); the scope-bound nullifier enforces one ballot while the vote stays anonymous.

Three-layer pattern: this library holds the logic + namespaced storage; the stateless PrivateVoting facet forwards to it. Composes SemaphoreLib for the membership + ZK check (the ISemaphore module must be present in the same diamond and the group created there).

Functions

privateVotingStorage

function privateVotingStorage() internal pure returns (PrivateVotingStorage storage $);

Returns

NameTypeDescription
$PrivateVotingStorage

__PrivateVoting_init

Initializes the PrivateVoting module.

Must be called inside a pre/postInitializer block. Registers IPrivateVoting for ERC-165.

function __PrivateVoting_init() internal;

registerInterface

Registers support for the IPrivateVoting interface via ERC-165.

function registerInterface() internal;

createPoll

Creates a poll over groupId. Only the Semaphore group admin may create it.

function createPoll(uint256 groupId, uint32 numChoices, uint64 startTime, uint64 endTime)
        internal
        returns (uint256 pollId);

Parameters

NameTypeDescription
groupIduint256
numChoicesuint32
startTimeuint64
endTimeuint64

Returns

NameTypeDescription
pollIduint256

vote

Casts an anonymous ballot. proof.scope must equal pollId; proof.message is the choice.

CEI: the nullifier is spent before the VoteCast event (no external interaction either way).

function vote(uint256 pollId, ISemaphore.SemaphoreProof calldata proof) internal;

Parameters

NameTypeDescription
pollIduint256
proofISemaphore.SemaphoreProof

pollCount

function pollCount() internal view returns (uint256);

Returns

NameTypeDescription
<none>uint256

getPoll

function getPoll(uint256 pollId) internal view returns (IPrivateVoting.PollView memory view_);

Parameters

NameTypeDescription
pollIduint256

Returns

NameTypeDescription
view_IPrivateVoting.PollView

getVotes

function getVotes(uint256 pollId, uint256 choice) internal view returns (uint256);

Parameters

NameTypeDescription
pollIduint256
choiceuint256

Returns

NameTypeDescription
<none>uint256

hasVoted

function hasVoted(uint256 pollId, uint256 nullifier) internal view returns (bool);

Parameters

NameTypeDescription
pollIduint256
nullifieruint256

Returns

NameTypeDescription
<none>bool