Skip to content

Phlegmelm/ZKVote

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ZKVote

Noir Solidity Barretenberg License

Anonymous on-chain voting using Zero Knowledge proofs. Voters prove membership in a Merkle tree without revealing their identity. Double-voting is prevented via nullifiers.


How It Works

  1. Each eligible voter has a secret whose hash is a leaf in a Merkle tree
  2. To vote, the voter generates a ZK proof that they know a secret in the tree — without revealing which leaf
  3. A nullifier derived from the secret is recorded on-chain to prevent voting twice
  4. The proof is verified by a Solidity contract generated by Barretenberg

Prerequisites

Tool Version Install
Nargo (Noir) 1.0.0-beta.20 See below
Barretenberg (bb) 4.2.0 See below
Node.js 18+ nodejs.org
Remix IDE remix.ethereum.org

Installation

1. Install Nargo (Noir)

curl -L https://raw.githubusercontent.com/noir-lang/noirup/main/install | bash
noirup --version 1.0.0-beta.20

Verify:

nargo --version

2. Install Barretenberg (bb)

curl -L https://raw.githubusercontent.com/AztecProtocol/aztec-packages/master/barretenberg/bbup/install | bash
bbup --version 4.2.0

If bbup fails, install manually:

curl -L -o /tmp/bb.tar.gz https://github.com/AztecProtocol/aztec-packages/releases/download/v4.2.0/barretenberg-amd64-linux.tar.gz
mkdir -p ~/.bb
tar -xzf /tmp/bb.tar.gz -C ~/.bb/
chmod +x ~/.bb/bb

Verify:

bb --version

Usage

1. Compile the circuit

nargo compile

2. Generate a witness

Edit Prover.toml with your inputs, then:

nargo execute

3. Generate the verification key

mkdir -p ./target/vk
bb write_vk -b ./target/zkvote.json -o ./target/vk -t evm

4. Generate a proof

bb prove -b ./target/zkvote.json -w ./target/zkvote.gz -k ./target/vk/vk -o ./target/proof -t evm

5. Export proof as hex (for on-chain use)

python3 -c "
with open('./target/proof/proof', 'rb') as f:
    data = f.read()
print('0x' + data.hex())
"

6. Generate Solidity verifier

bb write_solidity_verifier -k ./target/vk/vk -o ./target/HonkVerifier.sol

Deployment (Remix IDE)

  1. Open remix.ethereum.org
  2. Upload HonkVerifier.sol and ZKVote.sol
  3. Compile both with Solidity ^0.8.20
  4. Deploy HonkVerifier first — copy its address
  5. Deploy ZKVote with:
    • _verifier: HonkVerifier address
    • _merkleRoot: your Merkle root
    • _startTime: voting start (unix timestamp)
    • _endTime: voting end (unix timestamp)
  6. Call castVote with your proof hex, nullifier, and vote (0 or 1)

Prover.toml Format

secret = "42"          # your private secret
vote = "1"             # 0 = against, 1 = for
index = "0"            # leaf index in the Merkle tree
path = ["1", "2", "3"] # sibling hashes along the Merkle path
merkle_root = "0x..."  # public Merkle root
nullifier = "0x..."    # public nullifier (pedersen_hash([secret]))

To get the correct merkle_root and nullifier for your inputs:

nargo test --show-output

Project Structure

zkvote/
├── src/
│   └── main.nr          # Noir circuit
├── Nargo.toml           # Noir project config
├── Prover.toml          # Witness inputs
├── ZKVote.sol           # Voting contract
└── HonkVerifier.sol     # Auto-generated ZK verifier

License

MIT

About

A zero-knowledge voting system built with Merkle trees, Barretenberg, Noir, and Solidity. Voters prove eligibility and cast valid votes privately, while smart contracts verify proofs and tally results securely without revealing identities or choices.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors