离散日志问题的蛮力攻击(Python)

Brute force attack for discrete log problem (Python)

我仍在学习 Python 并尝试在 Python 中使用 for 循环进行暴力攻击。我做了这个 for 循环来尝试从 0 到 999999 的每个数字并将其存储在变量 n 中。我不想让我的 for look 运行 永远,而是希望它在特定的 scalarmult(n,P) 值处停止。例如,假设我希望 for 循环在 print(scalarmult(n,P)) 打印时结束,(111111,2334897).

p = 1000003
class Fp:
    def __init__(self,x):
        self.int = x % p
    def __str__(self):
        return str(self.int)
    __repr__ = __str__
Fp.__eq__ = \
    lambda a,b: a.int == b.int
Fp.__add__ = \
    lambda a,b: Fp(a.int + b.int)
Fp.__sub__ = \
    lambda a,b: Fp(a.int - b.int)
Fp.__mul__ = \
    lambda a,b: Fp(a.int * b.int)

def clockadd(P1, P2):
    x1,y1 = P1
    x2,y2 = P2
    x3 = x1*y2+y1*x2
    y3 = y1*y2-x1*x2
    return x3,y3

P = (Fp(1000),Fp(2))

def scalarmult(n,P):
    if n == 0: return (Fp(0), Fp(1))
    if n == 1: return P
    Q = scalarmult(n//2,P)
    Q = clockadd(Q,Q)
    if n % 2: Q=clockadd(P,Q)
    return Q
#(947472, 736284)
for n in range(0,10): 
    i = scalarmult(n,P)
    print(n, i) #i.e. 6 (780000, 1351)

    if n == 780000 and P == 1351: #should stop the for loop at iteration 7 which is 6 (780000, 1351)
        break

在循环内添加一个 if 语句,在 print

之后
if n == 111111 and p == 2334897:
    break