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.
First thing first, open Remix IDE in incognito mode.
Navigate to plugin manager and activate the following plugins:
- 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).
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.
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.
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 we’ve finished this, let’s return to Compute Witness and Generate Proof part in ZoKrates.
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.
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.
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? ;)
- Improving performances on more complex programs by running it outside of the browser and/or optimizing and upgrading ZoKrates itself.
- https://zokrates.github.io/sha256example.html (Proof of preimage) — if you are interested in more in-depth explanation
- https://z.cash/technology/zksnarks/ (zcash blog on zksnarks)
- https://www.youtube.com/results?search_query=bar+ilan+winter+school+cryptography+michael+walfish+snarks (Youtube videos)