Example #1
0
        /* 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();
        }