Skip to main content

Compiling a circuit

This tutorial contains step-by-step instructions on how to compile the arithmetic circuit created previously.

There are two possible ways for transforming a circuit into a proof binary and Solidity code.

Calling individual tools

The following diagram shows the flow for converting a .cpp or a .rs file into a proof binary and Solidity code for deployment.

Complete the below steps to follow this flow.

Running the compiler

To generate the circuit IR, complete one of the following tutorials.

Passing the IR to assigner

At this stage, the newly generated IR and the public info file are ready to be passed to assigner.

Run the following command to generate the circuit constraints and the assignment table.

assigner -b circuit.ll -i ../inputs/public_input.inp -c circuit.crct -t assignment.tbl -e pallas --generate-type circuit-assignment

Using transpiler to generate the circuit proof

The .crct and .tbl files can be passed to the transpiler tool to generate a binary with the proof and Solidity files containing the circuit.

Run the below command to generate proof.bin.

transpiler -m gen-test-proof -c circuit.crct -t assignment.tbl -o output -e pallas

Execute the following command to produce Solidity files.

transpiler -m gen-evm-verifier -c circuit.crct -t assignment.tbl -o output -e pallas --optimize-gates

Congratulations! At this stage, we recommend taking a look at other examples in the zkLLVM repository and compiling them.

Using zkllvm-template

zkllvm-template is a project designed to simplify working with complex circuits and zk-enabled apps.

The project provides an easy-to-use interface for compiling circuits in one command instead of completing each step individually or reconfiguring build targets in CMakeLists.txt.

Managing dependencies

zkllvm-templatehandles dependency management via a pre-configuredCMakeLists.txt` file. If a circuit fails to compile due to issues with dependencies, consider using the template project.

warning

zkllvm-template only supports C++ projects.

Compiling Rust code is done via a typical flow involving cargo, which eliminates the need for configuring CMakeLists.txt.

To work with zkllvm-template, clone its repository with all sub-modules.

git clone --recurse-submodules https://github.com/NilFoundation/zkllvm-template.git
cd zkllvm-template

Additionally, install all required tools from pre-built binaries.

Within zkllvm-template, the ./main folder contains the main.cpp file that acts as the entry point for the clang compiler.

To run the compiler, execute the following command.

scripts/run.sh

This is equivalent to completing each individual step described in the previous section.