ZoKrates — zkSNARKs On Ethereum (made easy)

Edi Sinovcic
Oct 3, 2019 · 5 min read

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.

Image for post
Image for post

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:

Then go to ZoKrates plugin and click on example hyperlink and accept needed permissions.

Image for post
Image for post
Image for post
Image for post

Update the code as you wish (Check official ZoKrates documentation for language reference on link).

Compilation

Once ready, compile your program by clicking Compile.

Image for post
Image for post

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 key by clicking the Download Keys button.

Image for post
Image for post

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.

Image for post
Image for post

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.

Image for post
Image for post

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.)

Image for post
Image for post

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.

Image for post
Image for post

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.

Image for post
Image for post

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.

Image for post
Image for post

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.

Image for post
Image for post

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? ;)

Next steps

Coinmonks

Coinmonks is a non-profit Crypto educational publication.

Sign up for Coinmonks

By Coinmonks

A newsletter that brings you week's best crypto and blockchain stories and trending news directly in your inbox, by CoinCodeCap.com Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Edi Sinovcic

Written by

Blockchain developer and consultant @ ShardLabs https://shardlabs.io

Coinmonks

Coinmonks

Coinmonks is a non-profit Crypto educational publication. Follow us on Twitter @coinmonks Our other project — https://coincodecap.com

Edi Sinovcic

Written by

Blockchain developer and consultant @ ShardLabs https://shardlabs.io

Coinmonks

Coinmonks

Coinmonks is a non-profit Crypto educational publication. Follow us on Twitter @coinmonks Our other project — https://coincodecap.com

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store