Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update zkapp-development-frameworks.mdx #1072

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
232 changes: 189 additions & 43 deletions docs/zkapps/zkapp-development-frameworks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,68 +15,214 @@ keywords:
- protokit
---

:::info

To protect end users and ensure your zkApps are secure, consider the information at [Security and zkApps](/zkapps/writing-a-zkapp/introduction-to-zkapps/secure-zkapps) while architecting your solution and consider a third-party security audit before deploying to Mina mainnet.
# zkApp Development Frameworks

:::
Applications on Mina are called zkApps, and they come in a wide range of forms, including DeFi, games, state bridges, and more. However, due to Mina’s client execution model, the developer experience differs somewhat from that of other blockchain networks, such as Ethereum.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: "client side execution model"


# zkApp Development Frameworks
Developers build zkApps with **o1js**. Developers can either use o1js directly or use o1js with **Protokit**, a framework built on top of o1js. Both options offer the full benefits of Mina’s verifiable, privacy-preserving zero knowledge proofs, but offer different tradeoffs between developer experience, performance, and decentralization.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both options offer the full benefits of Mina’s verifiable, privacy-preserving zero knowledge proofs

Do they actually? Natively, afaik Protokit executes business logic on a server/"on-chain" - away from the client - this would render privacy-preserving not true in the default case

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An example from the protokit docs:

@runtimeModule()
export class Balances extends RuntimeModule<Record<string, never>> {
  @state() public balances = StateMap.from(PublicKey, UInt64);
 
  @runtimeMethod()
  public mint(canMintProof: CanMintProof, amount: UInt64) {
    canMintProof.verify();
    this.balances.set(address, amount);
  }
}

Seems like using a proof input would be a valid way of preserving privacy on protokit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not the default tho whereas with o1js it is, because everything happens locally on the clients machine

Copy link
Member

@Trivo25 Trivo25 Nov 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a difference between opt-in and opt-out so this needs to be highlighted


Developers building zkApps in the Mina ecosystem can leverage two different frameworks, each tailored to optimize different types of solutions.
Explore the options below to find the perfect fit for your project.
<table>
<tr>
<td>Info
</td>
</tr>
<tr>
<td>Please note that **Protokit is in alpha**, and settlement support with reorgs is still in progress.
<p>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no closing tag for

</td>
</tr>
</table>


This page provides a guide to choosing the best approach for your zkApp development - o1js directly or Protokit on top of o1js.

