Learn

What You Should Learn Before Building a zkApp

zkApps are relatively simple to build but there are three fundamental things you should understand first.

The wave of private and scalable smart contracts in Web3 is underway, and Mina Protocol, with its constant-sized succinct blockchain and easy-to-write zero knowledge apps, zkApps, is leading in this transformation.

Mina is finally putting ZK in the hands of the Web3 developer with zkApps, short for zero knowledge decentralized applications.

Although building zkApps is relatively simple, there are three fundamental things you should understand before starting to build.

1. TypeScript Syntax

To ensure an intuitive developer experience, zkApps are written in TypeScript. TypeScript is a superset of JavaScript that adds type safety, meaning type correctness is checked at compile time. Another benefit of writing smart contracts in TypeScript is having familiar developer experience and TypeScript toolings, such as npm, Prettier, Jest, ESLint, VScode with IntelliSense code auto-completion, and more.

The main difference between writing zkApps and other zero knowledge smart contracts is that others use custom programming languages, requiring builders to learn something entirely new.

With most developers already knowing TypeScript, zkApps will be the easiest way to start building. If you aren’t familiar with TypeScript, however, there are many existing resources to learn how to write in TypeScript here or here. You can find the official TypeScript documentation here

As long as you know TypeScript syntax, you will be ready to get started quickly.  Installing TypeScript is unnecessary because the Mina zkApp CLI contains TypeScript locally. 

2. How zkApps Work

zkApps use zero knowledge proofs, specifically zk-SNARKs. They use off-chain computations and an off-chain state model, allowing for private executions and a flexible state model so users can choose to set their states private or public.  As opposed to other blockchains that run computations on-chain with variable gas fees for running dApp executions, zkApps offer limitless off-chain computations and on-chain verification for a low flat fee. o1js (formerly SnarkyJS), the TypeScript library for writing zkApps, uses a Merkle tree to store data off-chain and zkOracles, which are a feature on the roadmap, will also further help with off-chain storage. 

In order to understand how zkApps work, there are a couple of concepts that you have to generally understand about zero knowledge smart contracts. There’s a prover function and a verifier function

A zkApps prover function runs locally on users’ browsers and executes smart contract logic. Circuits, which are covered later in this blog, are a part of the prover function.
A zkApps verifier function is stored on the Mina blockchain and it validates the correctness of prover function execution.

zkApps consists of two parts: a UI and a smart contract.  The prover and verifier functions are part of the smart contract. As privacy becomes more important to end-users, zkApps protect sensitive data so they don’t need to share publicly on a blockchain, but instead via the zkApp’s UI. Since the prover function runs locally, this private data acts as a private input to the prover function. Then the prover function generates a zero knowledge proof of its execution, without revealing any sensitive user data. This ZK proof is sent to the Mina blockchain, where the corresponding verifier function validates the proof. 

Mina’s off-chain execution allows users to keep their data in user custody and only share proof of computation with the world. Learn more about how zkApps work here.

3. How Circuits and o1js (formerly SnarkyJS) Work

Lastly, you need to learn the difference between developing smart contracts and ZK smart contracts. In ZK smart contracts, a zero knowledge proof is generated over computations inside a function called a circuit.  

To understand the zk-SNARK circuits, think of the boolean circuits analogy. In boolean circuits, a gate represents one of the Boolean operations (AND, OR, XOR, NOR). Inputs are variables on which the boolean operations are applied, and outputs are results of the boolean operations on these variables.

In an arithmetic circuit, the gate represents arithmetic operations (PLUS, MINUS, MULTIPLICATION, DIVISION), where inputs are variables, and outputs are a result of arithmetic operation on the inputs. 

o1js (formerly SnarkyJS) is a library for writing zkApps for Mina Protocol, and it hides many zk-SNARK complexities from developers, providing a straightforward way of building on Mina. The experience of writing a zkApp is similar to writing regular TypeScript programs. SnarkysJS circuits work similarly to boolean and arithmetic circuits with their own assertions for inputs so you can set parameters to constrain state updates. The only difference is that o1js (formerly SnarkyJS) uses built-in data types and methods, instead of their JavaScript counterparts. Learn more in the o1js (formerly SnarkyJS) docs.

Next Steps

Once you’re familiar with these things, you will be equipped to start writing zkApps on Mina. Here are a few tips on how you can get started:

  1. Install the zkApp CLI here.
  2. Learn to code an example zkApp using this tutorial.  
  3. Build a zkApp with one of the three Mina developer grant programs to get hands-on support and funding.

There are many more resources on how to write, test, and deploy a zkApps here, but these steps should get you started.

If you have questions, please visit the  #zkapp-developers channel on Discord.

More from our Blog

SEE ALL POSTS
Retro / 2024-03-21 / Vitor Silva
Upgrade Mechanism Testing Retrospective
Track 3 allowed for the testing of various loads and helped uncover issues which have since been resolved. As a result, an optimal configuration was identified, and the release candidate for the Mainnet Upgrade is ready.
Read more
Learn / 2024-03-15 / Will Cove
Introducing ‘httpz’: the internet you can trust
Read more
Community, Events / 2024-03-13 / Mina Protocol
BUIDL with Mina Protocol at ETH Seoul 2024
Read more
Announcement / 2024-03-12 / Mina Foundation
zkIgnite, Cohort 3 Funded Projects
Read more

기술 살펴보기

AboutTechCta

Mina uses advanced cryptography and recursive zk-SNARKs to deliver true decentralization at scale.

시작하기

GetStartedCta

Mina makes it simple to run a node, build and join the community.