/* reduces this DBIG mod a DBIG in place */ /* public void mod(DBIG m) * { * int k=0; * if (comp(this,m)<0) return; * * do * { * m.shl(1); * k++; * } * while (comp(this,m)>=0); * * while (k>0) * { * m.shr(1); * if (comp(this,m)>=0) * { * sub(m); * norm(); * } * k--; * } * return; * * }*/ /* return this/c */ public virtual BIG div(BIG c) { int k = 0; DBIG m = new DBIG(c); BIG a = new BIG(0); BIG e = new BIG(1); norm(); while (comp(this, m) >= 0) { e.fshl(1); m.shl(1); k++; } while (k > 0) { m.shr(1); e.shr(1); if (comp(this, m) > 0) { a.add(e); a.norm(); sub(m); norm(); } k--; } return(a); }
/* Create random BIG in portable way, one bit at a time */ public static BIG randomnum(BIG q, RAND rng) { DBIG d = new DBIG(0); int i, b, j = 0, r = 0; for (i = 0; i < 2 * ROM.MODBITS; i++) { if (j == 0) { r = rng.Byte; } else { r >>= 1; } b = r & 1; d.shl(1); d.w[0] += b; // m.inc(b); j++; j &= 7; } BIG m = d.mod(q); return(m); }
/* reduces this DBIG mod a BIG, and returns the BIG */ public virtual BIG mod(BIG c) { int k = 0; norm(); DBIG m = new DBIG(c); if (comp(this, m) < 0) { return(new BIG(this)); } do { m.shl(1); k++; } while (comp(this, m) >= 0); while (k > 0) { m.shr(1); if (comp(this, m) >= 0) { sub(m); norm(); } k--; } return(new BIG(this)); }
/* convert to Montgomery n-residue form */ public void nres() { if (ROM.MODTYPE != ROM.PSEUDO_MERSENNE) { DBIG d = new DBIG(x); d.shl(ROM.NLEN * ROM.BASEBITS); x.copy(d.mod(p)); } }