Private voting
Shows: the owner check (4) · the proof-carrying message (7) · pseudonym (8) · write once (15) · windows for expiry (16)
Goal
An election where the eligible voters and the running tally are public, and nobody can tell how any voter voted, or whether they voted at all, beyond what the tally itself shows (a unanimous result, or a vote from every ballot issued).
Actors
| Actor | What it holds | Placement |
|---|---|---|
(election, voting) | the question, the deadline, the tally, a count of ballots issued | public PDA |
(voter, voting): a ballot | which election, and whether it has been used | private, on each voter’s account |
Messages
enum Message {
/// To the election, by the organiser: send one ballot to each address.
Open { voters: Vec<AccountId>, deadline: Timestamp },
/// Election to voter: you may vote.
Ballot,
/// To a ballot, by its owner.
CastVote { choice: u8 },
/// Voter to election: one valid vote.
Vote { choice: u8 },
}
How it runs
-
Open. The organiser sends
Openwith the voters’ addresses. The election is a PDA, so no signature authorizesOpen: the empty-state check makes the firstOpenfinal, and whoever sends it first sets the voter list and deadline. Voters read both back before trusting the election. The election actor sends oneBallotto each as an async message, stored from a public actor: everyone can see who is eligible. Each voter’s address needs its recovery binding on chain first (a public transaction cannot introduce one), orOpenis charged and reverted withUnboundAsyncDestination. -
Collect the ballot. Each voter collects their
Ballot; their private ballot actor checks that it comes from an actor of the voting program, and records the election. -
Vote. The voter sends
CastVoteto their ballot actor in a private transaction. The handler checks that the ballot is unused, marks it used, and sendsVoteto the election. The wallet presents the ballot under a fresh blinded address. -
Tally. The election’s handler checks
from_own_program()and the deadline, and adds one to the chosen option.
Handler
fn handle_message(input: &ReceiveInput, message: Message) -> Response {
let me = input.receiver;
match message {
// Voter side: receive the ballot. Only an election of this program can issue one.
Message::Ballot => {
let election = input.from.filter(|_| input.from_own_program()).expect("not a ballot");
Response::set_state(write_once(&input.pre_state, encode(&(election, false))))
}
// Voter side: use the ballot. Only the owner can, and only once.
Message::CastVote { choice } => {
assert!(input.is_authorized() && input.from.is_none());
let (election, used): (Actor, bool) = decode(&input.pre_state);
assert!(!used, "already voted");
Response::set_state(encode(&(election, true)))
.send_sync(election, &Message::Vote { choice })
}
// Election side: count. The sender is a ballot actor of this program.
Message::Vote { choice } => {
assert!(input.from_own_program(), "not a ballot");
let mut election = Election::from_bytes(&input.pre_state);
election.tally[choice as usize] += 1;
Response::set_state(election.to_bytes())
.timestamp_window(..election.deadline)
}
// Election side: open once, and send one `Ballot` to each voter as an async message.
Message::Open { voters, deadline } => {
assert!(input.pre_state.is_empty(), "already open");
let election = Election::new(deadline, voters.len());
voters.iter().fold(Response::set_state(election.to_bytes()), |response, voter| {
response.send_async(Actor::new(*voter, me.program_account_id), &Message::Ballot)
})
}
}
}
Why a voter cannot cheat
- Vote without a ballot. The ballot actor’s state is empty, so
CastVotefails; only aBallotfrom the voting program can write a ballot state. - Vote twice.
usedis true after the first vote, and the second run starts from that state, because the proof ties each run to the account’s real, latest state. - Forge a
Votedirectly. The election acceptsVoteonly from an actor of the voting program, whose only code path that sendsVoteis the one above. - Use someone else’s ballot. A ballot is state on the voter’s own private account; nobody else can run its handler.
The election never checks a signature, a membership proof or a nullifier: the engine does all of that.
What leaks
| Observer learns | Yes or no |
|---|---|
| who is eligible | yes |
| the tally, live | yes |
| that some ballot voted for option X at time T | yes |
| which voter that was | no |
| whether a given voter has voted | no, unless the number of votes is zero or reaches the number of ballots issued |
| whether a given voter collected their ballot | no |
Timing and the fee are the remaining risks. In a small election, a vote that appears seconds after one voter comes online is a strong hint, and a fee declared by a public signer, or a fee refund sent to a named public account, links that account to the vote.
Placements
| Voter | Works | Notes |
|---|---|---|
| Private | yes | as described |
| Public | yes | their vote is public: the ballot actor is public state and the vote is a signed transaction |
The program does not care: a voter who wants an open vote uses a public account.
A public voter’s ballot cannot be stored: only private addresses have a mailbox. The organiser’s Open transaction has to declare each public voter’s ballot actor so the ballot is delivered at once. If one is missing, its ballot becomes a clear publication to an address that has no recovery binding, and the whole Open is charged and reverted with UnboundAsyncDestination.
Going further
- Hidden tally until the end. Votes carry a commitment, opened after the deadline by a second message.
- Delegation. A voter forwards their ballot to a delegate’s address.
- What does not work: coercion resistance. A voter can prove how they voted by showing their transaction and its secrets.