Beispiel #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));
            }
        }
Beispiel #2
0
        /* test this=0? */
        public bool IsZilch()
        {
            FP z = new FP(this);

            z.Reduce();
            return(z.x.IsZilch());
        }
Beispiel #3
0
        /* return TRUE if this==a */
        public bool Equals(FP a)
        {
            FP f = new FP(this);
            FP s = new FP(a);

            f.Reduce();
            s.Reduce();
            if (BIG.Comp(f.x, s.x) == 0)
            {
                return(true);
            }
            return(false);
        }
Beispiel #4
0
        public FP Pow(BIG e)
        {
            sbyte[] w  = new sbyte[1 + (BIG.NLEN * BIG.BASEBITS + 3) / 4];
            FP[]    tb = new FP[16];
            BIG     t  = new BIG(e);

            t.Norm();
            int nb = 1 + (t.NBits() + 3) / 4;

            for (int i = 0; i < nb; i++)
            {
                int lsbs = t.LastBits(4);
                t.Dec(lsbs);
                t.Norm();
                w[i] = (sbyte)lsbs;
                t.FShr(4);
            }
            tb[0] = new FP(1);
            tb[1] = new FP(this);
            for (int i = 2; i < 16; i++)
            {
                tb[i] = new FP(tb[i - 1]);
                tb[i].Mul(this);
            }
            FP r = new FP(tb[w[nb - 1]]);

            for (int i = nb - 2; i >= 0; i--)
            {
                r.Sqr();
                r.Sqr();
                r.Sqr();
                r.Sqr();
                r.Mul(tb[w[i]]);
            }
            r.Reduce();
            return(r);
        }
Beispiel #5
0
 /* reduce components mod Modulus */
 public void Reduce()
 {
     a.Reduce();
     b.Reduce();
 }