CocosCreator with Telegram Game (Part 3) - ZKP

Learning how to use CocosCreator to develop Telegram game with ZKP

CocosCreator Development of Telegram Games (Part Three) - ZKP

Zero-Knowledge Proof (ZKP) is an advanced cryptography technology that allows one party (prover) ​​to prove to another party (verifier) ​​that it owns some information or meets a condition without revealing any specific information. The core of this technology is to protect the privacy of users while ensuring the authenticity and security of data. The wide application of zero-knowledge proof covers many fields such as blockchain, identity authentication, privacy protection, etc., helping to improve data security and user trust.

Take the game Ten Sum Rings for example, which is played by asking the user to circle a specific area on the screen. If the sum of all numbers in the range equals 10, the operation is successful. When users play games, they are actually utilizing the principle of zero-knowledge proof. Specifically, users can prove to the verifier that the sum of the areas they circle is indeed 10, without revealing the specific numbers in the area. Through such a design, users can protect their private information while ensuring the fairness of the game.

In the game implementation, the user's circled area can be verified through the zk circuit, and the verifier confirms and meets the condition based on the proof generated by the circuit. In this way, even if the verifier cannot access the user's specific circle number, it can still trust the user's operation result. Through zero-knowledge proof, the Ten Sum Rings game not only enhances privacy protection, but also improves participants’ sense of security and promotes the innovative use of blockchain technology in games and applications.

P1
P2

Zero-knowledge circuits (zk circuits) are the core components of zero-knowledge proof systems and are mainly used to describe specific logical or mathematical calculations. The zk circuit is a logic circuit model consisting of multiple gates and wires. Each gate represents a basic operation such as addition, multiplication, and comparison, while the lines represent numerical inputs, outputs, or intermediate results. The design of the zk circuit allows complex calculations to be combined through simple basic operations, thereby realizing arbitrary calculation logic.

In a zk circuit, there are three important components: Input value: including public input (public input) and private input (private input). Public inputs are data accessible to the verifier, while private inputs are confidential data of the prover and cannot be leaked directly to the verifier. Circuit logic: consists of a series of operations that define the logical flow of calculations. The design of the circuit needs to ensure that complex calculation processes can be effectively expressed and operational efficiency optimized. Output value: The output data finally generated by the zk circuit for use by the verifier. The output value is often used to verify the correctness of the calculation without revealing the specific content of the input. In this way, zk circuits ensure the transparency and credibility of computing while protecting privacy, and promote the application and development of private computing.

Here is the verifier contract for the circuit (FunC):

Before deploying the TON contract, the following preparations need to be completed:

  1. Write contract code: Use FunC language or other contract languages ​​that support TON Virtual Machine (TVM) to write contract logic to ensure the functional requirements and security of the contract.

  2. Install TON CLI tools: Use the command line tools officially provided by TON. These tools can help you compile, deploy contracts and interact with the blockchain.

  3. Prepare a TON wallet: Create and prepare a TON wallet to provide the initial deployment fee for the contract. This wallet must have enough TON tokens.

The overall process of deployment and interaction:

  1. Write contract code: Use FunC or other contract languages ​​to write contract logic.

  2. Compile contract code: Use the func tool to compile the contract code and generate a .fif file.

  3. Generate the .boc bytecode of the contract: Convert the .fif file into the bytecode (.boc file) of the contract through the fift tool, which is the key for the contract to run on TON.

  4. Generate contract address: Generate the contract address based on the bytecode and initialization parameters of the contract.

  5. Provide initial funds: Recharge the generated contract address to meet deployment costs.

  6. Deploy the contract to the TON network: Use the deploy command of the TON CLI to deploy the contract to the network.

  7. Verify contract status: Confirm whether the contract is successfully deployed by querying the contract address.

  8. Interact with the contract: Use TON SDK or CLI to call contract methods for data query or status update.

Here is Ten Sum Rings contract deployment code:

The process of calling the TON contract in Cocos Creator is as follows:

After 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 interface provided by the SDK, send a request based on the contract address and method name, and pass the necessary parameters and signature information. The data returned by the contract is then processed to realize the interaction between the front end and the contract on the chain.

The above is the on-chain verification process based on zk circuit and TON contract in Cocos Creator, which mainly includes the following steps: write zk circuit to design privacy calculation logic, generate zero-knowledge proof (ZKP) parameters, write the contract as FunC code and Compile it into TVM bytecode, and then deploy it on the TON network. Verification and data interaction are performed through the TON contract to achieve efficient and privacy-safe on-chain verification, ensuring data integrity and user privacy protection.

Last updated

Was this helpful?