Пример #1
0
/* Galbraith & Scott Method */
    public static BIG[] gs(BIG e)
    {
        int i, j;
        BIG t = new BIG(0);
        BIG q = new BIG(ROM.CURVE_Order);

        BIG[] u = new BIG[4];
        BIG[] v = new BIG[4];
        for (i = 0; i < 4; i++)
        {
            t.copy(new BIG(ROM.CURVE_WB[i]));
            DBIG d = BIG.mul(t, e);
            v[i] = new BIG(d.div(q));
            u[i] = new BIG(0);
        }
        u[0].copy(e);
        for (i = 0; i < 4; i++)
        {
            for (j = 0; j < 4; j++)
            {
                t.copy(new BIG(ROM.CURVE_BB[j][i]));
                t.copy(BIG.modmul(v[j], t, q));
                u[i].add(q);
                u[i].sub(t);
                u[i].mod(q);
            }
        }
        return(u);
    }
Пример #2
0
/* GLV method */
    public static BIG[] glv(BIG e)
    {
        int i, j;
        BIG t = new BIG(0);
        BIG q = new BIG(ROM.CURVE_Order);

        BIG[] u = new BIG[2];
        BIG[] v = new BIG[2];
        for (i = 0; i < 2; i++)
        {
            t.copy(new BIG(ROM.CURVE_W[i]));             // why not just t=new BIG(ROM.CURVE_W[i]);
            DBIG d = BIG.mul(t, e);
            v[i] = new BIG(d.div(q));
            u[i] = new BIG(0);
        }
        u[0].copy(e);
        for (i = 0; i < 2; i++)
        {
            for (j = 0; j < 2; j++)
            {
                t.copy(new BIG(ROM.CURVE_SB[j][i]));
                t.copy(BIG.modmul(v[j], t, q));
                u[i].add(q);
                u[i].sub(t);
                u[i].mod(q);
            }
        }
        return(u);
    }
Пример #3
0
/* IEEE ECDSA Signature, C and D are signature on F using private key S */
    public static int ECPSP_DSA(RAND RNG, sbyte[] S, sbyte[] F, sbyte[] C, sbyte[] D)
    {
        sbyte[] T = new sbyte[EFS];
        BIG     gx, gy, r, s, f, c, d, u, vx;
        ECP     G, V;

        HASH H = new HASH();

        H.process_array(F);
        sbyte[] B = H.hash();

        gx = new BIG(ROM.CURVE_Gx);
        gy = new BIG(ROM.CURVE_Gy);

        G = new ECP(gx, gy);
        r = new BIG(ROM.CURVE_Order);

        s = BIG.fromBytes(S);
        f = BIG.fromBytes(B);

        c = new BIG(0);
        d = new BIG(0);
        V = new ECP();

        do
        {
            u = BIG.randomnum(r, RNG);

            V.copy(G);
            V  = V.mul(u);
            vx = V.X;
            c.copy(vx);
            c.mod(r);
            if (c.iszilch())
            {
                continue;
            }
            u.invmodp(r);
            d.copy(BIG.modmul(s, c, r));
            d.add(f);
            d.copy(BIG.modmul(u, d, r));
        } while (d.iszilch());

        c.toBytes(T);
        for (int i = 0; i < EFS; i++)
        {
            C[i] = T[i];
        }
        d.toBytes(T);
        for (int i = 0; i < EFS; i++)
        {
            D[i] = T[i];
        }
        return(0);
    }
Пример #4
0
/* 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);
        }
    }
Пример #5
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 < 256; i <<= 1)
        {
            b.copy(this);
            b.mod2m(i);
            BIG t1 = BIG.smul(U, b);
            t1.shr(i);
            c.copy(this);
            c.shr(i);
            c.mod2m(i);

            BIG t2 = BIG.smul(U, c);
            t2.mod2m(i);
            t1.add(t2);
            b = BIG.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);
        }
        this.copy(U);
    }
Пример #6
0
/* Multiply P by e in group G1 */
    public static ECP G1mul(ECP P, BIG e)
    {
        ECP R;

        if (ROM.USE_GLV)
        {
            P.affine();
            R = new ECP();
            R.copy(P);
            int i, np, nn;
            ECP Q = new ECP();
            Q.copy(P);
            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();
            }

            R = R.mul2(u[0], Q, u[1]);
        }
        else
        {
            R = P.mul(e);
        }
        return(R);
    }
