Exploring zkApps on the Mina Protocol: Powering Privacy-Preserving Smart Contracts
A comprehensive guide to understanding Zero Knowledge Proofs, ZK SNARKs, and their applications in the Mina Protocol
Introduction
I know what you are thinking. Oh no, another blockchain, it feels like everyone is creating their blockchain. Oh, Zk is now the new buzz word and so on.
But wait, I wanted to let you know about Mina Protocol earlier known as Corda Protocol just reimagines the way we get to see the traditional blockchains. The way the blockchains came into existence was the need for a decentralised medium and p2p communication to leverage security and decentralisation but as we know the size of the blockchains is so big and they keep on getting bigger there are hardly any people who would set up and download the whole blockchain data just to transfer funds. So, in some sense, we are again trusting another centralised node service for that. Well, this seems too impractical to do so but it wouldn't if it's only 22KB.
Yes, This is possible with the help of Zero Knowledge Proofs that the Blockchain itself is a Succinct Proof. So, instead of nothing we get full node security with just 22KB of data. Now, let's understand how it does.
ZK Proofs
Zero-knowledge proofs are used to prove something to the verifier and are only limited to it without revealing any specific information about it. It is more like a yes or no question.
Let us understand with some famous examples -
Spotting Waldo - Where's Waldo is a puzzle game where you need to find a picture of Waldo on a page which is hidden.
Let us say I want to prove to you that I know the spot where Waldo is on a page without actually revealing the spot. To do that, I can take a very big piece of sheet and place it on the book and make a small hole just beneath the picture of Waldo without showing its neighbouring region.
This can be a very simple example of how ZK proofs work.
Prove Colour Blind - ColourBlindness is a condition where a person can not distinguish between certain colours.
Let us say I want to prove to a colour-blind person that I can distinguish between two identical balls of the same shape and size but red and blue. To the colour-blind person, they both seem to be blue-coloured.
Those balls can be given to them in their hands, now they can secretly choose to change their order without showing me and ask if the position of the ball is changed or not. Now the probability of choosing the right answer is 50% in it but increasing the number of times we perform the task, say 20 would drop it down to 0.00000095 and so on.
Throughout the proof, the colour-blind person do not get to know the individual colour of the balls and we just managed to proof them that we know that these balls are different but without giving away the colour of those balls.
Sudoku Solution - Suppose you claim to have solved a difficult Sudoku puzzle. To prove it without revealing the solution, you can allow the verifier to select any row, column, and region within the puzzle and provide them with the sum of the numbers in that selection. By repeating this process for different selections, you can convince the verifier that you possess the correct solution without disclosing it explicitly.
Hamiltonian Cycle - A Hamiltonian cycle is a cycle in a graph that visits each vertex exactly once. Zero-knowledge proofs can be used to prove the existence of a Hamiltonian cycle without revealing the specific cycle itself.
In a zero-knowledge proof for a Hamiltonian cycle:
The verifier selects a random subset of vertices in the graph.
The prover demonstrates that there exists a Hamiltonian cycle in the graph that includes all the vertices in the subset, without revealing the exact cycle or the remaining vertices.
By performing a series of permitted operations on the graph, the prover modifies it while preserving the Hamiltonian cycle.
The verifier checks if the modified graph contains the subset of vertices and satisfies the properties of a Hamiltonian cycle.
If the modified graph passes the verification, the verifier becomes convinced that the prover knows a Hamiltonian cycle without learning any specific details about the cycle or the remaining vertices.
There are many examples of zero-knowledge proofs that demonstrate the power of this concept. These examples demonstrate the versatility of zero-knowledge proofs in various scenarios, providing a way to prove knowledge or statements without revealing sensitive information. Zero-knowledge proofs have applications in areas such as cryptography, privacy-preserving computations, and secure multi-party protocols.
ZK SNARKs
ZK SNARKs, which stands for Zero-Knowledge Succinct Non-interactive Argument of Knowledge, play a crucial role in achieving the remarkable efficiency and security of the Mina Protocol. Let's break down what each component of ZK SNARKs represents:
Zero-Knowledge - In the context of ZK SNARKs, zero-knowledge means that a prover can convince a verifier of the validity of a statement without revealing any additional information beyond the statement's truthfulness. It allows for the transfer of knowledge while maintaining privacy and confidentiality.
Succinct - The term succinct refers to the concise nature of the proof generated by ZK SNARKs. Unlike traditional proofs, which can be lengthy and resource-intensive, ZK SNARKs produce incredibly compact proofs. In the case of the Mina Protocol, the entire blockchain can be represented as a succinct proof with just 22KB of data, making it highly efficient.
Non-interactive - ZK SNARKs are non-interactive, meaning that the proof generation process does not require back-and-forth communication between the prover and the verifier. This property eliminates the need for real-time interaction, allowing for faster and more streamlined verification.
Argument of Knowledge - ZK SNARKs provide an argument that demonstrates the knowledge of a specific statement without revealing any underlying details. It allows the prover to convince the verifier of the validity of the statement without disclosing the actual information or calculations used to derive the proof.
Using MINA
We would now learn how to start using and developing MINA Protocol and protect your data.
Wallet
Wallets play a crucial role in managing cryptocurrency assets, including MINA. Auro Wallet for MINA is designed with user-friendliness in mind, providing a seamless experience for users to manage their MINA assets.
During the account creation process, the network imposes a one-time fee of 1 MINA to prevent spam and ensure the security of the platform.
In addition to these measures, Auro Wallet for MINA offers all the basic features you would expect from a reliable wallet, allowing users to send, receive, and store their MINA securely and interact with zkApps.
ZkApp
ZKApp is an abbreviation for Zero-Knowledge Application. ZKApps leverage the power of zero-knowledge proofs to provide enhanced privacy and security in decentralized applications (dApps).
One key feature of zkApps is their off-chain execution and state model. This means that complex computations can be performed off-chain while maintaining privacy. The computed results can then be submitted to the blockchain as zero-knowledge proofs for verification, rather than running the computations directly on the chain itself. This approach reduces the computational burden on the blockchain and enables a flat fee model for sending the proofs for verification, as opposed to variable gas fees used by other blockchains.
Developing ZkApps
Although zkApp programmability is not yet available on the Mina Mainnet, developers can deploy zkApps to the Berkeley Testnet for testing and development purposes.
A zkApp consists of two main components: a smart contract and a user interface (UI). The smart contract, written in TypeScript using the Mina zkApp CLI, defines the custom logic of the zkApp. The UI provides a user-friendly interface for users to interact with the zkApp.
Smart contracts are written in Typescript using the SnarkyJS library and there are a lot of tutorials available on Mina Docs.
UI can be implemented in React/Next, Svelte or Vue. More preference for React and Svelte as the sample GitHub Repos are available for those.
Setting up the environment
To start developing ZkApps, you need
NodeJS 16+ (or 14 using node --experimental-wasm-threads)
NPM 6+
Git 2+
Just install the zkapp Command Line Interface with this command.
npm install -g zkapp-cli
After this, you can either
Work on Examples
zk example
Create a New Project
zk project <project-name>
This also prompts for UI, where you can select from different frameworks.
Now, this would allow you to create the next Product on MINA which would make an impact on a large amount of people.
Deploying on zkApps
After creating the smart contract, you would need to deploy it on the testnet. To do that, move on to the contracts folder and run the config command.
zk config
Now, the configuration would require some fields which you can fill in this way -
Deploy alias name: <project-name> (Can be anything)
Mina GraphQL API URL: https://proxy.berkeley.minaexplorer.com/graphql
Transaction fee to use when deploying: 0.1
Choose Account - Extract the private key from your Auro Wallet
It would create an alias which can be used while deploying the contract on Blockchain. Also, we can config as many aliases as we want for different accounts or contracts.
To deploy, you would need some funds which you can acquire from this Faucet.
It takes some time for the transaction to be confirmed.
To deploy your smart contract to the network, run the following command:
zk deploy <project-name>
It would ask for confirmation and then it would be sent to the testnet from where you can use it in UI.
Now you know how to deploy your zkApp contracts live on the blockchain. Kudos to you for this amazing achievement ๐.
Conclusion
In conclusion, the Mina Protocol offers a groundbreaking approach to blockchain technology through the use of Zero-Knowledge Proofs (ZK Proofs) and ZK SNARKs. With its compact and efficient representation of the blockchain, Mina provides scalability, privacy, and security.
In our next blog post, we will explore how to write smart contracts and connect them with user interfaces (UI) using Snarky, a TypeScript library, and popular UI frameworks like React, Next, Svelte, or Vue. Stay tuned for an in-depth tutorial on harnessing the power of Mina Protocol and building privacy-preserving decentralized applications (zkApps).
Join us on this journey as we delve into the exciting world of zkApps and unlock the full potential of the Mina Protocol.