# Modular arithmetic for encryption

5 views (last 30 days)
Fatma Alnabrisi on 1 Dec 2019
Commented: Fatma Alnabrisi on 2 Dec 2019
I'm trying to implement an encryption technique using Matlab for a college project.
However I'm facing some difficulties in modular calculation.
For example I have matrix A = [ 1 , 0 ; 1/2 , 1 ] , and I need to calculate A mod 29.
what I'm getting in matlab is [ 1 , 0 ; 1/2 ,1 ],
while what I'm supposed to get is [ 1 , 0 ; 15 , 1 ] as in https://planetcalc.com/8326/
Actually I don't understand the math behined the fact that (1/2 mod 29) = 15 .
So, if any one can help me in getting this result in Matlab, I would be grateful.

#### 1 Comment

Fatma Alnabrisi on 2 Dec 2019
My input is real number matrix A and I need to get (A mod 29)
The output should be integer matrix.
How to code this function in Matlab ??

Bruno Luong on 1 Dec 2019
Edited: Bruno Luong on 1 Dec 2019
"1/2" is integer (let us call it "a") so that
a*2 = 1 mod 29
This can be obtained by GCD function
>> [~,a]=gcd(2,29)
a =
-14
>> a = mod(a,29)
a =
15

Fatma Alnabrisi on 1 Dec 2019
But actually I didn't get it 100% ..
what if I need to calculate 3/2 mod 29 .. I'm getting wrong answer !
>> b = 3/2
b =
1.5000
>> [~,b]=gcd(2,29)
b =
-14
>> b = mod (b,29)
b =
15
Bruno Luong on 1 Dec 2019
You should wonder where 3 is used in your code (no where).
Here is the result
b = 3/2 = 3*(1/2) = 3*a = 3*15 = 45 = 16 mod 29
Fatma Alnabrisi on 1 Dec 2019
well, based on what you've mentioned can you please help me in writing a code which takes any matrix A as an input, and finds A mod 29 (considering that the elements of A are real numbers and the outputs have to be integers.)
Thank you