Write your first circom circuit and use groth16 to create zkproofs and verify it

Write your first circom circuit and use groth16 to create zkproofs and verify it


Web3 Zksnark Circom Snarkjs Groth16

Hackathon

I am currently working in HackFs hackathon hosted by ETH Global, in this hackathon we are trying to build a next gen decentralised AI resume builder with verifiable credentials. while working in this project, I touched base with circom and snarkjs after 2 years. since, this is one of the important concepts,I have decided to write this blog in order to share the instructions to write your first zksnark proof and verifier

Installing circom in your system

Install rust

curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh

clone the circom directory and compile

git clone https://github.com/iden3/circom.git
cd circom
cargo build --release
cargo install --path circom
circom --help

Install snark.js

npm install -g snarkjs

Testing circom

pragma circom 2.0.0;

/*This circuit template checks that c is the multiplication of a and b.*/  

template Multiplier2 () {  

   // Declaration of signals.  
   signal input a;  
   signal input b;  
   signal output c;  

   // Constraints.  
   c <== a * b;  
}

save it inside src/circom/multiplier2.circom

to compile circom

circom src/circom/multiplier2.circom --r1cs --wasm --sym --c

if it compiled successfully, you might get two folders namely multiplier2_cpp and multiplier2_js

cd multiplier2_js

creating a input file called input.json inside here

{"a": "3", "b": "11"}
node generate_witness.js multiplier2.wasm input.json witness.wtns

proving circuits with zk

We are going to use groth-16 zk-snark protocol to prove this. First we start a new “powers of tau” ceremony

snarkjs powersoftau new bn128 12 pot12_0000.ptau -v

Then, we contribute to the ceremony:

snarkjs powersoftau contribute pot12_0000.ptau pot12_0001.ptau --name="First contribution" -v

it will prompt a random text, i have added “test” here, you can try out different processes Now, we have the contributions to the powers of tau in the file pot12_0001.ptau and we can proceed with the Phase 2.

Phase-2

The phase 2 is circuit-specific. Execute the following command to start the generation of this phase:

snarkjs powersoftau prepare phase2 pot12_0001.ptau pot12_final.ptau -v
snarkjs groth16 setup multiplier2.r1cs pot12_final.ptau multiplier2_0000.zkey
snarkjs zkey contribute multiplier2_0000.zkey multiplier2_0001.zkey --name="1st Contributor Name" -v
snarkjs zkey export verificationkey multiplier2_0001.zkey verification_key.json

Generating a Proof

lets try to generate proof

snarkjs groth16 prove multiplier2_0001.zkey witness.wtns proof.json public.json

Verifying the Proof

snarkjs groth16 verify verification_key.json public.json proof.json

Generating the Solidity verifier contract

snarkjs zkey export solidityverifier multiplier2_0001.zkey verifier.sol

It will generate the verifier.sol, we can use remix.ethereum.org in order to deploy the contract in solidity. I am using Ethereum Sepolia to deploy the contract

You can use the quicknode sepolia faucet for getting funds https://faucet.quicknode.com/ethereum/sepolia?utm_source=faucet&utm_medium=twitter&utm_content=social-share&utm_term=sepolia-eth the contract which is deployed can be found in ethereum sepolia at this address 0x5e3565934209B3F04E03bA7FA78BAdc9b3cf0578

then we can able to generate the call using

snarkjs generatecall

copy the result and paste it as a param to the verifyProof method and hit the button, if everything went fine, the bool will return as true

Thanks

Thanks for giving time for this blog, we are working on more cool stuffs in this project. I will share more details as soon as possible.Until then Happy Hacking.

Web3 for Life