Beispiel #1
0
        /* Multiply P by e in group G1 */
        public static ECP G1Mul(ECP P, BIG e)
        {
            ECP R;

            if (USE_GLV)
            {
                //P.affine();
                R = new ECP();
                R.Copy(P);
                int np, nn;
                ECP Q = new ECP();
                Q.Copy(P);
                Q.Affine();
                BIG   q   = new BIG(ROM.CURVE_Order);
                FP    cru = new FP(new BIG(ROM.CURVE_Cru));
                BIG   t   = new BIG(0);
                BIG[] u   = Glv(e);
                Q.GetX().Mul(cru);

                np = u[0].NBits();
                t.Copy(BIG.ModNeg(u[0], q));
                nn = t.NBits();
                if (nn < np)
                {
                    u[0].Copy(t);
                    R.Neg();
                }

                np = u[1].NBits();
                t.Copy(BIG.ModNeg(u[1], q));
                nn = t.NBits();
                if (nn < np)
                {
                    u[1].Copy(t);
                    Q.Neg();
                }

                u[0].Norm();
                u[1].Norm();
                R = R.Mul2(u[0], Q, u[1]);
            }
            else
            {
                R = P.Mul(e);
            }

            return(R);
        }
Beispiel #2
0
        /* Optimal R-ate double pairing e(P,Q).e(R,S) */
        public static FP12 Ate2(ECP2 P1, ECP Q1, ECP2 R1, ECP S1)
        {
            FP2  f;
            BIG  x = new BIG(ROM.CURVE_Bnx);
            BIG  n = new BIG(x);
            ECP2 K = new ECP2();
            FP12 lv;
            int  bt;

            ECP2 P = new ECP2(P1);
            ECP  Q = new ECP(Q1);

            P.Affine();
            Q.Affine();

            ECP2 R = new ECP2(R1);
            ECP  S = new ECP(S1);

            R.Affine();
            S.Affine();

            if (ECP.CURVE_PAIRING_TYPE == ECP.BN)
            {
                f = new FP2(new BIG(ROM.Fra), new BIG(ROM.Frb));
                if (ECP.SEXTIC_TWIST == ECP.M_TYPE)
                {
                    f.Inverse();
                    f.Norm();
                }

                n.PMul(6);
                if (ECP.SIGN_OF_X == ECP.POSITIVEX)
                {
                    n.Inc(2);
                }
                else
                {
                    n.Dec(2);
                }
            }
            else
            {
                n.Copy(x);
            }

            n.Norm();

            BIG n3 = new BIG(n);

            n3.PMul(3);
            n3.Norm();

            FP Qx = new FP(Q.GetX());
            FP Qy = new FP(Q.GetY());
            FP Sx = new FP(S.GetX());
            FP Sy = new FP(S.GetY());

            ECP2 A = new ECP2();
            ECP2 B = new ECP2();
            FP12 r = new FP12(1);

            A.Copy(P);
            B.Copy(R);

            ECP2 MP = new ECP2();

            MP.Copy(P);
            MP.Neg();
            ECP2 MR = new ECP2();

            MR.Copy(R);
            MR.Neg();


            int nb = n3.NBits();

            for (int i = nb - 2; i >= 1; i--)
            {
                r.Sqr();
                lv = Line(A, A, Qx, Qy);
                r.SMul(lv, ECP.SEXTIC_TWIST);

                lv = Line(B, B, Sx, Sy);
                r.SMul(lv, ECP.SEXTIC_TWIST);

                bt = n3.Bit(i) - n.Bit(i); // bt=n.bit(i);
                if (bt == 1)
                {
                    lv = Line(A, P, Qx, Qy);
                    r.SMul(lv, ECP.SEXTIC_TWIST);
                    lv = Line(B, R, Sx, Sy);
                    r.SMul(lv, ECP.SEXTIC_TWIST);
                }

                if (bt == -1)
                {
                    //P.neg();
                    lv = Line(A, MP, Qx, Qy);
                    r.SMul(lv, ECP.SEXTIC_TWIST);
                    //P.neg();
                    //R.neg();
                    lv = Line(B, MR, Sx, Sy);
                    r.SMul(lv, ECP.SEXTIC_TWIST);
                    //R.neg();
                }
            }

            if (ECP.SIGN_OF_X == ECP.NEGATIVEX)
            {
                r.Conj();
            }

            /* R-ate fixup required for BN curves */
            if (ECP.CURVE_PAIRING_TYPE == ECP.BN)
            {
                if (ECP.SIGN_OF_X == ECP.NEGATIVEX)
                {
                    //	r.conj();
                    A.Neg();
                    B.Neg();
                }

                K.Copy(P);
                K.Frob(f);

                lv = Line(A, K, Qx, Qy);
                r.SMul(lv, ECP.SEXTIC_TWIST);
                K.Frob(f);
                K.Neg();
                lv = Line(A, K, Qx, Qy);
                r.SMul(lv, ECP.SEXTIC_TWIST);
                K.Copy(R);
                K.Frob(f);
                lv = Line(B, K, Sx, Sy);
                r.SMul(lv, ECP.SEXTIC_TWIST);
                K.Frob(f);
                K.Neg();
                lv = Line(B, K, Sx, Sy);
                r.SMul(lv, ECP.SEXTIC_TWIST);
            }

            return(r);
        }