Learning about Mina as a Developer (Part 1 — What is a zkApp & How to create one?)

Alysia Tech
7 min readOct 30, 2022

By the end of this article, you’ll understand:

  • What Mina is
  • What zero knowledge proofs are
  • What zkApps are (on Mina)
  • How to deploy a smart contract (on Mina)
  • Q&As — please add more in the comments

If you’re a developer 👩‍💻 👨‍💻 then you’re in the right place. I’m also a developer and together we’ll learn how to build on Mina. Accessing their developer documentation is easy, it’s the first menu option on their website.

Mina Website’s Menu Options

What’s Mina?

You can find the developer documentation here but for starter’s it’s a layer 1 blockchain. What makes it unique is the fact that you can create zero knowledge smart contracts (zkapps) AND the blockchain’s size is constant — 22KB! 😲

As a programmer, the first question you may have is, “What programming language do I have to learn to create apps on this blockchain?!”. The answer is Typescript, it’s easy for JavaScript developers to pick up and according to a Stack Overflow Developer Survey in 2022, it’s the 5th most used programming language. 😮

Top programming languages used by developers according to Stack Overflow 2022 Developer Survey

So, what is Mina again?

Mina is a L1 smart contract blockchain that’s based on ZK proofs and has a blockchain of a fixed size of 22KB.

(I’m sure I’m going to have to keep writing that again until I find a way to say it that makes more sense to me {and you}). 😅

So we’re used to smart contract platforms like Ethereum by now but what’s new here is the zero knowledge proofs and a blockchain of fixed size. Since the zero knowledge proofs cause the blockchain size to be fixed, let’s first try to understand what ZK Proofs are.

What are Zero Knowledge Proofs?

‘I searched the internet for more resources about ZK proofs and I liked this page posted by the Ethereum Foundation. [1]

A zero knowledge proof is a way of proving the validity of a statement without revealing any information apart from the fact that the statement is true. [1]

So if I say, Alysia has $20 Million dollars, and use a zero knowledge proof to prove that it’s true, then Alysia did not need to provide information to prove it, but using a ZK proof, we were able to validate that statement.

ZK Proofs help improve privacy whilst verifying information. What’s the alternative? Sharing your information with third parties which can leave person’s susceptible to identity theft etc.

Zero knowledge proofs use cryptography to prove that you know a secret without revealing it to anyone. [2]

It was actually invented in 1985 😲. Another project, Zcash, uses ZKPs to achieve anonymity in financial transactions.

Let’s get into some ZK Theory 😃

You may be tempted to skip this section but I’ve found that this really helped my appreciation for the code.

Properties of a ZK Protocol — CSZ [1]

TLDR: “You can’t tell me I’m wrong if I’m right, and if I’m wrong, there’s no way I’ll be right, and you have don’t know what I know.”

  • Completeness — if the input is valid, the ZK Protocol always returns true. This assumes that the prover and verifier act honestly and the statement is in fact true.
  • Soundness — if the input is invalid, it is theoretically impossible to fool the ZK protocol to return true.
  • Zero-knowledge — The verifier learns nothing about a statement beyond its validity or falsity (they have “zero knowledge” of the statement)

Elements of a ZK Proof — WCR [1]

TLDR: “The more questions I answer correctly, the higher the chance that I know what I’m talking about — even though you have no knowledge on the matter”

  • witness — the secret info is witness to a proof. The prover is trying to prove its knowledge of this hidden info
  • challenge — The verifier randomly picks another question from the set and asks the prover to answer it.
  • response — The prover accepts the question, calculates the answer, and returns it to the verifier. The prover’s response allows the verifier to check if the former really has access to the witness. The verifier keeps asking different questions to ensure that the prover isn’t guessing correctly by chance until the verifier is satisfied.

(Not all ZK Proofs are interactive, ZK Snarks and ZK Starks only have the prover and verifier interact once)

There are two types of ZK Proofs: [1]

(1) ZK Snarks

(2) ZK Starks

Use Cases for ZK Proofs are

  • private payments (payment can be validated without looking at the details of the transaction)
  • identity protection (validate identity and protect personal details)
  • authentication (prove your identity for access to a platform)
  • computation verification (outsource computation with verification that it’s correct — this is used in roll up solutions to improve layer 1 blockchains transaction throughput. Roll up solutions perform transactions off the layer 1 chains and roll up the proof that the transactions were valid )

