Zero Knowledge Proof Voting Contract
Even or Odd voting smart contract using Zero Knowledge Proof(zkSNARKs) on Ethereum. Zero Knowledge Proof allows you to vote with privacy.
The number you used for voting is hidden; Only the result is verified by zkSNARKs and stored on the blockchain.
demo_vote.mp4
Example of transaction result: https://ropsten.etherscan.io/tx/0x9447fd2e774b7a9bc465806d69539b53b8bc0d388edaa1eea2a76590f608fa29
Try it out
https://zkp-vote-tomoima525.vercel.app/
Make sure that you are connected to Ropsten network and have some ether in your account
How does this work?
Zokrates is used for Zero Knowledge Proof. Here's the steps:
- Compute
witness
by calculating Even or Odd using a program compiled with zokrates-js. - Using
witness
and theproving.key
provided by the verifier(in this project, proving.key is in https://github.com/tomoima525/zkp-toy/blob/main/public/proving.key), we generateproof
andinputs
- Submit tranactions to the blockchain. After the zkSNARKs-powered smart contract verifies your
proof
andinputs
, it will update the stored result.
The figure below illustrates the flow
Run locally
We use hardhat for deployment and testing
- Run
yarn install
- Start the local node
$ npx hardhat node
- Deploy the smart contract to your local network
$ npx hardhat run --network localhost scripts/deploy.ts ... VoteEvenOrOdd deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3
- Create
.env.local
file under the root of the project and copy the deployed addressNEXT_PUBLIC_VOTE_ADDRESS_LOCAL=0x5FbDB2315678afecb367f032d93F642f64180aa3
- Start the frontend
$ yarn run dev
Running test
$ npx hardhat test
Setup your own proving.key
You can provide your own proving.key by running Zokrates locally. You also need to generate one when you change your program code
- You need Docker!! E.g. https://docs.docker.com/desktop/mac/install/
- After installing Docker, run the command below to start zokrates with the root directory set to the contracts repository
e.g.
$ docker run -v <path for circuits locally>:/home/zokrates/code -ti zokrates/zokrates /bin/bash
$ docker run -v ~/workspace/solidity/zkp-toy/contracts/:/home/zokrates/code -ti zokrates/zokrates /bin/bash
- Run the command below to generate the proving.key and the verification.key
$ zokrates setup
- Upload proving.key somewhere accessible
- Update the download link at https://github.com/tomoima525/zkp-toy/blob/main/src/pages/index.tsx#L175
const res = await fetch("https://your-public.site/proving.key");