Skip to content

bug: ModularBigInteger.pow does work not with large Int/Long exponent and negative BigInt exponent #334

@manfredscheucher

Description

@manfredscheucher

Describe the bug
The functions ModularBigInteger.pow(exponent: Int) and ModularBigInteger.pow(exponent: Long) fail to compute large powers as it "computes residue.pow(exponent) % modulus" - for example when compute a^k mod n with a=2, k=(2^60), n=10, it actually first computes 2^(2^60), and this is too expensive for large k. See source code. The issue can be fixed by calling pow(BigInteger(exponent)).

Note that ModularBigInteger.pow(exponent: BigInteger) and BigInteger.pow(exponent: Int) and BigInteger.pow(exponent: Long) compute powers correctly.

To Reproduce

import com.ionspin.kotlin.bignum.integer.BigInteger
import com.ionspin.kotlin.bignum.modular.ModularBigInteger
import io.kotest.core.spec.style.StringSpec

class Test2 : StringSpec({
    "test ionspin" {
        val n = BigInteger(111)
        val a = BigInteger(2)
        val a_mod_n = ModularBigInteger.creatorForModulo(n).fromBigInteger(a)

        println("start computing b")
        val b_mod_n = a_mod_n.pow(BigInteger(1L shl 60)) // fine
        println("b:"+b_mod_n.toString())

        println("start computing c")
        val c_mod_n = a_mod_n.pow(1L shl 60) // bug
        println("c:"+c_mod_n.toString())
    }
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions