Inverse (reciprocal number theory) [notes] Cryptography

Reciprocal number theory, also known as inverse

Modulo

For modulo, there are about some properties:

But the only division is not satisfied:

Why divide wrong? Well documented:

For some problems, we must seek in the middle of the process, more than, or too big a number, no less computer memory, the division appears that if this formula, we need to reverse the yuan.

Inverse

definition:

We know that if a * x = 1, then x is a reciprocal, x = 1 / a

The problems in number theory, most cases are modulo, so the question becomes:

Then x is not necessarily equal in value to our 1 / a on the conventional sense, we can understand the requirement to find a number between 0,1,2 ...... p-1, is multiplied by this number and a then modulo p, obtained as a result.

Now we must return to the previous question, and divided by a number equal to the number multiplied by the inverse of this, in the case of modulo division, is multiplied by the inverse of this number, namely:

This put a division, a multiplication fully converted.

Solving the inverse yuan

For solving the inverse element, if n is small, it is readily calculated, for example, the inverse element 26 in the mold 3:

But when n is very large, it is necessary to introduce an algorithm to calculate

(1) extended Euclidean algorithm (extend_gcd)

For the inverse of expression may be doing some rearrangements:

 

When gcd (a, n) = 1, substituting extend_gcd (a, b, x, y), the non-negative x value obtained is inverse of a modulo n.

Algorithm and prove

That is, we get a and gcd algorithm, gcd (m, n) = gcd (n, m% n) similar identity

 What does that mean? For example, it is

 If you want to be positive x

 Just do it one step further:

if (x < 0) {
    x += b; y -= a;
}

Extended:

Complete algorithm:

int extend_gcd(int a, int b, int& x, int& y) {
    if (b == 0) {
        x = 1, y = 0;
        return a;
    }
    int q = extend_gcd(b, a % b, x, y);
    int temp = x;
    x = y;
    y = temp - a / b * y;
    return q;
}

(2)费马小定理

如果p是一个质数,并且gcd(a,p)=1

两边同除以 a

所以

用快速幂求一下,复杂度O(logn)

Guess you like

Origin www.cnblogs.com/czc1999/p/11666250.html