Пример #1
0
        /* return this^e mod Modulus
         *      public FP pow(BIG e)
         *      {
         *              int bt;
         *              FP r=new FP(1);
         *              e.norm();
         *              x.norm();
         *              FP m=new FP(this);
         *              while (true)
         *              {
         *                      bt=e.parity();
         *                      e.fshr(1);
         *                      if (bt==1) r.mul(m);
         *                      if (e.iszilch()) break;
         *                      m.sqr();
         *              }
         *              r.x.mod(p);
         *              return r;
         *      } */

        /* return sqrt(this) mod Modulus */
        public FP Sqrt()
        {
            Reduce();
            BIG b = new BIG(ROM.Modulus);

            if (MOD8 == 5)
            {
                b.Dec(5);
                b.Norm();
                b.Shr(3);
                FP i = new FP(this);
                i.x.Shl(1);
                FP v = i.Pow(b);
                i.Mul(v);
                i.Mul(v);
                i.x.Dec(1);
                FP r = new FP(this);
                r.Mul(v);
                r.Mul(i);
                r.Reduce();
                return(r);
            }
            else
            {
                b.Inc(1);
                b.Norm();
                b.Shr(2);
                return(Pow(b));
            }
        }
Пример #2
0
        /* return this/c */
        public virtual BIG Div(BIG c)
        {
            int  d, k = 0;
            DBIG m  = new DBIG(c);
            DBIG dr = new DBIG(0);
            BIG  r  = new BIG(0);
            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);

                dr.Copy(this);
                dr.Sub(m);
                dr.Norm();
                d = (int)(1 - ((dr.w[BIG.DNLEN - 1] >> (BIG.CHUNK - 1)) & 1));
                CMove(dr, d);
                r.Copy(a);
                r.Add(e);
                r.Norm();
                a.CMove(r, d);
                k--;
            }
            return(a);
        }