我想自己计算RSA算法。我需要计算一个数的某一幂的模数。问题是,在一定的功率下,这个数字可能会变得相当大。
这是我想要的:
x = pow(n, p) % q如何有效地确定x?
发布于 2011-03-25 22:50:26
如果您使用的是.NET 4,我建议您查看BigInteger,它甚至提供了在单个操作中完成所有操作的ModPow方法:)
BigInteger n = ...;
BigInteger p = ...;
BigInteger q = ...;
BigInteger x = BigInteger.ModPow(n, p, q);发布于 2011-03-25 22:54:20
这就是所谓的powermod function
function modular_pow(base, exponent, modulus)
c := 1
for e_prime = 1 to exponent
c := (c * base) mod modulus
return c这可以通过平方应用指数来提高效率:
function modular_pow(base, exponent, modulus)
result := 1
while exponent > 0
if (exponent & 1) equals 1:
result = (result * base) mod modulus
exponent := exponent >> 1
base = (base * base) mod modulus
return result发布于 2011-03-25 22:52:14
请查看此topic和此article,了解如何提高数学函数本身的效率。
https://stackoverflow.com/questions/5433992
复制相似问题