Skip to content

Commit

Permalink
draft
Browse files Browse the repository at this point in the history
  • Loading branch information
NickVolynkin committed Aug 10, 2023
1 parent b834bcc commit bf76d1c
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 67 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ jobs:
with:
submodules: recursive

- name: setup environment
run: |
export user_id=$(id -u)
export group_id=$(id -g)
scripts/run.sh init
- name: Compile a circuit
run: scripts/run.sh --verbose --docker compile

Expand Down
34 changes: 23 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ or Docker Desktop (on macOS).
- [Introduction](#introduction)
- [Getting started](#getting-started)
- [1. Clone the template repository and submodules](#1-clone-the-template-repository-and-submodules)
- [2. Get the Docker images with `=nil;` toolchain](#2-get-the-docker-images-with-nil-toolchain)
- [2. Start a development environment using Docker Compose](#2-start-a-development-environment-using-docker-compose)
- [Part 1. Circuit development workflow](#part-1-circuit-development-workflow)
- [Step 1: Compile a circuit](#step-1-compile-a-circuit)
- [Step 2: Build a circuit statement](#step-2-build-a-circuit-statement)
Expand Down Expand Up @@ -60,9 +60,22 @@ If you cloned without `--recurse-submodules`, initialize submodules explicitly:
git submodule update --init --recursive
```

## 2. Get the Docker images with `=nil;` toolchain
## 2. Start a development environment using Docker Compose

In the tutorial, we will use Docker images with parts of the `=nil;` toolchain.
In the tutorial, we will use an environment based on Docker and Docker Compose.
To work with Docker conveniently, we'll run containers under the same users that
you have on your machines.
Start with the following commands:

```bash
scripts/run.sh init
export user_id=$(id -u)
export group_id=$(id -g)
docker compose up -d
```

Docker Compose will download images with parts of the `=nil;` toolchain
and run them as Docker containers.
We recommend using them because they're tested for compatibility,
and they save you time on installing and compiling everything:

Expand All @@ -72,16 +85,12 @@ and they save you time on installing and compiling everything:
* The `nilfoundation/proof-market-toolchain` image has all you need to make an account on
the `=nil;` Proof Market, put your circuit on it, and order a proof.

Both images are versioned according to the products they contain.
In the tutorial, we'll use the latest compatible versions of both images:
* The `nilfoundation/evm-placeholder-verification` image allows you to run the
EVM Placeholder Verifier locally to test on-chain proof verification.

```bash
ZKLLVM_VERSION=0.0.86
docker pull ghcr.io/nilfoundation/zkllvm-template:${ZKLLVM_VERSION}
These images are versioned according to the products they contain.
In the tutorial, we'll use their latest compatible versions.

TOOLCHAIN_VERSION=0.0.37
docker pull ghcr.io/nilfoundation/proof-market-toolchain:${TOOLCHAIN_VERSION}
```

# Part 1. Circuit development workflow

Expand All @@ -98,6 +107,9 @@ Code in `./src` is using the

## Step 1: Compile a circuit

Before you start, make sure that you've initialized environment,
as shown in the [Getting started](#getting-started) section.

In `./src/main.cpp`, we have a function starting with `[[circuit]]`.
This code definition is what we call the circuit itself.
We will use zkLLVM compiler to make a byte-code representation of this circuit.
Expand Down
54 changes: 54 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
version: '3.5'

services:
zkllvm:
build:
context: .
dockerfile_inline: |
FROM ghcr.io/nilfoundation/zkllvm-template:0.0.86
RUN groupadd $USER -g $group_id || true
RUN useradd $USER -u $user_id -g $group_id || true
image: zkllvm:latest
platform: linux/amd64
user: "${user_id}:${group_id}"
volumes:
- ./:/opt/zkllvm-template
command: bash -c "while true; do sleep 60; done"
tty: true

toolchain:
# Proof Market toolchain
build:
context: .
dockerfile_inline: |
FROM ghcr.io/nilfoundation/proof-market-toolchain:0.0.37
RUN groupadd $USER -g $group_id || true
RUN useradd $USER -u $user_id -g $group_id || true
image: toolchain:latest
platform: linux/amd64
user: "${user_id}:${group_id}"
volumes:
- ./:/opt/zkllvm-template
- ./.config:/.config/
- ./.config:/$USER/.config/
- ./.config/.user:/proof-market-toolchain/scripts/.user
- ./.config/.secret:/proof-market-toolchain/scripts/.secret
command: bash -c "while true; do sleep 60; done"
tty: true

verifier:
build:
context: .
dockerfile_inline: |
FROM ghcr.io/nilfoundation/evm-placeholder-verification:latest
RUN groupadd $USER -g $group_id || true
RUN useradd $USER -u $user_id -g $group_id || true
image: verifier:latest
platform: linux/amd64
user: "${user_id}:${group_id}"
volumes:
- ./:/opt/zkllvm-template
- ./build/template:/opt/evm-placeholder-verification/contracts/zkllvm/template
command: bash -c "while true; do sleep 60; done"
tty: true

74 changes: 18 additions & 56 deletions scripts/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,10 @@ REPO_ROOT="$SCRIPT_DIR/.."
# If unset, default values will be used:
echo "using nilfoundation/zkllvm-template:${ZKLLVM_VERSION:=0.0.86}"
echo "using nilfoundation/proof-market-toolchain:${TOOLCHAIN_VERSION:=0.0.37}"
echo "using nilfoundation/evm-placeholder-verification:${VERIFIER_VERSION:=latest}"

# podman is a safer option for using on CI machines
if ! command -v podman; then
DOCKER="docker"
DOCKER_OPTS=""
else
DOCKER="podman"
DOCKER_OPTS='--detach-keys= --userns=keep-id'
fi
DOCKER="docker"
DOCKER_OPTS=""

# checking files that should be produced
# on all steps of the pipeline
Expand All @@ -44,6 +39,13 @@ check_file_exists() {
fi
}

init() {
touch .config/.user
touch .config/.secret
touch .config/config.ini
mkdir -p build/template
}

run_zkllvm() {
cd $REPO_ROOT
# silently stop the existing container if it's running already
Expand Down Expand Up @@ -78,17 +80,11 @@ run_proof_market_toolchain() {
compile() {
if [ "$USE_DOCKER" = true ] ; then
cd "$REPO_ROOT"
$DOCKER run $DOCKER_OPTS \
--rm \
--platform=linux/amd64 \
--user $(id -u ${USER}):$(id -g ${USER}) \
--volume $(pwd):/opt/zkllvm-template \
ghcr.io/nilfoundation/zkllvm-template:${ZKLLVM_VERSION} \
sh -c "bash ./scripts/run.sh compile"
$DOCKER compose exec zkllvm bash ./scripts/run.sh compile
cd -
else
rm -rf "$REPO_ROOT/build"
mkdir -p "$REPO_ROOT/build"
mkdir -p "$REPO_ROOT/build/template"
cd "$REPO_ROOT/build"
cmake -DCIRCUIT_ASSEMBLY_OUTPUT=TRUE ..
VERBOSE=1 make template
Expand All @@ -103,13 +99,7 @@ compile() {
build_constraint() {
if [ "$USE_DOCKER" = true ] ; then
cd "$REPO_ROOT"
$DOCKER run $DOCKER_OPTS \
--rm \
--platform=linux/amd64 \
--user $(id -u ${USER}):$(id -g ${USER}) \
--volume $(pwd):/opt/zkllvm-template \
ghcr.io/nilfoundation/zkllvm-template:${ZKLLVM_VERSION} \
sh -c "bash ./scripts/run.sh build_constraint"
$DOCKER compose exec zkllvm bash ./scripts/run.sh build_constraint
cd -
else
cd "$REPO_ROOT/build"
Expand All @@ -128,13 +118,7 @@ build_constraint() {
build_circuit_params() {
if [ "$USE_DOCKER" = true ] ; then
cd "$REPO_ROOT"
$DOCKER run $DOCKER_OPTS \
--rm \
--platform=linux/amd64 \
--user $(id -u ${USER}):$(id -g ${USER}) \
--volume $(pwd):/opt/zkllvm-template \
ghcr.io/nilfoundation/zkllvm-template:${ZKLLVM_VERSION} \
sh -c "bash ./scripts/run.sh build_circuit_params"
$DOCKER compose exec zkllvm bash ./scripts/run.sh build_circuit_params
cd -
else
cd "$REPO_ROOT/build"
Expand Down Expand Up @@ -167,16 +151,7 @@ build_circuit_params() {
build_statement() {
if [ "$USE_DOCKER" = true ] ; then
cd "$REPO_ROOT"
$DOCKER run $DOCKER_OPTS \
--rm \
--platform=linux/amd64 \
--user $(id -u ${USER}):$(id -g ${USER}) \
--volume $(pwd):/opt/zkllvm-template \
--volume $(pwd)/.config:/.config/ \
--volume $(pwd)/.config:/root/.config/ \
--volume $(pwd)/.config:/proof-market-toolchain/.config/ \
ghcr.io/nilfoundation/proof-market-toolchain:${TOOLCHAIN_VERSION} \
sh -c "bash /opt/zkllvm-template/scripts/run.sh build_statement"
$DOCKER compose exec toolchain bash /opt/zkllvm-template/scripts/run.sh build_statement
cd -
else
cd /opt/zkllvm-template/
Expand All @@ -203,16 +178,7 @@ prove() {
# workaround for https://github.com/NilFoundation/proof-market-toolchain/issues/61
mkdir -p .config
touch .config/config.ini
$DOCKER run $DOCKER_OPTS \
--rm \
--platform=linux/amd64 \
--user $(id -u ${USER}):$(id -g ${USER}) \
--volume $(pwd):/opt/zkllvm-template \
--volume $(pwd)/.config:/.config/ \
--volume $(pwd)/.config:/root/.config/ \
--volume $(pwd)/.config:/proof-market-toolchain/.config/ \
ghcr.io/nilfoundation/proof-market-toolchain:${TOOLCHAIN_VERSION} \
sh -c "bash /opt/zkllvm-template/scripts/run.sh prove"
$DOCKER compose exec toolchain bash /opt/zkllvm-template/scripts/run.sh prove
cd -
else
cd "$REPO_ROOT"
Expand All @@ -227,12 +193,7 @@ prove() {
verify() {
if [ "$USE_DOCKER" = true ] ; then
cd "$REPO_ROOT"
$DOCKER run $DOCKER_OPTS \
--rm \
--volume $(pwd):/opt/zkllvm-template \
--volume $(pwd)/build/template:/opt/evm-placeholder-verification/contracts/zkllvm/template \
ghcr.io/nilfoundation/evm-placeholder-verification:latest \
sh -c "bash /opt/zkllvm-template/scripts/run.sh verify"
$DOCKER compose exec verifier bash /opt/zkllvm-template/scripts/run.sh verify
cd -
else
cd /opt/evm-placeholder-verification
Expand Down Expand Up @@ -264,6 +225,7 @@ while [[ "$#" -gt 0 ]]; do
build_statement) SUBCOMMAND=build_statement ;;
prove) SUBCOMMAND=prove ;;
verify) SUBCOMMAND=verify ;;
init) SUBCOMMAND=init ;;
run_zkllvm) SUBCOMMAND=run_zkllvm ;;
run_proof_market_toolchain) SUBCOMMAND=run_proof_market_toolchain ;;
*) echo "Unknown parameter passed: $1"; exit 1 ;;
Expand Down

0 comments on commit bf76d1c

Please sign in to comment.