The hello world app of ZK: prove you know a factorization without revealing the factors.
This repo serves as a guide to show how to plug a ZK Haskell program into existing proving software. In this case we want to build a browser based proving application with an Ethereum smart contract verifier, and we will make use of the snarkjs proving library. There are similar integrations for arkworks and more to come.
- You must have nix and some recent version of node.
- You should have a local ethereum node with an unlocked default account and the web3 api running on the default port
8545
. E.g. use hardhat or cliquebait
A ZK program written in a Haskell DSL that expresses a factorization of a public input n
into a product of secret inputs a
and b
. You can produce a circom compatible r1cs
file for this program by running
> cabal run factors
A constraint solver applied to the factors
program. You can produce a circom compatible WASM binary for this solver by running
> cd factors-solver
> ./build-wasm
Assuming you have run the above, you should see a circuit.r1cs
file in the trusted-setup
directory. To produce a final proving key and solidity verifier:
> npx snarkjs groth16 setup trusted-setup/circuit.r1cs trusted-setup/pot14_final.ptau trusted-setup/circuit_final.zkey
> npx snarkjs zkey export solidityverifier trusted-setup/circuit_final.zkey contracts/Groth16Verifier.sol
You can verify your proving key:
> npx snarkjs zkey verify trusted-setup/circuit.r1cs trusted-setup/pot14_final.ptau trusted-setup/circuit_final.zkey
You can comple the contracts, build the purescript ffi, and deploy this smart contract via
> npm run chanterelle-build
> npm run chanterelle-deploy
Assuming you have done the previous steps, copy the proving key to the www
folder
> cp trusted-setup/circuit_final.zkey www
You should see the circuit.wasm
solver binary is already there. Assuming you have deployed the verifying contract, you can start the frontend:
export VERIFIER_ADDRESS=$(jq -r '.networks."420123".address' build/contracts/Groth16Verifier.json) && npm run parcel
NOTE: I used cliquebaite to write this readme, which has networkId/chainId 420123
. If you have a different chainId, you will need to subsitute it in the above command or just find the address in the artifact manually.
You should see a form load to test the application