If you are unsure about any of the information presented here and need guidance on choosing the most suitable framework for you, drop by [Discord](https://discord.gg/minaprotocol) and let us help!

## [o1js](/zkapps/o1js)

o1js is the framework for building **zkApps on the Mina L1** and new infrastructure such as rollups.
o1js is TypeScript based for ease of use, comes with a host of built-in features, is extensible to suit various use cases, and takes full advantage of the unique aspects of the Mina protocol.
## Choosing a framework

Developers can build zkApps either directly with **o1js**, or with **o1js** + **Protokit**.

Protokit is built on top of o1js and simplifies the development of **zkApps with concurrent users requiring a shared state**, such as DEXes or games. Protokit is recommended for most developers.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Protokit is recommended for most developers.
is it? What's some examples when this is the case and when it isn't? If we recommend something we should clearly lay out the motivations


O1js is the best way to write **custom zero knowledge circuits** and construct low-level primitives on Mina, such as an NFT standard or a zkML library.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

o1js not O1js



<table>
<tr>
<td>Info
</td>
</tr>
<tr>
<td>For developers coming from the Ethereum ecosystem, think of zkApps like smart contracts that can be verified off-chain.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

zkApps aren't verified off-chain, they are proven off-chain and verified on-chain

<p>
This enables Mina to employ a client execution model, which gives Mina its scalability and privacy properties, but it can make it difficult to develop applications with concurrent users requiring a shared state. Protokit builds on top of o1js to solve these concurrency challenges.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: client side

</td>
</tr>
</table>



## Protokit[​](https://docs.minaprotocol.com/zkapps/zkapp-development-frameworks#protokit)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this link is broken


Protokit is designed for **zkApps with concurrent users requiring a shared state**, for example DEXes and most games. In addition to this Protokit provides an integrated verifiable data storage solution that further simplifies the developer experience compared to o1js.

Protokit can operate in two modes: Based Sequencing and Hybrid Sequencing.

In **Based Sequencing mode**, Protokit sends all transactions to the Mina L1, providing the decentralization guarantees of the L1, but limited to the L1’s throughput and latency. Based Sequencing mode is best for zkApps where censorship resistance and liveness are more important than the additional performance benefits.

In **Hybrid Sequencing mode**, Protokit runs an off-chain sequencer with off-chain data availability, providing higher throughput and lower latency than the Mina L1 but at the expense of decentralization. Hybrid Sequencing mode is best for zkApps where performance is critical.

**Important:** Users of a zkApp in Hybrid Sequencing mode can always **submit transactions directly to the L1**, for example, to withdraw funds (in case the zkApp sequencer is unavailable) but it does rely on the Protokit’s data availability (DA). Solutions are in the works for DA guarantees even in Hybrid Sequencing mode so that developers can get higher throughput and latency while retaining censorship resistance and liveness guarantees.

Start developing zkApps with Protokit here:

o1js is also the **zkDSL** used for:

- Writing general-purpose zk circuits.
- Constructing new primitives and data structures.

There are some key considerations when choosing to build a zkApp with o1js on Mina L1:
* [Developer documentation](https://protokit.dev/docs/what-is-protokit)
* [Protokit repository](https://github.com/proto-kit)
* [Starter Kit](https://github.com/proto-kit/starter-kit)

- zkApps are subject to protocol throughput limitations.
- At present, zkApps that require support for multiple concurrent users require specific architecture to avoid race conditions:
- Where more than the eight on-chain field elements are required to manage state, and access to that state is not shared between users, the experimental [Offchain Storage API](/zkapps/writing-a-zkapp/feature-overview/offchain-storage) offers a solution.
- Where concurrent access to _shared global state_ is required, the required architecture is available **out of the box** when using the Protokit framework to build your zkApp as an zkApp-chain (L2). There is currently no easy-to-use equivalent for shared state in o1js L1 contracts.

Start here:
## o1js[​](https://docs.minaprotocol.com/zkapps/zkapp-development-frameworks#o1js)

- [Developer documentation](/zkapps/o1js)
- [o1js repository](https://github.com/o1-labs/o1js)
- [Discord](https://discord.gg/minaprotocol)
[o1js](https://docs.minaprotocol.com/zkapps/o1js) is the best way to author **highly-optimized ZK circuits and primitives**, but it does not provide an easy-to-use solution for managing a shared global state.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OffchainState provides an out-of-the box solution to this. It's worth mentioning here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

o1js is also the only way to interfact directly with the protocol, its "not just" a tool for primitives

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should highlight offchain state as a serious way to handle app state until it's out of the experimental namespace. I don't think we should make casual references to things in the docs until we're ready to stand behind them 100%.


## [Protokit](https://protokit.dev/)
o1js can be used directly on the Mina L1, and is thus limited by the L1’s throughput and latency, but maintains strong decentralization guarantees.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is a good way to phrase this. o1js is a tool that lets you build whatever on top of Mina, you can build your own app-specific rollups that would not be limited by the L1s throughput.


Protokit is a powerful framework designed to build **ZK appchains and smart contracts** that are user-facing, privacy-preserving, interoperable.
It offers a familiar developer experience similar to Solidity dApps, making it easier and intuitive for developers to leverage ZK in their blockchain solutions.

It provides a full set of tools necessary for:
<table>
<tr>
<td>Info
</td>
</tr>
<tr>
<td><strong>Did you know?</strong> zkApps written directly with o1js can orchestrate concurrent users requiring shared state using Mina L1’s <a href="https://docs.minaprotocol.com/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer">Actions & Reducer</a>, but it requires significant development to orchestrate Action & Reducers. At its core, Protokit is a framework for orchestrating the Mina L1 Actions & Reducers out of the box.
</td>
</tr>
</table>


Start developing zkApps with o1js here:



* [Developer documentation](https://docs.minaprotocol.com/zkapps/o1js)
* [o1js repository](https://github.com/o1-labs/o1js)
* [Discord](https://discord.gg/minaprotocol)


## Choosing between Protokit and o1js

**Still can’t decide between Protokit and o1js?**

This section provides a simple decision tree x`and comparison table to help zkApp developers choose which framework to use.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tree x`and

typo?


![Decision Tree for zkApp devs](/img/decision-tree.png)

Are you developing a zkApp with concurrent users requiring a shared state?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not a fan of this decision tree. It ignores many details and will, imo in most cases, point developers to the wrong tools. We should educate devs on the benefits of both options, instead of assuming their use cases will just fit into one of two buckets

1. Yes - Protokit
1. Are you willing to trade off L1 censorship resistance and liveness guarantees for high throughput and low latency?*
1. Yes - Hybrid mode
2. No - Based mode
2. No - o1js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Existence of OffchainState makes this tree a bit complicated than this. It's true that OffchainState is not scalable as base mode Protokit since it doesn't keep a db thus recreates the offchain state Merkle tree on-the-fly which creates an overhead. But, It's ready to use now (not production-ready) and worth mentioning in the tree.

*Solutions are in the works for DA guarantees even in Hybrid Sequencing mode so that developers can get higher throughput and latency while retaining censorship resistance and liveness guarantees.

- zkApps that require high throughput or multiple concurrent users.
- zkApps that require shared or global state access.
- Developers familiar with execution environments such as EVM.
- Developers who wish to leverage the modular architecture of Protokit.

Start here:

- [Developer documentation](https://protokit.dev/docs/what-is-protokit)
- [Protokit repository](https://github.com/proto-kit)
- [Starter Kit](https://github.com/proto-kit/starter-kit)
- [Discord](https://discord.gg/bEGZTWRy)
<table>
<tr>
<td>
</td>
<td><strong>Protokit Hybrid</strong>
</td>
<td><strong>Protokit Based</strong>
</td>
<td><strong>o1js</strong>
</td>
</tr>
<tr>
<td><strong>Concurrency</strong>
</td>
<td>✅
</td>
<td>✅
</td>
<td>🟡
</td>
</tr>
<tr>
<td><strong>Shared State</strong>
</td>
<td>✅
</td>
<td>✅
</td>
<td>🟡
</td>
</tr>
<tr>
<td><strong>Throughput</strong>
</td>
<td>✅
</td>
<td>🟡
</td>
<td>🟡
</td>
</tr>
<tr>
<td><strong>Latency</strong>
</td>
<td>✅
</td>
<td>🟡
</td>
<td>🟡
</td>
</tr>
<tr>
<td><strong>Censorship Resistance</strong>
</td>
<td>🟡
</td>
<td>✅
</td>
<td>✅
</td>
</tr>
<tr>
<td><strong>Liveness</strong>
</td>
<td>🟡
</td>
<td>✅
</td>
<td>✅
</td>
</tr>
<tr>
<td><strong>Security</strong>
</td>
<td>✅
</td>
<td>✅
</td>
<td>✅
</td>
</tr>
</table>


**Understanding the performance vs. centralization tradeoff**

Protokit includes an off-chain sequencer and off-chain storage. If one or the other becomes inaccessible, users may find they can no longer access the zkApp including any tokens they may have in the zkApp.

If the Protokit instance’s sequencer is unavailable but the storage is still accessible, users may submit transactions (e.g. withdrawals) directly to the Mina L1.

If the Protokit instance’s storage is inaccessible, users will not be able to submit transactions to the Mina L1 and their tokens may be locked until the storage can be recovered.

Solutions are in the works for DA guarantees even in Hybrid Sequencing mode so that developers can get higher throughput and latency while retaining censorship resistance and liveness guarantees.

## Framework comparison

|| o1js SmartContract | Protokit |
|--|--|--|
|**Production readiness**|v1.0 released, internal audit complete, 3rd party audit in progress.|Beta release, internal audit in progress, 3rd party audit not started. Testnet only.|
|**Censorship resistance**|Decentralized and censorship resistant.|Censorship resistance via hybrid sequencing model.|
|**Support for multi-user apps**|Many multi-user use cases require sophisticated architecture and are limited by L1 throughput.|Capable of handling higher throughput and multiple concurrent users, thanks to Protokit's modular sequencer.|
|**Execution environment**|Proving off-chain, verification on-chain, transaction ordering possible on-chain.|Hybrid execution model, both on-chain (sequencer) and off-chain thanks to recursive zk-proofs, verification on-chain (MINA L1).|
|**DX**|New programming model, distinct from traditional web3.0 development.|Module oriented app-chain development, similiar to Substrate Pallets, Cosmos SDK Modules or EVM smart contracts.|
|**Composability**|Fully composable. Contracts can call other contracts directly within a single transaction.|Protokit supports bi-directional L2 &harr; L1 messaging out of the box.|