/* Convert to Hex String */ public override string ToString() { BIG b; string s = ""; int len = NBits(); if (len % 4 == 0) { len /= 4; } else { len /= 4; len++; } if (len < MODBYTES * 2) { len = MODBYTES * 2; } for (int i = len - 1; i >= 0; i--) { b = new BIG(this); b.Shr(i * 4); s += (b.w[0] & 15).ToString("x"); } return(s); }
/* Jacobi Symbol (this/p). Returns 0, 1 or -1 */ public virtual int Jacobi(BIG p) { int n8, k, m = 0; BIG t = new BIG(0); BIG x = new BIG(0); BIG n = new BIG(0); BIG zilch = new BIG(0); BIG one = new BIG(1); if (p.Parity() == 0 || Comp(this, zilch) == 0 || Comp(p, one) <= 0) { return(0); } Norm(); x.Copy(this); n.Copy(p); x.Mod(p); while (Comp(n, one) > 0) { if (Comp(x, zilch) == 0) { return(0); } n8 = n.LastBits(3); k = 0; while (x.Parity() == 0) { k++; x.Shr(1); } if (k % 2 == 1) { m += (n8 * n8 - 1) / 8; } m += (n8 - 1) * (x.LastBits(2) - 1) / 4; t.Copy(n); t.Mod(x); n.Copy(x); x.Copy(t); m %= 2; } if (m == 0) { return(1); } else { return(-1); } }
/* 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(); }