Assert :: (modulus - 1) * (modulus - 1) does not overflow base
我将其解释为:
assert (modulus - 1) * (modulus - 1) < base
我认为这是不正确的,所以溢出实际上是什么意思?
这是python中pow(x,y,z)函数的伪代码。这是完整的伪代码:
function modular_pow(base, exponent, modulus) is
if modulus = 1 then
return 0
Assert :: (modulus - 1) * (modulus - 1) does not overflow base
result := 1
base := base mod modulus
while exponent > 0 do
if (exponent mod 2 == 1) then
result := (result * base) mod modulus
exponent := exponent >> 1
base := (base * base) mod modulus
return result
https://en.wikipedia.org/wiki/Modular_exponentiation
我有一个具有这样的断言的伪代码:声明::(模数-1)*(模数-1)不会溢出基数,我将其解释为:断言(模数-1)*(模数-1)
assert (modulus - 1) * (modulus - 1) <= ((1 << 31) - 1)