我想使用蒙哥马利乘法来解决模块化产品。
类似的东西:
a * a mod m
例如 一个= 3567730065637 m = 5524112124451
a * a mod m -> 12728697821250192328215769 % 5524112124451 = 822276999119
如果我执行蒙哥马利减法,则会得到错误的结果(3692678398982)。我该怎么做才能获得正确的结果?
N = 5524112124451
A = 3567730065637
B = 3567730065637
n = N.bit_length()
R = 0
for i in range(0, n):
q = int(R + (A & (1 << i) != 0) * B) % 2
R = int((R + (A & (1 << i) != 0) * B + q * N) / 2)
print("Result:", R % N)