Modular Arithmetics
After learning how to isolating number systems in our 1st lesson, we need a loop where these numbers should rotate or revolve around. And this exactly what this 2nd lesson of Modular Arithmetics is all about. This topic sets the stage for the next one coming which is finite fields. So, it is good to make sure you understand it well.
Let me give you some day-to-day example to understand the concept of today. Imagine a world where we do not use 7 days of the week. Instead we had new name everyday right from the day you were born. This would have been catastrophic for just one year alone, as we will have 365 different names for each of the 365 days. To keep things simple, we instead rotate only around 7 days. Surprise surprise, you have been practising modular math in your whole life and you never knew that.
Modern Computer Science has put this into practice. Modular arithmetics makes many things possible and this is why ZK finds it useful; Keeping numbers finite and manageable, enabling Discrete Logarithm Problem(DLP - Less worries about this for now) & expelling negative values, decimals and many more.
1. Rational Division / The Floating-Point Division​
You remember the normal division of 13 / 5 = 2.6, well, ZK circuits cannot process deicmals because it is a giant system of algebraic equations that can only perform two fundamental operations: addition and multiplication on whole numbers. Decimals and -tive values break this rule and this is exactly what this lesson of today wants to solve. Here is where the Euclidean Division comes and allows us to do division using only whole-number multiplication and addition.
2. The 'Scary' Euclidean Division(ED)​
ED is just my informal shortcut for Eucledian Division. I don't want to write this long fancy name evertime.
The ED eliminates the decimal we have seen before by curving out reminders from whatever number that has not been able to divide. For the 13 / 5 = 2.6, it checks how many times does 5 get into 13 which is 2 and then there is remainder of 3.
ED states: Given any two whole integers; a and m, where m > 0, there exist unique integers q and r such that:
a = (q * m) + r, where:
- a (The Dividend): The total number you are starting with.
- m (The Modulus/Divisor): Your cap limit or "group size".
- q (The Quotient): How many full groups you can completely pack away.
- r (The Remainder): The exact leftover pieces that couldn't form a full group.
To give an example for this, lets have; 13 = (2 * 5) + 3, where 13 represents the a, the 2 represents the q, the 5 represents the m and the 3 represents the r.
The core rules governing the remainder r are:
rmust be equal or greater than zero(r >= 0): This means that ifrless than zero, then the division has -tive value.rshould be less than them(r < m): It is mathemetically impossible to get a remainderrthat is greater than the modulo. For example for our case here, since our mod was 5, the remainder would only be either0 or 1 or 2 or 3 or 4, of which we got3for our example.
Quick Quiz: Has the decimal problem bee solved?: If you compare the 13 / 5 = 2.6 to that of ED 13 = (2 * 5) + 3, you see that ED has come with a remainder instead of the decimal.
3. Examples of modular arithmetic Operations​
Lets deal with the normal addition and multiplication with & without modular operation here. As for subtraction and division, their case is special and must be handled in 'the Supreme Court.'
Without Mod, (A) 5 + 5 = 10 (B) 5 * 5 = 25
You see 10 & 25 are the normal addition and multiplication results, but suppose we introduce cap limit called Mod, which brings back our operations back to limit as we have seen with the 7 days example of the week.
Without mod, 5 + 5 = 10 gets 10 as the result. But with mod, 5 + 5 (mod 7) = 3. the result equals 3, because 10 - (7 * 1) = 3. You check how many times does the mod get into the result, and then you subtract the rest and remain with the remainder.
The same goes for multiplication. 5 + 5 (mod 7) = 4. You check how many times does 7 get into the result 25 and then subtract remaining with the remainder, 25 - (7 * 3) = 4.
With Mod, (A) 5 + 5 (mod 7) = 3, (B) 5 * 5 (mod 7) = 4
Nb: You can check how each results falls within the modular number no matter how big the numbers are. For our case here all of the results must fall strictly within a range of 0 - 6.
Since division can bring about decimals and subtraction can come with negatives, we use notion known as inverse operations to avoid the two abominations of negative & decimal values. And this is what we tackle in the next section. Now lets take subtraction and division to The Supreme Court.
4. Inverse Elements(The Supreme Court Case Submission)​
Before we learn about inverse elements, lets first check on on something called Identity Elements(ID element). This is a mathematical 'do nothing' neutral baseline number that leaves another number completely unchanged.
For example, if you add 5 + 0 it remains 5. In this case, zero has not done any change to 5. For this matter, Zero is the identity element for addition. The same goes for multiplication, where its identity element is 1. This is because 5 * 1 remains just 5.
Definition of an Inverse: From basic mathematics, you know that an inverse is just a way of forcing a certain number back to its neutral baseline.
Quiz: Where am i taking this? Am i taking to you to Jupiter or a blackhole?
The point i am coming to is that, there are two inverse elements;
- Multiplicative Inverse (
mul invas i will informally call it): This is the number when multiplied by number, pulls it back to theidentity elementwhich is1. For an example, themul invof5is1/5since multiplying this two(5 * 1/5 = 1) takes us to the ID element of 1. - Additive Inverse(
add inv). The same goes for here. It is the number when added to another number, pulls it back to theidentity elementwhich is0. Theadd invof5is-5since adding the two(5 + -5 = 0) also takes us to the ID element of0.
Up to this point, i think i have clarified things well and here is where we shall use mul inv to solve for division and add inv to solve subtraction using modular operations. Now lets see what the two judges of Mul Inv and Add Inv have to say.
5. Mul Inv & Add Inv(The Supreme Court Judgement)​
- (A)
Additive Inverse: As we said b4, this is the number that pulls back another number back to the ID element of zero. Have a look at this image here below of Mod 7 (remember that we start counting from 0 till 6 which is a total of 7).

You see that, if you stand on 2 and count 5(clock-wise) steps from 3 you end up to the ID element Zero(0) at the top. This means that 5 & 2 are additive inverses of each other.
Lets solve this from pure negative operation direction. Suppose you have 3 - 5 = -2. In this case, we cannot have -tive value(-2) in modular math. This is how you solve it
The rule for getting Add Inv is : Mod - number(the sub target), which is 7 - 5 = 2. Just add the add inv to the 3, and replace the -5 with 3 + 2 (mod 7) = 5 instead of 3 - 5 (mod 7) = -2 . So, the whole operations now becomes 3 - 5 (mod 7) = 5.
You have clearly seen that additive inverse under modular arithemtics has made possible to circumvent the possible negative number we could have otherwise got. The same goes for multiplication where we will avoid division decimals using multiplicative inverse.
- (B)
Multiplicative Inverse: The script is the same. This is the number when you multiply, can you land you back to ID element of 1. Supppose you want to do division of3/4. This means that you are multpliying3* by1/4, where1/4is0.25right? Now, how can we escape this decimal? This is how you pull it off though don't try this at home!!!
For a small number of Mod 7, lets just manually make trial & error method. You take the target division number(4), and you find which number when multiplied by it among all the Modular numbers we have, give us a result of ID element 1.
4 * 0 (mod 7) = 0
4 * 1 (mod 7) = 4
4 * 2 (mod 7) = 1 ----> This is exactly what we are looking for
4 * 3 (mod 7) = 5
4 * 4 (mod 7) = 2
4 * 5 (mod 7) = 6
4 * 6 (mod 7) = 3
Nb: You see that out of all the modular numbers, 2 is the only one that brings 4 back the ID element. For this reason, 2 is the Mul inv of 4.
Now, you take the initial 3 and multiply by the Mul inv of 4(which is 2) instead of 1/4. Final result becomes 3 * 2 = 6, meaning 3/4 mod 7 = 6.
Trial and error technique is not feasible for the kind of systems real ZK-proving systems use, a 256 bit-prime number(a number with 77 digits). There is mathematical shortcut way of finding this, aka, Extended Eucledian Algorithm(EEA) which uses a backward technique to work on Bezout's identity, but i don't want to go down that rabbit hole! You can research it on your own.
Nb: Do you see how we avoided the decimal of 1/4 and instead used the Mul inv of it to get totally weird different results? This is now the whole point why Modular arithmetics is used in ZK. It enables the use of pure multiplication and addition to workaround and avoid disastrous division and subtractions.
6. Congruency(≡) & Non-Congruency​(≢)​
- [A]. Congruency is just the mathematical way of saying that two numbers leave the same reminder of
rupon the same modulo. Informally, you can say it as two numbers land on the same spot on the modular circle. This is how to define it using mathy terms: [a ≡ b (mod n)]. This means that two integers a & b are congruent upon modulo n, if they give out the same reminder when divided by the n. The mathematical sign for congruency is three bars on top of each other (≡).
Let me give you three numbers that all leave the same reminder upon the same mod and thus are all congruent to each other. (7 mod 23) & (30 mod 23) & (53 mod 23). Here, they all leave out reminder of 7.
- First,
(7 / 23 = 0), so it just stays like that with a remainder of7. - Second,
(30 / 23 = 1)with a remainder of7. - Third,
(53 \ 23 = 2)with a remainder of7.
For this matter, all three land on (7) on a 23-hour clock(The Mod), so we can notate them like this to mean they are all congruent. 7 ≡ 30 ≡ 53 (mod 23).
- [B]. Non-Congruency: This is the exact opposite. Two numbers
aandbare non-congruent upon modulo n, if they leave out different remainders when divided byn. This means they land on different spots on the clock. We write this with a slashed triple-bar(≢).
We can check out on this two examples here. 8 (mod 23) and 30 (mod 23).
- 30 (mod 23) leaves a remainder of 7.
- 8 (mod 23) leaves a remainder of 8.
Since the two remainders are different, we can say they are in-congruent like this: 30 ≢ 8 (mod 23)
This marks the end of the second lesson of Modular Arithmetics which is crucial before getting to Prime Fields which comes next.
Click Next below to advance to Lesson 3.