Simplifying Complex Tasks with Fixed-Point Numbers in zkSNARKS using LEO

Dmytriiev Petro
3 min readJul 31, 2023

--

Introduction

Are you intrigued by blockchain-based systems that offer privacy and scalability for complex applications like machine learning? The LEO programming language is here to simplify the development of zero-knowledge proofs (ZKPs)-based programs. However, one limitation is that LEO supports only integer-type-based numbers by default. This article explores the design of fixed-point numbers in zkSNARKS using LEO, enabling computation with fractions for a wide range of applications. Let’s delve into the world of fixed-point numbers and understand the trade-offs between precision and value range.

Representing Fixed-Point Numbers

Fixed-point numbers are a way to represent fractions using integers in zkSNARKS. To implement fixed-point notation in LEO, we use the integer type for variables and specify a scaling factor. This scaling factor determines the number of digits reserved for the integer and fractional parts of the value. For example, to represent 1.55 as a fixed-point number with two decimal digits, we assign 155 to the variable and set the scaling factor to 100.

Performing Arithmetic with Fixed-Point Numbers

With fixed-point numbers, we can perform various arithmetic operations like addition, multiplication, and division. For addition, we simply add the fixed-point numbers. For multiplication, we multiply the numbers and divide by the scaling factor to obtain the desired result. Division is similar to multiplication but involves multiplying by the scaling factor.

Generalization and Considerations

The scaling factor plays a crucial role in determining the precision and value range of fixed-point numbers. A higher scaling factor provides more precision but reduces the size of the integer part and, consequently, the representable value range. There is a trade-off between range and accuracy, and choosing the appropriate scaling factor depends on the specific application’s requirements.

Handling Overflow

When using fixed-point numbers with limited-size integer types, such as u32, there is a possibility of overflow during arithmetic operations. To avoid this, one can switch to a larger integer type, such as u64, but this increases the circuit size and may lead to higher proving costs.

Code Examples

The article provides code examples demonstrating addition, multiplication, and division of fixed-point numbers in LEO.

Addition of two numbers, a and b, in the fixed-point format:

‍Multiplication of two numbers, a and b, in the fixed-point format:

Division of two numbers, a and b, in the fixed-point format:

Conclusion

In conclusion, fixed-point numbers in zkSNARKS using LEO offer a powerful way to represent fractions and perform arithmetic operations in zero-knowledge applications. By carefully selecting the scaling factor, developers can strike a balance between precision and value range to suit their specific use cases. Although there are trade-offs to consider, fixed-point numbers pave the way for more advanced and privacy-preserving applications in the blockchain space.

To explore further and access the entire LEO project code, visit the GitHub page. Empower yourself with the knowledge to build complex programs in zkSNARKS and contribute to the exciting world of zero-knowledge proofs.

--

--

Dmytriiev Petro
Dmytriiev Petro

Written by Dmytriiev Petro

crypto geek from austria @ogpetya

No responses yet