Discrete Log Algorithms :Baby-step giant-step 【二】

import gmpy2
def discreteLog(g,p,a):              #离散对数,求 g^x=a mod p中的x
    table={}
    sq=gmpy2.isqrt(p-1)
    m=gmpy2.add(sq,1)       #向上取整
    for i in range(m):
        k=-i*m
        y=gmpy2.powmod(g,k,p)
        mod=((a%p)*y)%p
        table.update({mod:i})
    j=0
    while True:
        result=gmpy2.powmod(g,j,p)
        if result in table.keys():
            sa=m*table[result]+j
            return sa
            #return table[result],m      #将对应的下标返回
        else:
            j=j+1
        
        
p=93450983094850938450983409623
a=45416776270485369791375944998 

x=discreteLog(-2,p,a)
print(x)        

小的可以,大的数就报错“Memory error”了

原文地址:https://www.cnblogs.com/Guhongying/p/10153395.html