Zero Knowledge Proofs Workshop - Zokrates Tutorial


Pre requisites

docker run -v $PWD/code:/home/zokrates/code -ti zokrates/zokrates /bin/bash

Zokrates Introduction

Zokrates - A toolbox for zkSNARKS on Ethereum

Zokrates Repo -

It has a DSL with python like syntax

Zokrates Workflow

Zokrates DSL syntax

The python-like syntax is composed of
primitive uint types (positive numbers),
imperative algebraic statements,
for loops,
conditional if statements and function definitions.

The compiler transforms the conditions to a constraint system of an arithmetic circuit from which a zk-SNARK is generated.
The verification key can then be exported to a smart contract allowing for verification of proofs on the Ethereum blockchain.

The building blocks for the on-chain verification algorithm reside on the blockchain as pre-complied
contracts and the outcome of the verification algorithm on a provided proof can be used to further trigger
other on-chain activity (i.e., if true then …).

For documentation, see ZoKrates/zokrates_book

Zokrates DSL in more detail



def main(private field a, field b) -> (field):
field result = if a * a == b then 1 else 0 fi
return result

Variables are mutable, and always passed by values to functions.


def main() -> (field):
field res = 0
for field i in 0..4 do
res = res + i
return res

The bounds should be known at compile time, i.e. constant

For example code see :

A simple example with workflow

mkdir code
docker run -v $PWD/code:/home/zokrates/code -ti zokrates/zokrates /bin/bash

Write root.code

def main(private field a, field b) -> (field):
field result = if a * a == b then 1 else 0 fi
return result
./zokrates compile -i code/root.code

perform the setup phase

./zokrates setup

execute the program

./zokrates compute-witness -a 337 113569

generate a proof of computation

./zokrates generate-proof

export a solidity verifier

./zokrates export-verifier

Useful Resources

Gnosis Tutorial
Using Zokrates with Truffle Tutorial

zk-SNARKS and Zokrates Tutorial

Zokrates Repo

Zokrates Book