The best way to speed up the baby-step giant-step algorithm is to use an efficient table lookup scheme. The best in this case is a
hash table. The hashing is done on the second component, and to perform the check in step 1 of the main loop, γ is hashed and the resulting memory address checked. Since hash tables can retrieve and add elements in
O(1) time (constant time), this does not slow down the overall baby-step giant-step algorithm. The space complexity of the algorithm is O(\sqrt n), while the time complexity of the algorithm is O(\sqrt n). This running time is better than the O(n) running time of the naive brute force calculation. The baby-step giant-step algorithm could be used by an eavesdropper to derive the private key generated in the
Diffie Hellman key exchange, when the modulus is a prime number that is not too large. If the modulus is not prime, the
Pohlig–Hellman algorithm has a smaller algorithmic complexity, and potentially solves the same problem. == Notes ==