Knowledge overview
Well, I admit that this is my third day winter vacation on the knowledge, but I am now a tall (winter vacation, but I have been selected in the province ...) )
Well, this is an algorithm for finding discrete-mode logarithm
The smallest x (where P is prime) that satisfies the equation a^x≡b (mod p)
Consider a block of x? , according to Euler's theorem, just check if x=0,1,2...p-1 is a solution, because a^ (p-1) ≡1 (mod p) starts looping when x exceeds p-1.
Suppose the size of the block is M
First check the first M, a^0,a^1,a^2...a^ (M-1) whether to meet the requirements, the AI mod p exists in the hash table EI, find out a^m inverse a^ (-m)
Then consider the following m-term, a^m,a^ (m+1), a^ (m+2),..., a^ (2m-1), if there is a solution, it is equivalent to the existence of I make ei*a^m≡b (mod n), both sides of the left multiply a^ (-m) to Ei≡b ' (mod n) (b ' =a^ (-m) *b (mod p) ). This allows you to check for the presence of Ei=b '
Template
Make a hash table.
Template 1 is not tested. 0
//Orz POPOQQQ#include <math.h>#defineDMin (a) < (b) ( A):(B))intP//P is a prime.structpep{intFirst,second; Pep (int_=0,int__=0): First (_), second (__) {}BOOL operator< (ConstPep &other)Const { returnFirst <Other.first; }}namespacehash_table{#defineINF ~0u>>1#defineMAXN 10010structlinker{intHash,val; Linker*Next; Linker (int_,linker *__): Hash (_), Val (INF), next (__) {}}*FIR[MAXN]; Inlineint&hash (intx) { intpos=x%MAXN; for(Linker *iter=fir[pos];iter;iter=iter->next)if(iter->hash==x)returnIter->Val; return(fir[pos]=NewLinker (X,fir[pos])Val; }}inline Pep EXGCD (intAintb) { if(!B)returnPEP (1,0); Pep Temp=EXGCD (b,a%b); returnPep (temp.second,temp.first-x/y*temp.second);} InlineintBaby_step_giant_step (intAintB) { intI,m=ceil (sqrt (p)), temp=1, d=1; for(i=0; i<=m;i++, (Temp*=a)%=p) { int&val=Hash_table::hash (temp); Val=DMin (val,i); D=temp; } for(temp=1, i=0; i<=m;i++, (Temp*=d)%=p) { intX= ((EXGCD (temp,p) first%p) +p)%p; int&val=hash_table::hash (x*b%p); if(Val!=inf)returni*m+Val; } return-1;}
The theory of the number of Baby-step-giant-step (II.) the discrete modulus logarithm of the big step