Beispiel #1
0
 public BigInteger convert(BigInteger x)
 {
     if (x.s < 0 || x.compareTo(this.m) >= 0)
     {
         return x.mod(this.m);
     }
     else
     {
         return x;
     }
 }
Beispiel #2
0
 // xR mod m
 public BigInteger convert(BigInteger x)
 {
     if (x == null)
     {
         return null;
     }
     BigInteger r = new BigInteger();
     x.abs().dlShiftTo(this.m.t, r);
     r.divRemTo(this.m, null, r);
     if (x.s < 0 && r.compareTo(BigInteger.ZERO) > 0)
     {
         this.m.subTo(r, r);
     }
     return r;
 }
Beispiel #3
0
 // 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);
     }
 }
Beispiel #4
0
 // (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;
 }