Five Minutes to Quickly Deploy TON Game with CocosCreator (Part Three): Developing ZKP-Based Telegram Games
Web3 games are a special vertical segment in the traditional gaming field. Built on blockchain, Web3 games typically possess characteristics such as decentralization, asset ownership, and P2E, making them highly sought after. Although Web3 games represent an advanced ideology, they still cannot compete with Web2 games in terms of gaming experience and content due to insufficient underlying infrastructure.
At this stage, the vast majority of blockchain layers are showing performance limitations. For example, the previously popular inscription track has exposed the performance shortcomings of many Layer 1 and Layer 2 solutions. Therefore, with the underlying infrastructure still imperfect, directly deploying game logic entirely on-chain is usually not feasible, as it can lead to lag in game operation and high costs for players to participate. As a result, most Web3 game teams write game logic on off-chain servers, placing only the game assets on-chain and updating the server data through on-chain transactions, continuously synchronizing data through data hubs, which is referred to as Web2.5. Thus, when game logic runs on centralized servers, game data is stored on those servers, and data synchronization between on-chain and off-chain is required, the game itself faces challenges in terms of trust, efficiency, and security, and the Web2.5 approach contradicts the principles upheld by Web3.
At the same time, mainstream blockchains, adhering to the principle of openness and transparency, do not support privacy. Therefore, many private gaming information of players is usually publicly accessible. Similarly, in some SLG and MMORPG games, such as poker, Monopoly, and collectible card games, unpredictability and privacy are necessary, but most blockchains typically cannot provide effective support for these needs. On the other hand, some blockchain layers with native privacy features are relatively lagging in terms of asset liquidity and user activity, and they are not friendly to game developers.
ZKP May Be the Key Solution
Zero-Knowledge Proof (ZKP) is an advanced cryptographic technique that allows one party (the prover) to prove to another party (the verifier) that they possess certain information or meet a specific condition without revealing any specific details. The core of this technology lies in protecting user privacy while ensuring the authenticity, security, and trustworthiness of data. Currently, ZKP solutions are being introduced into the gaming field to further achieve trustworthy game operations, enhance game efficiency and security, and provide privacy features for games.
Zypher Network is a promoter of this trend, launching a game engine based on ZKP solutions that enables developers to more easily create on-chain games or migrate games to the chain. By providing a Services SDK toolkit, it allows developers to write games in ZKP circuits with low barriers and compile the circuits directly into wasm, which can run in browsers or applications.
During game operation, nodes in the Zypher Network engine aggregate the action logs generated by players' continuous gaming behaviors into ZK proofs. Within a certain period, these gaming actions (each being a transaction) are aggregated into a single ZKP proof, and multiple proofs are submitted to the chain as a single transaction. This approach not only significantly reduces gas fees but also ensures a seamless gaming experience for players.
At the same time, the secret engine launched by Zypher Network can provide optional privacy solutions for different games. This engine implements a Shuffling SDK through optimized WASM and precompiled contracts, achieving a high-performance, low-cost decentralized shuffling process. The shuffling circuits and protocols ensure the security of verifiable encrypted computations executed on-chain, keeping strategic elements confidential. In addition to poker, Monopoly, and collectible card games, the SDK can also be applied to other SLG use cases that require trustlessness and randomness. This engine will also be used to ensure the security and privacy of player information in games, including in-game information and player identity, which will be kept confidential on-chain based on ZK solutions to ensure fairness and hide player information.
Furthermore, Zypher Network's off-chain computing network will continuously provide the computational resources required for ZKP.
Overall, the Zypher Network's ZKP-based game engine solution is enabling on-chain games to break free from centralized servers, moving towards server abstraction and a new era truly oriented towards Web3.
Game Use Case of Zypher Network Engine: Ten Sum Rings
Taking the game Ten Sum Rings as an example, the gameplay involves having users circle a specific area on the screen. If the sum of all the numbers within that area equals 10, the operation is successful. When users play the game, they are actually utilizing the principles of zero-knowledge proof. Specifically, users can prove to the verifier that the sum of the circled area is indeed 10 without revealing the specific numbers within that area. Through this design, users can protect their private information while ensuring the fairness of the game.
In the implementation of the game, the user's selected area can be verified through zk circuits, and the verifier confirms whether the sum meets the conditions based on the proof generated by the circuit. Thus, even if the verifier cannot access the specific numbers selected by the user, they can still trust the user's operation results. Through zero-knowledge proof, the Ten Sum Rings game not only enhances privacy protection but also increases participants' sense of security, promoting innovative uses of blockchain technology in games and applications.
Currently, the Zypher Network game engine has integrated CocosCreator, allowing developers to directly develop on-chain games through CocosCreator. At this stage, Zypher ecosystem game developers have launched dozens of high-quality Telegram mini-games using ZKP solutions on the TON Network.
Next, we will introduce how to step by step develop ZKP-based Telegram games using CocosCreator, hoping this tutorial will be helpful to you.
Quickly Deploy TON Games Using CocosCreator (Part Three): How to Develop ZKP-Based Telegram Games with CocosCreator
If you are not very familiar with CocosCreator and Telegram, you can first refer to the following content (Part One of the tutorial):
https://www.chaincatcher.com/article/2140947
Part Two of the tutorial: How Web2 Games Use Ton Payments
https://www.chaincatcher.com/article/2146780
And the Zypher Network developer documentation:
Zero-knowledge circuits (zk circuits) are the core components of zero-knowledge proof systems, primarily used to describe specific logic or mathematical computations. Through zk circuits, the prover can demonstrate the correctness of a computation to the verifier without disclosing any private information. This technology is widely used in privacy protection, blockchain, identity verification, and other fields, providing guarantees for data privacy and security.
A zk circuit is a logical circuit model composed of multiple gates and wires. Each gate represents a basic operation, such as addition, multiplication, and comparison, while wires represent the input, output, or intermediate results of values. The design of zk circuits allows complex computations to be combined through simple basic operations, thus achieving arbitrary computational logic.
There are three important components in zk circuits:
- Input values: including public input and private input. Public input is data accessible to the verifier, while private input is the prover's confidential data that cannot be directly disclosed to the verifier.
- Circuit logic: composed of a series of operations that define the logical flow of the computation. The design of the circuit needs to ensure that it can effectively express complex computational processes and optimize operational efficiency.
- Output values: the output data generated by the zk circuit, used by the verifier. Output values are typically used to verify the correctness of the computation without revealing the specific contents of the inputs.
In this way, zk circuits protect privacy while ensuring the transparency and trustworthiness of computations, promoting the application and development of privacy computing.
Here are the contracts containing the circuits:
Before deploying the TON contract, the following preparations need to be completed:
- Write contract code: Use FunC language or other contract languages that support the TON Virtual Machine (TVM) to write contract logic, ensuring the functionality and security requirements of the contract.
- Install TON CLI tools: Use the command-line tools provided by TON, which can help you compile, deploy contracts, and interact with the blockchain.
- Prepare a TON wallet: Create and prepare a TON wallet to provide initial deployment costs for the contract, ensuring that this wallet has enough TON tokens.
The overall process of deployment and interaction:
- Write contract code: Use FunC or other contract languages to write contract logic.
- Compile contract code: Use the func tool to compile the contract code, generating a .fif file.
- Generate the contract's .boc bytecode: Convert the .fif file into the contract's bytecode (.boc file) using the fift tool, which is crucial for the contract to run on TON.
- Generate the contract address: Generate the contract address based on the bytecode and initialization parameters.
- Provide initial funds: Recharge the generated contract address to meet deployment costs.
- Deploy the contract to the TON network: Use the deploy command of TON CLI to deploy the contract to the network.
- Verify contract status: Confirm whether the contract has been successfully deployed by querying the contract address.
- Interact with the contract: Use the TON SDK or CLI to call the contract methods for data queries or state updates.
Ten Sum Rings contract deployment code:
The process of calling the TON contract in Cocos Creator is as follows: Once the TON contract is successfully deployed and the contract address is obtained, the front end can interact with the contract by integrating the TON SDK (such as TonWeb or TonConnect). In the Cocos Creator project, use JavaScript to call the interfaces provided by the SDK, sending requests based on the contract address and method name, along with the necessary parameters and signature information. Then, process the data returned by the contract to achieve interaction between the front end and the on-chain contract.
The above describes the on-chain verification process based on zk circuits and TON contracts in Cocos Creator, which mainly includes the following steps: writing zk circuits to design privacy computing logic, generating zero-knowledge proofs (ZKP), then writing the contract as FunC code and compiling it into TVM bytecode, deploying it to the TON network. By using the TON contract for verification and data interaction, efficient and privacy-secure on-chain verification is achieved, ensuring data integrity and the protection of user privacy.