-
Notifications
You must be signed in to change notification settings - Fork 47
Description
Describe the bug
The ModularBigInteger.pow(exponent: ModularBigInteger) function can be abused with an invalid modulus and might give false results!
More specifically, ModularBigInteger(residue=a,modulus=n).pow(ModularBigInteger(residue=b,modulus=m)) can only give the right result if m equals the order a modulo n. If you for example want to compute
Solution: Computing the order (or Euler phi function, prime factors,...) during run-time is a bad idea, but
my suggestion: Add annotations in the source code to inform the user that this must be provided. When programmers use this function, they should know what they are doing. Otherwise, they should not use it.
import com.ionspin.kotlin.bignum.integer.BigInteger
import com.ionspin.kotlin.bignum.modular.ModularBigInteger
val a = BigInteger(2)
val n = BigInteger(7)
val b = BigInteger(3)
val m = BigInteger(2)
val a_mod_n = ModularBigInteger.creatorForModulo(n).fromBigInteger(a)
val b_mod_m = ModularBigInteger.creatorForModulo(m).fromBigInteger(b)
println("correct:"+(a_mod_n.pow(b))) // correct
println("wrong :"+(a_mod_n.pow(b_mod_m))) // wrong