Zero-Knowledge Proofs: Full Guide, Part IV

From Zero to Knowledge: Modulus arithmetics

Lucas Martin Calderon
4 min readAug 10, 2023
Photo by Hubi's Tavern on Unsplash

We’ve all been there, scratching our heads, trying to make sense of numbers that seem to grow out of control in the computation of complex algorithms. When it comes to Zero-Knowledge Proofs (ZKPs), a foundational concept in blockchain security and cryptography, the need for managing large numbers is paramount. Enter the fascinating world of modular arithmetic.

In this article, we’ll dive into the workings of modular arithmetic, illustrate its applications in ZKPs, and explore how it helps simplify computations without losing the essential characteristics required for secure transactions.

Introduction to Modular Arithmetic

Modular arithmetic is often described as “clock arithmetic” because it operates in a cyclical manner, much like the hands of a clock. It’s a powerful mathematical tool used extensively in cryptography, computer science, and, of course, ZKPs.

The Basic Concept

Imagine dividing two integers, AA and BB, in the standard way:

A÷B=Q+RA÷B=Q+R

Here, QQ is the quotient, and RR is the remainder. In modular arithmetic, we’re interested in the remainder. The expression Amod BAmodB yields the remainder RR, so:

Amod B=RAmodB=R

For example, 10mod 3=110mod3=1.

Range of Remainders

An intriguing aspect of modular arithmetic is that for any given modulus nn, the remainder can range from 00 to n−1n−1.

Let’s illustrate this with an example using modulus 7. Here’s a set of integers: [1,5,10,15,22][1,5,10,15,22]. When we perform mod 7 on each of these numbers, the resulting values will fall within the range of 00 to 66:

[1mod 7,5mod 7,10mod 7,15mod 7,22mod 7]=[1,5,3,1,1][1mod7,5mod7,10mod7,15mod7,22mod7]=[1,5,3,1,1]

Congruence: A Closer Look

Congruence is a vital concept in modular arithmetic, represented by the ≡ symbol. Two integers, aa and bb, are said to be congruent mod nn if their remainders when divided by nn are the same.

Example 1:

25≡5 (mod 10)25≡5(mod10)

Because both 25 and 5 leave a remainder of 5 when divided by 10.

Example 2:

81≡9 (mod 12)81≡9(mod12)

Because both 81 and 9 leave a remainder of 9 when divided by 12.

Both 37 and 17, when divided by 10, leave a remainder of 7, so they are congruent mod 10.

Performing Arithmetic Operations with Modulus

Modular arithmetic isn’t just about remainders; it’s a full-fledged system allowing addition, subtraction, multiplication, and even a form of division.

Addition

In modular addition, you simply add two numbers and then take the remainder when dividing by the modulus.

Example 1:

4+7≡1 (mod 5)4+7≡1(mod5)

Example 2:

9+14≡8 (mod 15)9+14≡8(mod15)

Subtraction

Subtraction works similarly to addition. If the result is negative, you continue adding the modulus until you get a non-negative answer.

Example 1:

6−10≡2 (mod 4)6−10≡2(mod4)

Example 2:

17−22≡8 (mod 9)17−22≡8(mod9)

Multiplication

Multiplication is just as straightforward. Multiply the numbers and then take the remainder when dividing by the modulus.

Example 1:

4×5≡6 (mod 7)4×5≡6(mod7)

Example 2:

9×14≡11 (mod 19)9×14≡11(mod19)

Division: Modular Inverses

Division isn’t directly defined in modular arithmetic. Instead, we have the concept of modular inverses.

The modular inverse of A (mod C)A(modC) is A−1A−1, such that:

A×A−1≡1 (mod C)A×A−1≡1(modC)

To “divide” a number XX by AA in modular arithmetic, we multiply XX by the multiplicative inverse of AA:

AX​(modC)=X×A−1(modC)

Example:

If we want to divide 10 by 3 (mod 7), we first find the modular inverse of 3 (mod 7), which is 5. Then we multiply 10 by 5:

10×5≡6 (mod 7)10×5≡6(mod7)

Why Modular Arithmetic is Essential in ZKPs

Now that we’ve got the basics down, let’s examine why modular arithmetic is so crucial in ZKPs.

1. Handling Large Numbers

Modular arithmetic offers a finite set of numbers to work with. This avoids overflow issues and ensures that computations with large numbers remain manageable.

Example:

When working with 1024-bit numbers in cryptography, using a modulus keeps the numbers within a manageable range, reducing computational overhead.

2. Homomorphic Properties

The ability to perform operations on encrypted values without decryption is vital in ZKPs. Modular arithmetic’s homomorphic properties enable the proof generation without revealing the underlying values.

Example:

In a voting system, homomorphic encryption can be used to add encrypted votes together, and the result can be decrypted to reveal the final tally without exposing individual votes.

3. Computational Efficiency

As all computations are within a fixed range, modular arithmetic is typically faster and more resource-efficient. This is especially beneficial in blockchain systems and embedded devices.

Example:

In blockchain systems, fast modular computations ensure that transactions are processed quickly, even on resource-constrained devices.

Conclusion

Modular arithmetic is more than a mathematical curiosity; it’s a powerful tool that plays a pivotal role in modern cryptography and blockchain technologies. Its elegance in dealing with numbers, its rich set of operations, and its vital role in Zero-Knowledge Proofs make it an indispensable part of our digital world.

For those looking to delve deeper into modular math, the Euclidean algorithm for finding modular inverses and advanced cryptographic applications are exciting areas to explore. With a solid understanding of modular arithmetic, the world of cryptography and secure computing opens up with new possibilities and challenges.

--

--

Lucas Martin Calderon
Lucas Martin Calderon

Written by Lucas Martin Calderon

Founder & CEO @ Pentestify | Driving Blockchain Security with AI | SuperNova Under 30

No responses yet