// (public) this^e % m, 0 <= e < 2^32 public BigInteger modPowInt(int e, BigInteger m) { BigInteger r = new BigInteger(); if (e < 256 || m.isEven()) { Classic z = new Classic(m); r = this.exp(e, z); } else { Montgomery z = new Montgomery(m); r = this.exp(e, z); } return r; }
// (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79) public BigInteger exp(int e, Montgomery z) { if (e > 0x7fffffff || e < 1) { return ONE; } BigInteger r = new BigInteger(), r2 = new BigInteger(); BigInteger g = z.convert(this); int i = nbits(e) - 1; g.copyTo(r); while (--i >= 0) { z.sqrTo(r, r2); if ((e & (1 << i)) > 0) { z.mulTo(r2, g, r); } else { BigInteger t = r; r = r2; r2 = t; } } return z.revert(r); }