/* get 8*MODBYTES size random number */ public static BIG Random(RAND rng) { BIG m = new BIG(0); int i, b, j = 0, r = 0; /* generate random BIG */ for (i = 0; i < 8 * MODBYTES; i++) { if (j == 0) { r = rng.Byte; } else { r >>= 1; } b = r & 1; m.Shl(1); m.w[0] += b; // m.inc(b); j++; j &= 7; } return(m); }
/* a=1/a mod 2^256. This is very fast! */ public virtual void InvMod2m() { int i; BIG U = new BIG(0); BIG b = new BIG(0); BIG c = new BIG(0); U.Inc(InvMod256(LastBits(8))); for (i = 8; i < BIGBITS; i <<= 1) { U.Norm(); b.Copy(this); b.Mod2m(i); BIG t1 = SMul(U, b); t1.Shr(i); c.Copy(this); c.Shr(i); c.Mod2m(i); BIG t2 = SMul(U, c); t2.Mod2m(i); t1.Add(t2); t1.Norm(); b = SMul(t1, U); t1.Copy(b); t1.Mod2m(i); t2.One(); t2.Shl(i); t1.RSub(t2); t1.Norm(); t1.Shl(i); U.Add(t1); } U.Mod2m(BIGBITS); Copy(U); Norm(); }