Closed pepijno closed 5 years ago
This is an integer library. Allowing negative exponents would require handling of non-integer values. For example, 2^(-3)
is not an integer.
I am only talking about modular arithmetic in which case a negative exponent does result in an integer, for example 2^(-3) mod 11=7
.
2^(-3) mod 11 = 8^(-1) mod 11 = 1/8 mod 11 Since 11*0 < 1/8 < 11*1, = 1/8
Modular exponentiation will always result in an element from the group (Z/nZ)*
, since 1/8
is not an element of (Z/nZ)*
we have that 2^(-3) mod n != 1/8 mod n
. (See also https://www.wolframalpha.com/input/?i=PowerMod[2,+-3,+11])
For reference on how to calculate the negative exponent modulo n
:
https://math.stackexchange.com/questions/1015713/how-to-deal-with-negative-exponents-in-modular-arithmetic
https://ipfs.io/ipfs/QmXoypizjW3WknFiJnKLwHCnL72vedxjQkDDP1mXWo6uco/wiki/Modular_exponentiation.html
When
b
is negative the equationa^b mod n
is the same asc^-b mod n
wherec
is the multiplicative inverse ofa
modulon
. It should be a relative small change to the code to also allow for negative exponents in themodPow
method.