BSGS algorithm abbreviated

Introduction

BSGS algorithm for solving the discrete logarithm problem. specific:

On demand \ (X \) equation \ (a ^ x \ equiv b \ pmod c \) is the smallest non-negative integer solution. Wherein \ (C \) is a prime number.

Obviously violence enumeration time out, we need a more efficient algorithm.

BSGS Algorithm

Algorithmic process:

  • Order \ (m = \ lceil \ sqrt {C} \ rceil \) , the required \ (X \) is represented as \ (im - j \) form.

  • Then the original equation can be turned into \ (IM A ^ {- J} \ equiv B \ PMOD C \) . Transposition to give \ (a ^ {im} \ equiv a ^ jb \ pmod c \)

  • Enumerate all \ (J \ in [0, m] \) , obtained in advance \ (A JB ^ \) . For fast search, we refer to these \ (a ^ jb \) is inserted into a hash table.

  • Enumerate all \ (I \ in (0, m] \) , to find a maximum \ (J \) , satisfies \ (IM A ^ {} \ equiv A ^ JB \ PMOD C \) . If found then the answer is \ (IM - J \) .

  • If the last is not found, no solution.

Fake code:

clear hash_table;
make m = ceil(sqrt(c));
for each j in [0,m] do:
	push (b*a^j) into hash_table;
for each i in (0,m] do: {
	find the largest j that satisfy [a^(i*m) ≡ b*a^j (mod c)];
	if j is exist then:
		print (i*m-j) and quit.
}

Guess you like

Origin www.cnblogs.com/-Wallace-/p/12604105.html