3

For a toy program, I'd need an efficient way to compute

(2**64 * x) % m

where x and m are java longs and ** denotes exponentiation. This could be computed as

 BigInteger.valueOf(x).shiftLeft(64).mod(BigInteger.valueOf(m)).longValue()

or by repeatedly shifting x to the left and reducing, but both is pretty slow. It's no premature optimization.

Clarifications

‣ Any algorithm using BigInteger will probably be slower than the above expression.

‣ You can assume that m is too big for int, as otherwise

 long n = (1L<<32) % m; 
 return ((n*n) % m) * (x % m)) % m

would do.

‣ The slow shifting and reducing algorithm is something like

// assuming x >= 0
long shift32Left(long x, long m) {
    long result = x % m;
    for (int i=0; i<64; ++i) {
        x <<= 1;
        // here, `x<0` handles overflow
        if (x<0 || x>=m) {
            x -= m;
        }
    }
}
Community
  • 1
  • 1
maaartinus
  • 44,714
  • 32
  • 161
  • 320

1 Answers1

2

General form: (a1 * a2 * a3 ... * an) % m = [(a1 % m) * (a2 % m) * ... * (a3 % m) ] % m

Apply above formula, we have:

(2^64 * x) % m = (((2^64) % m) * (x % m)) % m

For the first part: 2^64 mod m. I can make more general case: 2^t mod m. I have this pseudocode. In will run in N(log t) times. This pseudocode just for t and m are normal integer. Base on range of t and m, you can fix inside function calculation to use BigInteger at suitable point.

long solve(long t, long m) {
   if (t == 0) return 1 % m;
   if (t == 1) return t % m;
   long res = solve(t/2, m);
   res = (res * res) % m;
   if (t % 2 == 1) res = (res * 2) % m;
   return res;
}

Thanks for OldCurmudgeon. Above code can be one simple line:

BigInteger res = (new BigInteger("2")).
   modPow(new BigInteger("64"), new BigInteger("" + m));

Here is the implementation of modPow. This implementation uses different approach. Algorithm starts from m: will break m in to m = 2^k*q. Then will find modulo of 2^k and q then use Chinese Reminder theorem combines result.

 public BigInteger modPow(BigInteger exponent, BigInteger m) {
        if (m.signum <= 0)
            throw new ArithmeticException("BigInteger: modulus not positive");

        // Trivial cases
        if (exponent.signum == 0)
            return (m.equals(ONE) ? ZERO : ONE);

        if (this.equals(ONE))
            return (m.equals(ONE) ? ZERO : ONE);

        if (this.equals(ZERO) && exponent.signum >= 0)
            return ZERO;

        if (this.equals(negConst[1]) && (!exponent.testBit(0)))
            return (m.equals(ONE) ? ZERO : ONE);

        boolean invertResult;
        if ((invertResult = (exponent.signum < 0)))
            exponent = exponent.negate();

        BigInteger base = (this.signum < 0 || this.compareTo(m) >= 0
                           ? this.mod(m) : this);
        BigInteger result;
        if (m.testBit(0)) { // odd modulus
            result = base.oddModPow(exponent, m);
        } else {
            /*
             * Even modulus.  Tear it into an "odd part" (m1) and power of two
             * (m2), exponentiate mod m1, manually exponentiate mod m2, and
             * use Chinese Remainder Theorem to combine results.
             */

            // Tear m apart into odd part (m1) and power of 2 (m2)
            int p = m.getLowestSetBit();   // Max pow of 2 that divides m

            BigInteger m1 = m.shiftRight(p);  // m/2**p
            BigInteger m2 = ONE.shiftLeft(p); // 2**p

            // Calculate new base from m1
            BigInteger base2 = (this.signum < 0 || this.compareTo(m1) >= 0
                                ? this.mod(m1) : this);

            // Caculate (base ** exponent) mod m1.
            BigInteger a1 = (m1.equals(ONE) ? ZERO :
                             base2.oddModPow(exponent, m1));

            // Calculate (this ** exponent) mod m2
            BigInteger a2 = base.modPow2(exponent, p);

            // Combine results using Chinese Remainder Theorem
            BigInteger y1 = m2.modInverse(m1);
            BigInteger y2 = m1.modInverse(m2);

            if (m.mag.length < MAX_MAG_LENGTH / 2) {
                result = a1.multiply(m2).multiply(y1).add(a2.multiply(m1).multiply(y2)).mod(m);
            } else {
                MutableBigInteger t1 = new MutableBigInteger();
                new MutableBigInteger(a1.multiply(m2)).multiply(new MutableBigInteger(y1), t1);
                MutableBigInteger t2 = new MutableBigInteger();
                new MutableBigInteger(a2.multiply(m1)).multiply(new MutableBigInteger(y2), t2);
                t1.add(t2);
                MutableBigInteger q = new MutableBigInteger();
                result = t1.divide(new MutableBigInteger(m), q).toBigInteger();
            }
        }

        return (invertResult ? result.modInverse(m) : result);
    }

For the second part: you can easily use BigInteger or simply normal calculation, depend on range of x and m.

hqt
  • 29,632
  • 51
  • 171
  • 250
  • `BigInteger` has [modPow](http://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#modPow%28java.math.BigInteger,%20java.math.BigInteger%29) that could handle the `2^64%m`. – OldCurmudgeon Jul 01 '15 at 10:28
  • @OldCurmudgeon I edited my answer. It seems that modPow implementation is faster, because java implementation includes my algorithm (break exponent/2) and with some modifications ... – hqt Jul 01 '15 at 10:46
  • @hqt I'm afraid, you missed my `BigInteger` approach (which was slower than I'd need). – maaartinus Jul 01 '15 at 10:57