Пример #7
0
/* convert to Montgomery n-residue form */
    public void nres()
    {
        if (ROM.MODTYPE != ROM.PSEUDO_MERSENNE)
        {
            DBIG d = new DBIG(x);
            d.shl(ROM.NLEN * ROM.BASEBITS);
            x.copy(d.mod(p));
        }
    }
Пример #8
0
/* Multiply P by e in group G2 */
    public static ECP2 G2mul(ECP2 P, BIG e)
    {
        ECP2 R;

        if (ROM.USE_GS_G2)
        {
            ECP2[] Q = new ECP2[4];
            FP2    f = new FP2(new BIG(ROM.CURVE_Fra), new BIG(ROM.CURVE_Frb));
            BIG    q = new BIG(ROM.CURVE_Order);
            BIG[]  u = gs(e);

            BIG t = new BIG(0);
            int i, np, nn;
            P.affine();
            Q[0] = new ECP2();
            Q[0].copy(P);
            for (i = 1; i < 4; i++)
            {
                Q[i] = new ECP2();
                Q[i].copy(Q[i - 1]);
                Q[i].frob(f);
            }
            for (i = 0; i < 4; i++)
            {
                np = u[i].nbits();
                t.copy(BIG.modneg(u[i], q));
                nn = t.nbits();
                if (nn < np)
                {
                    u[i].copy(t);
                    Q[i].neg();
                }
            }
            R = ECP2.mul4(Q, u);
        }
        else
        {
            R = P.mul(e);
        }
        return(R);
    }
Пример #9
0
/* returns u derived from P. Random value in range 1 to return value should then be added to u */
    public static int unmap(BIG u, ECP P)
    {
        int s = P.S;
        ECP R;
        int r = 0;
        BIG x = P.X;

        u.copy(x);
        while (true)
        {
            u.dec(1);
            u.norm();
            r++;
            R = new ECP(u, s);
            if (!R.is_infinity())
            {
                break;
            }
        }
        return(r);
    }
Пример #10
0
/* f=f^e */
/* Note that this method requires a lot of RAM! Better to use compressed XTR method, see FP4.java */
    public static FP12 GTpow(FP12 d, BIG e)
    {
        FP12 r;

        if (ROM.USE_GS_GT)
        {
            FP12[] g = new FP12[4];
            FP2    f = new FP2(new BIG(ROM.CURVE_Fra), new BIG(ROM.CURVE_Frb));
            BIG    q = new BIG(ROM.CURVE_Order);
            BIG    t = new BIG(0);
            int    i, np, nn;
            BIG[]  u = gs(e);

            g[0] = new FP12(d);
            for (i = 1; i < 4; i++)
            {
                g[i] = new FP12(0);
                g[i].copy(g[i - 1]);
                g[i].frob(f);
            }
            for (i = 0; i < 4; i++)
            {
                np = u[i].nbits();
                t.copy(BIG.modneg(u[i], q));
                nn = t.nbits();
                if (nn < np)
                {
                    u[i].copy(t);
                    g[i].conj();
                }
            }
            r = FP12.pow4(g, u);
        }
        else
        {
            r = d.pow(e);
        }
        return(r);
    }
