Esempio n. 1
0
    /* Miller-Rabin test for primality. Slow. */
    public static bool prime(FF p, RAND rng)
    {
        int  i, j, s = 0, n = p.length;
        bool loop;
        FF   d     = new FF(n);
        FF   x     = new FF(n);
        FF   unity = new FF(n);
        FF   nm1   = new FF(n);

        int sf = 4849845;         // 3*5*.. *19

        p.norm();

        if (p.cfactor(sf))
        {
            return(false);
        }
        unity.one();
        nm1.copy(p);
        nm1.sub(unity);
        nm1.norm();
        d.copy(nm1);

        while (d.parity() == 0)
        {
            d.shr();
            s++;
        }
        if (s == 0)
        {
            return(false);
        }
        for (i = 0; i < 10; i++)
        {
            x.randomnum(p, rng);
            x.pow(d, p);
            if (comp(x, unity) == 0 || comp(x, nm1) == 0)
            {
                continue;
            }
            loop = false;
            for (j = 1; j < s; j++)
            {
                x.power(2, p);
                if (comp(x, unity) == 0)
                {
                    return(false);
                }
                if (comp(x, nm1) == 0)
                {
                    loop = true;
                    break;
                }
            }
            if (loop)
            {
                continue;
            }
            return(false);
        }
        return(true);
    }