Private voting
DAOs, community treasuries and project councils increasingly vote on-chain, and on a public ledger every ballot is tied to an address. That invites vote buying (“show me your transaction and I’ll pay you”), social pressure from peers and employers, and whales watching the count before they decide. What you want is a vote where eligibility and one-person-one-vote can be checked by anyone, but nobody can tell who voted for what, and only the final result is ever revealed.
5 voters registered. The demo's tree has depth 10 (1,024 slots).
Second ballot, No: same key, so the same nullifier.
VoteZkMintingPolicyGroth16 proof validbefore the voting deadlinemints 1 token named nullifierVoteListValidator · nullifier → ballot0x19e2…d741inserted0x1e82…e9c60x7c51…fb29
Which voter cast which ballot, and how each one voted: unknown.
1. Register
The organizer collects each voter's public key, Poseidon(secretKey, 0), builds a Merkle tree and publishes only the root and the election ID. Three trustees publish the election key that ballots are encrypted to. Your secret key stays on your device.
2. Prove
Your device computes your nullifier, encrypts your vote to the election key with fresh randomness, and proves three things at once: your key is on the list, the nullifier is yours, and the ballot holds a 0 or a 1. Switch to Public view: the vote never leaves the device in the clear.
3. Submit
The transaction carries the 192-byte proof and a new list node holding your nullifier and ballot. The minting policy takes the election ID, voter root and election key from its own parameters, the ballot from that node, verifies the proof before the deadline, and mints one token named after the nullifier. The list validator inserts the node between its sorted neighbours.
4. Vote again
A second ballot from the same key, even with a different choice, has the same nullifier. It's already in the list, so the insert fails and the transaction is rejected. A different key would give a new nullifier, but that key isn't in the tree.
5. Tally
After the deadline anyone adds the encrypted ballots, and the trustees decrypt only that total, once, with proofs that they did it honestly. Nobody learns who cast which ballot or how any one voter voted. The encrypted tally walkthrough below shows the count step by step.
The zero-knowledge idea
Section titled “The zero-knowledge idea”The voter proves “I’m on the eligible-voter list, this is my one nullifier for this election, and this encrypted ballot holds a 0 or a 1”, without revealing who they are or how they voted.
Four pieces make this work:
- Voter list. Each voter’s public key is
Poseidon(secretKey, 0). The organizer puts all public keys into a Merkle tree and publishes only the root. - Nullifier.
Poseidon(secretKey, electionId). It’s deterministic, so the same voter in the same election always produces the same value, and a second vote is caught. A differentelectionIdgives an unrelated nullifier, so a voter’s ballots can’t be linked across elections. - Encrypted ballot. The vote, encrypted with exponential ElGamal to an election key that three trustees hold together. Nobody can open a single ballot, but ballots can be added up while still encrypted.
- Tally. After voting closes, the trustees decrypt only the total, once, and prove they did it honestly.
The circuit ties the first three to one secret key. A voter who switches keys to get a fresh nullifier isn’t in the tree, and a voter who keeps their key gets the same nullifier again.
If ElGamal is new to you, Commitments & encryption, explained introduces it with an interactive lockbox.
Why not just hash the vote?
Section titled “Why not just hash the vote?”A hash looks like a natural way to hide a vote: publish Poseidon(vote, nullifier) and nobody can
undo the hash. The first version of this demo did exactly that, and it doesn’t work. The
nullifier is public and a vote is only 0 or 1, so anyone can hash both choices and see which one
matches. Hiding a value with a hash only works when there are far too many possible values to
try. A yes/no vote, an age or a small amount can always be guessed this way.
Pick a vote, then see what the observer can do with the public ballot.
Match found: voter3 voted Yes.
1. Hash the vote
The first version of the demo stored Poseidon(vote, N). The hash can't be undone, but it doesn't need to be: N is public and a vote is only 0 or 1, so an observer hashes both and compares. One always matches. Flip the vote and try again. A hash hides a value only when there are too many possible values to try.
2. Add a salt
Mixing in a secret random salt stops the guessing, because the observer can't compute the hash without it. But now nobody can count either. To tally, every voter has to come back and reveal their salt, which reveals their vote at the end, and anyone who doesn't come back isn't counted.
3. Encrypt it
The demo encrypts the vote with exponential ElGamal to a key the trustees hold together. Each ballot uses fresh randomness k, so encrypting 0 or 1 yourself never reproduces it. Press Encrypt again: same vote, a completely different ballot. And ElGamal ciphertexts add up, so the chain can total the ballots and the trustees decrypt only the sum.
A secret random salt stops the guessing, but then nobody can count the votes without every voter coming back to reveal their salt, which reveals their vote at the end. Encryption solves both problems at once: each ballot uses fresh randomness, so it can’t be matched by guessing, and ElGamal ciphertexts can be added, so the total can be decrypted without opening any ballot. The hash version was reported as issue #7 and replaced with the encrypted ballot described on this page (ADR-0005).
What stays private, what’s public
Section titled “What stays private, what’s public”| Input | Visibility | Why |
|---|---|---|
secretKey | Secret | The voter’s identity; it derives both the public key and the nullifier |
Merkle path (siblings, pathBits) | Secret | The path would reveal the voter’s position in the list |
vote | Secret | Inside the ciphertext; the proof limits it to 0 or 1 |
randomness (252 bits) | Secret | Fresh for every ballot; it’s what makes two ballots for the same choice look unrelated |
electionId, voterRoot | Public | The election and its list of eligible voters, fixed in the on-chain script |
Election key PK (PK.u, PK.v) | Public | The trustees’ combined key, also fixed in the script |
nullifier | Public | Recorded on-chain to block a second vote |
Ballot (A, B) (A.u, A.v, B.u, B.v) | Public | The encrypted vote; it reveals nothing without the trustees |
How it works
Section titled “How it works”- Trustee keys. Each of the three trustees generates a key pair and publishes its public key with a zero-knowledge proof of possession (a DLEQ proof that it knows the matching secret). Without that proof, a dishonest trustee could choose its key to cancel the others’ and read every ballot alone. The election key is the sum of the three public keys.
- Register. The organizer collects each voter’s public key
Poseidon(secretKey, 0), builds the Merkle tree and publishesvoterRoot. The election ID, the root, the election key and the voting deadline become parameters of the on-chain scripts. - Encrypt and prove. The voter encrypts their vote
vwith fresh randomnessk:A = [k]·G,B = [v]·G + [k]·PK. A Groth16 proof, made with ZeroJ’s pure-Java prover, shows that the voter is on the list, that the nullifier is theirs, and that(A, B)encrypts a 0 or a 1 underPK. A ballot that tries to count twice (v = 2) has no valid proof. In a real deployment this runs on the voter’s own device; the demo proves server-side for convenience. - Submit. A transaction mints one token whose name is the nullifier and creates a list node that holds the ballot. The minting policy verifies the proof on-chain before the deadline, and the nullifier is inserted into an on-chain sorted list.
- Reject repeats. A second vote from the same key has the same nullifier. The list already contains it, so the insert fails.
- Add up. Anyone can add the ballots:
(ΣA, ΣB)is an encryption of the number of YES votes. - Decryption shares. After the deadline, each trustee publishes
D_j = [sk_j]·ΣAtogether with a DLEQ proof that it used the same secret as its registered key. A wrong share fails its proof. - Count.
ΣB − (D₁ + D₂ + D₃) = [YES]·G. The number of YES votes is small, so a short search recovers it. Anyone can repeat every check from chain data and the published election manifest.
voter (off-chain) Cardano───────────────── ───────secretKey, Merkle path, tx: redeemer = proof; new list node = (nullifier, A, B)vote, randomness ──encrypt + prove──▶ ├─ minting policy: Groth16 check, deadline, mint 1 token named <nullifier> └─ list validator: insert <nullifier> between sorted neighboursafter the deadline: anyone adds the ballots ─▶ trustees post verified shares ─▶ YES countThe trustees decrypt the total exactly once, after the deadline. Decrypting a running total twice would let anyone subtract the two results and learn the votes cast in between.
Counting without opening a ballot
Section titled “Counting without opening a ballot”Step through the tally. Switch to Public view to see what an observer gets: encrypted ballots, nullifiers, the trustees’ shares, and the total.
Each voter holds an eligibility secret. Its key is a leaf in the voter tree.
- N = 0x1bff…b202(A, B) = 0x4619…4d18
- N = 0x1b85…ab73(A, B) = 0xe56c…eaea
- N = 0xcc63…8910(A, B) = 0xffde…bc02
Adding the ciphertexts adds the votes inside them. Nobody has decrypted anything yet.
0xf589…0e28 + proofabsent: no share0x8002…b463 + proofabsent: no share0xd799…308e + proofabsent: no share1. Election key
Three trustees each make a key pair and publish the public half, with a proof that they know the secret behind it (this stops a trustee choosing a key that cancels the others). The election key is the sum of the three. Nobody holds its secret key.
2. Encrypted votes
Each voter encrypts 0 or 1 to the election key and proves, in zero knowledge, that they are on the voter list, that the nullifier is theirs, and that the ciphertext holds a 0 or a 1, nothing else. The chain sees only ciphertexts. Flip a vote in the voters' view: its ciphertext changes, and the public view stays unreadable.
3. Vote twice?
voter1 tries again. The same secret gives the same nullifier, which is already in the on-chain list, so the second ballot is refused, whatever it encrypts.
4. Add them up
Anyone can add the ciphertexts. The result is one ciphertext of the number of YES votes. No single ballot is ever decrypted, and the running total isn't decrypted before the deadline either.
5. Decryption shares
After the deadline, each trustee applies its secret to the sum, once, and posts the result with a proof that it used the same secret as its registered key. A wrong share fails its proof. In the demo every trustee must take part; tick the box to see what happens when one doesn't.
6. Result
Removing the three shares leaves the YES count as a small curve multiple, found by a quick search. Anyone can redo every check from chain data. Only the total is revealed, never who voted what.
The ballot circuit
Section titled “The ballot circuit”Lightly simplified from the demo’s PrivateBallotProof: constructor validation is trimmed.
ZkElGamal.encrypt is ZeroJ’s elgamal-jubjub-v1 encryption relation, so the demo doesn’t
implement any ElGamal itself.
import org.zeroj.circuit.annotation.*;import org.zeroj.circuit.lib.poseidon.PoseidonParams;import org.zeroj.circuit.lib.poseidon.PoseidonParamsBLS12_381T3;import org.zeroj.circuit.lib.zk.ZkElGamal;import org.zeroj.circuit.lib.zk.ZkElGamalPublicKey;import org.zeroj.circuit.lib.zk.ZkMerkle;import org.zeroj.circuit.lib.zk.ZkPoseidon;
@ZKCircuit(name = "private-ballot", nameTemplate = "private-ballot-d{treeDepth}-jubjub-elgamal", version = 3)public class PrivateBallotProof { private static final PoseidonParams POSEIDON = PoseidonParamsBLS12_381T3.INSTANCE;
public PrivateBallotProof(@CircuitParam("treeDepth") int treeDepth) {}
@Prove void prove(ZkContext zk, @Public ZkField electionId, @Public ZkField voterRoot, @Public ZkField electionKeyU, @Public ZkField electionKeyV, // PK @Public ZkField nullifier, @Public ZkField handleU, @Public ZkField handleV, // A @Public ZkField ballotU, @Public ZkField ballotV, // B @Secret @UInt(bits = 1) ZkUInt vote, // 0 or 1 @Secret @UInt(bits = 252) ZkUInt randomness, @Secret ZkField secretKey, @Secret @FixedSize(param = "treeDepth") ZkArray<ZkField> siblings, @Secret @FixedSize(param = "treeDepth") ZkArray<ZkBool> pathBits) {
// Eligibility, and one nullifier per voter and election. ZkPoseidon.hash(zk, POSEIDON, secretKey, electionId).assertEqual(nullifier); var voterKey = ZkPoseidon.hash(zk, POSEIDON, secretKey, zk.constant(0)); ZkMerkle.verifyProofPoseidon(zk, POSEIDON, voterKey, voterRoot, siblings, pathBits);
// The ballot: an encryption of a 1-bit vote under the election key. var electionKey = ZkElGamalPublicKey.fromVerifierFixedPublic(zk, electionKeyU, electionKeyV); ZkElGamal.encrypt(zk, vote, randomness, electionKey) .assertAffineEquals(zk, handleU, handleV, ballotU, ballotV); }}The @UInt(bits = 1) on vote is what limits a ballot to 0 or 1. The circuit has 9,477
constraints at tree depth 10. The annotation processor generates a PrivateBallotProofCircuit
companion that you compile to R1CS and prove with Groth16. Before you trust a circuit like this,
test it with invalid witnesses: a key that isn’t in the tree, a nullifier from another election, a
vote of 2, a ballot encrypted to another key. It should reject every one. See
Testing circuits.
On Cardano
Section titled “On Cardano”The demo uses two Plutus V3 scripts written in Java with JuLC:
VoteZkMintingPolicyverifies the ballot proof withGroth16BLS12381Lib.verify(...)fromzeroj-onchain-julc. The redeemer carries only the proof. Every one of the nine public inputs comes from the script’s parameters (electionId,voterRoot, the election key, and the verification key) or from the ledger: the nullifier is the name of the single token minted, and the ballot is read from the inline datum of the list node that receives it. The transaction must also be valid only up to the voting deadline.VoteListValidatorkeeps nullifiers in a sorted linked list, one UTxO per nullifier, each holding its ballot. Inserting between two neighbours proves the new nullifier wasn’t there before. Votes that land in different parts of the list touch different UTxOs, so they don’t contend. Each node locks about 2 ADA.
A valid proof is not authorization, which is why the election’s values are script parameters and not redeemer fields: otherwise a voter could prove membership in a tree they built themselves, or encrypt to a key they hold. See On-chain verification.
Counting the votes in code
Section titled “Counting the votes in code”Off-chain, the tally uses ZeroJ’s ElGamal API. Simplified from the demo’s TallyService, with one
trustee’s secret shown for brevity (each trustee computes its own share on its own machine):
import org.zeroj.circuit.lib.jubjub.ElGamal;import org.zeroj.circuit.lib.jubjub.ElGamalCiphertext;import org.zeroj.circuit.lib.jubjub.ElGamalPublicKey;import org.zeroj.circuit.lib.jubjub.VerifiedDecryptionShare;
import java.util.List;
// `ballots`: every ballot in the vote list, admitted under the election's key contextElGamalCiphertext total = ElGamalCiphertext.sum(ballots);
// Trustee j, on its own machine: its share of the decryption, D_j = [sk_j]·ΣAVerifiedDecryptionShare own = ElGamal.decryptionShare(trusteeSecret, total);byte[] published1 = own.encode(); // posted with a DLEQ proof of own.statement()
// Anyone: accept each published share only if its proof verifies (repeat for every trustee)VerifiedDecryptionShare share1 = VerifiedDecryptionShare.verify(total, trusteeKey1, published1, statement -> verifyDleqProof(proof1, statement.publicInputs()));// ... share2 and share3 the same way, then decrypt the totallong yes = ElGamal.decrypt(total, List.of(share1, share2, share3), total.bound().longValueExact());ElGamal.decrypt accepts only shares that went through VerifiedDecryptionShare.verify, and the
total’s bound is the number of ballots, so the final search is tiny. See the
ElGamal guide for the full API.
Limits
Section titled “Limits”- Every trustee is needed, and all of them together could read any ballot. The demo’s key is
n-of-n: if one trustee disappears, nothing can be decrypted, and the three together could
decrypt a single ballot instead of the total. ZeroJ’s
threshold ElGamal with a distributed key generation is
the t-of-n alternative: any
t + 1trustees can decrypt, and fewer learn nothing. The demo doesn’t use it yet. - The demo server knows every vote. For convenience it holds every voter’s and trustee’s secret and proves on their behalf. In a real election each voter proves on their own device and each trustee keeps its own key.
- Demo voter keys are random and kept. The test voters’ secret keys come from
SecureRandomand are stored, owner-only, in the demo’s data directory. Earlier versions used small fixed numbers (10000 + i), so anyone could compute each voter’s nullifier, match ballots to voters, or vote for a voter who hadn’t voted yet. Voting “as Alice” by label in the demo is the operator acting for that voter, not the voter signing in. - Small or unanimous results reveal votes. If all three ballots are YES, everyone knows how each voter voted. The total is always public.
- The vote list is public. Anyone can see how many ballots were cast and when.
Security considerations
Section titled “Security considerations”- The voter list is a trust point. Whoever builds the tree decides who can vote. Publish the leaves so members can check their inclusion and spot keys that shouldn’t be there.
- Check the trustees’ key proofs. The election key is only as good as the possession proofs behind it; the manifest publishes every key with its proof so anyone can rebuild it.
- Watch transaction-level metadata. In the demo a service wallet submits and pays for every vote. If voters pay fees from their own wallets, the fee input links them to their ballot. Use a relayer and be mindful of timing.
- No receipt-freeness or coercion resistance. A voter can prove to a vote buyer how they voted
by handing over the randomness
k, or hand over their secret key entirely. Anti-collusion designs such as MACI address this. This demo doesn’t. - Key theft is vote theft. Anyone who learns a voter’s secret key can vote in their place.
- Trusted setup. The demo uses a single-party development setup for both its circuits. A real election needs keys from a multi-party ceremony (Trusted setup, explained).
Try it
Section titled “Try it”The demo lives in
private-voting. With Yaci
DevKit running (see Run the demos):
./demo.sh voting --runAt startup the app creates an election with five funded test voters and three trustees, and opens
voting for 10 minutes. With --run, voter1 votes yes and voter2 votes no. Each ballot is
encrypted and proven in pure Java and verified on-chain. To see double-vote protection, vote again
as voter1 from the UI, or run:
curl -X POST http://localhost:8086/api/vote \ -H "Content-Type: application/json" -d '{"voterLabel":"voter1","vote":0}'The second vote is rejected because voter1’s nullifier is already in the on-chain list. Cast a
third vote (voter3 yes) and fetch the tally:
curl http://localhost:8086/api/resultsBefore the deadline it shows only the encrypted sum. After it, the trustees’ shares are posted and
checked, and the result is yes: 2, no: 1, total: 3, verified: true, with the list of checks
performed. GET /api/election/manifest returns everything needed to re-check the scripts and the
tally yourself.
Design notes: Private voting — detailed design (nullifier registries compared, batch rollups, Hydra, MACI).
Related
Section titled “Related”- Private allowlist with a Merkle tree: build membership and a nullifier yourself, step by step
- One claim per person: the same nullifier idea with a personhood credential
- Application security: what the validator must check beyond the proof
- Gadgets:
ZkMerkle,ZkPoseidonand friends - Commitments & encryption, explained: what an ElGamal ciphertext is and why ciphertexts add up
- Split trust between parties: when no single party should be able to decrypt