Matlab realizes the experiment of solving multiplicative inverse elements

1. Purpose of experiment

Familiarize yourself with the algorithm for finding the multiplicative inverse. On this basis, choose an algorithm, and design and implement a computing multiplication inverse element operator by using high-level programming language.

2. Experimental principle

【Definition】For integers a and m, if there is an integer b that satisfies ab mod m =1 , then b is called the multiplicative inverse of a modulo m, and is recorded as a-1 .

[Theorem] The necessary and sufficient condition for a to have a multiplicative inverse modulo m is that the greatest common divisor of a and m is 1, denoted as gcd(a,m)=1 .

It can be seen from the theorem that when a and m are mutually prime, the multiplicative inverse of a modulo m has a unique solution. If a and m are not mutually prime, then the multiplicative inverse element a of a modulo m does not exist. Obviously, if a is a prime number, any integer from 1 to a-1 is relatively prime to a, that is, any integer from 1 to a-1 has a multiplicative inverse modulo m.

The Extended Euclid algorithm is a common method for finding multiplicative inverses. Euclid (Euclid) algorithm is used to calculate the greatest common divisor of two integers. Its principle is to repeatedly use division with remainder until the remainder is 0. The greatest common divisor of two integers can be expressed by the linear combination of integer coefficients of these two integers . The extended Euclidean algorithm is to calculate the coefficients in the linear combination of integers, that is, to calculate gcd (a, m)- sNa+lNm integer sN

lN. In addition to being used to find the greatest common divisor of two integers, the extended Euclidean algorithm can also be used to find the multiplicative inverse a-1.

3. Experimental steps

The Matlab implementation is as follows:

u = 1;%%u=1,g=a,v1=0,v3=m
a=input("输入a:\n");
g = a;
v1 = 0;
m=input("输入m:\n");
v3 = m;
q = int16(g / v3);%%计算q=g/v3,t3=g%v3
t3 = mod(g, v3);
if(t3 == 0 )%%如果t3等于0则直接执行以下判断
    g = v3;
    if(g == 1)
        if(v1 > 0)
            fprintf(a+"mod"+m+"的乘法逆元为fir:"+v1);
        else
            fprintf(a+"mod"+m+"的乘法逆元为sed:"+(m+v1));
        end
    else
        fprintf(a+"mod"+m+"的乘法逆元不存在");
    end
else
    while (true)%%如果t3不等于0,则进行循环计算直至t3等于0
        t1 = rem(u -( q * v1),m);%%计算t1=(u-q*v1)modm,u=v1,g=v3,v1=t1,v3=t3
        u = v1;
        g = v3;
        v1 = t1;
        v3 = t3;
        q =floor( g / v3);
        t3 = rem(g , v3);
        if(t3 == 0 )
            g = v3;
            if(g == 1)%%如果g=1,
                if(v1 > 0)%%当v1>0时返回v1,否则返回m+v1
                    fprintf(a+"mod"+m+"的乘法逆元为th:"+v1+"\n");
                    break;
                else
                    fprintf(a+"mod"+m+"的乘法逆元为fo:"+(m+v1)+"\n");
                    break;
                end
            else%%g不等于1时返回乘法逆元不存在
                fprintf(a+"mod"+m+"的乘法逆元不存在\n");
                break;
            end
        end
    end
end

operation result

4. Experimental summary

The problem of solving the multiplicative inverse has full application in modern cryptography, especially the encryption and decryption process of the modern public key cryptosystem (for example, RSA algorithm) usually involves the problem of solving the multiplicative inverse. Therefore, mastering the algorithm for solving multiplicative inverse elements has important practical significance for learning modern cryptography.

Guess you like

Origin blog.csdn.net/weixin_55988897/article/details/127885777