'Writing modular inverse into code in python

I am working with Shank's algorithm for context, and with large numbers so have a code that computes fast modular exponentiation:

def fmod(a,e,n):
    s = 1; b = a;
    while True:
        if (e%2==1):
            s=s*b % n;
        e = e // 2
        if e==0:
            return s
        b=b*b %n;

Now I want to use this to produce an array with the the iterated values of hg^(-in) modp where h = 22070, g = 19, p = 45161, n = 34. I know it works as I have used it to produce an array of solutions to g^i modp

My issue is I am struggling with what the inputs need to be when it is an inverse modulo, I honestly don't know whether this is mathematical or coding knowledge to create the input but I want to be able to have something like this (my code for g^i)

x = [fmod(g,i,p)for i in range(n)]

My only thought was something like this:

y = [fmod(g,(-i*n),p)for i in range(n)]
y = [(i*h)%p for i in y]

But from the solutions in the array I can see it is being computed wrong I think the fault lies with the -i as python can't see this means an inverse modulo.

Any help would be really appreciated



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source