You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This document summarizes the best practices for compiling and generating Groth16 proofs for large ZK circuits using the circom / snarkjs toolstack. These techniques are most applicable to circuits with at least 20M constraints.
For such large circuits, you need a machine with an Intel processor, lots of RAM and a large hard drive with swap enabled. For example, the zkPairing project used an AWS r5.8xlarge instance with 32-core 3.1GHz, 256G RAM machine with 1T hard drive and 400G swap.
Our knowledge of the following best practices is almost entirely due to the generosity and guidance of Jordi Baylina from Polygon-Hermez.
Compilation and proving
Compilation: for circuits with >20M constraints, one should not compile to WebAssembly because witness generation will exceed the memory cap of WebAssembly. For this reason, one must compile with the C++ flag and remove the wasm flag.
For witness debugging, run: circom --O1 --c --sym (turns off .wasm and .r1cs). We are not concerned with generating a proving key, so the r1cs file is unnecessary. --O1 optimization only removes “equals” constraints but does not optimize out “linear” constraints.
For production, run: circom --O2 --c --sym --r1cs (turns off .wasm). In practice, one may still need to use --O1 because the further --O2 optimization takes significantly longer on large circuits (for reasons that aren’t totally clear).
Witness generation: As mentioned above, witness generation must be done by compiling from C++ code.
For C++, on Ubuntu one needs the following apt packages: build-essential libgmp-dev libsodium-dev nasm nlohmann-json3-dev
To generate the witness: ./"$CIRCUIT_NAME" [input.json] [witness.wtns]
Extract .json from .wtns using snarkjs wej [witness.wtns] [witness.json] (uses .sym file)
wasm witness generator will not work for circuits above a certain constraint size (~10-20M) due to memory limit
Note: C++ witness generator will not work on computes with Apple Silicon (e.g., M1) chips due to assembly incompatibility.
Key generation: (see full commands below) Groth16 requires a separate trusted ceremony for each circuit – this is the phase 2 trusted setup. This step requires using the Powers of Tau from the phase 1 trusted setup and performing elliptic curve operations which scale with the size of the circuit. Unfortunately this means that the amount of memory used also scales with the size of the circuit (number of constraints).
The speed of the phase 2 trusted setup is significantly slowed down due to automatic garbage collection performed by Node. To get around this, we use a patched version of Node introduced by Polygon-Hermez to disable garbage collection. This significantly speeds up the trusted setup; as a trade-off, Node uses an incredible amount of memory so the machine must have swap set up.
Remove Node internal memory limit and also any system memory limits. (Commands below.)
In the circuit debugging stage, it is useful to note that you do not need to go through the full setup with key generation above to extract the outputs (if any) of the proof.
After generating the witness file witness.wtns and converting it to json using snarkjs wej [witness.wtns] [witness.json], then indices 1-m of witness.json (index 0 is always equal to 1) will contain the m public outputs of the proof.
In fact, one can in theory extract all witnesses from intermediate steps of the proof from witness.json using the .sym file. We have built an experimental Python parser to do this here (the parser currently may break due to compiler optimizations, run with --O0 for safety).
Setup from scratch
Here are the steps to set up a blank slate machine/instance according to the configuration described above.
Install rust, circom, C++ dependencies, nvm, and yarn.
The rapidsnark executable is located at RAPIDSNARK_PATH = $HOME_DIR/rapidsnark/build/prover.
Build scripts
One can use the following bash script to implement all the proving steps described above. (For a full implementation, see here.)
The Powers of Tau file is located at $PHASE1. Let CIRCUIT_NAME be the name of the circuit. We assume the circuit has already been compiled, with all relevant files in the current directory.
Phase 2 trusted setup
Groth16 requires a separate trusted setup for each circuit. This generates a common reference string (CRS), which is stored in a .zkey file. The following commands should be run once per circuit.
To create the .zkey without phase 2 contributions:
We should contribute to the phase 2 ceremony, which requires some randomn input. (For production, one should do multiple contributions with more rigor.)
echo "****CONTRIBUTE TO PHASE 2 CEREMONY****"
start=`date +%s`
$NODE_PATH $SNARKJS_PATH zkey contribute -verbose "$CIRCUIT_NAME"_0.zkey "$CIRCUIT_NAME".zkey -n="First phase2 contribution" -e="some random text for entropy"
end=`date +%s`
echo "DONE ($((end-start))s)"
Best Practices for Large Circuits
This document summarizes the best practices for compiling and generating Groth16 proofs for large ZK circuits using the circom / snarkjs toolstack. These techniques are most applicable to circuits with at least 20M constraints.
For such large circuits, you need a machine with an Intel processor, lots of RAM and a large hard drive with swap enabled. For example, the zkPairing project used an AWS r5.8xlarge instance with 32-core 3.1GHz, 256G RAM machine with 1T hard drive and 400G swap.
Our knowledge of the following best practices is almost entirely due to the generosity and guidance of Jordi Baylina from Polygon-Hermez.
Compilation and proving
wasm
flag.circom --O1 --c --sym
(turns off.wasm
and.r1cs
). We are not concerned with generating a proving key, so ther1cs
file is unnecessary.--O1
optimization only removes “equals” constraints but does not optimize out “linear” constraints.circom --O2 --c --sym --r1cs
(turns off.wasm
). In practice, one may still need to use--O1
because the further--O2
optimization takes significantly longer on large circuits (for reasons that aren’t totally clear).apt
packages:build-essential libgmp-dev libsodium-dev nasm nlohmann-json3-dev
cd "$CIRCUIT_NAME"_cpp; make
./"$CIRCUIT_NAME" [input.json] [witness.wtns]
.json
from.wtns
usingsnarkjs wej [witness.wtns] [witness.json]
(uses.sym
file)wasm
witness generator will not work for circuits above a certain constraint size (~10-20M) due to memory limitWitness generation debugging
In the circuit debugging stage, it is useful to note that you do not need to go through the full setup with key generation above to extract the outputs (if any) of the proof.
After generating the witness file
witness.wtns
and converting it tojson
usingsnarkjs wej [witness.wtns] [witness.json]
, then indices1-m
ofwitness.json
(index0
is always equal to1
) will contain them
public outputs of the proof.In fact, one can in theory extract all witnesses from intermediate steps of the proof from
witness.json
using the.sym
file. We have built an experimental Python parser to do this here (the parser currently may break due to compiler optimizations, run with--O0
for safety).Setup from scratch
Here are the steps to set up a blank slate machine/instance according to the configuration described above.
Install rust, circom, C++ dependencies, nvm, and yarn.
Remove system memory limit
Run
and fix it to not be reset after a reboot by adding this line
in the file
/etc/sysctl.conf
.Setup swap
to make this persistent through reboots, add to
/etc/fstab
:Install patched node
We use
$HOME_DIR
as our home directory throughout.The patched node executable is located at
NODE_PATH = $HOME_DIR/node/out/Release/node
.Install snarkjs from source
The snarkjs executable is located at
SNARKJS_PATH = $HOME_DIR/snarkjs/cli.js
.Install rapidsnark from source
The rapidsnark executable is located at
RAPIDSNARK_PATH = $HOME_DIR/rapidsnark/build/prover
.Build scripts
One can use the following bash script to implement all the proving steps described above. (For a full implementation, see here.)
The Powers of Tau file is located at
$PHASE1
. LetCIRCUIT_NAME
be the name of the circuit. We assume the circuit has already been compiled, with all relevant files in the current directory.Phase 2 trusted setup
Groth16 requires a separate trusted setup for each circuit. This generates a common reference string (CRS), which is stored in a
.zkey
file. The following commands should be run once per circuit.To create the
.zkey
without phase 2 contributions:We should contribute to the phase 2 ceremony, which requires some randomn input. (For production, one should do multiple contributions with more rigor.)
Verify final zkey:
The verifier does not need the full zkey to verify a Groth16 proof. They only need a shorter verification key. To export the verification key:
Witness and proof generation
The following commands should be run once for each input to generate witness and a proof for that input.
Done by prover:
Generate witness (C++):
Change witness to
.json
:Generate proof:
Done by verifier:
To verify the proof, run:
The text was updated successfully, but these errors were encountered: