# 🐑 Circom cheatsheep
Last edited on: `June 6, 2023`, using `circom 2.0.0`
###### tags: `🐑cheatsheep`, `ZKP`
---
## waklthrough
### 1. write your circom file
let say you write a file named `circuit.circom.`
### 2. compile circuit
- `circom circuit.circom --r1cs --wasm --sym --c`
=> `./circuit_cpp`, `./circuit_js`, `circuit.r1cs`, `circuit.sym`
### 3. write input file
- `cd ./circuit.js`
and write your input in a json file, name it `input.json`
```json
{"x": "2", "y": "3", "z": "4"}
```
in `./circuit_js`, you will have `circuit.wasm`, `generate_witness.js`, `witness calculator.js`, and the `input.json` you just write.
### 4. compute witness
- `node generate_witness.js circuit.wasm input.json witness.wtns`
=> `witness.wtns`
move it outside the file
### 5. power of tau ceromony
- `cd ../`
phase 1
- `snarkjs powersoftau new bn128 12 pot12_0000.ptau -v`
=> `pot12_0001.ptau`
- `snarkjs powersoftau contribute pot12_0000.ptau pot12_0001.ptau --name="First contribution" -v`
=> `pot12_0000.ptau`
phase 2
- `snarkjs powersoftau prepare phase2 pot12_0001.ptau pot12_final.ptau -v`
=> `pot12_final.ptau`, `witness.wtns`
- `snarkjs groth16 setup circuit.r1cs pot12_final.ptau circuit_0000.zkey`
=> `circuit_0000.zkey`, this is the proving key
- `snarkjs zkey contribute circuit_0000.zkey circuit_0001.zkey --name="1st Contributor Name" -v`
=> `circuit_0001.zkey`, this is the verification key
- `snarkjs zkey export verificationkey circuit_0001.zkey verification_key.json`
=> `verification_key.json`
### 6. generate proof
- `snarkjs groth16 prove circuit_0001.zkey witness.wtns proof.json public.json`
=> `proof.json`, `public.json`, which is proof and witness
### 7. verify proof ( by snarkjs )
- `snarkjs groth16 verify verification_key.json public.json proof.json`
it should return
```bash
[INFO] snarkJS: OK!
```
### 8. verify proof ( by solidity )
generate the `verifier.sol`
- `snarkjs zkey export solidityverifier circuit_0001.zkey verifier.sol`
deploy the `Verifier.sol`
by calling `Verifier.verifyProof`, input the array generate by
- `snarkjs generatecall`
it should look like this:
`
["0x2d944a3d45551ec6685df3c1b922c0355cc3e2e3d370cdb61352f06108921cf7", "0x265c793a6428128e82cb0b6d389f36a2c579774fb3db706f3b8881dc0d964875"],[["0x0637043a722ea0ff9df781fe0094563d0cc3d2d14b399ae29e7d9b69a49d14b1", "0x116d14b01a88fdcef0d9c2c0d2dcd0748cb7ebac4bda6195f5d449856298e930"],["0x2f263b81aad560d276a4429133cf6473ecb50d26d0b8cbf11a4b398bebb53fda", "0x0af61cb1ed69f79c926ce94102e321fafd29cb0ab41cb753dcda8f111e8f9c0a"]],["0x2a6431d9af80c7df1362ca82f03b28ef8256b2236e92323694ed3e22d7d1f85f", "0x1989ff0d5d7b4000034cc2dfae0e8b293dcc91f28ec1d99c5c19129fd57f5abb"],["0x000000000000000000000000000000000000000000000000000000000000000a"]
`
That's all.