Baby-step, giant-step
Now p is 13 digits long. A simple brute force search requires about 10¹² steps — this is several days in Python. But there is a universal algorithm that solves the discrete logarithm much faster.
Baby-step giant-step (BSGS) — reduces time at the expense of memory. The idea is simple: let m = ⌈√n⌉ and write the unknown x in the form x = i·m + j, where 0 ≤ i, j < m. Then:
g^(i*m + j) = h (mod p)
g^j = h * (g^(-m))^i (mod p)
The algorithm consists of two stages. Baby steps: all values of g^j are written to a dictionary (dict) — m entries. Giant steps: h · (g^(-m))^i is calculated sequentially, and each time it is searched for in the dictionary. When a match is found, x = i·m + j. In total ~2√n steps, which means a few million in this challenge — a few seconds.
Intercepted session:
p = 1379880546293
g = 2
A = 666436569524
B = 989835935547
Find the shared secret s (you must first recover a or b).
Flag: BAYROQ{DH_bsgs_sir_<s>}. The dictionary will consist of ~10⁶ entries — this is normal.