Пример #11
0
/* return e.this */

    public ECP mul(BIG e)
    {
        if (e.iszilch() || is_infinity())
        {
            return(new ECP());
        }
        ECP P = new ECP();

        if (ROM.CURVETYPE == ROM.MONTGOMERY)
        {
/* use Ladder */
            int nb, i, b;
            ECP D  = new ECP();
            ECP R0 = new ECP();
            R0.copy(this);
            ECP R1 = new ECP();
            R1.copy(this);
            R1.dbl();
            D.copy(this);
            D.affine();
            nb = e.nbits();
            for (i = nb - 2; i >= 0; i--)
            {
                b = e.bit(i);
                P.copy(R1);
                P.dadd(R0, D);
                R0.cswap(R1, b);
                R1.copy(P);
                R0.dbl();
                R0.cswap(R1, b);
            }
            P.copy(R0);
        }
        else
        {
// fixed size windows
            int     i, b, nb, m, s, ns;
            BIG     mt = new BIG();
            BIG     t  = new BIG();
            ECP     Q  = new ECP();
            ECP     C  = new ECP();
            ECP[]   W  = new ECP[8];
            sbyte[] w  = new sbyte[1 + (ROM.NLEN * ROM.BASEBITS + 3) / 4];

            affine();

// precompute table
            Q.copy(this);
            Q.dbl();
            W[0] = new ECP();
            W[0].copy(this);

            for (i = 1; i < 8; i++)
            {
                W[i] = new ECP();
                W[i].copy(W[i - 1]);
                W[i].add(Q);
            }

// convert the table to affine
            if (ROM.CURVETYPE == ROM.WEIERSTRASS)
            {
                multiaffine(8, W);
            }

// make exponent odd - add 2P if even, P if odd
            t.copy(e);
            s = t.parity();
            t.inc(1);
            t.norm();
            ns = t.parity();
            mt.copy(t);
            mt.inc(1);
            mt.norm();
            t.cmove(mt, s);
            Q.cmove(this, ns);
            C.copy(Q);

            nb = 1 + (t.nbits() + 3) / 4;

// convert exponent to signed 4-bit window
            for (i = 0; i < nb; i++)
            {
                w[i] = (sbyte)(t.lastbits(5) - 16);
                t.dec(w[i]);
                t.norm();
                t.fshr(4);
            }
            w[nb] = (sbyte)t.lastbits(5);

            P.copy(W[(w[nb] - 1) / 2]);
            for (i = nb - 1; i >= 0; i--)
            {
                Q.select(W, w[i]);
                P.dbl();
                P.dbl();
                P.dbl();
                P.dbl();
                P.add(Q);
            }
            P.sub(C);             // apply correction
        }
        P.affine();
        return(P);
    }
Пример #12
0
/* Return e.this+f.Q */

    public ECP mul2(BIG e, ECP Q, BIG f)
    {
        BIG te = new BIG();
        BIG tf = new BIG();
        BIG mt = new BIG();
        ECP S  = new ECP();
        ECP T  = new ECP();
        ECP C  = new ECP();

        ECP[]   W = new ECP[8];
        sbyte[] w = new sbyte[1 + (ROM.NLEN * ROM.BASEBITS + 1) / 2];
        int     i, s, ns, nb;
        sbyte   a, b;

        affine();
        Q.affine();

        te.copy(e);
        tf.copy(f);

// precompute table
        W[1] = new ECP();
        W[1].copy(this);
        W[1].sub(Q);
        W[2] = new ECP();
        W[2].copy(this);
        W[2].add(Q);
        S.copy(Q);
        S.dbl();
        W[0] = new ECP();
        W[0].copy(W[1]);
        W[0].sub(S);
        W[3] = new ECP();
        W[3].copy(W[2]);
        W[3].add(S);
        T.copy(this);
        T.dbl();
        W[5] = new ECP();
        W[5].copy(W[1]);
        W[5].add(T);
        W[6] = new ECP();
        W[6].copy(W[2]);
        W[6].add(T);
        W[4] = new ECP();
        W[4].copy(W[5]);
        W[4].sub(S);
        W[7] = new ECP();
        W[7].copy(W[6]);
        W[7].add(S);

// convert the table to affine
        if (ROM.CURVETYPE == ROM.WEIERSTRASS)
        {
            multiaffine(8, W);
        }

// if multiplier is odd, add 2, else add 1 to multiplier, and add 2P or P to correction

        s = te.parity();
        te.inc(1);
        te.norm();
        ns = te.parity();
        mt.copy(te);
        mt.inc(1);
        mt.norm();
        te.cmove(mt, s);
        T.cmove(this, ns);
        C.copy(T);

        s = tf.parity();
        tf.inc(1);
        tf.norm();
        ns = tf.parity();
        mt.copy(tf);
        mt.inc(1);
        mt.norm();
        tf.cmove(mt, s);
        S.cmove(Q, ns);
        C.add(S);

        mt.copy(te);
        mt.add(tf);
        mt.norm();
        nb = 1 + (mt.nbits() + 1) / 2;

// convert exponent to signed 2-bit window
        for (i = 0; i < nb; i++)
        {
            a = (sbyte)(te.lastbits(3) - 4);
            te.dec(a);
            te.norm();
            te.fshr(2);
            b = (sbyte)(tf.lastbits(3) - 4);
            tf.dec(b);
            tf.norm();
            tf.fshr(2);
            w[i] = (sbyte)(4 * a + b);
        }
        w[nb] = (sbyte)(4 * te.lastbits(3) + tf.lastbits(3));
        S.copy(W[(w[nb] - 1) / 2]);

        for (i = nb - 1; i >= 0; i--)
        {
            T.select(W, w[i]);
            S.dbl();
            S.dbl();
            S.add(T);
        }
        S.sub(C);         // apply correction
        S.affine();
        return(S);
    }
