ZoKrates - zkSNARKs On Ethereum (made easy)

Written by edi.sinovcic | Published 2019/10/04
Tech Story Tags: ethererum | zk-protocols | zokrates | remix-plugin | zksnarks | remix-ide | latest-tech-stories | zero-knowledge-proofs

TLDR ZkSNARKs (Zero-Knowledge Succinct Non-Interactive Argument of Knowledge) or sometimes called "black magic" by some developers are generally considered hard to get into. We will try to guide you step by step through the process of compiling, setuping, exporting verifier, computing witness and generating proofs. For ease of use, you can copy remix compatible parameters which is a correctly formatted proof we use in the next step. You can examine the witness by opening it in Remix Editor.via the TL;DR App

ZkSNARKs (Zero-Knowledge Succinct Non-Interactive Argument of Knowledge) or sometimes called "black magic" by some developers are generally considered hard to get into.
What if we told you that there is a tool that can make this flow as easy as writing smart contracts?
Brace yourself, here comes ZoKrates as a Remix plugin.
For documentation on ZoKrates open the link.

From Zero to Hero (zkSNARKs edition)

We will try to guide you step by step through the process of compiling, setuping, exporting verifier, computing witness and generating proofs.
Let's go!
First thing first, open Remix IDE in incognito mode.
Navigate to plugin manager and activate the following plugins:
  • ZoKrates
  • Solidity Compiler
  • Deploy & Run Transactions
Then go to ZoKrates plugin and click on example hyperlink and accept needed permissions.
Update the code as you wish (Check official ZoKrates documentation for language reference on link).

Compilation

Once ready, compile your program by clicking Compile.
If no errors occurred, you should see a successful compilation message.
On the right side of the compilation panel, you can choose to Copy Bytecode or Download compilation output.

Setup

The next step is the Setup. Click on setup to expand and then click Run Setup.
If setup was successful you will see this message. You can download proving and verifying the key by clicking the Download Keys button.

Export Verifier

We can now proceed to the Export Verifier step.
By default, version 1 of the ABI is used. You can use version 2 by checking the Use ABI v2 checkbox before exporting the verifier.
If everything is successful, you can open the generated verifier in Remix Editor or Download the verifier. By clicking Open in Remix Editor button, the plugin will create and open verifier.sol file in Remix Editor. Verifier compilation and deployment is explained further on. 

Compile and deploy a verifier.sol

Now let’s compile and deploy a verifier.sol.
First, go to Solidity Compiler plugin and click on the compile verifier.sol.
Once that is finished go to Deploy & run transactions plugin and choose Verifier - browser/verifier.sol from the dropdown list and click deploy. (You can deploy it inside JavaScript VM or on any other testnet or mainnet. Be cautious, as these are big contracts and will consume a lot of gas.)
Once we’ve finished this, let’s return to Compute Witness and Generate Proof part in ZoKrates.

Compute Witness

A Compute Witness will ask you for input parameters. Once you fill the fields, click on Compute button to generate the witness.
If executed without errors, a success message will appear. You can examine the witness by opening it in Remix Editor. You can also download the witness if you wish.

Generate Proof

Last step is Generate Proof. Click on Generate. The output shown below should be shown.
If you remember from before, depending on the ABI version that we used on the export verifier step (ABI v1 vs ABI v2), the expected format of proof in the verifier is different. For ease of use, you can copy remix compatible parameters which is a correctly formatted proof we use in the next step.
You can Copy Output, Open in Remix Editor or Download it.

Verify

Now to validate the proof, go to Deploy & Run Transactions plugin and paste the remix compatible parameters into verifyTx field and click on the transact button.
After the transaction finishes executing, expand the logs and check the decoded output part. It should say "0": "bool: r true". That means that the transaction is successfully verified.
That's basically it. We've succeed! Now you are a ZoKrates ninja. Welcome to the club. It seems in the end that zkSNARKs are not that hard, don't you agree? ;)
Thanks to dark64 for the help.

Published by HackerNoon on 2019/10/04