The task is to find the generator of the finite field. There are multiple ways to do this: Naive implementation (brute-force). Credits to Landryl @ Cryptohack. ''' Rather than using a set and checking if every element of Fp has been generated, we can also rapidly disregard a number from being a generator by checking if the cycle it generates is smaller in size than p. If we detect a cycle before p elements, k can't be a generator of Fp.