# Returns a list storing [x, y, gcd(a,b)] where ax + by = gcd(a,b).
def EEA(a,b):

    # End of algorithm, 1*a + 0*b = a
    if b == 0:
        return [1,0,a]

    # Recursive case.
    else:

        # Next quotient and remainder.
        q = a//b
        r = a%b

        # Algorithm runs on b, r.
        rec = EEA(b,r)

        # Here is how we put the solution back together!
        return [rec[1], rec[0]-q*rec[1], rec[2]]

# Returns the modular inverse of x mod n. Returns 0 if there is no modular
# inverse.
def modInv(x,n):

    # Call the Extended Euclidean.
    arr = EEA(n, x)

    # Indicates that there is no solution.
    if arr[2] != 1:
        return 0

    # Do the wrap around, if necessary.
    if arr[1] < 0:
        arr[1] += n

    # This is the modular inverse.
    return arr[1]


p = 137
q = 17
h = 2
x = 3
k = 13
hashval = 12


g = (h**((p-1)//q))%p
print("g is",g)

y = (g**x)%p
print("y is",y)

r = (g**k)%p%q
print("sign r=",r)
s = modInv(k,q)
s *= (hashval + x*r)
s = s%q
print("sign s=",s)

w = modInv(s, q)
print("w is",w)
u1 = (hashval*w)%q
print("u1 is",u1)
u2 = (r*w)%q
print("u2 is",u2)
v = ((g**u1)*(y**u2))%p
v = v%q
print("verify is",v)
if v != r:
    print("oops")
