// x = x/R mod m (HAC 14.32) public void reduce(BigInteger x) { if (x == null) { return; } while (x.t <= this.mt2) { // pad x so am has enough room later x.SetData(x.t++, 0); } for (int i = 0; i < this.m.t; ++i) { // faster way of calculating u0 = x[i]*mp mod DV int j = x.datas[i] & 0x7fff; int u0 = (j * this.mpl + (((j * this.mph + (x.datas[i] >> 15) * this.mpl) & this.um) << 15)) & x.DM; // use am to combine the multiply-shift-add into one call j = i + this.m.t; x.datas[j] += this.m.am(0, u0, x.datas, i, 0, this.m.t); // propagate carry while (x.datas[j] >= x.DV) { x.datas[j] -= x.DV; x.datas[++j]++; } } x.clamp(); x.drShiftTo(this.m.t, x); if (x.compareTo(this.m) >= 0) { x.subTo(this.m, x); } }
// (public) this mod a public BigInteger mod(BigInteger a) { BigInteger r = new BigInteger(); this.abs().divRemTo(a, null, r); if (this.s < 0 && r.compareTo(ZERO) > 0) { a.subTo(r, r); } return r; }