How ZK Apps work on Mina 💻

Let’s say you wanted to create an app that maintains a variable and the variable can only be updated by its square. You’ll have to verify that the input to the function is the square of the stored variable, and return true if it is (& update the variable) and return false otherwise.

There are two parts:

  1. A smart contract (Code written in SnarkyJS — smartcontract.js)
  2. An User Interface (UI)
zkApp components
zkApp components

In smartcontract.js, developer write a circuit — the method from which a prover function and a corresponding verifier function are derived during the build process.

A circuit consists of:

  • a prover function — accepts inputs and creates a zero knowledge proof. In our example, the prover function will accept the new value to replace the variable if it can fit the constraints. This is accomplished using “assertions” in the code.
@method update(square: Field) {
const currentState = this.num.get();
this.num.assertEquals(currentState);
square.assertEquals(currentState.mul(currentState));
this.num.set(square);
}

When a user invokes a method on a smart contract, all assertions must be true in order to generate the zero knowledge proof from that smart contract. Data passed as input to the prover function is private and never seen by the network.

  • a verifier function — validates whether a zero-knowledge proof successfully passes all the constraints defined in the prover function.

Mina acts as the verifier and runs the verifier function. Mina only accepts the transaction and update the on-chain state if the attached proof is valid.

Mina acts as the verifier and runs the verifier function

Deploying a Smart Contract on Mina

What’s required:

  • build contract with npm build
  • run prover function which outputs the verification key (this lives on chain and is required to verify your prover function)
  • using the zkApp CLI, send a transaction to the Mina blockchain with the verification key.

The zkApp account (address) containing the verification key can only successfully receive transactions containing a proof that satisfies the verifier function. Invalid transactions will be rejected by the Mina Network (verifier function).

To prevent sybil attacks, you have to pay 1 MINA as a fee to deploy a smart contract (verifier key) to a new Mina address.

Q&A

Q: What is a zkAPP?

A: A zkApp on Mina is an application that consists of a smart contract written with SnarkyJS and a web browser UI so that users can interact with the app. The application uses ZK Snark zero knowledge proofs to validate transactions. The Mina blockchain verifies whether the proof is valid.

Q: How to create a zkApp?

A: To create a zkApp, you have to write Typescript code to create a smart contract. When you build the smart contract using the zkApp CLI, a verifier key is created which must be deployed to a Mina account on the blockchain. You have to pay a fee of 1 Mina to deploy a smart contract.

Q: What is needed in order for the Mina network to verify a proof.
A: The verification key lives on chain for a given zkApp account and is used by the Mina network to verify that a zero-knowledge proof has met all constraints defined in the prover.

Q: Can a Mina address have more than one smart contracts deployed against it i.e. contain more than one verification keys

A: (Not sure yet)

Q: Are zkApp Smart Contracts ran onchain or offchain?

A: Offchain

Q: Are state variables public on the Mina Blockchain?

A: So far, I know that state variables can be onchain or offchain. Onchain state of a zkApp is public [5].

Q: What’s private/public in a zkApp?

A: Onchain state is public, method parameters are private. [5] The only way method parameters can be exposed is when the computation explicitly exposes them. Example, if the method parameter/input is stored as the state of a variable in a smart contract.

Random Ideas

  • The data entered in a zkApp is private because the app lives in the browser but a programmer can specify to store the data on the blockchain. However, a programmer can make the data entered by the user public by storing it onchain or even offchain. Since no one actually reads code, there should be an app (zkApp) that verifies that the data that the user submits is private or not. So that they will know if the smart contract is safe.

References

[1] https://ethereum.org/en/zero-knowledge-proofs/

[2] https://minaprotocol.com/blog/zero-knowledge-proofs-an-intuitive-explanation

[3] https://docs.minaprotocol.com/zkapps/how-zkapps-work

[4] https://docs.minaprotocol.com/zkapps/tutorials/hello-world

[5] https://docs.minaprotocol.com/zkapps/how-to-write-a-zkapp

--

--