Пример #13
0
/* this=1/this mod p. Binary method */
    public virtual void invmodp(BIG p)
    {
        mod(p);
        BIG u = new BIG(this);

        BIG v   = new BIG(p);
        BIG x1  = new BIG(1);
        BIG x2  = new BIG(0);
        BIG t   = new BIG(0);
        BIG one = new BIG(1);

        while (comp(u, one) != 0 && comp(v, one) != 0)
        {
            while (u.parity() == 0)
            {
                u.shr(1);
                if (x1.parity() != 0)
                {
                    x1.add(p);
                    x1.norm();
                }
                x1.shr(1);
            }
            while (v.parity() == 0)
            {
                v.shr(1);
                if (x2.parity() != 0)
                {
                    x2.add(p);
                    x2.norm();
                }
                x2.shr(1);
            }
            if (comp(u, v) >= 0)
            {
                u.sub(v);
                u.norm();
                if (comp(x1, x2) >= 0)
                {
                    x1.sub(x2);
                }
                else
                {
                    t.copy(p);
                    t.sub(x2);
                    x1.add(t);
                }
                x1.norm();
            }
            else
            {
                v.sub(u);
                v.norm();
                if (comp(x2, x1) >= 0)
                {
                    x2.sub(x1);
                }
                else
                {
                    t.copy(p);
                    t.sub(x1);
                    x2.add(t);
                }
                x2.norm();
            }
        }
        if (comp(u, one) == 0)
        {
            copy(x1);
        }
        else
        {
            copy(x2);
        }
    }
Пример #14
0
/* P*=e */
    public ECP2 mul(BIG e)
    {
/* fixed size windows */
        int  i, b, nb, m, s, ns;
        BIG  mt = new BIG();
        BIG  t  = new BIG();
        ECP2 P  = new ECP2();
        ECP2 Q  = new ECP2();
        ECP2 C  = new ECP2();

        ECP2[]  W = new ECP2[8];
        sbyte[] w = new sbyte[1 + (ROM.NLEN * ROM.BASEBITS + 3) / 4];

        if (is_infinity())
        {
            return(new ECP2());
        }

        affine();

/* precompute table */
        Q.copy(this);
        Q.dbl();
        W[0] = new ECP2();
        W[0].copy(this);

        for (i = 1; i < 8; i++)
        {
            W[i] = new ECP2();
            W[i].copy(W[i - 1]);
            W[i].add(Q);
        }

/* convert the table to affine */

        multiaffine(8, W);

/* make exponent odd - add 2P if even, P if odd */
        t.copy(e);
        s = t.parity();
        t.inc(1);
        t.norm();
        ns = t.parity();
        mt.copy(t);
        mt.inc(1);
        mt.norm();
        t.cmove(mt, s);
        Q.cmove(this, ns);
        C.copy(Q);

        nb = 1 + (t.nbits() + 3) / 4;
/* convert exponent to signed 4-bit window */
        for (i = 0; i < nb; i++)
        {
            w[i] = (sbyte)(t.lastbits(5) - 16);
            t.dec(w[i]);
            t.norm();
            t.fshr(4);
        }
        w[nb] = (sbyte)t.lastbits(5);

        P.copy(W[(w[nb] - 1) / 2]);
        for (i = nb - 1; i >= 0; i--)
        {
            Q.select(W, w[i]);
            P.dbl();
            P.dbl();
            P.dbl();
            P.dbl();
            P.add(Q);
        }
        P.sub(C);
        P.affine();
        return(P);
    }
Пример #15
0
/* r=ck^a.cl^n using XTR double exponentiation method on traces of FP12s. See Stam thesis. */
    public FP4 xtr_pow2(FP4 ck, FP4 ckml, FP4 ckm2l, BIG a, BIG b)
    {
        a.norm();
        b.norm();
        BIG e = new BIG(a);
        BIG d = new BIG(b);
        BIG w = new BIG(0);

        FP4 cu    = new FP4(ck);      // can probably be passed in w/o copying
        FP4 cv    = new FP4(this);
        FP4 cumv  = new FP4(ckml);
        FP4 cum2v = new FP4(ckm2l);
        FP4 r     = new FP4(0);
        FP4 t     = new FP4(0);

        int f2 = 0;

        while (d.parity() == 0 && e.parity() == 0)
        {
            d.fshr(1);
            e.fshr(1);
            f2++;
        }

        while (BIG.comp(d, e) != 0)
        {
            if (BIG.comp(d, e) > 0)
            {
                w.copy(e);
                w.imul(4);
                w.norm();
                if (BIG.comp(d, w) <= 0)
                {
                    w.copy(d);
                    d.copy(e);
                    e.rsub(w);
                    e.norm();

                    t.copy(cv);
                    t.xtr_A(cu, cumv, cum2v);
                    cum2v.copy(cumv);
                    cum2v.conj();
                    cumv.copy(cv);
                    cv.copy(cu);
                    cu.copy(t);
                }
                else if (d.parity() == 0)
                {
                    d.fshr(1);
                    r.copy(cum2v);
                    r.conj();
                    t.copy(cumv);
                    t.xtr_A(cu, cv, r);
                    cum2v.copy(cumv);
                    cum2v.xtr_D();
                    cumv.copy(t);
                    cu.xtr_D();
                }
                else if (e.parity() == 1)
                {
                    d.sub(e);
                    d.norm();
                    d.fshr(1);
                    t.copy(cv);
                    t.xtr_A(cu, cumv, cum2v);
                    cu.xtr_D();
                    cum2v.copy(cv);
                    cum2v.xtr_D();
                    cum2v.conj();
                    cv.copy(t);
                }
                else
                {
                    w.copy(d);
                    d.copy(e);
                    d.fshr(1);
                    e.copy(w);
                    t.copy(cumv);
                    t.xtr_D();
                    cumv.copy(cum2v);
                    cumv.conj();
                    cum2v.copy(t);
                    cum2v.conj();
                    t.copy(cv);
                    t.xtr_D();
                    cv.copy(cu);
                    cu.copy(t);
                }
            }
            if (BIG.comp(d, e) < 0)
            {
                w.copy(d);
                w.imul(4);
                w.norm();
                if (BIG.comp(e, w) <= 0)
                {
                    e.sub(d);
                    e.norm();
                    t.copy(cv);
                    t.xtr_A(cu, cumv, cum2v);
                    cum2v.copy(cumv);
                    cumv.copy(cu);
                    cu.copy(t);
                }
                else if (e.parity() == 0)
                {
                    w.copy(d);
                    d.copy(e);
                    d.fshr(1);
                    e.copy(w);
                    t.copy(cumv);
                    t.xtr_D();
                    cumv.copy(cum2v);
                    cumv.conj();
                    cum2v.copy(t);
                    cum2v.conj();
                    t.copy(cv);
                    t.xtr_D();
                    cv.copy(cu);
                    cu.copy(t);
                }
                else if (d.parity() == 1)
                {
                    w.copy(e);
                    e.copy(d);
                    w.sub(d);
                    w.norm();
                    d.copy(w);
                    d.fshr(1);
                    t.copy(cv);
                    t.xtr_A(cu, cumv, cum2v);
                    cumv.conj();
                    cum2v.copy(cu);
                    cum2v.xtr_D();
                    cum2v.conj();
                    cu.copy(cv);
                    cu.xtr_D();
                    cv.copy(t);
                }
                else
                {
                    d.fshr(1);
                    r.copy(cum2v);
                    r.conj();
                    t.copy(cumv);
                    t.xtr_A(cu, cv, r);
                    cum2v.copy(cumv);
                    cum2v.xtr_D();
                    cumv.copy(t);
                    cu.xtr_D();
                }
            }
        }
        r.copy(cv);
        r.xtr_A(cu, cumv, cum2v);
        for (int i = 0; i < f2; i++)
        {
            r.xtr_D();
        }
        r = r.xtr_pow(d);
        return(r);
    }