or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
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
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.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).Witness generation: As mentioned above, witness generation must be done by compiling from C++ code.
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 limitKey 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.
Proof generation: For faster proving time, use rapidsnark for proving.
Witness 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: