Introduction to Mod and Multiplicative Inverse
In mathematics, the modulo operation finds the remainder after division of one number by another (sometimes called modulus).
Here are the rules:
For
1. a < b then a mod b = a
2. a = b then a mod b = 0
3. a > b then a/b = r where r is the remainder of a/b
The multiplicative inverse is simply :
This x is the multiplicative inverse of 15.
So let say we want to find the value of x of the following equation
The is the multiplicative inverse of and it is equivalent to
Our goal is to find the value of s.t the value of yields the same remainder as .
Therefore, we can say that the value of is because and with a remainder of 1 and it is equivalent to .
Overview of Felt252 type in Cairo
Felt252 type is a special data type that exists in the Cairo language by Starknet. As from the name, it occupies 252 bits and is used to represent a number in the range of
#1
where P is a large prime number equal to
In the case of an overflow issue, it uses to bring the value back to within the range.
Division Using Felt252
In Cairo, every division with not nice numbers assumes that it follows the following equation:
#2
Not nice numbers = , …
With nice numbers, the division operates normally.
Nice numbers = , …
As an example. equation #2 is expanded for the operation accordingly to the way that the division is implemented in Cairo.
#3 under
Cairo implements its division over a finite field and the finite field is defined in #1.
Therefore, every non-zero element in the finite field has a multiplicative inverse.
If we reduce equation #3, we get:
Therefore, if we summarize
is
under which simplifies to
The is the multiplicative inverse of and in implies that in case of an overflow, it uses to bring the value back to within the range.
And the implies that and from will yield the same remainder when divided by .
Therefore in Cairo will be same as
This type of division carries benefits when it comes to providing computation for cryptographic proofs.
So to summarize everything, if quotient is an integer, Cairo will output the result like a normal division if not, the computation will align with equation #2.
Original documentation can be found here.