Amendment XLS: Host Functions for zkSNARK Proof Over Curve BN254 #387
Replies: 1 comment 2 replies
-
|
Hi @ZhangxiangHu-Ripple Here are two interesting blogs on this topic. Perhaps also good to have an emergency function to easily rotate/migrate algorithm in case of vulnerability/compromise |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Amendment XLS: Host Functions for zkSNARK Proof Over Curve BN254
Host functions for elliptic curve arithmetic operations over BN254
Abstract
This XLS proposal suggests adding WASM host functions for elliptic curve arithmetic operations over the BN254 curve on the XRP Ledger. These functions provide the capability of efficiently verifying Zero-Knowledge Succinct Non-interactive Argument of Knowledge (zkSNARK) in XRPL WASM runtime. By enabling zkSNARK verification and combining with XRPL programmability, it is possible to unlock new features of privacy, scalability, and interoperability for XRPL.
1. Introduction
The Smart Escrows Specification proposes leveraging a WebAssembly (WASM) engine to enable programmability within XRPL Escrows, introducing the concept of smart escrows. Rather than the simple time-based or crypto-conditional constraints, the smart escrows allow users to write complex self-defined constraints for releasing the funds, including the verification of zkSNARK proofs.
However, executing the zkSNARK proof verification directly in WASM runtime is extremely expensive in terms of the running time, code size, and gas cost. This is because zkSNARK verification relies heavily on arithmetic operations over large integers such as elliptic curve operations and pairing computations, while WASM was originally designed to support high-performance, low-level code execution for web applications instead of computational workloads involving large-number arithmetic. Therefore, this specification proposes XRPL host functions for fundamental arithmetic operations over elliptic curves to enable the zkSNARK proof verification in a more efficient and economic way.
Here a host function is a function defined outside the WebAssembly module and made available to it via imports. These functions allow a WASM module to interact with the external environment, such as accessing system resources, performing native computations, or retrieving data, by invoking functionality implemented in the host runtime (e.g., the Rippled runtime), which can be used to save gas and computing time in WASM. Host functions are conceptually similar to precompiles in the EVM, as both provide efficient access to complex native operations. Some examples of XRPL host functions can be found in XLS-102d.
2. Curve and Group Specification
This specification proposes arithmetic operations over Barreto–Naehrig curve BN254. For pairing-friendly curves and the parameters, we refer to NIST standard, libff, and Arkworks for more details. BN254 is a pairing-friendly elliptic curve with embedding degree$$k = 12$$ over a 254-bit prime field. The seed parameter in BN254 is 4965661367192848881 which specifies the base field modulus $$q$$ and scalar field modulus $$r$$ as
The BN254 curve$$E$$ over $$F_q$$ is defined by $$E: y^2 = x^3 + 3$$ and its twist $$E'$$ over the extension field $$F_{q^2}$$ is defined by $$E': y^2 = x^3 + 3/\xi$$ where $$\xi = i + 9$$ is a non-residue with $$i^2 = -1$$ . $$F_{q^2}$$ is defined as $$Fq[i]/(x^2 + 1) = F_{q^2}$$
We also define two cyclic group,$$G_1$$ and $$G_2$$ , each of prime order $$r$$ , where
(x = 1, y = 2)Note that the expression here follows the convention in mathematics that the real part is presented before the imaginary part. However, in the serialization/deserialization of data, the imaginary part will be encoded before the real part. See section Encoding and Decoding.
3. Host Functions on BN254 Curve
This section specifies the host functions of elliptic curve arithmetic operations over BN254. All functions follow the same format defined in XLS-102d such that the returned value is
i32, unless otherwise noted. If the value is positive, it's a length. If it's negative, it's an error code.3.1 Negation
The negation host function
ec_neg_helpertakes the input of a point P1 and computes -P1, where the operation "-" is elliptic curve point negation. This function is used for negating an elliptic curve point to meet the requirements of the pairing check host function.ec_neg_helper(p1_ptr: i32,p1_len: i32,out_buff_ptr: i32,out_buff_len: i32)p1_lenis the length of input point P1 in bytes. The P1 length should be more than 64 bytes.out_buff_lenis the length of the output buffer that stores the result of -P1 after encoding.3.2 Addition
The elliptic curve addition host function
ec_add_helpertakes the input of two points (P1, P2) and computes P1 + P2, where the operation "+" is point addition over curve BN254.ec_add_helper(p1_ptr: i32,p1_len: i32,p2_ptr: i32,p2_len: i32,out_buff_ptr: i32,out_buff_len: i32)p1_lenandp2_lenare the length of input points P1 and P2 in bytes. The P1 length should be more than 64 bytes.out_buff_lenis the length of the output buffer that stores the result of P1 + P2 after encoding.3.3 Scalar Multiplication
The elliptic curve scalar multiplication host function
ec_mul_helpertakes the input of a point P1 and the scalar, and computes scalar * P1, where the operation "*" is scalar multiplication over the curve BN254ec_mul_helper(p1_ptr: i32,p1_len: i32,scalar_ptr: i32,scalar_len: i32,out_buff_ptr: i32,out_buff_len: i32)p1_lenandscalar_lenare the length of input point P1 and scalar in bytes. The P1 length should be more than 64 bytes and the scalar length should be more than 32 bytes.out_buff_lenis the length of the output buffer that stores the result of scalar * P1 after encoding.3.4 Pairing Check
The pairing check host function takes the input of$$n$$ pairs of points $$(Pi, Qi) \in G_1 \times G_2$$ , and checks if $$\Pi_{i=1}^{n} e(Pi, Qi) = 1$$ where $$e: G_1 \times G2 \rightarrow G_T$$ is a bilinear pairing operation. Here $$G_T$$ is the target group and 1 is the identity element of $$G_T$$ .
ec_pairing_check_helper(pairing_ptr: i32,pairing_len: i32)pairing_ptris a pointer that points to a byte array that stores the concatenation of encodedpairing_lenis the length of the byte array. The length should be more than 4*(64+128)=768 bytes. The function will return1if the pairing check is valid and-1if invalid. For other reasons that fail to perform the pairing check, it returns the error codes.Here, we use a fixed gas cost for the pairing check and limit the number of input pairs to a maximum of four. In the future, we will support an arbitrary number of input pairs once XRPL WASM VM allows dynamic gas cost.
4. Encoding and Decoding
All of the curve data is encoded in uncompressed format. Therefore,
Appendix
Appendix A: FAQ
A.1: Why picking BN254 curve instead of BLS12-381?
BN254 curve is natively supported by Ethereum. By supporting the same curve on XRPL, users can easily move their ZK verification code from Solidity to XRPL. In addition, most Zero-knowledge virtual machines (zkVMs) allow users to generate Groth16 proofs based on BN254 curve because of its efficiency when compared to BLS12-381. We are also considering adding host functions for operations over BLS12-381 curve in the future since it is more secure than BN254 curve.
A.2: Why add host operations for elliptic curve operations rather than a host function for directly verifying zkSNARK proof?
If XRPL provides a host function for directly verifying zkSNARK proof, once it is compromised, all escrows would be affected and the whole XRPL network would be under threat. If XRPL only provides host functions for EC operations and once the verifier in an escrow is compromised, only that escrow would be affected.
A.3: Why add an extra negation function?
For the purpose of user-friendly. Otherwise, users have to manually perform the negation of a point before sending it to the pairing check. It requires familiarity with the BN254 curve and arithmetic operations on big numbers in WASM runtime.
It is possible to flip the sign bit to negate a point, but the sign bit relies on the encoding/decoding details, which could be changed in the future.
Beta Was this translation helpful? Give feedback.
All reactions