Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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

ActorWhat it holdsPlacement
(election, voting)the question, the deadline, the tally, a count of ballots issuedpublic PDA
(voter, voting): a ballotwhich election, and whether it has been usedprivate, 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

  1. Open. The organiser sends Open with the voters’ addresses. The election is a PDA, so no signature authorizes Open: the empty-state check makes the first Open final, and whoever sends it first sets the voter list and deadline. Voters read both back before trusting the election. The election actor sends one Ballot to 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), or Open is charged and reverted with UnboundAsyncDestination.

  2. 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.

  3. Vote. The voter sends CastVote to their ballot actor in a private transaction. The handler checks that the ballot is unused, marks it used, and sends Vote to the election. The wallet presents the ballot under a fresh blinded address.

  4. 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 CastVote fails; only a Ballot from the voting program can write a ballot state.
  • Vote twice. used is 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 Vote directly. The election accepts Vote only from an actor of the voting program, whose only code path that sends Vote is 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 learnsYes or no
who is eligibleyes
the tally, liveyes
that some ballot voted for option X at time Tyes
which voter that wasno
whether a given voter has votedno, unless the number of votes is zero or reaches the number of ballots issued
whether a given voter collected their ballotno

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

VoterWorksNotes
Privateyesas described
Publicyestheir 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.